outline

introduction

context and goal

  • Bayesian statistics is becoming more important
  • many problems in Bayesian statistics are solved through simulation (thus, computation = simulation)
  • it is good as a (lead) user to have some idea of what’s going on
  • give you an overview of methods for Bayesian computing using simulations
  • we will try to focus on the general picture with a balance between theory and practice
  • have some simple do-it-yourself exercises

simulation: only for Bayes?

  • no! simulation is useful in many different contexts
    • learning the properties of your model (certainly when complex)
    • debugging
    • also in classical statistics: bootstrapping, permutation tests, model checking
    • …
  • because of the general importance of the topic, we will postpone the connection between simulation and Bayes for now

first some notation

  • \(X\) is a random variable or (vector of) random variable(s): \(X = (X_1, \dots, X_k)\)
  • \(x\) is a realization (or particular value) of \(X\)
  • occasionally, we will also use \(U\), \(\theta\), \(\dots\)
  • distribution of \(X\) is \(p\), this can be denoted in several ways
    • \(X \sim p\)
    • \(p(x)\) is the density function for \(X\) (pdf)
    • \(P(x) = \Pr(X \leq x)\) is the distribution function \(X\) (cdf)

examples of notation

  • \(X \sim N(\mu,\sigma^2)\) or
    • \(p(x) = N(x|\mu,\sigma^2)\)
    • \(p(x) = \frac{1}{\sqrt{2\pi}\sigma} e^{-\frac{1}{2}\frac{(x - \mu)^2}{\sigma^2}}\)
  • \(\theta \sim \text{beta}(a, b)\) or
    • \(p(\theta) = \text{beta}(\theta | a, b)\)
    • \(p(\theta) = \frac{1}{B(a, b)} \theta^{a - 1} (1 - \theta)^{b - 1}\)
  • \(U \sim U(0, 1)\) or
    • \(p(u) = 1\)

the problem

  • assume that \(p(x) = \text{beta}(x|a,b)\)
  • we’d like to know: \(E(X)\), \(\Pr(.2 < X < .8)\), \(\dots\)
  • classical solutions: try to derive this analytically, go to Wikipedia, \(\dots\)
    • e.g., \(E(X) = \frac{a}{a + b}\)
  • but what with
    • \(\Pr(.2 < X \leq .8) = ?\)
    • \(\text{median}(X) = ?\)

general solution

  • draw a random sample and use this sample to solve the problem
  • \(x^1, \dots, x^m, \dots, x^M \sim \text{beta}(a, b)\)
    • \(x^m\): the \(m\)th simulated value
  • from these \(M\) sampled values, we can estimate the various quantities

Monte Carlo integration: introduction

  • fundamental insight: most (but not all) things we want to compute from a distribution, can be written as an expectation or an integral \[E_p \left( f(X) \right) = \int f(x) p(x) dx\]
  • when clear from the context, we ignore the subscript \(p\)
  • examples
    • \(E(X) = \int x p(x) dx\) thus \(f(x) = x\)
    • \(\Pr(.2 < X \leq .8) = E\left( I_{.2 < X \leq .8}(X) \right)\) thus \(f(x) = I_{.2 < x \leq .8}(x)\)
      • \(I_A(x)\) is the indicator function taking value 1 if \(x \in A\) and 0 otherwise
      • note that \(\int I_{.2 < x \leq .8}(x) p(x) dx = \int_{.2}^{.8} p(x) dx\)

exercises i

exercises i

[1] How would you write \(\text{var}(X)\) as an expectation?

[2] Can you think of a quantity that cannot be written as an expectation but that nevertheless can be estimated by using random draws from a distribution?

some more Monte Carlo integration

  • in general, we want to know \(J = E_p \left( f(X) \right) = \int f(x) p(x) dx\)
  • hence we will estimate \(J\) using \(M\) simulations
  • so, if we have a sample \(x^1, \dots, x^m, \dots, x^M\) from \(p(x)\), we compute \[\hat{J} = \frac{1}{M} \sum_{m = 1}^M f(x^m)\]
  • by the law of large numbers, \(\hat{J} \rightarrow J\) as \(M \rightarrow \infty\)

an example: simulating from a beta distribution

exercises ii

exercises ii

[1] Estimate the mean of a standard normal distribution using a Monte Carlo sample (use rnorm()) with \(M = 10\) simulations, but also try larger values of \(M\) (e.g., 1000 or 10000).

[2] Assume \(X \sim N(0, 1)\). Estimate (using simulations, \(M = 1000\)) the probability that \(X\) is between \(-1.96\) than \(1.96\): \(\Pr(-1.96 < X < 1.96)\). (Note that you can also look at this probability as \(E\left( I_{|X| < 1.96}(X) \right)\) or \(\int_{-1.96}^{1.96} p(x) dx\).)

exercises ii

[3] Simulate \(M = 10000\) values from the standard normal. Compute the cumulative moving average/running mean: For each \(m\), \(\hat{J}_m = \frac{x_1 + \dots + x_m}{m}\). (Hint: use the cumsum() function and divide by 1:M.) Then plot the cumulative moving average/running mean \(\hat{J}_m\) against \(m\). You get something like the figure below. Discuss this result.

exercises ii

[4] A Monte Carlo classic: Estimate the value of \(\pi\) by simulating values in a square centered at the origin and with side lengths 2. Count the proportion of points falling in the inscribed circle (see plot) and multiply by 4 (why?) to get your estimate.

exercises ii

[5] Here’s an example of using simulations to estimate a quantity that cannot be written as an expectation. Estimate the \(.95\) (or \(95\%\)) quantile of a standard normal distribution. Check against the true value using the qnorm() function.

Monte Carlo: some history

  • Manhattan project:
    • nuclear fission
    • Stanislaw Ulam and his uncle
    • Neumann Janos Lajos/John von Neumann
  • before the age of digital computers:
    • needles and \(\pi\)
    • verifying the \(t\)-test procedure

Monte Carlo integration: the next step

  • we need efficient and reliable methods to simulate from distributions
  • there exist two broad type of methods
    • direct methods: they return an exact and independent sample (iid)
    • MCMC (markov chain Monte Carlo methods): return an approximate and dependent sample
  • for all these methods, a basic requirement is that we can sample uniformly distributed random numbers: \(U \sim U(0, 1)\)

pseudo random number generators (PRNG)

  • no real random numbers on a computer
  • instead: deterministic sequence that passes several tests for randomness
  • results can be reproduced exactly
  • PRNG needs to be seeded (set.seed())
  • default in R: Mersenne-Twister (period = \(2^{19937}-1\))

pseudo random number generators (PRNG): an example

set.seed(324)
runif(n = 4)
## [1] 0.72902036 0.42364850 0.57661580 0.09934177
set.seed(324)
runif(n = 4)
## [1] 0.72902036 0.42364850 0.57661580 0.09934177
runif(n = 4)
## [1] 0.70636940 0.60503700 0.71185461 0.06374409

outline

  • introduction: simulation, Monte Carlo integration, PRNG, exercises
  • direct sampling: transformations, rejection sampling, excercises
  • intermezzo 1: importance sampling, exercises
  • intermezzo 2: unnormalized densities, exercises
  • Markov chain Monte Carlo: general stuff, Metropolis, Gibbs, exercises

direct sampling

direct sampling

  • direct sampling algorithms for getting an iid sample
    • transformations of \(U\)
    • rejection sampling

transformation: probability integral transform (PIT)

  • PIT: general method for simulating from (univariate) distributions
  • if \(P\) is a cdf and \(P^{-1}\) is its inverse, then:
    • \(U \sim U(0,1)\)
    • calculate \(X = P^{-1}(U)\)
    • then: \(X \sim P\)
  • proof:
    • \(\Pr(X \leq x) = \Pr(P^{-1}(U) \leq x) = \Pr(U \leq P(x)) = P(x)\)
    • the last step is true because for a uniform distributed random variable it holds that \(\Pr(U \leq u) = u\)

illustration of PIT

  • example: \(X \sim \text{logistic}(0, 1)\) (the logistic distribution)
  • \(p(x) = \frac{e^x}{(1 + e^x)^2}\)
  • \(P(x) = \frac{1}{1 + e^{-x}}\)
  • \(P^{-1}(y) = \log \frac{y}{1 - y}\)

illustration of PIT

rejection sampling: the basics

  • if you want to sample from \(p(x)\) and you cannot use the previous methods
  • but suppose there is a pdf \(g(x)\), for which it holds:
    • there exists a \(C > 0\) such that \(Cg(x) \geq p(x)\) for all \(x\) (\(Cg(x)\) is called the envelope)
    • it is easy to sample from \(g(x)\) (e.g., using the previous methods)

rejection sampling: the actual algorithm

  • the rejection sampling algorithm is then as follows:
    1. generate a candidate \(x^*\) from \(g(x)\)
    2. simulate \(U \sim U(0, Cg(x^*))\), and accept if \(u \leq p(x^*)\)
    3. return to the first step otherwise

rejection sampling: an example

  • we want to simulate from a \(\text{beta}(a, b)\) distribution (with \(a, b \geq 1\)) but we don’t know how
  • however, assume we know that with \(a, b \geq 1\), the beta density is unimodal and the mode is located at \(\frac{a - 1}{a + b - 2}\)
  • \(C = \text{beta}\left( \frac{a - 1}{a + b - 2} | a, b \right)\)
  • so the constant \(C\) forms a suitable envelope in this case; therefore \(g(x) = 1\), the proposal generating distribution is a uniform

rejection sampling: step-by-step

rejection sampling: all at once

exercises iii

exercises iii

[1] Assume a positive real valued \(X\) follows an exponential distribution with density function \(p(x) = \lambda e^{-\lambda x}\) and distribution function \(P(x) = 1 - e^{-\lambda x}\) (with \(\lambda > 0\)). Simulate \(M = 1000\) random draws from an exponential distribution with \(\lambda = 2\) using the PIT method. Make a histogram and compare that to the density (using dexp()).

exercises iii

[2] Assume a real valued \(X\) follows a Laplace (or double exponential) distribution with density function \(g(x) = \frac{1}{2}e^{-|x|}\) (for \(x \in \cal{R}\)). Adapt your code from the first exercise to simulate from the Laplace distribution (\(M = 1000\)). Make a histogram and compare that to the density. (Hint: The Laplace density can be considered as an exponential density with \(\lambda = 1\) for the positive \(x\)-axis and the same density mirrored for the negative axis (divided by two in order to get a normalized pdf). With this knowledge, you can easily sample from the Laplace distribution, by first sampling from the exponential and then tossing a fair coin to add a minus sign.)

exercises iii

[3] Implement the rejection algorithm to sample from the beta distribution (with \(a, b \geq 1\)). What is the proportion of accepted proposals? Compare it to \(\frac{1}{C}\). Can you explain this? What goes wrong if \(a < 1\) or \(b < 1\)?

exercises iii

[4] Implement a rejection algorithm to sample from the standard normal distribution \(p(x)\). Use as \(g(x)\) the Laplace density from Exercise [2] and \(C = \frac{2e^{\frac{1}{2}}}{\sqrt{2\pi}}\). (You may also show that \(C g(x) \geq p(x)\) by using the following identity for this relation: \(\frac{x^2}{2} + \frac{1}{2} - |x| = \frac{1}{2}(|x| - 1)^2 \geq 0\).)

exercises iii

[5] A bivariate rejection algorithm: How to simulate points uniformly distributed on a disc with radius 1? (Hint: see the Monte Carlo classic of exercises ii.)

outline

  • introduction: simulation, Monte Carlo integration, PRNG, exercises
  • direct sampling: transformations, rejection sampling, exercises
  • intermezzo 1: importance sampling, exercises
  • intermezzo 2: unnormalized densities, exercises
  • Markov chain Monte Carlo: general stuff, Metropolis, Gibbs, exercises

intermezzo 1: IS (importance sampling)

short recap

  • we want to know \(J\) \[J = E_p \left( f(X) \right) = \int f(x) p(x) dx\]
  • so, if we have a sample \(x^1, \dots, x^m, \dots, x^M\) from \(p(x)\), we compute \[\hat{J} = \frac{1}{M} \sum_{m = 1}^M f(x^m)\]
  • thus, we need a sample from \(p(x)\)

the importance sampling identity

  • what now if we do not know how to obtain a sample from \(p(x)\) using the already discussed methods?
  • suppose we can sample from \(g(x)\), we can rewrite the integral as follows: \[ \begin{aligned} J &= E_p \left( f(X) \right) \\ &= \int f(x) p(x) dx \\ &= \int \frac{f(x) p(x)}{g(x)} g(x) dx \\ &= E_g\left( \frac{f(X) p(X)}{g(X)}\right) \end{aligned} \]

importance sampling: practicalities

  • draw a sample \(x^1, \dots, x^M\) from \(g(x)\) and calculate \[ \begin{aligned} \hat{J} &= \frac{1}{M}\sum_{m=1}^M \frac{ f(x^m) p(x^m) }{g(x^m)}\\ &= \frac{1}{M}\sum_{m=1}^M w_m f(x^m) \end{aligned} \] with \(w_m = \frac{p(x^m)}{g(x^m)}\) (\(m = 1, \dots, M\)) being the importance weights
  • again, by the law of large numbers: \(\hat{J} \rightarrow J\) as \(M \rightarrow \infty\)

importance sampling: some more practicalities

  • it should be easy to simulate from \(g(x)\) and \(g(x) > 0\) whenever \(f(x)p(x) \neq 0\)
  • method does not work well if \(g(x)\) has lighter tails than \(p(x)\)

SIR: sampling importance resampling

  • the bare IS method does not give you a sample from \(p(x)\)
  • however, you may compute the weights and then resample \(x^m\)’s according to the weights (using sample(x, replace = TRUE, prob = w))
  • this is called SIR

exercises iv

exercises iv

[1] Estimate the variance of a logistic distribution (i.e., \(p(x) = \frac{e^x}{(1 + e^{x})^2}\) ) using importance sampling (i.e., \(f(x) = x^2\), the mean is zero). Use the Laplace distribution as the importance distribution \(g(x)\) and \(M = 10000\). (The true value is \(\frac{\pi^2}{3}\).) Plot also the running mean \(\hat{J}_m\) against \(m\). Repeat the exercise, but now use the normal as importance distribution.

exercises iv

[2] Go back to [1] and use the weights to resample \(x\)-values. Compare to the density of a logistic distribution. What happens if you use the normal as the importance distribution?

outline

  • introduction: simulation, Monte Carlo integration, PRNG, exercises
  • direct sampling: transformations, rejection sampling, exercises
  • intermezzo 1: importance sampling, exercises
  • intermezzo 2: unnormalized densities, exercises
  • Markov chain Monte Carlo: general stuff, Metropolis, Gibbs, exercises

intermezzo 2: unnormalized densities

normalization?

  • we know that for a valid density it is required that: \(\int p(x) dx = 1\)
  • usually, we can write \(p(x)\) as: \[p(x) = \frac{q(x)}{Z}\] where \(q(x)\) has all terms that involve \(x\) and \(Z\) is a constant
  • \(Z\) is the normalizing constant \[ \begin{aligned} \int p(x) dx &= \int \frac{q(x)}{Z}dx = 1\\ &\Rightarrow Z = \int q(x) dx \end{aligned} \]
  • we will also write \(p(x) \propto q(x)\) (\(p\) is proportional to \(q\))

some examples

  • normal: \(p(x) = \frac{1}{\sqrt{2\pi}\sigma} e^{-\frac{1}{2\sigma^2}(x - \mu)^2} \Rightarrow q(x) = e^{-\frac{1}{2\sigma^2}(x - \mu)^2}\) and \(Z = \sqrt{2\pi}\sigma\)
  • exponential: \(p(x) = \lambda e^{-\lambda x} \Rightarrow q(x) = e^{-\lambda x}\) and \(Z = \frac{1}{\lambda}\)
  • beta: \(p(x) = \frac{1}{B(a,b)} x^{a - 1}(1 - x)^{b - 1} \Rightarrow q(x) = x^{a - 1}(1 - x)^{b - 1}\) and \(Z =B(a, b)\)
  • uniform on \([a, b]\): \(p(x) = \frac{1}{b - a} \Rightarrow q(x) = 1\) and \(Z = b - a\)
  • Laplace: \(p(x) = \frac{1}{2} e^{-|x|} \Rightarrow q(x) = e^{-|x|}\) and \(Z = 2\)

what’s the big deal?

  • in Bayesian statistics, we are often confronted with unnormalized densities (note that \(\theta\) takes the role of \(x\) now): \[ \begin{aligned} p(\theta|y) &= \frac{p(y|\theta)p(\theta)}{p(y)} \\ &\propto p(y|\theta)p(\theta) \\ &= q(\theta) \end{aligned} \]
  • \(Z = p(y)\) (the marginal likelihood) is the normalizing constant (because independent of \(\theta\))

challenges

  • two challenges arise
    1. estimating properties of \(p(x)\) without knowing \(Z\) (e.g., posterior)
    2. estimating \(Z\) (e.g., when computing a Bayes factor)

importance sampling revisited

  • importance sampling can deal with these challenges: \[ \begin{aligned} J &= \int f(x) p(x) dx = \frac{\int f(x) q(x) dx}{Z} \\ &= \frac{\int f(x) q(x) dx}{\int q(x) dx} = \frac{\int \frac{f(x) q(x)}{g(x)} g(x) dx}{\int \frac{q(x)}{g(x)} g(x) dx} \\ &\approx \frac{\frac{1}{M} \sum_{m = 1}^M \frac{f(x^m) q(x^m)}{g(x^m)}}{\frac{1}{M} \sum_{m = 1}^M \frac{q(x^m)}{g(x^m)}} = \frac{\sum_{m = 1}^M w_m f(x^m)}{\sum_{m=1}^M w_m} \end{aligned} \]

importance sampling revisited

  • the previous derivation shows two things
    1. IS can estimate \(J\) (or sample from \(p(x)\)) without knowing \(Z\)
    2. IS can estimate \(Z\): \(\hat{Z} = \frac{1}{M} \sum_{m = 1}^M \frac{q(x^m)}{g(x^m)}\)

exercises v

exercises v

[1] Assume \(N = 10\) coin tosses and \(y = 2\) heads. \(\theta\) is the probability of heads coming up. The binomial likelihood is: \(p(y|\theta) = {N \choose y} \theta^y (1 - \theta)^{N - y}\). Now consider two models: \(p(\theta|M_1) = \text{beta}(\theta|a_1 = 1, b_1 = 3)\) and \(p(\theta|M_2) = \text{beta}(\theta|a_2 = 3, b_2 = 1)\).

In this case, \(BF_{12} = \frac{B(a_1 + y, b_1 + N - y) B(a_2, b_2)}{B(a_2 + y, b_2 + N - y)B(a_1, b_1)} = \frac{p(y|M_1)}{p(y|M_2)} = 7.5\).

Estimate this Bayes factor by using the importance sampling method applied twice (once for \(p(y|M_1)\) and once for \(p(y|M_2)\)). (Use uniform importance distributions.) Some background on the Bayes factor: We expand the Bayesian inference by considering a set of models \(M_i\) that include their (possibly model-specific set of) parameters \(\theta_i\), \[ p(\theta_i | y, M_i) = \frac{p(\theta_i | M_i) p(y | \theta_i, M_i)} {p(y | M_i)}, \] where the marginal likelihood of the data given the model is \[ p(y | M_i) = \int p(\theta_i | M_i) p(y | \theta_i, M_i) d\theta_i. \] The Bayes factor comparing two models is the ratio of marginal likelihoods \[ BF_{12} = \frac{p(y | M_1)}{p(y | M_2)}, \] and it is used to update the prior model odds to the posterior model odds, \[ \frac{p(M_1 | y)}{p(M_2 | y)} = \frac{p(M_1) p(y | M_1)}{p(M_2) p(y | M_2)} = \frac{p(M_1)}{p(M_2)} \times BF_{12}. \] In the above example, the marginal likelihood for \(M_1\) is \[ \begin{aligned} p(y | M_1) &= \int p(\theta | M_1) p(y | \theta, M_1) d\theta\\ &= \int \frac{\theta^{a_1 - 1} (1 - \theta)^{b_1 - 1}} {B(a_1, b_1)} \binom{N}{y} \theta^{y} (1 - \theta)^{N - y} d\theta\\ &= \binom{N}{y}/B(a_1, b_1) \int \theta^{y + a_1 - 1} (1 - \theta)^{N - y + b_1 - 1} d\theta\\ &= \binom{N}{y}/B(a_1, b_1) B(y + a_1, N - y + b_1). \end{aligned} \] For two binomial models \(M_1\), \(M_2\) with different beta priors the Bayes factor is \[ \begin{aligned} BF_{12} &= \frac{\binom{N}{y}/B(a_1, b_1) B(y + a_1, N - y + b_1)} {\binom{N}{y}/B(a_2, b_2) B(y + a_2, N - y + b_2)}\\ &= \frac{B(y + a_1, N - y + b_1) B(a_2, b_2)} {B(y + a_2, N - y + b_2) B(a_1, b_1)}. \end{aligned} \]

outline

  • introduction: simulation, Monte Carlo integration, PRNG, exercises
  • direct sampling: transformations, rejection sampling, exercises
  • intermezzo 1: importance sampling, exercises
  • intermezzo 2: unnormalized densities, exercises
  • Markov chain Monte Carlo: general stuff, Metropolis, Gibbs, exercises

Markov chain Monte Carlo

introduction MCMC

  • all direct sampling methods yield an exact and iid sample
  • but these methods are not always possible to apply (e.g., high dimensionality, unknown normalizing constant, etc.)
  • often however we can estimate \(J = E\left(f(X) \right) = \int f(x) p(x) dx\) through \[\frac{1}{M} \sum_{m = 1}^M f(x^m)\] using a Markov chain \(x^1, \dots, x^m, \dots, x^M\) with \(p(x)\) as stationary distribution

more introduction MCMC

  • more general class: Markov chain Monte Carlo (MCMC) methods
  • more widely applicable class of methods and easier to apply, but do not yield an iid sample
  • moreover, they are not exact, but approximate (only in the long run convergence to \(p(x)\))
  • two well known algorithms will be discussed here: Metropolis(-Hastings) and Gibbs sampler

some general issues regarding MCMC

  • you will generate a chain: \(x^1 \rightarrow x^2 \rightarrow \dots \rightarrow x^M\)
  • Markov chain (MC): \(K(x^m|x^{m-1}, \dots, x^1) = K(x^m|x^{m-1})\)
  • \(p(x)\) is the stationary distribution of the MC: if \(X^m \sim p\), then \(X^{m + 1} \sim p\)
  • so if \(X^1 \sim p\), then the remainder of the chain comes from \(p(x)\) as well (but … if we could do this then we would have solved the problem)

some more general issues regarding MCMC

  • given some regularity constraints, MC theory ensures convergence: even if \(X^1 \nsim p\), then \(X^M \sim p\) when \(M \rightarrow \infty\)
  • for all practical purposes: from iteration \(c\), we consider the MC to be converged to \(p(x)\)
  • first part of the chain (iterations 1 to \(c\)) is considered to be burn-in or warm-up period
  • we estimate \(E\left( f(X) \right) = \int f(x) p(x) dx\) through \[\frac{1}{M - c} \sum_{m = c + 1}^M f(x^m)\] using the Markov chain \(x^{c + 1}, \dots, x^M\)
  • various tools exist to assess convergence

even more general issues regarding MCMC

  • our estimate of \(E\left(f(X) \right)\) is based on \(N = M - c\) numbers, but they are not independent
  • therefore, the effective sample size (\(N_{\text{eff}}\)) is typically smaller than \(N = M - c\)
  • how much smaller? depends on how strongly \(x^m\) and \(x^{m + d}\) correlate (for all \(d\))
  • the ratio \(\frac{N_{\text{eff}}}{N}\) gives an assessment of computational cost (low: bad)

Metropolis: the algorithm

  • start with \(x^0\) drawn from some starting distribution
  • at iteration \(t\) of the algorithm, we have \(x^{t - 1}\) and need to generate \(x^{t}\)
    1. sample a candidate value \(x^*\) from a proposal distribution \(g(x|x^{t - 1})\) (such that \(g(x^{t - 1}|x^*) = g(x^*|x^{t - 1})\))
    2. compute the MH ratio: \(R = \frac{p(x^*)}{p(x^{t - 1})}\)
    3. accept \(x^*\) as the next draw \(x^{t} = x^*\) with probability \(\min(R, 1)\), otherwise take \(x^{t} =x^{t - 1}\) (i.e., the previous value)
  • for a large \(t\), \(x^t\) will be a sample from \(p(x)\)

Metropolis: some notes

  • the Metropolis algorithm acts as a “stochastic mode seeker”: if the candidate value is clearly better than the current one, you always accept, but if it is not better, you accept with a certain probability (the ratio of the densities)
  • note that the algorithm also works when you only know \(q(x)\propto p(x)\) because \[R = \frac{p(x^*)}{p(x^{t - 1})} = \frac{\frac{1}{Z}q(x^*)}{\frac{1}{Z}q(x^{t - 1})} = \frac{q(x^*)}{q(x^{t - 1})}\]
  • you may also have \(g(x^*|x^t) = g(x^*)\) (i.e., independence sampler)
  • if \(g(x^t|x^*) \neq g(x^*|x^t)\): you need to adjust the ratio \(R\) (Metropolis-Hastings)

example Metropolis-Hastings

  • assume we want to sample from the (unnormalized) weirdo density \(q(x) = \exp(-0.5 x^2)\left[ \sin(6x)^2 + 3\cos(x)^2 \cdot \sin(4x)^2 + 1 \right]\) \((Z \approx 5.182874)\)
  • we use a \(g(x^*|x^t) = N(x^*|x^t,\sigma^2)\) proposal density

example Metropolis-Hastings: step-by-step

example Metropolis-Hastings: diagnostics

exercises vi

exercises vi

[1] Multinomial processing tree (see picture). Suppose I answer 10 two-alternative questions on Bayesian stats. I know the answer to a question with probability \(\theta\). If I know the answer, I always answer correctly; if I don’t know it, I guess. Suppose I have 9 correct responses. What does this tell me about my underlying Bayesian stats ability \(\theta\)? (Continued on next slide.)

exercises vi

We have 9 successes out of 10 trials. The binomial likelihood is \(p(y|\theta) = {10 \choose 9} \left(\frac{1 + \theta}{2}\right)^{9} \left(\frac{1 - \theta}{2} \right)^{1}\). Assuming a \(\text{beta}(2, 2)\) prior for \(\theta\), we find for the posterior: \[ \begin{aligned} p(\theta|y) &\propto p(y|\theta)p(\theta) = q(\theta) \\ &= \left(\frac{1 + \theta}{2}\right)^{9} \left(\frac{1 - \theta}{2} \right)^{1} \theta(1 - \theta) \end{aligned} \] Implement a Metropolis algorithm to sample from the posterior using as a proposal density \(g(\theta) = 1\) (i.e., a uniform). You may use the function for the log-posterior below.

logpost <- function(theta) {
  9*log((1 + theta)/2) + 1*log((1 - theta)/2) + log(theta) + log(1 - theta)
}

Gibbs sampler: introduction

  • Metropolis is a very general and versatile algorithm
  • however in specific situations, other algorithms may be better applicable
  • for example, if \(X = (X_1, \dots, X_k)\) is a vector of random variables and \(p(x)\) is a multidimensional distribution
  • in such cases, the Gibbs sampler may be helpful
  • basic idea: split the multidimensional problem into a series of simpler problems of lower dimensionality

Gibbs sampler: some notation

  • assume that \(k = 3\)
  • \(X = (X_1, X_2, X_3)\) and \(p(x) = p(x_1, x_2, x_3)\)
  • \(x_{-1} = (x_2, x_3)\): a vector with the first component removed
  • \(x_{-2} = (x_1, x_3)\): a vector with the second component removed
  • \(x_{-3} = (x_1, x_2)\): a vector with the third component removed
  • \(p(x_1|x_{-1})\): the full conditional, the conditional distribution of \(X_1\) given the other random variables
  • \(p(x_2|x_{-2})\)
  • \(p(x_3|x_{-3})\)

Gibbs sampler: the basics

  • start with \(x^0 = (x_1^0, x_2^0, x_3^0)\)
  • at iteration \(t\) of the Gibbs sampler, we have \(x^{t - 1}\) and need to generate \(x^{t}\)
    1. \(x_1^{t} \sim p(x_1|x_{-1}^{t - 1}) = p(x_1|x_2^{t - 1},x_3^{t - 1})\)
    2. \(x_2^{t} \sim p(x_2|x_{-2}^{t - 1}) = p(x_2|x_1^{t}, x_3^{t - 1})\)
    3. \(x_3^{t} \sim p(x_3|x_{-3}^{t - 1}) = p(x_3|x_1^{t}, x_2^{t})\)
  • for a large \(t\), \(x^t\) will be a sample from \(p(x)\)

Gibbs sampler: an example

  • assume \(p(x) = N(x|0, \Sigma)\) is a bivariate normal distribution with mean vector 0 and covariance matrix \(\Sigma = \begin{pmatrix} 1 & \rho\\ \rho & 1 \end{pmatrix}\): \(p(x) = \frac{1}{2\pi} |\Sigma|^{-\frac{1}{2}} e^{-\frac{1}{2}x'\Sigma^{-1}x}\)
  • Shiny app: bivarnorm

Gibbs sampler: some more example

  • for this case, the full conditionals are easy:
    • \(p(x_1|x_2) = N(x_1| \rho x_2, 1 - \rho^2)\)
    • \(p(x_2|x_1) = N(x_2| \rho x_1, 1 - \rho^2)\)
  • detailed derivations are available at https://fabiandablander.com/statistics/Two-Properties.html
  • we know how to sample from these univariate normals!

Gibbs sampler: step-by-step

exercises vii

exercises vii

[1] Program the Gibbs sampler to sample from a bivariate normal distribution. Use different starting points. To refresh your memory: \(p(x_1|x_2) = N(x_1|\rho x_2, 1 - \rho^2)\) and \(p(x_2|x_1) = N(x_2|\rho x_1, 1 - \rho^2)\).

exercises vii

[2] (advanced) The MPT example revisited. You may consider the correct category as the collapse of “knowing” and “guessing correct”. The number of times you guessed may be considered as a parameter \(\gamma\) as well (assume a uniform prior on \(\gamma\)).

The bivariate posterior then becomes: \[ \begin{aligned} p(\theta, \gamma|y) &\propto p(y|\theta, \gamma) p(\theta, \gamma) = q(\theta, \gamma) \\ &= \theta^{9 - \gamma} \left( \frac{1-\theta}{2} \right)^{\gamma} \left( \frac{1 - \theta}{2} \right)^{1} \theta(1 - \theta) \\ &\propto \theta^{9 - \gamma + 1} \left( \frac{1 - \theta}{2} \right)^{\gamma+2} \end{aligned} \] The two full conditionals are (derive these): \(p(\theta|y, \gamma) = \text{beta}(9 - \gamma + 2, \gamma + 3)\) and \(p(\gamma|y, \theta) = \text{binomial}\left( \frac{1 - \theta}{1 + \theta}, 9 \right)\). Compare your results to the Metropolis exercise.

to conclude

  • this was an overview of the most basic simulation methods
  • there is much more to discuss (Monte Carlo error, adaptive rejection sampling, Hamiltonian MC, DRAM, simulated annealing, \(\dots\))