{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Logistic Regression\n", "\n", "\n", "\n", "## Logistic Regression\n", "\n", "In linear regression our main interest was centered on learning the\n", "coefficients of a functional fit (say a polynomial) in order to be\n", "able to predict the response of a continuous variable on some unseen\n", "data. The fit to the continuous variable $y_i$ is based on some\n", "independent variables $x_i$. Linear regression resulted in\n", "analytical expressions for standard ordinary Least Squares or Ridge\n", "regression (in terms of matrices to invert) for several quantities,\n", "ranging from the variance and thereby the confidence intervals of the\n", "optimal parameters $\\hat{\\beta}$ to the mean squared error. If we can invert\n", "the product of the design matrices, linear regression gives then a\n", "simple recipe for fitting our data.\n", "\n", "\n", "Classification problems, however, are concerned with outcomes taking\n", "the form of discrete variables (i.e. categories). We may for example,\n", "on the basis of DNA sequencing for a number of patients, like to find\n", "out which mutations are important for a certain disease; or based on\n", "scans of various patients' brains, figure out if there is a tumor or\n", "not; or given a specific physical system, we'd like to identify its\n", "state, say whether it is an ordered or disordered system (typical\n", "situation in solid state physics); or classify the status of a\n", "patient, whether she/he has a stroke or not and many other similar\n", "situations.\n", "\n", "The most common situation we encounter when we apply logistic\n", "regression is that of two possible outcomes, normally denoted as a\n", "binary outcome, true or false, positive or negative, success or\n", "failure etc.\n", "\n", "\n", "Logistic regression will also serve as our stepping stone towards\n", "neural network algorithms and supervised deep learning. For logistic\n", "learning, the minimization of the cost function leads to a non-linear\n", "equation in the parameters $\\hat{\\beta}$. The optimization of the\n", "problem calls therefore for minimization algorithms. This forms the\n", "bottle neck of all machine learning algorithms, namely how to find\n", "reliable minima of a multi-variable function. This leads us to the\n", "family of gradient descent methods. The latter are the working horses\n", "of basically all modern machine learning algorithms.\n", "\n", "We note also that many of the topics discussed here on logistic \n", "regression are also commonly used in modern supervised Deep Learning\n", "models, as we will see later.\n", "\n", "\n", "\n", "## Basics\n", "\n", "We consider the case where the dependent variables, also called the\n", "responses or the outcomes, $y_i$ are discrete and only take values\n", "from $k=0,\\dots,K-1$ (i.e. $K$ classes).\n", "\n", "The goal is to predict the\n", "output classes from the design matrix $\\boldsymbol{X}\\in\\mathbb{R}^{n\\times p}$\n", "made of $n$ samples, each of which carries $p$ features or predictors. The\n", "primary goal is to identify the classes to which new unseen samples\n", "belong.\n", "\n", "Let us specialize to the case of two classes only, with outputs\n", "$y_i=0$ and $y_i=1$. Our outcomes could represent the status of a\n", "credit card user that could default or not on her/his credit card\n", "debt. That is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "y_i = \\begin{bmatrix} 0 & \\mathrm{no}\\\\ 1 & \\mathrm{yes} \\end{bmatrix}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before moving to the logistic model, let us try to use our linear\n", "regression model to classify these two outcomes. We could for example\n", "fit a linear model to the default case if $y_i > 0.5$ and the no\n", "default case $y_i \\leq 0.5$.\n", "\n", "We would then have our \n", "weighted linear combination, namely" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "\n", "$$\n", "\\begin{equation}\n", "\\boldsymbol{y} = \\boldsymbol{X}^T\\boldsymbol{\\beta} + \\boldsymbol{\\epsilon},\n", "\\label{_auto1} \\tag{1}\n", "\\end{equation}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where $\\boldsymbol{y}$ is a vector representing the possible outcomes, $\\boldsymbol{X}$ is our\n", "$n\\times p$ design matrix and $\\boldsymbol{\\beta}$ represents our estimators/predictors.\n", "\n", "\n", "The main problem with our function is that it takes values on the\n", "entire real axis. In the case of logistic regression, however, the\n", "labels $y_i$ are discrete variables. A typical example is the credit\n", "card data discussed below here, where we can set the state of\n", "defaulting the debt to $y_i=1$ and not to $y_i=0$ for one the persons\n", "in the data set (see the full example below).\n", "\n", "One simple way to get a discrete output is to have sign\n", "functions that map the output of a linear regressor to values $\\{0,1\\}$,\n", "$f(s_i)=sign(s_i)=1$ if $s_i\\ge 0$ and 0 if otherwise. \n", "We will encounter this model in our first demonstration of neural networks. Historically it is called the ``perceptron\" model in the machine learning\n", "literature. This model is extremely simple. However, in many cases it is more\n", "favorable to use a ``soft\" classifier that outputs\n", "the probability of a given category. This leads us to the logistic function.\n", "\n", "\n", "The following example on data for coronary heart disease (CHD) as function of age may serve as an illustration. In the code here we read and plot whether a person has had CHD (output = 1) or not (output = 0). This ouput is plotted the person's against age. Clearly, the figure shows that attempting to make a standard linear regression fit may not be very meaningful." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "data": { "text/html": [ "| \n", " | ID | \n", "Age | \n", "Agegroup | \n", "CHD | \n", "
|---|---|---|---|---|
| 0 | \n", "1 | \n", "21 | \n", "1 | \n", "0 | \n", "
| 1 | \n", "2 | \n", "23 | \n", "1 | \n", "0 | \n", "
| 2 | \n", "3 | \n", "25 | \n", "1 | \n", "1 | \n", "
| 3 | \n", "4 | \n", "29 | \n", "1 | \n", "0 | \n", "
| 4 | \n", "5 | \n", "21 | \n", "1 | \n", "0 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 95 | \n", "96 | \n", "61 | \n", "8 | \n", "1 | \n", "
| 96 | \n", "97 | \n", "69 | \n", "8 | \n", "1 | \n", "
| 97 | \n", "98 | \n", "65 | \n", "8 | \n", "1 | \n", "
| 98 | \n", "99 | \n", "64 | \n", "8 | \n", "1 | \n", "
| 99 | \n", "100 | \n", "63 | \n", "8 | \n", "0 | \n", "
100 rows × 4 columns
\n", "