Skip to content

fix(binomial-distribution): compute pmf in log-space to avoid overflow - #182

Merged
JimmyMAndersson merged 1 commit into
mainfrom
fix/176-binomial-pmf-overflow
May 23, 2026
Merged

fix(binomial-distribution): compute pmf in log-space to avoid overflow#182
JimmyMAndersson merged 1 commit into
mainfrom
fix/176-binomial-pmf-overflow

Conversation

@JimmyMAndersson

Copy link
Copy Markdown
Owner

Integer arithmetic in choose(n:k:) crashes on overflow for large trial counts. Replace the coefficient calculation with logGamma arithmetic so the PMF is computed entirely in floating-point without intermediate integer overflow.

Also adds boundary guards for p=0 and p=1 (preventing NaN from 0 × log(0)), a precondition on sample(_:), and expands the test suite with log-PMF cases, large-n cases, boundary-probability cases, and out-of-range x cases.

Fixes: #176

Integer arithmetic in choose(n:k:) crashes on overflow for large
trial counts. Replace the coefficient calculation with logGamma
arithmetic so the PMF is computed entirely in floating-point without
intermediate integer overflow.

Also adds boundary guards for p=0 and p=1 (preventing NaN from
0 × log(0)), a precondition on sample(_:), and expands the test
suite with log-PMF cases, large-n cases, boundary-probability
cases, and out-of-range x cases.

Fixes: #176
@JimmyMAndersson JimmyMAndersson self-assigned this May 23, 2026
@JimmyMAndersson JimmyMAndersson added the bug Something isn't working label May 23, 2026
@JimmyMAndersson
JimmyMAndersson merged commit 54c192a into main May 23, 2026
5 checks passed
@JimmyMAndersson
JimmyMAndersson deleted the fix/176-binomial-pmf-overflow branch May 23, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BinomialDistribution.pmf produces incorrect results and crashes for large trial counts

1 participant