Skip to content

fix: avoid cancellation for small p in stats/base/dists/kumaraswamy/quantile - #15769

Open
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/kumaraswamy-quantile-small-p
Open

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/kumaraswamy-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:

  • evaluates stats/base/dists/kumaraswamy/quantile as pow( -expm1( log1p(-p) / b ), 1/a ) instead of pow( 1 - pow( 1-p, 1/b ), 1/a ) (JS, factory and C),
  • adds the fixtures the tests had a TODO for.

Forming 1 - p rounds away the low-order bits of p, and 1 - (1-p)^(1/b) then cancels. For p < 2^-53 the quantile is 0:

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

quantile( 1.0e-20, 2.0, 3.0 );
// develop: 0.0
// this PR: 5.7735026918962574e-11 (= sqrt(1e-20/3))

quantile( 0.009392149102638081, 0.8085158437846791, 5.301823844827649 );
// develop: 0.0003969237106945341
// this PR: 0.0003969237106945461
// exact:   0.00039692371069454595

Tests:

  • There's a new test/fixtures/julia/runner.jl that evaluates the textbook formula (1 - (1-p)^(1/b))^(1/a) in 2048-bit BigFloat. It writes:
    • data.json: random p, with a and b in [0.5, 5.5],
    • small_p.json: p log-spaced over [1e-300, 0.1]. The a values there are 1, 2, 4 and 8, so 1/a is exact and the test measures the error from 1 - p. With a general a, rounding 1/a alone moves the result by up to |ln w| / a ULP, where w = 1 - (1-p)^(1/b).
  • The "evaluates the quantile" tests only checked that the result is a number (// TODO: Add fixtures). They are replaced with tests against the fixtures.
  • Max error in JS and native is 3 ULP on data (develop: 219) and 1 ULP on small_p (develop: 0 for p < 2^-53). On develop, 1,077 of the 2,021 assertions in test.quantile.js fail. With this change they all pass.

Related Issues

Does this pull request have any related issues?

None. Companion to #15767 (kumaraswamy/cdf) and #15768 (kumaraswamy/logcdf).

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


@stdlib-js/reviewers

…y/quantile`

`1 - p` rounds away the low-order bits of `p`, and `1 - (1-p)^(1/b)`
then cancels. For `p < 2^-53` the function returned `0`. Use
`-expm1( log1p(-p) / b )` and add fixtures generated from the textbook
formula in extended precision.
@Abhist17
Abhist17 requested a review from a team October 1, 2026 23:32
@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/kumaraswamy/quantile $\\color{green}323/323$
$\\color{green}+100.00\\%$
$\\color{green}25/25$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}323/323$
$\\color{green}+100.00\\%$

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

@Imkkey Imkkey left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI-assisted review of dafdeba723cb101c4c15a8fb33f6499a5e665784: no blocking issue found in the targeted numerical checks.

  • The unchanged JS quantile and factory tests pass: 2,021 and 2,022 assertions. Running the quantile tests against the base implementation reproduces the reported 1,077 failures.
  • Independently evaluated the textbook formula with mpmath at 2,048-bit precision. All 2,000 fixture expectations agree after rounding to float64.
  • Compiled the PR's C source with published stdlib C dependencies: maximum fixture errors were 3 ULP for data.json and 1 ULP for small_p.json. Also checked 1,816 additional finite-parameter cases and 348 C endpoint/invalid-input assertions.

For example, quantile(1e-20, 2, 3) now returns 5.7735026918962574e-11, matching the independent reference, where the base returns 0.

The checks used actual PR source and stdlib dependencies, with import-name mapping for monorepo paths. This was scoped numerical validation; the full monorepo suite and Node native addon were not run locally. The additional corpus excludes intermediate p/b underflow.

@0PrashantYadav0 0PrashantYadav0 left a comment

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.

LGTM

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.

4 participants