This commit is contained in:
Joakim Skogholt 2024-04-24 10:39:35 +02:00
parent 7ed335fa15
commit af4bbea81f

View file

@ -2,7 +2,7 @@
Solves the model update problem explicitly as a least squares problem with stacked matrices. Solves the model update problem explicitly as a least squares problem with stacked matrices.
In practice the most naive way of approaching the update problem In practice the most naive way of approaching the update problem
""" """
function TRLooCVUpdateNaive(X, y, lambdasu, bold) function TRLooCVUpdateNaive(X, y, lambdasu, bOld)
n, p = size(X); n, p = size(X);
rmsecvman = zeros(length(lambdasu)); rmsecvman = zeros(length(lambdasu));
@ -19,7 +19,7 @@ for i = 1:n
p2 = size(Xdata, 2); p2 = size(Xdata, 2);
for j = 1:length(lambdasu) for j = 1:length(lambdasu)
betas = [Xs; sqrt(lambdasu[j]) * I(p2)] \ [ys ; sqrt(lambdasu[j]) * bold]; betas = [Xs; sqrt(lambdasu[j]) * I(p2)] \ [ys ; sqrt(lambdasu[j]) * bOld];
rmsecvman[j] += (y[i] - (((X[i,:]' .- mX) * betas)[1] + my))^2; rmsecvman[j] += (y[i] - (((X[i,:]' .- mX) * betas)[1] + my))^2;
end end
end end
@ -33,7 +33,7 @@ end
Uses the 'svd-trick' for efficient calculation of regression coefficients, but does not use leverage corrections. Uses the 'svd-trick' for efficient calculation of regression coefficients, but does not use leverage corrections.
Hence regression coefficients are calculated for all lambda values Hence regression coefficients are calculated for all lambda values
""" """
function TRLooCVUpdateFair(X, y, lambdasu, bold) function TRLooCVUpdateFair(X, y, lambdasu, bOld)
n, p = size(X); n, p = size(X);
rmsecvman = zeros(length(lambdasu)) rmsecvman = zeros(length(lambdasu))
@ -53,7 +53,7 @@ for i = 1:n
denom2 = broadcast(.+, ones(n-1), broadcast(./, lambdasu', s.^2)) # denom2 = 1 + lambda/(s's) = (s's + lambda) / (s's) denom2 = broadcast(.+, ones(n-1), broadcast(./, lambdasu', s.^2)) # denom2 = 1 + lambda/(s's) = (s's + lambda) / (s's)
# Calculating regression coefficients and residual # Calculating regression coefficients and residual
bcoeffs = V * broadcast(./, (U' * ys), denom) .+ bold .- V * broadcast(./, V' * bold, denom2); bcoeffs = V * broadcast(./, (U' * ys), denom) .+ bOld .- V * broadcast(./, V' * bOld, denom2);
rmsecvman += ((y[i] .- ((X[i,:]' .- mX) * bcoeffs .+ my)).^2)'; rmsecvman += ((y[i] .- ((X[i,:]' .- mX) * bcoeffs .+ my)).^2)';
end end
@ -131,7 +131,7 @@ end
# Calculating rmsecv and regression coefficients # Calculating rmsecv and regression coefficients
press = sum(rescv.^2, dims=1)'; press = sum(rescv.^2, dims=1)';
rmsecv = sqrt.(1/n .* press); rmsecv = sqrt.(1/n .* press);
bcoeffs = V * broadcast(./, (U' * y), denom) .+ bold .- V * broadcast(./, V' * bold, denom2); bcoeffs = V * broadcast(./, (U' * y), denom) .+ bOld .- V * broadcast(./, V' * bOld, denom2);
bcoeffs = regMat \ bcoeffs; bcoeffs = regMat \ bcoeffs;
# Creating regression coefficients for uncentred data # Creating regression coefficients for uncentred data