als-statistics
Version:
A powerful and lightweight JavaScript library for descriptive statistics, regression, clustering, outlier detection, and noise analysis using a flexible table/column architecture.
49 lines (43 loc) • 2.12 kB
JavaScript
const { describe, it } = require('node:test');
const assert = require('node:assert');
const confidenceInterval = require('../../../lib/ratio-column/instruments/confidence');
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);
});
});