Skip to content

perf: sample Binomial via BINV/BTPE instead of n Bernoulli trials#409

Open
agene0001 wants to merge 1 commit into
statrs-dev:mainfrom
agene0001:fix/183-btpe-sampling
Open

perf: sample Binomial via BINV/BTPE instead of n Bernoulli trials#409
agene0001 wants to merge 1 commit into
statrs-dev:mainfrom
agene0001:fix/183-btpe-sampling

Conversation

@agene0001

Copy link
Copy Markdown

Binomial::sample ran one Bernoulli trial per trial, so a single draw was O(n). The case reported in #183 (5 categories, n = 100 000, sampled via Multinomial) took 22.4 ms per 100 draws here; numpy was reported ~400x faster.

This replaces it with Kachitvichyanukul & Schmeiser (1988): BINV inversion for n·min(p,1−p) < 10, BTPE rejection otherwise, exact shortcuts for p ∈ {0, 1}, and a Poisson-limit path when 1 − p rounds to 1.0.

before after
Binomial(0.4, 1_000).sample() 3.6 µs 56 ns
Binomial(0.4, 100_000).sample() 361 µs 39 ns
Binomial(0.4, 1e9).sample() ~3.6 s 39 ns
Multinomial(5 cats, n=1e5) ×100 22.4 ms 22.8 µs (981x)

Multinomial benefits automatically since it draws conditioned binomials per category.

The implementation is adapted from rand_distr (MIT/Apache-2.0, attributed in-source). One detail worth review attention: the final acceptance test uses the Stirling-series signs from GSL, which differ from the published paper and were verified by one of the algorithm's original designers (see the in-source comment).

Correctness: chi-square goodness-of-fit against the exact pmf over five parameter sets covering every code path (BINV, BTPE, both flipped variants), with expected-count pooling and a 6σ threshold on fixed seeds — deterministic, so a failure means a real defect, not chance. Plus moment tests for n = 1e9 and the Poisson-limit path, and degenerate-parameter tests. Relates to the sampling-verification wish list in #288.

Closes #183

🤖 Generated with Claude Code

`Binomial::sample` ran one Bernoulli trial per trial, so a single draw was
O(n). The reported case in statrs-dev#183 - 5 categories, n = 100_000, 100 samples via
`Multinomial` - took 22.4 ms; numpy was measured at ~400x faster.

Replaces it with Kachitvichyanukul & Schmeiser (1988): BINV inversion for
`n * min(p, 1-p) < 10`, BTPE rejection from a triangle/parallelogram/two
exponential tails envelope otherwise, plus exact shortcuts for p in {0, 1} and a
Poisson-limit path when `1 - p` rounds to 1.

    Binomial(0.4, 1_000).sample()          3.6 us -> 56 ns
    Binomial(0.4, 100_000).sample()         361 us -> 39 ns
    Binomial(0.4, 1e9).sample()           ~3.6 s  -> 39 ns
    Multinomial(5 cats, n=1e5) x100        22.4 ms -> 22.8 us   (981x)

`Multinomial` benefits automatically, since it draws conditioned binomials per
category - which is what the issue asked for.

The implementation is adapted from `rand_distr` (MIT/Apache-2.0, attributed
in-source). That matters for one detail: the final acceptance test uses the
Stirling-series signs from GSL, which differ from the published paper and were
verified by one of the algorithm's original designers.

Correctness is checked by a chi-square goodness-of-fit test against the exact
pmf over five parameter sets covering every code path (BINV, BTPE, and both
flipped variants), with expected-count pooling and a 6-sigma threshold on fixed
seeds - so it is deterministic and a failure means a real defect. Plus moment
tests for n = 1e9 and the Poisson-limit path, and degenerate-parameter tests.

Closes statrs-dev#183
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multinomial sampling is very slow

1 participant