root/applications/robust/main.cpp @ 1370

Revision 1370, 24.7 kB (checked in by sindj, 13 years ago)

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