Skip to content

fix: return +Infinity at x = 0 for k < 1 in stats/base/dists/weibull/pdf and logpdf - #15605

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

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/weibull-pdf-zero

Conversation

@Abhist17

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • fixes stats/base/dists/weibull/pdf and stats/base/dists/weibull/logpdf returning 0 and -Infinity at x = 0 when the shape k < 1. The density (k/lambda) (x/lambda)^(k-1) exp(-(x/lambda)^k) grows without bound as x -> 0+ in that case.
var pdf = require( '@stdlib/stats/base/dists/weibull/pdf' );

pdf( 0.0, 0.5, 2.0 );
// JavaScript returns 0 and the native addon returns Infinity

The C implementation of pdf has no special case for x = 0 and evaluates the formula, where pow( 0.0, k-1.0 ) is +Infinity. The JavaScript implementation special-cases x = 0 as k === 1 ? k/lambda : 0, which is only right for k >= 1. So the two backends currently disagree. R (dweibull( 0, 0.5, 2 )), SciPy (weibull_min.pdf( 0, 0.5, scale=2 )) and Julia (pdf( Weibull( 0.5, 2 ), 0 )) all return Inf. Within stdlib, gamma, beta, chisquare and f already return +Infinity for the same boundary case.

Changes:

  • pdf/lib/main.js, pdf/lib/factory.js: return PINF at x = 0 for k < 1.
  • logpdf/lib/main.js, logpdf/lib/factory.js, logpdf/src/main.c: return PINF at x = 0 for k < 1, where it used to be NINF. The C logpdf had the same special case as the JavaScript.
  • Tests: the existing "k not equal to one returns 0" cases (which use k = 2) are renamed to "greater than one", and there is a new k < 1 case in each of the six test files.

The k = 1 (1/lambda) and k > 1 (0) results are unchanged.

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.

Not changed here: in pdf/src/main.c, the x == +/-Infinity branch returns ( k == 0.0 ) ? k/lambda : 0.0. The k == 0.0 arm can't be reached, since k <= 0 has already returned NaN, so the branch always returns 0, which is the correct result.

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

…eibull/pdf` and `logpdf`

For shape `k < 1` the Weibull density grows without bound as `x -> 0+`,
but the JavaScript `pdf` returned `0` and `logpdf` returned `-Infinity`
at `x = 0`. The C implementation of `pdf` already returned `+Infinity`,
so the JavaScript and native results disagreed.
@Abhist17
Abhist17 requested a review from a team September 27, 2026 23:02
@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/weibull/logpdf $\\color{green}316/316$
$\\color{green}+100.00\\%$
$\\color{green}39/39$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}316/316$
$\\color{green}+100.00\\%$
stats/base/dists/weibull/pdf $\\color{green}318/318$
$\\color{green}+100.00\\%$
$\\color{green}40/40$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}318/318$
$\\color{green}+100.00\\%$

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.

2 participants