Conversation
`ln( 1 - (1 - x^a)^b )` cancels twice: `1 - x^a` rounds away the low-order bits of a small `x^a`, and `1 - (1 - x^a)^b` loses the digits of a small CDF, as `ln` of a value close to `1` does for a CDF close to `1`. Evaluate `ln(1-x^a)` with `log1p` or `expm1`, depending on the size of `x^a`, and finish with `log1mexp`. Add fixtures generated from the textbook formula in extended precision.
5 of 7 tasks
Contributor
Coverage Report
The above coverage report was generated for the changes in this PR. |
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.


Description
This pull request:
stats/base/dists/kumaraswamy/logcdf(JS, factory and C),TODOfor.The function computed
ln( 1 - pow( 1 - pow( x, a ), b ) ):x^a,1 - x^arounds away its low-order bits, and1 - (1 - x^a)^bcancels. Forx^a < 2^-53the result is-Infinity.xclose to1,1 - x^acancels (x^awas already rounded), andlnof a value close to1loses the digits of a small result. It returns0once(1 - x^a)^b < 2^-53.The function now computes
v = ln(1 - x^a)aslog1p( -x^a )whenx^a < 0.5, and asln( -expm1( a*ln(x) ) )otherwise, so1 - x^ais never formed. It then returnslog1mexp( b*v ).Tests:
There's a new
test/fixtures/julia/runner.jlthat evaluates the textbook formulaln(1 - (1 - x^a)^b)in 2048-bitBigFloat. It writes three fixtures:data.json: randomx, withaandbin[0.5, 5.5],small_x.json:xlog-spaced over[1e-100, 0.1], witha <= 3so thatx^adoesn't underflow,large_x.json:1 - xlog-spaced over[1e-15, 0.1].The "evaluates the logcdf" tests only checked that the result is a number (
// TODO: Add fixtures). They are replaced with tests against the fixtures.Max error, JS and native, which sets the tolerances:
developdatasmall_x-Infinitylarge_x0The remaining error comes from rounding in
b * v. A 1-ULP change inbmoves the exact result by|b ln(1 - x^a)|ULP, which reaches ~150 at the far end oflarge_x. So this is about as close as double precision gets without extended arithmetic. Ondevelop, 2,140 of the 3,027 assertions intest.logcdf.jsfail. With this change they all pass.Related Issues
None. Companion to #15767 (
kumaraswamy/cdf).Questions
When
x^aunderflows (e.g.x = 1e-100,a = 5), the result is still-Infinity, as ondevelop. It could returnln(b) + a*ln(x)there. I left that out to keep this PR to the cancellation, but I'm happy to add it.Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
This PR was written primarily by Claude Code. I found the bug by reading the
stats/base/distsfunctions for1 - ucancellation.@stdlib-js/reviewers