Changeset 286 for bdm/stat

Show
Ignore:
Timestamp:
03/05/09 14:03:35 (15 years ago)
Author:
smidl
Message:

make mpdm work again

Location:
bdm/stat
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/emix.h

    r271 r286  
    1818//#include <std> 
    1919 
    20 namespace bdm{ 
     20namespace bdm { 
    2121 
    2222//this comes first because it is used inside emix! 
     
    4747        //!Default constructor. By default, the given epdf is not copied! 
    4848        //! It is assumed that this function will be used only temporarily. 
    49         mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( ), dl ( rv,rvc,nom0->_rv() ) { 
    50                 ep->set_rv(_rv()); 
    51                 set_rvc(nom0->_rv().subt ( rv ) ); 
    52                 if ( copy ) { 
    53 //                      nom = nom0->_copy_(); 
    54                         it_error ( "todo" ); 
    55                         destroynom=true; 
    56                 } 
    57                 else { 
    58                         nom = nom0; 
    59                         destroynom = false; 
    60                 } 
    61                 it_assert_debug ( rvc.length() >0,"Makes no sense to use this object!" ); 
     49        mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( ), dl ( ) { 
     50                // adjust rv and rvc 
     51                rvc = nom0->_rv().subt ( rv ); 
     52                dimc = rvc._dsize(); 
     53                ep = new epdf; 
     54                ep->set_parameters(rv._dsize()); 
     55                ep->set_rv(rv); 
     56                 
     57                //prepare data structures 
     58                if ( copy ) {it_error ( "todo" ); destroynom=true; } 
     59                else { nom = nom0; destroynom = false; } 
     60                it_assert_debug ( rvc.length() >0,"Makes no sense to use this object!" );                
     61                 
     62                // build denominator 
    6263                den = nom->marginal ( rvc ); 
     64                dl.set_connection(rv,rvc,nom0->_rv()); 
    6365        }; 
    6466        double evallogcond ( const vec &val, const vec &cond ) { 
     
    6769                dl.pushup_cond ( nom_val,val,cond ); 
    6870                tmp = exp ( nom->evallog ( nom_val ) - den->evallog ( cond ) ); 
    69                 it_assert_debug(std::isfinite(tmp),"Infinite value"); 
     71                it_assert_debug ( std::isfinite ( tmp ),"Infinite value" ); 
    7072                return tmp; 
    7173        } 
     
    9698public: 
    9799        //!Default constructor 
    98         emix (  ) : epdf ( ) {}; 
     100        emix ( ) : epdf ( ) {}; 
    99101        //! Set weights \c w and components \c Coms 
    100102        //!By default Coms are copied inside. Parameter \c copy can be set to false if Coms live externally. Use method ownComs() if Coms should be destroyed by the destructor. 
     
    103105        vec sample() const; 
    104106        vec mean() const { 
    105                 int i; vec mu = zeros ( dim); 
     107                int i; vec mu = zeros ( dim ); 
    106108                for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); } 
    107109                return mu; 
     
    109111        vec variance() const { 
    110112                //non-central moment 
    111                 vec mom2 = zeros(dim); 
    112                 for ( int i = 0;i < w.length();i++ ) {mom2 += w ( i ) * pow(Coms ( i )->mean(),2); } 
     113                vec mom2 = zeros ( dim ); 
     114                for ( int i = 0;i < w.length();i++ ) {mom2 += w ( i ) * pow ( Coms ( i )->mean(),2 ); } 
    113115                //central moment 
    114                 return mom2-pow(mean(),2); 
     116                return mom2-pow ( mean(),2 ); 
    115117        } 
    116118        double evallog ( const vec &val ) const { 
     
    118120                double sum = 0.0; 
    119121                for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * exp ( Coms ( i )->evallog ( val ) );} 
    120                 if (sum==0.0){sum=std::numeric_limits<double>::epsilon();} 
     122                if ( sum==0.0 ) {sum=std::numeric_limits<double>::epsilon();} 
    121123                double tmp=log ( sum ); 
    122                 it_assert_debug(std::isfinite(tmp),"Infinite"); 
     124                it_assert_debug ( std::isfinite ( tmp ),"Infinite" ); 
    123125                return tmp; 
    124126        }; 
     
    151153        //!access function 
    152154        epdf* _Coms ( int i ) {return Coms ( i );} 
     155        void set_rv(const RV &rv){ 
     156                epdf::set_rv(rv); 
     157                for(int i=0;i<Coms.length();i++){Coms(i)->set_rv(rv);} 
     158        } 
    153159}; 
    154160 
     
    174180        mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf (), epdfs ( n ), dls ( n ) { 
    175181                ep=&dummy; 
    176                 RV rv=getrv(true); 
    177                 set_rv(rv);dummy.set_parameters(rv._dsize()); 
     182                RV rv=getrv ( true ); 
     183                set_rv ( rv );dummy.set_parameters ( rv._dsize() ); 
    178184                setrvc ( ep->_rv(),rvc ); 
    179185                // rv and rvc established = > we can link them with mpdfs 
    180186                for ( int i = 0;i < n;i++ ) { 
    181                         dls ( i ) = new datalink_m2m ( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), _rv(), _rvc() ); 
     187                        dls ( i ) = new datalink_m2m; 
     188                        dls(i)->set_connection( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), _rv(), _rvc() ); 
    182189                } 
    183190 
     
    237244        Array<datalink*> dls; 
    238245public: 
    239         eprod ( const Array<const epdf*> epdfs0 ) : epdf (  ),epdfs ( epdfs0 ),dls ( epdfs.length() ) { 
     246        eprod () : epdfs ( 0 ),dls ( 0 ) {}; 
     247        void set_parameters ( const Array<const epdf*> &epdfs0, bool named=true ) { 
     248                epdfs=epdfs0;//.set_length ( epdfs0.length() ); 
     249                dls.set_length ( epdfs.length() ); 
     250 
    240251                bool independent=true; 
    241                 for ( int i=0;i<epdfs.length();i++ ) { 
    242                         independent=rv.add ( epdfs ( i )->_rv() ); 
    243                         it_assert_debug ( independent==true, "eprod:: given components are not independent ." ); 
    244                 } 
    245                 for ( int i=0;i<epdfs.length();i++ ) { 
    246                         dls ( i ) = new datalink ( epdfs ( i )->_rv() , rv ); 
     252                if ( named ) { 
     253                        for ( int i=0;i<epdfs.length();i++ ) { 
     254                                independent=rv.add ( epdfs ( i )->_rv() ); 
     255                                it_assert_debug ( independent==true, "eprod:: given components are not independent." ); 
     256                        } 
     257                        dim=rv._dsize(); 
     258                } 
     259                else { 
     260                        dim =0; for ( int i=0;i<epdfs.length();i++ ) { 
     261                                dim+=epdfs ( i )->dimension(); 
     262                        } 
     263                } 
     264                // 
     265                int cumdim=0; 
     266                int dimi=0; 
     267                int i; 
     268                for ( i=0;i<epdfs.length();i++ ) { 
     269                        dls ( i ) = new datalink; 
     270                        if ( named ) {dls ( i )->set_connection ( epdfs ( i )->_rv() , rv );} 
     271                        else { 
     272                                dimi = epdfs ( i )->dimension(); 
     273                                dls ( i )->set_connection ( dimi, dim, linspace ( cumdim,cumdim+dimi-1 ) ); 
     274                                cumdim+=dimi; 
     275                        } 
    247276                } 
    248277        } 
     
    260289                for ( int i=0;i<epdfs.length();i++ ) { 
    261290                        vec pom = epdfs ( i )->mean(); 
    262                         dls ( i )->pushup ( tmp, pow(pom,2) ); 
    263                 } 
    264                 return tmp-pow(mean(),2); 
     291                        dls ( i )->pushup ( tmp, pow ( pom,2 ) ); 
     292                } 
     293                return tmp-pow ( mean(),2 ); 
    265294        } 
    266295        vec sample() const { 
     
    277306                        tmp+=epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) ); 
    278307                } 
    279                 it_assert_debug(std::isfinite(tmp),"Infinite"); 
     308                it_assert_debug ( std::isfinite ( tmp ),"Infinite" ); 
    280309                return tmp; 
    281310        } 
  • bdm/stat/libBM.h

    r283 r286  
    261261        //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default 
    262262        virtual void qbounds ( vec &lb, vec &ub, double percentage=0.95 ) const { 
    263                 vec mea=mean(); vec std=sqrt(variance());  
     263                vec mea=mean(); vec std=sqrt ( variance() ); 
    264264                lb = mea-2*std; ub=mea+2*std; 
    265265        }; 
     
    401401                it_assert_debug ( v2v_up.length() ==downsize,"rv is not fully in rv_up" ); 
    402402        } 
     403        //! set connection using indeces 
     404        void set_connection ( int ds, int us, const ivec &upind ) { 
     405                downsize = ds; 
     406                upsize = us; 
     407                v2v_up= upind; 
     408 
     409                it_assert_debug ( v2v_up.length() ==downsize,"rv is not fully in rv_up" ); 
     410        } 
    403411        //! Get val for myself from val of "Up" 
    404412        vec pushdown ( const vec &val_up ) { 
     
    425433 
    426434public: 
     435        datalink_m2e() {}; 
    427436        //! Constructor 
    428         datalink_m2e ( const RV &rv,  const RV &rvc, const RV &rv_up ) : 
    429                         datalink ( rv,rv_up ), condsize ( rvc._dsize() ) { 
     437        void set_connection ( const RV &rv,  const RV &rvc, const RV &rv_up ) { 
     438                datalink::set_connection ( rv,rv_up ); 
     439                condsize=  rvc._dsize(); 
    430440                //establish v2c connection 
    431441                rvc.dataind ( rv_up, v2c_lo, v2c_up ); 
     
    454464public: 
    455465        //! Constructor 
    456         datalink_m2m ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) : 
    457                         datalink_m2e ( rv, rvc, rv_up ) { 
     466        datalink_m2m() {}; 
     467        void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) { 
     468                datalink_m2e::set_connection ( rv, rvc, rv_up ); 
    458469                //establish c2c connection 
    459470                rvc.dataind ( rvc_up, c2c_lo, c2c_up ); 
     
    714725                if ( opt_L_bounds ) { 
    715726                        vec ub,lb; 
    716                         posterior().qbounds(lb,ub); 
     727                        posterior().qbounds ( lb,ub ); 
    717728                        L->logit ( LIDs ( 1 ), lb ); 
    718729                        L->logit ( LIDs ( 2 ), ub ); 
  • bdm/stat/libEF.h

    r285 r286  
    9494//      virtual void flatten ( double nu0 ) {it_error ( "Not implemented" );} 
    9595 
    96         BMEF* _copy_ ( bool changerv=false ) const {it_error ( "function _copy_ not implemented for this BM" ); return NULL;}; 
     96        BMEF* _copy_ () const {it_error ( "function _copy_ not implemented for this BM" ); return NULL;}; 
    9797}; 
    9898 
     
    707707class iW : public epdf{ 
    708708        public: 
    709                 vec sample(){} 
     709                vec sample(){return vec();} 
    710710}; 
    711711