UNPKG

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.

11 lines (10 loc) 411 B
function weightedMean(weights,values) { if (!Array.isArray(weights) || weights.length !== values.length) throw new Error("Weights must be an array of the same length as values"); let wSum = 0, valSum = 0; for (let i = 0; i < values.length; i++) { wSum += weights[i]; valSum += values[i] * weights[i]; } return wSum === 0 ? 0 : valSum / wSum; } module.exports = weightedMean