[412] | 1 | /*! |
---|
| 2 | \file |
---|
| 3 | \brief Application Estimator |
---|
[293] | 4 | |
---|
[412] | 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"]; |
---|
[622] | 11 | "Bayesian Model" -> "Result Logger" [label="estimated\n statistics"]; |
---|
[412] | 12 | "Data Source" -> "Result Logger" [label="Simulated\n data"]; |
---|
| 13 | } |
---|
| 14 | \enddot |
---|
| 15 | |
---|
[622] | 16 | Here, |
---|
[412] | 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 | |
---|
[800] | 50 | #include <estim/arx_ext.h> |
---|
[700] | 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> |
---|
[293] | 56 | |
---|
[384] | 57 | //#include "mex_datasource.h" |
---|
[373] | 58 | |
---|
[293] | 59 | using namespace bdm; |
---|
| 60 | |
---|
[412] | 61 | #ifdef MEX |
---|
| 62 | #include <itpp/itmex.h> |
---|
[700] | 63 | #include <mex/mex_BM.h> |
---|
| 64 | #include <mex/mex_logger.h> |
---|
| 65 | #include <mex/mex_datasource.h> |
---|
[797] | 66 | #include <mex/mex_function.h> |
---|
[412] | 67 | |
---|
[293] | 68 | void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { |
---|
| 69 | // Check the number of inputs and output arguments |
---|
[622] | 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 | |
---|
[490] | 80 | RV::clear_all(); |
---|
[622] | 81 | //CONFIG |
---|
[412] | 82 | UImxArray Cfg; |
---|
[622] | 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" ); |
---|
[944] | 91 | } |
---|
[622] | 92 | } catch ( SettingException e ) { |
---|
| 93 | it_error ( "error: "+string ( e.getPath() ) ); |
---|
| 94 | } |
---|
[412] | 95 | |
---|
| 96 | //DBG |
---|
[811] | 97 | Cfg.writeFile ( "estimator.cfg" ); |
---|
[412] | 98 | |
---|
| 99 | #else |
---|
| 100 | int main ( int argc, char* argv[] ) { |
---|
| 101 | const char *fname; |
---|
| 102 | if ( argc>1 ) { |
---|
| 103 | fname = argv[1]; |
---|
[293] | 104 | } else { |
---|
[412] | 105 | fname="estimator.cfg"; |
---|
[293] | 106 | } |
---|
[412] | 107 | UIFile Cfg ( fname ); |
---|
| 108 | #endif |
---|
[622] | 109 | |
---|
| 110 | shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" ); |
---|
| 111 | Array<shared_ptr<BM> > Es; |
---|
| 112 | UI::get ( Es,Cfg, "estimators" ); |
---|
[700] | 113 | int Ndat=10; |
---|
[622] | 114 | if ( Cfg.exists ( "experiment" ) ) { |
---|
[760] | 115 | if ( UI::get(Ndat, Cfg.getRoot()["experiment"],"ndat" ) ) { |
---|
[622] | 116 | bdm_assert ( Ndat<=Ds->max_length(), "Data source has less data then required" ); |
---|
[609] | 117 | }; |
---|
| 118 | } else { |
---|
[626] | 119 | if ( Ds->max_length() < std::numeric_limits< int >::max() ) { |
---|
[609] | 120 | Ndat=Ds->max_length(); |
---|
[622] | 121 | } |
---|
| 122 | ;// else Ndat=10; |
---|
[609] | 123 | } |
---|
[622] | 124 | shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional ); |
---|
| 125 | if ( !L ) { |
---|
| 126 | #ifdef MEX |
---|
[609] | 127 | //mex logger has only from_setting constructor - we have to fill it... |
---|
[622] | 128 | L=new mexlog ( Ndat ); |
---|
| 129 | #else |
---|
[611] | 130 | L=new stdlog(); |
---|
[622] | 131 | #endif |
---|
[609] | 132 | } |
---|
[622] | 133 | |
---|
[676] | 134 | Ds->log_register ( *L, "DS" ); |
---|
[412] | 135 | string Ename; |
---|
| 136 | Setting &S=Cfg; |
---|
[622] | 137 | for ( int i=0; i<Es.length(); i++ ) { |
---|
[685] | 138 | if (!UI::get ( Ename, S["estimators"][i], "name" ,UI::optional)){ |
---|
[622] | 139 | Ename="Est"+num2str ( i ); |
---|
[412] | 140 | } |
---|
[863] | 141 | if (!Es(i)->posterior().log_level[epdf::logmean]){ |
---|
| 142 | const_cast<epdf&>(Es (i) ->posterior()).log_level[epdf::logmean] =true; |
---|
| 143 | } |
---|
[676] | 144 | Es ( i )->log_register ( *L,Ename ); // estimate |
---|
[412] | 145 | } |
---|
[622] | 146 | L->init(); |
---|
[293] | 147 | |
---|
[593] | 148 | vec dt=zeros ( Ds->_drv()._dsize() ); //data variable |
---|
[622] | 149 | Array<datalink*> Dls ( Es.length() ); |
---|
[685] | 150 | Array<datalink*> Dlsc ( Es.length() ); |
---|
[626] | 151 | Array<datalink_buffered*> Dls_buf (0); |
---|
[622] | 152 | for ( int i=0; i<Es.length(); i++ ) { |
---|
[966] | 153 | //check if they are properly named |
---|
| 154 | bdm_assert(Es(i)->_yrv()._dsize() == Es(i)->dimensiony(), "Estimator["+num2str(i)+"] does not name yrv properly." |
---|
| 155 | "size(yrv)="+num2str(Es(i)->_yrv()._dsize() ) + ", declared dimension of y="+num2str(Es(i)->dimensiony())); |
---|
| 156 | bdm_assert(Es(i)->_rvc()._dsize() == Es(i)->dimensionc(), "Estimator["+num2str(i)+"] does not name rvc properly." |
---|
| 157 | "size(rvc)="+num2str(Es(i)->_rvc()._dsize() ) + ", declared dimension of rvc="+num2str(Es(i)->dimensionc())); |
---|
[685] | 158 | //connect actual data |
---|
| 159 | Dls ( i ) = new datalink ( Es ( i )->_yrv(),Ds->_drv() ); //datalink between a datasource and estimator |
---|
| 160 | //connect data in condition |
---|
| 161 | if (Es ( i )->_rvc().mint()<0){ |
---|
[631] | 162 | //delayed values are required |
---|
| 163 | |
---|
| 164 | //create delayed dl |
---|
[626] | 165 | int ith_buf=Dls_buf.size(); |
---|
[631] | 166 | Dls_buf.set_size( ith_buf + 1, true); |
---|
[626] | 167 | Dls_buf(ith_buf) = new datalink_buffered(); |
---|
[631] | 168 | //add dl to list of buffered DS |
---|
[685] | 169 | Dlsc(i) = Dls_buf(ith_buf); |
---|
| 170 | Dlsc(i)->set_connection ( Es ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator |
---|
[626] | 171 | |
---|
[966] | 172 | bdm_assert_debug(Dlsc(i)->_downsize() == Es ( i )->_rvc()._dsize(), "Data required by Est[" + num2str(i) + "], " + |
---|
| 173 | Es(i)->_rvc().to_string() + ", are not available in DS drv:" + Ds->_drv().to_string();); |
---|
[626] | 174 | } else { |
---|
[685] | 175 | Dlsc ( i ) = new datalink ( Es ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator |
---|
[626] | 176 | } |
---|
[412] | 177 | } |
---|
[622] | 178 | |
---|
[609] | 179 | for ( int tK=0;tK<Ndat;tK++ ) { |
---|
[593] | 180 | Ds->getdata ( dt ); // read data |
---|
[676] | 181 | Ds->log_write ( ); |
---|
[622] | 182 | |
---|
| 183 | for ( int i=0; i<Es.length(); i++ ) { |
---|
[701] | 184 | if (tK + Es ( i )->_rvc().mint() > 0 ) { |
---|
| 185 | Es ( i )->bayes ( Dls ( i )->pushdown ( dt ), Dlsc(i) ->pushdown(dt) ); // update estimates |
---|
| 186 | } |
---|
[676] | 187 | Es ( i )->log_write (); |
---|
[412] | 188 | } |
---|
[626] | 189 | |
---|
[293] | 190 | L->step(); |
---|
[609] | 191 | Ds->step(); // simulator step |
---|
[626] | 192 | //update buffered fdat links |
---|
| 193 | for (int i=0; i<Dls_buf.length(); i++){ |
---|
[723] | 194 | Dls_buf(i)->store_data(dt); |
---|
[626] | 195 | } |
---|
| 196 | |
---|
[293] | 197 | } |
---|
| 198 | |
---|
| 199 | L->finalize(); |
---|
| 200 | // ------------------ End of routine ----------------------------- |
---|
| 201 | |
---|
[412] | 202 | #ifdef MEX |
---|
[622] | 203 | mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); |
---|
[412] | 204 | |
---|
[622] | 205 | if ( mL ) { // user wants output!! |
---|
[293] | 206 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
---|
| 207 | output[0] = mL->toCell(); |
---|
[760] | 208 | if (n_output>1) { |
---|
| 209 | mL->_setting_conf().setAutoConvert(true); |
---|
[756] | 210 | output[1]= UImxArray::create_mxArray(mL->_setting_conf().getRoot()); |
---|
[760] | 211 | } |
---|
[293] | 212 | } |
---|
[412] | 213 | #endif |
---|
[685] | 214 | for (int i=0;i<Dls.length(); i++){delete Dls(i); delete Dlsc(i);} |
---|
[293] | 215 | } |
---|