This commit is contained in:
Morten Hjorth-Jensen
2025-09-02 06:25:20 +02:00
parent c8aa957670
commit 8eb5ccc1e2
20 changed files with 4404 additions and 427 deletions
@@ -175,18 +175,20 @@ MathJax.Hub.Config({
<h2 id="preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" class="anchor">Preamble: Note on writing reports, using reference material, AI and other tools </h2>
<p>We want you to answer the three different projects by handing in
reports written like a standard scientific/technical report. The links
at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
Furthermore, at the same link,
you can find examples of previous reports. How to write reports will
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
reports written like a standard scientific/technical report. The
links at
<a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
contain more information. There you can find examples of previous
reports, the projects themselves, how we rade reports etc. How to
write reports will also be discussed during the various lab
sessions. Please do ask us if you are in doubt.
</p>
<p>When using codes and material from other sources, you should refer to
these in the bibliography of your report, indicating wherefrom you for
example got the code, whether this is from the lecture notes,
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
AI software. These should always be cited correctly. How to cite some
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
should always be cited correctly. How to cite some
of the libraries is often indicated from their corresponding GitHub
sites or websites, see for example how to cite Scikit-Learn at
<a href="https://scikit-learn.org/dev/about.html" target="_self"><tt>https://scikit-learn.org/dev/about.html</tt></a>.
@@ -154,7 +154,7 @@ MathJax.Hub.Config({
<h2 id="regression-analysis-and-resampling-methods" class="anchor">Regression analysis and resampling methods </h2>
<p>The main aim of this project is to study in more detail various
regression methods, including the Ordinary Least Squares (OLS) method.
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
In addition to the scientific part, in this course we want also to
give you an experience in writing scientific reports.
</p>
@@ -170,27 +170,26 @@ $$
<p>Our first step will be to perform an OLS regression analysis of this
function, trying out a polynomial fit with an \( x \) dependence of the
form \( [x,x^2,\dots] \). We can use a uniform distribution to set up the
form \( [x,x^2,\dots] \). You can use a uniform distribution to set up the
arrays of values for \( x \in [-1,1] \), or alternatively use a fixed step size.
Thereafter we will repeat much of the
same procedure using the Ridge and Lasso regression methods,
introducing thus a dependence on the hyperparameter (penalty) \( \lambda \).
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
introducing thereby a dependence on the hyperparameter (penalty) \( \lambda \).
</p>
<p>We will also include bootstrap as a resampling technique in order to
study the so-called <b>bias-variance tradeoff</b>. After that we will
include the cross-validation technique.
include the so-called cross-validation technique.
</p>
<h3 id="part-a-ordinary-least-square-ols-for-the-runge-function" class="anchor">Part a : Ordinary Least Square (OLS) for the Runge function </h3>
<p>We will generate our own dataset for a function
<p>We will generate our own dataset for abovementioned function
\( \mathrm{Runge}(x) \) function with \( x\in [-1,1] \). You should explore also the addition
of an added stochastic noise to this function using the normal
distribution \( N(0,1) \).
</p>
<p><em>Write your own code</em> (using for example the pseudoinverse function <b>pinv</b> from <b>Numpy</b> ) and perform a standard <b>ordinary least square regression</b>
analysis using polynomials in \( x \) up to order \( 15 \). Explore the dependence on the number of data points and the polynomial degree.
analysis using polynomials in \( x \) up to order \( 15 \) or higher. Explore the dependence on the number of data points and the polynomial degree.
</p>
<p>Evaluate the mean Squared error (MSE)</p>
@@ -214,13 +213,13 @@ $$
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
$$
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
Plot also the parameters \( \theta \) as you increase the order of the polynomial. Comment your results.
</p>
<p>Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
a split of the data in training and test data. For the scaling you can
either write your own code or use for example the function for
splitting training data provided by the library <b>Scikit-Learn</b> (make
sure you have installed it). This function is called
@@ -243,11 +242,11 @@ See also the lecture slides from week 35 and week 36.
<h3 id="part-b-adding-ridge-regression-for-the-runge-function" class="anchor">Part b: Adding Ridge regression for the Runge function </h3>
<p>Write your own code for the Ridge method as done in the previous
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
</p>
<p>Perform the same analysis as you did in the previous exercise but now for different values of \( \lambda \). Compare and
analyze your results with those obtained in part a) with the ordinary least squares method. Study the
analyze your results with those obtained in part a) with the OLS method. Study the
dependence on \( \lambda \).
</p>
<h3 id="part-c-writing-your-own-gradient-descent-code" class="anchor">Part c: Writing your own gradient descent code </h3>
@@ -268,15 +267,15 @@ descent approch. Discuss in particular the role of the learning rate.
the gradient descent method by including <b>momentum</b>, <b>ADAgrad</b>,
<b>RMSprop</b> and <b>ADAM</b> as methods fro iteratively updating your learning
rate. Discuss the results and compare the different methods applied to
the one-dimensional Runge function.
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
</p>
<h3 id="part-e-writing-our-own-code-for-lasso-regression" class="anchor">Part e: Writing our own code for Lasso regression </h3>
<p>LASSO regression (see lecture slides from week 36 and week 37)
represents our first encounter with a machine learning method which
cannot be solved through analytical expressions. Use the gradient
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
descent methods you developed in parts c) and d) to solve the LASSO
optimization problem. You can compare your results using
optimization problem. You can compare your results with
the functionalities of <b>Scikit-Learn</b>.
</p>
@@ -286,14 +285,16 @@ approaches.
</p>
<h3 id="part-f-stochastic-gradient-descent" class="anchor">Part f: Stochastic gradient descent </h3>
<p>Our last gradient step is to include stochastic gradient descent using the
same methods to update the learning rates as in parts c-e).
Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods.
<p>Our last gradient step is to include stochastic gradient descent using
the same methods to update the learning rates as in parts c-e).
Compare and discuss your results with and without stochastic gradient
and give a critical assessment of the various methods.
</p>
<h3 id="part-g-bias-variance-trade-off-and-resampling-techniques" class="anchor">Part g: Bias-variance trade-off and resampling techniques </h3>
<p>Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.
<b>We will only use the simpler ordinary least squares here</b>.
<p>Our aim here is to study the bias-variance trade-off by implementing
the <b>bootstrap</b> resampling technique. <b>We will only use the simpler
ordinary least squares here</b>.
</p>
<p>With a code which does OLS and includes resampling techniques,
@@ -303,11 +304,14 @@ intuitions and ideas discussed here also carry over to classification
tasks and basically all Machine Learning algorithms.
</p>
<p>Before you perform an analysis of the bias-variance trade-off on your test data, make
first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
indicate possible regions of low/high bias and variance. You will most likely not get an
equally smooth curve!
<p>Before you perform an analysis of the bias-variance trade-off on your
test data, make first a figure similar to Fig. 2.11 of Hastie,
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
the test and training MSEs. The test MSE can be used to indicate
possible regions of low/high bias and variance. You will most likely
not get an equally smooth curve! You may also need to increase the
polynomial order and play around with the number of data points as
well (see also the exercise set from week 35).
</p>
<p>With this result we move on to the bias-variance trade-off analysis.</p>
@@ -317,7 +321,7 @@ dataset \( \mathcal{L} \) consisting of the data
\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
</p>
<p>As in part d), we assume that the true data is generated from a noisy model</p>
<p>We assume that the true data is generated from a noisy model</p>
$$
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
@@ -329,29 +333,32 @@ deviation \( \sigma^2 \).
<p>In our derivation of the ordinary least squares method we defined then
an approximation to the function \( f \) in terms of the parameters
\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \).
\( \boldsymbol{\theta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\theta} \).
</p>
<p>The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the mean
<p>The parameters \( \boldsymbol{\theta} \) are in turn found by optimizing the mean
squared error via the so-called cost function
</p>
$$
C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
C(\boldsymbol{X},\boldsymbol{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
$$
<p>Here the expected value \( \mathbb{E} \) is the sample value. </p>
<p>Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a
term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.
That is, show that
<p>Show that you can rewrite this in terms of a term which contains the
variance of the model itself (the so-called variance term), a term
which measures the deviation from the true data and the mean value of
the model (the bias term) and finally the variance of the noise.
</p>
<p>That is, show that</p>
$$
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
$$
<p>with </p>
<p>with (we approximate \( f(\boldsymbol{x})\approx \boldsymbol{y} \)) </p>
$$
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right],
$$
@@ -361,8 +368,12 @@ $$
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\boldsymbol{y}}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2.
$$
<p>The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.
Explain what the terms mean and discuss their interpretations.
<p><b>Important note</b>: Since the function \( f(x) \) is unknown, in order to be able to evalute the bias, we replace \( f(\boldsymbol{x}) \) in the expression for the bias with \( \boldsymbol{y} \). </p>
<p>The answer to this exercise should be included in the theory part of
the report. This exercise is also part of the weekly exercises of
week 38. Explain what the terms mean and discuss their
interpretations.
</p>
<p>Perform then a bias-variance analysis of the Runge function by
@@ -380,16 +391,18 @@ You can follow the code example in the jupyter-book at <a href="https://compphys
resampling technique, the so-called cross-validation method.
</p>
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use the functionality of <b>Scikit-Learn</b> or write your own code) and evaluate again the MSE function resulting
from the test folds.
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use
the functionality of <b>Scikit-Learn</b> or write your own code) and
evaluate again the MSE function resulting from the test folds.
</p>
<p>Compare the MSE you get from your cross-validation code with the one
you got from your <b>bootstrap</b> code. Comment your results. Try \( 5-10 \)
folds.
you got from your <b>bootstrap</b> code from the previous exercise. Comment and interpret your results.
</p>
<p>In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis. </p>
<p>In addition to using the ordinary least squares method, you should
include both Ridge and Lasso regression in the final analysis.
</p>
<h2 id="background-literature" class="anchor">Background literature </h2>
<ol>
@@ -175,18 +175,20 @@ MathJax.Hub.Config({
<h2 id="preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" class="anchor">Preamble: Note on writing reports, using reference material, AI and other tools </h2>
<p>We want you to answer the three different projects by handing in
reports written like a standard scientific/technical report. The links
at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
Furthermore, at the same link,
you can find examples of previous reports. How to write reports will
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
reports written like a standard scientific/technical report. The
links at
<a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
contain more information. There you can find examples of previous
reports, the projects themselves, how we rade reports etc. How to
write reports will also be discussed during the various lab
sessions. Please do ask us if you are in doubt.
</p>
<p>When using codes and material from other sources, you should refer to
these in the bibliography of your report, indicating wherefrom you for
example got the code, whether this is from the lecture notes,
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
AI software. These should always be cited correctly. How to cite some
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
should always be cited correctly. How to cite some
of the libraries is often indicated from their corresponding GitHub
sites or websites, see for example how to cite Scikit-Learn at
<a href="https://scikit-learn.org/dev/about.html" target="_self"><tt>https://scikit-learn.org/dev/about.html</tt></a>.
+63 -48
View File
@@ -210,18 +210,20 @@ MathJax.Hub.Config({
<h2 id="preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools">Preamble: Note on writing reports, using reference material, AI and other tools </h2>
<p>We want you to answer the three different projects by handing in
reports written like a standard scientific/technical report. The links
at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
Furthermore, at the same link,
you can find examples of previous reports. How to write reports will
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
reports written like a standard scientific/technical report. The
links at
<a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
contain more information. There you can find examples of previous
reports, the projects themselves, how we rade reports etc. How to
write reports will also be discussed during the various lab
sessions. Please do ask us if you are in doubt.
</p>
<p>When using codes and material from other sources, you should refer to
these in the bibliography of your report, indicating wherefrom you for
example got the code, whether this is from the lecture notes,
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
AI software. These should always be cited correctly. How to cite some
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
should always be cited correctly. How to cite some
of the libraries is often indicated from their corresponding GitHub
sites or websites, see for example how to cite Scikit-Learn at
<a href="https://scikit-learn.org/dev/about.html" target="_blank"><tt>https://scikit-learn.org/dev/about.html</tt></a>.
@@ -253,7 +255,7 @@ other data sets, make sure these are sets used for regression problems
<h2 id="regression-analysis-and-resampling-methods">Regression analysis and resampling methods </h2>
<p>The main aim of this project is to study in more detail various
regression methods, including the Ordinary Least Squares (OLS) method.
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
In addition to the scientific part, in this course we want also to
give you an experience in writing scientific reports.
</p>
@@ -269,27 +271,26 @@ $$
<p>Our first step will be to perform an OLS regression analysis of this
function, trying out a polynomial fit with an \( x \) dependence of the
form \( [x,x^2,\dots] \). We can use a uniform distribution to set up the
form \( [x,x^2,\dots] \). You can use a uniform distribution to set up the
arrays of values for \( x \in [-1,1] \), or alternatively use a fixed step size.
Thereafter we will repeat much of the
same procedure using the Ridge and Lasso regression methods,
introducing thus a dependence on the hyperparameter (penalty) \( \lambda \).
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
introducing thereby a dependence on the hyperparameter (penalty) \( \lambda \).
</p>
<p>We will also include bootstrap as a resampling technique in order to
study the so-called <b>bias-variance tradeoff</b>. After that we will
include the cross-validation technique.
include the so-called cross-validation technique.
</p>
<h3 id="part-a-ordinary-least-square-ols-for-the-runge-function">Part a : Ordinary Least Square (OLS) for the Runge function </h3>
<p>We will generate our own dataset for a function
<p>We will generate our own dataset for abovementioned function
\( \mathrm{Runge}(x) \) function with \( x\in [-1,1] \). You should explore also the addition
of an added stochastic noise to this function using the normal
distribution \( N(0,1) \).
</p>
<p><em>Write your own code</em> (using for example the pseudoinverse function <b>pinv</b> from <b>Numpy</b> ) and perform a standard <b>ordinary least square regression</b>
analysis using polynomials in \( x \) up to order \( 15 \). Explore the dependence on the number of data points and the polynomial degree.
analysis using polynomials in \( x \) up to order \( 15 \) or higher. Explore the dependence on the number of data points and the polynomial degree.
</p>
<p>Evaluate the mean Squared error (MSE)</p>
@@ -313,13 +314,13 @@ $$
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
$$
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
Plot also the parameters \( \theta \) as you increase the order of the polynomial. Comment your results.
</p>
<p>Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
a split of the data in training and test data. For the scaling you can
either write your own code or use for example the function for
splitting training data provided by the library <b>Scikit-Learn</b> (make
sure you have installed it). This function is called
@@ -342,11 +343,11 @@ See also the lecture slides from week 35 and week 36.
<h3 id="part-b-adding-ridge-regression-for-the-runge-function">Part b: Adding Ridge regression for the Runge function </h3>
<p>Write your own code for the Ridge method as done in the previous
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
</p>
<p>Perform the same analysis as you did in the previous exercise but now for different values of \( \lambda \). Compare and
analyze your results with those obtained in part a) with the ordinary least squares method. Study the
analyze your results with those obtained in part a) with the OLS method. Study the
dependence on \( \lambda \).
</p>
<h3 id="part-c-writing-your-own-gradient-descent-code">Part c: Writing your own gradient descent code </h3>
@@ -367,15 +368,15 @@ descent approch. Discuss in particular the role of the learning rate.
the gradient descent method by including <b>momentum</b>, <b>ADAgrad</b>,
<b>RMSprop</b> and <b>ADAM</b> as methods fro iteratively updating your learning
rate. Discuss the results and compare the different methods applied to
the one-dimensional Runge function.
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
</p>
<h3 id="part-e-writing-our-own-code-for-lasso-regression">Part e: Writing our own code for Lasso regression </h3>
<p>LASSO regression (see lecture slides from week 36 and week 37)
represents our first encounter with a machine learning method which
cannot be solved through analytical expressions. Use the gradient
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
descent methods you developed in parts c) and d) to solve the LASSO
optimization problem. You can compare your results using
optimization problem. You can compare your results with
the functionalities of <b>Scikit-Learn</b>.
</p>
@@ -385,14 +386,16 @@ approaches.
</p>
<h3 id="part-f-stochastic-gradient-descent">Part f: Stochastic gradient descent </h3>
<p>Our last gradient step is to include stochastic gradient descent using the
same methods to update the learning rates as in parts c-e).
Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods.
<p>Our last gradient step is to include stochastic gradient descent using
the same methods to update the learning rates as in parts c-e).
Compare and discuss your results with and without stochastic gradient
and give a critical assessment of the various methods.
</p>
<h3 id="part-g-bias-variance-trade-off-and-resampling-techniques">Part g: Bias-variance trade-off and resampling techniques </h3>
<p>Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.
<b>We will only use the simpler ordinary least squares here</b>.
<p>Our aim here is to study the bias-variance trade-off by implementing
the <b>bootstrap</b> resampling technique. <b>We will only use the simpler
ordinary least squares here</b>.
</p>
<p>With a code which does OLS and includes resampling techniques,
@@ -402,11 +405,14 @@ intuitions and ideas discussed here also carry over to classification
tasks and basically all Machine Learning algorithms.
</p>
<p>Before you perform an analysis of the bias-variance trade-off on your test data, make
first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
indicate possible regions of low/high bias and variance. You will most likely not get an
equally smooth curve!
<p>Before you perform an analysis of the bias-variance trade-off on your
test data, make first a figure similar to Fig. 2.11 of Hastie,
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
the test and training MSEs. The test MSE can be used to indicate
possible regions of low/high bias and variance. You will most likely
not get an equally smooth curve! You may also need to increase the
polynomial order and play around with the number of data points as
well (see also the exercise set from week 35).
</p>
<p>With this result we move on to the bias-variance trade-off analysis.</p>
@@ -416,7 +422,7 @@ dataset \( \mathcal{L} \) consisting of the data
\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
</p>
<p>As in part d), we assume that the true data is generated from a noisy model</p>
<p>We assume that the true data is generated from a noisy model</p>
$$
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
@@ -428,29 +434,32 @@ deviation \( \sigma^2 \).
<p>In our derivation of the ordinary least squares method we defined then
an approximation to the function \( f \) in terms of the parameters
\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \).
\( \boldsymbol{\theta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\theta} \).
</p>
<p>The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the mean
<p>The parameters \( \boldsymbol{\theta} \) are in turn found by optimizing the mean
squared error via the so-called cost function
</p>
$$
C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
C(\boldsymbol{X},\boldsymbol{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
$$
<p>Here the expected value \( \mathbb{E} \) is the sample value. </p>
<p>Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a
term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.
That is, show that
<p>Show that you can rewrite this in terms of a term which contains the
variance of the model itself (the so-called variance term), a term
which measures the deviation from the true data and the mean value of
the model (the bias term) and finally the variance of the noise.
</p>
<p>That is, show that</p>
$$
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
$$
<p>with </p>
<p>with (we approximate \( f(\boldsymbol{x})\approx \boldsymbol{y} \)) </p>
$$
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right],
$$
@@ -460,8 +469,12 @@ $$
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\boldsymbol{y}}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2.
$$
<p>The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.
Explain what the terms mean and discuss their interpretations.
<p><b>Important note</b>: Since the function \( f(x) \) is unknown, in order to be able to evalute the bias, we replace \( f(\boldsymbol{x}) \) in the expression for the bias with \( \boldsymbol{y} \). </p>
<p>The answer to this exercise should be included in the theory part of
the report. This exercise is also part of the weekly exercises of
week 38. Explain what the terms mean and discuss their
interpretations.
</p>
<p>Perform then a bias-variance analysis of the Runge function by
@@ -479,16 +492,18 @@ You can follow the code example in the jupyter-book at <a href="https://compphys
resampling technique, the so-called cross-validation method.
</p>
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use the functionality of <b>Scikit-Learn</b> or write your own code) and evaluate again the MSE function resulting
from the test folds.
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use
the functionality of <b>Scikit-Learn</b> or write your own code) and
evaluate again the MSE function resulting from the test folds.
</p>
<p>Compare the MSE you get from your cross-validation code with the one
you got from your <b>bootstrap</b> code. Comment your results. Try \( 5-10 \)
folds.
you got from your <b>bootstrap</b> code from the previous exercise. Comment and interpret your results.
</p>
<p>In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis. </p>
<p>In addition to using the ordinary least squares method, you should
include both Ridge and Lasso regression in the final analysis.
</p>
<h2 id="background-literature">Background literature </h2>
<ol>
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "f57c8fa4",
"id": "b209e219",
"metadata": {
"editable": true
},
@@ -14,20 +14,20 @@
},
{
"cell_type": "markdown",
"id": "26a16b0d",
"id": "6fa4c4bc",
"metadata": {
"editable": true
},
"source": [
"# Project 1 on Machine Learning, deadline October 6 (midnight), 2025\n",
"**[Data Analysis and Machine Learning FYS-STK3155/FYS4155](http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html)**, University of Oslo, Norway\n",
"**Data Analysis and Machine Learning FYS-STK3155/FYS4155**, University of Oslo, Norway\n",
"\n",
"Date: **September 2**"
]
},
{
"cell_type": "markdown",
"id": "f77c6ce6",
"id": "735b16c4",
"metadata": {
"editable": true
},
@@ -35,30 +35,30 @@
"## Preamble: Note on writing reports, using reference material, AI and other tools\n",
"\n",
"We want you to answer the three different projects by handing in\n",
"reports written like a standard scientific/technical report. The link\n",
"at\n",
"<https://github.com/CompPhysics/MachineLearning/blob/master/doc/Projects/ProjectWriting/projectwriting.ipynb>\n",
"gives some guidance. See also the grading suggestion at\n",
"<https://github.com/CompPhysics/MachineLearning/blob/master/doc/Projects/EvaluationGrading/EvaluationForm.md>.\n",
"reports written like a standard scientific/technical report. The\n",
"links at\n",
"<https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects>\n",
"contain more information. There you can find examples of previous\n",
"reports, the projects themselves, how we rade reports etc. How to\n",
"write reports will also be discussed during the various lab\n",
"sessions. Please do ask us if you are in doubt.\n",
"\n",
"Furthermore, at\n",
"<https://github.com/CompPhysics/MachineLearning/blob/master/doc/Projects/ReportExample/>\n",
"you can find examples of previous reports. How to write reports will\n",
"also be discussed during the various lab sessions. Please do ask us if you are in doubt.\n",
"\n",
"When using codes and material from other sources, you should refer to these in the bibliography of your report, indicating wherefrom you for example\n",
"got the code, whether this is from the lecture notes, softwares like\n",
"Scikit-Learn, TensorFlow, PyTorch or other sources such AI software. These should\n",
"always be cited correctly. How to cite some of the libraries is often\n",
"indicated from their corresponding GitHub sites or websites, see for example how to cite Scikit-Learn at <https://scikit-learn.org/dev/about.html>. \n",
"When using codes and material from other sources, you should refer to\n",
"these in the bibliography of your report, indicating wherefrom you for\n",
"example got the code, whether this is from the lecture notes,\n",
"softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources\n",
"should always be cited correctly. How to cite some\n",
"of the libraries is often indicated from their corresponding GitHub\n",
"sites or websites, see for example how to cite Scikit-Learn at\n",
"<https://scikit-learn.org/dev/about.html>.\n",
"\n",
"We enocurage you to use tools like\n",
"[ChatGPT](https://openai.com/chatgpt/) or similar in writing the report. If you use for example ChatGPT,\n",
"please do cite it properly and include (if possible) your questions and answers as an addition to the report. This can\n",
"be uploaded to for example your website, GitHub/GitLab or similar as supplemental material.\n",
"\n",
"If you would like to study other data sets, feel free to\n",
"propose other sets. What we have proposed here are mere suggestions from our\n",
"If you would like to study other data sets, feel free to propose other\n",
"sets. What we have proposed here are mere suggestions from our\n",
"side. If you opt for another data set, consider using a set which has\n",
"been studied in the scientific literature. This makes it easier for\n",
"you to compare and analyze your results. Comparing with existing\n",
@@ -68,12 +68,14 @@
"<https://archive.ics.uci.edu/ml/index.php> is an excellent site to\n",
"look up for examples and\n",
"inspiration. [Kaggle.com](https://www.kaggle.com/) is an equally\n",
"interesting site. Feel free to explore these sites. When selecting other data sets, make sure these are sets used for regression problems (not classification)."
"interesting site. Feel free to explore these sites. When selecting\n",
"other data sets, make sure these are sets used for regression problems\n",
"(not classification)."
]
},
{
"cell_type": "markdown",
"id": "fc679cbc",
"id": "0b7956ca",
"metadata": {
"editable": true
},
@@ -81,17 +83,19 @@
"## Regression analysis and resampling methods\n",
"\n",
"The main aim of this project is to study in more detail various\n",
"regression methods, including the Ordinary Least Squares (OLS) method.\n",
"regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.\n",
"In addition to the scientific part, in this course we want also to\n",
"give you an experience in writing scientific reports.\n",
"\n",
"We will first study how to fit polynomials to specific\n",
"one-dimensional functions. We will start with a function given by Runge's function (see <https://en.wikipedia.org/wiki/Runge%27s_phenomenon> for a discussion). The one-dimensional function we will study first is"
"We will study how to fit polynomials to specific\n",
"one-dimensional functions (feel free to replace the suggested function with more complicated ones).\n",
"\n",
"We will use Runge's function (see <https://en.wikipedia.org/wiki/Runge%27s_phenomenon> for a discussion). The one-dimensional function we will study is"
]
},
{
"cell_type": "markdown",
"id": "7a2784c7",
"id": "28ba3d22",
"metadata": {
"editable": true
},
@@ -103,48 +107,46 @@
},
{
"cell_type": "markdown",
"id": "16851f60",
"id": "9a3e10ba",
"metadata": {
"editable": true
},
"source": [
"Our first step will be to perform an OLS regression analysis of this\n",
"function, trying out a polynomial fit with an $x$ dependence of the\n",
"form $[x,x^2,\\dots]$. We can use a uniform distribution to set up the\n",
"form $[x,x^2,\\dots]$. You can use a uniform distribution to set up the\n",
"arrays of values for $x \\in [-1,1]$, or alternatively use a fixed step size.\n",
"Thereafter we will repeat much of the\n",
"same procedure using the Ridge and Lasso regression methods,\n",
"introducing thus a dependence on the hyperparameter (penalty) $\\lambda$.\n",
"Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,\n",
"introducing thereby a dependence on the hyperparameter (penalty) $\\lambda$.\n",
"\n",
"We will also include bootstrap as a resampling technique in order to\n",
"study the so-called **bias-variance tradeoff**. After that we will\n",
"include the cross-validation technique."
"include the so-called cross-validation technique."
]
},
{
"cell_type": "markdown",
"id": "0ba8f2df",
"id": "8aa547a5",
"metadata": {
"editable": true
},
"source": [
"### Part a : Ordinary Least Square (OLS) on the Franke function\n",
"### Part a : Ordinary Least Square (OLS) for the Runge function\n",
"\n",
"We will generate our own dataset for a function\n",
"$\\mathrm{FrankeFunction}(x,y)$ with $x,y \\in [0,1]$. The function\n",
"$f(x,y)$ is the Franke function. You should explore also the addition\n",
"We will generate our own dataset for abovementioned function\n",
"$\\mathrm{Runge}(x)$ function with $x\\in [-1,1]$. You should explore also the addition\n",
"of an added stochastic noise to this function using the normal\n",
"distribution $N(0,1)$.\n",
"\n",
"*Write your own code* (using for example the pseudoinverse function **pinv** from **Numpy** ) and perform a standard **ordinary least square regression**\n",
"analysis using polynomials in $x$ up to order $15$. Explore the dependence on the number of data points and the polynomial degree.\n",
"analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.\n",
"\n",
"Evaluate the mean Squared error (MSE)"
]
},
{
"cell_type": "markdown",
"id": "9fa926bd",
"id": "68fbf03d",
"metadata": {
"editable": true
},
@@ -157,7 +159,7 @@
},
{
"cell_type": "markdown",
"id": "29a3ee67",
"id": "b49509bc",
"metadata": {
"editable": true
},
@@ -169,7 +171,7 @@
},
{
"cell_type": "markdown",
"id": "95634981",
"id": "0fa4ffc6",
"metadata": {
"editable": true
},
@@ -181,7 +183,7 @@
},
{
"cell_type": "markdown",
"id": "8a1dafa5",
"id": "ce462b32",
"metadata": {
"editable": true
},
@@ -191,7 +193,7 @@
},
{
"cell_type": "markdown",
"id": "23e66218",
"id": "a5fbef36",
"metadata": {
"editable": true
},
@@ -203,17 +205,17 @@
},
{
"cell_type": "markdown",
"id": "b353e056",
"id": "a6afe9cb",
"metadata": {
"editable": true
},
"source": [
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).\n",
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).\n",
"Plot also the parameters $\\theta$ as you increase the order of the polynomial. Comment your results.\n",
"\n",
"Your code has to include a scaling/centering of the data (for example by\n",
"subtracting the mean value), and\n",
"a split of the data in training and test data. For this exercise you can\n",
"a split of the data in training and test data. For the scaling you can\n",
"either write your own code or use for example the function for\n",
"splitting training data provided by the library **Scikit-Learn** (make\n",
"sure you have installed it). This function is called\n",
@@ -234,7 +236,7 @@
},
{
"cell_type": "markdown",
"id": "9f3a2963",
"id": "3be10f68",
"metadata": {
"editable": true
},
@@ -242,16 +244,16 @@
"### Part b: Adding Ridge regression for the Runge function\n",
"\n",
"Write your own code for the Ridge method as done in the previous\n",
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.\n",
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.\n",
"\n",
"Perform the same analysis as you did in the previous exercise but now for different values of $\\lambda$. Compare and\n",
"analyze your results with those obtained in part a) with the ordinary least squares method. Study the\n",
"analyze your results with those obtained in part a) with the OLS method. Study the\n",
"dependence on $\\lambda$."
]
},
{
"cell_type": "markdown",
"id": "2d8fb899",
"id": "caa7909c",
"metadata": {
"editable": true
},
@@ -270,7 +272,7 @@
},
{
"cell_type": "markdown",
"id": "6ad3c9ce",
"id": "3aac4df1",
"metadata": {
"editable": true
},
@@ -281,12 +283,12 @@
"the gradient descent method by including **momentum**, **ADAgrad**,\n",
"**RMSprop** and **ADAM** as methods fro iteratively updating your learning\n",
"rate. Discuss the results and compare the different methods applied to\n",
"the one-dimensional Runge function."
"the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods."
]
},
{
"cell_type": "markdown",
"id": "234254d4",
"id": "d0862a53",
"metadata": {
"editable": true
},
@@ -295,9 +297,9 @@
"\n",
"LASSO regression (see lecture slides from week 36 and week 37)\n",
"represents our first encounter with a machine learning method which\n",
"cannot be solved through analytical expressions. Use the gradient\n",
"cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient\n",
"descent methods you developed in parts c) and d) to solve the LASSO\n",
"optimization problem. You can compare your results using\n",
"optimization problem. You can compare your results with \n",
"the functionalities of **Scikit-Learn**.\n",
"\n",
"Discuss (critically) your results for the Runge function from OLS,\n",
@@ -307,29 +309,31 @@
},
{
"cell_type": "markdown",
"id": "31a94810",
"id": "9170032e",
"metadata": {
"editable": true
},
"source": [
"### Part f: Stochastic gradient descent\n",
"\n",
"Our last gradient step is to include stochastic gradient descent using the\n",
"same methods to update the learning rates as in parts c-e).\n",
"Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods."
"Our last gradient step is to include stochastic gradient descent using\n",
"the same methods to update the learning rates as in parts c-e).\n",
"Compare and discuss your results with and without stochastic gradient\n",
"and give a critical assessment of the various methods."
]
},
{
"cell_type": "markdown",
"id": "e657f5a5",
"id": "bacd1035",
"metadata": {
"editable": true
},
"source": [
"### Part g: Bias-variance trade-off and resampling techniques\n",
"\n",
"Our aim here is to study the bias-variance trade-off by implementing the **bootstrap** resampling technique.\n",
"**We will only use the simpler ordinary least squares here**.\n",
"Our aim here is to study the bias-variance trade-off by implementing\n",
"the **bootstrap** resampling technique. **We will only use the simpler\n",
"ordinary least squares here**.\n",
"\n",
"With a code which does OLS and includes resampling techniques, \n",
"we will now discuss the bias-variance trade-off in the context of\n",
@@ -337,11 +341,14 @@
"intuitions and ideas discussed here also carry over to classification\n",
"tasks and basically all Machine Learning algorithms. \n",
"\n",
"Before you perform an analysis of the bias-variance trade-off on your test data, make\n",
"first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and\n",
"Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to \n",
"indicate possible regions of low/high bias and variance. You will most likely not get an\n",
"equally smooth curve!\n",
"Before you perform an analysis of the bias-variance trade-off on your\n",
"test data, make first a figure similar to Fig. 2.11 of Hastie,\n",
"Tibshirani, and Friedman. Figure 2.11 of this reference displays only\n",
"the test and training MSEs. The test MSE can be used to indicate\n",
"possible regions of low/high bias and variance. You will most likely\n",
"not get an equally smooth curve! You may also need to increase the\n",
"polynomial order and play around with the number of data points as\n",
"well (see also the exercise set from week 35).\n",
"\n",
"With this result we move on to the bias-variance trade-off analysis.\n",
"\n",
@@ -349,12 +356,12 @@
"dataset $\\mathcal{L}$ consisting of the data\n",
"$\\mathbf{X}_\\mathcal{L}=\\{(y_j, \\boldsymbol{x}_j), j=0\\ldots n-1\\}$.\n",
"\n",
"As in part d), we assume that the true data is generated from a noisy model"
"We assume that the true data is generated from a noisy model"
]
},
{
"cell_type": "markdown",
"id": "32261f48",
"id": "b871ec69",
"metadata": {
"editable": true
},
@@ -366,7 +373,7 @@
},
{
"cell_type": "markdown",
"id": "b9f27316",
"id": "b47c19bc",
"metadata": {
"editable": true
},
@@ -376,42 +383,45 @@
"\n",
"In our derivation of the ordinary least squares method we defined then\n",
"an approximation to the function $f$ in terms of the parameters\n",
"$\\boldsymbol{\\beta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\beta}$.\n",
"$\\boldsymbol{\\theta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\theta}$.\n",
"\n",
"The parameters $\\boldsymbol{\\beta}$ are in turn found by optimizing the mean\n",
"The parameters $\\boldsymbol{\\theta}$ are in turn found by optimizing the mean\n",
"squared error via the so-called cost function"
]
},
{
"cell_type": "markdown",
"id": "cf5f4bba",
"id": "6db622c2",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"C(\\boldsymbol{X},\\boldsymbol{\\beta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
"C(\\boldsymbol{X},\\boldsymbol{\\theta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "b8c5d66e",
"id": "5a7eb70d",
"metadata": {
"editable": true
},
"source": [
"Here the expected value $\\mathbb{E}$ is the sample value. \n",
"\n",
"Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a\n",
"term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.\n",
"Show that you can rewrite this in terms of a term which contains the\n",
"variance of the model itself (the so-called variance term), a term\n",
"which measures the deviation from the true data and the mean value of\n",
"the model (the bias term) and finally the variance of the noise.\n",
"\n",
"That is, show that"
]
},
{
"cell_type": "markdown",
"id": "c2f3364d",
"id": "d50292fe",
"metadata": {
"editable": true
},
@@ -423,17 +433,17 @@
},
{
"cell_type": "markdown",
"id": "96540fa5",
"id": "50fa641f",
"metadata": {
"editable": true
},
"source": [
"with"
"with (we approximate $f(\\boldsymbol{x})\\approx \\boldsymbol{y}$)"
]
},
{
"cell_type": "markdown",
"id": "0d8d58c5",
"id": "2bd429c9",
"metadata": {
"editable": true
},
@@ -445,7 +455,7 @@
},
{
"cell_type": "markdown",
"id": "6145f2ca",
"id": "737c2819",
"metadata": {
"editable": true
},
@@ -455,7 +465,7 @@
},
{
"cell_type": "markdown",
"id": "39915c14",
"id": "41ef92ef",
"metadata": {
"editable": true
},
@@ -467,13 +477,17 @@
},
{
"cell_type": "markdown",
"id": "f738ba25",
"id": "b948dab0",
"metadata": {
"editable": true
},
"source": [
"The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.\n",
"Explain what the terms mean and discuss their interpretations.\n",
"**Important note**: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\\boldsymbol{x})$ in the expression for the bias with $\\boldsymbol{y}$. \n",
"\n",
"The answer to this exercise should be included in the theory part of\n",
"the report. This exercise is also part of the weekly exercises of\n",
"week 38. Explain what the terms mean and discuss their\n",
"interpretations.\n",
"\n",
"Perform then a bias-variance analysis of the Runge function by\n",
"studying the MSE value as function of the complexity of your model.\n",
@@ -486,7 +500,7 @@
},
{
"cell_type": "markdown",
"id": "ad9cec79",
"id": "6a0548bf",
"metadata": {
"editable": true
},
@@ -496,19 +510,20 @@
"The aim here is to implement another widely popular\n",
"resampling technique, the so-called cross-validation method. \n",
"\n",
"Implement the $k$-fold cross-validation algorithm (feel free to use the functionality of **Scikit-Learn** or write your own code) and evaluate again the MSE function resulting\n",
"from the test folds. \n",
"Implement the $k$-fold cross-validation algorithm (feel free to use\n",
"the functionality of **Scikit-Learn** or write your own code) and\n",
"evaluate again the MSE function resulting from the test folds.\n",
"\n",
"Compare the MSE you get from your cross-validation code with the one\n",
"you got from your **bootstrap** code. Comment your results. Try $5-10$\n",
"folds. \n",
"you got from your **bootstrap** code from the previous exercise. Comment and interpret your results. \n",
"\n",
"In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis."
"In addition to using the ordinary least squares method, you should\n",
"include both Ridge and Lasso regression in the final analysis."
]
},
{
"cell_type": "markdown",
"id": "264c7e54",
"id": "df9845cb",
"metadata": {
"editable": true
},
@@ -517,12 +532,12 @@
"\n",
"1. For a discussion and derivation of the variances and mean squared errors using linear regression, see the [Lecture notes on ridge regression by Wessel N. van Wieringen](https://arxiv.org/abs/1509.09169)\n",
"\n",
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis here."
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis of parts g) and h)."
]
},
{
"cell_type": "markdown",
"id": "5261eeb8",
"id": "b9e04791",
"metadata": {
"editable": true
},
@@ -552,7 +567,7 @@
},
{
"cell_type": "markdown",
"id": "1ede6542",
"id": "3fab6237",
"metadata": {
"editable": true
},
@@ -574,7 +589,7 @@
},
{
"cell_type": "markdown",
"id": "242e8a69",
"id": "3388eb60",
"metadata": {
"editable": true
},
@@ -620,7 +635,25 @@
]
}
],
"metadata": {},
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+115 -83
View File
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "fabb4bef",
"id": "b209e219",
"metadata": {
"editable": true
},
@@ -14,7 +14,7 @@
},
{
"cell_type": "markdown",
"id": "408cd05f",
"id": "6fa4c4bc",
"metadata": {
"editable": true
},
@@ -27,7 +27,7 @@
},
{
"cell_type": "markdown",
"id": "c5348eff",
"id": "735b16c4",
"metadata": {
"editable": true
},
@@ -35,17 +35,19 @@
"## Preamble: Note on writing reports, using reference material, AI and other tools\n",
"\n",
"We want you to answer the three different projects by handing in\n",
"reports written like a standard scientific/technical report. The links\n",
"at <https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects>\n",
"Furthermore, at the same link, \n",
"you can find examples of previous reports. How to write reports will\n",
"also be discussed during the various lab sessions. Please do ask us if you are in doubt.\n",
"reports written like a standard scientific/technical report. The\n",
"links at\n",
"<https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects>\n",
"contain more information. There you can find examples of previous\n",
"reports, the projects themselves, how we rade reports etc. How to\n",
"write reports will also be discussed during the various lab\n",
"sessions. Please do ask us if you are in doubt.\n",
"\n",
"When using codes and material from other sources, you should refer to\n",
"these in the bibliography of your report, indicating wherefrom you for\n",
"example got the code, whether this is from the lecture notes,\n",
"softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such\n",
"AI software. These should always be cited correctly. How to cite some\n",
"softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources\n",
"should always be cited correctly. How to cite some\n",
"of the libraries is often indicated from their corresponding GitHub\n",
"sites or websites, see for example how to cite Scikit-Learn at\n",
"<https://scikit-learn.org/dev/about.html>.\n",
@@ -73,7 +75,7 @@
},
{
"cell_type": "markdown",
"id": "3b4ce1f7",
"id": "0b7956ca",
"metadata": {
"editable": true
},
@@ -81,7 +83,7 @@
"## Regression analysis and resampling methods\n",
"\n",
"The main aim of this project is to study in more detail various\n",
"regression methods, including the Ordinary Least Squares (OLS) method.\n",
"regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.\n",
"In addition to the scientific part, in this course we want also to\n",
"give you an experience in writing scientific reports.\n",
"\n",
@@ -93,7 +95,7 @@
},
{
"cell_type": "markdown",
"id": "ad2e0491",
"id": "28ba3d22",
"metadata": {
"editable": true
},
@@ -105,47 +107,46 @@
},
{
"cell_type": "markdown",
"id": "ad3e88a2",
"id": "9a3e10ba",
"metadata": {
"editable": true
},
"source": [
"Our first step will be to perform an OLS regression analysis of this\n",
"function, trying out a polynomial fit with an $x$ dependence of the\n",
"form $[x,x^2,\\dots]$. We can use a uniform distribution to set up the\n",
"form $[x,x^2,\\dots]$. You can use a uniform distribution to set up the\n",
"arrays of values for $x \\in [-1,1]$, or alternatively use a fixed step size.\n",
"Thereafter we will repeat much of the\n",
"same procedure using the Ridge and Lasso regression methods,\n",
"introducing thus a dependence on the hyperparameter (penalty) $\\lambda$.\n",
"Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,\n",
"introducing thereby a dependence on the hyperparameter (penalty) $\\lambda$.\n",
"\n",
"We will also include bootstrap as a resampling technique in order to\n",
"study the so-called **bias-variance tradeoff**. After that we will\n",
"include the cross-validation technique."
"include the so-called cross-validation technique."
]
},
{
"cell_type": "markdown",
"id": "0cf7b59c",
"id": "8aa547a5",
"metadata": {
"editable": true
},
"source": [
"### Part a : Ordinary Least Square (OLS) for the Runge function\n",
"\n",
"We will generate our own dataset for a function\n",
"We will generate our own dataset for abovementioned function\n",
"$\\mathrm{Runge}(x)$ function with $x\\in [-1,1]$. You should explore also the addition\n",
"of an added stochastic noise to this function using the normal\n",
"distribution $N(0,1)$.\n",
"\n",
"*Write your own code* (using for example the pseudoinverse function **pinv** from **Numpy** ) and perform a standard **ordinary least square regression**\n",
"analysis using polynomials in $x$ up to order $15$. Explore the dependence on the number of data points and the polynomial degree.\n",
"analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.\n",
"\n",
"Evaluate the mean Squared error (MSE)"
]
},
{
"cell_type": "markdown",
"id": "fbd565ab",
"id": "68fbf03d",
"metadata": {
"editable": true
},
@@ -158,7 +159,7 @@
},
{
"cell_type": "markdown",
"id": "af65f0a5",
"id": "b49509bc",
"metadata": {
"editable": true
},
@@ -170,7 +171,7 @@
},
{
"cell_type": "markdown",
"id": "840b2c14",
"id": "0fa4ffc6",
"metadata": {
"editable": true
},
@@ -182,7 +183,7 @@
},
{
"cell_type": "markdown",
"id": "006f0e51",
"id": "ce462b32",
"metadata": {
"editable": true
},
@@ -192,7 +193,7 @@
},
{
"cell_type": "markdown",
"id": "cfe91edf",
"id": "a5fbef36",
"metadata": {
"editable": true
},
@@ -204,17 +205,17 @@
},
{
"cell_type": "markdown",
"id": "2aea20dd",
"id": "a6afe9cb",
"metadata": {
"editable": true
},
"source": [
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).\n",
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).\n",
"Plot also the parameters $\\theta$ as you increase the order of the polynomial. Comment your results.\n",
"\n",
"Your code has to include a scaling/centering of the data (for example by\n",
"subtracting the mean value), and\n",
"a split of the data in training and test data. For this exercise you can\n",
"a split of the data in training and test data. For the scaling you can\n",
"either write your own code or use for example the function for\n",
"splitting training data provided by the library **Scikit-Learn** (make\n",
"sure you have installed it). This function is called\n",
@@ -235,7 +236,7 @@
},
{
"cell_type": "markdown",
"id": "6be73dff",
"id": "3be10f68",
"metadata": {
"editable": true
},
@@ -243,16 +244,16 @@
"### Part b: Adding Ridge regression for the Runge function\n",
"\n",
"Write your own code for the Ridge method as done in the previous\n",
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.\n",
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.\n",
"\n",
"Perform the same analysis as you did in the previous exercise but now for different values of $\\lambda$. Compare and\n",
"analyze your results with those obtained in part a) with the ordinary least squares method. Study the\n",
"analyze your results with those obtained in part a) with the OLS method. Study the\n",
"dependence on $\\lambda$."
]
},
{
"cell_type": "markdown",
"id": "14a95206",
"id": "caa7909c",
"metadata": {
"editable": true
},
@@ -271,7 +272,7 @@
},
{
"cell_type": "markdown",
"id": "30cf0ac3",
"id": "3aac4df1",
"metadata": {
"editable": true
},
@@ -282,12 +283,12 @@
"the gradient descent method by including **momentum**, **ADAgrad**,\n",
"**RMSprop** and **ADAM** as methods fro iteratively updating your learning\n",
"rate. Discuss the results and compare the different methods applied to\n",
"the one-dimensional Runge function."
"the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods."
]
},
{
"cell_type": "markdown",
"id": "99fc2b25",
"id": "d0862a53",
"metadata": {
"editable": true
},
@@ -296,9 +297,9 @@
"\n",
"LASSO regression (see lecture slides from week 36 and week 37)\n",
"represents our first encounter with a machine learning method which\n",
"cannot be solved through analytical expressions. Use the gradient\n",
"cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient\n",
"descent methods you developed in parts c) and d) to solve the LASSO\n",
"optimization problem. You can compare your results using\n",
"optimization problem. You can compare your results with \n",
"the functionalities of **Scikit-Learn**.\n",
"\n",
"Discuss (critically) your results for the Runge function from OLS,\n",
@@ -308,29 +309,31 @@
},
{
"cell_type": "markdown",
"id": "26655c62",
"id": "9170032e",
"metadata": {
"editable": true
},
"source": [
"### Part f: Stochastic gradient descent\n",
"\n",
"Our last gradient step is to include stochastic gradient descent using the\n",
"same methods to update the learning rates as in parts c-e).\n",
"Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods."
"Our last gradient step is to include stochastic gradient descent using\n",
"the same methods to update the learning rates as in parts c-e).\n",
"Compare and discuss your results with and without stochastic gradient\n",
"and give a critical assessment of the various methods."
]
},
{
"cell_type": "markdown",
"id": "990fba0d",
"id": "bacd1035",
"metadata": {
"editable": true
},
"source": [
"### Part g: Bias-variance trade-off and resampling techniques\n",
"\n",
"Our aim here is to study the bias-variance trade-off by implementing the **bootstrap** resampling technique.\n",
"**We will only use the simpler ordinary least squares here**.\n",
"Our aim here is to study the bias-variance trade-off by implementing\n",
"the **bootstrap** resampling technique. **We will only use the simpler\n",
"ordinary least squares here**.\n",
"\n",
"With a code which does OLS and includes resampling techniques, \n",
"we will now discuss the bias-variance trade-off in the context of\n",
@@ -338,11 +341,14 @@
"intuitions and ideas discussed here also carry over to classification\n",
"tasks and basically all Machine Learning algorithms. \n",
"\n",
"Before you perform an analysis of the bias-variance trade-off on your test data, make\n",
"first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and\n",
"Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to \n",
"indicate possible regions of low/high bias and variance. You will most likely not get an\n",
"equally smooth curve!\n",
"Before you perform an analysis of the bias-variance trade-off on your\n",
"test data, make first a figure similar to Fig. 2.11 of Hastie,\n",
"Tibshirani, and Friedman. Figure 2.11 of this reference displays only\n",
"the test and training MSEs. The test MSE can be used to indicate\n",
"possible regions of low/high bias and variance. You will most likely\n",
"not get an equally smooth curve! You may also need to increase the\n",
"polynomial order and play around with the number of data points as\n",
"well (see also the exercise set from week 35).\n",
"\n",
"With this result we move on to the bias-variance trade-off analysis.\n",
"\n",
@@ -350,12 +356,12 @@
"dataset $\\mathcal{L}$ consisting of the data\n",
"$\\mathbf{X}_\\mathcal{L}=\\{(y_j, \\boldsymbol{x}_j), j=0\\ldots n-1\\}$.\n",
"\n",
"As in part d), we assume that the true data is generated from a noisy model"
"We assume that the true data is generated from a noisy model"
]
},
{
"cell_type": "markdown",
"id": "0a53c96b",
"id": "b871ec69",
"metadata": {
"editable": true
},
@@ -367,7 +373,7 @@
},
{
"cell_type": "markdown",
"id": "57a94082",
"id": "b47c19bc",
"metadata": {
"editable": true
},
@@ -377,42 +383,45 @@
"\n",
"In our derivation of the ordinary least squares method we defined then\n",
"an approximation to the function $f$ in terms of the parameters\n",
"$\\boldsymbol{\\beta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\beta}$.\n",
"$\\boldsymbol{\\theta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\theta}$.\n",
"\n",
"The parameters $\\boldsymbol{\\beta}$ are in turn found by optimizing the mean\n",
"The parameters $\\boldsymbol{\\theta}$ are in turn found by optimizing the mean\n",
"squared error via the so-called cost function"
]
},
{
"cell_type": "markdown",
"id": "6ec185aa",
"id": "6db622c2",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"C(\\boldsymbol{X},\\boldsymbol{\\beta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
"C(\\boldsymbol{X},\\boldsymbol{\\theta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "ad16677a",
"id": "5a7eb70d",
"metadata": {
"editable": true
},
"source": [
"Here the expected value $\\mathbb{E}$ is the sample value. \n",
"\n",
"Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a\n",
"term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.\n",
"Show that you can rewrite this in terms of a term which contains the\n",
"variance of the model itself (the so-called variance term), a term\n",
"which measures the deviation from the true data and the mean value of\n",
"the model (the bias term) and finally the variance of the noise.\n",
"\n",
"That is, show that"
]
},
{
"cell_type": "markdown",
"id": "ea1fc9fb",
"id": "d50292fe",
"metadata": {
"editable": true
},
@@ -424,17 +433,17 @@
},
{
"cell_type": "markdown",
"id": "0fce3b1e",
"id": "50fa641f",
"metadata": {
"editable": true
},
"source": [
"with"
"with (we approximate $f(\\boldsymbol{x})\\approx \\boldsymbol{y}$)"
]
},
{
"cell_type": "markdown",
"id": "0a28aa27",
"id": "2bd429c9",
"metadata": {
"editable": true
},
@@ -446,7 +455,7 @@
},
{
"cell_type": "markdown",
"id": "bc135ab9",
"id": "737c2819",
"metadata": {
"editable": true
},
@@ -456,7 +465,7 @@
},
{
"cell_type": "markdown",
"id": "c7bee420",
"id": "41ef92ef",
"metadata": {
"editable": true
},
@@ -468,13 +477,17 @@
},
{
"cell_type": "markdown",
"id": "86efb942",
"id": "b948dab0",
"metadata": {
"editable": true
},
"source": [
"The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.\n",
"Explain what the terms mean and discuss their interpretations.\n",
"**Important note**: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\\boldsymbol{x})$ in the expression for the bias with $\\boldsymbol{y}$. \n",
"\n",
"The answer to this exercise should be included in the theory part of\n",
"the report. This exercise is also part of the weekly exercises of\n",
"week 38. Explain what the terms mean and discuss their\n",
"interpretations.\n",
"\n",
"Perform then a bias-variance analysis of the Runge function by\n",
"studying the MSE value as function of the complexity of your model.\n",
@@ -487,7 +500,7 @@
},
{
"cell_type": "markdown",
"id": "027501c3",
"id": "6a0548bf",
"metadata": {
"editable": true
},
@@ -497,19 +510,20 @@
"The aim here is to implement another widely popular\n",
"resampling technique, the so-called cross-validation method. \n",
"\n",
"Implement the $k$-fold cross-validation algorithm (feel free to use the functionality of **Scikit-Learn** or write your own code) and evaluate again the MSE function resulting\n",
"from the test folds. \n",
"Implement the $k$-fold cross-validation algorithm (feel free to use\n",
"the functionality of **Scikit-Learn** or write your own code) and\n",
"evaluate again the MSE function resulting from the test folds.\n",
"\n",
"Compare the MSE you get from your cross-validation code with the one\n",
"you got from your **bootstrap** code. Comment your results. Try $5-10$\n",
"folds. \n",
"you got from your **bootstrap** code from the previous exercise. Comment and interpret your results. \n",
"\n",
"In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis."
"In addition to using the ordinary least squares method, you should\n",
"include both Ridge and Lasso regression in the final analysis."
]
},
{
"cell_type": "markdown",
"id": "3c583b47",
"id": "df9845cb",
"metadata": {
"editable": true
},
@@ -518,12 +532,12 @@
"\n",
"1. For a discussion and derivation of the variances and mean squared errors using linear regression, see the [Lecture notes on ridge regression by Wessel N. van Wieringen](https://arxiv.org/abs/1509.09169)\n",
"\n",
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis here."
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis of parts g) and h)."
]
},
{
"cell_type": "markdown",
"id": "2d085c9a",
"id": "b9e04791",
"metadata": {
"editable": true
},
@@ -553,7 +567,7 @@
},
{
"cell_type": "markdown",
"id": "6df68456",
"id": "3fab6237",
"metadata": {
"editable": true
},
@@ -575,7 +589,7 @@
},
{
"cell_type": "markdown",
"id": "b68cec2c",
"id": "3388eb60",
"metadata": {
"editable": true
},
@@ -621,7 +635,25 @@
]
}
],
"metadata": {},
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+62 -47
View File
@@ -136,17 +136,19 @@ September 2
\subsection{Preamble: Note on writing reports, using reference material, AI and other tools}
We want you to answer the three different projects by handing in
reports written like a standard scientific/technical report. The links
at \href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
Furthermore, at the same link,
you can find examples of previous reports. How to write reports will
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
reports written like a standard scientific/technical report. The
links at
\href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
contain more information. There you can find examples of previous
reports, the projects themselves, how we rade reports etc. How to
write reports will also be discussed during the various lab
sessions. Please do ask us if you are in doubt.
When using codes and material from other sources, you should refer to
these in the bibliography of your report, indicating wherefrom you for
example got the code, whether this is from the lecture notes,
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
AI software. These should always be cited correctly. How to cite some
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
should always be cited correctly. How to cite some
of the libraries is often indicated from their corresponding GitHub
sites or websites, see for example how to cite Scikit-Learn at
\href{{https://scikit-learn.org/dev/about.html}}{\nolinkurl{https://scikit-learn.org/dev/about.html}}.
@@ -175,7 +177,7 @@ other data sets, make sure these are sets used for regression problems
\subsection{Regression analysis and resampling methods}
The main aim of this project is to study in more detail various
regression methods, including the Ordinary Least Squares (OLS) method.
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
In addition to the scientific part, in this course we want also to
give you an experience in writing scientific reports.
@@ -189,24 +191,23 @@ f(x) = \frac{1}{1+25x^2}.
Our first step will be to perform an OLS regression analysis of this
function, trying out a polynomial fit with an $x$ dependence of the
form $[x,x^2,\dots]$. We can use a uniform distribution to set up the
form $[x,x^2,\dots]$. You can use a uniform distribution to set up the
arrays of values for $x \in [-1,1]$, or alternatively use a fixed step size.
Thereafter we will repeat much of the
same procedure using the Ridge and Lasso regression methods,
introducing thus a dependence on the hyperparameter (penalty) $\lambda$.
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
introducing thereby a dependence on the hyperparameter (penalty) $\lambda$.
We will also include bootstrap as a resampling technique in order to
study the so-called \textbf{bias-variance tradeoff}. After that we will
include the cross-validation technique.
include the so-called cross-validation technique.
\paragraph{Part a : Ordinary Least Square (OLS) for the Runge function.}
We will generate our own dataset for a function
We will generate our own dataset for abovementioned function
$\mathrm{Runge}(x)$ function with $x\in [-1,1]$. You should explore also the addition
of an added stochastic noise to this function using the normal
distribution $N(0,1)$.
\emph{Write your own code} (using for example the pseudoinverse function \textbf{pinv} from \textbf{Numpy} ) and perform a standard \textbf{ordinary least square regression}
analysis using polynomials in $x$ up to order $15$. Explore the dependence on the number of data points and the polynomial degree.
analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.
Evaluate the mean Squared error (MSE)
@@ -228,12 +229,12 @@ where we have defined the mean value of $\bm{y}$ as
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
\]
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
Plot also the parameters $\theta$ as you increase the order of the polynomial. Comment your results.
Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
a split of the data in training and test data. For the scaling you can
either write your own code or use for example the function for
splitting training data provided by the library \textbf{Scikit-Learn} (make
sure you have installed it). This function is called
@@ -253,10 +254,10 @@ On scaling, we recommend reading the following section from the scikit-learn sof
\paragraph{Part b: Adding Ridge regression for the Runge function.}
Write your own code for the Ridge method as done in the previous
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
Perform the same analysis as you did in the previous exercise but now for different values of $\lambda$. Compare and
analyze your results with those obtained in part a) with the ordinary least squares method. Study the
analyze your results with those obtained in part a) with the OLS method. Study the
dependence on $\lambda$.
\paragraph{Part c: Writing your own gradient descent code.}
@@ -274,14 +275,14 @@ We keep our focus on OLS and Ridge regression and update our code for
the gradient descent method by including \textbf{momentum}, \textbf{ADAgrad},
\textbf{RMSprop} and \textbf{ADAM} as methods fro iteratively updating your learning
rate. Discuss the results and compare the different methods applied to
the one-dimensional Runge function.
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
\paragraph{Part e: Writing our own code for Lasso regression.}
LASSO regression (see lecture slides from week 36 and week 37)
represents our first encounter with a machine learning method which
cannot be solved through analytical expressions. Use the gradient
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
descent methods you developed in parts c) and d) to solve the LASSO
optimization problem. You can compare your results using
optimization problem. You can compare your results with
the functionalities of \textbf{Scikit-Learn}.
Discuss (critically) your results for the Runge function from OLS,
@@ -289,13 +290,15 @@ Ridge and LASSO regression using the various gradient descent
approaches.
\paragraph{Part f: Stochastic gradient descent.}
Our last gradient step is to include stochastic gradient descent using the
same methods to update the learning rates as in parts c-e).
Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods.
Our last gradient step is to include stochastic gradient descent using
the same methods to update the learning rates as in parts c-e).
Compare and discuss your results with and without stochastic gradient
and give a critical assessment of the various methods.
\paragraph{Part g: Bias-variance trade-off and resampling techniques.}
Our aim here is to study the bias-variance trade-off by implementing the \textbf{bootstrap} resampling technique.
\textbf{We will only use the simpler ordinary least squares here}.
Our aim here is to study the bias-variance trade-off by implementing
the \textbf{bootstrap} resampling technique. \textbf{We will only use the simpler
ordinary least squares here}.
With a code which does OLS and includes resampling techniques,
we will now discuss the bias-variance trade-off in the context of
@@ -303,11 +306,14 @@ continuous predictions such as regression. However, many of the
intuitions and ideas discussed here also carry over to classification
tasks and basically all Machine Learning algorithms.
Before you perform an analysis of the bias-variance trade-off on your test data, make
first a figure similar to Fig.~2.11 of Hastie, Tibshirani, and
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
indicate possible regions of low/high bias and variance. You will most likely not get an
equally smooth curve!
Before you perform an analysis of the bias-variance trade-off on your
test data, make first a figure similar to Fig.~2.11 of Hastie,
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
the test and training MSEs. The test MSE can be used to indicate
possible regions of low/high bias and variance. You will most likely
not get an equally smooth curve! You may also need to increase the
polynomial order and play around with the number of data points as
well (see also the exercise set from week 35).
With this result we move on to the bias-variance trade-off analysis.
@@ -315,7 +321,7 @@ Consider a
dataset $\mathcal{L}$ consisting of the data
$\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}$.
As in part d), we assume that the true data is generated from a noisy model
We assume that the true data is generated from a noisy model
\[
\bm{y}=f(\boldsymbol{x}) + \bm{\epsilon}.
@@ -326,24 +332,27 @@ deviation $\sigma^2$.
In our derivation of the ordinary least squares method we defined then
an approximation to the function $f$ in terms of the parameters
$\bm{\beta}$ and the design matrix $\bm{X}$ which embody our model,
that is $\bm{\tilde{y}}=\bm{X}\bm{\beta}$.
$\bm{\theta}$ and the design matrix $\bm{X}$ which embody our model,
that is $\bm{\tilde{y}}=\bm{X}\bm{\theta}$.
The parameters $\bm{\beta}$ are in turn found by optimizing the mean
The parameters $\bm{\theta}$ are in turn found by optimizing the mean
squared error via the so-called cost function
\[
C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
C(\bm{X},\bm{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
\]
Here the expected value $\mathbb{E}$ is the sample value.
Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a
term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.
Show that you can rewrite this in terms of a term which contains the
variance of the model itself (the so-called variance term), a term
which measures the deviation from the true data and the mean value of
the model (the bias term) and finally the variance of the noise.
That is, show that
\[
\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
\]
with
with (we approximate $f(\bm{x})\approx \bm{y}$)
\[
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\bm{y}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right],
\]
@@ -351,8 +360,13 @@ and
\[
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\bm{y}}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2.
\]
The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.
Explain what the terms mean and discuss their interpretations.
\textbf{Important note}: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\bm{x})$ in the expression for the bias with $\bm{y}$.
The answer to this exercise should be included in the theory part of
the report. This exercise is also part of the weekly exercises of
week 38. Explain what the terms mean and discuss their
interpretations.
Perform then a bias-variance analysis of the Runge function by
studying the MSE value as function of the complexity of your model.
@@ -366,14 +380,15 @@ You can follow the code example in the jupyter-book at \href{{https://compphysic
The aim here is to implement another widely popular
resampling technique, the so-called cross-validation method.
Implement the $k$-fold cross-validation algorithm (feel free to use the functionality of \textbf{Scikit-Learn} or write your own code) and evaluate again the MSE function resulting
from the test folds.
Implement the $k$-fold cross-validation algorithm (feel free to use
the functionality of \textbf{Scikit-Learn} or write your own code) and
evaluate again the MSE function resulting from the test folds.
Compare the MSE you get from your cross-validation code with the one
you got from your \textbf{bootstrap} code. Comment your results. Try $5-10$
folds.
you got from your \textbf{bootstrap} code from the previous exercise. Comment and interpret your results.
In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis.
In addition to using the ordinary least squares method, you should
include both Ridge and Lasso regression in the final analysis.
\subsection{Background literature}
+62 -47
View File
@@ -110,17 +110,19 @@ September 2
\subsection*{Preamble: Note on writing reports, using reference material, AI and other tools}
We want you to answer the three different projects by handing in
reports written like a standard scientific/technical report. The links
at \href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
Furthermore, at the same link,
you can find examples of previous reports. How to write reports will
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
reports written like a standard scientific/technical report. The
links at
\href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
contain more information. There you can find examples of previous
reports, the projects themselves, how we rade reports etc. How to
write reports will also be discussed during the various lab
sessions. Please do ask us if you are in doubt.
When using codes and material from other sources, you should refer to
these in the bibliography of your report, indicating wherefrom you for
example got the code, whether this is from the lecture notes,
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
AI software. These should always be cited correctly. How to cite some
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
should always be cited correctly. How to cite some
of the libraries is often indicated from their corresponding GitHub
sites or websites, see for example how to cite Scikit-Learn at
\href{{https://scikit-learn.org/dev/about.html}}{\nolinkurl{https://scikit-learn.org/dev/about.html}}.
@@ -149,7 +151,7 @@ other data sets, make sure these are sets used for regression problems
\subsection*{Regression analysis and resampling methods}
The main aim of this project is to study in more detail various
regression methods, including the Ordinary Least Squares (OLS) method.
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
In addition to the scientific part, in this course we want also to
give you an experience in writing scientific reports.
@@ -163,24 +165,23 @@ f(x) = \frac{1}{1+25x^2}.
Our first step will be to perform an OLS regression analysis of this
function, trying out a polynomial fit with an $x$ dependence of the
form $[x,x^2,\dots]$. We can use a uniform distribution to set up the
form $[x,x^2,\dots]$. You can use a uniform distribution to set up the
arrays of values for $x \in [-1,1]$, or alternatively use a fixed step size.
Thereafter we will repeat much of the
same procedure using the Ridge and Lasso regression methods,
introducing thus a dependence on the hyperparameter (penalty) $\lambda$.
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
introducing thereby a dependence on the hyperparameter (penalty) $\lambda$.
We will also include bootstrap as a resampling technique in order to
study the so-called \textbf{bias-variance tradeoff}. After that we will
include the cross-validation technique.
include the so-called cross-validation technique.
\paragraph{Part a : Ordinary Least Square (OLS) for the Runge function.}
We will generate our own dataset for a function
We will generate our own dataset for abovementioned function
$\mathrm{Runge}(x)$ function with $x\in [-1,1]$. You should explore also the addition
of an added stochastic noise to this function using the normal
distribution $N(0,1)$.
\emph{Write your own code} (using for example the pseudoinverse function \textbf{pinv} from \textbf{Numpy} ) and perform a standard \textbf{ordinary least square regression}
analysis using polynomials in $x$ up to order $15$. Explore the dependence on the number of data points and the polynomial degree.
analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.
Evaluate the mean Squared error (MSE)
@@ -202,12 +203,12 @@ where we have defined the mean value of $\bm{y}$ as
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
\]
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
Plot also the parameters $\theta$ as you increase the order of the polynomial. Comment your results.
Your code has to include a scaling/centering of the data (for example by
subtracting the mean value), and
a split of the data in training and test data. For this exercise you can
a split of the data in training and test data. For the scaling you can
either write your own code or use for example the function for
splitting training data provided by the library \textbf{Scikit-Learn} (make
sure you have installed it). This function is called
@@ -227,10 +228,10 @@ On scaling, we recommend reading the following section from the scikit-learn sof
\paragraph{Part b: Adding Ridge regression for the Runge function.}
Write your own code for the Ridge method as done in the previous
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
Perform the same analysis as you did in the previous exercise but now for different values of $\lambda$. Compare and
analyze your results with those obtained in part a) with the ordinary least squares method. Study the
analyze your results with those obtained in part a) with the OLS method. Study the
dependence on $\lambda$.
\paragraph{Part c: Writing your own gradient descent code.}
@@ -248,14 +249,14 @@ We keep our focus on OLS and Ridge regression and update our code for
the gradient descent method by including \textbf{momentum}, \textbf{ADAgrad},
\textbf{RMSprop} and \textbf{ADAM} as methods fro iteratively updating your learning
rate. Discuss the results and compare the different methods applied to
the one-dimensional Runge function.
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
\paragraph{Part e: Writing our own code for Lasso regression.}
LASSO regression (see lecture slides from week 36 and week 37)
represents our first encounter with a machine learning method which
cannot be solved through analytical expressions. Use the gradient
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
descent methods you developed in parts c) and d) to solve the LASSO
optimization problem. You can compare your results using
optimization problem. You can compare your results with
the functionalities of \textbf{Scikit-Learn}.
Discuss (critically) your results for the Runge function from OLS,
@@ -263,13 +264,15 @@ Ridge and LASSO regression using the various gradient descent
approaches.
\paragraph{Part f: Stochastic gradient descent.}
Our last gradient step is to include stochastic gradient descent using the
same methods to update the learning rates as in parts c-e).
Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods.
Our last gradient step is to include stochastic gradient descent using
the same methods to update the learning rates as in parts c-e).
Compare and discuss your results with and without stochastic gradient
and give a critical assessment of the various methods.
\paragraph{Part g: Bias-variance trade-off and resampling techniques.}
Our aim here is to study the bias-variance trade-off by implementing the \textbf{bootstrap} resampling technique.
\textbf{We will only use the simpler ordinary least squares here}.
Our aim here is to study the bias-variance trade-off by implementing
the \textbf{bootstrap} resampling technique. \textbf{We will only use the simpler
ordinary least squares here}.
With a code which does OLS and includes resampling techniques,
we will now discuss the bias-variance trade-off in the context of
@@ -277,11 +280,14 @@ continuous predictions such as regression. However, many of the
intuitions and ideas discussed here also carry over to classification
tasks and basically all Machine Learning algorithms.
Before you perform an analysis of the bias-variance trade-off on your test data, make
first a figure similar to Fig.~2.11 of Hastie, Tibshirani, and
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
indicate possible regions of low/high bias and variance. You will most likely not get an
equally smooth curve!
Before you perform an analysis of the bias-variance trade-off on your
test data, make first a figure similar to Fig.~2.11 of Hastie,
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
the test and training MSEs. The test MSE can be used to indicate
possible regions of low/high bias and variance. You will most likely
not get an equally smooth curve! You may also need to increase the
polynomial order and play around with the number of data points as
well (see also the exercise set from week 35).
With this result we move on to the bias-variance trade-off analysis.
@@ -289,7 +295,7 @@ Consider a
dataset $\mathcal{L}$ consisting of the data
$\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}$.
As in part d), we assume that the true data is generated from a noisy model
We assume that the true data is generated from a noisy model
\[
\bm{y}=f(\boldsymbol{x}) + \bm{\epsilon}.
@@ -300,24 +306,27 @@ deviation $\sigma^2$.
In our derivation of the ordinary least squares method we defined then
an approximation to the function $f$ in terms of the parameters
$\bm{\beta}$ and the design matrix $\bm{X}$ which embody our model,
that is $\bm{\tilde{y}}=\bm{X}\bm{\beta}$.
$\bm{\theta}$ and the design matrix $\bm{X}$ which embody our model,
that is $\bm{\tilde{y}}=\bm{X}\bm{\theta}$.
The parameters $\bm{\beta}$ are in turn found by optimizing the mean
The parameters $\bm{\theta}$ are in turn found by optimizing the mean
squared error via the so-called cost function
\[
C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
C(\bm{X},\bm{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
\]
Here the expected value $\mathbb{E}$ is the sample value.
Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a
term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.
Show that you can rewrite this in terms of a term which contains the
variance of the model itself (the so-called variance term), a term
which measures the deviation from the true data and the mean value of
the model (the bias term) and finally the variance of the noise.
That is, show that
\[
\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
\]
with
with (we approximate $f(\bm{x})\approx \bm{y}$)
\[
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\bm{y}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right],
\]
@@ -325,8 +334,13 @@ and
\[
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\bm{y}}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2.
\]
The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.
Explain what the terms mean and discuss their interpretations.
\textbf{Important note}: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\bm{x})$ in the expression for the bias with $\bm{y}$.
The answer to this exercise should be included in the theory part of
the report. This exercise is also part of the weekly exercises of
week 38. Explain what the terms mean and discuss their
interpretations.
Perform then a bias-variance analysis of the Runge function by
studying the MSE value as function of the complexity of your model.
@@ -340,14 +354,15 @@ You can follow the code example in the jupyter-book at \href{{https://compphysic
The aim here is to implement another widely popular
resampling technique, the so-called cross-validation method.
Implement the $k$-fold cross-validation algorithm (feel free to use the functionality of \textbf{Scikit-Learn} or write your own code) and evaluate again the MSE function resulting
from the test folds.
Implement the $k$-fold cross-validation algorithm (feel free to use
the functionality of \textbf{Scikit-Learn} or write your own code) and
evaluate again the MSE function resulting from the test folds.
Compare the MSE you get from your cross-validation code with the one
you got from your \textbf{bootstrap} code. Comment your results. Try $5-10$
folds.
you got from your \textbf{bootstrap} code from the previous exercise. Comment and interpret your results.
In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis.
In addition to using the ordinary least squares method, you should
include both Ridge and Lasso regression in the final analysis.
\subsection*{Background literature}