{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Data Analysis and Machine Learning: Linear Regression and more Advanced Regression Analysis\n", "\n", " \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: **Sep 11, 2020**\n", "\n", "Copyright 1999-2020, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n", "\n", "\n", "\n", "\n", "\n", "\n", "## Why Linear Regression (aka Ordinary Least Squares and family)\n", "\n", "Fitting a continuous function with linear parameterization in terms of the parameters $\\boldsymbol{\\beta}$.\n", "* Method of choice for fitting a continuous function!\n", "\n", "* Gives an excellent introduction to central Machine Learning features with **understandable pedagogical** links to other methods like **Neural Networks**, **Support Vector Machines** etc\n", "\n", "* Analytical expression for the fitting parameters $\\boldsymbol{\\beta}$\n", "\n", "* Analytical expressions for statistical propertiers like mean values, variances, confidence intervals and more\n", "\n", "* Analytical relation with probabilistic interpretations \n", "\n", "* Easy to introduce basic concepts like bias-variance tradeoff, cross-validation, resampling and regularization techniques and many other ML topics\n", "\n", "* Easy to code! And links well with classification problems and logistic regression and neural networks\n", "\n", "* Allows for **easy** hands-on understanding of gradient descent methods\n", "\n", "* and many more features\n", "\n", "For more discussions of Ridge and Lasso regression, [Wessel van Wieringen's](https://arxiv.org/abs/1509.09169) article is highly recommended.\n", "Similarly, [Mehta et al's article](https://arxiv.org/abs/1803.08823) is also recommended.\n", "\n", "\n", "## Regression analysis, overarching aims\n", "\n", "Regression modeling deals with the description of the sampling distribution of a given random variable $y$ and how it varies as function of another variable or a set of such variables $\\boldsymbol{x} =[x_0, x_1,\\dots, x_{n-1}]^T$. \n", "The first variable is called the **dependent**, the **outcome** or the **response** variable while the set of variables $\\boldsymbol{x}$ is called the independent variable, or the predictor variable or the explanatory variable. \n", "\n", "A regression model aims at finding a likelihood function $p(\\boldsymbol{y}\\vert \\boldsymbol{x})$, that is the conditional distribution for $\\boldsymbol{y}$ with a given $\\boldsymbol{x}$. The estimation of $p(\\boldsymbol{y}\\vert \\boldsymbol{x})$ is made using a data set with \n", "* $n$ cases $i = 0, 1, 2, \\dots, n-1$ \n", "\n", "* Response (target, dependent or outcome) variable $y_i$ with $i = 0, 1, 2, \\dots, n-1$ \n", "\n", "* $p$ so-called explanatory (independent or predictor) variables $\\boldsymbol{x}_i=[x_{i0}, x_{i1}, \\dots, x_{ip-1}]$ with $i = 0, 1, 2, \\dots, n-1$ and explanatory variables running from $0$ to $p-1$. See below for more explicit examples. \n", "\n", " The goal of the regression analysis is to extract/exploit relationship between $\\boldsymbol{y}$ and $\\boldsymbol{x}$ in or to infer causal dependencies, approximations to the likelihood functions, functional relationships and to make predictions, making fits and many other things.\n", "\n", "\n", "\n", "## Regression analysis, overarching aims II\n", "\n", "\n", "Consider an experiment in which $p$ characteristics of $n$ samples are\n", "measured. The data from this experiment, for various explanatory variables $p$ are normally represented by a matrix \n", "$\\mathbf{X}$.\n", "\n", "The matrix $\\mathbf{X}$ is called the *design\n", "matrix*. Additional information of the samples is available in the\n", "form of $\\boldsymbol{y}$ (also as above). The variable $\\boldsymbol{y}$ is\n", "generally referred to as the *response variable*. The aim of\n", "regression analysis is to explain $\\boldsymbol{y}$ in terms of\n", "$\\boldsymbol{X}$ through a functional relationship like $y_i =\n", "f(\\mathbf{X}_{i,\\ast})$. When no prior knowledge on the form of\n", "$f(\\cdot)$ is available, it is common to assume a linear relationship\n", "between $\\boldsymbol{X}$ and $\\boldsymbol{y}$. This assumption gives rise to\n", "the *linear regression model* where $\\boldsymbol{\\beta} = [\\beta_0, \\ldots,\n", "\\beta_{p-1}]^{T}$ are the *regression parameters*. \n", "\n", "Linear regression gives us a set of analytical equations for the parameters $\\beta_j$.\n", "\n", "\n", "\n", "\n", "\n", "## Examples\n", "In order to understand the relation among the predictors $p$, the set of data $n$ and the target (outcome, output etc) $\\boldsymbol{y}$,\n", "consider the model we discussed for describing nuclear binding energies. \n", "\n", "There we assumed that we could parametrize the data using a polynomial approximation based on the liquid drop model.\n", "Assuming" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "BE(A) = a_0+a_1A+a_2A^{2/3}+a_3A^{-1/3}+a_4A^{-1},\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we have five predictors, that is the intercept, the $A$ dependent term, the $A^{2/3}$ term and the $A^{-1/3}$ and $A^{-1}$ terms.\n", "This gives $p=0,1,2,3,4$. Furthermore we have $n$ entries for each predictor. It means that our design matrix is a \n", "$p\\times n$ matrix $\\boldsymbol{X}$.\n", "\n", "Here the predictors are based on a model we have made. A popular data set which is widely encountered in ML applications is the\n", "so-called [credit card default data from Taiwan](https://www.sciencedirect.com/science/article/pii/S0957417407006719?via%3Dihub). The data set contains data on $n=30000$ credit card holders with predictors like gender, marital status, age, profession, education, etc. In total there are $24$ such predictors or attributes leading to a design matrix of dimensionality $24 \\times 30000$. This is however a classification problem and we will come back to it when we discuss Logistic Regression.\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "## General linear models\n", "Before we proceed let us study a case from linear algebra where we aim at fitting a set of data $\\boldsymbol{y}=[y_0,y_1,\\dots,y_{n-1}]$. We could think of these data as a result of an experiment or a complicated numerical experiment. These data are functions of a series of variables $\\boldsymbol{x}=[x_0,x_1,\\dots,x_{n-1}]$, that is $y_i = y(x_i)$ with $i=0,1,2,\\dots,n-1$. The variables $x_i$ could represent physical quantities like time, temperature, position etc. We assume that $y(x)$ is a smooth function. \n", "\n", "Since obtaining these data points may not be trivial, we want to use these data to fit a function which can allow us to make predictions for values of $y$ which are not in the present set. The perhaps simplest approach is to assume we can parametrize our function in terms of a polynomial of degree $n-1$ with $n$ points, that is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "y=y(x) \\rightarrow y(x_i)=\\tilde{y}_i+\\epsilon_i=\\sum_{j=0}^{n-1} \\beta_j x_i^j+\\epsilon_i,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where $\\epsilon_i$ is the error in our approximation.\n", "\n", "\n", "\n", "\n", "## Rewriting the fitting procedure as a linear algebra problem\n", "For every set of values $y_i,x_i$ we have thus the corresponding set of equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\begin{align*}\n", "y_0&=\\beta_0+\\beta_1x_0^1+\\beta_2x_0^2+\\dots+\\beta_{n-1}x_0^{n-1}+\\epsilon_0\\\\\n", "y_1&=\\beta_0+\\beta_1x_1^1+\\beta_2x_1^2+\\dots+\\beta_{n-1}x_1^{n-1}+\\epsilon_1\\\\\n", "y_2&=\\beta_0+\\beta_1x_2^1+\\beta_2x_2^2+\\dots+\\beta_{n-1}x_2^{n-1}+\\epsilon_2\\\\\n", "\\dots & \\dots \\\\\n", "y_{n-1}&=\\beta_0+\\beta_1x_{n-1}^1+\\beta_2x_{n-1}^2+\\dots+\\beta_{n-1}x_{n-1}^{n-1}+\\epsilon_{n-1}.\\\\\n", "\\end{align*}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rewriting the fitting procedure as a linear algebra problem, more details\n", "Defining the vectors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{y} = [y_0,y_1, y_2,\\dots, y_{n-1}]^T,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\beta} = [\\beta_0,\\beta_1, \\beta_2,\\dots, \\beta_{n-1}]^T,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\epsilon} = [\\epsilon_0,\\epsilon_1, \\epsilon_2,\\dots, \\epsilon_{n-1}]^T,\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and the design matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}=\n", "\\begin{bmatrix} \n", "1& x_{0}^1 &x_{0}^2& \\dots & \\dots &x_{0}^{n-1}\\\\\n", "1& x_{1}^1 &x_{1}^2& \\dots & \\dots &x_{1}^{n-1}\\\\\n", "1& x_{2}^1 &x_{2}^2& \\dots & \\dots &x_{2}^{n-1}\\\\ \n", "\\dots& \\dots &\\dots& \\dots & \\dots &\\dots\\\\\n", "1& x_{n-1}^1 &x_{n-1}^2& \\dots & \\dots &x_{n-1}^{n-1}\\\\\n", "\\end{bmatrix}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we can rewrite our equations as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{y} = \\boldsymbol{X}\\boldsymbol{\\beta}+\\boldsymbol{\\epsilon}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above design matrix is called a [Vandermonde matrix](https://en.wikipedia.org/wiki/Vandermonde_matrix).\n", "\n", "\n", "\n", "\n", "## Generalizing the fitting procedure as a linear algebra problem\n", "\n", "We are obviously not limited to the above polynomial expansions. We\n", "could replace the various powers of $x$ with elements of Fourier\n", "series or instead of $x_i^j$ we could have $\\cos{(j x_i)}$ or $\\sin{(j\n", "x_i)}$, or time series or other orthogonal functions. For every set\n", "of values $y_i,x_i$ we can then generalize the equations to" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\begin{align*}\n", "y_0&=\\beta_0x_{00}+\\beta_1x_{01}+\\beta_2x_{02}+\\dots+\\beta_{n-1}x_{0n-1}+\\epsilon_0\\\\\n", "y_1&=\\beta_0x_{10}+\\beta_1x_{11}+\\beta_2x_{12}+\\dots+\\beta_{n-1}x_{1n-1}+\\epsilon_1\\\\\n", "y_2&=\\beta_0x_{20}+\\beta_1x_{21}+\\beta_2x_{22}+\\dots+\\beta_{n-1}x_{2n-1}+\\epsilon_2\\\\\n", "\\dots & \\dots \\\\\n", "y_{i}&=\\beta_0x_{i0}+\\beta_1x_{i1}+\\beta_2x_{i2}+\\dots+\\beta_{n-1}x_{in-1}+\\epsilon_i\\\\\n", "\\dots & \\dots \\\\\n", "y_{n-1}&=\\beta_0x_{n-1,0}+\\beta_1x_{n-1,2}+\\beta_2x_{n-1,2}+\\dots+\\beta_{n-1}x_{n-1,n-1}+\\epsilon_{n-1}.\\\\\n", "\\end{align*}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note that we have $p=n$ here. The matrix is symmetric. This is generally not the case!**\n", "\n", "\n", "\n", "\n", "## Generalizing the fitting procedure as a linear algebra problem\n", "We redefine in turn the matrix $\\boldsymbol{X}$ as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}=\n", "\\begin{bmatrix} \n", "x_{00}& x_{01} &x_{02}& \\dots & \\dots &x_{0,n-1}\\\\\n", "x_{10}& x_{11} &x_{12}& \\dots & \\dots &x_{1,n-1}\\\\\n", "x_{20}& x_{21} &x_{22}& \\dots & \\dots &x_{2,n-1}\\\\ \n", "\\dots& \\dots &\\dots& \\dots & \\dots &\\dots\\\\\n", "x_{n-1,0}& x_{n-1,1} &x_{n-1,2}& \\dots & \\dots &x_{n-1,n-1}\\\\\n", "\\end{bmatrix}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and without loss of generality we rewrite again our equations as" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\boldsymbol{y} = \\boldsymbol{X}\\boldsymbol{\\beta}+\\boldsymbol{\\epsilon}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The left-hand side of this equation is kwown. Our error vector $\\boldsymbol{\\epsilon}$ and the parameter vector $\\boldsymbol{\\beta}$ are our unknow quantities. How can we obtain the optimal set of $\\beta_i$ values?\n", "\n", "\n", "\n", "\n", "## Optimizing our parameters\n", "We have defined the matrix $\\boldsymbol{X}$ via the equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\begin{align*}\n", "y_0&=\\beta_0x_{00}+\\beta_1x_{01}+\\beta_2x_{02}+\\dots+\\beta_{n-1}x_{0n-1}+\\epsilon_0\\\\\n", "y_1&=\\beta_0x_{10}+\\beta_1x_{11}+\\beta_2x_{12}+\\dots+\\beta_{n-1}x_{1n-1}+\\epsilon_1\\\\\n", "y_2&=\\beta_0x_{20}+\\beta_1x_{21}+\\beta_2x_{22}+\\dots+\\beta_{n-1}x_{2n-1}+\\epsilon_1\\\\\n", "\\dots & \\dots \\\\\n", "y_{i}&=\\beta_0x_{i0}+\\beta_1x_{i1}+\\beta_2x_{i2}+\\dots+\\beta_{n-1}x_{in-1}+\\epsilon_1\\\\\n", "\\dots & \\dots \\\\\n", "y_{n-1}&=\\beta_0x_{n-1,0}+\\beta_1x_{n-1,2}+\\beta_2x_{n-1,2}+\\dots+\\beta_{n-1}x_{n-1,n-1}+\\epsilon_{n-1}.\\\\\n", "\\end{align*}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we noted above, we stayed with a system with the design matrix \n", " $\\boldsymbol{X}\\in {\\mathbb{R}}^{n\\times n}$, that is we have $p=n$. For reasons to come later (algorithmic arguments) we will hereafter define \n", "our matrix as $\\boldsymbol{X}\\in {\\mathbb{R}}^{n\\times p}$, with the predictors refering to the column numbers and the entries $n$ being the row elements.\n", "\n", "\n", "\n", "\n", "## Our model for the nuclear binding energies\n", "\n", "In our [introductory notes](https://compphysics.github.io/MachineLearning/doc/pub/How2ReadData/html/How2ReadData.html) we looked at the so-called [liquid drop model](https://en.wikipedia.org/wiki/Semi-empirical_mass_formula). Let us remind ourselves about what we did by looking at the code.\n", "\n", "We restate the parts of the code we are most interested in." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | 1 | \n", "A | \n", "A^(2/3) | \n", "A^(-1/3) | \n", "1/A | \n", "
|---|---|---|---|---|---|
| A | \n", "\n", " | \n", " | \n", " | \n", " | \n", " |
| 1 | \n", "1.0 | \n", "1.0 | \n", "1.000000 | \n", "1.000000 | \n", "1.000000 | \n", "
| 2 | \n", "1.0 | \n", "2.0 | \n", "1.587401 | \n", "0.793701 | \n", "0.500000 | \n", "
| 3 | \n", "1.0 | \n", "3.0 | \n", "2.080084 | \n", "0.693361 | \n", "0.333333 | \n", "
| 4 | \n", "1.0 | \n", "4.0 | \n", "2.519842 | \n", "0.629961 | \n", "0.250000 | \n", "
| 5 | \n", "1.0 | \n", "5.0 | \n", "2.924018 | \n", "0.584804 | \n", "0.200000 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 264 | \n", "1.0 | \n", "264.0 | \n", "41.153106 | \n", "0.155883 | \n", "0.003788 | \n", "
| 265 | \n", "1.0 | \n", "265.0 | \n", "41.256962 | \n", "0.155687 | \n", "0.003774 | \n", "
| 266 | \n", "1.0 | \n", "266.0 | \n", "41.360688 | \n", "0.155491 | \n", "0.003759 | \n", "
| 269 | \n", "1.0 | \n", "269.0 | \n", "41.671089 | \n", "0.154911 | \n", "0.003717 | \n", "
| 270 | \n", "1.0 | \n", "270.0 | \n", "41.774300 | \n", "0.154720 | \n", "0.003704 | \n", "
267 rows × 5 columns
\n", "