Changeset 675

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

experiment: epdf as a descendat of mpdf

Location:
library
Files:
15 modified

Legend:

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

    r624 r675  
    373373} 
    374374 
    375 void mepdf::from_setting ( const Setting &set ) { 
    376         shared_ptr<epdf> e ( UI::build<epdf> ( set, "epdf", UI::compulsory ) ); 
    377         iepdf = e; 
    378         set_ep ( iepdf.get() ); 
    379 } 
    380  
    381375RV get_composite_rv ( const Array<shared_ptr<mpdf> > &mpdfs, 
    382376                      bool checkoverlap ) { 
  • 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() ); 
  • library/bdm/estim/kalman.h

    r660 r675  
    214214                */ 
    215215                void bayes (const vec &dt); 
    216                  
     216 
    217217                void from_setting(const Setting &set){ 
    218218                        Kalman<chmat>::from_setting(set); 
  • library/bdm/estim/particles.h

    r665 r675  
    389389                } 
    390390        } 
    391          
     391 
    392392}; 
    393393UIREGISTER(MPF); 
  • library/bdm/stat/emix.cpp

    r620 r675  
    207207        dls.set_size ( mFacs.length() ); 
    208208 
    209         set_ep ( iepdf); 
    210         RV rv = get_composite_rv ( mpdfs, true ); 
    211         set_rv ( rv ); 
    212         iepdf.set_parameters ( rv._dsize() ); 
     209        rv = get_composite_rv ( mpdfs, true ); 
     210        dim = rv._dsize(); 
    213211 
    214212        for ( int i = 0; i < mpdfs.length(); i++ ) { 
  • library/bdm/stat/emix.h

    r660 r675  
    4747        //!datalink between conditional and nom 
    4848        datalink_m2e dl; 
    49         //! dummy epdf that stores only rv and dim 
    50         epdf iepdf; 
    5149public: 
    5250        //!Default constructor. By default, the given epdf is not copied! 
    5351        //! It is assumed that this function will be used only temporarily. 
    54         mratio ( const epdf* nom0, const RV &rv, bool copy = false ) : mpdf ( ), dl ( ),iepdf() { 
     52        mratio ( const epdf* nom0, const RV &rv, bool copy = false ) : mpdf ( ), dl ( ) { 
    5553                // adjust rv and rvc 
     54 
     55                set_rv( rv ); // TODO co kdyby tohle samo uz nastavovalo dimension?!?! 
     56                dim = rv._dsize(); 
     57 
    5658                rvc = nom0->_rv().subt ( rv ); 
    5759                dimc = rvc._dsize(); 
    58                 set_ep ( iepdf ); 
    59                 iepdf.set_parameters ( rv._dsize() ); 
    60                 iepdf.set_rv ( rv ); 
    61  
     60         
    6261                //prepare data structures 
    6362                if ( copy ) { 
     
    7473                dl.set_connection ( rv, rvc, nom0->_rv() ); 
    7574        }; 
     75 
    7676        double evallogcond ( const vec &val, const vec &cond ) { 
    7777                double tmp; 
     
    469469        //!weights of the components 
    470470        vec w; 
    471         //! dummy epdfs 
    472         epdf dummy_epdf; 
    473471public: 
    474472        //!Default constructor 
    475         mmix() : Coms(0), dummy_epdf() { set_ep(dummy_epdf);    } 
     473        mmix() : Coms(0) { } 
    476474 
    477475        //! Set weights \c w and components \c R 
     
    483481                if (Coms0.length()>0){ 
    484482                        set_rv(Coms(0)->_rv()); 
    485                         dummy_epdf.set_parameters(Coms(0)->_rv()._dsize()); 
     483                        dim = rv._dsize(); 
    486484                        set_rvc(Coms(0)->_rvc()); 
    487485                        dimc = rvc._dsize(); 
  • library/tests/epdf_harness.cpp

    r598 r675  
    7171                mpdf_array aa ( 2 ); 
    7272                aa ( 0 ) = c; 
    73                 aa ( 1 ) = new mepdf ( m ); 
     73                aa ( 1 ) = m; 
    7474                mprod mEp ( aa ); 
    7575 
  • library/tests/mepdf.cfg

    r489 r675  
    33  class = "mpdf_harness"; 
    44  mpdf = { 
    5     class = "mepdf"; 
    6     epdf = { 
    75      class = "enorm<ldmat>"; 
    86      mu = [ 1.1, -1.0 ]; 
     
    1311        names = ( "x", "y" ); 
    1412      }; 
    15     }; 
    1613  }; 
    1714  cond = [ 1.5, 1.7 ]; 
  • library/tests/merger.cfg

    r562 r675  
    11Sources = (  
    22  { 
    3     class = "mepdf"; 
    4     epdf :  
    5     { 
    63      class = "enorm<ldmat>"; 
    74      mu = ( "matrix", 1, 2, [ 0.0, 2.0 ] ); 
     
    129        names = ( "a", "b" ); 
    1310      }; 
    14     }; 
    1511  },  
    1612  { 
    17     class = "mepdf"; 
    18     epdf :  
    19     { 
    2013      class = "egiw"; 
    2114      V = ( "matrix", 2, 2, [ 20.0, 8.0, 8.0, 4.0 ] ); 
     
    2720        names = ( "a", "b" ); 
    2821      }; 
    29     }; 
    3022  } ); 
    3123Support :  
  • library/tests/merger_2d_test.cpp

    r529 r675  
    3232 
    3333        mpdf_array A ( 2 ); 
    34         mepdf_ptr A1 = new mepdf ( f1 ); 
    35         mepdf_ptr A2 = new mepdf ( f2 ); 
    36         A ( 0 ) = A1; 
    37         A ( 1 ) = A2; 
     34        A ( 0 ) = f1; 
     35        A ( 1 ) = f2; 
    3836 
    3937        int Npoints = 100; 
  • library/tests/merger_error.cfg

    r562 r675  
    11Sources = (  
    22  { 
    3     class = "mepdf"; 
    4     epdf :  
    5     { 
    63      class = "enorm<ldmat>"; 
    74      mu = ( "matrix", 1, 2, [ 0.0, 2.0 ] ); 
     
    129        names = ( "a", "b" ); 
    1310      }; 
    14     }; 
    1511  },  
    1612  { 
    17     class = "mepdf"; 
    18     epdf :  
    19     { 
    2013      class = "egiw"; 
    2114      V = ( "matrix", 2, 2, [ 20.0, 8.0, 8.0, 4.0 ] ); 
     
    2720        names = ( "a", "b" ); 
    2821      }; 
    29     }; 
    3022  } ); 
    3123Support :  
  • library/tests/merger_iter_test.cpp

    r529 r675  
    3333 
    3434        mpdf_array A ( 3 ); 
    35         A ( 0 ) = new mepdf ( f1 ); 
    36         A ( 1 ) = new mepdf ( f2 ); 
    37         A ( 2 ) = new mepdf ( f3 ); 
     35        A ( 0 ) = f1; 
     36        A ( 1 ) = f2; 
     37        A ( 2 ) = f3; 
    3838 
    3939        int Npoints = 100; 
  • library/tests/merger_test.cpp

    r562 r675  
    2222 
    2323        mpdf_array A ( 2 ); 
    24         A ( 0 ) = new mepdf ( f1 ); 
    25         A ( 1 ) = new mepdf ( f2 ); 
     24        A ( 0 ) = f1; 
     25        A ( 1 ) = f2; 
    2626 
    2727        int Npoints = 100; 
  • library/tests/mpdf_test.cpp

    r529 r675  
    5454 
    5555        eN->set_mu ( vec_2 ( 0.0, 0.0 ) ); 
    56         mepdf_ptr mEnorm = new mepdf ( eN ); 
    57         mComs ( 1 ) = mEnorm; 
     56        mComs ( 1 ) = eN; 
    5857 
    5958        mMix.set_parameters ( vec_2 ( 0.5, 0.5 ), mComs ); 
     
    8786 
    8887        eMix->set_parameters ( vec_2 ( 0.5, 0.5 ), Coms ); 
    89         mepdf meMix ( eMix ); 
    90         check_mean ( meMix, mu0, N, eMix->mean(), 0.1 ); 
     88        check_mean ( *eMix, mu0, N, eMix->mean(), 0.1 ); 
    9189} 
    9290 
  • library/tests/mprod.cfg

    r531 r675  
    2121    },  
    2222    { 
    23       class = "mepdf"; 
    24       epdf :  
    25       { 
    2623        class = "egamma"; 
    2724        alpha = 2.0; 
     
    3229          names = ( "a" ); 
    3330        }; 
    34       }; 
    3531    }); 
    3632  }; 
     
    4642    mpdfs = (  
    4743    { 
    48       class = "mepdf"; 
    49       epdf :  
    50       { 
    5144        class = "enorm<ldmat>"; 
    5245        mu = [ 1.5, 1.7 ]; 
     
    5851          sizes = [ 2 ]; 
    5952        }; 
    60       }; 
    6153    },  
    6254    { 
    63       class = "mepdf"; 
    64       epdf :  
    65       { 
    6655        class = "egamma"; 
    6756        alpha = [ 100000, 10000 ]; 
     
    7261          sizes = [ 2 ]; 
    7362        }; 
    74       }; 
    7563    }); 
    7664  };