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