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