root/applications/robust/robustlib.h @ 1225

Revision 1224, 33.9 kB (checked in by sindj, 14 years ago)

Drobne upravy robustlib. JS

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