Changeset 1204
- Timestamp:
- 09/29/10 17:37:40 (14 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/robustlib.h
r1186 r1204 15 15 using namespace bdm; 16 16 using namespace std; 17 using namespace itpp; 17 18 18 19 const double max_range = numeric_limits<double>::max()/10e-5; … … 30 31 int multiplicity; 31 32 33 int split_state; 34 35 int merge_state; 36 37 38 32 39 public: 33 40 /// A list of polyhedrons parents within the Hasse diagram. … … 49 56 vector<polyhedron*> neutralchildren; 50 57 58 vector<polyhedron*> mergechildren; 59 60 polyhedron* positiveparent; 61 62 polyhedron* negativeparent; 63 64 int message_counter; 65 51 66 /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. 52 67 vector<vector<vertex*>> triangulations; … … 58 73 polyhedron() 59 74 { 60 multiplicity = 1; 75 multiplicity = 1; 76 77 message_counter = 0; 61 78 } 62 79 63 80 /// Setter for raising multiplicity 64 void RaiseMultiplicity()81 void raise_multiplicity() 65 82 { 66 83 multiplicity++; … … 68 85 69 86 /// Setter for lowering multiplicity 70 void LowerMultiplicity()87 void lower_multiplicity() 71 88 { 72 89 multiplicity--; … … 83 100 { 84 101 return false; 102 } 103 104 void set_state(double state_indicator, actions action) 105 { 106 switch(action) 107 { 108 case MERGE: 109 merge_state = (int)sign(state_indicator); 110 break; 111 case SPLIT: 112 split_state = (int)sign(state_indicator); 113 break; 114 } 115 } 116 117 int get_state(actions action) 118 { 119 switch(action) 120 { 121 case MERGE: 122 return merge_state; 123 break; 124 case SPLIT: 125 return split_state; 126 break; 127 } 128 } 129 130 int number_of_children() 131 { 132 return children.size()+positivechildren.size()+negativechildren.size()+neutralchildren.size(); 133 } 134 135 void send_state_message(bool shouldsplit, bool shouldmerge) 136 { 137 if(shouldsplit||shouldmerge) 138 { 139 for(vector<polyhedron*>::iterator parent_iterator = parents.begin();parent_iterator<parents.end();parent_iterator++) 140 { 141 polyhedron* current_parent = *parent_iterator; 142 143 current_parent->message_counter++; 144 145 bool is_last = (current_parent->message_counter == current_parent->number_of_children()); 146 147 if(shouldmerge) 148 { 149 int child_state = get_state(MERGE); 150 int parent_state = current_parent->get_state(MERGE); 151 152 if(parent_state == NULL || parent_state == 0) 153 { 154 current_parent->set_state(child_state, MERGE); 155 156 if(child_state == 0) 157 { 158 current_parent->mergechildren.push_back(this); 159 } 160 } 161 else 162 { 163 if(child_state == 0) 164 { 165 if(parent_state > 0) 166 { 167 positiveparent = current_parent; 168 } 169 else 170 { 171 negativeparent = current_parent; 172 } 173 } 174 } 175 176 if(is_last) 177 { 178 if(parent_state > 0) 179 { 180 for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++) 181 { 182 (*merge_child)->positiveparent = current_parent; 183 } 184 } 185 186 if(parent_state < 0) 187 { 188 for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++) 189 { 190 (*merge_child)->negativeparent = current_parent; 191 } 192 } 193 194 current_parent->mergechildren.clear(); 195 } 196 197 198 } 199 200 } 201 } 85 202 } 86 203 }; … … 91 208 { 92 209 /// A dynamic array representing coordinates of the vertex 93 vector<double> coordinates; 210 vec coordinates; 211 212 enum actions {MERGE, SPLIT}; 94 213 95 214 public: 215 216 96 217 97 218 /// Default constructor … … 99 220 100 221 /// Constructor of a vertex from a set of coordinates 101 vertex(vec tor<double>coordinates)222 vertex(vec coordinates) 102 223 { 103 224 this->coordinates = coordinates; … … 108 229 void push_coordinate(double coordinate) 109 230 { 110 coordinates .push_back(coordinate);231 coordinates = concat(coordinates,coordinate); 111 232 } 112 233 113 234 /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer 114 235 /// (not given by reference), but a new copy is created (they are given by value). 115 vector<double> get_coordinates() 116 { 117 vector<double> returned_vec; 118 119 for(int i = 0;i<coordinates.size();i++) 120 { 121 returned_vec.push_back(coordinates[i]); 122 } 123 124 return returned_vec; 125 } 236 vec get_coordinates() 237 { 238 return coordinates; 239 } 240 241 126 242 }; 127 243 … … 130 246 class toprow : public polyhedron 131 247 { 248 249 public: 132 250 /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation 133 vector<double> condition; 134 135 public: 251 vec condition; 136 252 137 253 /// Default constructor … … 139 255 140 256 /// Constructor creating a toprow from the condition 141 toprow(vec tor<double>condition)257 toprow(vec condition) 142 258 { 143 259 this->condition = condition; … … 146 262 }; 147 263 148 264 class condition 265 { 266 public: 267 vec value; 268 269 int multiplicity; 270 271 condition(vec value) 272 { 273 this->value = value; 274 multiplicity = 1; 275 } 276 } 149 277 150 278 … … 156 284 /// of data update from Bayesian estimation or set by the user if this emlig is a prior density 157 285 vector<vector<polyhedron*>> statistic; 286 287 vector<condition*> conditions; 288 289 double normalization_factor; 290 291 void alter_toprow_conditions(vec condition, bool should_be_added) 292 { 293 for(vector<polyhedron*>::iterator horiz_ref = statistic[statistic.size()-1].begin();horiz_ref<statistic[statistic.size()-1].end();horiz_ref++) 294 { 295 double product = 0; 296 297 vector<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin(); 298 299 do 300 { 301 product = (*vertex_ref)->coordinates*condition; 302 } 303 while(product == 0) 304 305 if((product>0 && should_be_added)||(product<0 && !should_be_added)) 306 { 307 ((toprow*) (*horiz_ref))->condition += condition; 308 } 309 else 310 { 311 ((toprow*) (*horiz_ref))->condition -= condition; 312 } 313 } 314 } 158 315 159 316 public: … … 173 330 } 174 331 332 void add_and_remove_condition(vec toremove, vec toadd) 333 { 334 vector<condition*>::iterator toremove_ref = conditions.end(); 335 bool condition_should_be_added = false; 336 337 for(vector<condition*>::iterator ref = conditions.begin();ref<conditions.end();ref++) 338 { 339 if(toremove != NULL) 340 { 341 if((*ref)->value == toremove) 342 { 343 if(multiplicity>1) 344 { 345 multiplicity--; 346 347 alter_toprow_conditions(toremove,false); 348 349 toremove = NULL; 350 } 351 else 352 { 353 toremove_ref = ref; 354 } 355 } 356 } 357 358 if(toadd != NULL) 359 { 360 if((*iterator)->value == toadd) 361 { 362 (*iterator)->multiplicity++; 363 364 alter_toprow_conditions(toadd,true); 365 366 toadd = NULL; 367 } 368 else 369 { 370 condition_should_be_added = true; 371 } 372 } 373 } 374 375 if(toremove_ref!=conditions.end()) 376 { 377 conditions.erase(toremove_ref); 378 } 379 380 if(condition_should_be_added) 381 { 382 conditions.push_back(new condition(toadd)); 383 } 384 385 vector<vector<polyhedron*>> for_splitting; 386 vector<vector<polyhedron*>> for_merging; 387 388 for(vector<polyhedron*>::iterator horizontal_position = statistic[0].begin();horizontal_position<statistic[0].end();horizontal_position++) 389 { 390 vertex* current_vertex = (vertex*)horizontal_position; 391 392 if(toadd != NULL) 393 { 394 current_vertex->set_state(toadd*current_vertex->coordinates,SPLIT); 395 } 396 397 if(toremove != NULL) 398 { 399 current_vertex->set_state(toremove*current_vertex->coordinates,MERGE); 400 } 401 402 current_vertex->send_state_message(toadd != NULL, toremove != NULL); 403 } 404 } 405 175 406 protected: 176 407 … … 179 410 { 180 411 // An empty vector of coordinates. 181 vec tor<double>origin_coord;412 vec origin_coord; 182 413 183 414 // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. … … 205 436 // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the 206 437 // right amount of zero cooridnates by reading them from the origin 207 vector<double> origin_coord1 = origin->get_coordinates(); 208 vector<double> origin_coord2 = origin->get_coordinates(); 438 vec origin_coord = origin->get_coordinates(); 209 439 210 440 // And we incorporate the nonzero coordinates into the new cooordinate vectors 211 origin_coord1.push_back(max_range);212 origin_coord2.push_back(-max_range);441 vec origin_coord1 = concat(origin_coord,max_range); 442 vec origin_coord2 = concat(origin_coord,-max_range); 213 443 214 444 // Now we create the points … … 273 503 // this condition will be a vector of zeros. There are two vectors, because we need two copies of 274 504 // the original Hasse diagram. 275 vector<double> vec1(i+2,0); 276 vector<double> vec2(i+2,0); 505 vec vec1(i+2); 506 vec1.zeros(); 507 508 vec vec2(i+2); 509 vec2.zeros(); 277 510 278 511 // We create a new toprow with the previously specified condition. … … 378 611 379 612 //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density 380 class RARX : public BMEF{ 613 class RARX : public BM 614 { 615 private: 616 617 emlig posterior; 618 619 public: 620 RARX():BM() 621 { 622 }; 623 624 void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec) 625 { 626 627 } 628 381 629 }; 382 630