Changeset 1343 for applications/robust/main.cpp
- Timestamp:
- 04/29/11 13:26:04 (14 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/main.cpp
r1338 r1343 18 18 19 19 const int emlig_size = 2; 20 const int utility_constant = 2; 20 21 21 22 … … 138 139 vector<vector<string>> strings; 139 140 140 char* file_strings[3] = {"c:\\data CDClosePercDiff","c:\\ar_student_single","c:\\ar_cauchy_single"};141 char* file_strings[3] = {"c:\\dataADClosePercDiff","c:\\ar_student_single","c:\\ar_cauchy_single"}; 141 142 142 143 for(int i = 0;i<3;i++) … … 170 171 vector<vec> conditions; 171 172 //emlig* emliga = new emlig(2); 172 RARX* my_rarx = new RARX( 2,30,false);173 RARX* my_rarx = new RARX(3,30,true); 173 174 174 175 … … 199 200 conditions[k-3].ins(0,strings[j][k]); 200 201 201 //cout << "Condition:" << conditions[k-3] << endl;202 cout << "Condition:" << conditions[k-3] << endl; 202 203 203 204 my_rarx->bayes(conditions[k-3]); … … 210 211 211 212 212 if(k> 8)213 if(k>10) 213 214 { 214 215 //my_rarx->posterior->step_me(0); 215 216 216 mat samples = my_rarx->posterior->sample_mat(50); 217 mat samples = my_rarx->posterior->sample_mat(10); 218 219 pair<vec,mat> imp_samples = my_rarx->posterior->importance_sample(20); 220 221 cout << imp_samples.first << endl; 217 222 218 223 vec sample_prediction; 219 for(int t = 0;t< 50;t++)224 for(int t = 0;t<samples.cols();t++) 220 225 { 221 226 vec lap_sample = conditions[k-3].left(2); 222 //lap_sample.ins(lap_sample.size(),1.0);227 lap_sample.ins(lap_sample.size(),1.0); 223 228 224 229 lap_sample.ins(0,LapRNG()); … … 229 234 230 235 vec sample_pow = sample_prediction; 236 237 // cout << sample_prediction << endl; 231 238 vec poly_coefs; 232 239 bool stop_iteration = false; 233 int en = 0;240 int en = 1; 234 241 do 235 242 { 236 243 double poly_coef = ones(sample_pow.size())*sample_pow/sample_pow.size(); 244 245 poly_coef = poly_coef*en*fact(utility_constant-2+en)/fact(utility_constant-2); 237 246 238 247 if(abs(poly_coef)>numeric_limits<double>::epsilon()) 239 248 { 240 249 sample_pow = elem_mult(sample_pow,sample_prediction); 241 poly_coefs.ins( poly_coefs.size(),((-1)^en)*poly_coef);250 poly_coefs.ins(0,pow(-1.0,en+1)*poly_coef); 242 251 } 243 252 else … … 247 256 248 257 en++; 258 259 if(en>20) 260 { 261 stop_iteration = true; 262 } 249 263 } 250 264 while(!stop_iteration); 265 266 /* 267 ofstream myfile_coef; 268 269 myfile_coef.open("c:\\coefs.txt",ios::app); 270 271 for(int t = 0;t<poly_coefs.size();t++) 272 { 273 myfile_coef << poly_coefs[t] << ","; 274 } 275 276 myfile_coef << endl; 277 myfile_coef.close(); 278 */ 251 279 252 280 // cout << "Coefficients: " << poly_coefs << endl; 253 281 282 /* 283 vec bas_coef = vec("1.0 2.0 -8.0"); 284 cout << "Coefs: " << bas_coef << endl; 285 cvec actions2 = roots(bas_coef); 286 cout << "Roots: " << actions2 << endl; 287 */ 288 254 289 cvec actions = roots(poly_coefs); 290 291 255 292 bool is_max = false; 256 293 for(int t = 0;t<actions.size();t++) 257 294 { 258 if(actions[t].imag() == 0 && actions[t].real()>-1 && actions[t].real()<1)295 if(actions[t].imag() == 0) 259 296 { 260 cout << "Action:" << actions[t].real() << endl; 261 is_max = true; 297 298 299 double second_derivative = 0; 300 for(int q = 1;q<poly_coefs.size();q++) 301 { 302 second_derivative+=poly_coefs[q]*pow(actions[t].real(),q-1)*q; 303 } 304 305 if(second_derivative<0) 306 { 307 cout << "Action:" << actions[t].real() << endl; 308 309 is_max = true; 310 } 262 311 } 263 312 }