8 #ifdef DROPPINGTHISINRPACKAGE
12 #include <Eigen/Dense>
38 template<
size_t nparts,
size_t dimx,
size_t dimy,
typename resamp_t,
typename float_t,
bool debug=false>
44 using ssv = Eigen::Matrix<float_t,dimx,1>;
46 using osv = Eigen::Matrix<float_t,dimy,1>;
48 using Mat = Eigen::Matrix<float_t,Eigen::Dynamic,Eigen::Dynamic>;
62 APF(
const unsigned int &rs=1);
89 void filter(
const osv &data,
const std::vector<std::function<
const Mat(
const ssv&)> >& fs = std::vector<std::function<
const Mat(
const ssv&)> >());
139 virtual float_t
logGEv (
const osv &yt,
const ssv &xt) = 0;
171 template<
size_t nparts,
size_t dimx,
size_t dimy,
typename resamp_t,
typename float_t,
bool debug>
174 , m_logLastCondLike(0.0)
181 template<
size_t nparts,
size_t dimx,
size_t dimy,
typename resamp_t,
typename float_t,
bool debug>
185 template<
size_t nparts,
size_t dimx,
size_t dimy,
typename resamp_t,
typename float_t,
bool debug>
193 arrayfloat_t logFirstStageUnNormWeights = m_logUnNormWeights;
195 float_t m3(-std::numeric_limits<float_t>::infinity());
196 float_t m2(-std::numeric_limits<float_t>::infinity());
197 for(
size_t ii = 0; ii < nparts; ++ii)
200 if(m_logUnNormWeights[ii] > m3)
201 m3 = m_logUnNormWeights[ii];
204 ssv xtm1 = oldPartics[ii];
205 logFirstStageUnNormWeights[ii] += logGEv(data, propMu(xtm1));
208 if(logFirstStageUnNormWeights[ii] > m2)
209 m2 = logFirstStageUnNormWeights[ii];
212 #ifndef DROPPINGTHISINRPACKAGE
213 if constexpr(debug) {
214 std::cout <<
"time: " << m_now
215 <<
", first stage log unnorm weight: " << logFirstStageUnNormWeights[ii]
224 arrayUInt myKs = m_kGen.sample(logFirstStageUnNormWeights);
227 float_t m1(-std::numeric_limits<float_t>::infinity());
228 float_t first_cll_sum(0.0);
229 float_t second_cll_sum(0.0);
230 float_t third_cll_sum(0.0);
233 for(
size_t ii = 0; ii < nparts; ++ii)
236 second_cll_sum += std::exp( logFirstStageUnNormWeights[ii] - m2 );
237 third_cll_sum += std::exp( m_logUnNormWeights[ii] - m3 );
240 xtm1k = oldPartics[myKs[ii]];
241 m_particles[ii] = fSamp(xtm1k);
243 m_logUnNormWeights[ii] += logGEv(data, m_particles[ii]) - logGEv(data, muT);
246 #ifndef DROPPINGTHISINRPACKAGE
248 std::cout <<
"time: " << m_now
249 <<
", transposed sample: " << m_particles[ii].transpose()
250 <<
", log unnorm weight: " << m_logUnNormWeights[ii] <<
"\n";
255 if(m_logUnNormWeights[ii] > m1)
256 m1 = m_logUnNormWeights[ii];
260 for(
size_t p = 0; p < nparts; ++p)
261 first_cll_sum += std::exp( m_logUnNormWeights[p] - m1 );
262 m_logLastCondLike = m1 + std::log(first_cll_sum) + m2 + std::log(second_cll_sum) - 2*m3 - 2*std::log(third_cll_sum);
264 #ifndef DROPPINGTHISINRPACKAGE
266 std::cout <<
"time: " << m_now <<
", log cond like: " << m_logLastCondLike <<
"\n";
273 Mat testOutput = h(m_particles[0]);
274 unsigned int rows = testOutput.rows();
275 unsigned int cols = testOutput.cols();
276 Mat numer = Mat::Zero(rows,cols);
279 for(
size_t prtcl = 0; prtcl < nparts; ++prtcl){
280 numer += h(m_particles[prtcl]) * std::exp(m_logUnNormWeights[prtcl] - m1);
281 denom += std::exp(m_logUnNormWeights[prtcl] - m1);
283 m_expectations[fId] = numer/denom;
285 #ifndef DROPPINGTHISINRPACKAGE
287 std::cout <<
"transposed expectation " << fId <<
"; " << m_expectations[fId] <<
"\n";
294 if( (m_now+1)%m_rs == 0)
295 m_resampler.resampLogWts(m_particles, m_logUnNormWeights);
302 float_t max(-std::numeric_limits<float_t>::infinity());
303 for(
size_t ii = 0; ii < nparts; ++ii)
306 m_particles[ii] = q1Samp(data);
307 m_logUnNormWeights[ii] = logMuEv(m_particles[ii]);
308 m_logUnNormWeights[ii] += logGEv(data, m_particles[ii]);
309 m_logUnNormWeights[ii] -= logQ1Ev(m_particles[ii], data);
312 #ifndef DROPPINGTHISINRPACKAGE
313 if constexpr(debug) {
314 std::cout <<
"time: " << m_now
315 <<
", log unnorm weight: " << m_logUnNormWeights[ii]
316 <<
", transposed sample: " << m_particles[ii].transpose()
322 if( m_logUnNormWeights[ii] > max)
323 max = m_logUnNormWeights[ii];
328 for(
size_t i = 0; i < nparts; ++i){
329 sumExp += std::exp( m_logUnNormWeights[i] - max );
331 m_logLastCondLike = - std::log(
static_cast<float_t
>(nparts) ) + max + std::log(sumExp);
334 m_expectations.resize(fs.size());
338 Mat testOutput = h(m_particles[0]);
339 unsigned int rows = testOutput.rows();
340 unsigned int cols = testOutput.cols();
341 Mat numer = Mat::Zero(rows,cols);
343 for(
size_t prtcl = 0; prtcl < nparts; ++prtcl){
344 numer += h(m_particles[prtcl]) * std::exp(m_logUnNormWeights[prtcl] - max);
345 denom += std::exp(m_logUnNormWeights[prtcl] - max);
347 m_expectations[fId] = numer/denom;
349 #ifndef DROPPINGTHISINRPACKAGE
351 std::cout <<
"transposed expectation " << fId <<
"; " << m_expectations[fId] <<
"\n";
358 if( (m_now+1) % m_rs == 0)
359 m_resampler.resampLogWts(m_particles, m_logUnNormWeights);
368 template<
size_t nparts,
size_t dimx,
size_t dimy,
typename resamp_t,
typename float_t,
bool debug>
371 return m_logLastCondLike;
375 template<
size_t nparts,
size_t dimx,
size_t dimy,
typename resamp_t,
typename float_t,
bool debug>
378 return m_expectations;