-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilities.js
59 lines (41 loc) · 1.25 KB
/
utilities.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
//returns the mean of an Array
function mean (set) {
'use strict';
if (!(set instanceof Array)) {
throw ('Please pass an Array to the mean() function.');
}
var i, mean;
mean = 0;
for (i = 0; i < set.length; i++) {
mean += set[i];
}
return mean / set.length;
}
//returns the covariance of two Arrays of numbers of equal length.
function covariance (setA, setB) {
'use strict';
var i, product;
if (setA.length !== setB.length) {
throw ('covariance() only accepts Arrays() of equal length.');
}
product = [];
for (i = 0; i < setA.length; i++) {
product[i] = setA[i] * setB[i];
}
return mean(product) - mean(setA)*mean(setB);
}
//returns the standard deviation of an Array.
function stdev (set) {
if (!(set instanceof Array)) {
throw ('Please pass an Array to the stdev() function.');
}
return Math.sqrt(covariance(set, set));
}
//returns the correlation coefficient between two Arrays of numbers of equal length.
function correlation (setA, setB) {
'use strict';
if (setA.length !== setB.length) {
throw ('correlation() only accepts Arrays() of equal length.');
}
return covariance(setA, setB) / stdev(setA) / stdev(setB);
}