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.
23 lines (20 loc) • 885 B
JavaScript
const { describe, it } = require('node:test');
const assert = require('node:assert');
const weightedMean = require('../../../lib/ratio-column/instruments/weighted-mean');
describe('weightedMean function', () => {
it('should compute weighted mean correctly', () => {
const values = [10, 20, 30];
const weights = [1, 2, 3];
assert.strictEqual(weightedMean(weights, values).toFixed(2), '23.33');
});
it('should return 0 when weights sum to 0', () => {
const values = [10, 20, 30];
const weights = [0, 0, 0];
assert.strictEqual(weightedMean(weights, values), 0);
});
it('should throw error for mismatched lengths', () => {
const values = [10, 20, 30];
const weights = [1, 2];
assert.throws(() => weightedMean(weights, values), /Weights must be an array of the same length as values/);
});
});