update exercises w38 and w39

This commit is contained in:
KarlHenrik
2025-09-10 12:14:32 +02:00
parent cee5f8e1f0
commit fbdf9c32f8
196 changed files with 23368 additions and 300 deletions
@@ -28,7 +28,7 @@
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-brands-400.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-regular-400.woff2" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=03e43079" />
<link rel="stylesheet" type="text/css" href="_static/styles/sphinx-book-theme.css?v=eba8b062" />
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css?v=13237357" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css?v=76b2166b" />
@@ -495,9 +495,13 @@ A given parameter <span class="math notranslate nohighlight">\({\boldsymbol{\hat
\[
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]
\]</div>
<p><strong>a)</strong> 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 Note that in order to be able to evaluate the bias them, you will need to approximate the function <span class="math notranslate nohighlight">\(f\)</span> with the model <span class="math notranslate nohighlight">\({\bf y}\)</span>.
show that</p>
<p><strong>a)</strong> Show that you can rewrite this into an expression which contains</p>
<ul class="simple">
<li><p>the variance of the model (the variance term)</p></li>
<li><p>the expected deviation of the mean of the model from the true data (the bias term)</p></li>
<li><p>the variance of the noise</p></li>
</ul>
<p>In other words, show that:</p>
<div class="math notranslate nohighlight">
\[
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
@@ -520,7 +524,7 @@ show that</p>
<p><strong>a)</strong> Using the expression above, compute the mean squared error, bias and variance of the given data. Check that the sum of the bias and variance correctly gives (approximately) the mean squared error.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">100</span>
<span class="n">bootstraps</span> <span class="o">=</span> <span class="mi">1000</span>
@@ -540,15 +544,15 @@ show that</p>
<p><strong>d)</strong> Perform a bias-variance analysis of a polynomial OLS model fit to a one-dimensional function by computing and plotting the bias and variances values as a function of the polynomial degree of your model.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="p">(</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">matplotlib.pyplot</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">plt</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">sklearn.preprocessing</span><span class="w"> </span><span class="kn">import</span> <span class="p">(</span>
<span class="n">PolynomialFeatures</span><span class="p">,</span>
<span class="p">)</span> <span class="c1"># use the fit_transform method of the created object!</span>
<span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <span class="n">LinearRegression</span>
<span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="kn">import</span> <span class="n">mean_squared_error</span>
<span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">train_test_split</span>
<span class="kn">from</span> <span class="nn">sklearn.utils</span> <span class="kn">import</span> <span class="n">resample</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">sklearn.linear_model</span><span class="w"> </span><span class="kn">import</span> <span class="n">LinearRegression</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">sklearn.metrics</span><span class="w"> </span><span class="kn">import</span> <span class="n">mean_squared_error</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">sklearn.model_selection</span><span class="w"> </span><span class="kn">import</span> <span class="n">train_test_split</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">sklearn.utils</span><span class="w"> </span><span class="kn">import</span> <span class="n">resample</span>
</pre></div>
</div>
</div>
@@ -573,6 +577,8 @@ show that</p>
<span class="c1"># X = ...</span>
<span class="c1"># X_train, X_test, y_train, y_test = ...</span>
<span class="c1">#</span>
<span class="c1"># #this is where you fit your model on the sampled data</span>
<span class="c1">#</span>
<span class="c1"># predictions[b, :] =</span>
<span class="c1"># targets[b, :] =</span>
<span class="c1">#</span>
@@ -584,7 +590,7 @@ show that</p>
</div>
</div>
<p><strong>e)</strong> Discuss the bias-variance trade-off as function of your model complexity (the degree of the polynomial).</p>
<p><strong>f)</strong> Compute and discuss the bias and variance as function of the number of data points (choose a suitable polynomial degree to show something interesiting).</p>
<p><strong>f)</strong> Compute and discuss the bias and variance as function of the number of data points (choose a suitable polynomial degree to show something interesting).</p>
</section>
<section id="exercise-5-interpretation-of-scaling-and-metrics">
<h2>Exercise 5: Interpretation of scaling and metrics<a class="headerlink" href="#exercise-5-interpretation-of-scaling-and-metrics" title="Link to this heading">#</a></h2>