1 | #include <estim/arx.h> |
---|
2 | #include <itpp/itmex.h> |
---|
3 | #include "mex_parser.h" |
---|
4 | |
---|
5 | namespace bdm { |
---|
6 | /*! |
---|
7 | * \brief Wrapper of BM mapping BM's methods to matlab functions |
---|
8 | |
---|
9 | The data are stored in an internal matrix \c Data . Each column of Data corresponds to one discrete time observation \f$t\f$. Access to this matrix is via indices \c rowid and \c delays. |
---|
10 | |
---|
11 | The data can be loaded from a file. |
---|
12 | */ |
---|
13 | |
---|
14 | using namespace bdm; |
---|
15 | |
---|
16 | //! epdf with functions implemented in matlab |
---|
17 | class mexEpdf: public epdf { |
---|
18 | protected: |
---|
19 | //! prefix of matlab functions |
---|
20 | string name; |
---|
21 | //! pointer to storage structure |
---|
22 | mxArray *data; |
---|
23 | public: |
---|
24 | mexEpdf() {}; |
---|
25 | void from_setting ( const Setting &S ) { |
---|
26 | UIFile conf( ( const char * ) S["name"] ); |
---|
27 | data = UImxArray::create_mxArray( conf ); |
---|
28 | //mexCallMATLAB(1, &data, 0, 0, (name+"_new").c_str()); |
---|
29 | //TODO (future...): |
---|
30 | //mxArray * init_data = setting2mxarray S["init_data"]; |
---|
31 | //mexCallMATLAB(1, &data, 1, &init_data, name+"_from_setting"); |
---|
32 | //delete init_data; |
---|
33 | } |
---|
34 | vec mean() const { |
---|
35 | mxArray *tmp; |
---|
36 | string fname = name + "_mean"; |
---|
37 | mexCallMATLAB ( 1, &tmp, 1, ( mxArray ** ) &data, fname.c_str() ); |
---|
38 | return mxArray2vec ( tmp ); |
---|
39 | } |
---|
40 | |
---|
41 | virtual vec sample() const NOT_IMPLEMENTED(0); |
---|
42 | |
---|
43 | virtual double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); |
---|
44 | |
---|
45 | virtual vec variance() const NOT_IMPLEMENTED(0); |
---|
46 | }; |
---|
47 | UIREGISTER ( mexEpdf ); |
---|
48 | |
---|
49 | //! BM with functions implemented in matlab |
---|
50 | class mexBM: public BM { |
---|
51 | protected : |
---|
52 | //! prefix of matlab functions |
---|
53 | string name; |
---|
54 | //! internal estimator |
---|
55 | mexEpdf est; |
---|
56 | //! mxArray with attributes of this object |
---|
57 | mxArray *data; |
---|
58 | public: |
---|
59 | mexBM() {} |
---|
60 | |
---|
61 | //! duplicate internal data pointer? |
---|
62 | mxArray *get_data() { |
---|
63 | //mexCallMATLAB(0, NULL, 1, &data, "dump"); |
---|
64 | return mxDuplicateArray ( data ); |
---|
65 | } |
---|
66 | |
---|
67 | void from_setting ( const Setting &S ) { |
---|
68 | BM::from_setting ( S ); |
---|
69 | |
---|
70 | UIFile conf( ( const char * ) S["name"] ); |
---|
71 | data = UImxArray::create_mxArray( conf ); |
---|
72 | |
---|
73 | //string fname = name+"_new"; |
---|
74 | //mexCallMATLAB(1, &data, 0, 0, (name+"_new").c_str()); |
---|
75 | //the following works as long as the posterior is the |
---|
76 | //only member object there could be a structure of |
---|
77 | //member objects in the setting and a for cycle over |
---|
78 | //all of them right here in the code |
---|
79 | Setting &posterior = S["posterior"]; |
---|
80 | est.from_setting ( posterior ); |
---|
81 | } |
---|
82 | void bayes ( const vec &yt, const vec &cond ) { |
---|
83 | //void bayes() { |
---|
84 | mxArray *tmp, *old; |
---|
85 | mxArray *in[2]; |
---|
86 | in[0] = data; |
---|
87 | in[1] = mxCreateDoubleMatrix ( yt.size(), 1, mxREAL ); |
---|
88 | vec2mxArray ( yt, in[1] ); |
---|
89 | mexCallMATLAB ( 1, &tmp, 2, in, ( name + "_bayes" ).c_str() ); |
---|
90 | old = data; |
---|
91 | data = mxDuplicateArray ( tmp ); |
---|
92 | if ( old ) mxDestroyArray ( old ); |
---|
93 | if ( tmp ) mxDestroyArray ( tmp ); |
---|
94 | //mexCallMATLAB(0, NULL, 1, &data, "dump"); |
---|
95 | } |
---|
96 | //! return correctly typed posterior (covariant return) |
---|
97 | const mexEpdf& posterior() const { |
---|
98 | return est; |
---|
99 | } //tohle by melo zustat!! |
---|
100 | |
---|
101 | virtual double logpred ( const vec &yt ) const NOT_IMPLEMENTED(0); |
---|
102 | |
---|
103 | virtual epdf* epredictor() const NOT_IMPLEMENTED(NULL); |
---|
104 | |
---|
105 | virtual pdf* predictor() const NOT_IMPLEMENTED(NULL); |
---|
106 | }; |
---|
107 | UIREGISTER ( mexBM ); |
---|
108 | |
---|
109 | } |
---|