Metropolis-Hastings – Algorithm for Logarithmic Probability Density

gibbsmetropolis-hastings

Similar question to posted here: Metropolis-Hastings using log of the density however my question is around sampling a random number from a uniform distribution.
I am following the steps outlined in Murphy: Machine Learning A Probabilistic Perspective on page 850 for Metropolis Hastings algorithm. The difference is I am working with log probability densities as they are large and negative, so exponentiating them effecitvely results in 0.

enter image description here

I have started by taking the log of step 5:
$$
log(\alpha) = log(P(x')) – log(P(x))
$$

Assuming q is symmetric which it is in my case. Then to compute $r$, I am taking the lesser of $log(1)$ and $log(\alpha)$, so essentially if $\alpha$ is negative, I am assigning it to $r$, otherwise I am assigning $r$ as 0. Now for the issue: what do I do at step 6? The link above doesn't describe this. Obviously $r$ is either 0 or negative, and so it would always be less than $u$ and hence we always step where we are. Also, $r$ isn't bounded below – $\alpha$ could be very negative (though unlikely), so it is hard to redefine a uniform distribution range to sample from. Any ideas how to proceed from here?

Best Answer

In log scale, here is what your pseudocode should read from step 5 onwards:

5. Compute acceptance probability

$$\log(\alpha) = \log \frac{p(x')}{p(x)} = \log p(x')- \log p(x).$$

Compute

$$\log(r) =\min(0, \log(\alpha))$$

6. Sample $u \sim U(0,1)$

7. Set new sample to

$$x^{s+1} = \begin{cases} x' \quad \text{if} \quad \log(u) < \log(r) \\ x^s \quad \text{if} \quad \log(u) \geq \log(r)\end{cases}$$


As you've correctly noted, because $q$ is symmetric, we have that $q(x | x') = q(x' | x)$ and so these cancel in your expression for $\log (\alpha)$.

For $\log(r)$, note that $\log(r) = \log \min(1, \alpha) = \min(\log(1), \log(\alpha)).$

Now that you have $\log(r)$ you just need to compare it to $\log(u)$ as we can log both sides of the inequalities in the decision rule.