Skip to content

Commit

Permalink
plskern
Browse files Browse the repository at this point in the history
  • Loading branch information
mlesnoff committed Dec 22, 2024
1 parent 7c8c51e commit b6a426b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
11 changes: 8 additions & 3 deletions src/plskern.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ end

function plskern!(X::Matrix, Y::Union{Matrix, BitMatrix}, weights::Weight; kwargs...)
par = recovkw(ParPlsr, kwargs).par
## Specific for Plsda functions
Q = eltype(X)
isa(Y, BitMatrix) ? Y = convert.(Q, Y) : nothing
## End
n, p = size(X)
q = nco(Y)
nlv = min(n, p, maximum(par.nlv)) # 'maximum' required for plsravg
Expand All @@ -121,9 +123,12 @@ function plskern!(X::Matrix, Y::Union{Matrix, BitMatrix}, weights::Weight; kwarg
fcenter!(X, xmeans)
fcenter!(Y, ymeans)
end
D = Diagonal(weights.w)
XtY = X' * (D * Y) # = Xd' * Y = X' * D * Y (Xd = D * X Very costly!!)
#XtY = X' * (weights.w .* Y) # Can create OutOfMemory errors for very large matrices
## XtY
fweight!(Y, weights.w)
XtY = X' * Y
## Old
## D = Diagonal(weights.w)
## XtY = X' * (D * Y) # Xd = D * X Very costly!!
## Pre-allocation
T = similar(X, n, nlv)
W = similar(X, p, nlv)
Expand Down
2 changes: 1 addition & 1 deletion src/plsnipals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function plsnipals!(X::Matrix, Y::Matrix, weights::Weight; kwargs...)
zp ./= tt
mul!(c, Y', dt)
c ./= tt
# deflation with respect to t (asymetric PLS)
# deflation with respect to t: asymetric PLS
X .-= t * zp'
Y .-= t * c'
# end
Expand Down
4 changes: 2 additions & 2 deletions src/plsrosa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function plsrosa!(X::Matrix, Y::Matrix, weights::Weight; kwargs...)
c = similar(X, q)
# End
@inbounds for a = 1:nlv
XtY .= X' * (D * Y)
XtY .= X' * D * Y
if q == 1
w .= vec(XtY)
w ./= normv(w)
Expand All @@ -83,7 +83,7 @@ function plsrosa!(X::Matrix, Y::Matrix, weights::Weight; kwargs...)
t .= t .- z * inv(z' * D * z) * z' * (D * t)
z = vcol(W, 1:(a - 1))
w = w .- z * (z' * w)
w ./= sqrt(dot(w, w))
w ./= normv(w)
end
dt .= weights.w .* t
tt = dot(t, dt)
Expand Down
2 changes: 1 addition & 1 deletion src/plsrout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ function plsrout!(X::Matrix, Y::Matrix, weights::Weight; kwargs...)
w .*= wtal(d; a = quantile(d, 1 - par.prm))
w .*= weights.w
w[isequal.(w, 0)] .= 1e-10
plskern(X, Y, mweight(w); kwargs...)
plskern!(X, Y, mweight(w); kwargs...)
end
5 changes: 3 additions & 2 deletions src/plssimp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ function plssimp!(X::Matrix, Y::Matrix, weights::Weight; kwargs...)
fcenter!(X, xmeans)
fcenter!(Y, ymeans)
end
D = Diagonal(weights.w)
XtY = X' * (D * Y)
## XtY
fweight!(Y, weights.w)
XtY = X' * Y
## Pre-allocation
T = similar(X, n, nlv)
W = similar(X, p, nlv)
Expand Down
14 changes: 7 additions & 7 deletions src/plswold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ function plswold!(X::Matrix, Y::Matrix, weights::Weight; kwargs...)
n, p = size(X)
q = nco(Y)
nlv = min(par.nlv, n, p)
sqrtw = sqrt.(weights.w)
xmeans = colmean(X, weights)
ymeans = colmean(Y, weights)
xscales = ones(Q, p)
Expand All @@ -60,8 +59,9 @@ function plswold!(X::Matrix, Y::Matrix, weights::Weight; kwargs...)
fcenter!(Y, ymeans)
end
# Row metric
X .= sqrtw .* X
Y .= sqrtw .* Y
sqrtw = sqrt.(weights.w)
fweight!(X, sqrtw)
fweight!(Y, sqrtw)
## Pre-allocation
Tx = similar(X, n, nlv)
Wx = similar(X, p, nlv)
Expand All @@ -76,9 +76,9 @@ function plswold!(X::Matrix, Y::Matrix, weights::Weight; kwargs...)
px = copy(wx)
niter = zeros(nlv)
# End
@inbounds for a = 1:nlv
tx .= X[:, 1]
ty .= Y[:, 1]
@inbounds for a = 1:nlv
tx .= vcol(X, 1)
ty .= vcol(Y, 1)
cont = true
iter = 1
wx .= rand(p)
Expand Down Expand Up @@ -110,7 +110,7 @@ function plswold!(X::Matrix, Y::Matrix, weights::Weight; kwargs...)
Wytild[:, a] .= wytild
TTx[a] = ttx
end
Tx .= (1 ./ sqrtw) .* Tx
fweight!(Tx, 1 ./ sqrtw)
Rx = Wx * inv(Px' * Wx)
Plsr(Tx, Px, Rx, Wx, Wytild, TTx, xmeans, xscales, ymeans, yscales, weights, niter, par)
end

0 comments on commit b6a426b

Please sign in to comment.