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
25 lines (20 loc) • 952 B
JavaScript
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { OneSampleTTest } from '../../../lib/analyze/compare-means/one-sample.js';
function approx(a,b,eps=1e-10){ assert.ok(Math.abs(a-b)<=eps*(1+Math.max(Math.abs(a),Math.abs(b))),`~${b}, got ${a}`); }
describe('OneSampleTTest', () => {
it('basic case (n=4), mean!=mu0', () => {
const data = { X: [2,4,6,8] }; // mean=5, var_sample=6.666...
const ttest = new OneSampleTTest(data, /* mu0 */ 6);
assert.strictEqual(ttest.df, 3);
// sd = sqrt(6.666...), se = sd/sqrt(4) ~ 1.2909944, t = (5-6)/se ~ -0.7745967
approx(ttest.t, -0.7745966692414834, 1e-12);
assert.ok(ttest.p > 0.45 && ttest.p < 0.50, `p=${ttest.p}`);
});
it('variance=0 and mean==mu0 -> t=0, p=1', () => {
const t0 = new OneSampleTTest({ X: [3,3,3,3] }, 3);
assert.strictEqual(t0.df, 3);
approx(t0.t, 0);
approx(t0.p, 1, 1e-12);
});
});