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