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 <limits> |
---|
12 | #include <vector> |
---|
13 | #include <algorithm> |
---|
14 | |
---|
15 | using namespace bdm; |
---|
16 | using namespace std; |
---|
17 | using namespace itpp; |
---|
18 | |
---|
19 | const double max_range = numeric_limits<double>::max()/10e-5; |
---|
20 | |
---|
21 | enum actions {MERGE, SPLIT}; |
---|
22 | |
---|
23 | class polyhedron; |
---|
24 | class vertex; |
---|
25 | |
---|
26 | /// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram |
---|
27 | /// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created. |
---|
28 | class polyhedron |
---|
29 | { |
---|
30 | /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of |
---|
31 | /// more than just the necessary number of conditions. For example if a newly created line passes through an already |
---|
32 | /// existing point, the points multiplicity will rise by 1. |
---|
33 | int multiplicity; |
---|
34 | |
---|
35 | int split_state; |
---|
36 | |
---|
37 | int merge_state; |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | public: |
---|
42 | /// A list of polyhedrons parents within the Hasse diagram. |
---|
43 | vector<polyhedron*> parents; |
---|
44 | |
---|
45 | /// A list of polyhedrons children withing the Hasse diagram. |
---|
46 | vector<polyhedron*> children; |
---|
47 | |
---|
48 | /// All the vertices of the given polyhedron |
---|
49 | vector<vertex*> vertices; |
---|
50 | |
---|
51 | /// A list used for storing children that lie in the positive region related to a certain condition |
---|
52 | vector<polyhedron*> positivechildren; |
---|
53 | |
---|
54 | /// A list used for storing children that lie in the negative region related to a certain condition |
---|
55 | vector<polyhedron*> negativechildren; |
---|
56 | |
---|
57 | /// Children intersecting the condition |
---|
58 | vector<polyhedron*> neutralchildren; |
---|
59 | |
---|
60 | bool totally_neutral; |
---|
61 | |
---|
62 | vector<polyhedron*> mergechildren; |
---|
63 | |
---|
64 | polyhedron* positiveparent; |
---|
65 | |
---|
66 | polyhedron* negativeparent; |
---|
67 | |
---|
68 | int message_counter; |
---|
69 | |
---|
70 | /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. |
---|
71 | vector<vector<vertex*>> triangulations; |
---|
72 | |
---|
73 | /// A list of relative addresses serving for Hasse diagram construction. |
---|
74 | vector<int> kids_rel_addresses; |
---|
75 | |
---|
76 | /// Default constructor |
---|
77 | polyhedron() |
---|
78 | { |
---|
79 | multiplicity = 1; |
---|
80 | |
---|
81 | message_counter = 0; |
---|
82 | |
---|
83 | totally_neutral = NULL; |
---|
84 | } |
---|
85 | |
---|
86 | /// Setter for raising multiplicity |
---|
87 | void raise_multiplicity() |
---|
88 | { |
---|
89 | multiplicity++; |
---|
90 | } |
---|
91 | |
---|
92 | /// Setter for lowering multiplicity |
---|
93 | void lower_multiplicity() |
---|
94 | { |
---|
95 | multiplicity--; |
---|
96 | } |
---|
97 | |
---|
98 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
---|
99 | int operator==(polyhedron polyhedron2) |
---|
100 | { |
---|
101 | return true; |
---|
102 | } |
---|
103 | |
---|
104 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
---|
105 | int operator<(polyhedron polyhedron2) |
---|
106 | { |
---|
107 | return false; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | void set_state(double state_indicator, actions action) |
---|
113 | { |
---|
114 | switch(action) |
---|
115 | { |
---|
116 | case MERGE: |
---|
117 | merge_state = (int)sign(state_indicator); |
---|
118 | break; |
---|
119 | case SPLIT: |
---|
120 | split_state = (int)sign(state_indicator); |
---|
121 | break; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | int get_state(actions action) |
---|
126 | { |
---|
127 | switch(action) |
---|
128 | { |
---|
129 | case MERGE: |
---|
130 | return merge_state; |
---|
131 | break; |
---|
132 | case SPLIT: |
---|
133 | return split_state; |
---|
134 | break; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | int number_of_children() |
---|
139 | { |
---|
140 | return children.size(); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | }; |
---|
145 | |
---|
146 | /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse |
---|
147 | /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. |
---|
148 | class vertex : public polyhedron |
---|
149 | { |
---|
150 | /// A dynamic array representing coordinates of the vertex |
---|
151 | vec coordinates; |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | public: |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | /// Default constructor |
---|
160 | vertex(); |
---|
161 | |
---|
162 | /// Constructor of a vertex from a set of coordinates |
---|
163 | vertex(vec coordinates) |
---|
164 | { |
---|
165 | this->coordinates = coordinates; |
---|
166 | } |
---|
167 | |
---|
168 | /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter |
---|
169 | /// space of certain dimension is established, but the dimension is not known when the vertex is created. |
---|
170 | void push_coordinate(double coordinate) |
---|
171 | { |
---|
172 | coordinates = concat(coordinates,coordinate); |
---|
173 | } |
---|
174 | |
---|
175 | /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer |
---|
176 | /// (not given by reference), but a new copy is created (they are given by value). |
---|
177 | vec get_coordinates() |
---|
178 | { |
---|
179 | return coordinates; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | }; |
---|
184 | |
---|
185 | /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates |
---|
186 | /// it from polyhedrons in other rows. |
---|
187 | class toprow : public polyhedron |
---|
188 | { |
---|
189 | |
---|
190 | public: |
---|
191 | /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation |
---|
192 | vec condition; |
---|
193 | |
---|
194 | /// Default constructor |
---|
195 | toprow(); |
---|
196 | |
---|
197 | /// Constructor creating a toprow from the condition |
---|
198 | toprow(vec condition) |
---|
199 | { |
---|
200 | this->condition = condition; |
---|
201 | } |
---|
202 | |
---|
203 | }; |
---|
204 | |
---|
205 | class condition |
---|
206 | { |
---|
207 | public: |
---|
208 | vec value; |
---|
209 | |
---|
210 | int multiplicity; |
---|
211 | |
---|
212 | condition(vec value) |
---|
213 | { |
---|
214 | this->value = value; |
---|
215 | multiplicity = 1; |
---|
216 | } |
---|
217 | }; |
---|
218 | |
---|
219 | |
---|
220 | //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density |
---|
221 | class emlig // : eEF |
---|
222 | { |
---|
223 | |
---|
224 | /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result |
---|
225 | /// of data update from Bayesian estimation or set by the user if this emlig is a prior density |
---|
226 | vector<vector<polyhedron*>> statistic; |
---|
227 | |
---|
228 | vector<vector<polyhedron*>> for_splitting; |
---|
229 | |
---|
230 | vector<vector<polyhedron*>> for_merging; |
---|
231 | |
---|
232 | vector<condition*> conditions; |
---|
233 | |
---|
234 | double normalization_factor; |
---|
235 | |
---|
236 | void alter_toprow_conditions(vec condition, bool should_be_added) |
---|
237 | { |
---|
238 | for(vector<polyhedron*>::iterator horiz_ref = statistic[statistic.size()-1].begin();horiz_ref<statistic[statistic.size()-1].end();horiz_ref++) |
---|
239 | { |
---|
240 | double product = 0; |
---|
241 | |
---|
242 | vector<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin(); |
---|
243 | |
---|
244 | do |
---|
245 | { |
---|
246 | product = (*vertex_ref)->get_coordinates()*condition; |
---|
247 | } |
---|
248 | while(product == 0); |
---|
249 | |
---|
250 | if((product>0 && should_be_added)||(product<0 && !should_be_added)) |
---|
251 | { |
---|
252 | ((toprow*) (*horiz_ref))->condition += condition; |
---|
253 | } |
---|
254 | else |
---|
255 | { |
---|
256 | ((toprow*) (*horiz_ref))->condition -= condition; |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | void send_state_message(polyhedron* sender, bool shouldsplit, bool shouldmerge, int level) |
---|
263 | { |
---|
264 | if(shouldsplit||shouldmerge) |
---|
265 | { |
---|
266 | for(vector<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator<sender->parents.end();parent_iterator++) |
---|
267 | { |
---|
268 | polyhedron* current_parent = *parent_iterator; |
---|
269 | |
---|
270 | current_parent->message_counter++; |
---|
271 | |
---|
272 | bool is_last = (current_parent->message_counter == current_parent->number_of_children()); |
---|
273 | |
---|
274 | if(shouldmerge) |
---|
275 | { |
---|
276 | int child_state = sender->get_state(MERGE); |
---|
277 | int parent_state = current_parent->get_state(MERGE); |
---|
278 | |
---|
279 | if(parent_state == 0) |
---|
280 | { |
---|
281 | current_parent->set_state(child_state, MERGE); |
---|
282 | |
---|
283 | if(child_state == 0) |
---|
284 | { |
---|
285 | current_parent->mergechildren.push_back(sender); |
---|
286 | } |
---|
287 | } |
---|
288 | else |
---|
289 | { |
---|
290 | if(child_state == 0) |
---|
291 | { |
---|
292 | if(parent_state > 0) |
---|
293 | { |
---|
294 | sender->positiveparent = current_parent; |
---|
295 | } |
---|
296 | else |
---|
297 | { |
---|
298 | sender->negativeparent = current_parent; |
---|
299 | } |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | if(is_last) |
---|
304 | { |
---|
305 | if(parent_state > 0) |
---|
306 | { |
---|
307 | for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++) |
---|
308 | { |
---|
309 | (*merge_child)->positiveparent = current_parent; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | if(parent_state < 0) |
---|
314 | { |
---|
315 | for(vector<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child < current_parent->mergechildren.end();merge_child++) |
---|
316 | { |
---|
317 | (*merge_child)->negativeparent = current_parent; |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | if(parent_state == 0) |
---|
322 | { |
---|
323 | for_merging[level+1].push_back(current_parent); |
---|
324 | } |
---|
325 | |
---|
326 | current_parent->mergechildren.clear(); |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | } |
---|
331 | |
---|
332 | if(shouldsplit) |
---|
333 | { |
---|
334 | switch(sender->get_state(SPLIT)) |
---|
335 | { |
---|
336 | case 1: |
---|
337 | current_parent->positivechildren.push_back(sender); |
---|
338 | break; |
---|
339 | case 0: |
---|
340 | current_parent->neutralchildren.push_back(sender); |
---|
341 | |
---|
342 | if(current_parent->totally_neutral == NULL) |
---|
343 | { |
---|
344 | current_parent->totally_neutral = sender->totally_neutral; |
---|
345 | } |
---|
346 | else |
---|
347 | { |
---|
348 | current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; |
---|
349 | } |
---|
350 | |
---|
351 | break; |
---|
352 | case -1: |
---|
353 | current_parent->negativechildren.push_back(sender); |
---|
354 | break; |
---|
355 | } |
---|
356 | |
---|
357 | if(is_last) |
---|
358 | { |
---|
359 | if((current_parent->negativechildren.size()>0&¤t_parent->positivechildren.size()>0)|| |
---|
360 | (current_parent->neutralchildren.size()>0&¤t_parent->totally_neutral==false)) |
---|
361 | { |
---|
362 | |
---|
363 | for_splitting[level+1].push_back(current_parent); |
---|
364 | |
---|
365 | current_parent->set_state(0, SPLIT); |
---|
366 | } |
---|
367 | else if(current_parent->negativechildren.size()>0) |
---|
368 | { |
---|
369 | current_parent->set_state(-1, SPLIT); |
---|
370 | |
---|
371 | current_parent->negativechildren.clear(); |
---|
372 | current_parent->neutralchildren.clear(); |
---|
373 | |
---|
374 | } |
---|
375 | else if(current_parent->positivechildren.size()>0) |
---|
376 | { |
---|
377 | current_parent->set_state(1, SPLIT); |
---|
378 | |
---|
379 | current_parent->positivechildren.clear(); |
---|
380 | current_parent->neutralchildren.clear(); |
---|
381 | } |
---|
382 | else |
---|
383 | { |
---|
384 | current_parent->raise_multiplicity(); |
---|
385 | |
---|
386 | current_parent->neutralchildren.clear(); |
---|
387 | } |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | if(is_last) |
---|
392 | { |
---|
393 | send_state_message(current_parent,shouldsplit,shouldmerge,level+1); |
---|
394 | } |
---|
395 | |
---|
396 | } |
---|
397 | |
---|
398 | } |
---|
399 | } |
---|
400 | |
---|
401 | public: |
---|
402 | |
---|
403 | /// A default constructor creates an emlig with predefined statistic representing only the range of the given |
---|
404 | /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. |
---|
405 | emlig(int number_of_parameters) |
---|
406 | { |
---|
407 | create_statistic(number_of_parameters); |
---|
408 | |
---|
409 | for(int i = 0;i<statistic.size();i++) |
---|
410 | { |
---|
411 | vector<polyhedron*> empty_split; |
---|
412 | vector<polyhedron*> empty_merge; |
---|
413 | |
---|
414 | for_splitting.push_back(empty_split); |
---|
415 | for_merging.push_back(empty_merge); |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a |
---|
420 | /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. |
---|
421 | emlig(vector<vector<polyhedron*>> statistic) |
---|
422 | { |
---|
423 | this->statistic = statistic; |
---|
424 | } |
---|
425 | |
---|
426 | void add_condition(vec toadd) |
---|
427 | { |
---|
428 | vec null_vector = ""; |
---|
429 | |
---|
430 | add_and_remove_condition(toadd, null_vector); |
---|
431 | } |
---|
432 | |
---|
433 | void remove_condition(vec toremove) |
---|
434 | { |
---|
435 | vec null_vector = ""; |
---|
436 | |
---|
437 | add_and_remove_condition(null_vector, toremove); |
---|
438 | |
---|
439 | } |
---|
440 | |
---|
441 | void add_and_remove_condition(vec toadd, vec toremove) |
---|
442 | { |
---|
443 | bool should_remove = (toremove.size() != 0); |
---|
444 | bool should_add = (toadd.size() != 0); |
---|
445 | |
---|
446 | vector<condition*>::iterator toremove_ref = conditions.end(); |
---|
447 | bool condition_should_be_added = false; |
---|
448 | |
---|
449 | for(vector<condition*>::iterator ref = conditions.begin();ref<conditions.end();ref++) |
---|
450 | { |
---|
451 | if(should_remove) |
---|
452 | { |
---|
453 | if((*ref)->value == toremove) |
---|
454 | { |
---|
455 | if((*ref)->multiplicity>1) |
---|
456 | { |
---|
457 | (*ref)->multiplicity--; |
---|
458 | |
---|
459 | alter_toprow_conditions(toremove,false); |
---|
460 | |
---|
461 | should_remove = false; |
---|
462 | } |
---|
463 | else |
---|
464 | { |
---|
465 | toremove_ref = ref; |
---|
466 | } |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | if(should_add) |
---|
471 | { |
---|
472 | if((*ref)->value == toadd) |
---|
473 | { |
---|
474 | (*ref)->multiplicity++; |
---|
475 | |
---|
476 | alter_toprow_conditions(toadd,true); |
---|
477 | |
---|
478 | should_add = false; |
---|
479 | } |
---|
480 | else |
---|
481 | { |
---|
482 | condition_should_be_added = true; |
---|
483 | } |
---|
484 | } |
---|
485 | } |
---|
486 | |
---|
487 | if(toremove_ref!=conditions.end()) |
---|
488 | { |
---|
489 | conditions.erase(toremove_ref); |
---|
490 | } |
---|
491 | |
---|
492 | if(condition_should_be_added) |
---|
493 | { |
---|
494 | conditions.push_back(new condition(toadd)); |
---|
495 | } |
---|
496 | |
---|
497 | |
---|
498 | |
---|
499 | for(vector<polyhedron*>::iterator horizontal_position = statistic[0].begin();horizontal_position<statistic[0].end();horizontal_position++) |
---|
500 | { |
---|
501 | vertex* current_vertex = (vertex*)(*horizontal_position); |
---|
502 | |
---|
503 | if(should_add||should_remove) |
---|
504 | { |
---|
505 | vec appended_vec = current_vertex->get_coordinates(); |
---|
506 | appended_vec.ins(0,1.0); |
---|
507 | |
---|
508 | if(should_add) |
---|
509 | { |
---|
510 | current_vertex->set_state(toadd*appended_vec,SPLIT); |
---|
511 | } |
---|
512 | |
---|
513 | if(should_remove) |
---|
514 | { |
---|
515 | current_vertex->set_state(toremove*current_vertex->get_coordinates(),MERGE); |
---|
516 | } |
---|
517 | |
---|
518 | if(current_vertex->get_state(MERGE) == 0) |
---|
519 | { |
---|
520 | for_merging[0].push_back(current_vertex); |
---|
521 | } |
---|
522 | } |
---|
523 | |
---|
524 | send_state_message(current_vertex, should_add, should_remove, 0); |
---|
525 | } |
---|
526 | |
---|
527 | for(vector<vector<polyhedron*>>::iterator vert_ref = for_splitting.begin();vert_ref<for_splitting.end();vert_ref++) |
---|
528 | { |
---|
529 | int original_size = (*vert_ref).size(); |
---|
530 | |
---|
531 | for(int split_counter = 0;split_counter<original_size;split_counter++) |
---|
532 | { |
---|
533 | polyhedron* current_polyhedron = (*vert_ref)[original_size-1-split_counter]; |
---|
534 | |
---|
535 | |
---|
536 | } |
---|
537 | } |
---|
538 | |
---|
539 | |
---|
540 | } |
---|
541 | |
---|
542 | protected: |
---|
543 | |
---|
544 | /// A method for creating plain default statistic representing only the range of the parameter space. |
---|
545 | void create_statistic(int number_of_parameters) |
---|
546 | { |
---|
547 | // An empty vector of coordinates. |
---|
548 | vec origin_coord; |
---|
549 | |
---|
550 | // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. |
---|
551 | vertex *origin = new vertex(origin_coord); |
---|
552 | |
---|
553 | // It has itself as a vertex. There will be a nice use for this when the vertices of its parents are searched in |
---|
554 | // the recursive creation procedure below. |
---|
555 | origin->vertices.push_back(origin); |
---|
556 | |
---|
557 | // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse |
---|
558 | // diagram. First we create a vector of polyhedrons.. |
---|
559 | vector<polyhedron*> origin_vec; |
---|
560 | |
---|
561 | // ..we fill it with the origin.. |
---|
562 | origin_vec.push_back(origin); |
---|
563 | |
---|
564 | // ..and we fill the statistic with the created vector. |
---|
565 | statistic.push_back(origin_vec); |
---|
566 | |
---|
567 | // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to |
---|
568 | // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: |
---|
569 | for(int i=0;i<number_of_parameters;i++) |
---|
570 | { |
---|
571 | // We first will create two new vertices. These will be the borders of the parameter space in the dimension |
---|
572 | // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the |
---|
573 | // right amount of zero cooridnates by reading them from the origin |
---|
574 | vec origin_coord = origin->get_coordinates(); |
---|
575 | |
---|
576 | // And we incorporate the nonzero coordinates into the new cooordinate vectors |
---|
577 | vec origin_coord1 = concat(origin_coord,max_range); |
---|
578 | vec origin_coord2 = concat(origin_coord,-max_range); |
---|
579 | |
---|
580 | // Now we create the points |
---|
581 | vertex *new_point1 = new vertex(origin_coord1); |
---|
582 | vertex *new_point2 = new vertex(origin_coord2); |
---|
583 | |
---|
584 | //********************************************************************************************************* |
---|
585 | // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old |
---|
586 | // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points |
---|
587 | // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons |
---|
588 | // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is |
---|
589 | // connected to the first (second) newly created vertex by a parent-child relation. |
---|
590 | //********************************************************************************************************* |
---|
591 | |
---|
592 | |
---|
593 | // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram |
---|
594 | vector<vector<polyhedron*>> new_statistic1; |
---|
595 | vector<vector<polyhedron*>> new_statistic2; |
---|
596 | |
---|
597 | // Copy the statistic by rows |
---|
598 | for(int j=0;j<statistic.size();j++) |
---|
599 | { |
---|
600 | // an element counter |
---|
601 | int element_number = 0; |
---|
602 | |
---|
603 | vector<polyhedron*> supportnew_1; |
---|
604 | vector<polyhedron*> supportnew_2; |
---|
605 | |
---|
606 | new_statistic1.push_back(supportnew_1); |
---|
607 | new_statistic2.push_back(supportnew_2); |
---|
608 | |
---|
609 | // for each polyhedron in the given row |
---|
610 | for(vector<polyhedron*>::iterator horiz_ref = statistic[j].begin();horiz_ref<statistic[j].end();horiz_ref++) |
---|
611 | { |
---|
612 | // Append an extra zero coordinate to each of the vertices for the new dimension |
---|
613 | // If j==0 => we loop through vertices |
---|
614 | if(j == 0) |
---|
615 | { |
---|
616 | // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates |
---|
617 | ((vertex*) (*horiz_ref))->push_coordinate(0); |
---|
618 | } |
---|
619 | |
---|
620 | // if it has parents |
---|
621 | if(!(*horiz_ref)->parents.empty()) |
---|
622 | { |
---|
623 | // save the relative address of this child in a vector kids_rel_addresses of all its parents. |
---|
624 | // This information will later be used for copying the whole Hasse diagram with each of the |
---|
625 | // relations contained within. |
---|
626 | for(vector<polyhedron*>::iterator parent_ref = (*horiz_ref)->parents.begin();parent_ref < (*horiz_ref)->parents.end();parent_ref++) |
---|
627 | { |
---|
628 | (*parent_ref)->kids_rel_addresses.push_back(element_number); |
---|
629 | } |
---|
630 | } |
---|
631 | |
---|
632 | // ************************************************************************************************** |
---|
633 | // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron |
---|
634 | // will be created as a toprow, but this information will be later forgotten and only the polyhedrons |
---|
635 | // in the top row of the Hasse diagram will be considered toprow for later use. |
---|
636 | // ************************************************************************************************** |
---|
637 | |
---|
638 | // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic |
---|
639 | // this condition will be a vector of zeros. There are two vectors, because we need two copies of |
---|
640 | // the original Hasse diagram. |
---|
641 | vec vec1(i+2); |
---|
642 | vec1.zeros(); |
---|
643 | |
---|
644 | vec vec2(i+2); |
---|
645 | vec2.zeros(); |
---|
646 | |
---|
647 | // We create a new toprow with the previously specified condition. |
---|
648 | toprow *current_copy1 = new toprow(vec1); |
---|
649 | toprow *current_copy2 = new toprow(vec2); |
---|
650 | |
---|
651 | // The vertices of the copies will be inherited, because there will be a parent/child relation |
---|
652 | // between each polyhedron and its offspring (comming from the copy) and a parent has all the |
---|
653 | // vertices of its child plus more. |
---|
654 | for(vector<vertex*>::iterator vert_ref = (*horiz_ref)->vertices.begin();vert_ref<(*horiz_ref)->vertices.end();vert_ref++) |
---|
655 | { |
---|
656 | current_copy1->vertices.push_back(*vert_ref); |
---|
657 | current_copy2->vertices.push_back(*vert_ref); |
---|
658 | } |
---|
659 | |
---|
660 | // The only new vertex of the offspring should be the newly created point. |
---|
661 | current_copy1->vertices.push_back(new_point1); |
---|
662 | current_copy2->vertices.push_back(new_point2); |
---|
663 | |
---|
664 | // This method guarantees that each polyhedron is already triangulated, therefore its triangulation |
---|
665 | // is only one set of vertices and it is the set of all its vertices. |
---|
666 | current_copy1->triangulations.push_back(current_copy1->vertices); |
---|
667 | current_copy2->triangulations.push_back(current_copy2->vertices); |
---|
668 | |
---|
669 | // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying |
---|
670 | // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the |
---|
671 | // kids and when we do that and know the child, in the child we will remember the parent we came from. |
---|
672 | // This way all the parents/children relations are saved in both the parent and the child. |
---|
673 | if(!(*horiz_ref)->kids_rel_addresses.empty()) |
---|
674 | { |
---|
675 | for(vector<int>::iterator kid_ref = (*horiz_ref)->kids_rel_addresses.begin();kid_ref<(*horiz_ref)->kids_rel_addresses.end();kid_ref++) |
---|
676 | { |
---|
677 | // find the child and save the relation to the parent |
---|
678 | current_copy1->children.push_back(new_statistic1[j-1][(*kid_ref)]); |
---|
679 | current_copy2->children.push_back(new_statistic2[j-1][(*kid_ref)]); |
---|
680 | |
---|
681 | // in the child save the parents' address |
---|
682 | new_statistic1[j-1][(*kid_ref)]->parents.push_back(current_copy1); |
---|
683 | new_statistic2[j-1][(*kid_ref)]->parents.push_back(current_copy2); |
---|
684 | } |
---|
685 | |
---|
686 | // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the |
---|
687 | // Hasse diagram again) |
---|
688 | (*horiz_ref)->kids_rel_addresses.clear(); |
---|
689 | } |
---|
690 | // If there were no children previously, we are copying a polyhedron that has been a vertex before. |
---|
691 | // In this case it is a segment now and it will have a relation to its mother (copywise) and to the |
---|
692 | // newly created point. Here we create the connection to the new point, again from both sides. |
---|
693 | else |
---|
694 | { |
---|
695 | // Add the address of the new point in the former vertex |
---|
696 | current_copy1->children.push_back(new_point1); |
---|
697 | current_copy2->children.push_back(new_point2); |
---|
698 | |
---|
699 | // Add the address of the former vertex in the new point |
---|
700 | new_point1->parents.push_back(current_copy1); |
---|
701 | new_point2->parents.push_back(current_copy2); |
---|
702 | } |
---|
703 | |
---|
704 | // Save the mother in its offspring |
---|
705 | current_copy1->children.push_back(*horiz_ref); |
---|
706 | current_copy2->children.push_back(*horiz_ref); |
---|
707 | |
---|
708 | // Save the offspring in its mother |
---|
709 | (*horiz_ref)->parents.push_back(current_copy1); |
---|
710 | (*horiz_ref)->parents.push_back(current_copy2); |
---|
711 | |
---|
712 | |
---|
713 | // Add the copies into the relevant statistic. The statistic will later be appended to the previous |
---|
714 | // Hasse diagram |
---|
715 | new_statistic1[j].push_back(current_copy1); |
---|
716 | new_statistic2[j].push_back(current_copy2); |
---|
717 | |
---|
718 | // Raise the count in the vector of polyhedrons |
---|
719 | element_number++; |
---|
720 | |
---|
721 | } |
---|
722 | } |
---|
723 | |
---|
724 | statistic[0].push_back(new_point1); |
---|
725 | statistic[0].push_back(new_point2); |
---|
726 | |
---|
727 | // Merge the new statistics into the old one. This will either be the final statistic or we will |
---|
728 | // reenter the widening loop. |
---|
729 | for(int j=0;j<new_statistic1.size();j++) |
---|
730 | { |
---|
731 | if(j+1==statistic.size()) |
---|
732 | { |
---|
733 | vector<polyhedron*> support; |
---|
734 | statistic.push_back(support); |
---|
735 | } |
---|
736 | |
---|
737 | statistic[j+1].insert(statistic[j+1].end(),new_statistic1[j].begin(),new_statistic1[j].end()); |
---|
738 | statistic[j+1].insert(statistic[j+1].end(),new_statistic2[j].begin(),new_statistic2[j].end()); |
---|
739 | } |
---|
740 | } |
---|
741 | } |
---|
742 | |
---|
743 | |
---|
744 | |
---|
745 | |
---|
746 | }; |
---|
747 | |
---|
748 | /* |
---|
749 | |
---|
750 | //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density |
---|
751 | class RARX : public BM |
---|
752 | { |
---|
753 | private: |
---|
754 | |
---|
755 | emlig posterior; |
---|
756 | |
---|
757 | public: |
---|
758 | RARX():BM() |
---|
759 | { |
---|
760 | }; |
---|
761 | |
---|
762 | void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec) |
---|
763 | { |
---|
764 | |
---|
765 | } |
---|
766 | |
---|
767 | };*/ |
---|
768 | |
---|
769 | |
---|
770 | |
---|
771 | #endif //TRAGE_H |
---|