| 1 |  | #include <itpp/itmex.h> | 
                      
                        |  | 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 | */ | 
            
                      
                        | 14 |  | string fname; | 
                        | 15 |  | if (n_input>0){ | 
                        | 16 |  | fname=mxArray2string(input[0]); | 
                      
                        |  | 65 | if(n_input<3) mexErrMsgTxt("Usage:\n" | 
                        |  | 66 | "result=estimator(system, estimators, experiment, logger)\n" | 
                        |  | 67 | "  system     = struct('class','datasource',...);  % Estimated system\n" | 
                        |  | 68 | "  estimators = {struct('class','estimator',...),  % Estimators\n" | 
                        |  | 69 | "                struct('class','estimator',...),...} \n" | 
                        |  | 70 | "  experiment = struct('ndat',100);                % number of data in experiment \n" | 
                        |  | 71 | "  === optional ===" | 
                        |  | 72 | "  logger     = struct('class','mexlogger');       % How to store results, default=mexlog, i.e. matlab structure\n\n" | 
                        |  | 73 | "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM."); | 
                        |  | 74 |  | 
                        |  | 75 | UImxArray Cfg; | 
                        |  | 76 | try{ | 
                        |  | 77 | Cfg.addGroup(input[0],"system"); | 
                        |  | 78 | Cfg.addList(input[1],"estimators"); | 
                        |  | 79 | Cfg.addGroup(input[2],"experiment"); | 
                        |  | 80 | if (n_input>3){ | 
                        |  | 81 | Cfg.addGroup(input[3],"logger"); | 
                        |  | 82 | }else{ | 
                        |  | 83 | // define logger as mexlog | 
                        |  | 84 | Setting &S=Cfg.getRoot(); | 
                        |  | 85 | S.add("logger",Setting::TypeGroup); | 
                        |  | 86 | S["logger"].add("class",Setting::TypeString); | 
                        |  | 87 | S["logger"]["class"]="mexlog"; | 
                        |  | 88 | S["logger"].add("maxlen",Setting::TypeInt); | 
                        |  | 89 | int maxlen; | 
                        |  | 90 | S["experiment"].lookupValue("ndat",maxlen); | 
                        |  | 91 | S["logger"]["maxlen"]=maxlen; | 
                        |  | 92 | } | 
                        |  | 93 | } catch(SettingException e){it_error("error: "+string(e.getPath()));} | 
                        |  | 94 |  | 
                        |  | 95 | //DBG | 
                        |  | 96 | Cfg.writeFile("estimator.cfg"); | 
                        |  | 97 |  | 
                        |  | 98 | #else | 
                        |  | 99 | int main ( int argc, char* argv[] ) { | 
                        |  | 100 | const char *fname; | 
                        |  | 101 | if ( argc>1 ) { | 
                        |  | 102 | fname = argv[1]; | 
            
                      
                        | 22 |  | printf("name: %s", fname.c_str()); | 
                        | 23 |  | UIFile F ( fname.c_str()); | 
                        | 24 |  |  | 
                        | 25 |  | logger* L = UI::build<logger>( F, "logger"); | 
                        | 26 |  | ArxDS * DS = UI::build<ArxDS>( F, "system" ); | 
                        | 27 |  | BM* E = UI::build<BM>( F, "estimator" ); | 
                        | 28 |  | int Ndat = F.lookupValue ( "experiment.ndat",Ndat ); | 
                      
                        |  | 109 | logger* L = UI::build<logger>( Cfg, "logger"); | 
                        |  | 110 | ArxDS * DS = UI::build<ArxDS>( Cfg, "system" ); | 
                        |  | 111 | Array<BM*> Es;  UI::get(Es,Cfg, "estimators" ); | 
                        |  | 112 | int Ndat; | 
                        |  | 113 | Cfg.lookupValue ( "experiment.ndat",Ndat ); | 
            
                      
                        | 31 |  | int L_est= L->add ( E->posterior()._rv(), "est" ); // estimate | 
                        | 32 |  | int L_lb = L->add ( E->posterior()._rv(), "lb" ); // lower bound | 
                        | 33 |  | int L_ub = L->add ( E->posterior()._rv(), "ub" ); // upper bound | 
                      
                        |  | 116 | string Ename; | 
                        |  | 117 | Setting &S=Cfg; | 
                        |  | 118 | for (int i=0; i<Es.length(); i++){ | 
                        |  | 119 | try{ | 
                        |  | 120 | UI::get(Ename, S["estimators"][i], "name"); | 
                        |  | 121 | } catch (UIException e){ | 
                        |  | 122 | Ename="Est"+num2str(i); | 
                        |  | 123 | } | 
                        |  | 124 |  | 
                        |  | 125 | Es(i)->log_add(*L,Ename); // estimate | 
                        |  | 126 | } |