Skip to content

Commit 199afec

Browse files
committed
fix: avoid zero division errors in entropy
1 parent 5d11356 commit 199afec

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/komm/_util/information_theory.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ def entropy(pmf: npt.ArrayLike, base: float | Literal["e"] = 2.0) -> float:
4747
"""
4848
pmf = validate_pmf(pmf)
4949
validate_log_base(base)
50+
pmf1 = pmf[pmf > 0]
5051
if base == "e":
51-
return -np.dot(pmf, np.log(pmf, where=(pmf > 0)))
52+
log_pmf1 = np.log(pmf1)
5253
elif base == 2.0:
53-
return -np.dot(pmf, np.log2(pmf, where=(pmf > 0)))
54+
log_pmf1 = np.log2(pmf1)
5455
else:
55-
return -np.dot(pmf, np.log(pmf, where=(pmf > 0))) / np.log(base)
56+
log_pmf1 = np.log(pmf1) / np.log(base)
57+
return -np.dot(pmf1, log_pmf1)
5658

5759

5860
def binary_entropy(p: float) -> float:

0 commit comments

Comments
 (0)