| 1 | |
|---|
| 2 | /*! |
|---|
| 3 | \file |
|---|
| 4 | \brief Robust |
|---|
| 5 | \author Vasek Smidl |
|---|
| 6 | |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #include "robustlib.h" |
|---|
| 10 | #include <vector> |
|---|
| 11 | #include <iostream> |
|---|
| 12 | #include <fstream> |
|---|
| 13 | |
|---|
| 14 | using namespace itpp; |
|---|
| 15 | //using namespace bdm; |
|---|
| 16 | |
|---|
| 17 | const int emlig_size = 2; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | int main ( int argc, char* argv[] ) { |
|---|
| 21 | |
|---|
| 22 | /* |
|---|
| 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. |
|---|
| 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>>()); |
|---|
| 30 | |
|---|
| 31 | char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"}; |
|---|
| 32 | |
|---|
| 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 | { |
|---|
| 60 | |
|---|
| 61 | for(int i = 0;i<string_lists[j].size()-1;i++) |
|---|
| 62 | { |
|---|
| 63 | vector<vec> conditions; |
|---|
| 64 | //emlig* emliga = new emlig(2); |
|---|
| 65 | RARX* my_rarx = new RARX(2,30); |
|---|
| 66 | |
|---|
| 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 | |
|---|
| 88 | my_rarx->bayes(conditions[k-3]); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | //if(k>5) |
|---|
| 92 | //{ |
|---|
| 93 | // cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
|---|
| 94 | //} |
|---|
| 95 | |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | //emliga->step_me(0); |
|---|
| 101 | /* |
|---|
| 102 | ofstream myfile; |
|---|
| 103 | myfile.open("c:\\robust_ar1.txt",ios::app); |
|---|
| 104 | myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; |
|---|
| 105 | myfile.close(); |
|---|
| 106 | |
|---|
| 107 | myfile.open("c:\\robust_ar2.txt",ios::app); |
|---|
| 108 | myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; |
|---|
| 109 | myfile.close(); |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
|---|
| 113 | cout << "Step: " << i << endl; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | cout << "One experiment finished." << endl; |
|---|
| 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(); |
|---|
| 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 | |
|---|
| 136 | char* file_strings[3] = {"c:\\ar_student_single.txt", "c:\\ar_cauchy_single.txt","c:\\ar_normal_single.txt"}; |
|---|
| 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 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | for(int j = 0;j<strings.size();j++) |
|---|
| 161 | { |
|---|
| 162 | vector<vec> conditions; |
|---|
| 163 | //emlig* emliga = new emlig(2); |
|---|
| 164 | RARX* my_rarx = new RARX(2,0); |
|---|
| 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 | { |
|---|
| 193 | cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl; |
|---|
| 194 | |
|---|
| 195 | ofstream myfile; |
|---|
| 196 | char fstring[80]; |
|---|
| 197 | strcpy(fstring,file_strings[j]); |
|---|
| 198 | strcat(fstring,"_res.txt"); |
|---|
| 199 | |
|---|
| 200 | myfile.open(fstring,ios::app); |
|---|
| 201 | myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; |
|---|
| 202 | if(k!=strings[j].size()-1) |
|---|
| 203 | { |
|---|
| 204 | myfile << ","; |
|---|
| 205 | } |
|---|
| 206 | else |
|---|
| 207 | { |
|---|
| 208 | myfile << endl; |
|---|
| 209 | } |
|---|
| 210 | myfile.close(); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | //emliga->step_me(0); |
|---|
| 216 | /* |
|---|
| 217 | ofstream myfile; |
|---|
| 218 | myfile.open("c:\\robust_ar1.txt",ios::app); |
|---|
| 219 | myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; |
|---|
| 220 | myfile.close(); |
|---|
| 221 | |
|---|
| 222 | myfile.open("c:\\robust_ar2.txt",ios::app); |
|---|
| 223 | myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; |
|---|
| 224 | myfile.close(); |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; |
|---|
| 228 | cout << "Step: " << i << endl;*/ |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | /* |
|---|
| 233 | cout << "One experiment finished." << endl; |
|---|
| 234 | |
|---|
| 235 | ofstream myfile; |
|---|
| 236 | myfile.open("c:\\robust_ar1.txt",ios::app); |
|---|
| 237 | myfile << endl; |
|---|
| 238 | myfile.close(); |
|---|
| 239 | |
|---|
| 240 | myfile.open("c:\\robust_ar2.txt",ios::app); |
|---|
| 241 | myfile << endl; |
|---|
| 242 | myfile.close();*/ |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | //emlig* emlig1 = new emlig(emlig_size); |
|---|
| 246 | |
|---|
| 247 | //emlig1->step_me(0); |
|---|
| 248 | //emlig* emlig2 = new emlig(emlig_size); |
|---|
| 249 | |
|---|
| 250 | /* |
|---|
| 251 | emlig1->set_correction_factors(4); |
|---|
| 252 | |
|---|
| 253 | for(int j = 0;j<emlig1->correction_factors.size();j++) |
|---|
| 254 | { |
|---|
| 255 | for(set<my_ivec>::iterator vec_ref = emlig1->correction_factors[j].begin();vec_ref!=emlig1->correction_factors[j].end();vec_ref++) |
|---|
| 256 | { |
|---|
| 257 | cout << j << " "; |
|---|
| 258 | |
|---|
| 259 | for(int i=0;i<(*vec_ref).size();i++) |
|---|
| 260 | { |
|---|
| 261 | cout << (*vec_ref)[i]; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | cout << endl; |
|---|
| 265 | } |
|---|
| 266 | }*/ |
|---|
| 267 | |
|---|
| 268 | /* |
|---|
| 269 | vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; |
|---|
| 270 | |
|---|
| 271 | emlig1->add_condition(condition5); |
|---|
| 272 | //emlig1->step_me(0); |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | vec condition1a = "-1.0 1.02 0.5"; |
|---|
| 276 | //vec condition1b = "1.0 1.0 1.01"; |
|---|
| 277 | emlig1->add_condition(condition1a); |
|---|
| 278 | //emlig2->add_condition(condition1b); |
|---|
| 279 | |
|---|
| 280 | vec condition2a = "-0.3 1.7 1.5"; |
|---|
| 281 | //vec condition2b = "-1.0 1.0 1.0"; |
|---|
| 282 | emlig1->add_condition(condition2a); |
|---|
| 283 | //emlig2->add_condition(condition2b); |
|---|
| 284 | |
|---|
| 285 | vec condition3a = "0.5 -1.01 1.0"; |
|---|
| 286 | //vec condition3b = "0.5 -1.01 1.0"; |
|---|
| 287 | |
|---|
| 288 | emlig1->add_condition(condition3a); |
|---|
| 289 | //emlig2->add_condition(condition3b); |
|---|
| 290 | |
|---|
| 291 | vec condition4a = "-0.5 -1.0 1.0"; |
|---|
| 292 | //vec condition4b = "-0.5 -1.0 1.0"; |
|---|
| 293 | |
|---|
| 294 | emlig1->add_condition(condition4a); |
|---|
| 295 | //cout << "************************************************" << endl; |
|---|
| 296 | //emlig2->add_condition(condition4b); |
|---|
| 297 | //cout << "************************************************" << endl; |
|---|
| 298 | |
|---|
| 299 | //cout << emlig1->minimal_vertex->get_coordinates(); |
|---|
| 300 | |
|---|
| 301 | //emlig1->remove_condition(condition3a); |
|---|
| 302 | //emlig1->step_me(0); |
|---|
| 303 | //emlig1->remove_condition(condition2a); |
|---|
| 304 | //emlig1->remove_condition(condition1a); |
|---|
| 305 | //emlig1->remove_condition(condition5); |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | //emlig1->step_me(0); |
|---|
| 309 | //emlig2->step_me(0); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | // DA SE POUZIT PRO VYPIS DO SOUBORU |
|---|
| 313 | // emlig1->step_me(0); |
|---|
| 314 | |
|---|
| 315 | //emlig1->remove_condition(condition1); |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | /* |
|---|
| 322 | for(int i = 0;i<100;i++) |
|---|
| 323 | { |
|---|
| 324 | cout << endl << "Step:" << i << endl; |
|---|
| 325 | |
|---|
| 326 | double condition[emlig_size+1]; |
|---|
| 327 | |
|---|
| 328 | for(int k = 0;k<=emlig_size;k++) |
|---|
| 329 | { |
|---|
| 330 | condition[k] = (rand()-RAND_MAX/2)/1000.0; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | vec* condition_vec = new vec(condition,emlig_size+1); |
|---|
| 335 | emlig1->add_condition(*condition_vec); |
|---|
| 336 | |
|---|
| 337 | /* |
|---|
| 338 | for(polyhedron* toprow_ref = emlig1->statistic.rows[emlig_size]; toprow_ref != emlig1->statistic.end_poly; toprow_ref = toprow_ref->next_poly) |
|---|
| 339 | { |
|---|
| 340 | cout << ((toprow*)toprow_ref)->probability << endl; |
|---|
| 341 | } |
|---|
| 342 | */ |
|---|
| 343 | /* |
|---|
| 344 | cout << emlig1->statistic_rowsize(emlig_size) << endl << endl; |
|---|
| 345 | |
|---|
| 346 | /* |
|---|
| 347 | if(i-emlig1->number_of_parameters >= 0) |
|---|
| 348 | { |
|---|
| 349 | pause(30); |
|---|
| 350 | } |
|---|
| 351 | */ |
|---|
| 352 | |
|---|
| 353 | // emlig1->step_me(i); |
|---|
| 354 | |
|---|
| 355 | /* |
|---|
| 356 | vector<int> sizevector; |
|---|
| 357 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
|---|
| 358 | { |
|---|
| 359 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
|---|
| 360 | } |
|---|
| 361 | */ |
|---|
| 362 | //} |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | /* |
|---|
| 368 | emlig1->step_me(1); |
|---|
| 369 | |
|---|
| 370 | vec condition = "2.0 0.0 1.0"; |
|---|
| 371 | |
|---|
| 372 | emlig1->add_condition(condition); |
|---|
| 373 | |
|---|
| 374 | vector<int> sizevector; |
|---|
| 375 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
|---|
| 376 | { |
|---|
| 377 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | emlig1->step_me(2); |
|---|
| 381 | |
|---|
| 382 | condition = "2.0 1.0 0.0"; |
|---|
| 383 | |
|---|
| 384 | emlig1->add_condition(condition); |
|---|
| 385 | |
|---|
| 386 | sizevector.clear(); |
|---|
| 387 | for(int s = 0;s<=emlig1->number_of_parameters;s++) |
|---|
| 388 | { |
|---|
| 389 | sizevector.push_back(emlig1->statistic_rowsize(s)); |
|---|
| 390 | } |
|---|
| 391 | */ |
|---|
| 392 | |
|---|
| 393 | return 0; |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | |
|---|