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

42 lines (36 loc) 1.97 kB
import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import Stats from '../../lib/descriptive/index.js'; const nearly = (a,b,eps=1e-12)=> assert.ok(Math.abs(a-b)<=eps, `expected ~${b}, got ${a}`); describe('Stats.flatness & spectralPowerDensity*', () => { it('flatness GM/AM behavior: tone-like vs noise-like', () => { const tone = [0,0,0,10,0,0,0]; // spiky -> low flatness const noise = Array.from({length:64},(_,i)=>1); // flat -> flatness ~1 // nearly(Stats.flatness({ values: tone }), 0, 1e-12); nearly(Stats.flatness({ values: noise }), 1, 1e-12); // console.log(Stats.flatness({ values: tone })) }); it('spectralPowerDensityArray sums to 1', () => { const v = [1,2,3,4]; const spd = Stats.spectralPowerDensityArray({ values: v }); const sum = spd.reduce((s,x)=>s+x,0); nearly(sum, 1, 1e-12); }); it('spectralPowerDensityMetric (GM/AM of SPD)', () => { const v = [0,0,0,10,0,0,0]; // concentrated energy -> GM/AM near 0 const spd = Stats.spectralPowerDensityArray({ values: v }); const gm = Math.exp(spd.reduce((s,x)=>s+Math.log(x+Number.EPSILON),0)/spd.length); const am = spd.reduce((s,x)=>s+x,0)/spd.length; const expected = gm / am; const actual = Stats.spectralPowerDensityMetric({ spectralPowerDensityArray: spd, values: v }); nearly(actual, expected, 1e-12); assert.ok(actual < 0.1, 'SPD flatness should be very low for tone-like spectrum'); }); it('zScoresSorted returns sorted z and matching indices', () => { const values = [1, 2, 3, 10]; // 10 is the largest z const { zScores, indexes } = Stats.zScoresSorted({ values }); // z must be sorted ascending; last index corresponds to value 10 (index 3 in original) assert.ok(zScores.every((z,i)=> i===0 || zScores[i-1] <= z), 'zScores sorted ascending'); assert.equal(indexes[indexes.length-1], 3, 'largest z corresponds to last original index'); }); });