Files
FYS-STK4155/doc/src/week36/exercisesweek36.html
T
Morten Hjorth-Jensen 195eced7a4 small update
2023-09-05 05:57:31 +02:00

316 lines
12 KiB
HTML

<!--
HTML file automatically generated from DocOnce source
(https://github.com/doconce/doconce/)
doconce format html exercisesweek36.do.txt --pygments_html_style=default --html_style=bloodish --html_links_in_new_window --html_output=exercisesweek36 --no_mako
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Exercises week 36">
<title>Exercises week 36</title>
<style type="text/css">
/* bloodish style */
body {
font-family: Helvetica, Verdana, Arial, Sans-serif;
color: #404040;
background: #ffffff;
}
h1 { font-size: 1.8em; color: #8A0808; }
h2 { font-size: 1.6em; color: #8A0808; }
h3 { font-size: 1.4em; color: #8A0808; }
h4 { font-size: 1.2em; color: #8A0808; }
a { color: #8A0808; text-decoration:none; }
tt { font-family: "Courier New", Courier; }
p { text-indent: 0px; }
hr { border: 0; width: 80%; border-bottom: 1px solid #aaa}
p.caption { width: 80%; font-style: normal; text-align: left; }
hr.figure { border: 0; width: 80%; border-bottom: 1px solid #aaa; }div.highlight {
border: 1px solid #cfcfcf;
border-radius: 2px;
line-height: 1.21429em;
}
div.cell {
width: 100%;
padding: 5px 5px 5px 0;
margin: 0;
outline: none;
}
div.input {
page-break-inside: avoid;
box-orient: horizontal;
box-align: stretch;
display: flex;
flex-direction: row;
align-items: stretch;
}
div.inner_cell {
box-orient: vertical;
box-align: stretch;
display: flex;
flex-direction: column;
align-items: stretch;
box-flex: 1;
flex: 1;
}
div.input_area {
border: 1px solid #cfcfcf;
border-radius: 4px;
background: #f7f7f7;
line-height: 1.21429em;
}
div.input_area > div.highlight {
margin: .4em;
border: none;
padding: 0;
background-color: transparent;
}
div.output_wrapper {
position: relative;
box-orient: vertical;
box-align: stretch;
display: flex;
flex-direction: column;
align-items: stretch;
}
.output {
box-orient: vertical;
box-align: stretch;
display: flex;
flex-direction: column;
align-items: stretch;
}
div.output_area {
padding: 0;
page-break-inside: avoid;
box-orient: horizontal;
box-align: stretch;
display: flex;
flex-direction: row;
align-items: stretch;
}
div.output_subarea {
padding: .4em .4em 0 .4em;
box-flex: 1;
flex: 1;
}
div.output_text {
text-align: left;
color: #000;
line-height: 1.21429em;
}
div { text-align: justify; text-justify: inter-word; }
.tab {
padding-left: 1.5em;
}
div.toc p,a {
line-height: 1.3;
margin-top: 1.1;
margin-bottom: 1.1;
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Overarching aims of the exercises this week',
2,
None,
'overarching-aims-of-the-exercises-this-week'),
('Exercise 1: Analytical exercises',
2,
None,
'exercise-1-analytical-exercises'),
('a) Expression for Ridge regression',
3,
None,
'a-expression-for-ridge-regression'),
('b) The singular value decomposition',
3,
None,
'b-the-singular-value-decomposition'),
('Exercise 2: Adding Ridge Regression',
2,
None,
'exercise-2-adding-ridge-regression')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "AMS" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- ------------------- main content ---------------------- -->
<center>
<h1>Exercises week 36</h1>
</center> <!-- document title -->
<!-- author(s): September 4-8, 2023 -->
<center>
<b>September 4-8, 2023</b>
</center>
<!-- institution(s) -->
<br>
<center>
<h4>Deadline is Sunday September 10 at midnight</h4>
</center> <!-- date -->
<br>
<h2 id="overarching-aims-of-the-exercises-this-week">Overarching aims of the exercises this week </h2>
<p>This set of exercises form an important part of the first project. The
analytical exercises deal with the material covered last week on the
mathematical interpretations of ordinary least squares and of Ridge
regression. The numerical exercises can be seen as a continuation of
exercise 3 from week 35, with the inclusion of Ridge regression. This
material enters also the discussions of the first project.
</p>
<!-- --- begin exercise --- -->
<h2 id="exercise-1-analytical-exercises">Exercise 1: Analytical exercises </h2>
<p>The aim here is to derive the expression for the optimal parameters
using Ridge regression. Furthermore, using the singular value
decomposition, we will analyze the difference between the ordinary
least squares approach and Ridge regression.
</p>
<p>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, was given by the
optimization problem
</p>
$$
{\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\}.
$$
<p>which we can also write as</p>
$$
{\displaystyle \min_{\boldsymbol{\beta}\in
{\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,
$$
<p>where we have used the definition of a norm-2 vector, that is</p>
$$
\vert\vert \boldsymbol{x}\vert\vert_2 = \sqrt{\sum_i x_i^2}.
$$
<p>By minimizing the above equation with respect to the parameters
\( \boldsymbol{\beta} \) we could then obtain an analytical expression for the
parameters \( \boldsymbol{\beta} \).
</p>
<p>We can add a regularization parameter \( \lambda \) by
defining a new cost function to be optimized, that is
</p>
$$
{\displaystyle \min_{\boldsymbol{\beta}\in
{\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
$$
<p>which leads to the Ridge regression minimization problem. One can require as part of the optimization problem
that \( \vert\vert \boldsymbol{\beta}\vert\vert_2^2\le t \), where \( t \) is
a finite number larger than zero. We will not implement that here.
</p>
<h3 id="a-expression-for-ridge-regression">a) Expression for Ridge regression </h3>
<p>Show that the optimal parameters</p>
$$
\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y},
$$
<p>with \( \boldsymbol{I} \) being a \( p\times p \) identity matrix with the constraint that</p>
$$
\sum_{i=0}^{p-1} \beta_i^2 \leq t,
$$
<p>with \( t \) a finite positive number. In the optimization, we will not require that the latter is satisfied.</p>
<p>The ordinary least squares result is</p>
$$
\hat{\boldsymbol{\beta}}_{\mathrm{OLS}} = \left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y},
$$
<h3 id="b-the-singular-value-decomposition">b) The singular value decomposition </h3>
<p>Here we will use the singular value decomposition of an \( n\times p \) matrix \( \boldsymbol{X} \) (our design matrix)</p>
$$
\boldsymbol{X}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,
$$
<p>to study properties of Ridge regression and ordinary least squares regression.
Here \( \boldsymbol{U} \) and \( \boldsymbol{V} \) are orthogonal matrices of dimensions
\( n\times n \) and \( p\times p \), respectively, and \( \boldsymbol{\Sigma} \) is an
\( n\times p \) matrix which contains the singular values only. This material was discussed during the lectures of week 35.
</p>
<p>Show that you can write the
OLS solutions in terms of the eigenvectors (the columns) of the orthogonal matrix \( \boldsymbol{U} \) as
</p>
$$
\tilde{\boldsymbol{y}}_{\mathrm{OLS}}=\boldsymbol{X}\boldsymbol{\beta} = \sum_{j=0}^{p-1}\boldsymbol{u}_j\boldsymbol{u}_j^T\boldsymbol{y}.
$$
<p>For Ridge regression, show that the corresponding equation is</p>
$$
\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},
$$
<p>with the vectors \( \boldsymbol{u}_j \) being the columns of \( \boldsymbol{U} \) from the SVD of the matrix \( \boldsymbol{X} \). </p>
<p>Give an interpretation of the results. <a href="https://link.springer.com/book/10.1007/978-0-387-84858-7" target="_blank">Section 3.4 of Hastie et al's textbook gives a good discussion of the above results</a>.</p>
<!-- --- end exercise --- -->
<!-- --- begin exercise --- -->
<h2 id="exercise-2-adding-ridge-regression">Exercise 2: Adding Ridge Regression </h2>
<p>This exercise is a continuation of exercise 3 from week 35, see <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/exercisesweek35.html" target="_blank"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/exercisesweek35.html</tt></a>. We will use the same function to
generate our data set, still staying with a simple function \( y(x) \)
which we want to fit using linear regression, but now extending the
analysis to include the Ridge regression method.
</p>
<p>In this exercise you need to include the same elements from last week, that is</p>
<ol>
<li> scale your data by subtracting the mean value from each column in the design matrix.</li>
<li> perform a split of the data in a training set and a test set.</li>
</ol>
<p>The addition to the analysis this time is the introduction of the hyperparameter \( \lambda \) when introducing Ridge regression.</p>
<p>Extend the code from exercise 3 from <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/exercisesweek35.html" target="_blank">week 35</a> to include Ridge regression with the hyperparameter \( \lambda \). The optimal parameters \( \hat{\beta} \) for Ridge regression can be obtained by matrix inversion in a similar way as done for ordinary least squares. You need to add to your code the following equations</p>
$$
\hat{\boldsymbol{\beta}}_{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}.
$$
<p>The ordinary least squares result you encoded last week is given by</p>
$$
\hat{\boldsymbol{\beta}}_{\mathrm{OLS}} = \left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y},
$$
<p>Use these results to compute the mean squared error for ordinary least
squares and Ridge regression first for a polynomial of degree five
with \( n=100 \) data points and five selected values of
\( \lambda=[0.0001,0.001, 0.01,0.1,1.0] \). Compute thereafter the mean
squared error for the same values of \( \lambda \) for polynomials of degree ten
and \( 15 \). Discuss your results for the training MSE and test MSE with
Ridge regression and ordinary least squares.
</p>
<!-- --- end exercise --- -->
<!-- ------------------- end of main content --------------- -->
</body>
</html>