root/applications/robust/main.cpp @ 1359

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

DDE data. 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 "DDEClient.h"
17#include <conio.h>
18
19
20using namespace itpp;
21using namespace bdm;
22
23const int emlig_size = 2;
24const int utility_constant = 5;
25
26
27int main ( int argc, char* argv[] ) {
28       
29        itpp::Laplace_RNG LapRNG = Laplace_RNG();
30
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       
86        /*
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.
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>>());
94
95        char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"};
96       
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        {
124               
125                for(int i = 0;i<string_lists[j].size()-1;i++)
126                {
127                        vector<vec> conditions;
128                        //emlig* emliga = new emlig(2);
129                        RARX* my_rarx = new RARX(2,30);
130
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
152                                        my_rarx->bayes(conditions[k-3]);
153
154                                       
155                                        //if(k>5)
156                                        //{
157                                        //      cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
158                                        //}
159                                       
160                                }                               
161                               
162                        }
163
164                        //emliga->step_me(0);
165                        /*
166                        ofstream myfile;
167                        myfile.open("c:\\robust_ar1.txt",ios::app);
168                        myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";";
169                        myfile.close();
170
171                        myfile.open("c:\\robust_ar2.txt",ios::app);
172                        myfile << emliga->minimal_vertex->get_coordinates()[1] << ";";
173                        myfile.close();
174                       
175
176                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl;
177                        cout << "Step: " << i << endl;
178                }
179
180                cout << "One experiment finished." << endl;
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();
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
200        char* file_strings[3] = {"c:\\dataADClosePercDiff","c:\\ar_student_single","c:\\ar_cauchy_single"};
201
202        for(int i = 0;i<3;i++)
203        {                       
204                char dfstring[80];
205                strcpy(dfstring,file_strings[i]);
206                strcat(dfstring,".txt");
207               
208                ifstream myfile(dfstring);
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                }
226        }
227
228       
229       
230        for(int j = 0;j<strings.size();j++)
231        {               
232                vector<vec> conditions;
233                //emlig* emliga = new emlig(2);
234                RARX* my_rarx = new RARX(2,10,false);
235               
236               
237                mat V0 = 0.0001 * eye ( 3 );
238                ARX* my_arx = new ARX(0.85);
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();
242               
243
244                for(int k = 1;k<strings[j].size();k++)
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
263                                // cout << "Condition:" << conditions[k-3] << endl;
264
265                                my_rarx->bayes(conditions[k-3]);
266                                //my_rarx->posterior->step_me(1);
267                               
268                                vec cond_vec;
269                                cond_vec.ins(0,conditions[k-3][0]);
270                               
271                                my_arx->bayes(cond_vec,conditions[k-3].right(2));
272                                       
273                               
274                                if(k>8)
275                                {
276                                        //my_rarx->posterior->step_me(0);
277
278                                        //mat samples = my_rarx->posterior->sample_mat(10);
279
280                                        pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(1000);
281
282                                        //cout << imp_samples.first << endl;
283                                       
284                                        vec sample_prediction;
285                                        vec averaged_params = zeros(imp_samples.second.rows());
286                                        for(int t = 0;t<imp_samples.first.size();t++)
287                                        {
288                                                vec lap_sample = conditions[k-3].left(2);
289                                                //lap_sample.ins(lap_sample.size(),1.0);
290                                               
291                                                lap_sample.ins(0,LapRNG());
292
293                                                sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t));
294
295                                                averaged_params += imp_samples.first[t]*imp_samples.second.get_col(t);
296                                        }
297
298                                        averaged_params = averaged_params*(1/(imp_samples.first*ones(imp_samples.first.size())));
299
300                                        // cout << "Averaged estimated parameters: " << averaged_params << endl;
301                                       
302                                        vec sample_pow = sample_prediction;                                     
303                                       
304                                        // cout << sample_prediction << endl;
305                                        vec poly_coefs;
306                                        double prediction;
307                                        bool stop_iteration = false;
308                                        int en = 1;
309                                        do
310                                        {
311                                                double poly_coef = imp_samples.first*sample_pow/(imp_samples.first*ones(imp_samples.first.size()));
312
313                                                if(en==1)
314                                                {
315                                                        prediction = poly_coef;
316                                                }
317
318                                                poly_coef = poly_coef*en*fact(utility_constant-2+en)/fact(utility_constant-2);
319
320                                                if(abs(poly_coef)>numeric_limits<double>::epsilon())
321                                                {
322                                                        sample_pow = elem_mult(sample_pow,sample_prediction);
323                                                        poly_coefs.ins(0,pow(-1.0,en+1)*poly_coef);
324                                                }
325                                                else
326                                                {
327                                                        stop_iteration = true;
328                                                }
329                                               
330                                                en++;
331
332                                                if(en>20)
333                                                {
334                                                        stop_iteration = true;
335                                                }
336                                        }
337                                        while(!stop_iteration);
338
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
353                                        //cout << "Coefficients: " << poly_coefs << endl;
354                                                                               
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                                       
362
363
364                                        cvec actions = roots(poly_coefs);
365                                       
366
367                                        bool is_max = false;
368                                        for(int t = 0;t<actions.size();t++)
369                                        {
370                                                if(actions[t].imag() == 0)
371                                                {
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                                                        }
384                                                }
385                                        }
386
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
394                                        /*
395                                        double prediction = 0;
396                                        for(int s = 1;s<samples.rows();s++)
397                                        {
398                                               
399                                                double avg_parameter = imp_samples.get_row(s)*ones(samples.cols())/samples.cols();
400
401                                                prediction += avg_parameter*conditions[k-3][s-1];
402
403                                               
404                                               
405                                                /*
406                                                ofstream myfile;
407                                                char fstring[80];
408                                                strcpy(fstring,file_strings[j]);
409
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();
430                                                */
431
432                                       
433                                        //}
434
435                                        // cout << "Prediction: "<< prediction << endl;
436                                       
437                                        enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2));
438                                        double prediction2 = pred_mat->mean()[0];
439                                       
440
441                                        ofstream myfile;
442                                        char fstring[80];
443                                        char f2string[80];
444                                        strcpy(fstring,file_strings[j]);
445                                        strcpy(f2string,fstring);
446
447                                        strcat(fstring,"pred.txt");
448                                        strcat(f2string,"2pred.txt");
449                                       
450
451                                        myfile.open(fstring,ios::app);
452                                       
453                                        // myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0];
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
466                                       
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();
479                                        //*/
480
481                                }                                       
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                }
499
500
501        }
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       
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();*/
520       
521
522        //emlig* emlig1 = new emlig(emlig_size);
523
524        //emlig1->step_me(0);
525        //emlig* emlig2 = new emlig(emlig_size);
526       
527        /*
528        emlig1->set_correction_factors(4);
529
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                {
534                        cout << j << "    ";
535                       
536                        for(int i=0;i<(*vec_ref).size();i++)
537                        {
538                                cout << (*vec_ref)[i];
539                        }
540
541                        cout << endl;
542                }
543        }*/
544       
545        /*
546    vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5";
547
548        emlig1->add_condition(condition5);
549        //emlig1->step_me(0);
550
551
552        vec condition1a = "-1.0 1.02 0.5";
553        //vec condition1b = "1.0 1.0 1.01";
554        emlig1->add_condition(condition1a);
555        //emlig2->add_condition(condition1b);
556
557        vec condition2a = "-0.3 1.7 1.5";
558        //vec condition2b = "-1.0 1.0 1.0";
559        emlig1->add_condition(condition2a);
560        //emlig2->add_condition(condition2b);
561
562        vec condition3a = "0.5 -1.01 1.0";
563        //vec condition3b = "0.5 -1.01 1.0";
564
565        emlig1->add_condition(condition3a);
566        //emlig2->add_condition(condition3b);   
567
568        vec condition4a = "-0.5 -1.0 1.0";
569        //vec condition4b = "-0.5 -1.0 1.0";   
570
571        emlig1->add_condition(condition4a);
572        //cout << "************************************************" << endl;
573        //emlig2->add_condition(condition4b);
574        //cout << "************************************************" << endl;
575       
576        //cout << emlig1->minimal_vertex->get_coordinates();
577       
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       
584
585        //emlig1->step_me(0);
586        //emlig2->step_me(0);
587       
588
589        // DA SE POUZIT PRO VYPIS DO SOUBORU
590        // emlig1->step_me(0);
591
592        //emlig1->remove_condition(condition1);
593       
594       
595
596       
597       
598        /*
599        for(int i = 0;i<100;i++)
600        {
601                cout << endl << "Step:" << i << endl;           
602
603                double condition[emlig_size+1];         
604
605                for(int k = 0;k<=emlig_size;k++)
606                {
607                        condition[k] = (rand()-RAND_MAX/2)/1000.0;             
608                }
609                       
610
611                vec* condition_vec = new vec(condition,emlig_size+1);
612                emlig1->add_condition(*condition_vec);
613
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                */
620                /*
621                cout << emlig1->statistic_rowsize(emlig_size) << endl << endl;
622       
623                /*
624                if(i-emlig1->number_of_parameters >= 0)
625                {
626                        pause(30);
627                }
628                */
629
630                // emlig1->step_me(i);
631               
632                /*
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                }
638                */
639        //}
640   
641
642
643       
644        /*
645        emlig1->step_me(1);
646
647        vec condition = "2.0 0.0 1.0"; 
648
649        emlig1->add_condition(condition);
650
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
657        emlig1->step_me(2);
658
659        condition = "2.0 1.0 0.0";
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        }
668        */
669
670        return 0;
671}
672
673
Note: See TracBrowser for help on using the browser.