root/applications/robust/main.cpp @ 1401

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

Nevim, co se zmenilo, dodelani experimentu s maxlik odhady asi. JS

Line 
1
2/*!
3\file
4\brief Robust
5\author Vasek Smidl
6
7 */
8
9#include "estim/arx.h"
10#include "robustlib.h"
11#include <vector>
12#include <iostream>
13#include <fstream>
14//#include <itpp/itsignal.h>
15#include "windows.h"
16#include "ddeml.h"
17#include "stdio.h"
18
19//#include "DDEClient.h"
20//#include <conio.h>
21
22
23using namespace itpp;
24using namespace bdm;
25
26//const int emlig_size = 2;
27//const int utility_constant = 5;
28
29const int max_model_order = 2;
30const double apriorno     = 0.01;
31const int max_window_size = 30;
32
33class model
34{
35public:
36        set<pair<int,int>> ar_components;
37
38        // Best thing would be to inherit the two models from a single souce, this is planned, but now structurally
39        // problematic.
40        RARX* my_rarx; //vzmenovane parametre pre triedu model
41        ARXwin* my_arx;
42
43        bool has_constant;
44        int  window_size;  //musi byt vacsia ako pocet krokov ak to nema ovplyvnit
45        int  predicted_channel;
46        mat* data_matrix;
47        vec  predictions;
48        char name[80];
49       
50        model(set<pair<int,int>> ar_components, //funkcie treidz model-konstruktor
51                  bool robust, 
52                  bool has_constant, 
53                  int window_size, 
54                  int predicted_channel,
55                  mat* data_matrix)
56        {
57                this->ar_components.insert(ar_components.begin(),ar_components.end());
58
59                strcpy(name,"M");
60
61                for(set<pair<int,int>>::iterator ar_ref = ar_components.begin();ar_ref!=ar_components.end();ar_ref++)
62                {
63                        char buffer1[2];
64                        char buffer2[2];
65                        itoa((*ar_ref).first,buffer1,10);
66                        itoa((*ar_ref).second,buffer2,10);
67
68                        strcat(name,buffer1);
69                        strcat(name,buffer2);
70                        strcat(name,"_");
71                }
72
73                this->has_constant      = has_constant;
74
75                if(has_constant)
76                {
77                        strcat(name,"C");
78                }
79
80                this->window_size       = window_size;
81                this->predicted_channel = predicted_channel;
82                this->data_matrix       = data_matrix;
83
84                if(robust)
85                {
86                        strcat(name,"R");
87
88                        if(has_constant)
89                        {
90                                my_rarx = new RARX(ar_components.size()+1,window_size,true,sqrt(2*apriorno),sqrt(2*apriorno),ar_components.size()+4);
91                                my_arx  = NULL;
92                        }
93                else
94                        {
95                                my_rarx = new RARX(ar_components.size(),window_size,false,sqrt(2*apriorno),sqrt(2*apriorno),ar_components.size()+3);
96                                my_arx  = NULL;
97                        }
98                }
99                else
100                {
101                        my_rarx = NULL;
102                        my_arx  = new ARXwin();
103                        mat V0;
104
105                        if(has_constant)
106                        {                               
107                                V0  = apriorno * eye(ar_components.size()+2); //aj tu konst
108                                //V0(0,0) = 0;
109                                my_arx->set_constant(true);                             
110                        }
111                        else
112                        {
113                               
114                                V0  = apriorno * eye(ar_components.size()+1);//menit konstantu
115                                //V0(0,1) = -0.01;
116                                //V0(1,0) = -0.01;
117                                my_arx->set_constant(false);                           
118                               
119                        }
120
121                        my_arx->set_statistics(1, V0, V0.rows()+2);                     
122                        my_arx->set_parameters(window_size);
123                        my_arx->validate();
124
125                        vec mean = my_arx->posterior().mean();
126                        cout << mean << endl;
127                }
128        }
129
130        void data_update(int time) //vlozime cas a ono vlozi do data_vector podmineky(conditions) a predikce, ktore pouzije do bayes
131        {
132                vec data_vector;
133                for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++)
134                { 
135                        data_vector.ins(data_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second));
136                }
137                if(my_rarx!=NULL)
138                {       
139                        data_vector.ins(0,(*data_matrix).get(predicted_channel,time));
140                        my_rarx->bayes(data_vector);
141                }
142                else
143                {
144                        vec pred_vec;
145                        pred_vec.ins(0,(*data_matrix).get(predicted_channel,time));
146                        my_arx->bayes(pred_vec,data_vector);
147                }
148        }
149
150        pair<vec,vec> predict(int sample_size, int time, itpp::Laplace_RNG* LapRNG)  //nerozumiem, ale vraj to netreba, nepouziva to
151        {
152                vec condition_vector;
153                for(set<pair<int,int>>::iterator ar_iterator = ar_components.begin();ar_iterator!=ar_components.end();ar_iterator++)
154                {
155                        condition_vector.ins(condition_vector.size(),(*data_matrix).get(ar_iterator->first,time-ar_iterator->second+1));
156                }
157
158                if(my_rarx!=NULL)
159                {
160                        pair<vec,mat> imp_samples = my_rarx->posterior->sample(sample_size,false);
161
162                        //cout << imp_samples.first << endl;                   
163                       
164                        vec sample_prediction;                 
165                        for(int t = 0;t<sample_size;t++)
166                        {
167                                vec lap_sample = condition_vector;
168                               
169                                if(has_constant)
170                                {
171                                        lap_sample.ins(lap_sample.size(),1.0);
172                                }
173                               
174                                lap_sample.ins(lap_sample.size(),(*LapRNG)());
175
176                                sample_prediction.ins(0,lap_sample*imp_samples.second.get_col(t));                             
177                        }
178
179                        return pair<vec,vec>(imp_samples.first,sample_prediction);
180                }       
181                else
182                {
183                        mat samples = my_arx->posterior().sample_mat(sample_size);                     
184                       
185                        vec sample_prediction;
186                        for(int t = 0;t<sample_size;t++)
187                        {
188                                vec gau_sample = condition_vector;
189                               
190                                if(has_constant)
191                                {
192                                        gau_sample.ins(gau_sample.size(),1.0);
193                                }
194                               
195                                gau_sample.ins(gau_sample.size(),randn());
196
197                                sample_prediction.ins(0,gau_sample*samples.get_col(t));                         
198                        }
199
200                        return pair<vec,vec>(ones(sample_prediction.size()),sample_prediction);
201                }
202       
203        }
204
205
206        static set<set<pair<int,int>>> possible_models_recurse(int max_order,int number_of_channels)
207        {
208                set<set<pair<int,int>>> created_model_types;           
209
210                if(max_order == 1)
211                {                       
212                        for(int channel = 0;channel<number_of_channels;channel++)
213                        {
214                                set<pair<int,int>> returned_type;
215                                returned_type.insert(pair<int,int>(channel,1)); 
216                                created_model_types.insert(returned_type);
217                        }
218
219                        return created_model_types;
220                }
221                else
222                {
223                        created_model_types = possible_models_recurse(max_order-1,number_of_channels);
224                        set<set<pair<int,int>>> returned_types;
225                       
226                        for(set<set<pair<int,int>>>::iterator model_ref = created_model_types.begin();model_ref!=created_model_types.end();model_ref++)
227                        {                               
228                               
229                                for(int order = 1; order<=max_order; order++)
230                                {
231                                        for(int channel = 0;channel<number_of_channels;channel++)
232                                        {
233                                                set<pair<int,int>> returned_type;
234                                                pair<int,int> new_pair = pair<int,int>(channel,order);
235                                                if(find((*model_ref).begin(),(*model_ref).end(),new_pair)==(*model_ref).end()) 
236                                                {
237                                                        returned_type.insert((*model_ref).begin(),(*model_ref).end()); 
238                                                        returned_type.insert(new_pair);
239                                                       
240
241                                                        returned_types.insert(returned_type);                                                   
242                                                }
243                                        }
244                                }
245                        }
246
247                        created_model_types.insert(returned_types.begin(),returned_types.end());
248
249                        return created_model_types;
250                }               
251        }
252};
253
254
255
256
257int main ( int argc, char* argv[] ) 
258{
259        vector<vector<string>> strings;
260
261        char* file_string =  "C:\\results\\normalM"; // "C:\\dataADClosePercDiff"; // 
262
263        char dfstring[80];
264        strcpy(dfstring,file_string);
265        strcat(dfstring,".txt");
266       
267       
268        mat data_matrix;
269        ifstream myfile(dfstring);
270        if (myfile.is_open())
271        {               
272                string line;
273                while(getline(myfile,line))
274                {                       
275                        vec data_vector;
276                        while(line.find(',') != string::npos) 
277                        {                               
278                                int loc2 = line.find('\n');
279                                int loc  = line.find(',');
280                                data_vector.ins(data_vector.size(),atof(line.substr(0,loc).c_str()));                           
281                                line.erase(0,loc+1);                                   
282                        }
283
284                        data_matrix.ins_row(data_matrix.rows(),data_vector);
285                }               
286
287                myfile.close(); 
288        }
289        else
290        {
291                cout << "Can't open data file!" << endl;
292        }
293
294        set<pair<int,int>> model_type;
295        model_type.insert(pair<int,int>(0,1));
296        model_type.insert(pair<int,int>(0,2));
297
298        vector<model*> models;
299
300        ofstream myfilew;
301               
302
303        while(data_matrix.rows()!=0)
304        {
305                for(int i=0;i<models.size();i++)
306                {
307                        delete models[i];
308                }
309               
310                models.clear();
311                models.push_back(new model(model_type,true,false,max_window_size,0,&data_matrix));
312                models.push_back(new model(model_type,false,false,max_window_size,0,&data_matrix));
313
314                for(int time = max_model_order;time<max_window_size;time++) //time<data_matrix.cols()
315                {               
316                        vec cur_res_lognc;
317               
318                        vector<string> nazvy;
319                        for(vector<model*>::iterator model_ref = models.begin();model_ref!=models.end();model_ref++)
320                        {
321                                (*model_ref)->data_update(time);
322
323                                cout << "Updated:" << time << endl;     
324
325                                if(time == max_window_size-1)
326                                {
327                                        char fstring[80];                                       
328                                        strcpy(fstring,file_string);
329                                        strcat(fstring,"ml");                                   
330                                        strcat(fstring,(*model_ref)->name);
331                                        strcat(fstring,".txt");
332
333                                        vec coords;
334                                        if((*model_ref)->my_arx!=NULL)
335                                        {
336                                                coords = (*model_ref)->my_arx->posterior().est_theta();
337                                        }
338                                        else
339                                        {
340                                                coords = (*model_ref)->my_rarx->posterior->minimal_vertex->get_coordinates();                                           
341                                        }
342
343                                        myfilew.open(fstring,ios::app);
344
345                                        for(int i=0;i<coords.size();i++)
346                                        {
347                                                myfilew << coords.get(i) << ",";
348                                        }
349                                        myfilew << endl;
350
351                                        myfilew.close();                               
352                                }
353                        }
354                }
355
356                data_matrix.del_row(0);
357        }
358       
359
360
361       
362
363        return 0;
364}
365
366
Note: See TracBrowser for help on using the browser.