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