root/applications/robust/robustlib.h @ 1276

Revision 1275, 40.2 kB (checked in by sindj, 13 years ago)

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