Conversation
…dists/cosine/logpdf` `1 + cos(pi*z)` cancels as `z` approaches -1 or 1, and the log-density was `-Infinity` there although it is finite. Use `1 + cos(pi*z) = 2*cos(pi*z/2)^2`. The fixtures are now generated from the textbook formula in extended precision, with `x` inside the support: `positive_mean.json` held only out-of-support points, so its test checked nothing.
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/cosine/logpdfas2*ln( cospi(z/2) ) - ln(s)instead ofln( 1 + cospi(z) ) - ln(2s)(JS, factory and C),1 + cos(pi*z)cancels asz = (x-mu)/sapproaches-1or1. Close to the edges of the support the result is-Infinity, although the log-density is finite there:1 + cos(pi*z) = 2*cos(pi*z/2)^2holds exactly. On[-1, 1],cospi(z/2)is non-negative, so the log needs no subtraction.Fixtures and tests:
runner.jlnow evaluates the textbook formulaln((1 + cos(pi*z)) / (2s))in 2048-bitBigFloatinstead ofDistributions.logpdf.Its
xvalues were drawn from ranges that mostly missed the support.positive_mean.jsonhad 1,000 of 1,000 entriesnull(xin[-20, -10]withmuin[0, 10]), so its test checked nothing, andnegative_mean.jsonhad 964 of 1,000. All three now drawxinside the support.There's a new
edges.jsonwith1 - |z|log-spaced over[1e-15, 0.1]on both sides.mu = 0andsis a power of two, sozis exact.Max error in JS and native, against the exact values:
developpositive_meannegative_meanlarge_varianceedges-InfinityThe tolerances are set to those values. The remaining error is at results close to
0(e.g.-0.0013). There, a 1-ULP change insmoves the exact result by about1/|result|ULP, so that's conditioning. Ondevelop, 986 of the 4,022 assertions intest.logpdf.jsfail. With this change they all pass.Related Issues
None.
Questions
cosine/pdfhas the same1 + cospi(z)(it returns0near the edges); that one is #15875.cosine/cdfandlogcdfcancel too:1 + z + sinpi(z)/pinearz = -1is aboutpi^2 (1+z)^3 / 6, but fixing that needs a series foru - sin(u), so I'd do it separately.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 scanning
stats/base/distsforln( 1.0 + ... ).@stdlib-js/reviewers