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