Skip to content

fix: avoid cancellation near the edges of the support in stats/base/dists/cosine/logpdf - #15874

Open
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/cosine-logpdf-edges
Open

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/cosine-logpdf-edges

Conversation

@Abhist17

@Abhist17 Abhist17 commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • evaluates stats/base/dists/cosine/logpdf as 2*ln( cospi(z/2) ) - ln(s) instead of ln( 1 + cospi(z) ) - ln(2s) (JS, factory and C),
  • fixes the test fixtures, one of which tested nothing.

1 + cos(pi*z) cancels as z = (x-mu)/s approaches -1 or 1. Close to the edges of the support the result is -Infinity, although the log-density is finite there:

var logpdf = require( '@stdlib/stats/base/dists/cosine/logpdf' );

logpdf( 1.0-1.0e-9, 0.0, 1.0 );
// develop: -Infinity
// this PR: -40.54336631987778

logpdf( 1.0-1.0e-5, 0.0, 1.0 );
// develop: -22.122685445187134
// this PR: -22.122685519452897

1 + cos(pi*z) = 2*cos(pi*z/2)^2 holds exactly. On [-1, 1], cospi(z/2) is non-negative, so the log needs no subtraction.

Fixtures and tests:

  • runner.jl now evaluates the textbook formula ln((1 + cos(pi*z)) / (2s)) in 2048-bit BigFloat instead of Distributions.logpdf.

  • Its x values were drawn from ranges that mostly missed the support. positive_mean.json had 1,000 of 1,000 entries null (x in [-20, -10] with mu in [0, 10]), so its test checked nothing, and negative_mean.json had 964 of 1,000. All three now draw x inside the support.

  • There's a new edges.json with 1 - |z| log-spaced over [1e-15, 0.1] on both sides. mu = 0 and s is a power of two, so z is exact.

  • Max error in JS and native, against the exact values:

    fixture develop this PR
    positive_mean 22,613 ULP 1,062 ULP
    negative_mean 11,063 ULP 1,530 ULP
    large_variance 23,776 ULP 261 ULP
    edges -Infinity 1 ULP

    The tolerances are set to those values. The remaining error is at results close to 0 (e.g. -0.0013). There, a 1-ULP change in s moves the exact result by about 1/|result| ULP, so that's conditioning. On develop, 986 of the 4,022 assertions in test.logpdf.js fail. With this change they all pass.

Related Issues

Does this pull request have any related issues?

None.

Questions

Any questions for reviewers of this pull request?

cosine/pdf has the same 1 + cospi(z) (it returns 0 near the edges); that one is #15875. cosine/cdf and logcdf cancel too: 1 + z + sinpi(z)/pi near z = -1 is about pi^2 (1+z)^3 / 6, but fixing that needs a series for u - sin(u), so I'd do it separately.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

This PR was written primarily by Claude Code. I found the bug by scanning stats/base/dists for ln( 1.0 + ... ).


@stdlib-js/reviewers

…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.
@Abhist17
Abhist17 requested a review from a team October 3, 2026 20:05
@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Oct 3, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/cosine/logpdf $\\color{green}318/318$
$\\color{green}+100.00\\%$
$\\color{green}31/31$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}318/318$
$\\color{green}+100.00\\%$

The above coverage report was generated for the changes in this PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review A pull request which needs code review. Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants