[1270] | 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 | |
---|
[1325] | 10 | #include <stat/exp_family.h> |
---|
[1273] | 11 | #include <itpp/itbase.h> |
---|
[1337] | 12 | #include <itpp/base/random.h> |
---|
[1273] | 13 | #include <map> |
---|
[1270] | 14 | #include <limits> |
---|
| 15 | #include <vector> |
---|
| 16 | #include <list> |
---|
| 17 | #include <set> |
---|
| 18 | #include <algorithm> |
---|
| 19 | |
---|
[1336] | 20 | using namespace bdm; |
---|
[1270] | 21 | using namespace std; |
---|
| 22 | using namespace itpp; |
---|
| 23 | |
---|
[1349] | 24 | const double max_range = 50;//numeric_limits<double>::max()/10e-10; |
---|
[1270] | 25 | |
---|
[1301] | 26 | /// An enumeration of possible actions performed on the polyhedrons. We can merge them or split them. |
---|
[1270] | 27 | enum actions {MERGE, SPLIT}; |
---|
| 28 | |
---|
[1301] | 29 | // Forward declaration of polyhedron, vertex and emlig |
---|
[1270] | 30 | class polyhedron; |
---|
| 31 | class vertex; |
---|
[1301] | 32 | class emlig; |
---|
[1270] | 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 | |
---|
[1301] | 49 | /// A class representing a single condition that can be added to the emlig. A condition represents data entries in a statistical model. |
---|
[1300] | 50 | class condition |
---|
| 51 | { |
---|
| 52 | public: |
---|
[1301] | 53 | /// Value of the condition representing the data |
---|
[1300] | 54 | vec value; |
---|
[1270] | 55 | |
---|
[1301] | 56 | /// Mulitplicity of the given condition may represent multiple occurences of same data entry. |
---|
[1300] | 57 | int multiplicity; |
---|
[1299] | 58 | |
---|
[1301] | 59 | /// Default constructor of condition class takes the value of data entry and creates a condition with multiplicity 1 (first occurence of the data). |
---|
[1300] | 60 | condition(vec value) |
---|
| 61 | { |
---|
| 62 | this->value = value; |
---|
| 63 | multiplicity = 1; |
---|
| 64 | } |
---|
| 65 | }; |
---|
| 66 | |
---|
[1324] | 67 | class simplex |
---|
| 68 | { |
---|
[1331] | 69 | |
---|
| 70 | |
---|
[1324] | 71 | public: |
---|
[1300] | 72 | |
---|
[1324] | 73 | set<vertex*> vertices; |
---|
| 74 | |
---|
| 75 | double probability; |
---|
| 76 | |
---|
[1331] | 77 | vector<multimap<double,double>> positive_gamma_parameters; |
---|
[1324] | 78 | |
---|
[1331] | 79 | vector<multimap<double,double>> negative_gamma_parameters; |
---|
| 80 | |
---|
| 81 | double positive_gamma_sum; |
---|
| 82 | |
---|
| 83 | double negative_gamma_sum; |
---|
[1335] | 84 | |
---|
| 85 | double min_beta; |
---|
[1325] | 86 | |
---|
| 87 | |
---|
[1324] | 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 | } |
---|
[1331] | 99 | |
---|
| 100 | void clear_gammas() |
---|
| 101 | { |
---|
| 102 | positive_gamma_parameters.clear(); |
---|
[1335] | 103 | negative_gamma_parameters.clear(); |
---|
[1331] | 104 | |
---|
| 105 | positive_gamma_sum = 0; |
---|
| 106 | negative_gamma_sum = 0; |
---|
[1335] | 107 | |
---|
| 108 | min_beta = numeric_limits<double>::max(); |
---|
[1331] | 109 | } |
---|
| 110 | |
---|
| 111 | void insert_gamma(int order, double weight, double beta) |
---|
| 112 | { |
---|
| 113 | if(weight>=0) |
---|
| 114 | { |
---|
[1343] | 115 | while(positive_gamma_parameters.size()<order+1) |
---|
[1331] | 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 | { |
---|
[1343] | 127 | while(negative_gamma_parameters.size()<order+1) |
---|
[1331] | 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 | } |
---|
[1335] | 137 | |
---|
| 138 | if(beta < min_beta) |
---|
| 139 | { |
---|
| 140 | min_beta = beta; |
---|
| 141 | } |
---|
[1331] | 142 | } |
---|
[1324] | 143 | }; |
---|
| 144 | |
---|
| 145 | |
---|
[1270] | 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 | |
---|
[1301] | 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. |
---|
[1270] | 159 | int split_state; |
---|
| 160 | |
---|
[1301] | 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. |
---|
[1270] | 166 | int merge_state; |
---|
| 167 | |
---|
[1299] | 168 | |
---|
[1270] | 169 | |
---|
| 170 | public: |
---|
[1301] | 171 | /// A pointer to the multi-Laplace inverse gamma distribution this polyhedron belongs to. |
---|
[1270] | 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 | |
---|
[1301] | 183 | /// The conditions that gave birth to the polyhedron. If some of them is removed, the polyhedron ceases to exist. |
---|
[1300] | 184 | set<condition*> parentconditions; |
---|
| 185 | |
---|
[1270] | 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 | |
---|
[1301] | 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. |
---|
[1299] | 198 | set<polyhedron*> totallyneutralgrandchildren; |
---|
[1270] | 199 | |
---|
[1301] | 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. |
---|
[1299] | 203 | set<polyhedron*> totallyneutralchildren; |
---|
[1270] | 204 | |
---|
[1301] | 205 | /// Reverse relation to the totallyneutralgrandchildren set is needed for merging of already existing polyhedrons to keep |
---|
| 206 | /// totallyneutralgrandchildren list up to date. |
---|
[1299] | 207 | set<polyhedron*> grandparents; |
---|
| 208 | |
---|
[1301] | 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. |
---|
[1270] | 211 | set<vertex*> positiveneutralvertices; |
---|
| 212 | |
---|
[1301] | 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. |
---|
[1270] | 215 | set<vertex*> negativeneutralvertices; |
---|
| 216 | |
---|
[1301] | 217 | /// A bool specifying if the polyhedron lies exactly on the newly added condition or not. |
---|
[1270] | 218 | bool totally_neutral; |
---|
| 219 | |
---|
[1301] | 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. |
---|
[1299] | 222 | polyhedron* mergechild; |
---|
[1270] | 223 | |
---|
[1301] | 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. |
---|
[1270] | 226 | polyhedron* positiveparent; |
---|
| 227 | |
---|
[1301] | 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. |
---|
[1299] | 230 | polyhedron* negativeparent; |
---|
[1270] | 231 | |
---|
[1301] | 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.). |
---|
[1270] | 234 | polyhedron* next_poly; |
---|
| 235 | |
---|
[1301] | 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.). |
---|
[1270] | 238 | polyhedron* prev_poly; |
---|
| 239 | |
---|
[1301] | 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. |
---|
[1270] | 242 | int message_counter; |
---|
| 243 | |
---|
| 244 | /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. |
---|
[1324] | 245 | set<simplex*> triangulation; |
---|
[1270] | 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; |
---|
[1300] | 258 | |
---|
| 259 | mergechild = NULL; |
---|
[1270] | 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 | } |
---|
[1299] | 273 | |
---|
| 274 | int get_multiplicity() |
---|
| 275 | { |
---|
| 276 | return multiplicity; |
---|
| 277 | } |
---|
[1270] | 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 | |
---|
[1301] | 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. |
---|
[1300] | 295 | int set_state(double state_indicator, actions action) |
---|
[1270] | 296 | { |
---|
| 297 | switch(action) |
---|
| 298 | { |
---|
| 299 | case MERGE: |
---|
[1300] | 300 | merge_state = (int)sign(state_indicator); |
---|
| 301 | return merge_state; |
---|
[1270] | 302 | case SPLIT: |
---|
| 303 | split_state = (int)sign(state_indicator); |
---|
[1300] | 304 | return split_state; |
---|
[1270] | 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
[1301] | 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. |
---|
[1270] | 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 | |
---|
[1301] | 324 | /// Method for obtaining the number of children of given polyhedron. |
---|
[1270] | 325 | int number_of_children() |
---|
| 326 | { |
---|
| 327 | return children.size(); |
---|
| 328 | } |
---|
| 329 | |
---|
[1301] | 330 | /// A method for triangulation of given polyhedron. |
---|
[1335] | 331 | double triangulate(bool should_integrate); |
---|
[1270] | 332 | }; |
---|
| 333 | |
---|
[1273] | 334 | |
---|
[1270] | 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 |
---|
[1282] | 340 | vec coordinates; |
---|
[1270] | 341 | |
---|
| 342 | public: |
---|
[1301] | 343 | /// A property specifying the value of the density (ted nevim, jestli je to jakoby log nebo ne) above the vertex. |
---|
[1282] | 344 | double function_value; |
---|
[1270] | 345 | |
---|
| 346 | /// Default constructor |
---|
| 347 | vertex(); |
---|
| 348 | |
---|
| 349 | /// Constructor of a vertex from a set of coordinates |
---|
| 350 | vertex(vec coordinates) |
---|
| 351 | { |
---|
[1300] | 352 | this->coordinates = coordinates; |
---|
[1270] | 353 | |
---|
| 354 | vertices.insert(this); |
---|
| 355 | |
---|
[1324] | 356 | simplex* vert_simplex = new simplex(vertices); |
---|
[1270] | 357 | |
---|
[1324] | 358 | triangulation.insert(vert_simplex); |
---|
[1270] | 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 | { |
---|
[1300] | 365 | coordinates = concat(coordinates,coordinate); |
---|
[1270] | 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() |
---|
[1300] | 371 | { |
---|
[1270] | 372 | return coordinates; |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | }; |
---|
| 376 | |
---|
[1273] | 377 | |
---|
[1301] | 378 | /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differen tiates |
---|
[1270] | 379 | /// it from polyhedrons in other rows. |
---|
| 380 | class toprow : public polyhedron |
---|
| 381 | { |
---|
| 382 | |
---|
| 383 | public: |
---|
| 384 | double probability; |
---|
| 385 | |
---|
[1282] | 386 | vertex* minimal_vertex; |
---|
| 387 | |
---|
[1270] | 388 | /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation |
---|
[1300] | 389 | vec condition_sum; |
---|
[1270] | 390 | |
---|
| 391 | int condition_order; |
---|
| 392 | |
---|
| 393 | /// Default constructor |
---|
| 394 | toprow(){}; |
---|
| 395 | |
---|
| 396 | /// Constructor creating a toprow from the condition |
---|
[1300] | 397 | toprow(condition *condition, int condition_order) |
---|
[1270] | 398 | { |
---|
[1300] | 399 | this->condition_sum = condition->value; |
---|
[1270] | 400 | this->condition_order = condition_order; |
---|
| 401 | } |
---|
| 402 | |
---|
[1300] | 403 | toprow(vec condition_sum, int condition_order) |
---|
| 404 | { |
---|
[1301] | 405 | this->condition_sum = condition_sum; |
---|
| 406 | this->condition_order = condition_order; |
---|
[1300] | 407 | } |
---|
| 408 | |
---|
[1324] | 409 | double integrate_simplex(simplex* simplex, char c); |
---|
[1271] | 410 | |
---|
[1270] | 411 | }; |
---|
| 412 | |
---|
[1273] | 413 | |
---|
[1270] | 414 | |
---|
| 415 | |
---|
| 416 | |
---|
| 417 | |
---|
[1324] | 418 | |
---|
[1270] | 419 | class c_statistic |
---|
| 420 | { |
---|
[1272] | 421 | |
---|
| 422 | public: |
---|
[1270] | 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 | |
---|
[1349] | 436 | ~c_statistic() |
---|
| 437 | { |
---|
| 438 | delete end_poly; |
---|
| 439 | delete start_poly; |
---|
| 440 | } |
---|
| 441 | |
---|
[1270] | 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 | |
---|
[1273] | 572 | |
---|
[1267] | 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 | { |
---|
[1280] | 585 | return max(*this)>max(second); |
---|
| 586 | |
---|
| 587 | /* |
---|
[1267] | 588 | int size1 = this->size(); |
---|
[1280] | 589 | int size2 = second.size(); |
---|
[1267] | 590 | |
---|
| 591 | int counter1 = 0; |
---|
| 592 | while(0==0) |
---|
| 593 | { |
---|
| 594 | if((*this)[counter1]==0) |
---|
| 595 | { |
---|
| 596 | size1--; |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | if((*this)[counter1]!=0) |
---|
| 600 | break; |
---|
| 601 | |
---|
| 602 | counter1++; |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | int counter2 = 0; |
---|
| 606 | while(0==0) |
---|
| 607 | { |
---|
| 608 | if(second[counter2]==0) |
---|
| 609 | { |
---|
| 610 | size2--; |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | if(second[counter2]!=0) |
---|
| 614 | break; |
---|
| 615 | |
---|
| 616 | counter2++; |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | if(size1!=size2) |
---|
| 620 | { |
---|
| 621 | return size1>size2; |
---|
| 622 | } |
---|
| 623 | else |
---|
| 624 | { |
---|
| 625 | for(int i = 0;i<size1;i++) |
---|
| 626 | { |
---|
| 627 | if((*this)[counter1+i]!=second[counter2+i]) |
---|
| 628 | { |
---|
| 629 | return (*this)[counter1+i]>second[counter2+i]; |
---|
| 630 | } |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | return false; |
---|
[1280] | 634 | }*/ |
---|
[1267] | 635 | } |
---|
| 636 | |
---|
| 637 | |
---|
| 638 | bool operator==(const my_ivec &second) const |
---|
| 639 | { |
---|
[1280] | 640 | return max(*this)==max(second); |
---|
| 641 | |
---|
| 642 | /* |
---|
[1267] | 643 | int size1 = this->size(); |
---|
[1280] | 644 | int size2 = second.size(); |
---|
[1267] | 645 | |
---|
| 646 | int counter = 0; |
---|
| 647 | while(0==0) |
---|
| 648 | { |
---|
| 649 | if((*this)[counter]==0) |
---|
| 650 | { |
---|
| 651 | size1--; |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | if((*this)[counter]!=0) |
---|
| 655 | break; |
---|
| 656 | |
---|
| 657 | counter++; |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | counter = 0; |
---|
| 661 | while(0==0) |
---|
| 662 | { |
---|
| 663 | if(second[counter]==0) |
---|
| 664 | { |
---|
| 665 | size2--; |
---|
| 666 | } |
---|
| 667 | |
---|
| 668 | if(second[counter]!=0) |
---|
| 669 | break; |
---|
| 670 | |
---|
| 671 | counter++; |
---|
| 672 | } |
---|
| 673 | |
---|
| 674 | if(size1!=size2) |
---|
| 675 | { |
---|
| 676 | return false; |
---|
| 677 | } |
---|
| 678 | else |
---|
| 679 | { |
---|
| 680 | for(int i=0;i<size1;i++) |
---|
| 681 | { |
---|
| 682 | if((*this)[size()-1-i]!=second[second.size()-1-i]) |
---|
| 683 | { |
---|
| 684 | return false; |
---|
| 685 | } |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | return true; |
---|
[1280] | 689 | }*/ |
---|
[1267] | 690 | } |
---|
| 691 | |
---|
| 692 | bool operator<(const my_ivec &second) const |
---|
| 693 | { |
---|
| 694 | return !(((*this)>second)||((*this)==second)); |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | bool operator!=(const my_ivec &second) const |
---|
| 698 | { |
---|
| 699 | return !((*this)==second); |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | bool operator<=(const my_ivec &second) const |
---|
| 703 | { |
---|
| 704 | return !((*this)>second); |
---|
| 705 | } |
---|
| 706 | |
---|
| 707 | bool operator>=(const my_ivec &second) const |
---|
| 708 | { |
---|
| 709 | return !((*this)<second); |
---|
| 710 | } |
---|
| 711 | |
---|
| 712 | my_ivec right(my_ivec original) |
---|
| 713 | { |
---|
| 714 | |
---|
| 715 | } |
---|
| 716 | }; |
---|
| 717 | |
---|
| 718 | |
---|
[1270] | 719 | |
---|
| 720 | |
---|
| 721 | |
---|
[1273] | 722 | |
---|
| 723 | |
---|
[1270] | 724 | //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density |
---|
| 725 | class emlig // : eEF |
---|
| 726 | { |
---|
| 727 | |
---|
| 728 | /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result |
---|
| 729 | /// of data update from Bayesian estimation or set by the user if this emlig is a prior density |
---|
[1272] | 730 | |
---|
[1270] | 731 | |
---|
| 732 | vector<list<polyhedron*>> for_splitting; |
---|
| 733 | |
---|
| 734 | vector<list<polyhedron*>> for_merging; |
---|
| 735 | |
---|
| 736 | list<condition*> conditions; |
---|
| 737 | |
---|
| 738 | double normalization_factor; |
---|
| 739 | |
---|
[1349] | 740 | int condition_order; |
---|
| 741 | |
---|
[1282] | 742 | |
---|
| 743 | |
---|
[1300] | 744 | void alter_toprow_conditions(condition *condition, bool should_be_added) |
---|
[1270] | 745 | { |
---|
| 746 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
| 747 | { |
---|
| 748 | set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); |
---|
| 749 | |
---|
| 750 | do |
---|
| 751 | { |
---|
[1300] | 752 | vertex_ref++; |
---|
[1270] | 753 | } |
---|
[1300] | 754 | while((*vertex_ref)->parentconditions.find(condition)==(*vertex_ref)->parentconditions.end()); |
---|
[1270] | 755 | |
---|
[1300] | 756 | double product = (*vertex_ref)->get_coordinates()*condition->value; |
---|
| 757 | |
---|
[1301] | 758 | if(should_be_added) |
---|
[1270] | 759 | { |
---|
[1301] | 760 | ((toprow*) horiz_ref)->condition_order++; |
---|
| 761 | |
---|
| 762 | if(product>0) |
---|
| 763 | { |
---|
| 764 | ((toprow*) horiz_ref)->condition_sum += condition->value; |
---|
| 765 | } |
---|
| 766 | else |
---|
| 767 | { |
---|
| 768 | ((toprow*) horiz_ref)->condition_sum -= condition->value; |
---|
| 769 | } |
---|
[1270] | 770 | } |
---|
| 771 | else |
---|
[1301] | 772 | { |
---|
| 773 | ((toprow*) horiz_ref)->condition_order--; |
---|
| 774 | |
---|
| 775 | if(product<0) |
---|
| 776 | { |
---|
| 777 | ((toprow*) horiz_ref)->condition_sum += condition->value; |
---|
| 778 | } |
---|
| 779 | else |
---|
| 780 | { |
---|
| 781 | ((toprow*) horiz_ref)->condition_sum -= condition->value; |
---|
| 782 | } |
---|
[1270] | 783 | } |
---|
| 784 | } |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | |
---|
| 788 | |
---|
[1300] | 789 | void send_state_message(polyhedron* sender, condition *toadd, condition *toremove, int level) |
---|
[1270] | 790 | { |
---|
| 791 | |
---|
[1301] | 792 | bool shouldmerge = (toremove != NULL); |
---|
| 793 | bool shouldsplit = (toadd != NULL); |
---|
[1270] | 794 | |
---|
| 795 | if(shouldsplit||shouldmerge) |
---|
| 796 | { |
---|
| 797 | for(list<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator!=sender->parents.end();parent_iterator++) |
---|
| 798 | { |
---|
| 799 | polyhedron* current_parent = *parent_iterator; |
---|
| 800 | |
---|
| 801 | current_parent->message_counter++; |
---|
| 802 | |
---|
[1300] | 803 | bool is_last = (current_parent->message_counter == current_parent->number_of_children()); |
---|
| 804 | bool is_first = (current_parent->message_counter == 1); |
---|
[1270] | 805 | |
---|
[1356] | 806 | bool out_of_the_game = true; |
---|
| 807 | |
---|
[1270] | 808 | if(shouldmerge) |
---|
| 809 | { |
---|
| 810 | int child_state = sender->get_state(MERGE); |
---|
| 811 | int parent_state = current_parent->get_state(MERGE); |
---|
| 812 | |
---|
[1300] | 813 | if(parent_state == 0||is_first) |
---|
[1270] | 814 | { |
---|
[1300] | 815 | parent_state = current_parent->set_state(child_state, MERGE); |
---|
| 816 | } |
---|
[1270] | 817 | |
---|
[1299] | 818 | if(child_state == 0) |
---|
[1270] | 819 | { |
---|
[1299] | 820 | if(current_parent->mergechild == NULL) |
---|
[1270] | 821 | { |
---|
[1299] | 822 | current_parent->mergechild = sender; |
---|
| 823 | } |
---|
| 824 | } |
---|
[1270] | 825 | |
---|
| 826 | if(is_last) |
---|
[1299] | 827 | { |
---|
[1319] | 828 | if(parent_state == 1) |
---|
| 829 | { |
---|
[1356] | 830 | ((toprow*)current_parent)->condition_sum-=toremove->value; |
---|
[1319] | 831 | } |
---|
| 832 | |
---|
| 833 | if(parent_state == -1) |
---|
| 834 | { |
---|
[1356] | 835 | ((toprow*)current_parent)->condition_sum+=toremove->value; |
---|
[1319] | 836 | } |
---|
[1356] | 837 | |
---|
| 838 | ((toprow*)current_parent)->condition_order--; |
---|
[1319] | 839 | |
---|
[1356] | 840 | |
---|
[1301] | 841 | if(current_parent->mergechild != NULL) |
---|
[1270] | 842 | { |
---|
[1356] | 843 | out_of_the_game = false; |
---|
| 844 | |
---|
[1299] | 845 | if(current_parent->mergechild->get_multiplicity()==1) |
---|
[1270] | 846 | { |
---|
[1299] | 847 | if(parent_state > 0) |
---|
| 848 | { |
---|
| 849 | current_parent->mergechild->positiveparent = current_parent; |
---|
| 850 | } |
---|
[1270] | 851 | |
---|
[1299] | 852 | if(parent_state < 0) |
---|
| 853 | { |
---|
| 854 | current_parent->mergechild->negativeparent = current_parent; |
---|
| 855 | } |
---|
[1356] | 856 | } |
---|
| 857 | else |
---|
| 858 | { |
---|
| 859 | out_of_the_game = true; |
---|
| 860 | } |
---|
[1301] | 861 | } |
---|
[1356] | 862 | |
---|
| 863 | if(out_of_the_game) |
---|
[1301] | 864 | { |
---|
[1307] | 865 | //current_parent->set_state(0,MERGE); |
---|
| 866 | |
---|
[1346] | 867 | if((level == number_of_parameters - 1) && (!shouldsplit)) |
---|
[1307] | 868 | { |
---|
| 869 | toprow* cur_par_toprow = ((toprow*)current_parent); |
---|
| 870 | cur_par_toprow->probability = 0.0; |
---|
[1320] | 871 | |
---|
[1324] | 872 | //set<simplex*> new_triangulation; |
---|
[1320] | 873 | |
---|
[1324] | 874 | for(set<simplex*>::iterator s_ref = current_parent->triangulation.begin();s_ref!=current_parent->triangulation.end();s_ref++) |
---|
[1307] | 875 | { |
---|
[1324] | 876 | double cur_prob = cur_par_toprow->integrate_simplex((*s_ref),'C'); |
---|
[1320] | 877 | |
---|
| 878 | cur_par_toprow->probability += cur_prob; |
---|
| 879 | |
---|
[1324] | 880 | //new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); |
---|
[1320] | 881 | } |
---|
| 882 | |
---|
[1346] | 883 | normalization_factor += cur_par_toprow->probability; |
---|
[1335] | 884 | |
---|
[1324] | 885 | //current_parent->triangulation.clear(); |
---|
| 886 | //current_parent->triangulation.insert(new_triangulation.begin(),new_triangulation.end()); |
---|
[1307] | 887 | } |
---|
[1270] | 888 | } |
---|
| 889 | |
---|
| 890 | if(parent_state == 0) |
---|
| 891 | { |
---|
[1301] | 892 | for_merging[level+1].push_back(current_parent); |
---|
[1346] | 893 | //current_parent->parentconditions.erase(toremove); |
---|
[1301] | 894 | } |
---|
| 895 | |
---|
[1307] | 896 | |
---|
[1299] | 897 | } |
---|
[1270] | 898 | } |
---|
| 899 | |
---|
| 900 | if(shouldsplit) |
---|
[1301] | 901 | { |
---|
| 902 | current_parent->totallyneutralgrandchildren.insert(sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); |
---|
[1338] | 903 | |
---|
[1301] | 904 | for(set<polyhedron*>::iterator tot_child_ref = sender->totallyneutralchildren.begin();tot_child_ref!=sender->totallyneutralchildren.end();tot_child_ref++) |
---|
[1270] | 905 | { |
---|
[1301] | 906 | (*tot_child_ref)->grandparents.insert(current_parent); |
---|
| 907 | } |
---|
[1270] | 908 | |
---|
[1338] | 909 | if(current_parent->totally_neutral == NULL) |
---|
| 910 | { |
---|
| 911 | current_parent->totally_neutral = sender->totally_neutral; |
---|
| 912 | } |
---|
| 913 | else |
---|
| 914 | { |
---|
| 915 | current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; |
---|
| 916 | } |
---|
| 917 | |
---|
[1301] | 918 | switch(sender->get_state(SPLIT)) |
---|
| 919 | { |
---|
| 920 | case 1: |
---|
| 921 | current_parent->positivechildren.push_back(sender); |
---|
| 922 | current_parent->positiveneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
---|
| 923 | break; |
---|
| 924 | case 0: |
---|
| 925 | current_parent->neutralchildren.push_back(sender); |
---|
| 926 | current_parent->positiveneutralvertices.insert(sender->positiveneutralvertices.begin(),sender->positiveneutralvertices.end()); |
---|
[1338] | 927 | current_parent->negativeneutralvertices.insert(sender->negativeneutralvertices.begin(),sender->negativeneutralvertices.end()); |
---|
[1301] | 928 | |
---|
| 929 | if(sender->totally_neutral) |
---|
[1270] | 930 | { |
---|
[1301] | 931 | current_parent->totallyneutralchildren.insert(sender); |
---|
| 932 | } |
---|
[1270] | 933 | |
---|
[1301] | 934 | break; |
---|
| 935 | case -1: |
---|
| 936 | current_parent->negativechildren.push_back(sender); |
---|
| 937 | current_parent->negativeneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
---|
| 938 | break; |
---|
| 939 | } |
---|
[1270] | 940 | |
---|
[1301] | 941 | if(is_last) |
---|
[1338] | 942 | { |
---|
[1310] | 943 | |
---|
[1338] | 944 | if((current_parent->negativechildren.size()>0&¤t_parent->positivechildren.size()>0) |
---|
| 945 | ||(current_parent->neutralchildren.size()>0&¤t_parent->totallyneutralchildren.empty())) |
---|
[1310] | 946 | { |
---|
| 947 | for_splitting[level+1].push_back(current_parent); |
---|
[1270] | 948 | |
---|
[1310] | 949 | current_parent->set_state(0, SPLIT); |
---|
[1301] | 950 | } |
---|
| 951 | else |
---|
| 952 | { |
---|
| 953 | if(current_parent->negativechildren.size()>0) |
---|
| 954 | { |
---|
| 955 | current_parent->set_state(-1, SPLIT); |
---|
[1270] | 956 | |
---|
[1301] | 957 | ((toprow*)current_parent)->condition_sum-=toadd->value; |
---|
[1271] | 958 | |
---|
[1301] | 959 | } |
---|
| 960 | else if(current_parent->positivechildren.size()>0) |
---|
| 961 | { |
---|
| 962 | current_parent->set_state(1, SPLIT); |
---|
[1270] | 963 | |
---|
[1301] | 964 | ((toprow*)current_parent)->condition_sum+=toadd->value; |
---|
| 965 | } |
---|
| 966 | else |
---|
| 967 | { |
---|
[1349] | 968 | current_parent->raise_multiplicity(); |
---|
| 969 | current_parent->totally_neutral = true; |
---|
| 970 | current_parent->parentconditions.insert(toadd); |
---|
[1301] | 971 | } |
---|
[1270] | 972 | |
---|
[1301] | 973 | ((toprow*)current_parent)->condition_order++; |
---|
[1271] | 974 | |
---|
[1346] | 975 | if(level == number_of_parameters - 1 && current_parent->mergechild == NULL) |
---|
[1301] | 976 | { |
---|
| 977 | toprow* cur_par_toprow = ((toprow*)current_parent); |
---|
| 978 | cur_par_toprow->probability = 0.0; |
---|
| 979 | |
---|
[1324] | 980 | //map<double,set<vertex*>> new_triangulation; |
---|
[1320] | 981 | |
---|
[1324] | 982 | for(set<simplex*>::iterator s_ref = current_parent->triangulation.begin();s_ref!=current_parent->triangulation.end();s_ref++) |
---|
[1271] | 983 | { |
---|
[1324] | 984 | double cur_prob = cur_par_toprow->integrate_simplex((*s_ref),'C'); |
---|
[1320] | 985 | |
---|
| 986 | cur_par_toprow->probability += cur_prob; |
---|
| 987 | |
---|
[1324] | 988 | //new_triangulation.insert(pair<double,set<vertex*>>(cur_prob,(*t_ref).second)); |
---|
[1320] | 989 | } |
---|
| 990 | |
---|
[1346] | 991 | normalization_factor += cur_par_toprow->probability; |
---|
[1335] | 992 | |
---|
[1324] | 993 | //current_parent->triangulation.clear(); |
---|
| 994 | //current_parent->triangulation.insert(new_triangulation.begin(),new_triangulation.end()); |
---|
[1301] | 995 | } |
---|
[1271] | 996 | |
---|
[1356] | 997 | if(out_of_the_game) |
---|
[1307] | 998 | { |
---|
| 999 | current_parent->positivechildren.clear(); |
---|
| 1000 | current_parent->negativechildren.clear(); |
---|
| 1001 | current_parent->neutralchildren.clear(); |
---|
[1338] | 1002 | //current_parent->totallyneutralchildren.clear(); |
---|
[1307] | 1003 | current_parent->totallyneutralgrandchildren.clear(); |
---|
| 1004 | // current_parent->grandparents.clear(); |
---|
| 1005 | current_parent->positiveneutralvertices.clear(); |
---|
| 1006 | current_parent->negativeneutralvertices.clear(); |
---|
| 1007 | current_parent->totally_neutral = NULL; |
---|
| 1008 | current_parent->kids_rel_addresses.clear(); |
---|
| 1009 | } |
---|
[1270] | 1010 | } |
---|
| 1011 | } |
---|
[1301] | 1012 | } |
---|
[1270] | 1013 | |
---|
[1301] | 1014 | if(is_last) |
---|
| 1015 | { |
---|
[1307] | 1016 | current_parent->mergechild = NULL; |
---|
| 1017 | current_parent->message_counter = 0; |
---|
| 1018 | |
---|
[1301] | 1019 | send_state_message(current_parent,toadd,toremove,level+1); |
---|
| 1020 | } |
---|
[1270] | 1021 | |
---|
| 1022 | } |
---|
[1338] | 1023 | |
---|
| 1024 | sender->totallyneutralchildren.clear(); |
---|
[1270] | 1025 | } |
---|
| 1026 | } |
---|
| 1027 | |
---|
[1266] | 1028 | public: |
---|
[1272] | 1029 | c_statistic statistic; |
---|
[1266] | 1030 | |
---|
[1282] | 1031 | vertex* minimal_vertex; |
---|
| 1032 | |
---|
[1335] | 1033 | double min_ll; |
---|
[1282] | 1034 | |
---|
[1349] | 1035 | double log_nc; |
---|
| 1036 | |
---|
[1280] | 1037 | vector<multiset<my_ivec>> correction_factors; |
---|
[1266] | 1038 | |
---|
| 1039 | int number_of_parameters; |
---|
| 1040 | |
---|
| 1041 | /// A default constructor creates an emlig with predefined statistic representing only the range of the given |
---|
| 1042 | /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. |
---|
| 1043 | emlig(int number_of_parameters) |
---|
| 1044 | { |
---|
| 1045 | this->number_of_parameters = number_of_parameters; |
---|
| 1046 | |
---|
[1282] | 1047 | create_statistic(number_of_parameters); |
---|
| 1048 | |
---|
[1335] | 1049 | min_ll = numeric_limits<double>::max(); |
---|
[1349] | 1050 | |
---|
| 1051 | condition_order = 0; |
---|
[1266] | 1052 | } |
---|
| 1053 | |
---|
| 1054 | /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a |
---|
| 1055 | /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. |
---|
[1349] | 1056 | emlig(c_statistic statistic, int condition_order) |
---|
[1266] | 1057 | { |
---|
[1282] | 1058 | this->statistic = statistic; |
---|
| 1059 | |
---|
[1335] | 1060 | min_ll = numeric_limits<double>::max(); |
---|
[1349] | 1061 | |
---|
| 1062 | this->condition_order = condition_order; |
---|
[1266] | 1063 | } |
---|
| 1064 | |
---|
[1349] | 1065 | |
---|
[1266] | 1066 | void step_me(int marker) |
---|
| 1067 | { |
---|
[1301] | 1068 | |
---|
[1266] | 1069 | for(int i = 0;i<statistic.size();i++) |
---|
| 1070 | { |
---|
[1324] | 1071 | //int zero = 0; |
---|
| 1072 | //int one = 0; |
---|
| 1073 | //int two = 0; |
---|
[1319] | 1074 | |
---|
[1266] | 1075 | for(polyhedron* horiz_ref = statistic.rows[i];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
| 1076 | { |
---|
[1301] | 1077 | |
---|
[1338] | 1078 | /* |
---|
[1275] | 1079 | if(i==statistic.size()-1) |
---|
| 1080 | { |
---|
[1324] | 1081 | cout << ((toprow*)horiz_ref)->condition_sum << " " << ((toprow*)horiz_ref)->probability << endl; |
---|
[1275] | 1082 | cout << "Order:" << ((toprow*)horiz_ref)->condition_order << endl; |
---|
| 1083 | } |
---|
[1338] | 1084 | */ |
---|
| 1085 | |
---|
[1349] | 1086 | // cout << "Stepped." << endl; |
---|
| 1087 | |
---|
[1356] | 1088 | if(marker==101) |
---|
| 1089 | { |
---|
| 1090 | 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()) |
---|
| 1091 | { |
---|
| 1092 | cout << "Cleaning error!" << endl; |
---|
| 1093 | } |
---|
| 1094 | |
---|
| 1095 | } |
---|
| 1096 | |
---|
[1349] | 1097 | for(set<simplex*>::iterator sim_ref = (*horiz_ref).triangulation.begin();sim_ref!=(*horiz_ref).triangulation.end();sim_ref++) |
---|
| 1098 | { |
---|
| 1099 | if((*sim_ref)->vertices.size()!=i+1) |
---|
| 1100 | { |
---|
| 1101 | cout << "Something is wrong." << endl; |
---|
| 1102 | } |
---|
| 1103 | } |
---|
[1324] | 1104 | |
---|
| 1105 | /* |
---|
[1301] | 1106 | if(i==0) |
---|
| 1107 | { |
---|
| 1108 | cout << ((vertex*)horiz_ref)->get_coordinates() << endl; |
---|
| 1109 | } |
---|
[1310] | 1110 | */ |
---|
[1301] | 1111 | |
---|
[1324] | 1112 | /* |
---|
[1266] | 1113 | char* string = "Checkpoint"; |
---|
[1319] | 1114 | |
---|
[1324] | 1115 | |
---|
[1319] | 1116 | if((*horiz_ref).parentconditions.size()==0) |
---|
| 1117 | { |
---|
| 1118 | zero++; |
---|
| 1119 | } |
---|
| 1120 | else if((*horiz_ref).parentconditions.size()==1) |
---|
| 1121 | { |
---|
| 1122 | one++; |
---|
| 1123 | } |
---|
| 1124 | else |
---|
| 1125 | { |
---|
| 1126 | two++; |
---|
| 1127 | } |
---|
[1324] | 1128 | */ |
---|
[1319] | 1129 | |
---|
[1266] | 1130 | } |
---|
| 1131 | } |
---|
[1301] | 1132 | |
---|
[1282] | 1133 | |
---|
| 1134 | /* |
---|
| 1135 | list<vec> table_entries; |
---|
| 1136 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.row_ends[statistic.size()-1];horiz_ref=horiz_ref->next_poly) |
---|
| 1137 | { |
---|
| 1138 | toprow *current_toprow = (toprow*)(horiz_ref); |
---|
| 1139 | for(list<set<vertex*>>::iterator tri_ref = current_toprow->triangulation.begin();tri_ref!=current_toprow->triangulation.end();tri_ref++) |
---|
| 1140 | { |
---|
| 1141 | for(set<vertex*>::iterator vert_ref = (*tri_ref).begin();vert_ref!=(*tri_ref).end();vert_ref++) |
---|
| 1142 | { |
---|
| 1143 | vec table_entry = vec(); |
---|
| 1144 | |
---|
| 1145 | table_entry.ins(0,(*vert_ref)->get_coordinates()*current_toprow->condition.get(1,current_toprow->condition.size()-1)-current_toprow->condition.get(0,0)); |
---|
| 1146 | |
---|
| 1147 | table_entry.ins(0,(*vert_ref)->get_coordinates()); |
---|
| 1148 | |
---|
| 1149 | table_entries.push_back(table_entry); |
---|
| 1150 | } |
---|
| 1151 | } |
---|
| 1152 | } |
---|
| 1153 | |
---|
| 1154 | unique(table_entries.begin(),table_entries.end()); |
---|
| 1155 | |
---|
| 1156 | |
---|
| 1157 | |
---|
| 1158 | for(list<vec>::iterator entry_ref = table_entries.begin();entry_ref!=table_entries.end();entry_ref++) |
---|
| 1159 | { |
---|
| 1160 | ofstream myfile; |
---|
| 1161 | myfile.open("robust_data.txt", ios::out | ios::app); |
---|
| 1162 | if (myfile.is_open()) |
---|
| 1163 | { |
---|
| 1164 | for(int i = 0;i<(*entry_ref).size();i++) |
---|
| 1165 | { |
---|
| 1166 | myfile << (*entry_ref)[i] << ";"; |
---|
| 1167 | } |
---|
| 1168 | myfile << endl; |
---|
| 1169 | |
---|
| 1170 | myfile.close(); |
---|
| 1171 | } |
---|
| 1172 | else |
---|
| 1173 | { |
---|
| 1174 | cout << "File problem." << endl; |
---|
| 1175 | } |
---|
| 1176 | } |
---|
| 1177 | */ |
---|
| 1178 | |
---|
| 1179 | |
---|
| 1180 | return; |
---|
[1266] | 1181 | } |
---|
| 1182 | |
---|
| 1183 | int statistic_rowsize(int row) |
---|
| 1184 | { |
---|
| 1185 | return statistic.row_size(row); |
---|
| 1186 | } |
---|
| 1187 | |
---|
| 1188 | void add_condition(vec toadd) |
---|
| 1189 | { |
---|
| 1190 | vec null_vector = ""; |
---|
| 1191 | |
---|
| 1192 | add_and_remove_condition(toadd, null_vector); |
---|
| 1193 | } |
---|
| 1194 | |
---|
| 1195 | |
---|
| 1196 | void remove_condition(vec toremove) |
---|
| 1197 | { |
---|
| 1198 | vec null_vector = ""; |
---|
| 1199 | |
---|
[1346] | 1200 | add_and_remove_condition(null_vector, toremove); |
---|
[1266] | 1201 | } |
---|
| 1202 | |
---|
| 1203 | void add_and_remove_condition(vec toadd, vec toremove) |
---|
| 1204 | { |
---|
[1324] | 1205 | //step_me(0); |
---|
[1335] | 1206 | normalization_factor = 0; |
---|
| 1207 | min_ll = numeric_limits<double>::max(); |
---|
[1282] | 1208 | |
---|
[1266] | 1209 | bool should_remove = (toremove.size() != 0); |
---|
| 1210 | bool should_add = (toadd.size() != 0); |
---|
| 1211 | |
---|
[1349] | 1212 | if(should_remove) |
---|
| 1213 | { |
---|
| 1214 | condition_order--; |
---|
| 1215 | } |
---|
| 1216 | |
---|
| 1217 | if(should_add) |
---|
| 1218 | { |
---|
| 1219 | condition_order++; |
---|
| 1220 | } |
---|
| 1221 | |
---|
[1266] | 1222 | for_splitting.clear(); |
---|
| 1223 | for_merging.clear(); |
---|
| 1224 | |
---|
| 1225 | for(int i = 0;i<statistic.size();i++) |
---|
| 1226 | { |
---|
| 1227 | list<polyhedron*> empty_split; |
---|
| 1228 | list<polyhedron*> empty_merge; |
---|
| 1229 | |
---|
| 1230 | for_splitting.push_back(empty_split); |
---|
| 1231 | for_merging.push_back(empty_merge); |
---|
| 1232 | } |
---|
| 1233 | |
---|
| 1234 | list<condition*>::iterator toremove_ref = conditions.end(); |
---|
[1301] | 1235 | bool condition_should_be_added = should_add; |
---|
[1266] | 1236 | |
---|
| 1237 | for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++) |
---|
| 1238 | { |
---|
| 1239 | if(should_remove) |
---|
| 1240 | { |
---|
| 1241 | if((*ref)->value == toremove) |
---|
| 1242 | { |
---|
| 1243 | if((*ref)->multiplicity>1) |
---|
| 1244 | { |
---|
| 1245 | (*ref)->multiplicity--; |
---|
| 1246 | |
---|
[1300] | 1247 | alter_toprow_conditions(*ref,false); |
---|
[1266] | 1248 | |
---|
| 1249 | should_remove = false; |
---|
| 1250 | } |
---|
| 1251 | else |
---|
| 1252 | { |
---|
| 1253 | toremove_ref = ref; |
---|
| 1254 | } |
---|
| 1255 | } |
---|
| 1256 | } |
---|
| 1257 | |
---|
| 1258 | if(should_add) |
---|
| 1259 | { |
---|
| 1260 | if((*ref)->value == toadd) |
---|
| 1261 | { |
---|
| 1262 | (*ref)->multiplicity++; |
---|
| 1263 | |
---|
[1300] | 1264 | alter_toprow_conditions(*ref,true); |
---|
[1266] | 1265 | |
---|
| 1266 | should_add = false; |
---|
[1301] | 1267 | |
---|
| 1268 | condition_should_be_added = false; |
---|
| 1269 | } |
---|
[1266] | 1270 | } |
---|
[1301] | 1271 | } |
---|
[1266] | 1272 | |
---|
[1300] | 1273 | condition* condition_to_remove = NULL; |
---|
| 1274 | |
---|
[1266] | 1275 | if(toremove_ref!=conditions.end()) |
---|
| 1276 | { |
---|
[1300] | 1277 | condition_to_remove = *toremove_ref; |
---|
[1301] | 1278 | conditions.erase(toremove_ref); |
---|
[1266] | 1279 | } |
---|
| 1280 | |
---|
[1300] | 1281 | condition* condition_to_add = NULL; |
---|
| 1282 | |
---|
[1266] | 1283 | if(condition_should_be_added) |
---|
| 1284 | { |
---|
[1301] | 1285 | condition* new_condition = new condition(toadd); |
---|
| 1286 | |
---|
| 1287 | conditions.push_back(new_condition); |
---|
| 1288 | condition_to_add = new_condition; |
---|
| 1289 | } |
---|
[1266] | 1290 | |
---|
| 1291 | for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly) |
---|
| 1292 | { |
---|
| 1293 | vertex* current_vertex = (vertex*)horizontal_position; |
---|
| 1294 | |
---|
| 1295 | if(should_add||should_remove) |
---|
| 1296 | { |
---|
[1300] | 1297 | vec appended_coords = current_vertex->get_coordinates(); |
---|
| 1298 | appended_coords.ins(0,-1.0); |
---|
[1266] | 1299 | |
---|
| 1300 | if(should_add) |
---|
| 1301 | { |
---|
[1300] | 1302 | double local_condition = 0;// = toadd*(appended_coords.first/=appended_coords.second); |
---|
[1266] | 1303 | |
---|
[1300] | 1304 | local_condition = appended_coords*toadd; |
---|
| 1305 | |
---|
[1356] | 1306 | // cout << "Vertex multiplicity: "<< current_vertex->get_multiplicity() << endl; |
---|
[1349] | 1307 | |
---|
[1266] | 1308 | current_vertex->set_state(local_condition,SPLIT); |
---|
| 1309 | |
---|
[1300] | 1310 | /// \TODO There should be a rounding error tolerance used here to insure we are not having too many points because of rounding error. |
---|
[1266] | 1311 | if(local_condition == 0) |
---|
| 1312 | { |
---|
[1349] | 1313 | cout << "Condition to add: " << toadd << endl; |
---|
| 1314 | cout << "Vertex coords: " << appended_coords << endl; |
---|
| 1315 | |
---|
[1266] | 1316 | current_vertex->totally_neutral = true; |
---|
| 1317 | |
---|
| 1318 | current_vertex->raise_multiplicity(); |
---|
[1349] | 1319 | current_vertex->parentconditions.insert(condition_to_add); |
---|
[1266] | 1320 | |
---|
| 1321 | current_vertex->negativeneutralvertices.insert(current_vertex); |
---|
| 1322 | current_vertex->positiveneutralvertices.insert(current_vertex); |
---|
[1338] | 1323 | } |
---|
| 1324 | else |
---|
| 1325 | { |
---|
| 1326 | current_vertex->totally_neutral = false; |
---|
| 1327 | } |
---|
[1266] | 1328 | } |
---|
| 1329 | |
---|
| 1330 | if(should_remove) |
---|
[1300] | 1331 | { |
---|
| 1332 | set<condition*>::iterator cond_ref; |
---|
| 1333 | |
---|
| 1334 | for(cond_ref = current_vertex->parentconditions.begin();cond_ref!=current_vertex->parentconditions.end();cond_ref++) |
---|
| 1335 | { |
---|
| 1336 | if(*cond_ref == condition_to_remove) |
---|
| 1337 | { |
---|
| 1338 | break; |
---|
| 1339 | } |
---|
| 1340 | } |
---|
[1266] | 1341 | |
---|
[1300] | 1342 | if(cond_ref!=current_vertex->parentconditions.end()) |
---|
[1266] | 1343 | { |
---|
[1300] | 1344 | current_vertex->parentconditions.erase(cond_ref); |
---|
| 1345 | current_vertex->set_state(0,MERGE); |
---|
[1266] | 1346 | for_merging[0].push_back(current_vertex); |
---|
| 1347 | } |
---|
[1300] | 1348 | else |
---|
| 1349 | { |
---|
| 1350 | double local_condition = toremove*appended_coords; |
---|
| 1351 | current_vertex->set_state(local_condition,MERGE); |
---|
| 1352 | } |
---|
[1266] | 1353 | } |
---|
| 1354 | } |
---|
| 1355 | |
---|
[1300] | 1356 | send_state_message(current_vertex, condition_to_add, condition_to_remove, 0); |
---|
[1266] | 1357 | |
---|
| 1358 | } |
---|
| 1359 | |
---|
[1356] | 1360 | // step_me(1); |
---|
[1300] | 1361 | |
---|
[1299] | 1362 | if(should_remove) |
---|
| 1363 | { |
---|
[1338] | 1364 | /* |
---|
[1300] | 1365 | for(int i = 0;i<for_merging.size();i++) |
---|
| 1366 | { |
---|
| 1367 | for(list<polyhedron*>::iterator merge_ref = for_merging[i].begin();merge_ref!=for_merging[i].end();merge_ref++) |
---|
| 1368 | { |
---|
| 1369 | cout << (*merge_ref)->get_state(MERGE) << ","; |
---|
| 1370 | } |
---|
| 1371 | |
---|
| 1372 | cout << endl; |
---|
| 1373 | } |
---|
[1338] | 1374 | */ |
---|
[1300] | 1375 | |
---|
[1338] | 1376 | cout << "Merging." << endl; |
---|
| 1377 | |
---|
[1299] | 1378 | set<vertex*> vertices_to_be_reduced; |
---|
| 1379 | |
---|
| 1380 | int k = 1; |
---|
| 1381 | |
---|
| 1382 | for(vector<list<polyhedron*>>::iterator vert_ref = for_merging.begin();vert_ref<for_merging.end();vert_ref++) |
---|
| 1383 | { |
---|
| 1384 | for(list<polyhedron*>::reverse_iterator merge_ref = vert_ref->rbegin();merge_ref!=vert_ref->rend();merge_ref++) |
---|
| 1385 | { |
---|
| 1386 | if((*merge_ref)->get_multiplicity()>1) |
---|
| 1387 | { |
---|
[1349] | 1388 | (*merge_ref)->parentconditions.erase(condition_to_remove); |
---|
| 1389 | |
---|
[1299] | 1390 | if(k==1) |
---|
| 1391 | { |
---|
| 1392 | vertices_to_be_reduced.insert((vertex*)(*merge_ref)); |
---|
| 1393 | } |
---|
| 1394 | else |
---|
| 1395 | { |
---|
| 1396 | (*merge_ref)->lower_multiplicity(); |
---|
[1356] | 1397 | } |
---|
| 1398 | |
---|
| 1399 | if((*merge_ref)->get_state(SPLIT)!=0||(*merge_ref)->totally_neutral) |
---|
| 1400 | { |
---|
| 1401 | (*merge_ref)->positivechildren.clear(); |
---|
| 1402 | (*merge_ref)->negativechildren.clear(); |
---|
| 1403 | (*merge_ref)->neutralchildren.clear(); |
---|
| 1404 | (*merge_ref)->totallyneutralgrandchildren.clear(); |
---|
| 1405 | (*merge_ref)->positiveneutralvertices.clear(); |
---|
| 1406 | (*merge_ref)->negativeneutralvertices.clear(); |
---|
| 1407 | (*merge_ref)->totally_neutral = NULL; |
---|
| 1408 | (*merge_ref)->kids_rel_addresses.clear(); |
---|
[1299] | 1409 | } |
---|
| 1410 | } |
---|
| 1411 | else |
---|
| 1412 | { |
---|
[1346] | 1413 | bool will_be_split = false; |
---|
| 1414 | |
---|
[1299] | 1415 | toprow* current_positive = (toprow*)(*merge_ref)->positiveparent; |
---|
| 1416 | toprow* current_negative = (toprow*)(*merge_ref)->negativeparent; |
---|
| 1417 | |
---|
[1356] | 1418 | if(current_positive->totally_neutral!=current_negative->totally_neutral) |
---|
| 1419 | { |
---|
| 1420 | throw new exception("Both polyhedrons must be totally neutral if they should be merged!"); |
---|
| 1421 | } |
---|
| 1422 | |
---|
[1319] | 1423 | //current_positive->condition_sum -= toremove; |
---|
| 1424 | //current_positive->condition_order--; |
---|
| 1425 | |
---|
| 1426 | current_positive->parentconditions.erase(condition_to_remove); |
---|
[1299] | 1427 | |
---|
| 1428 | current_positive->children.insert(current_positive->children.end(),current_negative->children.begin(),current_negative->children.end()); |
---|
| 1429 | current_positive->children.remove(*merge_ref); |
---|
| 1430 | |
---|
| 1431 | for(list<polyhedron*>::iterator child_ref = current_negative->children.begin();child_ref!=current_negative->children.end();child_ref++) |
---|
| 1432 | { |
---|
| 1433 | (*child_ref)->parents.remove(current_negative); |
---|
[1301] | 1434 | (*child_ref)->parents.push_back(current_positive); |
---|
[1299] | 1435 | } |
---|
[1310] | 1436 | |
---|
| 1437 | // current_positive->parents.insert(current_positive->parents.begin(),current_negative->parents.begin(),current_negative->parents.end()); |
---|
| 1438 | // unique(current_positive->parents.begin(),current_positive->parents.end()); |
---|
| 1439 | |
---|
| 1440 | for(list<polyhedron*>::iterator parent_ref = current_negative->parents.begin();parent_ref!=current_negative->parents.end();parent_ref++) |
---|
| 1441 | { |
---|
| 1442 | (*parent_ref)->children.remove(current_negative); |
---|
| 1443 | |
---|
| 1444 | switch(current_negative->get_state(SPLIT)) |
---|
| 1445 | { |
---|
| 1446 | case -1: |
---|
| 1447 | (*parent_ref)->negativechildren.remove(current_negative); |
---|
| 1448 | break; |
---|
| 1449 | case 0: |
---|
| 1450 | (*parent_ref)->neutralchildren.remove(current_negative); |
---|
| 1451 | break; |
---|
| 1452 | case 1: |
---|
| 1453 | (*parent_ref)->positivechildren.remove(current_negative); |
---|
| 1454 | break; |
---|
| 1455 | } |
---|
| 1456 | //(*parent_ref)->children.push_back(current_positive); |
---|
| 1457 | } |
---|
| 1458 | |
---|
[1319] | 1459 | if(current_positive->get_state(SPLIT)!=0&¤t_negative->get_state(SPLIT)==0) |
---|
[1310] | 1460 | { |
---|
| 1461 | for(list<polyhedron*>::iterator parent_ref = current_positive->parents.begin();parent_ref!=current_positive->parents.end();parent_ref++) |
---|
| 1462 | { |
---|
| 1463 | if(current_positive->get_state(SPLIT)==1) |
---|
| 1464 | { |
---|
| 1465 | (*parent_ref)->positivechildren.remove(current_positive); |
---|
| 1466 | } |
---|
| 1467 | else |
---|
| 1468 | { |
---|
| 1469 | (*parent_ref)->negativechildren.remove(current_positive); |
---|
| 1470 | } |
---|
| 1471 | |
---|
| 1472 | (*parent_ref)->neutralchildren.push_back(current_positive); |
---|
| 1473 | } |
---|
| 1474 | |
---|
| 1475 | current_positive->set_state(0,SPLIT); |
---|
[1318] | 1476 | for_splitting[k].push_back(current_positive); |
---|
[1356] | 1477 | |
---|
[1346] | 1478 | will_be_split = true; |
---|
[1310] | 1479 | } |
---|
[1299] | 1480 | |
---|
[1307] | 1481 | if((current_positive->get_state(SPLIT)==0&&!current_positive->totally_neutral)||(current_negative->get_state(SPLIT)==0&&!current_negative->totally_neutral)) |
---|
| 1482 | { |
---|
| 1483 | current_positive->negativechildren.insert(current_positive->negativechildren.end(),current_negative->negativechildren.begin(),current_negative->negativechildren.end()); |
---|
| 1484 | |
---|
| 1485 | current_positive->positivechildren.insert(current_positive->positivechildren.end(),current_negative->positivechildren.begin(),current_negative->positivechildren.end()); |
---|
| 1486 | |
---|
| 1487 | current_positive->neutralchildren.insert(current_positive->neutralchildren.end(),current_negative->neutralchildren.begin(),current_negative->neutralchildren.end()); |
---|
[1299] | 1488 | |
---|
[1307] | 1489 | switch((*merge_ref)->get_state(SPLIT)) |
---|
| 1490 | { |
---|
| 1491 | case -1: |
---|
| 1492 | current_positive->negativechildren.remove(*merge_ref); |
---|
| 1493 | break; |
---|
| 1494 | case 0: |
---|
| 1495 | current_positive->neutralchildren.remove(*merge_ref); |
---|
| 1496 | break; |
---|
| 1497 | case 1: |
---|
| 1498 | current_positive->positivechildren.remove(*merge_ref); |
---|
| 1499 | break; |
---|
| 1500 | } |
---|
| 1501 | |
---|
[1338] | 1502 | /* |
---|
[1307] | 1503 | current_positive->totallyneutralchildren.insert(current_negative->totallyneutralchildren.begin(),current_negative->totallyneutralchildren.end()); |
---|
| 1504 | |
---|
| 1505 | current_positive->totallyneutralchildren.erase(*merge_ref); |
---|
[1338] | 1506 | */ |
---|
[1307] | 1507 | |
---|
| 1508 | current_positive->totallyneutralgrandchildren.insert(current_negative->totallyneutralgrandchildren.begin(),current_negative->totallyneutralgrandchildren.end()); |
---|
| 1509 | |
---|
| 1510 | current_positive->negativeneutralvertices.insert(current_negative->negativeneutralvertices.begin(),current_negative->negativeneutralvertices.end()); |
---|
| 1511 | current_positive->positiveneutralvertices.insert(current_negative->positiveneutralvertices.begin(),current_negative->positiveneutralvertices.end()); |
---|
[1346] | 1512 | |
---|
| 1513 | will_be_split = true; |
---|
[1307] | 1514 | } |
---|
| 1515 | else |
---|
[1356] | 1516 | { |
---|
| 1517 | current_positive->positivechildren.clear(); |
---|
| 1518 | current_positive->negativechildren.clear(); |
---|
| 1519 | current_positive->neutralchildren.clear(); |
---|
| 1520 | // current_positive->totallyneutralchildren.clear(); |
---|
| 1521 | current_positive->totallyneutralgrandchildren.clear(); |
---|
| 1522 | current_positive->positiveneutralvertices.clear(); |
---|
| 1523 | current_positive->negativeneutralvertices.clear(); |
---|
| 1524 | current_positive->totally_neutral = NULL; |
---|
| 1525 | current_positive->kids_rel_addresses.clear(); |
---|
| 1526 | } |
---|
[1307] | 1527 | |
---|
[1299] | 1528 | current_positive->vertices.insert(current_negative->vertices.begin(),current_negative->vertices.end()); |
---|
| 1529 | |
---|
[1307] | 1530 | |
---|
[1299] | 1531 | for(set<vertex*>::iterator vert_ref = (*merge_ref)->vertices.begin();vert_ref!=(*merge_ref)->vertices.end();vert_ref++) |
---|
| 1532 | { |
---|
| 1533 | if((*vert_ref)->get_multiplicity()==1) |
---|
| 1534 | { |
---|
| 1535 | current_positive->vertices.erase(*vert_ref); |
---|
[1307] | 1536 | |
---|
[1356] | 1537 | if(will_be_split) |
---|
[1307] | 1538 | { |
---|
| 1539 | current_positive->negativeneutralvertices.erase(*vert_ref); |
---|
| 1540 | current_positive->positiveneutralvertices.erase(*vert_ref); |
---|
| 1541 | } |
---|
[1299] | 1542 | } |
---|
| 1543 | } |
---|
| 1544 | |
---|
| 1545 | if(current_negative->get_state(SPLIT)==0&&!current_negative->totally_neutral) |
---|
| 1546 | { |
---|
[1318] | 1547 | for_splitting[k].remove(current_negative); |
---|
[1299] | 1548 | } |
---|
| 1549 | |
---|
[1356] | 1550 | |
---|
| 1551 | |
---|
[1299] | 1552 | if(current_positive->totally_neutral) |
---|
| 1553 | { |
---|
[1356] | 1554 | for(set<polyhedron*>::iterator grand_ref = current_negative->grandparents.begin();grand_ref!=current_negative->grandparents.end();grand_ref++) |
---|
[1299] | 1555 | { |
---|
[1356] | 1556 | (*grand_ref)->totallyneutralgrandchildren.erase(current_negative); |
---|
| 1557 | (*grand_ref)->totallyneutralgrandchildren.insert(current_positive); |
---|
[1299] | 1558 | } |
---|
[1356] | 1559 | } |
---|
[1299] | 1560 | |
---|
[1301] | 1561 | current_positive->grandparents.clear(); |
---|
[1356] | 1562 | |
---|
[1346] | 1563 | normalization_factor += current_positive->triangulate(k==for_splitting.size()-1 && !will_be_split); |
---|
[1299] | 1564 | |
---|
| 1565 | statistic.delete_polyhedron(k,current_negative); |
---|
| 1566 | |
---|
| 1567 | delete current_negative; |
---|
| 1568 | |
---|
| 1569 | for(list<polyhedron*>::iterator child_ref = (*merge_ref)->children.begin();child_ref!=(*merge_ref)->children.end();child_ref++) |
---|
| 1570 | { |
---|
| 1571 | (*child_ref)->parents.remove(*merge_ref); |
---|
| 1572 | } |
---|
| 1573 | |
---|
[1307] | 1574 | /* |
---|
[1299] | 1575 | for(list<polyhedron*>::iterator parent_ref = (*merge_ref)->parents.begin();parent_ref!=(*merge_ref)->parents.end();parent_ref++) |
---|
| 1576 | { |
---|
| 1577 | (*parent_ref)->positivechildren.remove(*merge_ref); |
---|
| 1578 | (*parent_ref)->negativechildren.remove(*merge_ref); |
---|
| 1579 | (*parent_ref)->neutralchildren.remove(*merge_ref); |
---|
| 1580 | (*parent_ref)->children.remove(*merge_ref); |
---|
| 1581 | } |
---|
[1307] | 1582 | */ |
---|
[1299] | 1583 | |
---|
| 1584 | for(set<polyhedron*>::iterator grand_ch_ref = (*merge_ref)->totallyneutralgrandchildren.begin();grand_ch_ref!=(*merge_ref)->totallyneutralgrandchildren.end();grand_ch_ref++) |
---|
| 1585 | { |
---|
| 1586 | (*grand_ch_ref)->grandparents.erase(*merge_ref); |
---|
| 1587 | } |
---|
| 1588 | |
---|
[1301] | 1589 | |
---|
[1299] | 1590 | for(set<polyhedron*>::iterator grand_p_ref = (*merge_ref)->grandparents.begin();grand_p_ref!=(*merge_ref)->grandparents.end();grand_p_ref++) |
---|
| 1591 | { |
---|
| 1592 | (*grand_p_ref)->totallyneutralgrandchildren.erase(*merge_ref); |
---|
| 1593 | } |
---|
[1301] | 1594 | |
---|
[1307] | 1595 | for_splitting[k-1].remove(*merge_ref); |
---|
[1299] | 1596 | |
---|
| 1597 | statistic.delete_polyhedron(k-1,*merge_ref); |
---|
| 1598 | |
---|
| 1599 | if(k==1) |
---|
| 1600 | { |
---|
| 1601 | vertices_to_be_reduced.insert((vertex*)(*merge_ref)); |
---|
| 1602 | } |
---|
| 1603 | else |
---|
| 1604 | { |
---|
| 1605 | delete *merge_ref; |
---|
| 1606 | } |
---|
| 1607 | } |
---|
| 1608 | } |
---|
| 1609 | |
---|
| 1610 | k++; |
---|
| 1611 | |
---|
| 1612 | } |
---|
| 1613 | |
---|
| 1614 | for(set<vertex*>::iterator vert_ref = vertices_to_be_reduced.begin();vert_ref!=vertices_to_be_reduced.end();vert_ref++) |
---|
| 1615 | { |
---|
| 1616 | if((*vert_ref)->get_multiplicity()>1) |
---|
| 1617 | { |
---|
| 1618 | (*vert_ref)->lower_multiplicity(); |
---|
| 1619 | } |
---|
| 1620 | else |
---|
| 1621 | { |
---|
| 1622 | delete *vert_ref; |
---|
| 1623 | } |
---|
[1319] | 1624 | } |
---|
| 1625 | |
---|
| 1626 | delete condition_to_remove; |
---|
[1299] | 1627 | } |
---|
[1300] | 1628 | |
---|
[1349] | 1629 | |
---|
[1319] | 1630 | vector<int> sizevector; |
---|
| 1631 | for(int s = 0;s<statistic.size();s++) |
---|
| 1632 | { |
---|
| 1633 | sizevector.push_back(statistic.row_size(s)); |
---|
| 1634 | cout << statistic.row_size(s) << ", "; |
---|
[1349] | 1635 | } |
---|
[1343] | 1636 | |
---|
[1299] | 1637 | |
---|
[1356] | 1638 | cout << endl; |
---|
[1319] | 1639 | |
---|
[1356] | 1640 | if(should_add) |
---|
[1349] | 1641 | { |
---|
[1356] | 1642 | cout << "Splitting." << endl; |
---|
[1338] | 1643 | |
---|
[1266] | 1644 | int k = 1; |
---|
[1356] | 1645 | int counter = 0; |
---|
[1266] | 1646 | |
---|
| 1647 | vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin(); |
---|
| 1648 | |
---|
| 1649 | for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++) |
---|
| 1650 | { |
---|
| 1651 | |
---|
| 1652 | for(list<polyhedron*>::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++) |
---|
| 1653 | { |
---|
[1356] | 1654 | counter++; |
---|
| 1655 | |
---|
[1266] | 1656 | polyhedron* new_totally_neutral_child; |
---|
| 1657 | |
---|
| 1658 | polyhedron* current_polyhedron = (*split_ref); |
---|
| 1659 | |
---|
| 1660 | if(vert_ref == beginning_ref) |
---|
| 1661 | { |
---|
| 1662 | vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates(); |
---|
[1271] | 1663 | vec coordinates2 = ((vertex*)(*(++current_polyhedron->children.begin())))->get_coordinates(); |
---|
[1266] | 1664 | |
---|
[1271] | 1665 | vec extended_coord2 = coordinates2; |
---|
[1300] | 1666 | extended_coord2.ins(0,-1.0); |
---|
[1266] | 1667 | |
---|
[1300] | 1668 | double t = (-toadd*extended_coord2)/(toadd(1,toadd.size()-1)*(coordinates1-coordinates2)); |
---|
[1266] | 1669 | |
---|
[1300] | 1670 | vec new_coordinates = (1-t)*coordinates2+t*coordinates1; |
---|
[1271] | 1671 | |
---|
| 1672 | // cout << "c1:" << coordinates1 << endl << "c2:" << coordinates2 << endl << "nc:" << new_coordinates << endl; |
---|
| 1673 | |
---|
[1269] | 1674 | vertex* neutral_vertex = new vertex(new_coordinates); |
---|
[1266] | 1675 | |
---|
| 1676 | new_totally_neutral_child = neutral_vertex; |
---|
| 1677 | } |
---|
| 1678 | else |
---|
| 1679 | { |
---|
[1270] | 1680 | toprow* neutral_toprow = new toprow(); |
---|
[1299] | 1681 | |
---|
[1300] | 1682 | neutral_toprow->condition_sum = ((toprow*)current_polyhedron)->condition_sum; // tohle tu bylo driv: zeros(number_of_parameters+1); |
---|
[1270] | 1683 | neutral_toprow->condition_order = ((toprow*)current_polyhedron)->condition_order+1; |
---|
| 1684 | |
---|
[1266] | 1685 | new_totally_neutral_child = neutral_toprow; |
---|
| 1686 | } |
---|
[1269] | 1687 | |
---|
[1301] | 1688 | new_totally_neutral_child->parentconditions.insert(current_polyhedron->parentconditions.begin(),current_polyhedron->parentconditions.end()); |
---|
| 1689 | new_totally_neutral_child->parentconditions.insert(condition_to_add); |
---|
| 1690 | |
---|
[1269] | 1691 | new_totally_neutral_child->my_emlig = this; |
---|
[1266] | 1692 | |
---|
| 1693 | new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(), |
---|
| 1694 | current_polyhedron->totallyneutralgrandchildren.begin(), |
---|
| 1695 | current_polyhedron->totallyneutralgrandchildren.end()); |
---|
| 1696 | |
---|
[1299] | 1697 | |
---|
[1266] | 1698 | |
---|
[1270] | 1699 | // cout << ((toprow*)current_polyhedron)->condition << endl << toadd << endl; |
---|
[1269] | 1700 | |
---|
[1300] | 1701 | toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition_sum+toadd, ((toprow*)current_polyhedron)->condition_order+1); |
---|
| 1702 | toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition_sum-toadd, ((toprow*)current_polyhedron)->condition_order+1); |
---|
[1266] | 1703 | |
---|
[1268] | 1704 | positive_poly->my_emlig = this; |
---|
| 1705 | negative_poly->my_emlig = this; |
---|
| 1706 | |
---|
[1319] | 1707 | positive_poly->parentconditions.insert(current_polyhedron->parentconditions.begin(),current_polyhedron->parentconditions.end()); |
---|
| 1708 | negative_poly->parentconditions.insert(current_polyhedron->parentconditions.begin(),current_polyhedron->parentconditions.end()); |
---|
| 1709 | |
---|
[1299] | 1710 | for(set<polyhedron*>::iterator grand_ref = current_polyhedron->totallyneutralgrandchildren.begin(); grand_ref != current_polyhedron->totallyneutralgrandchildren.end();grand_ref++) |
---|
| 1711 | { |
---|
| 1712 | (*grand_ref)->parents.push_back(new_totally_neutral_child); |
---|
[1301] | 1713 | |
---|
| 1714 | // tohle tu nebylo. ma to tu byt? |
---|
| 1715 | //positive_poly->totallyneutralgrandchildren.insert(*grand_ref); |
---|
| 1716 | //negative_poly->totallyneutralgrandchildren.insert(*grand_ref); |
---|
[1299] | 1717 | |
---|
[1301] | 1718 | //(*grand_ref)->grandparents.insert(positive_poly); |
---|
| 1719 | //(*grand_ref)->grandparents.insert(negative_poly); |
---|
| 1720 | |
---|
[1299] | 1721 | new_totally_neutral_child->vertices.insert((*grand_ref)->vertices.begin(),(*grand_ref)->vertices.end()); |
---|
| 1722 | } |
---|
| 1723 | |
---|
| 1724 | positive_poly->children.push_back(new_totally_neutral_child); |
---|
| 1725 | negative_poly->children.push_back(new_totally_neutral_child); |
---|
| 1726 | |
---|
| 1727 | |
---|
[1266] | 1728 | for(list<polyhedron*>::iterator parent_ref = current_polyhedron->parents.begin();parent_ref!=current_polyhedron->parents.end();parent_ref++) |
---|
| 1729 | { |
---|
[1299] | 1730 | (*parent_ref)->totallyneutralgrandchildren.insert(new_totally_neutral_child); |
---|
[1301] | 1731 | // new_totally_neutral_child->grandparents.insert(*parent_ref); |
---|
[1266] | 1732 | |
---|
| 1733 | (*parent_ref)->neutralchildren.remove(current_polyhedron); |
---|
| 1734 | (*parent_ref)->children.remove(current_polyhedron); |
---|
| 1735 | |
---|
| 1736 | (*parent_ref)->children.push_back(positive_poly); |
---|
| 1737 | (*parent_ref)->children.push_back(negative_poly); |
---|
| 1738 | (*parent_ref)->positivechildren.push_back(positive_poly); |
---|
| 1739 | (*parent_ref)->negativechildren.push_back(negative_poly); |
---|
| 1740 | } |
---|
| 1741 | |
---|
| 1742 | positive_poly->parents.insert(positive_poly->parents.end(), |
---|
| 1743 | current_polyhedron->parents.begin(), |
---|
| 1744 | current_polyhedron->parents.end()); |
---|
| 1745 | |
---|
| 1746 | negative_poly->parents.insert(negative_poly->parents.end(), |
---|
| 1747 | current_polyhedron->parents.begin(), |
---|
| 1748 | current_polyhedron->parents.end()); |
---|
| 1749 | |
---|
[1299] | 1750 | |
---|
[1266] | 1751 | |
---|
| 1752 | new_totally_neutral_child->parents.push_back(positive_poly); |
---|
| 1753 | new_totally_neutral_child->parents.push_back(negative_poly); |
---|
| 1754 | |
---|
| 1755 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->positivechildren.begin();child_ref!=current_polyhedron->positivechildren.end();child_ref++) |
---|
| 1756 | { |
---|
| 1757 | (*child_ref)->parents.remove(current_polyhedron); |
---|
| 1758 | (*child_ref)->parents.push_back(positive_poly); |
---|
| 1759 | } |
---|
| 1760 | |
---|
| 1761 | positive_poly->children.insert(positive_poly->children.end(), |
---|
| 1762 | current_polyhedron->positivechildren.begin(), |
---|
| 1763 | current_polyhedron->positivechildren.end()); |
---|
| 1764 | |
---|
| 1765 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->negativechildren.begin();child_ref!=current_polyhedron->negativechildren.end();child_ref++) |
---|
| 1766 | { |
---|
| 1767 | (*child_ref)->parents.remove(current_polyhedron); |
---|
| 1768 | (*child_ref)->parents.push_back(negative_poly); |
---|
| 1769 | } |
---|
| 1770 | |
---|
| 1771 | negative_poly->children.insert(negative_poly->children.end(), |
---|
| 1772 | current_polyhedron->negativechildren.begin(), |
---|
| 1773 | current_polyhedron->negativechildren.end()); |
---|
| 1774 | |
---|
| 1775 | positive_poly->vertices.insert(current_polyhedron->positiveneutralvertices.begin(),current_polyhedron->positiveneutralvertices.end()); |
---|
| 1776 | positive_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
---|
| 1777 | |
---|
| 1778 | negative_poly->vertices.insert(current_polyhedron->negativeneutralvertices.begin(),current_polyhedron->negativeneutralvertices.end()); |
---|
| 1779 | negative_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
---|
[1268] | 1780 | |
---|
[1266] | 1781 | new_totally_neutral_child->triangulate(false); |
---|
| 1782 | |
---|
[1346] | 1783 | normalization_factor += positive_poly->triangulate(k==for_splitting.size()-1); |
---|
| 1784 | normalization_factor += negative_poly->triangulate(k==for_splitting.size()-1); |
---|
[1266] | 1785 | |
---|
[1356] | 1786 | statistic.append_polyhedron(k-1, new_totally_neutral_child); |
---|
| 1787 | |
---|
[1268] | 1788 | |
---|
[1356] | 1789 | |
---|
[1266] | 1790 | statistic.insert_polyhedron(k, positive_poly, current_polyhedron); |
---|
| 1791 | statistic.insert_polyhedron(k, negative_poly, current_polyhedron); |
---|
| 1792 | |
---|
| 1793 | statistic.delete_polyhedron(k, current_polyhedron); |
---|
| 1794 | |
---|
| 1795 | delete current_polyhedron; |
---|
| 1796 | } |
---|
| 1797 | |
---|
| 1798 | k++; |
---|
| 1799 | } |
---|
| 1800 | } |
---|
| 1801 | |
---|
[1349] | 1802 | /* |
---|
[1343] | 1803 | vector<int> sizevector; |
---|
| 1804 | //sizevector.clear(); |
---|
[1266] | 1805 | for(int s = 0;s<statistic.size();s++) |
---|
| 1806 | { |
---|
| 1807 | sizevector.push_back(statistic.row_size(s)); |
---|
[1301] | 1808 | cout << statistic.row_size(s) << ", "; |
---|
[1268] | 1809 | } |
---|
[1319] | 1810 | |
---|
| 1811 | cout << endl; |
---|
[1349] | 1812 | */ |
---|
[1266] | 1813 | |
---|
[1349] | 1814 | cout << "Normalization factor: " << normalization_factor << endl; |
---|
| 1815 | |
---|
| 1816 | log_nc = log(normalization_factor) + logfact(condition_order-number_of_parameters-2); |
---|
| 1817 | |
---|
[1269] | 1818 | /* |
---|
| 1819 | 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) |
---|
| 1820 | { |
---|
| 1821 | cout << ((toprow*)topr_ref)->condition << endl; |
---|
| 1822 | } |
---|
| 1823 | */ |
---|
| 1824 | |
---|
[1356] | 1825 | step_me(101); |
---|
[1318] | 1826 | |
---|
[1266] | 1827 | } |
---|
| 1828 | |
---|
| 1829 | void set_correction_factors(int order) |
---|
| 1830 | { |
---|
[1280] | 1831 | for(int remaining_order = correction_factors.size();remaining_order<order;remaining_order++) |
---|
[1266] | 1832 | { |
---|
[1280] | 1833 | multiset<my_ivec> factor_templates; |
---|
| 1834 | multiset<my_ivec> final_factors; |
---|
[1266] | 1835 | |
---|
[1280] | 1836 | my_ivec orig_template = my_ivec(); |
---|
| 1837 | |
---|
| 1838 | for(int i = 1;i<number_of_parameters-remaining_order+1;i++) |
---|
[1266] | 1839 | { |
---|
[1280] | 1840 | bool in_cycle = false; |
---|
| 1841 | for(int j = 0;j<=remaining_order;j++) { |
---|
| 1842 | |
---|
| 1843 | multiset<my_ivec>::iterator fac_ref = factor_templates.begin(); |
---|
[1266] | 1844 | |
---|
[1280] | 1845 | do |
---|
[1266] | 1846 | { |
---|
[1280] | 1847 | my_ivec current_template; |
---|
| 1848 | if(!in_cycle) |
---|
| 1849 | { |
---|
| 1850 | current_template = orig_template; |
---|
| 1851 | in_cycle = true; |
---|
| 1852 | } |
---|
| 1853 | else |
---|
| 1854 | { |
---|
| 1855 | current_template = (*fac_ref); |
---|
| 1856 | fac_ref++; |
---|
| 1857 | } |
---|
[1266] | 1858 | |
---|
| 1859 | current_template.ins(current_template.size(),i); |
---|
| 1860 | |
---|
[1281] | 1861 | // cout << "template:" << current_template << endl; |
---|
[1266] | 1862 | |
---|
[1280] | 1863 | if(current_template.size()==remaining_order+1) |
---|
[1266] | 1864 | { |
---|
[1280] | 1865 | final_factors.insert(current_template); |
---|
[1266] | 1866 | } |
---|
| 1867 | else |
---|
| 1868 | { |
---|
| 1869 | factor_templates.insert(current_template); |
---|
| 1870 | } |
---|
| 1871 | } |
---|
[1280] | 1872 | while(fac_ref!=factor_templates.end()); |
---|
[1266] | 1873 | } |
---|
| 1874 | } |
---|
| 1875 | |
---|
| 1876 | correction_factors.push_back(final_factors); |
---|
| 1877 | |
---|
| 1878 | } |
---|
| 1879 | } |
---|
| 1880 | |
---|
[1343] | 1881 | pair<vec,simplex*> choose_simplex() |
---|
| 1882 | { |
---|
| 1883 | double rnumber = randu(); |
---|
[1320] | 1884 | |
---|
[1343] | 1885 | // cout << "RND:" << rnumber << endl; |
---|
[1320] | 1886 | |
---|
[1343] | 1887 | // This could be more efficient (log n), but map::upper_bound() doesn't let me dereference returned iterator |
---|
| 1888 | double prob_sum = 0; |
---|
| 1889 | toprow* sampled_toprow; |
---|
[1346] | 1890 | for(polyhedron* top_ref = statistic.rows[number_of_parameters];top_ref!=statistic.end_poly;top_ref=top_ref->next_poly) |
---|
[1343] | 1891 | { |
---|
| 1892 | // cout << "CDF:"<< (*top_ref).first << endl; |
---|
| 1893 | |
---|
| 1894 | toprow* current_toprow = ((toprow*)top_ref); |
---|
| 1895 | |
---|
| 1896 | prob_sum += current_toprow->probability; |
---|
| 1897 | |
---|
| 1898 | if(prob_sum >= rnumber*normalization_factor) |
---|
| 1899 | { |
---|
| 1900 | sampled_toprow = (toprow*)top_ref; |
---|
| 1901 | break; |
---|
[1346] | 1902 | } |
---|
| 1903 | else |
---|
| 1904 | { |
---|
| 1905 | if(top_ref->next_poly==statistic.end_poly) |
---|
| 1906 | { |
---|
| 1907 | cout << "Error."; |
---|
| 1908 | } |
---|
| 1909 | } |
---|
[1343] | 1910 | } |
---|
| 1911 | |
---|
| 1912 | //// cout << "Toprow/Count: " << toprow_count << "/" << ordered_toprows.size() << endl; |
---|
| 1913 | // cout << &sampled_toprow << ";"; |
---|
| 1914 | |
---|
| 1915 | rnumber = randu(); |
---|
| 1916 | |
---|
| 1917 | set<simplex*>::iterator s_ref; |
---|
| 1918 | prob_sum = 0; |
---|
| 1919 | for(s_ref = sampled_toprow->triangulation.begin();s_ref!=sampled_toprow->triangulation.end();s_ref++) |
---|
| 1920 | { |
---|
| 1921 | prob_sum += (*s_ref)->probability; |
---|
| 1922 | |
---|
| 1923 | if(prob_sum/sampled_toprow->probability >= rnumber) |
---|
| 1924 | break; |
---|
| 1925 | } |
---|
| 1926 | |
---|
| 1927 | return pair<vec,simplex*>(sampled_toprow->condition_sum,*s_ref); |
---|
| 1928 | } |
---|
| 1929 | |
---|
| 1930 | pair<double,double> choose_sigma(simplex* sampled_simplex) |
---|
| 1931 | { |
---|
[1346] | 1932 | double sigma = 0; |
---|
| 1933 | double pg_sum; |
---|
| 1934 | double ng_sum; |
---|
| 1935 | do |
---|
| 1936 | { |
---|
| 1937 | double rnumber = randu(); |
---|
| 1938 | |
---|
| 1939 | |
---|
| 1940 | double sum_g = 0; |
---|
| 1941 | for(int i = 0;i<sampled_simplex->positive_gamma_parameters.size();i++) |
---|
[1343] | 1942 | { |
---|
[1346] | 1943 | 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++) |
---|
| 1944 | { |
---|
| 1945 | sum_g += (*g_ref).first/sampled_simplex->positive_gamma_sum; |
---|
[1343] | 1946 | |
---|
| 1947 | |
---|
[1346] | 1948 | if(sum_g>rnumber) |
---|
| 1949 | { |
---|
| 1950 | //itpp::Gamma_RNG* gamma = new itpp::Gamma_RNG(conditions.size()-number_of_parameters,1/(*g_ref).second); |
---|
| 1951 | //sigma = 1/(*gamma)(); |
---|
| 1952 | |
---|
| 1953 | GamRNG.setup(conditions.size()-number_of_parameters,(*g_ref).second); |
---|
| 1954 | |
---|
| 1955 | sigma = 1/GamRNG(); |
---|
[1343] | 1956 | |
---|
[1346] | 1957 | // cout << "Sigma mean: " << (*g_ref).second/(conditions.size()-number_of_parameters-1) << endl; |
---|
| 1958 | break; |
---|
| 1959 | } |
---|
| 1960 | } |
---|
| 1961 | |
---|
| 1962 | if(sigma!=0) |
---|
| 1963 | { |
---|
[1343] | 1964 | break; |
---|
[1346] | 1965 | } |
---|
[1343] | 1966 | } |
---|
| 1967 | |
---|
[1346] | 1968 | rnumber = randu(); |
---|
| 1969 | |
---|
| 1970 | pg_sum = 0; |
---|
| 1971 | for(vector<multimap<double,double>>::iterator v_ref = sampled_simplex->positive_gamma_parameters.begin();v_ref!=sampled_simplex->positive_gamma_parameters.end();v_ref++) |
---|
[1343] | 1972 | { |
---|
[1346] | 1973 | for(multimap<double,double>::iterator pg_ref = (*v_ref).begin();pg_ref!=(*v_ref).end();pg_ref++) |
---|
| 1974 | { |
---|
| 1975 | pg_sum += exp((sampled_simplex->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; |
---|
| 1976 | } |
---|
[1343] | 1977 | } |
---|
| 1978 | |
---|
[1346] | 1979 | ng_sum = 0; |
---|
| 1980 | for(vector<multimap<double,double>>::iterator v_ref = sampled_simplex->negative_gamma_parameters.begin();v_ref!=sampled_simplex->negative_gamma_parameters.end();v_ref++) |
---|
[1343] | 1981 | { |
---|
[1346] | 1982 | for(multimap<double,double>::iterator ng_ref = (*v_ref).begin();ng_ref!=(*v_ref).end();ng_ref++) |
---|
| 1983 | { |
---|
| 1984 | ng_sum += exp((sampled_simplex->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; |
---|
| 1985 | } |
---|
| 1986 | } |
---|
[1343] | 1987 | } |
---|
[1346] | 1988 | while(pg_sum-ng_sum<0); |
---|
[1343] | 1989 | |
---|
| 1990 | return pair<double,double>((pg_sum-ng_sum)/pg_sum,sigma); |
---|
| 1991 | } |
---|
| 1992 | |
---|
[1320] | 1993 | mat sample_mat(int n) |
---|
[1331] | 1994 | { |
---|
[1320] | 1995 | |
---|
| 1996 | /// \TODO tady je to spatne, tady nesmi byt conditions.size(), viz RARX.bayes() |
---|
| 1997 | if(conditions.size()-2-number_of_parameters>=0) |
---|
[1335] | 1998 | { |
---|
[1324] | 1999 | mat sample_mat; |
---|
| 2000 | map<double,toprow*> ordered_toprows; |
---|
| 2001 | double sum_a = 0; |
---|
[1335] | 2002 | |
---|
| 2003 | //cout << "Likelihoods of toprows:" << endl; |
---|
[1320] | 2004 | |
---|
[1324] | 2005 | for(polyhedron* top_ref = statistic.rows[number_of_parameters];top_ref!=statistic.end_poly;top_ref=top_ref->next_poly) |
---|
[1320] | 2006 | { |
---|
| 2007 | toprow* current_top = (toprow*)top_ref; |
---|
| 2008 | |
---|
[1324] | 2009 | sum_a+=current_top->probability; |
---|
[1335] | 2010 | /* |
---|
| 2011 | cout << current_top->probability << " "; |
---|
| 2012 | |
---|
| 2013 | for(set<vertex*>::iterator vert_ref = (*top_ref).vertices.begin();vert_ref!=(*top_ref).vertices.end();vert_ref++) |
---|
| 2014 | { |
---|
| 2015 | cout << round(100*(*vert_ref)->get_coordinates())/100 << " ; "; |
---|
| 2016 | } |
---|
| 2017 | */ |
---|
| 2018 | |
---|
| 2019 | // cout << endl; |
---|
[1324] | 2020 | ordered_toprows.insert(pair<double,toprow*>(sum_a,current_top)); |
---|
[1335] | 2021 | } |
---|
| 2022 | |
---|
| 2023 | // cout << "Sum N: " << normalization_factor << endl; |
---|
[1320] | 2024 | |
---|
[1324] | 2025 | while(sample_mat.cols()<n) |
---|
| 2026 | { |
---|
[1336] | 2027 | //// cout << "*************************************" << endl; |
---|
| 2028 | |
---|
[1334] | 2029 | |
---|
[1336] | 2030 | |
---|
[1324] | 2031 | double rnumber = randu()*sum_a; |
---|
[1320] | 2032 | |
---|
[1324] | 2033 | // cout << "RND:" << rnumber << endl; |
---|
[1320] | 2034 | |
---|
[1324] | 2035 | // This could be more efficient (log n), but map::upper_bound() doesn't let me dereference returned iterator |
---|
[1334] | 2036 | int toprow_count = 0; |
---|
[1324] | 2037 | toprow* sampled_toprow; |
---|
| 2038 | for(map<double,toprow*>::iterator top_ref = ordered_toprows.begin();top_ref!=ordered_toprows.end();top_ref++) |
---|
| 2039 | { |
---|
| 2040 | // cout << "CDF:"<< (*top_ref).first << endl; |
---|
[1334] | 2041 | toprow_count++; |
---|
[1320] | 2042 | |
---|
[1324] | 2043 | if((*top_ref).first >= rnumber) |
---|
| 2044 | { |
---|
| 2045 | sampled_toprow = (*top_ref).second; |
---|
| 2046 | break; |
---|
| 2047 | } |
---|
| 2048 | } |
---|
[1320] | 2049 | |
---|
[1336] | 2050 | //// cout << "Toprow/Count: " << toprow_count << "/" << ordered_toprows.size() << endl; |
---|
[1324] | 2051 | // cout << &sampled_toprow << ";"; |
---|
[1320] | 2052 | |
---|
[1324] | 2053 | rnumber = randu(); |
---|
| 2054 | |
---|
| 2055 | set<simplex*>::iterator s_ref; |
---|
| 2056 | double sum_b = 0; |
---|
[1334] | 2057 | int simplex_count = 0; |
---|
[1324] | 2058 | for(s_ref = sampled_toprow->triangulation.begin();s_ref!=sampled_toprow->triangulation.end();s_ref++) |
---|
| 2059 | { |
---|
[1334] | 2060 | simplex_count++; |
---|
| 2061 | |
---|
[1324] | 2062 | sum_b += (*s_ref)->probability; |
---|
| 2063 | |
---|
| 2064 | if(sum_b/sampled_toprow->probability >= rnumber) |
---|
| 2065 | break; |
---|
| 2066 | } |
---|
| 2067 | |
---|
[1336] | 2068 | //// cout << "Simplex/Count: " << simplex_count << "/" << sampled_toprow->triangulation.size() << endl; |
---|
| 2069 | //// cout << "Simplex factor: " << (*s_ref)->probability << endl; |
---|
| 2070 | //// cout << "Toprow factor: " << sampled_toprow->probability << endl; |
---|
| 2071 | //// cout << "Emlig factor: " << normalization_factor << endl; |
---|
[1324] | 2072 | // cout << &(*tri_ref) << endl; |
---|
| 2073 | |
---|
[1335] | 2074 | int number_of_runs = 0; |
---|
[1331] | 2075 | bool have_sigma = false; |
---|
[1325] | 2076 | double sigma = 0; |
---|
[1331] | 2077 | do |
---|
[1325] | 2078 | { |
---|
[1331] | 2079 | rnumber = randu(); |
---|
| 2080 | |
---|
| 2081 | double sum_g = 0; |
---|
| 2082 | for(int i = 0;i<(*s_ref)->positive_gamma_parameters.size();i++) |
---|
[1325] | 2083 | { |
---|
[1331] | 2084 | 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++) |
---|
| 2085 | { |
---|
| 2086 | sum_g += (*g_ref).first/(*s_ref)->positive_gamma_sum; |
---|
[1325] | 2087 | |
---|
[1335] | 2088 | |
---|
[1331] | 2089 | if(sum_g>rnumber) |
---|
| 2090 | { |
---|
[1336] | 2091 | //itpp::Gamma_RNG* gamma = new itpp::Gamma_RNG(conditions.size()-number_of_parameters,1/(*g_ref).second); |
---|
| 2092 | //sigma = 1/(*gamma)(); |
---|
[1335] | 2093 | |
---|
[1336] | 2094 | GamRNG.setup(conditions.size()-number_of_parameters,(*g_ref).second); |
---|
| 2095 | |
---|
| 2096 | sigma = 1/GamRNG(); |
---|
| 2097 | |
---|
| 2098 | // cout << "Sigma mean: " << (*g_ref).second/(conditions.size()-number_of_parameters-1) << endl; |
---|
[1331] | 2099 | break; |
---|
[1335] | 2100 | } |
---|
[1331] | 2101 | } |
---|
| 2102 | |
---|
| 2103 | if(sigma!=0) |
---|
[1325] | 2104 | { |
---|
| 2105 | break; |
---|
[1331] | 2106 | } |
---|
[1325] | 2107 | } |
---|
| 2108 | |
---|
[1331] | 2109 | rnumber = randu(); |
---|
| 2110 | |
---|
| 2111 | double pg_sum = 0; |
---|
| 2112 | for(vector<multimap<double,double>>::iterator v_ref = (*s_ref)->positive_gamma_parameters.begin();v_ref!=(*s_ref)->positive_gamma_parameters.end();v_ref++) |
---|
[1325] | 2113 | { |
---|
[1331] | 2114 | for(multimap<double,double>::iterator pg_ref = (*v_ref).begin();pg_ref!=(*v_ref).end();pg_ref++) |
---|
| 2115 | { |
---|
[1335] | 2116 | 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; |
---|
[1331] | 2117 | } |
---|
[1325] | 2118 | } |
---|
[1331] | 2119 | |
---|
| 2120 | double ng_sum = 0; |
---|
| 2121 | for(vector<multimap<double,double>>::iterator v_ref = (*s_ref)->negative_gamma_parameters.begin();v_ref!=(*s_ref)->negative_gamma_parameters.end();v_ref++) |
---|
| 2122 | { |
---|
| 2123 | for(multimap<double,double>::iterator ng_ref = (*v_ref).begin();ng_ref!=(*v_ref).end();ng_ref++) |
---|
| 2124 | { |
---|
[1335] | 2125 | 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; |
---|
[1331] | 2126 | } |
---|
| 2127 | } |
---|
| 2128 | |
---|
| 2129 | if((pg_sum-ng_sum)/pg_sum>rnumber) |
---|
| 2130 | { |
---|
| 2131 | have_sigma = true; |
---|
| 2132 | } |
---|
[1335] | 2133 | |
---|
| 2134 | number_of_runs++; |
---|
[1325] | 2135 | } |
---|
[1331] | 2136 | while(!have_sigma); |
---|
[1325] | 2137 | |
---|
[1336] | 2138 | //// cout << "Sigma: " << sigma << endl; |
---|
[1338] | 2139 | //// cout << "Nr. of sigma runs: " << number_of_runs << endl; |
---|
[1335] | 2140 | |
---|
[1324] | 2141 | int dimension = (*s_ref)->vertices.size()-1; |
---|
| 2142 | |
---|
| 2143 | mat jacobian(dimension,dimension); |
---|
| 2144 | vec gradient = sampled_toprow->condition_sum.right(dimension); |
---|
| 2145 | |
---|
| 2146 | vertex* base_vert = *(*s_ref)->vertices.begin(); |
---|
[1335] | 2147 | |
---|
[1336] | 2148 | //// cout << "Base vertex coords(should be close to est. param.): " << base_vert->get_coordinates() << endl; |
---|
[1324] | 2149 | |
---|
| 2150 | int row_count = 0; |
---|
| 2151 | |
---|
| 2152 | for(set<vertex*>::iterator vert_ref = ++(*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) |
---|
| 2153 | { |
---|
[1335] | 2154 | vec current_coords = (*vert_ref)->get_coordinates(); |
---|
[1324] | 2155 | |
---|
[1336] | 2156 | //// cout << "Coords of vertex[" << row_count << "]: " << current_coords << endl; |
---|
[1335] | 2157 | |
---|
| 2158 | vec relative_coords = current_coords-base_vert->get_coordinates(); |
---|
| 2159 | |
---|
[1334] | 2160 | jacobian.set_row(row_count,relative_coords); |
---|
| 2161 | |
---|
[1324] | 2162 | row_count++; |
---|
[1335] | 2163 | } |
---|
[1334] | 2164 | |
---|
[1336] | 2165 | //// cout << "Jacobian: " << jacobian << endl; |
---|
[1324] | 2166 | |
---|
[1336] | 2167 | //// cout << "Gradient before trafo:" << gradient << endl; |
---|
[1334] | 2168 | |
---|
[1324] | 2169 | gradient = jacobian*gradient; |
---|
| 2170 | |
---|
[1336] | 2171 | //// cout << "Gradient after trafo:" << gradient << endl; |
---|
[1335] | 2172 | |
---|
[1334] | 2173 | // vec normal_gradient = gradient/sqrt(gradient*gradient); |
---|
| 2174 | // cout << gradient << endl; |
---|
| 2175 | // cout << normal_gradient << endl; |
---|
| 2176 | // cout << sqrt(gradient*gradient) << endl; |
---|
[1324] | 2177 | |
---|
[1334] | 2178 | mat rotation_matrix = eye(dimension); |
---|
[1324] | 2179 | |
---|
[1334] | 2180 | |
---|
[1324] | 2181 | |
---|
| 2182 | for(int i = 1;i<dimension;i++) |
---|
| 2183 | { |
---|
[1334] | 2184 | vec x_axis = zeros(dimension); |
---|
| 2185 | x_axis.set(0,1); |
---|
[1324] | 2186 | |
---|
[1334] | 2187 | x_axis = rotation_matrix*x_axis; |
---|
[1324] | 2188 | |
---|
[1335] | 2189 | double t = abs(gradient[i]/gradient*x_axis); |
---|
[1334] | 2190 | |
---|
| 2191 | double sin_theta = sign(gradient[i])*t/sqrt(1+pow(t,2)); |
---|
| 2192 | double cos_theta = sign(gradient*x_axis)/sqrt(1+pow(t,2)); |
---|
| 2193 | |
---|
[1324] | 2194 | mat partial_rotation = eye(dimension); |
---|
| 2195 | |
---|
| 2196 | partial_rotation.set(0,0,cos_theta); |
---|
| 2197 | partial_rotation.set(i,i,cos_theta); |
---|
| 2198 | |
---|
| 2199 | partial_rotation.set(0,i,sin_theta); |
---|
| 2200 | partial_rotation.set(i,0,-sin_theta); |
---|
| 2201 | |
---|
[1334] | 2202 | rotation_matrix = rotation_matrix*partial_rotation; |
---|
[1324] | 2203 | |
---|
| 2204 | } |
---|
| 2205 | |
---|
| 2206 | // cout << rotation_matrix << endl; |
---|
[1335] | 2207 | |
---|
| 2208 | mat extended_rotation = rotation_matrix; |
---|
| 2209 | extended_rotation.ins_col(0,zeros(extended_rotation.rows())); |
---|
[1324] | 2210 | |
---|
[1336] | 2211 | //// cout << "Extended rotation: " << extended_rotation << endl; |
---|
[1335] | 2212 | |
---|
| 2213 | vec minima = itpp::min(extended_rotation,2); |
---|
| 2214 | vec maxima = itpp::max(extended_rotation,2); |
---|
[1324] | 2215 | |
---|
[1336] | 2216 | //// cout << "Minima: " << minima << endl; |
---|
| 2217 | //// cout << "Maxima: " << maxima << endl; |
---|
[1324] | 2218 | |
---|
[1335] | 2219 | vec sample_coordinates; |
---|
| 2220 | bool is_inside = true; |
---|
| 2221 | |
---|
| 2222 | vec new_sample; |
---|
| 2223 | sample_coordinates = new_sample; |
---|
[1324] | 2224 | |
---|
| 2225 | for(int j = 0;j<number_of_parameters;j++) |
---|
| 2226 | { |
---|
| 2227 | rnumber = randu(); |
---|
| 2228 | |
---|
| 2229 | double coordinate; |
---|
| 2230 | |
---|
| 2231 | if(j==0) |
---|
[1335] | 2232 | { |
---|
| 2233 | vec new_gradient = rotation_matrix*gradient; |
---|
[1324] | 2234 | |
---|
[1336] | 2235 | //// cout << "New gradient(should have only first component nonzero):" << new_gradient << endl; |
---|
[1334] | 2236 | |
---|
[1335] | 2237 | // cout << "Max: " << maxima[0] << " Min: " << minima[0] << " Grad:" << new_gradient[0] << endl; |
---|
[1324] | 2238 | |
---|
[1335] | 2239 | double log_bracket = 1-rnumber*(1-exp(new_gradient[0]/sigma*(minima[0]-maxima[0]))); |
---|
[1334] | 2240 | |
---|
[1335] | 2241 | coordinate = minima[0]-sigma/new_gradient[0]*log(log_bracket); |
---|
[1324] | 2242 | } |
---|
[1335] | 2243 | else |
---|
| 2244 | { |
---|
| 2245 | coordinate = minima[j]+rnumber*(maxima[j]-minima[j]); |
---|
| 2246 | } |
---|
| 2247 | |
---|
| 2248 | sample_coordinates.ins(j,coordinate); |
---|
[1324] | 2249 | } |
---|
| 2250 | |
---|
[1336] | 2251 | //// cout << "Sampled coordinates(gradient direction): " << sample_coordinates << endl; |
---|
[1335] | 2252 | |
---|
| 2253 | sample_coordinates = rotation_matrix.T()*sample_coordinates; |
---|
| 2254 | |
---|
[1336] | 2255 | //// cout << "Sampled coordinates(backrotated direction):" << sample_coordinates << endl; |
---|
[1335] | 2256 | |
---|
| 2257 | |
---|
| 2258 | for(int j = 0;j<sample_coordinates.size();j++) |
---|
| 2259 | { |
---|
| 2260 | if(sample_coordinates[j]<0) |
---|
| 2261 | { |
---|
| 2262 | is_inside = false; |
---|
| 2263 | } |
---|
| 2264 | } |
---|
| 2265 | |
---|
| 2266 | double above_criterion = ones(sample_coordinates.size())*sample_coordinates; |
---|
| 2267 | |
---|
| 2268 | if(above_criterion>1) |
---|
| 2269 | { |
---|
| 2270 | is_inside = false; |
---|
| 2271 | } |
---|
| 2272 | |
---|
| 2273 | if(is_inside) |
---|
| 2274 | { |
---|
| 2275 | sample_coordinates = jacobian.T()*sample_coordinates+(*base_vert).get_coordinates(); |
---|
| 2276 | |
---|
| 2277 | sample_coordinates.ins(0,sigma); |
---|
| 2278 | |
---|
[1336] | 2279 | //// cout << "Sampled coordinates(parameter space):" << sample_coordinates << endl; |
---|
[1335] | 2280 | |
---|
| 2281 | sample_mat.ins_col(0,sample_coordinates); |
---|
[1336] | 2282 | |
---|
[1338] | 2283 | // cout << sample_mat.cols() << ","; |
---|
[1335] | 2284 | } |
---|
| 2285 | |
---|
[1324] | 2286 | // cout << sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*min_grad->get_coordinates()-sampled_toprow->condition_sum[0] << endl; |
---|
| 2287 | // cout << sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0] << endl; |
---|
| 2288 | |
---|
[1335] | 2289 | |
---|
[1320] | 2290 | } |
---|
| 2291 | |
---|
[1343] | 2292 | cout << endl; |
---|
[1324] | 2293 | return sample_mat; |
---|
[1320] | 2294 | } |
---|
[1324] | 2295 | else |
---|
| 2296 | { |
---|
| 2297 | throw new exception("You are trying to sample from density that is not determined (parameters can't be integrated out)!"); |
---|
| 2298 | |
---|
| 2299 | return 0; |
---|
| 2300 | } |
---|
[1320] | 2301 | |
---|
[1324] | 2302 | |
---|
[1320] | 2303 | } |
---|
| 2304 | |
---|
[1343] | 2305 | pair<vec,mat> importance_sample(int n) |
---|
| 2306 | { |
---|
| 2307 | vec probabilities; |
---|
| 2308 | mat samples; |
---|
| 2309 | |
---|
| 2310 | for(int i = 0;i<n;i++) |
---|
| 2311 | { |
---|
| 2312 | pair<vec,simplex*> condition_and_simplex = choose_simplex(); |
---|
| 2313 | |
---|
| 2314 | pair<double,double> probability_and_sigma = choose_sigma(condition_and_simplex.second); |
---|
| 2315 | |
---|
| 2316 | int dimension = condition_and_simplex.second->vertices.size()-1; |
---|
| 2317 | |
---|
| 2318 | mat jacobian(dimension,dimension); |
---|
| 2319 | vec gradient = condition_and_simplex.first.right(dimension); |
---|
| 2320 | |
---|
| 2321 | vertex* base_vert = *condition_and_simplex.second->vertices.begin(); |
---|
| 2322 | |
---|
| 2323 | //// cout << "Base vertex coords(should be close to est. param.): " << base_vert->get_coordinates() << endl; |
---|
| 2324 | |
---|
| 2325 | int row_count = 0; |
---|
| 2326 | |
---|
| 2327 | for(set<vertex*>::iterator vert_ref = ++condition_and_simplex.second->vertices.begin();vert_ref!=condition_and_simplex.second->vertices.end();vert_ref++) |
---|
| 2328 | { |
---|
| 2329 | vec current_coords = (*vert_ref)->get_coordinates(); |
---|
| 2330 | |
---|
| 2331 | //// cout << "Coords of vertex[" << row_count << "]: " << current_coords << endl; |
---|
| 2332 | |
---|
| 2333 | vec relative_coords = current_coords-base_vert->get_coordinates(); |
---|
| 2334 | |
---|
| 2335 | jacobian.set_row(row_count,relative_coords); |
---|
| 2336 | |
---|
| 2337 | row_count++; |
---|
| 2338 | } |
---|
| 2339 | |
---|
| 2340 | //// cout << "Jacobian: " << jacobian << endl; |
---|
| 2341 | |
---|
| 2342 | /// \todo Is this correct? Are the random coordinates really jointly uniform? I don't know. |
---|
| 2343 | vec sample_coords; |
---|
| 2344 | double sampling_diff = 1; |
---|
| 2345 | for(int j = 0;j<number_of_parameters;j++) |
---|
| 2346 | { |
---|
| 2347 | double rnumber = randu()*sampling_diff; |
---|
| 2348 | |
---|
| 2349 | sample_coords.ins(0,rnumber); |
---|
| 2350 | |
---|
| 2351 | sampling_diff -= rnumber; |
---|
| 2352 | } |
---|
| 2353 | |
---|
| 2354 | sample_coords = jacobian.T()*sample_coords+(*base_vert).get_coordinates(); |
---|
| 2355 | |
---|
| 2356 | vec extended_coords = sample_coords; |
---|
| 2357 | extended_coords.ins(0,-1.0); |
---|
| 2358 | |
---|
[1346] | 2359 | double exponent = extended_coords*condition_and_simplex.first; |
---|
| 2360 | double sample_prob = 1/condition_and_simplex.second->probability/pow(probability_and_sigma.second,(int)conditions.size()-number_of_parameters)*exp((-1)/probability_and_sigma.second*exponent); |
---|
[1343] | 2361 | sample_prob *= probability_and_sigma.first; |
---|
| 2362 | |
---|
| 2363 | sample_coords.ins(0,probability_and_sigma.second); |
---|
| 2364 | |
---|
| 2365 | samples.ins_col(0,sample_coords); |
---|
| 2366 | probabilities.ins(0,sample_prob); |
---|
| 2367 | } |
---|
| 2368 | |
---|
| 2369 | return pair<vec,mat>(probabilities,samples); |
---|
| 2370 | } |
---|
| 2371 | |
---|
[1349] | 2372 | int logfact(int factor) |
---|
| 2373 | { |
---|
| 2374 | if(factor>0) |
---|
| 2375 | { |
---|
| 2376 | return factor+logfact(factor-1); |
---|
| 2377 | } |
---|
| 2378 | else |
---|
| 2379 | { |
---|
| 2380 | return 0; |
---|
| 2381 | } |
---|
| 2382 | } |
---|
[1266] | 2383 | protected: |
---|
| 2384 | |
---|
| 2385 | /// A method for creating plain default statistic representing only the range of the parameter space. |
---|
| 2386 | void create_statistic(int number_of_parameters) |
---|
| 2387 | { |
---|
[1301] | 2388 | /* |
---|
[1266] | 2389 | for(int i = 0;i<number_of_parameters;i++) |
---|
| 2390 | { |
---|
| 2391 | vec condition_vec = zeros(number_of_parameters+1); |
---|
| 2392 | condition_vec[i+1] = 1; |
---|
| 2393 | |
---|
| 2394 | condition* new_condition = new condition(condition_vec); |
---|
| 2395 | |
---|
| 2396 | conditions.push_back(new_condition); |
---|
| 2397 | } |
---|
[1301] | 2398 | */ |
---|
[1266] | 2399 | |
---|
| 2400 | // An empty vector of coordinates. |
---|
| 2401 | vec origin_coord; |
---|
| 2402 | |
---|
| 2403 | // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. |
---|
| 2404 | vertex *origin = new vertex(origin_coord); |
---|
[1268] | 2405 | |
---|
| 2406 | origin->my_emlig = this; |
---|
[1266] | 2407 | |
---|
| 2408 | /* |
---|
| 2409 | // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse |
---|
| 2410 | // diagram. First we create a vector of polyhedrons.. |
---|
| 2411 | list<polyhedron*> origin_vec; |
---|
| 2412 | |
---|
| 2413 | // ..we fill it with the origin.. |
---|
| 2414 | origin_vec.push_back(origin); |
---|
| 2415 | |
---|
| 2416 | // ..and we fill the statistic with the created vector. |
---|
| 2417 | statistic.push_back(origin_vec); |
---|
| 2418 | */ |
---|
| 2419 | |
---|
[1268] | 2420 | statistic = *(new c_statistic()); |
---|
| 2421 | |
---|
[1266] | 2422 | statistic.append_polyhedron(0, origin); |
---|
| 2423 | |
---|
| 2424 | // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to |
---|
| 2425 | // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: |
---|
| 2426 | for(int i=0;i<number_of_parameters;i++) |
---|
| 2427 | { |
---|
| 2428 | // We first will create two new vertices. These will be the borders of the parameter space in the dimension |
---|
| 2429 | // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the |
---|
| 2430 | // right amount of zero cooridnates by reading them from the origin |
---|
| 2431 | vec origin_coord = origin->get_coordinates(); |
---|
| 2432 | |
---|
| 2433 | // And we incorporate the nonzero coordinates into the new cooordinate vectors |
---|
| 2434 | vec origin_coord1 = concat(origin_coord,-max_range); |
---|
| 2435 | vec origin_coord2 = concat(origin_coord,max_range); |
---|
| 2436 | |
---|
| 2437 | |
---|
| 2438 | // Now we create the points |
---|
| 2439 | vertex* new_point1 = new vertex(origin_coord1); |
---|
[1268] | 2440 | vertex* new_point2 = new vertex(origin_coord2); |
---|
| 2441 | |
---|
| 2442 | new_point1->my_emlig = this; |
---|
| 2443 | new_point2->my_emlig = this; |
---|
[1266] | 2444 | |
---|
| 2445 | //********************************************************************************************************* |
---|
| 2446 | // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old |
---|
| 2447 | // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points |
---|
| 2448 | // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons |
---|
| 2449 | // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is |
---|
| 2450 | // connected to the first (second) newly created vertex by a parent-child relation. |
---|
| 2451 | //********************************************************************************************************* |
---|
| 2452 | |
---|
| 2453 | |
---|
| 2454 | /* |
---|
| 2455 | // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram |
---|
| 2456 | vector<vector<polyhedron*>> new_statistic1; |
---|
| 2457 | vector<vector<polyhedron*>> new_statistic2; |
---|
| 2458 | */ |
---|
| 2459 | |
---|
| 2460 | c_statistic* new_statistic1 = new c_statistic(); |
---|
| 2461 | c_statistic* new_statistic2 = new c_statistic(); |
---|
| 2462 | |
---|
| 2463 | |
---|
| 2464 | // Copy the statistic by rows |
---|
| 2465 | for(int j=0;j<statistic.size();j++) |
---|
| 2466 | { |
---|
| 2467 | |
---|
| 2468 | |
---|
| 2469 | // an element counter |
---|
| 2470 | int element_number = 0; |
---|
| 2471 | |
---|
| 2472 | /* |
---|
| 2473 | vector<polyhedron*> supportnew_1; |
---|
| 2474 | vector<polyhedron*> supportnew_2; |
---|
| 2475 | |
---|
| 2476 | new_statistic1.push_back(supportnew_1); |
---|
| 2477 | new_statistic2.push_back(supportnew_2); |
---|
| 2478 | */ |
---|
| 2479 | |
---|
| 2480 | // for each polyhedron in the given row |
---|
| 2481 | for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
| 2482 | { |
---|
| 2483 | // Append an extra zero coordinate to each of the vertices for the new dimension |
---|
| 2484 | // If vert_ref is at the first index => we loop through vertices |
---|
| 2485 | if(j == 0) |
---|
| 2486 | { |
---|
| 2487 | // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates |
---|
| 2488 | ((vertex*) horiz_ref)->push_coordinate(0); |
---|
| 2489 | } |
---|
| 2490 | /* |
---|
| 2491 | else |
---|
| 2492 | { |
---|
| 2493 | ((toprow*) (*horiz_ref))->condition.ins(0,0); |
---|
| 2494 | }*/ |
---|
| 2495 | |
---|
| 2496 | // if it has parents |
---|
| 2497 | if(!horiz_ref->parents.empty()) |
---|
| 2498 | { |
---|
| 2499 | // save the relative address of this child in a vector kids_rel_addresses of all its parents. |
---|
| 2500 | // This information will later be used for copying the whole Hasse diagram with each of the |
---|
| 2501 | // relations contained within. |
---|
| 2502 | for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++) |
---|
| 2503 | { |
---|
| 2504 | (*parent_ref)->kids_rel_addresses.push_back(element_number); |
---|
| 2505 | } |
---|
| 2506 | } |
---|
| 2507 | |
---|
| 2508 | // ************************************************************************************************** |
---|
| 2509 | // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron |
---|
| 2510 | // will be created as a toprow, but this information will be later forgotten and only the polyhedrons |
---|
| 2511 | // in the top row of the Hasse diagram will be considered toprow for later use. |
---|
| 2512 | // ************************************************************************************************** |
---|
| 2513 | |
---|
| 2514 | // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic |
---|
| 2515 | // this condition will be a vector of zeros. There are two vectors, because we need two copies of |
---|
| 2516 | // the original Hasse diagram. |
---|
| 2517 | vec vec1(number_of_parameters+1); |
---|
| 2518 | vec1.zeros(); |
---|
| 2519 | |
---|
| 2520 | vec vec2(number_of_parameters+1); |
---|
| 2521 | vec2.zeros(); |
---|
| 2522 | |
---|
| 2523 | // We create a new toprow with the previously specified condition. |
---|
| 2524 | toprow* current_copy1 = new toprow(vec1, 0); |
---|
[1268] | 2525 | toprow* current_copy2 = new toprow(vec2, 0); |
---|
[1266] | 2526 | |
---|
[1268] | 2527 | current_copy1->my_emlig = this; |
---|
| 2528 | current_copy2->my_emlig = this; |
---|
| 2529 | |
---|
[1266] | 2530 | // The vertices of the copies will be inherited, because there will be a parent/child relation |
---|
| 2531 | // between each polyhedron and its offspring (comming from the copy) and a parent has all the |
---|
| 2532 | // vertices of its child plus more. |
---|
| 2533 | for(set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++) |
---|
| 2534 | { |
---|
| 2535 | current_copy1->vertices.insert(*vertex_ref); |
---|
| 2536 | current_copy2->vertices.insert(*vertex_ref); |
---|
| 2537 | } |
---|
| 2538 | |
---|
| 2539 | // The only new vertex of the offspring should be the newly created point. |
---|
| 2540 | current_copy1->vertices.insert(new_point1); |
---|
| 2541 | current_copy2->vertices.insert(new_point2); |
---|
| 2542 | |
---|
| 2543 | // This method guarantees that each polyhedron is already triangulated, therefore its triangulation |
---|
| 2544 | // is only one set of vertices and it is the set of all its vertices. |
---|
[1324] | 2545 | simplex* t_simplex1 = new simplex(current_copy1->vertices); |
---|
| 2546 | simplex* t_simplex2 = new simplex(current_copy2->vertices); |
---|
[1266] | 2547 | |
---|
[1324] | 2548 | current_copy1->triangulation.insert(t_simplex1); |
---|
| 2549 | current_copy2->triangulation.insert(t_simplex2); |
---|
[1266] | 2550 | |
---|
| 2551 | // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying |
---|
| 2552 | // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the |
---|
| 2553 | // kids and when we do that and know the child, in the child we will remember the parent we came from. |
---|
| 2554 | // This way all the parents/children relations are saved in both the parent and the child. |
---|
| 2555 | if(!horiz_ref->kids_rel_addresses.empty()) |
---|
| 2556 | { |
---|
| 2557 | for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++) |
---|
| 2558 | { |
---|
| 2559 | polyhedron* new_kid1 = new_statistic1->rows[j-1]; |
---|
| 2560 | polyhedron* new_kid2 = new_statistic2->rows[j-1]; |
---|
| 2561 | |
---|
| 2562 | // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but |
---|
| 2563 | // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline. |
---|
| 2564 | if(*kid_ref) |
---|
| 2565 | { |
---|
| 2566 | for(int k = 1;k<=(*kid_ref);k++) |
---|
| 2567 | { |
---|
| 2568 | new_kid1=new_kid1->next_poly; |
---|
| 2569 | new_kid2=new_kid2->next_poly; |
---|
| 2570 | } |
---|
| 2571 | } |
---|
| 2572 | |
---|
| 2573 | // find the child and save the relation to the parent |
---|
| 2574 | current_copy1->children.push_back(new_kid1); |
---|
| 2575 | current_copy2->children.push_back(new_kid2); |
---|
| 2576 | |
---|
| 2577 | // in the child save the parents' address |
---|
| 2578 | new_kid1->parents.push_back(current_copy1); |
---|
| 2579 | new_kid2->parents.push_back(current_copy2); |
---|
| 2580 | } |
---|
| 2581 | |
---|
| 2582 | // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the |
---|
| 2583 | // Hasse diagram again) |
---|
| 2584 | horiz_ref->kids_rel_addresses.clear(); |
---|
| 2585 | } |
---|
| 2586 | // If there were no children previously, we are copying a polyhedron that has been a vertex before. |
---|
| 2587 | // In this case it is a segment now and it will have a relation to its mother (copywise) and to the |
---|
| 2588 | // newly created point. Here we create the connection to the new point, again from both sides. |
---|
| 2589 | else |
---|
| 2590 | { |
---|
| 2591 | // Add the address of the new point in the former vertex |
---|
| 2592 | current_copy1->children.push_back(new_point1); |
---|
| 2593 | current_copy2->children.push_back(new_point2); |
---|
| 2594 | |
---|
| 2595 | // Add the address of the former vertex in the new point |
---|
| 2596 | new_point1->parents.push_back(current_copy1); |
---|
| 2597 | new_point2->parents.push_back(current_copy2); |
---|
| 2598 | } |
---|
| 2599 | |
---|
| 2600 | // Save the mother in its offspring |
---|
| 2601 | current_copy1->children.push_back(horiz_ref); |
---|
| 2602 | current_copy2->children.push_back(horiz_ref); |
---|
| 2603 | |
---|
| 2604 | // Save the offspring in its mother |
---|
| 2605 | horiz_ref->parents.push_back(current_copy1); |
---|
| 2606 | horiz_ref->parents.push_back(current_copy2); |
---|
| 2607 | |
---|
| 2608 | |
---|
| 2609 | // Add the copies into the relevant statistic. The statistic will later be appended to the previous |
---|
| 2610 | // Hasse diagram |
---|
| 2611 | new_statistic1->append_polyhedron(j,current_copy1); |
---|
| 2612 | new_statistic2->append_polyhedron(j,current_copy2); |
---|
| 2613 | |
---|
| 2614 | // Raise the count in the vector of polyhedrons |
---|
| 2615 | element_number++; |
---|
| 2616 | |
---|
| 2617 | } |
---|
| 2618 | |
---|
| 2619 | } |
---|
| 2620 | |
---|
| 2621 | /* |
---|
| 2622 | statistic.begin()->push_back(new_point1); |
---|
| 2623 | statistic.begin()->push_back(new_point2); |
---|
| 2624 | */ |
---|
| 2625 | |
---|
| 2626 | statistic.append_polyhedron(0, new_point1); |
---|
| 2627 | statistic.append_polyhedron(0, new_point2); |
---|
| 2628 | |
---|
| 2629 | // Merge the new statistics into the old one. This will either be the final statistic or we will |
---|
| 2630 | // reenter the widening loop. |
---|
| 2631 | for(int j=0;j<new_statistic1->size();j++) |
---|
| 2632 | { |
---|
| 2633 | /* |
---|
| 2634 | if(j+1==statistic.size()) |
---|
| 2635 | { |
---|
| 2636 | list<polyhedron*> support; |
---|
| 2637 | statistic.push_back(support); |
---|
| 2638 | } |
---|
| 2639 | |
---|
| 2640 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); |
---|
| 2641 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end()); |
---|
| 2642 | */ |
---|
| 2643 | statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]); |
---|
| 2644 | statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]); |
---|
[1268] | 2645 | } |
---|
[1266] | 2646 | } |
---|
| 2647 | |
---|
| 2648 | /* |
---|
| 2649 | vector<list<toprow*>> toprow_statistic; |
---|
| 2650 | int line_count = 0; |
---|
| 2651 | |
---|
| 2652 | for(vector<list<polyhedron*>>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++) |
---|
| 2653 | { |
---|
| 2654 | list<toprow*> support_list; |
---|
| 2655 | toprow_statistic.push_back(support_list); |
---|
| 2656 | |
---|
| 2657 | for(list<polyhedron*>::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++) |
---|
| 2658 | { |
---|
| 2659 | toprow* support_top = (toprow*)(*polyhedron_ref2); |
---|
| 2660 | |
---|
| 2661 | toprow_statistic[line_count].push_back(support_top); |
---|
| 2662 | } |
---|
| 2663 | |
---|
| 2664 | line_count++; |
---|
| 2665 | }*/ |
---|
| 2666 | |
---|
| 2667 | /* |
---|
| 2668 | vector<int> sizevector; |
---|
| 2669 | for(int s = 0;s<statistic.size();s++) |
---|
| 2670 | { |
---|
| 2671 | sizevector.push_back(statistic.row_size(s)); |
---|
| 2672 | } |
---|
| 2673 | */ |
---|
| 2674 | |
---|
| 2675 | } |
---|
[1270] | 2676 | |
---|
| 2677 | }; |
---|
| 2678 | |
---|
| 2679 | |
---|
[1300] | 2680 | |
---|
[1270] | 2681 | //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density |
---|
[1300] | 2682 | class RARX //: public BM |
---|
[1270] | 2683 | { |
---|
| 2684 | private: |
---|
[1336] | 2685 | bool has_constant; |
---|
[1270] | 2686 | |
---|
[1301] | 2687 | int window_size; |
---|
[1300] | 2688 | |
---|
[1301] | 2689 | list<vec> conditions; |
---|
| 2690 | |
---|
[1270] | 2691 | public: |
---|
[1301] | 2692 | emlig* posterior; |
---|
| 2693 | |
---|
[1336] | 2694 | RARX(int number_of_parameters, const int window_size, bool has_constant)//:BM() |
---|
[1270] | 2695 | { |
---|
[1336] | 2696 | this->has_constant = has_constant; |
---|
| 2697 | |
---|
[1300] | 2698 | posterior = new emlig(number_of_parameters); |
---|
| 2699 | |
---|
[1301] | 2700 | this->window_size = window_size; |
---|
[1270] | 2701 | }; |
---|
| 2702 | |
---|
[1336] | 2703 | void bayes(itpp::vec yt) |
---|
[1270] | 2704 | { |
---|
[1336] | 2705 | if(has_constant) |
---|
| 2706 | { |
---|
| 2707 | int c_size = yt.size(); |
---|
| 2708 | |
---|
| 2709 | yt.ins(c_size,1.0); |
---|
| 2710 | } |
---|
| 2711 | |
---|
| 2712 | if(yt.size() == posterior->number_of_parameters+1) |
---|
| 2713 | { |
---|
| 2714 | conditions.push_back(yt); |
---|
| 2715 | } |
---|
| 2716 | else |
---|
| 2717 | { |
---|
| 2718 | throw new exception("Wrong condition size for bayesian data update!"); |
---|
| 2719 | } |
---|
[1301] | 2720 | |
---|
| 2721 | //posterior->step_me(0); |
---|
[1270] | 2722 | |
---|
[1320] | 2723 | /// \TODO tohle je spatne, tady musi byt jiny vypocet poctu podminek, kdyby nejaka byla multiplicitni, tak tohle bude spatne |
---|
[1301] | 2724 | if(conditions.size()>window_size && window_size!=0) |
---|
| 2725 | { |
---|
| 2726 | posterior->add_and_remove_condition(yt,conditions.front()); |
---|
| 2727 | conditions.pop_front(); |
---|
| 2728 | |
---|
| 2729 | //posterior->step_me(1); |
---|
| 2730 | } |
---|
| 2731 | else |
---|
| 2732 | { |
---|
| 2733 | posterior->add_condition(yt); |
---|
| 2734 | } |
---|
[1349] | 2735 | |
---|
| 2736 | |
---|
[1301] | 2737 | |
---|
[1270] | 2738 | } |
---|
| 2739 | |
---|
[1300] | 2740 | }; |
---|
[1270] | 2741 | |
---|
| 2742 | |
---|
| 2743 | |
---|
| 2744 | #endif //TRAGE_H |
---|