Search code examples
pythonprobabilitylogarithm

Getting rid of ValueErrors when taking log of probabilities


I want to find the maximum probable path and I tried to associate log probabilities to get rid of underflow errors when multiplying sequence of steps.

While calculating the maximum probable path, I iteratively retain only the best paths and remove the others from the start to end (Consider the image I drawn here). After the first step (I dont have to remove any since, there is only one path from initial state to first state) I am retaining only the best possible path to that state and that state will have a probability up to that state.

In the step 3 (I have not shown here) this score will be multiplied with the other state transition probabilities and algorithm will choose the best path to each state at third step. However, as you could see since the score associated with the node(marked with *) is -3.0 upon which will throw an ValueError when trying to calculate it's log value. Any idea to get rid of this?

enter image description here

P.S I have already done the implementation and do not wanted to show it since it is very large and hard to explain. The error just lies on the following line where best_incoming_weight = -3.0 here

    probability = math.log2(best_incoming_weight) + math.log2(current_incoming_weights)

Solution

  • You computed the logarithm of the probability. It must be negative or zero because log of any number between zero and one is non-positive. To convert the obtained result to traditional probability, you need to compute 2**probability where probability is as above.