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
62 lines (48 loc) • 2.08 kB
JavaScript
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { Pearson } from '../../../lib/analyze/correlate/pearson.js';
const sample1 = [10, 20, 30, 40, 50]
const sample2 = [15, 25, 35, 45, 55]
describe('Pearson population', () => {
const analysis = new Pearson({ sample1, sample2 },true);
it('should calculate sum of covariance (sumCov)', () => {
assert.strictEqual(analysis.sumCov.toFixed(2), '1000.00');
});
it('should calculate covariance for population', () => {
assert.strictEqual(analysis.covariance.toFixed(2), '200.00');
});
it('should calculate correlation coefficient for population', () => {
assert.strictEqual(analysis.r.toFixed(2), '1.00');
});
});
describe('Pearson sample', () => {
const analysis = new Pearson({ sample1, sample2 }, false);
it('should calculate covariance for sample', () => {
assert.strictEqual(analysis.covariance.toFixed(2), '250.00');
});
it('should calculate correlation coefficient for sample', () => {
assert.strictEqual(analysis.r.toFixed(2), '1.00');
});
})
describe('Edge cases', () => {
it('should return correlation as 0 if one sample has zero variance', () => {
const sample1 = [1, 1, 1, 1, 1]
const sample2 = [10, 20, 30, 40, 50]
const comparison = new Pearson({ sample1, sample2 });
// assert.strictEqual(comparison.r, 0);
});
it('should return 0 correlationSample with zero variance', () => {
const sample1 = [1, 1, 1]
const sample2 = [10, 20, 30]
const comparison = new Pearson({ sample1, sample2 });
assert.strictEqual(comparison.r, 0);
});
})
describe('Pearson — extra', () => {
it('population flag produces same result as sample when both are valid (smoke)', () => {
const r1 = new Pearson({ s1: [1, 2, 3, 4], s2: [2, 3, 4, 5] }).r;
const r2 = new Pearson({ s1: [1, 2, 3, 4], s2: [2, 3, 4, 5] }).r;
// console.log({ r1, r2 })
assert.ok(Math.abs(r1 - r2) < 1e-12);
});
});