[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" |
---|
[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 |
---|
[609] | 67 | if(n_input<2) mexErrMsgTxt("Usage:\n" |
---|
[412] | 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 ===" |
---|
[609] | 73 | " experiment = struct('ndat',10); % number of data in experiment, full length of finite datasources, 10 otherwise \n" |
---|
[412] | 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(); |
---|
| 78 | //CONFIG |
---|
[412] | 79 | UImxArray Cfg; |
---|
| 80 | try{ |
---|
| 81 | Cfg.addGroup(input[0],"system"); |
---|
| 82 | Cfg.addList(input[1],"estimators"); |
---|
[609] | 83 | if (n_input>2){ |
---|
| 84 | Cfg.addGroup(input[2],"experiment"); |
---|
| 85 | } |
---|
[412] | 86 | if (n_input>3){ |
---|
| 87 | Cfg.addGroup(input[3],"logger"); |
---|
[609] | 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 | }*/ |
---|
[412] | 99 | } catch(SettingException e){it_error("error: "+string(e.getPath()));} |
---|
| 100 | |
---|
| 101 | //DBG |
---|
| 102 | Cfg.writeFile("estimator.cfg"); |
---|
| 103 | |
---|
| 104 | #else |
---|
| 105 | int main ( int argc, char* argv[] ) { |
---|
| 106 | const char *fname; |
---|
| 107 | if ( argc>1 ) { |
---|
| 108 | fname = argv[1]; |
---|
[293] | 109 | } else { |
---|
[412] | 110 | fname="estimator.cfg"; |
---|
[293] | 111 | } |
---|
[412] | 112 | UIFile Cfg ( fname ); |
---|
| 113 | #endif |
---|
[293] | 114 | |
---|
[593] | 115 | shared_ptr<DS> Ds = UI::build<DS>( Cfg, "system" ); |
---|
[568] | 116 | Array<shared_ptr<BM> > Es; UI::get(Es,Cfg, "estimators" ); |
---|
[609] | 117 | long Ndat=10; |
---|
| 118 | if (Cfg.exists("experiment")) { |
---|
| 119 | if (Cfg.lookupValue ( "experiment.ndat",Ndat )) { |
---|
| 120 | bdm_assert(Ndat<=Ds->max_length(), "Data source has less data then required"); |
---|
| 121 | }; |
---|
| 122 | } else { |
---|
| 123 | if (Ds->max_length() < inf) { |
---|
| 124 | Ndat=Ds->max_length(); |
---|
| 125 | };// else Ndat=10; |
---|
| 126 | } |
---|
| 127 | shared_ptr<logger> L = UI::build<logger>( Cfg, "logger",UI::optional); |
---|
| 128 | if (!L) { |
---|
| 129 | #ifdef MEX |
---|
| 130 | //mex logger has only from_setting constructor - we have to fill it... |
---|
| 131 | L=new mexlog(Ndat); |
---|
| 132 | #else |
---|
| 133 | L=new memlog(Ndat, "estimator_logger.it"); |
---|
| 134 | #endif |
---|
| 135 | } |
---|
| 136 | |
---|
[593] | 137 | Ds->log_add ( *L ); |
---|
[412] | 138 | string Ename; |
---|
| 139 | Setting &S=Cfg; |
---|
| 140 | for (int i=0; i<Es.length(); i++){ |
---|
| 141 | try{ |
---|
| 142 | UI::get(Ename, S["estimators"][i], "name"); |
---|
| 143 | } catch (UIException e){ |
---|
| 144 | Ename="Est"+num2str(i); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | Es(i)->log_add(*L,Ename); // estimate |
---|
| 148 | } |
---|
[593] | 149 | L->init(); |
---|
[293] | 150 | |
---|
[593] | 151 | vec dt=zeros ( Ds->_drv()._dsize() ); //data variable |
---|
[412] | 152 | Array<datalink*> Dls(Es.length()); |
---|
| 153 | for (int i=0; i<Es.length(); i++){ |
---|
[593] | 154 | Dls(i)=new datalink( Es(i)->_drv(),Ds->_drv() ); //datalink between a datasource and estimator |
---|
[412] | 155 | } |
---|
| 156 | |
---|
[609] | 157 | for ( int tK=0;tK<Ndat;tK++ ) { |
---|
[593] | 158 | Ds->getdata ( dt ); // read data |
---|
| 159 | Ds->logit ( *L ); |
---|
[412] | 160 | |
---|
| 161 | for (int i=0; i<Es.length(); i++){ |
---|
| 162 | Es(i)->bayes ( Dls(i)->pushdown ( dt ) ); // update estimates |
---|
| 163 | Es(i)->logit (*L); |
---|
| 164 | } |
---|
[293] | 165 | L->step(); |
---|
[609] | 166 | Ds->step(); // simulator step |
---|
[293] | 167 | } |
---|
| 168 | |
---|
| 169 | L->finalize(); |
---|
| 170 | // ------------------ End of routine ----------------------------- |
---|
| 171 | |
---|
[412] | 172 | #ifdef MEX |
---|
[568] | 173 | mexlog* mL=dynamic_cast<mexlog*>(L.get()); |
---|
[412] | 174 | |
---|
[293] | 175 | if (mL) { // user wants output!! |
---|
| 176 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
---|
| 177 | output[0] = mL->toCell(); |
---|
| 178 | } |
---|
[412] | 179 | #endif |
---|
[293] | 180 | } |
---|