Skip to content

fix: improve accuracy of stats/base/dists/pareto-type1/{cdf,logcdf} - #17148

Open
Udaygund wants to merge 2 commits into
stdlib-js:developfrom
Udaygund:fix/pareto-type1-cdf-accuracy
Open

Udaygund wants to merge 2 commits into
stdlib-js:developfrom
Udaygund:fix/pareto-type1-cdf-accuracy

Conversation

@Udaygund

@Udaygund Udaygund commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #17147.

Description

What is the purpose of this pull request?

This pull request:

  • fixes stats/base/dists/pareto-type1/cdf and stats/base/dists/pareto-type1/logcdf (lib/main.js, lib/factory.js, and src/main.c in each) losing accuracy when (beta/x)^alpha is close to 1, and returning 0/-Infinity when it rounds to 1 (e.g., cdf( 1.0e300, 1.0e-20, 1.0 ) returned 0 instead of 6.908e-18). It also fixes the case where beta/x underflows (e.g., cdf( 1.0e300, 0.001, 1.0e-300 ) returned 1 instead of 0.7488).
  • adds a private helper ln_ratio (lib/ln_ratio.js, and a static function in src/main.c) in each package, which evaluates ln(x/beta) accurately: log1p( (x-beta)/beta ) for x < 2*beta, where x-beta is exact, ln(x) - ln(beta) if x/beta overflows, and ln(x/beta) otherwise.
  • computes the CDF as -expm1( -t ), where t = alpha * ln(x/beta).
  • computes the log-CDF as log1mexp( t ) (i.e., ln( 1 - exp(-t) ) without cancellation), except when x >= 2*beta, t > ln(2), and beta/x is not subnormal, where it keeps log1p( -pow( beta/x, alpha ) ). There, pow is more accurate, as the rounding error of beta/x is amplified by alpha, while that of t is amplified by t >= alpha*ln(2) (using log1mexp( t ) everywhere gives up to 89 vs. 15 ULP on the large_alpha fixtures). Conversely, for x close to beta and large alpha, pow is much less accurate (e.g., logcdf( 1.0000000001, 1.0e12, 1.0 ) is off by about 7.5e7 ULP with pow, and by 83 ULP with log1mexp).
  • returns 0 (cdf) and -Infinity (logcdf) for x = beta explicitly, as alpha * ln(x/beta) is NaN for alpha = +Infinity.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

Questions

Any questions for reviewers of this pull request?

The existing Julia fixtures match the previous implementation, and are themselves off by up to about 1030 ULP (cdf) and 107 ULP (logcdf) from exact values (computed with Python's decimal module at 320 digits). As Julia isn't available to me, I kept the fixtures and set each fixture test's tolerance to the largest difference between the new implementation and the fixture, with a comment explaining why, and added tests against exact values. Regenerating the fixtures with the same Julia script would likely reproduce the cancellation, so new fixtures would need high-precision (e.g., BigFloat) reference values. Would you prefer that I do that instead?

Other

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

Maximum error in ULP relative to exact values, on the existing fixture inputs (1,000 cases each):

package fixture set fixture values previous new
cdf both_large 228 228 1.5
cdf large_alpha 1030 1030 1.3
cdf large_beta 359 359 1.4
logcdf both_large 47 47 23
logcdf large_alpha 15 15 15
logcdf large_beta 107 107 6.6

On the 8 new test cases (the cases from #17147, the underflow case, and a regular case), the new implementation returns the correctly rounded exact values in both JS and C, while the previous one returned 0, 1, -Infinity, or values with relative errors up to 9.5%. For large alpha and x close to beta, logcdf is within 83 ULP of the exact values on the added test cases (the result is ill-conditioned with respect to alpha; the previous implementation was off by up to 8.2e8 ULP). The JS and C implementations agree to within 1 ULP on all fixture inputs. x = beta (0 / -Infinity, now also for alpha = +Infinity), x < beta, x = Infinity (1 / -0), and NaN handling are unchanged.

Ran locally: the JS, factory, and native tests of both packages (native addons built), all passing, plus the JS linter and cppcheck.

Checklist

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

  • Read, understood, and followed the [contributing guidelines][contributing].

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.".


@stdlib-js/reviewers

Evaluates `t = alpha * ln(x/beta)` accurately and computes the CDF as
`-expm1( -t )` and the log-CDF as `ln( -expm1( -t ) )` for `t <= ln(2)`,
avoiding cancellation when `(beta/x)^alpha` is close to `1`, and handles
underflow of `beta/x`.

Resolves: stdlib-js#17147

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: na
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@Udaygund
Udaygund requested a review from a team December 4, 2026 13:47
@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Dec 4, 2026
@stdlib-bot

stdlib-bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/pareto-type1/cdf $\\color{green}390/390$
$\\color{green}+0.00\\%$
$\\color{green}31/31$
$\\color{green}+0.00\\%$
$\\color{green}5/5$
$\\color{green}+0.00\\%$
$\\color{green}390/390$
$\\color{green}+0.00\\%$
stats/base/dists/pareto-type1/logcdf $\\color{green}414/414$
$\\color{green}+0.00\\%$
$\\color{green}39/39$
$\\color{green}+0.00\\%$
$\\color{green}5/5$
$\\color{green}+0.00\\%$
$\\color{green}414/414$
$\\color{green}+0.00\\%$

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

… `x` near `beta`

-   returns `0` (`cdf`) and `-Infinity` (`logcdf`) for `x = beta`, even when `alpha` is `+Infinity`, instead of `NaN`
-   uses `pow( beta/x, alpha )` in `logcdf` only for `x >= 2*beta`, and `log1mexp( alpha*ln(x/beta) )` otherwise, as `pow` amplifies the rounding error of `beta/x` by `alpha`

Ref: stdlib-js#17147

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: na
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---

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.

[BUG]: stats/base/dists/pareto-type1/cdf and logcdf lose accuracy when (beta/x)^alpha is close to 1

2 participants