Move Products into Sums
For positive and ,
This is the exponential rule read backward. When a probability calculation contains a long product, the identity lets us work with a sum of log-probabilities instead.
Underflow Depends on the Data Type
For example, . That is far below float32's range and underflows to zero, while float64 can still represent a nonzero result. The computer did not universally “give up”; a particular representation ran out of range.
Log space stores , which is easy to represent. Exponentiating later can still underflow if the requested probability is outside the destination type's range, so comparisons, optimization, and accumulation should remain in log space as long as practical.
Where Log-Likelihood Appears
- Maximum likelihood estimation maximizes a sum of observation log-probabilities instead of their product.
- Cross-entropy for one-hot classification reduces to the negative log-probability of the target class.
- Language models train on next-token negative log-likelihood and derive perplexity from mean log loss.
import numpy as np
방법 1
x = np.prod(np.full(1000, 0.5)) print(x)
방법 2
y = np.exp(1000 * np.log(0.5)) print(y)
비교
print(x == y)
첫번째 방법에서는 확률을 계속 곱하기 때문에 언더플로우가 나버려서 0이되어버림