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
23 lines (20 loc) • 857 B
JavaScript
import { describe, it } from 'node:test';
import assert from 'node:assert';
import weightedMean from '../../lib/descriptive/weighted-mean.js';
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/);
});
});