-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcaeigen.jl
55 lines (50 loc) · 1.62 KB
/
pcaeigen.jl
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
"""
pcaeigen(X; kwargs...)
pcaeigen(X, weights::Weight; kwargs...)
pcaeigen!(X::Matrix, weights::Weight; kwargs...)
PCA by Eigen factorization.
* `X` : X-data (n, p).
* `weights` : Weights (n) of the observations.
Must be of type `Weight` (see e.g. function `mweight`).
Keyword arguments:
* `nlv` : Nb. of principal components (PCs).
* `scal` : Boolean. If `true`, each column of `X`
is scaled by its uncorrected standard deviation.
Let us note D the (n, n) diagonal matrix of weights
(`weights.w`) and X the centered matrix in metric D.
The function minimizes ||X - T * P'||^2 in metric D, by
computing an Eigen factorization of X' * D * X.
See function `pcasvd` for examples.
"""
function pcaeigen(X; kwargs...)
Q = eltype(X[1, 1])
weights = mweight(ones(Q, nro(X)))
pcaeigen(X, weights; kwargs...)
end
function pcaeigen(X, weights::Weight; kwargs...)
pcaeigen!(copy(ensure_mat(X)), weights;
kwargs...)
end
function pcaeigen!(X::Matrix, weights::Weight; kwargs...)
par = recovkwargs(Par, kwargs)
Q = eltype(X)
n, p = size(X)
nlv = min(par.nlv, n, p)
xmeans = colmean(X, weights)
xscales = ones(Q, p)
if par.scal
xscales .= colstd(X, weights)
fcscale!(X, xmeans, xscales)
else
fcenter!(X, xmeans)
end
sqrtw = sqrt.(weights.w)
X .= Diagonal(sqrtw) * X
res = eigen!(Symmetric(X' * X); sortby = x -> -abs(x))
P = res.vectors[:, 1:nlv]
eig = res.values[1:min(n, p)]
eig[eig .< 0] .= 0
sv = sqrt.(eig)
T = Diagonal(1 ./ sqrtw) * X * P
Pca(T, P, sv, xmeans, xscales, weights, nothing, kwargs, par)
end