Skip to content

Implement beta/gamma invlogcdf and invlogccdf in pure Julia#204

Draft
andreasnoack wants to merge 1 commit into
masterfrom
an/normath-betagamma
Draft

Implement beta/gamma invlogcdf and invlogccdf in pure Julia#204
andreasnoack wants to merge 1 commit into
masterfrom
an/normath-betagamma

Conversation

@andreasnoack
Copy link
Copy Markdown
Member

@andreasnoack andreasnoack commented Mar 22, 2026

Summary

  • Replace Rmath-dependent betainvlogcdf, betainvlogccdf, gammainvlogcdf, gammainvlogccdf with pure Julia implementations
  • For moderate log-probabilities: delegates to existing betainvcdf/gammainvcdf via -expm1(lp) for accuracy near lp=0
  • For extreme log-probabilities (where exp(lp) underflows): Newton iteration in log-space using betalogcdf/gammalogcdf and betalogpdf/gammalogpdf, with asymptotic initial estimates

Test plan

  • Existing test suite passes (beta/gamma still compared against Rmath via rmathcomp_tests)
  • Verified extreme log-probabilities down to lp = -5000 match Rmath where representable

🤖 Generated with Claude Code

Replace the Rmath-dependent invlogcdf/invlogccdf functions for beta and
gamma distributions with Newton iteration in log-space. Uses asymptotic
initial estimates for extreme tails and -expm1(lp) for accuracy near
lp=0. Leverages existing betainvcdf/gammainvcdf for moderate cases.
@andreasnoack andreasnoack force-pushed the an/normath-betagamma branch from d7c7e63 to 98c7729 Compare March 22, 2026 13:31
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.70492% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.76%. Comparing base (e0a0cab) to head (98c7729).

Files with missing lines Patch % Lines
src/distrs/beta.jl 85.18% 8 Missing ⚠️
src/distrs/gamma.jl 89.70% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #204      +/-   ##
==========================================
+ Coverage   72.39%   73.76%   +1.37%     
==========================================
  Files          22       22              
  Lines        1007     1117     +110     
==========================================
+ Hits          729      824      +95     
- Misses        278      293      +15     

☔ 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.

Comment thread src/distrs/beta.jl
Comment on lines +149 to +160
_lp = Float64(lp)

if isnan(_lp) || _lp > 0
return convert(T, NaN)
elseif _lp == 0
return one(T)
elseif isinf(_lp) # -Inf
return zero(T)
end

# Handle degenerate cases
_α = Float64(α); _β = Float64(β)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These Float64 calls will error for Duals and (arguably even worse) lead to silently incorrect results for higher-precision numbers. Ideally, we should just avoid the Float64 conversions in this function and instead ensure that the functions called here are written generically enough to support all common use cases. Then the responsibility for potentially handling other or higher-precision Reals would be off-loaded to those.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably convert the PRs to drafts. The current ones are mostly how Claude Code prepared them and I need to go through the code in detail.

I agree that a function like this should probably avoid conversion to Float64, but most of the functions containing the actual algorithms will necessarily be restricted to Float64 because the number of terms in the various approximations has been determined based on the precision.

@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.

3 participants