-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMahalanobis1.js
92 lines (68 loc) · 2.96 KB
/
Mahalanobis1.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
// Function: Mahalanobis1.js
// Authors: Corey Devin Anderson
//----------------------------------------------------------------------------
// Description:
// There are two main functions:
// Malhalanobis1() calculates the Malhalanobis distance between a row vector
// and its column means. D = sqrt((x - mu)T * S ^ -1 * (x - mu)), where x is a
// vector, mu is a vector of column means, and S ^ -1 is the inverted
// covariance matrix.
// Malhalanobis2() calculates the Malhalanobis distance between two row
// vectors. D = sqrt((x - y)T * S ^ -1 * (x - y)), where y is the vector to
// which the distance from x is to be calculated.
//----------------------------------------------------------------------------
// Parameters:
// Mahalanobis1()
// yourVector : the focal 1D Array to which the distance from the multivariate
// mean will be calculated.
// yourMatrix : a 2D Array representing all the data.
// axis : a character (either "columns" or "rows") indicating whether the
// sub-arrays represent row or column vectors. Default is
// is "columns".
// Mahalanobis2()
// yourVector1 : the first focal (1D) Array (x)
// yourVector2 : the second focal (1D) Array (y)
// yourMatrix : a 2D Array representing all the data.
// axis : a character (either "columns" or "rows") indicating whether
// the sub-arrays represent row or column vectors. Default is
// "columns".
// Returns:
// a number representing the Mahalanobis distance (from x to mu for
// Mahalanobis1 and from x to y for Mahalanobis2).
//----------------------------------------------------------------------------
// Requires: Mahalanobis_helpers.js
// Helper functions:
// getColumn()
// arrayClean()
// arrayMean()
// columnMeans()
// subtractArrays()
// multiplyArrays()
// addArrays()
// sumArray()
// vCov()
//----------------------------------------------------------------------------
// START
function Malhalanobis1(yourVector, yourMatrix, axis = "columns") {
if (axis == "columns") {
var invCov = math.inv(vCov(yourMatrix, axis = "columns")["_data"]);
var colMeans = columnMeans(yourMatrix);
var step1 = subtractArrays(yourVector, colMeans);
var step2 = math.multiply(step1, invCov);
var step3 = math.multiply(step2, math.transpose(step1))
var MD = step3 ** 0.5
return MD;
} else if (axis == "rows") {
var invCov = math.inv(vCov(yourMatrix, axis = "rows")["_data"]);
var means = columnMeans(yourMatrix, axis = "rows");
var step1 = subtractArrays(yourVector, means);
var step2 = math.multiply(step1, invCov);
var step3 = math.multiply(step2, math.transpose(step1));
var MD = step3 ** 0.5;
return MD;
}
}
// END
// console.log(Malhalanobis1(testVect, HSA))
// console.log(Malhalanobis1(testVect, HSA2, axis = "rows"))
// console.log(Malhalanobis1(getColumn(testMatrix_10, 9), testMatrix_10))