pf
rbpf.h
Go to the documentation of this file.
1 #ifndef RBPF_H
2 #define RBPF_H
3 
4 #include <functional>
5 #include <vector>
6 #include <array>
7 
8 #ifdef DROPPINGTHISINRPACKAGE
9  #include <RcppEigen.h>
10  // [[Rcpp::depends(RcppEigen)]]
11 #else
12  #include <Eigen/Dense>
13 #endif
14 
15 #include <algorithm> // std::fill
16 
17 #include "pf_base.h"
18 #include "cf_filters.h" // for closed form filter objects
19 
20 
21 namespace pf {
22 
23 namespace filters {
24 
25 
27 
38 template<size_t nparts, size_t dimnss,size_t dimss,size_t dimy,typename resamp_t, typename float_t, bool debug=false>
39 class rbpf_hmm : public bases::rbpf_base<float_t, dimss, dimnss, dimy >
40 {
41 public:
42 
44  using sssv = Eigen::Matrix<float_t,dimss,1>;
46  using nsssv = Eigen::Matrix<float_t,dimnss,1>;
48  using osv = Eigen::Matrix<float_t,dimy,1>;
50  using nsssMat = Eigen::Matrix<float_t,dimnss,dimnss>;
52  using Mat = Eigen::Matrix<float_t,Eigen::Dynamic,Eigen::Dynamic>;
54  using arrayVec = std::array<sssv,nparts>;
56  using arrayfloat_t = std::array<float_t,nparts>;
60  using arrayMod = std::array<cfModType,nparts>;
61 
63 
67  rbpf_hmm(const unsigned int &resamp_sched=1);
68 
69 
73  virtual ~rbpf_hmm();
74 
75 
77 
82  void filter(const osv &data,
83  const std::vector<std::function<const Mat(const nsssv &x1tLogProbs, const sssv &x2t)> >& fs
84  = std::vector<std::function<const Mat(const nsssv&, const sssv&)> >());//, const std::vector<std::function<const Mat(const Vec&)> >& fs);
85 
86 
88 
92  float_t getLogCondLike() const;
93 
95 
99  std::vector<Mat> getExpectations() const;
100 
102 
107  virtual float_t logMuEv(const sssv &x21) = 0;
108 
109 
111 
116  virtual sssv q1Samp(const osv &y1) = 0;
117 
118 
120 
125  virtual nsssv initHMMLogProbVec(const sssv &x21) = 0;
126 
127 
129 
134  virtual nsssMat initHMMLogTransMat(const sssv &x21) = 0;
135 
137 
143  virtual sssv qSamp(const sssv &x2tm1, const osv &yt) = 0;
144 
145 
147 
153  virtual float_t logQ1Ev(const sssv &x21, const osv &y1) = 0;
154 
155 
157 
163  virtual float_t logFEv(const sssv &x2t, const sssv &x2tm1) = 0;
164 
165 
167 
174  virtual float_t logQEv(const sssv &x2t, const sssv &x2tm1, const osv &yt ) = 0;
175 
176 
178 
184  virtual void updateHMM(cfModType &aModel, const osv &yt, const sssv &x2t) = 0;
185 
186 private:
187 
189  unsigned int m_now;
193  unsigned int m_rs;
201  resamp_t m_resampler;
203  std::vector<Mat> m_expectations;
204 
205 };
206 
207 
208 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
210  : m_now(0)
211  , m_lastLogCondLike(0.0)
212  , m_rs(resamp_sched)
213 {
214  std::fill(m_logUnNormWeights.begin(), m_logUnNormWeights.end(), 0.0);
215 }
216 
217 
218 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t,bool debug>
220 
221 
222 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t,bool debug>
223 void rbpf_hmm<nparts,dimnss,dimss,dimy,resamp_t,float_t,debug>::filter(const osv &data, const std::vector<std::function<const Mat(const nsssv &x1tLogProbs, const sssv &x2t)> >& fs)
224 {
225 
226  if(m_now > 0)
227  { //m_now > 0
228 
229  // update
230  sssv newX2Samp;
231  float_t sumexpdenom(0.0);
232  float_t m1(-std::numeric_limits<float_t>::infinity()); // for revised log weights
233  float_t m2 = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
234  for(size_t ii = 0; ii < nparts; ++ii){
235 
236  newX2Samp = qSamp(m_p_samps[ii], data);
237  updateHMM(m_p_innerMods[ii], data, newX2Samp);
238  sumexpdenom += std::exp(m_logUnNormWeights[ii] - m2);
239 
240  m_logUnNormWeights[ii] += m_p_innerMods[ii].getLogCondLike()
241  + logFEv(newX2Samp, m_p_samps[ii])
242  - logQEv(newX2Samp, m_p_samps[ii], data);
243 
244  // update a max
245  if(m_logUnNormWeights[ii] > m1)
246  m1 = m_logUnNormWeights[ii];
247 
248  // print stuff if debug mode is on
249  #ifndef DROPPINGTHISINRPACKAGE
250  if constexpr(debug)
251  std::cout << "time: " << m_now << ", transposed x2 sample: " << newX2Samp.transpose() << ", log unnorm weight: " << m_logUnNormWeights[ii] << "\n";
252  #endif
253 
254  m_p_samps[ii] = newX2Samp;
255  }
256 
257  // calculate log p(y_t | y_{1:t-1})
258  float_t sumexpnumer(0.0);
259  for(size_t p = 0; p < nparts; ++p)
260  sumexpnumer += std::exp(m_logUnNormWeights[p] - m1);
261  m_lastLogCondLike = m1 + std::log(sumexpnumer) - m2 - std::log(sumexpdenom);
262 
263  // calculate expectations before you resample
264  unsigned int fId(0);
265  //float_t m = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
266  for(auto & h : fs){
267 
268  Mat testOutput = h(m_p_innerMods[0].getFilterVecLogProbs(), m_p_samps[0]);
269  unsigned int rows = testOutput.rows();
270  unsigned int cols = testOutput.cols();
271  Mat numer = Mat::Zero(rows,cols);
272  float_t denom(0.0);
273  for(size_t prtcl = 0; prtcl < nparts; ++prtcl){
274  numer += h(m_p_innerMods[prtcl].getFilterVecLogProbs(), m_p_samps[prtcl]) * std::exp(m_logUnNormWeights[prtcl] - m1);
275  denom += std::exp( m_logUnNormWeights[prtcl] - m1 );
276  }
277  m_expectations[fId] = numer/denom;
278 
279  // print stuff if debug mode is on
280  #ifndef DROPPINGTHISINRPACKAGE
281  if constexpr(debug)
282  std::cout << "transposed expec " << fId << ": " << m_expectations[fId].transpose() << "\n";
283  #endif
284 
285  fId++;
286  }
287 
288  // resample (unnormalized weights ok)
289  if( (m_now+1) % m_rs == 0)
290  m_resampler.resampLogWts(m_p_innerMods, m_p_samps, m_logUnNormWeights);
291 
292  // update time step
293  m_now ++;
294  }
295  else //( m_now == 0)
296  { // first data point coming
297 
298  // initialize and update the closed-form mods
299  nsssv tmpLogProbs;
300  nsssMat tmpLogTransMat;
301  float_t m1(-std::numeric_limits<float_t>::infinity());
302  for(size_t ii = 0; ii < nparts; ++ii){
303 
304  m_p_samps[ii] = q1Samp(data);
305  tmpLogProbs = initHMMLogProbVec(m_p_samps[ii]);
306  tmpLogTransMat = initHMMLogTransMat(m_p_samps[ii]);
307  m_p_innerMods[ii] = cfModType(tmpLogProbs, tmpLogTransMat);
308  this->updateHMM(m_p_innerMods[ii], data, m_p_samps[ii]);
309  m_logUnNormWeights[ii] = m_p_innerMods[ii].getLogCondLike() + logMuEv(m_p_samps[ii]) - logQ1Ev(m_p_samps[ii], data);
310 
311  // print stuff if debug mode is on
312  #ifndef DROPPINGTHISINRPACKAGE
313  if constexpr(debug)
314  std::cout << "time: " << m_now << ", transposed x2 sample: " << m_p_samps[ii].transpose() << ", log unnorm weight: " << m_logUnNormWeights[ii] << "\n";
315  #endif
316 
317  // maximum to be used in likelihood calc
318  if(m_logUnNormWeights[ii] > m1)
319  m1 = m_logUnNormWeights[ii];
320  }
321 
322  // calc log p(y1)
323  float_t sumexp(0.0);
324  for(size_t p = 0; p < nparts; ++p)
325  sumexp += std::exp(m_logUnNormWeights[p] - m1);
326  m_lastLogCondLike = m1 + std::log(sumexp) - std::log(static_cast<float_t>(nparts));
327 
328  // calculate expectations before you resample
329  m_expectations.resize(fs.size());
330  unsigned int fId(0);
331  //float_t m = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
332  for(auto & h : fs){
333 
334  Mat testOutput = h(m_p_innerMods[0].getFilterVecLogProbs(), m_p_samps[0]);
335  unsigned int rows = testOutput.rows();
336  unsigned int cols = testOutput.cols();
337  Mat numer = Mat::Zero(rows,cols);
338  float_t denom(0.0);
339  for(size_t prtcl = 0; prtcl < nparts; ++prtcl){
340  numer += h(m_p_innerMods[prtcl].getFilterVecLogProbs(), m_p_samps[prtcl]) * std::exp(m_logUnNormWeights[prtcl] - m1);
341  denom += std::exp(m_logUnNormWeights[prtcl] - m1);
342  }
343  m_expectations[fId] = numer/denom;
344 
345  // print stuff if debug mode is on
346  #ifndef DROPPINGTHISINRPACKAGE
347  if constexpr(debug)
348  std::cout << "transposed expec " << fId << ": " << m_expectations[fId].transpose() << "\n";
349  #endif
350 
351  fId++;
352  }
353 
354  // resample (unnormalized weights ok)
355  if( (m_now+1) % m_rs == 0)
356  m_resampler.resampLogWts(m_p_innerMods, m_p_samps, m_logUnNormWeights);
357 
358  // advance time step
359  m_now ++;
360  }
361 
362 }
363 
364 
365 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
367 {
368  return m_lastLogCondLike;
369 }
370 
371 
372 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
374 {
375  return m_expectations;
376 }
377 
378 
380 
391 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug = false>
392 class rbpf_hmm_bs : public bases::rbpf_base<float_t,dimss,dimnss,dimy>
393 {
394 public:
395 
397  using sssv = Eigen::Matrix<float_t,dimss,1>;
399  using nsssv = Eigen::Matrix<float_t,dimnss,1>;
401  using osv = Eigen::Matrix<float_t,dimy,1>;
403  using nsssMat = Eigen::Matrix<float_t,dimnss,dimnss>;
405  using Mat = Eigen::Matrix<float_t,Eigen::Dynamic,Eigen::Dynamic>;
409  using arrayMod = std::array<cfModType,nparts>;
411  using arrayVec = std::array<sssv,nparts>;
413  using arrayfloat_t = std::array<float_t,nparts>;
414 
415 
417 
421  rbpf_hmm_bs(const unsigned int &resamp_sched=1);
422 
423 
427  virtual ~rbpf_hmm_bs();
428 
429 
431 
436  void filter(const osv &data,
437  const std::vector<std::function<const Mat(const nsssv &x1tLogProbs, const sssv &x2t)> >& fs
438  = std::vector<std::function<const Mat(const nsssv&, const sssv&)> >());//, const std::vector<std::function<const Mat(const Vec&)> >& fs);
439 
440 
442 
446  float_t getLogCondLike() const;
447 
449 
453  std::vector<Mat> getExpectations() const;
454 
455 
457 
461  virtual sssv muSamp() = 0;
462 
463 
465 
470  virtual nsssv initHMMLogProbVec(const sssv &x21) = 0;
471 
472 
474 
479  virtual nsssMat initHMMLogTransMat(const sssv &x21) = 0;
480 
482 
487  virtual sssv fSamp(const sssv &x2tm1) = 0;
488 
489 
491 
497  virtual void updateHMM(cfModType &aModel, const osv &yt, const sssv &x2t) = 0;
498 
499 private:
500 
502  unsigned int m_now;
506  unsigned int m_rs;
514  resamp_t m_resampler;
516  std::vector<Mat> m_expectations;
517 
518 };
519 
520 
521 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
523  : m_now(0)
524  , m_lastLogCondLike(0.0)
525  , m_rs(resamp_sched)
526 {
527  std::fill(m_logUnNormWeights.begin(), m_logUnNormWeights.end(), 0.0);
528 }
529 
530 
531 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
533 
534 
535 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
536 void rbpf_hmm_bs<nparts,dimnss,dimss,dimy,resamp_t,float_t,debug>::filter(const osv &data, const std::vector<std::function<const Mat(const nsssv &x1tLogProbs, const sssv &x2t)> >& fs)
537 {
538 
539  if(m_now > 0)
540  {
541  // update
542  sssv newX2Samp;
543  float_t sumexpdenom(0.0);
544  float_t m1(-std::numeric_limits<float_t>::infinity()); // for revised log weights
545  float_t m2 = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
546  for(size_t ii = 0; ii < nparts; ++ii){
547 
548  newX2Samp = fSamp(m_p_samps[ii]);
549  updateHMM(m_p_innerMods[ii], data, newX2Samp);
550  sumexpdenom += std::exp(m_logUnNormWeights[ii] - m2);
551 
552  m_logUnNormWeights[ii] += m_p_innerMods[ii].getLogCondLike();
553 
554  // print stuff if debug mode is on
555  #ifndef DROPPINGTHISINRPACKAGE
556  if constexpr(debug)
557  std::cout << "time: " << m_now << ", transposed x2 sample: " << newX2Samp.transpose() << ", log unnorm weight: " << m_logUnNormWeights[ii] << "\n";
558  #endif
559 
560  // update a max
561  if(m_logUnNormWeights[ii] > m1)
562  m1 = m_logUnNormWeights[ii];
563 
564  m_p_samps[ii] = newX2Samp;
565  }
566 
567  // calculate log p(y_t | y_{1:t-1})
568  float_t sumexpnumer(0.0);
569  for(size_t p = 0; p < nparts; ++p)
570  sumexpnumer += std::exp(m_logUnNormWeights[p] - m1);
571  m_lastLogCondLike = m1 + std::log(sumexpnumer) - m2 - std::log(sumexpdenom);
572 
573  // calculate expectations before you resample
574  unsigned int fId(0);
575  //float_t m = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
576  for(auto & h : fs){
577 
578  Mat testOutput = h(m_p_innerMods[0].getFilterVecLogProbs(), m_p_samps[0]);
579  unsigned int rows = testOutput.rows();
580  unsigned int cols = testOutput.cols();
581  Mat numer = Mat::Zero(rows,cols);
582  float_t denom(0.0);
583  for(size_t prtcl = 0; prtcl < nparts; ++prtcl){
584  numer += h(m_p_innerMods[prtcl].getFilterVecLogProbs(), m_p_samps[prtcl])*std::exp(m_logUnNormWeights[prtcl] - m1);
585  denom += std::exp( m_logUnNormWeights[prtcl] - m1 );
586  }
587  m_expectations[fId] = numer/denom;
588 
589  // print stuff if debug mode is on
590  #ifndef DROPPINGTHISINRPACKAGE
591  if constexpr(debug)
592  std::cout << "transposed expec " << fId << ": " << m_expectations[fId].transpose() << "\n";
593  #endif
594 
595  fId++;
596  }
597 
598  // resample (unnormalized weights ok)
599  if( (m_now+1) % m_rs == 0)
600  m_resampler.resampLogWts(m_p_innerMods, m_p_samps, m_logUnNormWeights);
601 
602  // update time step
603  m_now ++;
604  }
605  else// ( m_now == 0) // first data point coming
606  {
607  // initialize and update the closed-form mods
608  nsssv tmpLogProbs;
609  nsssMat tmpLogTransMat;
610  float_t m1(-std::numeric_limits<float_t>::infinity());
611  for(size_t ii = 0; ii < nparts; ++ii){
612 
613  m_p_samps[ii] = muSamp();
614  tmpLogProbs = initHMMLogProbVec(m_p_samps[ii]);
615  tmpLogTransMat = initHMMLogTransMat(m_p_samps[ii]);
616  m_p_innerMods[ii] = cfModType(tmpLogProbs, tmpLogTransMat);
617  this->updateHMM(m_p_innerMods[ii], data, m_p_samps[ii]);
618  m_logUnNormWeights[ii] = m_p_innerMods[ii].getLogCondLike();
619 
620  // print stuff if debug mode is on
621  #ifndef DROPPINGTHISINRPACKAGE
622  if constexpr(debug)
623  std::cout << "time: " << m_now << ", transposed x2 sample: " << m_p_samps[ii].transpose() << ", log unnorm weight: " << m_logUnNormWeights[ii] << "\n";
624  #endif
625 
626 
627  // maximum to be used in likelihood calc
628  if(m_logUnNormWeights[ii] > m1)
629  m1 = m_logUnNormWeights[ii];
630  }
631 
632  // calc log p(y1)
633  float_t sumexp(0.0);
634  for(size_t p = 0; p < nparts; ++p)
635  sumexp += std::exp(m_logUnNormWeights[p] - m1);
636  m_lastLogCondLike = m1 + std::log(sumexp) - std::log(static_cast<float_t>(nparts));
637 
638  // calculate expectations before you resample
639  m_expectations.resize(fs.size());
640  unsigned int fId(0);
641  //float_t m = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end()); /// TODO: can we just use m1?
642  for(auto & h : fs){
643 
644  Mat testOutput = h(m_p_innerMods[0].getFilterVecLogProbs(), m_p_samps[0]);
645  unsigned int rows = testOutput.rows();
646  unsigned int cols = testOutput.cols();
647  Mat numer = Mat::Zero(rows,cols);
648  float_t denom(0.0);
649  for(size_t prtcl = 0; prtcl < nparts; ++prtcl){
650  numer += h(m_p_innerMods[prtcl].getFilterVecLogProbs(), m_p_samps[prtcl]) * std::exp(m_logUnNormWeights[prtcl] - m1);
651  denom += std::exp( m_logUnNormWeights[prtcl] - m1 );
652  }
653  m_expectations[fId] = numer/denom;
654 
655  // print stuff if debug mode is on
656  #ifndef DROPPINGTHISINRPACKAGE
657  if constexpr(debug)
658  std::cout << "transposed expec " << fId << ": " << m_expectations[fId].transpose() << "\n";
659  #endif
660 
661  fId++;
662  }
663 
664  // resample (unnormalized weights ok)
665  if( (m_now+1) % m_rs == 0)
666  m_resampler.resampLogWts(m_p_innerMods, m_p_samps, m_logUnNormWeights);
667 
668  // advance time step
669  m_now ++;
670  }
671 
672 }
673 
674 
675 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
677 {
678  return m_lastLogCondLike;
679 }
680 
681 
682 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
684 {
685  return m_expectations;
686 }
687 
688 
690 
701 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug = false>
702 class rbpf_kalman : public bases::rbpf_base<float_t,dimss,dimnss,dimy>
703 {
704 
705 public:
706 
708  using sssv = Eigen::Matrix<float_t,dimss,1>;
710  using nsssv = Eigen::Matrix<float_t,dimnss,1>;
712  using osv = Eigen::Matrix<float_t,dimy,1>;
714  using Mat = Eigen::Matrix<float_t,Eigen::Dynamic,Eigen::Dynamic>;
716  using nsssMat = Eigen::Matrix<float_t,dimnss,dimnss>;
720  using arrayMod = std::array<cfModType,nparts>;
722  using arrayVec = std::array<sssv,nparts>;
724  using arrayfloat_t = std::array<float_t,nparts>;
725 
727 
730  rbpf_kalman(const unsigned int &resamp_sched=1);
731 
732 
736  virtual ~rbpf_kalman();
737 
738 
740 
745  void filter(const osv &data, const std::vector<std::function<const Mat(const nsssv &x1t, const sssv &x2t)> >& fs
746  = std::vector<std::function<const Mat(const nsssv &x1t, const sssv &x2t)> >() );
747 
749 
752  float_t getLogCondLike() const;
753 
754 
756 
760  std::vector<Mat> getExpectations() const;
761 
762 
764 
769  virtual float_t logMuEv(const sssv &x21) = 0;
770 
771 
773 
778  virtual sssv q1Samp(const osv &y1) = 0;
779 
780 
782 
787  virtual nsssv initKalmanMean(const sssv &x21) = 0;
788 
789 
791 
796  virtual nsssMat initKalmanVar(const sssv &x21) = 0;
797 
798 
800 
806  virtual sssv qSamp(const sssv &x2tm1, const osv &yt) = 0;
807 
808 
810 
816  virtual float_t logQ1Ev(const sssv &x21, const osv &y1) = 0;
817 
818 
820 
826  virtual float_t logFEv(const sssv &x2t, const sssv &x2tm1) = 0;
827 
828 
830 
837  virtual float_t logQEv(const sssv &x2t, const sssv &x2tm1, const osv &yt) = 0;
838 
839 
841 
847  virtual void updateKalman(cfModType &kMod, const osv &yt, const sssv &x2t) = 0;
848 
849 private:
850 
852  unsigned int m_rs;
860  unsigned int m_now;
864  resamp_t m_resampler;
866  std::vector<Mat> m_expectations;
867 };
868 
869 
870 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
872  : m_now(0)
873  , m_lastLogCondLike(0.0)
874  , m_rs(resamp_sched)
875 {
876  std::fill(m_logUnNormWeights.begin(), m_logUnNormWeights.end(), 0.0);
877 }
878 
879 
880 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
882 
883 
884 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
885 void rbpf_kalman<nparts,dimnss,dimss,dimy,resamp_t,float_t,debug>::filter(const osv &data, const std::vector<std::function<const Mat(const nsssv &x1t, const sssv &x2t)> >& fs)
886 {
887 
888  if(m_now > 0)
889  {
890 
891  // update
892  sssv newX2Samp;
893  float_t m1(-std::numeric_limits<float_t>::infinity()); // for updated weights
894  float_t m2 = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
895  float_t sumexpdenom(0.0);
896  for(size_t ii = 0; ii < nparts; ++ii){
897  newX2Samp = qSamp(m_p_samps[ii], data);
898  this->updateKalman(m_p_innerMods[ii], data, newX2Samp);
899 
900  // before you update the weights
901  sumexpdenom += std::exp(m_logUnNormWeights[ii] - m2);
902 
903  // update the weights
904  m_logUnNormWeights[ii] += m_p_innerMods[ii].getLogCondLike() + logFEv(newX2Samp, m_p_samps[ii]) - logQEv(newX2Samp, m_p_samps[ii], data);
905 
906  // print stuff if debug mode is on
907  #ifndef DROPPINGTHISINRPACKAGE
908  if constexpr(debug)
909  std::cout << "time: " << m_now << ", transposed x2 sample: " << newX2Samp.transpose() << ", log unnorm weight: " << m_logUnNormWeights[ii] << "\n";
910  #endif
911 
912  // update a max
913  if(m_logUnNormWeights[ii] > m1)
914  m1 = m_logUnNormWeights[ii];
915 
916  m_p_samps[ii] = newX2Samp;
917  }
918 
919  // calc log p(y_t | y_{1:t-1})
920  float_t sumexpnumer(0.0);
921  for(size_t p = 0; p < nparts; ++p)
922  sumexpnumer += std::exp(m_logUnNormWeights[p] - m1);
923  m_lastLogCondLike = m1 + std::log(sumexpnumer) - m2 - std::log(sumexpdenom);
924 
925  // calculate expectations before you resample
926  unsigned int fId(0);
927  //float_t m = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
928  for(auto & h : fs){
929 
930  Mat testOutput = h(m_p_innerMods[0].getFilterVec(), m_p_samps[0]);
931  unsigned int rows = testOutput.rows();
932  unsigned int cols = testOutput.cols();
933  Mat numer = Mat::Zero(rows,cols);
934  float_t denom(0.0);
935  for(size_t prtcl = 0; prtcl < nparts; ++prtcl){
936  numer += h(m_p_innerMods[prtcl].getFilterVec(), m_p_samps[prtcl]) * std::exp(m_logUnNormWeights[prtcl] - m1);
937  denom += std::exp( m_logUnNormWeights[prtcl] - m1 );
938  }
939  m_expectations[fId] = numer/denom;
940 
941  // print stuff if debug mode is on
942  #ifndef DROPPINGTHISINRPACKAGE
943  if constexpr(debug)
944  std::cout << "transposed expec " << fId << ": " << m_expectations[fId].transpose() << "\n";
945  #endif
946 
947  fId++;
948  }
949 
950  // resample (unnormalized weights ok)
951  if( (m_now+1)%m_rs == 0)
952  m_resampler.resampLogWts(m_p_innerMods, m_p_samps, m_logUnNormWeights);
953 
954  // update time step
955  m_now ++;
956  }
957  else //( m_now == 0) // first data point coming
958  {
959  // initialize and update the closed-form mods
960  nsssv tmpMean;
961  nsssMat tmpVar;
962  float_t m1(-std::numeric_limits<float_t>::infinity());
963  for(size_t ii = 0; ii < nparts; ++ii){
964  m_p_samps[ii] = q1Samp(data);
965  tmpMean = initKalmanMean(m_p_samps[ii]);
966  tmpVar = initKalmanVar(m_p_samps[ii]);
967  m_p_innerMods[ii] = cfModType(tmpMean, tmpVar); // TODO: allow for input or check to make sure this doesn't break anything else
968  this->updateKalman(m_p_innerMods[ii], data, m_p_samps[ii]);
969 
970  m_logUnNormWeights[ii] = m_p_innerMods[ii].getLogCondLike() + logMuEv(m_p_samps[ii]) - logQ1Ev(m_p_samps[ii], data);
971 
972  // print stuff if debug mode is on
973  #ifndef DROPPINGTHISINRPACKAGE
974  if constexpr(debug)
975  std::cout << "time: " << m_now << ", transposed x2 sample: " << m_p_samps[ii].transpose() << ", log unnorm weight: " << m_logUnNormWeights[ii] << "\n";
976  #endif
977 
978  // update a max
979  if(m_logUnNormWeights[ii] > m1)
980  m1 = m_logUnNormWeights[ii];
981  }
982 
983  // calculate log p(y1)
984  float_t sumexp(0.0);
985  for(size_t p = 0; p < nparts; ++p)
986  sumexp += std::exp(m_logUnNormWeights[p] - m1);
987  m_lastLogCondLike = m1 + std::log(sumexp) - std::log(static_cast<float_t>(nparts));
988 
989  // calculate expectations before you resample
990  m_expectations.resize(fs.size());
991  unsigned int fId(0);
992  //float_t m = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
993  for(auto & h : fs){
994 
995  Mat testOutput = h(m_p_innerMods[0].getFilterVec(), m_p_samps[0]);
996  unsigned int rows = testOutput.rows();
997  unsigned int cols = testOutput.cols();
998  Mat numer = Mat::Zero(rows,cols);
999  float_t denom(0.0);
1000  for(size_t prtcl = 0; prtcl < nparts; ++prtcl){
1001  numer += h(m_p_innerMods[prtcl].getFilterVec(), m_p_samps[prtcl]) * std::exp(m_logUnNormWeights[prtcl] - m1);
1002  denom += std::exp( m_logUnNormWeights[prtcl] - m1 );
1003  }
1004  m_expectations[fId] = numer/denom;
1005 
1006  // print stuff if debug mode is on
1007  #ifndef DROPPINGTHISINRPACKAGE
1008  if constexpr(debug)
1009  std::cout << "transposed expec " << fId << ": " << m_expectations[fId].transpose() << "\n";
1010  #endif
1011 
1012  fId++;
1013  }
1014 
1015  // resample (unnormalized weights ok)
1016  if( (m_now+1)%m_rs == 0)
1017  m_resampler.resampLogWts(m_p_innerMods, m_p_samps, m_logUnNormWeights);
1018 
1019  // advance time step
1020  m_now ++;
1021  }
1022 
1023 }
1024 
1025 
1026 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
1028 {
1029  return m_lastLogCondLike;
1030 }
1031 
1032 
1033 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
1035 {
1036  return m_expectations;
1037 }
1038 
1039 
1040 
1042 
1053 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug = false>
1054 class rbpf_kalman_bs : public bases::rbpf_base<float_t,dimss,dimnss,dimy>
1055 {
1056 
1057 public:
1058 
1060  using sssv = Eigen::Matrix<float_t,dimss,1>;
1062  using nsssv = Eigen::Matrix<float_t,dimnss,1>;
1064  using osv = Eigen::Matrix<float_t,dimy,1>;
1066  using Mat = Eigen::Matrix<float_t,Eigen::Dynamic,Eigen::Dynamic>;
1068  using nsssMat = Eigen::Matrix<float_t,dimnss,dimnss>;
1072  using arrayMod = std::array<cfModType,nparts>;
1074  using arrayVec = std::array<sssv,nparts>;
1076  using arrayfloat_t = std::array<float_t,nparts>;
1077 
1079 
1082  rbpf_kalman_bs(const unsigned int &resamp_sched=1);
1083 
1084 
1088  virtual ~rbpf_kalman_bs();
1089 
1090 
1092 
1097  void filter(const osv &data, const std::vector<std::function<const Mat(const nsssv &x1t, const sssv &x2t)> >& fs
1098  = std::vector<std::function<const Mat(const nsssv &x1t, const sssv &x2t)> >() );
1099 
1101 
1104  float_t getLogCondLike() const;
1105 
1106 
1108 
1112  std::vector<Mat> getExpectations() const;
1113 
1114 
1116 
1120  virtual sssv muSamp() = 0;
1121 
1122 
1124 
1129  virtual nsssv initKalmanMean(const sssv &x21) = 0;
1130 
1131 
1133 
1138  virtual nsssMat initKalmanVar(const sssv &x21) = 0;
1139 
1140 
1142 
1147  virtual sssv fSamp(const sssv &x2tm1) = 0;
1148 
1149 
1151 
1157  virtual void updateKalman(cfModType &kMod, const osv &yt, const sssv &x2t) = 0;
1158 
1159 private:
1160 
1162  unsigned int m_rs;
1170  unsigned int m_now;
1174  resamp_t m_resampler;
1176  std::vector<Mat> m_expectations;
1177 };
1178 
1179 
1180 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
1182  : m_now(0)
1183  , m_lastLogCondLike(0.0)
1184  , m_rs(resamp_sched)
1185 {
1186  std::fill(m_logUnNormWeights.begin(), m_logUnNormWeights.end(), 0.0);
1187 }
1188 
1189 
1190 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
1192 
1193 
1194 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
1195 void rbpf_kalman_bs<nparts,dimnss,dimss,dimy,resamp_t,float_t,debug>::filter(const osv &data, const std::vector<std::function<const Mat(const nsssv &x1t, const sssv &x2t)> >& fs)
1196 {
1197 
1198  if(m_now > 0)
1199  {
1200 
1201  // update
1202  sssv newX2Samp;
1203  float_t m1(-std::numeric_limits<float_t>::infinity()); // for updated weights
1204  float_t m2 = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
1205  float_t sumexpdenom(0.0);
1206  for(size_t ii = 0; ii < nparts; ++ii){
1207 
1208  newX2Samp = fSamp(m_p_samps[ii], data);
1209  this->updateKalman(m_p_innerMods[ii], data, newX2Samp);
1210 
1211  // before you update the weights
1212  sumexpdenom += std::exp(m_logUnNormWeights[ii] - m2);
1213 
1214  // update the weights
1215  m_logUnNormWeights[ii] += m_p_innerMods[ii].getLogCondLike();
1216 
1217  // print stuff if debug mode is on
1218  #ifndef DROPPINGTHISINRPACKAGE
1219  if constexpr(debug)
1220  std::cout << "time: " << m_now << ", transposed x2 sample: " << newX2Samp.transpose() << ", log unnorm weight: " << m_logUnNormWeights[ii] << "\n";
1221  #endif
1222 
1223  // update a max
1224  if(m_logUnNormWeights[ii] > m1)
1225  m1 = m_logUnNormWeights[ii];
1226 
1227  m_p_samps[ii] = newX2Samp;
1228  }
1229 
1230  // calc log p(y_t | y_{1:t-1})
1231  float_t sumexpnumer(0.0);
1232  for(size_t p = 0; p < nparts; ++p)
1233  sumexpnumer += std::exp(m_logUnNormWeights[p] - m1);
1234  m_lastLogCondLike = m1 + std::log(sumexpnumer) - m2 - std::log(sumexpdenom);
1235 
1236  // calculate expectations before you resample
1237  unsigned int fId(0);
1238  //float_t m = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
1239  for(auto & h : fs){
1240 
1241  Mat testOutput = h(m_p_innerMods[0].getFilterVec(), m_p_samps[0]);
1242  unsigned int rows = testOutput.rows();
1243  unsigned int cols = testOutput.cols();
1244  Mat numer = Mat::Zero(rows,cols);
1245  float_t denom(0.0);
1246  for(size_t prtcl = 0; prtcl < nparts; ++prtcl){
1247  numer += h(m_p_innerMods[prtcl].getFilterVec(), m_p_samps[prtcl]) * std::exp(m_logUnNormWeights[prtcl] - m1);
1248  denom += std::exp( m_logUnNormWeights[prtcl] - m1 );
1249  }
1250  m_expectations[fId] = numer/denom;
1251 
1252  // print stuff if debug mode is on
1253  #ifndef DROPPINGTHISINRPACKAGE
1254  if constexpr(debug)
1255  std::cout << "transposed expec " << fId << ": " << m_expectations[fId].transpose() << "\n";
1256  #endif
1257 
1258  fId++;
1259  }
1260 
1261  // resample (unnormalized weights ok)
1262  if( (m_now+1)%m_rs == 0)
1263  m_resampler.resampLogWts(m_p_innerMods, m_p_samps, m_logUnNormWeights);
1264 
1265  // update time step
1266  m_now ++;
1267  }
1268  else // ( m_now == 0) // first data point coming
1269  {
1270  // initialize and update the closed-form mods
1271  nsssv tmpMean;
1272  nsssMat tmpVar;
1273  float_t m1(-std::numeric_limits<float_t>::infinity());
1274  for(size_t ii = 0; ii < nparts; ++ii){
1275  m_p_samps[ii] = muSamp(data);
1276  tmpMean = initKalmanMean(m_p_samps[ii]);
1277  tmpVar = initKalmanVar(m_p_samps[ii]);
1278  m_p_innerMods[ii] = cfModType(tmpMean, tmpVar); // TODO: allow for input or check to make sure this doesn't break anything else
1279  this->updateKalman(m_p_innerMods[ii], data, m_p_samps[ii]);
1280 
1281  m_logUnNormWeights[ii] = m_p_innerMods[ii].getLogCondLike();
1282 
1283  // print stuff if debug mode is on
1284  #ifndef DROPPINGTHISINRPACKAGE
1285  if constexpr(debug)
1286  std::cout << "time: " << m_now << ", transposed x2 sample: " << m_p_samps[ii].transpose() << ", log unnorm weight: " << m_logUnNormWeights[ii] << "\n";
1287  #endif
1288 
1289  // update a max
1290  if(m_logUnNormWeights[ii] > m1)
1291  m1 = m_logUnNormWeights[ii];
1292  }
1293 
1294  // calculate log p(y1)
1295  float_t sumexp(0.0);
1296  for(size_t p = 0; p < nparts; ++p)
1297  sumexp += std::exp(m_logUnNormWeights[p] - m1);
1298  m_lastLogCondLike = m1 + std::log(sumexp) - std::log(static_cast<float_t>(nparts));
1299 
1300  // calculate expectations before you resample
1301  m_expectations.resize(fs.size());
1302  unsigned int fId(0);
1303  //float_t m = *std::max_element(m_logUnNormWeights.begin(), m_logUnNormWeights.end());
1304  for(auto & h : fs){
1305 
1306  Mat testOutput = h(m_p_innerMods[0].getFilterVec(), m_p_samps[0]);
1307  unsigned int rows = testOutput.rows();
1308  unsigned int cols = testOutput.cols();
1309  Mat numer = Mat::Zero(rows,cols);
1310  float_t denom(0.0);
1311  for(size_t prtcl = 0; prtcl < nparts; ++prtcl){
1312  numer += h(m_p_innerMods[prtcl].getFilterVec(), m_p_samps[prtcl])*std::exp(m_logUnNormWeights[prtcl] - m1);
1313  denom += std::exp( m_logUnNormWeights[prtcl] - m1 );
1314  }
1315  m_expectations[fId] = numer/denom;
1316 
1317  // print stuff if debug mode is on
1318  #ifndef DROPPINGTHISINRPACKAGE
1319  if constexpr(debug)
1320  std::cout << "transposed expec " << fId << ": " << m_expectations[fId].transpose() << "\n";
1321  #endif
1322 
1323  fId++;
1324  }
1325 
1326  // resample (unnormalized weights ok)
1327  if( (m_now+1)%m_rs == 0)
1328  m_resampler.resampLogWts(m_p_innerMods, m_p_samps, m_logUnNormWeights);
1329 
1330  // advance time step
1331  m_now ++;
1332  }
1333 
1334 }
1335 
1336 
1337 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
1339 {
1340  return m_lastLogCondLike;
1341 }
1342 
1343 
1344 template<size_t nparts, size_t dimnss, size_t dimss, size_t dimy, typename resamp_t, typename float_t, bool debug>
1346 {
1347  return m_expectations;
1348 }
1349 
1350 } //namespace filters
1351 } //namespace pf
1352 
1353 #endif //RBPF_H
pf::filters::rbpf_hmm::logQEv
virtual float_t logQEv(const sssv &x2t, const sssv &x2tm1, const osv &yt)=0
Evaluates the proposal density at time t > 1.
pf::filters::rbpf_kalman::getLogCondLike
float_t getLogCondLike() const
Get the latest log conditional likelihood.
Definition: rbpf.h:1027
pf::filters::rbpf_kalman_bs::muSamp
virtual sssv muSamp()=0
Sample from the first time's proposal distribution.
pf::filters::rbpf_hmm::arrayfloat_t
std::array< float_t, nparts > arrayfloat_t
Definition: rbpf.h:56
pf::filters::rbpf_kalman_bs::m_logUnNormWeights
arrayfloat_t m_logUnNormWeights
Definition: rbpf.h:1168
pf::filters::rbpf_kalman::nsssMat
Eigen::Matrix< float_t, dimnss, dimnss > nsssMat
Definition: rbpf.h:716
pf::filters::rbpf_hmm::updateHMM
virtual void updateHMM(cfModType &aModel, const osv &yt, const sssv &x2t)=0
How to update your inner HMM filter object at each time.
pf::filters::rbpf_kalman::rbpf_kalman
rbpf_kalman(const unsigned int &resamp_sched=1)
The constructor.
Definition: rbpf.h:871
pf::filters::rbpf_hmm_bs::m_rs
unsigned int m_rs
Definition: rbpf.h:506
pf::filters::rbpf_kalman_bs::Mat
Eigen::Matrix< float_t, Eigen::Dynamic, Eigen::Dynamic > Mat
Definition: rbpf.h:1066
pf::filters::rbpf_kalman::logMuEv
virtual float_t logMuEv(const sssv &x21)=0
Evaluates the first time state density.
pf::filters::rbpf_hmm::logFEv
virtual float_t logFEv(const sssv &x2t, const sssv &x2tm1)=0
Evaluates the state transition density for the second state component.
pf::filters::rbpf_kalman_bs
Rao-Blackwellized/Marginal Bootstrap Filter with inner Kalman Filter objectss.
Definition: rbpf.h:1054
pf::filters::rbpf_hmm_bs::muSamp
virtual sssv muSamp()=0
Sample from the first sampler.
pf::filters::rbpf_hmm::Mat
Eigen::Matrix< float_t, Eigen::Dynamic, Eigen::Dynamic > Mat
Definition: rbpf.h:52
pf::filters::rbpf_hmm::m_logUnNormWeights
arrayfloat_t m_logUnNormWeights
Definition: rbpf.h:199
pf::filters::hmm
A class template for HMM filtering.
Definition: cf_filters.h:348
pf::filters::rbpf_kalman::updateKalman
virtual void updateKalman(cfModType &kMod, const osv &yt, const sssv &x2t)=0
How to update your inner Kalman filter object at each time.
pf::filters::rbpf_hmm::nsssMat
Eigen::Matrix< float_t, dimnss, dimnss > nsssMat
Definition: rbpf.h:50
pf_base.h
All non Rao-Blackwellized particle filters without covariates inherit from this.
pf::filters::rbpf_kalman::m_now
unsigned int m_now
Definition: rbpf.h:860
pf::filters::rbpf_kalman
Rao-Blackwellized/Marginal Particle Filter with inner Kalman Filter objectss.
Definition: rbpf.h:702
pf::filters::rbpf_kalman_bs::sssv
Eigen::Matrix< float_t, dimss, 1 > sssv
Definition: rbpf.h:1060
pf::filters::rbpf_hmm_bs::~rbpf_hmm_bs
virtual ~rbpf_hmm_bs()
The (virtual) destructor.
Definition: rbpf.h:532
pf::filters::rbpf_hmm::qSamp
virtual sssv qSamp(const sssv &x2tm1, const osv &yt)=0
Samples the time t second component.
pf::filters::rbpf_hmm_bs::m_resampler
resamp_t m_resampler
Definition: rbpf.h:514
pf::filters::rbpf_hmm_bs::nsssMat
Eigen::Matrix< float_t, dimnss, dimnss > nsssMat
Definition: rbpf.h:403
pf::filters::rbpf_hmm::initHMMLogTransMat
virtual nsssMat initHMMLogTransMat(const sssv &x21)=0
Provides the transition matrix for each HMM filter object.
pf::filters::rbpf_hmm_bs::getLogCondLike
float_t getLogCondLike() const
Get the latest conditional likelihood.
Definition: rbpf.h:676
pf::filters::rbpf_hmm_bs::filter
void filter(const osv &data, const std::vector< std::function< const Mat(const nsssv &x1tLogProbs, const sssv &x2t)> > &fs=std::vector< std::function< const Mat(const nsssv &, const sssv &)> >())
Filter.
Definition: rbpf.h:536
pf::filters::rbpf_hmm::m_p_samps
arrayVec m_p_samps
Definition: rbpf.h:197
pf::filters::rbpf_hmm::logMuEv
virtual float_t logMuEv(const sssv &x21)=0
Evaluates the first time state density.
pf::filters::rbpf_hmm_bs::osv
Eigen::Matrix< float_t, dimy, 1 > osv
Definition: rbpf.h:401
pf::filters::rbpf_hmm::m_lastLogCondLike
float_t m_lastLogCondLike
Definition: rbpf.h:191
pf::filters::rbpf_hmm::logQ1Ev
virtual float_t logQ1Ev(const sssv &x21, const osv &y1)=0
Evaluates the proposal density of the second state component at time 1.
pf::bases::rbpf_base
Definition: pf_base.h:166
pf::filters::rbpf_kalman::q1Samp
virtual sssv q1Samp(const osv &y1)=0
Sample from the first time's proposal distribution.
pf::filters::rbpf_kalman_bs::~rbpf_kalman_bs
virtual ~rbpf_kalman_bs()
The (virtual) destructor.
Definition: rbpf.h:1191
pf::filters::rbpf_hmm_bs::m_now
unsigned int m_now
Definition: rbpf.h:502
pf::filters::rbpf_kalman_bs::arrayMod
std::array< cfModType, nparts > arrayMod
Definition: rbpf.h:1072
pf::filters::rbpf_hmm_bs::Mat
Eigen::Matrix< float_t, Eigen::Dynamic, Eigen::Dynamic > Mat
Definition: rbpf.h:405
pf::filters::rbpf_hmm::getExpectations
std::vector< Mat > getExpectations() const
Get vector of expectations.
Definition: rbpf.h:373
pf::filters::rbpf_hmm_bs::initHMMLogProbVec
virtual nsssv initHMMLogProbVec(const sssv &x21)=0
Provides the initial log probability vector for each HMM filter object.
pf::filters::rbpf_hmm_bs::fSamp
virtual sssv fSamp(const sssv &x2tm1)=0
Samples the time t second component.
pf::filters::rbpf_kalman_bs::getLogCondLike
float_t getLogCondLike() const
Get the latest log conditional likelihood.
Definition: rbpf.h:1338
pf::filters::rbpf_hmm_bs::arrayVec
std::array< sssv, nparts > arrayVec
Definition: rbpf.h:411
pf::filters::rbpf_kalman_bs::m_expectations
std::vector< Mat > m_expectations
Definition: rbpf.h:1176
pf::filters::rbpf_kalman_bs::m_lastLogCondLike
float_t m_lastLogCondLike
Definition: rbpf.h:1172
pf::filters::rbpf_kalman_bs::m_rs
unsigned int m_rs
Definition: rbpf.h:1162
pf::filters::rbpf_hmm_bs::m_expectations
std::vector< Mat > m_expectations
Definition: rbpf.h:516
pf::filters::rbpf_kalman_bs::nsssMat
Eigen::Matrix< float_t, dimnss, dimnss > nsssMat
Definition: rbpf.h:1068
pf::filters::rbpf_hmm_bs::sssv
Eigen::Matrix< float_t, dimss, 1 > sssv
Definition: rbpf.h:397
pf::filters::rbpf_kalman_bs::m_p_innerMods
arrayMod m_p_innerMods
Definition: rbpf.h:1164
pf::filters::rbpf_kalman_bs::m_now
unsigned int m_now
Definition: rbpf.h:1170
pf::filters::rbpf_hmm::q1Samp
virtual sssv q1Samp(const osv &y1)=0
Sample from the first sampler.
pf::filters::rbpf_kalman::arrayVec
std::array< sssv, nparts > arrayVec
Definition: rbpf.h:722
pf::filters::rbpf_kalman_bs::getExpectations
std::vector< Mat > getExpectations() const
Get the latest filtered expectation E[h(x_1t, x_2t) | y_{1:t}].
Definition: rbpf.h:1345
pf::filters::rbpf_kalman::getExpectations
std::vector< Mat > getExpectations() const
Get the latest filtered expectation E[h(x_1t, x_2t) | y_{1:t}].
Definition: rbpf.h:1034
pf::filters::rbpf_kalman::arrayfloat_t
std::array< float_t, nparts > arrayfloat_t
Definition: rbpf.h:724
pf::filters::rbpf_kalman_bs::m_p_samps
arrayVec m_p_samps
Definition: rbpf.h:1166
pf::filters::rbpf_kalman_bs::m_resampler
resamp_t m_resampler
Definition: rbpf.h:1174
pf::filters::rbpf_kalman::m_p_samps
arrayVec m_p_samps
Definition: rbpf.h:856
pf::filters::rbpf_hmm::sssv
Eigen::Matrix< float_t, dimss, 1 > sssv
Definition: rbpf.h:44
pf::filters::rbpf_kalman::osv
Eigen::Matrix< float_t, dimy, 1 > osv
Definition: rbpf.h:712
pf::filters::kalman
A class template for Kalman filtering.
Definition: cf_filters.h:32
pf::filters::rbpf_hmm::arrayMod
std::array< cfModType, nparts > arrayMod
Definition: rbpf.h:60
pf::filters::rbpf_hmm_bs::arrayMod
std::array< cfModType, nparts > arrayMod
Definition: rbpf.h:409
pf::filters::rbpf_kalman::nsssv
Eigen::Matrix< float_t, dimnss, 1 > nsssv
Definition: rbpf.h:710
pf::filters::rbpf_hmm_bs::rbpf_hmm_bs
rbpf_hmm_bs(const unsigned int &resamp_sched=1)
The constructor.
Definition: rbpf.h:522
pf::filters::rbpf_hmm_bs::m_p_samps
arrayVec m_p_samps
Definition: rbpf.h:510
pf::filters::rbpf_kalman::Mat
Eigen::Matrix< float_t, Eigen::Dynamic, Eigen::Dynamic > Mat
Definition: rbpf.h:714
pf::filters::rbpf_kalman::initKalmanVar
virtual nsssMat initKalmanVar(const sssv &x21)=0
Provides the initial covariance matrix for each Kalman filter object.
pf::filters::rbpf_kalman::logFEv
virtual float_t logFEv(const sssv &x2t, const sssv &x2tm1)=0
Evaluates the state transition density for the second state component.
pf::filters::rbpf_kalman_bs::arrayfloat_t
std::array< float_t, nparts > arrayfloat_t
Definition: rbpf.h:1076
pf::filters::rbpf_kalman::sssv
Eigen::Matrix< float_t, dimss, 1 > sssv
Definition: rbpf.h:708
pf::filters::rbpf_hmm::m_expectations
std::vector< Mat > m_expectations
Definition: rbpf.h:203
pf::filters::rbpf_hmm::~rbpf_hmm
virtual ~rbpf_hmm()
The (virtual) destructor.
Definition: rbpf.h:219
pf::filters::rbpf_hmm::m_resampler
resamp_t m_resampler
Definition: rbpf.h:201
pf::filters::rbpf_hmm_bs::m_p_innerMods
arrayMod m_p_innerMods
Definition: rbpf.h:508
cf_filters.h
Inherit from this for a model that admits Kalman filtering.
pf::filters::rbpf_hmm_bs::arrayfloat_t
std::array< float_t, nparts > arrayfloat_t
Definition: rbpf.h:413
pf::filters::rbpf_hmm_bs::getExpectations
std::vector< Mat > getExpectations() const
Get vector of expectations.
Definition: rbpf.h:683
pf::filters::rbpf_hmm::m_rs
unsigned int m_rs
Definition: rbpf.h:193
pf::filters::rbpf_hmm::filter
void filter(const osv &data, const std::vector< std::function< const Mat(const nsssv &x1tLogProbs, const sssv &x2t)> > &fs=std::vector< std::function< const Mat(const nsssv &, const sssv &)> >())
Filter.
Definition: rbpf.h:223
pf::filters::rbpf_hmm::getLogCondLike
float_t getLogCondLike() const
Get the latest conditional likelihood.
Definition: rbpf.h:366
pf::filters::rbpf_hmm::rbpf_hmm
rbpf_hmm(const unsigned int &resamp_sched=1)
The constructor.
Definition: rbpf.h:209
pf::filters::rbpf_kalman::qSamp
virtual sssv qSamp(const sssv &x2tm1, const osv &yt)=0
Samples the time t second component.
pf::filters::rbpf_hmm_bs::nsssv
Eigen::Matrix< float_t, dimnss, 1 > nsssv
Definition: rbpf.h:399
pf::filters::rbpf_kalman_bs::fSamp
virtual sssv fSamp(const sssv &x2tm1)=0
Samples the time t second component.
pf::filters::rbpf_hmm::arrayVec
std::array< sssv, nparts > arrayVec
Definition: rbpf.h:54
pf::filters::rbpf_kalman_bs::initKalmanMean
virtual nsssv initKalmanMean(const sssv &x21)=0
Provides the initial mean vector for each Kalman filter object.
pf::filters::rbpf_hmm
Rao-Blackwellized/Marginal Particle Filter with inner HMMs.
Definition: rbpf.h:39
pf::filters::rbpf_kalman::m_p_innerMods
arrayMod m_p_innerMods
Definition: rbpf.h:854
pf::filters::rbpf_hmm::m_p_innerMods
arrayMod m_p_innerMods
Definition: rbpf.h:195
pf::filters::rbpf_kalman::m_rs
unsigned int m_rs
Definition: rbpf.h:852
pf::filters::rbpf_kalman::arrayMod
std::array< cfModType, nparts > arrayMod
Definition: rbpf.h:720
pf::filters::rbpf_hmm::nsssv
Eigen::Matrix< float_t, dimnss, 1 > nsssv
Definition: rbpf.h:46
pf::filters::rbpf_kalman::filter
void filter(const osv &data, const std::vector< std::function< const Mat(const nsssv &x1t, const sssv &x2t)> > &fs=std::vector< std::function< const Mat(const nsssv &x1t, const sssv &x2t)> >())
Filter!
Definition: rbpf.h:885
pf::filters::rbpf_kalman::logQ1Ev
virtual float_t logQ1Ev(const sssv &x21, const osv &y1)=0
Evaluates the proposal density of the second state component at time 1.
pf::filters::rbpf_hmm_bs
Rao-Blackwellized/Marginal Bootstrap Filter with inner HMMs.
Definition: rbpf.h:392
pf::filters::rbpf_kalman_bs::filter
void filter(const osv &data, const std::vector< std::function< const Mat(const nsssv &x1t, const sssv &x2t)> > &fs=std::vector< std::function< const Mat(const nsssv &x1t, const sssv &x2t)> >())
Filter!
Definition: rbpf.h:1195
pf::filters::rbpf_kalman::m_resampler
resamp_t m_resampler
Definition: rbpf.h:864
pf::filters::rbpf_kalman_bs::rbpf_kalman_bs
rbpf_kalman_bs(const unsigned int &resamp_sched=1)
The constructor.
Definition: rbpf.h:1181
pf::filters::rbpf_hmm_bs::initHMMLogTransMat
virtual nsssMat initHMMLogTransMat(const sssv &x21)=0
Provides the log transition matrix for each HMM filter object.
pf::filters::rbpf_kalman::m_expectations
std::vector< Mat > m_expectations
Definition: rbpf.h:866
pf::filters::rbpf_hmm::m_now
unsigned int m_now
Definition: rbpf.h:189
pf::filters::rbpf_hmm_bs::updateHMM
virtual void updateHMM(cfModType &aModel, const osv &yt, const sssv &x2t)=0
How to update your inner HMM filter object at each time.
pf::filters::rbpf_hmm::osv
Eigen::Matrix< float_t, dimy, 1 > osv
Definition: rbpf.h:48
pf::filters::rbpf_kalman_bs::initKalmanVar
virtual nsssMat initKalmanVar(const sssv &x21)=0
Provides the initial covariance matrix for each Kalman filter object.
pf::filters::rbpf_kalman::initKalmanMean
virtual nsssv initKalmanMean(const sssv &x21)=0
Provides the initial mean vector for each Kalman filter object.
pf::filters::rbpf_hmm_bs::m_logUnNormWeights
arrayfloat_t m_logUnNormWeights
Definition: rbpf.h:512
pf::filters::rbpf_kalman::logQEv
virtual float_t logQEv(const sssv &x2t, const sssv &x2tm1, const osv &yt)=0
Evaluates the proposal density at time t > 1.
pf::filters::rbpf_kalman_bs::updateKalman
virtual void updateKalman(cfModType &kMod, const osv &yt, const sssv &x2t)=0
How to update your inner Kalman filter object at each time.
pf::filters::rbpf_kalman::m_logUnNormWeights
arrayfloat_t m_logUnNormWeights
Definition: rbpf.h:858
pf::filters::rbpf_kalman_bs::nsssv
Eigen::Matrix< float_t, dimnss, 1 > nsssv
Definition: rbpf.h:1062
pf::filters::rbpf_kalman::m_lastLogCondLike
float_t m_lastLogCondLike
Definition: rbpf.h:862
pf::filters::rbpf_kalman_bs::osv
Eigen::Matrix< float_t, dimy, 1 > osv
Definition: rbpf.h:1064
pf::filters::rbpf_hmm::initHMMLogProbVec
virtual nsssv initHMMLogProbVec(const sssv &x21)=0
Provides the initial mean vector for each HMM filter object.
pf::filters::rbpf_kalman_bs::arrayVec
std::array< sssv, nparts > arrayVec
Definition: rbpf.h:1074
pf::filters::rbpf_hmm_bs::m_lastLogCondLike
float_t m_lastLogCondLike
Definition: rbpf.h:504