Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCA Whitening #182

Open
wildart opened this issue Feb 11, 2022 · 0 comments
Open

PCA Whitening #182

wildart opened this issue Feb 11, 2022 · 0 comments

Comments

@wildart
Copy link
Collaborator

wildart commented Feb 11, 2022

Provide a separate implementation of PCA whitening similar to Whitening, and remove duplicated code:

function _invsqrtm!(C::AbstractMatrix{<:Real})
n = size(C, 1)
size(C, 2) == n || error("C must be a square matrix.")
E = eigen!(Symmetric(C))
U = E.vectors
evs = E.values
for i = 1:n
@inbounds evs[i] = 1.0 / sqrt(sqrt(evs[i]))
end
rmul!(U, Diagonal(evs))
return U * transpose(U)
end

Note: double sqrt application

C = rmul!(Z * transpose(Z), 1.0 / (n - 1))
Efac = eigen(C)
ord = sortperm(Efac.values; rev=true)
(v, P) = extract_kv(Efac, ord, k)
W0 = rmul!(P, Diagonal(1 ./ sqrt.(v)))
Z = W0'Z

function _lda_whitening!(C::AbstractMatrix{T}, regcoef::T) where T<:Real
n = size(C,1)
E = eigen!(Symmetric(C))
v = E.values
a = regcoef * maximum(v)
for i = 1:n
@inbounds v[i] = 1.0 / sqrt(v[i] + a)
end
return rmul!(E.vectors, Diagonal(v))
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant