The following example was adapted from the post in RPubs.
Simulate the data
Generate \(y_{1:T}\) as a sequence of noisy observations of a latent variable \(x_{1:T}\).
Code
# create a data set: x (latent variable) and y (observation)set.seed(42) # to make it reproducible (lots of random numbers follow)T <-50# number of observationsx <-rep(NA, T) # latent variabley <-rep(NA, T) # observed valuessx <-2.2# standard deviation for xsy <-0.3# standard deviation for yx[1] <-rnorm(1, 0, 1)y[1] <-rnorm(1, x[1], sy)for (t inseq(2, T)) { x[t] <-rnorm(1, x[t-1], sx) y[t] <-rnorm(1, x[t], sy)}x_true <- xobs <- y
Implement a particle filter (sequential Monte Carlo)
Code
# particle filter -----------------------------------------------------------T <-length(y) # number of observationsN <-100# number of particles# to store prior distributions for variables correspond to latent variable xx_prior <-matrix(nrow=N, ncol = T) x_post <-matrix(nrow=N, ncol = T) # posterior distributionsweights <-matrix(nrow=N, ncol = T) # weights used to draw posterior sampleW <-matrix(nrow = N, ncol = T) # normalized weightsA <-matrix(nrow = N, ncol = T) # indices based on the normalized weightsx_prior[, 1] <-rnorm(N, 0, sx)# initial X from a normal distribution# calculate weights, normal likelihoodweights[, 1] <-dnorm(obs[1], x_prior[, 1], sy)W[, 1] <- weights[, 1]/sum(weights[, 1])# normalise weights# indices based on the weighted resampling with replacement A[, 1] <-sample(1:N, prob = W[1:N, 1], replace = T) x_post[, 1] <- x_prior[A[, 1], 1] # posterior distribution using the indicesfor (t inseq(2, T)) { x_prior[, t] <-rnorm(N, x_post[, t-1], sx) # prior x_{t} based on x_{t-1} weights[, t] <-dnorm(obs[t], x_prior[, t], sy) # calculate weights W[, t] <- weights[, t]/sum(weights[, t]) # normalise weights A[, t] <-sample(1:N, prob = W[1:N, t], replace = T) # indices x_post[, t] <- x_prior[A[, t], t] # posterior samples}
Summarize results
Calculate the mean and 2.5\(^\textrm{th}\) and 97.\(^\textrm{th}\) percentile of the posterior sample as a means to get 95% credible interval.