Repository navigation
Improve accuracy of fdist and tdist quantiles in the upper tail - #240
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #240 +/- ##
==========================================
+ Coverage 77.22% 79.39% +2.17%
==========================================
Files 24 24
Lines 1190 1199 +9
==========================================
+ Hits 919 952 +33
+ Misses 271 247 -24 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds the full pdf/cdf/quantile API for the Laplace distribution, in standard and location-scale form. `laplaceinvlogcdf` splits `log(2)` into a high and a low part so that it stays accurate around the median, and `tdistinvlogcdf` now calls it instead of `log2mexp`, which drops its branches. There is no Rmath reference, so the tests compare against closed forms in `BigFloat`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
force-pushed
the
dmw/tdistinvlogcdf
branch
from
November 16, 2026 15:08
c9cccd5 to
99491cd
Compare
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since the tests call Rmath directly, `rmathcomp` looked up `Rmath.dfdist` and `Rmath.dtdist`, which do not exist (the Rmath names are `df`, `pf`, `qf` and `dt`, `pt`, `qt`), so the "RMath fdist" and "RMath tdist" test items silently ran no tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The F quantile `ν2 * x / (ν1 * (1 - x))` for the quantile `x` of the beta variate `u ~ Beta(ν1 / 2, ν2 / 2)` cancelled catastrophically for `x` close to 1. Now `1 - x` is computed directly: `beta_inc_inv` returns it alongside `x`, and for the log-probability versions the quantile of whichever of `u` and `1 - u ~ Beta(ν2 / 2, ν1 / 2)` is at most 1/2 is computed, which is decided upfront from `P(u ≤ 1/2)`. The handling of degenerate beta distributions in `betainvcdf` and `betainvccdf` (`beta_inc_inv` does not return for them, #154) is moved to a helper that returns both `x` and `1 - x` and is used by the F quantiles as well. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
force-pushed
the
dmw/tdistinvlogcdf
branch
from
November 16, 2026 15:55
99491cd to
4852379
Compare
`fdistinvlogccdf` is now the reciprocal of `fdistinvlogcdf` with swapped degrees of freedom (`1 / F ~ F(ν2, ν1)`), which removes the metaprogramming. The new tests cover `tdistinvcdf`/`tdistinvccdf`, which also benefit from the F quantile changes, and the tdistinvlogcdf test from #244 is merged into the more extensive one here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
marked this pull request as ready for review
November 17, 2026 14:02
andreasnoack
approved these changes
Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR improves the accuracy of the F and t quantile functions in the upper tail.
The
tdistinvlogcdfchanges originally in this PR were split off into #244 (merged), sotdistinvlogcdfnow useslaplaceinvlogcdf.F quantiles
The quantile is
ν2 * x / (ν1 * (1 - x))for the corresponding quantilexof the beta variateu ~ Beta(ν1 / 2, ν2 / 2). Previously1 - xwas computed by subtraction, which cancels catastrophically forxclose to 1.fdistinvcdf/fdistinvccdf:beta_inc_invreturns1 - xalongsidex.fdistinvlogcdf(Rmathqbeta, which only returnsx): the quantile of whichever ofuand1 - u ~ Beta(ν2 / 2, ν1 / 2)is at most 1/2 is computed. Which one that is is decided upfront fromP(u ≤ 1/2) = beta_inc(ν1 / 2, ν2 / 2, 1/2).fdistinvlogccdf(ν1, ν2, logq) = inv(fdistinvlogcdf(ν2, ν1, logq))since1 / F ~ F(ν2, ν1).The handling of degenerate beta distributions in
betainvcdf/betainvccdf(beta_inc_invdoes not return for them, #154) is moved to a helper_beta_inc_invthat returns bothxand1 - xand is used by the F quantiles as well.t quantiles
tdistinvcdf/tdistinvccdfandtdistinvlogcdf/tdistinvlogccdfare computed fromfdistinvccdfandfdistinvlogccdf, so they are fixed in both tails as well. Forν = 1, 2andFloat64, the maximum error oftdistinvcdfoverp ∈ [1e-30, 1 - 1e-7]drops fromInf(and about 1e10epsin the bulk) on master to 1.9eps.Tests
rmathcomplooked upRmath.dfdist/Rmath.dtdistinstead ofRmath.df/Rmath.dt. They now run (7500 and 14620 tests) and pass.BigFloat: F withν1 = 2orν2 = 2(Float32,Float64), andtdistinvcdf,tdistinvccdf,tdistinvlogcdfandtdistinvlogccdfwithν = 1, 2(Float16,Float32,Float64). The latter replace the smallertdistinvlogcdftest added in Add Laplace distribution functions #244.Accuracy
Relative errors (in units of
eps(T)) against the closed-form quantiles, about 10k samples per row covering both tails (p ∈ [1e-30, 1 - 1e-30],logp ∈ [-100, -1e-17]) and the bulk:Float64fdistinvcdffdistinvccdffdistinvlogcdffdistinvlogccdfFor
Float32the errors are about 1.Remaining limitations (not introduced here):
p → 1the error oftdistinvlogcdfgrows with|log(1 - p)|sincelog(2(1 - p))is passed tofdistinvlogccdf(e.g., ~18epsforlogp = -1e-100).t². Ift²exceedsfloatmax(T)the result is wrong: forFloat32Inf, forFloat64a finite value (e.g.,tdistinvcdf(1.0, 1e-300) = -1.4e77andtdistinvlogcdf(1.0, -700.0) = -9.48e153instead of about-3.2e299and-3.2e303). Ift²is subnormal the result loses precision (e.g., forFloat16andpclose to 1/2).beta_inc_invis inaccurate if the median of the beta distribution is far from 1/2 (beta_inc_invis inaccurate if the median of the beta distribution is far from 1/2 JuliaMath/SpecialFunctions.jl#578), sofdistinvcdf/fdistinvccdfare less accurate for very different degrees of freedom.This PR does not address the F and t cdfs (#239), large
ν(#165), or the performance of the t quantiles (#228).Performance
fdistinvcdf/fdistinvccdfandtdistinvcdf/tdistinvccdfare unchanged. The log versions are 15–30% slower (e.g.,fdistinvlogcdf(3.0, 7.0, log(0.9)): 731 ns → 896 ns,tdistinvlogcdf(5.0, log(1e-8)): 458 ns → 572 ns) due to the additionalbeta_incevaluation.🤖 Generated with Claude Code