-
Notifications
You must be signed in to change notification settings - Fork 61
/
statistical.js
175 lines (138 loc) · 4.3 KB
/
statistical.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const FormulaError = require('../error');
const {FormulaHelpers, Types, Criteria, Address} = require('../helpers');
const {Infix} = require('../operators');
const H = FormulaHelpers;
const {DistributionFunctions} = require('./distribution');
const StatisticalFunctions = {
AVEDEV: (...numbers) => {
let sum = 0;
const arr = [];
// parse number only if the input is literal
H.flattenParams(numbers, Types.NUMBER, true, (item, info) => {
if (typeof item === "number") {
sum += item;
arr.push(item);
}
});
const avg = sum / arr.length;
sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += Math.abs(arr[i] - avg);
}
return sum / arr.length;
},
AVERAGE: (...numbers) => {
let sum = 0, cnt = 0;
// parse number only if the input is literal
H.flattenParams(numbers, Types.NUMBER, true, (item, info) => {
if (typeof item === "number") {
sum += item;
cnt++;
}
});
return sum / cnt;
},
AVERAGEA: (...numbers) => {
let sum = 0, cnt = 0;
// parse number only if the input is literal
H.flattenParams(numbers, Types.NUMBER, true, (item, info) => {
const type = typeof item;
if (type === "number") {
sum += item;
cnt++;
} else if (type === "string") {
cnt++;
}
});
return sum / cnt;
},
// special
AVERAGEIF: (context, range, criteria, averageRange) => {
const ranges = H.retrieveRanges(context, range, averageRange);
range = ranges[0];
averageRange = ranges[1];
criteria = H.retrieveArg(context, criteria);
const isCriteriaArray = criteria.isArray;
criteria = Criteria.parse(H.accept(criteria));
let sum = 0, cnt = 0;
range.forEach((row, rowNum) => {
row.forEach((value, colNum) => {
const valueToAdd = averageRange[rowNum][colNum];
if (typeof valueToAdd !== "number")
return;
// wildcard
if (criteria.op === 'wc') {
if (criteria.match === criteria.value.test(value)) {
sum += valueToAdd;
cnt++;
}
} else if (Infix.compareOp(value, criteria.op, criteria.value, Array.isArray(value), isCriteriaArray)) {
sum += valueToAdd;
cnt++;
}
})
});
if (cnt === 0) throw FormulaError.DIV0;
return sum / cnt;
},
AVERAGEIFS: () => {
},
COUNT: (...ranges) => {
let cnt = 0;
H.flattenParams(ranges, null, true,
(item, info) => {
// literal will be parsed to Type.NUMBER
if (info.isLiteral && !isNaN(item)) {
cnt++;
} else {
if (typeof item === "number")
cnt++;
}
});
return cnt;
},
COUNTIF: (range, criteria) => {
// do not flatten the array
range = H.accept(range, Types.ARRAY, undefined, false, true);
const isCriteriaArray = criteria.isArray;
criteria = H.accept(criteria);
let cnt = 0;
// parse criteria
criteria = Criteria.parse(criteria);
range.forEach(row => {
row.forEach(value => {
// wildcard
if (criteria.op === 'wc') {
if (criteria.match === criteria.value.test(value))
cnt++;
} else if (Infix.compareOp(value, criteria.op, criteria.value, Array.isArray(value), isCriteriaArray)) {
cnt++;
}
})
});
return cnt;
},
LARGE: () => {
},
MAX: () => {
},
MAXA: () => {
},
MAXIFS: () => {
},
MEDIAN: () => {
},
MIN: () => {
},
MINA: () => {
},
MINIFS: () => {
},
PERMUT: () => {
},
PERMUTATIONA: () => {
},
SMALL: () => {
},
};
module.exports = Object.assign(StatisticalFunctions, DistributionFunctions);