root/applications/robust/robustlib.cpp @ 1285

Revision 1282, 6.7 kB (checked in by sindj, 13 years ago)

Prvni pokusy s robustnim AR modelem. JS

Line 
1#include "robustlib.h"
2
3
4void polyhedron::triangulate(bool should_integrate)
5        {
6                if(should_integrate)
7                {
8                        ((toprow *)this)->probability = 0.0;
9                }               
10               
11                for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++)
12                {                       
13                        for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++)
14                        {
15                                set<vertex*> new_simplex;
16                                new_simplex.insert((*t_ref).begin(),(*t_ref).end());                           
17
18                                pair<set<vertex*>::iterator,bool> ret_val = new_simplex.insert(*vertices.begin());
19
20                                if(ret_val.second == true)
21                                {
22                                        triangulation.push_back(new_simplex);
23
24                                        if(should_integrate)
25                                        {
26                                                ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(new_simplex, 'S');
27                                        }
28                                } 
29                        }       
30                }               
31        }
32
33
34        double toprow::integrate_simplex(set<vertex*> simplex, char c)
35        {
36                // cout << ((toprow*)this)->condition << endl;         
37
38                int condition_order = ((toprow*)this)->condition_order-2; // -2 by bylo, pokud chceme uniformni apriorno
39
40                int sigma_order = condition_order-my_emlig->number_of_parameters;
41
42                // cout << "C:" << condition_order << "  N:" << my_emlig->number_of_parameters << "  C+N:" << condition_order-my_emlig->number_of_parameters << endl;
43                // pause(0.1);
44
45                if(sigma_order >= 0)
46                {
47
48                        //cout << endl;
49                        //cout << ((toprow*)this)->condition << endl;
50                        //cout << "C:" << condition_order+2 << "  N:" << my_emlig->number_of_parameters << "  C+N:" << condition_order-my_emlig->number_of_parameters+2 << endl;
51                       
52
53                        emlig* current_emlig;
54
55                        if(this->my_emlig!=NULL)
56                        {
57                                current_emlig = this->my_emlig;
58                        }
59                        else
60                        {
61                                throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!");
62                        }                                               
63
64                        toprow* as_toprow = (toprow*)this;
65
66                        vec cur_condition = as_toprow->condition.get(1,as_toprow->condition.size()-1);
67
68                        // cout << as_toprow->condition << endl;                       
69
70                        int dimension = simplex.size()-1;
71
72                        mat jacobian(dimension,dimension);                     
73
74                        double a_0;
75                        map<double,int> as;
76                        vertex* base_vertex;
77                        set<vertex*>::iterator base_ref = simplex.begin();
78                        bool order_correct;
79                                               
80
81                        do
82                        {
83                                order_correct = true;
84                                as.clear();
85
86                                base_vertex = (*base_ref);
87
88                       
89                                //cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl;
90
91                                a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition[0];
92                               
93
94                                int row_count = 0;
95                                for(set<vertex*>::iterator vert_ref = simplex.begin(); vert_ref!=simplex.end();vert_ref++)
96                                {
97                                        if(vert_ref != base_ref)
98                                        {
99                                                vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates();                                           
100
101                                                jacobian.set_row(row_count,relative_coords);
102
103                                                double new_a = -relative_coords*cur_condition;
104
105                                                if(new_a + a_0 == 0 || new_a == 0)
106                                                {
107                                                        base_ref++;
108
109                                                        if(base_ref == simplex.end())
110                                                        {
111                                                                throw new exception("Equal local conditions are paired. If this ever occurs, the software has to be modified to include multiplied a_0!!");
112                                                        }
113                                                       
114                                                        order_correct = false;                                         
115
116                                                        break;
117                                                }                                               
118                                               
119                                               
120
121                                                if(a_0<current_emlig->likelihood_value)
122                                                {
123                                                        current_emlig->minimal_vertex = base_vertex;
124                                                        current_emlig->likelihood_value = a_0;
125                                                }
126
127                                                double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition[0];
128                                                if(a_m<current_emlig->likelihood_value)
129                                                {
130                                                        current_emlig->minimal_vertex = (*vert_ref);
131                                                        current_emlig->likelihood_value = a_m;                                         
132                                                }
133
134                                                //cout << "a0:" << a_0 << " a0 coords:" << base_vertex->get_coordinates() << " am:" << a_m << " am coords:" << (*vert_ref)->get_coordinates() << endl;
135
136                                                //cout << "Absolute coords:(V"  << row_count << ")" << (*vert_ref)->get_coordinates() << endl;
137                                                //cout << "Relative coords:(V"  << row_count << ")" << relative_coords << endl;
138
139                                                pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(new_a,1));
140                                                if(returned.second == false)
141                                                {
142                                                        (*returned.first).second++;
143                                                }
144                                                /*
145                                                else
146                                                {
147                                                        cout << "a[" << row_count << "] = " << new_a << endl;
148                                                }
149                                                */
150                                               
151                                                //as.ins(as.size(),new_a);                                                     
152                                               
153                                                row_count++;
154                                        }
155                                }
156                        }
157                        while(!order_correct);
158                       
159                        /*
160                        cout << "a_0: " << a_0 << "    ";
161                        int as_count = 1;
162                        for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++)
163                        {
164                                cout << "a_" << as_count << ": " << (*as_ref).first << "    ";
165                                as_count++;
166                        }*/
167
168                        double int_value = 0;
169
170                        // cout << jacobian << endl;
171
172                        double det_jacobian    = abs(det(jacobian));
173                        double correction_term = det_jacobian;                 
174                        for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++)
175                        {
176                                double multiplier = det_jacobian;                               
177                               
178                                int as1_order = (*as_ref).second;
179                               
180                                correction_term /= pow(-(*as_ref).first,as1_order);                                     
181                               
182                                current_emlig->set_correction_factors(as1_order);
183
184                                vector<double> factors;
185                                int number_of_factors = 0;                                                             
186                                for(map<double,int>::iterator as2_ref = as.begin();as2_ref!=as.end();as2_ref++)
187                                {
188                                       
189                                        if(as2_ref!=as_ref)
190                                        {                                                                               
191                                                for(int k = 0;k<(*as2_ref).second;k++)
192                                                {
193                                                        factors.push_back((*as_ref).first-(*as2_ref).first);
194                                                }
195
196                                                multiplier        /= pow((*as_ref).first-(*as2_ref).first,(*as2_ref).second);
197                                                number_of_factors += (*as2_ref).second;
198                                        }
199                                        else
200                                        {
201                                                factors.push_back((*as_ref).first);
202
203                                                multiplier        /= (*as_ref).first;
204                                                number_of_factors += 1;
205                                        }
206                                       
207                                }       
208
209                               
210
211                                double bracket = fact(sigma_order)/fact(as1_order-1)/pow(a_0-(*as_ref).first,sigma_order+1);
212                                for(int k = 0;k < as1_order-1;k++)
213                                {
214                                        double bracket_factor = -pow((double)-1,k+1)*fact(sigma_order-k)/fact(as1_order-1-k)/pow(a_0-(*as_ref).first,sigma_order-k);
215                                       
216                                        ivec control_vec = ivec();
217                                        control_vec.ins(0,my_emlig->number_of_parameters-as1_order+1);         
218                                       
219                                       
220                                        for(multiset<my_ivec>::iterator combi_ref = this->my_emlig->correction_factors[k].begin();combi_ref!=this->my_emlig->correction_factors[k].upper_bound(my_ivec(control_vec));combi_ref++)
221                                        {
222                                                double bracket_combination = 1;
223                                                for(int j = 0;j<(*combi_ref).size();j++)
224                                                {
225                                                        //cout << "Factor vector:" << (*combi_ref) << endl;
226                                                       
227                                                        bracket_combination /= factors[(*combi_ref)[j]-1];
228                                                }
229
230                                                bracket+=bracket_factor*bracket_combination;                                                                   
231                                        }                                                                       
232                                }
233
234                               
235
236                                int_value += multiplier*bracket;
237                                                                                                                                               
238                        }
239
240                       
241                        correction_term *= fact(sigma_order)/abs(pow(a_0,sigma_order+1));
242
243                        // cout << c << int_value << endl;
244
245                        int_value += correction_term;
246               
247
248                        //cout << "Probability:" << int_value << endl;
249                       
250                        return int_value;
251
252                       
253                       
254                }
255                else
256                {
257                        return 0.0;
258                }
259        }
Note: See TracBrowser for help on using the browser.