[976] | 1 | |
---|
| 2 | /*! |
---|
| 3 | \file |
---|
| 4 | \brief Robust |
---|
| 5 | \author Vasek Smidl |
---|
| 6 | |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | #include "robustlib.h" |
---|
[1216] | 10 | #include <vector> |
---|
[1284] | 11 | #include <iostream> |
---|
[1282] | 12 | #include <fstream> |
---|
| 13 | |
---|
[1208] | 14 | using namespace itpp; |
---|
[1275] | 15 | //using namespace bdm; |
---|
[976] | 16 | |
---|
[1275] | 17 | const int emlig_size = 2; |
---|
[1268] | 18 | |
---|
[1272] | 19 | |
---|
[976] | 20 | int main ( int argc, char* argv[] ) { |
---|
| 21 | |
---|
[1300] | 22 | /* |
---|
[1301] | 23 | // 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, |
---|
| 24 | // where e_t is normally, student(4) and cauchy distributed are tested using robust AR model, to obtain the |
---|
| 25 | // variance of location parameter estimators and compare it to the classical setup. |
---|
[1282] | 26 | vector<vector<vector<string>>> string_lists; |
---|
| 27 | string_lists.push_back(vector<vector<string>>()); |
---|
| 28 | string_lists.push_back(vector<vector<string>>()); |
---|
| 29 | string_lists.push_back(vector<vector<string>>()); |
---|
[1186] | 30 | |
---|
[1282] | 31 | char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"}; |
---|
[1268] | 32 | |
---|
[1282] | 33 | |
---|
| 34 | for(int i = 0;i<3;i++) |
---|
| 35 | { |
---|
| 36 | ifstream myfile(file_strings[i]); |
---|
| 37 | if (myfile.is_open()) |
---|
| 38 | { |
---|
| 39 | while ( myfile.good() ) |
---|
| 40 | { |
---|
| 41 | string line; |
---|
| 42 | getline(myfile,line); |
---|
| 43 | |
---|
| 44 | vector<string> parsed_line; |
---|
| 45 | while(line.find(',') != string::npos) |
---|
| 46 | { |
---|
| 47 | int loc = line.find(','); |
---|
| 48 | parsed_line.push_back(line.substr(0,loc)); |
---|
| 49 | line.erase(0,loc+1); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | string_lists[i].push_back(parsed_line); |
---|
| 53 | } |
---|
| 54 | myfile.close(); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | for(int j = 0;j<string_lists.size();j++) |
---|
| 59 | { |
---|
[1301] | 60 | |
---|
[1284] | 61 | for(int i = 0;i<string_lists[j].size()-1;i++) |
---|
[1282] | 62 | { |
---|
| 63 | vector<vec> conditions; |
---|
[1301] | 64 | //emlig* emliga = new emlig(2); |
---|
| 65 | RARX* my_rarx = new RARX(2,30); |
---|
| 66 | |
---|
[1282] | 67 | for(int k = 1;k<string_lists[j][i].size();k++) |
---|
| 68 | { |
---|
| 69 | vec condition; |
---|
| 70 | //condition.ins(0,1); |
---|
| 71 | condition.ins(0,string_lists[j][i][k]); |
---|
| 72 | conditions.push_back(condition); |
---|
| 73 | |
---|
| 74 | //cout << "orig:" << condition << endl; |
---|
| 75 | |
---|
| 76 | if(conditions.size()>1) |
---|
| 77 | { |
---|
| 78 | conditions[k-2].ins(0,string_lists[j][i][k]); |
---|
| 79 | |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if(conditions.size()>2) |
---|
| 83 | { |
---|
| 84 | conditions[k-3].ins(0,string_lists[j][i][k]); |
---|
| 85 | |
---|
| 86 | //cout << "modi:" << conditions[k-3] << endl; |
---|
| 87 | |
---|
[1301] | 88 | my_rarx->bayes(conditions[k-3]); |
---|
[1282] | 89 | |
---|
[1299] | 90 | |
---|
| 91 | //if(k>5) |
---|
| 92 | //{ |
---|
| 93 | // cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
---|
| 94 | //} |
---|
| 95 | |
---|
| 96 | } |
---|
| 97 | |
---|
[1282] | 98 | } |
---|
| 99 | |
---|
| 100 | //emliga->step_me(0); |
---|
[1301] | 101 | /* |
---|
[1284] | 102 | ofstream myfile; |
---|
| 103 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
[1301] | 104 | myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; |
---|
[1284] | 105 | myfile.close(); |
---|
| 106 | |
---|
| 107 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
| 108 | myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; |
---|
| 109 | myfile.close(); |
---|
[1301] | 110 | |
---|
[1284] | 111 | |
---|
[1282] | 112 | cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
---|
| 113 | cout << "Step: " << i << endl; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | cout << "One experiment finished." << endl; |
---|
[1284] | 117 | |
---|
| 118 | ofstream myfile; |
---|
| 119 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
| 120 | myfile << endl; |
---|
| 121 | myfile.close(); |
---|
| 122 | |
---|
| 123 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
| 124 | myfile << endl; |
---|
| 125 | myfile.close(); |
---|
[1301] | 126 | }*/ |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from |
---|
| 130 | // 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 |
---|
| 131 | // can be compared to the classical setup. |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | vector<vector<string>> strings; |
---|
| 135 | |
---|
[1324] | 136 | char* file_strings[3] = {"c:\\ar_cauchy_single.txt", "c:\\ar_normal_single.txt","c:\\ar_student_single.txt"}; |
---|
[1301] | 137 | |
---|
| 138 | for(int i = 0;i<3;i++) |
---|
| 139 | { |
---|
| 140 | ifstream myfile(file_strings[i]); |
---|
| 141 | if (myfile.is_open()) |
---|
| 142 | { |
---|
| 143 | string line; |
---|
| 144 | getline(myfile,line); |
---|
| 145 | |
---|
| 146 | vector<string> parsed_line; |
---|
| 147 | while(line.find(',') != string::npos) |
---|
| 148 | { |
---|
| 149 | int loc = line.find(','); |
---|
| 150 | parsed_line.push_back(line.substr(0,loc)); |
---|
| 151 | line.erase(0,loc+1); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | strings.push_back(parsed_line); |
---|
| 155 | |
---|
| 156 | myfile.close(); |
---|
| 157 | } |
---|
[1282] | 158 | } |
---|
| 159 | |
---|
[1301] | 160 | for(int j = 0;j<strings.size();j++) |
---|
| 161 | { |
---|
| 162 | vector<vec> conditions; |
---|
| 163 | //emlig* emliga = new emlig(2); |
---|
[1324] | 164 | RARX* my_rarx = new RARX(2,70); |
---|
[1301] | 165 | |
---|
| 166 | for(int k = 1;k<170;k++) |
---|
| 167 | { |
---|
| 168 | vec condition; |
---|
| 169 | //condition.ins(0,1); |
---|
| 170 | condition.ins(0,strings[j][k]); |
---|
| 171 | conditions.push_back(condition); |
---|
| 172 | |
---|
| 173 | //cout << "orig:" << condition << endl; |
---|
| 174 | |
---|
| 175 | if(conditions.size()>1) |
---|
| 176 | { |
---|
| 177 | conditions[k-2].ins(0,strings[j][k]); |
---|
| 178 | |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | if(conditions.size()>2) |
---|
| 182 | { |
---|
| 183 | conditions[k-3].ins(0,strings[j][k]); |
---|
| 184 | |
---|
| 185 | // cout << "modi:" << conditions[k-3] << endl; |
---|
| 186 | |
---|
| 187 | my_rarx->bayes(conditions[k-3]); |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | if(k>5) |
---|
| 192 | { |
---|
[1324] | 193 | //my_rarx->posterior->step_me(0); |
---|
| 194 | |
---|
| 195 | //my_rarx->posterior->sample_mat(1); |
---|
| 196 | |
---|
[1301] | 197 | cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl; |
---|
| 198 | |
---|
| 199 | ofstream myfile; |
---|
| 200 | char fstring[80]; |
---|
| 201 | strcpy(fstring,file_strings[j]); |
---|
| 202 | strcat(fstring,"_res.txt"); |
---|
| 203 | |
---|
| 204 | myfile.open(fstring,ios::app); |
---|
| 205 | myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; |
---|
| 206 | if(k!=strings[j].size()-1) |
---|
| 207 | { |
---|
| 208 | myfile << ","; |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | myfile << endl; |
---|
| 213 | } |
---|
| 214 | myfile.close(); |
---|
[1319] | 215 | } |
---|
[1301] | 216 | } |
---|
| 217 | |
---|
| 218 | //emliga->step_me(0); |
---|
| 219 | /* |
---|
| 220 | ofstream myfile; |
---|
| 221 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
| 222 | myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; |
---|
| 223 | myfile.close(); |
---|
| 224 | |
---|
| 225 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
| 226 | myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; |
---|
| 227 | myfile.close(); |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
---|
| 231 | cout << "Step: " << i << endl;*/ |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /* |
---|
| 236 | cout << "One experiment finished." << endl; |
---|
| 237 | |
---|
| 238 | ofstream myfile; |
---|
| 239 | myfile.open("c:\\robust_ar1.txt",ios::app); |
---|
| 240 | myfile << endl; |
---|
| 241 | myfile.close(); |
---|
| 242 | |
---|
| 243 | myfile.open("c:\\robust_ar2.txt",ios::app); |
---|
| 244 | myfile << endl; |
---|
| 245 | myfile.close();*/ |
---|
[1300] | 246 | |
---|
[1301] | 247 | |
---|
| 248 | //emlig* emlig1 = new emlig(emlig_size); |
---|
| 249 | |
---|
| 250 | //emlig1->step_me(0); |
---|
| 251 | //emlig* emlig2 = new emlig(emlig_size); |
---|
[1300] | 252 | |
---|
[1267] | 253 | /* |
---|
| 254 | emlig1->set_correction_factors(4); |
---|
[1266] | 255 | |
---|
[1267] | 256 | for(int j = 0;j<emlig1->correction_factors.size();j++) |
---|
| 257 | { |
---|
| 258 | for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++) |
---|
| 259 | { |
---|
[1268] | 260 | cout << j << " "; |
---|
| 261 | |
---|
[1267] | 262 | for(int i=0;i<(*vec_ref).size();i++) |
---|
| 263 | { |
---|
| 264 | cout << (*vec_ref)[i]; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | cout << endl; |
---|
| 268 | } |
---|
[1268] | 269 | }*/ |
---|
| 270 | |
---|
[1301] | 271 | /* |
---|
[1300] | 272 | vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; |
---|
| 273 | |
---|
[1299] | 274 | emlig1->add_condition(condition5); |
---|
[1301] | 275 | //emlig1->step_me(0); |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | vec condition1a = "-1.0 1.02 0.5"; |
---|
[1300] | 279 | //vec condition1b = "1.0 1.0 1.01"; |
---|
[1301] | 280 | emlig1->add_condition(condition1a); |
---|
[1300] | 281 | //emlig2->add_condition(condition1b); |
---|
[1267] | 282 | |
---|
[1301] | 283 | vec condition2a = "-0.3 1.7 1.5"; |
---|
[1300] | 284 | //vec condition2b = "-1.0 1.0 1.0"; |
---|
[1301] | 285 | emlig1->add_condition(condition2a); |
---|
[1300] | 286 | //emlig2->add_condition(condition2b); |
---|
[1234] | 287 | |
---|
[1301] | 288 | vec condition3a = "0.5 -1.01 1.0"; |
---|
[1300] | 289 | //vec condition3b = "0.5 -1.01 1.0"; |
---|
[1280] | 290 | |
---|
[1301] | 291 | emlig1->add_condition(condition3a); |
---|
[1300] | 292 | //emlig2->add_condition(condition3b); |
---|
[1280] | 293 | |
---|
[1301] | 294 | vec condition4a = "-0.5 -1.0 1.0"; |
---|
[1300] | 295 | //vec condition4b = "-0.5 -1.0 1.0"; |
---|
| 296 | |
---|
[1301] | 297 | emlig1->add_condition(condition4a); |
---|
[1300] | 298 | //cout << "************************************************" << endl; |
---|
| 299 | //emlig2->add_condition(condition4b); |
---|
| 300 | //cout << "************************************************" << endl; |
---|
| 301 | |
---|
[1299] | 302 | //cout << emlig1->minimal_vertex->get_coordinates(); |
---|
[1300] | 303 | |
---|
[1301] | 304 | //emlig1->remove_condition(condition3a); |
---|
| 305 | //emlig1->step_me(0); |
---|
| 306 | //emlig1->remove_condition(condition2a); |
---|
| 307 | //emlig1->remove_condition(condition1a); |
---|
| 308 | //emlig1->remove_condition(condition5); |
---|
| 309 | |
---|
[1275] | 310 | |
---|
[1299] | 311 | //emlig1->step_me(0); |
---|
| 312 | //emlig2->step_me(0); |
---|
| 313 | |
---|
[1282] | 314 | |
---|
| 315 | // DA SE POUZIT PRO VYPIS DO SOUBORU |
---|
[1275] | 316 | // emlig1->step_me(0); |
---|
| 317 | |
---|
| 318 | //emlig1->remove_condition(condition1); |
---|
| 319 | |
---|
[1301] | 320 | |
---|
[1275] | 321 | |
---|
| 322 | |
---|
[1301] | 323 | |
---|
[1275] | 324 | /* |
---|
[1282] | 325 | for(int i = 0;i<100;i++) |
---|
[1219] | 326 | { |
---|
[1282] | 327 | cout << endl << "Step:" << i << endl; |
---|
[1208] | 328 | |
---|
[1268] | 329 | double condition[emlig_size+1]; |
---|
[1220] | 330 | |
---|
[1268] | 331 | for(int k = 0;k<=emlig_size;k++) |
---|
| 332 | { |
---|
[1272] | 333 | condition[k] = (rand()-RAND_MAX/2)/1000.0; |
---|
[1268] | 334 | } |
---|
| 335 | |
---|
[1216] | 336 | |
---|
[1268] | 337 | vec* condition_vec = new vec(condition,emlig_size+1); |
---|
[1219] | 338 | emlig1->add_condition(*condition_vec); |
---|
[1271] | 339 | |
---|
[1272] | 340 | /* |
---|
| 341 | for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly) |
---|
| 342 | { |
---|
| 343 | cout << ((toprow*)toprow_ref)->probability << endl; |
---|
| 344 | } |
---|
| 345 | */ |
---|
[1275] | 346 | /* |
---|
[1271] | 347 | cout << emlig1->statistic_rowsize(emlig_size) << endl << endl; |
---|
[1268] | 348 | |
---|
[1272] | 349 | /* |
---|
[1271] | 350 | if(i-emlig1->number_of_parameters >= 0) |
---|
| 351 | { |
---|
| 352 | pause(30); |
---|
| 353 | } |
---|
[1272] | 354 | */ |
---|
[1219] | 355 | |
---|
[1271] | 356 | // emlig1->step_me(i); |
---|
[1219] | 357 | |
---|
[1272] | 358 | /* |
---|
[1219] | 359 | vector<int> sizevector; |
---|
| 360 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
---|
| 361 | { |
---|
| 362 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
---|
| 363 | } |
---|
[1272] | 364 | */ |
---|
[1275] | 365 | //} |
---|
| 366 | |
---|
[1219] | 367 | |
---|
| 368 | |
---|
| 369 | |
---|
| 370 | /* |
---|
| 371 | emlig1->step_me(1); |
---|
| 372 | |
---|
| 373 | vec condition = "2.0 0.0 1.0"; |
---|
| 374 | |
---|
[1208] | 375 | emlig1->add_condition(condition); |
---|
| 376 | |
---|
[1216] | 377 | vector<int> sizevector; |
---|
| 378 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
---|
| 379 | { |
---|
| 380 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
---|
| 381 | } |
---|
| 382 | |
---|
[1219] | 383 | emlig1->step_me(2); |
---|
[1216] | 384 | |
---|
[1219] | 385 | condition = "2.0 1.0 0.0"; |
---|
[1216] | 386 | |
---|
| 387 | emlig1->add_condition(condition); |
---|
| 388 | |
---|
| 389 | sizevector.clear(); |
---|
| 390 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
---|
| 391 | { |
---|
| 392 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
---|
| 393 | } |
---|
[1219] | 394 | */ |
---|
[1216] | 395 | |
---|
[976] | 396 | return 0; |
---|
| 397 | } |
---|
| 398 | |
---|
[1282] | 399 | |
---|