Repository navigation
Use StatsFuns for Laplace evaluations - #2115
Merged
Merged
Conversation
For `z ≥ 0` the log cdf `log(1 - exp(-z) / 2)` was computed as `loghalf + log2mexp(-z)`, which cancels and returns zero for large `z`. It is now computed as `log1p(-exp(-z) / 2)`, which is well-conditioned since `0 < exp(-z) / 2 ≤ 1/2` (and analogously for `logccdf`). 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 #2115 +/- ##
==========================================
- Coverage 87.37% 87.35% -0.02%
==========================================
Files 149 149
Lines 8948 8936 -12
==========================================
- Hits 7818 7806 -12
Misses 1130 1130 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…e median Close to the median the quantile is small and both `lp + logtwo` and `logtwo + log1mexp(lp)` cancel. `lp + logtwo` is exact there, so adding the low part of `log(2)` yields an accurate result; for `log(1/2) ≤ lp < log(1/√2)` the quantile is computed as `-log1p(-expm1(lp + log(2)))`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
logcdf and logccdf of Laplacelogcdf, logccdf, invlogcdf, and invlogccdf of Laplace
StatsFuns 2.3.0 provides `laplacepdf`, ..., `laplaceinvlogccdf` with the accurate `logcdf`/`logccdf`/`invlogcdf`/`invlogccdf` implementations, so delegate to them instead of maintaining them here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
logcdf, logccdf, invlogcdf, and invlogccdf of LaplaceLaplace evaluations
devmotion
marked this pull request as ready for review
November 17, 2026 13:08
andreasnoack
approved these changes
Nov 17, 2026
nsiccha
added a commit
to nsiccha/Distributions.jl
that referenced
this pull request
Nov 18, 2026
… (+3 more) Lands 4 commits: - d202c57 Test latest patch of the minimum Julia version in CI (JuliaStats#2110) - e433723 Replace CompatHelper with Dependabot for Julia compat updates (JuliaStats#2109) - dd5ad8e Fix missing power in `KSDist` Durbin matrix (JuliaStats#2112) - 264883f Use StatsFuns for `Laplace` evaluations (JuliaStats#2115) KB-Branch: kb-impl/Distributions-rebase KB-Committer-Id: Distributions:rebase
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 implements
pdf,logpdf,cdf,ccdf,logcdf,logccdf,quantile,cquantile,invlogcdf, andinvlogccdfofLaplacewith the functions added in StatsFuns 2.3.0 (JuliaStats/StatsFuns.jl#244), via@_delegate_statsfunsas forNormal,Gamma, etc. The StatsFuns compat is bumped to 2.3.This fixes the accuracy of
logcdf,logccdf,invlogcdf, andinvlogccdf:z = (x - μ) / θ ≥ 0,logcdf(::Laplace, x)computedlog(1 - exp(-z) / 2)asloghalf + log2mexp(-z). This cancels and returns0.0for largez, where the result is≈ -exp(-z) / 2(e.g., about-2e-18forz = 40). The same holds forlogccdfwithz ≤ 0. StatsFuns computes it aslog1p(-exp(-z) / 2).invlogcdfandinvlogccdfcancelled: e.g.,invlogcdf(Laplace(), log(0.5))returned0.0instead of2.3e-17(log(0.5)is rounded). StatsFuns adds the low part oflog(2)and uses-log1p(-expm1(⋯))in the range where-log(2(1 - p))would cancel.Further changes from StatsFuns:
logpdf,quantile,cquantile,invlogcdf, andinvlogccdfpromote the parameters and the argument before computing, so that, e.g.,log(θ)is not computed in a lower precision than the result.This also removes the only use of
log2mexpin Distributions (see JuliaStats/LogExpFunctions.jl#140).Examples
Float64results, relative error w.r.t. a 256-bitBigFloatreference in parentheses:logcdf(Laplace(), 1.0)-0.2032670549151953(1.1e-16)-0.20326705491519534(3.0e-17)-0.203267…logcdf(Laplace(), 5.0)-0.003374661268985668(1.0e-14)-0.0033746612689856343(5.6e-17)-0.00337466…logcdf(Laplace(), 20.0)-1.0305767350615724e-9(7.4e-8)-1.0305768117503233e-9(1.1e-16)-1.03058…e-9logcdf(Laplace(), 40.0)0.0(1.0)-2.1241771276457944e-18(3.0e-17)-2.12418…e-18logcdf(Laplace(), 100.0)0.0(1.0)-1.860037988010418e-44(4.2e-17)-1.86004…e-44logccdf(Laplace(), -40.0)0.0(1.0)-2.1241771276457944e-18(3.0e-17)-2.12418…e-18invlogcdf(Laplace(), log(0.5))0.0(1.0)2.3190468138462996e-17(4.8e-17)2.31905…e-17invlogcdf(Laplace(), nextfloat(log(0.5)))1.1102230246251565e-16(0.17)1.3421277060097865e-16(1.2e-16)1.34213…e-16invlogcdf(Laplace(), prevfloat(log(0.5), 16))-1.7763568394002505e-15(0.013)-1.7531663712617875e-15(1.4e-18)-1.75317…e-15invlogcdf(Laplace(), log(0.5) - 0.01)-0.010000000000000009(2.3e-15)-0.009999999999999986(6.4e-17)-0.01invlogccdf(Laplace(), log(0.5))0.0(1.0)-2.3190468138462996e-17(4.8e-17)-2.31905…e-17logpdf(Laplace(0f0, 3f0), 1.0)-2.1250928243001304(1.0e-8)-2.1250928025613884(4.9e-17)-2.12509…quantile(Laplace(0.0, 3.0), 0.3f0)-1.5324767231941223(1.9e-8)-1.5324767520886846(1.7e-16)-1.53248…Unchanged in the other tail, e.g.,
logcdf(Laplace(), -5.0)andinvlogcdf(Laplace(), -20.0)return the same values as on master.Timings (
Float64, mean over 1000 random inputs):logcdf6.8 ns → 4.3 ns,invlogcdf1.4 ns → 4.0 ns.Script
Tests
logcdfandlogccdfare compared with aBigFloatreference forFloat32andFloat64.invlogcdfandinvlogccdfare compared with aBigFloatreference close to the median, aroundlog(1/√2), and in both tails forFloat16,Float32,Float64, andBigFloat.🤖 Generated with Claude Code