Changeset 145 for bdm/stat

Show
Ignore:
Timestamp:
08/20/08 15:41:21 (16 years ago)
Author:
smidl
Message:

Oprava dokumentace

Location:
bdm/stat
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/emix.cpp

    r141 r145  
    1818 
    1919        int i=0; 
    20         while ( ( w ( i ) <u0 ) && ( i< ( w.length()-1 ) ) ) {i++;} 
     20        while ( ( cumDist ( i ) <u0 ) && ( i< ( w.length()-1 ) ) ) {i++;} 
    2121 
    2222        return Coms ( i )->sample(); 
  • bdm/stat/emix.h

    r141 r145  
    3030 
    3131*/ 
    32 class emix : public epdf 
    33 { 
     32class emix : public epdf { 
    3433        protected: 
    3534                //! weights of the components 
     
    3938        public: 
    4039                //!Default constructor 
    41                 emix ( RV &rv ) : epdf ( rv ) {}; 
     40                emix(RV &rv) : epdf(rv) {}; 
    4241                //! Set weights \c w and components \c R 
    43                 void set_parameters ( const vec &w, const Array<epdf*> &Coms ); 
     42                void set_parameters(const vec &w, const Array<epdf*> &Coms); 
    4443 
    4544                vec sample() const; 
    46                 vec mean() const 
    47                 { 
    48                         int i; vec mu=zeros ( rv.count() ); 
    49                         for ( i=0;i<w.length();i++ ) {mu+=w ( i ) *Coms ( i )->mean(); } 
     45                vec mean() const { 
     46                        int i; vec mu = zeros(rv.count()); 
     47                        for (i = 0;i < w.length();i++) {mu += w(i) * Coms(i)->mean(); } 
    5048                        return mu; 
    5149                } 
    52                 double evalpdflog ( const vec &val ) const 
    53                 { 
     50                double evalpdflog(const vec &val) const { 
    5451                        int i; 
    55                         double sum=0.0; 
    56                         for ( i=0;i<w.length();i++ ) {sum+=w ( i ) *Coms ( i )->evalpdflog ( val );} 
    57                         return log ( sum ); 
     52                        double sum = 0.0; 
     53                        for (i = 0;i < w.length();i++) {sum += w(i) * Coms(i)->evalpdflog(val);} 
     54                        return log(sum); 
    5855                }; 
    5956 
     
    6562/*! \brief Chain rule decomposition of epdf 
    6663 
     64Probability density in the form of Chain-rule decomposition: 
     65\[ 
     66f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3) 
     67\] 
     68Note that 
     69*/ 
     70class eprod: public epdf { 
     71        protected: 
     72                // 
     73                int n; 
     74                // pointers to epdfs 
     75                Array<epdf*> epdfs; 
     76                Array<mpdf*> mpdfs; 
     77                // 
     78                Array<ivec> rvinds; 
     79                Array<ivec> rvcinds; 
     80                //! Indicate independence of its factors 
     81                bool independent; 
     82                //! Indicate internal creation of mpdfs which must be destroyed 
     83                bool intermpdfs; 
     84        public: 
     85                //!Constructor from list of eFacs or list of mFacs 
     86                eprod(Array<mpdf*> mFacs): epdf(RV()), n(mFacs.length()), epdfs(n), mpdfs(mFacs), rvinds(n), rvcinds(n) { 
     87                        int i; 
     88                        intermpdfs = false; 
     89                        for (i = 0;i < n;i++) { 
     90                                rv.add(mpdfs(i)->_rv()); //add rv to common rvs. 
     91                                epdfs(i) = &(mpdfs(i)->_epdf()); // add pointer to epdf 
     92                        }; 
     93                        independent = true; 
     94                        //test rvc of mpdfs and fill rvinds 
     95                        for (i = 0;i < n;i++) { 
     96                                // find ith rv in common rv 
     97                                rvinds(i) = mpdfs(i)->_rv().dataind(rv); 
     98                                // find ith rvc in common rv 
     99                                rvcinds(i) = mpdfs(i)->_rvc().dataind(rv); 
     100                                if (rvcinds(i).length()>0) {independent = false;} 
     101                        } 
     102 
     103                }; 
     104                eprod(Array<epdf*> eFacs): epdf(RV()), n(eFacs.length()), epdfs(eFacs), mpdfs(n), rvinds(n), rvcinds(n) { 
     105                        int i; 
     106                        intermpdfs = true; 
     107                        for (i = 0;i < n;i++) { 
     108                                if (rv.add(eFacs(i)->_rv())) {it_error("Incompatible eFacs.rv!");} //add rv to common rvs. 
     109                                mpdfs(i) = new mepdf(*(epdfs(i))); 
     110                                epdfs(i) = &(mpdfs(i)->_epdf()); // add pointer to epdf 
     111                        }; 
     112                        independent = true; 
     113                        //test rvc of mpdfs and fill rvinds 
     114                        for (i = 0;i < n;i++) { 
     115                                // find ith rv in common rv 
     116                                rvinds(i) = mpdfs(i)->_rv().dataind(rv); 
     117                        } 
     118                }; 
     119 
     120                double evalpdflog(const vec &val) const { 
     121                        int i; 
     122                        double res = 0.0; 
     123                        for (i = n - 1;i > 0;i++) { 
     124                                if (rvcinds(i).length() > 0) 
     125                                        {mpdfs(i)->condition(val(rvcinds(i)));} 
     126                                // add logarithms 
     127                                res += epdfs(i)->evalpdflog(val(rvinds(i))); 
     128                        } 
     129                } 
     130                vec sample () const{ 
     131                        vec smp=zeros(rv.count()); 
     132                        for (int i = (n - 1);i >= 0;i--) { 
     133                                if (rvcinds(i).length() > 0) 
     134                                        {mpdfs(i)->condition(smp(rvcinds(i)));} 
     135                                set_subvector(smp,rvinds(i), epdfs(i)->sample()); 
     136                        }                        
     137                        return smp; 
     138                } 
     139                vec mean() const{ 
     140                        vec tmp(rv.count()); 
     141                        if (independent) { 
     142                                for (int i=0;i<n;i++) { 
     143                                        vec pom = epdfs(i)->mean(); 
     144                                        set_subvector(tmp,rvinds(i), pom); 
     145                                } 
     146                        } 
     147                        else { 
     148                                int N=50*rv.count(); 
     149                                it_warning("eprod.mean() computed by sampling"); 
     150                                tmp = zeros(rv.count()); 
     151                                for (int i=0;i<N;i++){ tmp += sample();} 
     152                                tmp /=N; 
     153                        } 
     154                        return tmp; 
     155                } 
     156                ~eprod(){if (intermpdfs) {for (int i=0;i<n;i++){delete mpdfs(i);}}}; 
     157}; 
     158 
     159/*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type 
    67160 
    68161*/ 
    69 class eprod: public epdf 
    70 { 
    71         protected: 
    72                 Array<epdf*> epdfs; 
    73                 Array<mpdf*> mpdfs; 
    74         public: 
    75  
    76  
    77 }; 
    78  
    79 /*! \brief Mixture of mpdfs with constant weights 
    80  
    81 */ 
    82 class mmix : public mpdf 
    83 { 
     162class mmix : public mpdf { 
    84163        protected: 
    85164                //! Component (epdfs) 
     
    89168        public: 
    90169                //!Default constructor 
    91                 mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep=&Epdf;}; 
     170                mmix(RV &rv, RV &rvc) : mpdf(rv, rvc), Epdf(rv) {ep = &Epdf;}; 
    92171                //! Set weights \c w and components \c R 
    93                 void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) 
    94                 { 
    95                         Array<epdf*> Eps ( Coms.length() ); 
     172                void set_parameters(const vec &w, const Array<mpdf*> &Coms) { 
     173                        Array<epdf*> Eps(Coms.length()); 
    96174 
    97                         for ( int i=0;i<Coms.length();i++ ) 
    98                         { 
    99                                 Eps ( i ) =& ( Coms ( i )->_epdf() ); 
     175                        for (int i = 0;i < Coms.length();i++) { 
     176                                Eps(i) = & (Coms(i)->_epdf()); 
    100177                        } 
    101                         Epdf.set_parameters ( w,Eps ); 
     178                        Epdf.set_parameters(w, Eps); 
    102179                }; 
    103180 
    104                 void condition ( const vec &cond ) 
    105                 { 
    106                         for ( int i=0;i<Coms.length();i++ ) {Coms ( i )->condition ( cond );} 
     181                void condition(const vec &cond) { 
     182                        for (int i = 0;i < Coms.length();i++) {Coms(i)->condition(cond);} 
    107183                }; 
    108184}; 
  • bdm/stat/libBM.cpp

    r102 r145  
    2525        times = in_times; 
    2626        tsize = 0; 
    27         for ( i=0;i<len;i++ ) {tsize+=sizes ( i );} 
     27        for ( i = 0;i < len;i++ ) {tsize += sizes ( i );} 
    2828}; 
    2929 
     
    3232} 
    3333 
    34 RV::RV () : tsize ( 0 ),len ( 0 ) {}; 
     34RV::RV() : tsize ( 0 ), len ( 0 ) {}; 
    3535 
    36 void RV::add ( const RV &rv2 ) { 
     36bool RV::add ( const RV &rv2 ) { 
    3737        // TODO 
    38         tsize+=rv2.tsize; 
    39         len +=rv2.len; 
    40         ids=concat ( ids,rv2.ids ); 
    41         sizes=concat ( sizes,rv2.sizes ); 
    42         times=concat ( times,rv2.times ); 
    43         names=concat ( names,rv2.names ); 
     38        ivec ind = rv2.findself ( *this ); //should be -1 all the time 
     39        ivec index = itpp::find(ind==-1);  
     40         
     41        if ( index.length() < rv2.len ) { //conflict 
     42                ids = concat ( ids, rv2.ids(index) ); 
     43                sizes = concat ( sizes, rv2.sizes(index) ); 
     44                times = concat ( times, rv2.times(index) ); 
     45                names = concat ( names, rv2.names(to_Arr(index)) ); 
     46        } 
     47        else { 
     48                ids = concat ( ids, rv2.ids ); 
     49                sizes = concat ( sizes, rv2.sizes ); 
     50                times = concat ( times, rv2.times ); 
     51                names = concat ( names, rv2.names ); 
     52        } 
     53        tsize = sum(sizes); 
     54        len = ids.length(); 
     55        return (index.length()<rv2.len); 
     56 
    4457//      return *this; 
    4558}; 
     
    5871} 
    5972 
    60 RV RV::subselect ( ivec ind ) { 
     73RV RV::subselect ( ivec ind ) const { 
    6174        return RV ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 
    6275} 
    6376 
    64 void RV::t ( int delta ) { times +=delta;} 
     77void RV::t ( int delta ) { times += delta;} 
    6578 
    66 RV RV::operator() ( ivec ind ) { 
     79RV RV::operator() ( ivec ind ) const { 
    6780        return RV ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 
    6881} 
    6982 
    70 bool RV::equal ( RV rv2 ) const { 
    71         return ( ids==rv2.ids ) && ( times == rv2.times ) && ( sizes==rv2.sizes ); 
     83bool RV::equal ( const RV &rv2 ) const { 
     84        return ( ids == rv2.ids ) && ( times == rv2.times ) && ( sizes == rv2.sizes ); 
    7285} 
    7386 
    7487mat epdf::sampleN ( int N ) const { 
    75         mat X =zeros ( rv.count(),N ); 
    76         for ( int i=0;i<N;i++ ) X.set_col ( i,this->sample() );  
     88        mat X = zeros ( rv.count(), N ); 
     89        for ( int i = 0;i < N;i++ ) X.set_col ( i, this->sample() ); 
    7790        return X; 
    7891}; 
     
    88101} 
    89102 
    90 ivec RV::indexlist() { 
    91         ivec indlist ( tsize ); 
     103str RV::tostr() const { 
     104        ivec idlist ( tsize ); 
     105        ivec tmlist ( tsize ); 
    92106        int i; 
    93107        int pos = 0; 
    94         for ( i=0;i<len;i++ ) { 
    95                 indlist.set_subvector ( pos,pos+sizes ( i )-1, ids ( i ) ); 
     108        for ( i = 0;i < len;i++ ) { 
     109                idlist.set_subvector ( pos, pos + sizes ( i ) - 1, ids ( i ) ); 
     110                tmlist.set_subvector ( pos, pos + sizes ( i ) - 1, times ( i ) ); 
     111                pos += sizes ( i ); 
    96112        } 
    97         return indlist; 
     113        return str ( idlist, tmlist ); 
     114} 
     115 
     116ivec RV::dataind ( RV rv2 ) const { 
     117        str str2 = rv2.tostr(); 
     118        ivec res ( 0 ); 
     119        ivec part; 
     120        int i; 
     121        for ( i = 0;i < len;i++ ) { 
     122                part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); 
     123                res = concat ( res, part ); 
     124        } 
     125        return res; 
     126} 
     127 
     128RV RV::subt ( const RV rv2 ) const { 
     129        ivec res = this->findself ( rv2 ); // nonzeros 
     130        ivec valid = itpp::find ( res == -1 ); //-1 => value not found => it remains 
     131        return ( *this ) ( valid ); //keep those that were not found in rv2 
     132} 
     133 
     134ivec RV::findself ( const RV &rv2 ) const { 
     135        int i, j; 
     136        ivec tmp = -ones_i ( len ); 
     137        for ( i = 0;i < len;i++ ) { 
     138                for ( j = 0;j < rv2.length();j++ ) { 
     139                        if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) { 
     140                                tmp ( i ) = j; 
     141                                break; 
     142                        } 
     143                } 
     144        } 
     145        return tmp; 
    98146} 
    99147 
  • bdm/stat/libBM.h

    r118 r145  
    1818 
    1919using namespace itpp; 
     20 
     21//! Structure of RV (used internally) 
     22class str{ 
     23public: 
     24        ivec ids; 
     25        ivec times; 
     26        str(ivec ids0, ivec times0):ids(ids0),times(times0){ 
     27                it_assert_debug(times0.length()==ids0.length(),"Incompatible input"); 
     28        }; 
     29}; 
    2030 
    2131/*! 
     
    6171        //TODO why not inline and later?? 
    6272 
    63         //! Find indexes of another rv in self 
    64         ivec find ( RV rv2 ); 
     73        //! Find indexes of self in another rv, \return ivec of the same size as self. 
     74        ivec findself (const RV &rv2 ) const; 
    6575        //! Compare if \c rv2 is identical to this \c RV 
    66         bool equal ( RV rv2 ) const; 
    67         //! Add (concat) another variable to the current one 
    68         void add ( const RV &rv2 ); 
    69         //! Add (concat) another variable to the current one 
    70         friend RV concat ( const RV &rv1, const RV &rv2 ); 
     76        bool equal (const RV &rv2 ) const; 
     77        //! Add (concat) another variable to the current one, \return 0 if all rv2 were added, 1 if rv2 is in conflict 
     78        bool add ( const RV &rv2 ); 
    7179        //! Subtract  another variable from the current one 
    72         RV subt ( RV rv2 ); 
     80        RV subt ( const RV rv2 ) const; 
    7381        //! Select only variables at indeces ind 
    74         RV subselect ( ivec ind ); 
     82        RV subselect ( ivec ind ) const; 
    7583        //! Select only variables at indeces ind 
    76         RV operator() ( ivec ind ); 
    77         //! Generate new \c RV with \c time shifted by delta. 
     84        RV operator() ( ivec ind ) const; 
     85        //! Shift \c time shifted by delta. 
    7886        void t ( int delta ); 
    79         //! generate a list of indeces, i.e. which 
    80         ivec indexlist(); 
     87        //! generate \c str from rv, by expanding sizes 
     88        str tostr() const; 
     89        //! generate indeces into \param crv data vector that form data vector of self. 
     90        ivec dataind(RV crv) const; 
    8191 
    8292        //!access function 
     
    92102        std::string name ( int at ) {return names ( at );}; 
    93103}; 
     104 
     105//! Concat two random variables 
     106RV concat ( const RV &rv1, const RV &rv2 ); 
    94107 
    95108 
     
    146159        //! Destructor for future use; 
    147160        virtual ~epdf() {}; 
    148         //! access function 
    149         RV _rv() const {return rv;} 
     161        //! access function, possibly dangerous! 
     162        RV& _rv() {return rv;} 
    150163}; 
    151164 
     
    170183        vec temp= ep->sample(); 
    171184        ll=ep->evalpdflog ( temp );return temp;}; 
    172         //! 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. 
     185        //! Returns \param 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. 
    173186        virtual mat samplecond ( vec &cond, vec &ll, int N ) { 
    174187                this->condition ( cond ); 
     
    190203        //! access function 
    191204        RV _rvc() {return rvc;} 
     205        //! access function 
     206        RV _rv() {return rv;} 
    192207        //!access function 
    193208        epdf& _epdf() {return *ep;} 
     
    201216public: 
    202217        //!Default constructor 
    203         mepdf ( const RV &rv, const RV &rvc, epdf* em ) :mpdf ( rv,rvc ) {ep=em;}; 
     218        mepdf (epdf &em ) :mpdf ( em._rv(),RV() ) {ep=&em;}; 
    204219}; 
    205220 
  • bdm/stat/loggers.h

    r102 r145  
    8484        void step(bool final=false) {if ( ind<maxlen ) ind++; else it_error ( "memlog::ind is too high;" );} 
    8585        void logit ( int id, vec v ) {vectors ( id ).set_row ( ind,v );} 
     86        //! Save values into an itfile named after \c fname. 
    8687        void itsave(const char* fname); 
    8788};