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.
86 lines (71 loc) • 3.68 kB
JavaScript
const { describe, it } = require('node:test');
const assert = require('node:assert');
const Statistics = require('../index');
const Table = require('../lib/table/index');
const RatioColumn = require('../lib/ratio-column/index');
describe('Integration Tests', () => {
it('should preserve filtering when transposed', () => {
const table = new Table();
table.addColumn('A', [10, 20, 30, 40, 50]);
table.addColumn('B', [5, 15, 25, 35, 45]);
table.filterRows([1, 2, 3]); // Фильтруем индексы 1, 2, 3
const transposed = table.transpose();
assert.strictEqual(Object.keys(transposed.columns).length, 2); // Осталось 2 строки (0 и 4)
});
it('should correctly handle different column lengths', () => {
const table = new Table();
table.addColumn('Short', [10, 20, 30]);
table.addColumn('Long', [5, 15, 25, 35, 45]);
assert.strictEqual(table.n, 5); // Максимальная длина
table.filterRows([1, 2, 3]);
assert.strictEqual(table.columns['Short'].n, 1); // Остался только индекс 0
assert.strictEqual(table.columns['Long'].n, 2); // Остались индексы 0 и 4
});
it('should compute new column correctly with filtering', () => {
const table = new Table();
table.addColumn('A', [1, 2, 3, 4, 5]);
table.addColumn('B', [10, 20, 30, 40, 50]);
table.filterRows([1, 2, 3]);
const sumCol = table.compute(({ A = 0, B = 0 }) => A + B, 'Sum');
assert.deepStrictEqual(sumCol.values, [11, 55]); // Остались индексы 0 и 4
});
it('should apply descriptive metrics correctly after filtering', () => {
const table = new Table();
table.addColumn('Numbers', [10, 20, 30, 40, 50]);
table.filterRows([2, 3, 4]);
const descTable = table.descriptive('mean');
assert.strictEqual(descTable.values[0], 15); // Среднее из [10, 20]
});
it('should correctly compare two filtered columns', () => {
const table = new Table();
table.addColumn('A', [10, 20, 30, 40, 50]);
table.addColumn('B', [5, 15, 25, 35, 45]);
table.filterRows([2, 3, 4]);
const comparison = table.compare('A', 'B');
assert.strictEqual(comparison.correlationSample.toFixed(2), '1.00'); // Линейная зависимость
});
it('should correctly perform two-sample t-test after filtering', () => {
const table = new Table();
table.addColumn('Group1', [10, 20, 30, 40, 50]);
table.addColumn('Group2', [12, 22, 32, 42, 52]);
table.filterRows([0, 1, 2]); // Оставляем последние два
const comparison = table.compare('Group1', 'Group2');
const { t } = comparison.twoSampleTTest();
assert.ok(t > -1 && t < 1); // Небольшое отличие
});
it('should apply filtering across all metrics', () => {
const table = new Table();
table.addColumn('Numbers', [10, 20, 30, 40, 50]);
table.filterRows([0, 1, 2]); // Оставляем 40 и 50
assert.strictEqual(table.columns['Numbers'].mean, 45); // (40+50)/2
assert.strictEqual(table.columns['Numbers'].n, 2);
});
it('should clone table with filtered values', () => {
const table = new Table();
table.addColumn('A', [10, 20, 30, 40, 50]);
table.filterRows([1, 2, 3]);
const cloned = table.clone(true);
assert.strictEqual(cloned.columns['A'].n, 2); // Остались 10 и 50
assert.deepStrictEqual(cloned.columns['A'].values, [10, 50]);
});
});