3415 lines
94 KiB
Plaintext
3415 lines
94 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b49fd9eb",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
|
|
"doconce format html week36.do.txt --no_mako -->\n",
|
|
"<!-- dom:TITLE: Week 36: Statistical interpretation of Linear Regression and Resampling techniques -->"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "138fe268",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"# Week 36: Statistical interpretation of Linear Regression and Resampling techniques\n",
|
|
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
|
|
"\n",
|
|
"Date: **September 4-8, 2023**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c20f461b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Plans for week 36\n",
|
|
"\n",
|
|
"* Material for the active learning sessions on Tuesday and Wednesday\n",
|
|
"\n",
|
|
" * Summary from last week on discussion of SVD, Ridge and Lasso linear regression.\n",
|
|
"\n",
|
|
" * Recommended Reading: Hastie et al chapter 3, see <https://link.springer.com/book/10.1007/978-0-387-84858-7>\n",
|
|
"\n",
|
|
" * Presentation and discussion of first project\n",
|
|
"\n",
|
|
"* Material for the lecture on Thursday September 7\n",
|
|
"\n",
|
|
" * Linear Regression and links with Statistics, Resampling methods\n",
|
|
"\n",
|
|
" * Recommended Reading: Goodfellow et al chapter 3 on probability theory, see URL:\"\"\n",
|
|
"\n",
|
|
" * See also Murphy, sections 2.4 (Gaussian distributions) and 3.2 (Bayesian Statistics, basis)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "55f0d080",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Material for the active learning sessions Tuesday and Wednesday\n",
|
|
"\n",
|
|
"The material here contains a summary from last Week and discussion of SVD, Ridge and Lasso regression with examples"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "69ce62a6",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Linear Regression and the SVD\n",
|
|
"\n",
|
|
"We used the SVD to analyse the matrix to invert in ordinary lineat regression"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a9aaa130",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "2b1c0902",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Since the matrices here have dimension $p\\times p$, with $p$ corresponding to the singular values, we defined last week the matrix"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "093e3a8c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "c0102cd2",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "084138a1",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "cb8ed4f2",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"meaning we can write"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d7c1c21b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{X}^T\\boldsymbol{X}=\\boldsymbol{V}\\tilde{\\boldsymbol{\\Sigma}}^2\\boldsymbol{V}^T.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "791e9c6d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Multiplying from the right with $\\boldsymbol{V}$ (using the orthogonality of $\\boldsymbol{V}$) we get"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "924b8081",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{V}=\\boldsymbol{V}\\tilde{\\boldsymbol{\\Sigma}}^2.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "949f6658",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## What does it mean?\n",
|
|
"\n",
|
|
"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",
|
|
"id": "46a4a83c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{v}_i=\\boldsymbol{v}_i\\sigma_i^2.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ce019aa6",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"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",
|
|
"id": "c662d90f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{C}[\\boldsymbol{X}]=\\frac{1}{n}\\boldsymbol{X}^T\\boldsymbol{X},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "7df45c97",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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}$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f0741872",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## And finally $\\boldsymbol{X}\\boldsymbol{X}^T$\n",
|
|
"\n",
|
|
"For $\\boldsymbol{X}\\boldsymbol{X}^T$ we found"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "05530ccb",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "3048f911",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Since the matrices here have dimension $n\\times n$, we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9e3c2de1",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "491923c1",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"leading to"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "77d1dbe5",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "fb7e2323",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Multiplying with $\\boldsymbol{U}$ from the right gives us the eigenvalue problem"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c9cca57a",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "04e4b486",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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}$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2b3c22fb",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## 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 pseudoinvers, that is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "12637a17",
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"Ainv = np.linlag.pinv(A)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "904adaf8",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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": 2,
|
|
"id": "6b5465dc",
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"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",
|
|
"id": "a80dcee4",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Inverse of Rectangular Matrix\n",
|
|
"\n",
|
|
"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",
|
|
"id": "0c4052d3",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{A}_{\\mathrm{PI}}= \\boldsymbol{V}\\boldsymbol{D}_{\\mathrm{PI}}\\boldsymbol{U}^T,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "185d433f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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": 3,
|
|
"id": "418f8797",
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"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",
|
|
"id": "bf5051ea",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"As you can see from this example, our own decomposition based on the SVD agrees with the pseudoinverse algorithm provided by **Numpy**."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "202d1f90",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## 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",
|
|
"id": "df0a8718",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "4ebd1d20",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"or we can state it as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "40df3859",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "980001f5",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"where we have used the definition of a norm-2 vector, that is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "699b1198",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\vert\\vert \\boldsymbol{x}\\vert\\vert_2 = \\sqrt{\\sum_i x_i^2}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ff01ef41",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## From OLS to Ridge and Lasso\n",
|
|
"\n",
|
|
"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",
|
|
"id": "d7b9188f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "778790dc",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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. We do not include such a constraints in the discussions here.\n",
|
|
"\n",
|
|
"By defining"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "38dd7428",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "6ed3deb0",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"we have a new optimization equation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e0860754",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "66751140",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "e1fa2bdb",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\vert\\vert \\boldsymbol{x}\\vert\\vert_1 = \\sum_i \\vert x_i\\vert.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "32131982",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Deriving the Ridge Regression Equations\n",
|
|
"\n",
|
|
"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",
|
|
"id": "c1979796",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "d7eb56d7",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "7c42f326",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "6496e5c0",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"with $\\boldsymbol{I}$ being a $p\\times p$ identity matrix with the constraint that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "837f7c04",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sum_{i=0}^{p-1} \\beta_i^2 \\leq t,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "55e9395f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"with $t$ a finite positive number."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a943f34a",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Note on Scikit-Learn\n",
|
|
"\n",
|
|
"Note well that a library like **Scikit-Learn** does not include the $1/n$ factor in the expression for the mean-squared error. If you include it, the optimal parameter $\\beta$ becomes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "63a227a9",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\hat{\\boldsymbol{\\beta}}_{\\mathrm{Ridge}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}+n\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8be2bd7b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"In our codes where we compare our own codes with **Scikit-Learn**, we do thus not include the $1/n$ factor in the cost function."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "35509652",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Comparison with OLS\n",
|
|
"When we compare this with the ordinary least squares result we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2fbe21d0",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\hat{\\boldsymbol{\\beta}}_{\\mathrm{OLS}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "71e986f8",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3a5e5f40",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## SVD analysis\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",
|
|
"id": "752b6ba2",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\tilde{\\boldsymbol{y}}_{\\mathrm{OLS}}=\\boldsymbol{X}\\boldsymbol{\\beta} =\\boldsymbol{U}\\boldsymbol{U}^T\\boldsymbol{y}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "4e724327",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"For Ridge regression this becomes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b15c2c90",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "82e70fa5",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"with the vectors $\\boldsymbol{u}_j$ being the columns of $\\boldsymbol{U}$ from the SVD of the matrix $\\boldsymbol{X}$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3394e2e7",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Interpreting the Ridge results\n",
|
|
"\n",
|
|
"Since $\\lambda \\geq 0$, it means that compared to OLS, we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "722ffa86",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda} \\leq 1.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "720ed773",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3b57864f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## More interpretations\n",
|
|
"\n",
|
|
"For the sake of simplicity, let us assume that the design matrix is orthonormal, that is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8f36ff27",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{X}^T\\boldsymbol{X}=(\\boldsymbol{X}^T\\boldsymbol{X})^{-1} =\\boldsymbol{I}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "70adc9ce",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"In this case the standard OLS results in"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "eb4a995f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{\\beta}^{\\mathrm{OLS}} = \\boldsymbol{X}^T\\boldsymbol{y}=\\sum_{i=0}^{n-1}\\boldsymbol{u}_i\\boldsymbol{u}_i^T\\boldsymbol{y},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e3cd4b92",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bbf9b1de",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "e30eff48",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9a8f8e2e",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Deriving the Lasso Regression Equations\n",
|
|
"\n",
|
|
"Using the matrix-vector expression for Lasso regression, we have the following **cost** function"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3cb7928c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\frac{1}{n}\\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",
|
|
"id": "b1c6c813",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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 simplicity)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "da336775",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\frac{d \\vert \\beta\\vert}{d \\beta}=\\mathrm{sgn}(\\beta)=\\left\\{\\begin{array}{cc} 1 & \\beta > 0 \\\\-1 & \\beta < 0, \\end{array}\\right.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2e0de141",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"we have that the derivative of the cost function is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "40985ec4",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\frac{\\partial C(\\boldsymbol{X},\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}}=-\\frac{2}{n}\\boldsymbol{X}^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})+\\lambda sgn(\\boldsymbol{\\beta})=0,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "31091d7b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and reordering we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e81d965e",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{X}^T\\boldsymbol{X}\\boldsymbol{\\beta}+\\lambda sgn(\\boldsymbol{\\beta})=\\boldsymbol{X}^T\\boldsymbol{y}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9984b3a4",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"This equation does not lead to a nice analytical equation as in Ridge regression or ordinary least squares. We have absorbed the factor $2/n$ in a redefinition of the parameter $\\lambda$. We will solve this type of problems using libraries like **scikit-learn**."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "21ec3768",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression\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",
|
|
"id": "6be0ec63",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "44c7278c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and minimizing we have that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "918a8cc3",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\hat{\\beta}_i^{\\mathrm{OLS}} = y_i.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3949af51",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Ridge Regression\n",
|
|
"\n",
|
|
"For Ridge regression our cost function is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "aa856d18",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "bd30d1d4",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and minimizing we have that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "142de535",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\hat{\\beta}_i^{\\mathrm{Ridge}} = \\frac{y_i}{1+\\lambda}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1efee0c3",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Lasso Regression\n",
|
|
"\n",
|
|
"For Lasso regression our cost function is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a6db019a",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "0f38deda",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and minimizing we have that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "7f9738a5",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "4e8460f8",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which leads to"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ae49ddce",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "1efb96d3",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Plotting these results 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$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1a030291",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Yet another Example\n",
|
|
"\n",
|
|
"Let us assume we have a data set with outputs/targets given by the vector"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "af486213",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{y}=\\begin{bmatrix}4 \\\\ 2 \\\\3\\end{bmatrix},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "87e5470d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and our inputs as a $3\\times 2$ design matrix"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "182f9f51",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{X}=\\begin{bmatrix}2 & 0\\\\ 0 & 1 \\\\ 0 & 0\\end{bmatrix},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "edc0ee3d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "673c64a0",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## The OLS case\n",
|
|
"\n",
|
|
"For ordinary least squares (OLS) we know that the optimal solution is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c0554476",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\left( \\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "cc9c761b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Inserting the above values we obtain that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b5c84ed1",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\begin{bmatrix}2 \\\\ 2\\end{bmatrix},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a8d2ba86",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"The code which implements this simpler case is presented after the discussion of Ridge and Lasso."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9e407326",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## The Ridge case\n",
|
|
"\n",
|
|
"For Ridge regression we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "36ce4758",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "8665e33b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Inserting the above values we obtain that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "16dbcd82",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\hat{\\boldsymbol{\\beta}}^{\\mathrm{Ridge}}=\\begin{bmatrix}\\frac{8}{4+\\lambda} \\\\ \\frac{2}{1+\\lambda}\\end{bmatrix},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "642f7768",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1784c79c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Writing the Cost Function\n",
|
|
"\n",
|
|
"We define the MSE without the $1/n$ factor and have then, using that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "72b7600f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{X}\\boldsymbol{\\beta}=\\begin{bmatrix} 2\\beta_0 \\\\ \\beta_1 \\\\0 \\end{bmatrix},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "84ca3171",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "d31aa45e",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and taking the derivative with respect to $\\beta_0$ we get"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1298ca6d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\beta_0=\\frac{8}{4+\\lambda},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fa2455d6",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and for $\\beta_1$ we obtain"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8cd50073",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\beta_1=\\frac{2}{1+\\lambda},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1f7e12c3",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Using the constraint for $\\beta_0^2+\\beta_1^2=1$ we can constrain $\\lambda$ by solving"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a79821fd",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\left(\\frac{8}{4+\\lambda}\\right)^2+\\left(\\frac{2}{1+\\lambda}\\right)^2=1,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2ac13531",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which gives $\\lambda=4.571$ and $\\beta_0=0.933$ and $\\beta_1=0.359$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "eed25f68",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Lasso case\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",
|
|
"id": "f74d71ec",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "f0797067",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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",
|
|
"id": "3cc5d5e0",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "04250e5c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\beta_1}=-2(2-\\beta_1)+\\lambda\\mathrm{sgn}(\\beta_1)=0.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2b24d2fa",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "aa950760",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## The first Case\n",
|
|
"\n",
|
|
"If we consider the first case, we have then"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "475389ed",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"-4(4-2\\beta_0)+\\lambda=0,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "086558d9",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1a3b2ba5",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"-2(2-\\beta_1)+\\lambda=0.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c928bc46",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which yields"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "809a58f7",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\beta_0=\\frac{16+\\lambda}{8},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3b937bf4",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6e5b2527",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\beta_1=\\frac{4+\\lambda}{2}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c5f88a45",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c57753b4",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Simple code for solving the above problem\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": 4,
|
|
"id": "392f218d",
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%matplotlib inline\n",
|
|
"\n",
|
|
"import os\n",
|
|
"import numpy as np\n",
|
|
"import pandas as pd\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"\n",
|
|
"def R2(y_data, y_model):\n",
|
|
" return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)\n",
|
|
"def MSE(y_data,y_model):\n",
|
|
" n = np.size(y_model)\n",
|
|
" return np.sum((y_data-y_model)**2)/n\n",
|
|
"\n",
|
|
"\n",
|
|
"# A seed just to ensure that the random numbers are the same for every run.\n",
|
|
"# Useful for eventual debugging.\n",
|
|
"\n",
|
|
"X = np.array( [ [ 2, 0], [0, 1], [0,0]])\n",
|
|
"y = np.array( [4, 2, 3])\n",
|
|
"\n",
|
|
"\n",
|
|
"# matrix inversion to find beta\n",
|
|
"OLSbeta = np.linalg.inv(X.T @ X) @ X.T @ y\n",
|
|
"print(OLSbeta)\n",
|
|
"# and then make the prediction\n",
|
|
"ytildeOLS = X @ OLSbeta\n",
|
|
"print(\"Training MSE for OLS\")\n",
|
|
"print(MSE(y,ytildeOLS))\n",
|
|
"ypredictOLS = X @ OLSbeta\n",
|
|
"\n",
|
|
"# Repeat now for Ridge regression and various values of the regularization parameter\n",
|
|
"I = np.eye(2,2)\n",
|
|
"# Decide which values of lambda to use\n",
|
|
"nlambdas = 100\n",
|
|
"MSEPredict = np.zeros(nlambdas)\n",
|
|
"lambdas = np.logspace(-4, 4, nlambdas)\n",
|
|
"for i in range(nlambdas):\n",
|
|
" lmb = lambdas[i]\n",
|
|
" Ridgebeta = np.linalg.inv(X.T @ X+lmb*I) @ X.T @ y\n",
|
|
"# print(Ridgebeta)\n",
|
|
" # and then make the prediction\n",
|
|
" ypredictRidge = X @ Ridgebeta\n",
|
|
" MSEPredict[i] = MSE(y,ypredictRidge)\n",
|
|
"# print(MSEPredict[i])\n",
|
|
" # Now plot the results\n",
|
|
"plt.figure()\n",
|
|
"plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Train')\n",
|
|
"plt.xlabel('log10(lambda)')\n",
|
|
"plt.ylabel('MSE')\n",
|
|
"plt.legend()\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "dd134ab1",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"We see here that we reach a plateau. What is actually happening?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8d9a61db",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## With Lasso Regression"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "afba7c4a",
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"import numpy as np\n",
|
|
"import pandas as pd\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"from sklearn import linear_model\n",
|
|
"\n",
|
|
"def R2(y_data, y_model):\n",
|
|
" return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)\n",
|
|
"def MSE(y_data,y_model):\n",
|
|
" n = np.size(y_model)\n",
|
|
" return np.sum((y_data-y_model)**2)/n\n",
|
|
"\n",
|
|
"\n",
|
|
"# A seed just to ensure that the random numbers are the same for every run.\n",
|
|
"# Useful for eventual debugging.\n",
|
|
"\n",
|
|
"X = np.array( [ [ 2, 0], [0, 1], [0,0]])\n",
|
|
"y = np.array( [4, 2, 3])\n",
|
|
"\n",
|
|
"\n",
|
|
"# matrix inversion to find beta\n",
|
|
"OLSbeta = np.linalg.inv(X.T @ X) @ X.T @ y\n",
|
|
"print(OLSbeta)\n",
|
|
"# and then make the prediction\n",
|
|
"ytildeOLS = X @ OLSbeta\n",
|
|
"print(\"Training MSE for OLS\")\n",
|
|
"print(MSE(y,ytildeOLS))\n",
|
|
"ypredictOLS = X @ OLSbeta\n",
|
|
"\n",
|
|
"# Repeat now for Ridge regression and various values of the regularization parameter\n",
|
|
"I = np.eye(2,2)\n",
|
|
"# Decide which values of lambda to use\n",
|
|
"nlambdas = 100\n",
|
|
"MSERidgePredict = np.zeros(nlambdas)\n",
|
|
"MSELassoPredict = np.zeros(nlambdas)\n",
|
|
"lambdas = np.logspace(-4, 4, nlambdas)\n",
|
|
"for i in range(nlambdas):\n",
|
|
" lmb = lambdas[i]\n",
|
|
" Ridgebeta = np.linalg.inv(X.T @ X+lmb*I) @ X.T @ y\n",
|
|
" print(Ridgebeta)\n",
|
|
" # and then make the prediction\n",
|
|
" ypredictRidge = X @ Ridgebeta\n",
|
|
" MSERidgePredict[i] = MSE(y,ypredictRidge)\n",
|
|
" RegLasso = linear_model.Lasso(lmb,fit_intercept=False)\n",
|
|
" RegLasso.fit(X,y)\n",
|
|
" ypredictLasso = RegLasso.predict(X)\n",
|
|
" print(RegLasso.coef_)\n",
|
|
" MSELassoPredict[i] = MSE(y,ypredictLasso)\n",
|
|
"# Now plot the results\n",
|
|
"plt.figure()\n",
|
|
"plt.plot(np.log10(lambdas), MSERidgePredict, 'r--', label = 'MSE Ridge Train')\n",
|
|
"plt.plot(np.log10(lambdas), MSELassoPredict, 'r--', label = 'MSE Lasso Train')\n",
|
|
"plt.xlabel('log10(lambda)')\n",
|
|
"plt.ylabel('MSE')\n",
|
|
"plt.legend()\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "4d63db0b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Another Example, now with a polynomial fit"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "81115144",
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"import numpy as np\n",
|
|
"import pandas as pd\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"from sklearn import linear_model\n",
|
|
"\n",
|
|
"def R2(y_data, y_model):\n",
|
|
" return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)\n",
|
|
"def MSE(y_data,y_model):\n",
|
|
" n = np.size(y_model)\n",
|
|
" return np.sum((y_data-y_model)**2)/n\n",
|
|
"\n",
|
|
"\n",
|
|
"# A seed just to ensure that the random numbers are the same for every run.\n",
|
|
"# Useful for eventual debugging.\n",
|
|
"np.random.seed(3155)\n",
|
|
"\n",
|
|
"x = np.random.rand(100)\n",
|
|
"y = 2.0+5*x*x+0.1*np.random.randn(100)\n",
|
|
"\n",
|
|
"# number of features p (here degree of polynomial\n",
|
|
"p = 3\n",
|
|
"# The design matrix now as function of a given polynomial\n",
|
|
"X = np.zeros((len(x),p))\n",
|
|
"X[:,0] = 1.0\n",
|
|
"X[:,1] = x\n",
|
|
"X[:,2] = x*x\n",
|
|
"# We split the data in test and training data\n",
|
|
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
|
|
"\n",
|
|
"# matrix inversion to find beta\n",
|
|
"OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train\n",
|
|
"print(OLSbeta)\n",
|
|
"# and then make the prediction\n",
|
|
"ytildeOLS = X_train @ OLSbeta\n",
|
|
"print(\"Training MSE for OLS\")\n",
|
|
"print(MSE(y_train,ytildeOLS))\n",
|
|
"ypredictOLS = X_test @ OLSbeta\n",
|
|
"print(\"Test MSE OLS\")\n",
|
|
"print(MSE(y_test,ypredictOLS))\n",
|
|
"\n",
|
|
"# Repeat now for Lasso and Ridge regression and various values of the regularization parameter\n",
|
|
"I = np.eye(p,p)\n",
|
|
"# Decide which values of lambda to use\n",
|
|
"nlambdas = 100\n",
|
|
"MSEPredict = np.zeros(nlambdas)\n",
|
|
"MSETrain = np.zeros(nlambdas)\n",
|
|
"MSELassoPredict = np.zeros(nlambdas)\n",
|
|
"MSELassoTrain = np.zeros(nlambdas)\n",
|
|
"lambdas = np.logspace(-4, 4, nlambdas)\n",
|
|
"for i in range(nlambdas):\n",
|
|
" lmb = lambdas[i]\n",
|
|
" Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train\n",
|
|
" # include lasso using Scikit-Learn\n",
|
|
" RegLasso = linear_model.Lasso(lmb,fit_intercept=False)\n",
|
|
" RegLasso.fit(X_train,y_train)\n",
|
|
" # and then make the prediction\n",
|
|
" ytildeRidge = X_train @ Ridgebeta\n",
|
|
" ypredictRidge = X_test @ Ridgebeta\n",
|
|
" ytildeLasso = RegLasso.predict(X_train)\n",
|
|
" ypredictLasso = RegLasso.predict(X_test)\n",
|
|
" MSEPredict[i] = MSE(y_test,ypredictRidge)\n",
|
|
" MSETrain[i] = MSE(y_train,ytildeRidge)\n",
|
|
" MSELassoPredict[i] = MSE(y_test,ypredictLasso)\n",
|
|
" MSELassoTrain[i] = MSE(y_train,ytildeLasso)\n",
|
|
"\n",
|
|
"# Now plot the results\n",
|
|
"plt.figure()\n",
|
|
"plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train')\n",
|
|
"plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test')\n",
|
|
"plt.plot(np.log10(lambdas), MSELassoTrain, label = 'MSE Lasso train')\n",
|
|
"plt.plot(np.log10(lambdas), MSELassoPredict, 'r--', label = 'MSE Lasso Test')\n",
|
|
"\n",
|
|
"plt.xlabel('log10(lambda)')\n",
|
|
"plt.ylabel('MSE')\n",
|
|
"plt.legend()\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e076a968",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Material for lecture Thursday September 7"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "db822c65",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Linking the regression analysis with a statistical interpretation\n",
|
|
"\n",
|
|
"We will now couple the discussions of ordinary least squares, Ridge\n",
|
|
"and Lasso regression with a statistical interpretation, that is we\n",
|
|
"move from a linear algebra analysis to a statistical analysis. In\n",
|
|
"particular, we will focus on what the regularization terms can result\n",
|
|
"in. We will amongst other things show that the regularization\n",
|
|
"parameter can reduce considerably the variance of the parameters\n",
|
|
"$\\beta$.\n",
|
|
"\n",
|
|
"The\n",
|
|
"advantage of doing linear regression is that we actually end up with\n",
|
|
"analytical expressions for several statistical quantities. \n",
|
|
"Standard least squares and Ridge regression allow us to\n",
|
|
"derive quantities like the variance and other expectation values in a\n",
|
|
"rather straightforward way.\n",
|
|
"\n",
|
|
"It is assumed that $\\varepsilon_i\n",
|
|
"\\sim \\mathcal{N}(0, \\sigma^2)$ and the $\\varepsilon_{i}$ are\n",
|
|
"independent, i.e.:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fc4a06b6",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{align*} \n",
|
|
"\\mbox{Cov}(\\varepsilon_{i_1},\n",
|
|
"\\varepsilon_{i_2}) & = \\left\\{ \\begin{array}{lcc} \\sigma^2 & \\mbox{if}\n",
|
|
"& i_1 = i_2, \\\\ 0 & \\mbox{if} & i_1 \\not= i_2. \\end{array} \\right.\n",
|
|
"\\end{align*}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "43299fb3",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"The randomness of $\\varepsilon_i$ implies that\n",
|
|
"$\\mathbf{y}_i$ is also a random variable. In particular,\n",
|
|
"$\\mathbf{y}_i$ is normally distributed, because $\\varepsilon_i \\sim\n",
|
|
"\\mathcal{N}(0, \\sigma^2)$ and $\\mathbf{X}_{i,\\ast} \\, \\boldsymbol{\\beta}$ is a\n",
|
|
"non-random scalar. To specify the parameters of the distribution of\n",
|
|
"$\\mathbf{y}_i$ we need to calculate its first two moments. \n",
|
|
"\n",
|
|
"Recall that $\\boldsymbol{X}$ is a matrix of dimensionality $n\\times p$. The\n",
|
|
"notation above $\\mathbf{X}_{i,\\ast}$ means that we are looking at the\n",
|
|
"row number $i$ and perform a sum over all values $p$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6cba00c7",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Assumptions made\n",
|
|
"\n",
|
|
"The assumption we have made here can be summarized as (and this is going to be useful when we discuss the bias-variance trade off)\n",
|
|
"that there exists a function $f(\\boldsymbol{x})$ and a normal distributed error $\\boldsymbol{\\varepsilon}\\sim \\mathcal{N}(0, \\sigma^2)$\n",
|
|
"which describe our data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8c85f61d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{y} = f(\\boldsymbol{x})+\\boldsymbol{\\varepsilon}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a1901c32",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"We approximate this function with our model from the solution of the linear regression equations, that is our\n",
|
|
"function $f$ is approximated by $\\boldsymbol{\\tilde{y}}$ where we want to minimize $(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2$, our MSE, with"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c59e8272",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{\\tilde{y}} = \\boldsymbol{X}\\boldsymbol{\\beta}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8cba4bae",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Expectation value and variance\n",
|
|
"\n",
|
|
"We can calculate the expectation value of $\\boldsymbol{y}$ for a given element $i$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5e18ebb5",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{align*} \n",
|
|
"\\mathbb{E}(y_i) & =\n",
|
|
"\\mathbb{E}(\\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta}) + \\mathbb{E}(\\varepsilon_i)\n",
|
|
"\\, \\, \\, = \\, \\, \\, \\mathbf{X}_{i, \\ast} \\, \\beta, \n",
|
|
"\\end{align*}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "10d86c58",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"while\n",
|
|
"its variance is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "efba1ba9",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{align*} \\mbox{Var}(y_i) & = \\mathbb{E} \\{ [y_i\n",
|
|
"- \\mathbb{E}(y_i)]^2 \\} \\, \\, \\, = \\, \\, \\, \\mathbb{E} ( y_i^2 ) -\n",
|
|
"[\\mathbb{E}(y_i)]^2 \\\\ & = \\mathbb{E} [ ( \\mathbf{X}_{i, \\ast} \\,\n",
|
|
"\\beta + \\varepsilon_i )^2] - ( \\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta})^2 \\\\ &\n",
|
|
"= \\mathbb{E} [ ( \\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta})^2 + 2 \\varepsilon_i\n",
|
|
"\\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta} + \\varepsilon_i^2 ] - ( \\mathbf{X}_{i,\n",
|
|
"\\ast} \\, \\beta)^2 \\\\ & = ( \\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta})^2 + 2\n",
|
|
"\\mathbb{E}(\\varepsilon_i) \\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta} +\n",
|
|
"\\mathbb{E}(\\varepsilon_i^2 ) - ( \\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta})^2 \n",
|
|
"\\\\ & = \\mathbb{E}(\\varepsilon_i^2 ) \\, \\, \\, = \\, \\, \\,\n",
|
|
"\\mbox{Var}(\\varepsilon_i) \\, \\, \\, = \\, \\, \\, \\sigma^2. \n",
|
|
"\\end{align*}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a3eccdab",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Hence, $y_i \\sim \\mathcal{N}( \\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta}, \\sigma^2)$, that is $\\boldsymbol{y}$ follows a normal distribution with \n",
|
|
"mean value $\\boldsymbol{X}\\boldsymbol{\\beta}$ and variance $\\sigma^2$ (not be confused with the singular values of the SVD)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "70b901b4",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Expectation value and variance for $\\boldsymbol{\\beta}$\n",
|
|
"\n",
|
|
"With the OLS expressions for the optimal parameters $\\boldsymbol{\\hat{\\beta}}$ we can evaluate the expectation value"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "752b72d2",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbb{E}(\\boldsymbol{\\hat{\\beta}}) = \\mathbb{E}[ (\\mathbf{X}^{\\top} \\mathbf{X})^{-1}\\mathbf{X}^{T} \\mathbf{Y}]=(\\mathbf{X}^{T} \\mathbf{X})^{-1}\\mathbf{X}^{T} \\mathbb{E}[ \\mathbf{Y}]=(\\mathbf{X}^{T} \\mathbf{X})^{-1} \\mathbf{X}^{T}\\mathbf{X}\\boldsymbol{\\beta}=\\boldsymbol{\\beta}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "108aba79",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"This means that the estimator of the regression parameters is unbiased.\n",
|
|
"\n",
|
|
"We can also calculate the variance\n",
|
|
"\n",
|
|
"The variance of the optimal value $\\boldsymbol{\\hat{\\beta}}$ is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "65801f3b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{eqnarray*}\n",
|
|
"\\mbox{Var}(\\boldsymbol{\\hat{\\beta}}) & = & \\mathbb{E} \\{ [\\boldsymbol{\\beta} - \\mathbb{E}(\\boldsymbol{\\beta})] [\\boldsymbol{\\beta} - \\mathbb{E}(\\boldsymbol{\\beta})]^{T} \\}\n",
|
|
"\\\\\n",
|
|
"& = & \\mathbb{E} \\{ [(\\mathbf{X}^{T} \\mathbf{X})^{-1} \\, \\mathbf{X}^{T} \\mathbf{Y} - \\boldsymbol{\\beta}] \\, [(\\mathbf{X}^{T} \\mathbf{X})^{-1} \\, \\mathbf{X}^{T} \\mathbf{Y} - \\boldsymbol{\\beta}]^{T} \\}\n",
|
|
"\\\\\n",
|
|
"% & = & \\mathbb{E} \\{ [(\\mathbf{X}^{T} \\mathbf{X})^{-1} \\, \\mathbf{X}^{T} \\mathbf{Y}] \\, [(\\mathbf{X}^{T} \\mathbf{X})^{-1} \\, \\mathbf{X}^{T} \\mathbf{Y}]^{T} \\} - \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T}\n",
|
|
"% \\\\\n",
|
|
"% & = & \\mathbb{E} \\{ (\\mathbf{X}^{T} \\mathbf{X})^{-1} \\, \\mathbf{X}^{T} \\mathbf{Y} \\, \\mathbf{Y}^{T} \\, \\mathbf{X} \\, (\\mathbf{X}^{T} \\mathbf{X})^{-1} \\} - \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T}\n",
|
|
"% \\\\\n",
|
|
"& = & (\\mathbf{X}^{T} \\mathbf{X})^{-1} \\, \\mathbf{X}^{T} \\, \\mathbb{E} \\{ \\mathbf{Y} \\, \\mathbf{Y}^{T} \\} \\, \\mathbf{X} \\, (\\mathbf{X}^{T} \\mathbf{X})^{-1} - \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T}\n",
|
|
"\\\\\n",
|
|
"& = & (\\mathbf{X}^{T} \\mathbf{X})^{-1} \\, \\mathbf{X}^{T} \\, \\{ \\mathbf{X} \\, \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T} \\, \\mathbf{X}^{T} + \\sigma^2 \\} \\, \\mathbf{X} \\, (\\mathbf{X}^{T} \\mathbf{X})^{-1} - \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T}\n",
|
|
"% \\\\\n",
|
|
"% & = & (\\mathbf{X}^T \\mathbf{X})^{-1} \\, \\mathbf{X}^T \\, \\mathbf{X} \\, \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^T \\, \\mathbf{X}^T \\, \\mathbf{X} \\, (\\mathbf{X}^T % \\mathbf{X})^{-1}\n",
|
|
"% \\\\\n",
|
|
"% & & + \\, \\, \\sigma^2 \\, (\\mathbf{X}^T \\mathbf{X})^{-1} \\, \\mathbf{X}^T \\, \\mathbf{X} \\, (\\mathbf{X}^T \\mathbf{X})^{-1} - \\boldsymbol{\\beta} \\boldsymbol{\\beta}^T\n",
|
|
"\\\\\n",
|
|
"& = & \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T} + \\sigma^2 \\, (\\mathbf{X}^{T} \\mathbf{X})^{-1} - \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T}\n",
|
|
"\\, \\, \\, = \\, \\, \\, \\sigma^2 \\, (\\mathbf{X}^{T} \\mathbf{X})^{-1},\n",
|
|
"\\end{eqnarray*}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "faf8da78",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"where we have used that $\\mathbb{E} (\\mathbf{Y} \\mathbf{Y}^{T}) =\n",
|
|
"\\mathbf{X} \\, \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T} \\, \\mathbf{X}^{T} +\n",
|
|
"\\sigma^2 \\, \\mathbf{I}_{nn}$. From $\\mbox{Var}(\\boldsymbol{\\beta}) = \\sigma^2\n",
|
|
"\\, (\\mathbf{X}^{T} \\mathbf{X})^{-1}$, one obtains an estimate of the\n",
|
|
"variance of the estimate of the $j$-th regression coefficient:\n",
|
|
"$\\boldsymbol{\\sigma}^2 (\\boldsymbol{\\beta}_j ) = \\boldsymbol{\\sigma}^2 [(\\mathbf{X}^{T} \\mathbf{X})^{-1}]_{jj} $. This may be used to\n",
|
|
"construct a confidence interval for the estimates.\n",
|
|
"\n",
|
|
"In a similar way, we can obtain analytical expressions for say the\n",
|
|
"expectation values of the parameters $\\boldsymbol{\\beta}$ and their variance\n",
|
|
"when we employ Ridge regression, allowing us again to define a confidence interval. \n",
|
|
"\n",
|
|
"It is rather straightforward to show that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1c71921e",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathbb{E} \\big[ \\boldsymbol{\\beta}^{\\mathrm{Ridge}} \\big]=(\\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I}_{pp})^{-1} (\\mathbf{X}^{\\top} \\mathbf{X})\\boldsymbol{\\beta}^{\\mathrm{OLS}}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "cfb57087",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"We see clearly that \n",
|
|
"$\\mathbb{E} \\big[ \\boldsymbol{\\beta}^{\\mathrm{Ridge}} \\big] \\not= \\boldsymbol{\\beta}^{\\mathrm{OLS}}$ for any $\\lambda > 0$. We say then that the ridge estimator is biased.\n",
|
|
"\n",
|
|
"We can also compute the variance as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "be4ad159",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mbox{Var}[\\boldsymbol{\\beta}^{\\mathrm{Ridge}}]=\\sigma^2[ \\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1} \\mathbf{X}^{T} \\mathbf{X} \\{ [ \\mathbf{X}^{\\top} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1}\\}^{T},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f5e11f36",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and it is easy to see that if the parameter $\\lambda$ goes to infinity then the variance of Ridge parameters $\\boldsymbol{\\beta}$ goes to zero. \n",
|
|
"\n",
|
|
"With this, we can compute the difference"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "dbf2700f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mbox{Var}[\\boldsymbol{\\beta}^{\\mathrm{OLS}}]-\\mbox{Var}(\\boldsymbol{\\beta}^{\\mathrm{Ridge}})=\\sigma^2 [ \\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1}[ 2\\lambda\\mathbf{I} + \\lambda^2 (\\mathbf{X}^{T} \\mathbf{X})^{-1} ] \\{ [ \\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1}\\}^{T}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "0f6d760d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"The difference is non-negative definite since each component of the\n",
|
|
"matrix product is non-negative definite. \n",
|
|
"This means the variance we obtain with the standard OLS will always for $\\lambda > 0$ be larger than the variance of $\\boldsymbol{\\beta}$ obtained with the Ridge estimator. This has interesting consequences when we discuss the so-called bias-variance trade-off below."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ce614956",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Deriving OLS from a probability distribution\n",
|
|
"\n",
|
|
"Our basic assumption when we derived the OLS equations was to assume\n",
|
|
"that our output is determined by a given continuous function\n",
|
|
"$f(\\boldsymbol{x})$ and a random noise $\\boldsymbol{\\epsilon}$ given by the normal\n",
|
|
"distribution with zero mean value and an undetermined variance\n",
|
|
"$\\sigma^2$.\n",
|
|
"\n",
|
|
"We found above that the outputs $\\boldsymbol{y}$ have a mean value given by\n",
|
|
"$\\boldsymbol{X}\\hat{\\boldsymbol{\\beta}}$ and variance $\\sigma^2$. Since the entries to\n",
|
|
"the design matrix are not stochastic variables, we can assume that the\n",
|
|
"probability distribution of our targets is also a normal distribution\n",
|
|
"but now with mean value $\\boldsymbol{X}\\hat{\\boldsymbol{\\beta}}$. This means that a\n",
|
|
"single output $y_i$ is given by the Gaussian distribution"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "086419d8",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"y_i\\sim \\mathcal{N}(\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta}, \\sigma^2)=\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a228dcf4",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Independent and Identically Distrubuted (iid)\n",
|
|
"\n",
|
|
"We assume now that the various $y_i$ values are stochastically distributed according to the above Gaussian distribution. \n",
|
|
"We define this distribution as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f04f6b76",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(y_i, \\boldsymbol{X}\\vert\\boldsymbol{\\beta})=\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "642936a8",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which reads as finding the likelihood of an event $y_i$ with the input variables $\\boldsymbol{X}$ given the parameters (to be determined) $\\boldsymbol{\\beta}$.\n",
|
|
"\n",
|
|
"Since these events are assumed to be independent and identicall distributed we can build the probability distribution function (PDF) for all possible event $\\boldsymbol{y}$ as the product of the single events, that is we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "4f137023",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(\\boldsymbol{y},\\boldsymbol{X}\\vert\\boldsymbol{\\beta})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}=\\prod_{i=0}^{n-1}p(y_i,\\boldsymbol{X}\\vert\\boldsymbol{\\beta}).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "48fdd9f7",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"We will write this in a more compact form reserving $\\boldsymbol{D}$ for the domain of events, including the ouputs (targets) and the inputs. That is\n",
|
|
"in case we have a simple one-dimensional input and output case"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ae36f857",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\\dots, (x_{n-1},y_{n-1})].\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d312d461",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"In the more general case the various inputs should be replaced by the possible features represented by the input data set $\\boldsymbol{X}$. \n",
|
|
"We can now rewrite the above probability as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "59ea8b57",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b9be32b5",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"It is a conditional probability (see below) and reads as the likelihood of a domain of events $\\boldsymbol{D}$ given a set of parameters $\\boldsymbol{\\beta}$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b378b2c9",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Maximum Likelihood Estimation (MLE)\n",
|
|
"\n",
|
|
"In statistics, maximum likelihood estimation (MLE) is a method of\n",
|
|
"estimating the parameters of an assumed probability distribution,\n",
|
|
"given some observed data. This is achieved by maximizing a likelihood\n",
|
|
"function so that, under the assumed statistical model, the observed\n",
|
|
"data is the most probable. \n",
|
|
"\n",
|
|
"We will assume here that our events are given by the above Gaussian\n",
|
|
"distribution and we will determine the optimal parameters $\\beta$ by\n",
|
|
"maximizing the above PDF. However, computing the derivatives of a\n",
|
|
"product function is cumbersome and can easily lead to overflow and/or\n",
|
|
"underflowproblems, with potentials for loss of numerical precision.\n",
|
|
"\n",
|
|
"In practice, it is more convenient to maximize the logarithm of the\n",
|
|
"PDF because it is a monotonically increasing function of the argument.\n",
|
|
"Alternatively, and this will be our option, we will minimize the\n",
|
|
"negative of the logarithm since this is a monotonically decreasing\n",
|
|
"function.\n",
|
|
"\n",
|
|
"Note also that maximization/minimization of the logarithm of the PDF\n",
|
|
"is equivalent to the maximization/minimization of the function itself."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6ddc5457",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## A new Cost Function\n",
|
|
"\n",
|
|
"We could now define a new cost function to minimize, namely the negative logarithm of the above PDF"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bb0f5795",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"C(\\boldsymbol{\\beta}=-\\log{\\prod_{i=0}^{n-1}p(y_i,\\boldsymbol{X}\\vert\\boldsymbol{\\beta})}=-\\sum_{i=0}^{n-1}\\log{p(y_i,\\boldsymbol{X}\\vert\\boldsymbol{\\beta})},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c969ba54",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which becomes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "630a5421",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"C(\\boldsymbol{\\beta}=\\frac{n}{2}\\log{2\\pi\\sigma^2}+\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "49969ea1",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Taking the derivative of the *new* cost function with respect to the parameters $\\beta$ we recognize our familiar OLS equation, namely"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "93b3d19b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{X}^T\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right) =0,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f61d06bf",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which leads to the well-known OLS equation for the optimal paramters $\\beta$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8c25aa29",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}!\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "add3ac78",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Before we make a similar analysis for Ridge and Lasso regression, we need a short reminder on statistics."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2cb62527",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## More basic Statistics and Bayes' theorem\n",
|
|
"\n",
|
|
"A central theorem in statistics is Bayes' theorem. This theorem plays a similar role as the good old Pythagoras' theorem in geometry.\n",
|
|
"Bayes' theorem is extremely simple to derive. But to do so we need some basic axioms from statistics.\n",
|
|
"\n",
|
|
"Assume we have two domains of events $X=[x_0,x_1,\\dots,x_{n-1}]$ and $Y=[y_0,y_1,\\dots,y_{n-1}]$.\n",
|
|
"\n",
|
|
"We define also the likelihood for $X$ and $Y$ as $p(X)$ and $p(Y)$ respectively.\n",
|
|
"The likelihood of a specific event $x_i$ (or $y_i$) is then written as $p(X=x_i)$ or just $p(x_i)=p_i$. \n",
|
|
"\n",
|
|
"**Union of events is given by.**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3704ebb3",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(X \\cup Y)= p(X)+p(Y)-p(X \\cap Y).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "76f1d92c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"**The product rule (aka joint probability) is given by.**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "31ef87a3",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(X \\cup Y)= p(X,Y)= p(X\\vert Y)p(Y)=p(Y\\vert X)p(X),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9f87e53d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"where we read $p(X\\vert Y)$ as the likelihood of obtaining $X$ given $Y$.\n",
|
|
"\n",
|
|
"If we have independent events then $p(X,Y)=p(X)p(Y)$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b465eba1",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Marginal Probability\n",
|
|
"\n",
|
|
"The marginal probability is defined in terms of only one of the set of variables $X,Y$. For a discrete probability we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b522f15f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(X)=\\sum_{i=0}^{n-1}p(X,Y=y_i)=\\sum_{i=0}^{n-1}p(X\\vert Y=y_i)p(Y=y_i)=\\sum_{i=0}^{n-1}p(X\\vert y_i)p(y_i).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a8bf634f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Conditional Probability\n",
|
|
"\n",
|
|
"The conditional probability, if $p(Y) > 0$, is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "51e8bfb6",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(X\\vert Y)= \\frac{p(X,Y)}{p(Y)}=\\frac{p(X,Y)}{\\sum_{i=0}^{n-1}p(Y\\vert X=x_i)p(x_i)}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bfb3c814",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Bayes' Theorem\n",
|
|
"\n",
|
|
"If we combine the conditional probability with the marginal probability and the standard product rule, we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "dfff0c7c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(X\\vert Y)= \\frac{p(X,Y)}{p(Y)},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "cad0634f",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which we can rewrite as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1de5e07a",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(X\\vert Y)= \\frac{p(X,Y)}{\\sum_{i=0}^{n-1}p(Y\\vert X=x_i)p(x_i)}=\\frac{p(Y\\vert X)p(X)}{\\sum_{i=0}^{n-1}p(Y\\vert X=x_i)p(x_i)},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "43f1714e",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which is Bayes' theorem. It allows us to evaluate the uncertainty in in $X$ after we have observed $Y$. We can easily interchange $X$ with $Y$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "166406ab",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Interpretations of Bayes' Theorem\n",
|
|
"\n",
|
|
"The quantity $p(Y\\vert X)$ on the right-hand side of the theorem is\n",
|
|
"evaluated for the observed data $Y$ and can be viewed as a function of\n",
|
|
"the parameter space represented by $X$. This function is not\n",
|
|
"necesseraly normalized and is normally called the likelihood function.\n",
|
|
"\n",
|
|
"The function $p(X)$ on the right hand side is called the prior while the function on the left hand side is the called the posterior probability. The denominator on the right hand side serves as a normalization factor for the posterior distribution.\n",
|
|
"\n",
|
|
"Let us try to illustrate Bayes' theorem through an example."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d547f8dd",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Example of Usage of Bayes' theorem\n",
|
|
"\n",
|
|
"Let us suppose that you are undergoing a series of mammography scans in\n",
|
|
"order to rule out possible breast cancer cases. We define the\n",
|
|
"sensitivity for a positive event by the variable $X$. It takes binary\n",
|
|
"values with $X=1$ representing a positive event and $X=0$ being a\n",
|
|
"negative event. We reserve $Y$ as a classification parameter for\n",
|
|
"either a negative or a positive breast cancer confirmation. (Short note on wordings: positive here means having breast cancer, although none of us would consider this being a positive thing).\n",
|
|
"\n",
|
|
"We let $Y=1$ represent the the case of having breast cancer and $Y=0$ as not.\n",
|
|
"\n",
|
|
"Let us assume that if you have breast cancer, the test will be positive with a probability of $0.8$, that is we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f848f875",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(X=1\\vert Y=1) =0.8.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8c506872",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"This obviously sounds scary since many would conclude that if the test is positive, there is a likelihood of $80\\%$ for having cancer.\n",
|
|
"It is however not correct, as the following Bayesian analysis shows."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "317558b2",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Doing it correctly\n",
|
|
"\n",
|
|
"If we look at various national surveys on breast cancer, the general likelihood of developing breast cancer is a very small number.\n",
|
|
"Let us assume that the prior probability in the population as a whole is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8f8f8c5c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(Y=1) =0.004.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "acb7607d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"We need also to account for the fact that the test may produce a false positive result (false alarm). Let us here assume that we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c5866ceb",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(X=1\\vert Y=0) =0.1.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e3648f6b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Using Bayes' theorem we can then find the posterior probability that the person has breast cancer in case of a positive test, that is we can compute"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "0b2cef16",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(Y=1\\vert X=1)=\\frac{p(X=1\\vert Y=1)p(Y=1)}{p(X=1\\vert Y=1)p(Y=1)+p(X=1\\vert Y=0)p(Y=0)}=\\frac{0.8\\times 0.004}{0.8\\times 0.004+0.1\\times 0.996}=0.031.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "94233096",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"That is, in case of a positive test, there is only a $3\\%$ chance of having breast cancer!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "91284f6d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Bayes' Theorem and Ridge and Lasso Regression\n",
|
|
"\n",
|
|
"Hitherto we have discussed Ridge and Lasso regression in terms of a\n",
|
|
"linear analysis. This may to many of you feel rather technical and\n",
|
|
"perhaps not that intuitive. The question is whether we can develop a\n",
|
|
"more intuitive way of understanding what Ridge and Lasso express.\n",
|
|
"\n",
|
|
"Before we proceed let us perform a Ridge, Lasso and OLS analysis of a polynomial fit."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3debb528",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Test Function for what happens with OLS, Ridge and Lasso\n",
|
|
"\n",
|
|
"We will play around with a study of the values for the optimal\n",
|
|
"parameters $\\boldsymbol{\\beta}$ using OLS, Ridge and Lasso regression. For\n",
|
|
"OLS, you will notice as function of the noise and polynomial degree,\n",
|
|
"that the parameters $\\beta$ will fluctuate from order to order in the\n",
|
|
"polynomial fit and that for larger and larger polynomial degrees of freedom, the parameters will tend to increase in value for OLS.\n",
|
|
"\n",
|
|
"For Ridge and Lasso regression, the higher order parameters will typically be reduced, providing thereby less fluctuations from one order to another one."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "a4cf2a42",
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"from sklearn import linear_model\n",
|
|
"\n",
|
|
"def R2(y_data, y_model):\n",
|
|
" return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)\n",
|
|
"def MSE(y_data,y_model):\n",
|
|
" n = np.size(y_model)\n",
|
|
" return np.sum((y_data-y_model)**2)/n\n",
|
|
"\n",
|
|
"# Make data set.\n",
|
|
"n = 10000\n",
|
|
"x = np.random.rand(n)\n",
|
|
"y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.randn(n)\n",
|
|
"\n",
|
|
"Maxpolydegree = 5\n",
|
|
"X = np.zeros((len(x),Maxpolydegree))\n",
|
|
"X[:,0] = 1.0\n",
|
|
"\n",
|
|
"for polydegree in range(1, Maxpolydegree):\n",
|
|
" for degree in range(polydegree):\n",
|
|
" X[:,degree] = x**(degree)\n",
|
|
"\n",
|
|
"\n",
|
|
"# We split the data in test and training data\n",
|
|
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
|
|
"\n",
|
|
"# matrix inversion to find beta\n",
|
|
"OLSbeta = np.linalg.pinv(X_train.T @ X_train) @ X_train.T @ y_train\n",
|
|
"print(OLSbeta)\n",
|
|
"ypredictOLS = X_test @ OLSbeta\n",
|
|
"print(\"Test MSE OLS\")\n",
|
|
"print(MSE(y_test,ypredictOLS))\n",
|
|
"# Repeat now for Lasso and Ridge regression and various values of the regularization parameter using Scikit-Learn\n",
|
|
"# Decide which values of lambda to use\n",
|
|
"nlambdas = 4\n",
|
|
"MSERidgePredict = np.zeros(nlambdas)\n",
|
|
"MSELassoPredict = np.zeros(nlambdas)\n",
|
|
"lambdas = np.logspace(-3, 1, nlambdas)\n",
|
|
"for i in range(nlambdas):\n",
|
|
" lmb = lambdas[i]\n",
|
|
" # Make the fit using Ridge and Lasso\n",
|
|
" RegRidge = linear_model.Ridge(lmb,fit_intercept=False)\n",
|
|
" RegRidge.fit(X_train,y_train)\n",
|
|
" RegLasso = linear_model.Lasso(lmb,fit_intercept=False)\n",
|
|
" RegLasso.fit(X_train,y_train)\n",
|
|
" # and then make the prediction\n",
|
|
" ypredictRidge = RegRidge.predict(X_test)\n",
|
|
" ypredictLasso = RegLasso.predict(X_test)\n",
|
|
" # Compute the MSE and print it\n",
|
|
" MSERidgePredict[i] = MSE(y_test,ypredictRidge)\n",
|
|
" MSELassoPredict[i] = MSE(y_test,ypredictLasso)\n",
|
|
" print(lmb,RegRidge.coef_)\n",
|
|
" print(lmb,RegLasso.coef_)\n",
|
|
"# Now plot the results\n",
|
|
"plt.figure()\n",
|
|
"plt.plot(np.log10(lambdas), MSERidgePredict, 'b', label = 'MSE Ridge Test')\n",
|
|
"plt.plot(np.log10(lambdas), MSELassoPredict, 'r', label = 'MSE Lasso Test')\n",
|
|
"plt.xlabel('log10(lambda)')\n",
|
|
"plt.ylabel('MSE')\n",
|
|
"plt.legend()\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "26270da6",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"How can we understand this?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d8f0e20a",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Invoking Bayes' theorem\n",
|
|
"\n",
|
|
"Using Bayes' theorem we can gain a better intuition about Ridge and Lasso regression. \n",
|
|
"\n",
|
|
"For ordinary least squares we postulated that the maximum likelihood for the doamin of events $\\boldsymbol{D}$ (one-dimensional case)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6c93bc4b",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"\\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\\dots, (x_{n-1},y_{n-1})],\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8c8b6cc8",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"is given by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f76e3a40",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "af323d99",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"In Bayes' theorem this function plays the role of the so-called likelihood. We could now ask the question what is the posterior probability of a parameter set $\\boldsymbol{\\beta}$ given a domain of events $\\boldsymbol{D}$? That is, how can we define the posterior probability"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "49a0988c",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(\\boldsymbol{\\beta}\\vert\\boldsymbol{D}).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "38568afe",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Bayes' theorem comes to our rescue here since (omitting the normalization constant)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c3d0eeb9",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(\\boldsymbol{\\beta}\\vert\\boldsymbol{D})\\propto p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})p(\\boldsymbol{\\beta}).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1d0ba152",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"We have a model for $p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})$ but need one for the **prior** $p(\\boldsymbol{\\beta}$!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f936c1ce",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Ridge and Bayes\n",
|
|
"\n",
|
|
"With the posterior probability defined by a likelihood which we have\n",
|
|
"already modeled and an unknown prior, we are now ready to make\n",
|
|
"additional models for the prior.\n",
|
|
"\n",
|
|
"We can, based on our discussions of the variance of $\\boldsymbol{\\beta}$ and the mean value, assume that the prior for the values $\\boldsymbol{\\beta}$ is given by a Gaussian with mean value zero and variance $\\tau^2$, that is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "dd10cea6",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(\\boldsymbol{\\beta})=\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\beta_j^2}{2\\tau^2}\\right)}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2534eb1d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Our posterior probability becomes then (omitting the normalization factor which is just a constant)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "104af119",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(\\boldsymbol{\\beta\\vert\\boldsymbol{D})}=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\beta_j^2}{2\\tau^2}\\right)}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5e8e89cf",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"We can now optimize this quantity with respect to $\\boldsymbol{\\beta}$. As we\n",
|
|
"did for OLS, this is most conveniently done by taking the negative\n",
|
|
"logarithm of the posterior probability. Doing so and leaving out the\n",
|
|
"constants terms that do not depend on $\\beta$, we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "467910cd",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"C(\\boldsymbol{\\beta})=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\frac{1}{2\\tau^2}\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_2^2,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "16f15265",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and replacing $1/2\\tau^2$ with $\\lambda$ we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1cd22464",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"C(\\boldsymbol{\\beta})=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_2^2,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "90676d1e",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which is our Ridge cost function! Nice, isn't it?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2082d057",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"## Lasso and Bayes\n",
|
|
"\n",
|
|
"To derive the Lasso cost function, we simply replace the Gaussian prior with an exponential distribution ([Laplace in this case](https://en.wikipedia.org/wiki/Laplace_distribution)) with zero mean value, that is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "544b15d3",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(\\boldsymbol{\\beta})=\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\vert\\beta_j\\vert}{\\tau}\\right)}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "592804c7",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Our posterior probability becomes then (omitting the normalization factor which is just a constant)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c11e6e06",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"p(\\boldsymbol{\\beta}\\vert\\boldsymbol{D})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\vert\\beta_j\\vert}{\\tau}\\right)}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fdc6e9b5",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"Taking the negative\n",
|
|
"logarithm of the posterior probability and leaving out the\n",
|
|
"constants terms that do not depend on $\\beta$, we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a0e8571d",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"C(\\boldsymbol{\\beta}=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\frac{1}{\\tau}\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "84eca415",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"and replacing $1/\\tau$ with $\\lambda$ we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "0924d8d4",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"$$\n",
|
|
"C(\\boldsymbol{\\beta}=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "7270fef7",
|
|
"metadata": {
|
|
"editable": true
|
|
},
|
|
"source": [
|
|
"which is our Lasso cost function!"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|