Solution (source code)

= Solution

Let $\widetilde\pi$ be a nonnegative target kernel and $q(y\mid x)$ a <proposal distribution>. At state $x$, propose $Y\sim q(\cdot\mid x)$, draw an independent uniform $U$, and move to $Y$ if
$$
\boxed{U\leq\alpha(x,Y),\qquad\alpha(x,y)=\min\left(1,\frac{\widetilde\pi(y)q(x\mid y)}{\widetilde\pi(x)q(y\mid x)}\right).}
$$
Otherwise keep $x$. This is the <Metropolis–Hastings algorithm>. Start on the positive target support, with the usual zero-ratio conventions. The unknown <normalizing constant> cancels.

An unnormalized kernel is sufficient, provided $0<\int\widetilde\pi<\infty$. A genuinely improper target with infinite integral is not a probability density and cannot supply a stationary target probability distribution. The acceptance formula can still be written formally, but cannot be said to generate samples from that nonexistent probability law.

Because rejected proposals leave the state unchanged, the <Markov kernel> includes an atom:
$$
K(x,dy)=q(y\mid x)\alpha(x,y)\,dy+r(x)\delta_x(dy),\quad
r(x)=1-\int q(y\mid x)\alpha(x,y)\,dy.
$$
<Detailed balance> with a probability measure $\pi$ means the measure identity
$$
\boxed{\pi(dx)K(x,dy)=\pi(dy)K(y,dx).}
$$
Where both sides have ordinary densities, this reads $\pi(x)K(x,y)=\pi(y)K(y,x)$. The measure formulation also includes the rejection atom. Integrating it shows that $\pi$ is invariant.