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
+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>