root/applications/robust/robustlib.cpp @ 1331

Revision 1331, 8.4 kB (checked in by sindj, 13 years ago)

Dodelan sampling sigma, zbyva doladit sampling alpha.JS

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