Skip to content

fix: use log1p for small p in stats/base/dists/weibull/quantile - #15762

Open
Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/weibull-quantile-small-p
Open

Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/weibull-quantile-small-p

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:

  • uses -log1p( -p ) instead of -ln( 1.0 - p ) in stats/base/dists/weibull/quantile (JS, factory and C).

Forming 1 - p in double precision rounds away the low-order bits of p. For p < 2^-53 it rounds to 1, so the function returns 0:

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

quantile( 1.0e-20, 2.0, 1.0 );
// develop: 0.0
// this PR: 1.0e-10

For p between 2^-53 and about 1e-8, develop returns a value with only a few correct digits. For p >= 0.5, 1 - p is exact, so nothing changes there.

Changes to the fixtures:

  • runner.jl now evaluates the textbook formula lambda * (-ln(1-p))^(1/k) in 2048-bit BigFloat instead of calling Distributions.quantile. Distributions computes the same -log(1-p) in double precision, so the old fixtures reproduced the bug instead of catching it. The three existing fixtures are regenerated with the same input ranges (new random draws).
  • There's a new small_p.json fixture with p log-spaced over [1e-300, 0.1]. Its shape parameters are 1, 2, 4 and 8, so 1/k is exact and the test measures the error from 1 - p. On develop, 997 of the 4,021 assertions in test.quantile.js fail. With this change they all pass at 1 ULP, in JS and native.

Tolerances are the smallest that pass in JS and native. both_large goes from 0 to 1 ULP, since the fixture is no longer bit-identical to the implementation's formula. large_scale goes from 6 to 56 ULP. That fixture has shapes down to about 0.01. Rounding 1/k to a double then shifts the result by up to |ln(-ln(1-p))| / k ULP. develop has the same error against the exact values, so this isn't a regression; the old fixture hid it because it used the same double-precision formula.

Related Issues

Does this pull request have any related issues?

None.

Questions

Any questions for reviewers of this pull request?

The same ln( 1.0 - p ) pattern is in exponential/quantile and, as pow( 1.0-p, 1.0/b ), in kumaraswamy/quantile. I can send those as separate PRs.

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 reading the stats/base/dists quantile functions for 1 - p cancellation.


@stdlib-js/reviewers

`1 - p` rounds away the low-order bits of `p`, and all of them for
`p < 2^-53`, where the function returned `0`. The fixtures are now
generated from the textbook formula in extended precision.
@Abhist17
Abhist17 requested a review from a team October 1, 2026 23:15
@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

stdlib-bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/weibull/quantile $\\color{green}294/294$
$\\color{green}+0.00\\%$
$\\color{green}25/25$
$\\color{green}+0.00\\%$
$\\color{green}4/4$
$\\color{green}+0.00\\%$
$\\color{green}294/294$
$\\color{green}+0.00\\%$

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

lambda = smallP.lambda;
k = smallP.k;
for ( i = 0; i < p.length; i++ ) {
y = quantile( p[i], k[i], lambda[i] );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
y = quantile( p[i], k[i], lambda[i] );
y = quantile( p[ i ], k[ i ], lambda[ i ] );

Non-blocking: Match the spacing used in the rest of this file. Also move the smallP require (line 33) below largeShape, as in test.quantile.js and test.factory.js.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 45ec184: the call now uses p[ i ], k[ i ] and lambda[ i ], and smallP is required after largeShape, matching test.quantile.js and test.factory.js.

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.

3 participants