Solution

ID: past-exam-of-the-msc-in-advanced-computer-science-course-of-the-university-of-oxford/2021/bayesian-statistical-probabilistic-programming/1/e/solution

Use the Poisson distribution with probability mass function on . A symmetric nearest-neighbour proposal with a reflecting boundary is
All other entries vanish. In particular, the boundary proposal does not force a move to one: retaining the probability makes .
The adjacent ratios of the Poisson distribution are and . The Metropolis–Hastings acceptance probability consequently gives
where the symbol denotes the upward transition probability. Thus the complete transition matrix is
with no downward entry at zero and all other entries zero. In matrix form,
Every row is nonnegative and sums to one. An executable Python step of this Poisson Metropolis chain is:
def poisson_step(n, lam, rng):
    if lam <= 0 or n < 0 or int(n) != n:
        raise ValueError("require lam > 0 and an integer n >= 0")
    y = n + 1 if rng.random() < 0.5 else max(0, n - 1)
    ratio = lam / (n + 1) if y > n else n / lam if y < n else 1.0
    return y if rng.random() < min(1.0, ratio) else n
Supply a pseudorandom number generator such as random.Random(2021) and retain each returned state, including repeats. No normalizing constant or evaluation of a factorial is needed by the algorithm.

New to topics? Read the docs here!