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