-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (45 loc) · 1.23 KB
/
index.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
module.exports = {
calculateMean: function(array) {
let sum = array.reduce((accumulator, val) => accumulator + val)
let result = sum / array.length
return result
},
subtractMeanFromElements: function(array, mean) {
let subtractedElements = []
for (let element in array) {
subtractedElements.push(array[element] - mean)
}
return subtractedElements
},
squareSubtractedElements: function(array) {
return array.map(x => Math.pow(x, 2))
},
addSquaredElements: function(array) {
return array.reduce((accumulator, val) => accumulator + val)
},
findVariance: function(squaredsum, originalset) {
let n = originalset.length
n = n - 1
return squaredsum / n
},
takeSquareRootOfVariance: function(variance) {
return Math.sqrt(variance)
},
calculateStandardDeviation: function(dataset) {
let standarddeviation = this.takeSquareRootOfVariance(
this.findVariance(
this.addSquaredElements(
this.squareSubtractedElements(
this.subtractMeanFromElements(
dataset,
this.calculateMean(dataset),
dataset
)
)
),
dataset
)
)
return standarddeviation
}
}