From f4458b199b08919d055f7256b26d4650394b72f4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 19 Sep 2026 10:16:14 +0000 Subject: [PATCH] test: migrate `stats/chi2test` to ULP-based assertions Replace relative tolerance comparisons with `isAlmostSameValue` ULP difference assertions, using the minimum required ULP value for each test case. Ref: https://github.com/stdlib-js/stdlib/issues/11352 Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_0123sP14BeF6ekQw7pe3qbWB --- .../@stdlib/stats/chi2test/test/test.js | 63 ++++--------------- 1 file changed, 13 insertions(+), 50 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/chi2test/test/test.js b/lib/node_modules/@stdlib/stats/chi2test/test/test.js index db318c6043f2..35284454155e 100644 --- a/lib/node_modules/@stdlib/stats/chi2test/test/test.js +++ b/lib/node_modules/@stdlib/stats/chi2test/test/test.js @@ -22,9 +22,8 @@ var tape = require( 'tape' ); var array = require( '@stdlib/ndarray/array' ); +var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); var contains = require( '@stdlib/assert/contains' ); -var abs = require( '@stdlib/math/base/special/abs' ); -var EPS = require( '@stdlib/constants/float64/eps' ); var chi2test = require( './../lib' ); @@ -143,8 +142,6 @@ tape( 'the function throws an error if provided a significance level `alpha` out tape( 'the function correctly computes the chi-square independence test for a two-way contingency table', function test( t ) { var expected; - var delta; - var tol; var out; var x; @@ -153,22 +150,16 @@ tape( 'the function correctly computes the chi-square independence test for a tw // Tested against R: expected = counts.pValue; - delta = abs( out.pValue - expected ); - tol = 4.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. pValue: '+out.pValue+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.pValue, expected, 1 ), true, 'returns expected value' ); expected = counts.statistic; - delta = abs( out.statistic - expected ); - tol = 12.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. statistic: '+out.statistic+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.statistic, expected, 2 ), true, 'returns expected value' ); t.end(); }); tape( 'the function correctly computes the chi-square independence test for a two-way contingency table (supplied as an `ndarray`)', function test( t ) { var expected; - var delta; - var tol; var out; var x; @@ -177,22 +168,16 @@ tape( 'the function correctly computes the chi-square independence test for a tw // Tested against R: expected = counts.pValue; - delta = abs( out.pValue - expected ); - tol = 4.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. pValue: '+out.pValue+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.pValue, expected, 1 ), true, 'returns expected value' ); expected = counts.statistic; - delta = abs( out.statistic - expected ); - tol = 12.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. statistic: '+out.statistic+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.statistic, expected, 2 ), true, 'returns expected value' ); t.end(); }); tape( 'the function correctly computes the chi-square independence test for a two-way contingency table with two categories each (with continuity correction)', function test( t ) { var expected; - var delta; - var tol; var out; var x; @@ -201,22 +186,16 @@ tape( 'the function correctly computes the chi-square independence test for a tw // Tested against R: expected = corrected.pValue; - delta = abs( out.pValue - expected ); - tol = 20.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. pValue: '+out.pValue+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.pValue, expected, 16 ), true, 'returns expected value' ); expected = corrected.statistic; - delta = abs( out.statistic - expected ); - tol = 2.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. statistic: '+out.statistic+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.statistic, expected, 0 ), true, 'returns expected value' ); t.end(); }); tape( 'the function correctly computes the chi-square independence test for a two-way contingency table with two categories each (without continuity correction)', function test( t ) { var expected; - var delta; - var tol; var out; var x; @@ -227,22 +206,16 @@ tape( 'the function correctly computes the chi-square independence test for a tw // Tested against R: expected = uncorrected.pValue; - delta = abs( out.pValue - expected ); - tol = 12.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. pValue: '+out.pValue+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.pValue, expected, 8 ), true, 'returns expected value' ); expected = uncorrected.statistic; - delta = abs( out.statistic - expected ); - tol = 2.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. statistic: '+out.statistic+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.statistic, expected, 0 ), true, 'returns expected value' ); t.end(); }); tape( 'the function correctly computes the chi-square independence test for a two-way contingency table with more columns than rows', function test( t ) { var expected; - var delta; - var tol; var out; var x; @@ -251,22 +224,16 @@ tape( 'the function correctly computes the chi-square independence test for a tw // Tested against R: expected = moreCols.pValue; - delta = abs( out.pValue - expected ); - tol = 8.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. pValue: '+out.pValue+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.pValue, expected, 2 ), true, 'returns expected value' ); expected = moreCols.statistic; - delta = abs( out.statistic - expected ); - tol = 2.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. statistic: '+out.statistic+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.statistic, expected, 0 ), true, 'returns expected value' ); t.end(); }); tape( 'the function correctly computes the chi-square independence test for a two-way contingency table with more rows than columns', function test( t ) { var expected; - var delta; - var tol; var out; var x; @@ -275,14 +242,10 @@ tape( 'the function correctly computes the chi-square independence test for a tw // Tested against R: expected = moreRows.pValue; - delta = abs( out.pValue - expected ); - tol = 2.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. pValue: '+out.pValue+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.pValue, expected, 0 ), true, 'returns expected value' ); expected = moreRows.statistic; - delta = abs( out.statistic - expected ); - tol = 2.0 * EPS * abs( expected ); - t.ok( delta <= tol, 'within tolerance. x: '+x+'. statistic: '+out.statistic+'. E: '+expected+'. Δ: '+delta+'. tol: '+tol ); + t.strictEqual( isAlmostSameValue( out.statistic, expected, 0 ), true, 'returns expected value' ); t.end(); });