| 177 | void MPF_ARXg::bayes ( const vec &yt, const vec &cond ) { |
| 178 | // follows PF::bayes in most places!!! |
| 179 | int i; |
| 180 | int n = pf->__w().length(); |
| 181 | vec &lls = pf->_lls(); |
| 182 | Array<vec> &samples = pf->__samples(); |
| 183 | |
| 184 | // generate samples - time step |
| 185 | for (int i =0;i<n; i++){ |
| 186 | mat M; chmat R; |
| 187 | BMsp(i)->posterior().sample_mat(M,R); |
| 188 | vec diff=R._Ch().T()*randn(samples(i).length()); |
| 189 | samples(i) = g->eval(samples(i)) + diff; |
| 190 | //////////// |
| 191 | // samples(i) = cond; |
| 192 | ///////// |
| 193 | BMsp(i)->bayes(diff); |
| 194 | } |
| 195 | // weight them - data step |
| 196 | enorm<ldmat> ooo; |
| 197 | ooo.set_parameters(zeros(2),0.1*eye(2)); |
| 198 | ooo.validate(); |
| 199 | |
| 200 | #pragma parallel for |
| 201 | for ( i = 0; i < n; i++ ) { |
| 202 | vec tmp=yt - h->eval(samples(i)); |
| 203 | BMso ( i ) -> bayes ( tmp ); |
| 204 | lls ( i ) += BMso ( i )->_ll(); |
| 205 | ///// |
| 206 | ooo._mu()=h->eval(samples(i)); |
| 207 | lls(i) = ooo.evallog_nn(yt); |
| 208 | } |
| 209 | |
| 210 | pf->bayes_weights(); |
| 211 | |
| 212 | ivec ind; |
| 213 | if ( pf->do_resampling() ) { |
| 214 | pf->resample ( ind ); |
| 215 | |
| 216 | #pragma omp parallel for |
| 217 | for ( i = 0; i < n; i++ ) { |
| 218 | if ( ind ( i ) != i ) {//replace the current Bm by a new one |
| 219 | delete BMso ( i ); |
| 220 | BMso ( i ) = (ARX*) BMso ( ind ( i ) )->_copy(); //copy constructor |
| 221 | delete BMsp ( i ); |
| 222 | BMsp ( i ) = (ARX*) BMsp ( ind ( i ) )->_copy(); //copy constructor |
| 223 | } |
| 224 | }; |
| 225 | } |
| 226 | }; |
| 227 | |
| 228 | void MPF_ARXg::from_setting(const libconfig::Setting& set) { |
| 229 | BM::from_setting(set); |
| 230 | |
| 231 | pf = new PF; |
| 232 | // prior must be set before BM |
| 233 | pf->prior_from_set ( set ); |
| 234 | pf->resmethod_from_set ( set ); |
| 235 | |
| 236 | string opt; |
| 237 | if ( UI::get ( opt, set, "options", UI::optional ) ) { |
| 238 | set_options ( opt ); |
| 239 | } |
| 240 | |
| 241 | // read functions g and h |
| 242 | g=UI::build<fnc>(set,"g"); |
| 243 | h=UI::build<fnc>(set,"h"); |
| 244 | |
| 245 | set_dim( g->dimension()); |
| 246 | dimy = h->dimension(); |
| 247 | |
| 248 | shared_ptr<ARX> arxo=UI::build<ARX>(set,"arxo"); |
| 249 | shared_ptr<ARX> arxp=UI::build<ARX>(set,"arxp"); |
| 250 | int n = pf->__samples().length(); |
| 251 | BMso.set_length(n); |
| 252 | BMsp.set_length(n); |
| 253 | for(int i=0; i<n; i++){ |
| 254 | BMso(i)=arxo->_copy(); |
| 255 | BMsp(i)=arxp->_copy(); |
| 256 | } |
| 257 | |
| 258 | yrv = arxo->_yrv(); |
| 259 | //rvc = arxo->_rvc(); |
| 260 | |
| 261 | validate(); |
| 262 | |
| 263 | } |
| 264 | |
| 265 | |