36 | | //! Returns indexes of particles that should be resampled. The ordering MUST guarantee inplace replacement. (Important for MPF.) |
37 | | ivec resample(RESAMPLING_METHOD method = SYSTEMATIC); |
38 | | PF (vec w); |
| 43 | PF ( const RV &rv0, mpdf &par0, mpdf &obs0, int n0 ) :BM ( rv0 ), |
| 44 | n ( n0 ),est ( rv0,n ),_w ( est._w() ),_samples ( est._samples() ), |
| 45 | par ( par0 ), obs ( obs0 ) {}; |
| 46 | |
| 47 | // void set_parametres(mpdf &par0, mpdf &obs0) {par=&par0;obs=&obs0;}; |
| 48 | void set_est ( const epdf &epdf0 ); |
59 | | TrivialPF(mpdf &par, mpdf &obs, BM &prop, int n0); |
60 | | TrivialPF(mpdf &par, mpdf &obs, int n0); |
61 | | void bayes(const vec &dt, bool evalll); |
| 72 | mpfepdf ( eEmp &E0, const RV &rvc ) : |
| 73 | epdf ( RV( ) ), E ( E0 ), _w ( E._w() ), |
| 74 | Coms ( _w.length() ) { |
| 75 | rv.add ( E._rv() ); |
| 76 | rv.add ( rvc ); |
| 77 | }; |
| 78 | |
| 79 | void set_elements ( int &i, double wi, epdf* ep ) |
| 80 | {_w ( i ) =wi; Coms ( i ) =ep;}; |
| 81 | |
| 82 | vec mean() const { |
| 83 | // ugly |
| 84 | vec pom=zeros ( ( Coms ( 0 )->_rv() ).count() ); |
| 85 | |
| 86 | for ( int i=0; i<_w.length(); i++ ) {pom += Coms ( i )->mean() * _w ( i );} |
| 87 | |
| 88 | return concat ( E.mean(),pom ); |
| 89 | } |
| 90 | |
| 91 | vec sample() const {it_error ( "Not implemented" );return 0;} |
| 92 | |
| 93 | double evalpdflog ( const vec &val ) const {it_error ( "not implemented" ); return 0.0;} |
| 94 | }; |
| 95 | |
| 96 | //! estimate joining PF.est with conditional |
| 97 | mpfepdf jest; |
| 98 | |
| 99 | public: |
| 100 | //! Default constructor. |
| 101 | MPF ( const RV &rvlin, const RV &rvpf, mpdf &par0, mpdf &obs0, int n, const BM_T &BMcond0 ) : PF ( rvpf ,par0,obs0,n ),jest ( est,rvlin ) { |
| 102 | // |
| 103 | //TODO test if rv and BMcond.rv are compatible. |
| 104 | rv.add ( rvlin ); |
| 105 | // |
| 106 | |
| 107 | if ( n>1000 ) it_error ( "increase 1000 here!" ); |
| 108 | |
| 109 | for ( int i=0;i<n;i++ ) { |
| 110 | Bms[i] = new BM_T ( BMcond0 ); //copy constructor |
| 111 | epdf& pom=Bms[i]->_epdf(); |
| 112 | jest.set_elements ( i,1.0/n,&pom ); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | ~MPF() { |
| 117 | } |
| 118 | |
| 119 | void bayes ( const vec &dt ); |
| 120 | epdf& _epdf() {return jest;} |
| 121 | |
| 122 | void set_est ( const epdf& epdf0 ) { |
| 123 | PF::set_est ( epdf0 ); // sample params in condition |
| 124 | // copy conditions to BMs |
| 125 | |
| 126 | for ( int i=0;i<n;i++ ) {Bms[i]->condition ( _samples ( i ) );} |
| 127 | } |
64 | | class MPF : public TrivialPF { |
65 | | Array<BM> Bms; |
66 | | public: |
67 | | MPF(BM &B, BM &prop, mpdf &obs, mpdf &par); |
68 | | void bayes(vec &dt); |
69 | | }; |
| 130 | template<class BM_T> |
| 131 | void MPF<BM_T>::bayes ( const vec &dt ) { |
| 132 | int i; |
| 133 | vec lls ( n ); |
| 134 | ivec ind; |
| 135 | double mlls=-std::numeric_limits<double>::infinity(); |
| 136 | double sum=0.0; |
| 137 | |
| 138 | for ( i=0;i<n;i++ ) { |
| 139 | //generate new samples from paramater evolution model; |
| 140 | _samples ( i ) = par.samplecond ( _samples ( i ), lls ( i ) ); |
| 141 | Bms[i]->condition ( _samples ( i ) ); |
| 142 | Bms[i]->bayes ( dt ); |
| 143 | lls ( i ) += Bms[i]->_ll(); |
| 144 | |
| 145 | if ( lls ( i ) >mlls ) mlls=lls ( i ); //find maximum likelihood (for numerical stability) |
| 146 | } |
| 147 | |
| 148 | // compute weights |
| 149 | for ( i=0;i<n;i++ ) { |
| 150 | _w ( i ) *= exp ( lls ( i ) - mlls ); // multiply w by likelihood |
| 151 | } |
| 152 | |
| 153 | //renormalize |
| 154 | for ( i=0;i<n;i++ ) {sum+=_w ( i );}; |
| 155 | |
| 156 | _w /=sum; //? |
| 157 | |
| 158 | if ( ( _w*_w ) < ( 0.5*n ) ) { |
| 159 | ind = est.resample(); |
| 160 | // Resample Bms! |
| 161 | |
| 162 | for ( i=0;i<n;i++ ) { |
| 163 | if ( ind ( i ) !=i ) {//replace the current Bm by a new one |
| 164 | //fixme this would require new assignment operator |
| 165 | // *Bms[i] = *Bms[ind ( i ) ]; |
| 166 | |
| 167 | // poor-man's solution: replicate constructor here |
| 168 | // copied from MPF::MPF |
| 169 | delete Bms[i]; |
| 170 | Bms[i] = new BM_T ( *Bms[ind ( i ) ] ); //copy constructor |
| 171 | epdf& pom=Bms[i]->_epdf(); |
| 172 | jest.set_elements ( i,1.0/n,&pom ); |
| 173 | } |
| 174 | }; |
| 175 | } |
| 176 | } |