root/applications/robust/main.cpp @ 1379

Revision 1379, 24.9 kB (checked in by sindj, 13 years ago)

Prepracovani integrace v robustlib.cpp, zjednoduseni, zesymetrizovani. Nevim jestli funguje, nutno overit a vyzkouset testovani hypotez a integraci na jednicku (u normalizacnich faktoru).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#include "windows.h"
16#include "ddeml.h"
17#include "stdio.h"
18
19//#include "DDEClient.h"
20//#include <conio.h>
21
22
23using namespace itpp;
24using namespace bdm;
25
26//const int emlig_size = 2;
27//const int utility_constant = 5;
28
29const int max_model_order = 2;
30const double apriorno=0.005;
31
32/*
33HDDEDATA CALLBACK DdeCallback(
34        UINT uType,     // Transaction type.
35        UINT uFmt,      // Clipboard data format.
36        HCONV hconv,    // Handle to the conversation.
37        HSZ hsz1,       // Handle to a string.
38        HSZ hsz2,       // Handle to a string.
39        HDDEDATA hdata, // Handle to a global memory object.
40        DWORD dwData1,  // Transaction-specific data.
41        DWORD dwData2)  // Transaction-specific data.
42{
43        return 0;
44}
45
46void DDERequest(DWORD idInst, HCONV hConv, char* szItem)
47{
48        HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0);
49        HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT,
50                                                                        XTYP_ADVSTART,TIMEOUT_ASYNC , NULL); //TIMEOUT_ASYNC
51        if (hData==NULL)
52        {
53                printf("Request failed: %s\n", szItem);
54        }
55
56        if (hData==0)
57        {
58                printf("Request failed: %s\n", szItem);
59        }
60}
61
62DWORD WINAPI ThrdFunc( LPVOID n )
63{   
64    return 0;
65}
66*/
67
68class model
69{
70public:
71        set<pair<int,int>> ar_components;
72
73        // Best thing would be to inherit the two models from a single souce, this is planned, but now structurally
74        // problematic.
75        RARX* my_rarx; //vzmenovane parametre pre triedu model
76        ARXwin* my_arx;
77
78        bool has_constant;
79        int  window_size;  //musi byt vacsia ako pocet krokov ak to nema ovplyvnit
80        int  predicted_channel;
81        mat* data_matrix;
82       
83        model(set<pair<int,int>> ar_components, //funkcie treidz model-konstruktor
84                  bool robust, 
85                  bool has_constant, 
86                  int window_size, 
87                  int predicted_channel,
88                  mat* data_matrix)
89        {
90                this->ar_components.insert(ar_components.begin(),ar_components.end());
91                this->has_constant      = has_constant;
92                this->window_size       = window_size;
93                this->predicted_channel = predicted_channel;
94                this->data_matrix       = data_matrix;
95
96                if(robust)
97                {
98                        if(has_constant)
99                        {
100                                my_rarx = new RARX(ar_components.size()+1,window_size,true);
101                                my_arx  = NULL;
102                        }
103                else
104                        {
105                                my_rarx = new RARX(ar_components.size(),window_size,false);
106                                my_arx  = NULL;
107                        }
108                }
109                else
110                {
111                        my_rarx = NULL;
112                        my_arx  = new ARXwin();
113                        mat V0;
114
115                        if(has_constant)
116                        {                               
117                                V0  = apriorno * eye(ar_components.size()+2); //aj tu konst
118                                V0(0,0) = 1;
119                                my_arx->set_constant(true);                             
120                        }
121                        else
122                        {
123                               
124                                V0  = apriorno * eye(ar_components.size()+1);//menit konstantu
125                                V0(0,0) = 1;
126                                my_arx->set_constant(false);                           
127                               
128                        }
129
130                        my_arx->set_statistics(1, V0, V0.rows()+1);                     
131                        my_arx->set_parameters(window_size);
132                        my_arx->validate();
133                }
134        }
135
136        void data_update(int time) //vlozime cas a ono vlozi do data_vector podmineky(conditions) a predikce, ktore pouzije do bayes
137        {
138                vec data_vector;
139                for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++)
140                {  //ar?iterator ide len od 1 pod 2, alebo niekedy len 1
141                        data_vector.ins(data_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second));
142// do data vector vlozi pre dany typ regresoru prislusne cisla z data_matrix. Ale ako? preco time-ar_iterator->second?
143                }
144                if(my_rarx!=NULL)
145                {       //pre robusr priradi az tu do data_vector aj rpedikciu
146                        data_vector.ins(0,(*data_matrix).get(predicted_channel,time));
147                        my_rarx->bayes(data_vector);
148                }
149                else
150                {
151                        vec pred_vec;//tu sa predikcia zadava zvlast
152                        pred_vec.ins(0,(*data_matrix).get(predicted_channel,time));
153                        my_arx->bayes(pred_vec,data_vector);
154                }
155        }
156
157        pair<vec,vec> predict(int sample_size, int time, itpp::Laplace_RNG* LapRNG)  //nerozumiem, ale vraj to netreba, nepouziva to
158        {
159                vec condition_vector;
160                for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++)
161                {
162                        condition_vector.ins(condition_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second+1));
163                }
164
165                if(my_rarx!=NULL)
166                {
167                        pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(sample_size);
168
169                        //cout << imp_samples.first << endl;
170                       
171                        vec sample_prediction;                 
172                        for(int t = 0;t<sample_size;t++)
173                        {
174                                vec lap_sample = condition_vector;
175                               
176                                if(has_constant)
177                                {
178                                        lap_sample.ins(lap_sample.size(),1.0);
179                                }
180                               
181                                lap_sample.ins(0,(*LapRNG)());
182
183                                sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t));                             
184                        }
185
186                        return pair<vec,vec>(imp_samples.first,sample_prediction);
187                }
188                else
189                {
190                        mat samples = my_arx->posterior().sample_mat(sample_size);
191                       
192                        vec sample_prediction;
193                        for(int t = 0;t<sample_size;t++)
194                        {
195                                vec gau_sample = condition_vector;
196                               
197                                if(has_constant)
198                                {
199                                        gau_sample.ins(gau_sample.size(),1.0);
200                                }
201                               
202                                gau_sample.ins(0,randn());
203
204                                sample_prediction.ins(0,gau_sample*samples.get_col(t));                         
205                        }
206
207                        return pair<vec,vec>(ones(sample_prediction.size()),sample_prediction);
208                }
209       
210        }
211
212
213        static set<set<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels)
214        {
215                set<set<pair<int,int>>> created_model_types;           
216
217                if(max_order == 1)//ukoncovacia vetva
218                {                       
219                        for(int channel = 0;channel<number_of_channels;channel++)//pre AR 1 model vytvori kombinace kanalov v prvom kroku poyadu
220                        {
221                                set<pair<int,int>> returned_type;
222                                returned_type.insert(pair<int,int>(channel,1));  //??
223                                created_model_types.insert(returned_type);
224                        }
225
226                        return created_model_types;
227                }
228                else
229                {
230                        created_model_types = possible_models_recurse(max_order-1,number_of_channels);//tu uz mame ulozene kombinace o jeden krok dozadu  //rekuryivne volanie
231                        set<set<pair<int,int>>> returned_types;
232                       
233                        for(set<set<pair<int,int>>>::iterator model_ref = created_model_types.begin();model_ref!=created_model_types.end();model_ref++)
234                        {                               
235                               
236                                for(int order = 1; order<=max_order; order++)
237                                {
238                                        for(int channel = 0;channel<number_of_channels;channel++)
239                                        {
240                                                set<pair<int,int>> returned_type;
241                                                pair<int,int> new_pair = pair<int,int>(channel,order);//??
242                                                if(find((*model_ref).begin(),(*model_ref).end(),new_pair)==(*model_ref).end()) //??
243                                                {
244                                                        returned_type.insert((*model_ref).begin(),(*model_ref).end()); //co vlozi na zaciatok retuned_type?
245                                                        returned_type.insert(new_pair);
246                                                       
247
248                                                        returned_types.insert(returned_type);                                                   
249                                                }
250                                        }
251                                }
252                        }
253
254                        created_model_types.insert(returned_types.begin(),returned_types.end());
255
256                        return created_model_types;
257                }               
258        }
259};
260
261
262
263
264int main ( int argc, char* argv[] ) {   
265       
266        /*
267        DWORD Id;
268        HANDLE hThrd = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ThrdFunc, (LPVOID)1, 0, &Id);
269 
270        if ( !hThrd )
271    {
272        cout<<"Error Creating Threads,,,,.exiting"<<endl;
273        return -1;
274    }
275    Sleep ( 100 );
276
277       
278        char szApp[] = "MT4";
279        char szTopic[] = "QUOTE";       
280        char szItem1[] = "EURUSD";     
281
282        //DDE Initialization
283        DWORD idInst=0;
284        UINT iReturn;
285        iReturn = DdeInitialize(&idInst, (PFNCALLBACK)DdeCallback,
286                                                        APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 );
287        if (iReturn!=DMLERR_NO_ERROR)
288        {
289                printf("DDE Initialization Failed: 0x%04x\n", iReturn);
290                Sleep(1500);
291                return 0;
292        }
293
294        //DDE Connect to Server using given AppName and topic.
295        HSZ hszApp, hszTopic;
296        HCONV hConv;
297        hszApp = DdeCreateStringHandle(idInst, szApp, 0);
298        hszTopic = DdeCreateStringHandle(idInst, szTopic, 0);
299        hConv = DdeConnect(idInst, hszApp, hszTopic, NULL);
300        //DdeFreeStringHandle(idInst, hszApp);
301        //DdeFreeStringHandle(idInst, hszTopic);
302        if (hConv == NULL)
303        {
304                printf("DDE Connection Failed.\n");
305                Sleep(1500); DdeUninitialize(idInst);
306                return 0;
307        }
308
309        //Execute commands/requests specific to the DDE Server.
310       
311        DDERequest(idInst, hConv, szItem1);             
312       
313        while(1)
314        {
315                MSG    msg;
316                BOOL   MsgReturn = GetMessage ( &msg , NULL , 0 , 0 );
317           
318                if(MsgReturn)
319                {
320                        TranslateMessage(&msg);
321                        DispatchMessage(&msg);                 
322                }
323        }
324
325        //DDE Disconnect and Uninitialize.
326        DdeDisconnect(hConv);
327        DdeUninitialize(idInst);
328        */
329
330       
331
332        /*
333        // 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,
334        // where e_t is normally, student(4) and cauchy distributed are tested using robust AR model, to obtain the
335        // variance of location parameter estimators and compare it to the classical setup.
336        vector<vector<vector<string>>> string_lists;
337        string_lists.push_back(vector<vector<string>>());
338        string_lists.push_back(vector<vector<string>>());
339        string_lists.push_back(vector<vector<string>>());
340
341        char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"};
342       
343
344        for(int i = 0;i<3;i++)
345        {       
346                ifstream myfile(file_strings[i]);
347                if (myfile.is_open())
348                {
349                        while ( myfile.good() )
350                        {
351                                string line;
352                                getline(myfile,line);
353                               
354                                vector<string> parsed_line;
355                                while(line.find(',') != string::npos)
356                                {
357                                        int loc = line.find(',');
358                                        parsed_line.push_back(line.substr(0,loc));
359                                        line.erase(0,loc+1);                                   
360                                }                               
361
362                                string_lists[i].push_back(parsed_line);
363                        }
364                        myfile.close();
365                }
366        }
367
368        for(int j = 0;j<string_lists.size();j++)
369        {
370               
371                for(int i = 0;i<string_lists[j].size()-1;i++)
372                {
373                        vector<vec> conditions;
374                        //emlig* emliga = new emlig(2);
375                        RARX* my_rarx = new RARX(2,30);
376
377                        for(int k = 1;k<string_lists[j][i].size();k++)
378                        {
379                                vec condition;
380                                //condition.ins(0,1);                           
381                                condition.ins(0,string_lists[j][i][k]);                         
382                                conditions.push_back(condition);
383
384                                //cout << "orig:" << condition << endl;
385
386                                if(conditions.size()>1)
387                                {               
388                                        conditions[k-2].ins(0,string_lists[j][i][k]);
389                                       
390                                }
391
392                                if(conditions.size()>2)
393                                {
394                                        conditions[k-3].ins(0,string_lists[j][i][k]);
395
396                                        //cout << "modi:" << conditions[k-3] << endl;
397
398                                        my_rarx->bayes(conditions[k-3]);
399
400                                       
401                                        //if(k>5)
402                                        //{
403                                        //      cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
404                                        //}
405                                       
406                                }                               
407                               
408                        }
409
410                        //emliga->step_me(0);
411                        /*
412                        ofstream myfile;
413                        myfile.open("c:\\robust_ar1.txt",ios::app);
414                        myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";";
415                        myfile.close();
416
417                        myfile.open("c:\\robust_ar2.txt",ios::app);
418                        myfile << emliga->minimal_vertex->get_coordinates()[1] << ";";
419                        myfile.close();
420                       
421
422                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
423                        cout << "Step: " << i << endl;
424                }
425
426                cout << "One experiment finished." << endl;
427
428                ofstream myfile;
429                myfile.open("c:\\robust_ar1.txt",ios::app);
430                myfile << endl;
431                myfile.close();
432
433                myfile.open("c:\\robust_ar2.txt",ios::app);
434                myfile << endl;
435                myfile.close();
436        }*/   
437       
438        // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from
439    // 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
440    // can be compared to the classical setup.
441       
442        itpp::Laplace_RNG LapRNG = Laplace_RNG();       
443
444        vector<vector<string>> strings;
445
446        char* file_string = "c:\\ar_normal_single"; // "c:\\dataTYClosePercDiff"; //
447
448        char dfstring[80];
449        strcpy(dfstring,file_string);
450        strcat(dfstring,".txt");
451       
452       
453        mat data_matrix;
454        ifstream myfile(dfstring);
455        if (myfile.is_open())
456        {               
457                string line;
458                while(getline(myfile,line))
459                {                       
460                        vec data_vector;
461                        while(line.find(',') != string::npos) //zmenil som ciarku za medzeru
462                        {
463                                line.erase(0,1); // toto som sem pridal
464                                int loc2 = line.find('\n');
465                                int loc  = line.find(',');
466                                data_vector.ins(data_vector.size(),atof(line.substr(0,loc).c_str()));                           
467                                line.erase(0,loc+1);                                   
468                        }
469
470                        data_matrix.ins_row(data_matrix.rows(),data_vector);
471                }               
472
473                myfile.close(); 
474        }
475        else
476        {
477                cout << "Can't open data file!" << endl;
478        }
479       
480        //konec nacitavania dat
481        set<set<pair<int,int>>> model_types = model::possible_models_recurse(max_model_order,data_matrix.rows()); //volanie funkce kde robi kombinace modelov
482                                                                                                //to priradime do model_types, data_matrix.row urcuje pocet kanalov dat
483        vector<model*> models;
484        for(set<set<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++)
485        {// prechadza rozne typy kanalov, a poctu regresorov
486                for(int window_size = 15;window_size < 16;window_size++)
487                {
488                        models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix));   // to su len konstruktory, len inicializujeme rozne typy
489                        models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix));
490                        models.push_back(new model((*model_type),true,false,window_size,0,&data_matrix));
491                        models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix));             
492                }
493
494                //set<pair<int,int>> empty_list;
495                //models.push_back(new model(empty_list,false,true,100,0,&data_matrix));
496        }
497       
498        mat result_lognc;
499        // mat result_preds;
500
501        for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols()
502        {       //pocet stlpcov data_matrix je pocet casovych krokov
503                vec cur_res_lognc;
504                // vec preds;
505                vector<string> nazvy;
506                for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++)
507                {//posuvam s apo models, co je pole modelov urobene o cyklus vyssie. Teda som v case time a robim to tam pre vsetky typy modelov, kombinace regresorov
508                        (*model_ref)->data_update(time); //pozret sa preco je toto tu nutne
509
510                        cout << "Updated." << endl;
511                        //if (time = max_model_order) nazvy.push_back(models.model_ref]);// ako by som mohol dostat nazov modelu?
512
513                        if((*model_ref)->my_rarx!=NULL) //vklada normalizacnz faktor do cur_res_lognc
514                        {
515                                cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_rarx->posterior->log_nc);
516                        }
517                        else
518                        {
519                                cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_arx->posterior().lognc());
520                        }
521
522                        // pair<vec,vec> predictions = (*model_ref)->predict(200,time,&LapRNG);
523
524                        // preds.ins(preds.size(),(predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size())));
525                        // preds.ins(0,data_matrix.get(0,time+1));
526
527                }
528
529                result_lognc.ins_col(result_lognc.cols(),cur_res_lognc);
530                // result_preds.ins_col(result_preds.cols(),preds);
531
532                // cout << "Updated." << endl;
533       
534                /*
535                vector<vec> conditions;
536                //emlig* emliga = new emlig(2);
537                RARX* my_rarx = new RARX(2,10,false);
538               
539               
540                mat V0 = 0.0001 * eye ( 3 );
541                ARX* my_arx = new ARX(0.85);
542                my_arx->set_statistics ( 1, V0 ); //nu is default (set to have finite moments)
543                my_arx->set_constant ( false );
544                my_arx->validate();
545               
546
547                for(int k = 1;k<strings[j].size();k++)
548                {
549                        vec condition;
550                        //condition.ins(0,1);                           
551                        condition.ins(0,strings[j][k]);                         
552                        conditions.push_back(condition);
553
554                        //cout << "orig:" << condition << endl;
555
556                        if(conditions.size()>1)
557                        {               
558                                conditions[k-2].ins(0,strings[j][k]);
559                                       
560                        }
561
562                        if(conditions.size()>2)
563                        {
564                                conditions[k-3].ins(0,strings[j][k]);
565
566                                // cout << "Condition:" << conditions[k-3] << endl;
567
568                                my_rarx->bayes(conditions[k-3]);
569                                //my_rarx->posterior->step_me(1);
570                               
571                                vec cond_vec;
572                                cond_vec.ins(0,conditions[k-3][0]);
573                               
574                                my_arx->bayes(cond_vec,conditions[k-3].right(2));
575                                       
576                                /*
577                                if(k>8)
578                                {
579                                        //my_rarx->posterior->step_me(0);
580
581                                        //mat samples = my_rarx->posterior->sample_mat(10);
582
583                                        pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(1000);
584
585                                        //cout << imp_samples.first << endl;
586                                       
587                                        vec sample_prediction;
588                                        vec averaged_params = zeros(imp_samples.second.rows());
589                                        for(int t = 0;t<imp_samples.first.size();t++)
590                                        {
591                                                vec lap_sample = conditions[k-3].left(2);
592                                                //lap_sample.ins(lap_sample.size(),1.0);
593                                               
594                                                lap_sample.ins(0,LapRNG());
595
596                                                sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t));
597
598                                                averaged_params += imp_samples.first[t]*imp_samples.second.get_col(t);
599                                        }
600
601                                        averaged_params = averaged_params*(1/(imp_samples.first*ones(imp_samples.first.size())));
602
603                                        // cout << "Averaged estimated parameters: " << averaged_params << endl;
604                                       
605                                        vec sample_pow = sample_prediction;                                     
606                                       
607                                        // cout << sample_prediction << endl;
608                                        vec poly_coefs;
609                                        double prediction;
610                                        bool stop_iteration = false;
611                                        int en = 1;
612                                        do
613                                        {
614                                                double poly_coef = imp_samples.first*sample_pow/(imp_samples.first*ones(imp_samples.first.size()));
615
616                                                if(en==1)
617                                                {
618                                                        prediction = poly_coef;
619                                                }
620
621                                                poly_coef = poly_coef*en*fact(utility_constant-2+en)/fact(utility_constant-2);
622
623                                                if(abs(poly_coef)>numeric_limits<double>::epsilon())
624                                                {
625                                                        sample_pow = elem_mult(sample_pow,sample_prediction);
626                                                        poly_coefs.ins(0,pow(-1.0,en+1)*poly_coef);
627                                                }
628                                                else
629                                                {
630                                                        stop_iteration = true;
631                                                }
632                                               
633                                                en++;
634
635                                                if(en>20)
636                                                {
637                                                        stop_iteration = true;
638                                                }
639                                        }
640                                        while(!stop_iteration);
641
642                                        /*
643                                        ofstream myfile_coef;                                           
644
645                                        myfile_coef.open("c:\\coefs.txt",ios::app);
646                                       
647                                        for(int t = 0;t<poly_coefs.size();t++)
648                                        {
649                                                myfile_coef << poly_coefs[t] << ",";                                   
650                                        }
651
652                                        myfile_coef << endl;
653                                        myfile_coef.close();
654                                        */
655
656                                        //cout << "Coefficients: " << poly_coefs << endl;
657                                                                               
658                                        /*
659                                        vec bas_coef = vec("1.0 2.0 -8.0");
660                                        cout << "Coefs: " << bas_coef << endl;
661                                        cvec actions2 = roots(bas_coef);
662                                        cout << "Roots: " << actions2 << endl;
663                                        */
664                                       
665                                    /*
666
667                                        cvec actions = roots(poly_coefs);
668                                       
669
670                                        bool is_max = false;
671                                        for(int t = 0;t<actions.size();t++)
672                                        {
673                                                if(actions[t].imag() == 0)
674                                                {
675                                                        double second_derivative = 0;
676                                                        for(int q = 1;q<poly_coefs.size();q++)
677                                                        {
678                                                                second_derivative+=poly_coefs[q]*pow(actions[t].real(),q-1)*q;
679                                                        }
680
681                                                        if(second_derivative<0)
682                                                        {
683                                                                cout << "Action:" << actions[t].real() << endl;
684
685                                                                is_max = true;
686                                                        }
687                                                }
688                                        }
689
690                                        if(!is_max)
691                                        {
692                                                cout << "No maximum." << endl;
693                                        }
694
695                                        // cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl;
696
697                                        /*
698                                        double prediction = 0;
699                                        for(int s = 1;s<samples.rows();s++)
700                                        {
701                                               
702                                                double avg_parameter = imp_samples.get_row(s)*ones(samples.cols())/samples.cols();
703
704                                                prediction += avg_parameter*conditions[k-3][s-1];
705
706                                               
707                                               
708                                                /*
709                                                ofstream myfile;
710                                                char fstring[80];
711                                                strcpy(fstring,file_strings[j]);
712
713                                                char es[5];
714                                                strcat(fstring,itoa(s,es,10));
715
716                                                strcat(fstring,"_res.txt");
717                                               
718
719                                                myfile.open(fstring,ios::app);
720                                               
721                                                //myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
722                                                myfile << avg_parameter;
723                                               
724                                                if(k!=strings[j].size()-1)
725                                                {
726                                                        myfile << ",";
727                                                }
728                                                else
729                                                {
730                                                        myfile << endl;
731                                                }
732                                                myfile.close();
733                                                */
734
735                                       
736                                        //}
737
738                                        // cout << "Prediction: "<< prediction << endl;
739                                        /*
740                                        enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2));
741                                        double prediction2 = pred_mat->mean()[0];
742                                        */
743
744                                       
745                                        ofstream myfile;
746                                        char fstring[80];                                       
747                                        strcpy(fstring,file_string);
748                                       
749                                        strcat(fstring,"lognc.txt");                                   
750
751                                        myfile.open(fstring,ios::app);
752                                       
753                                        // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
754                                       
755                                        if(time == max_model_order)
756                                        { 
757                                                for(int i = 0;i<cur_res_lognc.size();i++)
758                                                {
759                                                        for(set<pair<int,int>>::iterator ar_ref = models[i]->ar_components.begin();ar_ref != models[i]->ar_components.end();ar_ref++)
760                                                        {
761                                                                myfile << (*ar_ref).second << (*ar_ref).first;                                                 
762                                                        }
763
764                                                        myfile << ".";
765
766                                                        if(models[i]->my_arx == NULL)
767                                                        {
768                                                                myfile << "1";
769                                                        }
770                                                        else
771                                                        {
772                                                                myfile << "0";
773                                                        }
774                                                               
775                                                        if(models[i]->has_constant)
776                                                        {
777                                                                myfile << "1";
778                                                        }
779                                                        else
780                                                        {
781                                                                myfile << "0";
782                                                        }
783
784                                                        myfile << ",";
785                                                }
786
787                                                myfile << endl;
788                                        }
789                                       
790                                        for(int i = 0;i<cur_res_lognc.size();i++)
791                                        {
792                                                myfile << cur_res_lognc[i] << ' ';//zmenil som ciarku ze medzeru
793                                        }
794
795                                        myfile << endl;                         
796                                       
797                                        myfile.close();
798                        }
799                                        /*
800                                        myfile.open(f2string,ios::app);
801                                        myfile << prediction2;
802                                       
803                                        if(k!=strings[j].size()-1)
804                                        {
805                                                myfile << ",";
806                                        }
807                                        else
808                                        {
809                                                myfile << endl;
810                                        }
811                                        myfile.close();
812                                        //*//*
813
814                                }                                       
815                        }       */
816                       
817                        //emliga->step_me(0);
818                        /*
819                        ofstream myfile;
820                        myfile.open("c:\\robust_ar1.txt",ios::app);
821                        myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";";
822                        myfile.close();
823
824                        myfile.open("c:\\robust_ar2.txt",ios::app);
825                        myfile << emliga->minimal_vertex->get_coordinates()[1] << ";";
826                        myfile.close();
827                       
828
829                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
830                        cout << "Step: " << i << endl;*/
831                //}
832
833
834        //}
835
836
837        // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading
838    //             with maximization of logarithm of one-step ahead wealth.
839
840       
841               
842                /*
843                cout << "One experiment finished." << endl;
844
845                ofstream myfile;
846                myfile.open("c:\\robust_ar1.txt",ios::app);
847                myfile << endl;
848                myfile.close();
849
850                myfile.open("c:\\robust_ar2.txt",ios::app);
851                myfile << endl;
852                myfile.close();*/
853       
854
855        //emlig* emlig1 = new emlig(emlig_size);
856
857        //emlig1->step_me(0);
858        //emlig* emlig2 = new emlig(emlig_size);
859       
860        /*
861        emlig1->set_correction_factors(4);
862
863        for(int j = 0;j<emlig1->correction_factors.size();j++)
864        {
865                for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++)
866                {
867                        cout << j << "    ";
868                       
869                        for(int i=0;i<(*vec_ref).size();i++)
870                        {
871                                cout << (*vec_ref)[i];
872                        }
873
874                        cout << endl;
875                }
876        }*/
877       
878        /*
879    vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5";
880
881        emlig1->add_condition(condition5);
882        //emlig1->step_me(0);
883
884
885        vec condition1a = "-1.0 1.02 0.5";
886        //vec condition1b = "1.0 1.0 1.01";
887        emlig1->add_condition(condition1a);
888        //emlig2->add_condition(condition1b);
889
890        vec condition2a = "-0.3 1.7 1.5";
891        //vec condition2b = "-1.0 1.0 1.0";
892        emlig1->add_condition(condition2a);
893        //emlig2->add_condition(condition2b);
894
895        vec condition3a = "0.5 -1.01 1.0";
896        //vec condition3b = "0.5 -1.01 1.0";
897
898        emlig1->add_condition(condition3a);
899        //emlig2->add_condition(condition3b);   
900
901        vec condition4a = "-0.5 -1.0 1.0";
902        //vec condition4b = "-0.5 -1.0 1.0";   
903
904        emlig1->add_condition(condition4a);
905        //cout << "************************************************" << endl;
906        //emlig2->add_condition(condition4b);
907        //cout << "************************************************" << endl;
908       
909        //cout << emlig1->minimal_vertex->get_coordinates();
910       
911        //emlig1->remove_condition(condition3a);
912        //emlig1->step_me(0);
913        //emlig1->remove_condition(condition2a);
914        //emlig1->remove_condition(condition1a);
915        //emlig1->remove_condition(condition5);
916       
917
918        //emlig1->step_me(0);
919        //emlig2->step_me(0);
920       
921
922        // DA SE POUZIT PRO VYPIS DO SOUBORU
923        // emlig1->step_me(0);
924
925        //emlig1->remove_condition(condition1);
926       
927       
928
929       
930       
931        /*
932        for(int i = 0;i<100;i++)
933        {
934                cout << endl << "Step:" << i << endl;           
935
936                double condition[emlig_size+1];         
937
938                for(int k = 0;k<=emlig_size;k++)
939                {
940                        condition[k] = (rand()-RAND_MAX/2)/1000.0;             
941                }
942                       
943
944                vec* condition_vec = new vec(condition,emlig_size+1);
945                emlig1->add_condition(*condition_vec);
946
947                /*
948                for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly)
949                {
950                        cout << ((toprow*)toprow_ref)->probability << endl;
951                }
952                */
953                /*
954                cout << emlig1->statistic_rowsize(emlig_size) << endl << endl;
955       
956                /*
957                if(i-emlig1->number_of_parameters >= 0)
958                {
959                        pause(30);
960                }
961                */
962
963                // emlig1->step_me(i);
964               
965                /*
966                vector<int> sizevector;
967                for(int s = 0;s<=emlig1->number_of_parameters;s++)
968                {
969                        sizevector.push_back(emlig1->statistic_rowsize(s));
970                }
971                */
972        //}
973   
974
975
976       
977        /*
978        emlig1->step_me(1);
979
980        vec condition = "2.0 0.0 1.0"; 
981
982        emlig1->add_condition(condition);
983
984        vector<int> sizevector;
985        for(int s = 0;s<=emlig1->number_of_parameters;s++)
986        {
987                sizevector.push_back(emlig1->statistic_rowsize(s));
988        }
989
990        emlig1->step_me(2);
991
992        condition = "2.0 1.0 0.0";
993
994        emlig1->add_condition(condition);
995
996        sizevector.clear();
997        for(int s = 0;s<=emlig1->number_of_parameters;s++)
998        {
999                sizevector.push_back(emlig1->statistic_rowsize(s));
1000        }
1001        */
1002
1003        return 0;
1004}
1005
1006
Note: See TracBrowser for help on using the browser.