Skip to content

fix: evaluate the lower tail from p in stats/base/dists/laplace/quantile - #15773

Open
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/laplace-quantile-tails
Open

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/laplace-quantile-tails

Conversation

@Abhist17

@Abhist17 Abhist17 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • evaluates the lower tail of stats/base/dists/laplace/quantile from p itself (JS, factory and C).

The function computed mu - b*signum(p-0.5)*ln(1 - 2|p-0.5|). For p < 0.5, 1 - 2|p-0.5| is 2p, but forming 0.5 - p first rounds away the low-order bits of a small p. Precision degrades below p ~ 1e-8, and for p < 2^-54 the result is -Infinity:

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

quantile( 1.0e-20, 0.0, 1.0 );
// develop: -Infinity
// this PR: -45.35855467932097 (= ln(2e-20))

quantile( 1.0e-10, 0.0, 1.0 );
// develop: -22.332703666640143
// this PR: -22.33270374938051

It now returns mu + b*ln(2p) for p < 0.5, and mu - b*ln(2(1-p)) otherwise. Both products are exact, and 1 - p is exact for p >= 0.5. The upper tail was already accurate, since p - 0.5 is exact there. This also drops the signum and abs dependencies.

Tests: runner.jl gains a gen_tails function. It evaluates the textbook formula mu - b*sign(p-0.5)*ln(1 - 2|p-0.5|) in 2048-bit BigFloat and writes tails.json:

  • 500 points with p log-spaced over [1e-300, 0.1],
  • 500 points with 1-p log-spaced over [1e-16, 0.1].

mu is 0, so the test isolates the tail and not the rounding of mu + .... The new tests in test.quantile.js, test.factory.js and test.native.js pass at the existing 1-ULP tolerance. On develop, 498 of them fail. The existing fixtures still pass unchanged.

Related Issues

Does this pull request have any related issues?

None.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

This PR was written primarily by Claude Code. I found the bug by scanning stats/base/dists for ln( 1.0 - ... ).


@stdlib-js/reviewers

…antile`

`1 - 2|p-0.5|` rounds away the low-order bits of a small `p`: the lower
tail lost precision below `p ~ 1e-8` and returned `-Infinity` for
`p < 2^-54`. Use `ln(2p)` and `ln(2(1-p))`, which are exact up to the
logarithm.
@Abhist17
Abhist17 requested a review from a team October 1, 2026 23:40
@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 1, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/laplace/quantile $\\color{green}303/303$
$\\color{green}+100.00\\%$
$\\color{green}27/27$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}303/303$
$\\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