Skip to content

refactor: align JS implementation and migrate hypergeometric/variance to ULP-based assertions - #14370

Merged
Planeshifter merged 3 commits into
developfrom
kgryte/ulp-hypergeometric-variance
Sep 22, 2026
Merged

Planeshifter merged 3 commits into
developfrom
kgryte/ulp-hypergeometric-variance

Conversation

@kgryte

@kgryte kgryte commented Aug 18, 2026 •

Copy link
Copy Markdown
Member

Description

What is the purpose of this pull request?

This pull request:

  • aligns the JavaScript variance formula with the C implementation, computing p = K/N; n * p * (1-p) * ((N-n)/(N-1)) instead of n * (K/N) * ((N-K)/N) * ((N-n)/(N-1)). The new association matches the Julia reference implementation that generated the test fixtures, bringing the JS ULP bound from 29 down to 1.
  • migrates the tests for stats/base/dists/hypergeometric/variance from computed relative-tolerance comparisons (delta/tol against 18.0 * EPS * abs( expected[ i ] )) to ULP-based assertions using @stdlib/assert/is-almost-same-value, per [RFC]: Migrate math/base/special packages from relative tolerance testing to ULP difference testing (tracking issue) #11352.
  • removes the now-unused @stdlib/math/base/special/abs and @stdlib/constants/float64/eps requires from both test files.

ULP bounds

Both implementations now use the same formula association, so both achieve the same tight bound:

File ULP bound
test/test.js 1
test/test.native.js 1

Related Issues

Does this pull request have any related issues?

Checklist

AI Assistance

  • Yes

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
  • Research and understanding

@stdlib-js/reviewers


🤖 Generated with Claude Code

… assertions

Replace computed relative-tolerance comparisons with `isAlmostSameValue`
ULP-based assertions in the fixture loops.

The JavaScript and C implementations evaluate the same variance formula
using different associations, so they require different bounds. The
JavaScript implementation computes `n*(K/N)*((N-K)/N)*((N-n)/(N-1))`,
whereas the C implementation computes `n*p*(1-p)*((N-n)/(N-1))`, the
latter matching the association used by the Julia reference
implementation which generated the fixtures.

Bounds are the measured minima over the full fixture set: 29 ULP for
test.js and 1 ULP for test.native.js. One lower fails in each case.

Ref: #11352
@stdlib-bot stdlib-bot added Good First PR A pull request resolving a Good First Issue. Statistics Issue or pull request related to statistical functionality. labels Aug 18, 2026
@stdlib-bot

stdlib-bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/hypergeometric/variance $\\color{green}204/204$
$\\color{green}+0.00\\%$
$\\color{green}13/13$
$\\color{green}+0.00\\%$
$\\color{green}2/2$
$\\color{green}+0.00\\%$
$\\color{green}204/204$
$\\color{green}+0.00\\%$

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

@kgryte kgryte added the Tests Pull requests specifically adding tests. label Aug 18, 2026
@kgryte
kgryte marked this pull request as ready for review August 18, 2026 10:58
@kgryte
kgryte requested a review from a team August 18, 2026 10:58
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Aug 18, 2026
@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Aug 18, 2026
@kgryte

kgryte commented Aug 18, 2026

Copy link
Copy Markdown
Member Author

We should investigate the divergence between JS and C and determine whether we should align the implementations.

Compute p=K/N, then n*p*(1-p)*((N-n)/(N-1)) to match the C
implementation and the Julia reference. This reduces the maximum
ULP deviation from 29 to 1, eliminating the JS/native divergence.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Planeshifter

Copy link
Copy Markdown
Member

/stdlib merge

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label Sep 22, 2026

@Planeshifter Planeshifter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label Sep 22, 2026
@Planeshifter Planeshifter changed the title test: migrate stats/base/dists/hypergeometric/variance to ULP-based assertions refactor: align JS implementation and migrate hypergeometric/variance to ULP-based assertions Sep 22, 2026
@Planeshifter Planeshifter removed the Needs Changes Pull request which needs changes before being merged. label Sep 22, 2026
@Planeshifter
Planeshifter merged commit ef08b9f into develop Sep 22, 2026
22 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Good First PR A pull request resolving a Good First Issue. Statistics Issue or pull request related to statistical functionality. Tests Pull requests specifically adding tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants