Changeset 182 for bdm/stat

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

compilation error!

Location:
bdm/stat
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/emix.cpp

    r181 r182  
    3131} 
    3232 
    33 // mprod::mprod ( Array<mpdf*> mFacs, bool overlap) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), rvcsinrvc ( n ) { 
     33emix* emix::marginal(const RV &rv) const{ 
     34        Array<epdf*> Cn(Coms.length()); 
     35        for(int i=0;i<Coms.length();i++){Cn(i)=Coms(i)->marginal(rv);} 
     36        emix* tmp = new emix(rv); 
     37        tmp->set_parameters(w,Coms,false); 
     38        tmp->ownComs(); 
     39        return tmp; 
     40} 
     41 
     42mratio* emix::condition(const RV &rv) const{ 
     43        return new mratio(this,rv); 
     44}; 
     45 
     46// mprod::mprod ( Array<mpdf*> mFacs, bool overlap) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), irvcs_rvc ( n ) { 
    3447//              int i; 
    3548//              bool rvaddok; 
     
    5467//                      rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); 
    5568//                      // find ith rvc in common rv 
    56 //                      rvcsinrvc ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 
     69//                      irvcs_rvc ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 
    5770//                      // 
    5871// /*                   if ( rvcinrv ( i ).length() >0 ) {independent = false;} 
    59 //                      if ( rvcsinrvc ( i ).length() >0 ) {independent = false;}*/ 
     72//                      if ( irvcs_rvc ( i ).length() >0 ) {independent = false;}*/ 
    6073//              } 
    6174//      }; 
  • 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 
  • bdm/stat/libBM.cpp

    r181 r182  
    151151                } 
    152152        } 
     153        it_assert_debug(res.length()==tsize,"this rv is not fully present in crv!"); 
    153154        return res; 
    154155 
     156} 
     157 
     158void RV::dataind (const RV &rv2, ivec &selfi, ivec &rv2i) const { 
     159        //clean results 
     160        selfi.set_size(0); 
     161        rv2i.set_size(0); 
     162         
     163        // just in case any rv is empty 
     164        if ((len==0)||(rv2.length()==0)){return;}  
     165         
     166        //find comon rv 
     167        ivec cids=itpp::find(this->findself(rv2)>=0); 
     168         
     169        // index of  
     170        if ( cids.length()>0 ) { 
     171                str str1 = tostr(); 
     172                str str2 = rv2.tostr();  
     173                 
     174                ivec part1; 
     175                ivec part2; 
     176                int i,j; 
     177                // find common rv in strs 
     178                for ( j=0; j < cids.length();j++ ) { 
     179                        i = cids(j); 
     180                        part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) ); 
     181                        part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); 
     182                        selfi = concat ( selfi, part1 ); 
     183                        rv2i = concat ( rv2i, part2 ); 
     184                } 
     185        } 
     186        it_assert_debug(selfi.length() == rv2i.length(),"this should not happen!"); 
    155187} 
    156188 
     
    206238                // find ith rv in common rv 
    207239                rvsinrv ( i ) = mpdfs ( i )->_rv().dataind ( rv ); 
    208                 rvcsinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); 
    209                 rvinrvcs ( i ) = rv.dataind(mpdfs ( i )->_rvc()); 
    210                 //rvcsinrv and rvinrvcs must be 1-to-1 
    211                 it_assert_debug(rvcsinrv ( i ).length()==rvinrvcs(i).length(),"" ); 
     240                // establish link between rv and rvc 
     241                rv.dataind(mpdfs(i)->_rvc(), irv_rvcs(i), irvcs_rv(i)); 
    212242        } 
    213243} 
  • bdm/stat/libBM.h

    r181 r182  
    9292        //! generate \c str from rv, by expanding sizes 
    9393        str tostr() const; 
    94         //! generate indeces into \param crv data vector that form data vector of self. 
     94        //! when this rv is a part of bigger rv, this function returns indeces of self in the data vector of the bigger crv.   
     95        //! Then, data can be copied via: data_of_this = cdata(ind); 
    9596        ivec dataind(const RV &crv) const; 
     97        //! generate mutual indeces when copying data betwenn self and crv.  
     98        //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) 
     99        void dataind(const RV &rv2, ivec &selfi, ivec &rv2i) const; 
    96100 
    97101        //!access function 
     
    178182        } 
    179183        //! Return conditional density on the given RV, the remaining rvs will be in conditioning 
    180         mpdf* condition(const RV &rv){it_warning("Not implemented"); return NULL;} 
     184        virtual mpdf* condition(const RV &rv) const {it_warning("Not implemented"); return NULL;} 
    181185        //! Return marginal density on the given RV, the remainig rvs are intergrated out 
    182         epdf* marginal(const RV &rv){it_warning("Not implemented"); return NULL;} 
     186        epdf* marginal(const RV &rv) const {it_warning("Not implemented"); return NULL;} 
    183187 
    184188        //! return expected value 
     
    248252        //!Default constructor 
    249253        mepdf (epdf &em ) :mpdf ( em._rv(),RV() ) {ep=&em;}; 
     254        void condition(const vec &cond){} 
    250255}; 
    251256 
     
    260265                Array<ivec> rvsinrv; 
    261266                //! Indeces of rvc in common rv 
    262                 Array<ivec> rvcsinrv; 
     267                Array<ivec> irvcs_rv; 
    263268                //! Indeces of common rv in rvc 
    264                 Array<ivec> rvinrvcs; 
     269                Array<ivec> irv_rvcs; 
    265270        public: 
    266                 compositepdf(Array<mpdf*> A0): n(A0.length()), mpdfs(A0), rvsinrv(n), rvcsinrv(n),rvinrvcs(n){}; 
     271                compositepdf(Array<mpdf*> A0): n(A0.length()), mpdfs(A0), rvsinrv(n), irvcs_rv(n),irv_rvcs(n){}; 
    267272                //! find common rv, flag \param checkoverlap modifies whether overlaps are acceptable 
    268273                RV getrv(bool checkoverlap=false); 
  • bdm/stat/libEF.cpp

    r178 r182  
    9595         
    9696        // set mean value 
    97         M= iLsub*L ( xdim,end,0,xdim-1 ); 
     97        M= L ( xdim,end,0,xdim-1 ).T()*iLsub; 
    9898        R= ldR.to_mat()  ; 
    9999} 
     
    207207                N_babies ( j ) ++; 
    208208        } 
    209  
    210209        // We have assigned new babies for each Particle 
    211210        // Now, we fill the resulting index such that: 
  • bdm/stat/libEF.h

    r181 r182  
    130130        double lognc () const; 
    131131        vec mean() const {return mu;} 
    132         mlnorm<sq_T>* condition ( const RV &rvn ); 
    133         enorm<sq_T>* marginal ( const RV &rv ); 
     132        mlnorm<sq_T>* condition ( const RV &rvn ) const ; 
     133        enorm<sq_T>* marginal ( const RV &rv ) const; 
    134134//Access methods 
    135135        //! returns a pointer to the internal mean value. Use with Care! 
     
    384384        //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. 
    385385        void condition (const vec &cond ); 
     386         
     387/*      template<class sq_M> 
     388        friend std::ostream &operator<< ( std::ostream &os, const mlnorm<sq_M> &ml );*/ 
    386389}; 
    387390 
     
    594597 
    595598template<class sq_T> 
    596 enorm<sq_T>* enorm<sq_T>::marginal ( const RV &rvn ) { 
     599enorm<sq_T>* enorm<sq_T>::marginal ( const RV &rvn ) const { 
    597600        ivec irvn = rvn.dataind ( rv ); 
    598601 
     
    604607 
    605608template<class sq_T> 
    606 mlnorm<sq_T>* enorm<sq_T>::condition ( const RV &rvn ) { 
     609mlnorm<sq_T>* enorm<sq_T>::condition ( const RV &rvn ) const { 
    607610 
    608611        RV rvc = rv.subt ( rvn ); 
     
    620623        int end=R.rows()-1; 
    621624        mat S11 = S.get ( 0,n, 0, n ); 
    622         mat S12 = S.get ( rvn.count(), end, 0, n ); 
     625        mat S12 = S.get (0, n , rvn.count(), end ); 
    623626        mat S22 = S.get ( rvn.count(), end, rvn.count(), end ); 
    624627 
     
    636639/////////// 
    637640 
     641// template<class sq_T> 
     642// std::ostream &operator<< ( std::ostream &os, const mlnorm<sq_T> &ml ){ 
     643//      os << "A:"<< ml.A<<endl; 
     644//      os << "mu:"<< ml.mu_const<<endl; 
     645//      os << "R:" << ml.est._R().to_mat() <<endl; 
     646//      return os; 
     647// } 
     648// ; 
    638649 
    639650#endif //EF_H