-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrices
66 lines (59 loc) · 2.8 KB
/
Matrices
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
\d .gmb.mat
//null aware primitives from Nick Psaris's Funq
noflip:{system"g"} / redefine to customize behavior
ncount:{count[x]-$[type x;sum null x;0i {x+null y}/ x]}
nsum:{$[type x;sum x;0i {x+0i^y}/ x]}
navg:{$[type x;avg x;nsum[x]%ncount x]}
nwavg:{[w;x]$[type x;w wavg x;(%/){x+y*(0f^z;not null z)}/[0 0f;w;x]]}
nvar:{$[type x;var x;navg[x*x]-m*m:navg x]}
ndev:{sqrt nvar::}
nsvar:{$[type x;svar x;(n*nvar x)%-1+n:ncount x]}
nsdev:{sqrt nsvar::}
//Matrix functions & primitives from Nick Psaris's Funq
f2nd:{[f;X]$[noflip[];(f value::) peach flip (count[X]#`)!X;f peach flip X]} //avoids flips when garbage collection is on --Nick Psaris Funq
mm:{$[;y] peach x} //X mmu Y replacement
mmt:{(y$) peach x} // X mmu flip Y replacement
mtm:{f2nd[$[;y];x]} //flip X mmu Y replacement
// My custom Matrix Functions
nSCovMatrix:{[matrix]
//takes table or array
//null aware Sample Covariance Matrix
$[98h=type matrix;matrix:"f"$(flip matrix[cols matrix]);matrix:"f"$matrix];
corMatrix:u cor/:\:u: flip matrix;
sd:sqrt nsvar[matrix]; //standard dev of each variable
diagMatrix:sd*{x=/:x}til first count each matrix;
:covMatrix:(diagMatrix) mmu corMatrix mmu (diagMatrix);
};
chiSquareDistanceRow:{[table]
//takes table of categorical variables, returns chisquare distance matrix of rows(observations)
n:count table;
p:count cols table;
cDT:flip value flip .gmb.preprocessing.oneHotEncoder[table][`CDT];
step1:{[x;y]y[x]<>/:y}\:[til n;cDT]; //diff of CDT entries squared
step2:{[step1;cDT]step1[til count cDT]%\:(sum cDT)}\:[step1;cDT]; //step2 divide each entry by sum of columns of CDT
:({[x;y]sum each y[x]}\:[til count cDT;step2])*n%p; //matrix of chi squared distance between rows
};
chiSquareDistanceColumn:{[table]
//takes table of categorical variables, returns chisquare distance matrix of cols(variables)
n:count table;
cDT:flip value flip .gmb.preprocessing.oneHotEncoder[table][`CDT];
//n* sums squar diffs between CDT columns
:n*sum each {[n;cDT] flip((flip cDT)[n] <>/: (flip cDT)[til count flip cDT])}\:[til count flip cDT;cDT];
};
covarianceMatrix:{(x+flip(not n=\:n)*x:(n#'0.0),'(x$/:'(n:til count x)_\:x)%count first x)-a*\:a:avg each x} //from .stat.q
getDiagonal:{[x]
//retrieves diagonal from a matrix
:x@'til count x;
};
mahalanobisDistance:{[dataSet]
//takes a table OR array, null aware, uses sample cov
//each observation is included in set of observations
//room for improvement on finding covariance table
$[98h=type dataSet;matrix:"f"$(value flip dataSet);matrix:"f"$dataSet];
observation:(flip matrix);
step1: 0f^(observation -\: (navg peach matrix));
covMatrix:nSCovMatrix[0^observation];
step2: $[;inv covMatrix] peach step1;
step3:sqrt (step2 mmu/: step1);
:getDiagonal[step3];
};