1 | |
---|
2 | /*! |
---|
3 | \file |
---|
4 | \brief Robust |
---|
5 | \author Vasek Smidl |
---|
6 | |
---|
7 | */ |
---|
8 | |
---|
9 | //<<<<<<< .mine |
---|
10 | #include "robustlib.h" |
---|
11 | #include "trading_models_lib.h" |
---|
12 | #include <iostream> |
---|
13 | #include "estim/arx.h" |
---|
14 | |
---|
15 | //======= |
---|
16 | //#include "application/robust/robustlib.h" ma tu byt aj daka cesta k tomu suboru? |
---|
17 | #include "trading_models_lib.h" |
---|
18 | #include "estim/arx.h" |
---|
19 | #include <vector> |
---|
20 | #include <string> |
---|
21 | #include <sstream> |
---|
22 | #include <fstream> |
---|
23 | |
---|
24 | |
---|
25 | //>>>>>>> .r1284 |
---|
26 | using namespace bdm; |
---|
27 | |
---|
28 | double sumastlpec(int k,vector<vec> pole,vector<vec> pravd) { |
---|
29 | double r=0; |
---|
30 | for (int i=0;i < pole.size();i++) |
---|
31 | { |
---|
32 | r+=pole[i][k+4]*pravd[i][k]; |
---|
33 | //r=r+exp(pole[i][k+4]-m[k+4]+pravd[i][k]); |
---|
34 | } |
---|
35 | //cout <<"r"<< log(r); |
---|
36 | return r; //log |
---|
37 | } |
---|
38 | |
---|
39 | int main () { |
---|
40 | |
---|
41 | char* outfile_strings[18] = {"prsti_BO_.txt", "prsti_PA_.txt", "prsti_PL_.txt", "prsti_AD_.txt", "prsti_BP_.txt", "prsti_C__.txt", "prsti_CC_.txt", "prsti_CD_.txt", "prsti_CL_.txt", "prsti_CT_.txt", "prsti_CU_.txt", "prsti_ED_.txt", "prsti_FC2.txt", "prsti_FV2.txt", "prsti_GC_.txt", "prsti_HG_.txt", "prsti_HO_.txt", "prsti_HU_.txt"}; |
---|
42 | char* file_strings[18] = {"c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\BO_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\PA_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\PL_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\AD_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\BP_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\C__.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\CC_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\CD_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\CL_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\CT_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\CU_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\ED_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\FC2.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\FV2.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\GC_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\HG_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\HO_.txt", "c:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-MATHEMATIKA\\HU_.txt"}; |
---|
43 | //for (int a=0;a<18;a++) |
---|
44 | //{ |
---|
45 | int a=3; |
---|
46 | vector<vector<string>> ADdata; //nacitavanie dat do pola ADdata -funguje spravne |
---|
47 | ifstream myfile(file_strings[a]); //"C:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-Mathematika\\CL_.txt"); |
---|
48 | if (myfile.is_open()) |
---|
49 | { |
---|
50 | while ( myfile.good() ) |
---|
51 | { |
---|
52 | string line; |
---|
53 | getline(myfile,line); |
---|
54 | vector<string> parsed_line; |
---|
55 | while(line.find(' ') != string::npos) //jeden kanal je jeden riadok, na zaciatku a na konci {,}, data oddelene ciarkou a medzerou. |
---|
56 | { |
---|
57 | line.erase(0,1); //toto nie je yrovna peknz sposob,ale pri poslednom nacitani cisla v riadku sme uz nemali ziadnu medyeru a cyklus by sa posledny krat nevykonal, tak tu medzeru odstranujeme vzdy tu |
---|
58 | int loc = line.find(','); //ale pri poslednom cisla to nenajde ziadnu ciaarku, tak potom co prida do parsed_line? |
---|
59 | parsed_line.push_back(line.substr(0,loc)); |
---|
60 | line.erase(0,loc+1); //odstranujeme ciarku za kazdym cislom |
---|
61 | } |
---|
62 | ADdata.push_back(parsed_line); //3927 dat v riadku, 6 riadkov |
---|
63 | } |
---|
64 | } |
---|
65 | myfile.close(); //konec nacitavania dat |
---|
66 | /* //to malo byt rpevedenie str na double, aspon mysslim |
---|
67 | std::istringstream stme,stmd; //je to dake chybne, aj cisla z toho ADdata cita/zapisuje trochu ine |
---|
68 | double d,e; |
---|
69 | vector<vec> data_rozdiel; |
---|
70 | for (int b=0;b<ADdata.size();b++) |
---|
71 | { |
---|
72 | vec pom; |
---|
73 | stmd.str(ADdata[b][0]); |
---|
74 | stmd >>d; |
---|
75 | for (int c=1;c<ADdata[b].size();c++) |
---|
76 | { |
---|
77 | stme.str(ADdata[b][c]); |
---|
78 | stme >>e; |
---|
79 | cout<<e<<' '<<d ; |
---|
80 | pom.ins(pom.size(),e-d); |
---|
81 | cout<<pom; |
---|
82 | } |
---|
83 | }*/ |
---|
84 | vector<vec> norm; //do norm zapisujeme normalizacne faktory |
---|
85 | for (int h=1;h<=2;h++) //cyklus ktory ovplzvnuje konstantu h=1- model s konstantou, h=2, bez konstanty |
---|
86 | { |
---|
87 | bool b; //b pouzivame pri set_constant |
---|
88 | if(h==2) |
---|
89 | b=false; |
---|
90 | else |
---|
91 | b=true; |
---|
92 | int g=2; |
---|
93 | while (g<=4) //cyklus co meni rozmery matice V |
---|
94 | { |
---|
95 | mat V0 = 0.0001*eye ( g ); // aj tato matica ma vplyv na normalizacny faktor, nemoze byt aj preto taky velky, ako inak by sa dala zvolit? |
---|
96 | |
---|
97 | int p=0; |
---|
98 | while (p<=1) //tento cyklus prechadza vacsinou len raz, vtedy p=0 a nic to neovplvni, ale pri AR(2) modely to bude vykonavat 2 krat aj pre p=1, ked bude brat do condition aj rozne kanale z toho isteho casu |
---|
99 | { |
---|
100 | int i=0; |
---|
101 | while(i < ADdata.size()-p) //niekedy sa ten cyklus ma vykonat len raz, preto nepouzivam for cyklus |
---|
102 | { |
---|
103 | int j=p*(i+1); //j=0 alebo j=i+1 |
---|
104 | |
---|
105 | while(j < ADdata.size()) |
---|
106 | { |
---|
107 | ARX Ar; |
---|
108 | RARX RAr(g+h-3,0,b); |
---|
109 | Ar.set_statistics ( 1, V0 ); //nu is default (set to have finite moments) |
---|
110 | Ar.set_constant ( b ); |
---|
111 | Ar.validate(); // forgetting is default: 1.0 |
---|
112 | vec pomocka; //pri kazdej jednej hypoteze zapisujeme normalizacne faktory do pomocky, tu potom ako riadok pridame do norm |
---|
113 | for(int k = 0;k<341;k++) //prechadyame "po riadkoch", teda v case. Nejake hodnoty su len po index 340, dalej uz #INF000 |
---|
114 | { |
---|
115 | vec condition; |
---|
116 | vec predikce; |
---|
117 | predikce.ins(0,ADdata[3][k+2]); //predpovede nacitavame a zadavame do Bayes zvlast |
---|
118 | condition.ins(0,ADdata[i][k+1]); |
---|
119 | condition.ins(1,ADdata[j][k+p]);//zalezi na poradi v conditions? Teraz su na zaciatku starsie data |
---|
120 | //cout<<condition; |
---|
121 | RAr.bayes(condition.right(g+h-3)); |
---|
122 | Ar.bayes(predikce,condition.right(g+h-3)); //z condition berem len urcity pocet prvkov, bud 0, 1,alebo 2, lebo nepotrebujem vzdy vsetky (AR(1) model) |
---|
123 | pomocka.ins(pomocka.size(),Ar.posterior().lognc()); //nie je tu exponenciala! -aby to bolo mensie |
---|
124 | } |
---|
125 | //cout<<pomocka; |
---|
126 | pomocka.ins(0,j+1); |
---|
127 | pomocka.ins(0,i+1); |
---|
128 | pomocka.ins(0,g); |
---|
129 | pomocka.ins(0,h); |
---|
130 | RAr.posterior->log_nc; |
---|
131 | norm.push_back(pomocka); |
---|
132 | if ((g==3 && h==2) || (g==4)) //tento cyklus sa bude opakovat, len ak mame maticu V0 roymeru 4x4, to je AR(2) model s konst, alebo podobne len g=3, h=2, teda AR(2)bez kons |
---|
133 | { |
---|
134 | j++; |
---|
135 | } else |
---|
136 | { |
---|
137 | j=ADdata.size(); //priradenim tejto hodnoty do j sa cyklus uz viac krat nevykona |
---|
138 | } |
---|
139 | } |
---|
140 | if (b==true && g==2) //pre model AR(0) s konstantou robi tento cyklus len raz, v ostatnych pripadoch viac-krat |
---|
141 | { |
---|
142 | i=ADdata.size(); |
---|
143 | } else |
---|
144 | { |
---|
145 | i++; |
---|
146 | } |
---|
147 | } |
---|
148 | if ((g==3 && h==2) || (g==4) ) |
---|
149 | {p++;} else {p=2;} |
---|
150 | } |
---|
151 | if (h==2 && g==3) //pripad g=4, a konstanta zaroven nas uz nezaujima, vtedy to ukoncime |
---|
152 | { |
---|
153 | g=5; //ak priradime takuto hodnotu, cyklus while sa uz nevykona |
---|
154 | } else |
---|
155 | { |
---|
156 | g++; |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | //pocitanie maxima y kaydeho stlpcu norm |
---|
161 | vector<double> max; |
---|
162 | for (int i=4;i<345;i++) |
---|
163 | { |
---|
164 | double pom=norm[0][i]; |
---|
165 | for (int j=1;j<norm.size();j++) |
---|
166 | { |
---|
167 | if (pom<norm[j][i]) |
---|
168 | { |
---|
169 | pom=norm[j][i]; |
---|
170 | } |
---|
171 | } |
---|
172 | max.push_back(pom); |
---|
173 | } |
---|
174 | //nove pole namiesto norm, kde bude odcitane max a bude v spravnej forme, teda aj s exponencialov |
---|
175 | vector<vec> nfaktor; |
---|
176 | for (int j=1;j<norm.size();j++) |
---|
177 | { |
---|
178 | for (int i=1;j<norm[j].size();j++) |
---|
179 | { |
---|
180 | nfaktor[j][i]=exp(norm[j][i+4]-max[i]); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | vector<vec> prsti; |
---|
186 | int m,n,p; |
---|
187 | for(p=0;p<115;p++) |
---|
188 | { |
---|
189 | vec k; |
---|
190 | k.ins(0,1/115.); //log |
---|
191 | prsti.push_back(k); |
---|
192 | } |
---|
193 | // v ramci riadku v poli norm su hodnoty pre jednu hypotezu v roznych casoch, pocitanie pravdepodobnosti z norm. faktorov |
---|
194 | for (m=0;m<norm[1].size()-4;m++) |
---|
195 | { double k=sumastlpec(m,norm,prsti); |
---|
196 | for(n=0;n < norm.size();n++) |
---|
197 | { |
---|
198 | prsti[n].ins(prsti[n].size(),norm[n][m+4]*prsti[n][m]/k); |
---|
199 | //prsti[n].ins(prsti[n].size(),norm[n][m+4]+prsti[n][m]-k); |
---|
200 | } |
---|
201 | } |
---|
202 | ofstream file; |
---|
203 | |
---|
204 | file.open(outfile_strings[a]); //"prsti_hypot.txt",ios::app); |
---|
205 | for(int i=0;i < prsti.size();i++) |
---|
206 | { |
---|
207 | file << norm[i][0] <<" "<<norm[i][1]<<" "<<norm[i][2]<<" "<<norm[i][3]<<" "; |
---|
208 | for(int j=0;j < prsti[i].size();j++) |
---|
209 | { |
---|
210 | if(j!=prsti[i].size()-1) |
---|
211 | { |
---|
212 | file << prsti[i][j]<<" "; //exp |
---|
213 | }else |
---|
214 | { |
---|
215 | file<<prsti[i][j]<<endl; //exp |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | file<<endl; |
---|
220 | file.close(); |
---|
221 | //} |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | |
---|