Changeset 1347 for applications/trading_models
- Timestamp:
- 05/02/11 19:27:53 (14 years ago)
- Location:
- applications/trading_models
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/trading_models/CMakeLists.txt
r1260 r1347 7 7 SET(BDM_SOURCE_DIR "${CMAKE_SOURCE_DIR}/../../library") 8 8 SET(CMAKE_MODULE_PATH "${BDM_SOURCE_DIR}/system") 9 SET(ROBUST_DIR "${CMAKE_SOURCE_DIR}/../robust") 9 10 10 11 # Load BDM environment … … 14 15 link_directories (${BDM_SOURCE_DIR}/bdm) 15 16 17 include_directories(${ROBUST_DIR}) 18 link_directories(${ROBUST_DIR}) 19 16 20 add_library(trading_models_lib trading_models_lib.cpp trading_models_lib.h) 21 add_library(robustlib ../robust/robustlib.cpp ../robust/robustlib.h) 17 22 18 EXEC(trading_models_main trading_models_lib )23 EXEC(trading_models_main trading_models_lib robustlib) 19 24 20 25 -
applications/trading_models/trading_models_lib.h
r1260 r1347 5 5 */ 6 6 7 #ifndef EXAMPLE_LIB_H8 #define EXAMPLE_LIB_H7 #ifndef trading_models_LIB_H 8 #define trading_models_LIB_H 9 9 10 10 #include <stat/exp_family.h> … … 12 12 using namespace bdm; 13 13 using namespace std; 14 using namespace itpp; 14 15 15 16 class my_pdf: public egamma { … … 18 19 }; 19 20 20 21 21 22 //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density 22 23 class my_BM : public BM{ -
applications/trading_models/trading_models_main.cpp
r1338 r1347 8 8 9 9 //<<<<<<< .mine 10 #include "robustlib.h" 10 11 #include "trading_models_lib.h" 11 12 #include <iostream> 12 13 #include "estim/arx.h" 14 13 15 //======= 16 //#include "application/robust/robustlib.h" ma tu byt aj daka cesta k tomu suboru? 14 17 #include "trading_models_lib.h" 15 18 #include "estim/arx.h" … … 23 26 using namespace bdm; 24 27 25 /* 26 string cislo(string s) // tato procedura spracova tie cisla z toho textoveho suboru tak aby sa odstranilo to e-001 a boli v spravnom 27 //formate. Lebo predtym to vyhadyovalo divne cisla, nevedel som ci je to zato, ze bayes si stym neporadi, ale yjavne nepomohlo 28 { 29 int b=s.find('e'); 30 string t=s.substr(0,b); 31 string poz=s.substr(b+1,s.size()); 32 33 istringstream g(poz); 34 int pozc; 35 g>>pozc; 36 istringstream k(t); 37 double csl; 38 k>>csl; 39 if (pozc<0) //pozc moze byt niekedy aj kladne 28 double sumastlpec(int k,vector<vec> pole,vector<vec> pravd) { 29 double r=0; 30 for (int i=0;i < pole.size();i++) 40 31 { 41 for(int i=1;i<=-pozc;i++) 42 csl/=10; 43 } else 44 { 45 for(int j=1;j<=pozc;j++) 46 csl*=10; 32 r+=pole[i][k+4]*pravd[i][k]; 33 //r=r+exp(pole[i][k+4]-m[k+4]+pravd[i][k]); 47 34 } 35 //cout <<"r"<< log(r); 36 return r; //log 37 } 38 39 int main () { 48 40 49 ostringstream o; 50 o<<csl; 51 return o.str(); 52 } */ 53 54 double sumastlpec(int k,vector<vec> pole,vector<vec> pravd) { //robi sumu k-teho stlpca, pouzivam na konci pri ratani pravdepodobnosti 55 double r=0; 56 for (int i=0;i<pole.size();i++) 57 { 58 r+=pole[i][k]*pravd[i][k]; 59 } 60 return r; 61 } 62 63 int main () { 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; 64 46 vector<vector<string>> ADdata; //nacitavanie dat do pola ADdata -funguje spravne 65 ifstream myfile( "C:\\AD_dataupravene.txt");47 ifstream myfile(file_strings[a]); //"C:\\Users\\Peto\\Desktop\\PROG-BAK\\DATA-Mathematika\\CL_.txt"); 66 48 if (myfile.is_open()) 67 49 { … … 82 64 } 83 65 myfile.close(); //konec nacitavania dat 84 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 }*/ 85 84 vector<vec> norm; //do norm zapisujeme normalizacne faktory 86 85 for (int h=1;h<=2;h++) //cyklus ktory ovplzvnuje konstantu h=1- model s konstantou, h=2, bez konstanty … … 94 93 while (g<=4) //cyklus co meni rozmery matice V 95 94 { 96 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?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? 97 96 98 97 int p=0; … … 107 106 { 108 107 ARX Ar; 108 RARX RAr(g+h-3,0,b); 109 109 Ar.set_statistics ( 1, V0 ); //nu is default (set to have finite moments) 110 110 Ar.set_constant ( b ); … … 116 116 vec predikce; 117 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(0,ADdata[j][k+p]);//zmena i -> j aby to bralo regresory z roznych riadkov, ak p=1 bereme data z toho isteho casu 120 121 cout << "Pred:" << predikce << ", "; 122 cout << "Cond:" << condition << endl; 123 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)); 124 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) 125 123 pomocka.ins(pomocka.size(),Ar.posterior().lognc()); //nie je tu exponenciala! -aby to bolo mensie 126 } 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; 127 131 norm.push_back(pomocka); 128 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 … … 154 158 } 155 159 } 156 157 /* //tu je to povodne 158 mat V0 = 0.0001 * eye ( 2 ); //pre pripad samotnej konstanty 159 ARX Ar; 160 Ar.set_statistics ( 1, V0 ); //nu is default (set to have finite moments) 161 Ar.set_constant ( true ); 162 Ar.validate(); // forgetting is default: 1.0 163 vector<double> pom1; 164 for(int k = 0;k<140;k++) //prechadyame "po riadkoch" 165 { 166 vec predikce; 167 vec cond; 168 cond.ins(0,ADdata[3][3]); 169 predikce.ins(0,ADdata[3][k+2]); //predpovede nacitavame a zadavame do Bayes zvlast 170 Ar.bayes(predikce,cond.right(0)); 171 pom1.push_back(exp(Ar.posterior().lognc())); 172 } 173 norm.push_back(pom1); 174 175 for (int a=2;a<=3;a++) //AR(1) bez a potom s konstantou 176 { 177 bool b=false; //b pouzivame pri set_constant 178 if(a==3) 179 b=true; 180 mat V0 = 0.0001 * eye ( a ); //pre pripad samotnej konstanty 181 182 for (int p=0;p < ADdata.size();p++) 183 { 184 ARX Ar; 185 Ar.set_statistics ( 1, V0 ); //nu is default (set to have finite moments) 186 Ar.set_constant ( b ); 187 Ar.validate(); // forgetting is default: 1.0 188 vector<double> pom1; 189 for(int k = 0;k<140;k++) //prechadyame "po riadkoch" 190 { 191 vec predikce; 192 vec condition; 193 condition.ins(0,ADdata[p][k]); 194 predikce.ins(0,ADdata[3][k+1]); //predpovede nacitavame a zadavame do Bayes zvlast 195 Ar.bayes(predikce,condition); 196 pom1.push_back(exp(Ar.posterior().lognc())); 197 } 198 norm.push_back(pom1); //normalizacne faktory pre urcitu kombinaciu regresorov(teda po kazdom riadku) ulozi do pola norm 199 } 200 } 201 202 203 for (int g=3;g<=4;g++) //tento cyklus je az do konca, raz to robime s konstantou, raz bez. 204 { 205 bool b; //b pouzivame pri set_constant 206 if(g==3) 207 b=false; 208 else 209 b=true; 210 mat V0 = 0.0001 * eye ( g ); 211 for(int i = 0;i < ADdata.size();i++) //po pocet riadkov, co bz malo byt 6 212 { 213 for(int j = i+1; j<ADdata.size();j++) 214 { 215 ARX Ar; 216 Ar.set_statistics ( 1, V0 ); //nu is default (set to have finite moments) 217 Ar.set_constant ( b ); 218 Ar.validate(); 219 // forgetting is default: 1.0 220 vector<double> pomocka; 221 for(int k = 0;k<140;k++) //prechadyame "po riadkoch" 222 { 223 vec condition; 224 vec predikce; 225 predikce.ins(0,ADdata[3][k+1]); //predpovede nacitavame a zadavame do Bayes zvlast 226 condition.ins(0,ADdata[i][k]); 227 condition.ins(0,ADdata[j][k]);//zmena i -> j qby to bralo regresory z roznych riadkov 228 Ar.bayes(predikce,condition); 229 pomocka.push_back(exp(Ar.posterior().lognc())); 230 } 231 norm.push_back(pomocka); 232 } 233 } 234 for(int i = 0;i < ADdata.size();i++) //po pocet riadkov, co bz malo byt 6 235 { 236 for(int j = 0; j<ADdata.size();j++) 237 { 238 ARX Ar; 239 Ar.set_statistics ( 1, V0 ); //nu is default (set to have finite moments) 240 Ar.set_constant ( b ); 241 Ar.validate(); 242 // forgetting is default: 1.0 243 vector<double> pomocka; 244 for(int k = 0;k<140;k++) //prechadyame "po riadkoch" 245 { 246 vec condition; 247 vec predikce; 248 predikce.ins(0,ADdata[3][k+2]); //predpovede nacitavame a zadavame do Bayes zvlast 249 condition.ins(0,ADdata[i][k]); 250 251 condition.ins(0,ADdata[j][k+1]);//zmena i -> j qby to bralo regresory z roznych riadkov 252 Ar.bayes(predikce,condition); 253 pomocka.push_back(Ar.posterior().lognc()); 254 } 255 256 norm.push_back(pomocka); 257 } 258 } 259 }*/ 260 vector<vec> prsti; //hypotez je 85 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; 261 186 int m,n,p; 262 for(p=0;p<115;p++) //inicializuem apriorne pravdepodobnosti187 for(p=0;p<115;p++) 263 188 { 264 189 vec k; 265 k.ins(0,1/115.); 190 k.ins(0,1/115.); //log 266 191 prsti.push_back(k); 267 192 } 268 193 // v ramci riadku v poli norm su hodnoty pre jednu hypotezu v roznych casoch, pocitanie pravdepodobnosti z norm. faktorov 269 for (m=0;m<norm[1].size() ;m++)194 for (m=0;m<norm[1].size()-4;m++) 270 195 { double k=sumastlpec(m,norm,prsti); 271 196 for(n=0;n < norm.size();n++) 272 197 { 273 prsti[n].ins(prsti[n].size(),norm[n][m]*prsti[n][m]/k); 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); 274 200 } 275 201 } 276 ofstream file; //zapis pravdepodobnosti do suboru 277 file.open("prsti_hypot.txt"); 202 ofstream file; 203 204 file.open(outfile_strings[a]); //"prsti_hypot.txt",ios::app); 278 205 for(int i=0;i < prsti.size();i++) 279 206 { 207 file << norm[i][0] <<" "<<norm[i][1]<<" "<<norm[i][2]<<" "<<norm[i][3]<<" "; 280 208 for(int j=0;j < prsti[i].size();j++) 281 209 { 282 210 if(j!=prsti[i].size()-1) 283 211 { 284 file << prsti[i][j]<<" "; 212 file << prsti[i][j]<<" "; //exp 285 213 }else 286 214 { 287 file<<prsti[i][j]<<endl; 215 file<<prsti[i][j]<<endl; //exp 288 216 } 289 217 } … … 291 219 file<<endl; 292 220 file.close(); 293 221 //} 294 222 } 295 223