diff --git a/doc/pub/week36/html/week36-bs.html b/doc/pub/week36/html/week36-bs.html index 11aacc49a..63cff1914 100644 --- a/doc/pub/week36/html/week36-bs.html +++ b/doc/pub/week36/html/week36-bs.html @@ -88,6 +88,11 @@ Automatically generated HTML file from DocOnce source ('Yet another Example', 2, None, 'yet-another-example'), ('The OLS case', 2, None, 'the-ols-case'), ('The Ridge case', 2, None, 'the-ridge-case'), + ('Writing the Cost Function', + 2, + None, + 'writing-the-cost-function'), + ('Lasso case', 2, None, 'lasso-case'), ('Linking the regression analysis with a statistical ' 'interpretation', 2, @@ -255,40 +260,42 @@ MathJax.Hub.Config({
-
@@ -347,7 +354,7 @@ MathJax.Hub.Config({
-
@@ -654,7 +654,7 @@ $$
-Plotting these results (figure to come) shows clearly that Lasso regression suppresses (sets to zero) values of \( \beta_i \) for specific values of \( \lambda \). Ridge regression reduces on the hand the values of \( \beta_i \) as function of \( \lambda \). +Plotting these results (figure in handwritten notes for week 36) 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 \).
We will now couple the discussions of ordinary least squares, Ridge and Lasso regression with a statistical interpretation, that is we move from a linear algebra analysis to a statistical analysis. In particular, we will focus on what the regularization terms can result in. @@ -677,7 +677,7 @@ $$ and our inputs as a \( 3\times 2 \) design matrix
$$
-\boldsymbol{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 1 & 0\end{bmatrix},
+\boldsymbol{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 0 & 0\end{bmatrix},
$$
@@ -701,13 +701,10 @@ Inserting the above values we obtain that
$$
-\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}\frac{11}{5} \\ 2\end{bmatrix},
+\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}2 \\ 2\end{bmatrix},
$$
-
-Computing the mean squared error we obtian a value of \( 0.27 \). -
The code which implements this simpler case is presented after the discussion of Ridge and Lasso. @@ -729,7 +726,7 @@ Inserting the above values we obtain that
$$
-\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{11}{5+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix},
+\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{8}{4+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix},
$$
+We define the MSE without the \( 1/n \) factor and have then, using that
+
+Using the constraint for \( \beta_0^2+\beta_1^2=1 \) we can constrain \( \lambda \) by solving
+
+For Lasso we need now, keeping the same constraint on \( \beta_0^2+\beta_1^2=1 \), to take the derivative of the absolute values of \( \beta_0 \)
+and \( beta_1 \). This gives us the following derivatives of the cost function
+
-
-Plotting these results (figure to come) shows clearly that Lasso regression suppresses (sets to zero) values of \( \beta_i \) for specific values of \( \lambda \). Ridge regression reduces on the hand the values of \( \beta_i \) as function of \( \lambda \).
+Plotting these results (figure in handwritten notes for week 36) 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 \).
We will now couple the discussions of ordinary least squares, Ridge and Lasso regression with a statistical interpretation, that is we move from a linear algebra analysis to a statistical analysis. In particular, we will focus on what the regularization terms can result in.
@@ -701,7 +706,7 @@ $$
and our inputs as a \( 3\times 2 \) design matrix
$$
-\boldsymbol{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 1 & 0\end{bmatrix},
+\boldsymbol{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 0 & 0\end{bmatrix},
$$
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.
@@ -721,12 +726,9 @@ $$
Inserting the above values we obtain that
$$
-\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}\frac{11}{5} \\ 2\end{bmatrix},
+\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}2 \\ 2\end{bmatrix},
$$
-
-Computing the mean squared error we obtian a value of \( 0.27 \).
-
The code which implements this simpler case is presented after the discussion of Ridge and Lasso.
@@ -745,7 +747,7 @@ $$
Inserting the above values we obtain that
$$
-\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{11}{5+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix},
+\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{8}{4+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix},
$$
@@ -756,6 +758,70 @@ Let us for simplicity assume now that \( \beta_0^2+\beta_1^2=1 \) as constraint.
To see this, let us write the cost function for Ridge regression.
+
+We define the MSE without the \( 1/n \) factor and have then, using that
+$$
+\boldsymbol{X}\boldsymbol{\beta}=\begin{bmatrix} 2\beta_0 \\ \beta_1 \\0 \end{bmatrix},
+$$
+
+
+$$
+C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\beta_0^2+\beta_1^2),
+$$
+
+and taking the derivative with respect to \( \beta_0 \) we get
+$$
+\beta_0=\frac{8}{4+\lambda},
+$$
+
+and for \( \beta_1 \) we have
+$$
+\beta_1=\frac{2}{1+\lambda},
+$$
+
+
+Using the constraint for \( \beta_0^2+\beta_1^2=1 \) we can constrain \( \lambda \) by solving
+$$
+\left(\frac{8}{4+\lambda}\right)^2+\left(\frac{2}{1+\lambda}\right)^2=1,
+$$
+
+which gives \( \lambda=4.571 \) and \( \beta_0=0.933 \) and \( \beta_1=0.359 \).
+
+
+
+For Lasso we need now, keeping the same constraint on \( \beta_0^2+\beta_1^2=1 \), to take the derivative of the absolute values of \( \beta_0 \)
+and \( beta_1 \). This gives us the following derivatives of the cost function
+$$
+C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\vert\beta_0\vert+\vert\beta_1\vert),
+$$
+
+
+$$
+\frac{\partialC(\boldsymbol{\beta})}{\partial \beta_0}=-2(4-2\beta_0)+\lambda\mathrm{sgn}\beta_0=0,
+$$
+
+and
+$$
+\frac{\partialC(\boldsymbol{\beta})}{\partial \beta_1}=-2(2-\beta_1)+\lambda\mathrm{sgn}\beta_1=0.
+$$
+
+We have now four cases to solve besides the trivial cases \( \beta_0 \) and/or \( \beta_1 \) are zero, namely
+
+
-
-Plotting these results (figure to come) shows clearly that Lasso regression suppresses (sets to zero) values of \( \beta_i \) for specific values of \( \lambda \). Ridge regression reduces on the hand the values of \( \beta_i \) as function of \( \lambda \).
+Plotting these results (figure in handwritten notes for week 36) 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 \).
We will now couple the discussions of ordinary least squares, Ridge and Lasso regression with a statistical interpretation, that is we move from a linear algebra analysis to a statistical analysis. In particular, we will focus on what the regularization terms can result in.
@@ -706,7 +711,7 @@ $$
and our inputs as a \( 3\times 2 \) design matrix
$$
-\boldsymbol{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 1 & 0\end{bmatrix},
+\boldsymbol{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 0 & 0\end{bmatrix},
$$
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.
@@ -726,12 +731,9 @@ $$
Inserting the above values we obtain that
$$
-\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}\frac{11}{5} \\ 2\end{bmatrix},
+\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}2 \\ 2\end{bmatrix},
$$
-
-Computing the mean squared error we obtian a value of \( 0.27 \).
-
The code which implements this simpler case is presented after the discussion of Ridge and Lasso.
@@ -750,7 +752,7 @@ $$
Inserting the above values we obtain that
$$
-\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{11}{5+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix},
+\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{8}{4+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix},
$$
@@ -761,6 +763,70 @@ Let us for simplicity assume now that \( \beta_0^2+\beta_1^2=1 \) as constraint.
To see this, let us write the cost function for Ridge regression.
+
+We define the MSE without the \( 1/n \) factor and have then, using that
+$$
+\boldsymbol{X}\boldsymbol{\beta}=\begin{bmatrix} 2\beta_0 \\ \beta_1 \\0 \end{bmatrix},
+$$
+
+
+$$
+C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\beta_0^2+\beta_1^2),
+$$
+
+and taking the derivative with respect to \( \beta_0 \) we get
+$$
+\beta_0=\frac{8}{4+\lambda},
+$$
+
+and for \( \beta_1 \) we have
+$$
+\beta_1=\frac{2}{1+\lambda},
+$$
+
+
+Using the constraint for \( \beta_0^2+\beta_1^2=1 \) we can constrain \( \lambda \) by solving
+$$
+\left(\frac{8}{4+\lambda}\right)^2+\left(\frac{2}{1+\lambda}\right)^2=1,
+$$
+
+which gives \( \lambda=4.571 \) and \( \beta_0=0.933 \) and \( \beta_1=0.359 \).
+
+
+
+For Lasso we need now, keeping the same constraint on \( \beta_0^2+\beta_1^2=1 \), to take the derivative of the absolute values of \( \beta_0 \)
+and \( beta_1 \). This gives us the following derivatives of the cost function
+$$
+C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\vert\beta_0\vert+\vert\beta_1\vert),
+$$
+
+
+$$
+\frac{\partialC(\boldsymbol{\beta})}{\partial \beta_0}=-2(4-2\beta_0)+\lambda\mathrm{sgn}\beta_0=0,
+$$
+
+and
+$$
+\frac{\partialC(\boldsymbol{\beta})}{\partial \beta_1}=-2(2-\beta_1)+\lambda\mathrm{sgn}\beta_1=0.
+$$
+
+We have now four cases to solve besides the trivial cases \( \beta_0 \) and/or \( \beta_1 \) are zero, namely
+
+
@@ -742,6 +739,85 @@ To see this, let us write the cost function for Ridge regression.
+Writing the Cost Function
+
+
+$$
+\boldsymbol{X}\boldsymbol{\beta}=\begin{bmatrix} 2\beta_0 \\ \beta_1 \\0 \end{bmatrix},
+$$
+
+
+
+$$
+C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\beta_0^2+\beta_1^2),
+$$
+
+
+and taking the derivative with respect to \( \beta_0 \) we get
+
+$$
+\beta_0=\frac{8}{4+\lambda},
+$$
+
+
+and for \( \beta_1 \) we have
+
+$$
+\beta_1=\frac{2}{1+\lambda},
+$$
+
+
+
+$$
+\left(\frac{8}{4+\lambda}\right)^2+\left(\frac{2}{1+\lambda}\right)^2=1,
+$$
+
+
+which gives \( \lambda=4.571 \) and \( \beta_0=0.933 \) and \( \beta_1=0.359 \).
+Lasso case
+
+
+$$
+C(\boldsymbol{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\vert\beta_0\vert+\vert\beta_1\vert),
+$$
+
+
+
+$$
+\frac{\partialC(\boldsymbol{\beta})}{\partial \beta_0}=-2(4-2\beta_0)+\lambda\mathrm{sgn}\beta_0=0,
+$$
+
+
+and
+
+$$
+\frac{\partialC(\boldsymbol{\beta})}{\partial \beta_1}=-2(2-\beta_1)+\lambda\mathrm{sgn}\beta_1=0.
+$$
+
+
+We have now four cases to solve besides the trivial cases \( \beta_0 \) and/or \( \beta_1 \) are zero, namely
+
+
+
+Linking the regression analysis with a statistical interpretation
diff --git a/doc/pub/week36/html/week36-solarized.html b/doc/pub/week36/html/week36-solarized.html
index 1bccf3b26..8fdeadf2e 100644
--- a/doc/pub/week36/html/week36-solarized.html
+++ b/doc/pub/week36/html/week36-solarized.html
@@ -108,6 +108,11 @@ div { text-align: justify; text-justify: inter-word; }
('Yet another Example', 2, None, 'yet-another-example'),
('The OLS case', 2, None, 'the-ols-case'),
('The Ridge case', 2, None, 'the-ridge-case'),
+ ('Writing the Cost Function',
+ 2,
+ None,
+ 'writing-the-cost-function'),
+ ('Lasso case', 2, None, 'lasso-case'),
('Linking the regression analysis with a statistical '
'interpretation',
2,
@@ -260,7 +265,7 @@ MathJax.Hub.Config({
Sep 8, 2021
Sep 9, 2021
@@ -681,7 +686,7 @@ $$
$$
+
+Writing the Cost Function
+
+
+
+Lasso case
+
+
+
+
Linking the regression analysis with a statistical interpretation
diff --git a/doc/pub/week36/html/week36.html b/doc/pub/week36/html/week36.html
index fabda1bf3..293abf5ef 100644
--- a/doc/pub/week36/html/week36.html
+++ b/doc/pub/week36/html/week36.html
@@ -113,6 +113,11 @@ div { text-align: justify; text-justify: inter-word; }
('Yet another Example', 2, None, 'yet-another-example'),
('The OLS case', 2, None, 'the-ols-case'),
('The Ridge case', 2, None, 'the-ridge-case'),
+ ('Writing the Cost Function',
+ 2,
+ None,
+ 'writing-the-cost-function'),
+ ('Lasso case', 2, None, 'lasso-case'),
('Linking the regression analysis with a statistical '
'interpretation',
2,
@@ -265,7 +270,7 @@ MathJax.Hub.Config({
Sep 8, 2021
Sep 9, 2021
@@ -686,7 +691,7 @@ $$
$$
+
+Writing the Cost Function
+
+
+
+Lasso case
+
+
+
+
Linking the regression analysis with a statistical interpretation
diff --git a/doc/pub/week36/ipynb/ipynb-week36-src.tar.gz b/doc/pub/week36/ipynb/ipynb-week36-src.tar.gz
index fae1ac5a5..c989468ec 100644
Binary files a/doc/pub/week36/ipynb/ipynb-week36-src.tar.gz and b/doc/pub/week36/ipynb/ipynb-week36-src.tar.gz differ
diff --git a/doc/pub/week36/ipynb/week36.ipynb b/doc/pub/week36/ipynb/week36.ipynb
index 2c0731daa..920d58234 100644
--- a/doc/pub/week36/ipynb/week36.ipynb
+++ b/doc/pub/week36/ipynb/week36.ipynb
@@ -10,7 +10,7 @@
" \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 8, 2021**\n",
+ "Date: **Sep 9, 2021**\n",
"\n",
"Copyright 1999-2021, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -772,7 +772,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Plotting these results (figure to come) shows clearly that Lasso regression suppresses (sets to zero) values of $\\beta_i$ for specific values of $\\lambda$. Ridge regression reduces on the hand the values of $\\beta_i$ as function of $\\lambda$.\n",
+ "Plotting these results (figure in handwritten notes for week 36) shows clearly that Lasso regression suppresses (sets to zero) values of $\\beta_i$ for specific values of $\\lambda$. Ridge regression reduces on the other hand the values of $\\beta_i$ as function of $\\lambda$.\n",
"\n",
"We will now couple the discussions of ordinary least squares, Ridge and Lasso regression with a statistical interpretation, that is we move from a linear algebra analysis to a statistical analysis. In particular, we will focus on what the regularization terms can result in.\n",
"We will amongst other things show that the regularization parameter can reduce considerably the variance of the parameters $\\beta$.\n",
@@ -804,7 +804,7 @@
"metadata": {},
"source": [
"$$\n",
- "\\boldsymbol{X}=\\begin{bmatrix}2 & 0\\\\ 0 & 1 \\\\ 1 & 0\\end{bmatrix},\n",
+ "\\boldsymbol{X}=\\begin{bmatrix}2 & 0\\\\ 0 & 1 \\\\ 0 & 0\\end{bmatrix},\n",
"$$"
]
},
@@ -840,7 +840,7 @@
"metadata": {},
"source": [
"$$\n",
- "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\begin{bmatrix}\\frac{11}{5} \\\\ 2\\end{bmatrix},\n",
+ "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\begin{bmatrix}2 \\\\ 2\\end{bmatrix},\n",
"$$"
]
},
@@ -848,8 +848,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Computing the mean squared error we obtian a value of $0.27$. \n",
- "\n",
"The code which implements this simpler case is presented after the discussion of Ridge and Lasso.\n",
"\n",
"## The Ridge case\n",
@@ -878,7 +876,7 @@
"metadata": {},
"source": [
"$$\n",
- "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{Ridge}}=\\begin{bmatrix}\\frac{11}{5+\\lambda} \\\\ \\frac{2}{1+\\lambda}\\end{bmatrix},\n",
+ "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{Ridge}}=\\begin{bmatrix}\\frac{8}{4+\\lambda} \\\\ \\frac{2}{1+\\lambda}\\end{bmatrix},\n",
"$$"
]
},
@@ -892,6 +890,136 @@
"To see this, let us write the cost function for Ridge regression. \n",
"\n",
"\n",
+ "## Writing the Cost Function\n",
+ "\n",
+ "We define the MSE without the $1/n$ factor and have then, using that"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\boldsymbol{X}\\boldsymbol{\\beta}=\\begin{bmatrix} 2\\beta_0 \\\\ \\beta_1 \\\\0 \\end{bmatrix},\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "C(\\boldsymbol{\\beta})=(4-2\\beta_0)^2+(2-\\beta_1)^2+\\lambda(\\beta_0^2+\\beta_1^2),\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "and taking the derivative with respect to $\\beta_0$ we get"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\beta_0=\\frac{8}{4+\\lambda},\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "and for $\\beta_1$ we have"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\beta_1=\\frac{2}{1+\\lambda},\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Using the constraint for $\\beta_0^2+\\beta_1^2=1$ we can constrain $\\lambda$ by solving"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\left(\\frac{8}{4+\\lambda}\\right)^2+\\left(\\frac{2}{1+\\lambda}\\right)^2=1,\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "which gives $\\lambda=4.571$ and $\\beta_0=0.933$ and $\\beta_1=0.359$.\n",
+ "\n",
+ "## Lasso case\n",
+ "\n",
+ "For Lasso we need now, keeping the same constraint on $\\beta_0^2+\\beta_1^2=1$, to take the derivative of the absolute values of $\\beta_0$\n",
+ "and $beta_1$. This gives us the following derivatives of the cost function"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "C(\\boldsymbol{\\beta})=(4-2\\beta_0)^2+(2-\\beta_1)^2+\\lambda(\\vert\\beta_0\\vert+\\vert\\beta_1\\vert),\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\frac{\\partialC(\\boldsymbol{\\beta})}{\\partial \\beta_0}=-2(4-2\\beta_0)+\\lambda\\mathrm{sgn}\\beta_0=0,\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "and"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "$$\n",
+ "\\frac{\\partialC(\\boldsymbol{\\beta})}{\\partial \\beta_1}=-2(2-\\beta_1)+\\lambda\\mathrm{sgn}\\beta_1=0.\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We have now four cases to solve besides the trivial cases $\\beta_0$ and/or $\\beta_1$ are zero, namely\n",
+ "1. $\\beta_0 > 0$ and $\\beta_1 > 0$,\n",
+ "\n",
+ "2. $\\beta_0 > 0$ and $\\beta_1 < 0$,\n",
+ "\n",
+ "3. $\\beta_0 < 0$ and $\\beta_1 > 0$,\n",
+ "\n",
+ "4. $\\beta_0 < 0$ and $\\beta_1 < 0$,\n",
+ "\n",
"\n",
"## Linking the regression analysis with a statistical interpretation\n",
"\n",
diff --git a/doc/src/week36/week36.do.txt b/doc/src/week36/week36.do.txt
index aef89b5ad..35f0d253d 100644
--- a/doc/src/week36/week36.do.txt
+++ b/doc/src/week36/week36.do.txt
@@ -423,7 +423,7 @@ which leads to
\]
!et
-Plotting these results (figure to come) shows clearly that Lasso regression suppresses (sets to zero) values of $\beta_i$ for specific values of $\lambda$. Ridge regression reduces on the hand the values of $\beta_i$ as function of $\lambda$.
+Plotting these results (figure in handwritten notes for week 36) 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$.
We will now couple the discussions of ordinary least squares, Ridge and Lasso regression with a statistical interpretation, that is we move from a linear algebra analysis to a statistical analysis. In particular, we will focus on what the regularization terms can result in.
We will amongst other things show that the regularization parameter can reduce considerably the variance of the parameters $\beta$.
@@ -442,7 +442,7 @@ Let us assume we have a data set with outputs/targets given by the vector
and our inputs as a $3\times 2$ design matrix
!bt
\[
-\bm{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 1 & 0\end{bmatrix},
+\bm{X}=\begin{bmatrix}2 & 0\\ 0 & 1 \\ 0 & 0\end{bmatrix},
\]
!et
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.
@@ -461,12 +461,10 @@ Inserting the above values we obtain that
!bt
\[
-\hat{\bm{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}\frac{11}{5} \\ 2\end{bmatrix},
+\hat{\bm{\beta}}^{\mathrm{OLS}}=\begin{bmatrix}2 \\ 2\end{bmatrix},
\]
!et
-Computing the mean squared error we obtian a value of $0.27$.
-
The code which implements this simpler case is presented after the discussion of Ridge and Lasso.
!split
@@ -483,7 +481,7 @@ Inserting the above values we obtain that
!bt
\[
-\hat{\bm{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{11}{5+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix},
+\hat{\bm{\beta}}^{\mathrm{Ridge}}=\begin{bmatrix}\frac{8}{4+\lambda} \\ \frac{2}{1+\lambda}\end{bmatrix},
\]
!et
@@ -493,6 +491,73 @@ Let us for simplicity assume now that $\beta_0^2+\beta_1^2=1$ as constraint. Thi
To see this, let us write the cost function for Ridge regression.
+!split
+===== Writing the Cost Function =====
+
+We define the MSE without the $1/n$ factor and have then, using that
+!bt
+\[
+\bm{X}\bm{\beta}=\begin{bmatrix} 2\beta_0 \\ \beta_1 \\0 \end{bmatrix},
+\]
+!et
+
+!bt
+\[
+C(\bm{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\beta_0^2+\beta_1^2),
+\]
+!et
+and taking the derivative with respect to $\beta_0$ we get
+!bt
+\[
+\beta_0=\frac{8}{4+\lambda},
+\]
+!et
+and for $\beta_1$ we have
+!bt
+\[
+\beta_1=\frac{2}{1+\lambda},
+\]
+!et
+
+Using the constraint for $\beta_0^2+\beta_1^2=1$ we can constrain $\lambda$ by solving
+!bt
+\[
+\left(\frac{8}{4+\lambda}\right)^2+\left(\frac{2}{1+\lambda}\right)^2=1,
+\]
+!et
+which gives $\lambda=4.571$ and $\beta_0=0.933$ and $\beta_1=0.359$.
+
+!split
+===== Lasso case =====
+
+For Lasso we need now, keeping the same constraint on $\beta_0^2+\beta_1^2=1$, to take the derivative of the absolute values of $\beta_0$
+and $beta_1$. This gives us the following derivatives of the cost function
+!bt
+\[
+C(\bm{\beta})=(4-2\beta_0)^2+(2-\beta_1)^2+\lambda(\vert\beta_0\vert+\vert\beta_1\vert),
+\]
+!et
+
+!bt
+\[
+\frac{\partialC(\bm{\beta})}{\partial \beta_0}=-2(4-2\beta_0)+\lambda\mathrm{sgn}\beta_0=0,
+\]
+!et
+and
+!bt
+\[
+\frac{\partialC(\bm{\beta})}{\partial \beta_1}=-2(2-\beta_1)+\lambda\mathrm{sgn}\beta_1=0.
+\]
+!et
+We have now four cases to solve besides the trivial cases $\beta_0$ and/or $\beta_1$ are zero, namely
+o $\beta_0 > 0$ and $\beta_1 > 0$,
+o $\beta_0 > 0$ and $\beta_1 < 0$,
+o $\beta_0 < 0$ and $\beta_1 > 0$,
+o $\beta_0 < 0$ and $\beta_1 < 0$,
+
+
+
+
!split
===== Linking the regression analysis with a statistical interpretation =====