{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ridge and Lasso Regression\n", "\n", "\n", "\n", "## Mathematical Interpretation of Ordinary Least Squares\n", "\n", "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). \n", "\n", "\n", "We have shown that in ordinary least squares the optimal parameters $\\beta$ are given by" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **hat** over $\\boldsymbol{\\beta}$ means we have the optimal parameters after minimization of the cost function.\n", "\n", "This means that our best model is defined as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}=\\boldsymbol{X}\\hat{\\boldsymbol{\\beta}} = \\boldsymbol{X}\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now define a matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{A}=\\boldsymbol{X}\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can rewrite" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}=\\boldsymbol{X}\\hat{\\boldsymbol{\\beta}} = \\boldsymbol{A}\\boldsymbol{y}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The matrix $\\boldsymbol{A}$ has the important property that $\\boldsymbol{A}^2=\\boldsymbol{A}$. This is the definition of a projection matrix.\n", "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.\n", "\n", "\n", "\n", "\n", "We have defined the residual error as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\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}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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}$.\n", "\n", "\n", "If the matrix $\\boldsymbol{X}$ is an orthogonal (or unitary in case of complex values) matrix, we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=\\boldsymbol{X}\\boldsymbol{X}^T = \\boldsymbol{I}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case the matrix $\\boldsymbol{A}$ becomes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{A}=\\boldsymbol{X}\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T)=\\boldsymbol{I},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and we have the obvious case" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\epsilon}=\\boldsymbol{y}-\\tilde{\\boldsymbol{y}}=0.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This serves also as a useful test of our codes. \n", "\n", "\n", "\n", "\n", "## The singular value decomposition\n", "\n", "\n", "The examples we have looked at so far are cases where we normally can\n", "invert the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$. Using a polynomial expansion where we fit of various functions leads to\n", "row vectors of the design matrix which are essentially orthogonal due\n", "to the polynomial character of our model. Obtaining the inverse of the\n", "design matrix is then often done via a so-called LU, QR or Cholesky\n", "decomposition.\n", "\n", "\n", "As we will also see in the first project, \n", "this may\n", "however not the be case in general and a standard matrix inversion\n", "algorithm based on say LU, QR or Cholesky decomposition may lead to singularities. We will see examples of this below.\n", "\n", "There is however a way to circumvent this problem and also\n", "gain some insights about the ordinary least squares approach, and\n", "later shrinkage methods like Ridge and Lasso regressions.\n", "\n", "This is given by the **Singular Value Decomposition** (SVD) algorithm,\n", "perhaps the most powerful linear algebra algorithm. The SVD provides\n", "a numerically stable matrix decomposition that is used in a large\n", "swath oc applications and the decomposition is always stable\n", "numerically.\n", "\n", "In machine learning it plays a central role in dealing with for\n", "example design matrices that may be near singular or singular.\n", "Furthermore, as we will see here, the singular values can be related\n", "to the covariance matrix (and thereby the correlation matrix) and in\n", "turn the variance of a given quantity. It plays also an important role\n", "in the principal component analysis where high-dimensional data can be\n", "reduced to the statistically relevant features.\n", "\n", "\n", "\n", "\n", "One of the typical problems we encounter with linear regression, in particular \n", "when the matrix $\\boldsymbol{X}$ (our so-called design matrix) is high-dimensional, \n", "are problems with near singular or singular matrices. The column vectors of $\\boldsymbol{X}$ \n", "may be linearly dependent, normally referred to as super-collinearity. \n", "This means that the matrix may be rank deficient and it is basically impossible to \n", "to model the data using linear regression. As an example, consider the matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\begin{align*}\n", "\\mathbf{X} & = \\left[\n", "\\begin{array}{rrr}\n", "1 & -1 & 2\n", "\\\\\n", "1 & 0 & 1\n", "\\\\\n", "1 & 2 & -1\n", "\\\\\n", "1 & 1 & 0\n", "\\end{array} \\right]\n", "\\end{align*}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The columns of $\\boldsymbol{X}$ are linearly dependent. We see this easily since the \n", "the first column is the row-wise sum of the other two columns. The rank (more correct,\n", "the column rank) of a matrix is the dimension of the space spanned by the\n", "column vectors. Hence, the rank of $\\mathbf{X}$ is equal to the number\n", "of linearly independent columns. In this particular case the matrix has rank 2.\n", "\n", "Super-collinearity of an $(n \\times p)$-dimensional design matrix $\\mathbf{X}$ implies\n", "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\begin{align*}\n", "\\boldsymbol{X} & = \\left[\n", "\\begin{array}{rr}\n", "1 & -1\n", "\\\\\n", "1 & -1\n", "\\end{array} \\right].\n", "\\end{align*}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "This is equivalent to saying that the matrix $\\boldsymbol{X}$ has at least an eigenvalue which is zero.\n", "\n", "\n", "\n", "If our design matrix $\\boldsymbol{X}$ which enters the linear regression problem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "\\boldsymbol{\\beta} = (\\boldsymbol{X}^{T} \\boldsymbol{X})^{-1} \\boldsymbol{X}^{T} \\boldsymbol{y},\n", "\\label{_auto1} \\tag{1}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "has linearly dependent column vectors, we will not be able to compute the inverse\n", "of $\\boldsymbol{X}^T\\boldsymbol{X}$ and we cannot find the parameters (estimators) $\\beta_i$. \n", "The estimators are only well-defined if $(\\boldsymbol{X}^{T}\\boldsymbol{X})^{-1}$ exits. \n", "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 \n", "the regression parameters $\\beta_i$ cannot be estimated.\n", "\n", "A cheap *ad hoc* approach is simply to add a small diagonal component to the matrix to invert, that is we change" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^{T} \\boldsymbol{X} \\rightarrow \\boldsymbol{X}^{T} \\boldsymbol{X}+\\lambda \\boldsymbol{I},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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. \n", "\n", "\n", "\n", "\n", "## Basic math of the SVD\n", "\n", "\n", "From standard linear algebra we know that a square matrix $\\boldsymbol{X}$ can be diagonalized if and only it is \n", "a so-called [normal matrix](https://en.wikipedia.org/wiki/Normal_matrix), that is if $\\boldsymbol{X}\\in {\\mathbb{R}}^{n\\times n}$\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}$.\n", "The matrix has then a set of eigenpairs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "(\\lambda_1,\\boldsymbol{u}_1),\\dots, (\\lambda_n,\\boldsymbol{u}_n),\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and the eigenvalues are given by the diagonal matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\Sigma}=\\mathrm{Diag}(\\lambda_1, \\dots,\\lambda_n).\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The matrix $\\boldsymbol{X}$ can be written in terms of an orthogonal/unitary transformation $\\boldsymbol{U}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X} = \\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with $\\boldsymbol{U}\\boldsymbol{U}^T=\\boldsymbol{I}$ or $\\boldsymbol{U}\\boldsymbol{U}^{\\dagger}=\\boldsymbol{I}$.\n", "\n", "Not all square matrices are diagonalizable. A matrix like the one discussed above" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X} = \\begin{bmatrix} \n", "1& -1 \\\\\n", "1& -1\\\\\n", "\\end{bmatrix}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is not diagonalizable, it is a so-called [defective matrix](https://en.wikipedia.org/wiki/Defective_matrix). It is easy to see that the condition\n", "$\\boldsymbol{X}\\boldsymbol{X}^T=\\boldsymbol{X}^T\\boldsymbol{X}$ is not fulfilled. \n", "\n", "\n", "\n", "\n", "\n", "However, and this is the strength of the SVD algorithm, any general\n", "matrix $\\boldsymbol{X}$ can be decomposed in terms of a diagonal matrix and\n", "two orthogonal/unitary matrices. The [Singular Value Decompostion\n", "(SVD) theorem](https://en.wikipedia.org/wiki/Singular_value_decomposition)\n", "states that a general $m\\times n$ matrix $\\boldsymbol{X}$ can be written in\n", "terms of a diagonal matrix $\\boldsymbol{\\Sigma}$ of dimensionality $m\\times n$\n", "and two orthognal matrices $\\boldsymbol{U}$ and $\\boldsymbol{V}$, where the first has\n", "dimensionality $m \\times m$ and the last dimensionality $n\\times n$.\n", "We have then" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X} = \\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As an example, the above defective matrix can be decomposed as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\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,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with eigenvalues $\\sigma_1=2$ and $\\sigma_2=0$. \n", "The SVD exits always! \n", "\n", "The SVD\n", "decomposition (singular values) gives eigenvalues \n", "$\\sigma_i\\geq\\sigma_{i+1}$ for all $i$ and for dimensions larger than $i=p$, the\n", "eigenvalues (singular values) are zero.\n", "\n", "In the general case, where our design matrix $\\boldsymbol{X}$ has dimension\n", "$n\\times p$, the matrix is thus decomposed into an $n\\times n$\n", "orthogonal matrix $\\boldsymbol{U}$, a $p\\times p$ orthogonal matrix $\\boldsymbol{V}$\n", "and a diagonal matrix $\\boldsymbol{\\Sigma}$ with $r=\\mathrm{min}(n,p)$\n", "singular values $\\sigma_i\\geq 0$ on the main diagonal and zeros filling\n", "the rest of the matrix. There are at most $p$ singular values\n", "assuming that $n > p$. In our regression examples for the nuclear\n", "masses and the equation of state this is indeed the case, while for\n", "the Ising model we have $p > n$. These are often cases that lead to\n", "near singular or singular matrices.\n", "\n", "The columns of $\\boldsymbol{U}$ are called the left singular vectors while the columns of $\\boldsymbol{V}$ are the right singular vectors.\n", "\n", "\n", "If we assume that $n > p$, then our matrix $\\boldsymbol{U}$ has dimension $n\n", "\\times n$. The last $n-p$ columns of $\\boldsymbol{U}$ become however\n", "irrelevant in our calculations since they are multiplied with the\n", "zeros in $\\boldsymbol{\\Sigma}$.\n", "\n", "The economy-size decomposition removes extra rows or columns of zeros\n", "from the diagonal matrix of singular values, $\\boldsymbol{\\Sigma}$, along with the columns\n", "in either $\\boldsymbol{U}$ or $\\boldsymbol{V}$ that multiply those zeros in the expression. \n", "Removing these zeros and columns can improve execution time\n", "and reduce storage requirements without compromising the accuracy of\n", "the decomposition.\n", "\n", "If $n > p$, we keep only the first $p$ columns of $\\boldsymbol{U}$ and $\\boldsymbol{\\Sigma}$ has dimension $p\\times p$. \n", "If $p > n$, then only the first $n$ columns of $\\boldsymbol{V}$ are computed and $\\boldsymbol{\\Sigma}$ has dimension $n\\times n$.\n", "The $n=p$ case is obvious, we retain the full SVD. \n", "In general the economy-size SVD leads to less FLOPS and still conserving the desired accuracy.\n", "\n", "## Codes for the SVD" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1. -1.]\n", " [ 1. -1.]]\n", "test U\n", "[[0. 0.]\n", " [0. 0.]]\n", "test VT\n", "[[0. 0.]\n", " [0. 0.]]\n", "[[-0.70710678 -0.70710678]\n", " [-0.70710678 0.70710678]]\n", "[2. 0.]\n", "[[-0.70710678 0.70710678]\n", " [ 0.70710678 0.70710678]]\n", "[[-3.33066907e-16 4.44089210e-16]\n", " [ 0.00000000e+00 2.22044605e-16]]\n" ] } ], "source": [ "import numpy as np\n", "# SVD inversion\n", "def SVD(A):\n", " ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).\n", " SVD is numerically more stable than the inversion algorithms provided by\n", " numpy and scipy.linalg at the cost of being slower.\n", " '''\n", " U, S, VT = np.linalg.svd(A,full_matrices=True)\n", " print('test U')\n", " print( (np.transpose(U) @ U - U @np.transpose(U)))\n", " print('test VT')\n", " print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))\n", " print(U)\n", " print(S)\n", " print(VT)\n", "\n", " D = np.zeros((len(U),len(VT)))\n", " for i in range(0,len(VT)):\n", " D[i,i]=S[i]\n", " return U @ D @ VT\n", "\n", "\n", "X = np.array([ [1.0,-1.0], [1.0,-1.0]])\n", "#X = np.array([[1, 2], [3, 4], [5, 6]])\n", "\n", "print(X)\n", "C = SVD(X)\n", "# Print the difference between the original matrix and the SVD one\n", "print(C-X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The matrix $\\boldsymbol{X}$ has columns that are linearly dependent. The first\n", "column is the row-wise sum of the other two columns. The rank of a\n", "matrix (the column rank) is the dimension of space spanned by the\n", "column vectors. The rank of the matrix is the number of linearly\n", "independent columns, in this case just $2$. We see this from the\n", "singular values when running the above code. Running the standard\n", "inversion algorithm for matrix inversion with $\\boldsymbol{X}^T\\boldsymbol{X}$ results\n", "in the program terminating due to a singular matrix.\n", "\n", "\n", "\n", "The $U$, $S$, and $V$ matrices returned from the **svd()** function\n", "cannot be multiplied directly.\n", "\n", "As you can see from the code, the $S$ vector must be converted into a\n", "diagonal matrix. This may cause a problem as the size of the matrices\n", "do not fit the rules of matrix multiplication, where the number of\n", "columns in a matrix must match the number of rows in the subsequent\n", "matrix.\n", "\n", "If you wish to include the zero singular values, you will need to\n", "resize the matrices and set up a diagonal matrix as done in the above\n", "example\n", "\n", "\n", "\n", "\n", "\n", "## Mathematics of the SVD and implications\n", "\n", "Let us take a closer look at the mathematics of the SVD and the various implications for machine learning studies.\n", "\n", "Our starting point is our design matrix $\\boldsymbol{X}$ of dimension $n\\times p$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}=\\begin{bmatrix}\n", "x_{0,0} & x_{0,1} & x_{0,2}& \\dots & \\dots x_{0,p-1}\\\\\n", "x_{1,0} & x_{1,1} & x_{1,2}& \\dots & \\dots x_{1,p-1}\\\\\n", "x_{2,0} & x_{2,1} & x_{2,2}& \\dots & \\dots x_{2,p-1}\\\\\n", "\\dots & \\dots & \\dots & \\dots \\dots & \\dots \\\\\n", "x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \\dots & \\dots x_{n-2,p-1}\\\\\n", "x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \\dots & \\dots x_{n-1,p-1}\\\\\n", "\\end{bmatrix}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can SVD decompose our matrix as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}=\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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$.\n", "\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}_n$ is the unit matrix of dimension $p \\times p$.\n", "\n", "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\sigma_0 > \\sigma_1 > \\sigma_2 > \\dots > \\sigma_{p-1} > 0.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All values beyond $p-1$ are all zero.\n", "\n", "\n", "As an example, consider the following $3\\times 2$ example for the matrix $\\boldsymbol{\\Sigma}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\Sigma}=\n", "\\begin{bmatrix}\n", "2& 0 \\\\\n", "0 & 1 \\\\\n", "0 & 0 \\\\\n", "\\end{bmatrix}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The singular values are $\\sigma_0=2$ and $\\sigma_1=1$. It is common to rewrite the matrix $\\boldsymbol{\\Sigma}$ as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\Sigma}=\n", "\\begin{bmatrix}\n", "\\boldsymbol{\\tilde{\\Sigma}}\\\\\n", "\\boldsymbol{0}\\\\\n", "\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\tilde{\\Sigma}}=\n", "\\begin{bmatrix}\n", "2& 0 \\\\\n", "0 & 1 \\\\\n", "\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "contains only the singular values. Note also (and we will use this below) that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma}=\n", "\\begin{bmatrix}\n", "4& 0 \\\\\n", "0 & 1 \\\\\n", "\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which is a $2\\times 2 $ matrix while" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\Sigma}\\boldsymbol{\\Sigma}^T=\n", "\\begin{bmatrix}\n", "4& 0 & 0\\\\\n", "0 & 1 & 0\\\\\n", "0 & 0 & 0\\\\\n", "\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is a $3\\times 3 $ matrix. The last row and column of this last matrix\n", "contain only zeros. This will have important consequences for our SVD\n", "decomposition of the design matrix.\n", "\n", "\n", "\n", "The matrix that may cause problems for us is $\\boldsymbol{X}^T\\boldsymbol{X}$. Using the SVD we can rewrite this matrix as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{U}^T\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and using the orthogonality of the matrix $\\boldsymbol{U}$ we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma}\\boldsymbol{V}^T.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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$.\n", "\n", "This means, using the orthogonality of $\\boldsymbol{V}$, that we get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=\\tilde{\\boldsymbol{\\Sigma}}^2.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now insert the result for the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$ into our equation for ordinary least squares where" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{y}_{\\mathrm{OLS}}=\\boldsymbol{X}\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and using our SVD decomposition of $\\boldsymbol{X}$ we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{y}_{\\mathrm{OLS}}=\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T\\tilde{\\boldsymbol{\\Sigma}}^{-2}\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{U}^T\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which gives us, using the orthogonality of the matrices $\\boldsymbol{U}$ and $\\boldsymbol{V}$," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{y}_{\\mathrm{OLS}}=\\boldsymbol{U}\\boldsymbol{U}^T\\boldsymbol{y}=\\sum_{i=0}^{p-1}\\boldsymbol{u}_i\\boldsymbol{u}^T_j\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note here that when we perform the multiplication of the various matrices, the orthogonal vectors of the matrix $\\boldsymbol{U}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{U}=[\\boldsymbol{u}_0,\\boldsymbol{u}_1,\\dots,\\boldsymbol{u}_{n-1}],\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "that belong to $i>p-1$, result in only zeros when we perform the multiplications. This means that the sum above has non-zero elements only up to $i=p-1$. This corresponds also to the number of singular values (these are all non-zero).\n", "\n", "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}$.\n", "\n", "\n", "## Further properties (important for our analyses later)\n", "\n", "Let us study again $\\boldsymbol{X}^T\\boldsymbol{X}$ in terms of our SVD," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\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.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we now multiply from the right with $\\boldsymbol{V}$ (using the orthogonality of $\\boldsymbol{V}$) we get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{V}=\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This means the vectors $\\boldsymbol{v}_i$ of the orthogonal matrix $\\boldsymbol{V}$ are the eigenvectors of the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$\n", "with eigenvalues given by the singular values squared, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{v}_i=\\boldsymbol{v}_i\\sigma_i^2.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, if we use the SVD decomposition for the matrix $\\boldsymbol{X}\\boldsymbol{X}^T$, we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\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.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we now multiply from the right with $\\boldsymbol{U}$ (using the orthogonality of $\\boldsymbol{U}$) we get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\left(\\boldsymbol{X}\\boldsymbol{X}^T\\right)\\boldsymbol{U}=\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{\\Sigma}^T.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This means the vectors $\\boldsymbol{u}_i$ of the orthogonal matrix $\\boldsymbol{U}$ are the eigenvectors of the matrix $\\boldsymbol{X}\\boldsymbol{X}^T$\n", "with eigenvalues given by the singular values squared, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\left(\\boldsymbol{X}\\boldsymbol{X}^T\\right)\\boldsymbol{u}_i=\\boldsymbol{u}_i\\sigma_i^2.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Important note**: we have defined our design matrix $\\boldsymbol{X}$ to be an\n", "$n\\times p$ matrix. In most supervised learning cases we have that $n\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).\n", "\n", "In our lectures, the number of columns will\n", "always refer to the number of features in our data set, while the\n", "number of rows represents the number of data inputs. Note that in\n", "other texts you may find the opposite notation. This has consequences\n", "for the definition of for example the covariance matrix and its relation to the SVD.\n", "\n", "\n", "## Meet the Covariance Matrix\n", "\n", "\n", "Before we move on to a discussion of Ridge and Lasso regression, we want to show an important example of the above.\n", "\n", "We have already noted that the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$ in ordinary\n", "least squares is proportional to the second derivative of the cost\n", "function, that is we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{\\partial^2 C(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}^T\\partial \\boldsymbol{\\beta}} =\\frac{2}{n}\\boldsymbol{X}^T\\boldsymbol{X}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This quantity defines was what is called the Hessian matrix (the second derivative of a function we want to optimize).\n", "\n", "The Hessian matrix plays an important role and is defined in this course as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{H}=\\boldsymbol{X}^T\\boldsymbol{X}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Hessian matrix for ordinary least squares is also proportional to\n", "the covariance matrix. This means also that we can use the SVD to find\n", "the eigenvalues of the covariance matrix and the Hessian matrix in\n", "terms of the singular values. Let us develop these arguments, as they will play an important role in our machine learning studies.\n", "\n", "\n", "\n", "Before we discuss the link between for example Ridge regression and the singular value decomposition, we need to remind ourselves about\n", "the definition of the covariance and the correlation function. These are quantities that play a central role in machine learning methods.\n", "\n", "Suppose we have defined two vectors\n", "$\\hat{x}$ and $\\hat{y}$ with $n$ elements each. The covariance matrix $\\boldsymbol{C}$ is defined as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{C}[\\boldsymbol{x},\\boldsymbol{y}] = \\begin{bmatrix} \\mathrm{cov}[\\boldsymbol{x},\\boldsymbol{x}] & \\mathrm{cov}[\\boldsymbol{x},\\boldsymbol{y}] \\\\\n", " \\mathrm{cov}[\\boldsymbol{y},\\boldsymbol{x}] & \\mathrm{cov}[\\boldsymbol{y},\\boldsymbol{y}] \\\\\n", " \\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where for example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\mathrm{cov}[\\boldsymbol{x},\\boldsymbol{y}] =\\frac{1}{n} \\sum_{i=0}^{n-1}(x_i- \\overline{x})(y_i- \\overline{y}).\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this definition and recalling that the variance is defined as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\mathrm{var}[\\boldsymbol{x}]=\\frac{1}{n} \\sum_{i=0}^{n-1}(x_i- \\overline{x})^2,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we can rewrite the covariance matrix as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{C}[\\boldsymbol{x},\\boldsymbol{y}] = \\begin{bmatrix} \\mathrm{var}[\\boldsymbol{x}] & \\mathrm{cov}[\\boldsymbol{x},\\boldsymbol{y}] \\\\\n", " \\mathrm{cov}[\\boldsymbol{x},\\boldsymbol{y}] & \\mathrm{var}[\\boldsymbol{y}] \\\\\n", " \\end{bmatrix}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note:** we have used $1/n$ in the above definitions of the *sample* variance and covariance. We assume then that we can calculate the exact mean value. \n", "What you will find in essentially all statistics texts are equations\n", "with a factor $1/(n-1)$. This is called [Bessel's correction](https://mathworld.wolfram.com/BesselsCorrection.html). This\n", "method corrects the bias in the estimation of the population variance\n", "and covariance. It also partially corrects the bias in the estimation\n", "of the population standard deviation. If you use a library like\n", "**Scikit-Learn** or **nunmpy's** function calculate the covariance, this\n", "quantity will be computed with a factor $1/(n-1)$.\n", "\n", "\n", "The covariance takes values between zero and infinity and may thus\n", "lead to problems with loss of numerical precision for particularly\n", "large values. It is common to scale the covariance matrix by\n", "introducing instead the correlation matrix defined via the so-called\n", "correlation function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\mathrm{corr}[\\boldsymbol{x},\\boldsymbol{y}]=\\frac{\\mathrm{cov}[\\boldsymbol{x},\\boldsymbol{y}]}{\\sqrt{\\mathrm{var}[\\boldsymbol{x}] \\mathrm{var}[\\boldsymbol{y}]}}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The correlation function is then given by values $\\mathrm{corr}[\\boldsymbol{x},\\boldsymbol{y}]\n", "\\in [-1,1]$. This avoids eventual problems with too large values. We\n", "can then define the correlation matrix for the two vectors $\\boldsymbol{x}$\n", "and $\\boldsymbol{y}$ as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{K}[\\boldsymbol{x},\\boldsymbol{y}] = \\begin{bmatrix} 1 & \\mathrm{corr}[\\boldsymbol{x},\\boldsymbol{y}] \\\\\n", " \\mathrm{corr}[\\boldsymbol{y},\\boldsymbol{x}] & 1 \\\\\n", " \\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above example this is the function we constructed using **pandas**.\n", "\n", "\n", "\n", "In our derivation of the various regression algorithms like **Ordinary Least Squares** or **Ridge regression**\n", "we defined the design/feature matrix $\\boldsymbol{X}$ as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}=\\begin{bmatrix}\n", "x_{0,0} & x_{0,1} & x_{0,2}& \\dots & \\dots x_{0,p-1}\\\\\n", "x_{1,0} & x_{1,1} & x_{1,2}& \\dots & \\dots x_{1,p-1}\\\\\n", "x_{2,0} & x_{2,1} & x_{2,2}& \\dots & \\dots x_{2,p-1}\\\\\n", "\\dots & \\dots & \\dots & \\dots \\dots & \\dots \\\\\n", "x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \\dots & \\dots x_{n-2,p-1}\\\\\n", "x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \\dots & \\dots x_{n-1,p-1}\\\\\n", "\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with $\\boldsymbol{X}\\in {\\mathbb{R}}^{n\\times p}$, with the predictors/features $p$ refering to the column numbers and the\n", "entries $n$ being the row elements.\n", "We can rewrite the design/feature matrix in terms of its column vectors as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}=\\begin{bmatrix} \\boldsymbol{x}_0 & \\boldsymbol{x}_1 & \\boldsymbol{x}_2 & \\dots & \\dots & \\boldsymbol{x}_{p-1}\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with a given vector" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{x}_i^T = \\begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \\dots & \\dots x_{n-1,i}\\end{bmatrix}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With these definitions, we can now rewrite our $2\\times 2$\n", "correlation/covariance matrix in terms of a moe general design/feature\n", "matrix $\\boldsymbol{X}\\in {\\mathbb{R}}^{n\\times p}$. This leads to a $p\\times p$\n", "covariance matrix for the vectors $\\boldsymbol{x}_i$ with $i=0,1,\\dots,p-1$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{C}[\\boldsymbol{x}] = \\begin{bmatrix}\n", "\\mathrm{var}[\\boldsymbol{x}_0] & \\mathrm{cov}[\\boldsymbol{x}_0,\\boldsymbol{x}_1] & \\mathrm{cov}[\\boldsymbol{x}_0,\\boldsymbol{x}_2] & \\dots & \\dots & \\mathrm{cov}[\\boldsymbol{x}_0,\\boldsymbol{x}_{p-1}]\\\\\n", "\\mathrm{cov}[\\boldsymbol{x}_1,\\boldsymbol{x}_0] & \\mathrm{var}[\\boldsymbol{x}_1] & \\mathrm{cov}[\\boldsymbol{x}_1,\\boldsymbol{x}_2] & \\dots & \\dots & \\mathrm{cov}[\\boldsymbol{x}_1,\\boldsymbol{x}_{p-1}]\\\\\n", "\\mathrm{cov}[\\boldsymbol{x}_2,\\boldsymbol{x}_0] & \\mathrm{cov}[\\boldsymbol{x}_2,\\boldsymbol{x}_1] & \\mathrm{var}[\\boldsymbol{x}_2] & \\dots & \\dots & \\mathrm{cov}[\\boldsymbol{x}_2,\\boldsymbol{x}_{p-1}]\\\\\n", "\\dots & \\dots & \\dots & \\dots & \\dots & \\dots \\\\\n", "\\dots & \\dots & \\dots & \\dots & \\dots & \\dots \\\\\n", "\\mathrm{cov}[\\boldsymbol{x}_{p-1},\\boldsymbol{x}_0] & \\mathrm{cov}[\\boldsymbol{x}_{p-1},\\boldsymbol{x}_1] & \\mathrm{cov}[\\boldsymbol{x}_{p-1},\\boldsymbol{x}_{2}] & \\dots & \\dots & \\mathrm{var}[\\boldsymbol{x}_{p-1}]\\\\\n", "\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and the correlation matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{K}[\\boldsymbol{x}] = \\begin{bmatrix}\n", "1 & \\mathrm{corr}[\\boldsymbol{x}_0,\\boldsymbol{x}_1] & \\mathrm{corr}[\\boldsymbol{x}_0,\\boldsymbol{x}_2] & \\dots & \\dots & \\mathrm{corr}[\\boldsymbol{x}_0,\\boldsymbol{x}_{p-1}]\\\\\n", "\\mathrm{corr}[\\boldsymbol{x}_1,\\boldsymbol{x}_0] & 1 & \\mathrm{corr}[\\boldsymbol{x}_1,\\boldsymbol{x}_2] & \\dots & \\dots & \\mathrm{corr}[\\boldsymbol{x}_1,\\boldsymbol{x}_{p-1}]\\\\\n", "\\mathrm{corr}[\\boldsymbol{x}_2,\\boldsymbol{x}_0] & \\mathrm{corr}[\\boldsymbol{x}_2,\\boldsymbol{x}_1] & 1 & \\dots & \\dots & \\mathrm{corr}[\\boldsymbol{x}_2,\\boldsymbol{x}_{p-1}]\\\\\n", "\\dots & \\dots & \\dots & \\dots & \\dots & \\dots \\\\\n", "\\dots & \\dots & \\dots & \\dots & \\dots & \\dots \\\\\n", "\\mathrm{corr}[\\boldsymbol{x}_{p-1},\\boldsymbol{x}_0] & \\mathrm{corr}[\\boldsymbol{x}_{p-1},\\boldsymbol{x}_1] & \\mathrm{corr}[\\boldsymbol{x}_{p-1},\\boldsymbol{x}_{2}] & \\dots & \\dots & 1\\\\\n", "\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Numpy function **np.cov** calculates the covariance elements using\n", "the factor $1/(n-1)$ instead of $1/n$ since it assumes we do not have\n", "the exact mean values. The following simple function uses the\n", "**np.vstack** function which takes each vector of dimension $1\\times n$\n", "and produces a $2\\times n$ matrix $\\boldsymbol{W}$\n", "\n", "Note that this assumes you have the features as the rows, and the inputs as columns, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{W} = \\begin{bmatrix} x_0 & x_1 & x_2 & \\dots & x_{n-2} & x_{n-1} \\\\\n", " y_0 & y_1 & y_2 & \\dots & y_{n-2} & y_{n-1} \\\\\n", " \\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which in turn is converted into into the $2\\times 2$ covariance matrix\n", "$\\boldsymbol{C}$ via the Numpy function **np.cov()**. We note that we can also calculate\n", "the mean value of each set of samples $\\boldsymbol{x}$ etc using the Numpy\n", "function **np.mean(x)**. We can also extract the eigenvalues of the\n", "covariance matrix through the **np.linalg.eig()** function." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.033037772005753835\n", "3.7371165871823337\n", "[[ 1.22443803 3.75195757]\n", " [ 3.75195757 12.47441766]]\n" ] } ], "source": [ "# Importing various packages\n", "import numpy as np\n", "n = 100\n", "x = np.random.normal(size=n)\n", "print(np.mean(x))\n", "y = 4+3*x+np.random.normal(size=n)\n", "print(np.mean(y))\n", "W = np.vstack((x, y))\n", "C = np.cov(W)\n", "print(C)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The previous example can be converted into the correlation matrix by\n", "simply scaling the matrix elements with the variances. We should also\n", "subtract the mean values for each column. This leads to the following\n", "code which sets up the correlations matrix for the previous example in\n", "a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the $2\\times 2$ correlation matrix (since we have only two vectors)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.08665060086846632\n", "1.6796265324852733\n", "[[1. 0.6183694]\n", " [0.6183694 1. ]]\n" ] } ], "source": [ "import numpy as np\n", "n = 100\n", "# define two vectors \n", "x = np.random.random(size=n)\n", "y = 4+3*x+np.random.normal(size=n)\n", "#scaling the x and y vectors \n", "x = x - np.mean(x)\n", "y = y - np.mean(y)\n", "variance_x = np.sum(x@x)/n\n", "variance_y = np.sum(y@y)/n\n", "print(variance_x)\n", "print(variance_y)\n", "cov_xy = np.sum(x@y)/n\n", "cov_xx = np.sum(x@x)/n\n", "cov_yy = np.sum(y@y)/n\n", "C = np.zeros((2,2))\n", "C[0,0]= cov_xx/variance_x\n", "C[1,1]= cov_yy/variance_y\n", "C[0,1]= cov_xy/np.sqrt(variance_y*variance_x)\n", "C[1,0]= C[0,1]\n", "print(C)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that the matrix elements along the diagonal are one as they\n", "should be and that the matrix is symmetric. Furthermore, diagonalizing\n", "this matrix we easily see that it is a positive definite matrix.\n", "\n", "The above procedure with **numpy** can be made more compact if we use **pandas**.\n", "\n", "\n", "\n", "We whow here how we can set up the correlation matrix using **pandas**, as done in this simple code" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0.68002363 0.95517094]\n", " [-0.53545715 0.03652792]\n", " [ 1.33886902 4.84251485]\n", " [ 0.20375701 -0.16861772]\n", " [-2.04455272 -5.89742056]\n", " [ 1.17449733 2.3728892 ]\n", " [ 0.03019554 0.07581375]\n", " [ 0.60949193 1.95628168]\n", " [ 0.0530533 1.07839924]\n", " [-1.5098779 -5.2515593 ]]\n", " 0 1\n", "0 0.680024 0.955171\n", "1 -0.535457 0.036528\n", "2 1.338869 4.842515\n", "3 0.203757 -0.168618\n", "4 -2.044553 -5.897421\n", "5 1.174497 2.372889\n", "6 0.030196 0.075814\n", "7 0.609492 1.956282\n", "8 0.053053 1.078399\n", "9 -1.509878 -5.251559\n", " 0 1\n", "0 1.000000 0.959247\n", "1 0.959247 1.000000\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "n = 10\n", "x = np.random.normal(size=n)\n", "x = x - np.mean(x)\n", "y = 4+3*x+np.random.normal(size=n)\n", "y = y - np.mean(y)\n", "# Note that we transpose the matrix in order to stay with our ordering n x p\n", "X = (np.vstack((x, y))).T\n", "print(X)\n", "Xpd = pd.DataFrame(X)\n", "print(Xpd)\n", "correlation_matrix = Xpd.corr()\n", "print(correlation_matrix)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We expand this model to the Franke function discussed earlier." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0 1 2 3 4 5 6 7 \\\n", "0 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 \n", "1 0.0 0.073902 0.077303 0.072279 0.077386 0.082866 0.063526 0.068073 \n", "2 0.0 0.077303 0.081480 0.074665 0.080336 0.086465 0.064929 0.069872 \n", "3 0.0 0.072279 0.074665 0.075754 0.080381 0.085289 0.069635 0.074099 \n", "4 0.0 0.077386 0.080336 0.080381 0.085597 0.091159 0.073333 0.078279 \n", "5 0.0 0.082866 0.086465 0.085289 0.091159 0.097452 0.077212 0.082687 \n", "6 0.0 0.063526 0.064929 0.069635 0.073333 0.077212 0.066047 0.069859 \n", "7 0.0 0.068073 0.069872 0.074099 0.078279 0.082687 0.069859 0.074096 \n", "8 0.0 0.073035 0.075289 0.078937 0.083657 0.088660 0.073966 0.078674 \n", "9 0.0 0.078451 0.081227 0.084181 0.089504 0.095173 0.078388 0.083618 \n", "10 0.0 0.055085 0.055774 0.062327 0.065204 0.068185 0.060520 0.063670 \n", "11 0.0 0.058892 0.059865 0.066249 0.069510 0.072910 0.064000 0.067504 \n", "12 0.0 0.063055 0.064354 0.070517 0.074207 0.078076 0.067769 0.071667 \n", "13 0.0 0.067610 0.069282 0.075163 0.079333 0.083728 0.071855 0.076189 \n", "14 0.0 0.072598 0.074696 0.080223 0.084931 0.089916 0.076283 0.081103 \n", "\n", " 8 9 10 11 12 13 14 \n", "0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 \n", "1 0.073035 0.078451 0.055085 0.058892 0.063055 0.067610 0.072598 \n", "2 0.075289 0.081227 0.055774 0.059865 0.064354 0.069282 0.074696 \n", "3 0.078937 0.084181 0.062327 0.066249 0.070517 0.075163 0.080223 \n", "4 0.083657 0.089504 0.065204 0.069510 0.074207 0.079333 0.084931 \n", "5 0.088660 0.095173 0.068185 0.072910 0.078076 0.083728 0.089916 \n", "6 0.073966 0.078388 0.060520 0.064000 0.067769 0.071855 0.076283 \n", "7 0.078674 0.083618 0.063670 0.067504 0.071667 0.076189 0.081103 \n", "8 0.083775 0.089300 0.067043 0.071268 0.075865 0.080871 0.086325 \n", "9 0.089300 0.095472 0.070653 0.075309 0.080387 0.085928 0.091979 \n", "10 0.067043 0.070653 0.056484 0.059455 0.062659 0.066116 0.069848 \n", "11 0.071268 0.075309 0.059455 0.062731 0.066272 0.070103 0.074248 \n", "12 0.075865 0.080387 0.062659 0.066272 0.070188 0.074432 0.079037 \n", "13 0.080871 0.085928 0.066116 0.070103 0.074432 0.079137 0.084251 \n", "14 0.086325 0.091979 0.069848 0.074248 0.079037 0.084251 0.089931 \n" ] } ], "source": [ "# Common imports\n", "import numpy as np\n", "import pandas as pd\n", "\n", "\n", "def FrankeFunction(x,y):\n", "\tterm1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))\n", "\tterm2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))\n", "\tterm3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))\n", "\tterm4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)\n", "\treturn term1 + term2 + term3 + term4\n", "\n", "\n", "def create_X(x, y, n ):\n", "\tif len(x.shape) > 1:\n", "\t\tx = np.ravel(x)\n", "\t\ty = np.ravel(y)\n", "\n", "\tN = len(x)\n", "\tl = int((n+1)*(n+2)/2)\t\t# Number of elements in beta\n", "\tX = np.ones((N,l))\n", "\n", "\tfor i in range(1,n+1):\n", "\t\tq = int((i)*(i+1)/2)\n", "\t\tfor k in range(i+1):\n", "\t\t\tX[:,q+k] = (x**(i-k))*(y**k)\n", "\n", "\treturn X\n", "\n", "\n", "# Making meshgrid of datapoints and compute Franke's function\n", "n = 4\n", "N = 100\n", "x = np.sort(np.random.uniform(0, 1, N))\n", "y = np.sort(np.random.uniform(0, 1, N))\n", "z = FrankeFunction(x, y)\n", "X = create_X(x, y, n=n) \n", "\n", "Xpd = pd.DataFrame(X)\n", "# subtract the mean values and set up the covariance matrix\n", "Xpd = Xpd - Xpd.mean()\n", "covariance_matrix = Xpd.cov()\n", "print(covariance_matrix)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We note here that the covariance is zero for the first rows and\n", "columns since all matrix elements in the design matrix were set to one\n", "(we are fitting the function in terms of a polynomial of degree $n$).\n", "\n", "This means that the variance for these elements will be zero and will\n", "cause problems when we set up the correlation matrix. We can simply\n", "drop these elements and construct a correlation\n", "matrix without these elements. \n", "\n", "\n", "\n", "We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix $\\boldsymbol{X}$ as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{C}[\\boldsymbol{x}] = \\frac{1}{n}\\boldsymbol{X}^T\\boldsymbol{X}= \\mathbb{E}[\\boldsymbol{X}^T\\boldsymbol{X}].\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see this let us simply look at a design matrix $\\boldsymbol{X}\\in {\\mathbb{R}}^{2\\times 2}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}=\\begin{bmatrix}\n", "x_{00} & x_{01}\\\\\n", "x_{10} & x_{11}\\\\\n", "\\end{bmatrix}=\\begin{bmatrix}\n", "\\boldsymbol{x}_{0} & \\boldsymbol{x}_{1}\\\\\n", "\\end{bmatrix}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we then compute the expectation value (note the $1/n$ factor instead of $1/(n-1)$)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\mathbb{E}[\\boldsymbol{X}^T\\boldsymbol{X}] = \\frac{1}{n}\\boldsymbol{X}^T\\boldsymbol{X}=\\frac{1}{n}\\begin{bmatrix}\n", "x_{00}^2+x_{10}^2 & x_{00}x_{01}+x_{10}x_{11}\\\\\n", "x_{01}x_{00}+x_{11}x_{10} & x_{01}^2+x_{11}^2\\\\\n", "\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which is just" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{C}[\\boldsymbol{x}_0,\\boldsymbol{x}_1] = \\boldsymbol{C}[\\boldsymbol{x}]=\\begin{bmatrix} \\mathrm{var}[\\boldsymbol{x}_0] & \\mathrm{cov}[\\boldsymbol{x}_0,\\boldsymbol{x}_1] \\\\\n", " \\mathrm{cov}[\\boldsymbol{x}_1,\\boldsymbol{x}_0] & \\mathrm{var}[\\boldsymbol{x}_1] \\\\\n", " \\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where we wrote $$\\boldsymbol{C}[\\boldsymbol{x}_0,\\boldsymbol{x}_1] = \\boldsymbol{C}[\\boldsymbol{x}]$$ to indicate that this is the covariance of the vectors $\\boldsymbol{x}$ of the design/feature matrix $\\boldsymbol{X}$.\n", "\n", "It is easy to generalize this to a matrix $\\boldsymbol{X}\\in {\\mathbb{R}}^{n\\times p}$.\n", "\n", "\n", "\n", "## Linking with the SVD\n", "\n", "We saw earlier that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\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.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since the matrices here have dimension $p\\times p$, with $p$ corresponding to the singular values, we defined earlier the matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma} = \\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0}\\\\ \\end{bmatrix}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} \\\\ \\boldsymbol{0}\\\\ \\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where the tilde-matrix $\\tilde{\\boldsymbol{\\Sigma}}$ is a matrix of dimension $p\\times p$ containing only the singular values $\\sigma_i$, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{\\Sigma}}=\\begin{bmatrix} \\sigma_0 & 0 & 0 & \\dots & 0 & 0 \\\\\n", " 0 & \\sigma_1 & 0 & \\dots & 0 & 0 \\\\\n", "\t\t\t\t 0 & 0 & \\sigma_2 & \\dots & 0 & 0 \\\\\n", "\t\t\t\t 0 & 0 & 0 & \\dots & \\sigma_{p-2} & 0 \\\\\n", "\t\t\t\t 0 & 0 & 0 & \\dots & 0 & \\sigma_{p-1} \\\\\n", "\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "meaning we can write" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=\\boldsymbol{V}\\tilde{\\boldsymbol{\\Sigma}}^2\\boldsymbol{V}^T.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiplying from the right with $\\boldsymbol{V}$ (using the orthogonality of $\\boldsymbol{V}$) we get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{V}=\\boldsymbol{V}\\tilde{\\boldsymbol{\\Sigma}}^2.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This means the vectors $\\boldsymbol{v}_i$ of the orthogonal matrix $\\boldsymbol{V}$\n", "are the eigenvectors of the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$ with eigenvalues\n", "given by the singular values squared, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{v}_i=\\boldsymbol{v}_i\\sigma_i^2.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In other words, each non-zero singular value of $\\boldsymbol{X}$ is a positive\n", "square root of an eigenvalue of $\\boldsymbol{X}^T\\boldsymbol{X}$. It means also that\n", "the columns of $\\boldsymbol{V}$ are the eigenvectors of\n", "$\\boldsymbol{X}^T\\boldsymbol{X}$. Since we have ordered the singular values of\n", "$\\boldsymbol{X}$ in a descending order, it means that the column vectors\n", "$\\boldsymbol{v}_i$ are hierarchically ordered by how much correlation they\n", "encode from the columns of $\\boldsymbol{X}$. \n", "\n", "\n", "Note that these are also the eigenvectors and eigenvalues of the\n", "Hessian matrix.\n", "\n", "If we now recall the definition of the covariance matrix (not using\n", "Bessel's correction) we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{C}[\\boldsymbol{X}]=\\frac{1}{n}\\boldsymbol{X}^T\\boldsymbol{X},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "meaning that every squared non-singular value of $\\boldsymbol{X}$ divided by $n$ (\n", "the number of samples) are the eigenvalues of the covariance\n", "matrix. Every singular value of $\\boldsymbol{X}$ is thus a positive square\n", "root of an eigenvalue of $\\boldsymbol{X}^T\\boldsymbol{X}$. If the matrix $\\boldsymbol{X}$ is\n", "self-adjoint, the singular values of $\\boldsymbol{X}$ are equal to the\n", "absolute value of the eigenvalues of $\\boldsymbol{X}$.\n", "\n", "\n", "For $\\boldsymbol{X}\\boldsymbol{X}^T$ we found" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}\\boldsymbol{X}^T=\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{U}^T=\\boldsymbol{U}\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma}\\boldsymbol{U}^T.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since the matrices here have dimension $n\\times n$, we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\Sigma}\\boldsymbol{\\Sigma}^T = \\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} \\\\ \\boldsymbol{0}\\\\ \\end{bmatrix}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} \\boldsymbol{0}\\\\ \\end{bmatrix}=\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0} \\\\ \\boldsymbol{0} & \\boldsymbol{0}\\\\ \\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "leading to" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}\\boldsymbol{X}^T=\\boldsymbol{U}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0} \\\\ \\boldsymbol{0} & \\boldsymbol{0}\\\\ \\end{bmatrix}\\boldsymbol{U}^T.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiplying with $\\boldsymbol{U}$ from the right gives us the eigenvalue problem" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "(\\boldsymbol{X}\\boldsymbol{X}^T)\\boldsymbol{U}=\\boldsymbol{U}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0} \\\\ \\boldsymbol{0} & \\boldsymbol{0}\\\\ \\end{bmatrix}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It means that the eigenvalues of $\\boldsymbol{X}\\boldsymbol{X}^T$ are again given by\n", "the non-zero singular values plus now a series of zeros. The column\n", "vectors of $\\boldsymbol{U}$ are the eigenvectors of $\\boldsymbol{X}\\boldsymbol{X}^T$ and\n", "measure how much correlations are contained in the rows of $\\boldsymbol{X}$.\n", "\n", "Since we will mainly be interested in the correlations among the features\n", "of our data (the columns of $\\boldsymbol{X}$, the quantity of interest for us are the non-zero singular\n", "values and the column vectors of $\\boldsymbol{V}$.\n", "\n", "\n", "\n", "## Ridge and LASSO Regression\n", "\n", "Let us remind ourselves about the expression for the standard Mean Squared Error (MSE) which we used to define our cost function and the equations for the ordinary least squares (OLS) method, that is \n", "our optimization problem is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in {\\mathbb{R}}^{p}}}\\frac{1}{n}\\left\\{\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)^T\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)\\right\\}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or we can state it as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", "{\\mathbb{R}}^{p}}}\\frac{1}{n}\\sum_{i=0}^{n-1}\\left(y_i-\\tilde{y}_i\\right)^2=\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where we have used the definition of a norm-2 vector, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\vert\\vert \\boldsymbol{x}\\vert\\vert_2 = \\sqrt{\\sum_i x_i^2}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By minimizing the above equation with respect to the parameters\n", "$\\boldsymbol{\\beta}$ we could then obtain an analytical expression for the\n", "parameters $\\boldsymbol{\\beta}$. We can add a regularization parameter $\\lambda$ by\n", "defining a new cost function to be optimized, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", "{\\mathbb{R}}^{p}}}\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2+\\lambda\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_2^2\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which leads to the Ridge regression minimization problem where we\n", "require that $\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_2^2\\le t$, where $t$ is\n", "a finite number larger than zero. By defining" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2+\\lambda\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_1,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we have a new optimization equation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", "{\\mathbb{R}}^{p}}}\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2+\\lambda\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_1\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator. \n", "\n", "Here we have defined the norm-1 as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\vert\\vert \\boldsymbol{x}\\vert\\vert_1 = \\sum_i \\vert x_i\\vert.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the matrix-vector expression for Ridge regression and dropping the parameter $1/n$ in front of the standard means squared error equation, we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\left\\{(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\right\\}+\\lambda\\boldsymbol{\\beta}^T\\boldsymbol{\\beta},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and \n", "taking the derivatives with respect to $\\boldsymbol{\\beta}$ we obtain then\n", "a slightly modified matrix inversion problem which for finite values\n", "of $\\lambda$ does not suffer from singularity problems. We obtain\n", "the optimal parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{Ridge}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with $\\boldsymbol{I}$ being a $p\\times p$ identity matrix with the constraint that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\sum_{i=0}^{p-1} \\beta_i^2 \\leq t,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with $t$ a finite positive number. \n", "\n", "When we compare this with the ordinary least squares result we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{OLS}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which can lead to singular matrices. However, with the SVD, we can always compute the inverse of the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$.\n", "\n", "\n", "We see that Ridge regression is nothing but the standard OLS with a\n", "modified diagonal term added to $\\boldsymbol{X}^T\\boldsymbol{X}$. The consequences, in\n", "particular for our discussion of the bias-variance tradeoff are rather\n", "interesting. We will see that for specific values of $\\lambda$, we may\n", "even reduce the variance of the optimal parameters $\\boldsymbol{\\beta}$. These topics and other related ones, will be discussed after the more linear algebra oriented analysis here.\n", "\n", "Using our insights about the SVD of the design matrix $\\boldsymbol{X}$ \n", "We have already analyzed the OLS solutions in terms of the eigenvectors (the columns) of the right singular value matrix $\\boldsymbol{U}$ as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}_{\\mathrm{OLS}}=\\boldsymbol{X}\\boldsymbol{\\beta} =\\boldsymbol{U}\\boldsymbol{U}^T\\boldsymbol{y}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For Ridge regression this becomes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}_{\\mathrm{Ridge}}=\\boldsymbol{X}\\boldsymbol{\\beta}_{\\mathrm{Ridge}} = \\boldsymbol{U\\Sigma V^T}\\left(\\boldsymbol{V}\\boldsymbol{\\Sigma}^2\\boldsymbol{V}^T+\\lambda\\boldsymbol{I} \\right)^{-1}(\\boldsymbol{U\\Sigma V^T})^T\\boldsymbol{y}=\\sum_{j=0}^{p-1}\\boldsymbol{u}_j\\boldsymbol{u}_j^T\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda}\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with the vectors $\\boldsymbol{u}_j$ being the columns of $\\boldsymbol{U}$ from the SVD of the matrix $\\boldsymbol{X}$. \n", "\n", "\n", "Since $\\lambda \\geq 0$, it means that compared to OLS, we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda} \\leq 1.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ridge regression finds the coordinates of $\\boldsymbol{y}$ with respect to the\n", "orthonormal basis $\\boldsymbol{U}$, it then shrinks the coordinates by\n", "$\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda}$. Recall that the SVD has\n", "eigenvalues ordered in a descending way, that is $\\sigma_i \\geq\n", "\\sigma_{i+1}$.\n", "\n", "For small eigenvalues $\\sigma_i$ it means that their contributions become less important, a fact which can be used to reduce the number of degrees of freedom. More about this when we have covered the material on a statistical interpretation of various linear regression methods.\n", "\n", "\n", "\n", "For the sake of simplicity, let us assume that the design matrix is orthonormal, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=(\\boldsymbol{X}^T\\boldsymbol{X})^{-1} =\\boldsymbol{I}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case the standard OLS results in" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{OLS}} = \\boldsymbol{X}^T\\boldsymbol{y}=\\sum_{i=0}^{p-1}\\boldsymbol{u}_j\\boldsymbol{u}_j^T\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{Ridge}} = \\left(\\boldsymbol{I}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}=\\left(1+\\lambda\\right)^{-1}\\boldsymbol{\\beta}^{\\mathrm{OLS}},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "that is the Ridge estimator scales the OLS estimator by the inverse of a factor $1+\\lambda$, and\n", "the Ridge estimator converges to zero when the hyperparameter goes to\n", "infinity.\n", "\n", "We will come back to more interpreations after we have gone through some of the statistical analysis part. \n", "\n", "For more discussions of Ridge and Lasso regression, [Wessel van Wieringen's](https://arxiv.org/abs/1509.09169) article is highly recommended.\n", "Similarly, [Mehta et al's article](https://arxiv.org/abs/1803.08823) is also recommended.\n", "\n", "\n", "Using the matrix-vector expression for Lasso regression and dropping the parameter $1/n$ in front of the standard means squared error equation, we have the following **cost** function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\left\\{(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\right\\}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Taking the derivative with respect to $\\boldsymbol{\\beta}$ and recalling that the derivative of the absolute value is (we drop the boldfaced vector symbol for simplicty)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{d \\vert \\beta\\vert}{d \\boldsymbol{\\beta}}=\\mathrm{sgn}(\\boldsymbol{\\beta})=\\left\\{\\begin{array}{cc} 1 & \\beta > 0 \\\\ 0 & \\beta =0\\\\-1 & \\beta < 0, \\end{array}\\right.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we have that the derivative of the cost function is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{X},\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}}=-2\\boldsymbol{X}^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})+\\lambda sgn(\\boldsymbol{\\beta})=0,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and reordering we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}\\boldsymbol{\\beta})+\\lambda sgn(\\boldsymbol{\\beta})=2\\boldsymbol{X}^T(\\boldsymbol{y}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This equation does not lead to a nice analytical equation as in either Ridge regression or ordinary least squares. This equation can however be solved by using standard convex optimization algorithms using for example the Python package [CVXOPT](https://cvxopt.org/). We will discuss this later. \n", "\n", "## Code for SVD and Inversion of Matrices\n", "\n", "How do we use the SVD to invert a matrix $\\boldsymbol{X}^\\boldsymbol{X}$ which is singular or near singular?\n", "The simple answer is to use the linear algebra function for the pseudoinverse, that is" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "editable": true }, "outputs": [], "source": [ "#Ainv = np.linlag.pinv(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us first look at a matrix which does not causes problems and write our own function where we just use the SVD." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [2 4 5]\n", " [3 5 6]]\n", "test U\n", "[[ 2.22044605e-16 -7.77156117e-16 -5.55111512e-16]\n", " [-7.77156117e-16 0.00000000e+00 -1.11022302e-16]\n", " [-5.55111512e-16 -1.11022302e-16 0.00000000e+00]]\n", "test VT\n", "[[ 1.11022302e-16 -2.22044605e-16 1.38777878e-16]\n", " [-2.22044605e-16 -1.11022302e-16 -1.11022302e-16]\n", " [ 1.38777878e-16 -1.11022302e-16 0.00000000e+00]]\n", "[[2.35367281e-12 1.70885528e-12 3.20632410e-13]\n", " [2.17248441e-12 1.46016532e-12 2.00728323e-13]\n", " [6.95443703e-13 4.13891144e-13 2.13162821e-14]]\n" ] } ], "source": [ "import numpy as np\n", "# SVD inversion\n", "def SVDinv(A):\n", " ''' Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).\n", " SVD is numerically more stable than the inversion algorithms provided by\n", " numpy and scipy.linalg at the cost of being slower.\n", " '''\n", " U, s, VT = np.linalg.svd(A)\n", " print('test U')\n", " print( (np.transpose(U) @ U - U @np.transpose(U)))\n", " print('test VT')\n", " print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))\n", "\n", "\n", " D = np.zeros((len(U),len(VT)))\n", " D = np.diag(s)\n", " UT = np.transpose(U); V = np.transpose(VT); invD = np.linalg.inv(D)\n", " return np.matmul(V,np.matmul(invD,UT))\n", "\n", "\n", "#X = np.array([ [1.0, -1.0, 2.0], [1.0, 0.0, 1.0], [1.0, 2.0, -1.0], [1.0, 1.0, 0.0] ])\n", "# Non-singular square matrix\n", "X = np.array( [ [1,2,3],[2,4,5],[3,5,6]])\n", "print(X)\n", "A = np.transpose(X) @ X\n", "# Brute force inversion\n", "B = np.linalg.inv(A) # here we could use np.linalg.pinv(A)\n", "C = SVDinv(A)\n", "print(np.abs(B-C))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although our matrix to invert $\\boldsymbol{X}^T\\boldsymbol{X}$ is a square matrix, our matrix may be singular. \n", "\n", "The pseudoinverse is the generalization of the matrix inverse for square matrices to\n", "rectangular matrices where the number of rows and columns are not equal.\n", "\n", "It is also called the the Moore-Penrose Inverse after two independent discoverers of the method or the Generalized Inverse.\n", "It is used for the calculation of the inverse for singular or near singular matrices and for rectangular matrices.\n", "\n", "Using the SVD we can obtain the pseudoinverse of a matrix $\\boldsymbol{A}$ (labeled here as $\\boldsymbol{A}_{\\mathrm{PI}}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{A}_{\\mathrm{PI}}= \\boldsymbol{V}\\boldsymbol{D}_{\\mathrm{PI}}\\boldsymbol{U}^T,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.3 0.4]\n", " [0.5 0.6]\n", " [0.7 0.8]\n", " [0.9 1. ]]\n", "[[-13. -6. 1. 8. ]\n", " [ 11.5 5.5 -0.5 -6.5]]\n", "[[0. 0. 0. 0.]\n", " [0. 0. 0. 0.]]\n" ] } ], "source": [ "import numpy as np\n", "# SVD inversion\n", "def SVDinv(A):\n", " U, s, VT = np.linalg.svd(A)\n", " # reciprocals of singular values of s\n", " d = 1.0 / s\n", " # create m x n D matrix\n", " D = np.zeros(A.shape)\n", " # populate D with n x n diagonal matrix\n", " D[:A.shape[1], :A.shape[1]] = np.diag(d)\n", " UT = np.transpose(U)\n", " V = np.transpose(VT)\n", " return np.matmul(V,np.matmul(D.T,UT))\n", "\n", "\n", "A = np.array([ [0.3, 0.4], [0.5, 0.6], [0.7, 0.8],[0.9, 1.0]])\n", "print(A)\n", "# Brute force inversion of super-collinear matrix\n", "B = np.linalg.pinv(A)\n", "print(B)\n", "# Compare our own algorithm with pinv\n", "C = SVDinv(A)\n", "print(np.abs(C-B))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see from this example, our own decomposition based on the SVD agrees the pseudoinverse algorithm provided by **Numpy**.\n", "\n", "\n", "\n", "## Deriving the Ridge Regression Equations\n", "\n", "Let us remind ourselves about the expression for the standard Mean Squared Error (MSE) which we used to define our cost function and the equations for the ordinary least squares (OLS) method, that is \n", "our optimization problem is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in {\\mathbb{R}}^{p}}}\\frac{1}{n}\\left\\{\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)^T\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)\\right\\}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or we can state it as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", "{\\mathbb{R}}^{p}}}\\frac{1}{n}\\sum_{i=0}^{n-1}\\left(y_i-\\tilde{y}_i\\right)^2=\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where we have used the definition of a norm-2 vector, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\vert\\vert \\boldsymbol{x}\\vert\\vert_2 = \\sqrt{\\sum_i x_i^2}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By minimizing the above equation with respect to the parameters\n", "$\\boldsymbol{\\beta}$ we could then obtain an analytical expression for the\n", "parameters $\\boldsymbol{\\beta}$. We can add a regularization parameter $\\lambda$ by\n", "defining a new cost function to be optimized, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", "{\\mathbb{R}}^{p}}}\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2+\\lambda\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_2^2\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which leads to the Ridge regression minimization problem where we\n", "require that $\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_2^2\\le t$, where $t$ is\n", "a finite number larger than zero. By defining" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2+\\lambda\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_1,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we have a new optimization equation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", "{\\mathbb{R}}^{p}}}\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2+\\lambda\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_1\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator. \n", "\n", "Here we have defined the norm-1 as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\vert\\vert \\boldsymbol{x}\\vert\\vert_1 = \\sum_i \\vert x_i\\vert.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the matrix-vector expression for Ridge regression and dropping the parameter $1/n$ in front of the standard means squared error equation, we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\left\\{(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\right\\}+\\lambda\\boldsymbol{\\beta}^T\\boldsymbol{\\beta},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and \n", "taking the derivatives with respect to $\\boldsymbol{\\beta}$ we obtain then\n", "a slightly modified matrix inversion problem which for finite values\n", "of $\\lambda$ does not suffer from singularity problems. We obtain\n", "the optimal parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{Ridge}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with $\\boldsymbol{I}$ being a $p\\times p$ identity matrix with the constraint that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\sum_{i=0}^{p-1} \\beta_i^2 \\leq t,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with $t$ a finite positive number. \n", "\n", "When we compare this with the ordinary least squares result we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{OLS}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which can lead to singular matrices. However, with the SVD, we can always compute the inverse of the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$.\n", "\n", "\n", "We see that Ridge regression is nothing but the standard OLS with a\n", "modified diagonal term added to $\\boldsymbol{X}^T\\boldsymbol{X}$. The consequences, in\n", "particular for our discussion of the bias-variance tradeoff are rather\n", "interesting. We will see that for specific values of $\\lambda$, we may\n", "even reduce the variance of the optimal parameters $\\boldsymbol{\\beta}$. These topics and other related ones, will be discussed after the more linear algebra oriented analysis here.\n", "\n", "\n", "\n", "Using our insights about the SVD of the design matrix $\\boldsymbol{X}$ \n", "We have already analyzed the OLS solutions in terms of the eigenvectors (the columns) of the right singular value matrix $\\boldsymbol{U}$ as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}_{\\mathrm{OLS}}=\\boldsymbol{X}\\boldsymbol{\\beta} =\\boldsymbol{U}\\boldsymbol{U}^T\\boldsymbol{y}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For Ridge regression this becomes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}_{\\mathrm{Ridge}}=\\boldsymbol{X}\\boldsymbol{\\beta}_{\\mathrm{Ridge}} = \\boldsymbol{U\\Sigma V^T}\\left(\\boldsymbol{V}\\boldsymbol{\\Sigma}^2\\boldsymbol{V}^T+\\lambda\\boldsymbol{I} \\right)^{-1}(\\boldsymbol{U\\Sigma V^T})^T\\boldsymbol{y}=\\sum_{j=0}^{p-1}\\boldsymbol{u}_j\\boldsymbol{u}_j^T\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda}\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "with the vectors $\\boldsymbol{u}_j$ being the columns of $\\boldsymbol{U}$ from the SVD of the matrix $\\boldsymbol{X}$. \n", "\n", "\n", "\n", "Since $\\lambda \\geq 0$, it means that compared to OLS, we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda} \\leq 1.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ridge regression finds the coordinates of $\\boldsymbol{y}$ with respect to the\n", "orthonormal basis $\\boldsymbol{U}$, it then shrinks the coordinates by\n", "$\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda}$. Recall that the SVD has\n", "eigenvalues ordered in a descending way, that is $\\sigma_i \\geq\n", "\\sigma_{i+1}$.\n", "\n", "For small eigenvalues $\\sigma_i$ it means that their contributions become less important, a fact which can be used to reduce the number of degrees of freedom. More about this when we have covered the material on a statistical interpretation of various linear regression methods.\n", "\n", "\n", "\n", "For the sake of simplicity, let us assume that the design matrix is orthonormal, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=(\\boldsymbol{X}^T\\boldsymbol{X})^{-1} =\\boldsymbol{I}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case the standard OLS results in" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{OLS}} = \\boldsymbol{X}^T\\boldsymbol{y}=\\sum_{i=0}^{p-1}\\boldsymbol{u}_j\\boldsymbol{u}_j^T\\boldsymbol{y},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{Ridge}} = \\left(\\boldsymbol{I}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}=\\left(1+\\lambda\\right)^{-1}\\boldsymbol{\\beta}^{\\mathrm{OLS}},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "that is the Ridge estimator scales the OLS estimator by the inverse of a factor $1+\\lambda$, and\n", "the Ridge estimator converges to zero when the hyperparameter goes to\n", "infinity.\n", "\n", "We will come back to more interpreations after we have gone through some of the statistical analysis part. \n", "\n", "\n", "\n", "Using the matrix-vector expression for Lasso regression and dropping the parameter $1/n$ in front of the standard mean squared error equation, we have the following **cost** function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\left\\{(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\right\\}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Taking the derivative with respect to $\\boldsymbol{\\beta}$ and recalling that the derivative of the absolute value is (we drop the boldfaced vector symbol for simplicty)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{d \\vert \\beta\\vert}{d \\boldsymbol{\\beta}}=\\mathrm{sgn}(\\boldsymbol{\\beta})=\\left\\{\\begin{array}{cc} 1 & \\beta > 0 \\\\ 0 & \\beta =0\\\\-1 & \\beta < 0, \\end{array}\\right.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we have that the derivative of the cost function is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{X},\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}}=-2\\boldsymbol{X}^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})+\\lambda sgn(\\boldsymbol{\\beta})=0,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and reordering we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}\\boldsymbol{\\beta}+\\lambda sgn(\\boldsymbol{\\beta})=2\\boldsymbol{X}^T\\boldsymbol{y}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This equation does not lead to a nice analytical equation as in Ridge regression or ordinary least squares. This equation can however be solved by using standard convex optimization algorithms using for example the Python package [CVXOPT](https://cvxopt.org/). We will discuss this later. \n", "\n", "\n", "\n", "\n", "\n", "Let us assume that our design matrix is given by unit (identity) matrix, that is a square diagonal matrix with ones only along the\n", "diagonal. In this case we have an equal number of rows and columns $n=p$.\n", "\n", "Our model approximation is just $\\tilde{\\boldsymbol{y}}=\\boldsymbol{\\beta}$ and the mean squared error and thereby the cost function for ordinary least sqquares (OLS) is then (we drop the term $1/n$)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and minimizing we have that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\beta}_i^{\\mathrm{OLS}} = y_i.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For Ridge regression our cost function is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2+\\lambda\\sum_{i=0}^{p-1}\\beta_i^2,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and minimizing we have that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\beta}_i^{\\mathrm{Ridge}} = \\frac{y_i}{1+\\lambda}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For Lasso regression our cost function is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2+\\lambda\\sum_{i=0}^{p-1}\\vert\\beta_i\\vert=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2+\\lambda\\sum_{i=0}^{p-1}\\sqrt{\\beta_i^2},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and minimizing we have that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "-2\\sum_{i=0}^{p-1}(y_i-\\beta_i)+\\lambda \\sum_{i=0}^{p-1}\\frac{(\\beta_i)}{\\vert\\beta_i\\vert}=0,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which leads to" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_i^{\\mathrm{Lasso}} = \\left\\{\\begin{array}{ccc}y_i-\\frac{\\lambda}{2} &\\mathrm{if} & y_i> \\frac{\\lambda}{2}\\\\\n", " y_i+\\frac{\\lambda}{2} &\\mathrm{if} & y_i< -\\frac{\\lambda}{2}\\\\\n", "\t\t\t\t\t\t\t 0 &\\mathrm{if} & \\vert y_i\\vert\\le \\frac{\\lambda}{2}\\end{array}\\right.\\\\.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting these results ([figure in handwritten notes for week 36](https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2021/NotesSeptember9.pdf)) shows clearly that Lasso regression suppresses (sets to zero) values of $\\beta_i$ for specific values of $\\lambda$. Ridge regression reduces on the other hand the values of $\\beta_i$ as function of $\\lambda$.\n", "\n", "\n", "As another examples, \n", "let us assume we have a data set with outputs/targets given by the vector" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{y}=\\begin{bmatrix}4 \\\\ 2 \\\\3\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and our inputs as a $3\\times 2$ design matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}=\\begin{bmatrix}2 & 0\\\\ 0 & 1 \\\\ 0 & 0\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "meaning that we have two features and two unknown parameters $\\beta_0$ and $\\beta_1$ to be determined either by ordinary least squares, Ridge or Lasso regression.\n", "\n", "\n", "For ordinary least squares (OLS) we know that the optimal solution is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\left( \\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inserting the above values we obtain that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\begin{bmatrix}2 \\\\ 2\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The code which implements this simpler case is presented after the discussion of Ridge and Lasso.\n", "\n", "\n", "For Ridge regression we have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{Ridge}}=\\left( \\boldsymbol{X}^T\\boldsymbol{X}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inserting the above values we obtain that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{Ridge}}=\\begin{bmatrix}\\frac{8}{4+\\lambda} \\\\ \\frac{2}{1+\\lambda}\\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is normally a constraint on the value of $\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_2$ via the parameter $\\lambda$.\n", "Let us for simplicity assume that $\\beta_0^2+\\beta_1^2=1$ as constraint. This will allow us to find an expression for the optimal values of $\\beta$ and $\\lambda$.\n", "\n", "To see this, let us write the cost function for Ridge regression. \n", "\n", "\n", "\n", "We define the MSE without the $1/n$ factor and have then, using that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}\\boldsymbol{\\beta}=\\begin{bmatrix} 2\\beta_0 \\\\ \\beta_1 \\\\0 \\end{bmatrix},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=(4-2\\beta_0)^2+(2-\\beta_1)^2+\\lambda(\\beta_0^2+\\beta_1^2),\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and taking the derivative with respect to $\\beta_0$ we get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\beta_0=\\frac{8}{4+\\lambda},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and for $\\beta_1$ we obtain" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\beta_1=\\frac{2}{1+\\lambda},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the constraint for $\\beta_0^2+\\beta_1^2=1$ we can constrain $\\lambda$ by solving" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\left(\\frac{8}{4+\\lambda}\\right)^2+\\left(\\frac{2}{1+\\lambda}\\right)^2=1,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which gives $\\lambda=4.571$ and $\\beta_0=0.933$ and $\\beta_1=0.359$.\n", "\n", "\n", "For Lasso we need now, keeping a constraint on $\\vert\\beta_0\\vert+\\vert\\beta_1\\vert=1$, to take the derivative of the absolute values of $\\beta_0$\n", "and $\\beta_1$. This gives us the following derivatives of the cost function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=(4-2\\beta_0)^2+(2-\\beta_1)^2+\\lambda(\\vert\\beta_0\\vert+\\vert\\beta_1\\vert),\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\beta_0}=-4(4-2\\beta_0)+\\lambda\\mathrm{sgn}(\\beta_0)=0,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\beta_1}=-2(2-\\beta_1)+\\lambda\\mathrm{sgn}(\\beta_1)=0.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have now four cases to solve besides the trivial cases $\\beta_0$ and/or $\\beta_1$ are zero, namely\n", "1. $\\beta_0 > 0$ and $\\beta_1 > 0$,\n", "\n", "2. $\\beta_0 > 0$ and $\\beta_1 < 0$,\n", "\n", "3. $\\beta_0 < 0$ and $\\beta_1 > 0$,\n", "\n", "4. $\\beta_0 < 0$ and $\\beta_1 < 0$.\n", "\n", "If we consider the first case, we have then" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "-4(4-2\\beta_0)+\\lambda=0,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "-2(2-\\beta_1)+\\lambda=0.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "which yields" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\beta_0=\\frac{16+\\lambda}{8},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\beta_1=\\frac{4+\\lambda}{2}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the constraint on $\\beta_0$ and $\\beta_1$ we can then find the optimal value of $\\lambda$ for the different cases. We leave this as an exercise to you.\n", "\n", "\n", "Here we set up the OLS, Ridge and Lasso functionality in order to study the above example. Note that here we have opted for a set of values of $\\lambda$, meaning that we need to perform a search in order to find the optimal values.\n", "\n", "First we study and compare the OLS and Ridge results. The next code compares all three methods." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2. 2.]\n", "Training MSE for OLS\n", "3.0\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAEGCAYAAABvtY4XAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAAjgUlEQVR4nO3daZhU1bn28f8DIs0kyOTAIBiVIAgILYoomggiwkFFI4hDEBETJzxOUWM0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