root/applications/bdmtoolbox/mex/estimator.cpp @ 568

Revision 568, 4.6 kB (checked in by smidl, 15 years ago)

bdmtoolbox adapted to shared_ptr, merger_grid (partially) working,

Line 
1/*!
2\file
3\brief Application Estimator
4
5The general task of estimation is defined on the following scheme:
6\dot
7digraph 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
16Here,
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
22Execute command:
23\code
24$> estimator config_file.cfg
25\endcode
26
27Full description of the experiment is in the file config_file.cfg which is expected to have the following structure:
28\code
29system = {type = "DS_offspring", ...};      // definition of a data source
30estimator = {type = "BM_offspring", ...};   // definition of an estimator
31logger = {type = "logger_type",...};        // definition of a logger
32experiment = {ndat = 11000; };              // definition of number of data records
33\endcode
34
35The 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
38Execute command:
39\code
40>> estimator('config_file.cfg');
41\endcode
42when using loggers storing results on hard drives, and
43\code
44>> Res=estimator('config_file.cfg');
45\endcode
46when using logger of the type \c "mex_logger". The results will be stored in structure \c M.
47
48 */
49
50#include "estim/arx.h"
51#include "base/datasources.h"
52#include "base/loggers.h"
53
54//#include "mex_datasource.h"
55
56using namespace bdm;
57
58#ifdef MEX
59#include <itpp/itmex.h>
60#include "mex/mex_logger.h"
61#include "mex/mex_parser.h"
62
63void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) {
64        // Check the number of inputs and output arguments
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        RV::clear_all();
76        //CONFIG
77        UImxArray Cfg;
78        try{
79        Cfg.addGroup(input[0],"system");
80        Cfg.addList(input[1],"estimators");
81        Cfg.addGroup(input[2],"experiment");
82        if (n_input>3){
83                Cfg.addGroup(input[3],"logger");
84        }else{
85                // define logger as mexlog
86                Setting &S=Cfg.getRoot();
87                S.add("logger",Setting::TypeGroup);
88                S["logger"].add("class",Setting::TypeString);
89                S["logger"]["class"]="mexlog";
90                S["logger"].add("maxlen",Setting::TypeInt);
91                int maxlen;
92                S["experiment"].lookupValue("ndat",maxlen);
93                S["logger"]["maxlen"]=maxlen;
94        }
95        } catch(SettingException e){it_error("error: "+string(e.getPath()));}
96
97        //DBG
98        Cfg.writeFile("estimator.cfg");
99
100#else
101int main ( int argc, char* argv[] ) {
102        const char *fname;
103        if ( argc>1 ) {
104                fname = argv[1];
105        } else {
106                fname="estimator.cfg";
107        }
108        UIFile Cfg ( fname );
109#endif
110       
111        shared_ptr<logger> L = UI::build<logger>( Cfg, "logger");
112        shared_ptr<ArxDS> DS = UI::build<ArxDS>( Cfg, "system" );
113        Array<shared_ptr<BM> > Es;      UI::get(Es,Cfg, "estimators" );
114        int Ndat;
115        Cfg.lookupValue ( "experiment.ndat",Ndat );
116
117        DS->log_add ( *L );
118        string Ename;
119        Setting &S=Cfg;
120        for (int i=0; i<Es.length(); i++){
121                try{
122                        UI::get(Ename, S["estimators"][i], "name");
123                } catch (UIException e){
124                        Ename="Est"+num2str(i);
125                }
126               
127                Es(i)->log_add(*L,Ename); // estimate
128        }
129        L->init();
130
131        vec dt=zeros ( DS->_drv()._dsize() );   //data variable
132        Array<datalink*> Dls(Es.length()); 
133        for (int i=0; i<Es.length(); i++){
134                Dls(i)=new datalink( Es(i)->_drv(),DS->_drv() ); //datalink between a datasource and estimator
135        }
136       
137        for ( int tK=1;tK<Ndat;tK++ ) {
138                DS->step();                                                     // simulator step
139                DS->getdata ( dt );                                     // read data
140                DS->logit ( *L );
141               
142                for (int i=0; i<Es.length(); i++){
143                        Es(i)->bayes ( Dls(i)->pushdown ( dt ) );               // update estimates
144                        Es(i)->logit (*L);
145                }
146                L->step();
147        }
148
149        L->finalize();
150        // ------------------ End of routine -----------------------------
151
152#ifdef MEX
153        mexlog* mL=dynamic_cast<mexlog*>(L.get());
154
155        if (mL) { // user wants output!!
156                if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" );
157                output[0] = mL->toCell();
158        }
159#endif
160}
Note: See TracBrowser for help on using the browser.