# Hat matrix for regularised linear regression

In the blog post here it states that the diagonal of the hat matrix

H = X inv(X'X) X'


for a linear regression can be used to quickly calculate the leave one out cross validation error of the regression fit. To check if this is this only true for an unregularised linear fit I used the Octave code below

clear all
lambda = input( 'Enter lambda: ' ) ;
% data
y = rand(20,1) ;
X = [ ones(20,1) rand(20,5) ] ;
w = ( inv( X' * X + lambda * eye(size(X'*X)) ) * X' ) * y ;
y_fit = X * w ;
H = X * inv( X' * X + lambda * eye(size(X'*X)) ) * X' ;
yh_fit = H * y ;
both_fits = [ y_fit yh_fit ]
plot( y , 'b', y_fit , 'r' , yh_fit , 'g' )


to calculate a regularised linear fit and using the calculation

H = X * inv( X' * X + lambda * I ) * X' ;


for the hat matrix H, where lambda is the regularisation coefficient and I the identity matrix. Given that the numerical results, i.e. the y_fit using the weights and the yh_fit using the hat matrix, are the same, can I safely assume that the LOOCV statistic for a regularised fit can be calculated in the same way as outlined in the above linked blog post.

$H=X(X^{T}X+\lambda I)^{-1}X^{T}$.
• Yes, but perhaps adding (L2-norm) or "Ridge regression") after "Tikhonov" in yr answer would help search engines reference yr perfectly fine answer better. Cheers. Commented Jun 8, 2018 at 20:23