update book

This commit is contained in:
Morten Hjorth-Jensen
2025-09-07 15:58:51 +02:00
parent 610cdc0c4d
commit 831312ce60
49 changed files with 15660 additions and 99 deletions
@@ -62,7 +62,7 @@
<script>DOCUMENTATION_OPTIONS.pagename = 'exercisesweek37';</script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Project 1 on Machine Learning, deadline October 6 (midnight), 2025" href="project1.html" />
<link rel="next" title="Week 37: Statistical interpretations and Resampling Methods" href="week37.html" />
<link rel="prev" title="Week 36: Linear Regression and Gradient descent" href="week36.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
@@ -230,6 +230,8 @@
<li class="toctree-l1"><a class="reference internal" href="exercisesweek36.html">Exercises week 36</a></li>
<li class="toctree-l1"><a class="reference internal" href="week36.html">Week 36: Linear Regression and Gradient descent</a></li>
<li class="toctree-l1 current active"><a class="current reference internal" href="#">Exercises week 37</a></li>
<li class="toctree-l1"><a class="reference internal" href="week37.html">Week 37: Statistical interpretations and Resampling Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="exercisesweek38.html">Exercises week 37</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Projects</span></p>
<ul class="nav bd-sidenav">
@@ -470,7 +472,7 @@ y_centered = ?
</div>
</div>
</div>
<p>Fill in the necessary details.</p>
<p>Fill in the necessary details. Do we need to center the <span class="math notranslate nohighlight">\(y\)</span>-values?</p>
<p>After this preprocessing, each column of <span class="math notranslate nohighlight">\(\boldsymbol{X}_{\mathrm{norm}}\)</span> has mean zero and standard deviation <span class="math notranslate nohighlight">\(1\)</span>
and <span class="math notranslate nohighlight">\(\boldsymbol{y}_{\mathrm{centered}}\)</span> has mean 0. This makes the optimization landscape
nicer and ensures the regularization penalty <span class="math notranslate nohighlight">\(\lambda \sum_j
@@ -487,7 +489,9 @@ same scale).</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># Set regularization parameter, either a single value or a vector of values
lambda = ?
# Note that lambda is a python keyword. The lambda keyword is used to create small, single-expression functions without a formal name. These are often called &quot;anonymous functions&quot; or &quot;lambda functions.&quot;
lam = ?
# Analytical form for OLS and Ridge solution: theta_Ridge = (X^T X + lambda * I)^{-1} X^T y and theta_OLS = (X^T X)^{-1} X^T y
I = np.eye(n_features)
@@ -533,19 +537,8 @@ num_iters = 1000
# Initialize weights for gradient descent
theta = np.zeros(n_features)
# Arrays to store history for plotting
cost_history = np.zeros(num_iters)
# Gradient descent loop
m = n_samples # number of data points
for t in range(num_iters):
# Compute prediction error
error = X_norm.dot(theta) - y_centered
# Compute cost for OLS and Ridge (MSE + regularization for Ridge) for monitoring
cost_OLS = ?
cost_Ridge = ?
# You could add a history for both methods (optional)
cost_history[t] = ?
# Compute gradients for OSL and Ridge
grad_OLS = ?
grad_Ridge = ?
@@ -564,11 +557,13 @@ print(&quot;Gradient Descent Ridge coefficients:&quot;, theta_gdRidge)
</div>
<section id="id2">
<h3>4a)<a class="headerlink" href="#id2" title="Link to this heading">#</a></h3>
<p>Discuss the results as function of the learning rate parameters and the number of iterations.</p>
<p>Write first a gradient descent code for OLS only using the above template.
Discuss the results as function of the learning rate parameters and the number of iterations</p>
</section>
<section id="id3">
<h3>4b)<a class="headerlink" href="#id3" title="Link to this heading">#</a></h3>
<p>Try to add a stopping parameter as function of the number iterations and the difference between the new and old <span class="math notranslate nohighlight">\(\theta\)</span> values. How would you define a stopping criterion?</p>
<p>Write then a similar code for Ridge regression using the above template.
Try to add a stopping parameter as function of the number iterations and the difference between the new and old <span class="math notranslate nohighlight">\(\theta\)</span> values. How would you define a stopping criterion?</p>
</section>
</section>
<section id="exercise-5-ridge-regression-and-a-new-synthetic-dataset">
@@ -667,11 +662,11 @@ should be in the same ballpark. Which method (OLS or Ridge) gives the best resu
</div>
</a>
<a class="right-next"
href="project1.html"
href="week37.html"
title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Project 1 on Machine Learning, deadline October 6 (midnight), 2025</p>
<p class="prev-next-title">Week 37: Statistical interpretations and Resampling Methods</p>
</div>
<i class="fa-solid fa-angle-right"></i>
</a>