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

Revision 609, 5.2 kB (checked in by smidl, 15 years ago)

remove "delays" from memory DS, check length of datasource

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 "stat/emix.h"
52#include "base/datasources.h"
53#include "base/loggers.h"
54
55//#include "mex_datasource.h"
56
57using namespace bdm;
58
59#ifdef MEX
60#include <itpp/itmex.h>
61#include "mex/mex_BM.h"
62#include "mex/mex_logger.h"
63#include "mex/mex_datasource.h"
64
65void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) {
66        // Check the number of inputs and output arguments
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       
77        RV::clear_all();
78        //CONFIG
79        UImxArray Cfg;
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{
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;
98        }*/
99        } catch(SettingException e){it_error("error: "+string(e.getPath()));}
100
101        //DBG
102        Cfg.writeFile("estimator.cfg");
103
104#else
105int main ( int argc, char* argv[] ) {
106        const char *fname;
107        if ( argc>1 ) {
108                fname = argv[1];
109        } else {
110                fname="estimator.cfg";
111        }
112        UIFile Cfg ( fname );
113#endif
114       
115        shared_ptr<DS> Ds = UI::build<DS>( Cfg, "system" );
116        Array<shared_ptr<BM> > Es;      UI::get(Es,Cfg, "estimators" );
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       
137        Ds->log_add ( *L );
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        }
149                L->init();
150
151        vec dt=zeros ( Ds->_drv()._dsize() );   //data variable
152        Array<datalink*> Dls(Es.length()); 
153        for (int i=0; i<Es.length(); i++){
154                Dls(i)=new datalink( Es(i)->_drv(),Ds->_drv() ); //datalink between a datasource and estimator
155        }
156       
157        for ( int tK=0;tK<Ndat;tK++ ) {
158                Ds->getdata ( dt );                                     // read data
159                Ds->logit ( *L );
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                }
165                L->step();
166                Ds->step();                                                     // simulator step
167        }
168
169        L->finalize();
170        // ------------------ End of routine -----------------------------
171
172#ifdef MEX
173        mexlog* mL=dynamic_cast<mexlog*>(L.get());
174
175        if (mL) { // user wants output!!
176                if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" );
177                output[0] = mL->toCell();
178        }
179#endif
180}
Note: See TracBrowser for help on using the browser.