root/applications/robust/robustlib.h @ 1216

Revision 1216, 31.4 kB (checked in by sindj, 14 years ago)

Splitting polyhedrons in statistic - continued. Not finished. JS

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