I writing a program in Java which requires me to compute some probabilities, and for larger inputs, the probabilities can eventually become very small. Therefore, to prevent underflow issues, I would like to take the log probabilities instead.
I am, however, having trouble implementing this. At each stage of computation there can be a different number of options, to which probabilities need to be assigned, and each stage they need to add up to 1. The probabilities are based on a number of different variables. I take a sum over all possibilities using the following formula:
Math.pow(d[i], a) * Math.pow(1/c[i], b)
This gives me a variable, total
. To then establish the probability p_i,
p_i = (Math.pow(d[i], a) * Math.pow(1/c[i], b)) / total
My question is, how can I implement this using log probabilities, so that I do not get 'Infinity' and 'NaN' values, since these are what I have been getting so far.