Skip to content

fix: evaluate the upper tail without cancellation in stats/base/dists/laplace/logcdf - #15968

Open
Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/laplace-logcdf-upper-tail
Open

Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/laplace-logcdf-upper-tail

Conversation

@Abhist17

@Abhist17 Abhist17 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • evaluates the upper tail of stats/base/dists/laplace/logcdf without cancellation (JS, factory and C).

Above the mean the function computed ln(1/2) + log1p( 1 - exp(-z) ). That's ln(1/2) + ln(2 - exp(-z)), which cancels and rounds to 0 once exp(-z) is below an ULP of 2:

var logcdf = require( '@stdlib/stats/base/dists/laplace/logcdf' );

logcdf( 50.0, 0.0, 1.0 );
// develop: 0
// this PR: -9.643749239819589e-23

logcdf( 3.0, 0.0, 1.0 );
// develop and this PR: -0.025208618236919594

It now uses the equivalent log1p( -exp(-z)/2 ), which has no subtraction. The lower branch and x = mu are unchanged.

Fixtures and tolerances. The fixtures came from Distributions.jl's logcdf, which evaluates the same expression in Float64, and the tests asserted them at 0 ULP. So develop was being checked for bit-equality with a reference that has the same cancellation. gen now evaluates the log CDF in 2048-bit BigFloat, and all three fixtures were regenerated. Measured against them:

fixture this PR: p99 / max ULP develop: p99 / max ULP
positive_mean 1 / 1 1 / 1
negative_mean 1 / 1 1 / 1
large_variance 3 / 155 803 / ~4.3e18 (0)

The tolerances are set from those numbers: 1, 1 and 160 ULP. The 160 is not slack in the formula. Far above the mean the result is about -exp(-z)/2, so the rounding of z = (x-mu)/b gets amplified by z, and large_variance reaches z ≈ 120. Any double-precision evaluation hits that; there's a comment on the assertion saying so. test.logcdf.js / test.factory.js / test.native.js pass 3,013 / 3,018 / 3,013.

Related Issues

Does this pull request have any related issues?

None.

Questions

Any questions for reviewers of this pull request?

…s/laplace/logcdf`

Above the mean, `ln(1/2) + log1p( 1 - exp(-z) )` cancels and rounds to 0:
`logcdf( 50, 0, 1 )` returned 0 instead of about -9.64e-23. Use the
equivalent `log1p( -exp(-z)/2 )`.

The fixtures came from Distributions.jl's Float64 logcdf, which has the same
cancellation, and were asserted at 0 ULP; they are now evaluated in
2048-bit BigFloat, with tolerances set from measured error.
@Abhist17
Abhist17 requested a review from a team October 7, 2026 21:40
@stdlib-bot

Copy link
Copy Markdown
Contributor

Hello! Thank you for your contribution to stdlib.

We noticed that the contributing guidelines acknowledgment is missing from your pull request. Here's what you need to do:

  1. Please read our contributing guidelines.

  2. Update your pull request description to include this checked box:

    - [x] Read, understood, and followed the [contributing guidelines](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md)

This acknowledgment confirms that you've read the guidelines, which include:

  • The developer's certificate of origin
  • Your agreement to license your contributions under the project's terms

We can't review or accept contributions without this acknowledgment.

Thank you for your understanding and cooperation. We look forward to reviewing your contribution!

@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Oct 7, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/laplace/logcdf $\\color{green}293/293$
$\\color{green}+100.00\\%$
$\\color{green}23/23$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}293/293$
$\\color{green}+100.00\\%$

The above coverage report was generated for the changes in this PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review A pull request which needs code review. Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants