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

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>
[1376]14//#include <itpp/itsignal.h>
[1361]15#include "windows.h"
16#include "ddeml.h"
17#include "stdio.h"
[1282]18
[1361]19//#include "DDEClient.h"
20//#include <conio.h>
[1358]21
[1361]22
[1208]23using namespace itpp;
[1337]24using namespace bdm;
[976]25
[1361]26//const int emlig_size = 2;
27//const int utility_constant = 5;
[1268]28
[1361]29const int max_model_order = 2;
[1379]30const double apriorno=0.005;
[1272]31
[1376]32/*
[1361]33HDDEDATA CALLBACK DdeCallback(
[1376]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.
[1361]42{
[1376]43        return 0;
[1361]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,
[1365]50                                                                        XTYP_ADVSTART,TIMEOUT_ASYNC , NULL); //TIMEOUT_ASYNC
[1361]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
[1367]62DWORD WINAPI ThrdFunc( LPVOID n )
63{   
64    return 0;
65}
[1376]66*/
[1367]67
[1361]68class model
69{
70public:
[1376]71        set<pair<int,int>> ar_components;
[1358]72
[1361]73        // Best thing would be to inherit the two models from a single souce, this is planned, but now structurally
74        // problematic.
[1376]75        RARX* my_rarx; //vzmenovane parametre pre triedu model
[1379]76        ARXwin* my_arx;
[1361]77
78        bool has_constant;
[1376]79        int  window_size;  //musi byt vacsia ako pocet krokov ak to nema ovplyvnit
[1361]80        int  predicted_channel;
81        mat* data_matrix;
82       
[1376]83        model(set<pair<int,int>> ar_components, //funkcie treidz model-konstruktor
[1361]84                  bool robust, 
85                  bool has_constant, 
86                  int window_size, 
[1376]87                  int predicted_channel,
[1361]88                  mat* data_matrix)
[1358]89        {
[1376]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;
[1361]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                        }
[1376]103                else
[1361]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;
[1379]112                        my_arx  = new ARXwin();
[1361]113                        mat V0;
114
115                        if(has_constant)
116                        {                               
[1376]117                                V0  = apriorno * eye(ar_components.size()+2); //aj tu konst
[1370]118                                V0(0,0) = 1;
[1379]119                                my_arx->set_constant(true);                             
[1361]120                        }
121                        else
122                        {
123                               
[1376]124                                V0  = apriorno * eye(ar_components.size()+1);//menit konstantu
[1370]125                                V0(0,0) = 1;
[1361]126                                my_arx->set_constant(false);                           
127                               
128                        }
129
[1379]130                        my_arx->set_statistics(1, V0, V0.rows()+1);                     
131                        my_arx->set_parameters(window_size);
[1361]132                        my_arx->validate();
133                }
[1358]134        }
[1361]135
[1376]136        void data_update(int time) //vlozime cas a ono vlozi do data_vector podmineky(conditions) a predikce, ktore pouzije do bayes
[1358]137        {
[1376]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?
[1358]143                }
[1376]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                }
[1358]149                else
150                {
[1376]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);
[1361]154                }
155        }
156
[1376]157        pair<vec,vec> predict(int sample_size, int time, itpp::Laplace_RNG* LapRNG)  //nerozumiem, ale vraj to netreba, nepouziva to
[1367]158        {
[1376]159                vec condition_vector;
160                for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++)
[1367]161                {
[1376]162                        condition_vector.ins(condition_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second+1));
163                }
[1367]164
[1376]165                if(my_rarx!=NULL)
166                {
167                        pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(sample_size);
[1367]168
[1376]169                        //cout << imp_samples.first << endl;
170                       
171                        vec sample_prediction;                 
172                        for(int t = 0;t<sample_size;t++)
[1367]173                        {
[1376]174                                vec lap_sample = condition_vector;
[1367]175                               
[1376]176                                if(has_constant)
[1367]177                                {
[1376]178                                        lap_sample.ins(lap_sample.size(),1.0);
[1367]179                                }
[1376]180                               
181                                lap_sample.ins(0,(*LapRNG)());
[1367]182
[1376]183                                sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t));                             
[1367]184                        }
185
[1376]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;
[1367]196                               
[1376]197                                if(has_constant)
[1367]198                                {
[1376]199                                        gau_sample.ins(gau_sample.size(),1.0);
[1367]200                                }
[1376]201                               
202                                gau_sample.ins(0,randn());
[1367]203
[1376]204                                sample_prediction.ins(0,gau_sample*samples.get_col(t));                         
[1367]205                        }
[1376]206
207                        return pair<vec,vec>(ones(sample_prediction.size()),sample_prediction);
[1367]208                }
209       
210        }
211
212
[1376]213        static set<set<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels)
[1361]214        {
[1376]215                set<set<pair<int,int>>> created_model_types;           
[1361]216
[1376]217                if(max_order == 1)//ukoncovacia vetva
[1361]218                {                       
[1376]219                        for(int channel = 0;channel<number_of_channels;channel++)//pre AR 1 model vytvori kombinace kanalov v prvom kroku poyadu
[1358]220                        {
[1376]221                                set<pair<int,int>> returned_type;
222                                returned_type.insert(pair<int,int>(channel,1));  //??
223                                created_model_types.insert(returned_type);
[1358]224                        }
[1361]225
226                        return created_model_types;
227                }
228                else
229                {
[1376]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;
[1361]232                       
[1376]233                        for(set<set<pair<int,int>>>::iterator model_ref = created_model_types.begin();model_ref!=created_model_types.end();model_ref++)
[1361]234                        {                               
235                               
236                                for(int order = 1; order<=max_order; order++)
[1358]237                                {
[1361]238                                        for(int channel = 0;channel<number_of_channels;channel++)
239                                        {
[1376]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()) //??
[1361]243                                                {
[1376]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);                                                   
[1361]249                                                }
250                                        }
[1358]251                                }
252                        }
[1361]253
[1376]254                        created_model_types.insert(returned_types.begin(),returned_types.end());
[1361]255
256                        return created_model_types;
257                }               
[1358]258        }
[1361]259};
260
261
262
263
[1367]264int main ( int argc, char* argv[] ) {   
[1358]265       
[1367]266        /*
[1376]267        DWORD Id;
[1367]268        HANDLE hThrd = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ThrdFunc, (LPVOID)1, 0, &Id);
[1376]269 
270        if ( !hThrd )
[1367]271    {
272        cout<<"Error Creating Threads,,,,.exiting"<<endl;
273        return -1;
274    }
275    Sleep ( 100 );
[1376]276
[1361]277       
[1376]278        char szApp[] = "MT4";
279        char szTopic[] = "QUOTE";       
280        char szItem1[] = "EURUSD";     
[1361]281
282        //DDE Initialization
[1376]283        DWORD idInst=0;
[1361]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);
[1367]300        //DdeFreeStringHandle(idInst, hszApp);
301        //DdeFreeStringHandle(idInst, hszTopic);
[1361]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       
[1376]311        DDERequest(idInst, hConv, szItem1);             
[1361]312       
[1367]313        while(1)
314        {
[1376]315                MSG    msg;
316                BOOL   MsgReturn = GetMessage ( &msg , NULL , 0 , 0 );
317           
318                if(MsgReturn)
[1367]319                {
[1376]320                        TranslateMessage(&msg);
321                        DispatchMessage(&msg);                 
[1367]322                }
323        }
[1376]324
[1361]325        //DDE Disconnect and Uninitialize.
[1367]326        DdeDisconnect(hConv);
327        DdeUninitialize(idInst);
[1376]328        */
[1361]329
[1376]330       
[1361]331
[1376]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>>());
[1186]340
[1376]341        char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"};
342       
[1282]343
[1376]344        for(int i = 0;i<3;i++)
345        {       
346                ifstream myfile(file_strings[i]);
[1282]347                if (myfile.is_open())
[1376]348                {
349                        while ( myfile.good() )
350                        {
351                                string line;
352                                getline(myfile,line);
353                               
354                                vector<string> parsed_line;
[1282]355                                while(line.find(',') != string::npos)
356                                {
[1376]357                                        int loc = line.find(',');
358                                        parsed_line.push_back(line.substr(0,loc));
[1282]359                                        line.erase(0,loc+1);                                   
[1376]360                                }                               
[1282]361
[1376]362                                string_lists[i].push_back(parsed_line);
363                        }
364                        myfile.close();
[1282]365                }
[1376]366        }
[1282]367
[1376]368        for(int j = 0;j<string_lists.size();j++)
369        {
[1301]370               
[1376]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);
[1301]376
[1376]377                        for(int k = 1;k<string_lists[j][i].size();k++)
[1282]378                        {
[1376]379                                vec condition;
380                                //condition.ins(0,1);                           
381                                condition.ins(0,string_lists[j][i][k]);                         
382                                conditions.push_back(condition);
[1282]383
[1376]384                                //cout << "orig:" << condition << endl;
385
386                                if(conditions.size()>1)
387                                {               
388                                        conditions[k-2].ins(0,string_lists[j][i][k]);
389                                       
[1282]390                                }
[1376]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                               
[1282]408                        }
409
[1376]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();
[1301]420                       
[1284]421
[1376]422                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
423                        cout << "Step: " << i << endl;
424                }
[1282]425
[1376]426                cout << "One experiment finished." << endl;
[1284]427
[1376]428                ofstream myfile;
429                myfile.open("c:\\robust_ar1.txt",ios::app);
430                myfile << endl;
431                myfile.close();
[1284]432
[1376]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();       
[1301]443
[1376]444        vector<vector<string>> strings;
[1301]445
[1379]446        char* file_string = "c:\\ar_normal_single"; // "c:\\dataTYClosePercDiff"; //
[1301]447
[1376]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                        }
[1301]469
[1376]470                        data_matrix.ins_row(data_matrix.rows(),data_vector);
471                }               
[1361]472
[1376]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
[1379]486                for(int window_size = 15;window_size < 16;window_size++)
[1376]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                }
[1365]493
[1379]494                //set<pair<int,int>> empty_list;
495                //models.push_back(new model(empty_list,false,true,100,0,&data_matrix));
[1376]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
[1379]509
510                        cout << "Updated." << endl;
[1376]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);
[1361]516                        }
[1376]517                        else
518                        {
519                                cur_res_lognc.ins(cur_res_lognc.size(),(*model_ref)->my_arx->posterior().lognc());
520                        }
[1367]521
[1376]522                        // pair<vec,vec> predictions = (*model_ref)->predict(200,time,&LapRNG);
[1367]523
[1376]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));
[1361]526
[1376]527                }
[1367]528
[1376]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       
[1361]534                /*
[1301]535                vector<vec> conditions;
536                //emlig* emliga = new emlig(2);
[1358]537                RARX* my_rarx = new RARX(2,10,false);
[1337]538               
[1338]539               
[1337]540                mat V0 = 0.0001 * eye ( 3 );
[1349]541                ARX* my_arx = new ARX(0.85);
[1337]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();
[1338]545               
[1301]546
[1338]547                for(int k = 1;k<strings[j].size();k++)
[1301]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
[1349]566                                // cout << "Condition:" << conditions[k-3] << endl;
[1301]567
568                                my_rarx->bayes(conditions[k-3]);
[1338]569                                //my_rarx->posterior->step_me(1);
[1337]570                               
571                                vec cond_vec;
572                                cond_vec.ins(0,conditions[k-3][0]);
573                               
[1338]574                                my_arx->bayes(cond_vec,conditions[k-3].right(2));
[1301]575                                       
[1361]576                                /*
[1346]577                                if(k>8)
[1301]578                                {
[1324]579                                        //my_rarx->posterior->step_me(0);
580
[1346]581                                        //mat samples = my_rarx->posterior->sample_mat(10);
[1343]582
[1346]583                                        pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(1000);
[1343]584
[1346]585                                        //cout << imp_samples.first << endl;
[1336]586                                       
[1337]587                                        vec sample_prediction;
[1358]588                                        vec averaged_params = zeros(imp_samples.second.rows());
[1346]589                                        for(int t = 0;t<imp_samples.first.size();t++)
[1337]590                                        {
591                                                vec lap_sample = conditions[k-3].left(2);
[1346]592                                                //lap_sample.ins(lap_sample.size(),1.0);
[1337]593                                               
594                                                lap_sample.ins(0,LapRNG());
595
[1346]596                                                sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t));
[1358]597
598                                                averaged_params += imp_samples.first[t]*imp_samples.second.get_col(t);
[1337]599                                        }
600
[1358]601                                        averaged_params = averaged_params*(1/(imp_samples.first*ones(imp_samples.first.size())));
602
603                                        // cout << "Averaged estimated parameters: " << averaged_params << endl;
[1338]604                                       
[1358]605                                        vec sample_pow = sample_prediction;                                     
[1343]606                                       
607                                        // cout << sample_prediction << endl;
[1337]608                                        vec poly_coefs;
[1346]609                                        double prediction;
[1337]610                                        bool stop_iteration = false;
[1343]611                                        int en = 1;
[1337]612                                        do
613                                        {
[1346]614                                                double poly_coef = imp_samples.first*sample_pow/(imp_samples.first*ones(imp_samples.first.size()));
[1337]615
[1346]616                                                if(en==1)
617                                                {
618                                                        prediction = poly_coef;
619                                                }
620
[1343]621                                                poly_coef = poly_coef*en*fact(utility_constant-2+en)/fact(utility_constant-2);
622
[1337]623                                                if(abs(poly_coef)>numeric_limits<double>::epsilon())
624                                                {
625                                                        sample_pow = elem_mult(sample_pow,sample_prediction);
[1343]626                                                        poly_coefs.ins(0,pow(-1.0,en+1)*poly_coef);
[1337]627                                                }
628                                                else
629                                                {
630                                                        stop_iteration = true;
631                                                }
632                                               
633                                                en++;
[1343]634
635                                                if(en>20)
636                                                {
637                                                        stop_iteration = true;
638                                                }
[1337]639                                        }
640                                        while(!stop_iteration);
641
[1343]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
[1349]656                                        //cout << "Coefficients: " << poly_coefs << endl;
[1338]657                                                                               
[1343]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                                       
[1361]665                                    /*
[1346]666
[1338]667                                        cvec actions = roots(poly_coefs);
[1343]668                                       
669
[1338]670                                        bool is_max = false;
671                                        for(int t = 0;t<actions.size();t++)
672                                        {
[1343]673                                                if(actions[t].imag() == 0)
[1338]674                                                {
[1343]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                                                        }
[1338]687                                                }
688                                        }
[1301]689
[1338]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
[1346]697                                        /*
[1337]698                                        double prediction = 0;
699                                        for(int s = 1;s<samples.rows();s++)
[1336]700                                        {
701                                               
[1346]702                                                double avg_parameter = imp_samples.get_row(s)*ones(samples.cols())/samples.cols();
[1337]703
704                                                prediction += avg_parameter*conditions[k-3][s-1];
705
[1336]706                                               
[1337]707                                               
708                                                /*
[1336]709                                                ofstream myfile;
710                                                char fstring[80];
711                                                strcpy(fstring,file_strings[j]);
[1301]712
[1336]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();
[1337]733                                                */
734
[1338]735                                       
[1346]736                                        //}
737
738                                        // cout << "Prediction: "<< prediction << endl;
[1361]739                                        /*
[1337]740                                        enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2));
741                                        double prediction2 = pred_mat->mean()[0];
[1361]742                                        */
[1337]743
[1365]744                                       
[1376]745                                        ofstream myfile;
746                                        char fstring[80];                                       
747                                        strcpy(fstring,file_string);
[1337]748                                       
[1376]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                        }
[1361]799                                        /*
[1337]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();
[1361]812                                        //*//*
[1337]813
[1319]814                                }                                       
[1361]815                        }       */
[1301]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;*/
[1361]831                //}
[1337]832
833
[1365]834        //}
[1337]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       
[1301]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();*/
[1300]853       
[1301]854
855        //emlig* emlig1 = new emlig(emlig_size);
856
857        //emlig1->step_me(0);
858        //emlig* emlig2 = new emlig(emlig_size);
[1300]859       
[1267]860        /*
861        emlig1->set_correction_factors(4);
[1266]862
[1267]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                {
[1268]867                        cout << j << "    ";
868                       
[1267]869                        for(int i=0;i<(*vec_ref).size();i++)
870                        {
871                                cout << (*vec_ref)[i];
872                        }
873
874                        cout << endl;
875                }
[1268]876        }*/
877       
[1301]878        /*
[1300]879    vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5";
880
[1299]881        emlig1->add_condition(condition5);
[1301]882        //emlig1->step_me(0);
883
884
885        vec condition1a = "-1.0 1.02 0.5";
[1300]886        //vec condition1b = "1.0 1.0 1.01";
[1301]887        emlig1->add_condition(condition1a);
[1300]888        //emlig2->add_condition(condition1b);
[1267]889
[1301]890        vec condition2a = "-0.3 1.7 1.5";
[1300]891        //vec condition2b = "-1.0 1.0 1.0";
[1301]892        emlig1->add_condition(condition2a);
[1300]893        //emlig2->add_condition(condition2b);
[1234]894
[1301]895        vec condition3a = "0.5 -1.01 1.0";
[1300]896        //vec condition3b = "0.5 -1.01 1.0";
[1280]897
[1301]898        emlig1->add_condition(condition3a);
[1300]899        //emlig2->add_condition(condition3b);   
[1280]900
[1301]901        vec condition4a = "-0.5 -1.0 1.0";
[1300]902        //vec condition4b = "-0.5 -1.0 1.0";   
903
[1301]904        emlig1->add_condition(condition4a);
[1300]905        //cout << "************************************************" << endl;
906        //emlig2->add_condition(condition4b);
907        //cout << "************************************************" << endl;
908       
[1299]909        //cout << emlig1->minimal_vertex->get_coordinates();
[1300]910       
[1301]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       
[1275]917
[1299]918        //emlig1->step_me(0);
919        //emlig2->step_me(0);
920       
[1282]921
922        // DA SE POUZIT PRO VYPIS DO SOUBORU
[1275]923        // emlig1->step_me(0);
924
925        //emlig1->remove_condition(condition1);
926       
[1301]927       
[1275]928
929       
[1301]930       
[1275]931        /*
[1282]932        for(int i = 0;i<100;i++)
[1219]933        {
[1282]934                cout << endl << "Step:" << i << endl;           
[1208]935
[1268]936                double condition[emlig_size+1];         
[1220]937
[1268]938                for(int k = 0;k<=emlig_size;k++)
939                {
[1272]940                        condition[k] = (rand()-RAND_MAX/2)/1000.0;             
[1268]941                }
942                       
[1216]943
[1268]944                vec* condition_vec = new vec(condition,emlig_size+1);
[1219]945                emlig1->add_condition(*condition_vec);
[1271]946
[1272]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                */
[1275]953                /*
[1271]954                cout << emlig1->statistic_rowsize(emlig_size) << endl << endl;
[1268]955       
[1272]956                /*
[1271]957                if(i-emlig1->number_of_parameters >= 0)
958                {
959                        pause(30);
960                }
[1272]961                */
[1219]962
[1271]963                // emlig1->step_me(i);
[1219]964               
[1272]965                /*
[1219]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                }
[1272]971                */
[1275]972        //}
973   
[1219]974
975
976       
977        /*
978        emlig1->step_me(1);
979
980        vec condition = "2.0 0.0 1.0"; 
981
[1208]982        emlig1->add_condition(condition);
983
[1216]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
[1219]990        emlig1->step_me(2);
[1216]991
[1219]992        condition = "2.0 1.0 0.0";
[1216]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        }
[1219]1001        */
[1216]1002
[1376]1003        return 0;
1004}
[976]1005
[1282]1006
Note: See TracBrowser for help on using the browser.