-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (36 loc) · 770 Bytes
/
index.js
File metadata and controls
41 lines (36 loc) · 770 Bytes
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
module.exports = function (params) {
if (!params) params = {}
var n = 0
var mean = 0
var m2 = 0
var value = 0
var ddof = params.ddof || 0
var variance = function variance (x, seriesObject) {
if (!isNaN(x)) {
if (typeof x !== 'number') {
x = parseFloat(x)
}
n += 1
var delta = x - mean
mean = mean + delta / n
m2 = m2 + delta * (x - mean)
if (n > 1) {
value = m2 / (n - ddof)
}
} else if (Array.isArray(x)) {
x.forEach(el => variance(el))
}
return value
}
Object.defineProperty(variance, 'value', {
get: function () {
return value
}
})
Object.defineProperty(variance, 'n', {
get: function () {
return n
}
})
return variance
}