[1266] | 1 | /*! |
---|
| 2 | \file |
---|
| 3 | \brief Robust Bayesian auto-regression model |
---|
| 4 | \author Jan Sindelar. |
---|
| 5 | */ |
---|
[1262] | 6 | |
---|
[1266] | 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 <map> |
---|
| 15 | #include <set> |
---|
| 16 | #include <algorithm> |
---|
| 17 | #include <string> |
---|
| 18 | |
---|
| 19 | using namespace bdm; |
---|
| 20 | using namespace std; |
---|
| 21 | using namespace itpp; |
---|
| 22 | |
---|
| 23 | const double max_range = 1000.0;//numeric_limits<double>::max()/10e-10; |
---|
| 24 | |
---|
| 25 | enum actions {MERGE, SPLIT}; |
---|
| 26 | |
---|
| 27 | class polyhedron; |
---|
| 28 | class vertex; |
---|
| 29 | class toprow; |
---|
| 30 | |
---|
| 31 | /* |
---|
| 32 | class t_simplex |
---|
| 33 | { |
---|
| 34 | public: |
---|
| 35 | set<vertex*> minima; |
---|
| 36 | |
---|
| 37 | set<vertex*> simplex; |
---|
| 38 | |
---|
| 39 | t_simplex(vertex* origin_vertex) |
---|
| 40 | { |
---|
| 41 | simplex.insert(origin_vertex); |
---|
| 42 | minima.insert(origin_vertex); |
---|
| 43 | } |
---|
| 44 | };*/ |
---|
| 45 | |
---|
| 46 | /// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram |
---|
| 47 | /// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created. |
---|
| 48 | class polyhedron |
---|
| 49 | { |
---|
| 50 | /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of |
---|
| 51 | /// more than just the necessary number of conditions. For example if a newly created line passes through an already |
---|
| 52 | /// existing point, the points multiplicity will rise by 1. |
---|
| 53 | int multiplicity; |
---|
| 54 | |
---|
| 55 | int split_state; |
---|
| 56 | |
---|
| 57 | int merge_state; |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | public: |
---|
| 62 | /// A list of polyhedrons parents within the Hasse diagram. |
---|
| 63 | list<polyhedron*> parents; |
---|
| 64 | |
---|
| 65 | /// A list of polyhedrons children withing the Hasse diagram. |
---|
| 66 | list<polyhedron*> children; |
---|
| 67 | |
---|
| 68 | /// All the vertices of the given polyhedron |
---|
| 69 | set<vertex*> vertices; |
---|
| 70 | |
---|
| 71 | /// A list used for storing children that lie in the positive region related to a certain condition |
---|
| 72 | list<polyhedron*> positivechildren; |
---|
| 73 | |
---|
| 74 | /// A list used for storing children that lie in the negative region related to a certain condition |
---|
| 75 | list<polyhedron*> negativechildren; |
---|
| 76 | |
---|
| 77 | /// Children intersecting the condition |
---|
| 78 | list<polyhedron*> neutralchildren; |
---|
| 79 | |
---|
| 80 | list<polyhedron*> totallyneutralgrandchildren; |
---|
| 81 | |
---|
| 82 | list<polyhedron*> totallyneutralchildren; |
---|
| 83 | |
---|
| 84 | set<vertex*> positiveneutralvertices; |
---|
| 85 | |
---|
| 86 | set<vertex*> negativeneutralvertices; |
---|
| 87 | |
---|
| 88 | bool totally_neutral; |
---|
| 89 | |
---|
| 90 | list<polyhedron*> mergechildren; |
---|
| 91 | |
---|
| 92 | polyhedron* positiveparent; |
---|
| 93 | |
---|
| 94 | polyhedron* negativeparent; |
---|
| 95 | |
---|
| 96 | polyhedron* next_poly; |
---|
| 97 | |
---|
| 98 | polyhedron* prev_poly; |
---|
| 99 | |
---|
| 100 | int message_counter; |
---|
| 101 | |
---|
| 102 | /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. |
---|
| 103 | list<set<vertex*>> triangulation; |
---|
| 104 | |
---|
| 105 | /// A list of relative addresses serving for Hasse diagram construction. |
---|
| 106 | list<int> kids_rel_addresses; |
---|
| 107 | |
---|
| 108 | /// Default constructor |
---|
| 109 | polyhedron() |
---|
| 110 | { |
---|
| 111 | multiplicity = 1; |
---|
| 112 | |
---|
| 113 | message_counter = 0; |
---|
| 114 | |
---|
| 115 | totally_neutral = NULL; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /// Setter for raising multiplicity |
---|
| 119 | void raise_multiplicity() |
---|
| 120 | { |
---|
| 121 | multiplicity++; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /// Setter for lowering multiplicity |
---|
| 125 | void lower_multiplicity() |
---|
| 126 | { |
---|
| 127 | multiplicity--; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
---|
| 131 | int operator==(polyhedron polyhedron2) |
---|
| 132 | { |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
---|
| 137 | int operator<(polyhedron polyhedron2) |
---|
| 138 | { |
---|
| 139 | return false; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | void set_state(double state_indicator, actions action) |
---|
| 145 | { |
---|
| 146 | switch(action) |
---|
| 147 | { |
---|
| 148 | case MERGE: |
---|
| 149 | merge_state = (int)sign(state_indicator); |
---|
| 150 | break; |
---|
| 151 | case SPLIT: |
---|
| 152 | split_state = (int)sign(state_indicator); |
---|
| 153 | break; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | int get_state(actions action) |
---|
| 158 | { |
---|
| 159 | switch(action) |
---|
| 160 | { |
---|
| 161 | case MERGE: |
---|
| 162 | return merge_state; |
---|
| 163 | break; |
---|
| 164 | case SPLIT: |
---|
| 165 | return split_state; |
---|
| 166 | break; |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | int number_of_children() |
---|
| 171 | { |
---|
| 172 | return children.size(); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void triangulate(bool should_integrate); |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | }; |
---|
| 180 | |
---|
| 181 | /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse |
---|
| 182 | /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. |
---|
| 183 | class vertex : public polyhedron |
---|
| 184 | { |
---|
| 185 | /// A dynamic array representing coordinates of the vertex |
---|
| 186 | vec coordinates; |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | public: |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | /// Default constructor |
---|
| 195 | vertex(); |
---|
| 196 | |
---|
| 197 | /// Constructor of a vertex from a set of coordinates |
---|
| 198 | vertex(vec coordinates) |
---|
| 199 | { |
---|
| 200 | this->coordinates = coordinates; |
---|
| 201 | |
---|
| 202 | vertices.insert(this); |
---|
| 203 | |
---|
| 204 | set<vertex*> vert_simplex; |
---|
| 205 | |
---|
| 206 | vert_simplex.insert(this); |
---|
| 207 | |
---|
| 208 | triangulation.push_back(vert_simplex); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter |
---|
| 212 | /// space of certain dimension is established, but the dimension is not known when the vertex is created. |
---|
| 213 | void push_coordinate(double coordinate) |
---|
| 214 | { |
---|
| 215 | coordinates = concat(coordinates,coordinate); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer |
---|
| 219 | /// (not given by reference), but a new copy is created (they are given by value). |
---|
| 220 | vec get_coordinates() |
---|
| 221 | { |
---|
| 222 | return coordinates; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | }; |
---|
| 227 | |
---|
| 228 | /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates |
---|
| 229 | /// it from polyhedrons in other rows. |
---|
| 230 | class toprow : public polyhedron |
---|
| 231 | { |
---|
| 232 | |
---|
| 233 | public: |
---|
| 234 | double probability; |
---|
| 235 | |
---|
| 236 | /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation |
---|
| 237 | vec condition; |
---|
| 238 | |
---|
| 239 | int condition_order; |
---|
| 240 | |
---|
| 241 | /// Default constructor |
---|
| 242 | toprow(){}; |
---|
| 243 | |
---|
| 244 | /// Constructor creating a toprow from the condition |
---|
| 245 | toprow(vec condition, int condition_order) |
---|
| 246 | { |
---|
| 247 | this->condition = condition; |
---|
| 248 | this->condition_order = condition_order; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | }; |
---|
| 252 | |
---|
| 253 | class condition |
---|
| 254 | { |
---|
| 255 | public: |
---|
| 256 | vec value; |
---|
| 257 | |
---|
| 258 | int multiplicity; |
---|
| 259 | |
---|
| 260 | condition(vec value) |
---|
| 261 | { |
---|
| 262 | this->value = value; |
---|
| 263 | multiplicity = 1; |
---|
| 264 | } |
---|
| 265 | }; |
---|
| 266 | |
---|
| 267 | class c_statistic |
---|
| 268 | { |
---|
| 269 | polyhedron* end_poly; |
---|
| 270 | polyhedron* start_poly; |
---|
| 271 | |
---|
| 272 | public: |
---|
| 273 | vector<polyhedron*> rows; |
---|
| 274 | |
---|
| 275 | vector<polyhedron*> row_ends; |
---|
| 276 | |
---|
| 277 | c_statistic() |
---|
| 278 | { |
---|
| 279 | end_poly = new polyhedron(); |
---|
| 280 | start_poly = new polyhedron(); |
---|
| 281 | }; |
---|
| 282 | |
---|
| 283 | void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end) |
---|
| 284 | { |
---|
| 285 | if(row>((int)rows.size())-1) |
---|
| 286 | { |
---|
| 287 | if(row>rows.size()) |
---|
| 288 | { |
---|
| 289 | throw new exception("You are trying to append a polyhedron whose children are not in the statistic yet!"); |
---|
| 290 | return; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | rows.push_back(end_poly); |
---|
| 294 | row_ends.push_back(end_poly); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | // POSSIBLE FAILURE: the function is not checking if start and end are connected |
---|
| 298 | |
---|
| 299 | if(rows[row] != end_poly) |
---|
| 300 | { |
---|
| 301 | appended_start->prev_poly = row_ends[row]; |
---|
| 302 | row_ends[row]->next_poly = appended_start; |
---|
| 303 | |
---|
| 304 | } |
---|
| 305 | else if((row>0 && rows[row-1]!=end_poly)||row==0) |
---|
| 306 | { |
---|
| 307 | appended_start->prev_poly = start_poly; |
---|
| 308 | rows[row]= appended_start; |
---|
| 309 | } |
---|
| 310 | else |
---|
| 311 | { |
---|
| 312 | throw new exception("Wrong polyhedron insertion into statistic: missing intermediary polyhedron!"); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | appended_end->next_poly = end_poly; |
---|
| 316 | row_ends[row] = appended_end; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | void append_polyhedron(int row, polyhedron* appended_poly) |
---|
| 320 | { |
---|
| 321 | append_polyhedron(row,appended_poly,appended_poly); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | void insert_polyhedron(int row, polyhedron* inserted_poly, polyhedron* following_poly) |
---|
| 325 | { |
---|
| 326 | if(following_poly != end_poly) |
---|
| 327 | { |
---|
| 328 | inserted_poly->next_poly = following_poly; |
---|
| 329 | inserted_poly->prev_poly = following_poly->prev_poly; |
---|
| 330 | |
---|
| 331 | if(following_poly->prev_poly == start_poly) |
---|
| 332 | { |
---|
| 333 | rows[row] = inserted_poly; |
---|
| 334 | } |
---|
| 335 | else |
---|
| 336 | { |
---|
| 337 | inserted_poly->prev_poly->next_poly = inserted_poly; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | following_poly->prev_poly = inserted_poly; |
---|
| 341 | } |
---|
| 342 | else |
---|
| 343 | { |
---|
| 344 | this->append_polyhedron(row, inserted_poly); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | |
---|
| 352 | void delete_polyhedron(int row, polyhedron* deleted_poly) |
---|
| 353 | { |
---|
| 354 | if(deleted_poly->prev_poly != start_poly) |
---|
| 355 | { |
---|
| 356 | deleted_poly->prev_poly->next_poly = deleted_poly->next_poly; |
---|
| 357 | } |
---|
| 358 | else |
---|
| 359 | { |
---|
| 360 | rows[row] = deleted_poly->next_poly; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | if(deleted_poly->next_poly!=end_poly) |
---|
| 364 | { |
---|
| 365 | deleted_poly->next_poly->prev_poly = deleted_poly->prev_poly; |
---|
| 366 | } |
---|
| 367 | else |
---|
| 368 | { |
---|
| 369 | row_ends[row] = deleted_poly->prev_poly; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | |
---|
| 373 | |
---|
| 374 | deleted_poly->next_poly = NULL; |
---|
| 375 | deleted_poly->prev_poly = NULL; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | int size() |
---|
| 379 | { |
---|
| 380 | return rows.size(); |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | polyhedron* get_end() |
---|
| 384 | { |
---|
| 385 | return end_poly; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | polyhedron* get_start() |
---|
| 389 | { |
---|
| 390 | return start_poly; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | int row_size(int row) |
---|
| 394 | { |
---|
| 395 | if(this->size()>row && row>=0) |
---|
| 396 | { |
---|
| 397 | int row_size = 0; |
---|
| 398 | |
---|
| 399 | for(polyhedron* row_poly = rows[row]; row_poly!=end_poly; row_poly=row_poly->next_poly) |
---|
| 400 | { |
---|
| 401 | row_size++; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | return row_size; |
---|
| 405 | } |
---|
| 406 | else |
---|
| 407 | { |
---|
| 408 | throw new exception("There is no row to obtain size from!"); |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | }; |
---|
| 412 | |
---|
| 413 | |
---|
| 414 | //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density |
---|
| 415 | class emlig // : eEF |
---|
| 416 | { |
---|
| 417 | |
---|
| 418 | |
---|
| 419 | /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result |
---|
| 420 | /// of data update from Bayesian estimation or set by the user if this emlig is a prior density |
---|
| 421 | c_statistic statistic; |
---|
| 422 | |
---|
| 423 | vector<list<polyhedron*>> for_splitting; |
---|
| 424 | |
---|
| 425 | vector<list<polyhedron*>> for_merging; |
---|
| 426 | |
---|
| 427 | list<condition*> conditions; |
---|
| 428 | |
---|
| 429 | double normalization_factor; |
---|
| 430 | |
---|
| 431 | |
---|
| 432 | |
---|
| 433 | void alter_toprow_conditions(vec condition, bool should_be_added) |
---|
| 434 | { |
---|
| 435 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
| 436 | { |
---|
| 437 | double product = 0; |
---|
| 438 | |
---|
| 439 | set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); |
---|
| 440 | |
---|
| 441 | do |
---|
| 442 | { |
---|
| 443 | product = (*vertex_ref)->get_coordinates()*condition; |
---|
| 444 | } |
---|
| 445 | while(product == 0); |
---|
| 446 | |
---|
| 447 | if((product>0 && should_be_added)||(product<0 && !should_be_added)) |
---|
| 448 | { |
---|
| 449 | ((toprow*) horiz_ref)->condition += condition; |
---|
| 450 | } |
---|
| 451 | else |
---|
| 452 | { |
---|
| 453 | ((toprow*) horiz_ref)->condition -= condition; |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | |
---|
| 459 | |
---|
| 460 | void send_state_message(polyhedron* sender, vec toadd, vec toremove, int level) |
---|
| 461 | { |
---|
| 462 | |
---|
| 463 | bool shouldmerge = (toremove.size() != 0); |
---|
| 464 | bool shouldsplit = (toadd.size() != 0); |
---|
| 465 | |
---|
| 466 | if(shouldsplit||shouldmerge) |
---|
| 467 | { |
---|
| 468 | for(list<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator!=sender->parents.end();parent_iterator++) |
---|
| 469 | { |
---|
| 470 | polyhedron* current_parent = *parent_iterator; |
---|
| 471 | |
---|
| 472 | current_parent->message_counter++; |
---|
| 473 | |
---|
| 474 | bool is_last = (current_parent->message_counter == current_parent->number_of_children()); |
---|
| 475 | |
---|
| 476 | if(shouldmerge) |
---|
| 477 | { |
---|
| 478 | int child_state = sender->get_state(MERGE); |
---|
| 479 | int parent_state = current_parent->get_state(MERGE); |
---|
| 480 | |
---|
| 481 | if(parent_state == 0) |
---|
| 482 | { |
---|
| 483 | current_parent->set_state(child_state, MERGE); |
---|
| 484 | |
---|
| 485 | if(child_state == 0) |
---|
| 486 | { |
---|
| 487 | current_parent->mergechildren.push_back(sender); |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | else |
---|
| 491 | { |
---|
| 492 | if(child_state == 0) |
---|
| 493 | { |
---|
| 494 | if(parent_state > 0) |
---|
| 495 | { |
---|
| 496 | sender->positiveparent = current_parent; |
---|
| 497 | } |
---|
| 498 | else |
---|
| 499 | { |
---|
| 500 | sender->negativeparent = current_parent; |
---|
| 501 | } |
---|
| 502 | } |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | if(is_last) |
---|
| 506 | { |
---|
| 507 | if(parent_state > 0) |
---|
| 508 | { |
---|
| 509 | for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) |
---|
| 510 | { |
---|
| 511 | (*merge_child)->positiveparent = current_parent; |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | if(parent_state < 0) |
---|
| 516 | { |
---|
| 517 | for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) |
---|
| 518 | { |
---|
| 519 | (*merge_child)->negativeparent = current_parent; |
---|
| 520 | } |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | if(parent_state == 0) |
---|
| 524 | { |
---|
| 525 | for_merging[level+1].push_back(current_parent); |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | current_parent->mergechildren.clear(); |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | |
---|
| 532 | } |
---|
| 533 | |
---|
| 534 | if(shouldsplit) |
---|
| 535 | { |
---|
| 536 | current_parent->totallyneutralgrandchildren.insert(current_parent->totallyneutralgrandchildren.end(),sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); |
---|
| 537 | |
---|
| 538 | switch(sender->get_state(SPLIT)) |
---|
| 539 | { |
---|
| 540 | case 1: |
---|
| 541 | current_parent->positivechildren.push_back(sender); |
---|
| 542 | current_parent->positiveneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
---|
| 543 | break; |
---|
| 544 | case 0: |
---|
| 545 | current_parent->neutralchildren.push_back(sender); |
---|
| 546 | current_parent->positiveneutralvertices.insert(sender->positiveneutralvertices.begin(),sender->positiveneutralvertices.end()); |
---|
| 547 | current_parent->negativeneutralvertices.insert(sender->negativeneutralvertices.begin(),sender->negativeneutralvertices.end()); |
---|
| 548 | |
---|
| 549 | if(current_parent->totally_neutral == NULL) |
---|
| 550 | { |
---|
| 551 | current_parent->totally_neutral = sender->totally_neutral; |
---|
| 552 | } |
---|
| 553 | else |
---|
| 554 | { |
---|
| 555 | current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | if(sender->totally_neutral) |
---|
| 559 | { |
---|
| 560 | current_parent->totallyneutralchildren.push_back(sender); |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | break; |
---|
| 564 | case -1: |
---|
| 565 | current_parent->negativechildren.push_back(sender); |
---|
| 566 | current_parent->negativeneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
---|
| 567 | break; |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | if(is_last) |
---|
| 571 | { |
---|
| 572 | unique(current_parent->totallyneutralgrandchildren.begin(),current_parent->totallyneutralgrandchildren.end()); |
---|
| 573 | |
---|
| 574 | if((current_parent->negativechildren.size()>0&¤t_parent->positivechildren.size()>0)|| |
---|
| 575 | (current_parent->neutralchildren.size()>0&¤t_parent->totally_neutral==false)) |
---|
| 576 | { |
---|
| 577 | |
---|
| 578 | for_splitting[level+1].push_back(current_parent); |
---|
| 579 | |
---|
| 580 | current_parent->set_state(0, SPLIT); |
---|
| 581 | } |
---|
| 582 | else |
---|
| 583 | { |
---|
| 584 | ((toprow*)current_parent)->condition_order++; |
---|
| 585 | |
---|
| 586 | if(current_parent->negativechildren.size()>0) |
---|
| 587 | { |
---|
| 588 | current_parent->set_state(-1, SPLIT); |
---|
| 589 | |
---|
| 590 | ((toprow*)current_parent)->condition-=toadd; |
---|
| 591 | |
---|
| 592 | } |
---|
| 593 | else if(current_parent->positivechildren.size()>0) |
---|
| 594 | { |
---|
| 595 | current_parent->set_state(1, SPLIT); |
---|
| 596 | |
---|
| 597 | ((toprow*)current_parent)->condition+=toadd; |
---|
| 598 | } |
---|
| 599 | else |
---|
| 600 | { |
---|
| 601 | current_parent->raise_multiplicity(); |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | current_parent->positivechildren.clear(); |
---|
| 605 | current_parent->negativechildren.clear(); |
---|
| 606 | current_parent->neutralchildren.clear(); |
---|
| 607 | current_parent->totallyneutralchildren.clear(); |
---|
| 608 | current_parent->totallyneutralgrandchildren.clear(); |
---|
| 609 | current_parent->positiveneutralvertices.clear(); |
---|
| 610 | current_parent->negativeneutralvertices.clear(); |
---|
| 611 | current_parent->totally_neutral = NULL; |
---|
| 612 | current_parent->kids_rel_addresses.clear(); |
---|
| 613 | current_parent->message_counter = 0; |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | } |
---|
| 617 | |
---|
| 618 | if(is_last) |
---|
| 619 | { |
---|
| 620 | send_state_message(current_parent,toadd,toremove,level+1); |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | } |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | public: |
---|
| 629 | |
---|
| 630 | vector<set<ivec>> correction_factors; |
---|
| 631 | |
---|
| 632 | int number_of_parameters; |
---|
| 633 | |
---|
| 634 | /// A default constructor creates an emlig with predefined statistic representing only the range of the given |
---|
| 635 | /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. |
---|
| 636 | emlig(int number_of_parameters) |
---|
| 637 | { |
---|
| 638 | this->number_of_parameters = number_of_parameters; |
---|
| 639 | |
---|
| 640 | create_statistic(number_of_parameters); |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a |
---|
| 644 | /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. |
---|
| 645 | emlig(c_statistic statistic) |
---|
| 646 | { |
---|
| 647 | this->statistic = statistic; |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | void step_me(int marker) |
---|
| 651 | { |
---|
| 652 | for(int i = 0;i<statistic.size();i++) |
---|
| 653 | { |
---|
| 654 | for(polyhedron* horiz_ref = statistic.rows[i];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
| 655 | { |
---|
| 656 | char* string = "Checkpoint"; |
---|
| 657 | } |
---|
| 658 | } |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | int statistic_rowsize(int row) |
---|
| 662 | { |
---|
| 663 | return statistic.row_size(row); |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | void add_condition(vec toadd) |
---|
| 667 | { |
---|
| 668 | vec null_vector = ""; |
---|
| 669 | |
---|
| 670 | add_and_remove_condition(toadd, null_vector); |
---|
| 671 | } |
---|
| 672 | |
---|
| 673 | |
---|
| 674 | void remove_condition(vec toremove) |
---|
| 675 | { |
---|
| 676 | vec null_vector = ""; |
---|
| 677 | |
---|
| 678 | add_and_remove_condition(null_vector, toremove); |
---|
| 679 | |
---|
| 680 | } |
---|
| 681 | |
---|
| 682 | |
---|
| 683 | void add_and_remove_condition(vec toadd, vec toremove) |
---|
| 684 | { |
---|
| 685 | bool should_remove = (toremove.size() != 0); |
---|
| 686 | bool should_add = (toadd.size() != 0); |
---|
| 687 | |
---|
| 688 | for_splitting.clear(); |
---|
| 689 | for_merging.clear(); |
---|
| 690 | |
---|
| 691 | for(int i = 0;i<statistic.size();i++) |
---|
| 692 | { |
---|
| 693 | list<polyhedron*> empty_split; |
---|
| 694 | list<polyhedron*> empty_merge; |
---|
| 695 | |
---|
| 696 | for_splitting.push_back(empty_split); |
---|
| 697 | for_merging.push_back(empty_merge); |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | list<condition*>::iterator toremove_ref = conditions.end(); |
---|
| 701 | bool condition_should_be_added = false; |
---|
| 702 | |
---|
| 703 | for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++) |
---|
| 704 | { |
---|
| 705 | if(should_remove) |
---|
| 706 | { |
---|
| 707 | if((*ref)->value == toremove) |
---|
| 708 | { |
---|
| 709 | if((*ref)->multiplicity>1) |
---|
| 710 | { |
---|
| 711 | (*ref)->multiplicity--; |
---|
| 712 | |
---|
| 713 | alter_toprow_conditions(toremove,false); |
---|
| 714 | |
---|
| 715 | should_remove = false; |
---|
| 716 | } |
---|
| 717 | else |
---|
| 718 | { |
---|
| 719 | toremove_ref = ref; |
---|
| 720 | } |
---|
| 721 | } |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | if(should_add) |
---|
| 725 | { |
---|
| 726 | if((*ref)->value == toadd) |
---|
| 727 | { |
---|
| 728 | (*ref)->multiplicity++; |
---|
| 729 | |
---|
| 730 | alter_toprow_conditions(toadd,true); |
---|
| 731 | |
---|
| 732 | should_add = false; |
---|
| 733 | } |
---|
| 734 | else |
---|
| 735 | { |
---|
| 736 | condition_should_be_added = true; |
---|
| 737 | } |
---|
| 738 | } |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | if(toremove_ref!=conditions.end()) |
---|
| 742 | { |
---|
| 743 | conditions.erase(toremove_ref); |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | if(condition_should_be_added) |
---|
| 747 | { |
---|
| 748 | conditions.push_back(new condition(toadd)); |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | |
---|
| 752 | |
---|
| 753 | for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly) |
---|
| 754 | { |
---|
| 755 | vertex* current_vertex = (vertex*)horizontal_position; |
---|
| 756 | |
---|
| 757 | if(should_add||should_remove) |
---|
| 758 | { |
---|
| 759 | vec appended_vec = current_vertex->get_coordinates(); |
---|
| 760 | appended_vec.ins(0,-1.0); |
---|
| 761 | |
---|
| 762 | if(should_add) |
---|
| 763 | { |
---|
| 764 | double local_condition = toadd*appended_vec; |
---|
| 765 | |
---|
| 766 | current_vertex->set_state(local_condition,SPLIT); |
---|
| 767 | |
---|
| 768 | if(local_condition == 0) |
---|
| 769 | { |
---|
| 770 | current_vertex->totally_neutral = true; |
---|
| 771 | |
---|
| 772 | current_vertex->raise_multiplicity(); |
---|
| 773 | |
---|
| 774 | current_vertex->negativeneutralvertices.insert(current_vertex); |
---|
| 775 | current_vertex->positiveneutralvertices.insert(current_vertex); |
---|
| 776 | } |
---|
| 777 | } |
---|
| 778 | |
---|
| 779 | if(should_remove) |
---|
| 780 | { |
---|
| 781 | double local_condition = toremove*appended_vec; |
---|
| 782 | |
---|
| 783 | current_vertex->set_state(local_condition,MERGE); |
---|
| 784 | |
---|
| 785 | if(local_condition == 0) |
---|
| 786 | { |
---|
| 787 | for_merging[0].push_back(current_vertex); |
---|
| 788 | } |
---|
| 789 | } |
---|
| 790 | } |
---|
| 791 | |
---|
| 792 | send_state_message(current_vertex, toadd, toremove, 0); |
---|
| 793 | |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | if(should_add) |
---|
| 797 | { |
---|
| 798 | int k = 1; |
---|
| 799 | |
---|
| 800 | vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin(); |
---|
| 801 | |
---|
| 802 | for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++) |
---|
| 803 | { |
---|
| 804 | |
---|
| 805 | for(list<polyhedron*>::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++) |
---|
| 806 | { |
---|
| 807 | polyhedron* new_totally_neutral_child; |
---|
| 808 | |
---|
| 809 | polyhedron* current_polyhedron = (*split_ref); |
---|
| 810 | |
---|
| 811 | if(vert_ref == beginning_ref) |
---|
| 812 | { |
---|
| 813 | vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates(); |
---|
| 814 | vec coordinates2 = ((vertex*)(*(current_polyhedron->children.begin()++)))->get_coordinates(); |
---|
| 815 | coordinates2.ins(0,-1.0); |
---|
| 816 | |
---|
| 817 | double t = (-toadd*coordinates2)/(toadd(1,toadd.size()-1)*coordinates1)+1; |
---|
| 818 | |
---|
| 819 | vec new_coordinates = coordinates1*t+(coordinates2(1,coordinates2.size()-1)-coordinates1); |
---|
| 820 | |
---|
| 821 | vertex* neutral_vertex = new vertex(new_coordinates); |
---|
| 822 | |
---|
| 823 | new_totally_neutral_child = neutral_vertex; |
---|
| 824 | } |
---|
| 825 | else |
---|
| 826 | { |
---|
| 827 | toprow* neutral_toprow = new toprow(); |
---|
| 828 | |
---|
| 829 | new_totally_neutral_child = neutral_toprow; |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(), |
---|
| 833 | current_polyhedron->totallyneutralgrandchildren.begin(), |
---|
| 834 | current_polyhedron->totallyneutralgrandchildren.end()); |
---|
| 835 | |
---|
| 836 | for(list<polyhedron*>::iterator grand_ref = current_polyhedron->totallyneutralgrandchildren.begin(); grand_ref != current_polyhedron->totallyneutralgrandchildren.end();grand_ref++) |
---|
| 837 | { |
---|
| 838 | (*grand_ref)->parents.push_back(new_totally_neutral_child); |
---|
| 839 | |
---|
| 840 | new_totally_neutral_child->vertices.insert((*grand_ref)->vertices.begin(),(*grand_ref)->vertices.end()); |
---|
| 841 | } |
---|
| 842 | |
---|
| 843 | toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition+toadd, ((toprow*)current_polyhedron)->condition_order+1); |
---|
| 844 | toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition-toadd, ((toprow*)current_polyhedron)->condition_order+1); |
---|
| 845 | |
---|
| 846 | for(list<polyhedron*>::iterator parent_ref = current_polyhedron->parents.begin();parent_ref!=current_polyhedron->parents.end();parent_ref++) |
---|
| 847 | { |
---|
| 848 | (*parent_ref)->totallyneutralgrandchildren.push_back(new_totally_neutral_child); |
---|
| 849 | |
---|
| 850 | (*parent_ref)->neutralchildren.remove(current_polyhedron); |
---|
| 851 | (*parent_ref)->children.remove(current_polyhedron); |
---|
| 852 | |
---|
| 853 | (*parent_ref)->children.push_back(positive_poly); |
---|
| 854 | (*parent_ref)->children.push_back(negative_poly); |
---|
| 855 | (*parent_ref)->positivechildren.push_back(positive_poly); |
---|
| 856 | (*parent_ref)->negativechildren.push_back(negative_poly); |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | positive_poly->parents.insert(positive_poly->parents.end(), |
---|
| 860 | current_polyhedron->parents.begin(), |
---|
| 861 | current_polyhedron->parents.end()); |
---|
| 862 | |
---|
| 863 | negative_poly->parents.insert(negative_poly->parents.end(), |
---|
| 864 | current_polyhedron->parents.begin(), |
---|
| 865 | current_polyhedron->parents.end()); |
---|
| 866 | |
---|
| 867 | positive_poly->children.push_back(new_totally_neutral_child); |
---|
| 868 | negative_poly->children.push_back(new_totally_neutral_child); |
---|
| 869 | |
---|
| 870 | new_totally_neutral_child->parents.push_back(positive_poly); |
---|
| 871 | new_totally_neutral_child->parents.push_back(negative_poly); |
---|
| 872 | |
---|
| 873 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->positivechildren.begin();child_ref!=current_polyhedron->positivechildren.end();child_ref++) |
---|
| 874 | { |
---|
| 875 | (*child_ref)->parents.remove(current_polyhedron); |
---|
| 876 | (*child_ref)->parents.push_back(positive_poly); |
---|
| 877 | } |
---|
| 878 | |
---|
| 879 | positive_poly->children.insert(positive_poly->children.end(), |
---|
| 880 | current_polyhedron->positivechildren.begin(), |
---|
| 881 | current_polyhedron->positivechildren.end()); |
---|
| 882 | |
---|
| 883 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->negativechildren.begin();child_ref!=current_polyhedron->negativechildren.end();child_ref++) |
---|
| 884 | { |
---|
| 885 | (*child_ref)->parents.remove(current_polyhedron); |
---|
| 886 | (*child_ref)->parents.push_back(negative_poly); |
---|
| 887 | } |
---|
| 888 | |
---|
| 889 | negative_poly->children.insert(negative_poly->children.end(), |
---|
| 890 | current_polyhedron->negativechildren.begin(), |
---|
| 891 | current_polyhedron->negativechildren.end()); |
---|
| 892 | |
---|
| 893 | positive_poly->vertices.insert(current_polyhedron->positiveneutralvertices.begin(),current_polyhedron->positiveneutralvertices.end()); |
---|
| 894 | positive_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
---|
| 895 | |
---|
| 896 | negative_poly->vertices.insert(current_polyhedron->negativeneutralvertices.begin(),current_polyhedron->negativeneutralvertices.end()); |
---|
| 897 | negative_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
---|
| 898 | |
---|
| 899 | new_totally_neutral_child->triangulate(false); |
---|
| 900 | |
---|
| 901 | positive_poly->triangulate(k==for_splitting.size()-1); |
---|
| 902 | negative_poly->triangulate(k==for_splitting.size()-1); |
---|
| 903 | |
---|
| 904 | statistic.append_polyhedron(k-1, new_totally_neutral_child); |
---|
| 905 | |
---|
| 906 | statistic.insert_polyhedron(k, positive_poly, current_polyhedron); |
---|
| 907 | statistic.insert_polyhedron(k, negative_poly, current_polyhedron); |
---|
| 908 | |
---|
| 909 | statistic.delete_polyhedron(k, current_polyhedron); |
---|
| 910 | |
---|
| 911 | delete current_polyhedron; |
---|
| 912 | } |
---|
| 913 | |
---|
| 914 | k++; |
---|
| 915 | } |
---|
| 916 | } |
---|
| 917 | |
---|
| 918 | /* |
---|
| 919 | vector<int> sizevector; |
---|
| 920 | for(int s = 0;s<statistic.size();s++) |
---|
| 921 | { |
---|
| 922 | sizevector.push_back(statistic.row_size(s)); |
---|
| 923 | }*/ |
---|
| 924 | |
---|
| 925 | |
---|
| 926 | } |
---|
| 927 | |
---|
| 928 | |
---|
| 929 | void set_correction_factors(int order) |
---|
| 930 | { |
---|
| 931 | for(int remaining_order = correction_factors.size();remaining_order>order;remaining_order++) |
---|
| 932 | { |
---|
| 933 | set<ivec> factor_templates; |
---|
| 934 | set<ivec> final_factors; |
---|
| 935 | |
---|
| 936 | |
---|
| 937 | for(int i = 1;i==number_of_parameters-order+1;i++) |
---|
| 938 | { |
---|
| 939 | ivec new_template = vec_2(1,i); |
---|
| 940 | //new_template[0]=1; |
---|
| 941 | //new_template[1]=i; |
---|
| 942 | //factor_templates.insert(new_template); |
---|
| 943 | |
---|
| 944 | |
---|
| 945 | for(int j = 1;j==remaining_order-1;j++) |
---|
| 946 | { |
---|
| 947 | |
---|
| 948 | for(set<ivec>::iterator fac_ref = factor_templates.begin();fac_ref!=factor_templates.end();fac_ref++) |
---|
| 949 | { |
---|
| 950 | ivec current_template = (*fac_ref); |
---|
| 951 | |
---|
| 952 | current_template[0]+=1; |
---|
| 953 | current_template.ins(current_template.size(),i); |
---|
| 954 | |
---|
| 955 | |
---|
| 956 | if(current_template[0]==remaining_order) |
---|
| 957 | { |
---|
| 958 | final_factors.insert(current_template.right(current_template.size()-1)); |
---|
| 959 | } |
---|
| 960 | else |
---|
| 961 | { |
---|
| 962 | factor_templates.insert(current_template); |
---|
| 963 | } |
---|
| 964 | } |
---|
| 965 | } |
---|
| 966 | } |
---|
| 967 | |
---|
| 968 | correction_factors.push_back(final_factors); |
---|
| 969 | |
---|
| 970 | } |
---|
| 971 | } |
---|
| 972 | |
---|
| 973 | protected: |
---|
| 974 | |
---|
| 975 | /// A method for creating plain default statistic representing only the range of the parameter space. |
---|
| 976 | void create_statistic(int number_of_parameters) |
---|
| 977 | { |
---|
| 978 | for(int i = 0;i<number_of_parameters;i++) |
---|
| 979 | { |
---|
| 980 | vec condition_vec = zeros(number_of_parameters+1); |
---|
| 981 | condition_vec[i+1] = 1; |
---|
| 982 | |
---|
| 983 | condition* new_condition = new condition(condition_vec); |
---|
| 984 | |
---|
| 985 | conditions.push_back(new_condition); |
---|
| 986 | } |
---|
| 987 | |
---|
| 988 | // An empty vector of coordinates. |
---|
| 989 | vec origin_coord; |
---|
| 990 | |
---|
| 991 | // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. |
---|
| 992 | vertex *origin = new vertex(origin_coord); |
---|
| 993 | |
---|
| 994 | /* |
---|
| 995 | // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse |
---|
| 996 | // diagram. First we create a vector of polyhedrons.. |
---|
| 997 | list<polyhedron*> origin_vec; |
---|
| 998 | |
---|
| 999 | // ..we fill it with the origin.. |
---|
| 1000 | origin_vec.push_back(origin); |
---|
| 1001 | |
---|
| 1002 | // ..and we fill the statistic with the created vector. |
---|
| 1003 | statistic.push_back(origin_vec); |
---|
| 1004 | */ |
---|
| 1005 | |
---|
| 1006 | statistic = *(new c_statistic()); |
---|
| 1007 | |
---|
| 1008 | statistic.append_polyhedron(0, origin); |
---|
| 1009 | |
---|
| 1010 | // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to |
---|
| 1011 | // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: |
---|
| 1012 | for(int i=0;i<number_of_parameters;i++) |
---|
| 1013 | { |
---|
| 1014 | // We first will create two new vertices. These will be the borders of the parameter space in the dimension |
---|
| 1015 | // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the |
---|
| 1016 | // right amount of zero cooridnates by reading them from the origin |
---|
| 1017 | vec origin_coord = origin->get_coordinates(); |
---|
| 1018 | |
---|
| 1019 | // And we incorporate the nonzero coordinates into the new cooordinate vectors |
---|
| 1020 | vec origin_coord1 = concat(origin_coord,-max_range); |
---|
| 1021 | vec origin_coord2 = concat(origin_coord,max_range); |
---|
| 1022 | |
---|
| 1023 | |
---|
| 1024 | // Now we create the points |
---|
| 1025 | vertex* new_point1 = new vertex(origin_coord1); |
---|
| 1026 | vertex* new_point2 = new vertex(origin_coord2); |
---|
| 1027 | |
---|
| 1028 | //********************************************************************************************************* |
---|
| 1029 | // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old |
---|
| 1030 | // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points |
---|
| 1031 | // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons |
---|
| 1032 | // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is |
---|
| 1033 | // connected to the first (second) newly created vertex by a parent-child relation. |
---|
| 1034 | //********************************************************************************************************* |
---|
| 1035 | |
---|
| 1036 | |
---|
| 1037 | /* |
---|
| 1038 | // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram |
---|
| 1039 | vector<vector<polyhedron*>> new_statistic1; |
---|
| 1040 | vector<vector<polyhedron*>> new_statistic2; |
---|
| 1041 | */ |
---|
| 1042 | |
---|
| 1043 | c_statistic* new_statistic1 = new c_statistic(); |
---|
| 1044 | c_statistic* new_statistic2 = new c_statistic(); |
---|
| 1045 | |
---|
| 1046 | |
---|
| 1047 | // Copy the statistic by rows |
---|
| 1048 | for(int j=0;j<statistic.size();j++) |
---|
| 1049 | { |
---|
| 1050 | |
---|
| 1051 | |
---|
| 1052 | // an element counter |
---|
| 1053 | int element_number = 0; |
---|
| 1054 | |
---|
| 1055 | /* |
---|
| 1056 | vector<polyhedron*> supportnew_1; |
---|
| 1057 | vector<polyhedron*> supportnew_2; |
---|
| 1058 | |
---|
| 1059 | new_statistic1.push_back(supportnew_1); |
---|
| 1060 | new_statistic2.push_back(supportnew_2); |
---|
| 1061 | */ |
---|
| 1062 | |
---|
| 1063 | // for each polyhedron in the given row |
---|
| 1064 | for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
| 1065 | { |
---|
| 1066 | // Append an extra zero coordinate to each of the vertices for the new dimension |
---|
| 1067 | // If vert_ref is at the first index => we loop through vertices |
---|
| 1068 | if(j == 0) |
---|
| 1069 | { |
---|
| 1070 | // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates |
---|
| 1071 | ((vertex*) horiz_ref)->push_coordinate(0); |
---|
| 1072 | } |
---|
| 1073 | /* |
---|
| 1074 | else |
---|
| 1075 | { |
---|
| 1076 | ((toprow*) (*horiz_ref))->condition.ins(0,0); |
---|
| 1077 | }*/ |
---|
| 1078 | |
---|
| 1079 | // if it has parents |
---|
| 1080 | if(!horiz_ref->parents.empty()) |
---|
| 1081 | { |
---|
| 1082 | // save the relative address of this child in a vector kids_rel_addresses of all its parents. |
---|
| 1083 | // This information will later be used for copying the whole Hasse diagram with each of the |
---|
| 1084 | // relations contained within. |
---|
| 1085 | for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++) |
---|
| 1086 | { |
---|
| 1087 | (*parent_ref)->kids_rel_addresses.push_back(element_number); |
---|
| 1088 | } |
---|
| 1089 | } |
---|
| 1090 | |
---|
| 1091 | // ************************************************************************************************** |
---|
| 1092 | // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron |
---|
| 1093 | // will be created as a toprow, but this information will be later forgotten and only the polyhedrons |
---|
| 1094 | // in the top row of the Hasse diagram will be considered toprow for later use. |
---|
| 1095 | // ************************************************************************************************** |
---|
| 1096 | |
---|
| 1097 | // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic |
---|
| 1098 | // this condition will be a vector of zeros. There are two vectors, because we need two copies of |
---|
| 1099 | // the original Hasse diagram. |
---|
| 1100 | vec vec1(number_of_parameters+1); |
---|
| 1101 | vec1.zeros(); |
---|
| 1102 | |
---|
| 1103 | vec vec2(number_of_parameters+1); |
---|
| 1104 | vec2.zeros(); |
---|
| 1105 | |
---|
| 1106 | // We create a new toprow with the previously specified condition. |
---|
| 1107 | toprow* current_copy1 = new toprow(vec1, 0); |
---|
| 1108 | toprow* current_copy2 = new toprow(vec2, 0); |
---|
| 1109 | |
---|
| 1110 | // The vertices of the copies will be inherited, because there will be a parent/child relation |
---|
| 1111 | // between each polyhedron and its offspring (comming from the copy) and a parent has all the |
---|
| 1112 | // vertices of its child plus more. |
---|
| 1113 | for(set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++) |
---|
| 1114 | { |
---|
| 1115 | current_copy1->vertices.insert(*vertex_ref); |
---|
| 1116 | current_copy2->vertices.insert(*vertex_ref); |
---|
| 1117 | } |
---|
| 1118 | |
---|
| 1119 | // The only new vertex of the offspring should be the newly created point. |
---|
| 1120 | current_copy1->vertices.insert(new_point1); |
---|
| 1121 | current_copy2->vertices.insert(new_point2); |
---|
| 1122 | |
---|
| 1123 | // This method guarantees that each polyhedron is already triangulated, therefore its triangulation |
---|
| 1124 | // is only one set of vertices and it is the set of all its vertices. |
---|
| 1125 | set<vertex*> t_simplex1; |
---|
| 1126 | set<vertex*> t_simplex2; |
---|
| 1127 | |
---|
| 1128 | t_simplex1.insert(current_copy1->vertices.begin(),current_copy1->vertices.end()); |
---|
| 1129 | t_simplex2.insert(current_copy2->vertices.begin(),current_copy2->vertices.end()); |
---|
| 1130 | |
---|
| 1131 | current_copy1->triangulation.push_back(t_simplex1); |
---|
| 1132 | current_copy2->triangulation.push_back(t_simplex2); |
---|
| 1133 | |
---|
| 1134 | // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying |
---|
| 1135 | // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the |
---|
| 1136 | // kids and when we do that and know the child, in the child we will remember the parent we came from. |
---|
| 1137 | // This way all the parents/children relations are saved in both the parent and the child. |
---|
| 1138 | if(!horiz_ref->kids_rel_addresses.empty()) |
---|
| 1139 | { |
---|
| 1140 | for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++) |
---|
| 1141 | { |
---|
| 1142 | polyhedron* new_kid1 = new_statistic1->rows[j-1]; |
---|
| 1143 | polyhedron* new_kid2 = new_statistic2->rows[j-1]; |
---|
| 1144 | |
---|
| 1145 | // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but |
---|
| 1146 | // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline. |
---|
| 1147 | if(*kid_ref) |
---|
| 1148 | { |
---|
| 1149 | for(int k = 1;k<=(*kid_ref);k++) |
---|
| 1150 | { |
---|
| 1151 | new_kid1=new_kid1->next_poly; |
---|
| 1152 | new_kid2=new_kid2->next_poly; |
---|
| 1153 | } |
---|
| 1154 | } |
---|
| 1155 | |
---|
| 1156 | // find the child and save the relation to the parent |
---|
| 1157 | current_copy1->children.push_back(new_kid1); |
---|
| 1158 | current_copy2->children.push_back(new_kid2); |
---|
| 1159 | |
---|
| 1160 | // in the child save the parents' address |
---|
| 1161 | new_kid1->parents.push_back(current_copy1); |
---|
| 1162 | new_kid2->parents.push_back(current_copy2); |
---|
| 1163 | } |
---|
| 1164 | |
---|
| 1165 | // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the |
---|
| 1166 | // Hasse diagram again) |
---|
| 1167 | horiz_ref->kids_rel_addresses.clear(); |
---|
| 1168 | } |
---|
| 1169 | // If there were no children previously, we are copying a polyhedron that has been a vertex before. |
---|
| 1170 | // In this case it is a segment now and it will have a relation to its mother (copywise) and to the |
---|
| 1171 | // newly created point. Here we create the connection to the new point, again from both sides. |
---|
| 1172 | else |
---|
| 1173 | { |
---|
| 1174 | // Add the address of the new point in the former vertex |
---|
| 1175 | current_copy1->children.push_back(new_point1); |
---|
| 1176 | current_copy2->children.push_back(new_point2); |
---|
| 1177 | |
---|
| 1178 | // Add the address of the former vertex in the new point |
---|
| 1179 | new_point1->parents.push_back(current_copy1); |
---|
| 1180 | new_point2->parents.push_back(current_copy2); |
---|
| 1181 | } |
---|
| 1182 | |
---|
| 1183 | // Save the mother in its offspring |
---|
| 1184 | current_copy1->children.push_back(horiz_ref); |
---|
| 1185 | current_copy2->children.push_back(horiz_ref); |
---|
| 1186 | |
---|
| 1187 | // Save the offspring in its mother |
---|
| 1188 | horiz_ref->parents.push_back(current_copy1); |
---|
| 1189 | horiz_ref->parents.push_back(current_copy2); |
---|
| 1190 | |
---|
| 1191 | |
---|
| 1192 | // Add the copies into the relevant statistic. The statistic will later be appended to the previous |
---|
| 1193 | // Hasse diagram |
---|
| 1194 | new_statistic1->append_polyhedron(j,current_copy1); |
---|
| 1195 | new_statistic2->append_polyhedron(j,current_copy2); |
---|
| 1196 | |
---|
| 1197 | // Raise the count in the vector of polyhedrons |
---|
| 1198 | element_number++; |
---|
| 1199 | |
---|
| 1200 | } |
---|
| 1201 | |
---|
| 1202 | } |
---|
| 1203 | |
---|
| 1204 | /* |
---|
| 1205 | statistic.begin()->push_back(new_point1); |
---|
| 1206 | statistic.begin()->push_back(new_point2); |
---|
| 1207 | */ |
---|
| 1208 | |
---|
| 1209 | statistic.append_polyhedron(0, new_point1); |
---|
| 1210 | statistic.append_polyhedron(0, new_point2); |
---|
| 1211 | |
---|
| 1212 | // Merge the new statistics into the old one. This will either be the final statistic or we will |
---|
| 1213 | // reenter the widening loop. |
---|
| 1214 | for(int j=0;j<new_statistic1->size();j++) |
---|
| 1215 | { |
---|
| 1216 | /* |
---|
| 1217 | if(j+1==statistic.size()) |
---|
| 1218 | { |
---|
| 1219 | list<polyhedron*> support; |
---|
| 1220 | statistic.push_back(support); |
---|
| 1221 | } |
---|
| 1222 | |
---|
| 1223 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); |
---|
| 1224 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end()); |
---|
| 1225 | */ |
---|
| 1226 | statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]); |
---|
| 1227 | statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]); |
---|
| 1228 | } |
---|
| 1229 | |
---|
| 1230 | |
---|
| 1231 | } |
---|
| 1232 | |
---|
| 1233 | /* |
---|
| 1234 | vector<list<toprow*>> toprow_statistic; |
---|
| 1235 | int line_count = 0; |
---|
| 1236 | |
---|
| 1237 | for(vector<list<polyhedron*>>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++) |
---|
| 1238 | { |
---|
| 1239 | list<toprow*> support_list; |
---|
| 1240 | toprow_statistic.push_back(support_list); |
---|
| 1241 | |
---|
| 1242 | for(list<polyhedron*>::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++) |
---|
| 1243 | { |
---|
| 1244 | toprow* support_top = (toprow*)(*polyhedron_ref2); |
---|
| 1245 | |
---|
| 1246 | toprow_statistic[line_count].push_back(support_top); |
---|
| 1247 | } |
---|
| 1248 | |
---|
| 1249 | line_count++; |
---|
| 1250 | }*/ |
---|
| 1251 | |
---|
| 1252 | /* |
---|
| 1253 | vector<int> sizevector; |
---|
| 1254 | for(int s = 0;s<statistic.size();s++) |
---|
| 1255 | { |
---|
| 1256 | sizevector.push_back(statistic.row_size(s)); |
---|
| 1257 | } |
---|
| 1258 | */ |
---|
| 1259 | |
---|
| 1260 | } |
---|
| 1261 | |
---|
| 1262 | |
---|
| 1263 | |
---|
| 1264 | |
---|
| 1265 | }; |
---|
| 1266 | |
---|
| 1267 | /* |
---|
| 1268 | |
---|
| 1269 | //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density |
---|
| 1270 | class RARX : public BM |
---|
| 1271 | { |
---|
| 1272 | private: |
---|
| 1273 | |
---|
| 1274 | emlig posterior; |
---|
| 1275 | |
---|
| 1276 | public: |
---|
| 1277 | RARX():BM() |
---|
| 1278 | { |
---|
| 1279 | }; |
---|
| 1280 | |
---|
| 1281 | void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec) |
---|
| 1282 | { |
---|
| 1283 | |
---|
| 1284 | } |
---|
| 1285 | |
---|
| 1286 | };*/ |
---|
| 1287 | |
---|
| 1288 | bool ivec::operator>(const ivec &first, const ivec &second) |
---|
| 1289 | { |
---|
| 1290 | int size1 = first.size(); |
---|
| 1291 | int size2 = second.size(); |
---|
| 1292 | |
---|
| 1293 | int counter1 = 0; |
---|
| 1294 | while(0==0) |
---|
| 1295 | { |
---|
| 1296 | if(first[counter1]==0) |
---|
| 1297 | { |
---|
| 1298 | size1--; |
---|
| 1299 | } |
---|
| 1300 | |
---|
| 1301 | if(first[counter1]!=0) |
---|
| 1302 | break; |
---|
| 1303 | } |
---|
| 1304 | |
---|
| 1305 | int counter2 = 0; |
---|
| 1306 | while(0==0) |
---|
| 1307 | { |
---|
| 1308 | if(second[counter2]==0) |
---|
| 1309 | { |
---|
| 1310 | size2--; |
---|
| 1311 | } |
---|
| 1312 | |
---|
| 1313 | if(second[counter2]!=0) |
---|
| 1314 | break; |
---|
| 1315 | } |
---|
| 1316 | |
---|
| 1317 | if(size1!=size2) |
---|
| 1318 | { |
---|
| 1319 | return size1>size2; |
---|
| 1320 | } |
---|
| 1321 | else |
---|
| 1322 | { |
---|
| 1323 | for(int i = 0;i<size1;i++) |
---|
| 1324 | { |
---|
| 1325 | if(first[counter1+i]!=second[counter2+i]) |
---|
| 1326 | { |
---|
| 1327 | return first[counter1+i]>second[counter2+i] |
---|
| 1328 | } |
---|
| 1329 | } |
---|
| 1330 | |
---|
| 1331 | return false; |
---|
| 1332 | } |
---|
| 1333 | } |
---|
| 1334 | |
---|
| 1335 | |
---|
| 1336 | #endif //TRAGE_H |
---|