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_cauchy_single.txt", "c:\\ar_normal_single.txt","c:\\ar_student_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,70); |
---|
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 | //my_rarx->posterior->step_me(0); |
---|
194 | |
---|
195 | my_rarx->posterior->sample_mat(1); |
---|
196 | |
---|
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(); |
---|
215 | } |
---|
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();*/ |
---|
246 | |
---|
247 | |
---|
248 | //emlig* emlig1 = new emlig(emlig_size); |
---|
249 | |
---|
250 | //emlig1->step_me(0); |
---|
251 | //emlig* emlig2 = new emlig(emlig_size); |
---|
252 | |
---|
253 | /* |
---|
254 | emlig1->set_correction_factors(4); |
---|
255 | |
---|
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 | { |
---|
260 | cout << j << " "; |
---|
261 | |
---|
262 | for(int i=0;i<(*vec_ref).size();i++) |
---|
263 | { |
---|
264 | cout << (*vec_ref)[i]; |
---|
265 | } |
---|
266 | |
---|
267 | cout << endl; |
---|
268 | } |
---|
269 | }*/ |
---|
270 | |
---|
271 | /* |
---|
272 | vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; |
---|
273 | |
---|
274 | emlig1->add_condition(condition5); |
---|
275 | //emlig1->step_me(0); |
---|
276 | |
---|
277 | |
---|
278 | vec condition1a = "-1.0 1.02 0.5"; |
---|
279 | //vec condition1b = "1.0 1.0 1.01"; |
---|
280 | emlig1->add_condition(condition1a); |
---|
281 | //emlig2->add_condition(condition1b); |
---|
282 | |
---|
283 | vec condition2a = "-0.3 1.7 1.5"; |
---|
284 | //vec condition2b = "-1.0 1.0 1.0"; |
---|
285 | emlig1->add_condition(condition2a); |
---|
286 | //emlig2->add_condition(condition2b); |
---|
287 | |
---|
288 | vec condition3a = "0.5 -1.01 1.0"; |
---|
289 | //vec condition3b = "0.5 -1.01 1.0"; |
---|
290 | |
---|
291 | emlig1->add_condition(condition3a); |
---|
292 | //emlig2->add_condition(condition3b); |
---|
293 | |
---|
294 | vec condition4a = "-0.5 -1.0 1.0"; |
---|
295 | //vec condition4b = "-0.5 -1.0 1.0"; |
---|
296 | |
---|
297 | emlig1->add_condition(condition4a); |
---|
298 | //cout << "************************************************" << endl; |
---|
299 | //emlig2->add_condition(condition4b); |
---|
300 | //cout << "************************************************" << endl; |
---|
301 | |
---|
302 | //cout << emlig1->minimal_vertex->get_coordinates(); |
---|
303 | |
---|
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 | |
---|
310 | |
---|
311 | //emlig1->step_me(0); |
---|
312 | //emlig2->step_me(0); |
---|
313 | |
---|
314 | |
---|
315 | // DA SE POUZIT PRO VYPIS DO SOUBORU |
---|
316 | // emlig1->step_me(0); |
---|
317 | |
---|
318 | //emlig1->remove_condition(condition1); |
---|
319 | |
---|
320 | |
---|
321 | |
---|
322 | |
---|
323 | |
---|
324 | /* |
---|
325 | for(int i = 0;i<100;i++) |
---|
326 | { |
---|
327 | cout << endl << "Step:" << i << endl; |
---|
328 | |
---|
329 | double condition[emlig_size+1]; |
---|
330 | |
---|
331 | for(int k = 0;k<=emlig_size;k++) |
---|
332 | { |
---|
333 | condition[k] = (rand()-RAND_MAX/2)/1000.0; |
---|
334 | } |
---|
335 | |
---|
336 | |
---|
337 | vec* condition_vec = new vec(condition,emlig_size+1); |
---|
338 | emlig1->add_condition(*condition_vec); |
---|
339 | |
---|
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 | */ |
---|
346 | /* |
---|
347 | cout << emlig1->statistic_rowsize(emlig_size) << endl << endl; |
---|
348 | |
---|
349 | /* |
---|
350 | if(i-emlig1->number_of_parameters >= 0) |
---|
351 | { |
---|
352 | pause(30); |
---|
353 | } |
---|
354 | */ |
---|
355 | |
---|
356 | // emlig1->step_me(i); |
---|
357 | |
---|
358 | /* |
---|
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 | } |
---|
364 | */ |
---|
365 | //} |
---|
366 | |
---|
367 | |
---|
368 | |
---|
369 | |
---|
370 | /* |
---|
371 | emlig1->step_me(1); |
---|
372 | |
---|
373 | vec condition = "2.0 0.0 1.0"; |
---|
374 | |
---|
375 | emlig1->add_condition(condition); |
---|
376 | |
---|
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 | |
---|
383 | emlig1->step_me(2); |
---|
384 | |
---|
385 | condition = "2.0 1.0 0.0"; |
---|
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 | } |
---|
394 | */ |
---|
395 | |
---|
396 | return 0; |
---|
397 | } |
---|
398 | |
---|
399 | |
---|