# Computation within log space

What is the conversion of the following equation into log space?

$$bf2 = 1 + (p * (bf1 - 1))$$

Given log.bf1 (log Bayes factor), how do I get to log.bf2 without having to compute bf1, but instead using log.bf1?

Context: Numerical issues are arising because bf1 is often very large or very small. Hence, the computations are performed in log space to avoid numerical over/underflow.

• Please explain what "numerical issues" are arising and (if possible) tell us why they happen. For instance, would it be because $\text{bf}_1$ is very large, very close to $0,$ very close to $1,$ or something else? – whuber Jan 9 at 16:28
• Thanks. I have added a clarification to the question. – user3302113 Jan 9 at 16:41
• Have a look at how other people have defined functions such as log1p_exp, that should help. – Björn Jan 9 at 16:44
• Thank you, I'm currently using numerical work arounds like that. The aim of my question was check if there really aren't any mathematical tricks I could use instead. – user3302113 Jan 9 at 17:59

To simplify the notation, write $$x$$ for $$\text{bf}_1$$ and $$x^\prime$$ for $$\text{bf}_2.$$ By subtracting $$1$$ from each side, write their relation as
$$y^\prime = x^\prime - 1 = p(x - 1) = py.$$
Let $$s = \operatorname{signum}(y)$$ be the sign of $$y$$ (equal to $$1$$ when $$y\gt 0,$$ $$-1$$ when $$y \lt 0,$$ and $$0$$ when $$y=0$$). Multiplying by $$s$$ to make both sides nonnegative (assuming $$p\ge 0$$) allows us to take logarithms, giving
$$\log(s y^\prime) = \log(s y) + \log(p).$$
By representing the information in terms of $$s = \operatorname{signum}(x-1)$$ together with $$\eta = \log|x - 1|,$$ the update step merely adds $$\log(p)$$ to $$\eta.$$ After a sequence of updates you can recover the final value of $$x$$ from the final value of $$\eta$$ as $$x = s \exp(\eta) + 1.$$