root/applications/robust/robustlib.h @ 1256

Revision 1254, 34.2 kB (checked in by sindj, 14 years ago)

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