-
Notifications
You must be signed in to change notification settings - Fork 12
/
xMeanWeighted.ts
42 lines (36 loc) · 1.14 KB
/
xMeanWeighted.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { NumberArray } from 'cheminfo-types';
import { xCheck } from './xCheck';
import { xGetFromToIndex, XGetFromToIndexOptions } from './xGetFromToIndex';
/**
* Computes the weighted mean value of an array of values.
*
* @deprecated please use xyMassCenter
* @param array - array of numbers
* @param weights - array of weights
* @param options - options
*/
export function xMeanWeighted(
array: NumberArray,
weights: NumberArray,
options: XGetFromToIndexOptions = {},
): number {
xCheck(array, { minLength: 1 });
xCheck(weights);
if (array.length !== weights.length) {
throw new Error('array and weights must have the same length');
}
const { fromIndex, toIndex } = xGetFromToIndex(array, options);
// normalize weights
let sumWeights = 0;
let sumValue = array[fromIndex] * weights[fromIndex];
sumWeights += weights[fromIndex];
for (let i = fromIndex + 1; i <= toIndex; i++) {
sumValue += array[i] * weights[i];
sumWeights += weights[i];
}
if (sumWeights === 0) {
// We throw because the case is not well defined
throw new Error('sum of weights must be > 0');
}
return sumValue / sumWeights;
}