Skip to content

fix: resolve log-density underflow in stats/base/dists/gamma/logpdf - #17022

Open
wanjinhao1 wants to merge 3 commits into
stdlib-js:developfrom
wanjinhao1:agent/fix-17004-gamma-logpdf-underflow
Open

wanjinhao1 wants to merge 3 commits into
stdlib-js:developfrom
wanjinhao1:agent/fix-17004-gamma-logpdf-underflow

Conversation

@wanjinhao1

@wanjinhao1 wanjinhao1 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #17004.

Description

What is the purpose of this pull request?

This pull request:

  • Fixes stats/base/dists/gamma/logpdf returning -Infinity when the PDF underflows to 0 (or loses digits when subnormal), by evaluating the log-density directly in log space when gamma_p_derivative( alpha, x*beta ) < FLOAT64_SMALLEST_NORMAL: (alpha-1)*ln(x*beta) - x*beta - gammaln(alpha) + ln(beta). All currently accurate results are unchanged (the fallback only triggers for subnormal densities).
  • Applies the same guard in lib/main.js, lib/factory.js, and the native src/main.c (with @stdlib/constants/float64/smallest-normal added to the add-on manifest.json dependencies).
  • erlang/logpdf and chisquare/logpdf call gamma/logpdf, so they inherit the fix; a regression test is added for erlang as well.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

Reference Packages

Did the content of this pull request follow one or more existing packages? Answering is optional.

This pull request followed these packages:

  • math/base/special/cinv (for the smallest-normal constant include/manifest convention)

What carried over from the reference(s)?

  • Include path and manifest.json dependency naming for @stdlib/constants/float64/smallest-normal.

Validation

  • node test/test.js, node test/test.logpdf.js (3036 passed), node test/test.factory.js (3040 passed) in gamma/logpdf; test.native.js skips locally (no built add-on) but gcc -fsyntax-only on src/main.c passes and the C change mirrors the JS logic.
  • erlang/logpdf tests: 3021 passed; chisquare/logpdf tests: 5115 passed.
  • Spot-checked the issue's failing domain against a math.lgamma-based reference: e.g. logpdf( 1000, 10, 1 ) = -950.632…, logpdf( 755, 10, 1 ) = -708.161… (subnormal density case) — finite and accurate instead of -Infinity.

AI Assistance

Portions of this pull request were authored with the assistance of an AI coding agent (GLM, trained by Z.ai); the approach follows the fix suggested in issue #17004, and all code and tests were reviewed and verified locally by the contributor.

…nderflows

Signed-off-by: wanjinhao1 <394959433@qq.com>
@wanjinhao1
wanjinhao1 requested a review from a team November 30, 2026 22:13
@stdlib-bot stdlib-bot added the Statistics Issue or pull request related to statistical functionality. label Nov 30, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Hello! Thank you for your contribution to stdlib.

We noticed that the contributing guidelines acknowledgment is missing from your pull request. Here's what you need to do:

  1. Please read our contributing guidelines.

  2. Update your pull request description to include this checked box:

    - [x] Read, understood, and followed the [contributing guidelines](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md)

This acknowledgment confirms that you've read the guidelines, which include:

  • The developer's certificate of origin
  • Your agreement to license your contributions under the project's terms

We can't review or accept contributions without this acknowledgment.

Thank you for your understanding and cooperation. We look forward to reviewing your contribution!

@stdlib-bot stdlib-bot added Needs Review A pull request which needs code review. and removed Needs Review A pull request which needs code review. labels Nov 30, 2026
@wanjinhao1 wanjinhao1 changed the title Fix stats/base/dists/gamma/logpdf to return finite log-density when PDF underflows fix: resolve log-density underflow in stats/base/dists/gamma/logpdf Nov 30, 2026
@stdlib-bot

stdlib-bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/erlang/logpdf $\\color{green}290/290$
$\\color{green}+0.00\\%$
$\\color{green}11/11$
$\\color{green}+0.00\\%$
$\\color{green}3/3$
$\\color{green}+0.00\\%$
$\\color{green}290/290$
$\\color{green}+0.00\\%$
stats/base/dists/gamma/logpdf $\\color{red}529/568$
$\\color{green}+0.00\\%$
$\\color{red}49/57$
$\\color{green}+0.00\\%$
$\\color{green}6/6$
$\\color{green}+0.00\\%$
$\\color{red}529/568$
$\\color{green}+0.00\\%$

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

Signed-off-by: wanjinhao1 <394959433@qq.com>
Signed-off-by: wanjinhao1 <394959433@qq.com>

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/logpdf returns -Infinity when the PDF underflows

2 participants