Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

A MCMC implementation for a standard normal distribution

A MCMC implementation for a standard normal distribution

Target Distribution (Posterior)

In notebook, the goal is to generate samples from the standard normal distribution (a normal distribution centered around 0 with standard deviation of 1):

p(x)=12πexp⁡(−x22)p(x) = \frac{1}{\sqrt{2\pi}}\exp\left(-\frac{x^2}{2}\right)

Since there is no data (I’m not conditioning on any observations for this simple example), the posterior is simply the target distribution p(x)p(x), the standard normal density.

Likelihood

For the standard normal, when simulating from the distribution (not fitting to data), the likelihood term is just the value of the target density itself:

L(x)=p(x)=12πexp⁡(−x22)L(x) = p(x) = \frac{1}{\sqrt{2\pi}}\exp\left(-\frac{x^2}{2}\right)

MCMC steps

  1. Proposal Step:

    • Propose a new point x∗x^* from a normal distribution centered at the current xx:

      x∗∼N(x(t−1),σ2)x^* \sim \mathcal{N}(x^{(t-1)}, \sigma^2)
    • This is a random walk Metropolis proposal: q(x∗∣x(t−1))=N(x(t−1),σ2)q(x^* | x^{(t-1)}) = \mathcal{N}(x^{(t-1)}, \sigma^2).

  2. Acceptance Ratio:

    • Since the proposal distribution is symmetric, the qq terms cancel out in the general M-H acceptance ratio:

      α=min⁡(1,p(x∗)p(x(t−1)))\alpha = \min\left(1, \frac{p(x^*)}{p(x^{(t-1)})}\right)
    • Plugging in the normal density:

      α=min⁡(1,exp⁡(−(x∗)22)exp⁡(−(x(t−1))22))\alpha = \min\left(1, \frac{\exp\left(-\frac{(x^*)^2}{2}\right)}{\exp\left(-\frac{(x^{(t-1)})^2}{2}\right)}\right)

      =min⁡(1,exp⁡(−12[(x∗)2−(x(t−1))2]))= \min\left(1, \exp\left(-\frac{1}{2}\left[(x^*)^2 - (x^{(t-1)})^2\right]\right)\right)
  3. Update Step:

    • Accept x∗x^* with probability α\alpha. Otherwise, keep the current value.

<Figure size 640x480 with 1 Axes>
<Figure size 800x400 with 1 Axes>