Repository navigation
Add Laplace distribution functions - #244
Merged
Merged
Conversation
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>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #244 +/- ##
==========================================
+ Coverage 75.85% 77.22% +1.37%
==========================================
Files 23 24 +1
Lines 1147 1190 +43
==========================================
+ Hits 870 919 +49
+ Misses 277 271 -6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
andreasnoack
approved these changes
Nov 16, 2026
devmotion
added a commit
that referenced
this pull request
Nov 17, 2026
`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
added a commit
that referenced
this pull request
Nov 17, 2026
* Rename extensions to ChainRulesCoreExt and InverseFunctionsExt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add Laplace distribution functions 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> * Test the low part of log(2) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Test promotion of all quantile functions and tdistinvlogcdf Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Compare fdist and tdist with Rmath in the tests again 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> * Improve accuracy of fdist quantiles in the upper tail 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> * Test tdistinvlogcdf also for Float16 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Simplify log F quantiles and test tdistinvcdf tails `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> * Bump version from 2.3.0 to 2.3.1 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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 adds
laplace{pdf,logpdf,cdf,ccdf,logcdf,logccdf,invcdf,invccdf,invlogcdf,invlogccdf}in standard and(μ, θ, x)form.Motivation
#240 (
tdistinvlogcdf) and JuliaStats/Distributions.jl#2115 (invlogcdf(::Laplace, ...)) contain the same branches for computing, fromlp = log(p),This is the Laplace quantile at
exp(lp). LogExpFunctions'log2mexpcontains a third copy of the high/low split oflog(2)that it needs. Withlaplaceinvlogcdfhere, the callers have no branches:l = laplaceinvlogcdf(logp); copysign(sqrt(fdistinvlogccdf(1, ν, -abs(l))), l)(this PR; it replaces thetdist.jlpart of Improve accuracy of fdist and tdist quantiles in the upper tail #240, whose F-quantile and test changes are independent)invlogcdf(d::Laplace, lp) = laplaceinvlogcdf(d.μ, d.θ, lp)and so on (follow-up PR)StatsFuns no longer uses
log2mexpinternally, but still reexports it. I plan to deprecate it in LogExpFunctions.Implementation
laplaceinvlogcdf:y = (lp + T(logtwo)) + _logtwo_lo(T)islog(2p), accurate even atlp == T(loghalf), where its sign decides the side. Fory > 0, the result is-log1p(-expm1(y))near the median and-log(-2 * expm1(lp))in the upper tail.laplacelogcdf:log1p(-exp(-z) / 2)forz ≥ 0is well conditioned.(μ, θ, x)methods oflogpdfand of the inverse functions promote their arguments first.laplacelogpdf, and InverseFunctions pairs for all four cdf/inverse pairs.ChainRulesCoreExtandInverseFunctionsExt.Accuracy
Maximum error in ulps against 1024-bit
BigFloatclosed forms, over about 3e5 samples per function covering the tails, the median and the branch cutoff. "Distributions" is the currentLaplaceimplementation in Distributions master (with LogExpFunctions 1.0.6):Float64: Distributions → this PRFloat32Float16logcdf: Distributions'loghalf + log2mexp(-z)cancels completely for largezand returns0instead of about-exp(-z) / 2.invlogcdf:logtwo + lplacks the low part oflog(2), which dominates the result near the median.ForwardDiff through the primals yields the exact closed forms of the derivatives (
1,u / (2 - u)andp / (1 - p)), with maximum errors of 2.84 (d logcdf) and 2.58 (d invlogcdf) ulps forFloat64. So no hand-written rules are needed for the cdf and quantile functions.Tests
Rmath has no Laplace distribution. The new tests compare against closed forms in
BigFloat(forFloat16,Float32,Float64andBigFloat), check special values and promotion, and compare ForwardDiff derivatives.laplaceis also added to the genericDual, ChainRules and InverseFunctions tests.🤖 Generated with Claude Code