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
49 lines (43 loc) • 2.1 kB
JavaScript
import { describe, it } from 'node:test';
import assert from 'node:assert';
import confidenceInterval from '../../lib/descriptive/confidence.js';
describe('confidenceInterval function', () => {
it('calculates correct confidence intervals', () => {
const sample = { mean: 50, stdDevSample: 10, n: 30 };
const ci = confidenceInterval(sample);
assert(ci.low < 50 && ci.high > 50);
});
it('returns same value when n < 2', () => {
const sample = { mean: 50, stdDevSample: 10, n: 1 };
const ci = confidenceInterval(sample);
assert.strictEqual(ci.low, 50);
assert.strictEqual(ci.high, 50);
assert.strictEqual(ci.width, 0);
});
it('handles zero standard deviation', () => {
const sample = { mean: 50, stdDevSample: 0, n: 10 };
const ci = confidenceInterval(sample);
assert.strictEqual(ci.low, 50);
assert.strictEqual(ci.high, 50);
assert.strictEqual(ci.width, 0);
});
it('should use correct t-table value when df <= 30', () => {
const sample = { mean: 50, stdDevSample: 10, n: 10 };
const ci = confidenceInterval(sample);
assert.ok(ci.width > 0);
});
it('should handle zero mean in confidence interval', () => {
const sample = { mean: 0, stdDevSample: 10, n: 10 };
const ci = confidenceInterval(sample);
assert.ok(Math.abs(ci.low - (-7.153)) < 0.001, `Expected ci.low ≈ -7.153, but got ${ci.low}`); // Допуск на погрешность
assert.ok(Math.abs(ci.high - 7.153) < 0.001, `Expected ci.high ≈ 7.153, but got ${ci.high}`);
assert.strictEqual(ci.width.toFixed(2), '14.31'); // Округляем до 2 знаков
});
it('should use 1.96 for df > 30', () => {
const sample = { mean: 100, stdDevSample: 20, n: 40 };
const ci = confidenceInterval(sample);
const expectedMargin = 1.96 * (20 / Math.sqrt(40));
assert.ok(Math.abs(ci.low - (100 - expectedMargin)) < 0.001);
assert.ok(Math.abs(ci.high - (100 + expectedMargin)) < 0.001);
});
});