Skip to content

fix: avoid overflow for large quantiles in stats/base/dists/betaprime/quantile - #17145

Open
Udaygund wants to merge 2 commits into
stdlib-js:developfrom
Udaygund:fix/betaprime-quantile-upper-tail
Open

Udaygund wants to merge 2 commits into
stdlib-js:developfrom
Udaygund:fix/betaprime-quantile-upper-tail

Conversation

@Udaygund

@Udaygund Udaygund commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #17099.

Description

What is the purpose of this pull request?

This pull request:

  • fixes stats/base/dists/betaprime/quantile (lib/main.js, lib/factory.js, and src/main.c) returning Infinity for large but finite quantiles. The quantile was computed as x / ( 1.0 - x ) with x = betaincinv( p, alpha, beta ). When the quantile exceeds about 1e16, x rounds to 1, so 1.0 - x is 0. For example, quantile( 0.5, 1.0, 0.001 ) returned Infinity instead of 2^1000 - 1 ≈ 1.0715e301.
  • now calls kernelBetaincinv( alpha, beta, p, 1.0 - p ) directly, which returns both x and 1-x, and computes x / (1-x) from those two values. betaincinv (lower tail) is a thin wrapper around the same call, so p = 0 (0), p = 1 (Infinity), and NaN handling are unchanged.
  • returns Infinity when the kernel's 1-x is below the smallest normal number (2.2e-308). The kernel clamps its search at that value, so without this check the ratio would be a large but meaningless finite number (e.g., quantile( 0.9, 2.0, 0.001 ) returned 4.494e307 instead of 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?

No.

Other

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

New tests (JS, factory, and native) use 7 cases with alpha = 1, where the quantile has the closed form (1-p)^(-1/beta) - 1. Reference values were computed with 50 digits of precision, choosing p such that 1-p is exact in double precision. Errors in ULP:

p beta before after
0.99 0.01 Infinity 57
0.5 0.001 Infinity 486
0.9 0.004 Infinity 75
0.75 0.002 Infinity 130
1 - 2^-40 0.05 Infinity 277
1 - 2^-30 0.5 Infinity 0
1 - 2^-50 1 0 0

The tolerance is 1000 ULP. For small beta, the inverse is ill-conditioned: a relative error d in the incomplete beta function corresponds to a relative error of about d/beta in the quantile. Six of the seven checks fail on the previous code.

I also checked:

  • on the existing Julia fixtures (3,000 cases), the maximum error is unchanged or lower (29 → 29, 36 → 34, and 34 → 34 ULP). Mean errors change by at most 0.17 ULP (3.83 → 4.00 for large_alpha), which is within the fixtures' own error. A variant that uses the kernel's 1-x only for x > 0.99 keeps the fixture results identical, but is less accurate near x = 1 (max 27 vs. 7 ULP on 179 alpha = 1 cases with p from 0.9 to 1 - 1e-15), so I kept the simpler version.
  • three further tests (JS, factory, and native) check that quantiles beyond the largest double ((0.55, 2, 0.001), (0.9, 2, 0.001), (0.6, 0.5, 0.001)) return Infinity.
  • the native addon builds, and the JS, factory, and native tests pass, along with the commit-hook linters.

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

…e/quantile`

Uses both `x` and `1-x` as returned by `kernelBetaincinv`, instead of
computing `1-x` by subtraction, which returns `Infinity` when `x` rounds
to `1` (i.e., when the quantile exceeds about `1e16`).

Resolves: stdlib-js#17099

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 12:49
@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/betaprime/quantile $\\color{green}332/332$
$\\color{green}+0.00\\%$
$\\color{green}29/29$
$\\color{green}+0.00\\%$
$\\color{green}4/4$
$\\color{green}+0.00\\%$
$\\color{green}332/332$
$\\color{green}+0.00\\%$

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

…n range

When `1-x` returned by the incomplete beta inverse falls below the smallest normal number, the ratio `x/(1-x)` is not representable and the clamped kernel result would otherwise yield a large finite value.

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/betaprime/quantile returns Infinity for large finite quantiles

2 participants