Does numpy.log(numpy.prod(y)) not equal numpy.sum(numpy.log(y)) in log-likelihood? y is an n-dimensional array with n probabilities, and it comes from putting n samples into the density function. Obviously, The product of n probability densities is going to be almost zero, i.e., np.prod(y)=0, resulting in np.log(np.prod(y)) = -inf. However, np.log(y) usually get n negative numbers, so np.sum(np.log(y)) is a scalar. That means that numpy.log(numpy.prod(y)) is not equal to np.sum(np.log(y)) ?
1 Answer
Mathematically, they're equal, but hardware has finite precision. If you push its limits, they won't be equal or even close. Truth is log(prod(y))
may overflow much easier, so the rounding error will be much larger, especially when the number of terms is large.
Moreover, as a general rule of thumb, never compare floating numbers by equality operator. Use np.isclose
or measure the difference in numbers by an epsilon of your choice.
As shown here:
>>> 1.2 - 1.0 == 0.2
False