Repository navigation
Conversation
…lambda` The series `sum_k lambda^k ln(k!)/k!` overflowed once `lambda^k` or `k!` reached `Infinity`, so the function returned `Infinity` for `lambda >= ~80` and `NaN` for `lambda >= ~745`. Evaluate `-sum p_k ln(p_k)` with weights relative to the mode instead, and use the asymptotic expansion for `lambda >= 5000`. Regenerate the fixtures in extended precision and add fixtures for medium and large `lambda`.
Contributor
Coverage Report
The above coverage report was generated for the changes in this PR. |
Member
|
@Planeshifter Mind taking a look at this PR? |
Move the explanation of the evaluation method out of the function body, as requested in review of stdlib-js#15611.
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/poisson/entropy(JS and C) returningInfinityforlambda >= ~80andNaNforlambda >= ~745.The implementation evaluates
lambda*(1-ln(lambda)) + exp(-lambda) * sum_k lambda^k ln(k!)/k!. Bothlambda^kandk!overflow long before the series converges oncelambdais more than a few dozen, so the terms becomeInfinityand thenInfinity/Infinity. The existing fixtures only coverlambdain[0, 20], which is why the tests never saw it. (Julia'sentropy( Poisson( lambda ) ), which generated them, switches to an asymptotic expansion atlambda >= 50for the same reason.)The fix evaluates
H = -sum_k p_k ln(p_k)directly, with weights relative to the mode,w_k = p_k/p_m:ln(w_k)comes from the recurrencew_{k+1} = w_k * lambda/(k+1)(and downward,w_{k-1} = w_k * k/lambda), so nothing overflows and there is no cancellation betweenk*ln(lambda),lambdaandln(k!), each of which is far larger than the entropy.p_ksum to one,p_m = 1/WwithW = sum w_k, soH = ln(W) - sum w_k ln(w_k) / W. Nogammalnis needed.ln(lambda/(k+1))near the mode goes throughlog1p, where the subtraction is exact (the two operands are within a factor of two).lambda >= 5000, it uses the asymptotic expansionln(2*pi*e*lambda)/2 - 1/(12 lambda) - 1/(24 lambda^2) - 19/(360 lambda^3), whose truncation error there is below1e-16relative. That keeps the loop to at most about 1,300 iterations.Accuracy against a 256-bit reference: at most 3 ULP across
lambdain[0.005, 5e5], compared with up to 76 ULP for the old series on[0, 20], where it did work.Related Issues
No.
Questions
No.
Other
runner.jlnow computes-sum p_k ln(p_k)inBigFloat(256 bits) overlambda ± 40 sd, and no longer usesDistributions.entropy. Its double-precision series was itself up to 76 ULP off on the existingdata.json, and it becomes an asymptotic approximation atlambda >= 50.data.jsonis regenerated, andmedium.json(lambdain[20, 5000]) andlarge.json(lambdain[5e3, 5e5]) are new. The tolerance is tightened from 20 to 4 ULP, and the worst case is 3.test/test.jsfail. With this change, all pass in bothtest.jsandtest.native.js, and thepoisson/ctortests also pass.entropy( Infinity )now returnsInfinity(the expansion's limit) where it used to returnNaN.Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
This PR was written primarily by Claude Code. The bug was found by comparing every
stats/base/distsfunction against SciPy and mpmath on random parameters.@stdlib-js/reviewers