Show
Ignore:
Timestamp:
10/21/09 20:19:40 (15 years ago)
Author:
mido
Message:

experiment: epdf as a descendat of mpdf

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.h

    r665 r675  
    326326}; 
    327327 
    328 class mpdf; 
     328class epdf; 
     329 
     330//! Conditional probability density, e.g. modeling \f$ f( x | y) \f$, where \f$ x \f$ is random variable, \c rv, and \f$ y \f$ is conditioning variable, \c rvc. 
     331class mpdf : public root { 
     332protected: 
     333        //!dimension of the condition 
     334        int dimc; 
     335        //! random variable in condition 
     336        RV rvc; 
     337 
     338        //! TODO 
     339        int dim; 
     340 
     341        //! TODO 
     342        RV rv; 
     343 
     344public: 
     345        //! \name Constructors 
     346        //! @{ 
     347 
     348        mpdf() : dimc ( 0 ), rvc(), dim(0), rv() { } 
     349 
     350        mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), rv( m.rv ), dim( m.dim) { } 
     351        //!@} 
     352 
     353        //! \name Matematical operations 
     354        //!@{ 
     355 
     356        //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 
     357        virtual vec samplecond ( const vec &cond ) { 
     358                bdm_error ( "Not implemented" ); 
     359                return vec(); 
     360        } 
     361 
     362        //! Returns \param N samples from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 
     363        virtual mat samplecond_m ( const vec &cond, int N ); 
     364 
     365        //! Shortcut for conditioning and evaluation of the internal epdf. In some cases,  this operation can be implemented efficiently. 
     366        virtual double evallogcond ( const vec &dt, const vec &cond ) { 
     367                bdm_error ( "Not implemented" ); 
     368                return 0.0; 
     369        } 
     370 
     371        //! Matrix version of evallogcond 
     372        virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) { 
     373                vec v ( Dt.cols() ); 
     374                for ( int i = 0; i < Dt.cols(); i++ ) { 
     375                        v ( i ) = evallogcond ( Dt.get_col ( i ), cond ); 
     376                } 
     377                return v; 
     378        } 
     379 
     380        //! Array<vec> version of evallogcond 
     381        virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { 
     382                bdm_error ( "Not implemented" ); 
     383                return vec(); 
     384        } 
     385 
     386        //! \name Access to attributes 
     387        //! @{ 
     388 
     389        const RV& _rv() const { 
     390                return rv; 
     391        } 
     392        const RV& _rvc() const { 
     393                return rvc; 
     394        } 
     395 
     396        int dimension() const { 
     397                return dim; 
     398        } 
     399        int dimensionc() { 
     400                return dimc; 
     401        } 
     402 
     403        //! Load from structure with elements: 
     404        //!  \code 
     405        //! { class = "mpdf_offspring", 
     406        //!   rv = {class="RV", names=(...),}; // RV describing meaning of random variable 
     407        //!   rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition 
     408        //!   // elements of offsprings 
     409        //! } 
     410        //! \endcode 
     411        //!@} 
     412        void from_setting ( const Setting &set ); 
     413        //!@} 
     414 
     415        //! \name Connection to other objects 
     416        //!@{ 
     417        void set_rvc ( const RV &rvc0 ) { 
     418                rvc = rvc0; 
     419        } 
     420        void set_rv ( const RV &rv0 ) { 
     421                rv = rv0; 
     422        } 
     423 
     424        bool isnamed() { 
     425                return ( dim == rv._dsize() ) && ( dimc == rvc._dsize() ); 
     426        } 
     427        //!@} 
     428}; 
     429SHAREDPTR ( mpdf ); 
    329430 
    330431//! Probability density function with numerical statistics, e.g. posterior density. 
    331  
    332 class epdf : public root { 
     432class epdf : public mpdf { 
    333433protected: 
    334434        //! dimension of the random variable 
     
    456556        } 
    457557 
     558 
     559/*! \brief Unconditional mpdf, allows using epdf in the role of mpdf. 
     560 
     561*/ 
     562 
     563 
     564/// MEPDF BEGINS HERE TODO 
     565        //! empty 
     566        vec samplecond ( const vec &cond ) { 
     567                return sample(); 
     568        } 
     569        double evallogcond ( const vec &val, const vec &cond ) { 
     570                return evallog ( val ); 
     571        } 
    458572}; 
    459573SHAREDPTR ( epdf ); 
    460  
    461 //! Conditional probability density, e.g. modeling \f$ f( x | y) \f$, where \f$ x \f$ is random variable, \c rv, and \f$ y \f$ is conditioning variable, \c rvc. 
    462 class mpdf : public root { 
    463 protected: 
    464         //!dimension of the condition 
    465         int dimc; 
    466         //! random variable in condition 
    467         RV rvc; 
    468 private: 
    469         //! internal epdf, used only as cache to avoid virtual calls of \c _rv() and \c _dimension() 
    470         epdf* ep; 
    471  
    472 protected: 
    473         //! set internal pointer \c ep to point to given \c iepdf 
    474         void set_ep ( epdf &iepdf ) { 
    475                 ep = &iepdf; 
    476         } 
    477         //! set internal pointer \c ep to point to given \c iepdf 
    478         void set_ep ( epdf *iepdfp ) { 
    479                 ep = iepdfp; 
    480         } 
    481  
    482 public: 
    483         //! \name Constructors 
    484         //! @{ 
    485  
    486         mpdf() : dimc ( 0 ), rvc(), ep ( NULL ) { } 
    487  
    488         mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), ep ( m.ep ) { } 
    489         //!@} 
    490  
    491         //! \name Matematical operations 
    492         //!@{ 
    493  
    494         //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 
    495         virtual vec samplecond ( const vec &cond ) { 
    496                 bdm_error ( "Not implemented" ); 
    497                 return vec(); 
    498         } 
    499  
    500         //! Returns \param N samples from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 
    501         virtual mat samplecond_m ( const vec &cond, int N ); 
    502  
    503         //! Shortcut for conditioning and evaluation of the internal epdf. In some cases,  this operation can be implemented efficiently. 
    504         virtual double evallogcond ( const vec &dt, const vec &cond ) { 
    505                 bdm_error ( "Not implemented" ); 
    506                 return 0.0; 
    507         } 
    508  
    509         //! Matrix version of evallogcond 
    510         virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) { 
    511                 vec v ( Dt.cols() ); 
    512                 for ( int i = 0; i < Dt.cols(); i++ ) { 
    513                         v ( i ) = evallogcond ( Dt.get_col ( i ), cond ); 
    514                 } 
    515                 return v; 
    516         } 
    517  
    518         //! Array<vec> version of evallogcond 
    519         virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { 
    520                 bdm_error ( "Not implemented" ); 
    521                 return vec(); 
    522         } 
    523  
    524         //! \name Access to attributes 
    525         //! @{ 
    526  
    527         const RV& _rv() const { 
    528                 return ep->_rv(); 
    529         } 
    530         const RV& _rvc() const { 
    531                 return rvc; 
    532         } 
    533  
    534         int dimension() const { 
    535                 return ep->dimension(); 
    536         } 
    537         int dimensionc() { 
    538                 return dimc; 
    539         } 
    540  
    541         //! Load from structure with elements: 
    542         //!  \code 
    543         //! { class = "mpdf_offspring", 
    544         //!   rv = {class="RV", names=(...),}; // RV describing meaning of random variable 
    545         //!   rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition 
    546         //!   // elements of offsprings 
    547         //! } 
    548         //! \endcode 
    549         //!@} 
    550         void from_setting ( const Setting &set ); 
    551         //!@} 
    552  
    553         //! \name Connection to other objects 
    554         //!@{ 
    555         void set_rvc ( const RV &rvc0 ) { 
    556                 rvc = rvc0; 
    557         } 
    558         void set_rv ( const RV &rv0 ) { 
    559                 ep->set_rv ( rv0 ); 
    560         } 
    561         bool isnamed() { 
    562                 return ( ep->isnamed() ) && ( dimc == rvc._dsize() ); 
    563         } 
    564         //!@} 
    565 }; 
    566 SHAREDPTR ( mpdf ); 
    567574 
    568575//! Mpdf with internal epdf that is modified by function \c condition 
     
    575582        //! constructor 
    576583        mpdf_internal() : mpdf(), iepdf() { 
    577                 set_ep ( iepdf ); 
     584//              set_ep ( iepdf ); TODO! 
    578585        } 
    579586 
     
    906913}; 
    907914 
    908 /*! \brief Unconditional mpdf, allows using epdf in the role of mpdf. 
    909  
    910 */ 
    911 class mepdf : public mpdf { 
    912         //! Internal shared pointer to epdf 
    913         shared_ptr<epdf> iepdf; 
    914 public: 
    915         //!Default constructor 
    916         mepdf() { } 
    917         //! Set internal shared pointer 
    918         mepdf ( shared_ptr<epdf> em ) { 
    919                 iepdf = em; 
    920                 set_ep ( *iepdf.get() ); 
    921                 dimc = 0; 
    922         } 
    923  
    924         //! empty 
    925         vec samplecond ( const vec &cond ) { 
    926                 return iepdf->sample(); 
    927         } 
    928         double evallogcond ( const vec &val, const vec &cond ) { 
    929                 return iepdf->evallog ( val ); 
    930         } 
    931  
    932         //! Load from structure with elements: 
    933         //!  \code 
    934         //! { class = "mepdf", 
    935         //!   epdf = {class="epdf_offspring",...} 
    936         //! } 
    937         //! \endcode 
    938         //!@} 
    939         void from_setting ( const Setting &set ); 
    940 }; 
    941 UIREGISTER ( mepdf ); 
    942 SHAREDPTR ( mepdf ); 
    943915 
    944916//! \brief Combines RVs from a list of mpdfs to a single one. 
     
    12091181                if ( LFlags ( 3 ) ) LIDs ( 3 ) = L.add ( RV ( "ll", 1 ), name );    //TODO: "local" RV 
    12101182        } 
    1211         //! Save results to the given logger, details of what is stored is configured by \c LIDs and \c options 
    12121183        virtual void logit ( logger &L ) { 
    12131184                L.logit ( LIDs ( 0 ), posterior().mean() );