1
$\begingroup$

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)) ?

$\endgroup$

1 Answer 1

1
$\begingroup$

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
$\endgroup$
1
  • $\begingroup$ THX buddy, got it $\endgroup$
    – Gid
    Commented Dec 11, 2020 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.