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
30 lines (25 loc) • 1.94 kB
JavaScript
import test from 'node:test';
import assert from 'node:assert';
import CDF from '../../../lib/analyze/cdf/index.js';
import { gammaLn } from '../../../lib/analyze/cdf/gammaLn.js';
import { constants } from '../../../lib/analyze/cdf/constants.js';
const betaLn = (a,b) => gammaLn(a, constants) + gammaLn(b, constants) - gammaLn(a + b, constants)
function approxEqual(actual, expected, tolerance, message = '') {
const diff = Math.abs(actual - expected);
assert.ok(diff <= tolerance, `${message}\nactual=${actual}, expected=${expected}, diff=${diff}`);
}
test('gammaLn: проверка нескольких известных точек', () => {
approxEqual(gammaLn(1,constants), 0, 1e-14, 'lnGamma(1) должно быть ~0'); // 1) gamma(1) = 1 => ln(gamma(1)) = 0
approxEqual(gammaLn(0.5,constants), Math.log(Math.sqrt(Math.PI)), 1e-12, 'lnGamma(0.5) должно быть ln(sqrt(pi))'); // 2) gamma(1/2) = sqrt(pi) => lnGamma(1/2) = 0.5723649... Для удобства округлим до ~7 знаков
approxEqual(gammaLn(5,constants), Math.log(24), 1e-12, 'lnGamma(5) должно быть ln(24)'); // 3) gamma(5) = 24 => ln(gamma(5)) = ln(24) = 3.1780538303...
});
test('betaLn: проверка известных значений Beta(a,b)', () => {
approxEqual(betaLn(1, 1), 0, 1e-14, 'betaLn(1,1) должно быть 0'); // 1) Beta(1,1)=1 => ln(Beta(1,1))=0
approxEqual(betaLn(0.5, 0.5), Math.log(Math.PI), 1e-12, 'betaLn(0.5,0.5) должно быть ln(pi)'); // 2) Beta(0.5,0.5) = pi => ln(pi)=1.14472988585...
});
test('regularizedIncompleteBeta: несколько тестов I_x(a,b)', () => {
approxEqual(CDF.regularizedIncompleteBeta(0.4, 2, 3), 0.5248, 1e-6, 'I_0.4(2,3) ~0.5248');
});
test('tCDF: проверка распределения Стьюдента', () => {
approxEqual(CDF.t(10, 1), 0.968274, 1e-6, 'tCDF(10; df=1) ~ 0.968274');
});