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