UNPKG

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

66 lines (57 loc) 2.18 kB
import { describe, it } from 'node:test' import assert from 'node:assert'; import Statistics from '../../lib/index.js' import { BaseStats } from '../../lib/analyze/test-base/stats.js' const { Table, Stats, Analyze } = Statistics; function approx(actual, expected, epsilon = 1e-6) { assert.ok(Math.abs(actual - expected) < epsilon, `Expected ${actual}${expected}`); } describe('BaseStats', () => { it('mean and variance', () => { const stats = new BaseStats([1, 2, 3, NaN, 4], 'test'); assert(stats.mean === 2.5); assert(stats.variance === 1.25); // Population assert(stats.varianceSample === 1.6666666666666667); // Sample }); it('empty array', () => { const stats = new BaseStats([], 'test'); assert(isNaN(stats.mean)); assert(isNaN(stats.variance)); }); }); describe('OneWayAnova', () => { it.skip('standard ANOVA', () => { const test = new Analyze.CompareMeans.OneWayAnova({ g1: [1, 2, 3], g2: [4, 5, 6], g3: [7, 8, 9] }); assert(test.F === 12); // 27 approx(test.p,0.002, 3); // 0.001 }); it('Welch ANOVA (post-fix)', () => { const test = new Analyze.CompareMeans.OneWayAnova({ g1: [1, 2], g2: [3, 6, 9], g3: [10, 20] }, true); const {p,F} = test assert(!isNaN(F)); // Pre-fix was NaN assert(p >= 0); }); }); describe('Table', () => { it('addColumn and mean', () => { const table = new Table({ col1: [1, 2, 3], col2: [4, 5, 6] }); assert(table.columns.col1.mean === 2); table.addColumn('col3', [7, 8, 9]); assert(table.n === 3); assert(table.columns.col3.mean === 8); }); }); describe('IndependentTTest', () => { it('basic t-test', () => { const test = new Analyze.CompareMeans.IndependentTTest({ group1: [1, 2, 3], group2: [4, 5, 6] }); approx(test.t,-3, 1); approx(test.p,0.039, 3) }); }); describe('Pearson', () => { it('correlation', () => { const pearson = new Analyze.Correlate.Pearson({ x: [1, 2, 3], y: [4, 5, 6] }); assert(pearson.r === 1); approx(pearson.p, 0, 3); // With df=1, but edge }); });