Changeset 175

Show
Ignore:
Timestamp:
10/09/08 11:32:11 (16 years ago)
Author:
smidl
Message:

mprod is now based on compositepdf

Location:
bdm/stat
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/emix.cpp

    r168 r175  
    2323} 
    2424 
    25 mprod::mprod ( Array<mpdf*> mFacs, bool overlap) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), rvcinds ( n ) { 
    26                 int i; 
    27                 bool rvaddok; 
    28                 // Create rv 
    29                 for ( i = 0;i < n;i++ ) { 
    30                         rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 
    31                         // If rvaddok==false, mpdfs overlap => assert error. 
    32                         it_assert_debug(rvaddok||overlap,"mprod::mprod() input mpdfs overlap in rv!"); 
    33                         epdfs ( i ) = & ( mpdfs ( i )->_epdf() ); // add pointer to epdf 
    34                 }; 
    35                 // Create rvc 
    36                 for ( i = 0;i < n;i++ ) { 
    37                         rvc.add ( mpdfs ( i )->_rvc().subt ( rv ) ); //add rv to common rvs. 
    38                 }; 
    39                  
    40 //              independent = true; 
    41                 //test rvc of mpdfs and fill rvinds 
    42                 for ( i = 0;i < n;i++ ) { 
    43                         // find ith rv in common rv 
    44                         rvinds ( i ) = mpdfs ( i )->_rv().dataind ( rv ); 
    45                         // find ith rvc in common rv 
    46                         rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); 
    47                         // find ith rvc in common rv 
    48                         rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 
    49                         // 
    50 /*                      if ( rvcinrv ( i ).length() >0 ) {independent = false;} 
    51                         if ( rvcinds ( i ).length() >0 ) {independent = false;}*/ 
    52                 } 
    53         }; 
     25// mprod::mprod ( Array<mpdf*> mFacs, bool overlap) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), rvcinds ( n ) { 
     26//              int i; 
     27//              bool rvaddok; 
     28//              // Create rv 
     29//              for ( i = 0;i < n;i++ ) { 
     30//                      rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 
     31//                      // If rvaddok==false, mpdfs overlap => assert error. 
     32//                      it_assert_debug(rvaddok||overlap,"mprod::mprod() input mpdfs overlap in rv!"); 
     33//                      epdfs ( i ) = & ( mpdfs ( i )->_epdf() ); // add pointer to epdf 
     34//              }; 
     35//              // Create rvc 
     36//              for ( i = 0;i < n;i++ ) { 
     37//                      rvc.add ( mpdfs ( i )->_rvc().subt ( rv ) ); //add rv to common rvs. 
     38//              }; 
     39//               
     40// //           independent = true; 
     41//              //test rvc of mpdfs and fill rvinds 
     42//              for ( i = 0;i < n;i++ ) { 
     43//                      // find ith rv in common rv 
     44//                      rvsinrv ( i ) = mpdfs ( i )->_rv().dataind ( rv ); 
     45//                      // find ith rvc in common rv 
     46//                      rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); 
     47//                      // find ith rvc in common rv 
     48//                      rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 
     49//                      // 
     50// /*                   if ( rvcinrv ( i ).length() >0 ) {independent = false;} 
     51//                      if ( rvcinds ( i ).length() >0 ) {independent = false;}*/ 
     52//              } 
     53//      }; 
  • bdm/stat/emix.h

    r170 r175  
    6868Note that 
    6969*/ 
    70 class mprod: public mpdf { 
    71 protected: 
    72         // 
    73         int n; 
     70class mprod: public compositepdf, public mpdf { 
     71protected: 
    7472        // pointers to epdfs 
    7573        Array<epdf*> epdfs; 
    76         Array<mpdf*> mpdfs; 
    77         // 
    78         //! Indeces of rvs in common rv 
    79         Array<ivec> rvinds; 
    80         //! Indeces of rvc in common rv 
    81         Array<ivec> rvcinrv; 
    8274        //! Indeces of rvc in common rvc 
    8375        Array<ivec> rvcinds; 
    84 //      //! Indicate independence of its factors 
    85 //      bool independent; 
    86 //      //! Indicate internal creation of mpdfs which must be destroyed 
    87 //      bool intermpdfs; 
    8876public: 
    8977        /*!\brief Constructor from list of mFacs, 
    9078        Additional parameter overlap is left for future use. Do not set to true for mprod. 
    9179        */ 
    92         mprod ( Array<mpdf*> mFacs, bool overlap=false ); 
     80        mprod ( Array<mpdf*> mFacs): compositepdf(mFacs), mpdf(getrv(true),RV()), rvcinds(n), epdfs(n) 
     81        {       setrvc(rv,rvc); 
     82                setrvcinrv(rvc,rvcinds); 
     83                setindices(rv);  
     84        for(int i=0;i<n;i++){epdfs(i)=&(mpdfs(i)->_epdf());} 
     85        }; 
    9386 
    9487        double evalpdflog ( const vec &val ) const { 
     
    9992                                {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );} 
    10093                        // add logarithms 
    101                         res += epdfs ( i )->evalpdflog ( val ( rvinds ( i ) ) ); 
     94                        res += epdfs ( i )->evalpdflog ( val ( rvsinrv ( i ) ) ); 
    10295                } 
    10396                return res; 
     
    110103                for ( int i = ( n - 1 );i >= 0;i-- ) { 
    111104                        if ( rvcinds ( i ).length() > 0 ) { 
    112                                 condi = zeros ( rvcinrv.length() + rvcinds.length() ); 
     105                                condi = zeros ( rvcsinrv.length() + rvcinds.length() ); 
    113106                                // copy data in condition 
    114107                                set_subvector ( condi,rvcinds ( i ), cond ); 
    115108                                // copy data in already generated sample 
    116                                 set_subvector ( condi,rvcinrv ( i ), smp ); 
     109                                set_subvector ( condi,rvcsinrv ( i ), smp ); 
    117110 
    118111                                mpdfs ( i )->condition ( condi ); 
     
    120113                        smpi = epdfs ( i )->sample(); 
    121114                        // copy contribution of this pdf into smp 
    122                         set_subvector ( smp,rvinds ( i ), smpi ); 
     115                        set_subvector ( smp,rvsinrv ( i ), smpi ); 
    123116                        // add ith likelihood contribution 
    124117                        ll+=epdfs ( i )->evalpdflog ( smpi ); 
  • bdm/stat/libBM.cpp

    r170 r175  
    183183void RV::newids(){ids=linspace ( RVcounter+1, RVcounter+len ),RVcounter+=len;} 
    184184 
     185RV compositepdf::getrv(bool checkoverlap){ 
     186        RV rv; //empty rv 
     187        bool rvaddok; 
     188        for (int i = 0;i < n;i++ ) { 
     189                rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 
     190                        // If rvaddok==false, mpdfs overlap => assert error. 
     191                it_assert_debug(rvaddok||(!checkoverlap),"mprod::mprod() input mpdfs overlap in rv!"); 
     192        }; 
     193        return rv; 
     194} 
     195 
     196void compositepdf::setrvc(const RV &rv, RV &rvc){ 
     197        for (int i = 0;i < n;i++ ) { 
     198                rvc.add ( mpdfs ( i )->_rvc().subt ( rv ) ); //add rv to common rvc 
     199        }; 
     200} 
     201 
     202void compositepdf::setindices(const RV &rv){ 
     203        for (int i = 0;i < n;i++ ) { 
     204                        // find ith rv in common rv 
     205                rvsinrv ( i ) = mpdfs ( i )->_rv().dataind ( rv ); 
     206                rvcsinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); 
     207        } 
     208} 
     209 
     210void compositepdf::setrvcinrv(const RV &rvc, Array<ivec> &rvinds){ 
     211        for (int i = 0;i < n;i++ ) { 
     212                        // find ith rv in common rv 
     213                rvinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 
     214        } 
     215} 
     216 
     217 
    185218void BM::bayesB(const mat &Data){ 
    186219        for(int t=0;t<Data.cols();t++){bayes(Data.get_col(t));} 
  • bdm/stat/libBM.h

    r170 r175  
    233233}; 
    234234 
     235//!\brief Abstract composition of pdfs, a base for specific classes  
     236class compositepdf{ 
     237        protected: 
     238                //!Number of mpdfs in the composite 
     239                int n; 
     240                //! Elements of composition 
     241                Array<mpdf*> mpdfs; 
     242                //! Indeces of rvs in common rv 
     243                Array<ivec> rvsinrv; 
     244                //! Indeces of rvc in common rv 
     245                Array<ivec> rvcsinrv; 
     246        public: 
     247                compositepdf(Array<mpdf*> A0): n(A0.length()), mpdfs(A0), rvsinrv(n), rvcsinrv(n){}; 
     248                RV getrv(bool checkoverlap=false); 
     249                void setrvc(const RV &rv, RV &rvc); 
     250                void setindices(const RV &rv); 
     251                void setrvcinrv(const RV &rvc, Array<ivec> &rvcind); 
     252}; 
     253 
    235254/*! \brief Abstract class for discrete-time sources of data. 
    236255