Skip to content

fix: improve accuracy for small x in stats/base/dists/gamma/pdf - #17053

Open
Udaygund wants to merge 1 commit into
stdlib-js:developfrom
Udaygund:fix/gamma-pdf-small-x
Open

Udaygund wants to merge 1 commit into
stdlib-js:developfrom
Udaygund:fix/gamma-pdf-small-x

Conversation

@Udaygund

@Udaygund Udaygund commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #17052.

Description

What is the purpose of this pull request?

This pull request:

  • fixes stats/base/dists/gamma/pdf losing most of its precision for small x when alpha > 1. gamma_p_derivative computes the density as regularised_gamma_prefix( a, x ) / x. When the prefix x^a * exp(-x) / gamma(a) falls below the smallest normal double, it carries only a few significant bits, and dividing by x scales it back into the normal range without restoring them. For example, pdf( 1.0e-215, 1.5, 1.0 ) was off by 38%, and pdf( 1.0e-161, 2.0, 1.0 ) returned 1.0375e-161 instead of 1.0e-161. The fix uses the existing log-space fallback whenever the prefix is below FLOAT64_SMALLEST_NORMAL, instead of only when it is exactly 0. This is a one-line condition change in lib/gamma_p_derivative.js and src/main.c, plus the new constant dependency in manifest.json.
  • applies the same change to the copy of gamma_p_derivative in stats/base/dists/gamma/logpdf, which had the same error (e.g., logpdf( 1.0e-215, 1.5, 1.0 ) returned -247.0817 instead of -247.4071).
  • as a result, also fixes chisquare/pdf and erlang/pdf, which delegate to gamma/pdf (e.g., chisquare/pdf( 3.0e-161, 4.0 ) was off by 7.8%).

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.

Checks done outside the test suite, against mpmath at 60 digits, on 20,000 random (x, alpha, beta) with x from 1e-320 to 10, alpha from 0.01 to 200, and beta from 1e-3 to 1e3. The figures below cover the 19,458 cases where x*beta is a normal double:

before after
gamma/pdf max relative error 0.95 4.5e-12
gamma/logpdf max relative error 5.9e-3 6.9e-14
chisquare/pdf( x, 4 ) vs. exact x/4 for x in [1e-300, 1e-150] 0.31 5.7e-14
erlang/pdf( x, 2, 1 ) vs. exact x for x in [1e-300, 1e-150] 3.8e-2 5.7e-14

JS (main and factory) and native results are identical on all of these. The remaining error of up to ~4.5e-12 comes from exp() of a log-density of several hundred in magnitude. I kept the existing fallback expression; rewriting it as (a-1)*ln(x) - x - gammaln(a) gave the same maximum error.

Two edge cases are out of scope here, as noted in the issue: inputs where x*beta is itself subnormal, and intermediate overflow of gammaDeriv( alpha, x*beta ) before it is multiplied by beta < 1 near the overflow threshold.

The new tests (main, factory, and native, for both packages) use reference values for alpha = 2, 1.5, and 1.75. For alpha = 2, the PDF is exactly beta^2 * x * exp(-beta*x). On the previous implementation, these tests are off by ~1e14 to ~1e15 ULPs for the PDF and ~1e12 ULPs for the log-PDF.

Ran locally: the gamma/pdf, gamma/logpdf, chisquare/pdf, erlang/pdf, and chi/pdf test suites (JS and native), and the commit-hook linters, all passing. The exception is one existing assertion in erlang/pdf/test/test.native.js (line 195), which fails on my machine on unmodified develop as well.

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

When `x` is small and `alpha > 1`, the regularized gamma prefix
`x^a * exp(-x) / gamma(a)` becomes subnormal, so dividing it by `x`
produced results with only a few correct bits (e.g., a 38% error for
`pdf( 1.0e-215, 1.5, 1.0 )`). Use the existing log-space fallback in
`gamma_p_derivative` whenever the prefix is below the smallest normal
double, instead of only when it is exactly zero.

Apply the same change to the copy of `gamma_p_derivative` used by
`stats/base/dists/gamma/logpdf`. This also fixes `chisquare/pdf` and
`erlang/pdf`, which delegate to `gamma/pdf`.

Resolves: stdlib-js#17052

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: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - 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 1, 2026 13:56
@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 1, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/gamma/logpdf $\\color{red}508/547$
$\\color{green}+92.87\\%$
$\\color{red}45/53$
$\\color{green}+84.91\\%$
$\\color{green}6/6$
$\\color{green}+100.00\\%$
$\\color{red}508/547$
$\\color{green}+92.87\\%$
stats/base/dists/gamma/pdf $\\color{red}501/540$
$\\color{green}+92.78\\%$
$\\color{red}45/53$
$\\color{green}+84.91\\%$
$\\color{green}6/6$
$\\color{green}+100.00\\%$
$\\color{red}501/540$
$\\color{green}+92.78\\%$

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

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/gamma/pdf and gamma/logpdf lose accuracy for small x

2 participants