update of notes

This commit is contained in:
Morten Hjorth-Jensen
2022-12-11 22:07:05 +01:00
parent 8051880d95
commit 5473efe28f
70 changed files with 3777 additions and 1484 deletions
+305 -85
View File
@@ -2,12 +2,32 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"id": "f63b8c1a",
"metadata": {
"editable": true
},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html chapter4.do.txt -->"
]
},
{
"cell_type": "markdown",
"id": "9a284cf5",
"metadata": {
"editable": true
},
"source": [
"# Logistic Regression"
]
},
{
"cell_type": "markdown",
"id": "d78b73e2",
"metadata": {
"editable": true
},
"source": [
"# Logistic Regression\n",
"\n",
"\n",
"\n",
"## Logistic Regression\n",
"\n",
"In linear regression our main interest was centered on learning the\n",
@@ -22,7 +42,6 @@
"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",
@@ -39,7 +58,6 @@
"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",
@@ -52,10 +70,16 @@
"\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",
"models, as we will see later."
]
},
{
"cell_type": "markdown",
"id": "d0c829b4",
"metadata": {
"editable": true
},
"source": [
"## Basics\n",
"\n",
"We consider the case where the dependent variables, also called the\n",
@@ -76,7 +100,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "f1f4d5f2",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"y_i = \\begin{bmatrix} 0 & \\mathrm{no}\\\\ 1 & \\mathrm{yes} \\end{bmatrix}.\n",
@@ -85,7 +112,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "8e2f836b",
"metadata": {
"editable": true
},
"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",
@@ -98,7 +128,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "6b58f1ba",
"metadata": {
"editable": true
},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto1\"></div>\n",
@@ -113,12 +146,14 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "54e3604b",
"metadata": {
"editable": true
},
"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",
@@ -134,13 +169,13 @@
"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": null,
"execution_count": 1,
"id": "cd1dd1c5",
"metadata": {
"collapsed": false,
"editable": true
@@ -207,14 +242,18 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "2e06ee82",
"metadata": {
"editable": true
},
"source": [
"What we could attempt however is to plot the mean value for each group."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "a774f001",
"metadata": {
"collapsed": false,
"editable": true
@@ -233,7 +272,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "e102fa56",
"metadata": {
"editable": true
},
"source": [
"We are now trying to find a function $f(y\\vert x)$, that is a function which gives us an expected value for the output $y$ with a given input $x$.\n",
"In standard linear regression with a linear dependence on $x$, we would write this in terms of our model"
@@ -241,7 +283,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "75d995ba",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"f(y_i\\vert x_i)=\\beta_0+\\beta_1 x_i.\n",
@@ -250,7 +295,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "e2cacca5",
"metadata": {
"editable": true
},
"source": [
"This expression implies however that $f(y_i\\vert x_i)$ could take any\n",
"value from minus infinity to plus infinity. If we however let\n",
@@ -261,9 +309,16 @@
"us to a very popular model for the function $f$, namely the so-called\n",
"Sigmoid function or logistic model. We will consider this function as\n",
"representing the probability for finding a value of $y_i$ with a given\n",
"$x_i$.\n",
"\n",
"\n",
"$x_i$."
]
},
{
"cell_type": "markdown",
"id": "af9ac586",
"metadata": {
"editable": true
},
"source": [
"## The logistic function\n",
"\n",
"Another widely studied model, is the so-called \n",
@@ -281,7 +336,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "80aa3cbe",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"p(t) = \\frac{1}{1+\\mathrm \\exp{-t}}=\\frac{\\exp{t}}{1+\\mathrm \\exp{t}}.\n",
@@ -290,19 +348,30 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "28d49915",
"metadata": {
"editable": true
},
"source": [
"Note that $1-p(t)= p(-t)$.\n",
"\n",
"## Examples of likelihood functions used in logistic regression and nueral networks\n",
"\n",
"Note that $1-p(t)= p(-t)$."
]
},
{
"cell_type": "markdown",
"id": "eeff6cd9",
"metadata": {
"editable": true
},
"source": [
"## Examples of likelihood functions used in logistic regression and neural networks\n",
"\n",
"The following code plots the logistic function, the step function and other functions we will encounter from here and on."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "5ecec05e",
"metadata": {
"collapsed": false,
"editable": true
@@ -367,14 +436,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "5802efe3",
"metadata": {
"editable": true
},
"source": [
"We assume now that we have two classes with $y_i$ either $0$ or $1$. Furthermore we assume also that we have only two parameters $\\beta$ in our fitting of the Sigmoid function, that is we define probabilities"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "f85d14ea",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\begin{align*}\n",
@@ -386,7 +461,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "6c4e0334",
"metadata": {
"editable": true
},
"source": [
"where $\\boldsymbol{\\beta}$ are the weights we wish to extract from data, in our case $\\beta_0$ and $\\beta_1$. \n",
"\n",
@@ -395,7 +473,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "df7facc9",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"p(y_i=0\\vert x_i, \\boldsymbol{\\beta}) = 1-p(y_i=1\\vert x_i, \\boldsymbol{\\beta}).\n",
@@ -404,7 +485,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "a920e6d8",
"metadata": {
"editable": true
},
"source": [
"In order to define the total likelihood for all possible outcomes from a \n",
"dataset $\\mathcal{D}=\\{(y_i,x_i)\\}$, with the binary labels\n",
@@ -416,7 +500,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "a780bfe1",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\begin{align*}\n",
@@ -427,14 +514,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "9f810a44",
"metadata": {
"editable": true
},
"source": [
"from which we obtain the log-likelihood and our **cost/loss** function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "3fac4ef4",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathcal{C}(\\boldsymbol{\\beta}) = \\sum_{i=1}^n \\left( y_i\\log{p(y_i=1|x_i,\\boldsymbol{\\beta})} + (1-y_i)\\log\\left[1-p(y_i=1|x_i,\\boldsymbol{\\beta}))\\right]\\right).\n",
@@ -443,14 +536,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "75aa2c14",
"metadata": {
"editable": true
},
"source": [
"Reordering the logarithms, we can rewrite the **cost/loss** function as"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "780f2038",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathcal{C}(\\boldsymbol{\\beta}) = \\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{(1+\\exp{(\\beta_0+\\beta_1x_i)})}\\right).\n",
@@ -459,7 +558,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "c8c940aa",
"metadata": {
"editable": true
},
"source": [
"The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to $\\beta$.\n",
"Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that"
@@ -467,7 +569,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "9d4a527b",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathcal{C}(\\boldsymbol{\\beta})=-\\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{(1+\\exp{(\\beta_0+\\beta_1x_i)})}\\right).\n",
@@ -476,23 +581,27 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "c1706606",
"metadata": {
"editable": true
},
"source": [
"This equation is known in statistics as the **cross entropy**. Finally, we note that just as in linear regression, \n",
"in practice we often supplement the cross-entropy with additional regularization terms, usually $L_1$ and $L_2$ regularization as we did for Ridge and Lasso regression.\n",
"\n",
"\n",
"The cross entropy is a convex function of the weights $\\boldsymbol{\\beta}$ and,\n",
"therefore, any local minimizer is a global minimizer. \n",
"\n",
"\n",
"Minimizing this\n",
"cost function with respect to the two parameters $\\beta_0$ and $\\beta_1$ we obtain"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "fe8fb387",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\beta_0} = -\\sum_{i=1}^n \\left(y_i -\\frac{\\exp{(\\beta_0+\\beta_1x_i)}}{1+\\exp{(\\beta_0+\\beta_1x_i)}}\\right),\n",
@@ -501,14 +610,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "bb564e81",
"metadata": {
"editable": true
},
"source": [
"and"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "700c5443",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\beta_1} = -\\sum_{i=1}^n \\left(y_ix_i -x_i\\frac{\\exp{(\\beta_0+\\beta_1x_i)}}{1+\\exp{(\\beta_0+\\beta_1x_i)}}\\right).\n",
@@ -517,7 +632,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "f442c956",
"metadata": {
"editable": true
},
"source": [
"Let us now define a vector $\\boldsymbol{y}$ with $n$ elements $y_i$, an\n",
"$n\\times p$ matrix $\\boldsymbol{X}$ which contains the $x_i$ values and a\n",
@@ -527,7 +645,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "83937f6f",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}} = -\\boldsymbol{X}^T\\left(\\boldsymbol{y}-\\boldsymbol{p}\\right).\n",
@@ -536,7 +657,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "71819c1d",
"metadata": {
"editable": true
},
"source": [
"If we in addition define a diagonal matrix $\\boldsymbol{W}$ with elements \n",
"$p(y_i\\vert x_i,\\boldsymbol{\\beta})(1-p(y_i\\vert x_i,\\boldsymbol{\\beta})$, we can obtain a compact expression of the second derivative as"
@@ -544,7 +668,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "23dfd975",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\frac{\\partial^2 \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}\\partial \\boldsymbol{\\beta}^T} = \\boldsymbol{X}^T\\boldsymbol{W}\\boldsymbol{X}.\n",
@@ -553,14 +680,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "6ca9f74b",
"metadata": {
"editable": true
},
"source": [
"Within a binary classification problem, we can easily expand our model to include multiple predictors. Our ratio between likelihoods is then with $p$ predictors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "ddb3d93a",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\log{ \\frac{p(\\boldsymbol{\\beta}\\boldsymbol{x})}{1-p(\\boldsymbol{\\beta}\\boldsymbol{x})}} = \\beta_0+\\beta_1x_1+\\beta_2x_2+\\dots+\\beta_px_p.\n",
@@ -569,14 +702,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "0cfae560",
"metadata": {
"editable": true
},
"source": [
"Here we defined $\\boldsymbol{x}=[1,x_1,x_2,\\dots,x_p]$ and $\\boldsymbol{\\beta}=[\\beta_0, \\beta_1, \\dots, \\beta_p]$ leading to"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "7c427fc3",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"p(\\boldsymbol{\\beta}\\boldsymbol{x})=\\frac{ \\exp{(\\beta_0+\\beta_1x_1+\\beta_2x_2+\\dots+\\beta_px_p)}}{1+\\exp{(\\beta_0+\\beta_1x_1+\\beta_2x_2+\\dots+\\beta_px_p)}}.\n",
@@ -585,7 +724,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "6a6814bf",
"metadata": {
"editable": true
},
"source": [
"Till now we have mainly focused on two classes, the so-called binary\n",
"system. Suppose we wish to extend to $K$ classes. Let us for the sake\n",
@@ -594,7 +736,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "85282137",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\log{\\frac{p(C=1\\vert x)}{p(K\\vert x)}} = \\beta_{10}+\\beta_{11}x_1,\n",
@@ -603,14 +748,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "4864f76d",
"metadata": {
"editable": true
},
"source": [
"and"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "390e9a55",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\log{\\frac{p(C=2\\vert x)}{p(K\\vert x)}} = \\beta_{20}+\\beta_{21}x_1,\n",
@@ -619,14 +770,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "c3f39c44",
"metadata": {
"editable": true
},
"source": [
"and so on till the class $C=K-1$ class"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "92f32a03",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\log{\\frac{p(C=K-1\\vert x)}{p(K\\vert x)}} = \\beta_{(K-1)0}+\\beta_{(K-1)1}x_1,\n",
@@ -635,13 +792,14 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "677a5c4d",
"metadata": {
"editable": true
},
"source": [
"and the model is specified in term of $K-1$ so-called log-odds or\n",
"**logit** transformations.\n",
"\n",
"\n",
"\n",
"In our discussion of neural networks we will encounter the above again\n",
"in terms of a slightly modified function, the so-called **Softmax** function.\n",
"\n",
@@ -658,7 +816,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "fa70e3bf",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"p(C=k\\vert \\mathbf {x} )=\\frac{\\exp{(\\beta_{k0}+\\beta_{k1}x_1)}}{1+\\sum_{l=1}^{K-1}\\exp{(\\beta_{l0}+\\beta_{l1}x_1)}}.\n",
@@ -667,14 +828,20 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "4c59400c",
"metadata": {
"editable": true
},
"source": [
"It is easy to extend to more predictors. The final class is"
]
},
{
"cell_type": "markdown",
"metadata": {},
"id": "f4073652",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"p(C=K\\vert \\mathbf {x} )=\\frac{1}{1+\\sum_{l=1}^{K-1}\\exp{(\\beta_{l0}+\\beta_{l1}x_1)}},\n",
@@ -683,7 +850,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "810e872a",
"metadata": {
"editable": true
},
"source": [
"and they sum to one. Our earlier discussions were all specialized to\n",
"the case with two classes only. It is easy to see from the above that\n",
@@ -692,8 +862,16 @@
"To find the optimal parameters we would typically use a gradient\n",
"descent method. Newton's method and gradient descent methods are\n",
"discussed in the material on [optimization\n",
"methods](https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html).\n",
"\n",
"methods](https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html)."
]
},
{
"cell_type": "markdown",
"id": "f4ebc1da",
"metadata": {
"editable": true
},
"source": [
"## Wisconsin Cancer Data\n",
"\n",
"We show here how we can use a simple regression case on the breast\n",
@@ -703,7 +881,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "b7a8cbac",
"metadata": {
"collapsed": false,
"editable": true
@@ -739,7 +918,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "10f74b93",
"metadata": {
"editable": true
},
"source": [
"In addition to the above scores, we could also study the covariance (and the correlation matrix).\n",
"We use **Pandas** to compute the correlation matrix."
@@ -747,7 +929,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "d0b8025d",
"metadata": {
"collapsed": false,
"editable": true
@@ -792,7 +975,10 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "9d84a2aa",
"metadata": {
"editable": true
},
"source": [
"In the above example we note two things. In the first plot we display\n",
"the overlap of benign and malignant tumors as functions of the various\n",
@@ -811,7 +997,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "6144ea0a",
"metadata": {
"collapsed": false,
"editable": true
@@ -823,14 +1010,18 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "5627f5ae",
"metadata": {
"editable": true
},
"source": [
"and then"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "a7c8662a",
"metadata": {
"collapsed": false,
"editable": true
@@ -842,17 +1033,46 @@
},
{
"cell_type": "markdown",
"metadata": {},
"id": "d03ee2a7",
"metadata": {
"editable": true
},
"source": [
"Diagonalizing this matrix we can in turn say something about which\n",
"features are of relevance and which are not. This leads us to\n",
"the classical Principal Component Analysis (PCA) theorem with\n",
"applications. This will be discussed later this semester ([week 43](https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week43-bs.html))."
"applications. This will be discussed later this semester ([week 43](https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week43-bs.html)).\n",
"\n",
"Here we present a further way to present our results in terms of a so-called **confusion matrix**, the cumulative gain and the **ROC** curve.\n",
"This way of displaying our data are based upon different ways to classify our possible outcomes. Before we proceed we need some definitions.\n",
"1. **TP**: true positive or in other words, something equivalent with a proper classification\n",
"\n",
"2. **TN**: true negative, which is equivalent with a correct rejection\n",
"\n",
"3. **FP**: false positive, or in simpler words something that is equivalent with a false alarm\n",
"\n",
"4. **FN**: false negative, which is mean to be equivalent with a miss.\n",
"\n",
"The total data set is then the sum of the true positive and true negative targets or outputs, labeled by $n$.\n",
"Based on this we can then define the accuracy score as the sum of correctly predicted **TP** and **TN** cases divided by the sum of true positive and treue negative events in our data set, or as"
]
},
{
"cell_type": "markdown",
"id": "44ce5095",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathrm{Accuracy} = \\frac{\\sum_{i=0}^{n-1}I(y_i=\\tilde{y}_i)}{n}.\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "24ff3dd3",
"metadata": {
"collapsed": false,
"editable": true
@@ -908,5 +1128,5 @@
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
"nbformat_minor": 5
}