forked from helske/bssm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_sde_state_sampler.cpp
More file actions
47 lines (35 loc) · 1.62 KB
/
Copy pathR_sde_state_sampler.cpp
File metadata and controls
47 lines (35 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "model_ssm_sde.h"
#include "sitmo.h"
#include "filter_smoother.h"
#include "summary.h"
// not used anywhere?!?
// [[Rcpp::export]]
Rcpp::List sde_state_sampler_bsf_is2(const arma::vec& y, const double x0,
const bool positive, SEXP drift_pntr, SEXP diffusion_pntr,
SEXP ddiffusion_pntr, SEXP log_prior_pdf_pntr, SEXP log_obs_density_pntr,
const unsigned int nsim,
const unsigned int L_f, const unsigned int seed,
const arma::vec& approx_loglik_storage, const arma::mat& theta) {
Rcpp::XPtr<fnPtr> xpfun_drift(drift_pntr);
Rcpp::XPtr<fnPtr> xpfun_diffusion(diffusion_pntr);
Rcpp::XPtr<fnPtr> xpfun_ddiffusion(ddiffusion_pntr);
Rcpp::XPtr<prior_fnPtr> xpfun_prior(log_prior_pdf_pntr);
Rcpp::XPtr<obs_fnPtr> xpfun_obs(log_obs_density_pntr);
ssm_sde model(y, theta.col(0), x0, positive, *xpfun_drift,
*xpfun_diffusion, *xpfun_ddiffusion, *xpfun_obs, *xpfun_prior, L_f, L_f, seed);
arma::vec weights(theta.n_cols);
arma::cube alpha(model.n + 1, 1, theta.n_cols);
std::uniform_int_distribution<unsigned int> sample(0, nsim - 1);
for (unsigned int i = 0; i < theta.n_cols; i++) {
model.theta = theta.col(i);
arma::cube alpha_i(1, model.n + 1, nsim);
arma::mat weights_i(nsim, model.n + 1);
arma::umat indices(nsim, model.n);
double loglik = model.bsf_filter(nsim, L_f, alpha_i, weights_i, indices);
weights(i) = std::exp(loglik - approx_loglik_storage(i));
filter_smoother(alpha_i, indices);
alpha.slice(i) = alpha_i.slice(sample(model.engine)).t();
}
return Rcpp::List::create(Rcpp::Named("alpha") = alpha,
Rcpp::Named("weights") = weights);
}