als-statistics
Version:
Modular JS statistics toolkit for Node.js and the browser: descriptive stats, correlations (Pearson/Spearman/Kendall), t-tests & ANOVA (Student/Welch), reliability (Cronbach’s alpha), regression (linear/logistic), clustering (DBSCAN/HDBSCAN), and table/co
56 lines (44 loc) • 1.77 kB
JavaScript
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { IndependentTTest } from '../../../lib/analyze/compare-means/independent-t-test.js';
function approx(a, b, eps = 1e-10) {
assert.ok(Math.abs(a - b) <= eps, `expected ~${b}, got ${a}`);
}
describe('Comparative', () => {
const data = {
sample1: [10, 20, 30, 40, 50],
sample2: [15, 25, 35, 45, 55],
}
const analysis = new IndependentTTest(data);
it('should perform two-sample t-test', () => {
const { t, df, F } = analysis;
assert.strictEqual(t.toFixed(2), '-0.50');
assert.strictEqual(df, 8);
assert.strictEqual(F.toFixed(2), '0.25');
});
it('handles zero variance in one group for two-sample t-test', () => {
const data = {
sample1: [1, 1, 1, 1, 1], // variance = 0
sample2: [10, 20, 30, 40, 50], // variance > 0
};
const tt = new IndependentTTest(data); // pooled вариант
const { t, df, F, p } = tt;
// t определён и конечен
assert.ok(Number.isFinite(t));
// df в pooled варианте = n1+n2-2
assert.strictEqual(df, 5 + 5 - 2); // 8
// F = t^2 (ANOVA-эквивалент)
approx(F, t * t, 1e-12);
// При столь разных средних p должно быть маленьким
assert.ok(p < 0.01, `expected small p, got ${p}`);
});
it('should return zero t-statistic when means are equal', () => {
const data = {
sample1: [1, 2, 3],
sample2: [1, 2, 3],
}
const comparison = new IndependentTTest(data);
const { t } = comparison;
assert.strictEqual(t, 0);
});
});