root/applications/robust/main.cpp @ 1337

Revision 1337, 11.6 kB (checked in by sindj, 13 years ago)

Zacatky experimentovani s knihovnou robustlib. JS

Line 
1
2/*!
3\file
4\brief Robust
5\author Vasek Smidl
6
7 */
8
9#include "estim/arx.h"
10#include "robustlib.h"
11#include <vector>
12#include <iostream>
13#include <fstream>
14#include <itpp/signal/poly.h>
15
16using namespace itpp;
17using namespace bdm;
18
19const int emlig_size = 2;
20
21
22int main ( int argc, char* argv[] ) {
23       
24        itpp::Laplace_RNG LapRNG = Laplace_RNG();
25
26        /*
27        // EXPERIMENT: 100 AR model generated time series of length of 30 from y_t=0.95*y_(t-1)+0.05*y_(t-2)+0.2*e_t,
28        // where e_t is normally, student(4) and cauchy distributed are tested using robust AR model, to obtain the
29        // variance of location parameter estimators and compare it to the classical setup.
30        vector<vector<vector<string>>> string_lists;
31        string_lists.push_back(vector<vector<string>>());
32        string_lists.push_back(vector<vector<string>>());
33        string_lists.push_back(vector<vector<string>>());
34
35        char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"};
36       
37
38        for(int i = 0;i<3;i++)
39        {       
40                ifstream myfile(file_strings[i]);
41                if (myfile.is_open())
42                {
43                        while ( myfile.good() )
44                        {
45                                string line;
46                                getline(myfile,line);
47                               
48                                vector<string> parsed_line;
49                                while(line.find(',') != string::npos)
50                                {
51                                        int loc = line.find(',');
52                                        parsed_line.push_back(line.substr(0,loc));
53                                        line.erase(0,loc+1);                                   
54                                }                               
55
56                                string_lists[i].push_back(parsed_line);
57                        }
58                        myfile.close();
59                }
60        }
61
62        for(int j = 0;j<string_lists.size();j++)
63        {
64               
65                for(int i = 0;i<string_lists[j].size()-1;i++)
66                {
67                        vector<vec> conditions;
68                        //emlig* emliga = new emlig(2);
69                        RARX* my_rarx = new RARX(2,30);
70
71                        for(int k = 1;k<string_lists[j][i].size();k++)
72                        {
73                                vec condition;
74                                //condition.ins(0,1);                           
75                                condition.ins(0,string_lists[j][i][k]);                         
76                                conditions.push_back(condition);
77
78                                //cout << "orig:" << condition << endl;
79
80                                if(conditions.size()>1)
81                                {               
82                                        conditions[k-2].ins(0,string_lists[j][i][k]);
83                                       
84                                }
85
86                                if(conditions.size()>2)
87                                {
88                                        conditions[k-3].ins(0,string_lists[j][i][k]);
89
90                                        //cout << "modi:" << conditions[k-3] << endl;
91
92                                        my_rarx->bayes(conditions[k-3]);
93
94                                       
95                                        //if(k>5)
96                                        //{
97                                        //      cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
98                                        //}
99                                       
100                                }                               
101                               
102                        }
103
104                        //emliga->step_me(0);
105                        /*
106                        ofstream myfile;
107                        myfile.open("c:\\robust_ar1.txt",ios::app);
108                        myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";";
109                        myfile.close();
110
111                        myfile.open("c:\\robust_ar2.txt",ios::app);
112                        myfile << emliga->minimal_vertex->get_coordinates()[1] << ";";
113                        myfile.close();
114                       
115
116                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
117                        cout << "Step: " << i << endl;
118                }
119
120                cout << "One experiment finished." << endl;
121
122                ofstream myfile;
123                myfile.open("c:\\robust_ar1.txt",ios::app);
124                myfile << endl;
125                myfile.close();
126
127                myfile.open("c:\\robust_ar2.txt",ios::app);
128                myfile << endl;
129                myfile.close();
130        }*/
131   
132       
133        // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from
134    // y_t=0.95*y_(t-1)+0.05*y_(t-2)+0.2*e_t, where e_t is normally, student(4) and cauchy distributed. It
135    // can be compared to the classical setup.
136       
137
138        vector<vector<string>> strings;
139
140        char* file_strings[3] = {"c:\\dataADClosePercDiff", "c:\\ar_student_single","c:\\ar_cauchy_single"};
141
142        for(int i = 0;i<3;i++)
143        {                       
144                char dfstring[80];
145                strcpy(dfstring,file_strings[i]);
146                strcat(dfstring,".txt");
147               
148                ifstream myfile(dfstring);
149                if (myfile.is_open())
150                {                       
151                        string line;
152                        getline(myfile,line);
153                               
154                        vector<string> parsed_line;
155                        while(line.find(',') != string::npos)
156                        {
157                                int loc = line.find(',');
158                                parsed_line.push_back(line.substr(0,loc));
159                                line.erase(0,loc+1);                                   
160                        }                               
161
162                        strings.push_back(parsed_line);
163                       
164                        myfile.close();
165                }
166        }
167       
168        for(int j = 0;j<strings.size();j++)
169        {               
170                vector<vec> conditions;
171                //emlig* emliga = new emlig(2);
172                RARX* my_rarx = new RARX(2,30,false);
173               
174                /*
175                mat V0 = 0.0001 * eye ( 3 );
176                ARX* my_arx = new ARX(0.97);
177                my_arx->set_statistics ( 1, V0 ); //nu is default (set to have finite moments)
178                my_arx->set_constant ( false );
179                my_arx->validate();
180                */
181
182                for(int k = 1;k<170;k++)
183                {
184                        vec condition;
185                        //condition.ins(0,1);                           
186                        condition.ins(0,strings[j][k]);                         
187                        conditions.push_back(condition);
188
189                        //cout << "orig:" << condition << endl;
190
191                        if(conditions.size()>1)
192                        {               
193                                conditions[k-2].ins(0,strings[j][k]);
194                                       
195                        }
196
197                        if(conditions.size()>2)
198                        {
199                                conditions[k-3].ins(0,strings[j][k]);
200
201                                // cout << "modi:" << conditions[k-3] << endl;
202
203                                my_rarx->bayes(conditions[k-3]);
204                               
205                                vec cond_vec;
206                                cond_vec.ins(0,conditions[k-3][0]);
207                               
208                                // my_arx->bayes(cond_vec,conditions[k-3].right(2));
209                                       
210                               
211                                if(k>5)
212                                {
213                                        //my_rarx->posterior->step_me(0);
214
215                                        mat samples = my_rarx->posterior->sample_mat(50);
216                                       
217                                        vec sample_prediction;
218                                        for(int t = 0;t<50;t++)
219                                        {
220                                                vec lap_sample = conditions[k-3].left(2);
221                                               
222                                                lap_sample.ins(0,LapRNG());
223
224                                                sample_prediction.ins(0,lap_sample*samples.get_col(t));
225                                        }
226
227                                        vec sample_pow = sample_prediction;
228                                        vec poly_coefs;
229                                        bool stop_iteration = false;
230                                        int en = 0;
231                                        do
232                                        {
233                                                double poly_coef = ones(sample_pow.size())*sample_pow/sample_pow.size();
234
235                                                if(abs(poly_coef)>numeric_limits<double>::epsilon())
236                                                {
237                                                        sample_pow = elem_mult(sample_pow,sample_prediction);
238                                                        poly_coefs.ins(poly_coefs.size(),pow(-1,en)*poly_coef);
239                                                }
240                                                else
241                                                {
242                                                        stop_iteration = true;
243                                                }
244                                               
245                                                en++;
246                                        }
247                                        while(!stop_iteration);
248
249                                        cvec action = 
250                                       
251                                       
252                                        cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl;
253
254                                        double prediction = 0;
255                                        for(int s = 1;s<samples.rows();s++)
256                                        {
257                                               
258                                                double avg_parameter = samples.get_row(s)*ones(samples.cols())/samples.cols();
259
260                                                prediction += avg_parameter*conditions[k-3][s-1];
261
262                                               
263                                               
264                                                /*
265                                                ofstream myfile;
266                                                char fstring[80];
267                                                strcpy(fstring,file_strings[j]);
268
269                                                char es[5];
270                                                strcat(fstring,itoa(s,es,10));
271
272                                                strcat(fstring,"_res.txt");
273                                               
274
275                                                myfile.open(fstring,ios::app);
276                                               
277                                                //myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
278                                                myfile << avg_parameter;
279                                               
280                                                if(k!=strings[j].size()-1)
281                                                {
282                                                        myfile << ",";
283                                                }
284                                                else
285                                                {
286                                                        myfile << endl;
287                                                }
288                                                myfile.close();
289                                                */
290                                        }
291
292                                        /*
293                                        enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2));
294                                        double prediction2 = pred_mat->mean()[0];
295                                        */
296
297                                        ofstream myfile;
298                                        char fstring[80];
299                                        //char f2string[80];
300                                        strcpy(fstring,file_strings[j]);
301                                        //strcpy(f2string,fstring);
302
303                                        strcat(fstring,"pred.txt");
304                                        //strcat(f2string,"2pred.txt");
305                                       
306
307                                        myfile.open(fstring,ios::app);
308                                       
309                                        //myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
310                                        myfile << prediction;
311                                       
312                                        if(k!=strings[j].size()-1)
313                                        {
314                                                myfile << ",";
315                                        }
316                                        else
317                                        {
318                                                myfile << endl;
319                                        }
320                                        myfile.close();
321
322                                        /*
323                                        myfile.open(f2string,ios::app);
324                                        myfile << prediction2;
325                                       
326                                        if(k!=strings[j].size()-1)
327                                        {
328                                                myfile << ",";
329                                        }
330                                        else
331                                        {
332                                                myfile << endl;
333                                        }
334                                        myfile.close();
335                                        */
336
337                                }                                       
338                        }       
339                       
340                        //emliga->step_me(0);
341                        /*
342                        ofstream myfile;
343                        myfile.open("c:\\robust_ar1.txt",ios::app);
344                        myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";";
345                        myfile.close();
346
347                        myfile.open("c:\\robust_ar2.txt",ios::app);
348                        myfile << emliga->minimal_vertex->get_coordinates()[1] << ";";
349                        myfile.close();
350                       
351
352                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
353                        cout << "Step: " << i << endl;*/
354                }
355
356
357        }
358
359
360        // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading
361    //             with maximization of logarithm of one-step ahead wealth.
362
363       
364               
365                /*
366                cout << "One experiment finished." << endl;
367
368                ofstream myfile;
369                myfile.open("c:\\robust_ar1.txt",ios::app);
370                myfile << endl;
371                myfile.close();
372
373                myfile.open("c:\\robust_ar2.txt",ios::app);
374                myfile << endl;
375                myfile.close();*/
376       
377
378        //emlig* emlig1 = new emlig(emlig_size);
379
380        //emlig1->step_me(0);
381        //emlig* emlig2 = new emlig(emlig_size);
382       
383        /*
384        emlig1->set_correction_factors(4);
385
386        for(int j = 0;j<emlig1->correction_factors.size();j++)
387        {
388                for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++)
389                {
390                        cout << j << "    ";
391                       
392                        for(int i=0;i<(*vec_ref).size();i++)
393                        {
394                                cout << (*vec_ref)[i];
395                        }
396
397                        cout << endl;
398                }
399        }*/
400       
401        /*
402    vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5";
403
404        emlig1->add_condition(condition5);
405        //emlig1->step_me(0);
406
407
408        vec condition1a = "-1.0 1.02 0.5";
409        //vec condition1b = "1.0 1.0 1.01";
410        emlig1->add_condition(condition1a);
411        //emlig2->add_condition(condition1b);
412
413        vec condition2a = "-0.3 1.7 1.5";
414        //vec condition2b = "-1.0 1.0 1.0";
415        emlig1->add_condition(condition2a);
416        //emlig2->add_condition(condition2b);
417
418        vec condition3a = "0.5 -1.01 1.0";
419        //vec condition3b = "0.5 -1.01 1.0";
420
421        emlig1->add_condition(condition3a);
422        //emlig2->add_condition(condition3b);   
423
424        vec condition4a = "-0.5 -1.0 1.0";
425        //vec condition4b = "-0.5 -1.0 1.0";   
426
427        emlig1->add_condition(condition4a);
428        //cout << "************************************************" << endl;
429        //emlig2->add_condition(condition4b);
430        //cout << "************************************************" << endl;
431       
432        //cout << emlig1->minimal_vertex->get_coordinates();
433       
434        //emlig1->remove_condition(condition3a);
435        //emlig1->step_me(0);
436        //emlig1->remove_condition(condition2a);
437        //emlig1->remove_condition(condition1a);
438        //emlig1->remove_condition(condition5);
439       
440
441        //emlig1->step_me(0);
442        //emlig2->step_me(0);
443       
444
445        // DA SE POUZIT PRO VYPIS DO SOUBORU
446        // emlig1->step_me(0);
447
448        //emlig1->remove_condition(condition1);
449       
450       
451
452       
453       
454        /*
455        for(int i = 0;i<100;i++)
456        {
457                cout << endl << "Step:" << i << endl;           
458
459                double condition[emlig_size+1];         
460
461                for(int k = 0;k<=emlig_size;k++)
462                {
463                        condition[k] = (rand()-RAND_MAX/2)/1000.0;             
464                }
465                       
466
467                vec* condition_vec = new vec(condition,emlig_size+1);
468                emlig1->add_condition(*condition_vec);
469
470                /*
471                for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly)
472                {
473                        cout << ((toprow*)toprow_ref)->probability << endl;
474                }
475                */
476                /*
477                cout << emlig1->statistic_rowsize(emlig_size) << endl << endl;
478       
479                /*
480                if(i-emlig1->number_of_parameters >= 0)
481                {
482                        pause(30);
483                }
484                */
485
486                // emlig1->step_me(i);
487               
488                /*
489                vector<int> sizevector;
490                for(int s = 0;s<=emlig1->number_of_parameters;s++)
491                {
492                        sizevector.push_back(emlig1->statistic_rowsize(s));
493                }
494                */
495        //}
496   
497
498
499       
500        /*
501        emlig1->step_me(1);
502
503        vec condition = "2.0 0.0 1.0"; 
504
505        emlig1->add_condition(condition);
506
507        vector<int> sizevector;
508        for(int s = 0;s<=emlig1->number_of_parameters;s++)
509        {
510                sizevector.push_back(emlig1->statistic_rowsize(s));
511        }
512
513        emlig1->step_me(2);
514
515        condition = "2.0 1.0 0.0";
516
517        emlig1->add_condition(condition);
518
519        sizevector.clear();
520        for(int s = 0;s<=emlig1->number_of_parameters;s++)
521        {
522                sizevector.push_back(emlig1->statistic_rowsize(s));
523        }
524        */
525
526        return 0;
527}
528
529
Note: See TracBrowser for help on using the browser.