root/applications/robust/robustlib.cpp @ 1325

Revision 1325, 8.9 kB (checked in by sindj, 13 years ago)

Pokracuji v dodelavani samplovani, problem se zapornymi vahami pred gamma rozdelenimi sigmy. 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->gamma_parameters.clear();
100                        simplex->gamma_sum = 0;
101
102                        if(this->my_emlig!=NULL)
103                        {
104                                current_emlig = this->my_emlig;
105                        }
106                        else
107                        {
108                                throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!");
109                        }                                               
110
111                        toprow* as_toprow = (toprow*)this;
112
113                        vec cur_condition = as_toprow->condition_sum.get(1,as_toprow->condition_sum.size()-1);
114
115                        // cout << as_toprow->condition << endl;                       
116
117                        int dimension = simplex->vertices.size()-1;
118
119                        mat jacobian(dimension,dimension);                     
120
121                        double a_0;
122                        map<double,int> as;
123                        vertex* base_vertex;
124                        set<vertex*>::iterator base_ref = simplex->vertices.begin();
125                        bool order_correct;
126                                               
127
128                        do
129                        {
130                                order_correct = true;
131                                as.clear();
132
133                                base_vertex = (*base_ref);
134
135                       
136                                // cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl;
137
138                                a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition_sum[0];
139                               
140
141                                int row_count = 0;
142                                for(set<vertex*>::iterator vert_ref = simplex->vertices.begin(); vert_ref!=simplex->vertices.end();vert_ref++)
143                                {
144                                        if(vert_ref != base_ref)
145                                        {
146                                                vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates();                                           
147
148                                                jacobian.set_row(row_count,relative_coords);
149
150                                                double new_a = -relative_coords*cur_condition;
151
152                                                if(new_a + a_0 == 0 || new_a == 0)
153                                                {
154                                                        base_ref++;
155
156                                                        if(base_ref == simplex->vertices.end())
157                                                        {
158                                                                throw new exception("Equal local conditions are paired. If this ever occurs, the software has to be modified to include multiplied a_0!!");
159                                                        }
160                                                       
161                                                        order_correct = false;                                         
162
163                                                        break;
164                                                }                                               
165                                               
166                                               
167
168                                                if(a_0<current_emlig->likelihood_value)
169                                                {
170                                                        current_emlig->minimal_vertex = base_vertex;
171                                                        current_emlig->likelihood_value = a_0;
172                                                }
173
174                                                double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition_sum[0];
175                                                if(a_m<current_emlig->likelihood_value)
176                                                {
177                                                        current_emlig->minimal_vertex = (*vert_ref);
178                                                        current_emlig->likelihood_value = a_m;                                         
179                                                }
180
181                                                //cout << "a0:" << a_0 << " a0 coords:" << base_vertex->get_coordinates() << " am:" << a_m << " am coords:" << (*vert_ref)->get_coordinates() << endl;
182
183                                                // cout << "Absolute coords:(V"  << row_count << ")" << (*vert_ref)->get_coordinates() << endl;
184                                                //cout << "Relative coords:(V"  << row_count << ")" << relative_coords << endl;
185
186                                                pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(new_a,1));
187                                                if(returned.second == false)
188                                                {
189                                                        (*returned.first).second++;
190                                                }
191                                                /*
192                                                else
193                                                {
194                                                        cout << "a[" << row_count << "] = " << new_a << endl;
195                                                }
196                                                */
197                                               
198                                                //as.ins(as.size(),new_a);                                                     
199                                               
200                                                row_count++;
201                                        }
202                                }
203                        }
204                        while(!order_correct);
205                       
206                        /*
207                        cout << "a_0: " << a_0 << "    ";
208                        int as_count = 1;
209                        for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++)
210                        {
211                                cout << "a_" << as_count << ": " << (*as_ref).first << "    ";
212                                as_count++;
213                        }*/
214
215                        double int_value = 0;
216
217                        // cout << jacobian << endl;
218
219                        double det_jacobian    = abs(det(jacobian));
220                        double correction_term = det_jacobian;                 
221                        for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++)
222                        {
223                                double multiplier = det_jacobian;                               
224                               
225                                int as1_order = (*as_ref).second;
226                               
227                                correction_term /= pow(-(*as_ref).first,as1_order);                                     
228                               
229                                current_emlig->set_correction_factors(as1_order);
230
231                                vector<double> factors;
232                                int number_of_factors = 0;                                                             
233                                for(map<double,int>::iterator as2_ref = as.begin();as2_ref!=as.end();as2_ref++)
234                                {
235                                       
236                                        if(as2_ref!=as_ref)
237                                        {                                                                               
238                                                for(int k = 0;k<(*as2_ref).second;k++)
239                                                {
240                                                        factors.push_back((*as_ref).first-(*as2_ref).first);
241                                                }
242
243                                                multiplier        /= pow((*as_ref).first-(*as2_ref).first,(*as2_ref).second);
244                                                number_of_factors += (*as2_ref).second;
245                                        }
246                                        else
247                                        {
248                                                factors.push_back((*as_ref).first);
249
250                                                multiplier        /= (*as_ref).first;
251                                                number_of_factors += 1;
252                                        }
253                                       
254                                }
255                               
256                                                               
257
258                                double bracket = fact(as1_order-1)/pow(a_0-(*as_ref).first,sigma_order+1);
259                               
260                                simplex->gamma_sum += bracket*multiplier;
261                               
262                                multimap<double,double> map;
263                                simplex->gamma_parameters.push_back(map);
264                                simplex->gamma_parameters[0].insert(pair<double,double>(bracket*multiplier,a_0-(*as_ref).first));
265
266                                bracket *= fact(sigma_order);
267
268                                for(int k = 0;k < as1_order-1;k++)
269                                {
270                                        double local_bracket = 0;
271                                        double bracket_factor = -pow((double)-1,k+1)/fact(as1_order-1-k)/pow(a_0-(*as_ref).first,sigma_order-k);
272                                       
273                                        ivec control_vec = ivec();
274                                        control_vec.ins(0,my_emlig->number_of_parameters-as1_order+1);                                 
275                                       
276                                        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++)
277                                        {
278                                                double bracket_combination = 1;
279                                                for(int j = 0;j<(*combi_ref).size();j++)
280                                                {
281                                                        //cout << "Factor vector:" << (*combi_ref) << endl;
282                                                       
283                                                        bracket_combination /= factors[(*combi_ref)[j]-1];
284                                                }                                               
285                                                                                               
286                                                local_bracket += bracket_factor*bracket_combination;                                                                   
287                                        }
288
289                                        simplex->gamma_sum += local_bracket*multiplier;
290                                       
291                                        if(simplex->gamma_parameters.size()<=k+1)
292                                        {
293                                                multimap<double,double> loc_map;
294                                                simplex->gamma_parameters.push_back(loc_map);
295                                        }
296
297                                        simplex->gamma_parameters[k+1].insert(pair<double,double>(local_bracket*multiplier,a_0-(*as_ref).first));
298
299                                        bracket += local_bracket*fact(sigma_order-k);
300                                }                               
301
302                                int_value += multiplier*bracket;
303                                                                                                                                               
304                        }
305
306                        double correction_term_base = correction_term/pow(a_0,sigma_order+1);
307
308                        simplex->gamma_sum += correction_term_base;
309                        simplex->gamma_parameters[0].insert(pair<double,double>(correction_term_base,a_0));
310
311                        correction_term = fact(sigma_order)*correction_term_base;
312
313                        //cout << c << int_value << endl;
314
315                        int_value += correction_term;
316               
317
318                        //cout << "Probability:" << int_value << endl;
319                        //pause(0.100);
320
321                        simplex->probability = int_value;
322                       
323                        return int_value;       
324                       
325                }
326                else
327                {
328                        return 0.0;
329                }
330        }
Note: See TracBrowser for help on using the browser.