From 781389663cb9740782e04b031df74fe30bbb525f Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 18 Aug 2026 06:21:37 +0000 Subject: [PATCH 1/2] test: migrate `stats/base/dists/hypergeometric/variance` to ULP-based 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: https://github.com/stdlib-js/stdlib/issues/11352 --- .../base/dists/hypergeometric/variance/test/test.js | 13 ++----------- .../hypergeometric/variance/test/test.native.js | 13 ++----------- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.js index bfdf1fe386ba..8ff9c0b139ad 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.js @@ -21,10 +21,9 @@ // MODULES // var tape = require( 'tape' ); +var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var abs = require( '@stdlib/math/base/special/abs' ); var PINF = require( '@stdlib/constants/float64/pinf' ); -var EPS = require( '@stdlib/constants/float64/eps' ); var variance = require( './../lib' ); @@ -119,8 +118,6 @@ tape( 'if provided an `n` which is not a nonnegative integer, the function retur tape( 'the function returns the variance of a hypergeometric distribution', function test( t ) { var expected; - var delta; - var tol; var N; var K; var n; @@ -133,13 +130,7 @@ tape( 'the function returns the variance of a hypergeometric distribution', func n = data.n; for ( i = 0; i < n.length; i++ ) { y = variance( N[i], K[i], n[i] ); - if ( y === expected[i] ) { - t.strictEqual( y, expected[i], 'N: '+N[i]+', K: '+K[i]+', n: '+n[i]+', y: '+y+', expected: '+expected[i] ); - } else { - delta = abs( y - expected[ i ] ); - tol = 18.0 * EPS * abs( expected[ i ] ); - t.ok( delta <= tol, 'within tolerance. N: '+N[i]+'. K: '+K[i]+'. n: '+n[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' ); - } + t.strictEqual( isAlmostSameValue( y, expected[ i ], 29 ), true, 'returns expected value' ); } t.end(); }); diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.native.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.native.js index b4a3375fd6ee..1a6b84f0f6c2 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.native.js +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.native.js @@ -23,9 +23,8 @@ var resolve = require( 'path' ).resolve; var tape = require( 'tape' ); var tryRequire = require( '@stdlib/utils/try-require' ); +var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var abs = require( '@stdlib/math/base/special/abs' ); -var EPS = require( '@stdlib/constants/float64/eps' ); // VARIABLES // @@ -87,8 +86,6 @@ tape( 'if provided an `n` which is not a nonnegative integer, the function retur tape( 'the function returns the variance of a hypergeometric distribution', opts, function test( t ) { var expected; - var delta; - var tol; var N; var K; var n; @@ -101,13 +98,7 @@ tape( 'the function returns the variance of a hypergeometric distribution', opts n = data.n; for ( i = 0; i < n.length; i++ ) { y = variance( N[i], K[i], n[i] ); - if ( y === expected[i] ) { - t.strictEqual( y, expected[i], 'N: '+N[i]+', K: '+K[i]+', n: '+n[i]+', y: '+y+', expected: '+expected[i] ); - } else { - delta = abs( y - expected[ i ] ); - tol = 18.0 * EPS * abs( expected[ i ] ); - t.ok( delta <= tol, 'within tolerance. N: '+N[i]+'. K: '+K[i]+'. n: '+n[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' ); - } + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); } t.end(); }); From 8fe9e7ff97bad609068b937ad3e7fbe213ccd589 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Mon, 21 Sep 2026 23:17:44 -0500 Subject: [PATCH 2/2] refactor: align JS variance formula with C implementation 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) --- .../stats/base/dists/hypergeometric/variance/lib/main.js | 4 +++- .../stats/base/dists/hypergeometric/variance/test/test.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/lib/main.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/lib/main.js index 76641f02203c..9cec7eef91da 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/lib/main.js +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/lib/main.js @@ -71,6 +71,7 @@ var PINF = require( '@stdlib/constants/float64/pinf' ); * // returns NaN */ function variance( N, K, n ) { + var p; if ( !isNonNegativeInteger( N ) || !isNonNegativeInteger( K ) || @@ -82,7 +83,8 @@ function variance( N, K, n ) { ) { return NaN; } - return n * ( K/N ) * ( (N-K)/N ) * ( (N-n)/(N-1) ); + p = K / N; + return n * p * ( 1.0-p ) * ( (N-n)/(N-1) ); } diff --git a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.js b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.js index 8ff9c0b139ad..659dab41fcec 100644 --- a/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.js +++ b/lib/node_modules/@stdlib/stats/base/dists/hypergeometric/variance/test/test.js @@ -130,7 +130,7 @@ tape( 'the function returns the variance of a hypergeometric distribution', func n = data.n; for ( i = 0; i < n.length; i++ ) { y = variance( N[i], K[i], n[i] ); - t.strictEqual( isAlmostSameValue( y, expected[ i ], 29 ), true, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); } t.end(); });