Search code examples
pythonfloating-pointlogarithmnatural-logarithm

Convert float to log space in python


I am implementing the Viterbi algorithm (a dynamic algorithm) in Python, and I notice that for large input files, the probabilities keep getting multiplied and shrinking beyond the floating point precision. I need to store the numbers in log space.

Can anyone give a simple example Python code-snippet of how to convert, say, 0.0000003 to log-space? (I'm not sure if it needs to be natural log or some other log. I have only heard of "log-space" but don't really know about it.)

Thanks!


Solution

  • To move to log space, use log. To move back again use exp. The rules in log space are different - eg. to perform multiplication is to add in logspace.

    >>> from math import log, exp
    >>> log(0.0000003)
    -15.01948336229021
    >>> exp(-15.01948336229021)
    3.0000000000000015e-07
    >>> log(0.0000003) + log(0.0000003)
    -30.03896672458042
    >>> exp(-30.03896672458042)
    9.000000000000011e-14 # 0.0000003 * 0.0000003 
    

    Here is an example using some small probabilities

    >>> probabilities = [0.0000003, 0.0000004, 0.0000005]
    >>> exp(sum(log(p) for p in probabilities))
    5.999999999999992e-20