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