root/applications/robust/robustlib.cpp @ 1376

Revision 1376, 7.3 kB (checked in by sindj, 13 years ago)

Uprava integrace v souladu s clankem. 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                                        /*
37                                        if(should_integrate&&new_simplex->vertices.size()!=3)
38                                        {
39                                                cout << "Error: Wrong vertex count for integration!";
40                                        }
41                                        */
42
43                                        if(should_integrate)
44                                        {
45                                                cur_prob = ((toprow *)this)->integrate_simplex(new_simplex, 'S');
46
47                                                ((toprow *)this)->probability += cur_prob;
48                                        }
49
50                                        triangulation.insert(new_simplex);
51                                }
52                                else
53                                {
54                                        delete new_simplex;
55                                }
56                        }       
57                }       
58
59                if(should_integrate)
60                {
61                        return ((toprow *)this)->probability;
62                }
63                else
64                {
65                        return 0.0;
66                }
67               
68                /*
69                for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++)
70                {                       
71                        for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++)
72                        {
73                                set<vertex*> new_simplex;
74                                new_simplex.insert((*t_ref).begin(),(*t_ref).end());                   
75
76                                for(set<vertex*>::iterator suitable_vert_ref = vertices.begin();*suitable_vert_ref<*(*t_ref).begin();suitable_vert_ref++)
77                                {
78                                        set<vertex*> suitable_simplex;
79                                        suitable_simplex.insert(new_simplex.begin(),new_simplex.end());
80                                       
81                                        suitable_simplex.insert(*suitable_vert_ref);
82
83                                        triangulation.push_back(suitable_simplex);
84
85                                        if(should_integrate)
86                                        {
87                                                ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(suitable_simplex, 'S');
88                                        }
89                                }                               
90                        }       
91                }*/             
92        }
93
94
95        double toprow::integrate_simplex(simplex* simplex, char c)
96        {
97                // cout << ((toprow*)this)->condition << endl;         
98                int sigma_order = ((toprow*)this)->condition_order-simplex->vertices.size()-1;
99
100                // cout << "C:" << condition_order << "  N:" << my_emlig->number_of_parameters << "  C+N:" << condition_order-my_emlig->number_of_parameters << endl;
101                // pause(0.1);
102
103                if(sigma_order >= 0)
104                {
105
106                        //cout << endl;
107                        //cout << ((toprow*)this)->condition << endl;
108                        //cout << "C:" << condition_order+2 << "  N:" << my_emlig->number_of_parameters << "  C+N:" << condition_order-my_emlig->number_of_parameters+2 << endl;
109                       
110
111                        emlig* current_emlig;
112                        simplex->clear_gammas();
113
114                        if(this->my_emlig!=NULL)
115                        {
116                                current_emlig = this->my_emlig;
117                        }
118                        else
119                        {
120                                throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!");
121                        }                                               
122
123                        toprow* as_toprow = (toprow*)this;                                             
124
125                        int dimension = simplex->vertices.size()-1;
126
127                        mat jacobian(dimension,dimension);                     
128
129                        map<double,int> as;
130                        vertex* base_vertex = simplex->vertices.begin();
131                                                       
132                               
133                        for(set<vertex*>::iterator vert_ref = simplex->vertices.begin(); vert_ref!=simplex->vertices.end();vert_ref++)
134                        {
135                                if(vert_ref!=simplex->vertices.begin())
136                                {
137                                        vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates();
138                                        jacobian.set_row(row_count,relative_coords);
139                                }
140
141                                double a = -((*vert_ref)->get_coordinates().ins(0,-1)*cur_condition);
142                                if(a<current_emlig->min_ll)
143                                {
144                                        current_emlig->minimal_vertex = (*vert_ref);
145                                        current_emlig->min_ll = a;                                             
146                                }
147
148                                //cout << "a0:" << a_0 << " a0 coords:" << base_vertex->get_coordinates() << " am:" << a_m << " am coords:" << (*vert_ref)->get_coordinates() << endl;
149
150                                // cout << "Absolute coords:(V"  << row_count << ")" << (*vert_ref)->get_coordinates() << endl;
151                                //cout << "Relative coords:(V"  << row_count << ")" << relative_coords << endl;
152
153                                pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(a,1));
154                                if(returned.second == false)
155                                {
156                                        (*returned.first).second++;
157                                }                                               
158                        }
159                       
160                       
161                        /*
162                        cout << "a_0: " << a_0 << "    ";
163                        int as_count = 1;
164                        for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++)
165                        {
166                                cout << "a_" << as_count << ": " << (*as_ref).first << "    ";
167                                as_count++;
168                        }
169                        */
170
171                        double int_value = 0;                   
172
173                        // cout << jacobian << endl;
174
175                        double det_jacobian    = abs(det(jacobian));
176                        double correction_term;
177                        for(map<double,int>::iterator a_ref = as.begin();as_ref!=as.end();as_ref++)
178                        {
179                                double multiplier = det_jacobian/fact(jacobian.rows());                         
180                               
181                                int a_order = (*a_ref).second;                 
182                                current_emlig->set_correction_factors(a_order);
183
184                                vector<double> factors;
185                                int number_of_factors = 0;                                                             
186                                for(map<double,int>::iterator a2_ref = as.begin();a2_ref!=as.end();a2_ref++)
187                                {                                       
188                                        if(a2_ref!=a_ref)
189                                        {                                                                               
190                                                for(int k = 0;k<(*a2_ref).second;k++)
191                                                {
192                                                        factors.push_back((*a_ref).first-(*a2_ref).first);
193                                                }
194
195                                                multiplier        /= pow((*a_ref).first-(*a2_ref).first,(*a2_ref).second);
196                                                number_of_factors += (*a2_ref).second;
197                                        }                                                                               
198                                }       
199
200                                double cur_as_ref = (*a_ref).first;                             
201
202                                double gamma_multiplier = -cur_as_ref-a_0;
203
204                                double bracket = fact(as1_order-1)/pow(gamma_multiplier,sigma_order);
205                                                               
206                                simplex->insert_gamma(0,bracket*multiplier,gamma_multiplier);                                                           
207
208                                // bracket *= fact(sigma_order);                               
209
210                                for(int k = 0;k < as1_order-1;k++)
211                                {
212                                        double local_bracket = 0;
213                                        double bracket_factor = 1/fact(as1_order-1-k)/pow(gamma_multiplier,sigma_order-k-1);//pow((double)-1,k+1)
214                                       
215                                        ivec control_vec = ivec();
216                                        control_vec.ins(0,my_emlig->number_of_parameters-as1_order+1);                                 
217                                       
218                                        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++)
219                                        {
220                                                double bracket_combination = 1;
221                                                for(int j = 0;j<(*combi_ref).size();j++)
222                                                {
223                                                        //cout << "Factor vector:" << (*combi_ref) << endl;
224                                                       
225                                                        bracket_combination /= factors[(*combi_ref)[j]-1];
226                                                }                                               
227                                                                                               
228                                                local_bracket += bracket_factor*bracket_combination;                                                                   
229                                        }
230
231                                        simplex->insert_gamma(k+1,local_bracket*multiplier,gamma_multiplier);
232
233                                        int division_factor = 1;
234                                        for(int s = 0;s<k;s++)
235                                        {
236                                                division_factor *= k-s;
237                                        }
238                                       
239                                        bracket += local_bracket/division_factor; //local_bracket*fact(sigma_order-k);
240                                }                               
241
242                                int_value += multiplier*bracket;
243
244                               
245                                                                                                                                               
246                        }
247
248                        double correction_term_base = correction_term/pow(-a_0,sigma_order);
249
250                        simplex->insert_gamma(0,correction_term_base,-a_0);                     
251
252                        correction_term = correction_term_base;//fact(sigma_order)*correction_term_base;
253
254                        //cout << c << int_value << endl;
255
256                        int_value += correction_term;                   
257
258                        //cout << "Probability:" << int_value << endl;
259                        //pause(0.100);
260
261                        simplex->probability = int_value;
262                       
263                        return int_value;       
264                       
265                }
266                else
267                {
268                        cout << "Improper probability density." << endl;
269                        return 0.0;
270                }
271        }
Note: See TracBrowser for help on using the browser.