# Takes a while on CRAN
library("sde")
set.seed(1)
# theta_0 = rho = 0.5
# theta_1 = nu = 2
# theta_2 = sigma = 0.3
x <- sde.sim(t0 = 0, T = 50, X0 = 1, N = 50,
drift = expression(0.5 * (2 - x)),
sigma = expression(0.3),
sigma.x = expression(0))
y <- rpois(50, exp(x[-1]))
# source c++ snippets
pntrs <- cpp_example_model("sde_poisson_OU")
sde_model <- ssm_sde(y, pntrs$drift, pntrs$diffusion,
pntrs$ddiffusion, pntrs$obs_density, pntrs$prior,
c(rho = 0.5, nu = 2, sigma = 0.3), 1, positive = FALSE)
est <- particle_smoother(sde_model, L = 12, particles = 500)
ts.plot(cbind(x, est$alphahat,
est$alphahat - 2*sqrt(c(est$Vt)),
est$alphahat + 2*sqrt(c(est$Vt))),
col = c(2, 1, 1, 1), lty = c(1, 1, 2, 2))
# Takes time with finer mesh, parallelization with IS-MCMC helps a lot
out <- run_mcmc(sde_model, L_c = 4, L_f = 8,
particles = 50, iter = 2e4,
threads = 4L)
Run the code above in your browser using DataLab