[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> |
---|
[1376] | 14 | //#include <itpp/itsignal.h> |
---|
[1361] | 15 | #include "windows.h" |
---|
| 16 | #include "ddeml.h" |
---|
| 17 | #include "stdio.h" |
---|
[1412] | 18 | #include <dirent.h> |
---|
| 19 | //#include <itpp/itoptim.h> |
---|
[1282] | 20 | |
---|
[1361] | 21 | //#include "DDEClient.h" |
---|
| 22 | //#include <conio.h> |
---|
[1358] | 23 | |
---|
[1361] | 24 | |
---|
[1208] | 25 | using namespace itpp; |
---|
[1337] | 26 | using namespace bdm; |
---|
[976] | 27 | |
---|
[1361] | 28 | //const int emlig_size = 2; |
---|
| 29 | //const int utility_constant = 5; |
---|
[1268] | 30 | |
---|
[1412] | 31 | const int max_model_order = 2; |
---|
| 32 | const double apriorno = 0.01; |
---|
[1423] | 33 | const int max_window_size = 40; |
---|
[1412] | 34 | const int utility_order = 25; |
---|
| 35 | const int prediction_time = 30; |
---|
| 36 | const double min_utility_argument = 0.001; |
---|
[1423] | 37 | const double max_investment = 3.0; |
---|
[1413] | 38 | const int sample_size = 5000; |
---|
[1423] | 39 | const char* commodity = "TY\\"; |
---|
[1272] | 40 | |
---|
[1405] | 41 | /* |
---|
| 42 | HDDEDATA CALLBACK DdeCallback( |
---|
| 43 | UINT uType, // Transaction type. |
---|
| 44 | UINT uFmt, // Clipboard data format. |
---|
| 45 | HCONV hconv, // Handle to the conversation. |
---|
| 46 | HSZ hsz1, // Handle to a string. |
---|
| 47 | HSZ hsz2, // Handle to a string. |
---|
| 48 | HDDEDATA hdata, // Handle to a global memory object. |
---|
| 49 | DWORD dwData1, // Transaction-specific data. |
---|
| 50 | DWORD dwData2) // Transaction-specific data. |
---|
| 51 | { |
---|
| 52 | return 0; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void DDERequest(DWORD idInst, HCONV hConv, char* szItem) |
---|
| 56 | { |
---|
| 57 | HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0); |
---|
| 58 | HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT, |
---|
| 59 | XTYP_ADVSTART,TIMEOUT_ASYNC , NULL); //TIMEOUT_ASYNC |
---|
| 60 | if (hData==NULL) |
---|
| 61 | { |
---|
| 62 | printf("Request failed: %s\n", szItem); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if (hData==0) |
---|
| 66 | { |
---|
| 67 | printf("Request failed: %s\n", szItem); |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | DWORD WINAPI ThrdFunc( LPVOID n ) |
---|
| 72 | { |
---|
| 73 | return 0; |
---|
| 74 | } |
---|
| 75 | */ |
---|
[1412] | 76 | vector<char*> listFiles(char* dir){ |
---|
| 77 | vector<char*> files; |
---|
| 78 | DIR *pDIR; |
---|
| 79 | struct dirent *entry; |
---|
| 80 | if( pDIR=opendir(dir) ){ |
---|
| 81 | while(entry = readdir(pDIR)){ |
---|
| 82 | if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 ) |
---|
| 83 | { |
---|
| 84 | char *file_string = new char[strlen(entry->d_name) + 1]; |
---|
| 85 | strcpy(file_string, entry->d_name); |
---|
| 86 | files.push_back(file_string); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | closedir(pDIR); |
---|
| 90 | } |
---|
| 91 | return files; |
---|
| 92 | } |
---|
[1405] | 93 | |
---|
[1413] | 94 | double valueCRRAUtility(const double &position, const pair<vec,vec> &samples, const int order) |
---|
[1406] | 95 | { |
---|
| 96 | double value = 0; |
---|
[1413] | 97 | set<double> values; |
---|
[1406] | 98 | |
---|
[1413] | 99 | for(int i=0;i<samples.first.length();i++) |
---|
[1406] | 100 | { |
---|
[1413] | 101 | double sample = samples.second.get(i); |
---|
| 102 | double probability = samples.first.get(i); |
---|
[1412] | 103 | if((position*sample+1)>min_utility_argument) |
---|
| 104 | { |
---|
[1413] | 105 | values.insert(probability*sample/pow(position*sample+1,order+1)); |
---|
[1412] | 106 | } |
---|
| 107 | else |
---|
| 108 | { |
---|
[1413] | 109 | values.insert(probability*(min_utility_argument-1)/position/pow(min_utility_argument,order+1)); |
---|
[1412] | 110 | } |
---|
[1406] | 111 | } |
---|
| 112 | |
---|
[1413] | 113 | for(set<double>::iterator val_ref = values.begin();val_ref!=values.end();val_ref++) |
---|
| 114 | { |
---|
| 115 | value+=(*val_ref); |
---|
| 116 | } |
---|
| 117 | |
---|
[1406] | 118 | return value; |
---|
| 119 | } |
---|
| 120 | |
---|
[1413] | 121 | double gradientCRRAUtility(const double &position, const pair<vec,vec> &samples, const int order) |
---|
[1406] | 122 | { |
---|
| 123 | double value = 0; |
---|
[1413] | 124 | set<double> values; |
---|
| 125 | |
---|
| 126 | for(int i=0;i<samples.first.length();i++) |
---|
[1406] | 127 | { |
---|
[1413] | 128 | double sample = samples.second.get(i); |
---|
| 129 | double probability = samples.first.get(i); |
---|
[1412] | 130 | if((position*sample+1)>min_utility_argument) |
---|
| 131 | { |
---|
[1413] | 132 | values.insert((-(order+1)*pow(sample,2)*probability)/pow(position*sample+1,order+2)); |
---|
[1412] | 133 | } |
---|
[1406] | 134 | } |
---|
| 135 | |
---|
[1413] | 136 | for(set<double>::iterator val_ref = values.begin();val_ref!=values.end();val_ref++) |
---|
| 137 | { |
---|
| 138 | value+=(*val_ref); |
---|
| 139 | } |
---|
| 140 | |
---|
[1406] | 141 | return value; |
---|
| 142 | } |
---|
| 143 | |
---|
[1413] | 144 | double newtonRaphson(double startingPoint, double epsilon, pair<vec,vec> samples, int order) |
---|
[1406] | 145 | { |
---|
[1413] | 146 | /* |
---|
[1406] | 147 | if(samples.length()>800) |
---|
| 148 | { |
---|
| 149 | samples.del(801,samples.size()-1); |
---|
| 150 | } |
---|
[1413] | 151 | */ |
---|
[1406] | 152 | |
---|
| 153 | int count = 0; |
---|
| 154 | |
---|
| 155 | bool epsilon_reached = false; |
---|
| 156 | |
---|
| 157 | while(count<1000&&!epsilon_reached) |
---|
| 158 | { |
---|
| 159 | double cur_value = valueCRRAUtility(startingPoint,samples,order); |
---|
| 160 | double cur_gradient = gradientCRRAUtility(startingPoint,samples,order); |
---|
| 161 | |
---|
| 162 | startingPoint = startingPoint - cur_value/cur_gradient; |
---|
| 163 | |
---|
| 164 | if(cur_value<epsilon) |
---|
| 165 | { |
---|
| 166 | epsilon_reached = true; |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | if(count==100) |
---|
| 171 | { |
---|
| 172 | return startingPoint; // can be different! |
---|
| 173 | } |
---|
| 174 | else |
---|
| 175 | { |
---|
| 176 | return startingPoint; |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
[1361] | 180 | class model |
---|
| 181 | { |
---|
| 182 | public: |
---|
[1376] | 183 | set<pair<int,int>> ar_components; |
---|
[1358] | 184 | |
---|
[1361] | 185 | // Best thing would be to inherit the two models from a single souce, this is planned, but now structurally |
---|
| 186 | // problematic. |
---|
[1376] | 187 | RARX* my_rarx; //vzmenovane parametre pre triedu model |
---|
[1379] | 188 | ARXwin* my_arx; |
---|
[1361] | 189 | |
---|
| 190 | bool has_constant; |
---|
[1376] | 191 | int window_size; //musi byt vacsia ako pocet krokov ak to nema ovplyvnit |
---|
[1361] | 192 | int predicted_channel; |
---|
| 193 | mat* data_matrix; |
---|
[1383] | 194 | vec predictions; |
---|
[1393] | 195 | char name[80]; |
---|
[1405] | 196 | |
---|
| 197 | double previous_lognc; |
---|
[1361] | 198 | |
---|
[1376] | 199 | model(set<pair<int,int>> ar_components, //funkcie treidz model-konstruktor |
---|
[1361] | 200 | bool robust, |
---|
| 201 | bool has_constant, |
---|
| 202 | int window_size, |
---|
[1376] | 203 | int predicted_channel, |
---|
[1361] | 204 | mat* data_matrix) |
---|
[1405] | 205 | { |
---|
[1376] | 206 | this->ar_components.insert(ar_components.begin(),ar_components.end()); |
---|
[1393] | 207 | |
---|
| 208 | strcpy(name,"M"); |
---|
| 209 | |
---|
| 210 | for(set<pair<int,int>>::iterator ar_ref = ar_components.begin();ar_ref!=ar_components.end();ar_ref++) |
---|
| 211 | { |
---|
| 212 | char buffer1[2]; |
---|
| 213 | char buffer2[2]; |
---|
| 214 | itoa((*ar_ref).first,buffer1,10); |
---|
| 215 | itoa((*ar_ref).second,buffer2,10); |
---|
| 216 | |
---|
| 217 | strcat(name,buffer1); |
---|
| 218 | strcat(name,buffer2); |
---|
| 219 | strcat(name,"_"); |
---|
| 220 | } |
---|
| 221 | |
---|
[1376] | 222 | this->has_constant = has_constant; |
---|
[1393] | 223 | |
---|
| 224 | if(has_constant) |
---|
| 225 | { |
---|
| 226 | strcat(name,"C"); |
---|
| 227 | } |
---|
| 228 | |
---|
[1376] | 229 | this->window_size = window_size; |
---|
| 230 | this->predicted_channel = predicted_channel; |
---|
| 231 | this->data_matrix = data_matrix; |
---|
[1361] | 232 | |
---|
| 233 | if(robust) |
---|
| 234 | { |
---|
[1405] | 235 | previous_lognc = 0; |
---|
[1393] | 236 | strcat(name,"R"); |
---|
| 237 | |
---|
[1361] | 238 | if(has_constant) |
---|
| 239 | { |
---|
[1405] | 240 | my_rarx = new RARX(ar_components.size()+1,window_size,true,sqrt(2*apriorno),sqrt(2*apriorno),ar_components.size()+3); |
---|
[1361] | 241 | my_arx = NULL; |
---|
| 242 | } |
---|
[1376] | 243 | else |
---|
[1361] | 244 | { |
---|
[1405] | 245 | my_rarx = new RARX(ar_components.size(),window_size,false,sqrt(2*apriorno),sqrt(2*apriorno),ar_components.size()+2); |
---|
[1361] | 246 | my_arx = NULL; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | else |
---|
| 250 | { |
---|
| 251 | my_rarx = NULL; |
---|
[1379] | 252 | my_arx = new ARXwin(); |
---|
[1361] | 253 | mat V0; |
---|
| 254 | |
---|
| 255 | if(has_constant) |
---|
| 256 | { |
---|
[1376] | 257 | V0 = apriorno * eye(ar_components.size()+2); //aj tu konst |
---|
[1405] | 258 | V0(0,0) = 0; |
---|
| 259 | my_arx->set_constant(true); |
---|
| 260 | my_arx->set_statistics(1, V0, V0.rows()+1); |
---|
[1361] | 261 | } |
---|
| 262 | else |
---|
| 263 | { |
---|
| 264 | |
---|
[1376] | 265 | V0 = apriorno * eye(ar_components.size()+1);//menit konstantu |
---|
[1405] | 266 | V0(0,0) = 0; |
---|
[1396] | 267 | //V0(0,1) = -0.01; |
---|
| 268 | //V0(1,0) = -0.01; |
---|
[1405] | 269 | my_arx->set_constant(false); |
---|
| 270 | my_arx->set_statistics(1, V0, V0.rows()+1); |
---|
[1361] | 271 | |
---|
[1405] | 272 | } |
---|
| 273 | |
---|
[1379] | 274 | my_arx->set_parameters(window_size); |
---|
[1361] | 275 | my_arx->validate(); |
---|
[1396] | 276 | |
---|
[1405] | 277 | previous_lognc = my_arx->posterior().lognc(); |
---|
| 278 | |
---|
| 279 | /* |
---|
[1396] | 280 | vec mean = my_arx->posterior().mean(); |
---|
| 281 | cout << mean << endl; |
---|
[1405] | 282 | */ |
---|
[1361] | 283 | } |
---|
[1358] | 284 | } |
---|
[1361] | 285 | |
---|
[1412] | 286 | ~model() |
---|
| 287 | { |
---|
| 288 | if(my_rarx!=NULL) |
---|
| 289 | { |
---|
| 290 | delete my_rarx; |
---|
| 291 | } |
---|
| 292 | else |
---|
| 293 | { |
---|
| 294 | delete my_arx; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
[1405] | 298 | void data_update(int time) |
---|
[1358] | 299 | { |
---|
[1376] | 300 | vec data_vector; |
---|
| 301 | for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++) |
---|
[1401] | 302 | { |
---|
[1376] | 303 | data_vector.ins(data_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second)); |
---|
[1358] | 304 | } |
---|
[1376] | 305 | if(my_rarx!=NULL) |
---|
[1401] | 306 | { |
---|
[1376] | 307 | data_vector.ins(0,(*data_matrix).get(predicted_channel,time)); |
---|
| 308 | my_rarx->bayes(data_vector); |
---|
| 309 | } |
---|
[1358] | 310 | else |
---|
| 311 | { |
---|
[1401] | 312 | vec pred_vec; |
---|
[1376] | 313 | pred_vec.ins(0,(*data_matrix).get(predicted_channel,time)); |
---|
| 314 | my_arx->bayes(pred_vec,data_vector); |
---|
[1361] | 315 | } |
---|
| 316 | } |
---|
| 317 | |
---|
[1376] | 318 | pair<vec,vec> predict(int sample_size, int time, itpp::Laplace_RNG* LapRNG) //nerozumiem, ale vraj to netreba, nepouziva to |
---|
[1367] | 319 | { |
---|
[1376] | 320 | vec condition_vector; |
---|
| 321 | for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++) |
---|
[1367] | 322 | { |
---|
[1376] | 323 | condition_vector.ins(condition_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second+1)); |
---|
| 324 | } |
---|
[1367] | 325 | |
---|
[1376] | 326 | if(my_rarx!=NULL) |
---|
| 327 | { |
---|
[1413] | 328 | pair<vec,mat> imp_samples = my_rarx->posterior->sample(sample_size,false); |
---|
[1367] | 329 | |
---|
[1393] | 330 | //cout << imp_samples.first << endl; |
---|
[1376] | 331 | |
---|
| 332 | vec sample_prediction; |
---|
[1405] | 333 | for(int t = 0;t<imp_samples.second.cols();t++) |
---|
[1367] | 334 | { |
---|
[1376] | 335 | vec lap_sample = condition_vector; |
---|
[1367] | 336 | |
---|
[1376] | 337 | if(has_constant) |
---|
[1367] | 338 | { |
---|
[1376] | 339 | lap_sample.ins(lap_sample.size(),1.0); |
---|
[1367] | 340 | } |
---|
[1376] | 341 | |
---|
[1393] | 342 | lap_sample.ins(lap_sample.size(),(*LapRNG)()); |
---|
[1367] | 343 | |
---|
[1376] | 344 | sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t)); |
---|
[1367] | 345 | } |
---|
| 346 | |
---|
[1376] | 347 | return pair<vec,vec>(imp_samples.first,sample_prediction); |
---|
[1393] | 348 | } |
---|
[1376] | 349 | else |
---|
| 350 | { |
---|
[1383] | 351 | mat samples = my_arx->posterior().sample_mat(sample_size); |
---|
[1376] | 352 | |
---|
| 353 | vec sample_prediction; |
---|
| 354 | for(int t = 0;t<sample_size;t++) |
---|
| 355 | { |
---|
| 356 | vec gau_sample = condition_vector; |
---|
[1367] | 357 | |
---|
[1376] | 358 | if(has_constant) |
---|
[1367] | 359 | { |
---|
[1376] | 360 | gau_sample.ins(gau_sample.size(),1.0); |
---|
[1367] | 361 | } |
---|
[1376] | 362 | |
---|
[1383] | 363 | gau_sample.ins(gau_sample.size(),randn()); |
---|
[1367] | 364 | |
---|
[1412] | 365 | vec param_sample = samples.get_col(t); |
---|
| 366 | param_sample.set(param_sample.size()-1,sqrt(param_sample[param_sample.size()-1])); |
---|
| 367 | sample_prediction.ins(0,gau_sample*param_sample); |
---|
[1367] | 368 | } |
---|
[1376] | 369 | |
---|
| 370 | return pair<vec,vec>(ones(sample_prediction.size()),sample_prediction); |
---|
[1367] | 371 | } |
---|
| 372 | |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | |
---|
[1376] | 376 | static set<set<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels) |
---|
[1361] | 377 | { |
---|
[1376] | 378 | set<set<pair<int,int>>> created_model_types; |
---|
[1361] | 379 | |
---|
[1405] | 380 | if(max_order == 1)//ukoncovacia vetva |
---|
[1361] | 381 | { |
---|
[1405] | 382 | for(int channel = 0;channel<number_of_channels;channel++)//pre AR 1 model vytvori kombinace kanalov v prvom kroku poyadu |
---|
[1358] | 383 | { |
---|
[1376] | 384 | set<pair<int,int>> returned_type; |
---|
[1405] | 385 | returned_type.insert(pair<int,int>(channel,1)); //?? |
---|
[1376] | 386 | created_model_types.insert(returned_type); |
---|
[1358] | 387 | } |
---|
[1361] | 388 | |
---|
| 389 | return created_model_types; |
---|
| 390 | } |
---|
| 391 | else |
---|
| 392 | { |
---|
[1405] | 393 | created_model_types = possible_models_recurse(max_order-1,number_of_channels);//tu uz mame ulozene kombinace o jeden krok dozadu //rekuryivne volanie |
---|
[1376] | 394 | set<set<pair<int,int>>> returned_types; |
---|
[1361] | 395 | |
---|
[1376] | 396 | for(set<set<pair<int,int>>>::iterator model_ref = created_model_types.begin();model_ref!=created_model_types.end();model_ref++) |
---|
[1361] | 397 | { |
---|
| 398 | |
---|
| 399 | for(int order = 1; order<=max_order; order++) |
---|
[1358] | 400 | { |
---|
[1361] | 401 | for(int channel = 0;channel<number_of_channels;channel++) |
---|
| 402 | { |
---|
[1376] | 403 | set<pair<int,int>> returned_type; |
---|
[1405] | 404 | pair<int,int> new_pair = pair<int,int>(channel,order);//?? |
---|
| 405 | if(find((*model_ref).begin(),(*model_ref).end(),new_pair)==(*model_ref).end()) //?? |
---|
[1361] | 406 | { |
---|
[1405] | 407 | returned_type.insert((*model_ref).begin(),(*model_ref).end()); //co vlozi na zaciatok retuned_type? |
---|
| 408 | returned_type.insert(new_pair); |
---|
[1376] | 409 | |
---|
| 410 | returned_types.insert(returned_type); |
---|
[1361] | 411 | } |
---|
| 412 | } |
---|
[1358] | 413 | } |
---|
| 414 | } |
---|
[1361] | 415 | |
---|
[1376] | 416 | created_model_types.insert(returned_types.begin(),returned_types.end()); |
---|
[1361] | 417 | |
---|
| 418 | return created_model_types; |
---|
| 419 | } |
---|
[1358] | 420 | } |
---|
[1361] | 421 | }; |
---|
| 422 | |
---|
[1412] | 423 | // **************************************************** |
---|
| 424 | // MAIN MAIN MAIN MAIN MAIN |
---|
| 425 | // **************************************************** |
---|
[1383] | 426 | int main ( int argc, char* argv[] ) |
---|
| 427 | { |
---|
[1405] | 428 | |
---|
| 429 | // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from |
---|
| 430 | // 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 |
---|
| 431 | // can be compared to the classical setup. |
---|
| 432 | |
---|
[1412] | 433 | itpp::Laplace_RNG LapRNG = Laplace_RNG(); |
---|
[1301] | 434 | |
---|
[1412] | 435 | char* folder_string = "C:\\RobustExperiments\\"; // "C:\\dataADClosePercDiff"; // |
---|
| 436 | char* data_folder = "data\\"; |
---|
| 437 | char* results_folder = "results\\"; |
---|
[1301] | 438 | |
---|
[1412] | 439 | char dfstring[150]; |
---|
| 440 | strcpy(dfstring,folder_string); |
---|
| 441 | strcat(dfstring,data_folder); |
---|
| 442 | strcat(dfstring,commodity); |
---|
| 443 | vector<char*> files = listFiles(dfstring); |
---|
[1376] | 444 | |
---|
[1412] | 445 | for(int contract=0;contract<files.size();contract++) |
---|
| 446 | { |
---|
| 447 | char *cdf_str = new char[strlen(dfstring) + 1]; |
---|
| 448 | strcpy(cdf_str, dfstring); |
---|
| 449 | strcat(cdf_str,files[contract]); |
---|
[1376] | 450 | |
---|
[1412] | 451 | mat data_matrix; |
---|
| 452 | ifstream myfile(cdf_str); |
---|
| 453 | if (myfile.is_open()) |
---|
| 454 | { |
---|
| 455 | string line; |
---|
| 456 | while(getline(myfile,line)) |
---|
| 457 | { |
---|
| 458 | vec data_vector; |
---|
| 459 | while(line.find(',') != string::npos) //zmenil som ciarku za medzeru |
---|
| 460 | { |
---|
| 461 | //line.erase(0,1); // toto som sem pridal |
---|
| 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())); |
---|
| 465 | line.erase(0,loc+1); |
---|
| 466 | } |
---|
[1301] | 467 | |
---|
[1412] | 468 | data_matrix.ins_row(data_matrix.rows(),data_vector); |
---|
| 469 | } |
---|
[1361] | 470 | |
---|
[1412] | 471 | myfile.close(); |
---|
| 472 | } |
---|
| 473 | else |
---|
[1405] | 474 | { |
---|
[1412] | 475 | cout << "Can't open data file!" << endl; |
---|
[1405] | 476 | } |
---|
[1412] | 477 | |
---|
| 478 | //konec nacitavania dat |
---|
| 479 | set<set<pair<int,int>>> model_types = model::possible_models_recurse(max_model_order,data_matrix.rows()); //volanie funkce kde robi kombinace modelov |
---|
| 480 | //to priradime do model_types, data_matrix.row urcuje pocet kanalov dat |
---|
| 481 | vector<model*> models; |
---|
| 482 | for(set<set<pair<int,int>>>::iterator model_type = model_types.begin();model_type!=model_types.end();model_type++) |
---|
| 483 | {// prechadza rozne typy kanalov, a poctu regresorov |
---|
| 484 | for(int window_size = max_window_size-1;window_size < max_window_size;window_size++) |
---|
| 485 | { |
---|
[1423] | 486 | //if(model_type->size()<max_model_order) |
---|
| 487 | //{ |
---|
| 488 | models.push_back(new model((*model_type),true,true,window_size,0,&data_matrix)); // to su len konstruktory, len inicializujeme rozne typy |
---|
| 489 | models.push_back(new model((*model_type),false,true,window_size,0,&data_matrix)); |
---|
| 490 | //} |
---|
[1412] | 491 | models.push_back(new model((*model_type),true,false,window_size,0,&data_matrix)); |
---|
| 492 | models.push_back(new model((*model_type),false,false,window_size,0,&data_matrix)); |
---|
| 493 | } |
---|
[1365] | 494 | |
---|
[1412] | 495 | //set<pair<int,int>> empty_list; |
---|
| 496 | //models.push_back(new model(empty_list,false,true,100,0,&data_matrix)); |
---|
| 497 | } |
---|
[1405] | 498 | |
---|
[1412] | 499 | mat result_lognc; |
---|
| 500 | // mat result_preds; |
---|
[1376] | 501 | |
---|
[1412] | 502 | ofstream myfilew; |
---|
| 503 | char rfstring[150]; |
---|
| 504 | strcpy(rfstring,folder_string); |
---|
| 505 | strcat(rfstring,results_folder); |
---|
| 506 | strcat(rfstring,commodity); |
---|
| 507 | strcat(rfstring,files[contract]); |
---|
[1401] | 508 | |
---|
[1405] | 509 | /* |
---|
[1412] | 510 | char predstring[150]; |
---|
| 511 | strcpy(predstring,folder_string); |
---|
| 512 | strcat(predstring,results_folder); |
---|
| 513 | strcat(predstring,commodity); |
---|
| 514 | strcat(predstring,"PRED"); |
---|
| 515 | strcat(predstring,files[contract]); |
---|
| 516 | */ |
---|
| 517 | |
---|
| 518 | for(int time = max_model_order;time<data_matrix.cols();time++) //time<data_matrix.cols() |
---|
| 519 | { |
---|
| 520 | cout << "Steps: " << time << endl; |
---|
[1405] | 521 | |
---|
[1412] | 522 | if(time == max_model_order) |
---|
| 523 | { |
---|
[1413] | 524 | /* |
---|
[1412] | 525 | myfilew.open(rfstring,ios::app); |
---|
[1413] | 526 | for(int i = 0;models.size();i++) |
---|
[1412] | 527 | { |
---|
| 528 | for(set<pair<int,int>>::iterator ar_ref = models[i]->ar_components.begin();ar_ref != models[i]->ar_components.end();ar_ref++) |
---|
| 529 | { |
---|
| 530 | myfilew << (*ar_ref).second << (*ar_ref).first; |
---|
| 531 | } |
---|
[1383] | 532 | |
---|
[1412] | 533 | myfilew << "."; |
---|
[1405] | 534 | |
---|
[1412] | 535 | if(models[i]->my_arx == NULL) |
---|
| 536 | { |
---|
| 537 | myfilew << "31"; |
---|
| 538 | } |
---|
| 539 | else |
---|
| 540 | { |
---|
| 541 | myfilew << "30"; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | if(models[i]->has_constant) |
---|
| 545 | { |
---|
| 546 | myfilew << "61"; |
---|
| 547 | } |
---|
| 548 | else |
---|
| 549 | { |
---|
| 550 | myfilew << "60"; |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | myfilew << ",999,999,999,"; |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | myfilew << "888" << endl; |
---|
| 557 | myfilew.close(); |
---|
[1413] | 558 | */ |
---|
[1405] | 559 | } |
---|
[1412] | 560 | |
---|
| 561 | //pocet stlpcov data_matrix je pocet casovych krokov |
---|
| 562 | double cur_loglikelihood; |
---|
| 563 | // vec preds; |
---|
| 564 | int prev_samples_nr; |
---|
| 565 | bool previous_switch = true; |
---|
[1413] | 566 | for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++) //.begin()+1;model_ref++) |
---|
[1412] | 567 | {//posuvam s apo models, co je pole modelov urobene o cyklus vyssie. Teda som v case time a robim to tam pre vsetky typy modelov, kombinace regresorov |
---|
| 568 | (*model_ref)->data_update(time); //pozret sa preco je toto tu nutne |
---|
| 569 | |
---|
| 570 | //cout << "Updated." << endl; |
---|
| 571 | |
---|
| 572 | if((*model_ref)->my_rarx!=NULL) //vklada normalizacni faktor do cur_res_lognc |
---|
[1405] | 573 | { |
---|
[1412] | 574 | //cout << "Maxlik vertex(robust):" << (*model_ref)->my_rarx->posterior->minimal_vertex->get_coordinates() << endl; |
---|
| 575 | cur_loglikelihood = (*model_ref)->my_rarx->posterior->_ll(); |
---|
| 576 | } |
---|
| 577 | else |
---|
| 578 | { |
---|
| 579 | //cout << "Mean(classical):" << (*model_ref)->my_arx->posterior().mean() << endl; |
---|
| 580 | double cur_lognc = (*model_ref)->my_arx->posterior().lognc(); |
---|
| 581 | cur_loglikelihood = cur_lognc-(*model_ref)->previous_lognc; |
---|
| 582 | |
---|
| 583 | (*model_ref)->previous_lognc = cur_lognc; |
---|
| 584 | } |
---|
[1405] | 585 | |
---|
[1412] | 586 | /* |
---|
[1405] | 587 | if(time == max_window_size-1) |
---|
| 588 | { |
---|
| 589 | //*********************** |
---|
| 590 | int sample_size = 100000; |
---|
| 591 | //*********************** |
---|
| 592 | |
---|
| 593 | pair<vec,mat> samples; |
---|
| 594 | if((*model_ref)->my_arx!=NULL) |
---|
| 595 | { |
---|
| 596 | mat samp_mat = (*model_ref)->my_arx->posterior().sample_mat(sample_size); |
---|
| 597 | samples = pair<vec,mat>(ones(samp_mat.cols()),samp_mat); |
---|
| 598 | } |
---|
| 599 | else |
---|
| 600 | { |
---|
| 601 | samples = (*model_ref)->my_rarx->posterior->sample(sample_size,true); |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | char fstring[80]; |
---|
| 605 | strcpy(fstring,file_string); |
---|
| 606 | strcat(fstring,(*model_ref)->name); |
---|
| 607 | strcat(fstring,".txt"); |
---|
| 608 | |
---|
| 609 | //cout << samples.first << endl; |
---|
| 610 | |
---|
| 611 | myfilew.open(fstring,ios::app); |
---|
| 612 | |
---|
| 613 | |
---|
| 614 | //for(int i = 0;i<samples.first.size();i++) |
---|
| 615 | //{ |
---|
| 616 | // myfilew << samples.first.get(i) << ","; |
---|
| 617 | //} |
---|
| 618 | //myfilew << endl; |
---|
| 619 | |
---|
| 620 | |
---|
| 621 | for(int j = 0;j<samples.second.rows()+1;j++) |
---|
| 622 | { |
---|
| 623 | for(int i = 0;i<samples.second.cols();i++) |
---|
| 624 | { |
---|
| 625 | if(j!=samples.second.rows()) |
---|
| 626 | { |
---|
| 627 | myfilew << samples.second.get(j,i) << ","; |
---|
| 628 | } |
---|
| 629 | |
---|
| 630 | //else |
---|
| 631 | //{ |
---|
| 632 | // myfilew << "0,"; |
---|
| 633 | //} |
---|
| 634 | |
---|
| 635 | } |
---|
| 636 | myfilew << endl; |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | cout << "*************************************" << endl; |
---|
| 640 | |
---|
| 641 | myfilew.close(); |
---|
| 642 | } |
---|
[1412] | 643 | */ |
---|
[1405] | 644 | |
---|
[1412] | 645 | if(time>prediction_time) |
---|
| 646 | { |
---|
| 647 | int samples_nr; |
---|
| 648 | if(previous_switch) |
---|
| 649 | { |
---|
[1413] | 650 | samples_nr = sample_size; |
---|
[1412] | 651 | } |
---|
| 652 | else |
---|
| 653 | { |
---|
| 654 | samples_nr = prev_samples_nr; |
---|
| 655 | } |
---|
[1405] | 656 | |
---|
[1412] | 657 | |
---|
| 658 | // PREDICTIONS |
---|
| 659 | pair<vec,vec> predictions = (*model_ref)->predict(samples_nr,time,&LapRNG); |
---|
[1396] | 660 | |
---|
[1412] | 661 | /* |
---|
| 662 | myfilew.open(predstring,ios::app); |
---|
| 663 | for(int i=0;i<10000;i++) |
---|
| 664 | { |
---|
| 665 | if(i<predictions.second.size()) |
---|
| 666 | { |
---|
| 667 | myfilew << predictions.second.get(i) << ","; |
---|
| 668 | } |
---|
| 669 | else |
---|
| 670 | { |
---|
| 671 | myfilew << ","; |
---|
| 672 | } |
---|
| 673 | } |
---|
| 674 | myfilew << endl; |
---|
| 675 | myfilew.close(); |
---|
| 676 | */ |
---|
[1396] | 677 | |
---|
[1412] | 678 | if(previous_switch) |
---|
| 679 | { |
---|
| 680 | prev_samples_nr = predictions.second.size(); |
---|
| 681 | samples_nr = prev_samples_nr; |
---|
| 682 | } |
---|
[1396] | 683 | |
---|
[1412] | 684 | previous_switch = !previous_switch; |
---|
[1367] | 685 | |
---|
[1413] | 686 | double optimalInvestment = newtonRaphson(0,0.00001,predictions,utility_order); |
---|
[1405] | 687 | |
---|
[1412] | 688 | if(abs(optimalInvestment)>max_investment) |
---|
| 689 | { |
---|
| 690 | optimalInvestment = max_investment*sign(optimalInvestment); |
---|
| 691 | } |
---|
[1405] | 692 | |
---|
| 693 | |
---|
[1412] | 694 | /* |
---|
| 695 | vec utilityValues; |
---|
| 696 | for(int j=0;j<1000;j++) |
---|
| 697 | { |
---|
| 698 | utilityValues.ins(utilityValues.length(),valueCRRAUtility(-0.5+0.001*j, predictions.second, utility_order)); |
---|
| 699 | }*/ |
---|
[1405] | 700 | |
---|
[1412] | 701 | double avg_prediction = (predictions.first*predictions.second)/(predictions.first*ones(predictions.first.size())); |
---|
[1405] | 702 | |
---|
[1412] | 703 | (*model_ref)->predictions.ins((*model_ref)->predictions.size(),avg_prediction); |
---|
| 704 | |
---|
| 705 | myfilew.open(rfstring,ios::app); |
---|
| 706 | |
---|
| 707 | /* |
---|
| 708 | for(int j=0;j<utilityValues.length();j++) |
---|
| 709 | { |
---|
| 710 | myfilew << utilityValues.get(j) << ","; |
---|
| 711 | } |
---|
| 712 | myfilew << endl; |
---|
| 713 | */ |
---|
[1405] | 714 | |
---|
[1412] | 715 | myfilew << avg_prediction << "," << optimalInvestment << "," << samples_nr << "," << cur_loglikelihood << ","; |
---|
| 716 | myfilew.close(); |
---|
| 717 | } |
---|
[1376] | 718 | } |
---|
[1405] | 719 | |
---|
[1412] | 720 | if(time>prediction_time&&(time+1)<data_matrix.cols()) |
---|
| 721 | { |
---|
| 722 | // REAL PRICE |
---|
| 723 | myfilew.open(rfstring,ios::app); |
---|
| 724 | myfilew << data_matrix.get(0,time+1) << endl; |
---|
| 725 | myfilew.close(); |
---|
| 726 | } |
---|
[1383] | 727 | } |
---|
[1301] | 728 | |
---|
[1412] | 729 | for(vector<model*>::reverse_iterator model_ref = models.rbegin();model_ref!=models.rend();model_ref++) |
---|
[1405] | 730 | { |
---|
[1412] | 731 | delete *model_ref; |
---|
[1405] | 732 | } |
---|
| 733 | } |
---|
| 734 | |
---|
[1376] | 735 | return 0; |
---|
| 736 | } |
---|
[976] | 737 | |
---|
[1282] | 738 | |
---|