Changeset 1301 for applications

Show
Ignore:
Timestamp:
03/21/11 09:01:57 (13 years ago)
Author:
sindj
Message:

Dokoncovani mergovani polyhedronu a oprava chyb. Mergovani je skoro dokonceno, ale je tam nekde skryta chybka. JS

Location:
applications/robust
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r1300 r1301  
    2121         
    2222        /* 
     23        // EXPERIMENT: 100 AR model generated time series of length of 30 from y_t=0.95*y_(t-1)+0.05*y_(t-2)+0.2*e_t,  
     24        // where e_t is normally, student(4) and cauchy distributed are tested using robust AR model, to obtain the  
     25        // variance of location parameter estimators and compare it to the classical setup. 
    2326        vector<vector<vector<string>>> string_lists; 
    2427        string_lists.push_back(vector<vector<string>>()); 
     
    5558        for(int j = 0;j<string_lists.size();j++) 
    5659        {  
    57                 /* 
     60                 
    5861                for(int i = 0;i<string_lists[j].size()-1;i++) 
    5962                { 
    6063                        vector<vec> conditions; 
    61                         emlig* emliga = new emlig(2); 
     64                        //emlig* emliga = new emlig(2); 
     65                        RARX* my_rarx = new RARX(2,30); 
     66 
    6267                        for(int k = 1;k<string_lists[j][i].size();k++) 
    6368                        { 
     
    8186                                        //cout << "modi:" << conditions[k-3] << endl; 
    8287 
    83                                         emliga->add_condition(conditions[k-3]); 
     88                                        my_rarx->bayes(conditions[k-3]); 
    8489 
    8590                                         
     
    9499 
    95100                        //emliga->step_me(0); 
     101                        /* 
    96102                        ofstream myfile; 
    97103                        myfile.open("c:\\robust_ar1.txt",ios::app); 
    98                         myfile << emliga->minimal_vertex->get_coordinates()[0] << ";"; 
     104                        myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; 
    99105                        myfile.close(); 
    100106 
     
    102108                        myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; 
    103109                        myfile.close(); 
     110                         
    104111 
    105112                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; 
     
    117124                myfile << endl; 
    118125                myfile.close(); 
    119         } 
    120     */ 
    121          
    122          
    123         emlig* emlig1 = new emlig(emlig_size); 
    124         emlig* emlig2 = new emlig(emlig_size); 
     126        }*/ 
     127     
     128         
     129        // EXPERIMENT: A moving window estimation and prediction of RARX is tested on data generated from  
     130    // y_t=0.95*y_(t-1)+0.05*y_(t-2)+0.2*e_t, where e_t is normally, student(4) and cauchy distributed. It 
     131    // can be compared to the classical setup. 
     132         
     133 
     134        vector<vector<string>> strings; 
     135 
     136        char* file_strings[3] = {"c:\\ar_student_single.txt", "c:\\ar_cauchy_single.txt","c:\\ar_normal_single.txt"}; 
     137 
     138        for(int i = 0;i<3;i++) 
     139        {                        
     140                ifstream myfile(file_strings[i]); 
     141                if (myfile.is_open()) 
     142                {                        
     143                        string line; 
     144                        getline(myfile,line); 
     145                                 
     146                        vector<string> parsed_line; 
     147                        while(line.find(',') != string::npos) 
     148                        { 
     149                                int loc = line.find(','); 
     150                                parsed_line.push_back(line.substr(0,loc)); 
     151                                line.erase(0,loc+1);                                     
     152                        }                                
     153 
     154                        strings.push_back(parsed_line); 
     155                         
     156                        myfile.close(); 
     157                } 
     158        } 
     159         
     160        for(int j = 0;j<strings.size();j++) 
     161        {                
     162                vector<vec> conditions; 
     163                //emlig* emliga = new emlig(2); 
     164                RARX* my_rarx = new RARX(2,0); 
     165 
     166                for(int k = 1;k<170;k++) 
     167                { 
     168                        vec condition; 
     169                        //condition.ins(0,1);                            
     170                        condition.ins(0,strings[j][k]);                          
     171                        conditions.push_back(condition); 
     172 
     173                        //cout << "orig:" << condition << endl; 
     174 
     175                        if(conditions.size()>1) 
     176                        {                
     177                                conditions[k-2].ins(0,strings[j][k]); 
     178                                         
     179                        } 
     180 
     181                        if(conditions.size()>2) 
     182                        { 
     183                                conditions[k-3].ins(0,strings[j][k]); 
     184 
     185                                // cout << "modi:" << conditions[k-3] << endl; 
     186 
     187                                my_rarx->bayes(conditions[k-3]); 
     188 
     189                                         
     190                                 
     191                                if(k>5) 
     192                                { 
     193                                        cout << "MaxLik coords:" << my_rarx->posterior->minimal_vertex->get_coordinates() << endl; 
     194 
     195                                        ofstream myfile; 
     196                                        char fstring[80]; 
     197                                        strcpy(fstring,file_strings[j]); 
     198                                        strcat(fstring,"_res.txt"); 
     199 
     200                                        myfile.open(fstring,ios::app); 
     201                                        myfile << my_rarx->posterior->minimal_vertex->get_coordinates()[0]; 
     202                                        if(k!=strings[j].size()-1) 
     203                                        { 
     204                                                myfile << ","; 
     205                                        } 
     206                                        else 
     207                                        { 
     208                                                myfile << endl; 
     209                                        } 
     210                                        myfile.close(); 
     211                                } 
     212                                         
     213                        }        
     214                         
     215                        //emliga->step_me(0); 
     216                        /* 
     217                        ofstream myfile; 
     218                        myfile.open("c:\\robust_ar1.txt",ios::app); 
     219                        myfile << my_rarx->minimal_vertex->get_coordinates()[0] << ";"; 
     220                        myfile.close(); 
     221 
     222                        myfile.open("c:\\robust_ar2.txt",ios::app); 
     223                        myfile << emliga->minimal_vertex->get_coordinates()[1] << ";"; 
     224                        myfile.close(); 
     225                         
     226 
     227                        cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; 
     228                        cout << "Step: " << i << endl;*/ 
     229                } 
     230        } 
     231                 
     232                /* 
     233                cout << "One experiment finished." << endl; 
     234 
     235                ofstream myfile; 
     236                myfile.open("c:\\robust_ar1.txt",ios::app); 
     237                myfile << endl; 
     238                myfile.close(); 
     239 
     240                myfile.open("c:\\robust_ar2.txt",ios::app); 
     241                myfile << endl; 
     242                myfile.close();*/ 
     243         
     244 
     245        //emlig* emlig1 = new emlig(emlig_size); 
     246 
     247        //emlig1->step_me(0); 
     248        //emlig* emlig2 = new emlig(emlig_size); 
    125249         
    126250        /* 
     
    142266        }*/ 
    143267         
    144          
     268        /* 
    145269    vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; 
    146270 
    147271        emlig1->add_condition(condition5); 
    148          
    149         //vec condition1a = "1.0 1.0 1.01"; 
     272        //emlig1->step_me(0); 
     273 
     274 
     275        vec condition1a = "-1.0 1.02 0.5"; 
    150276        //vec condition1b = "1.0 1.0 1.01"; 
    151         //emlig1->add_condition(condition1a); 
     277        emlig1->add_condition(condition1a); 
    152278        //emlig2->add_condition(condition1b); 
    153279 
    154         //vec condition2a = "-1.0 1.0 1.0"; 
     280        vec condition2a = "-0.3 1.7 1.5"; 
    155281        //vec condition2b = "-1.0 1.0 1.0"; 
    156         //emlig1->add_condition(condition2a); 
     282        emlig1->add_condition(condition2a); 
    157283        //emlig2->add_condition(condition2b); 
    158284 
    159         //vec condition3a = "0.5 -1.01 1.0"; 
     285        vec condition3a = "0.5 -1.01 1.0"; 
    160286        //vec condition3b = "0.5 -1.01 1.0"; 
    161287 
    162         //emlig1->add_condition(condition3a); 
     288        emlig1->add_condition(condition3a); 
    163289        //emlig2->add_condition(condition3b);    
    164290 
    165         //vec condition4a = "-0.5 -1.0 1.0"; 
     291        vec condition4a = "-0.5 -1.0 1.0"; 
    166292        //vec condition4b = "-0.5 -1.0 1.0";     
    167293 
    168         //emlig1->add_condition(condition4a); 
     294        emlig1->add_condition(condition4a); 
    169295        //cout << "************************************************" << endl; 
    170296        //emlig2->add_condition(condition4b); 
     
    173299        //cout << emlig1->minimal_vertex->get_coordinates(); 
    174300         
    175         emlig1->remove_condition(condition5); 
     301        //emlig1->remove_condition(condition3a); 
     302        //emlig1->step_me(0); 
     303        //emlig1->remove_condition(condition2a); 
     304        //emlig1->remove_condition(condition1a); 
     305        //emlig1->remove_condition(condition5); 
     306         
    176307 
    177308        //emlig1->step_me(0); 
     
    183314 
    184315        //emlig1->remove_condition(condition1); 
    185  
    186          
    187  
    188          
    189         /* 
    190          
     316         
     317         
     318 
     319         
     320         
     321        /* 
    191322        for(int i = 0;i<100;i++) 
    192323        { 
  • applications/robust/robustlib.cpp

    r1300 r1301  
    44void polyhedron::triangulate(bool should_integrate) 
    55        { 
    6                 this->triangulation.clear(); 
     6                triangulation.clear(); 
    77 
    88                if(should_integrate) 
     
    1111                }                
    1212                 
     13                if(vertices.size()==1) 
     14                { 
     15                        set<vertex*> vert_simplex; 
     16                        vert_simplex.insert((vertex*)this); 
     17 
     18                        triangulation.push_back(vert_simplex); 
     19                } 
     20 
    1321                for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) 
    1422                {                        
     
    1624                        { 
    1725                                set<vertex*> new_simplex; 
     26                                new_simplex.insert((*t_ref).begin(),(*t_ref).end());                             
     27 
     28                                pair<set<vertex*>::iterator,bool> ret_val = new_simplex.insert(*vertices.begin()); 
     29 
     30                                if(ret_val.second == true) 
     31                                { 
     32                                        triangulation.push_back(new_simplex); 
     33 
     34                                        if(should_integrate) 
     35                                        { 
     36                                                ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(new_simplex, 'S'); 
     37                                        } 
     38                                }  
     39                        }        
     40                }                
     41                 
     42                /* 
     43                for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) 
     44                {                        
     45                        for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++) 
     46                        { 
     47                                set<vertex*> new_simplex; 
    1848                                new_simplex.insert((*t_ref).begin(),(*t_ref).end());                     
    1949 
    2050                                for(set<vertex*>::iterator suitable_vert_ref = vertices.begin();*suitable_vert_ref<*(*t_ref).begin();suitable_vert_ref++) 
    2151                                { 
    22                                         new_simplex.insert(*suitable_vert_ref); 
    23  
    24                                         triangulation.push_back(new_simplex); 
     52                                        set<vertex*> suitable_simplex; 
     53                                        suitable_simplex.insert(new_simplex.begin(),new_simplex.end()); 
     54                                         
     55                                        suitable_simplex.insert(*suitable_vert_ref); 
     56 
     57                                        triangulation.push_back(suitable_simplex); 
    2558 
    2659                                        if(should_integrate) 
    2760                                        { 
    28                                                 ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(new_simplex, 'S'); 
     61                                                ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(suitable_simplex, 'S'); 
    2962                                        } 
    3063                                }                                
    3164                        }        
    32                 }                
     65                }*/              
    3366        } 
    3467 
     
    89122 
    90123                         
    91                                 cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl; 
     124                                // cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl; 
    92125 
    93126                                a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition_sum[0]; 
     
    136169                                                //cout << "a0:" << a_0 << " a0 coords:" << base_vertex->get_coordinates() << " am:" << a_m << " am coords:" << (*vert_ref)->get_coordinates() << endl; 
    137170 
    138                                                 cout << "Absolute coords:(V"  << row_count << ")" << (*vert_ref)->get_coordinates() << endl; 
     171                                                // cout << "Absolute coords:(V"  << row_count << ")" << (*vert_ref)->get_coordinates() << endl; 
    139172                                                //cout << "Relative coords:(V"  << row_count << ")" << relative_coords << endl; 
    140173 
  • applications/robust/robustlib.h

    r1300 r1301  
    2121using namespace itpp; 
    2222 
    23 const double max_range = 10;//numeric_limits<double>::max()/10e-10; 
    24  
     23const double max_range = 100;//numeric_limits<double>::max()/10e-10; 
     24 
     25/// An enumeration of possible actions performed on the polyhedrons. We can merge them or split them. 
    2526enum actions {MERGE, SPLIT}; 
    2627 
    27  
    28  
     28// Forward declaration of polyhedron, vertex and emlig 
    2929class polyhedron; 
    3030class vertex; 
     31class emlig; 
    3132 
    3233/* 
     
    4546};*/ 
    4647 
    47 class emlig; 
    48 class polyhedron; 
    49  
     48/// A class representing a single condition that can be added to the emlig. A condition represents data entries in a statistical model. 
    5049class condition 
    5150{        
    5251public: 
     52        /// Value of the condition representing the data 
    5353        vec value;       
    5454 
     55        /// Mulitplicity of the given condition may represent multiple occurences of same data entry. 
    5556        int multiplicity; 
    5657 
     58        /// Default constructor of condition class takes the value of data entry and creates a condition with multiplicity 1 (first occurence of the data). 
    5759        condition(vec value) 
    5860        { 
     
    7274        int multiplicity;        
    7375 
     76        /// A property representing the position of the polyhedron related to current condition with relation to which we 
     77        /// are splitting the parameter space (new data has arrived). This property is setup within a classification procedure and 
     78        /// is only valid while the new condition is being added. It has to be reset when new condition is added and new classification 
     79        /// has to be performed. 
    7480        int split_state; 
    7581 
     82        /// A property representing the position of the polyhedron related to current condition with relation to which we 
     83        /// are merging the parameter space (data is being deleted usually due to a moving window model which is more adaptive and  
     84        /// steps in for the forgetting in a classical Gaussian AR model). This property is setup within a classification procedure and 
     85        /// is only valid while the new condition is being removed. It has to be reset when new condition is removed and new classification 
     86        /// has to be performed. 
    7687        int merge_state; 
    7788 
     
    7990 
    8091public: 
     92        /// A pointer to the multi-Laplace inverse gamma distribution this polyhedron belongs to. 
    8193        emlig* my_emlig; 
    8294 
     
    90102        set<vertex*> vertices; 
    91103 
     104        /// The conditions that gave birth to the polyhedron. If some of them is removed, the polyhedron ceases to exist. 
    92105        set<condition*> parentconditions; 
    93106 
     
    101114        list<polyhedron*> neutralchildren; 
    102115 
     116        /// A set of grandchildren of the polyhedron that when new condition is added lie exactly on the condition hyperplane. These grandchildren 
     117        /// behave differently from other grandchildren, when the polyhedron is split. New grandchild is not necessarily created on the crossection of 
     118        /// the polyhedron and new condition. 
    103119        set<polyhedron*> totallyneutralgrandchildren; 
    104120 
     121        /// A set of children of the polyhedron that when new condition is added lie exactly on the condition hyperplane. These children 
     122        /// behave differently from other children, when the polyhedron is split. New child is not necessarily created on the crossection of 
     123        /// the polyhedron and new condition. 
    105124        set<polyhedron*> totallyneutralchildren; 
    106125 
     126        /// Reverse relation to the totallyneutralgrandchildren set is needed for merging of already existing polyhedrons to keep  
     127        /// totallyneutralgrandchildren list up to date. 
    107128        set<polyhedron*> grandparents; 
    108129 
     130        /// Vertices of the polyhedron classified as positive related to an added condition. When the polyhderon is split by the new condition, 
     131        /// these vertices will belong to the positive part of the splitted polyhedron. 
    109132        set<vertex*> positiveneutralvertices; 
    110133 
     134        /// Vertices of the polyhedron classified as negative related to an added condition. When the polyhderon is split by the new condition, 
     135        /// these vertices will belong to the negative part of the splitted polyhedron. 
    111136        set<vertex*> negativeneutralvertices; 
    112137 
     138        /// A bool specifying if the polyhedron lies exactly on the newly added condition or not. 
    113139        bool totally_neutral; 
    114140 
     141        /// When two polyhedrons are merged, there always exists a child lying on the former border of the polyhedrons. This child manages the merge 
     142        /// of the two polyhedrons. This property gives us the address of the mediator child. 
    115143        polyhedron* mergechild; 
    116144 
     145        /// If the polyhedron serves as a mergechild for two of its parents, we need to have the address of the parents to access them. This  
     146        /// is the pointer to the positive parent being merged. 
    117147        polyhedron* positiveparent; 
    118148 
     149        /// If the polyhedron serves as a mergechild for two of its parents, we need to have the address of the parents to access them. This  
     150        /// is the pointer to the negative parent being merged. 
    119151        polyhedron* negativeparent;      
    120152 
     153        /// Adressing withing the statistic. Next_poly is a pointer to the next polyhedron in the statistic on the same level (if this is a point, 
     154        /// next_poly will be a point etc.). 
    121155        polyhedron* next_poly; 
    122156 
     157        /// Adressing withing the statistic. Prev_poly is a pointer to the previous polyhedron in the statistic on the same level (if this is a point, 
     158        /// next_poly will be a point etc.). 
    123159        polyhedron* prev_poly; 
    124160 
     161        /// A property counting the number of messages obtained from children within a classification procedure of position of the polyhedron related 
     162        /// an added/removed condition. If the message counter reaches the number of children, we know the polyhedrons' position has been fully classified. 
    125163        int message_counter; 
    126164 
     
    173211 
    174212         
    175  
     213        /// A setter of state of current polyhedron relative to the action specified in the argument. The three possible states of the 
     214        /// polyhedron are -1 - NEGATIVE, 0 - NEUTRAL, 1 - POSITIVE. Neutral state means that either the state has been reset or the polyhedron is 
     215        /// ready to be split/merged. 
    176216        int set_state(double state_indicator, actions action) 
    177217        { 
     
    187227        } 
    188228 
     229        /// A getter of state of current polyhedron relative to the action specified in the argument. The three possible states of the 
     230        /// polyhedron are -1 - NEGATIVE, 0 - NEUTRAL, 1 - POSITIVE. Neutral state means that either the state has been reset or the polyhedron is 
     231        /// ready to be split/merged. 
    189232        int get_state(actions action) 
    190233        { 
     
    200243        } 
    201244 
     245        /// Method for obtaining the number of children of given polyhedron. 
    202246        int number_of_children() 
    203247        { 
     
    205249        } 
    206250 
    207          
     251        /// A method for triangulation of given polyhedron. 
    208252        void triangulate(bool should_integrate);         
    209253}; 
     
    218262 
    219263public: 
    220  
     264        /// A property specifying the value of the density (ted nevim, jestli je to jakoby log nebo ne) above the vertex. 
    221265        double function_value; 
    222266 
     
    255299 
    256300 
    257 /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates 
     301/// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differen   tiates 
    258302/// it from polyhedrons in other rows.  
    259303class toprow : public polyhedron 
     
    282326        toprow(vec condition_sum, int condition_order) 
    283327        { 
    284                 this->condition_sum = condition_sum; 
     328                this->condition_sum   = condition_sum; 
     329                this->condition_order = condition_order; 
    285330        } 
    286331 
     
    625670                        double product = (*vertex_ref)->get_coordinates()*condition->value; 
    626671 
    627                         if((product>0 && should_be_added)||(product<0 && !should_be_added)) 
    628                         { 
    629                                 ((toprow*) horiz_ref)->condition_sum += condition->value; 
     672                        if(should_be_added) 
     673                        { 
     674                                ((toprow*) horiz_ref)->condition_order++; 
     675 
     676                                if(product>0) 
     677                                { 
     678                                        ((toprow*) horiz_ref)->condition_sum += condition->value; 
     679                                } 
     680                                else 
     681                                { 
     682                                        ((toprow*) horiz_ref)->condition_sum -= condition->value; 
     683                                } 
    630684                        } 
    631685                        else 
    632                         { 
    633                                 ((toprow*) horiz_ref)->condition_sum -= condition->value; 
     686                        {  
     687                                ((toprow*) horiz_ref)->condition_order--; 
     688 
     689                                if(product<0)                    
     690                                { 
     691                                        ((toprow*) horiz_ref)->condition_sum += condition->value; 
     692                                } 
     693                                else 
     694                                { 
     695                                        ((toprow*) horiz_ref)->condition_sum -= condition->value; 
     696                                } 
    634697                        }                                
    635698                } 
     
    641704        {                        
    642705 
    643                 bool shouldmerge    = (toremove == NULL); 
    644                 bool shouldsplit    = (toadd == NULL); 
     706                bool shouldmerge    = (toremove != NULL); 
     707                bool shouldsplit    = (toadd != NULL); 
    645708                 
    646709                if(shouldsplit||shouldmerge) 
     
    675738                                        if(is_last) 
    676739                                        {                                                
    677                                                 if(current_parent->mergechild!=NULL) 
     740                                                if(current_parent->mergechild != NULL) 
    678741                                                { 
    679742                                                        if(current_parent->mergechild->get_multiplicity()==1) 
     
    688751                                                                        current_parent->mergechild->negativeparent = current_parent;                                                     
    689752                                                                } 
     753                                                        }                                                        
     754                                                }                                                
     755                                                else 
     756                                                { 
     757                                                        if(parent_state == 1) 
     758                                                        { 
     759                                                                ((toprow*)current_parent)->condition_sum-=toremove->value; 
     760                                                                ((toprow*)current_parent)->condition_order--; 
    690761                                                        } 
    691                                                 } 
    692                                                  
     762 
     763                                                        if(parent_state == -1) 
     764                                                        { 
     765                                                                ((toprow*)current_parent)->condition_sum+=toremove->value; 
     766                                                                ((toprow*)current_parent)->condition_order--; 
     767                                                        } 
     768 
     769                                                        //current_parent->set_state(0,MERGE);                                                    
     770                                                } 
    693771 
    694772                                                if(parent_state == 0) 
    695773                                                { 
    696                                                         for_merging[level+1].push_back(current_parent);                                                  
     774                                                        for_merging[level+1].push_back(current_parent); 
     775                                                        // current_parent->parentconditions.erase(toremove); 
     776                                                }                                                
     777 
     778                                                current_parent->mergechild = NULL; 
     779                                                current_parent->message_counter = 0;                                             
     780 
     781                                                if(level == number_of_parameters - 1) 
     782                                                { 
     783                                                        toprow* cur_par_toprow = ((toprow*)current_parent); 
     784                                                        cur_par_toprow->probability = 0.0; 
     785                                                                         
     786                                                        for(list<set<vertex*>>::iterator t_ref = current_parent->triangulation.begin();t_ref!=current_parent->triangulation.end();t_ref++) 
     787                                                        { 
     788                                                                cur_par_toprow->probability += cur_par_toprow->integrate_simplex(*t_ref,'C'); 
     789                                                        }                                                                        
     790                                                } 
     791                                        }                                        
     792                                } 
     793 
     794                                if(shouldsplit) 
     795                                { 
     796                                        current_parent->totallyneutralgrandchildren.insert(sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); 
     797 
     798                                        for(set<polyhedron*>::iterator tot_child_ref = sender->totallyneutralchildren.begin();tot_child_ref!=sender->totallyneutralchildren.end();tot_child_ref++) 
     799                                        { 
     800                                                (*tot_child_ref)->grandparents.insert(current_parent); 
     801                                        } 
     802 
     803                                        switch(sender->get_state(SPLIT)) 
     804                                        { 
     805                                        case 1: 
     806                                                current_parent->positivechildren.push_back(sender); 
     807                                                current_parent->positiveneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); 
     808                                        break; 
     809                                        case 0: 
     810                                                current_parent->neutralchildren.push_back(sender); 
     811                                                current_parent->positiveneutralvertices.insert(sender->positiveneutralvertices.begin(),sender->positiveneutralvertices.end()); 
     812                                                current_parent->negativeneutralvertices.insert(sender->negativeneutralvertices.begin(),sender->negativeneutralvertices.end()); 
     813 
     814                                                if(current_parent->totally_neutral == NULL) 
     815                                                { 
     816                                                        current_parent->totally_neutral = sender->totally_neutral; 
    697817                                                } 
    698818                                                else 
    699819                                                { 
    700                                                         current_parent->set_state(0,MERGE);                                                      
    701                                                 } 
    702  
    703                                                 current_parent->mergechild = NULL; 
    704                                         }                                        
    705                                 } 
    706  
    707                                 if(shouldsplit) 
    708                                         { 
    709                                                 current_parent->totallyneutralgrandchildren.insert(sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); 
    710  
    711                                                 for(set<polyhedron*>::iterator tot_child_ref = sender->totallyneutralchildren.begin();tot_child_ref!=sender->totallyneutralchildren.end();tot_child_ref++) 
    712                                                 { 
    713                                                         (*tot_child_ref)->grandparents.insert(current_parent); 
    714                                                 } 
    715  
    716                                                 switch(sender->get_state(SPLIT)) 
    717                                                 { 
    718                                                 case 1: 
    719                                                         current_parent->positivechildren.push_back(sender); 
    720                                                         current_parent->positiveneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); 
    721                                                 break; 
    722                                                 case 0: 
    723                                                         current_parent->neutralchildren.push_back(sender); 
    724                                                         current_parent->positiveneutralvertices.insert(sender->positiveneutralvertices.begin(),sender->positiveneutralvertices.end()); 
    725                                                         current_parent->negativeneutralvertices.insert(sender->negativeneutralvertices.begin(),sender->negativeneutralvertices.end()); 
    726  
    727                                                         if(current_parent->totally_neutral == NULL) 
     820                                                        current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; 
     821                                                } 
     822 
     823                                                if(sender->totally_neutral) 
     824                                                { 
     825                                                        current_parent->totallyneutralchildren.insert(sender); 
     826                                                } 
     827                                                         
     828                                        break; 
     829                                        case -1: 
     830                                                current_parent->negativechildren.push_back(sender); 
     831                                                current_parent->negativeneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); 
     832                                        break; 
     833                                        } 
     834 
     835                                        if(is_last) 
     836                                        { 
     837                                                if((current_parent->negativechildren.size()>0&&current_parent->positivechildren.size()>0)|| 
     838                                                                                                        (current_parent->neutralchildren.size()>0&&current_parent->totally_neutral==false)) 
     839                                                {                                                                
     840                                                                 
     841                                                                for_splitting[level+1].push_back(current_parent); 
     842                                                                 
     843                                                                current_parent->set_state(0, SPLIT); 
     844                                                } 
     845                                                else 
     846                                                { 
     847                                                                 
     848 
     849                                                        if(current_parent->negativechildren.size()>0) 
    728850                                                        { 
    729                                                                 current_parent->totally_neutral = sender->totally_neutral; 
     851                                                                current_parent->set_state(-1, SPLIT); 
     852 
     853                                                                ((toprow*)current_parent)->condition_sum-=toadd->value; 
     854 
     855                                                                         
     856                                                        } 
     857                                                        else if(current_parent->positivechildren.size()>0) 
     858                                                        { 
     859                                                                current_parent->set_state(1, SPLIT); 
     860 
     861                                                                ((toprow*)current_parent)->condition_sum+=toadd->value;                                                                  
    730862                                                        } 
    731863                                                        else 
    732864                                                        { 
    733                                                                 current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; 
     865                                                                current_parent->raise_multiplicity();                                                            
    734866                                                        } 
    735867 
    736                                                         if(sender->totally_neutral) 
     868                                                        ((toprow*)current_parent)->condition_order++; 
     869 
     870                                                        if(level == number_of_parameters - 1) 
    737871                                                        { 
    738                                                                 current_parent->totallyneutralchildren.insert(sender); 
     872                                                                toprow* cur_par_toprow = ((toprow*)current_parent); 
     873                                                                cur_par_toprow->probability = 0.0; 
     874                                                                         
     875                                                                for(list<set<vertex*>>::iterator t_ref = current_parent->triangulation.begin();t_ref!=current_parent->triangulation.end();t_ref++) 
     876                                                                { 
     877                                                                        cur_par_toprow->probability += cur_par_toprow->integrate_simplex(*t_ref,'C'); 
     878                                                                }                                                                        
    739879                                                        } 
    740                                                          
    741                                                 break; 
    742                                                 case -1: 
    743                                                         current_parent->negativechildren.push_back(sender); 
    744                                                         current_parent->negativeneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); 
    745                                                 break; 
    746                                                 } 
    747  
    748                                                 if(is_last) 
    749                                                 { 
    750                                                         if((current_parent->negativechildren.size()>0&&current_parent->positivechildren.size()>0)|| 
    751                                                                                                                 (current_parent->neutralchildren.size()>0&&current_parent->totally_neutral==false)) 
    752                                                         {                                                                
    753                                                                  
    754                                                                         for_splitting[level+1].push_back(current_parent); 
    755                                                                  
    756                                                                         current_parent->set_state(0, SPLIT); 
    757                                                         } 
    758                                                         else 
    759                                                         { 
    760                                                                  
    761  
    762                                                                 if(current_parent->negativechildren.size()>0) 
    763                                                                 { 
    764                                                                         current_parent->set_state(-1, SPLIT); 
    765  
    766                                                                         ((toprow*)current_parent)->condition_sum-=toadd->value; 
    767  
    768                                                                          
    769                                                                 } 
    770                                                                 else if(current_parent->positivechildren.size()>0) 
    771                                                                 { 
    772                                                                         current_parent->set_state(1, SPLIT); 
    773  
    774                                                                         ((toprow*)current_parent)->condition_sum+=toadd->value;                                                                  
    775                                                                 } 
    776                                                                 else 
    777                                                                 { 
    778                                                                         current_parent->raise_multiplicity();                                                            
    779                                                                 } 
    780  
    781                                                                 ((toprow*)current_parent)->condition_order++; 
    782  
    783                                                                 if(level == number_of_parameters - 1) 
    784                                                                 { 
    785                                                                         toprow* cur_par_toprow = ((toprow*)current_parent); 
    786                                                                         cur_par_toprow->probability = 0.0; 
    787                                                                          
    788                                                                         for(list<set<vertex*>>::iterator t_ref = current_parent->triangulation.begin();t_ref!=current_parent->triangulation.end();t_ref++) 
    789                                                                         { 
    790                                                                                 cur_par_toprow->probability += cur_par_toprow->integrate_simplex(*t_ref,'C'); 
    791                                                                         }                                                                        
    792                                                                 } 
    793  
    794                                                                 current_parent->positivechildren.clear(); 
    795                                                                 current_parent->negativechildren.clear(); 
    796                                                                 current_parent->neutralchildren.clear(); 
    797                                                                 current_parent->totallyneutralchildren.clear(); 
    798                                                                 current_parent->totallyneutralgrandchildren.clear(); 
    799                                                                 current_parent->positiveneutralvertices.clear(); 
    800                                                                 current_parent->negativeneutralvertices.clear(); 
    801                                                                 current_parent->totally_neutral = NULL; 
    802                                                                 current_parent->kids_rel_addresses.clear(); 
    803                                                                 current_parent->message_counter = 0; 
    804                                                         } 
    805                                                 } 
    806                                         } 
    807  
    808                                         if(is_last) 
    809                                         { 
    810                                                 send_state_message(current_parent,toadd,toremove,level+1); 
    811                                         } 
     880 
     881                                                        current_parent->positivechildren.clear(); 
     882                                                        current_parent->negativechildren.clear(); 
     883                                                        current_parent->neutralchildren.clear(); 
     884                                                        current_parent->totallyneutralchildren.clear(); 
     885                                                        current_parent->totallyneutralgrandchildren.clear(); 
     886                                                        // current_parent->grandparents.clear(); 
     887                                                        current_parent->positiveneutralvertices.clear(); 
     888                                                        current_parent->negativeneutralvertices.clear(); 
     889                                                        current_parent->totally_neutral = NULL; 
     890                                                        current_parent->kids_rel_addresses.clear(); 
     891                                                        current_parent->message_counter = 0; 
     892                                                } 
     893                                        } 
     894                                } 
     895 
     896                                if(is_last) 
     897                                { 
     898                                        send_state_message(current_parent,toadd,toremove,level+1); 
     899                                } 
    812900                         
    813901                        } 
     
    849937        void step_me(int marker) 
    850938        { 
    851                 /* 
     939                 
    852940                for(int i = 0;i<statistic.size();i++) 
    853941                { 
    854942                        for(polyhedron* horiz_ref = statistic.rows[i];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) 
    855943                        { 
     944                                 
    856945                                if(i==statistic.size()-1) 
    857946                                { 
    858                                         cout << ((toprow*)horiz_ref)->condition << "   " << ((toprow*)horiz_ref)->probability << endl; 
     947                                        //cout << ((toprow*)horiz_ref)->condition_sum << "   " << ((toprow*)horiz_ref)->probability << endl; 
    859948                                        cout << "Order:" << ((toprow*)horiz_ref)->condition_order << endl; 
    860949                                } 
     950                                if(i==0) 
     951                                { 
     952                                        cout << ((vertex*)horiz_ref)->get_coordinates() << endl; 
     953                                } 
     954 
    861955                                char* string = "Checkpoint"; 
    862956                        } 
    863957                } 
    864                 */ 
     958                 
    865959 
    866960                /* 
     
    9541048 
    9551049                list<condition*>::iterator toremove_ref = conditions.end(); 
    956                 bool condition_should_be_added = false; 
     1050                bool condition_should_be_added = should_add; 
    9571051 
    9581052                for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++) 
     
    9861080 
    9871081                                        should_add = false; 
    988                                 } 
    989                                 else 
    990                                 { 
    991                                         condition_should_be_added = true; 
    992                                 } 
    993                         } 
    994                 } 
     1082 
     1083                                        condition_should_be_added = false; 
     1084                                }                                
     1085                        } 
     1086                }        
    9951087 
    9961088                condition* condition_to_remove = NULL; 
     
    9981090                if(toremove_ref!=conditions.end()) 
    9991091                { 
    1000                         conditions.erase(toremove_ref); 
    10011092                        condition_to_remove = *toremove_ref; 
     1093                        conditions.erase(toremove_ref);                  
    10021094                } 
    10031095 
     
    10061098                if(condition_should_be_added) 
    10071099                { 
    1008                         conditions.push_back(new condition(toadd)); 
    1009                 } 
    1010  
    1011                  
     1100                        condition* new_condition = new condition(toadd); 
     1101                         
     1102                        conditions.push_back(new_condition); 
     1103                        condition_to_add = new_condition; 
     1104                }                
    10121105                 
    10131106                for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly) 
     
    11171210                                                { 
    11181211                                                        (*child_ref)->parents.remove(current_negative); 
    1119                                                         (*child_ref)->parents.push_back(current_positive); 
     1212                                                        (*child_ref)->parents.push_back(current_positive);                                                                                                       
    11201213                                                } 
    11211214                                                 
     
    11401233 
    11411234                                                current_positive->parents.insert(current_positive->parents.begin(),current_negative->parents.begin(),current_negative->parents.end()); 
    1142                                                 unique(current_positive->parents.begin(),current_positive->parents.end()); 
     1235                                                // unique(current_positive->parents.begin(),current_positive->parents.end()); 
    11431236 
    11441237                                                for(list<polyhedron*>::iterator parent_ref = current_negative->parents.begin();parent_ref!=current_negative->parents.end();parent_ref++) 
     
    11841277                                                                { 
    11851278                                                                        (*grand_ref)->totallyneutralgrandchildren.erase(current_positive); 
    1186                                                                 } 
    1187  
    1188                                                                 current_positive->grandparents.clear(); 
     1279                                                                }                                                                
    11891280                                                        } 
    11901281                                                        else 
     
    11941285                                                                        (*grand_ref)->totallyneutralgrandchildren.erase(current_negative); 
    11951286                                                                        (*grand_ref)->totallyneutralgrandchildren.insert(current_positive); 
    1196                                                                 } 
    1197  
    1198                                                                 current_positive->grandparents.insert(current_negative->grandparents.begin(),current_negative->grandparents.end()); 
     1287                                                                }                                                                
    11991288                                                        } 
    12001289                                                } 
     
    12101299                                                } 
    12111300 
     1301                                                current_positive->grandparents.clear(); 
     1302 
    12121303                                                current_positive->totally_neutral = (current_positive->totally_neutral && current_negative->totally_neutral); 
    12131304 
     
    12361327                                                } 
    12371328 
     1329                                                 
    12381330                                                for(set<polyhedron*>::iterator grand_p_ref = (*merge_ref)->grandparents.begin();grand_p_ref!=(*merge_ref)->grandparents.end();grand_p_ref++) 
    12391331                                                { 
    12401332                                                        (*grand_p_ref)->totallyneutralgrandchildren.erase(*merge_ref); 
    12411333                                                } 
     1334                                                 
    12421335 
    12431336                                                statistic.delete_polyhedron(k-1,*merge_ref); 
     
    13151408                                        } 
    13161409 
     1410                                        new_totally_neutral_child->parentconditions.insert(current_polyhedron->parentconditions.begin(),current_polyhedron->parentconditions.end()); 
     1411                                        new_totally_neutral_child->parentconditions.insert(condition_to_add); 
     1412 
    13171413                                        new_totally_neutral_child->my_emlig = this; 
    13181414                                         
     
    13341430                                        { 
    13351431                                                (*grand_ref)->parents.push_back(new_totally_neutral_child); 
    1336                                                 (*grand_ref)->grandparents.insert(positive_poly); 
    1337                                                 (*grand_ref)->grandparents.insert(negative_poly); 
     1432                                                 
     1433                                                // tohle tu nebylo. ma to tu byt? 
     1434                                                //positive_poly->totallyneutralgrandchildren.insert(*grand_ref); 
     1435                                                //negative_poly->totallyneutralgrandchildren.insert(*grand_ref); 
     1436 
     1437                                                //(*grand_ref)->grandparents.insert(positive_poly); 
     1438                                                //(*grand_ref)->grandparents.insert(negative_poly); 
    13381439 
    13391440                                                new_totally_neutral_child->vertices.insert((*grand_ref)->vertices.begin(),(*grand_ref)->vertices.end()); 
     
    13471448                                        { 
    13481449                                                (*parent_ref)->totallyneutralgrandchildren.insert(new_totally_neutral_child); 
    1349                                                 new_totally_neutral_child->grandparents.insert(*parent_ref); 
     1450                                                // new_totally_neutral_child->grandparents.insert(*parent_ref); 
    13501451 
    13511452                                                (*parent_ref)->neutralchildren.remove(current_polyhedron); 
     
    14211522                { 
    14221523                        sizevector.push_back(statistic.row_size(s)); 
    1423                 } 
     1524                        cout << statistic.row_size(s) << ", "; 
     1525                } 
     1526 
     1527                cout << endl; 
    14241528 
    14251529                /* 
     
    14891593    void create_statistic(int number_of_parameters) 
    14901594        { 
     1595                /* 
    14911596                for(int i = 0;i<number_of_parameters;i++) 
    14921597                { 
     
    14981603                        conditions.push_back(new_condition); 
    14991604                } 
     1605                */ 
    15001606 
    15011607                // An empty vector of coordinates. 
     
    17911897private: 
    17921898 
     1899         
     1900 
     1901        int window_size;         
     1902 
     1903        list<vec> conditions; 
     1904 
     1905public: 
    17931906        emlig* posterior; 
    17941907 
    1795         int window_size; 
    1796  
    1797 public: 
    17981908        RARX(int number_of_parameters, const int window_size)//:BM() 
    17991909        { 
    18001910                posterior = new emlig(number_of_parameters); 
    18011911 
    1802                 this->window_size = window_size; 
     1912                this->window_size = window_size;                 
    18031913        }; 
    18041914 
    18051915        void bayes(const itpp::vec &yt, const itpp::vec &cond = "") 
    18061916        { 
     1917                conditions.push_back(yt);                
     1918 
     1919                //posterior->step_me(0); 
    18071920                 
     1921                if(conditions.size()>window_size && window_size!=0) 
     1922                {                        
     1923                        posterior->add_and_remove_condition(yt,conditions.front()); 
     1924                        conditions.pop_front(); 
     1925 
     1926                        //posterior->step_me(1); 
     1927                } 
     1928                else 
     1929                { 
     1930                        posterior->add_condition(yt); 
     1931                } 
     1932                                 
    18081933        } 
    18091934