Changeset 1337
- Timestamp:
- 04/22/11 18:35:46 (14 years ago)
- Location:
- applications/robust
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/main.cpp
r1336 r1337 7 7 */ 8 8 9 #include "estim/arx.h" 9 10 #include "robustlib.h" 10 11 #include <vector> 11 12 #include <iostream> 12 13 #include <fstream> 14 #include <itpp/signal/poly.h> 13 15 14 16 using namespace itpp; 15 //using namespace bdm;17 using namespace bdm; 16 18 17 19 const int emlig_size = 2; … … 20 22 int main ( int argc, char* argv[] ) { 21 23 24 itpp::Laplace_RNG LapRNG = Laplace_RNG(); 25 22 26 /* 23 27 // 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, … … 134 138 vector<vector<string>> strings; 135 139 136 char* file_strings[3] = {"c:\\ ar_student_single.txt", "c:\\ar_student_single.txt","c:\\ar_cauchy_single.txt"};140 char* file_strings[3] = {"c:\\dataADClosePercDiff", "c:\\ar_student_single","c:\\ar_cauchy_single"}; 137 141 138 142 for(int i = 0;i<3;i++) 139 143 { 140 ifstream myfile(file_strings[i]); 144 char dfstring[80]; 145 strcpy(dfstring,file_strings[i]); 146 strcat(dfstring,".txt"); 147 148 ifstream myfile(dfstring); 141 149 if (myfile.is_open()) 142 150 { … … 162 170 vector<vec> conditions; 163 171 //emlig* emliga = new emlig(2); 164 RARX* my_rarx = new RARX(3,20,true); 172 RARX* my_rarx = new RARX(2,30,false); 173 174 /* 175 mat V0 = 0.0001 * eye ( 3 ); 176 ARX* my_arx = new ARX(0.97); 177 my_arx->set_statistics ( 1, V0 ); //nu is default (set to have finite moments) 178 my_arx->set_constant ( false ); 179 my_arx->validate(); 180 */ 165 181 166 182 for(int k = 1;k<170;k++) … … 186 202 187 203 my_rarx->bayes(conditions[k-3]); 188 189 190 204 191 if(k>10) 205 vec cond_vec; 206 cond_vec.ins(0,conditions[k-3][0]); 207 208 // my_arx->bayes(cond_vec,conditions[k-3].right(2)); 209 210 211 if(k>5) 192 212 { 193 213 //my_rarx->posterior->step_me(0); … … 195 215 mat samples = my_rarx->posterior->sample_mat(50); 196 216 217 vec sample_prediction; 218 for(int t = 0;t<50;t++) 219 { 220 vec lap_sample = conditions[k-3].left(2); 221 222 lap_sample.ins(0,LapRNG()); 223 224 sample_prediction.ins(0,lap_sample*samples.get_col(t)); 225 } 226 227 vec sample_pow = sample_prediction; 228 vec poly_coefs; 229 bool stop_iteration = false; 230 int en = 0; 231 do 232 { 233 double poly_coef = ones(sample_pow.size())*sample_pow/sample_pow.size(); 234 235 if(abs(poly_coef)>numeric_limits<double>::epsilon()) 236 { 237 sample_pow = elem_mult(sample_pow,sample_prediction); 238 poly_coefs.ins(poly_coefs.size(),pow(-1,en)*poly_coef); 239 } 240 else 241 { 242 stop_iteration = true; 243 } 244 245 en++; 246 } 247 while(!stop_iteration); 248 249 cvec action = 250 197 251 198 252 cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl; 199 253 200 for(int s = 0;s<samples.rows();s++) 201 { 202 203 double avg_parameter = samples.get_row(s)*ones(samples.cols())/samples.cols(); 204 254 double prediction = 0; 255 for(int s = 1;s<samples.rows();s++) 256 { 257 258 double avg_parameter = samples.get_row(s)*ones(samples.cols())/samples.cols(); 259 260 prediction += avg_parameter*conditions[k-3][s-1]; 261 262 263 264 /* 205 265 ofstream myfile; 206 266 char fstring[80]; … … 227 287 } 228 288 myfile.close(); 229 } 289 */ 290 } 291 292 /* 293 enorm<ldmat>* pred_mat = my_arx->epredictor(conditions[k-3].left(2)); 294 double prediction2 = pred_mat->mean()[0]; 295 */ 296 297 ofstream myfile; 298 char fstring[80]; 299 //char f2string[80]; 300 strcpy(fstring,file_strings[j]); 301 //strcpy(f2string,fstring); 302 303 strcat(fstring,"pred.txt"); 304 //strcat(f2string,"2pred.txt"); 305 306 307 myfile.open(fstring,ios::app); 308 309 //myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; 310 myfile << prediction; 311 312 if(k!=strings[j].size()-1) 313 { 314 myfile << ","; 315 } 316 else 317 { 318 myfile << endl; 319 } 320 myfile.close(); 321 322 /* 323 myfile.open(f2string,ios::app); 324 myfile << prediction2; 325 326 if(k!=strings[j].size()-1) 327 { 328 myfile << ","; 329 } 330 else 331 { 332 myfile << endl; 333 } 334 myfile.close(); 335 */ 336 230 337 } 231 338 } … … 246 353 cout << "Step: " << i << endl;*/ 247 354 } 355 356 248 357 } 358 359 360 // EXPERIMENT: One step ahead price prediction. Comparison of classical and robust model using optimal trading 361 // with maximization of logarithm of one-step ahead wealth. 362 363 249 364 250 365 /* -
applications/robust/robustlib.h
r1336 r1337 10 10 #include <stat/exp_family.h> 11 11 #include <itpp/itbase.h> 12 #include <itpp/base/random.h> 12 13 #include <map> 13 14 #include <limits> … … 30 31 class vertex; 31 32 class emlig; 32 33 33 34 34 /* … … 2402 2402 2403 2403 } 2404 2405 2406 2407 2404 2408 2405 };