Skip to content

numpy/log-of-product

Pack Severity Fix Versions
NumPy warning unsafe, with xpd check --fix --unsafe-fixes All versions

A product can overflow to inf or underflow to 0 before the log is taken. Sum the logs instead.

log_likelihood = np.log(np.prod(probabilities))
log_likelihood = np.sum(np.log(probabilities))

The rule reports np.log(np.prod(x)).

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The unsafe fix writes np.sum(np.log(x)).

The last bits can change. An even number of negative inputs can yield a positive product with a real logarithm, while taking the individual logs yields NaN.

The product can overflow to inf or underflow to 0 before the log is taken. Write $NP.sum($NP.log($X)).

See all rules in the NumPy pack.