| 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); |
| | 1 | #include <mex/mex_parser.h> |
| | 2 | #include <mex/mex_pdf.h> |
| | 3 | class mexBM: public BM{ |
| | 4 | protected: |
| | 5 | mxArray *data; |
| | 6 | mexEpdf est; |
| | 7 | public: |
| | 8 | mexBM() {}; |
| | 9 | void from_setting(const Setting &S) { |
| | 10 | Setting &So=S["object"]; |
| | 11 | data = (mxArray*)long(So); |
| | 12 | } |
| | 13 | void validate() { |
| | 14 | mexCallMATLAB(0, 0, 1, &data, "validate"); |
| | 15 | mxArray *tmp; |
| | 16 | mexCallMATLAB(1, &tmp, 1, &data, "dimensions"); |
| | 17 | vec v=mxArray2vec(tmp); |
| | 18 | if (v.length()<3) {bdm_error("Three dimensions are expected in mexBM.dimensions");} |
| | 19 | set_dim(v(0)); |
| | 20 | dimy = v(1); |
| | 21 | dimc = v(2); |
| | 22 | } |
| | 23 | void bayes(const vec &dt, const vec &cond) { |
| | 24 | mxArray *tmp; |
| | 25 | mxArray *in[3]; |
| | 26 | in[0]=data; |
| | 27 | in[1]=mxCreateDoubleMatrix(dt.length(),1,mxREAL); |
| | 28 | vec2mxArray(dt,in[1]); |
| | 29 | in[2]=mxCreateDoubleMatrix(cond.length(),1,mxREAL); |
| | 30 | vec2mxArray(cond,in[2]); |
| | 31 | |
| | 32 | mexCallMATLAB(1, &tmp, 3, in, "bayes"); |
| | 33 | if (data!=tmp){ |
| | 34 | mxDestroyArray ( data ); |
| | 35 | data = mxDuplicateArray ( tmp ); |
| | 36 | } // |
| | 37 | } |
| | 38 | epdf* predictor(const vec &cond) const { |
| | 39 | mxArray *tmp; |
| | 40 | mxArray *in[3]; |
| | 41 | in[0]=data; |
| | 42 | in[1]=mxCreateDoubleMatrix(cond.length(),1,mxREAL); |
| | 43 | vec2mxArray(cond,in[1]); |
| | 44 | |
| | 45 | mexCallMATLAB(1, &tmp, 3, in, "predictor"); |
| | 46 | |
| | 47 | UImxArray uitmp(tmp); |
| | 48 | shared_ptr<epdf> pred = UI::build<epdf>(uitmp); |
| | 49 | return pred.get(); |
| | 50 | // |
| | 51 | } |
| | 52 | const epdf& posterior() const { |
| | 53 | return est; |
| | 54 | } |
| 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 | | }; |
| 102 | | UIREGISTER ( mexBM ); |
| 103 | | |
| 104 | | } |
| | 56 | UIREGISTER(mexBM); |