Changeset 1282 for applications
- Timestamp:
- 02/21/11 09:22:08 (14 years ago)
- Location:
- applications/robust
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/main.cpp
r1281 r1282 9 9 #include "robustlib.h" 10 10 #include <vector> 11 #include <iostream> 12 #include <fstream> 13 11 14 using namespace itpp; 12 13 15 //using namespace bdm; 14 16 … … 18 20 int main ( int argc, char* argv[] ) { 19 21 20 21 emlig* emlig1 = new emlig(emlig_size); 22 23 24 25 vector<vector<vector<string>>> string_lists; 26 string_lists.push_back(vector<vector<string>>()); 27 string_lists.push_back(vector<vector<string>>()); 28 string_lists.push_back(vector<vector<string>>()); 29 30 char* file_strings[3] = {"c:\\ar_normal.txt", "c:\\ar_student.txt", "c:\\ar_cauchy.txt"}; 31 32 33 for(int i = 0;i<3;i++) 34 { 35 ifstream myfile(file_strings[i]); 36 if (myfile.is_open()) 37 { 38 while ( myfile.good() ) 39 { 40 string line; 41 getline(myfile,line); 42 43 vector<string> parsed_line; 44 while(line.find(',') != string::npos) 45 { 46 int loc = line.find(','); 47 parsed_line.push_back(line.substr(0,loc)); 48 line.erase(0,loc+1); 49 } 50 51 string_lists[i].push_back(parsed_line); 52 } 53 myfile.close(); 54 } 55 } 56 57 for(int j = 0;j<string_lists.size();j++) 58 { 59 60 for(int i = 0;i<string_lists[j].size();i++) 61 { 62 vector<vec> conditions; 63 emlig* emliga = new emlig(2); 64 for(int k = 1;k<string_lists[j][i].size();k++) 65 { 66 vec condition; 67 //condition.ins(0,1); 68 condition.ins(0,string_lists[j][i][k]); 69 conditions.push_back(condition); 70 71 //cout << "orig:" << condition << endl; 72 73 if(conditions.size()>1) 74 { 75 conditions[k-2].ins(0,string_lists[j][i][k]); 76 77 } 78 79 if(conditions.size()>2) 80 { 81 conditions[k-3].ins(0,string_lists[j][i][k]); 82 83 //cout << "modi:" << conditions[k-3] << endl; 84 85 emliga->add_condition(conditions[k-3]); 86 87 /* 88 if(k>5) 89 { 90 cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; 91 } 92 */ 93 } 94 } 95 96 //emliga->step_me(0); 97 cout << "MaxLik coords:" << emliga->minimal_vertex->get_coordinates() << endl; 98 cout << "Step: " << i << endl; 99 } 100 101 cout << "One experiment finished." << endl; 102 } 103 104 105 106 //emlig* emlig1 = new emlig(emlig_size); 22 107 23 108 /* … … 39 124 }*/ 40 125 41 vec condition1 = "1.0 1.0 1.0"; 126 /* 127 vec condition1 = "1.0 1.0 1.01"; 42 128 emlig1->add_condition(condition1); 43 129 … … 45 131 emlig1->add_condition(condition2); 46 132 47 vec condition3 = "0.5 -1. 1 1.0";133 vec condition3 = "0.5 -1.01 1.0"; 48 134 emlig1->add_condition(condition3); 49 135 … … 51 137 emlig1->add_condition(condition4); 52 138 53 vec condition5 = "-0.3 1.7 1.5"; 54 emlig1->add_condition(condition5); 55 139 cout << emlig1->minimal_vertex->get_coordinates(); 140 141 //vec condition5 = "-0.3 1.7 1.5"; 142 //emlig1->add_condition(condition5); 143 144 // DA SE POUZIT PRO VYPIS DO SOUBORU 56 145 // emlig1->step_me(0); 57 146 … … 61 150 62 151 63 64 152 /* 65 for(int i = 0;i<500;i++) 66 { 67 cout << endl << "Step:" << i << endl; 68 69 153 154 for(int i = 0;i<100;i++) 155 { 156 cout << endl << "Step:" << i << endl; 70 157 71 158 double condition[emlig_size+1]; … … 139 226 } 140 227 228 229 -
applications/robust/robustlib.cpp
r1281 r1282 1 1 #include "robustlib.h" 2 2 3 3 4 void polyhedron::triangulate(bool should_integrate) … … 33 34 double toprow::integrate_simplex(set<vertex*> simplex, char c) 34 35 { 35 // cout << ((toprow*)this)->condition << endl; 36 36 // cout << ((toprow*)this)->condition << endl; 37 37 38 int condition_order = ((toprow*)this)->condition_order-2; // -2 by bylo, pokud chceme uniformni apriorno 38 39 … … 45 46 { 46 47 47 cout << endl; 48 cout << ((toprow*)this)->condition << endl; 49 cout << "C:" << condition_order+2 << " N:" << my_emlig->number_of_parameters << " C+N:" << condition_order-my_emlig->number_of_parameters+2 << endl; 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 50 52 51 53 emlig* current_emlig; … … 84 86 base_vertex = (*base_ref); 85 87 86 cout << "Base coords:" << base_vertex->get_coordinates() << endl; 88 89 //cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl; 87 90 88 91 a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition[0]; 92 89 93 90 94 int row_count = 0; … … 111 115 112 116 break; 113 } 114 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 115 136 //cout << "Absolute coords:(V" << row_count << ")" << (*vert_ref)->get_coordinates() << endl; 116 cout << "Relative coords:(V" << row_count << ")" << relative_coords << endl;137 //cout << "Relative coords:(V" << row_count << ")" << relative_coords << endl; 117 138 118 139 pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(new_a,1)); … … 136 157 while(!order_correct); 137 158 159 /* 138 160 cout << "a_0: " << a_0 << " "; 139 161 int as_count = 1; … … 142 164 cout << "a_" << as_count << ": " << (*as_ref).first << " "; 143 165 as_count++; 144 } 166 }*/ 145 167 146 168 double int_value = 0; … … 224 246 225 247 226 cout << "Probability:" << int_value << endl;248 //cout << "Probability:" << int_value << endl; 227 249 228 250 return int_value; -
applications/robust/robustlib.h
r1281 r1282 21 21 using namespace itpp; 22 22 23 const double max_range = 10 .0;//numeric_limits<double>::max()/10e-10;23 const double max_range = 10;//numeric_limits<double>::max()/10e-10; 24 24 25 25 enum actions {MERGE, SPLIT}; 26 27 26 28 27 29 class polyhedron; … … 187 189 { 188 190 /// A dynamic array representing coordinates of the vertex 189 vec coordinates; 190 191 191 vec coordinates; 192 192 193 193 public: 194 194 195 195 double function_value; 196 196 197 197 /// Default constructor … … 238 238 double probability; 239 239 240 vertex* minimal_vertex; 241 240 242 /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation 241 243 vec condition; … … 589 591 590 592 double normalization_factor; 593 594 591 595 592 596 void alter_toprow_conditions(vec condition, bool should_be_added) … … 802 806 c_statistic statistic; 803 807 808 vertex* minimal_vertex; 809 810 double likelihood_value; 811 804 812 vector<multiset<my_ivec>> correction_factors; 805 813 … … 812 820 this->number_of_parameters = number_of_parameters; 813 821 814 create_statistic(number_of_parameters); 822 create_statistic(number_of_parameters); 823 824 likelihood_value = numeric_limits<double>::max(); 815 825 } 816 826 … … 819 829 emlig(c_statistic statistic) 820 830 { 821 this->statistic = statistic; 831 this->statistic = statistic; 832 833 likelihood_value = numeric_limits<double>::max(); 822 834 } 823 835 824 836 void step_me(int marker) 825 837 { 838 826 839 for(int i = 0;i<statistic.size();i++) 827 840 { … … 836 849 } 837 850 } 851 852 /* 853 list<vec> table_entries; 854 for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.row_ends[statistic.size()-1];horiz_ref=horiz_ref->next_poly) 855 { 856 toprow *current_toprow = (toprow*)(horiz_ref); 857 for(list<set<vertex*>>::iterator tri_ref = current_toprow->triangulation.begin();tri_ref!=current_toprow->triangulation.end();tri_ref++) 858 { 859 for(set<vertex*>::iterator vert_ref = (*tri_ref).begin();vert_ref!=(*tri_ref).end();vert_ref++) 860 { 861 vec table_entry = vec(); 862 863 table_entry.ins(0,(*vert_ref)->get_coordinates()*current_toprow->condition.get(1,current_toprow->condition.size()-1)-current_toprow->condition.get(0,0)); 864 865 table_entry.ins(0,(*vert_ref)->get_coordinates()); 866 867 table_entries.push_back(table_entry); 868 } 869 } 870 } 871 872 unique(table_entries.begin(),table_entries.end()); 873 874 875 876 for(list<vec>::iterator entry_ref = table_entries.begin();entry_ref!=table_entries.end();entry_ref++) 877 { 878 ofstream myfile; 879 myfile.open("robust_data.txt", ios::out | ios::app); 880 if (myfile.is_open()) 881 { 882 for(int i = 0;i<(*entry_ref).size();i++) 883 { 884 myfile << (*entry_ref)[i] << ";"; 885 } 886 myfile << endl; 887 888 myfile.close(); 889 } 890 else 891 { 892 cout << "File problem." << endl; 893 } 894 } 895 */ 896 897 898 return; 838 899 } 839 900 … … 862 923 void add_and_remove_condition(vec toadd, vec toremove) 863 924 { 925 likelihood_value = numeric_limits<double>::max(); 926 864 927 bool should_remove = (toremove.size() != 0); 865 928 bool should_add = (toadd.size() != 0);