In the previous post I gave a quick introduction to the CRAN R package `smfsb`, and how it can be used for simulation of Markov processes determined by stochastic kinetic networks. In this post I’ll show how to use data and particle MCMC techniques in order to carry out Bayesian inference for the parameters of partially observed Markov processes.

### The simulation model and the data

For this post we will assume that the `smfsb` package is installed and loaded (see previous post for details). The package includes the function `stepLVc` which simulates from the Markov transition kernel of a Lotka-Volterra process by calling out to some native C code for speed. So, for example,

stepLVc(c(x1=50,x2=100),0,1)

will simulate the state of the process at time 1 given an initial condition of 50 prey and 100 predators at time 0, using the default rate parameters of the function, `th = c(1, 0.005, 0.6)`. The package also includes some data simulated from this model using these parameters, with and without added noise. The datasets can be loaded with

data(LVdata)

For simplicity, we will just make use of the dataset `LVnoise10` in this post. This dataset is a multivariate time series consisting of 16 equally spaced observations on both prey and predators subject to Gaussian measurement error with a standard deviation of 10. We can plot the data with

plot(LVnoise10,plot.type="single",col=c(2,4))

giving:

The Bayesian inference problem is to see how much we are able to learn about the parameters which generated the data using only the data and our knowledge of the structure of the problem. There are many approaches one can take to this problem, but most are computationally intensive, due to the analytical intractability of the transition kernel of the LV process. Here we will follow Wilkinson (2011) and take a particle MCMC (pMCMC) approach, and specifically, use a pseudo-marginal “exact approximate” MCMC algorithm based on the particle marginal Metropolis-Hastings (PMMH) algorithm. I have discussed the pseudo-marginal approach, using particle filters for marginal likelihood estimation, and the PMMH algorithm in previous posts, so if you have been following my posts for a while, this should all make perfect sense…

### Particle filter

One of the key ingredients required to implement the pseudo-marginal MCMC scheme is a (bootstrap) particle filter which generates an unbiased estimate of the marginal likelihood of the data given the parameters (integrated over the unobserved state trajectory). The algorithm was discussed in this post, and R code to implement this is included in the `smfsb` R package as `pfMLLik`. For reasons of numerical stability, the function computes and returns the log of the marginal likelihood, but it is important to understand that it is the actually likelihood estimate that is unbiased for the true likelihood, and not the corresponding statement for the logs. The actual code of the function is relatively short, and for completeness is given below:

pfMLLik <- function (n, simx0, t0, stepFun, dataLik, data) { times = c(t0, as.numeric(rownames(data))) deltas = diff(times) return(function(...) { xmat = simx0(n, t0, ...) ll = 0 for (i in 1:length(deltas)) { xmat = t(apply(xmat, 1, stepFun, t0 = times[i], deltat = deltas[i], ...)) w = apply(xmat, 1, dataLik, t = times[i + 1], y = data[i,], log = FALSE, ...) if (max(w) < 1e-20) { warning("Particle filter bombed") return(-1e+99) } ll = ll + log(mean(w)) rows = sample(1:n, n, replace = TRUE, prob = w) xmat = xmat[rows, ] } ll }) }

We need to set up the prior and the data likelihood correctly before we can use this function, but first note that the function does not actually run a particle filter at all, but instead stores everything it needs to know to run the particle filter in the local environment, and then returns a function closure for evaluating the marginal likelihood at a given set of parameters. The resulting function (closure) can then be used to run a particle filter for a given set of parameters, simply by passing the required parameters into the function. This functional programming style is consistent with that used throughout the `smfsb` R package, and leads to quite simple, modular code. To use `pfMLLik`, we first need to define a function which evaluates the log-likelihood of an observation conditional on the true state, and another which samples from the prior distribution of the initial state of the system. Here, we can do that as follows.

# set up data likelihood noiseSD=10 dataLik <- function(x,t,y,log=TRUE,...) { ll=sum(dnorm(y,x,noiseSD,log=TRUE)) if (log) return(ll) else return(exp(ll)) } # now define a sampler for the prior on the initial state simx0 <- function(N,t0,...) { mat=cbind(rpois(N,50),rpois(N,100)) colnames(mat)=c("x1","x2") mat } # convert the time series to a timed data matrix LVdata=as.timedData(LVnoise10) # create marginal log-likelihood functions, based on a particle filter mLLik=pfMLLik(100,simx0,0,stepLVc,dataLik,LVdata)

Now the function (closure) `mLLik` will, for a given parameter vector, run a particle filter (using 100 particles) and return the log of the particle filter’s unbiased estimate of the marginal likelihood of the data. It is then very easy to use this function to create a simple PMMH algorithm for parameter inference.

### PMMH algorithm

Below is an algorithm based on flat priors and a simple Metropolis-Hastings update for the parameters using the function closure `mLLik`, defined above.

iters=1000 tune=0.01 thin=10 th=c(th1 = 1, th2 = 0.005, th3 = 0.6) p=length(th) ll=-1e99 thmat=matrix(0,nrow=iters,ncol=p) colnames(thmat)=names(th) # Main pMCMC loop for (i in 1:iters) { message(paste(i,""),appendLF=FALSE) for (j in 1:thin) { thprop=th*exp(rnorm(p,0,tune)) llprop=mLLik(thprop) if (log(runif(1)) < llprop - ll) { th=thprop ll=llprop } } thmat[i,]=th } message("Done!") # Compute and plot some basic summaries mcmcSummary(thmat)

This will take a little while to run, but in the end should give a plot something like the following (click for full size):

So, although we should really run the chain for a bit longer, we see that we can learn a great deal about the parameters of the process from very little data. For completeness, a full runnable demo script is included below the references. Of course there are many obvious extensions of this basic problem, such as partial observation (eg. only observing the prey) and unknown measurement error. These are discussed in Wilkinson (2011), and code for these cases is included within the `demo(PMCMC)`, which should be inspected for further details.

### Discussion

At this point it is probably worth emphasising that there are other “likelihood free” approaches which can be taken to parameter inference for partially observed Markov process (POMP) models. Many of these are implemented in the `pomp` R package, also available from CRAN, by King et al (2008). The `pomp` package is well documented, and has a couple of good tutorial vignettes which should be sufficient to get people started. The API of the package is rather cumbersome, but the algorithms appear to be quite robust. Approximate Bayesian computation (ABC) approaches are also quite natural for POMP models (see, for example, Toni et al (2009)). This is because “exact” likelihood free procedures break down in the case of low/no measurement error or high-dimensional observations. There are some R packages for ABC, but I am not sufficiently familiar with them to be able to give recommendations.

If one is able to move away from the “likelihood free” paradigm, it is possible to develop “exact” pMCMC algorithms which do not break down in challenging observation scenarios. The problem here is the intractability of the Markov transition kernel. In the case of nonlinear Markov jump processes, finding very generic solutions seems quite difficult, but for diffusion (approximation) processes based on stochastic differential equations, it seems to be possible to develop irreducible pMCMC algorithms which have very broad applicability – see Golightly and Wilkinson (2011) for further details of how such techniques can be used in the context of stochastic kinetic models similar to those considered in this post.

### References

*Interface Focus*,

**1**(6):807-820.

*CRAN*.

*J. R. Soc. Interface*

**6**(31): 187-202.

### Demo script

require(smfsb) data(LVdata) # set up data likelihood noiseSD=10 dataLik <- function(x,t,y,log=TRUE,...) { ll=sum(dnorm(y,x,noiseSD,log=TRUE)) if (log) return(ll) else return(exp(ll)) } # now define a sampler for the prior on the initial state simx0 <- function(N,t0,...) { mat=cbind(rpois(N,50),rpois(N,100)) colnames(mat)=c("x1","x2") mat } # convert the time series to a timed data matrix LVdata=as.timedData(LVnoise10) # create marginal log-likelihood functions, based on a particle filter mLLik=pfMLLik(100,simx0,0,stepLVc,dataLik,LVdata) iters=1000 tune=0.01 thin=10 th=c(th1 = 1, th2 = 0.005, th3 = 0.6) p=length(th) ll=-1e99 thmat=matrix(0,nrow=iters,ncol=p) colnames(thmat)=names(th) # Main pMCMC loop for (i in 1:iters) { message(paste(i,""),appendLF=FALSE) for (j in 1:thin) { thprop=th*exp(rnorm(p,0,tune)) llprop=mLLik(thprop) if (log(runif(1)) < llprop - ll) { th=thprop ll=llprop } } thmat[i,]=th } message("Done!") # Compute and plot some basic summaries mcmcSummary(thmat)

## 4 thoughts on “Particle filtering and pMCMC using R”