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!
To move to log space, use
log
. To move back again useexp
. The rules in log space are different - eg. to perform multiplication is to add in logspace.Here is an example using some small probabilities