root/applications/robust/robustlib.h @ 1273

Revision 1273, 40.0 kB (checked in by sindj, 13 years ago)

Rozdelano slucovani bunek pri odstranovani podminky pro moving window learning. 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                                char* string = "Checkpoint";
825                        }
826                }
827        }
828
829        int statistic_rowsize(int row)
830        {
831                return statistic.row_size(row);
832        }
833
834        void add_condition(vec toadd)
835        {
836                vec null_vector = "";
837
838                add_and_remove_condition(toadd, null_vector);
839        }
840
841
842        void remove_condition(vec toremove)
843        {               
844                vec null_vector = "";
845
846                add_and_remove_condition(null_vector, toremove);
847       
848        }
849
850
851        void add_and_remove_condition(vec toadd, vec toremove)
852        {
853                bool should_remove = (toremove.size() != 0);
854                bool should_add    = (toadd.size() != 0);
855
856                for_splitting.clear();
857                for_merging.clear();
858
859                for(int i = 0;i<statistic.size();i++)
860                {
861                        list<polyhedron*> empty_split;
862                        list<polyhedron*> empty_merge;
863
864                        for_splitting.push_back(empty_split);
865                        for_merging.push_back(empty_merge);
866                }
867
868                list<condition*>::iterator toremove_ref = conditions.end();
869                bool condition_should_be_added = false;
870
871                for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++)
872                {
873                        if(should_remove)
874                        {
875                                if((*ref)->value == toremove)
876                                {
877                                        if((*ref)->multiplicity>1)
878                                        {
879                                                (*ref)->multiplicity--;
880
881                                                alter_toprow_conditions(toremove,false);
882
883                                                should_remove = false;
884                                        }
885                                        else
886                                        {
887                                                toremove_ref = ref;                                                     
888                                        }
889                                }
890                        }
891
892                        if(should_add)
893                        {
894                                if((*ref)->value == toadd)
895                                {
896                                        (*ref)->multiplicity++;
897
898                                        alter_toprow_conditions(toadd,true);
899
900                                        should_add = false;
901                                }
902                                else
903                                {
904                                        condition_should_be_added = true;
905                                }
906                        }
907                }
908
909                if(toremove_ref!=conditions.end())
910                {
911                        conditions.erase(toremove_ref);
912                }
913
914                if(condition_should_be_added)
915                {
916                        conditions.push_back(new condition(toadd));
917                }
918
919               
920               
921                for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly)
922                {               
923                        vertex* current_vertex = (vertex*)horizontal_position;
924                       
925                        if(should_add||should_remove)
926                        {
927                                vec appended_vec = current_vertex->get_coordinates();
928                                appended_vec.ins(0,-1.0);
929
930                                if(should_add)
931                                {
932                                        double local_condition = toadd*appended_vec;
933
934                                        current_vertex->set_state(local_condition,SPLIT);
935
936                                        if(local_condition == 0)
937                                        {
938                                                current_vertex->totally_neutral = true;
939
940                                                current_vertex->raise_multiplicity();
941
942                                                current_vertex->negativeneutralvertices.insert(current_vertex);
943                                                current_vertex->positiveneutralvertices.insert(current_vertex);
944                                        }                                       
945                                }
946                       
947                                if(should_remove)
948                                {
949                                        double local_condition = toremove*appended_vec;
950
951                                        current_vertex->set_state(local_condition,MERGE);
952
953                                        if(local_condition == 0)
954                                        {
955                                                for_merging[0].push_back(current_vertex);
956                                        }
957                                }                               
958                        }
959
960                        send_state_message(current_vertex, toadd, toremove, 0);         
961                       
962                }
963
964                if(should_add)
965                {
966                        int k = 1;
967
968                        vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin();
969
970                        for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++)
971                        {                       
972
973                                for(list<polyhedron*>::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++)
974                                {
975                                        polyhedron* new_totally_neutral_child;
976
977                                        polyhedron* current_polyhedron = (*split_ref);
978                                       
979                                        if(vert_ref == beginning_ref)
980                                        {
981                                                vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates();                                             
982                                                vec coordinates2 = ((vertex*)(*(++current_polyhedron->children.begin())))->get_coordinates();
983                                               
984                                                vec extended_coord2 = coordinates2;
985                                                extended_coord2.ins(0,-1.0);
986
987                                                double t = (-toadd*extended_coord2)/((toadd(1,toadd.size()-1)*(coordinates1-coordinates2)));
988
989                                                vec new_coordinates = coordinates2+t*(coordinates1-coordinates2);                                       
990
991                                                // cout << "c1:" << coordinates1 << endl << "c2:" << coordinates2 << endl << "nc:" << new_coordinates << endl;
992
993                                                vertex* neutral_vertex = new vertex(new_coordinates);                                           
994
995                                                new_totally_neutral_child = neutral_vertex;
996                                        }
997                                        else
998                                        {
999                                                toprow* neutral_toprow = new toprow();
1000
1001                                                neutral_toprow->condition       = zeros(number_of_parameters+1);
1002                                                neutral_toprow->condition_order = ((toprow*)current_polyhedron)->condition_order+1;
1003
1004                                                new_totally_neutral_child = neutral_toprow;
1005                                        }
1006
1007                                        new_totally_neutral_child->my_emlig = this;
1008                                       
1009                                        new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(),
1010                                                                                                                current_polyhedron->totallyneutralgrandchildren.begin(),
1011                                                                                                                                current_polyhedron->totallyneutralgrandchildren.end());
1012
1013                                        for(list<polyhedron*>::iterator grand_ref = current_polyhedron->totallyneutralgrandchildren.begin(); grand_ref != current_polyhedron->totallyneutralgrandchildren.end();grand_ref++)
1014                                        {
1015                                                (*grand_ref)->parents.push_back(new_totally_neutral_child);
1016
1017                                                new_totally_neutral_child->vertices.insert((*grand_ref)->vertices.begin(),(*grand_ref)->vertices.end());
1018                                        }
1019
1020                                        // cout << ((toprow*)current_polyhedron)->condition << endl << toadd << endl;
1021
1022                                        toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition+toadd, ((toprow*)current_polyhedron)->condition_order+1);
1023                                        toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition-toadd, ((toprow*)current_polyhedron)->condition_order+1);
1024
1025                                        positive_poly->my_emlig = this;
1026                                        negative_poly->my_emlig = this;
1027
1028                                        for(list<polyhedron*>::iterator parent_ref = current_polyhedron->parents.begin();parent_ref!=current_polyhedron->parents.end();parent_ref++)
1029                                        {
1030                                                (*parent_ref)->totallyneutralgrandchildren.push_back(new_totally_neutral_child);
1031
1032                                                (*parent_ref)->neutralchildren.remove(current_polyhedron);
1033                                                (*parent_ref)->children.remove(current_polyhedron);
1034
1035                                                (*parent_ref)->children.push_back(positive_poly);
1036                                                (*parent_ref)->children.push_back(negative_poly);
1037                                                (*parent_ref)->positivechildren.push_back(positive_poly);
1038                                                (*parent_ref)->negativechildren.push_back(negative_poly);
1039                                        }
1040
1041                                        positive_poly->parents.insert(positive_poly->parents.end(),
1042                                                                                                current_polyhedron->parents.begin(),
1043                                                                                                                current_polyhedron->parents.end());
1044
1045                                        negative_poly->parents.insert(negative_poly->parents.end(),
1046                                                                                                current_polyhedron->parents.begin(),
1047                                                                                                                current_polyhedron->parents.end());
1048
1049                                        positive_poly->children.push_back(new_totally_neutral_child);
1050                                        negative_poly->children.push_back(new_totally_neutral_child);
1051
1052                                        new_totally_neutral_child->parents.push_back(positive_poly);
1053                                        new_totally_neutral_child->parents.push_back(negative_poly);
1054
1055                                        for(list<polyhedron*>::iterator child_ref = current_polyhedron->positivechildren.begin();child_ref!=current_polyhedron->positivechildren.end();child_ref++)
1056                                        {
1057                                                (*child_ref)->parents.remove(current_polyhedron);
1058                                                (*child_ref)->parents.push_back(positive_poly);                                         
1059                                        }                                       
1060
1061                                        positive_poly->children.insert(positive_poly->children.end(),
1062                                                                                                current_polyhedron->positivechildren.begin(),
1063                                                                                                                        current_polyhedron->positivechildren.end());
1064
1065                                        for(list<polyhedron*>::iterator child_ref = current_polyhedron->negativechildren.begin();child_ref!=current_polyhedron->negativechildren.end();child_ref++)
1066                                        {
1067                                                (*child_ref)->parents.remove(current_polyhedron);
1068                                                (*child_ref)->parents.push_back(negative_poly);
1069                                        }
1070
1071                                        negative_poly->children.insert(negative_poly->children.end(),
1072                                                                                                current_polyhedron->negativechildren.begin(),
1073                                                                                                                        current_polyhedron->negativechildren.end());
1074
1075                                        positive_poly->vertices.insert(current_polyhedron->positiveneutralvertices.begin(),current_polyhedron->positiveneutralvertices.end());
1076                                        positive_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end());
1077
1078                                        negative_poly->vertices.insert(current_polyhedron->negativeneutralvertices.begin(),current_polyhedron->negativeneutralvertices.end());
1079                                        negative_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end());
1080                                                               
1081                                        new_totally_neutral_child->triangulate(false);
1082
1083                                        positive_poly->triangulate(k==for_splitting.size()-1);
1084                                        negative_poly->triangulate(k==for_splitting.size()-1);
1085                                       
1086                                        statistic.append_polyhedron(k-1, new_totally_neutral_child);                                   
1087                                       
1088                                        statistic.insert_polyhedron(k, positive_poly, current_polyhedron);
1089                                        statistic.insert_polyhedron(k, negative_poly, current_polyhedron);                                     
1090
1091                                        statistic.delete_polyhedron(k, current_polyhedron);
1092
1093                                        delete current_polyhedron;
1094                                }
1095
1096                                k++;
1097                        }
1098                }
1099
1100               
1101                vector<int> sizevector;
1102                for(int s = 0;s<statistic.size();s++)
1103                {
1104                        sizevector.push_back(statistic.row_size(s));
1105                }
1106
1107                /*
1108                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)
1109                {
1110                        cout << ((toprow*)topr_ref)->condition << endl;
1111                }
1112                */
1113
1114        }
1115
1116        void set_correction_factors(int order)
1117                {
1118                        for(int remaining_order = correction_factors.size();!(remaining_order>order);remaining_order++)
1119                        {
1120                                set<my_ivec> factor_templates;
1121                                set<my_ivec> final_factors;
1122
1123                               
1124                                for(int i = 1;i!=number_of_parameters-order+1;i++)
1125                                {                                       
1126                                        my_ivec new_template = my_ivec();
1127                                        new_template.ins(0,1);
1128                                        new_template.ins(1,i);
1129                                        factor_templates.insert(new_template);
1130
1131                                       
1132                                        for(int j = 1;j<remaining_order;j++)
1133                                        {
1134                                               
1135                                                for(set<my_ivec>::iterator fac_ref = factor_templates.begin();fac_ref!=factor_templates.end();fac_ref++)
1136                                                {
1137                                                        ivec current_template = (*fac_ref);
1138                                                       
1139                                                        current_template[0]+=1;
1140                                                        current_template.ins(current_template.size(),i);
1141
1142                                                       
1143                                                        if(current_template[0]==remaining_order)
1144                                                        {
1145                                                                final_factors.insert(current_template.right(current_template.size()-1));
1146                                                        }
1147                                                        else
1148                                                        {
1149                                                                factor_templates.insert(current_template);
1150                                                        }
1151                                                }
1152                                        }
1153                                }       
1154
1155                                correction_factors.push_back(final_factors);                   
1156
1157                        }
1158                }
1159
1160protected:
1161
1162        /// A method for creating plain default statistic representing only the range of the parameter space.
1163    void create_statistic(int number_of_parameters)
1164        {
1165                for(int i = 0;i<number_of_parameters;i++)
1166                {
1167                        vec condition_vec = zeros(number_of_parameters+1);
1168                        condition_vec[i+1]  = 1;
1169
1170                        condition* new_condition = new condition(condition_vec);
1171                       
1172                        conditions.push_back(new_condition);
1173                }
1174
1175                // An empty vector of coordinates.
1176                vec origin_coord;       
1177
1178                // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords.
1179                vertex *origin = new vertex(origin_coord);
1180
1181                origin->my_emlig = this;
1182               
1183                /*
1184                // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse
1185                // diagram. First we create a vector of polyhedrons..
1186                list<polyhedron*> origin_vec;
1187
1188                // ..we fill it with the origin..
1189                origin_vec.push_back(origin);
1190
1191                // ..and we fill the statistic with the created vector.
1192                statistic.push_back(origin_vec);
1193                */
1194
1195                statistic = *(new c_statistic());               
1196               
1197                statistic.append_polyhedron(0, origin);
1198
1199                // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to
1200                // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows:
1201                for(int i=0;i<number_of_parameters;i++)
1202                {
1203                        // We first will create two new vertices. These will be the borders of the parameter space in the dimension
1204                        // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the
1205                        // right amount of zero cooridnates by reading them from the origin
1206                        vec origin_coord = origin->get_coordinates();                                           
1207
1208                        // And we incorporate the nonzero coordinates into the new cooordinate vectors
1209                        vec origin_coord1 = concat(origin_coord,-max_range); 
1210                        vec origin_coord2 = concat(origin_coord,max_range);                             
1211                                       
1212
1213                        // Now we create the points
1214                        vertex* new_point1 = new vertex(origin_coord1);
1215                        vertex* new_point2 = new vertex(origin_coord2);
1216
1217                        new_point1->my_emlig = this;
1218                        new_point2->my_emlig = this;
1219                       
1220                        //*********************************************************************************************************
1221                        // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old
1222                        // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points
1223                        // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons
1224                        // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is
1225                        // connected to the first (second) newly created vertex by a parent-child relation.
1226                        //*********************************************************************************************************
1227
1228
1229                        /*
1230                        // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram
1231                        vector<vector<polyhedron*>> new_statistic1;
1232                        vector<vector<polyhedron*>> new_statistic2;
1233                        */
1234
1235                        c_statistic* new_statistic1 = new c_statistic();
1236                        c_statistic* new_statistic2 = new c_statistic();
1237
1238                       
1239                        // Copy the statistic by rows                   
1240                        for(int j=0;j<statistic.size();j++)
1241                        {
1242                               
1243
1244                                // an element counter
1245                                int element_number = 0;
1246
1247                                /*
1248                                vector<polyhedron*> supportnew_1;
1249                                vector<polyhedron*> supportnew_2;
1250
1251                                new_statistic1.push_back(supportnew_1);
1252                                new_statistic2.push_back(supportnew_2);
1253                                */
1254
1255                                // for each polyhedron in the given row
1256                                for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly)
1257                                {       
1258                                        // Append an extra zero coordinate to each of the vertices for the new dimension
1259                                        // If vert_ref is at the first index => we loop through vertices
1260                                        if(j == 0)
1261                                        {
1262                                                // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates
1263                                                ((vertex*) horiz_ref)->push_coordinate(0);
1264                                        }
1265                                        /*
1266                                        else
1267                                        {
1268                                                ((toprow*) (*horiz_ref))->condition.ins(0,0);
1269                                        }*/
1270
1271                                        // if it has parents
1272                                        if(!horiz_ref->parents.empty())
1273                                        {
1274                                                // save the relative address of this child in a vector kids_rel_addresses of all its parents.
1275                                                // This information will later be used for copying the whole Hasse diagram with each of the
1276                                                // relations contained within.
1277                                                for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++)
1278                                                {
1279                                                        (*parent_ref)->kids_rel_addresses.push_back(element_number);                                                   
1280                                                }                                               
1281                                        }
1282
1283                                        // **************************************************************************************************
1284                                        // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron
1285                                        // will be created as a toprow, but this information will be later forgotten and only the polyhedrons
1286                                        // in the top row of the Hasse diagram will be considered toprow for later use.
1287                                        // **************************************************************************************************
1288
1289                                        // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic
1290                                        // this condition will be a vector of zeros. There are two vectors, because we need two copies of
1291                                        // the original Hasse diagram.
1292                                        vec vec1(number_of_parameters+1);
1293                                        vec1.zeros();
1294
1295                                        vec vec2(number_of_parameters+1);
1296                                        vec2.zeros();
1297
1298                                        // We create a new toprow with the previously specified condition.
1299                                        toprow* current_copy1 = new toprow(vec1, 0);
1300                                        toprow* current_copy2 = new toprow(vec2, 0);
1301
1302                                        current_copy1->my_emlig = this;
1303                                        current_copy2->my_emlig = this;
1304
1305                                        // The vertices of the copies will be inherited, because there will be a parent/child relation
1306                                        // between each polyhedron and its offspring (comming from the copy) and a parent has all the
1307                                        // vertices of its child plus more.
1308                                        for(set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++)
1309                                        {
1310                                                current_copy1->vertices.insert(*vertex_ref);
1311                                                current_copy2->vertices.insert(*vertex_ref);                                           
1312                                        }
1313                                       
1314                                        // The only new vertex of the offspring should be the newly created point.
1315                                        current_copy1->vertices.insert(new_point1);
1316                                        current_copy2->vertices.insert(new_point2);                                     
1317                                       
1318                                        // This method guarantees that each polyhedron is already triangulated, therefore its triangulation
1319                                        // is only one set of vertices and it is the set of all its vertices.
1320                                        set<vertex*> t_simplex1;
1321                                        set<vertex*> t_simplex2;
1322
1323                                        t_simplex1.insert(current_copy1->vertices.begin(),current_copy1->vertices.end());
1324                                        t_simplex2.insert(current_copy2->vertices.begin(),current_copy2->vertices.end());
1325                                       
1326                                        current_copy1->triangulation.push_back(t_simplex1);
1327                                        current_copy2->triangulation.push_back(t_simplex2);                                     
1328                                       
1329                                        // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying
1330                                        // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the
1331                                        // kids and when we do that and know the child, in the child we will remember the parent we came from.
1332                                        // This way all the parents/children relations are saved in both the parent and the child.
1333                                        if(!horiz_ref->kids_rel_addresses.empty())
1334                                        {
1335                                                for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++)
1336                                                {       
1337                                                        polyhedron* new_kid1 = new_statistic1->rows[j-1];
1338                                                        polyhedron* new_kid2 = new_statistic2->rows[j-1];
1339
1340                                                        // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but
1341                                                        // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline.
1342                                                        if(*kid_ref)
1343                                                        {
1344                                                                for(int k = 1;k<=(*kid_ref);k++)
1345                                                                {
1346                                                                        new_kid1=new_kid1->next_poly;
1347                                                                        new_kid2=new_kid2->next_poly;
1348                                                                }
1349                                                        }
1350                                                       
1351                                                        // find the child and save the relation to the parent
1352                                                        current_copy1->children.push_back(new_kid1);
1353                                                        current_copy2->children.push_back(new_kid2);
1354
1355                                                        // in the child save the parents' address
1356                                                        new_kid1->parents.push_back(current_copy1);
1357                                                        new_kid2->parents.push_back(current_copy2);
1358                                                }                                               
1359
1360                                                // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the
1361                                                // Hasse diagram again)
1362                                                horiz_ref->kids_rel_addresses.clear();
1363                                        }
1364                                        // If there were no children previously, we are copying a polyhedron that has been a vertex before.
1365                                        // In this case it is a segment now and it will have a relation to its mother (copywise) and to the
1366                                        // newly created point. Here we create the connection to the new point, again from both sides.
1367                                        else
1368                                        {
1369                                                // Add the address of the new point in the former vertex
1370                                                current_copy1->children.push_back(new_point1);
1371                                                current_copy2->children.push_back(new_point2);
1372
1373                                                // Add the address of the former vertex in the new point
1374                                                new_point1->parents.push_back(current_copy1);
1375                                                new_point2->parents.push_back(current_copy2);
1376                                        }
1377
1378                                        // Save the mother in its offspring
1379                                        current_copy1->children.push_back(horiz_ref);
1380                                        current_copy2->children.push_back(horiz_ref);
1381
1382                                        // Save the offspring in its mother
1383                                        horiz_ref->parents.push_back(current_copy1);
1384                                        horiz_ref->parents.push_back(current_copy2);   
1385                                                               
1386                                       
1387                                        // Add the copies into the relevant statistic. The statistic will later be appended to the previous
1388                                        // Hasse diagram
1389                                        new_statistic1->append_polyhedron(j,current_copy1);
1390                                        new_statistic2->append_polyhedron(j,current_copy2);
1391                                       
1392                                        // Raise the count in the vector of polyhedrons
1393                                        element_number++;                       
1394                                       
1395                                }
1396                               
1397                        }
1398
1399                        /*
1400                        statistic.begin()->push_back(new_point1);
1401                        statistic.begin()->push_back(new_point2);
1402                        */
1403
1404                        statistic.append_polyhedron(0, new_point1);
1405                        statistic.append_polyhedron(0, new_point2);
1406
1407                        // Merge the new statistics into the old one. This will either be the final statistic or we will
1408                        // reenter the widening loop.
1409                        for(int j=0;j<new_statistic1->size();j++)
1410                        {
1411                                /*
1412                                if(j+1==statistic.size())
1413                                {
1414                                        list<polyhedron*> support;
1415                                        statistic.push_back(support);
1416                                }
1417                               
1418                                (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end());
1419                                (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end());
1420                                */
1421                                statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]);
1422                                statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]);
1423                        }                       
1424                }
1425
1426                /*
1427                vector<list<toprow*>> toprow_statistic;
1428                int line_count = 0;
1429
1430                for(vector<list<polyhedron*>>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++)
1431                {
1432                        list<toprow*> support_list;
1433                        toprow_statistic.push_back(support_list);                                               
1434
1435                        for(list<polyhedron*>::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++)
1436                        {
1437                                toprow* support_top = (toprow*)(*polyhedron_ref2);
1438
1439                                toprow_statistic[line_count].push_back(support_top);
1440                        }
1441
1442                        line_count++;
1443                }*/
1444
1445                /*
1446                vector<int> sizevector;
1447                for(int s = 0;s<statistic.size();s++)
1448                {
1449                        sizevector.push_back(statistic.row_size(s));
1450                }
1451                */
1452               
1453        }
1454
1455
1456       
1457       
1458};
1459
1460/*
1461
1462//! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density
1463class RARX : public BM
1464{
1465private:
1466
1467        emlig posterior;
1468
1469public:
1470        RARX():BM()
1471        {
1472        };
1473
1474        void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec)
1475        {
1476               
1477        }
1478
1479};*/
1480
1481
1482
1483#endif //TRAGE_H
Note: See TracBrowser for help on using the browser.