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 <list> |
---|
14 | #include <algorithm> |
---|
15 | |
---|
16 | using namespace bdm; |
---|
17 | using namespace std; |
---|
18 | using namespace itpp; |
---|
19 | |
---|
20 | const double max_range = numeric_limits<double>::max()/10e-5; |
---|
21 | |
---|
22 | enum actions {MERGE, SPLIT}; |
---|
23 | |
---|
24 | class polyhedron; |
---|
25 | class vertex; |
---|
26 | |
---|
27 | /// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram |
---|
28 | /// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created. |
---|
29 | class polyhedron |
---|
30 | { |
---|
31 | /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of |
---|
32 | /// more than just the necessary number of conditions. For example if a newly created line passes through an already |
---|
33 | /// existing point, the points multiplicity will rise by 1. |
---|
34 | int multiplicity; |
---|
35 | |
---|
36 | int split_state; |
---|
37 | |
---|
38 | int merge_state; |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | public: |
---|
43 | /// A list of polyhedrons parents within the Hasse diagram. |
---|
44 | list<polyhedron*> parents; |
---|
45 | |
---|
46 | /// A list of polyhedrons children withing the Hasse diagram. |
---|
47 | list<polyhedron*> children; |
---|
48 | |
---|
49 | /// All the vertices of the given polyhedron |
---|
50 | list<vertex*> vertices; |
---|
51 | |
---|
52 | /// A list used for storing children that lie in the positive region related to a certain condition |
---|
53 | list<polyhedron*> positivechildren; |
---|
54 | |
---|
55 | /// A list used for storing children that lie in the negative region related to a certain condition |
---|
56 | list<polyhedron*> negativechildren; |
---|
57 | |
---|
58 | /// Children intersecting the condition |
---|
59 | list<polyhedron*> neutralchildren; |
---|
60 | |
---|
61 | list<polyhedron*> totallyneutralgrandchildren; |
---|
62 | |
---|
63 | list<polyhedron*> totallyneutralchildren; |
---|
64 | |
---|
65 | bool totally_neutral; |
---|
66 | |
---|
67 | list<polyhedron*> mergechildren; |
---|
68 | |
---|
69 | polyhedron* positiveparent; |
---|
70 | |
---|
71 | polyhedron* negativeparent; |
---|
72 | |
---|
73 | polyhedron* next_poly; |
---|
74 | |
---|
75 | polyhedron* prev_poly; |
---|
76 | |
---|
77 | int message_counter; |
---|
78 | |
---|
79 | /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. |
---|
80 | list<list<vertex*>> triangulations; |
---|
81 | |
---|
82 | /// A list of relative addresses serving for Hasse diagram construction. |
---|
83 | list<int> kids_rel_addresses; |
---|
84 | |
---|
85 | /// Default constructor |
---|
86 | polyhedron() |
---|
87 | { |
---|
88 | multiplicity = 1; |
---|
89 | |
---|
90 | message_counter = 0; |
---|
91 | |
---|
92 | totally_neutral = NULL; |
---|
93 | } |
---|
94 | |
---|
95 | /// Setter for raising multiplicity |
---|
96 | void raise_multiplicity() |
---|
97 | { |
---|
98 | multiplicity++; |
---|
99 | } |
---|
100 | |
---|
101 | /// Setter for lowering multiplicity |
---|
102 | void lower_multiplicity() |
---|
103 | { |
---|
104 | multiplicity--; |
---|
105 | } |
---|
106 | |
---|
107 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
---|
108 | int operator==(polyhedron polyhedron2) |
---|
109 | { |
---|
110 | return true; |
---|
111 | } |
---|
112 | |
---|
113 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
---|
114 | int operator<(polyhedron polyhedron2) |
---|
115 | { |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | void set_state(double state_indicator, actions action) |
---|
122 | { |
---|
123 | switch(action) |
---|
124 | { |
---|
125 | case MERGE: |
---|
126 | merge_state = (int)sign(state_indicator); |
---|
127 | break; |
---|
128 | case SPLIT: |
---|
129 | split_state = (int)sign(state_indicator); |
---|
130 | break; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | int get_state(actions action) |
---|
135 | { |
---|
136 | switch(action) |
---|
137 | { |
---|
138 | case MERGE: |
---|
139 | return merge_state; |
---|
140 | break; |
---|
141 | case SPLIT: |
---|
142 | return split_state; |
---|
143 | break; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | int number_of_children() |
---|
148 | { |
---|
149 | return children.size(); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | }; |
---|
154 | |
---|
155 | /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse |
---|
156 | /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. |
---|
157 | class vertex : public polyhedron |
---|
158 | { |
---|
159 | /// A dynamic array representing coordinates of the vertex |
---|
160 | vec coordinates; |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | public: |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | /// Default constructor |
---|
169 | vertex(); |
---|
170 | |
---|
171 | /// Constructor of a vertex from a set of coordinates |
---|
172 | vertex(vec coordinates) |
---|
173 | { |
---|
174 | this->coordinates = coordinates; |
---|
175 | } |
---|
176 | |
---|
177 | /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter |
---|
178 | /// space of certain dimension is established, but the dimension is not known when the vertex is created. |
---|
179 | void push_coordinate(double coordinate) |
---|
180 | { |
---|
181 | coordinates = concat(coordinates,coordinate); |
---|
182 | } |
---|
183 | |
---|
184 | /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer |
---|
185 | /// (not given by reference), but a new copy is created (they are given by value). |
---|
186 | vec get_coordinates() |
---|
187 | { |
---|
188 | return coordinates; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | }; |
---|
193 | |
---|
194 | /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates |
---|
195 | /// it from polyhedrons in other rows. |
---|
196 | class toprow : public polyhedron |
---|
197 | { |
---|
198 | |
---|
199 | public: |
---|
200 | /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation |
---|
201 | vec condition; |
---|
202 | |
---|
203 | /// Default constructor |
---|
204 | toprow(){}; |
---|
205 | |
---|
206 | /// Constructor creating a toprow from the condition |
---|
207 | toprow(vec condition) |
---|
208 | { |
---|
209 | this->condition = condition; |
---|
210 | } |
---|
211 | |
---|
212 | }; |
---|
213 | |
---|
214 | class condition |
---|
215 | { |
---|
216 | public: |
---|
217 | vec value; |
---|
218 | |
---|
219 | int multiplicity; |
---|
220 | |
---|
221 | condition(vec value) |
---|
222 | { |
---|
223 | this->value = value; |
---|
224 | multiplicity = 1; |
---|
225 | } |
---|
226 | }; |
---|
227 | |
---|
228 | class c_statistic |
---|
229 | { |
---|
230 | polyhedron* end_poly; |
---|
231 | polyhedron* start_poly; |
---|
232 | |
---|
233 | public: |
---|
234 | vector<polyhedron*> rows; |
---|
235 | |
---|
236 | vector<polyhedron*> row_ends; |
---|
237 | |
---|
238 | c_statistic() |
---|
239 | { |
---|
240 | end_poly = new polyhedron(); |
---|
241 | start_poly = new polyhedron(); |
---|
242 | }; |
---|
243 | |
---|
244 | void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end) |
---|
245 | { |
---|
246 | if(row>((int)rows.size())-1) |
---|
247 | { |
---|
248 | if(row>rows.size()) |
---|
249 | { |
---|
250 | throw new exception("You are trying to append a polyhedron whose children are not in the statistic yet!"); |
---|
251 | return; |
---|
252 | } |
---|
253 | |
---|
254 | rows.push_back(end_poly); |
---|
255 | row_ends.push_back(end_poly); |
---|
256 | } |
---|
257 | |
---|
258 | // POSSIBLE FAILURE: the function is not checking if start and end are connected |
---|
259 | |
---|
260 | if(rows[row] != end_poly) |
---|
261 | { |
---|
262 | appended_start->prev_poly = row_ends[row]; |
---|
263 | row_ends[row]->next_poly = appended_start; |
---|
264 | |
---|
265 | } |
---|
266 | else if((row>0 && rows[row-1]!=end_poly)||row==0) |
---|
267 | { |
---|
268 | appended_start->prev_poly = start_poly; |
---|
269 | rows[row]= appended_start; |
---|
270 | } |
---|
271 | else |
---|
272 | { |
---|
273 | throw new exception("Wrong polyhedron insertion into statistic: missing intermediary polyhedron!"); |
---|
274 | } |
---|
275 | |
---|
276 | appended_end->next_poly = end_poly; |
---|
277 | row_ends[row] = appended_end; |
---|
278 | } |
---|
279 | |
---|
280 | void append_polyhedron(int row, polyhedron* appended_poly) |
---|
281 | { |
---|
282 | append_polyhedron(row,appended_poly,appended_poly); |
---|
283 | } |
---|
284 | |
---|
285 | void insert_polyhedron(int row, polyhedron* inserted_poly, polyhedron* following_poly) |
---|
286 | { |
---|
287 | if(following_poly != end_poly) |
---|
288 | { |
---|
289 | inserted_poly->next_poly = following_poly; |
---|
290 | inserted_poly->prev_poly = following_poly->prev_poly; |
---|
291 | |
---|
292 | if(following_poly->prev_poly == start_poly) |
---|
293 | { |
---|
294 | rows[row] = inserted_poly; |
---|
295 | } |
---|
296 | else |
---|
297 | { |
---|
298 | inserted_poly->prev_poly->next_poly = inserted_poly; |
---|
299 | } |
---|
300 | |
---|
301 | following_poly->prev_poly = inserted_poly; |
---|
302 | } |
---|
303 | else |
---|
304 | { |
---|
305 | this->append_polyhedron(row, inserted_poly); |
---|
306 | } |
---|
307 | |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | |
---|
313 | void delete_polyhedron(int row, polyhedron* deleted_poly) |
---|
314 | { |
---|
315 | if(deleted_poly->prev_poly != start_poly) |
---|
316 | { |
---|
317 | deleted_poly->prev_poly->next_poly = deleted_poly->next_poly; |
---|
318 | } |
---|
319 | else |
---|
320 | { |
---|
321 | rows[row] = deleted_poly->next_poly; |
---|
322 | } |
---|
323 | |
---|
324 | if(deleted_poly->next_poly!=end_poly) |
---|
325 | { |
---|
326 | deleted_poly->next_poly->prev_poly = deleted_poly->prev_poly; |
---|
327 | } |
---|
328 | else |
---|
329 | { |
---|
330 | row_ends[row] = deleted_poly->prev_poly; |
---|
331 | } |
---|
332 | |
---|
333 | |
---|
334 | |
---|
335 | deleted_poly->next_poly = NULL; |
---|
336 | deleted_poly->prev_poly = NULL; |
---|
337 | } |
---|
338 | |
---|
339 | int size() |
---|
340 | { |
---|
341 | return rows.size(); |
---|
342 | } |
---|
343 | |
---|
344 | polyhedron* get_end() |
---|
345 | { |
---|
346 | return end_poly; |
---|
347 | } |
---|
348 | |
---|
349 | polyhedron* get_start() |
---|
350 | { |
---|
351 | return start_poly; |
---|
352 | } |
---|
353 | |
---|
354 | int row_size(int row) |
---|
355 | { |
---|
356 | if(this->size()>row && row>=0) |
---|
357 | { |
---|
358 | int row_size = 0; |
---|
359 | |
---|
360 | for(polyhedron* row_poly = rows[row]; row_poly!=end_poly; row_poly=row_poly->next_poly) |
---|
361 | { |
---|
362 | row_size++; |
---|
363 | } |
---|
364 | |
---|
365 | return row_size; |
---|
366 | } |
---|
367 | else |
---|
368 | { |
---|
369 | throw new exception("There is no row to obtain size from!"); |
---|
370 | } |
---|
371 | } |
---|
372 | }; |
---|
373 | |
---|
374 | |
---|
375 | //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density |
---|
376 | class emlig // : eEF |
---|
377 | { |
---|
378 | |
---|
379 | /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result |
---|
380 | /// of data update from Bayesian estimation or set by the user if this emlig is a prior density |
---|
381 | c_statistic statistic; |
---|
382 | |
---|
383 | vector<list<polyhedron*>> for_splitting; |
---|
384 | |
---|
385 | vector<list<polyhedron*>> for_merging; |
---|
386 | |
---|
387 | list<condition*> conditions; |
---|
388 | |
---|
389 | double normalization_factor; |
---|
390 | |
---|
391 | void alter_toprow_conditions(vec condition, bool should_be_added) |
---|
392 | { |
---|
393 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
394 | { |
---|
395 | double product = 0; |
---|
396 | |
---|
397 | list<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); |
---|
398 | |
---|
399 | do |
---|
400 | { |
---|
401 | product = (*vertex_ref)->get_coordinates()*condition; |
---|
402 | } |
---|
403 | while(product == 0); |
---|
404 | |
---|
405 | if((product>0 && should_be_added)||(product<0 && !should_be_added)) |
---|
406 | { |
---|
407 | ((toprow*) horiz_ref)->condition += condition; |
---|
408 | } |
---|
409 | else |
---|
410 | { |
---|
411 | ((toprow*) horiz_ref)->condition -= condition; |
---|
412 | } |
---|
413 | } |
---|
414 | } |
---|
415 | |
---|
416 | |
---|
417 | void send_state_message(polyhedron* sender, vec toadd, vec toremove, int level) |
---|
418 | { |
---|
419 | |
---|
420 | bool shouldmerge = (toremove.size() != 0); |
---|
421 | bool shouldsplit = (toadd.size() != 0); |
---|
422 | |
---|
423 | if(shouldsplit||shouldmerge) |
---|
424 | { |
---|
425 | for(list<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator!=sender->parents.end();parent_iterator++) |
---|
426 | { |
---|
427 | polyhedron* current_parent = *parent_iterator; |
---|
428 | |
---|
429 | current_parent->message_counter++; |
---|
430 | |
---|
431 | bool is_last = (current_parent->message_counter == current_parent->number_of_children()); |
---|
432 | |
---|
433 | if(shouldmerge) |
---|
434 | { |
---|
435 | int child_state = sender->get_state(MERGE); |
---|
436 | int parent_state = current_parent->get_state(MERGE); |
---|
437 | |
---|
438 | if(parent_state == 0) |
---|
439 | { |
---|
440 | current_parent->set_state(child_state, MERGE); |
---|
441 | |
---|
442 | if(child_state == 0) |
---|
443 | { |
---|
444 | current_parent->mergechildren.push_back(sender); |
---|
445 | } |
---|
446 | } |
---|
447 | else |
---|
448 | { |
---|
449 | if(child_state == 0) |
---|
450 | { |
---|
451 | if(parent_state > 0) |
---|
452 | { |
---|
453 | sender->positiveparent = current_parent; |
---|
454 | } |
---|
455 | else |
---|
456 | { |
---|
457 | sender->negativeparent = current_parent; |
---|
458 | } |
---|
459 | } |
---|
460 | } |
---|
461 | |
---|
462 | if(is_last) |
---|
463 | { |
---|
464 | if(parent_state > 0) |
---|
465 | { |
---|
466 | for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) |
---|
467 | { |
---|
468 | (*merge_child)->positiveparent = current_parent; |
---|
469 | } |
---|
470 | } |
---|
471 | |
---|
472 | if(parent_state < 0) |
---|
473 | { |
---|
474 | for(list<polyhedron*>::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) |
---|
475 | { |
---|
476 | (*merge_child)->negativeparent = current_parent; |
---|
477 | } |
---|
478 | } |
---|
479 | |
---|
480 | if(parent_state == 0) |
---|
481 | { |
---|
482 | for_merging[level+1].push_back(current_parent); |
---|
483 | } |
---|
484 | |
---|
485 | current_parent->mergechildren.clear(); |
---|
486 | } |
---|
487 | |
---|
488 | |
---|
489 | } |
---|
490 | |
---|
491 | if(shouldsplit) |
---|
492 | { |
---|
493 | current_parent->totallyneutralgrandchildren.insert(current_parent->totallyneutralgrandchildren.end(),sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); |
---|
494 | |
---|
495 | switch(sender->get_state(SPLIT)) |
---|
496 | { |
---|
497 | case 1: |
---|
498 | current_parent->positivechildren.push_back(sender); |
---|
499 | break; |
---|
500 | case 0: |
---|
501 | current_parent->neutralchildren.push_back(sender); |
---|
502 | |
---|
503 | if(current_parent->totally_neutral == NULL) |
---|
504 | { |
---|
505 | current_parent->totally_neutral = sender->totally_neutral; |
---|
506 | } |
---|
507 | else |
---|
508 | { |
---|
509 | current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; |
---|
510 | } |
---|
511 | |
---|
512 | if(sender->totally_neutral) |
---|
513 | { |
---|
514 | current_parent->totallyneutralchildren.push_back(sender); |
---|
515 | } |
---|
516 | |
---|
517 | break; |
---|
518 | case -1: |
---|
519 | current_parent->negativechildren.push_back(sender); |
---|
520 | break; |
---|
521 | } |
---|
522 | |
---|
523 | if(is_last) |
---|
524 | { |
---|
525 | unique(current_parent->totallyneutralgrandchildren.begin(),current_parent->totallyneutralgrandchildren.end()); |
---|
526 | |
---|
527 | if((current_parent->negativechildren.size()>0&¤t_parent->positivechildren.size()>0)|| |
---|
528 | (current_parent->neutralchildren.size()>0&¤t_parent->totally_neutral==false)) |
---|
529 | { |
---|
530 | |
---|
531 | for_splitting[level+1].push_back(current_parent); |
---|
532 | |
---|
533 | current_parent->set_state(0, SPLIT); |
---|
534 | } |
---|
535 | else |
---|
536 | { |
---|
537 | if(current_parent->negativechildren.size()>0) |
---|
538 | { |
---|
539 | current_parent->set_state(-1, SPLIT); |
---|
540 | |
---|
541 | ((toprow*)current_parent)->condition-=toadd; |
---|
542 | } |
---|
543 | else if(current_parent->positivechildren.size()>0) |
---|
544 | { |
---|
545 | current_parent->set_state(1, SPLIT); |
---|
546 | |
---|
547 | ((toprow*)current_parent)->condition+=toadd; |
---|
548 | } |
---|
549 | else |
---|
550 | { |
---|
551 | current_parent->raise_multiplicity(); |
---|
552 | } |
---|
553 | |
---|
554 | current_parent->positivechildren.clear(); |
---|
555 | current_parent->negativechildren.clear(); |
---|
556 | current_parent->neutralchildren.clear(); |
---|
557 | current_parent->totallyneutralchildren.clear(); |
---|
558 | current_parent->totallyneutralgrandchildren.clear(); |
---|
559 | current_parent->totally_neutral = NULL; |
---|
560 | } |
---|
561 | } |
---|
562 | } |
---|
563 | |
---|
564 | if(is_last) |
---|
565 | { |
---|
566 | send_state_message(current_parent,toadd,toremove,level+1); |
---|
567 | } |
---|
568 | |
---|
569 | } |
---|
570 | |
---|
571 | } |
---|
572 | } |
---|
573 | |
---|
574 | public: |
---|
575 | |
---|
576 | /// A default constructor creates an emlig with predefined statistic representing only the range of the given |
---|
577 | /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. |
---|
578 | emlig(int number_of_parameters) |
---|
579 | { |
---|
580 | |
---|
581 | create_statistic(number_of_parameters); |
---|
582 | |
---|
583 | for(int i = 0;i<statistic.size();i++) |
---|
584 | { |
---|
585 | list<polyhedron*> empty_split; |
---|
586 | list<polyhedron*> empty_merge; |
---|
587 | |
---|
588 | for_splitting.push_back(empty_split); |
---|
589 | for_merging.push_back(empty_merge); |
---|
590 | } |
---|
591 | } |
---|
592 | |
---|
593 | /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a |
---|
594 | /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. |
---|
595 | emlig(c_statistic statistic) |
---|
596 | { |
---|
597 | this->statistic = statistic; |
---|
598 | } |
---|
599 | |
---|
600 | void add_condition(vec toadd) |
---|
601 | { |
---|
602 | vec null_vector = ""; |
---|
603 | |
---|
604 | add_and_remove_condition(toadd, null_vector); |
---|
605 | } |
---|
606 | |
---|
607 | void remove_condition(vec toremove) |
---|
608 | { |
---|
609 | vec null_vector = ""; |
---|
610 | |
---|
611 | add_and_remove_condition(null_vector, toremove); |
---|
612 | |
---|
613 | } |
---|
614 | |
---|
615 | void add_and_remove_condition(vec toadd, vec toremove) |
---|
616 | { |
---|
617 | bool should_remove = (toremove.size() != 0); |
---|
618 | bool should_add = (toadd.size() != 0); |
---|
619 | |
---|
620 | list<condition*>::iterator toremove_ref = conditions.end(); |
---|
621 | bool condition_should_be_added = false; |
---|
622 | |
---|
623 | for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++) |
---|
624 | { |
---|
625 | if(should_remove) |
---|
626 | { |
---|
627 | if((*ref)->value == toremove) |
---|
628 | { |
---|
629 | if((*ref)->multiplicity>1) |
---|
630 | { |
---|
631 | (*ref)->multiplicity--; |
---|
632 | |
---|
633 | alter_toprow_conditions(toremove,false); |
---|
634 | |
---|
635 | should_remove = false; |
---|
636 | } |
---|
637 | else |
---|
638 | { |
---|
639 | toremove_ref = ref; |
---|
640 | } |
---|
641 | } |
---|
642 | } |
---|
643 | |
---|
644 | if(should_add) |
---|
645 | { |
---|
646 | if((*ref)->value == toadd) |
---|
647 | { |
---|
648 | (*ref)->multiplicity++; |
---|
649 | |
---|
650 | alter_toprow_conditions(toadd,true); |
---|
651 | |
---|
652 | should_add = false; |
---|
653 | } |
---|
654 | else |
---|
655 | { |
---|
656 | condition_should_be_added = true; |
---|
657 | } |
---|
658 | } |
---|
659 | } |
---|
660 | |
---|
661 | if(toremove_ref!=conditions.end()) |
---|
662 | { |
---|
663 | conditions.erase(toremove_ref); |
---|
664 | } |
---|
665 | |
---|
666 | if(condition_should_be_added) |
---|
667 | { |
---|
668 | conditions.push_back(new condition(toadd)); |
---|
669 | } |
---|
670 | |
---|
671 | |
---|
672 | |
---|
673 | for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly) |
---|
674 | { |
---|
675 | vertex* current_vertex = (vertex*)horizontal_position; |
---|
676 | |
---|
677 | if(should_add||should_remove) |
---|
678 | { |
---|
679 | vec appended_vec = current_vertex->get_coordinates(); |
---|
680 | appended_vec.ins(0,-1.0); |
---|
681 | |
---|
682 | if(should_add) |
---|
683 | { |
---|
684 | double local_condition = toadd*appended_vec; |
---|
685 | |
---|
686 | current_vertex->set_state(local_condition,SPLIT); |
---|
687 | |
---|
688 | if(local_condition == 0) |
---|
689 | { |
---|
690 | current_vertex->totally_neutral = true; |
---|
691 | |
---|
692 | current_vertex->raise_multiplicity(); |
---|
693 | } |
---|
694 | } |
---|
695 | |
---|
696 | if(should_remove) |
---|
697 | { |
---|
698 | double local_condition = toremove*appended_vec; |
---|
699 | |
---|
700 | current_vertex->set_state(local_condition,MERGE); |
---|
701 | |
---|
702 | if(local_condition == 0) |
---|
703 | { |
---|
704 | for_merging[0].push_back(current_vertex); |
---|
705 | } |
---|
706 | } |
---|
707 | } |
---|
708 | |
---|
709 | send_state_message(current_vertex, toadd, toremove, 0); |
---|
710 | |
---|
711 | } |
---|
712 | |
---|
713 | if(should_add) |
---|
714 | { |
---|
715 | int k = 1; |
---|
716 | |
---|
717 | vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin(); |
---|
718 | |
---|
719 | for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++) |
---|
720 | { |
---|
721 | |
---|
722 | for(list<polyhedron*>::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++) |
---|
723 | { |
---|
724 | polyhedron* new_totally_neutral_child; |
---|
725 | |
---|
726 | polyhedron* current_polyhedron = (*split_ref); |
---|
727 | |
---|
728 | if(vert_ref == beginning_ref) |
---|
729 | { |
---|
730 | vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates(); |
---|
731 | vec coordinates2 = ((vertex*)(*(current_polyhedron->children.begin()++)))->get_coordinates(); |
---|
732 | coordinates2.ins(0,-1.0); |
---|
733 | |
---|
734 | double t = (-toadd*coordinates2)/(toadd(1,toadd.size()-1)*coordinates1)+1; |
---|
735 | |
---|
736 | vec new_coordinates = coordinates1*t+(coordinates2(1,coordinates2.size()-1)-coordinates1); |
---|
737 | |
---|
738 | vertex* neutral_vertex = new vertex(new_coordinates); |
---|
739 | |
---|
740 | new_totally_neutral_child = neutral_vertex; |
---|
741 | } |
---|
742 | else |
---|
743 | { |
---|
744 | toprow* neutral_toprow = new toprow(); |
---|
745 | |
---|
746 | new_totally_neutral_child = neutral_toprow; |
---|
747 | } |
---|
748 | |
---|
749 | new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(), |
---|
750 | current_polyhedron->totallyneutralgrandchildren.begin(), |
---|
751 | current_polyhedron->totallyneutralgrandchildren.end()); |
---|
752 | |
---|
753 | toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition+toadd); |
---|
754 | toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition-toadd); |
---|
755 | |
---|
756 | positive_poly->children.push_back(new_totally_neutral_child); |
---|
757 | negative_poly->children.push_back(new_totally_neutral_child); |
---|
758 | |
---|
759 | positive_poly->parents.insert(positive_poly->parents.end(), |
---|
760 | current_polyhedron->parents.begin(), |
---|
761 | current_polyhedron->parents.end()); |
---|
762 | |
---|
763 | negative_poly->parents.insert(negative_poly->parents.end(), |
---|
764 | current_polyhedron->parents.begin(), |
---|
765 | current_polyhedron->parents.end()); |
---|
766 | |
---|
767 | positive_poly->children.insert(positive_poly->children.end(), |
---|
768 | current_polyhedron->positivechildren.begin(), |
---|
769 | current_polyhedron->positivechildren.end()); |
---|
770 | |
---|
771 | negative_poly->children.insert(negative_poly->children.end(), |
---|
772 | current_polyhedron->negativechildren.begin(), |
---|
773 | current_polyhedron->negativechildren.end()); |
---|
774 | |
---|
775 | statistic.append_polyhedron(k-1, new_totally_neutral_child); |
---|
776 | |
---|
777 | statistic.insert_polyhedron(k, positive_poly, current_polyhedron); |
---|
778 | statistic.insert_polyhedron(k, negative_poly, current_polyhedron); |
---|
779 | |
---|
780 | statistic.delete_polyhedron(k, current_polyhedron); |
---|
781 | } |
---|
782 | |
---|
783 | k++; |
---|
784 | } |
---|
785 | } |
---|
786 | |
---|
787 | vector<int> sizevector; |
---|
788 | for(int s = 0;s<statistic.size();s++) |
---|
789 | { |
---|
790 | sizevector.push_back(statistic.row_size(s)); |
---|
791 | } |
---|
792 | } |
---|
793 | |
---|
794 | protected: |
---|
795 | |
---|
796 | /// A method for creating plain default statistic representing only the range of the parameter space. |
---|
797 | void create_statistic(int number_of_parameters) |
---|
798 | { |
---|
799 | // An empty vector of coordinates. |
---|
800 | vec origin_coord; |
---|
801 | |
---|
802 | // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. |
---|
803 | vertex *origin = new vertex(origin_coord); |
---|
804 | |
---|
805 | // It has itself as a vertex. There will be a nice use for this when the vertices of its parents are searched in |
---|
806 | // the recursive creation procedure below. |
---|
807 | origin->vertices.push_back(origin); |
---|
808 | |
---|
809 | /* |
---|
810 | // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse |
---|
811 | // diagram. First we create a vector of polyhedrons.. |
---|
812 | list<polyhedron*> origin_vec; |
---|
813 | |
---|
814 | // ..we fill it with the origin.. |
---|
815 | origin_vec.push_back(origin); |
---|
816 | |
---|
817 | // ..and we fill the statistic with the created vector. |
---|
818 | statistic.push_back(origin_vec); |
---|
819 | */ |
---|
820 | |
---|
821 | statistic = *(new c_statistic()); |
---|
822 | |
---|
823 | statistic.append_polyhedron(0, origin); |
---|
824 | |
---|
825 | // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to |
---|
826 | // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: |
---|
827 | for(int i=0;i<number_of_parameters;i++) |
---|
828 | { |
---|
829 | // We first will create two new vertices. These will be the borders of the parameter space in the dimension |
---|
830 | // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the |
---|
831 | // right amount of zero cooridnates by reading them from the origin |
---|
832 | vec origin_coord = origin->get_coordinates(); |
---|
833 | |
---|
834 | // And we incorporate the nonzero coordinates into the new cooordinate vectors |
---|
835 | vec origin_coord1 = concat(origin_coord,max_range); |
---|
836 | vec origin_coord2 = concat(origin_coord,-max_range); |
---|
837 | |
---|
838 | // Now we create the points |
---|
839 | vertex *new_point1 = new vertex(origin_coord1); |
---|
840 | vertex *new_point2 = new vertex(origin_coord2); |
---|
841 | |
---|
842 | //********************************************************************************************************* |
---|
843 | // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old |
---|
844 | // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points |
---|
845 | // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons |
---|
846 | // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is |
---|
847 | // connected to the first (second) newly created vertex by a parent-child relation. |
---|
848 | //********************************************************************************************************* |
---|
849 | |
---|
850 | |
---|
851 | /* |
---|
852 | // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram |
---|
853 | vector<vector<polyhedron*>> new_statistic1; |
---|
854 | vector<vector<polyhedron*>> new_statistic2; |
---|
855 | */ |
---|
856 | |
---|
857 | c_statistic* new_statistic1 = new c_statistic(); |
---|
858 | c_statistic* new_statistic2 = new c_statistic(); |
---|
859 | |
---|
860 | |
---|
861 | // Copy the statistic by rows |
---|
862 | for(int j=0;j<statistic.size();j++) |
---|
863 | { |
---|
864 | |
---|
865 | |
---|
866 | // an element counter |
---|
867 | int element_number = 0; |
---|
868 | |
---|
869 | /* |
---|
870 | vector<polyhedron*> supportnew_1; |
---|
871 | vector<polyhedron*> supportnew_2; |
---|
872 | |
---|
873 | new_statistic1.push_back(supportnew_1); |
---|
874 | new_statistic2.push_back(supportnew_2); |
---|
875 | */ |
---|
876 | |
---|
877 | // for each polyhedron in the given row |
---|
878 | for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
---|
879 | { |
---|
880 | // Append an extra zero coordinate to each of the vertices for the new dimension |
---|
881 | // If vert_ref is at the first index => we loop through vertices |
---|
882 | if(j == 0) |
---|
883 | { |
---|
884 | // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates |
---|
885 | ((vertex*) horiz_ref)->push_coordinate(0); |
---|
886 | } |
---|
887 | /* |
---|
888 | else |
---|
889 | { |
---|
890 | ((toprow*) (*horiz_ref))->condition.ins(0,0); |
---|
891 | }*/ |
---|
892 | |
---|
893 | // if it has parents |
---|
894 | if(!horiz_ref->parents.empty()) |
---|
895 | { |
---|
896 | // save the relative address of this child in a vector kids_rel_addresses of all its parents. |
---|
897 | // This information will later be used for copying the whole Hasse diagram with each of the |
---|
898 | // relations contained within. |
---|
899 | for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++) |
---|
900 | { |
---|
901 | (*parent_ref)->kids_rel_addresses.push_back(element_number); |
---|
902 | } |
---|
903 | } |
---|
904 | |
---|
905 | // ************************************************************************************************** |
---|
906 | // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron |
---|
907 | // will be created as a toprow, but this information will be later forgotten and only the polyhedrons |
---|
908 | // in the top row of the Hasse diagram will be considered toprow for later use. |
---|
909 | // ************************************************************************************************** |
---|
910 | |
---|
911 | // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic |
---|
912 | // this condition will be a vector of zeros. There are two vectors, because we need two copies of |
---|
913 | // the original Hasse diagram. |
---|
914 | vec vec1(number_of_parameters+1); |
---|
915 | vec1.zeros(); |
---|
916 | |
---|
917 | vec vec2(number_of_parameters+1); |
---|
918 | vec2.zeros(); |
---|
919 | |
---|
920 | // We create a new toprow with the previously specified condition. |
---|
921 | toprow* current_copy1 = new toprow(vec1); |
---|
922 | toprow* current_copy2 = new toprow(vec2); |
---|
923 | |
---|
924 | // The vertices of the copies will be inherited, because there will be a parent/child relation |
---|
925 | // between each polyhedron and its offspring (comming from the copy) and a parent has all the |
---|
926 | // vertices of its child plus more. |
---|
927 | for(list<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++) |
---|
928 | { |
---|
929 | current_copy1->vertices.push_back(*vertex_ref); |
---|
930 | current_copy2->vertices.push_back(*vertex_ref); |
---|
931 | } |
---|
932 | |
---|
933 | // The only new vertex of the offspring should be the newly created point. |
---|
934 | current_copy1->vertices.push_back(new_point1); |
---|
935 | current_copy2->vertices.push_back(new_point2); |
---|
936 | |
---|
937 | // This method guarantees that each polyhedron is already triangulated, therefore its triangulation |
---|
938 | // is only one set of vertices and it is the set of all its vertices. |
---|
939 | current_copy1->triangulations.push_back(current_copy1->vertices); |
---|
940 | current_copy2->triangulations.push_back(current_copy2->vertices); |
---|
941 | |
---|
942 | // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying |
---|
943 | // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the |
---|
944 | // kids and when we do that and know the child, in the child we will remember the parent we came from. |
---|
945 | // This way all the parents/children relations are saved in both the parent and the child. |
---|
946 | if(!horiz_ref->kids_rel_addresses.empty()) |
---|
947 | { |
---|
948 | for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++) |
---|
949 | { |
---|
950 | polyhedron* new_kid1 = new_statistic1->rows[j-1]; |
---|
951 | polyhedron* new_kid2 = new_statistic2->rows[j-1]; |
---|
952 | |
---|
953 | // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but |
---|
954 | // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline. |
---|
955 | if(*kid_ref) |
---|
956 | { |
---|
957 | for(int k = 1;k<=(*kid_ref);k++) |
---|
958 | { |
---|
959 | new_kid1=new_kid1->next_poly; |
---|
960 | new_kid2=new_kid2->next_poly; |
---|
961 | } |
---|
962 | } |
---|
963 | |
---|
964 | // find the child and save the relation to the parent |
---|
965 | current_copy1->children.push_back(new_kid1); |
---|
966 | current_copy2->children.push_back(new_kid2); |
---|
967 | |
---|
968 | // in the child save the parents' address |
---|
969 | new_kid1->parents.push_back(current_copy1); |
---|
970 | new_kid2->parents.push_back(current_copy2); |
---|
971 | } |
---|
972 | |
---|
973 | // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the |
---|
974 | // Hasse diagram again) |
---|
975 | horiz_ref->kids_rel_addresses.clear(); |
---|
976 | } |
---|
977 | // If there were no children previously, we are copying a polyhedron that has been a vertex before. |
---|
978 | // In this case it is a segment now and it will have a relation to its mother (copywise) and to the |
---|
979 | // newly created point. Here we create the connection to the new point, again from both sides. |
---|
980 | else |
---|
981 | { |
---|
982 | // Add the address of the new point in the former vertex |
---|
983 | current_copy1->children.push_back(new_point1); |
---|
984 | current_copy2->children.push_back(new_point2); |
---|
985 | |
---|
986 | // Add the address of the former vertex in the new point |
---|
987 | new_point1->parents.push_back(current_copy1); |
---|
988 | new_point2->parents.push_back(current_copy2); |
---|
989 | } |
---|
990 | |
---|
991 | // Save the mother in its offspring |
---|
992 | current_copy1->children.push_back(horiz_ref); |
---|
993 | current_copy2->children.push_back(horiz_ref); |
---|
994 | |
---|
995 | // Save the offspring in its mother |
---|
996 | horiz_ref->parents.push_back(current_copy1); |
---|
997 | horiz_ref->parents.push_back(current_copy2); |
---|
998 | |
---|
999 | |
---|
1000 | // Add the copies into the relevant statistic. The statistic will later be appended to the previous |
---|
1001 | // Hasse diagram |
---|
1002 | new_statistic1->append_polyhedron(j,current_copy1); |
---|
1003 | new_statistic2->append_polyhedron(j,current_copy2); |
---|
1004 | |
---|
1005 | // Raise the count in the vector of polyhedrons |
---|
1006 | element_number++; |
---|
1007 | |
---|
1008 | } |
---|
1009 | |
---|
1010 | } |
---|
1011 | |
---|
1012 | /* |
---|
1013 | statistic.begin()->push_back(new_point1); |
---|
1014 | statistic.begin()->push_back(new_point2); |
---|
1015 | */ |
---|
1016 | |
---|
1017 | statistic.append_polyhedron(0, new_point1); |
---|
1018 | statistic.append_polyhedron(0, new_point2); |
---|
1019 | |
---|
1020 | // Merge the new statistics into the old one. This will either be the final statistic or we will |
---|
1021 | // reenter the widening loop. |
---|
1022 | for(int j=0;j<new_statistic1->size();j++) |
---|
1023 | { |
---|
1024 | /* |
---|
1025 | if(j+1==statistic.size()) |
---|
1026 | { |
---|
1027 | list<polyhedron*> support; |
---|
1028 | statistic.push_back(support); |
---|
1029 | } |
---|
1030 | |
---|
1031 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); |
---|
1032 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end()); |
---|
1033 | */ |
---|
1034 | statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]); |
---|
1035 | statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]); |
---|
1036 | } |
---|
1037 | |
---|
1038 | |
---|
1039 | } |
---|
1040 | |
---|
1041 | /* |
---|
1042 | vector<list<toprow*>> toprow_statistic; |
---|
1043 | int line_count = 0; |
---|
1044 | |
---|
1045 | for(vector<list<polyhedron*>>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++) |
---|
1046 | { |
---|
1047 | list<toprow*> support_list; |
---|
1048 | toprow_statistic.push_back(support_list); |
---|
1049 | |
---|
1050 | for(list<polyhedron*>::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++) |
---|
1051 | { |
---|
1052 | toprow* support_top = (toprow*)(*polyhedron_ref2); |
---|
1053 | |
---|
1054 | toprow_statistic[line_count].push_back(support_top); |
---|
1055 | } |
---|
1056 | |
---|
1057 | line_count++; |
---|
1058 | }*/ |
---|
1059 | |
---|
1060 | |
---|
1061 | vector<int> sizevector; |
---|
1062 | for(int s = 0;s<statistic.size();s++) |
---|
1063 | { |
---|
1064 | sizevector.push_back(statistic.row_size(s)); |
---|
1065 | } |
---|
1066 | |
---|
1067 | } |
---|
1068 | |
---|
1069 | |
---|
1070 | |
---|
1071 | |
---|
1072 | }; |
---|
1073 | |
---|
1074 | /* |
---|
1075 | |
---|
1076 | //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density |
---|
1077 | class RARX : public BM |
---|
1078 | { |
---|
1079 | private: |
---|
1080 | |
---|
1081 | emlig posterior; |
---|
1082 | |
---|
1083 | public: |
---|
1084 | RARX():BM() |
---|
1085 | { |
---|
1086 | }; |
---|
1087 | |
---|
1088 | void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec) |
---|
1089 | { |
---|
1090 | |
---|
1091 | } |
---|
1092 | |
---|
1093 | };*/ |
---|
1094 | |
---|
1095 | |
---|
1096 | |
---|
1097 | #endif //TRAGE_H |
---|