root/applications/robust/robustlib.cpp @ 1337

Revision 1335, 8.5 kB (checked in by sindj, 13 years ago)

Dokoncovani samplingu, uz to skoro funguje, jen to spatne generuje sigma. JS

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