Repository navigation
Conversation
…rmal/pdf` When both endpoints lie on the same side of `mu`, evaluates the normalizing constant relative to the density at the endpoint closest to `mu` using `erfcx`, avoiding the cancellation of `Phi(zb) - Phi(za)` and the underflow of `exp(-z^2/2)`. Resolves: stdlib-js#17149 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: na - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
Contributor
Coverage Report
The above coverage report was generated for the changes in this PR. |
…dard deviations from `mu`
For intervals closer to `mu`, the difference of the erfcx terms is
less accurate than `Phi(zb) - Phi(za)` (e.g., up to 30 times larger
errors for `[0, 1e-8]` and `[0.3, 0.4]`). The crossover is near
`1.8` standard deviations. Also adds tests for intervals near `mu`
and a test file for the `factory` method.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown_pkg_readmes
status: na
- task: lint_markdown_docs
status: na
- task: lint_markdown
status: na
- task: lint_package_json
status: na
- task: lint_repl_help
status: na
- task: lint_javascript_src
status: passed
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: na
- task: lint_javascript_tests
status: passed
- task: lint_javascript_benchmarks
status: na
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: missing_dependencies
- task: lint_c_examples
status: na
- task: lint_c_benchmarks
status: na
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: na
- task: lint_license_headers
status: passed
---
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:
fixes
stats/base/dists/truncated-normal/pdf(lib/main.js,lib/factory.js, andsrc/main.c) returningInfinity,NaN, or inaccurate values when both endpoints lie in the upper tail (e.g.,pdf( 11.0, 10.0, 12.0, 0.0, 1.0 )returnedInfinityinstead of2.7807e-4), andNaNin either tail whenexp(-z^2/2)underflows.when both endpoints lie on the same side of
mu, at least two standard deviations away from it (a - mu >= 2*sigmaormu - b >= 2*sigma), letscbe the endpoint closest tomuandothe other one, and computeswhich follows from
Q(t) = 1 - Phi(t) = 0.5 * exp(-t^2/2) * erfcx(t/sqrt(2))(andPhi(-t) = Q(t)) after dividing both the numerator and the normalizing constant byexp(-zc^2/2). Differences of squares such aszc^2 - z^2are computed from the inputs, as((c-x)/sigma) * ((c-mu)+(x-mu))/sigma, rather than from the standardized values, which keeps the error in the lower tail at the level of the previous implementation.keeps the previous formula otherwise. Closer to
mu, the previous formula is more accurate (see below for how the threshold was chosen).Related Issues
This pull request has the following related issues:
Questions
While writing the C implementation, I found that
STDLIB_CONSTANT_FLOAT64_SQRT_HALFinconstants/float64/sqrt-half/include/stdlib/constants/float64/sqrt_half.his defined as7.071067811865475244(missinge-01), so it is ten times too large. I therefore usedx / STDLIB_CONSTANT_FLOAT64_SQRT2(andx / SQRT_TWOin JavaScript, to keep both implementations identical) instead of multiplying bySQRT_HALF. As far as I can tell, no C code uses this macro (therayleighpackages useSQRT_HALF_PI, which is correct). Should I open a separate issue for it?Other
New tests (JS, native, and a new
test.factory.js) use 9 cases in both tails, including infinite endpoints. Reference values were computed with Python'sdecimalmodule at 130 digits fromphi(z) / ( sigma * ( Q(za) - Q(zb) ) ), witherfcevaluated from its power series and continued fraction (checked againstmath.erfc). The new implementation is within 3 ULP of all of them in both JS and C (tolerance 5 ULP); the previous one fails all 9 (from 17 ULP toInfinity/NaN). A second test uses 9 intervals close tomu(including intervals ending atmuand intervals just below and above the threshold), where all implementations are within 12 ULP (tolerance 15 ULP; the narrow interval[0.3, 0.4]is ill-conditioned).On 300 random cases (endpoints up to 15 standard deviations from
mu, including narrow intervals), relative to the same references:Infinity/NaN(The column "errors > 1e-12" counts finite results only.)
To choose the threshold, I compared both formulas on 578 intervals with the nearest endpoint 0 to 3 standard deviations from
muand widths from1e-8to10standard deviations. Theerfcxform is more accurate from about 1.8 standard deviations upward, but up to about 30 times less accurate closer tomu(e.g.,[0, 1e-8],[0.3, 0.4]), so it is used only from 2 standard deviations. For very narrow intervals, both formulas lose digits to cancellation in the normalizing constant, roughly in proportion to1/(b-a); this pull request does not change that. On the 300 random cases, the new implementation is worse than the previous one by at most 2.9e-15 relative error, and the JS and C implementations agree to within 2 ULP.Ran locally: the JS and native tests (native addon built), all passing, plus the JS linter and
cppcheck.Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
@stdlib-js/reviewers