simple-statistics
Version:
23 lines (21 loc) • 810 B
JavaScript
/**
* When combining two lists of values for which one already knows the means,
* one does not have to necessary recompute the mean of the combined lists in
* linear time. They can instead use this function to compute the combined
* mean by providing the mean & number of values of the first list and the mean
* & number of values of the second list.
*
* @since 3.0.0
* @param {number} mean1 mean of the first list
* @param {number} n1 number of items in the first list
* @param {number} mean2 mean of the second list
* @param {number} n2 number of items in the second list
* @returns {number} the combined mean
*
* @example
* combineMeans(5, 3, 4, 3); // => 4.5
*/
function combineMeans(mean1, n1, mean2, n2) {
return (mean1 * n1 + mean2 * n2) / (n1 + n2);
}
export default combineMeans;