Repository navigation
Conversation
Evaluates `t = alpha * ln(x/beta)` accurately and computes the CDF as `-expm1( -t )` and the log-CDF as `ln( -expm1( -t ) )` for `t <= ln(2)`, avoiding cancellation when `(beta/x)^alpha` is close to `1`, and handles underflow of `beta/x`. Resolves: stdlib-js#17147 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. |
… `x` near `beta` - returns `0` (`cdf`) and `-Infinity` (`logcdf`) for `x = beta`, even when `alpha` is `+Infinity`, instead of `NaN` - uses `pow( beta/x, alpha )` in `logcdf` only for `x >= 2*beta`, and `log1mexp( alpha*ln(x/beta) )` otherwise, as `pow` amplifies the rounding error of `beta/x` by `alpha` Ref: stdlib-js#17147 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 #17147.
Description
This pull request:
stats/base/dists/pareto-type1/cdfandstats/base/dists/pareto-type1/logcdf(lib/main.js,lib/factory.js, andsrc/main.cin each) losing accuracy when(beta/x)^alphais close to1, and returning0/-Infinitywhen it rounds to1(e.g.,cdf( 1.0e300, 1.0e-20, 1.0 )returned0instead of6.908e-18). It also fixes the case wherebeta/xunderflows (e.g.,cdf( 1.0e300, 0.001, 1.0e-300 )returned1instead of0.7488).ln_ratio(lib/ln_ratio.js, and a static function insrc/main.c) in each package, which evaluatesln(x/beta)accurately:log1p( (x-beta)/beta )forx < 2*beta, wherex-betais exact,ln(x) - ln(beta)ifx/betaoverflows, andln(x/beta)otherwise.-expm1( -t ), wheret = alpha * ln(x/beta).log1mexp( t )(i.e.,ln( 1 - exp(-t) )without cancellation), except whenx >= 2*beta,t > ln(2), andbeta/xis not subnormal, where it keepslog1p( -pow( beta/x, alpha ) ). There,powis more accurate, as the rounding error ofbeta/xis amplified byalpha, while that oftis amplified byt >= alpha*ln(2)(usinglog1mexp( t )everywhere gives up to 89 vs. 15 ULP on thelarge_alphafixtures). Conversely, forxclose tobetaand largealpha,powis much less accurate (e.g.,logcdf( 1.0000000001, 1.0e12, 1.0 )is off by about 7.5e7 ULP withpow, and by 83 ULP withlog1mexp).0(cdf) and-Infinity(logcdf) forx = betaexplicitly, asalpha * ln(x/beta)isNaNforalpha = +Infinity.Related Issues
This pull request has the following related issues:
Questions
The existing Julia fixtures match the previous implementation, and are themselves off by up to about 1030 ULP (
cdf) and 107 ULP (logcdf) from exact values (computed with Python'sdecimalmodule at 320 digits). As Julia isn't available to me, I kept the fixtures and set each fixture test's tolerance to the largest difference between the new implementation and the fixture, with a comment explaining why, and added tests against exact values. Regenerating the fixtures with the same Julia script would likely reproduce the cancellation, so new fixtures would need high-precision (e.g.,BigFloat) reference values. Would you prefer that I do that instead?Other
Maximum error in ULP relative to exact values, on the existing fixture inputs (1,000 cases each):
cdfboth_largecdflarge_alphacdflarge_betalogcdfboth_largelogcdflarge_alphalogcdflarge_betaOn the 8 new test cases (the cases from #17147, the underflow case, and a regular case), the new implementation returns the correctly rounded exact values in both JS and C, while the previous one returned
0,1,-Infinity, or values with relative errors up to 9.5%. For largealphaandxclose tobeta,logcdfis within 83 ULP of the exact values on the added test cases (the result is ill-conditioned with respect toalpha; the previous implementation was off by up to 8.2e8 ULP). The JS and C implementations agree to within 1 ULP on all fixture inputs.x = beta(0/-Infinity, now also foralpha = +Infinity),x < beta,x = Infinity(1/-0), andNaNhandling are unchanged.Ran locally: the JS, factory, and native tests of both packages (native addons 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