Added TRSegCVUpdateNaive
This commit is contained in:
parent
c094f7b7e9
commit
f50a442350
2 changed files with 64 additions and 0 deletions
63
src/TR.jl
63
src/TR.jl
|
|
@ -462,3 +462,66 @@ R = Matrix(factorisation.R);
|
||||||
|
|
||||||
return Q, R
|
return Q, R
|
||||||
end
|
end
|
||||||
|
|
||||||
|
"""
|
||||||
|
"Manual" k-fold cv for solving the Ridge regression problem.
|
||||||
|
The LS problem is solved explicitly and no shortcuts are used.
|
||||||
|
"""
|
||||||
|
function TRSegCVNaive(X, y, lambdas, cvfolds)
|
||||||
|
|
||||||
|
n, p = size(X);
|
||||||
|
rmsecvman = zeros(length(lambdas));
|
||||||
|
nfolds = length(unique(cvfolds));
|
||||||
|
|
||||||
|
for j = 1:length(lambdas)
|
||||||
|
for i = 1:nfolds
|
||||||
|
inds = (cvfolds .== i);
|
||||||
|
Xdata = X[vec(.!inds),:];
|
||||||
|
ydata = y[vec(.!inds)];
|
||||||
|
|
||||||
|
mX = mean(Xdata, dims=1);
|
||||||
|
my = mean(ydata);
|
||||||
|
Xs = Xdata .- mX;
|
||||||
|
ys = ydata .- my;
|
||||||
|
|
||||||
|
betas = [Xs; sqrt(lambdas[j]) * I(p)] \ [ys; zeros(p,1)];
|
||||||
|
rmsecvman[j] += sum((y[vec(inds)] - ((X[vec(inds),:] .- mX) * betas .+ my)).^2);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
rmsecvman = sqrt.(1/n .* rmsecvman);
|
||||||
|
|
||||||
|
return rmsecvman
|
||||||
|
end
|
||||||
|
|
||||||
|
"""
|
||||||
|
"Manual" k-fold cv for solving the Ridge regression problem update.
|
||||||
|
The LS problem is solved explicitly and no shortcuts are used.
|
||||||
|
"""
|
||||||
|
function TRSegCVUpdateNaive(X, y, lambdas, cvfolds, bOld)
|
||||||
|
|
||||||
|
n, p = size(X);
|
||||||
|
rmsecvman = zeros(length(lambdas));
|
||||||
|
nfolds = length(unique(cvfolds));
|
||||||
|
|
||||||
|
for j = 1:length(lambdas)
|
||||||
|
for i = 1:nfolds
|
||||||
|
inds = (cvfolds .== i);
|
||||||
|
Xdata = X[vec(.!inds),:];
|
||||||
|
ydata = y[vec(.!inds)];
|
||||||
|
|
||||||
|
mX = mean(Xdata, dims=1);
|
||||||
|
my = mean(ydata);
|
||||||
|
Xs = Xdata .- mX;
|
||||||
|
ys = ydata .- my;
|
||||||
|
|
||||||
|
betas = [Xs; sqrt(lambdas[j]) * I(p)] \ [ys; sqrt(lambdas[j]) * bOld];
|
||||||
|
rmsecvman[j] += sum((y[vec(inds)] - ((X[vec(inds),:] .- mX) * betas .+ my)).^2);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
rmsecvman = sqrt.(1/n .* rmsecvman);
|
||||||
|
|
||||||
|
return rmsecvman
|
||||||
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ export TRSegCVUpdate
|
||||||
export plegendre
|
export plegendre
|
||||||
export TRLooCVUpdateFair
|
export TRLooCVUpdateFair
|
||||||
export TRLooCVUpdateNaive
|
export TRLooCVUpdateNaive
|
||||||
|
export TRSegCVUpdateNaive
|
||||||
|
|
||||||
include("convenience.jl")
|
include("convenience.jl")
|
||||||
include("TR.jl")
|
include("TR.jl")
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue