1 | /*! |
---|
2 | \file |
---|
3 | \brief Application Estimator |
---|
4 | |
---|
5 | The general task of estimation is defined on the following scheme: |
---|
6 | \dot |
---|
7 | digraph estimation{ |
---|
8 | node [shape=box]; |
---|
9 | {rank="same"; "Data Source"; "Bayesian Model"} |
---|
10 | "Data Source" -> "Bayesian Model" [label="data"]; |
---|
11 | "Bayesian Model" -> "Result Logger" [label="estimated\n statistics"]; |
---|
12 | "Data Source" -> "Result Logger" [label="Simulated\n data"]; |
---|
13 | } |
---|
14 | \enddot |
---|
15 | |
---|
16 | Here, |
---|
17 | \li Data Source is an object (class DS) providing sequential data, \f$ [d_1, d_2, \ldots d_t] \f$. |
---|
18 | \li Bayesian Model is an object (class BM) performing Bayesian filtering, |
---|
19 | \li Result Logger is an object (class logger) dedicated to storing important data from the experiment. |
---|
20 | |
---|
21 | \section cmd Command-line usage |
---|
22 | Execute command: |
---|
23 | \code |
---|
24 | $> estimator config_file.cfg |
---|
25 | \endcode |
---|
26 | |
---|
27 | Full description of the experiment is in the file config_file.cfg which is expected to have the following structure: |
---|
28 | \code |
---|
29 | system = {type = "DS_offspring", ...}; // definition of a data source |
---|
30 | estimator = {type = "BM_offspring", ...}; // definition of an estimator |
---|
31 | logger = {type = "logger_type",...}; // definition of a logger |
---|
32 | experiment = {ndat = 11000; }; // definition of number of data records |
---|
33 | \endcode |
---|
34 | |
---|
35 | The above description must be specialized to specific classes. See, \subpage arx_ui how to do it for estimation of an ARX model. |
---|
36 | |
---|
37 | \section ex Matlab usage |
---|
38 | Execute command: |
---|
39 | \code |
---|
40 | >> estimator('config_file.cfg'); |
---|
41 | \endcode |
---|
42 | when using loggers storing results on hard drives, and |
---|
43 | \code |
---|
44 | >> Res=estimator('config_file.cfg'); |
---|
45 | \endcode |
---|
46 | when using logger of the type \c "mex_logger". The results will be stored in structure \c M. |
---|
47 | |
---|
48 | */ |
---|
49 | |
---|
50 | #include <estim/arx_ext.h> |
---|
51 | #include <stat/emix.h> |
---|
52 | #include <base/datasources.h> |
---|
53 | #include <base/loggers.h> |
---|
54 | #include <estim/particles.h> |
---|
55 | #include <estim/kalman.h> |
---|
56 | |
---|
57 | //#include "mex_datasource.h" |
---|
58 | |
---|
59 | using namespace bdm; |
---|
60 | |
---|
61 | #ifdef MEX |
---|
62 | #include <itpp/itmex.h> |
---|
63 | #include <mex/mex_BM.h> |
---|
64 | #include <mex/mex_logger.h> |
---|
65 | #include <mex/mex_datasource.h> |
---|
66 | #include <mex/mex_function.h> |
---|
67 | |
---|
68 | void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { |
---|
69 | // Check the number of inputs and output arguments |
---|
70 | if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n" |
---|
71 | "result=estimator(system, estimators, experiment, logger)\n" |
---|
72 | " system = struct('class','datasource',...); % Estimated system\n" |
---|
73 | " estimators = {struct('class','estimator',...), % Estimators\n" |
---|
74 | " struct('class','estimator',...),...} \n" |
---|
75 | " === optional ===" |
---|
76 | " experiment = struct('ndat',10); % number of data in experiment, full length of finite datasources, 10 otherwise \n" |
---|
77 | " logger = struct('class','mexlogger'); % How to store results, default=mexlog, i.e. matlab structure\n\n" |
---|
78 | "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." ); |
---|
79 | |
---|
80 | RV::clear_all(); |
---|
81 | //CONFIG |
---|
82 | UImxArray Cfg; |
---|
83 | try { |
---|
84 | Cfg.addGroup ( input[0],"system" ); |
---|
85 | Cfg.addList ( input[1],"estimators" ); |
---|
86 | if ( n_input>2 ) { |
---|
87 | Cfg.addGroup ( input[2],"experiment" ); |
---|
88 | } |
---|
89 | if ( n_input>3 ) { |
---|
90 | Cfg.addGroup ( input[3],"logger" ); |
---|
91 | }/*else{ |
---|
92 | // define logger as mexlog |
---|
93 | Setting &S=Cfg.getRoot(); |
---|
94 | S.add("logger",Setting::TypeGroup); |
---|
95 | S["logger"].add("class",Setting::TypeString); |
---|
96 | S["logger"]["class"]="mexlog"; |
---|
97 | S["logger"].add("maxlen",Setting::TypeInt); |
---|
98 | int maxlen; |
---|
99 | S["experiment"].lookupValue("ndat",maxlen); |
---|
100 | S["logger"]["maxlen"]=maxlen; |
---|
101 | }*/ |
---|
102 | } catch ( SettingException e ) { |
---|
103 | it_error ( "error: "+string ( e.getPath() ) ); |
---|
104 | } |
---|
105 | |
---|
106 | //DBG |
---|
107 | Cfg.writeFile ( "estimator.cfg" ); |
---|
108 | |
---|
109 | #else |
---|
110 | int main ( int argc, char* argv[] ) { |
---|
111 | const char *fname; |
---|
112 | if ( argc>1 ) { |
---|
113 | fname = argv[1]; |
---|
114 | } else { |
---|
115 | fname="estimator.cfg"; |
---|
116 | } |
---|
117 | UIFile Cfg ( fname ); |
---|
118 | #endif |
---|
119 | |
---|
120 | shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" ); |
---|
121 | Array<shared_ptr<BM> > Es; |
---|
122 | UI::get ( Es,Cfg, "estimators" ); |
---|
123 | int Ndat=10; |
---|
124 | if ( Cfg.exists ( "experiment" ) ) { |
---|
125 | if ( UI::get(Ndat, Cfg.getRoot()["experiment"],"ndat" ) ) { |
---|
126 | bdm_assert ( Ndat<=Ds->max_length(), "Data source has less data then required" ); |
---|
127 | }; |
---|
128 | } else { |
---|
129 | if ( Ds->max_length() < std::numeric_limits< int >::max() ) { |
---|
130 | Ndat=Ds->max_length(); |
---|
131 | } |
---|
132 | ;// else Ndat=10; |
---|
133 | } |
---|
134 | shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional ); |
---|
135 | if ( !L ) { |
---|
136 | #ifdef MEX |
---|
137 | //mex logger has only from_setting constructor - we have to fill it... |
---|
138 | L=new mexlog ( Ndat ); |
---|
139 | #else |
---|
140 | L=new stdlog(); |
---|
141 | #endif |
---|
142 | } |
---|
143 | |
---|
144 | Ds->log_register ( *L, "DS" ); |
---|
145 | string Ename; |
---|
146 | Setting &S=Cfg; |
---|
147 | for ( int i=0; i<Es.length(); i++ ) { |
---|
148 | if (!UI::get ( Ename, S["estimators"][i], "name" ,UI::optional)){ |
---|
149 | Ename="Est"+num2str ( i ); |
---|
150 | } |
---|
151 | if (!Es(i)->posterior().log_level[epdf::logmean]){ |
---|
152 | const_cast<epdf&>(Es (i) ->posterior()).log_level[epdf::logmean] =true; |
---|
153 | } |
---|
154 | Es ( i )->log_register ( *L,Ename ); // estimate |
---|
155 | } |
---|
156 | L->init(); |
---|
157 | |
---|
158 | vec dt=zeros ( Ds->_drv()._dsize() ); //data variable |
---|
159 | Array<datalink*> Dls ( Es.length() ); |
---|
160 | Array<datalink*> Dlsc ( Es.length() ); |
---|
161 | Array<datalink_buffered*> Dls_buf (0); |
---|
162 | for ( int i=0; i<Es.length(); i++ ) { |
---|
163 | //connect actual data |
---|
164 | Dls ( i ) = new datalink ( Es ( i )->_yrv(),Ds->_drv() ); //datalink between a datasource and estimator |
---|
165 | //connect data in condition |
---|
166 | if (Es ( i )->_rvc().mint()<0){ |
---|
167 | //delayed values are required |
---|
168 | |
---|
169 | //create delayed dl |
---|
170 | int ith_buf=Dls_buf.size(); |
---|
171 | Dls_buf.set_size( ith_buf + 1, true); |
---|
172 | Dls_buf(ith_buf) = new datalink_buffered(); |
---|
173 | //add dl to list of buffered DS |
---|
174 | Dlsc(i) = Dls_buf(ith_buf); |
---|
175 | Dlsc(i)->set_connection ( Es ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator |
---|
176 | |
---|
177 | } else { |
---|
178 | Dlsc ( i ) = new datalink ( Es ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | for ( int tK=0;tK<Ndat;tK++ ) { |
---|
183 | Ds->getdata ( dt ); // read data |
---|
184 | Ds->log_write ( ); |
---|
185 | |
---|
186 | for ( int i=0; i<Es.length(); i++ ) { |
---|
187 | if (tK + Es ( i )->_rvc().mint() > 0 ) { |
---|
188 | Es ( i )->bayes ( Dls ( i )->pushdown ( dt ), Dlsc(i) ->pushdown(dt) ); // update estimates |
---|
189 | } |
---|
190 | Es ( i )->log_write (); |
---|
191 | } |
---|
192 | |
---|
193 | L->step(); |
---|
194 | Ds->step(); // simulator step |
---|
195 | //update buffered fdat links |
---|
196 | for (int i=0; i<Dls_buf.length(); i++){ |
---|
197 | Dls_buf(i)->store_data(dt); |
---|
198 | } |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | L->finalize(); |
---|
203 | // ------------------ End of routine ----------------------------- |
---|
204 | |
---|
205 | #ifdef MEX |
---|
206 | mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); |
---|
207 | |
---|
208 | if ( mL ) { // user wants output!! |
---|
209 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
---|
210 | output[0] = mL->toCell(); |
---|
211 | if (n_output>1) { |
---|
212 | mL->_setting_conf().setAutoConvert(true); |
---|
213 | output[1]= UImxArray::create_mxArray(mL->_setting_conf().getRoot()); |
---|
214 | } |
---|
215 | } |
---|
216 | #endif |
---|
217 | for (int i=0;i<Dls.length(); i++){delete Dls(i); delete Dlsc(i);} |
---|
218 | } |
---|