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

Revision 800, 6.8 kB (checked in by smidl, 14 years ago)

new ARX model (allowing non-linear transformation)

  • Property svn:eol-style set to native
RevLine 
[412]1/*!
2\file
3\brief Application Estimator
[293]4
[412]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"];
[622]11        "Bayesian Model" -> "Result Logger" [label="estimated\n statistics"];
[412]12        "Data Source" -> "Result Logger" [label="Simulated\n data"];
13}
14\enddot
15
[622]16Here,
[412]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
[800]50#include <estim/arx_ext.h>
[700]51#include <stat/emix.h>
52#include <base/datasources.h>
53#include <base/loggers.h>
54#include <estim/particles.h>
55#include <estim/kalman.h>
[293]56
[384]57//#include "mex_datasource.h"
[373]58
[293]59using namespace bdm;
60
[412]61#ifdef MEX
62#include <itpp/itmex.h>
[700]63#include <mex/mex_BM.h>
64#include <mex/mex_logger.h>
65#include <mex/mex_datasource.h>
[797]66#include <mex/mex_function.h>
[412]67
[293]68void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) {
69        // Check the number of inputs and output arguments
[622]70        if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n"
71                                                "result=estimator(system, estimators, experiment, logger)\n"
72                                                "  system     = struct('class','datasource',...);  % Estimated system\n"
73                                                "  estimators = {struct('class','estimator',...),  % Estimators\n"
74                                                "                struct('class','estimator',...),...} \n"
75                                                "  === optional ==="
76                                                "  experiment = struct('ndat',10);                 % number of data in experiment, full length of finite datasources, 10 otherwise \n"
77                                                "  logger     = struct('class','mexlogger');       % How to store results, default=mexlog, i.e. matlab structure\n\n"
78                                                "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." );
79
[490]80        RV::clear_all();
[622]81        //CONFIG
[412]82        UImxArray Cfg;
[622]83        try {
84                Cfg.addGroup ( input[0],"system" );
85                Cfg.addList ( input[1],"estimators" );
86                if ( n_input>2 ) {
87                        Cfg.addGroup ( input[2],"experiment" );
88                }
89                if ( n_input>3 ) {
90                        Cfg.addGroup ( input[3],"logger" );
91                }/*else{
[412]92                // define logger as mexlog
93                Setting &S=Cfg.getRoot();
94                S.add("logger",Setting::TypeGroup);
95                S["logger"].add("class",Setting::TypeString);
96                S["logger"]["class"]="mexlog";
97                S["logger"].add("maxlen",Setting::TypeInt);
98                int maxlen;
99                S["experiment"].lookupValue("ndat",maxlen);
100                S["logger"]["maxlen"]=maxlen;
[609]101        }*/
[622]102        } catch ( SettingException e ) {
103                it_error ( "error: "+string ( e.getPath() ) );
104        }
[412]105
106        //DBG
[622]107        Cfg.writeFile ( "estimator.cfg" );
[412]108
109#else
110int main ( int argc, char* argv[] ) {
111        const char *fname;
112        if ( argc>1 ) {
113                fname = argv[1];
[293]114        } else {
[412]115                fname="estimator.cfg";
[293]116        }
[412]117        UIFile Cfg ( fname );
118#endif
[622]119
120        shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" );
121        Array<shared_ptr<BM> > Es;
122        UI::get ( Es,Cfg, "estimators" );
[700]123        int Ndat=10;
[622]124        if ( Cfg.exists ( "experiment" ) ) {
[760]125                if (  UI::get(Ndat, Cfg.getRoot()["experiment"],"ndat" ) ) {
[622]126                        bdm_assert ( Ndat<=Ds->max_length(), "Data source has less data then required" );
[609]127                };
128        } else {
[626]129                if ( Ds->max_length() < std::numeric_limits< int >::max() ) {
[609]130                        Ndat=Ds->max_length();
[622]131                }
132                ;// else Ndat=10;
[609]133        }
[622]134        shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional );
135        if ( !L ) {
136#ifdef MEX
[609]137                //mex logger has only from_setting constructor - we  have to fill it...
[622]138                L=new mexlog ( Ndat );
139#else
[611]140                L=new stdlog();
[622]141#endif
[609]142        }
[622]143
[676]144        Ds->log_register ( *L, "DS" );
[412]145        string Ename;
146        Setting &S=Cfg;
[622]147        for ( int i=0; i<Es.length(); i++ ) {
[685]148                if (!UI::get ( Ename, S["estimators"][i], "name" ,UI::optional)){
[622]149                        Ename="Est"+num2str ( i );
[412]150                }
[701]151                if (Es(i)->posterior()._log_level()<1){
152                        const_cast<epdf&>(Es (i) ->posterior()).set_log_level (1);
153                }
154               
[676]155                Es ( i )->log_register ( *L,Ename ); // estimate
[412]156        }
[622]157        L->init();
[293]158
[593]159        vec dt=zeros ( Ds->_drv()._dsize() );   //data variable
[622]160        Array<datalink*> Dls ( Es.length() );
[685]161        Array<datalink*> Dlsc ( Es.length() );
[626]162        Array<datalink_buffered*> Dls_buf (0);
[622]163        for ( int i=0; i<Es.length(); i++ ) {
[685]164                //connect actual data
165                Dls ( i ) = new datalink ( Es ( i )->_yrv(),Ds->_drv() ); //datalink between a datasource and estimator
166                //connect data in condition
167                if (Es ( i )->_rvc().mint()<0){ 
[631]168                        //delayed values are required
169                       
170                        //create delayed dl
[626]171                        int ith_buf=Dls_buf.size();
[631]172                        Dls_buf.set_size( ith_buf + 1, true);
[626]173                        Dls_buf(ith_buf) = new datalink_buffered(); 
[631]174                        //add dl to list of buffered DS
[685]175                        Dlsc(i) = Dls_buf(ith_buf);
176                        Dlsc(i)->set_connection ( Es ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator
[626]177                       
178                } else {
[685]179                        Dlsc ( i ) = new datalink ( Es ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator
[626]180                }
[412]181        }
[622]182
[609]183        for ( int tK=0;tK<Ndat;tK++ ) {
[593]184                Ds->getdata ( dt );                                     // read data
[676]185                Ds->log_write ( );
[622]186
187                for ( int i=0; i<Es.length(); i++ ) {
[701]188                        if (tK + Es ( i )->_rvc().mint() > 0 ) {
189                                Es ( i )->bayes ( Dls ( i )->pushdown ( dt ), Dlsc(i) ->pushdown(dt) );         // update estimates
190                        }
[676]191                        Es ( i )->log_write ();
[412]192                }
[626]193               
[293]194                L->step();
[609]195                Ds->step();                                                     // simulator step
[626]196                //update buffered fdat links
197                for (int i=0; i<Dls_buf.length(); i++){
[723]198                        Dls_buf(i)->store_data(dt);
[626]199                }
200                       
[293]201        }
202
203        L->finalize();
204        // ------------------ End of routine -----------------------------
205
[412]206#ifdef MEX
[622]207        mexlog* mL=dynamic_cast<mexlog*> ( L.get() );
[412]208
[622]209        if ( mL ) { // user wants output!!
[293]210                if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" );
211                output[0] = mL->toCell();
[760]212                if (n_output>1) {
213                        mL->_setting_conf().setAutoConvert(true);
[756]214                        output[1]= UImxArray::create_mxArray(mL->_setting_conf().getRoot());
[760]215                }
[293]216        }
[412]217#endif
[685]218        for (int i=0;i<Dls.length(); i++){delete Dls(i); delete Dlsc(i);}
[293]219}
Note: See TracBrowser for help on using the browser.