| 1 | /*! |
|---|
| 2 | \file |
|---|
| 3 | \brief Robust Bayesian auto-regression model |
|---|
| 4 | \author Jan Sindelar. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #ifndef ROBUST_H |
|---|
| 8 | #define ROBUST_H |
|---|
| 9 | |
|---|
| 10 | #include <stat/exp_family.h> |
|---|
| 11 | #include <limits> |
|---|
| 12 | #include <vector> |
|---|
| 13 | #include <list> |
|---|
| 14 | #include <algorithm> |
|---|
| 15 | |
|---|
| 16 | using namespace bdm; |
|---|
| 17 | using namespace std; |
|---|
| 18 | using namespace itpp; |
|---|
| 19 | |
|---|
| 20 | const double max_range = numeric_limits<double>::max()/10e-5; |
|---|
| 21 | |
|---|
| 22 | enum actions {MERGE, SPLIT}; |
|---|
| 23 | |
|---|
| 24 | class polyhedron; |
|---|
| 25 | class vertex; |
|---|
| 26 | |
|---|
| 27 | /// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram |
|---|
| 28 | /// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created. |
|---|
| 29 | class polyhedron |
|---|
| 30 | { |
|---|
| 31 | /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of |
|---|
| 32 | /// more than just the necessary number of conditions. For example if a newly created line passes through an already |
|---|
| 33 | /// existing point, the points multiplicity will rise by 1. |
|---|
| 34 | int multiplicity; |
|---|
| 35 | |
|---|
| 36 | int split_state; |
|---|
| 37 | |
|---|
| 38 | int merge_state; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | public: |
|---|
| 43 | /// A list of polyhedrons parents within the Hasse diagram. |
|---|
| 44 | list<polyhedron*> parents; |
|---|
| 45 | |
|---|
| 46 | /// A list of polyhedrons children withing the Hasse diagram. |
|---|
| 47 | list<polyhedron*> children; |
|---|
| 48 | |
|---|
| 49 | /// All the vertices of the given polyhedron |
|---|
| 50 | list<vertex*> vertices; |
|---|
| 51 | |
|---|
| 52 | /// A list used for storing children that lie in the positive region related to a certain condition |
|---|
| 53 | list<polyhedron*> positivechildren; |
|---|
| 54 | |
|---|
| 55 | /// A list used for storing children that lie in the negative region related to a certain condition |
|---|
| 56 | list<polyhedron*> negativechildren; |
|---|
| 57 | |
|---|
| 58 | /// Children intersecting the condition |
|---|
| 59 | list<polyhedron*> neutralchildren; |
|---|
| 60 | |
|---|
| 61 | list<polyhedron*> totallyneutralgrandchildren; |
|---|
| 62 | |
|---|
| 63 | list<polyhedron*> totallyneutralchildren; |
|---|
| 64 | |
|---|
| 65 | bool totally_neutral; |
|---|
| 66 | |
|---|
| 67 | list<polyhedron*> mergechildren; |
|---|
| 68 | |
|---|
| 69 | polyhedron* positiveparent; |
|---|
| 70 | |
|---|
| 71 | polyhedron* negativeparent; |
|---|
| 72 | |
|---|
| 73 | int message_counter; |
|---|
| 74 | |
|---|
| 75 | /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. |
|---|
| 76 | list<list<vertex*>> triangulations; |
|---|
| 77 | |
|---|
| 78 | /// A list of relative addresses serving for Hasse diagram construction. |
|---|
| 79 | list<int> kids_rel_addresses; |
|---|
| 80 | |
|---|
| 81 | /// Default constructor |
|---|
| 82 | polyhedron() |
|---|
| 83 | { |
|---|
| 84 | multiplicity = 1; |
|---|
| 85 | |
|---|
| 86 | message_counter = 0; |
|---|
| 87 | |
|---|
| 88 | totally_neutral = NULL; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /// Setter for raising multiplicity |
|---|
| 92 | void raise_multiplicity() |
|---|
| 93 | { |
|---|
| 94 | multiplicity++; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /// Setter for lowering multiplicity |
|---|
| 98 | void lower_multiplicity() |
|---|
| 99 | { |
|---|
| 100 | multiplicity--; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
|---|
| 104 | int operator==(polyhedron polyhedron2) |
|---|
| 105 | { |
|---|
| 106 | return true; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
|---|
| 110 | int operator<(polyhedron polyhedron2) |
|---|
| 111 | { |
|---|
| 112 | return false; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | void set_state(double state_indicator, actions action) |
|---|
| 118 | { |
|---|
| 119 | switch(action) |
|---|
| 120 | { |
|---|
| 121 | case MERGE: |
|---|
| 122 | merge_state = (int)sign(state_indicator); |
|---|
| 123 | break; |
|---|
| 124 | case SPLIT: |
|---|
| 125 | split_state = (int)sign(state_indicator); |
|---|
| 126 | break; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | int get_state(actions action) |
|---|
| 131 | { |
|---|
| 132 | switch(action) |
|---|
| 133 | { |
|---|
| 134 | case MERGE: |
|---|
| 135 | return merge_state; |
|---|
| 136 | break; |
|---|
| 137 | case SPLIT: |
|---|
| 138 | return split_state; |
|---|
| 139 | break; |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | int number_of_children() |
|---|
| 144 | { |
|---|
| 145 | return children.size(); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse |
|---|
| 152 | /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. |
|---|
| 153 | class vertex : public polyhedron |
|---|
| 154 | { |
|---|
| 155 | /// A dynamic array representing coordinates of the vertex |
|---|
| 156 | vec coordinates; |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | public: |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | /// Default constructor |
|---|
| 165 | vertex(); |
|---|
| 166 | |
|---|
| 167 | /// Constructor of a vertex from a set of coordinates |
|---|
| 168 | vertex(vec coordinates) |
|---|
| 169 | { |
|---|
| 170 | this->coordinates = coordinates; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter |
|---|
| 174 | /// space of certain dimension is established, but the dimension is not known when the vertex is created. |
|---|
| 175 | void push_coordinate(double coordinate) |
|---|
| 176 | { |
|---|
| 177 | coordinates = concat(coordinates,coordinate); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer |
|---|
| 181 | /// (not given by reference), but a new copy is created (they are given by value). |
|---|
| 182 | vec get_coordinates() |
|---|
| 183 | { |
|---|
| 184 | return coordinates; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | }; |
|---|
| 189 | |
|---|
| 190 | /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates |
|---|
| 191 | /// it from polyhedrons in other rows. |
|---|
| 192 | class toprow : public polyhedron |
|---|
| 193 | { |
|---|
| 194 | |
|---|
| 195 | public: |
|---|
| 196 | /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation |
|---|
| 197 | vec condition; |
|---|
| 198 | |
|---|
| 199 | /// Default constructor |
|---|
| 200 | toprow(){}; |
|---|
| 201 | |
|---|
| 202 | /// Constructor creating a toprow from the condition |
|---|
| 203 | toprow(vec condition) |
|---|
| 204 | { |
|---|
| 205 | this->condition = condition; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | }; |
|---|
| 209 | |
|---|
| 210 | class condition |
|---|
| 211 | { |
|---|
| 212 | public: |
|---|
| 213 | vec value; |
|---|
| 214 | |
|---|
| 215 | int multiplicity; |
|---|
| 216 | |
|---|
| 217 | condition(vec value) |
|---|
| 218 | { |
|---|
| 219 | this->value = value; |
|---|
| 220 | multiplicity = 1; |
|---|
| 221 | } |
|---|
| 222 | }; |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density |
|---|
| 226 | class emlig // : eEF |
|---|
| 227 | { |
|---|
| 228 | |
|---|
| 229 | /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result |
|---|
| 230 | /// of data update from Bayesian estimation or set by the user if this emlig is a prior density |
|---|
| 231 | vector<list<polyhedron*>> statistic; |
|---|
| 232 | |
|---|
| 233 | vector<list<polyhedron*>> for_splitting; |
|---|
| 234 | |
|---|
| 235 | vector<list<polyhedron*>> for_merging; |
|---|
| 236 | |
|---|
| 237 | list<condition*> conditions; |
|---|
| 238 | |
|---|
| 239 | double normalization_factor; |
|---|
| 240 | |
|---|
| 241 | void alter_toprow_conditions(vec condition, bool should_be_added) |
|---|
| 242 | { |
|---|
| 243 | for(list<polyhedron*>::iterator horiz_ref = (statistic.end()--)->begin();horiz_ref!=(statistic.end()--)->end();horiz_ref++) |
|---|
| 244 | { |
|---|
| 245 | double product = 0; |
|---|
| 246 | |
|---|
| 247 | list<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin(); |
|---|
| 248 | |
|---|
| 249 | do |
|---|
| 250 | { |
|---|
| 251 | product = (*vertex_ref)->get_coordinates()*condition; |
|---|
| 252 | } |
|---|
| 253 | while(product == 0); |
|---|
| 254 | |
|---|
| 255 | if((product>0 && should_be_added)||(product<0 && !should_be_added)) |
|---|
| 256 | { |
|---|
| 257 | ((toprow*) (*horiz_ref))->condition += condition; |
|---|
| 258 | } |
|---|
| 259 | else |
|---|
| 260 | { |
|---|
| 261 | ((toprow*) (*horiz_ref))->condition -= condition; |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | void send_state_message(polyhedron* sender, vec toadd, vec toremove, int level) |
|---|
| 268 | { |
|---|
| 269 | |
|---|
| 270 | bool shouldmerge = (toremove.size() != 0); |
|---|
| 271 | bool shouldsplit = (toadd.size() != 0); |
|---|
| 272 | |
|---|
| 273 | if(shouldsplit||shouldmerge) |
|---|
| 274 | { |
|---|
| 275 | for(list<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator!=sender->parents.end();parent_iterator++) |
|---|
| 276 | { |
|---|
| 277 | polyhedron* current_parent = *parent_iterator; |
|---|
| 278 | |
|---|
| 279 | current_parent->message_counter++; |
|---|
| 280 | |
|---|
| 281 | bool is_last = (current_parent->message_counter == current_parent->number_of_children()); |
|---|
| 282 | |
|---|
| 283 | if(shouldmerge) |
|---|
| 284 | { |
|---|
| 285 | int child_state = sender->get_state(MERGE); |
|---|
| 286 | int parent_state = current_parent->get_state(MERGE); |
|---|
| 287 | |
|---|
| 288 | if(parent_state == 0) |
|---|
| 289 | { |
|---|
| 290 | current_parent->set_state(child_state, MERGE); |
|---|
| 291 | |
|---|
| 292 | if(child_state == 0) |
|---|
| 293 | { |
|---|
| 294 | current_parent->mergechildren.push_back(sender); |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | else |
|---|
| 298 | { |
|---|
| 299 | if(child_state == 0) |
|---|
| 300 | { |
|---|
| 301 | if(parent_state > 0) |
|---|
| 302 | { |
|---|
| 303 | sender->positiveparent = current_parent; |
|---|
| 304 | } |
|---|
| 305 | else |
|---|
| 306 | { |
|---|
| 307 | sender->negativeparent = current_parent; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | if(is_last) |
|---|
| 313 | { |
|---|
| 314 | if(parent_state > 0) |
|---|
| 315 | { |
|---|
| 316 | for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) |
|---|
| 317 | { |
|---|
| 318 | (*merge_child)->positiveparent = current_parent; |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | if(parent_state < 0) |
|---|
| 323 | { |
|---|
| 324 | for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) |
|---|
| 325 | { |
|---|
| 326 | (*merge_child)->negativeparent = current_parent; |
|---|
| 327 | } |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | if(parent_state == 0) |
|---|
| 331 | { |
|---|
| 332 | for_merging[level+1].push_back(current_parent); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | current_parent->mergechildren.clear(); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | if(shouldsplit) |
|---|
| 342 | { |
|---|
| 343 | current_parent->totallyneutralgrandchildren.insert(current_parent->totallyneutralgrandchildren.end(),sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); |
|---|
| 344 | |
|---|
| 345 | switch(sender->get_state(SPLIT)) |
|---|
| 346 | { |
|---|
| 347 | case 1: |
|---|
| 348 | current_parent->positivechildren.push_back(sender); |
|---|
| 349 | break; |
|---|
| 350 | case 0: |
|---|
| 351 | current_parent->neutralchildren.push_back(sender); |
|---|
| 352 | |
|---|
| 353 | if(current_parent->totally_neutral == NULL) |
|---|
| 354 | { |
|---|
| 355 | current_parent->totally_neutral = sender->totally_neutral; |
|---|
| 356 | } |
|---|
| 357 | else |
|---|
| 358 | { |
|---|
| 359 | current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | if(sender->totally_neutral) |
|---|
| 363 | { |
|---|
| 364 | current_parent->totallyneutralchildren.push_back(sender); |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | break; |
|---|
| 368 | case -1: |
|---|
| 369 | current_parent->negativechildren.push_back(sender); |
|---|
| 370 | break; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | if(is_last) |
|---|
| 374 | { |
|---|
| 375 | unique(current_parent->totallyneutralgrandchildren.begin(),current_parent->totallyneutralgrandchildren.end()); |
|---|
| 376 | |
|---|
| 377 | if((current_parent->negativechildren.size()>0&¤t_parent->positivechildren.size()>0)|| |
|---|
| 378 | (current_parent->neutralchildren.size()>0&¤t_parent->totally_neutral==false)) |
|---|
| 379 | { |
|---|
| 380 | |
|---|
| 381 | for_splitting[level+1].push_back(current_parent); |
|---|
| 382 | |
|---|
| 383 | current_parent->set_state(0, SPLIT); |
|---|
| 384 | } |
|---|
| 385 | else |
|---|
| 386 | { |
|---|
| 387 | if(current_parent->negativechildren.size()>0) |
|---|
| 388 | { |
|---|
| 389 | current_parent->set_state(-1, SPLIT); |
|---|
| 390 | |
|---|
| 391 | ((toprow*)current_parent)->condition-=toadd; |
|---|
| 392 | } |
|---|
| 393 | else if(current_parent->positivechildren.size()>0) |
|---|
| 394 | { |
|---|
| 395 | current_parent->set_state(1, SPLIT); |
|---|
| 396 | |
|---|
| 397 | ((toprow*)current_parent)->condition+=toadd; |
|---|
| 398 | } |
|---|
| 399 | else |
|---|
| 400 | { |
|---|
| 401 | current_parent->raise_multiplicity(); |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | current_parent->positivechildren.clear(); |
|---|
| 405 | current_parent->negativechildren.clear(); |
|---|
| 406 | current_parent->neutralchildren.clear(); |
|---|
| 407 | current_parent->totallyneutralchildren.clear(); |
|---|
| 408 | current_parent->totallyneutralgrandchildren.clear(); |
|---|
| 409 | current_parent->totally_neutral = NULL; |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | if(is_last) |
|---|
| 415 | { |
|---|
| 416 | send_state_message(current_parent,toadd,toremove,level+1); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | public: |
|---|
| 425 | |
|---|
| 426 | /// A default constructor creates an emlig with predefined statistic representing only the range of the given |
|---|
| 427 | /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. |
|---|
| 428 | emlig(int number_of_parameters) |
|---|
| 429 | { |
|---|
| 430 | create_statistic(number_of_parameters); |
|---|
| 431 | |
|---|
| 432 | for(vector<list<polyhedron*>>::iterator local_iter = statistic.begin();local_iter<statistic.end();local_iter++) |
|---|
| 433 | { |
|---|
| 434 | list<polyhedron*> empty_split; |
|---|
| 435 | list<polyhedron*> empty_merge; |
|---|
| 436 | |
|---|
| 437 | for_splitting.push_back(empty_split); |
|---|
| 438 | for_merging.push_back(empty_merge); |
|---|
| 439 | } |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a |
|---|
| 443 | /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. |
|---|
| 444 | emlig(vector<list<polyhedron*>> statistic) |
|---|
| 445 | { |
|---|
| 446 | this->statistic = statistic; |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | void add_condition(vec toadd) |
|---|
| 450 | { |
|---|
| 451 | vec null_vector = ""; |
|---|
| 452 | |
|---|
| 453 | add_and_remove_condition(toadd, null_vector); |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | void remove_condition(vec toremove) |
|---|
| 457 | { |
|---|
| 458 | vec null_vector = ""; |
|---|
| 459 | |
|---|
| 460 | add_and_remove_condition(null_vector, toremove); |
|---|
| 461 | |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | void add_and_remove_condition(vec toadd, vec toremove) |
|---|
| 465 | { |
|---|
| 466 | bool should_remove = (toremove.size() != 0); |
|---|
| 467 | bool should_add = (toadd.size() != 0); |
|---|
| 468 | |
|---|
| 469 | list<condition*>::iterator toremove_ref = conditions.end(); |
|---|
| 470 | bool condition_should_be_added = false; |
|---|
| 471 | |
|---|
| 472 | for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++) |
|---|
| 473 | { |
|---|
| 474 | if(should_remove) |
|---|
| 475 | { |
|---|
| 476 | if((*ref)->value == toremove) |
|---|
| 477 | { |
|---|
| 478 | if((*ref)->multiplicity>1) |
|---|
| 479 | { |
|---|
| 480 | (*ref)->multiplicity--; |
|---|
| 481 | |
|---|
| 482 | alter_toprow_conditions(toremove,false); |
|---|
| 483 | |
|---|
| 484 | should_remove = false; |
|---|
| 485 | } |
|---|
| 486 | else |
|---|
| 487 | { |
|---|
| 488 | toremove_ref = ref; |
|---|
| 489 | } |
|---|
| 490 | } |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | if(should_add) |
|---|
| 494 | { |
|---|
| 495 | if((*ref)->value == toadd) |
|---|
| 496 | { |
|---|
| 497 | (*ref)->multiplicity++; |
|---|
| 498 | |
|---|
| 499 | alter_toprow_conditions(toadd,true); |
|---|
| 500 | |
|---|
| 501 | should_add = false; |
|---|
| 502 | } |
|---|
| 503 | else |
|---|
| 504 | { |
|---|
| 505 | condition_should_be_added = true; |
|---|
| 506 | } |
|---|
| 507 | } |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | if(toremove_ref!=conditions.end()) |
|---|
| 511 | { |
|---|
| 512 | conditions.erase(toremove_ref); |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | if(condition_should_be_added) |
|---|
| 516 | { |
|---|
| 517 | conditions.push_back(new condition(toadd)); |
|---|
| 518 | } |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | for(list<polyhedron*>::iterator horizontal_position = statistic.begin()->begin();horizontal_position!=statistic.begin()->end();horizontal_position++) |
|---|
| 523 | { |
|---|
| 524 | vertex* current_vertex = (vertex*)(*horizontal_position); |
|---|
| 525 | |
|---|
| 526 | if(should_add||should_remove) |
|---|
| 527 | { |
|---|
| 528 | vec appended_vec = current_vertex->get_coordinates(); |
|---|
| 529 | appended_vec.ins(0,-1.0); |
|---|
| 530 | |
|---|
| 531 | if(should_add) |
|---|
| 532 | { |
|---|
| 533 | double local_condition = toadd*appended_vec; |
|---|
| 534 | |
|---|
| 535 | current_vertex->set_state(local_condition,SPLIT); |
|---|
| 536 | |
|---|
| 537 | if(local_condition == 0) |
|---|
| 538 | { |
|---|
| 539 | current_vertex->totally_neutral = true; |
|---|
| 540 | |
|---|
| 541 | current_vertex->raise_multiplicity(); |
|---|
| 542 | } |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | if(should_remove) |
|---|
| 546 | { |
|---|
| 547 | double local_condition = toremove*appended_vec; |
|---|
| 548 | |
|---|
| 549 | current_vertex->set_state(local_condition,MERGE); |
|---|
| 550 | |
|---|
| 551 | if(local_condition == 0) |
|---|
| 552 | { |
|---|
| 553 | for_merging[0].push_back(current_vertex); |
|---|
| 554 | } |
|---|
| 555 | } |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | send_state_message(current_vertex, toadd, toremove, 0); |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | if(should_add) |
|---|
| 562 | { |
|---|
| 563 | for(vector<list<polyhedron*>>::iterator vert_ref = for_splitting.begin();vert_ref<for_splitting.end();vert_ref++) |
|---|
| 564 | { |
|---|
| 565 | |
|---|
| 566 | for(list<polyhedron*>::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++) |
|---|
| 567 | { |
|---|
| 568 | polyhedron* new_totally_neutral_child; |
|---|
| 569 | |
|---|
| 570 | polyhedron* current_polyhedron = (*split_ref); |
|---|
| 571 | |
|---|
| 572 | if(vert_ref == for_splitting.begin()) |
|---|
| 573 | { |
|---|
| 574 | vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates(); |
|---|
| 575 | vec coordinates2 = ((vertex*)(*(current_polyhedron->children.begin()++)))->get_coordinates(); |
|---|
| 576 | coordinates2.ins(0,-1.0); |
|---|
| 577 | |
|---|
| 578 | double t = (-toadd*coordinates2)/(toadd(1,toadd.size()-1)*coordinates1)+1; |
|---|
| 579 | |
|---|
| 580 | vec new_coordinates = coordinates1*t+(coordinates2(1,coordinates2.size()-1)-coordinates1); |
|---|
| 581 | |
|---|
| 582 | vertex* neutral_vertex = new vertex(new_coordinates); |
|---|
| 583 | |
|---|
| 584 | new_totally_neutral_child = neutral_vertex; |
|---|
| 585 | } |
|---|
| 586 | else |
|---|
| 587 | { |
|---|
| 588 | toprow* neutral_toprow = new toprow(); |
|---|
| 589 | |
|---|
| 590 | new_totally_neutral_child = neutral_toprow; |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(), |
|---|
| 594 | current_polyhedron->totallyneutralgrandchildren.begin(), |
|---|
| 595 | current_polyhedron->totallyneutralgrandchildren.end()); |
|---|
| 596 | |
|---|
| 597 | toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition+toadd); |
|---|
| 598 | toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition-toadd); |
|---|
| 599 | |
|---|
| 600 | positive_poly->children.push_back(new_totally_neutral_child); |
|---|
| 601 | negative_poly->children.push_back(new_totally_neutral_child); |
|---|
| 602 | |
|---|
| 603 | positive_poly->parents.insert(positive_poly->parents.end(), |
|---|
| 604 | current_polyhedron->parents.begin(), |
|---|
| 605 | current_polyhedron->parents.end()); |
|---|
| 606 | |
|---|
| 607 | negative_poly->parents.insert(negative_poly->parents.end(), |
|---|
| 608 | current_polyhedron->parents.begin(), |
|---|
| 609 | current_polyhedron->parents.end()); |
|---|
| 610 | |
|---|
| 611 | positive_poly->children.insert(positive_poly->children.end(), |
|---|
| 612 | current_polyhedron->positivechildren.begin(), |
|---|
| 613 | current_polyhedron->positivechildren.end()); |
|---|
| 614 | |
|---|
| 615 | negative_poly->children.insert(negative_poly->children.end(), |
|---|
| 616 | current_polyhedron->negativechildren.begin(), |
|---|
| 617 | current_polyhedron->negativechildren.end()); |
|---|
| 618 | |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | |
|---|
| 624 | } |
|---|
| 625 | } |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | protected: |
|---|
| 632 | |
|---|
| 633 | /// A method for creating plain default statistic representing only the range of the parameter space. |
|---|
| 634 | void create_statistic(int number_of_parameters) |
|---|
| 635 | { |
|---|
| 636 | // An empty vector of coordinates. |
|---|
| 637 | vec origin_coord; |
|---|
| 638 | |
|---|
| 639 | // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. |
|---|
| 640 | vertex *origin = new vertex(origin_coord); |
|---|
| 641 | |
|---|
| 642 | // It has itself as a vertex. There will be a nice use for this when the vertices of its parents are searched in |
|---|
| 643 | // the recursive creation procedure below. |
|---|
| 644 | origin->vertices.push_back(origin); |
|---|
| 645 | |
|---|
| 646 | // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse |
|---|
| 647 | // diagram. First we create a vector of polyhedrons.. |
|---|
| 648 | list<polyhedron*> origin_vec; |
|---|
| 649 | |
|---|
| 650 | // ..we fill it with the origin.. |
|---|
| 651 | origin_vec.push_back(origin); |
|---|
| 652 | |
|---|
| 653 | // ..and we fill the statistic with the created vector. |
|---|
| 654 | statistic.push_back(origin_vec); |
|---|
| 655 | |
|---|
| 656 | // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to |
|---|
| 657 | // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: |
|---|
| 658 | for(int i=0;i<number_of_parameters;i++) |
|---|
| 659 | { |
|---|
| 660 | // We first will create two new vertices. These will be the borders of the parameter space in the dimension |
|---|
| 661 | // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the |
|---|
| 662 | // right amount of zero cooridnates by reading them from the origin |
|---|
| 663 | vec origin_coord = origin->get_coordinates(); |
|---|
| 664 | |
|---|
| 665 | // And we incorporate the nonzero coordinates into the new cooordinate vectors |
|---|
| 666 | vec origin_coord1 = concat(origin_coord,max_range); |
|---|
| 667 | vec origin_coord2 = concat(origin_coord,-max_range); |
|---|
| 668 | |
|---|
| 669 | // Now we create the points |
|---|
| 670 | vertex *new_point1 = new vertex(origin_coord1); |
|---|
| 671 | vertex *new_point2 = new vertex(origin_coord2); |
|---|
| 672 | |
|---|
| 673 | //********************************************************************************************************* |
|---|
| 674 | // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old |
|---|
| 675 | // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points |
|---|
| 676 | // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons |
|---|
| 677 | // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is |
|---|
| 678 | // connected to the first (second) newly created vertex by a parent-child relation. |
|---|
| 679 | //********************************************************************************************************* |
|---|
| 680 | |
|---|
| 681 | |
|---|
| 682 | // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram |
|---|
| 683 | vector<vector<polyhedron*>> new_statistic1; |
|---|
| 684 | vector<vector<polyhedron*>> new_statistic2; |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | // Copy the statistic by rows |
|---|
| 688 | for(int j=0;j<statistic.size();j++) |
|---|
| 689 | { |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | // an element counter |
|---|
| 693 | int element_number = 0; |
|---|
| 694 | |
|---|
| 695 | vector<polyhedron*> supportnew_1; |
|---|
| 696 | vector<polyhedron*> supportnew_2; |
|---|
| 697 | |
|---|
| 698 | new_statistic1.push_back(supportnew_1); |
|---|
| 699 | new_statistic2.push_back(supportnew_2); |
|---|
| 700 | |
|---|
| 701 | // for each polyhedron in the given row |
|---|
| 702 | for(list<polyhedron*>::iterator horiz_ref = (statistic.begin()+j)->begin();horiz_ref!=(statistic.begin()+j)->end();horiz_ref++) |
|---|
| 703 | { |
|---|
| 704 | // Append an extra zero coordinate to each of the vertices for the new dimension |
|---|
| 705 | // If vert_ref is at the first index => we loop through vertices |
|---|
| 706 | if(j == 0) |
|---|
| 707 | { |
|---|
| 708 | // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates |
|---|
| 709 | ((vertex*) (*horiz_ref))->push_coordinate(0); |
|---|
| 710 | } |
|---|
| 711 | /* |
|---|
| 712 | else |
|---|
| 713 | { |
|---|
| 714 | ((toprow*) (*horiz_ref))->condition.ins(0,0); |
|---|
| 715 | }*/ |
|---|
| 716 | |
|---|
| 717 | // if it has parents |
|---|
| 718 | if(!(*horiz_ref)->parents.empty()) |
|---|
| 719 | { |
|---|
| 720 | // save the relative address of this child in a vector kids_rel_addresses of all its parents. |
|---|
| 721 | // This information will later be used for copying the whole Hasse diagram with each of the |
|---|
| 722 | // relations contained within. |
|---|
| 723 | for(list<polyhedron*>::iterator parent_ref = (*horiz_ref)->parents.begin();parent_ref != (*horiz_ref)->parents.end();parent_ref++) |
|---|
| 724 | { |
|---|
| 725 | (*parent_ref)->kids_rel_addresses.push_back(element_number); |
|---|
| 726 | } |
|---|
| 727 | } |
|---|
| 728 | |
|---|
| 729 | // ************************************************************************************************** |
|---|
| 730 | // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron |
|---|
| 731 | // will be created as a toprow, but this information will be later forgotten and only the polyhedrons |
|---|
| 732 | // in the top row of the Hasse diagram will be considered toprow for later use. |
|---|
| 733 | // ************************************************************************************************** |
|---|
| 734 | |
|---|
| 735 | // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic |
|---|
| 736 | // this condition will be a vector of zeros. There are two vectors, because we need two copies of |
|---|
| 737 | // the original Hasse diagram. |
|---|
| 738 | vec vec1(number_of_parameters+1); |
|---|
| 739 | vec1.zeros(); |
|---|
| 740 | |
|---|
| 741 | vec vec2(number_of_parameters+1); |
|---|
| 742 | vec2.zeros(); |
|---|
| 743 | |
|---|
| 744 | // We create a new toprow with the previously specified condition. |
|---|
| 745 | toprow* current_copy1 = new toprow(vec1); |
|---|
| 746 | toprow* current_copy2 = new toprow(vec2); |
|---|
| 747 | |
|---|
| 748 | // The vertices of the copies will be inherited, because there will be a parent/child relation |
|---|
| 749 | // between each polyhedron and its offspring (comming from the copy) and a parent has all the |
|---|
| 750 | // vertices of its child plus more. |
|---|
| 751 | for(list<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin();vertex_ref!=(*horiz_ref)->vertices.end();vertex_ref++) |
|---|
| 752 | { |
|---|
| 753 | current_copy1->vertices.push_back(*vertex_ref); |
|---|
| 754 | current_copy2->vertices.push_back(*vertex_ref); |
|---|
| 755 | } |
|---|
| 756 | |
|---|
| 757 | // The only new vertex of the offspring should be the newly created point. |
|---|
| 758 | current_copy1->vertices.push_back(new_point1); |
|---|
| 759 | current_copy2->vertices.push_back(new_point2); |
|---|
| 760 | |
|---|
| 761 | // This method guarantees that each polyhedron is already triangulated, therefore its triangulation |
|---|
| 762 | // is only one set of vertices and it is the set of all its vertices. |
|---|
| 763 | current_copy1->triangulations.push_back(current_copy1->vertices); |
|---|
| 764 | current_copy2->triangulations.push_back(current_copy2->vertices); |
|---|
| 765 | |
|---|
| 766 | // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying |
|---|
| 767 | // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the |
|---|
| 768 | // kids and when we do that and know the child, in the child we will remember the parent we came from. |
|---|
| 769 | // This way all the parents/children relations are saved in both the parent and the child. |
|---|
| 770 | if(!(*horiz_ref)->kids_rel_addresses.empty()) |
|---|
| 771 | { |
|---|
| 772 | for(list<int>::iterator kid_ref = (*horiz_ref)->kids_rel_addresses.begin();kid_ref!=(*horiz_ref)->kids_rel_addresses.end();kid_ref++) |
|---|
| 773 | { |
|---|
| 774 | // find the child and save the relation to the parent |
|---|
| 775 | current_copy1->children.push_back(new_statistic1[j-1][(*kid_ref)]); |
|---|
| 776 | current_copy2->children.push_back(new_statistic2[j-1][(*kid_ref)]); |
|---|
| 777 | |
|---|
| 778 | // in the child save the parents' address |
|---|
| 779 | new_statistic1[j-1][(*kid_ref)]->parents.push_back(current_copy1); |
|---|
| 780 | new_statistic2[j-1][(*kid_ref)]->parents.push_back(current_copy2); |
|---|
| 781 | } |
|---|
| 782 | |
|---|
| 783 | // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the |
|---|
| 784 | // Hasse diagram again) |
|---|
| 785 | (*horiz_ref)->kids_rel_addresses.clear(); |
|---|
| 786 | } |
|---|
| 787 | // If there were no children previously, we are copying a polyhedron that has been a vertex before. |
|---|
| 788 | // In this case it is a segment now and it will have a relation to its mother (copywise) and to the |
|---|
| 789 | // newly created point. Here we create the connection to the new point, again from both sides. |
|---|
| 790 | else |
|---|
| 791 | { |
|---|
| 792 | // Add the address of the new point in the former vertex |
|---|
| 793 | current_copy1->children.push_back(new_point1); |
|---|
| 794 | current_copy2->children.push_back(new_point2); |
|---|
| 795 | |
|---|
| 796 | // Add the address of the former vertex in the new point |
|---|
| 797 | new_point1->parents.push_back(current_copy1); |
|---|
| 798 | new_point2->parents.push_back(current_copy2); |
|---|
| 799 | } |
|---|
| 800 | |
|---|
| 801 | // Save the mother in its offspring |
|---|
| 802 | current_copy1->children.push_back(*horiz_ref); |
|---|
| 803 | current_copy2->children.push_back(*horiz_ref); |
|---|
| 804 | |
|---|
| 805 | // Save the offspring in its mother |
|---|
| 806 | (*horiz_ref)->parents.push_back(current_copy1); |
|---|
| 807 | (*horiz_ref)->parents.push_back(current_copy2); |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | // Add the copies into the relevant statistic. The statistic will later be appended to the previous |
|---|
| 811 | // Hasse diagram |
|---|
| 812 | new_statistic1[j].push_back(current_copy1); |
|---|
| 813 | new_statistic2[j].push_back(current_copy2); |
|---|
| 814 | |
|---|
| 815 | // Raise the count in the vector of polyhedrons |
|---|
| 816 | element_number++; |
|---|
| 817 | |
|---|
| 818 | } |
|---|
| 819 | |
|---|
| 820 | } |
|---|
| 821 | |
|---|
| 822 | statistic.begin()->push_back(new_point1); |
|---|
| 823 | statistic.begin()->push_back(new_point2); |
|---|
| 824 | |
|---|
| 825 | // Merge the new statistics into the old one. This will either be the final statistic or we will |
|---|
| 826 | // reenter the widening loop. |
|---|
| 827 | for(int j=0;j<new_statistic1.size();j++) |
|---|
| 828 | { |
|---|
| 829 | if(j+1==statistic.size()) |
|---|
| 830 | { |
|---|
| 831 | list<polyhedron*> support; |
|---|
| 832 | statistic.push_back(support); |
|---|
| 833 | } |
|---|
| 834 | |
|---|
| 835 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); |
|---|
| 836 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end()); |
|---|
| 837 | } |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | } |
|---|
| 841 | |
|---|
| 842 | vector<list<toprow*>> toprow_statistic; |
|---|
| 843 | int line_count = 0; |
|---|
| 844 | |
|---|
| 845 | for(vector<list<polyhedron*>>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++) |
|---|
| 846 | { |
|---|
| 847 | list<toprow*> support_list; |
|---|
| 848 | toprow_statistic.push_back(support_list); |
|---|
| 849 | |
|---|
| 850 | for(list<polyhedron*>::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++) |
|---|
| 851 | { |
|---|
| 852 | toprow* support_top = (toprow*)(*polyhedron_ref2); |
|---|
| 853 | |
|---|
| 854 | toprow_statistic[line_count].push_back(support_top); |
|---|
| 855 | } |
|---|
| 856 | |
|---|
| 857 | line_count++; |
|---|
| 858 | } |
|---|
| 859 | } |
|---|
| 860 | |
|---|
| 861 | |
|---|
| 862 | |
|---|
| 863 | |
|---|
| 864 | }; |
|---|
| 865 | |
|---|
| 866 | /* |
|---|
| 867 | |
|---|
| 868 | //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density |
|---|
| 869 | class RARX : public BM |
|---|
| 870 | { |
|---|
| 871 | private: |
|---|
| 872 | |
|---|
| 873 | emlig posterior; |
|---|
| 874 | |
|---|
| 875 | public: |
|---|
| 876 | RARX():BM() |
|---|
| 877 | { |
|---|
| 878 | }; |
|---|
| 879 | |
|---|
| 880 | void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec) |
|---|
| 881 | { |
|---|
| 882 | |
|---|
| 883 | } |
|---|
| 884 | |
|---|
| 885 | };*/ |
|---|
| 886 | |
|---|
| 887 | |
|---|
| 888 | |
|---|
| 889 | #endif //TRAGE_H |
|---|