Skip to content

fix: return ln(sqrt(2/pi)) instead of NaN for x = 0 and k = 1 in stats/base/dists/chi/logpdf - #15604

Open
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/chi-logpdf-zero
Open

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/chi-logpdf-zero

Conversation

@Abhist17

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • fixes stats/base/dists/chi/logpdf (JS, factory, and C) returning NaN at x = 0 when k = 1.
var pdf = require( '@stdlib/stats/base/dists/chi/pdf' );
var logpdf = require( '@stdlib/stats/base/dists/chi/logpdf' );

pdf( 0.0, 1.0 );
// returns ~0.798 (= sqrt(2/pi), the half-normal density at 0)

logpdf( 0.0, 1.0 );
// returns NaN, expected ~-0.226

The log density is (1-k/2) ln 2 + (k-1) ln(x) - x^2/2 - ln Gamma(k/2). With k = 1 and x = 0, the middle term is 0 * -Infinity = NaN, but x^(k-1) = x^0 is 1 there. The fix skips (k-1) ln(x) when k = 1. For other k, x = 0 still gives +Infinity (k < 1) or -Infinity (k > 1), as it did before. I kept the order of the remaining additions as it was, so every other result is unchanged bit for bit. Rearranging the sum shifts rounding enough to fail 388 of the existing fixture checks.

Related Issues

Does this pull request have any related issues?

No.

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.

  • Added a test for x = 0 with k = 1, k < 1 and k > 1 in test.logpdf.js, test.factory.js and test.native.js. The k = 1 assertion fails on develop, and all tests pass here.
  • Found with a sweep comparing every stats/base/dists function against SciPy at the support boundaries. Some other densities also return 0 at x = 0 where the density is finite or infinite: weibull for k <= 1, kumaraswamy for a <= 1, betaprime for alpha <= 1, and log-logistic for beta <= 1. gamma, beta, chisquare and f already handle this case. I'll send those as separate PRs so each can be reviewed on its own.

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. The bug was found by comparing every stats/base/dists function against SciPy on random parameters.


@stdlib-js/reviewers

…in `stats/base/dists/chi/logpdf`

`(k-1) * ln(x)` is `0 * -Infinity = NaN` at `x = 0` when `k = 1`, while
`chi/pdf` correctly returns `sqrt(2/π)` there. Skip the term when `k = 1`,
keeping the order of the remaining operations so other results are
unchanged.
@Abhist17
Abhist17 requested a review from a team September 27, 2026 23:00
@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Sep 27, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/chi/logpdf $\\color{green}319/319$
$\\color{green}+100.00\\%$
$\\color{green}33/33$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}319/319$
$\\color{green}+100.00\\%$

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

@kgryte

kgryte commented Sep 28, 2026

Copy link
Copy Markdown
Member

@Planeshifter Mind taking a look at 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.

3 participants