Skip to content

fix: resolve Infinity, NaN, and inaccurate values in truncated-normal pdf when both endpoints lie in the same tail - #17155

Open
wanjinhao1 wants to merge 6 commits into
stdlib-js:developfrom
wanjinhao1:agent/fix-17149-truncnorm-pdf
Open

wanjinhao1 wants to merge 6 commits into
stdlib-js:developfrom
wanjinhao1:agent/fix-17149-truncnorm-pdf

Conversation

@wanjinhao1

@wanjinhao1 wanjinhao1 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #17149.

Description

What is the purpose of this pull request?

This pull request:

  • Fixes stats/base/dists/truncated-normal/pdf to return accurate values when both endpoints a and b lie in the same tail of the underlying normal distribution (previously Infinity, NaN, or values off by up to ~6.6% due to cancellation in C = Phi(zb) - Phi(za) and underflow of exp(-z^2/2)).
  • When both endpoints are on the same side of mu, the normalizer is now evaluated via the survival function written with erfcx: with ta <= tb the standardized endpoint distances from mu, both the numerator and C = Q(ta) - Q(tb) are divided by exp(-ta^2/2), yielding exp(0.5*(ta^2 - z^2)) / (0.5 * (erfcx(ta/sqrt(2)) - erfcx(tb/sqrt(2)) * exp(0.5*(ta^2 - tb^2)))). The lower tail is handled by mirroring ((mu - x), etc.). The existing formula is kept when a < mu < b.
  • The fix is applied consistently in lib/main.js, lib/factory.js, and src/main.c, and new tape tests cover the upper-tail and lower-tail cases from the issue (all match the high-precision reference values to within ~1e-15 relative).

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/erfcx (used for the stable tail evaluation)

What carried over from the reference(s)?

  • Use of erfcx for evaluating normal tail probabilities without cancellation, in the same spirit as its use for scaled complementary error function evaluations elsewhere in stdlib.

What differed, or what did you have to work out yourself?

  • The mirrored-variable formulation for the lower tail and the rescaling of both the numerator and normalizer by exp(-ta^2/2) to avoid underflow.

Questions

Any questions for reviewers of this pull request?

No.


Verification:

  • npx tape test/test.js test/test.pdf.js in lib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf: 1027/1027 pass (including the 1000 fixture cases and the new tail cases).
  • src/main.c compiles cleanly (gcc -fsyntax-only with the package include paths). The native addon test file is updated alongside the JavaScript tests, but the native addon was not built locally, so the native tests were not executed in this environment.
  • The six reproduction cases from the issue now return 0.000278065632930861, 40.02496884720726, 0.00005561312658617219, 0.0016524415441719568, 0.009258904116942952, and 0.37700665594056254, matching the expected results.

AI Assistance:

…ted-normal pdf when both endpoints lie in the same tail
@wanjinhao1
wanjinhao1 requested a review from a team December 4, 2026 16:07
@stdlib-bot stdlib-bot added the Statistics Issue or pull request related to statistical functionality. label Dec 4, 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 the Needs Review A pull request which needs code review. label 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/truncated-normal/pdf $\\color{red}333/396$
$\\color{red}-0.10\\%$
$\\color{green}20/20$
$\\color{green}+0.00\\%$
$\\color{red}3/4$
$\\color{green}+0.00\\%$
$\\color{red}333/396$
$\\color{red}-0.10\\%$

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/truncated-normal/pdf returns Infinity, NaN, or inaccurate values when both endpoints lie in the upper tail

2 participants