Skip to main content

Scientific Computing

Bayesian Inference

Applied Bayes... []

This material has been adapted from material by Ben Lambert from the "Scientific Computing" module of the SABS R³ Center for Doctoral Training.

This material has been adapted from material by Ben Lambert from the "Scientific Computing" module of the SABS R³ Center for Doctoral Training.

Creative Commons License
This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

Creative Commons License

Applied Bayesian Inference - AM exercise

You can download the slides for the AM lecture here

Logistic growth

In this example, we are going to perform inference for the logistic growth model commonly used in population biology to describe resource-limited growth. The population density of a given bacteria is supposed to follow:

dydt=ry(1yk)\frac{d y}{d t} = r y (1 - \frac{y}{k})

where we assume y(0)>0y(0)>0; r>0r > 0 is the initial (exponential) growth rate when resources are abundant; κ>0\kappa > 0 is the carrying capacity denoting the steady-state population density.

Logistic growth

Using Scipy's integrator, write a function which numerically solves the logistic equation. Plot the solution for y(0)=5,r=0.2,κ=20y(0)=5, r=0.2, \kappa=20 from t=0t=0 to t=40t=40.

Observed data

Assume that the observed data differs from the logistic model solution according to the measurement equation:

y~(t)N(y(t),σ),\tilde y(t) \sim \mathcal{N}(y(t), \sigma),

where σ>0\sigma >0 controls the measurement variability. Generate 'observed' data at t=0,5,10,15,20,25,30,35,40t= 0, 5, 10, 15, 20, 25, 30, 35, 40 assuming y(0)=5,r=0.2,κ=20,σ=2y(0)=5, r=0.2, \kappa=20, \sigma=2. Plot these data overlaid on top of the true solution.

Logistic likelihood

The likelihood for this model is given by:

L=i=1N12πσ2exp((y~(ti)y(ti))22σ2)L = \prod_{i=1}^{N} \frac{1}{\sqrt{2\pi\sigma^2}} \exp(-\frac{(\tilde y(t_i) - y(t_i))^2}{2\sigma^2})

where NN is the number of observations and y(t)=y(tr,κ,y(0))y(t) = y(t|r,\kappa, y(0)) is the ODE solution at time tt. Write a function which calculates the log-likelihood for given (r,κ,y(0),σ)(r, \kappa, y(0), \sigma).

Plot likelihood

Using the simulated data you previously generated, plot the log-likelihood as a function of rr holding all other parameters at their true values.

Plot likelihood contour

Plot the likelihood (not the log-likelihood as this is harder to distinguish) contour surface for (r,k)(r,k) holding all other parameters at their true values.

Prior predictive

Assume we have priors on parameters: rN(0.2,0.02),κN(20,4)r\sim \mathcal{N}(0.2, 0.02), \kappa\sim \mathcal{N}(20, 4), and we fix y(0)=5y(0)=5. Generate 100 prior predictive simulations of the ODE solution yy and plot these. Remember, a single prior predictive simulation is obtained by sampling each parameter from its prior and (in this case) solving the ODE for this parameter set.

Initial condition

Now also allow y(0)N(5,1)y(0)\sim \mathcal{N}(5, 1). How does the prior predictive distribution change?

Metropolis Sampler: Proposal

We are now going to write a random walk Metropolis sampler. The first step is to write a method which takes as input (r,κ,σ)(r,\kappa, \sigma) and proposes new values using univariate normal distributions centered at the current values. So, for example,

rN(r,τr)r'\sim\mathcal{N}(r,\tau_r)

where τr\tau_r is a hyperparameter of the method. Write such a function.

Metropolis Sampler: Prior

Assume priors: rN(0.2,0.02),κN(20,4),σN(2,0.2)r\sim \mathcal{N}(0.2, 0.02), \kappa\sim \mathcal{N}(20, 4), \sigma \sim \mathcal{N}(2, 0.2) and fix y(0)=5y(0)=5. Write a function that accepts as input (r,κ,σ)(r,\kappa, \sigma) and outputs the log-prior density log-p(r,κ,σ)\log{\text -}p(r,\kappa, \sigma).

Metropolis Sampler: Posterior

Write a function which calculates the unnormalised log-posterior (i.e. the sum of the log-prior and log-likelihood), log-p(r,κ,σdata)\text{log-}p(r,\kappa,\sigma|\text{data}), for a given parameter set.

Metropolis Sampler: Step Accept

The next step is to write a function called 'step_accept' which takes as input (r,κ,σ)(r,\kappa, \sigma), proposes new values (r,κ,σ)(r',\kappa', \sigma'). Then calculates the ratio:

t=exp(log-p(r,κ,σdata)log-p(r,κ,σdata))t = \exp(\text{log-}p(r',\kappa', \sigma'|\text{data}) - \text{log-}p(r,\kappa, \sigma|\text{data}))

Then generates uU(0,1)u\sim U(0,1) and does the following: if tut \geq u, return (r,κ,σ)(r',\kappa',\sigma'); else return (r,κ,σ)(r,\kappa,\sigma).

Metropolis Sampler: MCMC

Write a function which iterates 'step_accept' generating a chain of MCMC samples of (r,κ,σ)(r,\kappa,\sigma). Initialise (r,κ,σ)(r,\kappa,\sigma) using samples from the prior.

Metropolis Sampler: Plot sampled values

Assuming step sizes of (τr=0.01,τk=1,τ_σ=0.1)(\tau_r=0.01, \tau_k=1, \tau\_{\sigma} = 0.1) , generate an MCMC sample of 1000 draws. Visualise the sampled values of rr over time.

Metropolis Sampler: Plot sampled values contour

Plot the pairwise distribution of (r,κ)(r,\kappa). How do the sampled values compare to the true parameters?

Metropolis Sampler: Convergence

Modify your MCMC routine to use the following half-normal distributions to sample initial points for your chains. Run four independent chains for 1000 iterations each and plot the κ\kappa samples over time. How long does it appear your chains be run until they reach a stationary distribution?

rhalf-N(0.2,0.1)r \sim \text{half-N}(0.2, 0.1)
κhalf-N(0,20,10)\kappa \sim \text{half-N}(0, 20, 10)
σhalf-N(2,1)\sigma \sim \text{half-N}(2, 1)

Note, to do so, you can use the following function:

def truncated_normal(mu, sd): myclip_a = 0 myclip_b = 1000000 my_mean = mu my_std = sd a, b = (myclip_a - my_mean) / my_std, (myclip_b - my_mean) / my_std r = scipy.stats.truncnorm.rvs(a, b, loc=my_mean, scale=my_std, size=1) return r[0]

Metropolis Sampler: Posterior predictive

Using a random subset of 100 samples from all four chains taken from after they appear to have reached the stationary distribution, draw from the posterior predictive distribution of the logistic equation solution and overlay the observations.