-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (67 loc) · 1.66 KB
/
index.js
File metadata and controls
83 lines (67 loc) · 1.66 KB
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
// @ts-check
"use strict"
/**
* @param {number} a
* @param {number} b
*/
function ascending (a, b) {
return a < b
? -1
: a > b
? 1
: 0
}
function add (a, b) {
return a + b
}
function sum (list) {
return list.reduce (add, 0)
}
function mean (list) {
return sum (list) / list.length
}
/** @param {number[]} list */
const median = list => {
const index = list.length / 2
// console.debug ("list", list, "length", list.length, "index", index)
if (list.length % 2 === 0) return mean (list.slice (index - 1, index + 1))
else return list[index|0]
}
/**
* outliers: extreme observations.
* Data points that are more than 1.5 times away from the interquartile range.
* @param {number} interquartileRange
* @param {number[]} list
*/
function outliers (Q1, Q3, interquartileRange, list) {
const quartileDistance = interquartileRange * 1.5
const min = Q1 - quartileDistance
const max = Q3 + quartileDistance
// console.debug ("quartileDistance", min, max)
return list.filter (n => n < min || n > max)
}
/** @param {number[]} list */
function quartiles (list) {
list.sort (ascending)
const min = list[0] // Math.min.apply (Math, list)
const max = list[list.length - 1] // Math.max.apply (Math, list)
// variationsbredde AKA range
const range = max - min
const index = list.length / 2
const M = median (list)
const Q1 = median (list.slice (0, index))
const Q3 = median (list.slice (Math.ceil (index)))
// interquartile range AKA kvartilafstand = Q3 - Q1
const interquartileRange = Q3 - Q1
return {
min,
Q1,
M,
Q3,
max,
range,
interquartileRange,
outliers: outliers(Q1, Q3, interquartileRange, list)
}
}
export { median, quartiles, mean }