| [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 |  | 
|---|
| [373] | 50 | #include "estim/arx.h" | 
|---|
| [602] | 51 | #include "stat/emix.h" | 
|---|
| [412] | 52 | #include "base/datasources.h" | 
|---|
|  | 53 | #include "base/loggers.h" | 
|---|
| [293] | 54 |  | 
|---|
| [384] | 55 | //#include "mex_datasource.h" | 
|---|
| [373] | 56 |  | 
|---|
| [293] | 57 | using namespace bdm; | 
|---|
|  | 58 |  | 
|---|
| [412] | 59 | #ifdef MEX | 
|---|
|  | 60 | #include <itpp/itmex.h> | 
|---|
| [593] | 61 | #include "mex/mex_BM.h" | 
|---|
| [412] | 62 | #include "mex/mex_logger.h" | 
|---|
| [593] | 63 | #include "mex/mex_datasource.h" | 
|---|
| [412] | 64 |  | 
|---|
| [293] | 65 | void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { | 
|---|
|  | 66 | // Check the number of inputs and output arguments | 
|---|
| [622] | 67 | if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n" | 
|---|
|  | 68 | "result=estimator(system, estimators, experiment, logger)\n" | 
|---|
|  | 69 | "  system     = struct('class','datasource',...);  % Estimated system\n" | 
|---|
|  | 70 | "  estimators = {struct('class','estimator',...),  % Estimators\n" | 
|---|
|  | 71 | "                struct('class','estimator',...),...} \n" | 
|---|
|  | 72 | "  === optional ===" | 
|---|
|  | 73 | "  experiment = struct('ndat',10);                 % number of data in experiment, full length of finite datasources, 10 otherwise \n" | 
|---|
|  | 74 | "  logger     = struct('class','mexlogger');       % How to store results, default=mexlog, i.e. matlab structure\n\n" | 
|---|
|  | 75 | "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." ); | 
|---|
|  | 76 |  | 
|---|
| [490] | 77 | RV::clear_all(); | 
|---|
| [622] | 78 | //CONFIG | 
|---|
| [412] | 79 | UImxArray Cfg; | 
|---|
| [622] | 80 | try { | 
|---|
|  | 81 | Cfg.addGroup ( input[0],"system" ); | 
|---|
|  | 82 | Cfg.addList ( input[1],"estimators" ); | 
|---|
|  | 83 | if ( n_input>2 ) { | 
|---|
|  | 84 | Cfg.addGroup ( input[2],"experiment" ); | 
|---|
|  | 85 | } | 
|---|
|  | 86 | if ( n_input>3 ) { | 
|---|
|  | 87 | Cfg.addGroup ( input[3],"logger" ); | 
|---|
|  | 88 | }/*else{ | 
|---|
| [412] | 89 | // define logger as mexlog | 
|---|
|  | 90 | Setting &S=Cfg.getRoot(); | 
|---|
|  | 91 | S.add("logger",Setting::TypeGroup); | 
|---|
|  | 92 | S["logger"].add("class",Setting::TypeString); | 
|---|
|  | 93 | S["logger"]["class"]="mexlog"; | 
|---|
|  | 94 | S["logger"].add("maxlen",Setting::TypeInt); | 
|---|
|  | 95 | int maxlen; | 
|---|
|  | 96 | S["experiment"].lookupValue("ndat",maxlen); | 
|---|
|  | 97 | S["logger"]["maxlen"]=maxlen; | 
|---|
| [609] | 98 | }*/ | 
|---|
| [622] | 99 | } catch ( SettingException e ) { | 
|---|
|  | 100 | it_error ( "error: "+string ( e.getPath() ) ); | 
|---|
|  | 101 | } | 
|---|
| [412] | 102 |  | 
|---|
|  | 103 | //DBG | 
|---|
| [622] | 104 | Cfg.writeFile ( "estimator.cfg" ); | 
|---|
| [412] | 105 |  | 
|---|
|  | 106 | #else | 
|---|
|  | 107 | int main ( int argc, char* argv[] ) { | 
|---|
|  | 108 | const char *fname; | 
|---|
|  | 109 | if ( argc>1 ) { | 
|---|
|  | 110 | fname = argv[1]; | 
|---|
| [293] | 111 | } else { | 
|---|
| [412] | 112 | fname="estimator.cfg"; | 
|---|
| [293] | 113 | } | 
|---|
| [412] | 114 | UIFile Cfg ( fname ); | 
|---|
|  | 115 | #endif | 
|---|
| [622] | 116 |  | 
|---|
|  | 117 | shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" ); | 
|---|
|  | 118 | Array<shared_ptr<BM> > Es; | 
|---|
|  | 119 | UI::get ( Es,Cfg, "estimators" ); | 
|---|
| [609] | 120 | long Ndat=10; | 
|---|
| [622] | 121 | if ( Cfg.exists ( "experiment" ) ) { | 
|---|
|  | 122 | if ( Cfg.lookupValue ( "experiment.ndat",Ndat ) ) { | 
|---|
|  | 123 | bdm_assert ( Ndat<=Ds->max_length(), "Data source has less data then required" ); | 
|---|
| [609] | 124 | }; | 
|---|
|  | 125 | } else { | 
|---|
| [622] | 126 | if ( Ds->max_length() < inf ) { | 
|---|
| [609] | 127 | Ndat=Ds->max_length(); | 
|---|
| [622] | 128 | } | 
|---|
|  | 129 | ;// else Ndat=10; | 
|---|
| [609] | 130 | } | 
|---|
| [622] | 131 | shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional ); | 
|---|
|  | 132 | if ( !L ) { | 
|---|
|  | 133 | #ifdef MEX | 
|---|
| [609] | 134 | //mex logger has only from_setting constructor - we  have to fill it... | 
|---|
| [622] | 135 | L=new mexlog ( Ndat ); | 
|---|
|  | 136 | #else | 
|---|
| [611] | 137 | L=new stdlog(); | 
|---|
| [622] | 138 | #endif | 
|---|
| [609] | 139 | } | 
|---|
| [622] | 140 |  | 
|---|
| [593] | 141 | Ds->log_add ( *L ); | 
|---|
| [412] | 142 | string Ename; | 
|---|
|  | 143 | Setting &S=Cfg; | 
|---|
| [622] | 144 | for ( int i=0; i<Es.length(); i++ ) { | 
|---|
|  | 145 | try { | 
|---|
|  | 146 | UI::get ( Ename, S["estimators"][i], "name" ); | 
|---|
|  | 147 | } catch ( UIException e ) { | 
|---|
|  | 148 | Ename="Est"+num2str ( i ); | 
|---|
| [412] | 149 | } | 
|---|
| [622] | 150 |  | 
|---|
|  | 151 | Es ( i )->log_add ( *L,Ename ); // estimate | 
|---|
| [412] | 152 | } | 
|---|
| [622] | 153 | L->init(); | 
|---|
| [293] | 154 |  | 
|---|
| [593] | 155 | vec dt=zeros ( Ds->_drv()._dsize() );   //data variable | 
|---|
| [622] | 156 | Array<datalink*> Dls ( Es.length() ); | 
|---|
|  | 157 | for ( int i=0; i<Es.length(); i++ ) { | 
|---|
|  | 158 | Dls ( i ) =new datalink ( Es ( i )->_drv(),Ds->_drv() ); //datalink between a datasource and estimator | 
|---|
| [412] | 159 | } | 
|---|
| [622] | 160 |  | 
|---|
| [609] | 161 | for ( int tK=0;tK<Ndat;tK++ ) { | 
|---|
| [593] | 162 | Ds->getdata ( dt );                                     // read data | 
|---|
|  | 163 | Ds->logit ( *L ); | 
|---|
| [622] | 164 |  | 
|---|
|  | 165 | for ( int i=0; i<Es.length(); i++ ) { | 
|---|
|  | 166 | Es ( i )->bayes ( Dls ( i )->pushdown ( dt ) );         // update estimates | 
|---|
|  | 167 | Es ( i )->logit ( *L ); | 
|---|
| [412] | 168 | } | 
|---|
| [293] | 169 | L->step(); | 
|---|
| [609] | 170 | Ds->step();                                                     // simulator step | 
|---|
| [293] | 171 | } | 
|---|
|  | 172 |  | 
|---|
|  | 173 | L->finalize(); | 
|---|
|  | 174 | // ------------------ End of routine ----------------------------- | 
|---|
|  | 175 |  | 
|---|
| [412] | 176 | #ifdef MEX | 
|---|
| [622] | 177 | mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); | 
|---|
| [412] | 178 |  | 
|---|
| [622] | 179 | if ( mL ) { // user wants output!! | 
|---|
| [293] | 180 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); | 
|---|
|  | 181 | output[0] = mL->toCell(); | 
|---|
|  | 182 | } | 
|---|
| [412] | 183 | #endif | 
|---|
| [293] | 184 | } | 
|---|