Changeset 102 for bdm/stat/libBM.h

Show
Ignore:
Timestamp:
05/12/08 17:34:07 (16 years ago)
Author:
smidl
Message:

corrections of sampling

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/libBM.h

    r96 r102  
    3131        //! len = number of individual rvs 
    3232        int len; 
    33         //! Vector of unique IDs  
     33        //! Vector of unique IDs 
    3434        ivec ids; 
    3535        //! Vector of sizes 
     
    4242private: 
    4343        //! auxiliary function used in constructor 
    44         void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times); 
     44        void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ); 
    4545public: 
    4646        //! Full constructor which is called by the others 
    47         RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times); 
     47        RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ); 
    4848        //! default constructor 
    4949        RV ( ivec ids ); 
     
    6363        //! Find indexes of another rv in self 
    6464        ivec find ( RV rv2 ); 
     65        //! Compare if \c rv2 is identical to this \c RV 
     66        bool equal ( RV rv2 ) const; 
    6567        //! Add (concat) another variable to the current one 
    66         void add (const RV &rv2 ); 
     68        void add ( const RV &rv2 ); 
    6769        //! Add (concat) another variable to the current one 
    68         friend RV concat (const RV &rv1, const RV &rv2 ); 
     70        friend RV concat ( const RV &rv1, const RV &rv2 ); 
    6971        //! Subtract  another variable from the current one 
    7072        RV subt ( RV rv2 ); 
     
    7981 
    8082        //!access function 
    81         Array<std::string>& _names(){return names;}; 
    82  
    83         //!access function 
    84         int id(int at){return ids(at);}; 
    85         //!access function 
    86         int size(int at){return sizes(at);}; 
    87         //!access function 
    88         int time(int at){return times(at);}; 
    89         //!access function 
    90         std::string name(int at){return names(at);}; 
     83        Array<std::string>& _names() {return names;}; 
     84 
     85        //!access function 
     86        int id ( int at ) {return ids ( at );}; 
     87        //!access function 
     88        int size ( int at ) {return sizes ( at );}; 
     89        //!access function 
     90        int time ( int at ) {return times ( at );}; 
     91        //!access function 
     92        std::string name ( int at ) {return names ( at );}; 
    9193}; 
    9294 
     
    100102public: 
    101103        //!default constructor 
    102         fnc(int dy):dimy(dy){}; 
     104        fnc ( int dy ) :dimy ( dy ) {}; 
    103105        //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond 
    104106        virtual vec eval ( const vec &cond ) { 
    105107                return vec ( 0 ); 
    106         };  
     108        }; 
    107109 
    108110        //! access function 
     
    131133        //! Returns a sample, \f$x\f$ from density \f$epdf(rv)\f$ 
    132134        virtual vec sample () const =0; 
     135        //! Returns N samples from density \f$epdf(rv)\f$ 
     136        virtual mat sampleN ( int N ) const; 
    133137        //! Compute probability of argument \c val 
    134         virtual double eval ( const vec &val ) const {return exp(this->evalpdflog(val));}; 
     138        virtual double eval ( const vec &val ) const {return exp ( this->evalpdflog ( val ) );}; 
    135139 
    136140        //! Compute log-probability of argument \c val 
    137141        virtual double evalpdflog ( const vec &val ) const =0; 
    138142 
    139         //! return expected value  
     143        //! return expected value 
    140144        virtual vec mean() const =0; 
    141          
     145 
    142146        //! Destructor for future use; 
    143147        virtual ~epdf() {}; 
     
    163167//      virtual fnc moment ( const int order = 1 ); 
    164168        //! 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 \param ll is a return value of log-likelihood of the sample. 
    165         virtual vec samplecond ( vec &cond, double &ll ) {this->condition(cond);vec temp= ep->sample();ll=ep->evalpdflog(temp);return temp;}; 
     169        virtual vec samplecond ( vec &cond, double &ll ) {this->condition ( cond );vec temp= ep->sample();ll=ep->evalpdflog ( temp );return temp;}; 
     170        //! Returns N samples from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv \param ll is a return value of log-likelihood of the sample. 
     171        virtual mat samplecond ( vec &cond, vec &ll, int N ) { 
     172                this->condition ( cond ); 
     173                mat temp ( rv.count(),N ); vec smp ( rv.count() ); for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );} 
     174                return temp; 
     175        }; 
    166176        //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond 
    167177        virtual void condition ( const vec &cond ) {}; 
    168          
     178 
    169179        //! Shortcut for conditioning and evaluation of the internal epdf. In some cases,  this operation can be implemented efficiently. 
    170         virtual double evalcond (const vec &dt, const vec &cond ) {this->condition(cond);return ep->eval(dt);}; 
     180        virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );}; 
    171181 
    172182        //! Destructor for future use; 
     
    176186        mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {}; 
    177187        //! access function 
    178         RV _rvc(){return rvc;} 
    179         //!access function 
    180         epdf& _epdf(){return *ep;} 
     188        RV _rvc() {return rvc;} 
     189        //!access function 
     190        epdf& _epdf() {return *ep;} 
    181191}; 
    182192 
     
    230240 
    231241        //!Default constructor 
    232         BM(const RV &rv0) :rv(rv0), ll ( 0 ),evalll ( true ) {//Fixme: test rv  
     242        BM ( const RV &rv0 ) :rv ( rv0 ), ll ( 0 ),evalll ( true ) {//Fixme: test rv 
    233243        }; 
    234244 
     
    240250        void bayes ( mat Dt ); 
    241251        //! Returns a pointer to the epdf representing posterior density on parameters. Use with care! 
    242         virtual epdf& _epdf()=0; 
     252        virtual epdf& _epdf() =0; 
    243253 
    244254        //! Destructor for future use; 
     
    267277        virtual void condition ( const vec &val ) =0; 
    268278        //! Default constructor 
    269         BMcond(RV &rv0):rvc(rv0){}; 
     279        BMcond ( RV &rv0 ) :rvc ( rv0 ) {}; 
    270280        //! Destructor for future use 
    271         virtual ~BMcond(){}; 
     281        virtual ~BMcond() {}; 
    272282        //! access function 
    273283        const RV& _rvc() const {return rvc;}