Skip to content

Improve accuracy of fdist and tdist quantiles in the upper tail - #240

Merged
devmotion merged 11 commits into
masterfrom
dmw/tdistinvlogcdf
Nov 17, 2026
Merged

devmotion merged 11 commits into
masterfrom
dmw/tdistinvlogcdf

Conversation

@devmotion

@devmotion devmotion commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

This PR improves the accuracy of the F and t quantile functions in the upper tail.

The tdistinvlogcdf changes originally in this PR were split off into #244 (merged), so tdistinvlogcdf now uses laplaceinvlogcdf.

F quantiles

The quantile is ν2 * x / (ν1 * (1 - x)) for the corresponding quantile x of the beta variate u ~ Beta(ν1 / 2, ν2 / 2). Previously 1 - x was computed by subtraction, which cancels catastrophically for x close to 1.

  • fdistinvcdf/fdistinvccdf: beta_inc_inv returns 1 - x alongside x.
  • fdistinvlogcdf (Rmath qbeta, which only returns x): the quantile of whichever of u and 1 - u ~ Beta(ν2 / 2, ν1 / 2) is at most 1/2 is computed. Which one that is is decided upfront from P(u ≤ 1/2) = beta_inc(ν1 / 2, ν2 / 2, 1/2).
  • fdistinvlogccdf(ν1, ν2, logq) = inv(fdistinvlogcdf(ν2, ν1, logq)) since 1 / F ~ F(ν2, ν1).

The handling of degenerate beta distributions in betainvcdf/betainvccdf (beta_inc_inv does not return for them, #154) is moved to a helper _beta_inc_inv that returns both x and 1 - x and is used by the F quantiles as well.

t quantiles

tdistinvcdf/tdistinvccdf and tdistinvlogcdf/tdistinvlogccdf are computed from fdistinvccdf and fdistinvlogccdf, so they are fixed in both tails as well. For ν = 1, 2 and Float64, the maximum error of tdistinvcdf over p ∈ [1e-30, 1 - 1e-7] drops from Inf (and about 1e10 eps in the bulk) on master to 1.9 eps.

Tests

  • The "RMath fdist" and "RMath tdist" test items silently ran no tests: rmathcomp looked up Rmath.dfdist/Rmath.dtdist instead of Rmath.df/Rmath.dt. They now run (7500 and 14620 tests) and pass.
  • New tests compare against closed-form quantiles in 1024-bit BigFloat: F with ν1 = 2 or ν2 = 2 (Float32, Float64), and tdistinvcdf, tdistinvccdf, tdistinvlogcdf and tdistinvlogccdf with ν = 1, 2 (Float16, Float32, Float64). The latter replace the smaller tdistinvlogcdf test 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:

Float64 this PR: median / max master: max
fdistinvcdf 0.3–1.5 / 7–36 up to 2e15, NaN/Inf
fdistinvccdf 0.3–1.6 / 6–64 up to 2e15, NaN/Inf
fdistinvlogcdf 0.7–4 / 15–39 up to 2e15, NaN/Inf
fdistinvlogccdf 0.7–4 / 30–39 up to 2e15, NaN/Inf

For Float32 the errors are about 1.

Remaining limitations (not introduced here):

  • For p → 1 the error of tdistinvlogcdf grows with |log(1 - p)| since log(2(1 - p)) is passed to fdistinvlogccdf (e.g., ~18 eps for logp = -1e-100).
  • The t quantile is computed via the F quantile t². If t² exceeds floatmax(T) the result is wrong: for Float32 Inf, for Float64 a finite value (e.g., tdistinvcdf(1.0, 1e-300) = -1.4e77 and tdistinvlogcdf(1.0, -700.0) = -9.48e153 instead of about -3.2e299 and -3.2e303). If t² is subnormal the result loses precision (e.g., for Float16 and p close to 1/2).
  • beta_inc_inv is inaccurate if the median of the beta distribution is far from 1/2 (beta_inc_inv is inaccurate if the median of the beta distribution is far from 1/2 JuliaMath/SpecialFunctions.jl#578), so fdistinvcdf/fdistinvccdf are 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/fdistinvccdf and tdistinvcdf/tdistinvccdf are 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 additional beta_inc evaluation.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.39%. Comparing base (b4b6b37) to head (b927e68).

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

devmotion and others added 2 commits November 16, 2026 14:45
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
devmotion changed the base branch from master to dmw/laplace November 16, 2026 15:11
devmotion and others added 4 commits November 16, 2026 16:55
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>
Base automatically changed from dmw/laplace to master November 17, 2026 00:15
devmotion and others added 2 commits November 17, 2026 01:17
`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
devmotion marked this pull request as ready for review November 17, 2026 14:02
@devmotion
devmotion merged commit 08bf7f7 into master Nov 17, 2026
14 checks passed
@devmotion
devmotion deleted the dmw/tdistinvlogcdf branch November 17, 2026 21:18
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