Repository navigation
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
Conversation
…ted-normal pdf when both endpoints lie in the same tail
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:
This acknowledgment confirms that you've read the guidelines, which include:
We can't review or accept contributions without this acknowledgment. Thank you for your understanding and cooperation. We look forward to reviewing your contribution! |
Contributor
Coverage Report
The above coverage report was generated for the changes in this PR. |
…rs order, mixed operators)
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #17149.
Description
This pull request:
stats/base/dists/truncated-normal/pdfto return accurate values when both endpointsaandblie in the same tail of the underlying normal distribution (previouslyInfinity,NaN, or values off by up to ~6.6% due to cancellation inC = Phi(zb) - Phi(za)and underflow ofexp(-z^2/2)).mu, the normalizer is now evaluated via the survival function written witherfcx: withta <= tbthe standardized endpoint distances frommu, both the numerator andC = Q(ta) - Q(tb)are divided byexp(-ta^2/2), yieldingexp(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 whena < mu < b.lib/main.js,lib/factory.js, andsrc/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
This pull request has the following related issues:
Reference Packages
This pull request followed these packages:
math/base/special/erfcx(used for the stable tail evaluation)erfcxfor evaluating normal tail probabilities without cancellation, in the same spirit as its use for scaled complementary error function evaluations elsewhere in stdlib.exp(-ta^2/2)to avoid underflow.Questions
No.
Verification:
npx tape test/test.js test/test.pdf.jsinlib/node_modules/@stdlib/stats/base/dists/truncated-normal/pdf: 1027/1027 pass (including the 1000 fixture cases and the new tail cases).src/main.ccompiles cleanly (gcc -fsyntax-onlywith 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.0.000278065632930861,40.02496884720726,0.00005561312658617219,0.0016524415441719568,0.009258904116942952, and0.37700665594056254, matching the expected results.AI Assistance:
The fix was implemented with AI assistance (GLM via the pi coding agent) following the approach proposed in issue [BUG]: stats/base/dists/truncated-normal/pdf returns Infinity, NaN, or inaccurate values when both endpoints lie in the upper tail #17149; the implementation, tests, and verification steps above were carried out and checked locally by the contributor (wanjinhao1).
Read, understood, and followed the contributing guidelines