root/applications/robust/main.cpp @ 1358

Revision 1358, 15.0 kB (checked in by sindj, 14 years ago)

DDE data. JS

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