root/applications/robust/main.cpp @ 1341

Revision 1338, 12.2 kB (checked in by sindj, 13 years ago)

Zacatky experimentovani v robustlib. Zacleneni vypoctu polynomialni rovnice pro optimalni akci s vyuzitim funkce itpp::roots(). Odstranovani zbylych chyb. 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/itsignal.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:\\dataCDClosePercDiff", "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<strings[j].size();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 << "Condition:" << conditions[k-3] << endl;
202
203                                my_rarx->bayes(conditions[k-3]);
204                                //my_rarx->posterior->step_me(1);
205                               
206                                vec cond_vec;
207                                cond_vec.ins(0,conditions[k-3][0]);
208                               
209                                my_arx->bayes(cond_vec,conditions[k-3].right(2));
210                                       
211                               
212                                if(k>8)
213                                {
214                                        //my_rarx->posterior->step_me(0);
215
216                                        mat samples = my_rarx->posterior->sample_mat(50);
217                                       
218                                        vec sample_prediction;
219                                        for(int t = 0;t<50;t++)
220                                        {
221                                                vec lap_sample = conditions[k-3].left(2);
222                                                //lap_sample.ins(lap_sample.size(),1.0);
223                                               
224                                                lap_sample.ins(0,LapRNG());
225
226                                                sample_prediction.ins(0,lap_sample*samples.get_col(t));
227                                        }
228
229                                       
230                                        vec sample_pow = sample_prediction;
231                                        vec poly_coefs;
232                                        bool stop_iteration = false;
233                                        int en = 0;
234                                        do
235                                        {
236                                                double poly_coef = ones(sample_pow.size())*sample_pow/sample_pow.size();
237
238                                                if(abs(poly_coef)>numeric_limits<double>::epsilon())
239                                                {
240                                                        sample_pow = elem_mult(sample_pow,sample_prediction);
241                                                        poly_coefs.ins(poly_coefs.size(),((-1)^en)*poly_coef);
242                                                }
243                                                else
244                                                {
245                                                        stop_iteration = true;
246                                                }
247                                               
248                                                en++;
249                                        }
250                                        while(!stop_iteration);
251
252                                        // cout << "Coefficients: " << poly_coefs << endl;
253                                                                               
254                                        cvec actions = roots(poly_coefs);
255                                        bool is_max = false;
256                                        for(int t = 0;t<actions.size();t++)
257                                        {
258                                                if(actions[t].imag() == 0 && actions[t].real()>-1 && actions[t].real()<1)
259                                                {
260                                                        cout << "Action:" << actions[t].real() << endl;
261                                                        is_max = true;
262                                                }
263                                        }
264
265                                        if(!is_max)
266                                        {
267                                                cout << "No maximum." << endl;
268                                        }
269
270                                        // cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl;
271
272                                        double prediction = 0;
273                                        for(int s = 1;s<samples.rows();s++)
274                                        {
275                                               
276                                                double avg_parameter = samples.get_row(s)*ones(samples.cols())/samples.cols();
277
278                                                prediction += avg_parameter*conditions[k-3][s-1];
279
280                                               
281                                               
282                                                /*
283                                                ofstream myfile;
284                                                char fstring[80];
285                                                strcpy(fstring,file_strings[j]);
286
287                                                char es[5];
288                                                strcat(fstring,itoa(s,es,10));
289
290                                                strcat(fstring,"_res.txt");
291                                               
292
293                                                myfile.open(fstring,ios::app);
294                                               
295                                                //myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
296                                                myfile << avg_parameter;
297                                               
298                                                if(k!=strings[j].size()-1)
299                                                {
300                                                        myfile << ",";
301                                                }
302                                                else
303                                                {
304                                                        myfile << endl;
305                                                }
306                                                myfile.close();
307                                                */
308                                        }
309
310                                        cout << "Prediction: "<< prediction << endl;
311                                       
312                                        enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2));
313                                        double prediction2 = pred_mat->mean()[0];
314                                       
315
316                                        ofstream myfile;
317                                        char fstring[80];
318                                        char f2string[80];
319                                        strcpy(fstring,file_strings[j]);
320                                        strcpy(f2string,fstring);
321
322                                        strcat(fstring,"pred.txt");
323                                        strcat(f2string,"2pred.txt");
324                                       
325
326                                        myfile.open(fstring,ios::app);
327                                       
328                                        // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
329                                        myfile << prediction;
330                                       
331                                        if(k!=strings[j].size()-1)
332                                        {
333                                                myfile << ",";
334                                        }
335                                        else
336                                        {
337                                                myfile << endl;
338                                        }
339                                        myfile.close();
340
341                                       
342                                        myfile.open(f2string,ios::app);
343                                        myfile << prediction2;
344                                       
345                                        if(k!=strings[j].size()-1)
346                                        {
347                                                myfile << ",";
348                                        }
349                                        else
350                                        {
351                                                myfile << endl;
352                                        }
353                                        myfile.close();
354                                       
355
356                                }                                       
357                        }       
358                       
359                        //emliga->step_me(0);
360                        /*
361                        ofstream myfile;
362                        myfile.open("c:\\robust_ar1.txt",ios::app);
363                        myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";";
364                        myfile.close();
365
366                        myfile.open("c:\\robust_ar2.txt",ios::app);
367                        myfile << emliga->minimal_vertex->get_coordinates()[1] << ";";
368                        myfile.close();
369                       
370
371                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
372                        cout << "Step: " << i << endl;*/
373                }
374
375
376        }
377
378
379        // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading
380    //             with maximization of logarithm of one-step ahead wealth.
381
382       
383               
384                /*
385                cout << "One experiment finished." << endl;
386
387                ofstream myfile;
388                myfile.open("c:\\robust_ar1.txt",ios::app);
389                myfile << endl;
390                myfile.close();
391
392                myfile.open("c:\\robust_ar2.txt",ios::app);
393                myfile << endl;
394                myfile.close();*/
395       
396
397        //emlig* emlig1 = new emlig(emlig_size);
398
399        //emlig1->step_me(0);
400        //emlig* emlig2 = new emlig(emlig_size);
401       
402        /*
403        emlig1->set_correction_factors(4);
404
405        for(int j = 0;j<emlig1->correction_factors.size();j++)
406        {
407                for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++)
408                {
409                        cout << j << "    ";
410                       
411                        for(int i=0;i<(*vec_ref).size();i++)
412                        {
413                                cout << (*vec_ref)[i];
414                        }
415
416                        cout << endl;
417                }
418        }*/
419       
420        /*
421    vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5";
422
423        emlig1->add_condition(condition5);
424        //emlig1->step_me(0);
425
426
427        vec condition1a = "-1.0 1.02 0.5";
428        //vec condition1b = "1.0 1.0 1.01";
429        emlig1->add_condition(condition1a);
430        //emlig2->add_condition(condition1b);
431
432        vec condition2a = "-0.3 1.7 1.5";
433        //vec condition2b = "-1.0 1.0 1.0";
434        emlig1->add_condition(condition2a);
435        //emlig2->add_condition(condition2b);
436
437        vec condition3a = "0.5 -1.01 1.0";
438        //vec condition3b = "0.5 -1.01 1.0";
439
440        emlig1->add_condition(condition3a);
441        //emlig2->add_condition(condition3b);   
442
443        vec condition4a = "-0.5 -1.0 1.0";
444        //vec condition4b = "-0.5 -1.0 1.0";   
445
446        emlig1->add_condition(condition4a);
447        //cout << "************************************************" << endl;
448        //emlig2->add_condition(condition4b);
449        //cout << "************************************************" << endl;
450       
451        //cout << emlig1->minimal_vertex->get_coordinates();
452       
453        //emlig1->remove_condition(condition3a);
454        //emlig1->step_me(0);
455        //emlig1->remove_condition(condition2a);
456        //emlig1->remove_condition(condition1a);
457        //emlig1->remove_condition(condition5);
458       
459
460        //emlig1->step_me(0);
461        //emlig2->step_me(0);
462       
463
464        // DA SE POUZIT PRO VYPIS DO SOUBORU
465        // emlig1->step_me(0);
466
467        //emlig1->remove_condition(condition1);
468       
469       
470
471       
472       
473        /*
474        for(int i = 0;i<100;i++)
475        {
476                cout << endl << "Step:" << i << endl;           
477
478                double condition[emlig_size+1];         
479
480                for(int k = 0;k<=emlig_size;k++)
481                {
482                        condition[k] = (rand()-RAND_MAX/2)/1000.0;             
483                }
484                       
485
486                vec* condition_vec = new vec(condition,emlig_size+1);
487                emlig1->add_condition(*condition_vec);
488
489                /*
490                for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly)
491                {
492                        cout << ((toprow*)toprow_ref)->probability << endl;
493                }
494                */
495                /*
496                cout << emlig1->statistic_rowsize(emlig_size) << endl << endl;
497       
498                /*
499                if(i-emlig1->number_of_parameters >= 0)
500                {
501                        pause(30);
502                }
503                */
504
505                // emlig1->step_me(i);
506               
507                /*
508                vector<int> sizevector;
509                for(int s = 0;s<=emlig1->number_of_parameters;s++)
510                {
511                        sizevector.push_back(emlig1->statistic_rowsize(s));
512                }
513                */
514        //}
515   
516
517
518       
519        /*
520        emlig1->step_me(1);
521
522        vec condition = "2.0 0.0 1.0"; 
523
524        emlig1->add_condition(condition);
525
526        vector<int> sizevector;
527        for(int s = 0;s<=emlig1->number_of_parameters;s++)
528        {
529                sizevector.push_back(emlig1->statistic_rowsize(s));
530        }
531
532        emlig1->step_me(2);
533
534        condition = "2.0 1.0 0.0";
535
536        emlig1->add_condition(condition);
537
538        sizevector.clear();
539        for(int s = 0;s<=emlig1->number_of_parameters;s++)
540        {
541                sizevector.push_back(emlig1->statistic_rowsize(s));
542        }
543        */
544
545        return 0;
546}
547
548
Note: See TracBrowser for help on using the browser.