Skip to content

fix: avoid cancellation for small x in stats/base/dists/kumaraswamy/cdf - #15767

Open
Abhist17 wants to merge 4 commits into
stdlib-js:developfrom
Abhist17:fix/kumaraswamy-cdf-small-x
Open

Abhist17 wants to merge 4 commits into
stdlib-js:developfrom
Abhist17:fix/kumaraswamy-cdf-small-x

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/cdf as -expm1( b * log1p( -pow( x, a ) ) ) instead of 1 - pow( 1 - pow( x, a ), b ) (JS, factory and C),
  • adds the fixtures the tests had a TODO for.

Forming 1 - x^a rounds away the low-order bits of x^a, and subtracting (1 - x^a)^b from 1 then cancels. Whenever the CDF is small, most of the digits are wrong. For x^a < 2^-53 the result is exactly 0:

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

cdf( 0.004028458748472796, 4.866163078075099, 0.8091128390341935 );
// develop: 1.7956747200287282e-12
// exact:   1.7956617511906528e-12  (this PR, within 2 ULP)

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

Tests:

  • There's a new test/fixtures/julia/runner.jl that evaluates the textbook formula 1 - (1 - x^a)^b in 2048-bit BigFloat. It writes data.json (random x in (0,1), a and b in [0.5, 5.5]) and small_x.json (x log-spaced over [1e-100, 0.1]).
  • The "evaluates the cdf" tests in test.cdf.js, test.factory.js and test.native.js only checked that the result is a number (// TODO: Add fixtures). They are replaced with tests against the two fixtures at 2 ULP. That's the max error in both JS and native.
  • On develop, the max error is about 6.4e10 ULP on data.json, and 0 where the CDF is around 1e-16 on small_x.json. 1,220 of the 2,127 assertions in test.cdf.js fail. With this change they all pass.

Related Issues

Does this pull request have any related issues?

None.

Questions

Any questions for reviewers of this pull request?

kumaraswamy/logcdf and kumaraswamy/quantile have the same cancellation. I'll send them separately.

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 - u cancellation.


@stdlib-js/reviewers

…y/cdf`

`1 - x^a` rounds away the low-order bits of `x^a`, and subtracting
`(1 - x^a)^b` from `1` then cancels: for random inputs only about four
digits were right, and for `x^a < 2^-53` the function returned `0`.
Use `-expm1( b * log1p( -x^a ) )` and add fixtures generated from the
textbook formula in extended precision.
@Abhist17
Abhist17 requested a review from a team October 1, 2026 23:28
@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/kumaraswamy/cdf $\\color{green}353/353$
$\\color{green}+0.00\\%$
$\\color{green}33/33$
$\\color{green}+0.00\\%$
$\\color{green}4/4$
$\\color{green}+0.00\\%$
$\\color{green}353/353$
$\\color{green}+0.00\\%$

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

return 1.0;
}
return 1.0 - pow( 1.0 - pow( x, a ), b );
return -expm1( b * log1p( -pow( x, a ) ) );

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
return -expm1( b * log1p( -pow( x, a ) ) );
u = pow( x, a );
if ( u < 0.5 ) {
return -expm1( b * log1p( -u ) );
}
return -expm1( b * ln( -expm1( a * ln( x ) ) ) );

When x^a is close to 1, pow( x, a ) rounds before log1p: cdf( 0.999999999999999, 1.3786, 0.0841 ) returns 0.943898… (exact 0.943740…, ~1.4e12 ULP, same on develop). #15768 already splits on u < 0.5 for logcdf; with the same split here the max error is 2 ULP across the range. Could you fold it in? Same in lib/factory.js:80 and src/main.c:53; needs var u; and ln.

…/cdf`

Per review: when `x^a` is close to 1, `pow( x, a )` rounds before `log1p`,
so `cdf( 0.999999999999999, 1.3786, 0.0841 )` gave 0.943898 (exact 0.943740).
Split on `u < 0.5` as `logcdf` does and take `ln( -expm1( a*ln(x) ) )` above.
@Abhist17

Abhist17 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

@0PrashantYadav0 folded in, 1adfd59: the u < 0.5 split in lib/main.js, lib/factory.js and src/main.c, with var u; / double u; and ln. cdf( 0.999999999999999, 1.3786, 0.0841 ) now gives 0.9437402815751305 in main and factory. test.cdf.js / test.factory.js / test.native.js pass 2,027 / 2,031 / 2,027.

@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. Non-blocking: no fixture reaches x near 1 yet (data.json stops at 0.99908), so the new u >= 0.5 branch isn't tested where the old path lost accuracy. Maybe add x = 1.0 .- exp10.( range( -16.0, stop = -1.0, length = 1000 ) ) to runner.jl?

Per review: no fixture reached the `u >= 0.5` branch where the old path
lost accuracy. Adds `large_x.json` with `x = 1 - 10^k`, `k` in [-16, -1].
This branch is within 1 ULP; develop is over 2 ULP on 51 of the points
(max 23,412).
@Abhist17

Copy link
Copy Markdown
Contributor Author

@0PrashantYadav0 added in fa51bb0: large_x.json with your x = 1.0 .- exp10.( range( -16.0, stop = -1.0, length = 1000 ) ), plus a block in each test file at 2 ULP. This branch is within 1 ULP on it; develop is over 2 ULP on 51 of the 1,000 points (max 23,412), so it does exercise the u >= 0.5 path. test.cdf.js / test.factory.js / test.native.js pass 3,027 / 3,031 / 3,027.

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