| 1 | /*! |
|---|
| 2 | \file |
|---|
| 3 | \brief Robust Bayesian auto-regression model |
|---|
| 4 | \author Jan Sindelar. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #ifndef ROBUST_H |
|---|
| 8 | #define ROBUST_H |
|---|
| 9 | |
|---|
| 10 | #include <stat/exp_family.h> |
|---|
| 11 | #include <itpp/itbase.h> |
|---|
| 12 | #include <itpp/base/random.h> |
|---|
| 13 | #include <map> |
|---|
| 14 | #include <limits> |
|---|
| 15 | #include <vector> |
|---|
| 16 | #include <list> |
|---|
| 17 | #include <set> |
|---|
| 18 | #include <algorithm> |
|---|
| 19 | |
|---|
| 20 | using namespace bdm; |
|---|
| 21 | using namespace std; |
|---|
| 22 | using namespace itpp; |
|---|
| 23 | |
|---|
| 24 | static Exponential_RNG ExpRNG; |
|---|
| 25 | |
|---|
| 26 | const double max_range = 5;//numeric_limits<double>::max()/10e-10; |
|---|
| 27 | |
|---|
| 28 | /// An enumeration of possible actions performed on the polyhedrons. We can merge them or split them. |
|---|
| 29 | enum actions {MERGE, SPLIT}; |
|---|
| 30 | |
|---|
| 31 | // Forward declaration of polyhedron, vertex and emlig |
|---|
| 32 | class polyhedron; |
|---|
| 33 | class vertex; |
|---|
| 34 | class emlig; |
|---|
| 35 | |
|---|
| 36 | /* |
|---|
| 37 | class t_simplex |
|---|
| 38 | { |
|---|
| 39 | public: |
|---|
| 40 | set<vertex*> minima; |
|---|
| 41 | |
|---|
| 42 | set<vertex*> simplex; |
|---|
| 43 | |
|---|
| 44 | t_simplex(vertex* origin_vertex) |
|---|
| 45 | { |
|---|
| 46 | simplex.insert(origin_vertex); |
|---|
| 47 | minima.insert(origin_vertex); |
|---|
| 48 | } |
|---|
| 49 | };*/ |
|---|
| 50 | |
|---|
| 51 | /// A class representing a single condition that can be added to the emlig. A condition represents data entries in a statistical model. |
|---|
| 52 | class condition |
|---|
| 53 | { |
|---|
| 54 | public: |
|---|
| 55 | /// Value of the condition representing the data |
|---|
| 56 | vec value; |
|---|
| 57 | |
|---|
| 58 | /// Mulitplicity of the given condition may represent multiple occurences of same data entry. |
|---|
| 59 | int multiplicity; |
|---|
| 60 | |
|---|
| 61 | /// Default constructor of condition class takes the value of data entry and creates a condition with multiplicity 1 (first occurence of the data). |
|---|
| 62 | condition(vec value) |
|---|
| 63 | { |
|---|
| 64 | this->value = value; |
|---|
| 65 | multiplicity = 1; |
|---|
| 66 | } |
|---|
| 67 | }; |
|---|
| 68 | |
|---|
| 69 | class simplex |
|---|
| 70 | { |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | public: |
|---|
| 74 | |
|---|
| 75 | set<vertex*> vertices; |
|---|
| 76 | |
|---|
| 77 | double probability; |
|---|
| 78 | |
|---|
| 79 | double volume; |
|---|
| 80 | |
|---|
| 81 | vector<multimap<double,double>> positive_gamma_parameters; |
|---|
| 82 | |
|---|
| 83 | vector<multimap<double,double>> negative_gamma_parameters; |
|---|
| 84 | |
|---|
| 85 | double positive_gamma_sum; |
|---|
| 86 | |
|---|
| 87 | double negative_gamma_sum; |
|---|
| 88 | |
|---|
| 89 | double min_beta; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | simplex(set<vertex*> vertices) |
|---|
| 93 | { |
|---|
| 94 | this->vertices.insert(vertices.begin(),vertices.end()); |
|---|
| 95 | probability = 0; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | simplex(vertex* vertex) |
|---|
| 99 | { |
|---|
| 100 | this->vertices.insert(vertex); |
|---|
| 101 | probability = 0; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void clear_gammas() |
|---|
| 105 | { |
|---|
| 106 | positive_gamma_parameters.clear(); |
|---|
| 107 | negative_gamma_parameters.clear(); |
|---|
| 108 | |
|---|
| 109 | positive_gamma_sum = 0; |
|---|
| 110 | negative_gamma_sum = 0; |
|---|
| 111 | |
|---|
| 112 | min_beta = numeric_limits<double>::max(); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void insert_gamma(int order, double weight, double beta) |
|---|
| 116 | { |
|---|
| 117 | if(weight>=0) |
|---|
| 118 | { |
|---|
| 119 | while(positive_gamma_parameters.size()<order+1) |
|---|
| 120 | { |
|---|
| 121 | multimap<double,double> map; |
|---|
| 122 | positive_gamma_parameters.push_back(map); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | positive_gamma_sum += weight; |
|---|
| 126 | |
|---|
| 127 | positive_gamma_parameters[order].insert(pair<double,double>(weight,beta)); |
|---|
| 128 | } |
|---|
| 129 | else |
|---|
| 130 | { |
|---|
| 131 | while(negative_gamma_parameters.size()<order+1) |
|---|
| 132 | { |
|---|
| 133 | multimap<double,double> map; |
|---|
| 134 | negative_gamma_parameters.push_back(map); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | negative_gamma_sum -= weight; |
|---|
| 138 | |
|---|
| 139 | negative_gamma_parameters[order].insert(pair<double,double>(-weight,beta)); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | if(beta < min_beta) |
|---|
| 143 | { |
|---|
| 144 | min_beta = beta; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | /// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram |
|---|
| 151 | /// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created. |
|---|
| 152 | class polyhedron |
|---|
| 153 | { |
|---|
| 154 | /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of |
|---|
| 155 | /// more than just the necessary number of conditions. For example if a newly created line passes through an already |
|---|
| 156 | /// existing point, the points multiplicity will rise by 1. |
|---|
| 157 | int multiplicity; |
|---|
| 158 | |
|---|
| 159 | /// A property representing the position of the polyhedron related to current condition with relation to which we |
|---|
| 160 | /// are splitting the parameter space (new data has arrived). This property is setup within a classification procedure and |
|---|
| 161 | /// is only valid while the new condition is being added. It has to be reset when new condition is added and new classification |
|---|
| 162 | /// has to be performed. |
|---|
| 163 | int split_state; |
|---|
| 164 | |
|---|
| 165 | /// A property representing the position of the polyhedron related to current condition with relation to which we |
|---|
| 166 | /// are merging the parameter space (data is being deleted usually due to a moving window model which is more adaptive and |
|---|
| 167 | /// steps in for the forgetting in a classical Gaussian AR model). This property is setup within a classification procedure and |
|---|
| 168 | /// is only valid while the new condition is being removed. It has to be reset when new condition is removed and new classification |
|---|
| 169 | /// has to be performed. |
|---|
| 170 | int merge_state; |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | public: |
|---|
| 175 | /// A pointer to the multi-Laplace inverse gamma distribution this polyhedron belongs to. |
|---|
| 176 | emlig* my_emlig; |
|---|
| 177 | |
|---|
| 178 | /// A list of polyhedrons parents within the Hasse diagram. |
|---|
| 179 | list<polyhedron*> parents; |
|---|
| 180 | |
|---|
| 181 | /// A list of polyhedrons children withing the Hasse diagram. |
|---|
| 182 | list<polyhedron*> children; |
|---|
| 183 | |
|---|
| 184 | /// All the vertices of the given polyhedron |
|---|
| 185 | set<vertex*> vertices; |
|---|
| 186 | |
|---|
| 187 | /// The conditions that gave birth to the polyhedron. If some of them is removed, the polyhedron ceases to exist. |
|---|
| 188 | set<condition*> parentconditions; |
|---|
| 189 | |
|---|
| 190 | /// A list used for storing children that lie in the positive region related to a certain condition |
|---|
| 191 | list<polyhedron*> positivechildren; |
|---|
| 192 | |
|---|
| 193 | /// A list used for storing children that lie in the negative region related to a certain condition |
|---|
| 194 | list<polyhedron*> negativechildren; |
|---|
| 195 | |
|---|
| 196 | /// Children intersecting the condition |
|---|
| 197 | list<polyhedron*> neutralchildren; |
|---|
| 198 | |
|---|
| 199 | /// A set of grandchildren of the polyhedron that when new condition is added lie exactly on the condition hyperplane. These grandchildren |
|---|
| 200 | /// behave differently from other grandchildren, when the polyhedron is split. New grandchild is not necessarily created on the crossection of |
|---|
| 201 | /// the polyhedron and new condition. |
|---|
| 202 | set<polyhedron*> totallyneutralgrandchildren; |
|---|
| 203 | |
|---|
| 204 | /// A set of children of the polyhedron that when new condition is added lie exactly on the condition hyperplane. These children |
|---|
| 205 | /// behave differently from other children, when the polyhedron is split. New child is not necessarily created on the crossection of |
|---|
| 206 | /// the polyhedron and new condition. |
|---|
| 207 | set<polyhedron*> totallyneutralchildren; |
|---|
| 208 | |
|---|
| 209 | /// Reverse relation to the totallyneutralgrandchildren set is needed for merging of already existing polyhedrons to keep |
|---|
| 210 | /// totallyneutralgrandchildren list up to date. |
|---|
| 211 | set<polyhedron*> grandparents; |
|---|
| 212 | |
|---|
| 213 | /// Vertices of the polyhedron classified as positive related to an added condition. When the polyhderon is split by the new condition, |
|---|
| 214 | /// these vertices will belong to the positive part of the splitted polyhedron. |
|---|
| 215 | set<vertex*> positiveneutralvertices; |
|---|
| 216 | |
|---|
| 217 | /// Vertices of the polyhedron classified as negative related to an added condition. When the polyhderon is split by the new condition, |
|---|
| 218 | /// these vertices will belong to the negative part of the splitted polyhedron. |
|---|
| 219 | set<vertex*> negativeneutralvertices; |
|---|
| 220 | |
|---|
| 221 | /// A bool specifying if the polyhedron lies exactly on the newly added condition or not. |
|---|
| 222 | bool totally_neutral; |
|---|
| 223 | |
|---|
| 224 | /// When two polyhedrons are merged, there always exists a child lying on the former border of the polyhedrons. This child manages the merge |
|---|
| 225 | /// of the two polyhedrons. This property gives us the address of the mediator child. |
|---|
| 226 | polyhedron* mergechild; |
|---|
| 227 | |
|---|
| 228 | /// If the polyhedron serves as a mergechild for two of its parents, we need to have the address of the parents to access them. This |
|---|
| 229 | /// is the pointer to the positive parent being merged. |
|---|
| 230 | polyhedron* positiveparent; |
|---|
| 231 | |
|---|
| 232 | /// If the polyhedron serves as a mergechild for two of its parents, we need to have the address of the parents to access them. This |
|---|
| 233 | /// is the pointer to the negative parent being merged. |
|---|
| 234 | polyhedron* negativeparent; |
|---|
| 235 | |
|---|
| 236 | /// Adressing withing the statistic. Next_poly is a pointer to the next polyhedron in the statistic on the same level (if this is a point, |
|---|
| 237 | /// next_poly will be a point etc.). |
|---|
| 238 | polyhedron* next_poly; |
|---|
| 239 | |
|---|
| 240 | /// Adressing withing the statistic. Prev_poly is a pointer to the previous polyhedron in the statistic on the same level (if this is a point, |
|---|
| 241 | /// next_poly will be a point etc.). |
|---|
| 242 | polyhedron* prev_poly; |
|---|
| 243 | |
|---|
| 244 | /// A property counting the number of messages obtained from children within a classification procedure of position of the polyhedron related |
|---|
| 245 | /// an added/removed condition. If the message counter reaches the number of children, we know the polyhedrons' position has been fully classified. |
|---|
| 246 | int message_counter; |
|---|
| 247 | |
|---|
| 248 | /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. |
|---|
| 249 | set<simplex*> triangulation; |
|---|
| 250 | |
|---|
| 251 | /// A list of relative addresses serving for Hasse diagram construction. |
|---|
| 252 | list<int> kids_rel_addresses; |
|---|
| 253 | |
|---|
| 254 | /// Default constructor |
|---|
| 255 | polyhedron() |
|---|
| 256 | { |
|---|
| 257 | multiplicity = 1; |
|---|
| 258 | |
|---|
| 259 | message_counter = 0; |
|---|
| 260 | |
|---|
| 261 | totally_neutral = NULL; |
|---|
| 262 | |
|---|
| 263 | mergechild = NULL; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | /// Setter for raising multiplicity |
|---|
| 267 | void raise_multiplicity() |
|---|
| 268 | { |
|---|
| 269 | multiplicity++; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | /// Setter for lowering multiplicity |
|---|
| 273 | void lower_multiplicity() |
|---|
| 274 | { |
|---|
| 275 | multiplicity--; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | int get_multiplicity() |
|---|
| 279 | { |
|---|
| 280 | return multiplicity; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | void set_multiplicity(int multiplicity) |
|---|
| 284 | { |
|---|
| 285 | this->multiplicity = multiplicity; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
|---|
| 289 | int operator==(polyhedron polyhedron2) |
|---|
| 290 | { |
|---|
| 291 | return true; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
|---|
| 295 | int operator<(polyhedron polyhedron2) |
|---|
| 296 | { |
|---|
| 297 | return false; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | /// A setter of state of current polyhedron relative to the action specified in the argument. The three possible states of the |
|---|
| 302 | /// polyhedron are -1 - NEGATIVE, 0 - NEUTRAL, 1 - POSITIVE. Neutral state means that either the state has been reset or the polyhedron is |
|---|
| 303 | /// ready to be split/merged. |
|---|
| 304 | int set_state(double state_indicator, actions action) |
|---|
| 305 | { |
|---|
| 306 | switch(action) |
|---|
| 307 | { |
|---|
| 308 | case MERGE: |
|---|
| 309 | merge_state = (int)sign(state_indicator); |
|---|
| 310 | return merge_state; |
|---|
| 311 | case SPLIT: |
|---|
| 312 | split_state = (int)sign(state_indicator); |
|---|
| 313 | return split_state; |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | /// A getter of state of current polyhedron relative to the action specified in the argument. The three possible states of the |
|---|
| 318 | /// polyhedron are -1 - NEGATIVE, 0 - NEUTRAL, 1 - POSITIVE. Neutral state means that either the state has been reset or the polyhedron is |
|---|
| 319 | /// ready to be split/merged. |
|---|
| 320 | int get_state(actions action) |
|---|
| 321 | { |
|---|
| 322 | switch(action) |
|---|
| 323 | { |
|---|
| 324 | case MERGE: |
|---|
| 325 | return merge_state; |
|---|
| 326 | break; |
|---|
| 327 | case SPLIT: |
|---|
| 328 | return split_state; |
|---|
| 329 | break; |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | /// Method for obtaining the number of children of given polyhedron. |
|---|
| 334 | int number_of_children() |
|---|
| 335 | { |
|---|
| 336 | return children.size(); |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | /// A method for triangulation of given polyhedron. |
|---|
| 340 | double triangulate(bool should_integrate); |
|---|
| 341 | }; |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse |
|---|
| 345 | /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. |
|---|
| 346 | class vertex : public polyhedron |
|---|
| 347 | { |
|---|
| 348 | /// A dynamic array representing coordinates of the vertex |
|---|
| 349 | vec coordinates; |
|---|
| 350 | |
|---|
| 351 | public: |
|---|
| 352 | /// A property specifying the value of the density (ted nevim, jestli je to jakoby log nebo ne) above the vertex. |
|---|
| 353 | double function_value; |
|---|
| 354 | |
|---|
| 355 | /// Default constructor |
|---|
| 356 | vertex(); |
|---|
| 357 | |
|---|
| 358 | /// Constructor of a vertex from a set of coordinates |
|---|
| 359 | vertex(vec coordinates) |
|---|
| 360 | { |
|---|
| 361 | this->coordinates = coordinates; |
|---|
| 362 | |
|---|
| 363 | vertices.insert(this); |
|---|
| 364 | |
|---|
| 365 | simplex* vert_simplex = new simplex(vertices); |
|---|
| 366 | |
|---|
| 367 | triangulation.insert(vert_simplex); |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter |
|---|
| 371 | /// space of certain dimension is established, but the dimension is not known when the vertex is created. |
|---|
| 372 | void push_coordinate(double coordinate) |
|---|
| 373 | { |
|---|
| 374 | coordinates = concat(coordinates,coordinate); |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer |
|---|
| 378 | /// (not given by reference), but a new copy is created (they are given by value). |
|---|
| 379 | vec get_coordinates() |
|---|
| 380 | { |
|---|
| 381 | return coordinates; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | }; |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differen tiates |
|---|
| 388 | /// it from polyhedrons in other rows. |
|---|
| 389 | class toprow : public polyhedron |
|---|
| 390 | { |
|---|
| 391 | |
|---|
| 392 | public: |
|---|
| 393 | double probability; |
|---|
| 394 | |
|---|
| 395 | vertex* minimal_vertex; |
|---|
| 396 | |
|---|
| 397 | /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation |
|---|
| 398 | vec condition_sum; |
|---|
| 399 | |
|---|
| 400 | int condition_order; |
|---|
| 401 | |
|---|
| 402 | /// Default constructor |
|---|
| 403 | toprow(){}; |
|---|
| 404 | |
|---|
| 405 | /// Constructor creating a toprow from the condition |
|---|
| 406 | toprow(condition *condition, int condition_order) |
|---|
| 407 | { |
|---|
| 408 | this->condition_sum = condition->value; |
|---|
| 409 | this->condition_order = condition_order; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | toprow(vec condition_sum, int condition_order) |
|---|
| 413 | { |
|---|
| 414 | this->condition_sum = condition_sum; |
|---|
| 415 | this->condition_order = condition_order; |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | double integrate_simplex(simplex* simplex, char c); |
|---|
| 419 | |
|---|
| 420 | }; |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | class c_statistic |
|---|
| 429 | { |
|---|
| 430 | |
|---|
| 431 | public: |
|---|
| 432 | polyhedron* end_poly; |
|---|
| 433 | polyhedron* start_poly; |
|---|
| 434 | |
|---|
| 435 | vector<polyhedron*> rows; |
|---|
| 436 | |
|---|
| 437 | vector<polyhedron*> row_ends; |
|---|
| 438 | |
|---|
| 439 | c_statistic() |
|---|
| 440 | { |
|---|
| 441 | end_poly = new polyhedron(); |
|---|
| 442 | start_poly = new polyhedron(); |
|---|
| 443 | }; |
|---|
| 444 | |
|---|
| 445 | void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end) |
|---|
| 446 | { |
|---|
| 447 | if(row>((int)rows.size())-1) |
|---|
| 448 | { |
|---|
| 449 | if(row>rows.size()) |
|---|
| 450 | { |
|---|
| 451 | throw new exception("You are trying to append a polyhedron whose children are not in the statistic yet!"); |
|---|
| 452 | return; |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | rows.push_back(end_poly); |
|---|
| 456 | row_ends.push_back(end_poly); |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | // POSSIBLE FAILURE: the function is not checking if start and end are connected |
|---|
| 460 | |
|---|
| 461 | if(rows[row] != end_poly) |
|---|
| 462 | { |
|---|
| 463 | appended_start->prev_poly = row_ends[row]; |
|---|
| 464 | row_ends[row]->next_poly = appended_start; |
|---|
| 465 | |
|---|
| 466 | } |
|---|
| 467 | else if((row>0 && rows[row-1]!=end_poly)||row==0) |
|---|
| 468 | { |
|---|
| 469 | appended_start->prev_poly = start_poly; |
|---|
| 470 | rows[row]= appended_start; |
|---|
| 471 | } |
|---|
| 472 | else |
|---|
| 473 | { |
|---|
| 474 | throw new exception("Wrong polyhedron insertion into statistic: missing intermediary polyhedron!"); |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | appended_end->next_poly = end_poly; |
|---|
| 478 | row_ends[row] = appended_end; |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | void append_polyhedron(int row, polyhedron* appended_poly) |
|---|
| 482 | { |
|---|
| 483 | append_polyhedron(row,appended_poly,appended_poly); |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | void insert_polyhedron(int row, polyhedron* inserted_poly, polyhedron* following_poly) |
|---|
| 487 | { |
|---|
| 488 | if(following_poly != end_poly) |
|---|
| 489 | { |
|---|
| 490 | inserted_poly->next_poly = following_poly; |
|---|
| 491 | inserted_poly->prev_poly = following_poly->prev_poly; |
|---|
| 492 | |
|---|
| 493 | if(following_poly->prev_poly == start_poly) |
|---|
| 494 | { |
|---|
| 495 | rows[row] = inserted_poly; |
|---|
| 496 | } |
|---|
| 497 | else |
|---|
| 498 | { |
|---|
| 499 | inserted_poly->prev_poly->next_poly = inserted_poly; |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | following_poly->prev_poly = inserted_poly; |
|---|
| 503 | } |
|---|
| 504 | else |
|---|
| 505 | { |
|---|
| 506 | this->append_polyhedron(row, inserted_poly); |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | void delete_polyhedron(int row, polyhedron* deleted_poly) |
|---|
| 515 | { |
|---|
| 516 | if(deleted_poly->prev_poly != start_poly) |
|---|
| 517 | { |
|---|
| 518 | deleted_poly->prev_poly->next_poly = deleted_poly->next_poly; |
|---|
| 519 | } |
|---|
| 520 | else |
|---|
| 521 | { |
|---|
| 522 | rows[row] = deleted_poly->next_poly; |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | if(deleted_poly->next_poly!=end_poly) |
|---|
| 526 | { |
|---|
| 527 | deleted_poly->next_poly->prev_poly = deleted_poly->prev_poly; |
|---|
| 528 | } |
|---|
| 529 | else |
|---|
| 530 | { |
|---|
| 531 | row_ends[row] = deleted_poly->prev_poly; |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | |
|---|
| 536 | deleted_poly->next_poly = NULL; |
|---|
| 537 | deleted_poly->prev_poly = NULL; |
|---|
| 538 | } |
|---|
| 539 | |
|---|
| 540 | int size() |
|---|
| 541 | { |
|---|
| 542 | return rows.size(); |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | polyhedron* get_end() |
|---|
| 546 | { |
|---|
| 547 | return end_poly; |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | polyhedron* get_start() |
|---|
| 551 | { |
|---|
| 552 | return start_poly; |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | int row_size(int row) |
|---|
| 556 | { |
|---|
| 557 | if(this->size()>row && row>=0) |
|---|
| 558 | { |
|---|
| 559 | int row_size = 0; |
|---|
| 560 | |
|---|
| 561 | for(polyhedron* row_poly = rows[row]; row_poly!=end_poly; row_poly=row_poly->next_poly) |
|---|
| 562 | { |
|---|
| 563 | row_size++; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | return row_size; |
|---|
| 567 | } |
|---|
| 568 | else |
|---|
| 569 | { |
|---|
| 570 | throw new exception("There is no row to obtain size from!"); |
|---|
| 571 | } |
|---|
| 572 | } |
|---|
| 573 | }; |
|---|
| 574 | |
|---|
| 575 | |
|---|
| 576 | class my_ivec : public ivec |
|---|
| 577 | { |
|---|
| 578 | public: |
|---|
| 579 | my_ivec():ivec(){}; |
|---|
| 580 | |
|---|
| 581 | my_ivec(ivec origin):ivec() |
|---|
| 582 | { |
|---|
| 583 | this->ins(0,origin); |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | bool operator>(const my_ivec &second) const |
|---|
| 587 | { |
|---|
| 588 | return max(*this)>max(second); |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | bool operator==(const my_ivec &second) const |
|---|
| 592 | { |
|---|
| 593 | return max(*this)==max(second); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | bool operator<(const my_ivec &second) const |
|---|
| 597 | { |
|---|
| 598 | return !(((*this)>second)||((*this)==second)); |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | bool operator!=(const my_ivec &second) const |
|---|
| 602 | { |
|---|
| 603 | return !((*this)==second); |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | bool operator<=(const my_ivec &second) const |
|---|
| 607 | { |
|---|
| 608 | return !((*this)>second); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | bool operator>=(const my_ivec &second) const |
|---|
| 612 | { |
|---|
| 613 | return !((*this)<second); |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | my_ivec right(my_ivec original) |
|---|
| 617 | { |
|---|
| 618 | |
|---|
| 619 | } |
|---|
| 620 | }; |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | |
|---|
| 628 | //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density |
|---|
| 629 | class emlig // : eEF |
|---|
| 630 | { |
|---|
| 631 | |
|---|
| 632 | /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result |
|---|
| 633 | /// of data update from Bayesian estimation or set by the user if this emlig is a prior density |
|---|
| 634 | |
|---|
| 635 | |
|---|
| 636 | vector<list<polyhedron*>> for_splitting; |
|---|
| 637 | |
|---|
| 638 | vector<list<polyhedron*>> for_merging; |
|---|
| 639 | |
|---|
| 640 | list<condition*> conditions; |
|---|
| 641 | |
|---|
| 642 | double normalization_factor; |
|---|
| 643 | |
|---|
| 644 | int condition_order; |
|---|
| 645 | |
|---|
| 646 | double last_log_nc; |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | void alter_toprow_conditions(condition *condition, bool should_be_added) |
|---|
| 651 | { |
|---|
| 652 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
|---|
| 653 | { |
|---|
| 654 | set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); |
|---|
| 655 | |
|---|
| 656 | do |
|---|
| 657 | { |
|---|
| 658 | vertex_ref++; |
|---|
| 659 | |
|---|
| 660 | if(vertex_ref==horiz_ref->vertices.end()) |
|---|
| 661 | { |
|---|
| 662 | return; |
|---|
| 663 | } |
|---|
| 664 | } |
|---|
| 665 | while((*vertex_ref)->parentconditions.find(condition)!=(*vertex_ref)->parentconditions.end()); |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | vec appended_coords = (*vertex_ref)->get_coordinates(); |
|---|
| 670 | appended_coords.ins(0,-1.0); |
|---|
| 671 | |
|---|
| 672 | double product = appended_coords*condition->value; |
|---|
| 673 | |
|---|
| 674 | if(should_be_added) |
|---|
| 675 | { |
|---|
| 676 | ((toprow*) horiz_ref)->condition_order++; |
|---|
| 677 | |
|---|
| 678 | if(product>0) |
|---|
| 679 | { |
|---|
| 680 | ((toprow*) horiz_ref)->condition_sum += condition->value; |
|---|
| 681 | } |
|---|
| 682 | else |
|---|
| 683 | { |
|---|
| 684 | ((toprow*) horiz_ref)->condition_sum -= condition->value; |
|---|
| 685 | } |
|---|
| 686 | } |
|---|
| 687 | else |
|---|
| 688 | { |
|---|
| 689 | ((toprow*) horiz_ref)->condition_order--; |
|---|
| 690 | |
|---|
| 691 | if(product<0) |
|---|
| 692 | { |
|---|
| 693 | ((toprow*) horiz_ref)->condition_sum += condition->value; |
|---|
| 694 | } |
|---|
| 695 | else |
|---|
| 696 | { |
|---|
| 697 | ((toprow*) horiz_ref)->condition_sum -= condition->value; |
|---|
| 698 | } |
|---|
| 699 | } |
|---|
| 700 | } |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | /// A method for recursive classification of polyhedrons with respect to SPLITting and MERGEing conditions. |
|---|
| 705 | void send_state_message(polyhedron* sender, condition *toadd, condition *toremove, int level) |
|---|
| 706 | { |
|---|
| 707 | |
|---|
| 708 | int multi = sender->get_multiplicity(); |
|---|
| 709 | // We translate existence of toremove and toadd conditions to booleans for ease of manipulation |
|---|
| 710 | bool shouldmerge = (toremove != NULL); |
|---|
| 711 | bool shouldsplit = (toadd != NULL); |
|---|
| 712 | |
|---|
| 713 | // If such operation is desired, in the following cycle we send a message about polyhedrons classification |
|---|
| 714 | // to all its parents. We loop through the parents and report the child sending its message. |
|---|
| 715 | if(shouldsplit||shouldmerge) |
|---|
| 716 | { |
|---|
| 717 | for(list<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator!=sender->parents.end();parent_iterator++) |
|---|
| 718 | { |
|---|
| 719 | // We set an individual pointer to the value at parent_iterator for ease of use |
|---|
| 720 | polyhedron* current_parent = *parent_iterator; |
|---|
| 721 | |
|---|
| 722 | // The message_counter counts the number of messages received by the parent |
|---|
| 723 | current_parent->message_counter++; |
|---|
| 724 | |
|---|
| 725 | // If the child is the last one to send its message, the parent can as well be classified and |
|---|
| 726 | // send its message further up. |
|---|
| 727 | bool is_last = (current_parent->message_counter == current_parent->number_of_children()); |
|---|
| 728 | |
|---|
| 729 | // Certain properties need to be set if this is the first message received by the parent |
|---|
| 730 | bool is_first = (current_parent->message_counter == 1); |
|---|
| 731 | |
|---|
| 732 | // This boolean watches for polyhedrons that are already out of the game for further MERGEing |
|---|
| 733 | // and SPLITting purposes. This may seem quite straightforward at first, but because of all |
|---|
| 734 | // the operations involved it may be quite complicated. For example a polyhedron laying in the |
|---|
| 735 | // positive side of the MERGEing hyperplane should not be split, because it lays in the positive |
|---|
| 736 | // part of the location parameter space relative to the SPLITting hyperplane, but because it |
|---|
| 737 | // is merged with its MERGE negative counterpart, which is being SPLIT, the polyhedron itself |
|---|
| 738 | // will be SPLIT after it has been merged and needs to retain all properties needed for the |
|---|
| 739 | // purposes of SPLITting. |
|---|
| 740 | bool out_of_the_game = true; |
|---|
| 741 | |
|---|
| 742 | if(shouldmerge) |
|---|
| 743 | { |
|---|
| 744 | // get the MERGE state of the child |
|---|
| 745 | int child_state = sender->get_state(MERGE); |
|---|
| 746 | // get the MERGE state of the parent so far, the parent can be partially classified |
|---|
| 747 | int parent_state = current_parent->get_state(MERGE); |
|---|
| 748 | |
|---|
| 749 | // In case this is the first message received by the parent, its state has not been set yet |
|---|
| 750 | // and therefore it inherits the MERGE state of the child. On the other hand if the state |
|---|
| 751 | // of the parent is 0, all the children so far were neutral and if the next child isn't |
|---|
| 752 | // neutral the parent should be in state of the child again. |
|---|
| 753 | if(parent_state == 0||is_first) |
|---|
| 754 | { |
|---|
| 755 | parent_state = current_parent->set_state(child_state, MERGE); |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | // If a child is contained in the hyperplane of a condition that should be removed and it is |
|---|
| 759 | // not of multiplicity higher than 1, it will later serve as a merger for two of its parents |
|---|
| 760 | // each lying on one side of the removed hyperplane (one being classified MERGE positive, the |
|---|
| 761 | // other MERGE negative). Here we set the possible merger candidates. |
|---|
| 762 | if(child_state == 0) |
|---|
| 763 | { |
|---|
| 764 | if(current_parent->mergechild == NULL) |
|---|
| 765 | { |
|---|
| 766 | current_parent->mergechild = sender; |
|---|
| 767 | } |
|---|
| 768 | } |
|---|
| 769 | |
|---|
| 770 | // If the parent obtained a message from the last one of its children we have to classify it |
|---|
| 771 | // with respect to the MERGE condition. |
|---|
| 772 | if(is_last) |
|---|
| 773 | { |
|---|
| 774 | // If the parent is a toprow from the top row of the Hasse diagram, we alter the condition |
|---|
| 775 | // sum and condition order with respect to on which side of the cutting hyperplane the |
|---|
| 776 | // toprow is located. |
|---|
| 777 | if(level == number_of_parameters-1) |
|---|
| 778 | { |
|---|
| 779 | // toprow on the positive side |
|---|
| 780 | if(parent_state == 1) |
|---|
| 781 | { |
|---|
| 782 | ((toprow*)current_parent)->condition_sum-=toremove->value; |
|---|
| 783 | } |
|---|
| 784 | |
|---|
| 785 | // toprow on the negative side |
|---|
| 786 | if(parent_state == -1) |
|---|
| 787 | { |
|---|
| 788 | ((toprow*)current_parent)->condition_sum+=toremove->value; |
|---|
| 789 | } |
|---|
| 790 | } |
|---|
| 791 | |
|---|
| 792 | // lowering the condition order. |
|---|
| 793 | // REMARK: This maybe could be done more globally for the whole statistic. |
|---|
| 794 | ((toprow*)current_parent)->condition_order--; |
|---|
| 795 | |
|---|
| 796 | // If the parent is a candidate for being MERGEd |
|---|
| 797 | if(current_parent->mergechild != NULL) |
|---|
| 798 | { |
|---|
| 799 | // It might not be out of the game |
|---|
| 800 | out_of_the_game = false; |
|---|
| 801 | |
|---|
| 802 | // If the mergechild multiplicity is 1 it will disappear after merging |
|---|
| 803 | if(current_parent->mergechild->get_multiplicity()==1) |
|---|
| 804 | { |
|---|
| 805 | // and because we need the child to have an address of the two parents it is |
|---|
| 806 | // supposed to merge, we assign the address of current parent to one of the |
|---|
| 807 | // two pointers existing in the child for this purpose regarding to its position |
|---|
| 808 | // in the location parameter space with respect to the MERGE hyperplane. |
|---|
| 809 | if(parent_state > 0) |
|---|
| 810 | { |
|---|
| 811 | current_parent->mergechild->positiveparent = current_parent; |
|---|
| 812 | } |
|---|
| 813 | |
|---|
| 814 | if(parent_state < 0) |
|---|
| 815 | { |
|---|
| 816 | current_parent->mergechild->negativeparent = current_parent; |
|---|
| 817 | } |
|---|
| 818 | } |
|---|
| 819 | else |
|---|
| 820 | { |
|---|
| 821 | // If the mergechild has higher multiplicity, it will not disappear after the |
|---|
| 822 | // condition is removed and the parent will still be out of the game, because |
|---|
| 823 | // no MERGEing will occur. |
|---|
| 824 | out_of_the_game = true; |
|---|
| 825 | } |
|---|
| 826 | } |
|---|
| 827 | |
|---|
| 828 | // If so far the parent is out of the game, it is the toprow polyhedron and there will |
|---|
| 829 | // be no SPLITting, we compute its probability integral by summing all the integral |
|---|
| 830 | // from the simplices contained in it. |
|---|
| 831 | if(out_of_the_game) |
|---|
| 832 | { |
|---|
| 833 | if((level == number_of_parameters - 1) && (!shouldsplit)) |
|---|
| 834 | { |
|---|
| 835 | toprow* cur_par_toprow = ((toprow*)current_parent); |
|---|
| 836 | cur_par_toprow->probability = 0.0; |
|---|
| 837 | |
|---|
| 838 | for(set<simplex*>::iterator s_ref = current_parent->triangulation.begin();s_ref!=current_parent->triangulation.end();s_ref++) |
|---|
| 839 | { |
|---|
| 840 | double cur_prob = cur_par_toprow->integrate_simplex((*s_ref),'C'); |
|---|
| 841 | |
|---|
| 842 | cur_par_toprow->probability += cur_prob; |
|---|
| 843 | } |
|---|
| 844 | |
|---|
| 845 | normalization_factor += cur_par_toprow->probability; |
|---|
| 846 | } |
|---|
| 847 | } |
|---|
| 848 | |
|---|
| 849 | // If the parent is classified MERGE neutral, it will serve as a merger for two of its |
|---|
| 850 | // parents so we report it to the for_merging list. |
|---|
| 851 | if(parent_state == 0) |
|---|
| 852 | { |
|---|
| 853 | for_merging[level+1].push_back(current_parent); |
|---|
| 854 | } |
|---|
| 855 | } |
|---|
| 856 | } |
|---|
| 857 | |
|---|
| 858 | // In the second part of the classification procedure, we will classify the parent polyhedron |
|---|
| 859 | // for the purposes of SPLITting. Since splitting comes from a parent that is being split by |
|---|
| 860 | // creating a neutral child that cuts the split polyhedron in two parts, the created child has |
|---|
| 861 | // to be connected to all the neutral grandchildren of the source parent. We therefore have to |
|---|
| 862 | // report all such grandchildren of the parent. More complication is brought in by grandchildren |
|---|
| 863 | // that have not been created in the process of splitting, but were classified SPLIT neutral |
|---|
| 864 | // already in the classification stage. Such grandchildren and children were already present |
|---|
| 865 | // in the Hasse diagram befor the SPLITting condition emerged. We call such object totallyneutral. |
|---|
| 866 | // They have to be watched and treated separately. |
|---|
| 867 | if(shouldsplit) |
|---|
| 868 | { |
|---|
| 869 | // We report the totally neutral children of the message sending child into the totally neutral |
|---|
| 870 | // grandchildren list of current parent. |
|---|
| 871 | current_parent->totallyneutralgrandchildren.insert(sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); |
|---|
| 872 | |
|---|
| 873 | // We need to have the pointers from grandchildren to grandparents as well, we therefore set |
|---|
| 874 | // the opposite relation as well. |
|---|
| 875 | for(set<polyhedron*>::iterator tot_child_ref = sender->totallyneutralchildren.begin();tot_child_ref!=sender->totallyneutralchildren.end();tot_child_ref++) |
|---|
| 876 | { |
|---|
| 877 | (*tot_child_ref)->grandparents.insert(current_parent); |
|---|
| 878 | } |
|---|
| 879 | |
|---|
| 880 | // If this is the first child to report its total neutrality, the parent inherits its state. |
|---|
| 881 | if(current_parent->totally_neutral == NULL) |
|---|
| 882 | { |
|---|
| 883 | current_parent->totally_neutral = sender->totally_neutral; |
|---|
| 884 | } |
|---|
| 885 | // else the parent is totally neutral only if all the children up to now are totally neutral. |
|---|
| 886 | else |
|---|
| 887 | { |
|---|
| 888 | current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; |
|---|
| 889 | } |
|---|
| 890 | |
|---|
| 891 | // For splitting purposes, we have to mark all the children of the given parent by their SPLIT |
|---|
| 892 | // state, because when we split the parent, we create its positive and negative offsprings and |
|---|
| 893 | // its children have to be assigned accordingly. |
|---|
| 894 | switch(sender->get_state(SPLIT)) |
|---|
| 895 | { |
|---|
| 896 | case 1: |
|---|
| 897 | // child classified positive |
|---|
| 898 | current_parent->positivechildren.push_back(sender); |
|---|
| 899 | |
|---|
| 900 | // all the vertices of the positive child are assigned to the positive and neutral vertex |
|---|
| 901 | // set |
|---|
| 902 | current_parent->positiveneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
|---|
| 903 | break; |
|---|
| 904 | case 0: |
|---|
| 905 | // child classified neutral |
|---|
| 906 | current_parent->neutralchildren.push_back(sender); |
|---|
| 907 | |
|---|
| 908 | // all the vertices of the neutral child are assigned to both negative and positive vertex |
|---|
| 909 | // sets |
|---|
| 910 | if(level!=0) |
|---|
| 911 | { |
|---|
| 912 | current_parent->positiveneutralvertices.insert(sender->positiveneutralvertices.begin(),sender->positiveneutralvertices.end()); |
|---|
| 913 | current_parent->negativeneutralvertices.insert(sender->negativeneutralvertices.begin(),sender->negativeneutralvertices.end()); |
|---|
| 914 | } |
|---|
| 915 | else |
|---|
| 916 | { |
|---|
| 917 | current_parent->positiveneutralvertices.insert(*sender->vertices.begin()); |
|---|
| 918 | current_parent->negativeneutralvertices.insert(*sender->vertices.begin()); |
|---|
| 919 | } |
|---|
| 920 | |
|---|
| 921 | // if the child is totally neutral it is also assigned to the totallyneutralchildren |
|---|
| 922 | if(sender->totally_neutral) |
|---|
| 923 | { |
|---|
| 924 | current_parent->totallyneutralchildren.insert(sender); |
|---|
| 925 | } |
|---|
| 926 | |
|---|
| 927 | break; |
|---|
| 928 | case -1: |
|---|
| 929 | // child classified negative |
|---|
| 930 | current_parent->negativechildren.push_back(sender); |
|---|
| 931 | current_parent->negativeneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
|---|
| 932 | break; |
|---|
| 933 | } |
|---|
| 934 | |
|---|
| 935 | // If the last child has sent its message to the parent, we have to decide if the polyhedron |
|---|
| 936 | // needs to be split. |
|---|
| 937 | if(is_last) |
|---|
| 938 | { |
|---|
| 939 | // If the polyhedron extends to both sides of the cutting hyperplane it needs to be SPLIT. Such |
|---|
| 940 | // situation occurs if either the polyhedron has negative and also positive children or |
|---|
| 941 | // if the polyhedron contains neutral children that cross the cutting hyperplane. Such |
|---|
| 942 | // neutral children cannot be totally neutral, since totally neutral children lay within |
|---|
| 943 | // the cutting hyperplane. If the polyhedron is to be cut its state is set to SPLIT neutral |
|---|
| 944 | if((current_parent->negativechildren.size()>0&¤t_parent->positivechildren.size()>0) |
|---|
| 945 | ||(current_parent->neutralchildren.size()>0&¤t_parent->totallyneutralchildren.empty())) |
|---|
| 946 | { |
|---|
| 947 | for_splitting[level+1].push_back(current_parent); |
|---|
| 948 | current_parent->set_state(0, SPLIT); |
|---|
| 949 | } |
|---|
| 950 | else |
|---|
| 951 | { |
|---|
| 952 | // Else if the polyhedron has a positive number of negative children we set its state |
|---|
| 953 | // to SPLIT negative. In such a case we subtract current condition from the overall |
|---|
| 954 | // condition sum |
|---|
| 955 | if(current_parent->negativechildren.size()>0) |
|---|
| 956 | { |
|---|
| 957 | // set the state |
|---|
| 958 | current_parent->set_state(-1, SPLIT); |
|---|
| 959 | |
|---|
| 960 | // alter the condition sum |
|---|
| 961 | if(level == number_of_parameters-1) |
|---|
| 962 | { |
|---|
| 963 | ((toprow*)current_parent)->condition_sum-=toadd->value; |
|---|
| 964 | } |
|---|
| 965 | } |
|---|
| 966 | // If the polyhedron has a positive number of positive children we set its state |
|---|
| 967 | // to SPLIT positive. In such a case we add current condition to the overall |
|---|
| 968 | // condition sum |
|---|
| 969 | else if(current_parent->positivechildren.size()>0) |
|---|
| 970 | { |
|---|
| 971 | // set the state |
|---|
| 972 | current_parent->set_state(1, SPLIT); |
|---|
| 973 | |
|---|
| 974 | // alter the condition sum |
|---|
| 975 | if(level == number_of_parameters-1) |
|---|
| 976 | { |
|---|
| 977 | ((toprow*)current_parent)->condition_sum+=toadd->value; |
|---|
| 978 | } |
|---|
| 979 | } |
|---|
| 980 | // Else the polyhedron only has children that are totally neutral. In such a case, |
|---|
| 981 | // we mark it totally neutral as well and insert the SPLIT condition into the |
|---|
| 982 | // parent conditions of the polyhedron. No addition or subtraction is needed in |
|---|
| 983 | // this case. |
|---|
| 984 | else |
|---|
| 985 | { |
|---|
| 986 | current_parent->raise_multiplicity(); |
|---|
| 987 | current_parent->totally_neutral = true; |
|---|
| 988 | current_parent->parentconditions.insert(toadd); |
|---|
| 989 | } |
|---|
| 990 | |
|---|
| 991 | // In either case we raise the condition order (statistical condition sum significance) |
|---|
| 992 | ((toprow*)current_parent)->condition_order++; |
|---|
| 993 | |
|---|
| 994 | // In case the polyhedron is a toprow and it will not be SPLIT, we compute its probability |
|---|
| 995 | // integral with the altered condition. |
|---|
| 996 | if(level == number_of_parameters - 1 && current_parent->mergechild == NULL) |
|---|
| 997 | { |
|---|
| 998 | toprow* cur_par_toprow = ((toprow*)current_parent); |
|---|
| 999 | cur_par_toprow->probability = 0.0; |
|---|
| 1000 | |
|---|
| 1001 | // We compute the integral as a sum over all simplices contained within the |
|---|
| 1002 | // polyhedron. |
|---|
| 1003 | for(set<simplex*>::iterator s_ref = current_parent->triangulation.begin();s_ref!=current_parent->triangulation.end();s_ref++) |
|---|
| 1004 | { |
|---|
| 1005 | double cur_prob = cur_par_toprow->integrate_simplex((*s_ref),'C'); |
|---|
| 1006 | |
|---|
| 1007 | cur_par_toprow->probability += cur_prob; |
|---|
| 1008 | } |
|---|
| 1009 | |
|---|
| 1010 | normalization_factor += cur_par_toprow->probability; |
|---|
| 1011 | } |
|---|
| 1012 | |
|---|
| 1013 | // If the parent polyhedron is out of the game, so that it will not be MERGEd or |
|---|
| 1014 | // SPLIT any more, we will reset the lists specifying its relation with respect |
|---|
| 1015 | // to the SPLITting condition, so that they will be clear for future use. |
|---|
| 1016 | if(out_of_the_game) |
|---|
| 1017 | { |
|---|
| 1018 | current_parent->positivechildren.clear(); |
|---|
| 1019 | current_parent->negativechildren.clear(); |
|---|
| 1020 | current_parent->neutralchildren.clear(); |
|---|
| 1021 | current_parent->totallyneutralgrandchildren.clear(); |
|---|
| 1022 | current_parent->positiveneutralvertices.clear(); |
|---|
| 1023 | current_parent->negativeneutralvertices.clear(); |
|---|
| 1024 | current_parent->totally_neutral = NULL; |
|---|
| 1025 | current_parent->kids_rel_addresses.clear(); |
|---|
| 1026 | } |
|---|
| 1027 | } |
|---|
| 1028 | } |
|---|
| 1029 | } |
|---|
| 1030 | |
|---|
| 1031 | // Finally if the the parent polyhedron has been SPLIT and MERGE classified, we will send a message |
|---|
| 1032 | // about its classification to its parents. |
|---|
| 1033 | if(is_last) |
|---|
| 1034 | { |
|---|
| 1035 | current_parent->mergechild = NULL; |
|---|
| 1036 | current_parent->message_counter = 0; |
|---|
| 1037 | |
|---|
| 1038 | send_state_message(current_parent,toadd,toremove,level+1); |
|---|
| 1039 | } |
|---|
| 1040 | |
|---|
| 1041 | } |
|---|
| 1042 | |
|---|
| 1043 | // We clear the totally neutral children of the child here, because we needed them to be assigned as |
|---|
| 1044 | // totally neutral grandchildren to all its parents. |
|---|
| 1045 | sender->totallyneutralchildren.clear(); |
|---|
| 1046 | } |
|---|
| 1047 | } |
|---|
| 1048 | |
|---|
| 1049 | public: |
|---|
| 1050 | c_statistic statistic; |
|---|
| 1051 | |
|---|
| 1052 | vertex* minimal_vertex; |
|---|
| 1053 | |
|---|
| 1054 | double min_ll; |
|---|
| 1055 | |
|---|
| 1056 | double log_nc; |
|---|
| 1057 | |
|---|
| 1058 | |
|---|
| 1059 | |
|---|
| 1060 | vector<multiset<my_ivec>> correction_factors; |
|---|
| 1061 | |
|---|
| 1062 | int number_of_parameters; |
|---|
| 1063 | |
|---|
| 1064 | /// A default constructor creates an emlig with predefined statistic representing only the range of the given |
|---|
| 1065 | /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. |
|---|
| 1066 | emlig(int number_of_parameters, double alpha_deviation, double sigma_deviation, int nu) |
|---|
| 1067 | { |
|---|
| 1068 | this->number_of_parameters = number_of_parameters; |
|---|
| 1069 | |
|---|
| 1070 | condition_order = nu; |
|---|
| 1071 | |
|---|
| 1072 | create_statistic(number_of_parameters, alpha_deviation, sigma_deviation); |
|---|
| 1073 | |
|---|
| 1074 | //step_me(10); |
|---|
| 1075 | |
|---|
| 1076 | min_ll = numeric_limits<double>::max(); |
|---|
| 1077 | |
|---|
| 1078 | |
|---|
| 1079 | double normalization_factor = 0; |
|---|
| 1080 | int counter = 0; |
|---|
| 1081 | for(polyhedron* top_ref = statistic.rows[number_of_parameters];top_ref!=statistic.get_end();top_ref=top_ref->next_poly) |
|---|
| 1082 | { |
|---|
| 1083 | counter++; |
|---|
| 1084 | toprow* cur_toprow = (toprow*)top_ref; |
|---|
| 1085 | |
|---|
| 1086 | set<simplex*>::iterator cur_simplex = cur_toprow->triangulation.begin(); |
|---|
| 1087 | normalization_factor += cur_toprow->integrate_simplex(*cur_simplex,'X'); |
|---|
| 1088 | } |
|---|
| 1089 | |
|---|
| 1090 | last_log_nc = NULL; |
|---|
| 1091 | log_nc = log(normalization_factor); |
|---|
| 1092 | |
|---|
| 1093 | cout << "Prior constructed." << endl; |
|---|
| 1094 | } |
|---|
| 1095 | |
|---|
| 1096 | /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a |
|---|
| 1097 | /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. |
|---|
| 1098 | emlig(c_statistic statistic, int condition_order) |
|---|
| 1099 | { |
|---|
| 1100 | this->statistic = statistic; |
|---|
| 1101 | |
|---|
| 1102 | min_ll = numeric_limits<double>::max(); |
|---|
| 1103 | |
|---|
| 1104 | this->condition_order = condition_order; |
|---|
| 1105 | } |
|---|
| 1106 | |
|---|
| 1107 | ~emlig() |
|---|
| 1108 | { |
|---|
| 1109 | for(int i = 0;i<statistic.size();i++) |
|---|
| 1110 | { |
|---|
| 1111 | polyhedron* current_polyhedron; |
|---|
| 1112 | while(statistic.rows[i]!=statistic.end_poly) |
|---|
| 1113 | { |
|---|
| 1114 | current_polyhedron = statistic.rows[i]; |
|---|
| 1115 | statistic.delete_polyhedron(i,statistic.rows[i]); |
|---|
| 1116 | delete current_polyhedron; |
|---|
| 1117 | } |
|---|
| 1118 | } |
|---|
| 1119 | |
|---|
| 1120 | delete statistic.end_poly; |
|---|
| 1121 | delete statistic.start_poly; |
|---|
| 1122 | |
|---|
| 1123 | for(list<condition*>::reverse_iterator c_ref = conditions.rbegin();c_ref!=conditions.rend();c_ref++) |
|---|
| 1124 | { |
|---|
| 1125 | delete *c_ref; |
|---|
| 1126 | } |
|---|
| 1127 | |
|---|
| 1128 | cout << "Emlig deleted." << endl; |
|---|
| 1129 | } |
|---|
| 1130 | |
|---|
| 1131 | |
|---|
| 1132 | void step_me(int marker) |
|---|
| 1133 | { |
|---|
| 1134 | set<int> orders; |
|---|
| 1135 | |
|---|
| 1136 | for(int i = 0;i<statistic.size();i++) |
|---|
| 1137 | { |
|---|
| 1138 | //int zero = 0; |
|---|
| 1139 | //int one = 0; |
|---|
| 1140 | //int two = 0; |
|---|
| 1141 | |
|---|
| 1142 | for(polyhedron* horiz_ref = statistic.rows[i];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
|---|
| 1143 | { |
|---|
| 1144 | |
|---|
| 1145 | |
|---|
| 1146 | |
|---|
| 1147 | //if(i==statistic.size()-1) |
|---|
| 1148 | //{ |
|---|
| 1149 | // orders.insert(((toprow*)horiz_ref)->condition_order); |
|---|
| 1150 | |
|---|
| 1151 | /* |
|---|
| 1152 | cout << ((toprow*)horiz_ref)->condition_sum << " " << ((toprow*)horiz_ref)->probability << endl; |
|---|
| 1153 | cout << "Condition: " << ((toprow*)horiz_ref)->condition_sum << endl; |
|---|
| 1154 | cout << "Order:" << ((toprow*)horiz_ref)->condition_order << endl;*/ |
|---|
| 1155 | //} |
|---|
| 1156 | |
|---|
| 1157 | |
|---|
| 1158 | // cout << "Stepped." << endl; |
|---|
| 1159 | |
|---|
| 1160 | if(marker==101) |
|---|
| 1161 | { |
|---|
| 1162 | if(!(*horiz_ref).negativechildren.empty()||!(*horiz_ref).positivechildren.empty()||!(*horiz_ref).neutralchildren.empty()||!(*horiz_ref).kids_rel_addresses.empty()||!(*horiz_ref).mergechild==NULL||!(*horiz_ref).negativeneutralvertices.empty()) |
|---|
| 1163 | { |
|---|
| 1164 | cout << "Cleaning error!" << endl; |
|---|
| 1165 | } |
|---|
| 1166 | |
|---|
| 1167 | } |
|---|
| 1168 | |
|---|
| 1169 | /* |
|---|
| 1170 | for(set<simplex*>::iterator sim_ref = (*horiz_ref).triangulation.begin();sim_ref!=(*horiz_ref).triangulation.end();sim_ref++) |
|---|
| 1171 | { |
|---|
| 1172 | if((*sim_ref)->vertices.size()!=i+1) |
|---|
| 1173 | { |
|---|
| 1174 | cout << "Something is wrong." << endl; |
|---|
| 1175 | } |
|---|
| 1176 | } |
|---|
| 1177 | */ |
|---|
| 1178 | |
|---|
| 1179 | /* |
|---|
| 1180 | if(i==0) |
|---|
| 1181 | { |
|---|
| 1182 | cout << ((vertex*)horiz_ref)->get_coordinates() << endl; |
|---|
| 1183 | } |
|---|
| 1184 | */ |
|---|
| 1185 | |
|---|
| 1186 | /* |
|---|
| 1187 | char* string = "Checkpoint"; |
|---|
| 1188 | |
|---|
| 1189 | |
|---|
| 1190 | if((*horiz_ref).parentconditions.size()==0) |
|---|
| 1191 | { |
|---|
| 1192 | zero++; |
|---|
| 1193 | } |
|---|
| 1194 | else if((*horiz_ref).parentconditions.size()==1) |
|---|
| 1195 | { |
|---|
| 1196 | one++; |
|---|
| 1197 | } |
|---|
| 1198 | else |
|---|
| 1199 | { |
|---|
| 1200 | two++; |
|---|
| 1201 | } |
|---|
| 1202 | */ |
|---|
| 1203 | |
|---|
| 1204 | } |
|---|
| 1205 | } |
|---|
| 1206 | |
|---|
| 1207 | |
|---|
| 1208 | /* |
|---|
| 1209 | list<vec> table_entries; |
|---|
| 1210 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.row_ends[statistic.size()-1];horiz_ref=horiz_ref->next_poly) |
|---|
| 1211 | { |
|---|
| 1212 | toprow *current_toprow = (toprow*)(horiz_ref); |
|---|
| 1213 | for(list<set<vertex*>>::iterator tri_ref = current_toprow->triangulation.begin();tri_ref!=current_toprow->triangulation.end();tri_ref++) |
|---|
| 1214 | { |
|---|
| 1215 | for(set<vertex*>::iterator vert_ref = (*tri_ref).begin();vert_ref!=(*tri_ref).end();vert_ref++) |
|---|
| 1216 | { |
|---|
| 1217 | vec table_entry = vec(); |
|---|
| 1218 | |
|---|
| 1219 | table_entry.ins(0,(*vert_ref)->get_coordinates()*current_toprow->condition.get(1,current_toprow->condition.size()-1)-current_toprow->condition.get(0,0)); |
|---|
| 1220 | |
|---|
| 1221 | table_entry.ins(0,(*vert_ref)->get_coordinates()); |
|---|
| 1222 | |
|---|
| 1223 | table_entries.push_back(table_entry); |
|---|
| 1224 | } |
|---|
| 1225 | } |
|---|
| 1226 | } |
|---|
| 1227 | |
|---|
| 1228 | unique(table_entries.begin(),table_entries.end()); |
|---|
| 1229 | |
|---|
| 1230 | |
|---|
| 1231 | |
|---|
| 1232 | for(list<vec>::iterator entry_ref = table_entries.begin();entry_ref!=table_entries.end();entry_ref++) |
|---|
| 1233 | { |
|---|
| 1234 | ofstream myfile; |
|---|
| 1235 | myfile.open("robust_data.txt", ios::out | ios::app); |
|---|
| 1236 | if (myfile.is_open()) |
|---|
| 1237 | { |
|---|
| 1238 | for(int i = 0;i<(*entry_ref).size();i++) |
|---|
| 1239 | { |
|---|
| 1240 | myfile << (*entry_ref)[i] << ";"; |
|---|
| 1241 | } |
|---|
| 1242 | myfile << endl; |
|---|
| 1243 | |
|---|
| 1244 | myfile.close(); |
|---|
| 1245 | } |
|---|
| 1246 | else |
|---|
| 1247 | { |
|---|
| 1248 | cout << "File problem." << endl; |
|---|
| 1249 | } |
|---|
| 1250 | } |
|---|
| 1251 | */ |
|---|
| 1252 | |
|---|
| 1253 | |
|---|
| 1254 | return; |
|---|
| 1255 | } |
|---|
| 1256 | |
|---|
| 1257 | int statistic_rowsize(int row) |
|---|
| 1258 | { |
|---|
| 1259 | return statistic.row_size(row); |
|---|
| 1260 | } |
|---|
| 1261 | |
|---|
| 1262 | void add_condition(vec toadd) |
|---|
| 1263 | { |
|---|
| 1264 | vec null_vector = ""; |
|---|
| 1265 | |
|---|
| 1266 | add_and_remove_condition(toadd, null_vector); |
|---|
| 1267 | } |
|---|
| 1268 | |
|---|
| 1269 | |
|---|
| 1270 | void remove_condition(vec toremove) |
|---|
| 1271 | { |
|---|
| 1272 | vec null_vector = ""; |
|---|
| 1273 | |
|---|
| 1274 | add_and_remove_condition(null_vector, toremove); |
|---|
| 1275 | } |
|---|
| 1276 | |
|---|
| 1277 | void add_and_remove_condition(vec toadd, vec toremove) |
|---|
| 1278 | { |
|---|
| 1279 | |
|---|
| 1280 | // New condition arrived (new data are available). Here we will perform the Bayesian data update |
|---|
| 1281 | // step by splitting the location parameter space with respect to the new condition and computing |
|---|
| 1282 | // normalization integrals for each polyhedron in the location parameter space. |
|---|
| 1283 | |
|---|
| 1284 | // First we reset previous value of normalization factor and maximum value of the log likelihood. |
|---|
| 1285 | // Because there is a minus sign in the exponent of the likelihood, we really search for a minimum |
|---|
| 1286 | // and here we set min_ll to a high value. |
|---|
| 1287 | normalization_factor = 0; |
|---|
| 1288 | min_ll = numeric_limits<double>::max(); |
|---|
| 1289 | |
|---|
| 1290 | // We translate the presence of a condition to add to a boolean. Also, if moving window version of |
|---|
| 1291 | // data update is used, we check for the presence of a condition to be removed from consideration. |
|---|
| 1292 | // To take care of addition and deletion of a condition in one method is computationally better than |
|---|
| 1293 | // treating both cases separately. |
|---|
| 1294 | bool should_remove = (toremove.size() != 0); |
|---|
| 1295 | bool should_add = (toadd.size() != 0); |
|---|
| 1296 | |
|---|
| 1297 | // We lower the number of conditions so far considered if we remove one. |
|---|
| 1298 | if(should_remove) |
|---|
| 1299 | { |
|---|
| 1300 | condition_order--; |
|---|
| 1301 | } |
|---|
| 1302 | |
|---|
| 1303 | // We raise the number of conditions so far considered if we add one. |
|---|
| 1304 | if(should_add) |
|---|
| 1305 | { |
|---|
| 1306 | condition_order++; |
|---|
| 1307 | } |
|---|
| 1308 | |
|---|
| 1309 | // We erase the support lists used in splitting/merging operations later on to keep track of the |
|---|
| 1310 | // split/merged polyhedrons. |
|---|
| 1311 | for_splitting.clear(); |
|---|
| 1312 | for_merging.clear(); |
|---|
| 1313 | |
|---|
| 1314 | // This is a somewhat stupid operation, where we fill the vector of lists by empty lists, so that |
|---|
| 1315 | // we can extend the lists contained in the vector later on. |
|---|
| 1316 | for(int i = 0;i<statistic.size();i++) |
|---|
| 1317 | { |
|---|
| 1318 | list<polyhedron*> empty_split; |
|---|
| 1319 | list<polyhedron*> empty_merge; |
|---|
| 1320 | |
|---|
| 1321 | for_splitting.push_back(empty_split); |
|---|
| 1322 | for_merging.push_back(empty_merge); |
|---|
| 1323 | } |
|---|
| 1324 | |
|---|
| 1325 | // We set`the iterator in the conditions list to a blind end() iterator |
|---|
| 1326 | list<condition*>::iterator toremove_ref = conditions.end(); |
|---|
| 1327 | |
|---|
| 1328 | // We search the list of conditions for existence of toremove and toadd conditions and check their |
|---|
| 1329 | // possible multiplicity. |
|---|
| 1330 | for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++) |
|---|
| 1331 | { |
|---|
| 1332 | // If condition should be removed.. |
|---|
| 1333 | if(should_remove) |
|---|
| 1334 | { |
|---|
| 1335 | // if it exists in the list |
|---|
| 1336 | if((*ref)->value == toremove) |
|---|
| 1337 | { |
|---|
| 1338 | // if it has multiplicity higher than 1 |
|---|
| 1339 | if((*ref)->multiplicity>1) |
|---|
| 1340 | { |
|---|
| 1341 | // we just lower the multiplicity |
|---|
| 1342 | (*ref)->multiplicity--; |
|---|
| 1343 | |
|---|
| 1344 | // In this case the parameter space remains unchanged (we have to process no merging), |
|---|
| 1345 | // so we only alter the condition sums in all the cells and compute the integrals |
|---|
| 1346 | // over the cells with the subtracted condition |
|---|
| 1347 | alter_toprow_conditions(*ref,false); |
|---|
| 1348 | |
|---|
| 1349 | // By altering the condition sums in each individual unchanged cell, we have finished |
|---|
| 1350 | // all the tasks of this method related to merging and removing given condition. Therefore |
|---|
| 1351 | // we switch the should_remove switch to false. |
|---|
| 1352 | should_remove = false; |
|---|
| 1353 | } |
|---|
| 1354 | else |
|---|
| 1355 | { |
|---|
| 1356 | // In case the condition to be removed has a multiplicity of 1, we mark its position in |
|---|
| 1357 | // the vector of conditions by assigning its iterator to toremove_ref variable. |
|---|
| 1358 | toremove_ref = ref; |
|---|
| 1359 | } |
|---|
| 1360 | } |
|---|
| 1361 | } |
|---|
| 1362 | |
|---|
| 1363 | // If a condition should be added.. |
|---|
| 1364 | if(should_add) |
|---|
| 1365 | { |
|---|
| 1366 | // We search the vector of conditions if a condition with the same value already exists. |
|---|
| 1367 | if((*ref)->value == toadd) |
|---|
| 1368 | { |
|---|
| 1369 | // If it does, there will be no further splitting necessary. We have to raise its multiplicity.. |
|---|
| 1370 | (*ref)->multiplicity++; |
|---|
| 1371 | |
|---|
| 1372 | // Again as with the condition to be removed, if no splitting is performed, we only have to |
|---|
| 1373 | // perform the computations in the individual cells in the top row of Hasse diagram of the |
|---|
| 1374 | // complex of polyhedrons by changing the condition sums in individual cells and computing |
|---|
| 1375 | // integrals with changed condition sum. |
|---|
| 1376 | alter_toprow_conditions(*ref,true); |
|---|
| 1377 | |
|---|
| 1378 | // We switch off any further operations on the complex by switching the should_add variable |
|---|
| 1379 | // to false. |
|---|
| 1380 | should_add = false; |
|---|
| 1381 | } |
|---|
| 1382 | } |
|---|
| 1383 | } |
|---|
| 1384 | |
|---|
| 1385 | // Here we erase the removed condition from the conditions vector and assign a pointer to the |
|---|
| 1386 | // condition object of the removed condition, if there is such, else the pointer remains NULL. |
|---|
| 1387 | condition* condition_to_remove = NULL; |
|---|
| 1388 | if(should_remove) |
|---|
| 1389 | { |
|---|
| 1390 | if(toremove_ref!=conditions.end()) |
|---|
| 1391 | { |
|---|
| 1392 | condition_to_remove = *toremove_ref; |
|---|
| 1393 | conditions.erase(toremove_ref); |
|---|
| 1394 | } |
|---|
| 1395 | } |
|---|
| 1396 | |
|---|
| 1397 | // Here we create the condition object for a condition value to be added and we insert it in |
|---|
| 1398 | // the list of conditions in case new condition should be added, else the pointer is set to NULL. |
|---|
| 1399 | condition* condition_to_add = NULL; |
|---|
| 1400 | if(should_add) |
|---|
| 1401 | { |
|---|
| 1402 | condition_to_add = new condition(toadd); |
|---|
| 1403 | conditions.push_back(condition_to_add); |
|---|
| 1404 | } |
|---|
| 1405 | |
|---|
| 1406 | //********************************************************************************************** |
|---|
| 1407 | // Classification of points related to added and removed conditions |
|---|
| 1408 | //********************************************************************************************** |
|---|
| 1409 | // Here the preliminary and preparation part ends and we begin classifying individual vertices in |
|---|
| 1410 | // the bottom row of the representing Hasse diagram relative to the condition to be removed and the |
|---|
| 1411 | // one to be added. This classification proceeds further in a recursive manner. Each classified |
|---|
| 1412 | // polyhedron sends an information about its classification to its parent, when all the children of |
|---|
| 1413 | // given parents are classified, the parent can be itself classified and send information further to |
|---|
| 1414 | // its parent and so on. |
|---|
| 1415 | |
|---|
| 1416 | // We loop through all ther vertices |
|---|
| 1417 | for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly) |
|---|
| 1418 | { |
|---|
| 1419 | // Cast from general polyhedron to a vertex |
|---|
| 1420 | vertex* current_vertex = (vertex*)horizontal_position; |
|---|
| 1421 | |
|---|
| 1422 | // If a condition should be added or removed.. |
|---|
| 1423 | if(should_add||should_remove) |
|---|
| 1424 | { |
|---|
| 1425 | // The coordinates are extended by a -1 representing there is no parameter multiplying the |
|---|
| 1426 | // regressor in the autoregressive model. The condition is passed to the method as a vector |
|---|
| 1427 | // (y_t,psi_{t-1}), where y_t is the value of regressor and psi_t is the vector of regressands. |
|---|
| 1428 | // Minus sign is needed, because the AR model equation reads y_t = theta*psi_{t-1}+e_t, which |
|---|
| 1429 | // can be rewriten as (y_t, psi_{t-1})*(-1,theta)', where ' stands for transposition and * for |
|---|
| 1430 | // scalar product |
|---|
| 1431 | vec appended_coords = current_vertex->get_coordinates(); |
|---|
| 1432 | appended_coords.ins(0,-1.0); |
|---|
| 1433 | |
|---|
| 1434 | if(should_add) |
|---|
| 1435 | { |
|---|
| 1436 | // We compute the position of the vertex relative to the added condition |
|---|
| 1437 | double local_condition = appended_coords*toadd;// = toadd*(appended_coords.first/=appended_coords.second); |
|---|
| 1438 | |
|---|
| 1439 | // The method set_state classifies the SPLIT state of the vertex as positive, negative or |
|---|
| 1440 | // neutral |
|---|
| 1441 | current_vertex->set_state(local_condition,SPLIT); |
|---|
| 1442 | |
|---|
| 1443 | /// \TODO There should be a rounding error tolerance used here to insure we are not having too many points because of rounding error. |
|---|
| 1444 | // If the vertex lays on the hyperplane related to the condition cutting the location parameter |
|---|
| 1445 | // space in half, we say it is totally neutral. This way it will be different than the later |
|---|
| 1446 | // newly created vertices appearing on the cuts of line segments. In an environment, where |
|---|
| 1447 | // the data variables are continuous (they don't have positive probability mass at any point |
|---|
| 1448 | // in the data space) the occurence of a point on the cutting hyperplane has probability 0. |
|---|
| 1449 | // In real world application, where data are often discrete, we have to take such situation |
|---|
| 1450 | // into account. |
|---|
| 1451 | if(local_condition == 0) |
|---|
| 1452 | { |
|---|
| 1453 | // In certain scenarios this situation is rather rare. We might then want to know about |
|---|
| 1454 | // occurence of a point laying on the cutting hyperplane (Programmers note:Also such |
|---|
| 1455 | // scenarios were not so well tested and computation errors may occur!) |
|---|
| 1456 | //cout << "Condition to add: " << toadd << endl; |
|---|
| 1457 | cout << "Vertex coords: " << appended_coords << endl; |
|---|
| 1458 | |
|---|
| 1459 | // We classify the vertex totally neutral |
|---|
| 1460 | current_vertex->totally_neutral = true; |
|---|
| 1461 | |
|---|
| 1462 | // We raise its multiplicity and set current splitting condition as a parent condition |
|---|
| 1463 | // of the vertex, since if we later remove the original parent condition, the vertex |
|---|
| 1464 | // has to have a parent condition its right to exist. |
|---|
| 1465 | current_vertex->raise_multiplicity(); |
|---|
| 1466 | current_vertex->parentconditions.insert(condition_to_add); |
|---|
| 1467 | } |
|---|
| 1468 | else |
|---|
| 1469 | { |
|---|
| 1470 | // If the vertex lays off the cutting hyperplane, we set its totally_neutral property |
|---|
| 1471 | // to false. |
|---|
| 1472 | current_vertex->totally_neutral = false; |
|---|
| 1473 | } |
|---|
| 1474 | } |
|---|
| 1475 | |
|---|
| 1476 | // Now we classify the vertex with respect to the MERGEing condition.. |
|---|
| 1477 | if(should_remove) |
|---|
| 1478 | { |
|---|
| 1479 | // We search the condition to be removed in the list of vertice's parent conditions |
|---|
| 1480 | set<condition*>::iterator cond_ref; |
|---|
| 1481 | for(cond_ref = current_vertex->parentconditions.begin();cond_ref!=current_vertex->parentconditions.end();cond_ref++) |
|---|
| 1482 | { |
|---|
| 1483 | if(*cond_ref == condition_to_remove) |
|---|
| 1484 | { |
|---|
| 1485 | break; |
|---|
| 1486 | } |
|---|
| 1487 | } |
|---|
| 1488 | |
|---|
| 1489 | // If the list of parent conditions of the given vertex contain the condition that is being |
|---|
| 1490 | // removed, we erase it from the list, we set the vertice's MERGE state to neutral and we |
|---|
| 1491 | // insert the vertex into the set of polyhedrons that are supposed to be used for merging |
|---|
| 1492 | // (themselves possibly being deleted). |
|---|
| 1493 | |
|---|
| 1494 | // REMARK: One may think it would be easier to check the condition again computationally. |
|---|
| 1495 | // Such design has been used before in the software, but due to rounding errors it was |
|---|
| 1496 | // very unreliable. These rounding errors are avoided using current design. |
|---|
| 1497 | if(cond_ref!=current_vertex->parentconditions.end()) |
|---|
| 1498 | { |
|---|
| 1499 | current_vertex->parentconditions.erase(cond_ref); |
|---|
| 1500 | current_vertex->set_state(0,MERGE); |
|---|
| 1501 | for_merging[0].push_back(current_vertex); |
|---|
| 1502 | } |
|---|
| 1503 | else |
|---|
| 1504 | { |
|---|
| 1505 | // If parent conditions of the vertex don't contain the condition to be removed, we |
|---|
| 1506 | // check in which halfspace it is located and set its MERGE state accordingly. |
|---|
| 1507 | double local_condition = toremove*appended_coords; |
|---|
| 1508 | current_vertex->set_state(local_condition,MERGE); |
|---|
| 1509 | } |
|---|
| 1510 | } |
|---|
| 1511 | } |
|---|
| 1512 | |
|---|
| 1513 | // Once classified we proceed recursively by calling the send_state_message method |
|---|
| 1514 | send_state_message(current_vertex, condition_to_add, condition_to_remove, 0); |
|---|
| 1515 | |
|---|
| 1516 | } |
|---|
| 1517 | |
|---|
| 1518 | // step_me(1); |
|---|
| 1519 | |
|---|
| 1520 | if(should_remove) |
|---|
| 1521 | { |
|---|
| 1522 | /* |
|---|
| 1523 | for(int i = 0;i<for_merging.size();i++) |
|---|
| 1524 | { |
|---|
| 1525 | for(list<polyhedron*>::iterator merge_ref = for_merging[i].begin();merge_ref!=for_merging[i].end();merge_ref++) |
|---|
| 1526 | { |
|---|
| 1527 | |
|---|
| 1528 | for(list<polyhedron*>::iterator par_ref = (*merge_ref)->children.begin();par_ref!=(*merge_ref)->children.end();par_ref++) |
|---|
| 1529 | { |
|---|
| 1530 | if(find((*par_ref)->parents.begin(),(*par_ref)->parents.end(),(*merge_ref))==(*par_ref)->parents.end()) |
|---|
| 1531 | { |
|---|
| 1532 | cout << "Parent/child relations are not matched!" << endl; |
|---|
| 1533 | } |
|---|
| 1534 | } |
|---|
| 1535 | |
|---|
| 1536 | //cout << (*merge_ref)->get_state(MERGE) << ","; |
|---|
| 1537 | } |
|---|
| 1538 | |
|---|
| 1539 | // cout << endl; |
|---|
| 1540 | } |
|---|
| 1541 | */ |
|---|
| 1542 | |
|---|
| 1543 | |
|---|
| 1544 | // Here we have finished the classification part and we have at hand two sets of polyhedrons used for |
|---|
| 1545 | // further operation on the location parameter space. The first operation will be merging of polyhedrons |
|---|
| 1546 | // with respect to the MERGE condition. For that purpose, we have a set of mergers in a list called |
|---|
| 1547 | // for_merging. After we are finished merging, we need to split the polyhedrons cut by the SPLIT |
|---|
| 1548 | // condition. These polyhedrons are gathered in the for_splitting list. As can be seen, the MERGE |
|---|
| 1549 | // operation is done from below, in the terms of the Hasse diagram and therefore we start to merge |
|---|
| 1550 | // from the very bottom row, from the vertices. On the other hand splitting is done from the top |
|---|
| 1551 | // and we therefore start with the segments that need to be split. |
|---|
| 1552 | |
|---|
| 1553 | // We start the MERGE operation here. Some of the vertices will disappear from the Hasse diagram. |
|---|
| 1554 | // Because they are part of polyhedrons in the Hasse diagram above the segments, we need to remember |
|---|
| 1555 | // them in the separate set and get rid of them only after the process of merging all the polyhedrons |
|---|
| 1556 | // has been finished. |
|---|
| 1557 | cout << "Merging." << endl; |
|---|
| 1558 | set<vertex*> vertices_to_be_reduced; |
|---|
| 1559 | |
|---|
| 1560 | // We loop through the vector list of polyhedrons for merging from the bottom row up. We keep track |
|---|
| 1561 | // of the number of the processed row. |
|---|
| 1562 | int k = 1; |
|---|
| 1563 | for(vector<list<polyhedron*>>::iterator vert_ref = for_merging.begin();vert_ref<for_merging.end();vert_ref++) |
|---|
| 1564 | { |
|---|
| 1565 | // Within a row we loop through all the polyhedrons that we use as mergers. |
|---|
| 1566 | for(list<polyhedron*>::iterator merge_ref = (*vert_ref).begin();merge_ref!=(*vert_ref).end();merge_ref++) |
|---|
| 1567 | { |
|---|
| 1568 | // *************************************************** |
|---|
| 1569 | // First we treat the case of a multiple merger. |
|---|
| 1570 | // *************************************************** |
|---|
| 1571 | |
|---|
| 1572 | // If the multiplicity of the merger is greater than one, the merger will remain in the Hasse |
|---|
| 1573 | // diagram and its parents will remain split. |
|---|
| 1574 | if((*merge_ref)->get_multiplicity()>1) |
|---|
| 1575 | { |
|---|
| 1576 | // We remove the condition to be removed (the MERGE condition) from the list of merger's |
|---|
| 1577 | // parents. |
|---|
| 1578 | (*merge_ref)->parentconditions.erase(condition_to_remove); |
|---|
| 1579 | |
|---|
| 1580 | (*merge_ref)->negativeparent = NULL; |
|---|
| 1581 | (*merge_ref)->positiveparent = NULL; |
|---|
| 1582 | |
|---|
| 1583 | // If the merger will not be split and it is not totally neutral with respect to SPLIT |
|---|
| 1584 | // condition (it doesn't lay in the hyperplane defined by the condition), we will not |
|---|
| 1585 | // need it for splitting purposes and we can therefore clean all the splitting related |
|---|
| 1586 | // properties, to be able to reuse them when new data arrive. A merger is never a toprow |
|---|
| 1587 | // so we do not need to integrate. |
|---|
| 1588 | if((*merge_ref)->get_state(SPLIT)!=0||(*merge_ref)->totally_neutral) |
|---|
| 1589 | { |
|---|
| 1590 | (*merge_ref)->positivechildren.clear(); |
|---|
| 1591 | (*merge_ref)->negativechildren.clear(); |
|---|
| 1592 | (*merge_ref)->neutralchildren.clear(); |
|---|
| 1593 | (*merge_ref)->totallyneutralgrandchildren.clear(); |
|---|
| 1594 | (*merge_ref)->positiveneutralvertices.clear(); |
|---|
| 1595 | (*merge_ref)->negativeneutralvertices.clear(); |
|---|
| 1596 | (*merge_ref)->totally_neutral = NULL; |
|---|
| 1597 | (*merge_ref)->kids_rel_addresses.clear(); |
|---|
| 1598 | } |
|---|
| 1599 | } |
|---|
| 1600 | // Else, if the multiplicity of the merger is equal to 1, we proceed with the merging part of |
|---|
| 1601 | // the algorithm. |
|---|
| 1602 | else |
|---|
| 1603 | { |
|---|
| 1604 | // A boolean that will be true, if after being merged, the new polyhedron should be split |
|---|
| 1605 | // in the next step of the algorithm. |
|---|
| 1606 | bool will_be_split = false; |
|---|
| 1607 | |
|---|
| 1608 | // The newly created polyhedron will be merged of a negative and positive part specified |
|---|
| 1609 | // by its merger. |
|---|
| 1610 | toprow* current_positive = (toprow*)(*merge_ref)->positiveparent; |
|---|
| 1611 | toprow* current_negative = (toprow*)(*merge_ref)->negativeparent; |
|---|
| 1612 | |
|---|
| 1613 | // An error check for situation that should not occur. |
|---|
| 1614 | if(current_positive->totally_neutral!=current_negative->totally_neutral) |
|---|
| 1615 | { |
|---|
| 1616 | throw new exception("Both polyhedrons must be totally neutral if they should be merged!"); |
|---|
| 1617 | } |
|---|
| 1618 | |
|---|
| 1619 | // ************************************************************************************* |
|---|
| 1620 | // Now we rewire the Hasse properties of the MERGE negative part of the merged |
|---|
| 1621 | // polyhedron to the MERGE positive part - it will be used as the merged polyhedron |
|---|
| 1622 | // ************************************************************************************* |
|---|
| 1623 | |
|---|
| 1624 | // Instead of establishing a new polyhedron and filling in all the necessary connections |
|---|
| 1625 | // and thus adding it into the Hasse diagram, we use the positive polyhedron with its |
|---|
| 1626 | // connections and we merge it with all the connections from the negative side so that |
|---|
| 1627 | // the positive polyhedron becomes the merged one. |
|---|
| 1628 | |
|---|
| 1629 | // We remove the MERGE condition from parent conditions. |
|---|
| 1630 | current_positive->parentconditions.erase(condition_to_remove); |
|---|
| 1631 | |
|---|
| 1632 | // We add the children from the negative part into the children list and remove from it the |
|---|
| 1633 | // merger. |
|---|
| 1634 | current_positive->children.insert(current_positive->children.end(),current_negative->children.begin(),current_negative->children.end()); |
|---|
| 1635 | current_positive->children.remove(*merge_ref); |
|---|
| 1636 | |
|---|
| 1637 | // We reconnect the reciprocal addresses from children to parents. |
|---|
| 1638 | for(list<polyhedron*>::iterator child_ref = current_negative->children.begin();child_ref!=current_negative->children.end();child_ref++) |
|---|
| 1639 | { |
|---|
| 1640 | (*child_ref)->parents.remove(current_negative); |
|---|
| 1641 | (*child_ref)->parents.push_back(current_positive); |
|---|
| 1642 | } |
|---|
| 1643 | |
|---|
| 1644 | // We loop through the parents of the negative polyhedron. |
|---|
| 1645 | for(list<polyhedron*>::iterator parent_ref = current_negative->parents.begin();parent_ref!=current_negative->parents.end();parent_ref++) |
|---|
| 1646 | { |
|---|
| 1647 | // Remove the negative polyhedron from its children |
|---|
| 1648 | (*parent_ref)->children.remove(current_negative); |
|---|
| 1649 | |
|---|
| 1650 | // Remove it from the according list with respect to the negative polyhedron's |
|---|
| 1651 | // SPLIT state. |
|---|
| 1652 | switch(current_negative->get_state(SPLIT)) |
|---|
| 1653 | { |
|---|
| 1654 | case -1: |
|---|
| 1655 | (*parent_ref)->negativechildren.remove(current_negative); |
|---|
| 1656 | break; |
|---|
| 1657 | case 0: |
|---|
| 1658 | (*parent_ref)->neutralchildren.remove(current_negative); |
|---|
| 1659 | break; |
|---|
| 1660 | case 1: |
|---|
| 1661 | (*parent_ref)->positivechildren.remove(current_negative); |
|---|
| 1662 | break; |
|---|
| 1663 | } |
|---|
| 1664 | } |
|---|
| 1665 | |
|---|
| 1666 | // We merge the vertices of the negative and positive part |
|---|
| 1667 | current_positive->vertices.insert(current_negative->vertices.begin(),current_negative->vertices.end()); |
|---|
| 1668 | |
|---|
| 1669 | // ************************************************************************** |
|---|
| 1670 | // Now we treat the situation that one of the MERGEd polyhedrons is to be |
|---|
| 1671 | // SPLIT. |
|---|
| 1672 | // ************************************************************************** |
|---|
| 1673 | |
|---|
| 1674 | if(current_negative->get_state(SPLIT)==0) |
|---|
| 1675 | { |
|---|
| 1676 | for_splitting[k].remove(current_negative); |
|---|
| 1677 | } |
|---|
| 1678 | |
|---|
| 1679 | if(!current_positive->totally_neutral) |
|---|
| 1680 | { |
|---|
| 1681 | // If the positive polyhedron was not to be SPLIT and the negative polyhedron was.. |
|---|
| 1682 | if(current_positive->get_state(SPLIT)!=0&¤t_negative->get_state(SPLIT)==0) |
|---|
| 1683 | { |
|---|
| 1684 | //..we loop through the parents of the positive polyhedron.. |
|---|
| 1685 | for(list<polyhedron*>::iterator parent_ref = current_positive->parents.begin();parent_ref!=current_positive->parents.end();parent_ref++) |
|---|
| 1686 | { |
|---|
| 1687 | //..and if the MERGE positive polyhedron is SPLIT positive, we remove it |
|---|
| 1688 | //from the list of SPLIT positive children.. |
|---|
| 1689 | if(current_positive->get_state(SPLIT)==1) |
|---|
| 1690 | { |
|---|
| 1691 | (*parent_ref)->positivechildren.remove(current_positive); |
|---|
| 1692 | } |
|---|
| 1693 | //..or if the MERGE positive polyhedron is SPLIT negative, we remove it |
|---|
| 1694 | //from the list of SPLIT positive children.. |
|---|
| 1695 | else |
|---|
| 1696 | { |
|---|
| 1697 | (*parent_ref)->negativechildren.remove(current_positive); |
|---|
| 1698 | } |
|---|
| 1699 | //..and we add it to the SPLIT neutral children, because the MERGE negative polyhedron |
|---|
| 1700 | //that is being MERGEd with it causes it to be SPLIT neutral (the hyperplane runs |
|---|
| 1701 | //through the merged polyhedron) |
|---|
| 1702 | (*parent_ref)->neutralchildren.push_back(current_positive); |
|---|
| 1703 | } |
|---|
| 1704 | |
|---|
| 1705 | // Because of the above mentioned reason, we set the SPLIT state of the MERGE positive |
|---|
| 1706 | // polyhedron to neutral |
|---|
| 1707 | current_positive->set_state(0,SPLIT); |
|---|
| 1708 | |
|---|
| 1709 | // and we add it to the list of polyhedrons to be SPLIT |
|---|
| 1710 | for_splitting[k].push_back(current_positive); |
|---|
| 1711 | } |
|---|
| 1712 | |
|---|
| 1713 | |
|---|
| 1714 | // If the MERGEd polyhedron is to be split.. |
|---|
| 1715 | if(current_positive->get_state(SPLIT)==0) |
|---|
| 1716 | { |
|---|
| 1717 | // We need to fill the lists related to split with correct values, adding the SPLIT |
|---|
| 1718 | // positive, negative and neutral children to according list in the MERGE positive, |
|---|
| 1719 | // or future MERGEd polyhedron |
|---|
| 1720 | current_positive->negativechildren.insert(current_positive->negativechildren.end(),current_negative->negativechildren.begin(),current_negative->negativechildren.end()); |
|---|
| 1721 | current_positive->positivechildren.insert(current_positive->positivechildren.end(),current_negative->positivechildren.begin(),current_negative->positivechildren.end()); |
|---|
| 1722 | current_positive->neutralchildren.insert(current_positive->neutralchildren.end(),current_negative->neutralchildren.begin(),current_negative->neutralchildren.end()); |
|---|
| 1723 | |
|---|
| 1724 | // and remove the merger, which will be later deleted from the lists of SPLIT classified |
|---|
| 1725 | // children. |
|---|
| 1726 | switch((*merge_ref)->get_state(SPLIT)) |
|---|
| 1727 | { |
|---|
| 1728 | case -1: |
|---|
| 1729 | current_positive->negativechildren.remove(*merge_ref); |
|---|
| 1730 | break; |
|---|
| 1731 | case 0: |
|---|
| 1732 | current_positive->neutralchildren.remove(*merge_ref); |
|---|
| 1733 | break; |
|---|
| 1734 | case 1: |
|---|
| 1735 | current_positive->positivechildren.remove(*merge_ref); |
|---|
| 1736 | break; |
|---|
| 1737 | } |
|---|
| 1738 | |
|---|
| 1739 | // We also have to merge the lists of totally neutral children laying in the SPLIT related |
|---|
| 1740 | // cutting hyperpalne and the lists of positive+neutral and negative+neutral vertices. |
|---|
| 1741 | current_positive->totallyneutralgrandchildren.insert(current_negative->totallyneutralgrandchildren.begin(),current_negative->totallyneutralgrandchildren.end()); |
|---|
| 1742 | // Because a vertex cannot be SPLIT, we don't need to remove the merger from the |
|---|
| 1743 | // positive+neutral and negative+neutral lists |
|---|
| 1744 | current_positive->negativeneutralvertices.insert(current_negative->negativeneutralvertices.begin(),current_negative->negativeneutralvertices.end()); |
|---|
| 1745 | current_positive->positiveneutralvertices.insert(current_negative->positiveneutralvertices.begin(),current_negative->positiveneutralvertices.end()); |
|---|
| 1746 | |
|---|
| 1747 | // And we set the will be split property to true |
|---|
| 1748 | will_be_split = true; |
|---|
| 1749 | } |
|---|
| 1750 | } |
|---|
| 1751 | |
|---|
| 1752 | // If the polyhedron will not be split (both parts are totally neutral or neither of them |
|---|
| 1753 | // was classified SPLIT neutral), we clear all the lists holding the SPLIT information for |
|---|
| 1754 | // them to be ready to reuse. |
|---|
| 1755 | if(!will_be_split) |
|---|
| 1756 | { |
|---|
| 1757 | current_positive->positivechildren.clear(); |
|---|
| 1758 | current_positive->negativechildren.clear(); |
|---|
| 1759 | current_positive->neutralchildren.clear(); |
|---|
| 1760 | current_positive->totallyneutralgrandchildren.clear(); |
|---|
| 1761 | current_positive->positiveneutralvertices.clear(); |
|---|
| 1762 | current_positive->negativeneutralvertices.clear(); |
|---|
| 1763 | current_positive->totally_neutral = NULL; |
|---|
| 1764 | current_positive->kids_rel_addresses.clear(); |
|---|
| 1765 | } |
|---|
| 1766 | |
|---|
| 1767 | // If both the merged polyhedrons are totally neutral, we have to rewire the addressing |
|---|
| 1768 | // in the grandparents from the negative to the positive (merged) polyhedron. |
|---|
| 1769 | if(current_positive->totally_neutral) |
|---|
| 1770 | { |
|---|
| 1771 | for(set<polyhedron*>::iterator grand_ref = current_negative->grandparents.begin();grand_ref!=current_negative->grandparents.end();grand_ref++) |
|---|
| 1772 | { |
|---|
| 1773 | (*grand_ref)->totallyneutralgrandchildren.erase(current_negative); |
|---|
| 1774 | (*grand_ref)->totallyneutralgrandchildren.insert(current_positive); |
|---|
| 1775 | } |
|---|
| 1776 | } |
|---|
| 1777 | |
|---|
| 1778 | // We clear the grandparents list for further reuse. |
|---|
| 1779 | current_positive->grandparents.clear(); |
|---|
| 1780 | |
|---|
| 1781 | |
|---|
| 1782 | // Delete the negative polyhedron from the Hasse diagram (rewire all the connections) |
|---|
| 1783 | statistic.delete_polyhedron(k,current_negative); |
|---|
| 1784 | |
|---|
| 1785 | // Delete the negative polyhedron object |
|---|
| 1786 | delete current_negative; |
|---|
| 1787 | |
|---|
| 1788 | // ********************************************* |
|---|
| 1789 | // Here we treat the deletion of the merger. |
|---|
| 1790 | // ********************************************* |
|---|
| 1791 | |
|---|
| 1792 | // We erase the vertices of the merger from all the respective lists. |
|---|
| 1793 | for(set<vertex*>::iterator vert_ref = (*merge_ref)->vertices.begin();vert_ref!=(*merge_ref)->vertices.end();vert_ref++) |
|---|
| 1794 | { |
|---|
| 1795 | if((*vert_ref)->get_multiplicity()==1) |
|---|
| 1796 | { |
|---|
| 1797 | current_positive->vertices.erase(*vert_ref); |
|---|
| 1798 | |
|---|
| 1799 | if(will_be_split) |
|---|
| 1800 | { |
|---|
| 1801 | current_positive->negativeneutralvertices.erase(*vert_ref); |
|---|
| 1802 | current_positive->positiveneutralvertices.erase(*vert_ref); |
|---|
| 1803 | } |
|---|
| 1804 | } |
|---|
| 1805 | } |
|---|
| 1806 | |
|---|
| 1807 | // We remove the connection to the merger from the merger's children |
|---|
| 1808 | for(list<polyhedron*>::iterator child_ref = (*merge_ref)->children.begin();child_ref!=(*merge_ref)->children.end();child_ref++) |
|---|
| 1809 | { |
|---|
| 1810 | (*child_ref)->parents.remove(*merge_ref); |
|---|
| 1811 | } |
|---|
| 1812 | |
|---|
| 1813 | // We remove the connection to the merger from the merger's grandchildren |
|---|
| 1814 | for(set<polyhedron*>::iterator grand_ch_ref = (*merge_ref)->totallyneutralgrandchildren.begin();grand_ch_ref!=(*merge_ref)->totallyneutralgrandchildren.end();grand_ch_ref++) |
|---|
| 1815 | { |
|---|
| 1816 | (*grand_ch_ref)->grandparents.erase(*merge_ref); |
|---|
| 1817 | } |
|---|
| 1818 | |
|---|
| 1819 | // We remove the connection to the merger from the merger's grandparents |
|---|
| 1820 | for(set<polyhedron*>::iterator grand_p_ref = (*merge_ref)->grandparents.begin();grand_p_ref!=(*merge_ref)->grandparents.end();grand_p_ref++) |
|---|
| 1821 | { |
|---|
| 1822 | (*grand_p_ref)->totallyneutralgrandchildren.erase(*merge_ref); |
|---|
| 1823 | } |
|---|
| 1824 | |
|---|
| 1825 | // We remove the merger from the Hasse diagram |
|---|
| 1826 | statistic.delete_polyhedron(k-1,*merge_ref); |
|---|
| 1827 | // And we delete the merger from the list of polyhedrons to be split |
|---|
| 1828 | for_splitting[k-1].remove(*merge_ref); |
|---|
| 1829 | // If the merger is a vertex with multiplicity 1, we add it to the list of vertices to get |
|---|
| 1830 | // rid of at the end of the merging procedure. |
|---|
| 1831 | if(k==1) |
|---|
| 1832 | { |
|---|
| 1833 | vertices_to_be_reduced.insert((vertex*)(*merge_ref)); |
|---|
| 1834 | } |
|---|
| 1835 | |
|---|
| 1836 | // Triangulate the newly created polyhedron and compute its normalization integral if the |
|---|
| 1837 | // polyhedron is a toprow. |
|---|
| 1838 | normalization_factor += current_positive->triangulate(k==for_splitting.size()-1 && !will_be_split); |
|---|
| 1839 | } |
|---|
| 1840 | |
|---|
| 1841 | // If the merger is a vertex.. |
|---|
| 1842 | if(k==1) |
|---|
| 1843 | { |
|---|
| 1844 | // ..we will later reduce its multiplicity (this is to prevent multiple reduction of |
|---|
| 1845 | // the same vertex) |
|---|
| 1846 | vertices_to_be_reduced.insert((vertex*)(*merge_ref)); |
|---|
| 1847 | } |
|---|
| 1848 | // If the merger is not a vertex.. |
|---|
| 1849 | else |
|---|
| 1850 | { |
|---|
| 1851 | // lower the multiplicity of the merger |
|---|
| 1852 | (*merge_ref)->lower_multiplicity(); |
|---|
| 1853 | } |
|---|
| 1854 | } |
|---|
| 1855 | |
|---|
| 1856 | // And we go to the next row |
|---|
| 1857 | k++; |
|---|
| 1858 | |
|---|
| 1859 | } |
|---|
| 1860 | |
|---|
| 1861 | // At the end of the merging procedure, we delete all the merger's objects. These should now be already |
|---|
| 1862 | // disconnected from the Hasse diagram. |
|---|
| 1863 | for(int i = 1;i<for_merging.size();i++) |
|---|
| 1864 | { |
|---|
| 1865 | for(list<polyhedron*>::iterator merge_ref = for_merging[i].begin();merge_ref!=for_merging[i].end();merge_ref++) |
|---|
| 1866 | { |
|---|
| 1867 | if((*merge_ref)->get_multiplicity()==0) |
|---|
| 1868 | { |
|---|
| 1869 | delete (*merge_ref); |
|---|
| 1870 | } |
|---|
| 1871 | } |
|---|
| 1872 | } |
|---|
| 1873 | |
|---|
| 1874 | // We also treat the vertices that we called to be reduced by either lowering their multiplicity or |
|---|
| 1875 | // deleting them in case the already have multiplicity 1. |
|---|
| 1876 | for(set<vertex*>::iterator vert_ref = vertices_to_be_reduced.begin();vert_ref!=vertices_to_be_reduced.end();vert_ref++) |
|---|
| 1877 | { |
|---|
| 1878 | if((*vert_ref)->get_multiplicity()>1) |
|---|
| 1879 | { |
|---|
| 1880 | (*vert_ref)->lower_multiplicity(); |
|---|
| 1881 | } |
|---|
| 1882 | else |
|---|
| 1883 | { |
|---|
| 1884 | delete (*vert_ref); |
|---|
| 1885 | } |
|---|
| 1886 | } |
|---|
| 1887 | |
|---|
| 1888 | // Finally we delete the condition object |
|---|
| 1889 | delete condition_to_remove; |
|---|
| 1890 | } |
|---|
| 1891 | |
|---|
| 1892 | //step_me(3); |
|---|
| 1893 | // This is a control check for errors in the merging procedure. |
|---|
| 1894 | /* |
|---|
| 1895 | vector<int> sizevector; |
|---|
| 1896 | for(int s = 0;s<statistic.size();s++) |
|---|
| 1897 | { |
|---|
| 1898 | sizevector.push_back(statistic.row_size(s)); |
|---|
| 1899 | cout << statistic.row_size(s) << ", "; |
|---|
| 1900 | } |
|---|
| 1901 | cout << endl; |
|---|
| 1902 | */ |
|---|
| 1903 | |
|---|
| 1904 | // After the merging is finished or if there is no condition to be removed from the conditions list, |
|---|
| 1905 | // we split the location parameter space with respect to the condition to be added or SPLIT condition. |
|---|
| 1906 | if(should_add) |
|---|
| 1907 | { |
|---|
| 1908 | cout << "Splitting." << endl; |
|---|
| 1909 | |
|---|
| 1910 | // We reset the row counter |
|---|
| 1911 | int k = 1; |
|---|
| 1912 | |
|---|
| 1913 | // Since the bottom row of the for_splitting list is empty - we can't split vertices, we start from |
|---|
| 1914 | // the second row from the bottom - the row containing segments |
|---|
| 1915 | vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin(); |
|---|
| 1916 | |
|---|
| 1917 | // We loop through the rows |
|---|
| 1918 | for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++) |
|---|
| 1919 | { |
|---|
| 1920 | |
|---|
| 1921 | // and we loop through the polyhedrons in each row |
|---|
| 1922 | for(list<polyhedron*>::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++) |
|---|
| 1923 | { |
|---|
| 1924 | // If we split a polyhedron by a SPLIT condition hyperplane, in the crossection of the two a |
|---|
| 1925 | // new polyhedron is created. It is totally neutral, because it lays in the condition hyperplane. |
|---|
| 1926 | polyhedron* new_totally_neutral_child; |
|---|
| 1927 | |
|---|
| 1928 | // For clear notation we rename the value referenced by split_ref iterator |
|---|
| 1929 | polyhedron* current_polyhedron = (*split_ref); |
|---|
| 1930 | |
|---|
| 1931 | // If the current polyhedron is a segment, the new totally neutral child will be a vertex and |
|---|
| 1932 | // we have to assign coordinates to it. |
|---|
| 1933 | if(vert_ref == beginning_ref) |
|---|
| 1934 | { |
|---|
| 1935 | // The coordinates will be computed from the equation of the straight line containing the |
|---|
| 1936 | // segment, obtained from the coordinates of the endpoints of the segment |
|---|
| 1937 | vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates(); |
|---|
| 1938 | vec coordinates2 = ((vertex*)(*(++current_polyhedron->children.begin())))->get_coordinates(); |
|---|
| 1939 | |
|---|
| 1940 | // For computation of the scalar product with the SPLIT condition, we need extended coordinates |
|---|
| 1941 | vec extended_coord2 = coordinates2; |
|---|
| 1942 | extended_coord2.ins(0,-1.0); |
|---|
| 1943 | |
|---|
| 1944 | // We compute the parameter t an element of (0,1) describing where the segment is cut |
|---|
| 1945 | double t = (-toadd*extended_coord2)/(toadd(1,toadd.size()-1)*(coordinates1-coordinates2)); |
|---|
| 1946 | |
|---|
| 1947 | // And compute the coordinates as convex sum of the coordinates |
|---|
| 1948 | vec new_coordinates = (1-t)*coordinates2+t*coordinates1; |
|---|
| 1949 | |
|---|
| 1950 | // cout << "c1:" << coordinates1 << endl << "c2:" << coordinates2 << endl << "nc:" << new_coordinates << endl; |
|---|
| 1951 | |
|---|
| 1952 | // We create a new vertex object |
|---|
| 1953 | vertex* neutral_vertex = new vertex(new_coordinates); |
|---|
| 1954 | |
|---|
| 1955 | // and assign it to the new totally neutral child |
|---|
| 1956 | new_totally_neutral_child = neutral_vertex; |
|---|
| 1957 | } |
|---|
| 1958 | else |
|---|
| 1959 | { |
|---|
| 1960 | // If the split polyhedron isn't a segment, the totally neutral child will be a general |
|---|
| 1961 | // polyhedron. Because a toprow inherits from polyhedron, we make it a toprow for further |
|---|
| 1962 | // universality \TODO: is this really needed? |
|---|
| 1963 | toprow* neutral_toprow = new toprow(); |
|---|
| 1964 | |
|---|
| 1965 | if(k==number_of_parameters) |
|---|
| 1966 | { |
|---|
| 1967 | // A toprow needs a valid condition |
|---|
| 1968 | neutral_toprow->condition_sum = ((toprow*)current_polyhedron)->condition_sum; // tohle tu bylo driv: zeros(number_of_parameters+1); |
|---|
| 1969 | neutral_toprow->condition_order = ((toprow*)current_polyhedron)->condition_order+1; |
|---|
| 1970 | } |
|---|
| 1971 | |
|---|
| 1972 | // We assign it to the totally neutral child variable |
|---|
| 1973 | new_totally_neutral_child = neutral_toprow; |
|---|
| 1974 | } |
|---|
| 1975 | |
|---|
| 1976 | // We assign current SPLIT condition as a parent condition of the totally neutral child and also |
|---|
| 1977 | // the child inherits all the parent conditions of the split polyhedron |
|---|
| 1978 | new_totally_neutral_child->parentconditions.insert(current_polyhedron->parentconditions.begin(),current_polyhedron->parentconditions.end()); |
|---|
| 1979 | new_totally_neutral_child->parentconditions.insert(condition_to_add); |
|---|
| 1980 | |
|---|
| 1981 | // The totally neutral child is a polyhedron belonging to my_emlig distribution |
|---|
| 1982 | new_totally_neutral_child->my_emlig = this; |
|---|
| 1983 | |
|---|
| 1984 | // We connect the totally neutral child to all totally neutral grandchildren of the polyhedron |
|---|
| 1985 | // being split. This is what we need the totally neutral grandchildren for. It complicates the |
|---|
| 1986 | // algorithm, because it is a second level dependence (opposed to the children <-> parents |
|---|
| 1987 | // relations, but it is needed.) |
|---|
| 1988 | new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(), |
|---|
| 1989 | current_polyhedron->totallyneutralgrandchildren.begin(), |
|---|
| 1990 | current_polyhedron->totallyneutralgrandchildren.end()); |
|---|
| 1991 | |
|---|
| 1992 | // We also create the reciprocal connection from the totally neutral grandchildren to the |
|---|
| 1993 | // new totally neutral child and add all the vertices of the totally neutral grandchildren |
|---|
| 1994 | // to the set of vertices of the new totally neutral child. |
|---|
| 1995 | for(set<polyhedron*>::iterator grand_ref = current_polyhedron->totallyneutralgrandchildren.begin(); grand_ref != current_polyhedron->totallyneutralgrandchildren.end();grand_ref++) |
|---|
| 1996 | { |
|---|
| 1997 | // parent connection |
|---|
| 1998 | (*grand_ref)->parents.push_back(new_totally_neutral_child); |
|---|
| 1999 | |
|---|
| 2000 | // vertices |
|---|
| 2001 | new_totally_neutral_child->vertices.insert((*grand_ref)->vertices.begin(),(*grand_ref)->vertices.end()); |
|---|
| 2002 | } |
|---|
| 2003 | |
|---|
| 2004 | // We create a condition sum for the split parts of the split polyhedron |
|---|
| 2005 | vec cur_pos_condition = ((toprow*)current_polyhedron)->condition_sum; |
|---|
| 2006 | vec cur_neg_condition = ((toprow*)current_polyhedron)->condition_sum; |
|---|
| 2007 | |
|---|
| 2008 | // If the split polyhedron is a toprow, we update the condition sum with the use of the SPLIT |
|---|
| 2009 | // condition. The classification of the intermediate row polyhedrons as toprows probably isn't |
|---|
| 2010 | // necessary and it could be changed for more elegance, but it is here for historical reasons. |
|---|
| 2011 | if(k == number_of_parameters) |
|---|
| 2012 | { |
|---|
| 2013 | cur_pos_condition = cur_pos_condition + toadd; |
|---|
| 2014 | cur_neg_condition = cur_neg_condition - toadd; |
|---|
| 2015 | } |
|---|
| 2016 | |
|---|
| 2017 | // We create the positive and negative parts of the split polyhedron completely from scratch, |
|---|
| 2018 | // using the condition sum constructed earlier. This is different from the merging part, where |
|---|
| 2019 | // we have reused one of the parts to create the merged entity. This way, we don't have to |
|---|
| 2020 | // clean up old information from the split parts and the operation will be more symetrical. |
|---|
| 2021 | toprow* positive_poly = new toprow(cur_pos_condition, ((toprow*)current_polyhedron)->condition_order+1); |
|---|
| 2022 | toprow* negative_poly = new toprow(cur_neg_condition, ((toprow*)current_polyhedron)->condition_order+1); |
|---|
| 2023 | |
|---|
| 2024 | // Set the new SPLIT positive and negative parts of the split polyhedrons as parents of the new |
|---|
| 2025 | // totally neutral child |
|---|
| 2026 | new_totally_neutral_child->parents.push_back(positive_poly); |
|---|
| 2027 | new_totally_neutral_child->parents.push_back(negative_poly); |
|---|
| 2028 | |
|---|
| 2029 | // and the new totally neutral child as a child of the SPLIT positive and negative parts |
|---|
| 2030 | // of the split polyhedron |
|---|
| 2031 | positive_poly->children.push_back(new_totally_neutral_child); |
|---|
| 2032 | negative_poly->children.push_back(new_totally_neutral_child); |
|---|
| 2033 | |
|---|
| 2034 | // The new polyhedrons belong to my_emlig |
|---|
| 2035 | positive_poly->my_emlig = this; |
|---|
| 2036 | negative_poly->my_emlig = this; |
|---|
| 2037 | |
|---|
| 2038 | // Parent conditions of the new polyhedrons are the same as parent conditions of the split polyhedron |
|---|
| 2039 | positive_poly->parentconditions.insert(current_polyhedron->parentconditions.begin(),current_polyhedron->parentconditions.end()); |
|---|
| 2040 | negative_poly->parentconditions.insert(current_polyhedron->parentconditions.begin(),current_polyhedron->parentconditions.end()); |
|---|
| 2041 | |
|---|
| 2042 | positive_poly->set_multiplicity(current_polyhedron->get_multiplicity()); |
|---|
| 2043 | negative_poly->set_multiplicity(current_polyhedron->get_multiplicity()); |
|---|
| 2044 | |
|---|
| 2045 | // We loop through the parents of the split polyhedron |
|---|
| 2046 | for(list<polyhedron*>::iterator parent_ref = current_polyhedron->parents.begin();parent_ref!=current_polyhedron->parents.end();parent_ref++) |
|---|
| 2047 | { |
|---|
| 2048 | // We set the new totally neutral child to be a totally neutral grandchild of the parent |
|---|
| 2049 | (*parent_ref)->totallyneutralgrandchildren.insert(new_totally_neutral_child); |
|---|
| 2050 | |
|---|
| 2051 | // We remove the split polyhedron from both lists, where it should be present |
|---|
| 2052 | (*parent_ref)->neutralchildren.remove(current_polyhedron); |
|---|
| 2053 | (*parent_ref)->children.remove(current_polyhedron); |
|---|
| 2054 | |
|---|
| 2055 | // And instead set the newly created SPLIT negative and positive parts as children of |
|---|
| 2056 | // the parent (maybe the parent will be split once we get to treating its row, but that |
|---|
| 2057 | // should be taken care of later) and we add it to the classified positive and negative |
|---|
| 2058 | // children list accordingly. |
|---|
| 2059 | (*parent_ref)->children.push_back(positive_poly); |
|---|
| 2060 | (*parent_ref)->children.push_back(negative_poly); |
|---|
| 2061 | (*parent_ref)->positivechildren.push_back(positive_poly); |
|---|
| 2062 | (*parent_ref)->negativechildren.push_back(negative_poly); |
|---|
| 2063 | } |
|---|
| 2064 | |
|---|
| 2065 | // Here we set the reciprocal connections to the ones set in the previous list. All the parents |
|---|
| 2066 | // of currently split polyhedron are added as parents of the SPLIT negative and positive parts. |
|---|
| 2067 | |
|---|
| 2068 | // for positive part.. |
|---|
| 2069 | positive_poly->parents.insert(positive_poly->parents.end(), |
|---|
| 2070 | current_polyhedron->parents.begin(), |
|---|
| 2071 | current_polyhedron->parents.end()); |
|---|
| 2072 | // for negative part.. |
|---|
| 2073 | negative_poly->parents.insert(negative_poly->parents.end(), |
|---|
| 2074 | current_polyhedron->parents.begin(), |
|---|
| 2075 | current_polyhedron->parents.end()); |
|---|
| 2076 | |
|---|
| 2077 | // We loop through the positive children of the split polyhedron, remove it from their parents |
|---|
| 2078 | // lists and add the SPLIT positive part as their parent. |
|---|
| 2079 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->positivechildren.begin();child_ref!=current_polyhedron->positivechildren.end();child_ref++) |
|---|
| 2080 | { |
|---|
| 2081 | (*child_ref)->parents.remove(current_polyhedron); |
|---|
| 2082 | (*child_ref)->parents.push_back(positive_poly); |
|---|
| 2083 | } |
|---|
| 2084 | |
|---|
| 2085 | // And again we set the reciprocal connections from the SPLIT positive part by adding |
|---|
| 2086 | // all the positive children of the split polyhedron to its list of children. |
|---|
| 2087 | positive_poly->children.insert(positive_poly->children.end(), |
|---|
| 2088 | current_polyhedron->positivechildren.begin(), |
|---|
| 2089 | current_polyhedron->positivechildren.end()); |
|---|
| 2090 | |
|---|
| 2091 | // We loop through the negative children of the split polyhedron, remove it from their parents |
|---|
| 2092 | // lists and add the SPLIT negative part as their parent. |
|---|
| 2093 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->negativechildren.begin();child_ref!=current_polyhedron->negativechildren.end();child_ref++) |
|---|
| 2094 | { |
|---|
| 2095 | (*child_ref)->parents.remove(current_polyhedron); |
|---|
| 2096 | (*child_ref)->parents.push_back(negative_poly); |
|---|
| 2097 | } |
|---|
| 2098 | |
|---|
| 2099 | // And again we set the reciprocal connections from the SPLIT negative part by adding |
|---|
| 2100 | // all the negative children of the split polyhedron to its list of children. |
|---|
| 2101 | negative_poly->children.insert(negative_poly->children.end(), |
|---|
| 2102 | current_polyhedron->negativechildren.begin(), |
|---|
| 2103 | current_polyhedron->negativechildren.end()); |
|---|
| 2104 | |
|---|
| 2105 | // The vertices of the SPLIT positive part are the union of positive and neutral vertices of |
|---|
| 2106 | // the split polyhedron and vertices of the new neutral child |
|---|
| 2107 | positive_poly->vertices.insert(current_polyhedron->positiveneutralvertices.begin(),current_polyhedron->positiveneutralvertices.end()); |
|---|
| 2108 | positive_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
|---|
| 2109 | |
|---|
| 2110 | // The vertices of the SPLIT negative part are the union of negative and neutral vertices of |
|---|
| 2111 | // the split polyhedron and vertices of the new neutral child |
|---|
| 2112 | negative_poly->vertices.insert(current_polyhedron->negativeneutralvertices.begin(),current_polyhedron->negativeneutralvertices.end()); |
|---|
| 2113 | negative_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
|---|
| 2114 | |
|---|
| 2115 | // Triangulate the new totally neutral child without computing its normalization intergral |
|---|
| 2116 | // (because the child is never a toprow polyhedron) |
|---|
| 2117 | new_totally_neutral_child->triangulate(false); |
|---|
| 2118 | |
|---|
| 2119 | // Triangulate the new SPLIT positive and negative parts of the split polyhedron and compute |
|---|
| 2120 | // their normalization integral if they are toprow polyhedrons |
|---|
| 2121 | normalization_factor += positive_poly->triangulate(k==for_splitting.size()-1); |
|---|
| 2122 | normalization_factor += negative_poly->triangulate(k==for_splitting.size()-1); |
|---|
| 2123 | |
|---|
| 2124 | // Insert all the newly created polyhedrons into the Hasse diagram |
|---|
| 2125 | statistic.append_polyhedron(k-1, new_totally_neutral_child); |
|---|
| 2126 | statistic.insert_polyhedron(k, positive_poly, current_polyhedron); |
|---|
| 2127 | statistic.insert_polyhedron(k, negative_poly, current_polyhedron); |
|---|
| 2128 | |
|---|
| 2129 | // and delete the split polyhedron from the diagram |
|---|
| 2130 | statistic.delete_polyhedron(k, current_polyhedron); |
|---|
| 2131 | |
|---|
| 2132 | // and also delete its object from the memory |
|---|
| 2133 | delete current_polyhedron; |
|---|
| 2134 | } |
|---|
| 2135 | |
|---|
| 2136 | // Goto a higher row of the for_splitting list |
|---|
| 2137 | k++; |
|---|
| 2138 | } |
|---|
| 2139 | } |
|---|
| 2140 | |
|---|
| 2141 | /* |
|---|
| 2142 | vector<int> sizevector; |
|---|
| 2143 | //sizevector.clear(); |
|---|
| 2144 | for(int s = 0;s<statistic.size();s++) |
|---|
| 2145 | { |
|---|
| 2146 | sizevector.push_back(statistic.row_size(s)); |
|---|
| 2147 | cout << statistic.row_size(s) << ", "; |
|---|
| 2148 | } |
|---|
| 2149 | |
|---|
| 2150 | cout << endl; |
|---|
| 2151 | */ |
|---|
| 2152 | |
|---|
| 2153 | // cout << "Normalization factor: " << normalization_factor << endl; |
|---|
| 2154 | |
|---|
| 2155 | last_log_nc = log_nc; |
|---|
| 2156 | log_nc = log(normalization_factor); |
|---|
| 2157 | |
|---|
| 2158 | /* |
|---|
| 2159 | for(polyhedron* topr_ref = statistic.rows[statistic.size()-1];topr_ref!=statistic.row_ends[statistic.size()-1]->next_poly;topr_ref=topr_ref->next_poly) |
|---|
| 2160 | { |
|---|
| 2161 | cout << ((toprow*)topr_ref)->condition << endl; |
|---|
| 2162 | } |
|---|
| 2163 | */ |
|---|
| 2164 | |
|---|
| 2165 | // step_me(101); |
|---|
| 2166 | } |
|---|
| 2167 | |
|---|
| 2168 | double _ll() |
|---|
| 2169 | { |
|---|
| 2170 | if(last_log_nc!=NULL) |
|---|
| 2171 | { |
|---|
| 2172 | return log_nc - last_log_nc; |
|---|
| 2173 | } |
|---|
| 2174 | else |
|---|
| 2175 | { |
|---|
| 2176 | throw new exception("You can not ask for log likelihood difference for a prior!"); |
|---|
| 2177 | } |
|---|
| 2178 | } |
|---|
| 2179 | |
|---|
| 2180 | void set_correction_factors(int order) |
|---|
| 2181 | { |
|---|
| 2182 | for(int remaining_order = correction_factors.size();remaining_order<order;remaining_order++) |
|---|
| 2183 | { |
|---|
| 2184 | multiset<my_ivec> factor_templates; |
|---|
| 2185 | multiset<my_ivec> final_factors; |
|---|
| 2186 | |
|---|
| 2187 | my_ivec orig_template = my_ivec(); |
|---|
| 2188 | |
|---|
| 2189 | for(int i = 1;i<number_of_parameters-remaining_order+1;i++) |
|---|
| 2190 | { |
|---|
| 2191 | bool in_cycle = false; |
|---|
| 2192 | for(int j = 0;j<=remaining_order;j++) { |
|---|
| 2193 | |
|---|
| 2194 | multiset<my_ivec>::iterator fac_ref = factor_templates.begin(); |
|---|
| 2195 | |
|---|
| 2196 | do |
|---|
| 2197 | { |
|---|
| 2198 | my_ivec current_template; |
|---|
| 2199 | if(!in_cycle) |
|---|
| 2200 | { |
|---|
| 2201 | current_template = orig_template; |
|---|
| 2202 | in_cycle = true; |
|---|
| 2203 | } |
|---|
| 2204 | else |
|---|
| 2205 | { |
|---|
| 2206 | current_template = (*fac_ref); |
|---|
| 2207 | fac_ref++; |
|---|
| 2208 | } |
|---|
| 2209 | |
|---|
| 2210 | current_template.ins(current_template.size(),i); |
|---|
| 2211 | |
|---|
| 2212 | // cout << "template:" << current_template << endl; |
|---|
| 2213 | |
|---|
| 2214 | if(current_template.size()==remaining_order+1) |
|---|
| 2215 | { |
|---|
| 2216 | final_factors.insert(current_template); |
|---|
| 2217 | } |
|---|
| 2218 | else |
|---|
| 2219 | { |
|---|
| 2220 | factor_templates.insert(current_template); |
|---|
| 2221 | } |
|---|
| 2222 | } |
|---|
| 2223 | while(fac_ref!=factor_templates.end()); |
|---|
| 2224 | } |
|---|
| 2225 | } |
|---|
| 2226 | |
|---|
| 2227 | correction_factors.push_back(final_factors); |
|---|
| 2228 | |
|---|
| 2229 | } |
|---|
| 2230 | } |
|---|
| 2231 | |
|---|
| 2232 | pair<vec,simplex*> choose_simplex() |
|---|
| 2233 | { |
|---|
| 2234 | double rnumber = randu(); |
|---|
| 2235 | |
|---|
| 2236 | // cout << "RND:" << rnumber << endl; |
|---|
| 2237 | |
|---|
| 2238 | // This could be more efficient (log n), but map::upper_bound() doesn't let me dereference returned iterator |
|---|
| 2239 | double prob_sum = 0; |
|---|
| 2240 | toprow* sampled_toprow; |
|---|
| 2241 | for(polyhedron* top_ref = statistic.rows[number_of_parameters];top_ref!=statistic.end_poly;top_ref=top_ref->next_poly) |
|---|
| 2242 | { |
|---|
| 2243 | // cout << "CDF:"<< (*top_ref).first << endl; |
|---|
| 2244 | |
|---|
| 2245 | toprow* current_toprow = ((toprow*)top_ref); |
|---|
| 2246 | |
|---|
| 2247 | prob_sum += current_toprow->probability; |
|---|
| 2248 | |
|---|
| 2249 | if(prob_sum >= rnumber*normalization_factor) |
|---|
| 2250 | { |
|---|
| 2251 | sampled_toprow = (toprow*)top_ref; |
|---|
| 2252 | break; |
|---|
| 2253 | } |
|---|
| 2254 | else |
|---|
| 2255 | { |
|---|
| 2256 | if(top_ref->next_poly==statistic.end_poly) |
|---|
| 2257 | { |
|---|
| 2258 | cout << "Error."; |
|---|
| 2259 | } |
|---|
| 2260 | } |
|---|
| 2261 | } |
|---|
| 2262 | |
|---|
| 2263 | //// cout << "Toprow/Count: " << toprow_count << "/" << ordered_toprows.size() << endl; |
|---|
| 2264 | // cout << &sampled_toprow << ";"; |
|---|
| 2265 | |
|---|
| 2266 | rnumber = randu(); |
|---|
| 2267 | |
|---|
| 2268 | set<simplex*>::iterator s_ref; |
|---|
| 2269 | prob_sum = 0; |
|---|
| 2270 | for(s_ref = sampled_toprow->triangulation.begin();s_ref!=sampled_toprow->triangulation.end();s_ref++) |
|---|
| 2271 | { |
|---|
| 2272 | prob_sum += (*s_ref)->probability; |
|---|
| 2273 | |
|---|
| 2274 | if(prob_sum/sampled_toprow->probability >= rnumber) |
|---|
| 2275 | break; |
|---|
| 2276 | } |
|---|
| 2277 | |
|---|
| 2278 | return pair<vec,simplex*>(sampled_toprow->condition_sum,*s_ref); |
|---|
| 2279 | } |
|---|
| 2280 | |
|---|
| 2281 | pair<double,double> choose_sigma(simplex* sampled_simplex) |
|---|
| 2282 | { |
|---|
| 2283 | double sigma = 0; |
|---|
| 2284 | double pg_sum; |
|---|
| 2285 | double rnumber = randu(); |
|---|
| 2286 | |
|---|
| 2287 | double sum_g = 0; |
|---|
| 2288 | for(int i = 0;i<sampled_simplex->positive_gamma_parameters.size();i++) |
|---|
| 2289 | { |
|---|
| 2290 | for(multimap<double,double>::iterator g_ref = sampled_simplex->positive_gamma_parameters[i].begin();g_ref != sampled_simplex->positive_gamma_parameters[i].end();g_ref++) |
|---|
| 2291 | { |
|---|
| 2292 | sum_g += (*g_ref).first/sampled_simplex->positive_gamma_sum; |
|---|
| 2293 | |
|---|
| 2294 | |
|---|
| 2295 | if(sum_g>rnumber) |
|---|
| 2296 | { |
|---|
| 2297 | // tady je mozna chyba ve vaskove kodu |
|---|
| 2298 | GamRNG.setup(condition_order-number_of_parameters-1+i,(*g_ref).second); |
|---|
| 2299 | |
|---|
| 2300 | sigma = 1/GamRNG(); |
|---|
| 2301 | // cout << "Sigma mean: " << (*g_ref).second/(conditions.size()-number_of_parameters-1) << endl; |
|---|
| 2302 | |
|---|
| 2303 | break; |
|---|
| 2304 | } |
|---|
| 2305 | } |
|---|
| 2306 | |
|---|
| 2307 | if(sigma!=0) |
|---|
| 2308 | { |
|---|
| 2309 | break; |
|---|
| 2310 | } |
|---|
| 2311 | } |
|---|
| 2312 | |
|---|
| 2313 | pg_sum = 0; |
|---|
| 2314 | int i = 0; |
|---|
| 2315 | for(vector<multimap<double,double>>::iterator v_ref = sampled_simplex->positive_gamma_parameters.begin();v_ref!=sampled_simplex->positive_gamma_parameters.end();v_ref++) |
|---|
| 2316 | { |
|---|
| 2317 | for(multimap<double,double>::iterator pg_ref = (*v_ref).begin();pg_ref!=(*v_ref).end();pg_ref++) |
|---|
| 2318 | { |
|---|
| 2319 | pg_sum += exp(-(*pg_ref).second/sigma)*pow((*pg_ref).second/sigma,condition_order-number_of_parameters-1+i)/sigma/fact(condition_order-number_of_parameters-2+i)*(*pg_ref).first; // pg_sum += exp((sampled_simplex->min_beta-(*pg_ref).second)/sigma)*pow((*pg_ref).second/sigma,(int)conditions.size())*(*pg_ref).second/fact(conditions.size())*(*pg_ref).first; |
|---|
| 2320 | } |
|---|
| 2321 | |
|---|
| 2322 | i++; |
|---|
| 2323 | } |
|---|
| 2324 | |
|---|
| 2325 | return pair<double,double>(sampled_simplex->positive_gamma_sum/pg_sum,sigma); |
|---|
| 2326 | } |
|---|
| 2327 | |
|---|
| 2328 | pair<vec,mat> sample(int n, bool rejection) |
|---|
| 2329 | { |
|---|
| 2330 | vec probabilities; |
|---|
| 2331 | mat samples; |
|---|
| 2332 | |
|---|
| 2333 | while(samples.cols()<n) |
|---|
| 2334 | { |
|---|
| 2335 | pair<vec,simplex*> condition_and_simplex = choose_simplex(); |
|---|
| 2336 | |
|---|
| 2337 | pair<double,double> probability_and_sigma = choose_sigma(condition_and_simplex.second); |
|---|
| 2338 | |
|---|
| 2339 | /* |
|---|
| 2340 | if(samples.cols()<10) |
|---|
| 2341 | { |
|---|
| 2342 | cout << "S-P:" << probability_and_sigma.first << ", "; |
|---|
| 2343 | pause(0.3); |
|---|
| 2344 | }*/ |
|---|
| 2345 | |
|---|
| 2346 | int dimension = condition_and_simplex.second->vertices.size(); |
|---|
| 2347 | |
|---|
| 2348 | mat jacobian(dimension,dimension-1); |
|---|
| 2349 | vec gradient = condition_and_simplex.first; |
|---|
| 2350 | |
|---|
| 2351 | int row_count = 0; |
|---|
| 2352 | |
|---|
| 2353 | for(set<vertex*>::iterator vert_ref = condition_and_simplex.second->vertices.begin();vert_ref!=condition_and_simplex.second->vertices.end();vert_ref++) |
|---|
| 2354 | { |
|---|
| 2355 | jacobian.set_row(row_count,(*vert_ref)->get_coordinates()); |
|---|
| 2356 | row_count++; |
|---|
| 2357 | } |
|---|
| 2358 | |
|---|
| 2359 | ExpRNG.setup(1); |
|---|
| 2360 | |
|---|
| 2361 | vec sample_coords; |
|---|
| 2362 | double sample_sum = 0; |
|---|
| 2363 | for(int j = 0;j<dimension;j++) |
|---|
| 2364 | { |
|---|
| 2365 | double rnumber = ExpRNG(); |
|---|
| 2366 | |
|---|
| 2367 | sample_sum += rnumber; |
|---|
| 2368 | |
|---|
| 2369 | sample_coords.ins(0,rnumber); |
|---|
| 2370 | } |
|---|
| 2371 | |
|---|
| 2372 | sample_coords /= sample_sum; |
|---|
| 2373 | |
|---|
| 2374 | sample_coords = jacobian.T()*sample_coords; |
|---|
| 2375 | |
|---|
| 2376 | vec extended_coords = sample_coords; |
|---|
| 2377 | extended_coords.ins(0,-1.0); |
|---|
| 2378 | |
|---|
| 2379 | double exponent = extended_coords*condition_and_simplex.first; |
|---|
| 2380 | double sample_prob = 1*condition_and_simplex.second->volume/condition_and_simplex.second->probability/pow(2*probability_and_sigma.second,condition_order)*exp(-exponent/probability_and_sigma.second)*probability_and_sigma.first; |
|---|
| 2381 | |
|---|
| 2382 | /* |
|---|
| 2383 | if(samples.cols()<20) |
|---|
| 2384 | { |
|---|
| 2385 | cout << "prob:" << sample_prob << endl; |
|---|
| 2386 | pause(0.2); |
|---|
| 2387 | } |
|---|
| 2388 | */ |
|---|
| 2389 | |
|---|
| 2390 | sample_coords.ins(sample_coords.size(),probability_and_sigma.second); |
|---|
| 2391 | |
|---|
| 2392 | samples.ins_col(0,sample_coords); |
|---|
| 2393 | probabilities.ins(0,sample_prob); |
|---|
| 2394 | |
|---|
| 2395 | |
|---|
| 2396 | |
|---|
| 2397 | //cout << "C:" << sample_coords << " p:" << sample_prob << endl; |
|---|
| 2398 | //pause(1); |
|---|
| 2399 | } |
|---|
| 2400 | |
|---|
| 2401 | if(rejection) |
|---|
| 2402 | { |
|---|
| 2403 | double max_prob = max(probabilities); |
|---|
| 2404 | |
|---|
| 2405 | set<int> indices; |
|---|
| 2406 | for(int i = 0;i<n;i++) |
|---|
| 2407 | { |
|---|
| 2408 | double randn = randu(); |
|---|
| 2409 | |
|---|
| 2410 | if(probabilities.get(i)<randn*max_prob) |
|---|
| 2411 | { |
|---|
| 2412 | indices.insert(i); |
|---|
| 2413 | } |
|---|
| 2414 | } |
|---|
| 2415 | |
|---|
| 2416 | for(set<int>::reverse_iterator ind_ref = indices.rbegin();ind_ref!=indices.rend();ind_ref++) |
|---|
| 2417 | { |
|---|
| 2418 | samples.del_col(*ind_ref); |
|---|
| 2419 | } |
|---|
| 2420 | |
|---|
| 2421 | return pair<vec,mat>(ones(samples.cols()),samples); |
|---|
| 2422 | } |
|---|
| 2423 | else |
|---|
| 2424 | { |
|---|
| 2425 | return pair<vec,mat>(probabilities,samples); |
|---|
| 2426 | } |
|---|
| 2427 | } |
|---|
| 2428 | |
|---|
| 2429 | int logfact(int factor) |
|---|
| 2430 | { |
|---|
| 2431 | if(factor>1) |
|---|
| 2432 | { |
|---|
| 2433 | return log((double)factor)+logfact(factor-1); |
|---|
| 2434 | } |
|---|
| 2435 | else |
|---|
| 2436 | { |
|---|
| 2437 | return 0; |
|---|
| 2438 | } |
|---|
| 2439 | } |
|---|
| 2440 | protected: |
|---|
| 2441 | |
|---|
| 2442 | /// A method for creating plain default statistic representing only the range of the parameter space. |
|---|
| 2443 | void create_statistic(int number_of_parameters, double alpha_deviation, double sigma_deviation) |
|---|
| 2444 | { |
|---|
| 2445 | /* |
|---|
| 2446 | for(int i = 0;i<number_of_parameters;i++) |
|---|
| 2447 | { |
|---|
| 2448 | vec condition_vec = zeros(number_of_parameters+1); |
|---|
| 2449 | condition_vec[i+1] = 1; |
|---|
| 2450 | |
|---|
| 2451 | condition* new_condition = new condition(condition_vec); |
|---|
| 2452 | |
|---|
| 2453 | conditions.push_back(new_condition); |
|---|
| 2454 | } |
|---|
| 2455 | */ |
|---|
| 2456 | |
|---|
| 2457 | // An empty vector of coordinates. |
|---|
| 2458 | vec origin_coord; |
|---|
| 2459 | |
|---|
| 2460 | // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. |
|---|
| 2461 | vertex *origin = new vertex(origin_coord); |
|---|
| 2462 | |
|---|
| 2463 | origin->my_emlig = this; |
|---|
| 2464 | |
|---|
| 2465 | /* |
|---|
| 2466 | // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse |
|---|
| 2467 | // diagram. First we create a vector of polyhedrons.. |
|---|
| 2468 | list<polyhedron*> origin_vec; |
|---|
| 2469 | |
|---|
| 2470 | // ..we fill it with the origin.. |
|---|
| 2471 | origin_vec.push_back(origin); |
|---|
| 2472 | |
|---|
| 2473 | // ..and we fill the statistic with the created vector. |
|---|
| 2474 | statistic.push_back(origin_vec); |
|---|
| 2475 | */ |
|---|
| 2476 | |
|---|
| 2477 | statistic = *(new c_statistic()); |
|---|
| 2478 | |
|---|
| 2479 | statistic.append_polyhedron(0, origin); |
|---|
| 2480 | |
|---|
| 2481 | // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to |
|---|
| 2482 | // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: |
|---|
| 2483 | for(int i=0;i<number_of_parameters;i++) |
|---|
| 2484 | { |
|---|
| 2485 | // We first will create two new vertices. These will be the borders of the parameter space in the dimension |
|---|
| 2486 | // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the |
|---|
| 2487 | // right amount of zero cooridnates by reading them from the origin |
|---|
| 2488 | vec origin_coord = origin->get_coordinates(); |
|---|
| 2489 | |
|---|
| 2490 | |
|---|
| 2491 | |
|---|
| 2492 | // And we incorporate the nonzero coordinates into the new cooordinate vectors |
|---|
| 2493 | vec origin_coord1 = concat(origin_coord,-max_range); |
|---|
| 2494 | vec origin_coord2 = concat(origin_coord,max_range); |
|---|
| 2495 | |
|---|
| 2496 | |
|---|
| 2497 | // Now we create the points |
|---|
| 2498 | vertex* new_point1 = new vertex(origin_coord1); |
|---|
| 2499 | vertex* new_point2 = new vertex(origin_coord2); |
|---|
| 2500 | |
|---|
| 2501 | new_point1->my_emlig = this; |
|---|
| 2502 | new_point2->my_emlig = this; |
|---|
| 2503 | |
|---|
| 2504 | //********************************************************************************************************* |
|---|
| 2505 | // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old |
|---|
| 2506 | // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points |
|---|
| 2507 | // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons |
|---|
| 2508 | // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is |
|---|
| 2509 | // connected to the first (second) newly created vertex by a parent-child relation. |
|---|
| 2510 | //********************************************************************************************************* |
|---|
| 2511 | |
|---|
| 2512 | |
|---|
| 2513 | /* |
|---|
| 2514 | // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram |
|---|
| 2515 | vector<vector<polyhedron*>> new_statistic1; |
|---|
| 2516 | vector<vector<polyhedron*>> new_statistic2; |
|---|
| 2517 | */ |
|---|
| 2518 | |
|---|
| 2519 | c_statistic* new_statistic1 = new c_statistic(); |
|---|
| 2520 | c_statistic* new_statistic2 = new c_statistic(); |
|---|
| 2521 | |
|---|
| 2522 | |
|---|
| 2523 | // Copy the statistic by rows |
|---|
| 2524 | for(int j=0;j<statistic.size();j++) |
|---|
| 2525 | { |
|---|
| 2526 | |
|---|
| 2527 | |
|---|
| 2528 | // an element counter |
|---|
| 2529 | int element_number = 0; |
|---|
| 2530 | |
|---|
| 2531 | /* |
|---|
| 2532 | vector<polyhedron*> supportnew_1; |
|---|
| 2533 | vector<polyhedron*> supportnew_2; |
|---|
| 2534 | |
|---|
| 2535 | new_statistic1.push_back(supportnew_1); |
|---|
| 2536 | new_statistic2.push_back(supportnew_2); |
|---|
| 2537 | */ |
|---|
| 2538 | |
|---|
| 2539 | // for each polyhedron in the given row |
|---|
| 2540 | for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
|---|
| 2541 | { |
|---|
| 2542 | // Append an extra zero coordinate to each of the vertices for the new dimension |
|---|
| 2543 | // If vert_ref is at the first index => we loop through vertices |
|---|
| 2544 | if(j == 0) |
|---|
| 2545 | { |
|---|
| 2546 | // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates |
|---|
| 2547 | ((vertex*) horiz_ref)->push_coordinate(0); |
|---|
| 2548 | } |
|---|
| 2549 | /* |
|---|
| 2550 | else |
|---|
| 2551 | { |
|---|
| 2552 | ((toprow*) (*horiz_ref))->condition.ins(0,0); |
|---|
| 2553 | }*/ |
|---|
| 2554 | |
|---|
| 2555 | // if it has parents |
|---|
| 2556 | if(!horiz_ref->parents.empty()) |
|---|
| 2557 | { |
|---|
| 2558 | // save the relative address of this child in a vector kids_rel_addresses of all its parents. |
|---|
| 2559 | // This information will later be used for copying the whole Hasse diagram with each of the |
|---|
| 2560 | // relations contained within. |
|---|
| 2561 | for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++) |
|---|
| 2562 | { |
|---|
| 2563 | (*parent_ref)->kids_rel_addresses.push_back(element_number); |
|---|
| 2564 | } |
|---|
| 2565 | } |
|---|
| 2566 | |
|---|
| 2567 | // ************************************************************************************************** |
|---|
| 2568 | // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron |
|---|
| 2569 | // will be created as a toprow, but this information will be later forgotten and only the polyhedrons |
|---|
| 2570 | // in the top row of the Hasse diagram will be considered toprow for later use. |
|---|
| 2571 | // ************************************************************************************************** |
|---|
| 2572 | |
|---|
| 2573 | // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic |
|---|
| 2574 | // this condition will be a vector of zeros. There are two vectors, because we need two copies of |
|---|
| 2575 | // the original Hasse diagram. |
|---|
| 2576 | vec vec1; |
|---|
| 2577 | vec vec2; |
|---|
| 2578 | if(!horiz_ref->kids_rel_addresses.empty()) |
|---|
| 2579 | { |
|---|
| 2580 | vec1 = ((toprow*)horiz_ref)->condition_sum; |
|---|
| 2581 | vec1.ins(vec1.size(),-alpha_deviation); |
|---|
| 2582 | |
|---|
| 2583 | vec2 = ((toprow*)horiz_ref)->condition_sum; |
|---|
| 2584 | vec2.ins(vec2.size(),alpha_deviation); |
|---|
| 2585 | } |
|---|
| 2586 | else |
|---|
| 2587 | { |
|---|
| 2588 | vec1.ins(0,-alpha_deviation); |
|---|
| 2589 | vec2.ins(0,alpha_deviation); |
|---|
| 2590 | |
|---|
| 2591 | vec1.ins(0,-sigma_deviation); |
|---|
| 2592 | vec2.ins(0,-sigma_deviation); |
|---|
| 2593 | } |
|---|
| 2594 | |
|---|
| 2595 | // cout << vec1 << endl; |
|---|
| 2596 | // cout << vec2 << endl; |
|---|
| 2597 | |
|---|
| 2598 | |
|---|
| 2599 | // We create a new toprow with the previously specified condition. |
|---|
| 2600 | toprow* current_copy1 = new toprow(vec1, this->condition_order); |
|---|
| 2601 | toprow* current_copy2 = new toprow(vec2, this->condition_order); |
|---|
| 2602 | |
|---|
| 2603 | current_copy1->my_emlig = this; |
|---|
| 2604 | current_copy2->my_emlig = this; |
|---|
| 2605 | |
|---|
| 2606 | // The vertices of the copies will be inherited, because there will be a parent/child relation |
|---|
| 2607 | // between each polyhedron and its offspring (comming from the copy) and a parent has all the |
|---|
| 2608 | // vertices of its child plus more. |
|---|
| 2609 | for(set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++) |
|---|
| 2610 | { |
|---|
| 2611 | current_copy1->vertices.insert(*vertex_ref); |
|---|
| 2612 | current_copy2->vertices.insert(*vertex_ref); |
|---|
| 2613 | } |
|---|
| 2614 | |
|---|
| 2615 | // The only new vertex of the offspring should be the newly created point. |
|---|
| 2616 | current_copy1->vertices.insert(new_point1); |
|---|
| 2617 | current_copy2->vertices.insert(new_point2); |
|---|
| 2618 | |
|---|
| 2619 | // This method guarantees that each polyhedron is already triangulated, therefore its triangulation |
|---|
| 2620 | // is only one set of vertices and it is the set of all its vertices. |
|---|
| 2621 | simplex* t_simplex1 = new simplex(current_copy1->vertices); |
|---|
| 2622 | simplex* t_simplex2 = new simplex(current_copy2->vertices); |
|---|
| 2623 | |
|---|
| 2624 | current_copy1->triangulation.insert(t_simplex1); |
|---|
| 2625 | current_copy2->triangulation.insert(t_simplex2); |
|---|
| 2626 | |
|---|
| 2627 | // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying |
|---|
| 2628 | // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the |
|---|
| 2629 | // kids and when we do that and know the child, in the child we will remember the parent we came from. |
|---|
| 2630 | // This way all the parents/children relations are saved in both the parent and the child. |
|---|
| 2631 | if(!horiz_ref->kids_rel_addresses.empty()) |
|---|
| 2632 | { |
|---|
| 2633 | for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++) |
|---|
| 2634 | { |
|---|
| 2635 | polyhedron* new_kid1 = new_statistic1->rows[j-1]; |
|---|
| 2636 | polyhedron* new_kid2 = new_statistic2->rows[j-1]; |
|---|
| 2637 | |
|---|
| 2638 | // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but |
|---|
| 2639 | // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline. |
|---|
| 2640 | if(*kid_ref) |
|---|
| 2641 | { |
|---|
| 2642 | for(int k = 1;k<=(*kid_ref);k++) |
|---|
| 2643 | { |
|---|
| 2644 | new_kid1=new_kid1->next_poly; |
|---|
| 2645 | new_kid2=new_kid2->next_poly; |
|---|
| 2646 | } |
|---|
| 2647 | } |
|---|
| 2648 | |
|---|
| 2649 | // find the child and save the relation to the parent |
|---|
| 2650 | current_copy1->children.push_back(new_kid1); |
|---|
| 2651 | current_copy2->children.push_back(new_kid2); |
|---|
| 2652 | |
|---|
| 2653 | // in the child save the parents' address |
|---|
| 2654 | new_kid1->parents.push_back(current_copy1); |
|---|
| 2655 | new_kid2->parents.push_back(current_copy2); |
|---|
| 2656 | } |
|---|
| 2657 | |
|---|
| 2658 | // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the |
|---|
| 2659 | // Hasse diagram again) |
|---|
| 2660 | horiz_ref->kids_rel_addresses.clear(); |
|---|
| 2661 | } |
|---|
| 2662 | // If there were no children previously, we are copying a polyhedron that has been a vertex before. |
|---|
| 2663 | // In this case it is a segment now and it will have a relation to its mother (copywise) and to the |
|---|
| 2664 | // newly created point. Here we create the connection to the new point, again from both sides. |
|---|
| 2665 | else |
|---|
| 2666 | { |
|---|
| 2667 | // Add the address of the new point in the former vertex |
|---|
| 2668 | current_copy1->children.push_back(new_point1); |
|---|
| 2669 | current_copy2->children.push_back(new_point2); |
|---|
| 2670 | |
|---|
| 2671 | // Add the address of the former vertex in the new point |
|---|
| 2672 | new_point1->parents.push_back(current_copy1); |
|---|
| 2673 | new_point2->parents.push_back(current_copy2); |
|---|
| 2674 | } |
|---|
| 2675 | |
|---|
| 2676 | // Save the mother in its offspring |
|---|
| 2677 | current_copy1->children.push_back(horiz_ref); |
|---|
| 2678 | current_copy2->children.push_back(horiz_ref); |
|---|
| 2679 | |
|---|
| 2680 | // Save the offspring in its mother |
|---|
| 2681 | horiz_ref->parents.push_back(current_copy1); |
|---|
| 2682 | horiz_ref->parents.push_back(current_copy2); |
|---|
| 2683 | |
|---|
| 2684 | |
|---|
| 2685 | // Add the copies into the relevant statistic. The statistic will later be appended to the previous |
|---|
| 2686 | // Hasse diagram |
|---|
| 2687 | new_statistic1->append_polyhedron(j,current_copy1); |
|---|
| 2688 | new_statistic2->append_polyhedron(j,current_copy2); |
|---|
| 2689 | |
|---|
| 2690 | // Raise the count in the vector of polyhedrons |
|---|
| 2691 | element_number++; |
|---|
| 2692 | |
|---|
| 2693 | } |
|---|
| 2694 | |
|---|
| 2695 | } |
|---|
| 2696 | |
|---|
| 2697 | /* |
|---|
| 2698 | statistic.begin()->push_back(new_point1); |
|---|
| 2699 | statistic.begin()->push_back(new_point2); |
|---|
| 2700 | */ |
|---|
| 2701 | |
|---|
| 2702 | statistic.append_polyhedron(0, new_point1); |
|---|
| 2703 | statistic.append_polyhedron(0, new_point2); |
|---|
| 2704 | |
|---|
| 2705 | // Merge the new statistics into the old one. This will either be the final statistic or we will |
|---|
| 2706 | // reenter the widening loop. |
|---|
| 2707 | for(int j=0;j<new_statistic1->size();j++) |
|---|
| 2708 | { |
|---|
| 2709 | /* |
|---|
| 2710 | if(j+1==statistic.size()) |
|---|
| 2711 | { |
|---|
| 2712 | list<polyhedron*> support; |
|---|
| 2713 | statistic.push_back(support); |
|---|
| 2714 | } |
|---|
| 2715 | |
|---|
| 2716 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); |
|---|
| 2717 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end()); |
|---|
| 2718 | */ |
|---|
| 2719 | statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]); |
|---|
| 2720 | statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]); |
|---|
| 2721 | } |
|---|
| 2722 | } |
|---|
| 2723 | |
|---|
| 2724 | /* |
|---|
| 2725 | vector<list<toprow*>> toprow_statistic; |
|---|
| 2726 | int line_count = 0; |
|---|
| 2727 | |
|---|
| 2728 | for(vector<list<polyhedron*>>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++) |
|---|
| 2729 | { |
|---|
| 2730 | list<toprow*> support_list; |
|---|
| 2731 | toprow_statistic.push_back(support_list); |
|---|
| 2732 | |
|---|
| 2733 | for(list<polyhedron*>::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++) |
|---|
| 2734 | { |
|---|
| 2735 | toprow* support_top = (toprow*)(*polyhedron_ref2); |
|---|
| 2736 | |
|---|
| 2737 | toprow_statistic[line_count].push_back(support_top); |
|---|
| 2738 | } |
|---|
| 2739 | |
|---|
| 2740 | line_count++; |
|---|
| 2741 | }*/ |
|---|
| 2742 | |
|---|
| 2743 | /* |
|---|
| 2744 | vector<int> sizevector; |
|---|
| 2745 | for(int s = 0;s<statistic.size();s++) |
|---|
| 2746 | { |
|---|
| 2747 | sizevector.push_back(statistic.row_size(s)); |
|---|
| 2748 | } |
|---|
| 2749 | */ |
|---|
| 2750 | |
|---|
| 2751 | } |
|---|
| 2752 | |
|---|
| 2753 | }; |
|---|
| 2754 | |
|---|
| 2755 | |
|---|
| 2756 | |
|---|
| 2757 | //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density |
|---|
| 2758 | class RARX //: public BM |
|---|
| 2759 | { |
|---|
| 2760 | private: |
|---|
| 2761 | bool has_constant; |
|---|
| 2762 | |
|---|
| 2763 | int window_size; |
|---|
| 2764 | |
|---|
| 2765 | list<vec> conditions; |
|---|
| 2766 | |
|---|
| 2767 | public: |
|---|
| 2768 | emlig* posterior; |
|---|
| 2769 | |
|---|
| 2770 | RARX(int number_of_parameters, const int window_size, bool has_constant, double alpha_deviation, double sigma_deviation, int nu)//:BM() |
|---|
| 2771 | { |
|---|
| 2772 | this->has_constant = has_constant; |
|---|
| 2773 | |
|---|
| 2774 | posterior = new emlig(number_of_parameters,alpha_deviation,sigma_deviation,nu); |
|---|
| 2775 | |
|---|
| 2776 | this->window_size = window_size; |
|---|
| 2777 | }; |
|---|
| 2778 | |
|---|
| 2779 | RARX(int number_of_parameters, const int window_size, bool has_constant)//:BM() |
|---|
| 2780 | { |
|---|
| 2781 | this->has_constant = has_constant; |
|---|
| 2782 | |
|---|
| 2783 | posterior = new emlig(number_of_parameters,1.0,1.0,number_of_parameters+3); |
|---|
| 2784 | |
|---|
| 2785 | this->window_size = window_size; |
|---|
| 2786 | }; |
|---|
| 2787 | |
|---|
| 2788 | ~RARX() |
|---|
| 2789 | { |
|---|
| 2790 | delete posterior; |
|---|
| 2791 | } |
|---|
| 2792 | |
|---|
| 2793 | void bayes(itpp::vec yt) |
|---|
| 2794 | { |
|---|
| 2795 | bool informative = true; |
|---|
| 2796 | for(int i=1;i<yt.size();i++) |
|---|
| 2797 | { |
|---|
| 2798 | if(yt.get(i)==0.0) |
|---|
| 2799 | { |
|---|
| 2800 | informative = false; |
|---|
| 2801 | } |
|---|
| 2802 | } |
|---|
| 2803 | |
|---|
| 2804 | if(informative) |
|---|
| 2805 | { |
|---|
| 2806 | if(has_constant) |
|---|
| 2807 | { |
|---|
| 2808 | int c_size = yt.size(); |
|---|
| 2809 | |
|---|
| 2810 | yt.ins(c_size,1.0); |
|---|
| 2811 | } |
|---|
| 2812 | |
|---|
| 2813 | if(yt.size() == posterior->number_of_parameters+1) |
|---|
| 2814 | { |
|---|
| 2815 | conditions.push_back(yt); |
|---|
| 2816 | } |
|---|
| 2817 | else |
|---|
| 2818 | { |
|---|
| 2819 | throw new exception("Wrong condition size for bayesian data update!"); |
|---|
| 2820 | } |
|---|
| 2821 | |
|---|
| 2822 | //posterior->step_me(0); |
|---|
| 2823 | |
|---|
| 2824 | cout << "*************************************" << endl << "Added condition: " << yt << endl << "*************************************" << endl; |
|---|
| 2825 | |
|---|
| 2826 | /// \TODO tohle je spatne, tady musi byt jiny vypocet poctu podminek, kdyby nejaka byla multiplicitni, tak tohle bude spatne |
|---|
| 2827 | if(conditions.size()>window_size && window_size!=0) |
|---|
| 2828 | { |
|---|
| 2829 | cout << "*************************************" << endl << "Removed condition:" << conditions.front() << endl << "*************************************" << endl; |
|---|
| 2830 | |
|---|
| 2831 | posterior->add_and_remove_condition(yt,conditions.front()); |
|---|
| 2832 | conditions.pop_front(); |
|---|
| 2833 | //posterior->step_me(1); |
|---|
| 2834 | } |
|---|
| 2835 | else |
|---|
| 2836 | { |
|---|
| 2837 | posterior->add_condition(yt); |
|---|
| 2838 | } |
|---|
| 2839 | } |
|---|
| 2840 | } |
|---|
| 2841 | |
|---|
| 2842 | }; |
|---|
| 2843 | |
|---|
| 2844 | |
|---|
| 2845 | |
|---|
| 2846 | #endif //TRAGE_H |
|---|