Skip to content

Implement binomial, Poisson, and negative binomial inverse functions in pure Julia#205

Draft
andreasnoack wants to merge 1 commit intomasterfrom
an/normath-discrete
Draft

Implement binomial, Poisson, and negative binomial inverse functions in pure Julia#205
andreasnoack wants to merge 1 commit intomasterfrom
an/normath-discrete

Conversation

@andreasnoack
Copy link
Member

@andreasnoack andreasnoack commented Mar 22, 2026

Summary

Replace Rmath-dependent inverse functions for three discrete distributions with pure Julia implementations based on VBA code by Ian Smith:

  • Binomial: binominvcdf, binominvccdf, binominvlogcdf, binominvlogccdf
  • Poisson: poisinvcdf, poisinvccdf, poisinvlogcdf, poisinvlogccdf
  • Negative binomial: nbinominvcdf, nbinominvccdf, nbinominvlogcdf, nbinominvlogccdf

All use normal approximation for initial guess followed by linear search with direct PMF evaluation. Each search function uses its own CDF variant (cdf or ccdf) directly, avoiding precision loss from 1-p subtraction. The invlog variants use -expm1(lq) when lq is close to 0.

Test plan

  • Self-consistency round-trip tests: invcdf(cdf(k)) == k for all support points with non-degenerate CDF values
  • PDF/CDF/CCDF still compared against Rmath
  • The invlog round-trips allow ±1 tolerance due to inherent exp(log(cdf)) precision loss

🤖 Generated with Claude Code

…in pure Julia

Replace Rmath-dependent inverse functions for three discrete distributions
with pure Julia implementations based on VBA code by Ian Smith:

- Binomial: binominvcdf, binominvccdf, binominvlogcdf, binominvlogccdf
- Poisson: poisinvcdf, poisinvccdf, poisinvlogcdf, poisinvlogccdf
- Negative binomial: nbinominvcdf, nbinominvccdf, nbinominvlogcdf, nbinominvlogccdf

All use normal approximation for initial guess followed by linear search
with direct PMF evaluation. Each search function uses its own CDF variant
(cdf or ccdf) directly, avoiding precision loss from 1-p subtraction.
The invlog variants use -expm1(lq) when lq is close to 0.

Tests use self-consistency round-trips for inverse functions, since our CDF
implementation differs from Rmath's by ~1 ULP which can flip discrete answers.
@codecov-commenter
Copy link

codecov-commenter commented Mar 22, 2026

Codecov Report

❌ Patch coverage is 90.51095% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.04%. Comparing base (e0a0cab) to head (df74a25).

Files with missing lines Patch % Lines
src/distrs/nbinom.jl 88.77% 11 Missing ⚠️
src/distrs/pois.jl 91.11% 8 Missing ⚠️
src/distrs/binom.jl 91.86% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #205      +/-   ##
==========================================
+ Coverage   72.39%   76.04%   +3.65%     
==========================================
  Files          22       22              
  Lines        1007     1269     +262     
==========================================
+ Hits          729      965     +236     
- Misses        278      304      +26     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@andreasnoack andreasnoack marked this pull request as draft March 23, 2026 08:51
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.

2 participants