5

Suppose I have values of log(P(x_i)), i.e. log-probabilities to events x_i. The probabilities are very small, so that these log-values are of the order of -1e3.

I want to compute an expectation value. In order to evaluate the corresponding sum, I need the probabilities P(x_i) themselves so I tried to call numpy.exp. This however, returns only 0 as the precision of basic floats in Python is not high enough to resolve a number like exp(-1000).

What is the typical way to circumvent this issue?

reloh100
  • 153
  • 6

1 Answers1

8

If your final result is of the order of magnitude of exp(-1000) $\approx 5 \cdot 10^{-435}$, then you are out of luck; no matter how you compute it, it will always underflow. There is simply no representable floating-point binary64 number that small. The slower Bigfloats are the only way out.

Some of the issues with large/small intermediate values, however, can be solved with a few manipulations:

Federico Poloni
  • 11,344
  • 1
  • 31
  • 59