Changeset 182 for bdm/stat/emix.h

Show
Ignore:
Timestamp:
10/17/08 10:28:15 (16 years ago)
Author:
smidl
Message:

compilation error!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/emix.h

    r181 r182  
    1919 
    2020using namespace itpp; 
     21 
     22//this comes first because it is used inside emix! 
     23 
     24/*! \brief Class representing ratio of two densities 
     25which arise e.g. by applying the Bayes rule. 
     26It represents density in the form: 
     27\f[ 
     28f(rv|rvc) = \frac{f(rv,rvc)}{f(rvc)} 
     29\f] 
     30where \f$ f(rvc) = \int f(rv,rvc) d\ rv \f$. 
     31 
     32In particular this type of arise by conditioning of a mixture model. 
     33 
     34At present the only supported operation is evalcond(). 
     35 */ 
     36class mratio: public mpdf { 
     37        protected: 
     38        //! Nominator in the form of mpdf 
     39                const epdf* nom; 
     40        //!Denominator in the form of epdf 
     41                epdf* den; 
     42        //!flag for destructor  
     43                bool destroynom; 
     44        public: 
     45        //!Default constructor. By default, the given epdf is not copied!  
     46        //! It is assumed that this function will be used only temporarily. 
     47                mratio(const epdf* nom0, const RV &rv, bool copy=false):mpdf(rv,RV()){ 
     48                        if (copy){ 
     49//                      nom = nom0->_copy_(); 
     50                                destroynom=true; 
     51                        } else { 
     52                                nom = nom0; 
     53                                destroynom = false; 
     54                        } 
     55                        rvc = nom->_rv().subt(rv); 
     56                        it_assert_debug(rvc.length()>0,"Makes no sense to use this object!"); 
     57                        den = nom->marginal(rvc); 
     58                }; 
     59                double evalcond(const vec &val, const vec &cond) const { 
     60                        return exp(nom->evalpdflog(concat(val,cond)) - den->evalpdflog(cond)); 
     61                } 
     62        //! Object takes ownership of nom and will destroy it 
     63                void ownnom(){destroynom=true;} 
     64        //! Default destructor 
     65                ~mratio(){delete den; if (destroynom) {delete nom;}} 
     66}; 
    2167 
    2268/*! 
     
    4187        //!Default constructor 
    4288        emix ( const RV &rv ) : epdf ( rv ) {}; 
    43         //! Set weights \c w and components \c Coms , Coms are not copied! 
     89        //! Set weights \c w and components \c Coms 
     90        //!By default Coms are copied inside. \param copy can be set to false if Coms live externally. Use method ownComs() if Coms should be destroyed by the destructor. 
    4491        void set_parameters ( const vec &w, const Array<epdf*> &Coms, bool copy=true ); 
    4592 
     
    56103                return log ( sum ); 
    57104        }; 
     105        vec evalpdflog_m ( const mat &Val ) const { 
     106                vec x=ones(Val.cols()); 
     107                vec y(Val.cols()); 
     108                for (int i = 0; i < w.length(); i++ ) { 
     109                        y = w ( i )*exp(Coms ( i )->evalpdflog_m ( Val ) ); 
     110                        elem_mult_inplace(y,x); //result is in x 
     111                } 
     112                return log(x); 
     113        }; 
     114 
     115        emix* marginal ( const RV &rv ) const; 
     116        mratio* condition ( const RV &rv ) const; //why not mratio!! 
    58117 
    59118//Access methods 
     
    78137        Array<epdf*> epdfs; 
    79138        //! Indeces of rvc in common rvc 
    80         Array<ivec> rvcsinrvc; 
     139        Array<ivec> irvcs_rvc; 
    81140        //! Indeces of common rvc in rvcs 
    82         Array<ivec> rvcinrvcs; 
     141        Array<ivec> irvc_rvcs; 
    83142public: 
    84143        /*!\brief Constructor from list of mFacs, 
    85144        */ 
    86         mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), rvcsinrvc ( n ) { 
     145        mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), irvcs_rvc ( n ), irvc_rvcs ( n ) { 
    87146                setrvc ( rv,rvc ); 
    88147                setindices ( rv ); 
    89148                for ( int i = 0;i < n;i++ ) { 
    90                         // find ith rvc in common rv 
    91                         rvcsinrvc ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 
    92                         //and vice-versa 
    93                         rvcinrvcs ( i ) = rvc.dataind ( mpdfs ( i )->_rvc() ); 
     149                        // establish link between rvc and rvcs 
     150                        rvc.dataind ( mpdfs ( i )->_rvc(), irvc_rvcs ( i ), irvcs_rvc ( i ) ); 
    94151                } 
    95152 
     
    99156        }; 
    100157 
    101         double evalcond ( const vec &val, const vec &cond ) const { 
     158        double evalcond ( const vec &val, const vec &cond ) { 
    102159                int i; 
    103160                double res = 0.0; 
    104161                vec condi; 
    105                 for ( i = n - 1;i > 0;i++ ) { 
    106                         if ( ( rvcsinrvc ( i ).length() > 0 ) || ( rvcsinrv ( i ).length() > 0 ) ) { 
    107                                 condi = zeros ( rvcsinrvc ( i ).length() +rvcsinrv ( i ).length() ); 
     162                for ( i = n - 1;i >= 0;i-- ) { 
     163                        if ( ( irvcs_rvc ( i ).length() > 0 ) || ( irvcs_rv ( i ).length() > 0 ) ) { 
     164                                condi = zeros ( irvcs_rvc ( i ).length() +irvcs_rv ( i ).length() ); 
    108165                                //copy part of the rvc into condi 
    109                                 set_subvector ( condi, rvcinrvcs ( i ), get_vec ( cond,rvcsinrvc ( i ) ) ); 
     166                                set_subvector ( condi, irvcs_rvc ( i ), get_vec ( cond,irvc_rvcs ( i ) ) ); 
    110167                                //copy part of the rv into condi 
    111                                 set_subvector ( condi, rvinrvcs ( i ), get_vec ( val,rvcsinrv ( i ) ) ); 
     168                                set_subvector ( condi, irvcs_rv ( i ), get_vec ( val,irv_rvcs ( i ) ) ); 
    112169                                mpdfs ( i )->condition ( condi ); 
    113170                        } 
     
    115172                        res += epdfs ( i )->evalpdflog ( val ( rvsinrv ( i ) ) ); 
    116173                } 
    117                 return res; 
     174                return exp ( res ); 
    118175        } 
    119176        vec samplecond ( const vec &cond, double &ll ) { 
     
    123180                ll = 0; 
    124181                for ( int i = ( n - 1 );i >= 0;i-- ) { 
    125                         if ( ( rvcsinrvc ( i ).length() > 0 ) || ( rvcsinrv ( i ).length() > 0 ) ) { 
    126                                 condi = zeros ( rvcsinrv ( i ).length() + rvcsinrvc ( i ).length() ); 
     182                        if ( ( irvcs_rvc ( i ).length() > 0 ) || ( irvcs_rv ( i ).length() > 0 ) ) { 
     183                                condi = zeros ( irvcs_rv ( i ).length() + irvcs_rvc ( i ).length() ); 
    127184                                // copy data in condition 
    128                                 set_subvector ( condi,rvcsinrvc ( i ), cond ); 
     185                                set_subvector ( condi,irvcs_rvc ( i ), get_vec ( cond, irvc_rvcs ( i ) ) ); 
    129186                                // copy data in already generated sample 
    130                                 set_subvector ( condi,rvinrvcs ( i ), get_vec ( smp,rvcsinrv ( i ) ) ); 
     187                                set_subvector ( condi,irvcs_rv ( i ), get_vec ( smp,irv_rvcs ( i ) ) ); 
    131188 
    132189                                mpdfs ( i )->condition ( condi ); 
     
    220277        }; 
    221278}; 
     279 
    222280#endif //MX_H