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

RevLine 
[976]1
2/*!
3\file
4\brief Robust
5\author Vasek Smidl
6
7 */
8
[1337]9#include "estim/arx.h"
[976]10#include "robustlib.h"
[1216]11#include <vector>
[1284]12#include <iostream>
[1282]13#include <fstream>
[1338]14#include <itpp/itsignal.h>
[1282]15
[1208]16using namespace itpp;
[1337]17using namespace bdm;
[976]18
[1275]19const int emlig_size = 2;
[1357]20const int utility_constant = 5;
[1268]21
[1272]22
[976]23int main ( int argc, char* argv[] ) {
24       
[1337]25        itpp::Laplace_RNG LapRNG = Laplace_RNG();
26
[1300]27        /*
[1301]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.
[1282]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>>());
[1186]35
[1282]36        char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"};
[1268]37       
[1282]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        {
[1301]65               
[1284]66                for(int i = 0;i<string_lists[j].size()-1;i++)
[1282]67                {
68                        vector<vec> conditions;
[1301]69                        //emlig* emliga = new emlig(2);
70                        RARX* my_rarx = new RARX(2,30);
71
[1282]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
[1301]93                                        my_rarx->bayes(conditions[k-3]);
[1282]94
[1299]95                                       
96                                        //if(k>5)
97                                        //{
98                                        //      cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
99                                        //}
100                                       
101                                }                               
102                               
[1282]103                        }
104
105                        //emliga->step_me(0);
[1301]106                        /*
[1284]107                        ofstream myfile;
108                        myfile.open("c:\\robust_ar1.txt",ios::app);
[1301]109                        myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";";
[1284]110                        myfile.close();
111
112                        myfile.open("c:\\robust_ar2.txt",ios::app);
113                        myfile << emliga->minimal_vertex->get_coordinates()[1] << ";";
114                        myfile.close();
[1301]115                       
[1284]116
[1282]117                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
118                        cout << "Step: " << i << endl;
119                }
120
121                cout << "One experiment finished." << endl;
[1284]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();
[1301]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
[1357]141        char* file_strings[3] = {"c:\\dataADClosePercDiff","c:\\ar_student_single","c:\\ar_cauchy_single"};
[1301]142
143        for(int i = 0;i<3;i++)
144        {                       
[1337]145                char dfstring[80];
146                strcpy(dfstring,file_strings[i]);
147                strcat(dfstring,".txt");
148               
149                ifstream myfile(dfstring);
[1301]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                }
[1282]167        }
[1357]168
[1282]169       
[1357]170       
[1301]171        for(int j = 0;j<strings.size();j++)
172        {               
173                vector<vec> conditions;
174                //emlig* emliga = new emlig(2);
[1357]175                RARX* my_rarx = new RARX(2,8,false);
[1337]176               
[1338]177               
[1337]178                mat V0 = 0.0001 * eye ( 3 );
[1349]179                ARX* my_arx = new ARX(0.85);
[1337]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();
[1338]183               
[1301]184
[1338]185                for(int k = 1;k<strings[j].size();k++)
[1301]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
[1349]204                                // cout << "Condition:" << conditions[k-3] << endl;
[1301]205
206                                my_rarx->bayes(conditions[k-3]);
[1338]207                                //my_rarx->posterior->step_me(1);
[1337]208                               
209                                vec cond_vec;
210                                cond_vec.ins(0,conditions[k-3][0]);
211                               
[1338]212                                my_arx->bayes(cond_vec,conditions[k-3].right(2));
[1301]213                                       
214                               
[1346]215                                if(k>8)
[1301]216                                {
[1324]217                                        //my_rarx->posterior->step_me(0);
218
[1346]219                                        //mat samples = my_rarx->posterior->sample_mat(10);
[1343]220
[1346]221                                        pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(1000);
[1343]222
[1346]223                                        //cout << imp_samples.first << endl;
[1336]224                                       
[1337]225                                        vec sample_prediction;
[1346]226                                        for(int t = 0;t<imp_samples.first.size();t++)
[1337]227                                        {
228                                                vec lap_sample = conditions[k-3].left(2);
[1346]229                                                //lap_sample.ins(lap_sample.size(),1.0);
[1337]230                                               
231                                                lap_sample.ins(0,LapRNG());
232
[1346]233                                                sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t));
[1337]234                                        }
235
[1338]236                                       
[1337]237                                        vec sample_pow = sample_prediction;
[1343]238                                       
239                                        // cout << sample_prediction << endl;
[1337]240                                        vec poly_coefs;
[1346]241                                        double prediction;
[1337]242                                        bool stop_iteration = false;
[1343]243                                        int en = 1;
[1337]244                                        do
245                                        {
[1346]246                                                double poly_coef = imp_samples.first*sample_pow/(imp_samples.first*ones(imp_samples.first.size()));
[1337]247
[1346]248                                                if(en==1)
249                                                {
250                                                        prediction = poly_coef;
251                                                }
252
[1343]253                                                poly_coef = poly_coef*en*fact(utility_constant-2+en)/fact(utility_constant-2);
254
[1337]255                                                if(abs(poly_coef)>numeric_limits<double>::epsilon())
256                                                {
257                                                        sample_pow = elem_mult(sample_pow,sample_prediction);
[1343]258                                                        poly_coefs.ins(0,pow(-1.0,en+1)*poly_coef);
[1337]259                                                }
260                                                else
261                                                {
262                                                        stop_iteration = true;
263                                                }
264                                               
265                                                en++;
[1343]266
267                                                if(en>20)
268                                                {
269                                                        stop_iteration = true;
270                                                }
[1337]271                                        }
272                                        while(!stop_iteration);
273
[1343]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
[1349]288                                        //cout << "Coefficients: " << poly_coefs << endl;
[1338]289                                                                               
[1343]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                                       
[1346]297
298
[1338]299                                        cvec actions = roots(poly_coefs);
[1343]300                                       
301
[1338]302                                        bool is_max = false;
303                                        for(int t = 0;t<actions.size();t++)
304                                        {
[1343]305                                                if(actions[t].imag() == 0)
[1338]306                                                {
[1343]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                                                        }
[1338]319                                                }
320                                        }
[1301]321
[1338]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
[1346]329                                        /*
[1337]330                                        double prediction = 0;
331                                        for(int s = 1;s<samples.rows();s++)
[1336]332                                        {
333                                               
[1346]334                                                double avg_parameter = imp_samples.get_row(s)*ones(samples.cols())/samples.cols();
[1337]335
336                                                prediction += avg_parameter*conditions[k-3][s-1];
337
[1336]338                                               
[1337]339                                               
340                                                /*
[1336]341                                                ofstream myfile;
342                                                char fstring[80];
343                                                strcpy(fstring,file_strings[j]);
[1301]344
[1336]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();
[1337]365                                                */
366
[1338]367                                       
[1346]368                                        //}
369
370                                        // cout << "Prediction: "<< prediction << endl;
371                                       
[1337]372                                        enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2));
373                                        double prediction2 = pred_mat->mean()[0];
[1338]374                                       
[1337]375
376                                        ofstream myfile;
377                                        char fstring[80];
[1338]378                                        char f2string[80];
[1337]379                                        strcpy(fstring,file_strings[j]);
[1338]380                                        strcpy(f2string,fstring);
[1337]381
382                                        strcat(fstring,"pred.txt");
[1338]383                                        strcat(f2string,"2pred.txt");
[1337]384                                       
385
386                                        myfile.open(fstring,ios::app);
387                                       
[1338]388                                        // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
[1337]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
[1338]401                                       
[1337]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();
[1346]414                                        //*/
[1337]415
[1319]416                                }                                       
[1301]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                }
[1337]434
435
[1301]436        }
[1337]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       
[1301]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();*/
[1300]455       
[1301]456
457        //emlig* emlig1 = new emlig(emlig_size);
458
459        //emlig1->step_me(0);
460        //emlig* emlig2 = new emlig(emlig_size);
[1300]461       
[1267]462        /*
463        emlig1->set_correction_factors(4);
[1266]464
[1267]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                {
[1268]469                        cout << j << "    ";
470                       
[1267]471                        for(int i=0;i<(*vec_ref).size();i++)
472                        {
473                                cout << (*vec_ref)[i];
474                        }
475
476                        cout << endl;
477                }
[1268]478        }*/
479       
[1301]480        /*
[1300]481    vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5";
482
[1299]483        emlig1->add_condition(condition5);
[1301]484        //emlig1->step_me(0);
485
486
487        vec condition1a = "-1.0 1.02 0.5";
[1300]488        //vec condition1b = "1.0 1.0 1.01";
[1301]489        emlig1->add_condition(condition1a);
[1300]490        //emlig2->add_condition(condition1b);
[1267]491
[1301]492        vec condition2a = "-0.3 1.7 1.5";
[1300]493        //vec condition2b = "-1.0 1.0 1.0";
[1301]494        emlig1->add_condition(condition2a);
[1300]495        //emlig2->add_condition(condition2b);
[1234]496
[1301]497        vec condition3a = "0.5 -1.01 1.0";
[1300]498        //vec condition3b = "0.5 -1.01 1.0";
[1280]499
[1301]500        emlig1->add_condition(condition3a);
[1300]501        //emlig2->add_condition(condition3b);   
[1280]502
[1301]503        vec condition4a = "-0.5 -1.0 1.0";
[1300]504        //vec condition4b = "-0.5 -1.0 1.0";   
505
[1301]506        emlig1->add_condition(condition4a);
[1300]507        //cout << "************************************************" << endl;
508        //emlig2->add_condition(condition4b);
509        //cout << "************************************************" << endl;
510       
[1299]511        //cout << emlig1->minimal_vertex->get_coordinates();
[1300]512       
[1301]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       
[1275]519
[1299]520        //emlig1->step_me(0);
521        //emlig2->step_me(0);
522       
[1282]523
524        // DA SE POUZIT PRO VYPIS DO SOUBORU
[1275]525        // emlig1->step_me(0);
526
527        //emlig1->remove_condition(condition1);
528       
[1301]529       
[1275]530
531       
[1301]532       
[1275]533        /*
[1282]534        for(int i = 0;i<100;i++)
[1219]535        {
[1282]536                cout << endl << "Step:" << i << endl;           
[1208]537
[1268]538                double condition[emlig_size+1];         
[1220]539
[1268]540                for(int k = 0;k<=emlig_size;k++)
541                {
[1272]542                        condition[k] = (rand()-RAND_MAX/2)/1000.0;             
[1268]543                }
544                       
[1216]545
[1268]546                vec* condition_vec = new vec(condition,emlig_size+1);
[1219]547                emlig1->add_condition(*condition_vec);
[1271]548
[1272]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                */
[1275]555                /*
[1271]556                cout << emlig1->statistic_rowsize(emlig_size) << endl << endl;
[1268]557       
[1272]558                /*
[1271]559                if(i-emlig1->number_of_parameters >= 0)
560                {
561                        pause(30);
562                }
[1272]563                */
[1219]564
[1271]565                // emlig1->step_me(i);
[1219]566               
[1272]567                /*
[1219]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                }
[1272]573                */
[1275]574        //}
575   
[1219]576
577
578       
579        /*
580        emlig1->step_me(1);
581
582        vec condition = "2.0 0.0 1.0"; 
583
[1208]584        emlig1->add_condition(condition);
585
[1216]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
[1219]592        emlig1->step_me(2);
[1216]593
[1219]594        condition = "2.0 1.0 0.0";
[1216]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        }
[1219]603        */
[1216]604
[976]605        return 0;
606}
607
[1282]608
Note: See TracBrowser for help on using the browser.