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

25 lines (20 loc) 971 B
import { describe, it } from 'node:test'; import assert from 'node:assert'; import { Spearman } from '../../../lib/analyze/correlate/spearman.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('Spearman', () => { it('perfectly increasing -> r=1, p≈0', () => { const s = new Spearman({ X:[1,2,3,4,5], Y:[2,3,4,5,6] }); approx(s.r, 1, 1e-12); assert.ok(s.p < 1e-6, `p=${s.p}`); }); it('ties (average ranks): expected high positive r', () => { const s = new Spearman({ X:[1,1,2,2,3], Y:[10,10,20,30,40] }); // ожидаемое r_s ≈ 0.9733285267845753 (посчитано по ранговому Пирсону) approx(s.r, 0.9733285267845753, 1e-10); }); it('different lengths -> trimmed to min length', () => { const s = new Spearman({ X:[1,2,3,4], Y:[2,4,6] }); // min n=3 assert.strictEqual(s.n, 3); }); });