-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstats.js
151 lines (127 loc) · 3.82 KB
/
stats.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
/* Mini statistics library to calculate/perform:
* - Mean
* - Median
* - Standard Deviation
* - Mean Absolute Deviation (MAD)
* - Outlier detection (Iglewicz and Hoaglin method)
* - Remove outliers from an array of numbers
*/
var outlierMethod = {
MAD: 'MAD',
medianDiff: 'medianDiff'
}
function mean (arr) {
return (arr.reduce(function (prev, curr) {
return prev + curr
}) / arr.length)
}
function variance (arr) {
var dataMean = mean(arr)
return mean(arr.map(function (val) {
return Math.pow(val - dataMean, 2)
}))
}
function stdev (arr) {
return Math.sqrt(variance(arr))
}
function median (arr) {
var half = Math.floor(arr.length / 2)
arr = arr.slice(0).sort(numSorter)
if (arr.length % 2) { // Odd length, true middle element
return arr[half]
} else { // Even length, average middle two elements
return (arr[half - 1] + arr[half]) / 2.0
}
}
function medianAbsoluteDeviation (arr, dataMedian) {
dataMedian = dataMedian || median(arr)
var absoluteDeviation = arr.map(function (val) {
return Math.abs(val - dataMedian)
})
return median(absoluteDeviation)
}
function numSorter (a, b) {
return a - b
}
// Iglewicz and Hoaglin method
// values with a Z-score > 3.5 are considered potential outliers
function isMADoutlier (val, threshold, dataMedian, dataMAD) {
return Math.abs((0.6745 * (val - dataMedian)) / dataMAD) > threshold
}
function indexOfMADoutliers (arr, threshold) {
threshold = threshold || 3.5 // Default recommended threshold
var dataMedian = median(arr)
var dataMAD = medianAbsoluteDeviation(arr, dataMedian)
return arr.reduce(function (res, val, i) {
if (isMADoutlier(val, threshold, dataMedian, dataMAD)) {
res.push(i)
}
return res
}, [])
}
function filterMADoutliers (arr, threshold) {
threshold = threshold || 3.5 // Default recommended threshold
var dataMedian = median(arr)
var dataMAD = medianAbsoluteDeviation(arr, dataMedian)
return arr.filter(function (val) {
return !isMADoutlier(val, threshold, dataMedian, dataMAD)
})
}
// Median filtering from difference between values
function differences (arr) {
return arr.map(function (d, i) {
return Math.round(Math.abs(d - (arr[i - 1] || d[0]))) + 1
})
}
function isMedianDiffOutlier (threshold, difference, medianDiff) {
return difference / medianDiff > threshold
}
function indexOfMedianDiffOutliers (arr, threshold) {
threshold = threshold || 3 // Default threshold of 3 std
var differencesArr = differences(arr)
var medianDiff = median(differencesArr)
return arr.reduce(function (res, val, i) {
if (isMedianDiffOutlier(threshold, differencesArr[i], medianDiff)) {
res.push(i)
}
return res
}, [])
}
function filterMedianDiffOutliers (arr, threshold) {
threshold = threshold || 3 // Default threshold of 3 std
var differencesArr = differences(arr)
var medianDiff = median(differencesArr)
return arr.filter(function (_, i) {
return !isMedianDiffOutlier(threshold, differencesArr[i], medianDiff)
})
}
function filterOutliers (arr, method, threshold) {
switch (method) {
case outlierMethod.MAD:
return filterMADoutliers(arr, threshold)
default:
return filterMedianDiffOutliers(arr, threshold)
}
}
function indexOfOutliers (arr, method, threshold) {
switch (method) {
case outlierMethod.MAD:
return indexOfMADoutliers(arr, threshold)
default:
return indexOfMedianDiffOutliers(arr, threshold)
}
}
module.exports = {
stdev: stdev,
mean: mean,
median: median,
MAD: medianAbsoluteDeviation,
numSorter: numSorter,
outlierMethod: outlierMethod,
filterOutliers: filterOutliers,
indexOfOutliers: indexOfOutliers,
filterMADoutliers: filterMADoutliers,
indexOfMADoutliers: indexOfMADoutliers,
filterMedianDiffOutliers: filterMedianDiffOutliers,
indexOfMedianDiffOutliers: indexOfMedianDiffOutliers
}