root/applications/robust/main.cpp @ 1357

Revision 1357, 13.4 kB (checked in by sindj, 13 years ago)

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