Solution (source code)

= Solution

Write the target posterior density as $\pi(\theta)$ and the <proposal distribution> density as $q(\theta'\mid\theta)$. The <Metropolis–Hastings algorithm> accepts a proposed move with
$$
a(\theta,\theta')=
\min\!\left\{1,
\frac{\pi(\theta')q(\theta\mid\theta')}
{\pi(\theta)q(\theta'\mid\theta)}\right\}.
$$
For distinct states,
$$
\pi(\theta)q(\theta'\mid\theta)a(\theta,\theta')
=\min\{\pi(\theta)q(\theta'\mid\theta),
\pi(\theta')q(\theta\mid\theta')\},
$$
which is symmetric in $\theta$ and $\theta'$. The rejection probability supplies the diagonal part, so the entire transition kernel satisfies <detailed balance>. Integrating the detailed-balance identity over the starting state proves $\int\pi(\theta)P(\theta,d\theta')=\pi(\theta')d\theta'$. Hence the posterior is a <stationary distribution>; an <irreducible Markov chain> that is also an <aperiodic Markov chain> converges uniquely to it.