Skip to content

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

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

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/cosine-pdf-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/pdf as cospi(z/2)^2 / s instead of ( 1 + cospi(z) ) / (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 density is 0, although it is positive there:

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

pdf( 1.0-1.0e-9, 0.0, 1.0 );
// develop: 0.0
// this PR: 2.467400960706604e-18

pdf( 1.0-1.0e-5, 0.0, 1.0 );
// develop: 2.4674012832903713e-10
// this PR: 2.4674011000469455e-10

1 + cos(pi*z) = 2*cos(pi*z/2)^2 holds exactly, so this form has no subtraction.

Fixtures and tests (the same changes as #15874 for logpdf):

  • runner.jl evaluates (1 + cos(pi*z)) / (2s) in 2048-bit BigFloat instead of Distributions.pdf.

  • x is now drawn inside the support. positive_mean.json had all 1,000 points outside the support, so its test checked nothing, and negative_mean.json had 968 of 1,000.

  • There's a new edges.json with 1 - |z| log-spaced over [1e-15, 0.1] (mu = 0, s a power of two).

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

    fixture develop this PR
    positive_mean 50,539 ULP 255 ULP
    negative_mean 245,026 ULP 1,742 ULP
    large_variance 578,308 ULP 370 ULP
    edges 0 3 ULP

    The tolerances are set to those values. The remaining error comes from rounding z = (x-mu)/s close to an edge, where the density is very sensitive to z. On develop, 1,016 of the 4,022 assertions in test.pdf.js fail. With this change they all pass.

Related Issues

Does this pull request have any related issues?

None. Companion to #15874 (cosine/logpdf).

Questions

Any questions for reviewers of this pull request?

No.

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 1.0 + cos....


@stdlib-js/reviewers

…dists/cosine/pdf`

`1 + cos(pi*z)` cancels as `z` approaches -1 or 1, and the density was
`0` there although it is positive. 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:06
@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/pdf $\\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