Files
FYS-STK4155/doc/LectureNotes/chapter2.ipynb
T
2022-10-05 07:54:20 +02:00

149 KiB

Ridge and Lasso Regression

Mathematical Interpretation of Ordinary Least Squares

What is presented here is a mathematical analysis of various regression algorithms (ordinary least squares, Ridge and Lasso Regression). The analysis is based on an important algorithm in linear algebra, the so-called Singular Value Decomposition (SVD).

We have shown that in ordinary least squares (OLS) the optimal parameters \beta are given by


\hat{\boldsymbol{\beta}}_{\mathrm{OLS}} = \left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}.

The hat over \boldsymbol{\beta} means we have the optimal parameters after minimization of the cost function.

This means that our best model is defined as


\tilde{\boldsymbol{y}}=\boldsymbol{X}\hat{\boldsymbol{\beta}} = \boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}.

We now define a matrix


\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T.

We can rewrite


\tilde{\boldsymbol{y}}=\boldsymbol{X}\hat{\boldsymbol{\beta}} = \boldsymbol{A}\boldsymbol{y}.

The matrix \boldsymbol{A} has the important property that \boldsymbol{A}^2=\boldsymbol{A}. This is the definition of a projection matrix. We can then interpret our optimal model \tilde{\boldsymbol{y}} as being represented by an orthogonal projection of \boldsymbol{y} onto a space defined by the column vectors of \boldsymbol{X}. In our case here the matrix \boldsymbol{A} is a square matrix. If it is a general rectangular matrix we have an oblique projection matrix.

We have defined the residual error as


\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=\left[\boldsymbol{I}-\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\right]\boldsymbol{y}.

The residual errors are then the projections of \boldsymbol{y} onto the orthogonal component of the space defined by the column vectors of \boldsymbol{X}.

If the matrix \boldsymbol{X} is an orthogonal (or unitary in case of complex values) matrix, we have


\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{X}\boldsymbol{X}^T = \boldsymbol{I}.

In this case the matrix \boldsymbol{A} becomes


\boldsymbol{A}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T)=\boldsymbol{I},

and we have the obvious case


\boldsymbol{\epsilon}=\boldsymbol{y}-\tilde{\boldsymbol{y}}=0.

This serves also as a useful test of our codes.

The singular value decomposition

The examples we have looked at so far are cases where we normally can invert the matrix \boldsymbol{X}^T\boldsymbol{X}. Using a polynomial expansion where we fit of various functions leads to row vectors of the design matrix which are essentially orthogonal due to the polynomial character of our model. Obtaining the inverse of the design matrix is then often done via a so-called LU, QR or Cholesky decomposition.

As we will also see in the first project, this may however not the be case in general and a standard matrix inversion algorithm based on say LU, QR or Cholesky decomposition may lead to singularities. We will see examples of this below.

There is however a way to circumvent this problem and also gain some insights about the ordinary least squares approach, and later shrinkage methods like Ridge and Lasso regressions.

This is given by the Singular Value Decomposition (SVD) algorithm, perhaps the most powerful linear algebra algorithm. The SVD provides a numerically stable matrix decomposition that is used in a large swath of applications and the decomposition is always stable numerically.

In machine learning it plays a central role in dealing with for example design matrices that may be near singular or singular. Furthermore, as we will see here, the singular values can be related to the covariance matrix (and thereby the correlation matrix) and in turn the variance of a given quantity. It plays also an important role in the principal component analysis where high-dimensional data can be reduced to the statistically relevant features.

One of the typical problems we encounter with linear regression, in particular when the matrix \boldsymbol{X} (our so-called design matrix) is high-dimensional, are problems with near singular or singular matrices. The column vectors of \boldsymbol{X} may be linearly dependent, normally referred to as super-collinearity.
This means that the matrix may be rank deficient and it is basically impossible to model the data using linear regression. As an example, consider the matrix


\begin{align*}
\mathbf{X} & =  \left[
\begin{array}{rrr}
1 & -1 & 2
\\
1 & 0 & 1
\\
1 & 2  & -1
\\
1 & 1  & 0
\end{array} \right]
\end{align*}

The columns of \boldsymbol{X} are linearly dependent. We see this easily since the the first column is the row-wise sum of the other two columns. The rank (more correct, the column rank) of a matrix is the dimension of the space spanned by the column vectors. Hence, the rank of \mathbf{X} is equal to the number of linearly independent columns. In this particular case the matrix has rank 1.

Super-collinearity of an $(n \times p)$-dimensional design matrix \mathbf{X} implies that the inverse of the matrix \boldsymbol{X}^T\boldsymbol{X} (the matrix we need to invert to solve the linear regression equations) is non-invertible. If we have a square matrix that does not have an inverse, we say this matrix singular. The example here demonstrates this


\begin{align*}
\boldsymbol{X} & =  \left[
\begin{array}{rr}
1 & -1
\\
1 & -1
\end{array} \right].
\end{align*}

We see easily that \mbox{det}(\boldsymbol{X}) = x_{11} x_{22} - x_{12} x_{21} = 1 \times (-1) - 1 \times (-1) = 0. Hence, \mathbf{X} is singular and its inverse is undefined. This is equivalent to saying that the matrix \boldsymbol{X} has at least an eigenvalue which is zero.

If our design matrix \boldsymbol{X} which enters the linear regression problem


\begin{equation}
\boldsymbol{\beta}  =  (\boldsymbol{X}^{T} \boldsymbol{X})^{-1} \boldsymbol{X}^{T} \boldsymbol{y},
\label{_auto1} \tag{1}
\end{equation}

has linearly dependent column vectors, we will not be able to compute the inverse of \boldsymbol{X}^T\boldsymbol{X} and we cannot find the parameters (estimators) \beta_i. The estimators are only well-defined if (\boldsymbol{X}^{T}\boldsymbol{X}) can be inverted. This is more likely to happen when the matrix \boldsymbol{X} is high-dimensional. In this case it is likely to encounter a situation where the regression parameters \beta_i cannot be estimated.

A cheap ad hoc approach is simply to add a small diagonal component to the matrix to invert, that is we change


\boldsymbol{X}^{T} \boldsymbol{X} \rightarrow \boldsymbol{X}^{T} \boldsymbol{X}+\lambda \boldsymbol{I},

where \boldsymbol{I} is the identity matrix. When we discuss Ridge regression this is actually what we end up evaluating. The parameter \lambda is called a hyperparameter. More about this later.

Basic math of the SVD

From standard linear algebra we know that a square matrix \boldsymbol{X} can be diagonalized if and only if it is a so-called normal matrix, that is if \boldsymbol{X}\in {\mathbb{R}}^{n\times n} we have \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} or if \boldsymbol{X}\in {\mathbb{C}}^{n\times n} we have \boldsymbol{X}\boldsymbol{X}^{\dagger}=\boldsymbol{X}^{\dagger}\boldsymbol{X}. The matrix has then a set of eigenpairs


(\lambda_1,\boldsymbol{u}_1),\dots, (\lambda_n,\boldsymbol{u}_n),

and the eigenvalues are given by the diagonal matrix


\boldsymbol{\Sigma}=\mathrm{Diag}(\lambda_1, \dots,\lambda_n).

The matrix \boldsymbol{X} can be written in terms of an orthogonal/unitary transformation \boldsymbol{U}


\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,

with \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{I} or \boldsymbol{U}\boldsymbol{U}^{\dagger}=\boldsymbol{I}.

Not all square matrices are diagonalizable. A matrix like the one discussed above


\boldsymbol{X} = \begin{bmatrix} 
1&  -1 \\
1& -1\\
\end{bmatrix}

is not diagonalizable, it is a so-called defective matrix. It is easy to see that the condition \boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{X}^T\boldsymbol{X} is not fulfilled.

However, and this is the strength of the SVD algorithm, any general matrix \boldsymbol{X} can be decomposed in terms of a diagonal matrix and two orthogonal/unitary matrices. The Singular Value Decompostion (SVD) theorem states that a general m\times n matrix \boldsymbol{X} can be written in terms of a diagonal matrix \boldsymbol{\Sigma} of dimensionality m\times n and two orthognal matrices \boldsymbol{U} and \boldsymbol{V}, where the first has dimensionality m \times m and the last dimensionality n\times n. We have then


\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T

As an example, the above defective matrix can be decomposed as


\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix}  1&  1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix}  2&  0 \\ 0& 0\\ \end{bmatrix}    \frac{1}{\sqrt{2}}\begin{bmatrix}  1&  -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,

with eigenvalues \sigma_1=2 and \sigma_2=0. The SVD exits always!

The SVD decomposition (singular values) gives eigenvalues \sigma_i\geq\sigma_{i+1} for all i and for dimensions larger than i=p, the eigenvalues (singular values) are zero.

In the general case, where our design matrix \boldsymbol{X} has dimension n\times p, the matrix is thus decomposed into an n\times n orthogonal matrix \boldsymbol{U}, a p\times p orthogonal matrix \boldsymbol{V} and a diagonal matrix \boldsymbol{\Sigma} with r=\mathrm{min}(n,p) singular values \sigma_i\geq 0 on the main diagonal and zeros filling the rest of the matrix. There are at most p singular values assuming that n > p. In our regression examples for the nuclear masses and the equation of state this is indeed the case, while for the Ising model we have p > n. These are often cases that lead to near singular or singular matrices.

The columns of \boldsymbol{U} are called the left singular vectors while the columns of \boldsymbol{V} are the right singular vectors.

If we assume that n > p, then our matrix \boldsymbol{U} has dimension $n \times n$. The last n-p columns of \boldsymbol{U} become however irrelevant in our calculations since they are multiplied with the zeros in \boldsymbol{\Sigma}.

The economy-size decomposition removes extra rows or columns of zeros from the diagonal matrix of singular values, \boldsymbol{\Sigma}, along with the columns in either \boldsymbol{U} or \boldsymbol{V} that multiply those zeros in the expression. Removing these zeros and columns can improve execution time and reduce storage requirements without compromising the accuracy of the decomposition.

If n > p, we keep only the first p columns of \boldsymbol{U} and \boldsymbol{\Sigma} has dimension p\times p. If p > n, then only the first n columns of \boldsymbol{V} are computed and \boldsymbol{\Sigma} has dimension n\times n. The n=p case is obvious, we retain the full SVD. In general the economy-size SVD leads to less FLOPS and still conserving the desired accuracy.

Codes for the SVD

In [1]:
import numpy as np
# SVD inversion
def SVD(A):
    ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).
    SVD is numerically more stable than the inversion algorithms provided by
    numpy and scipy.linalg at the cost of being slower.
    '''
    U, S, VT = np.linalg.svd(A,full_matrices=True)
    print('test U')
    print( (np.transpose(U) @ U - U @np.transpose(U)))
    print('test VT')
    print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
    print(U)
    print(S)
    print(VT)

    D = np.zeros((len(U),len(VT)))
    for i in range(0,len(VT)):
        D[i,i]=S[i]
    return U @ D @ VT


X = np.array([ [1.0,-1.0], [1.0,-1.0]])
#X = np.array([[1, 2], [3, 4], [5, 6]])

print(X)
C = SVD(X)
# Print the difference between the original matrix and the SVD one
print(C-X)

The matrix \boldsymbol{X} has columns that are linearly dependent. The first column is the row-wise sum of the other two columns. The rank of a matrix (the column rank) is the dimension of space spanned by the column vectors. The rank of the matrix is the number of linearly independent columns, in this case just 2. We see this from the singular values when running the above code. Running the standard inversion algorithm for matrix inversion with \boldsymbol{X}^T\boldsymbol{X} results in the program terminating due to a singular matrix.

The U, S, and V matrices returned from the svd() function cannot be multiplied directly.

As you can see from the code, the S vector must be converted into a diagonal matrix. This may cause a problem as the size of the matrices do not fit the rules of matrix multiplication, where the number of columns in a matrix must match the number of rows in the subsequent matrix.

If you wish to include the zero singular values, you will need to resize the matrices and set up a diagonal matrix as done in the above example

Code for SVD and Inversion of Matrices

How do we use the SVD to invert a matrix \boldsymbol{X}^T\boldsymbol{X} which is singular or near singular? The simple answer is to use the linear algebra function for the pseudoinverse, that is

In [2]:
#Ainv = np.linlag.pinv(A)

Let us first look at a matrix which does not causes problems and write our own function where we just use the SVD.

In [3]:
import numpy as np
# SVD inversion
def SVDinv(A):
    ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).
    SVD is numerically more stable than the inversion algorithms provided by
    numpy and scipy.linalg at the cost of being slower.
    '''
    U, s, VT = np.linalg.svd(A)
    print('test U')
    print( (np.transpose(U) @ U - U @np.transpose(U)))
    print('test VT')
    print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))


    D = np.zeros((len(U),len(VT)))
    D = np.diag(s)
    UT = np.transpose(U); V = np.transpose(VT); invD = np.linalg.inv(D)
    return np.matmul(V,np.matmul(invD,UT))


# Non-singular square matrix
X = np.array( [ [1,2,3],[2,4,5],[3,5,6]])
print(X)
A = np.transpose(X) @ X
# Brute force inversion
B = np.linalg.pinv(A)  # here we could use np.linalg.inv(A), try it!
C = SVDinv(A)
print(np.abs(B-C))

Although our matrix to invert \boldsymbol{X}^T\boldsymbol{X} is a square matrix, our matrix may be singular.

The pseudoinverse is the generalization of the matrix inverse for square matrices to rectangular matrices where the number of rows and columns are not equal.

It is also called the the Moore-Penrose Inverse after two independent discoverers of the method or the Generalized Inverse. It is used for the calculation of the inverse for singular or near singular matrices and for rectangular matrices.

Using the SVD we can obtain the pseudoinverse (PI) of a matrix \boldsymbol{A} (labeled here as \boldsymbol{A}_{\mathrm{PI}}


\boldsymbol{A}_{\mathrm{PI}}= \boldsymbol{V}\boldsymbol{D}_{\mathrm{PI}}\boldsymbol{U}^T,

where \boldsymbol{D}_{\mathrm{PI}} can be calculated by creating a diagonal matrix from \boldsymbol{\Sigma} where we only keep the singular values (the non-zero values). The following code computes the pseudoinvers of the matrix based on the SVD.

In [4]:
import numpy as np
# SVD inversion
def SVDinv(A):
    U, s, VT = np.linalg.svd(A)
    # reciprocals of singular values of s
    d = 1.0 / s
    # create m x n D matrix
    D = np.zeros(A.shape)
    # populate D with n x n diagonal matrix
    D[:A.shape[1], :A.shape[1]] = np.diag(d)
    UT = np.transpose(U)
    V = np.transpose(VT)
    return np.matmul(V,np.matmul(D.T,UT))


A = np.array([ [0.3, 0.4], [0.5, 0.6], [0.7, 0.8],[0.9, 1.0]])
print(A)
# Brute force inversion of super-collinear matrix
B = np.linalg.pinv(A)
print(B)
# Compare our own algorithm with pinv
C = SVDinv(A)
print(np.abs(C-B))

As you can see from these examples, our own decomposition based on the SVD agrees the pseudoinverse algorithm provided by Numpy.

Mathematics of the SVD and implications

Let us take a closer look at the mathematics of the SVD and the various implications for machine learning studies.

Our starting point is our design matrix \boldsymbol{X} of dimension n\times p


\boldsymbol{X}=\begin{bmatrix}
x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\
x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\
x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\
\dots & \dots & \dots & \dots \dots & \dots \\
x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\
x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\
\end{bmatrix}.

We can SVD decompose our matrix as


\boldsymbol{X}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,

where \boldsymbol{U} is an orthogonal matrix of dimension n\times n, meaning that \boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{U}^T\boldsymbol{U}=\boldsymbol{I}_n. Here \boldsymbol{I}_n is the unit matrix of dimension n \times n.

Similarly, \boldsymbol{V} is an orthogonal matrix of dimension p\times p, meaning that \boldsymbol{V}\boldsymbol{V}^T=\boldsymbol{V}^T\boldsymbol{V}=\boldsymbol{I}_p. Here \boldsymbol{I}_p is the unit matrix of dimension p \times p.

Finally \boldsymbol{\Sigma} contains the singular values \sigma_i. This matrix has dimension n\times p and the singular values \sigma_i are all positive. The non-zero values are ordered in descending order, that is


\sigma_0 > \sigma_1 > \sigma_2 > \dots > \sigma_{p-1} > 0.

All values beyond p-1 are all zero.

As an example, consider the following 3\times 2 example for the matrix \boldsymbol{\Sigma}


\boldsymbol{\Sigma}=
\begin{bmatrix}
2& 0 \\
0 & 1 \\
0 & 0 \\
\end{bmatrix}

The singular values are \sigma_0=2 and \sigma_1=1. It is common to rewrite the matrix \boldsymbol{\Sigma} as


\boldsymbol{\Sigma}=
\begin{bmatrix}
\boldsymbol{\tilde{\Sigma}}\\
\boldsymbol{0}\\
\end{bmatrix},

where


\boldsymbol{\tilde{\Sigma}}=
\begin{bmatrix}
2& 0 \\
0 & 1 \\
\end{bmatrix},

contains only the singular values. Note also (and we will use this below) that


\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}=
\begin{bmatrix}
4& 0 \\
0 & 1 \\
\end{bmatrix},

which is a 2\times 2 matrix while


\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T=
\begin{bmatrix}
4& 0 & 0\\
0 & 1 & 0\\
0 & 0 & 0\\
\end{bmatrix},

is a 3\times 3 matrix. The last row and column of this last matrix contain only zeros. This will have important consequences for our SVD decomposition of the design matrix.

The matrix that may cause problems for us is \boldsymbol{X}^T\boldsymbol{X}. Using the SVD we can rewrite this matrix as


\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,

and using the orthogonality of the matrix \boldsymbol{U} we have


\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T.

We define \boldsymbol{\Sigma}^T\boldsymbol{\Sigma}=\tilde{\boldsymbol{\Sigma}}^2 which is a diagonal matrix containing only the singular values squared. It has dimensionality p \times p.

We can now insert the result for the matrix \boldsymbol{X}^T\boldsymbol{X} into our equation for ordinary least squares where


\tilde{y}_{\mathrm{OLS}}=\boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y},

and using our SVD decomposition of \boldsymbol{X} we have


\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\left(\boldsymbol{V}\tilde{\boldsymbol{\Sigma}}^{2}(\boldsymbol{V}^T\right)^{-1}\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{y},

which gives us, using the orthogonality of the matrices \boldsymbol{U} and \boldsymbol{V},,


\tilde{y}_{\mathrm{OLS}}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}=\sum_{i=0}^{p-1}\boldsymbol{u}_i\boldsymbol{u}^T_i\boldsymbol{y},

It means that the ordinary least square model (with the optimal parameters) \boldsymbol{\tilde{y}}, corresponds to an orthogonal transformation of the output (or target) vector \boldsymbol{y} by the vectors of the matrix \boldsymbol{U}.

Further properties (important for our analyses later)

Let us study again \boldsymbol{X}^T\boldsymbol{X} in terms of our SVD,


\boldsymbol{X}^T\boldsymbol{X}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T.

If we now multiply from the right with \boldsymbol{V} (using the orthogonality of \boldsymbol{V}) we get


\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{V}=\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}.

This means the vectors \boldsymbol{v}_i of the orthogonal matrix \boldsymbol{V} are the eigenvectors of the matrix \boldsymbol{X}^T\boldsymbol{X} with eigenvalues given by the singular values squared, that is


\left(\boldsymbol{X}^T\boldsymbol{X}\right)\boldsymbol{v}_i=\boldsymbol{v}_i\sigma_i^2.

Similarly, if we use the SVD decomposition for the matrix \boldsymbol{X}\boldsymbol{X}^T, we have


\boldsymbol{X}\boldsymbol{X}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T\boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T\boldsymbol{U}^T.

If we now multiply from the right with \boldsymbol{U} (using the orthogonality of \boldsymbol{U}) we get


\left(\boldsymbol{X}\boldsymbol{X}^T\right)\boldsymbol{U}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{\Sigma}^T.

This means the vectors \boldsymbol{u}_i of the orthogonal matrix \boldsymbol{U} are the eigenvectors of the matrix \boldsymbol{X}\boldsymbol{X}^T with eigenvalues given by the singular values squared, that is


\left(\boldsymbol{X}\boldsymbol{X}^T\right)\boldsymbol{u}_i=\boldsymbol{u}_i\sigma_i^2.

Important note: we have defined our design matrix \boldsymbol{X} to be an n\times p matrix. In most supervised learning cases we have that $n \ge p$, and quite often we have n >> p. For linear algebra based methods like ordinary least squares or Ridge regression, this leads to a matrix \boldsymbol{X}^T\boldsymbol{X} which is small and thereby easier to handle from a computational point of view (in terms of number of floating point operations).

In our lectures, the number of columns will always refer to the number of features in our data set, while the number of rows represents the number of data inputs. Note that in other texts you may find the opposite notation. This has consequences for the definition of for example the covariance matrix and its relation to the SVD.

Meet the Covariance Matrix

Before we move on to a discussion of Ridge and Lasso regression, we want to show an important example of the above.

We have already noted that the matrix \boldsymbol{X}^T\boldsymbol{X} in ordinary least squares is proportional to the second derivative of the cost function, that is we have


\frac{\partial^2 C(\boldsymbol{\beta})}{\partial \boldsymbol{\beta}^T\partial \boldsymbol{\beta}} =\frac{2}{n}\boldsymbol{X}^T\boldsymbol{X}.

This quantity defines what is called the Hessian matrix (the second derivative of the cost function we want to optimize).

The Hessian matrix plays an important role and is defined in this course as


\boldsymbol{H}=\boldsymbol{X}^T\boldsymbol{X}.

The Hessian matrix for ordinary least squares is also proportional to the covariance matrix. This means also that we can use the SVD to find the eigenvalues of the covariance matrix and the Hessian matrix in terms of the singular values. Let us develop these arguments, as they will play an important role in our machine learning studies.

Before we discuss the link between for example Ridge regression and the singular value decomposition, we need to remind ourselves about the definition of the covariance and the correlation function. These are quantities that play a central role in machine learning methods.

Suppose we have defined two vectors \hat{x} and \hat{y} with n elements each. The covariance matrix \boldsymbol{C} is defined as


\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\
                              \mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\
             \end{bmatrix},

where for example

Warning:
Output truncated. This notebook contains too many cells to display efficiently.