mirror of
https://github.com/RoboSats/robosats.git
synced 2025-01-09 15:51:34 +00:00
17 lines
417 B
TypeScript
17 lines
417 B
TypeScript
|
export const weightedMean = (arrValues: number[], arrWeights: number[]): number => {
|
||
|
if (arrValues.length === 0) {
|
||
|
return 0;
|
||
|
}
|
||
|
const result = arrValues
|
||
|
.map((value, i) => {
|
||
|
const weight = arrWeights[i];
|
||
|
const sum = value * weight;
|
||
|
return [sum, weight];
|
||
|
})
|
||
|
.reduce((p, c) => [p[0] + c[0], p[1] + c[1]], [0, 0]);
|
||
|
|
||
|
return result[0] / result[1];
|
||
|
};
|
||
|
|
||
|
export default weightedMean;
|