Show
Ignore:
Timestamp:
08/05/09 14:40:03 (15 years ago)
Author:
mido
Message:

panove, vite, jak jsem peclivej na upravu kodu.. snad se vam bude libit:) konfigurace je v souboru /system/astylerc

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/estim/particles.h

    r461 r477  
    5555        //! \name Constructors 
    5656        //!@{ 
    57         PF ( ) :est(), _w ( est._w() ),_samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) {LIDs.set_size ( 5 );}; 
     57        PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) { 
     58                LIDs.set_size ( 5 ); 
     59        }; 
    5860        /*      PF ( mpdf *par0, mpdf *obs0, epdf *epdf0, int n0 ) : 
    5961                                est ( ),_w ( est._w() ),_samples ( est._samples() ),opt_L_smp(false), opt_L_wei(false) 
    6062                { set_parameters ( par0,obs0,n0 ); set_statistics ( ones ( n0 ),epdf0 ); };*/ 
    61         void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm=SYSTEMATIC ) 
    62         { par = par0; obs=obs0; n=n0; resmethod= rm;}; 
    63         void set_statistics ( const vec w0, epdf *epdf0 ) {est.set_statistics ( w0,epdf0 );}; 
     63        void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) { 
     64                par = par0; 
     65                obs = obs0; 
     66                n = n0; 
     67                resmethod = rm; 
     68        }; 
     69        void set_statistics ( const vec w0, epdf *epdf0 ) { 
     70                est.set_statistics ( w0, epdf0 ); 
     71        }; 
    6472        //!@} 
    6573        //! Set posterior density by sampling from epdf0 
    6674//      void set_est ( const epdf &epdf0 ); 
    6775        void set_options ( const string &opt ) { 
    68                 BM::set_options(opt); 
    69                 opt_L_wei= ( opt.find ( "logweights" ) !=string::npos ); 
    70                 opt_L_smp= ( opt.find ( "logsamples" ) !=string::npos ); 
     76                BM::set_options ( opt ); 
     77                opt_L_wei = ( opt.find ( "logweights" ) != string::npos ); 
     78                opt_L_smp = ( opt.find ( "logsamples" ) != string::npos ); 
    7179        } 
    7280        void bayes ( const vec &dt ); 
    7381        //!access function 
    74         vec* __w() {return &_w;} 
     82        vec* __w() { 
     83                return &_w; 
     84        } 
    7585}; 
    7686 
     
    8797        //! internal class for MPDF providing composition of eEmp with external components 
    8898 
    89 class mpfepdf : public epdf  { 
     99        class mpfepdf : public epdf  { 
    90100        protected: 
    91101                eEmp &E; 
     
    99109                //! read statistics from MPF 
    100110                void read_statistics ( Array<BM_T*> &A ) { 
    101                         dim = E.dimension() +A ( 0 )->posterior().dimension(); 
    102                         for ( int i=0; i<_w.length() ;i++ ) {Coms ( i ) = A ( i )->_e();} 
     111                        dim = E.dimension() + A ( 0 )->posterior().dimension(); 
     112                        for ( int i = 0; i < _w.length() ; i++ ) { 
     113                                Coms ( i ) = A ( i )->_e(); 
     114                        } 
    103115                } 
    104116                //! needed in resampling 
    105                 void set_elements ( int &i, double wi, const epdf* ep ) 
    106                 {_w ( i ) =wi; Coms ( i ) =ep;}; 
     117                void set_elements ( int &i, double wi, const epdf* ep ) { 
     118                        _w ( i ) = wi; 
     119                        Coms ( i ) = ep; 
     120                }; 
    107121 
    108122                void set_parameters ( int n ) { 
     
    112126                vec mean() const { 
    113127                        // ugly 
    114                         vec pom=zeros ( Coms ( 0 )->dimension() ); 
    115                         for ( int i=0; i<_w.length(); i++ ) {pom += Coms ( i )->mean() * _w ( i );} 
    116                         return concat ( E.mean(),pom ); 
     128                        vec pom = zeros ( Coms ( 0 )->dimension() ); 
     129                        for ( int i = 0; i < _w.length(); i++ ) { 
     130                                pom += Coms ( i )->mean() * _w ( i ); 
     131                        } 
     132                        return concat ( E.mean(), pom ); 
    117133                } 
    118134                vec variance() const { 
    119135                        // ugly 
    120                         vec pom=zeros ( Coms ( 0 )->dimension() ); 
    121                         vec pom2=zeros ( Coms ( 0 )->dimension() ); 
    122                         for ( int i=0; i<_w.length(); i++ ) { 
     136                        vec pom = zeros ( Coms ( 0 )->dimension() ); 
     137                        vec pom2 = zeros ( Coms ( 0 )->dimension() ); 
     138                        for ( int i = 0; i < _w.length(); i++ ) { 
    123139                                pom += Coms ( i )->mean() * _w ( i ); 
    124                                 pom2 += ( Coms ( i )->variance() + pow ( Coms ( i )->mean(),2 ) ) * _w ( i ); 
    125                         } 
    126                         return concat ( E.variance(),pom2-pow ( pom,2 ) ); 
    127                 } 
    128                 void qbounds ( vec &lb, vec &ub, double perc=0.95 ) const { 
     140                                pom2 += ( Coms ( i )->variance() + pow ( Coms ( i )->mean(), 2 ) ) * _w ( i ); 
     141                        } 
     142                        return concat ( E.variance(), pom2 - pow ( pom, 2 ) ); 
     143                } 
     144                void qbounds ( vec &lb, vec &ub, double perc = 0.95 ) const { 
    129145                        //bounds on particles 
    130146                        vec lbp; 
    131147                        vec ubp; 
    132                         E.qbounds ( lbp,ubp ); 
     148                        E.qbounds ( lbp, ubp ); 
    133149 
    134150                        //bounds on Components 
    135                         int dimC=Coms ( 0 )->dimension(); 
     151                        int dimC = Coms ( 0 )->dimension(); 
    136152                        int j; 
    137153                        // temporary 
    138                         vec lbc(dimC); 
    139                         vec ubc(dimC); 
     154                        vec lbc ( dimC ); 
     155                        vec ubc ( dimC ); 
    140156                        // minima and maxima 
    141                         vec Lbc(dimC); 
    142                         vec Ubc(dimC); 
     157                        vec Lbc ( dimC ); 
     158                        vec Ubc ( dimC ); 
    143159                        Lbc = std::numeric_limits<double>::infinity(); 
    144160                        Ubc = -std::numeric_limits<double>::infinity(); 
    145161 
    146                         for ( int i=0;i<_w.length();i++ ) { 
     162                        for ( int i = 0; i < _w.length(); i++ ) { 
    147163                                // check Coms 
    148                                 Coms ( i )->qbounds ( lbc,ubc ); 
    149                                 for ( j=0;j<dimC; j++ ) { 
    150                                         if ( lbc ( j ) <Lbc ( j ) ) {Lbc ( j ) =lbc ( j );} 
    151                                         if ( ubc ( j ) >Ubc ( j ) ) {Ubc ( j ) =ubc ( j );} 
     164                                Coms ( i )->qbounds ( lbc, ubc ); 
     165                                for ( j = 0; j < dimC; j++ ) { 
     166                                        if ( lbc ( j ) < Lbc ( j ) ) { 
     167                                                Lbc ( j ) = lbc ( j ); 
     168                                        } 
     169                                        if ( ubc ( j ) > Ubc ( j ) ) { 
     170                                                Ubc ( j ) = ubc ( j ); 
     171                                        } 
    152172                                } 
    153173                        } 
    154                         lb=concat(lbp,Lbc); 
    155                         ub=concat(ubp,Ubc); 
    156                 } 
    157  
    158                 vec sample() const {it_error ( "Not implemented" );return 0;} 
    159  
    160                 double evallog ( const vec &val ) const {it_error ( "not implemented" ); return 0.0;} 
     174                        lb = concat ( lbp, Lbc ); 
     175                        ub = concat ( ubp, Ubc ); 
     176                } 
     177 
     178                vec sample() const { 
     179                        it_error ( "Not implemented" ); 
     180                        return 0; 
     181                } 
     182 
     183                double evallog ( const vec &val ) const { 
     184                        it_error ( "not implemented" ); 
     185                        return 0.0; 
     186                } 
    161187        }; 
    162188 
     
    170196        //! Default constructor. 
    171197        MPF () : PF (), jest ( est ) {}; 
    172         void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm=SYSTEMATIC ) { 
     198        void set_parameters ( mpdf *par0, mpdf *obs0, int n0, RESAMPLING_METHOD rm = SYSTEMATIC ) { 
    173199                PF::set_parameters ( par0, obs0, n0, rm ); 
    174200                jest.set_parameters ( n0 );//duplication of rm 
     
    177203        void set_statistics ( epdf *epdf0, const BM_T* BMcond0 ) { 
    178204 
    179                 PF::set_statistics ( ones ( n ) /n, epdf0 ); 
     205                PF::set_statistics ( ones ( n ) / n, epdf0 ); 
    180206                // copy 
    181                 for ( int i=0;i<n;i++ ) { BMs ( i ) = new BM_T ( *BMcond0 ); BMs ( i )->condition ( _samples ( i ) );} 
     207                for ( int i = 0; i < n; i++ ) { 
     208                        BMs ( i ) = new BM_T ( *BMcond0 ); 
     209                        BMs ( i )->condition ( _samples ( i ) ); 
     210                } 
    182211 
    183212                jest.read_statistics ( BMs ); 
     
    186215 
    187216        void bayes ( const vec &dt ); 
    188         const epdf& posterior() const {return jest;} 
    189         const epdf* _e() const {return &jest;} //Fixme: is it useful? 
     217        const epdf& posterior() const { 
     218                return jest; 
     219        } 
     220        const epdf* _e() const { 
     221                return &jest;    //Fixme: is it useful? 
     222        } 
    190223        //! Set postrior of \c rvc to samples from epdf0. Statistics of BMs are not re-computed! Use only for initialization! 
    191224        /*      void set_est ( const epdf& epdf0 ) { 
     
    197230        void set_options ( const string &opt ) { 
    198231                PF::set_options ( opt ); 
    199                 opt_L_mea = ( opt.find ( "logmeans" ) !=string::npos ); 
     232                opt_L_mea = ( opt.find ( "logmeans" ) != string::npos ); 
    200233        } 
    201234 
    202235        //!Access function 
    203         BM* _BM ( int i ) {return BMs ( i );} 
     236        BM* _BM ( int i ) { 
     237                return BMs ( i ); 
     238        } 
    204239}; 
    205240 
     
    210245        vec llsP ( n ); 
    211246        ivec ind; 
    212         double mlls=-std::numeric_limits<double>::infinity(); 
     247        double mlls = -std::numeric_limits<double>::infinity(); 
    213248 
    214249#pragma omp parallel for 
    215         for ( i=0;i<n;i++ ) { 
     250        for ( i = 0; i < n; i++ ) { 
    216251                //generate new samples from paramater evolution model; 
    217252                _samples ( i ) = par->samplecond ( _samples ( i ) ); 
     
    220255                BMs ( i )->bayes ( dt ); 
    221256                lls ( i ) = BMs ( i )->_ll(); // lls above is also in proposal her must be lls(i) =, not +=!! 
    222                 if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability) 
    223         } 
    224  
    225         double sum_w=0.0; 
     257                if ( lls ( i ) > mlls ) mlls = lls ( i ); //find maximum likelihood (for numerical stability) 
     258        } 
     259 
     260        double sum_w = 0.0; 
    226261        // compute weights 
    227262#pragma omp parallel for 
    228         for ( i=0;i<n;i++ ) { 
     263        for ( i = 0; i < n; i++ ) { 
    229264                _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood 
    230                 sum_w+=_w ( i ); 
    231         } 
    232  
    233         if ( sum_w  >0.0 ) { 
    234                 _w /=sum_w; //? 
    235         } 
    236         else { 
    237                 cout<<"sum(w)==0"<<endl; 
    238         } 
    239  
    240  
    241         double eff = 1.0/ ( _w*_w ); 
     265                sum_w += _w ( i ); 
     266        } 
     267 
     268        if ( sum_w  > 0.0 ) { 
     269                _w /= sum_w; //? 
     270        } else { 
     271                cout << "sum(w)==0" << endl; 
     272        } 
     273 
     274 
     275        double eff = 1.0 / ( _w * _w ); 
    242276        if ( eff < ( 0.3*n ) ) { 
    243277                ind = est.resample ( resmethod ); 
     
    245279 
    246280#pragma omp parallel for 
    247                 for ( i=0;i<n;i++ ) { 
    248                         if ( ind ( i ) !=i ) {//replace the current Bm by a new one 
     281                for ( i = 0; i < n; i++ ) { 
     282                        if ( ind ( i ) != i ) {//replace the current Bm by a new one 
    249283                                //fixme this would require new assignment operator 
    250284                                // *Bms[i] = *Bms[ind ( i ) ]; 
     
    254288                                delete BMs ( i ); 
    255289                                BMs ( i ) = new BM_T ( *BMs ( ind ( i ) ) ); //copy constructor 
    256                                 const epdf& pom=BMs ( i )->posterior(); 
    257                                 jest.set_elements ( i,1.0/n,&pom ); 
     290                                const epdf& pom = BMs ( i )->posterior(); 
     291                                jest.set_elements ( i, 1.0 / n, &pom ); 
    258292                        } 
    259293                };