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
11 lines (10 loc) • 409 B
JavaScript
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;
}
export default weightedMean