CIS practical 2
Computer Intensive Statistics: APTS 2024–25 Computer Practical 2
Markov Chains and Monte Carlo
Andi Wang (andi.wang@warwick.ac.uk), Richard Everitt (richard.everitt@warwick.ac.uk)
June 2025
The first problem sheet contained several problems and if you want to continue working on those that is fine; this sheet just contains a couple of simple questions to give you a chance to try some Markov chain-based problems.
-
A warm-up which also appeared in the preliminary material; if you’ve never implemented something like this before then this might be a useful preliminary step.
In a simplified model of the game of Monopoly, we consider the motion of the piece around a loop of 40 spaces. We can model this as a Markov chain with a state space consisting of the integers (0,...,39) in which the transition kernel adds the result of two six-sided dice to the current state modulo 40 to obtain the new state.
-
Implement a piece of R code which simulates this Markov chain.
-
Run the code for a large number of iterations, say 100,000, and plot a histogram of the states visited.
-
Based on the output of the chain, would you conjecture that there is an invariant distribution for this Markov chain? If so, what?
-
Write the transition kernel down mathematically.
-
Check whether the Markov kernel you have written down is invariant with respect to any distribution conjectured in part (c).
-
-
An actual Gibbs Sampler.
Recall the Poisson changepoint model discussed in lectures, and on p21-22 of the supporting notes, and think about the following closely related model: Observations y_1,...,y_n comprise a sequence of M iid N(\mu_1,1) random variables followed by a second sequence of n-M iid N( \mu_2,1) random variables. (M), \mu_1 and \mu_2 are unknown. The prior distribution over M is a discrete uniform distribution on 1,...,n-1 (there is at least one observation of each component). The prior distribution over \mu_i (i=1,2) is N( 0,10^2). The three parameters are treated as being a priori independent.
-
Write down the joint density of y_1,...,y_n, \mu_1,\mu_2 and M, and obtain the posterior distribution of \mu_1,\mu_2 and M, up to proportionality, in as simple a form as you can.
-
Find the “full conditional” distributions of \mu_1, \mu_2 and M. (i.e. the conditional distributions of each of these variables given all other variables).
-
Implement a Gibbs sampler making use of these full conditional distributions in order to target the posterior distribution identified in part (b).
-
Simulate some data from the model for various parameter values and test your Gibbs sampler.
-
How might you extend this algorithm if instead of a changepoint model you had a mixture model in which every observation is drawn from a mixture, i.e.: Y_1,...,Y_n iid ~ p N ( . ; \mu_1,1) + (1-p) N ( . ; \mu_2,1). (The likelihood is now \prod_{i=1}^n [ p N ( y_i ; \mu_1,1) + (1-p) N ( y_i ; \mu_2,1)], with p, \mu_1, and \mu_2 unknown (and M is no longer a parameter of the model.)
Consider the following things:
-
The prior distribution over p.
-
Any other variables you may need to introduce.
-
The resulting algorithm.
If you have time, implement the resulting algorithm and apply it to some simulated data.
-
-