This commit is contained in:
Morten Hjorth-Jensen
2024-09-09 13:11:22 +02:00
parent 804d3bbab9
commit 3d57a8ff6c
25 changed files with 1690 additions and 576 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f9bcf943",
"metadata": {
"editable": true
},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html exercisesweek38.do.txt -->\n",
"<!-- dom:TITLE: Exercises week 38 -->"
]
},
{
"cell_type": "markdown",
"id": "ead6d6d6",
"metadata": {
"editable": true
},
"source": [
"# Exercises week 38\n",
"**September 16-20, 2024**\n",
"\n",
"Date: **Deadline is Friday September 20 at midnight**"
]
},
{
"cell_type": "markdown",
"id": "b6ce9344",
"metadata": {
"editable": true
},
"source": [
"## Overarching aims of the exercises this week\n",
"\n",
"The aim of the exercises this week is to derive the equations for the bias-variance tradeoff to be used in project 1 as well as testing this for a simpler function using the bootstrap method. The exercises here can be reused in project 1 as well.\n",
"\n",
"Consider a\n",
"dataset $\\mathcal{L}$ consisting of the data\n",
"$\\mathbf{X}_\\mathcal{L}=\\{(y_j, \\boldsymbol{x}_j), j=0\\ldots n-1\\}$.\n",
"\n",
"We assume that the true data is generated from a noisy model"
]
},
{
"cell_type": "markdown",
"id": "29371f21",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\boldsymbol{y}=f(\\boldsymbol{x}) + \\boldsymbol{\\epsilon}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "36a8765e",
"metadata": {
"editable": true
},
"source": [
"Here $\\epsilon$ is normally distributed with mean zero and standard\n",
"deviation $\\sigma^2$.\n",
"\n",
"In our derivation of the ordinary least squares method we defined \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",
"\n",
"The parameters $\\boldsymbol{\\beta}$ are in turn found by optimizing the mean\n",
"squared error via the so-called cost function"
]
},
{
"cell_type": "markdown",
"id": "68dd52df",
"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",
"$$"
]
},
{
"cell_type": "markdown",
"id": "68a07606",
"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",
"That is, show that"
]
},
{
"cell_type": "markdown",
"id": "fbbb3fd7",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right]=\\mathrm{Bias}[\\tilde{y}]+\\mathrm{var}[\\tilde{y}]+\\sigma^2,\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "5ecc0f22",
"metadata": {
"editable": true
},
"source": [
"with"
]
},
{
"cell_type": "markdown",
"id": "8c17bd0a",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathrm{Bias}[\\tilde{y}]=\\mathbb{E}\\left[\\left(\\boldsymbol{y}-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right]\\right)^2\\right],\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "55ab4f0c",
"metadata": {
"editable": true
},
"source": [
"and"
]
},
{
"cell_type": "markdown",
"id": "98f93c68",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\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.\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "d6d099d4",
"metadata": {
"editable": true
},
"source": [
"Explain what the terms mean and discuss their interpretations.\n",
"\n",
"Perform then a bias-variance analysis of a simple one-dimensional (or other models of your choice) function by\n",
"studying the MSE value as function of the complexity of your model. Use ordinary least squares only.\n",
"\n",
"Discuss the bias and variance trade-off as function\n",
"of your model complexity (the degree of the polynomial) and the number\n",
"of data points, and possibly also your training and test data using the **bootstrap** resampling method.\n",
"You can follow the code example in the jupyter-book at <https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff>."
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,535 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Exercises week 38 &#8212; Applied Data Analysis and Machine Learning</title>
<link href="_static/css/theme.css" rel="stylesheet">
<link href="_static/css/index.ff1ffe594081f20da1ef19478df9384b.css" rel="stylesheet">
<link rel="stylesheet"
href="_static/vendor/fontawesome/5.13.0/css/all.min.css">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-solid-900.woff2">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-brands-400.woff2">
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-book-theme.css?digest=c3fdc42140077d1ad13ad2f1588a4309" />
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="_static/mystnb.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" />
<link rel="preload" as="script" href="_static/js/index.be7d3bbb2ef33a8344ce.js">
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="_static/togglebutton.js"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
<script src="_static/sphinx-book-theme.d59cb220de22ca1c485ebbdc042f0030.js"></script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"
const thebe_selector = ".thebe,.cell"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output, .cell_output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<script>window.MathJax = {"options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></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 7 (midnight), 2024" href="project1.html" />
<link rel="prev" title="Week 37: Statistical interpretations and Resampling Methods" href="week37.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="None">
<!-- Google Analytics -->
</head>
<body data-spy="scroll" data-target="#bd-toc-nav" data-offset="80">
<div class="container-fluid" id="banner"></div>
<div class="container-xl">
<div class="row">
<div class="col-12 col-md-3 bd-sidebar site-navigation show" id="site-navigation">
<div class="navbar-brand-box">
<a class="navbar-brand text-wrap" href="index.html">
<!-- `logo` is deprecated in Sphinx 4.0, so remove this when we stop supporting 3 -->
<img src="_static/logo.png" class="logo" alt="logo">
<h1 class="site-logo" id="site-title">Applied Data Analysis and Machine Learning</h1>
</a>
</div><form class="bd-search d-flex align-items-center" action="search.html" method="get">
<i class="icon fas fa-search"></i>
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search this book..." aria-label="Search this book..." autocomplete="off" >
</form><nav class="bd-links" id="bd-docs-nav" aria-label="Main">
<div class="bd-toc-item active">
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="intro.html">
Applied Data Analysis and Machine Learning
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
About the course
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="schedule.html">
Teaching schedule with links to material
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="teachers.html">
Teachers and Grading
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="textbooks.html">
Textbooks
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Review of Statistics with Resampling Techniques and Linear Algebra
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="statistics.html">
1. Elements of Probability Theory and Statistical Data Analysis
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="linalg.html">
2. Linear Algebra, Handling of Arrays and more Python Features
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
From Regression to Support Vector Machines
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="chapter1.html">
3. Linear Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter2.html">
4. Ridge and Lasso Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter3.html">
5. Resampling Methods
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter4.html">
6. Logistic Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapteroptimization.html">
7. Optimization, the central part of any Machine Learning algortithm
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter5.html">
8. Support Vector Machines, overarching aims
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Decision Trees, Ensemble Methods and Boosting
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="chapter6.html">
9. Decision trees, overarching aims
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter7.html">
10. Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Dimensionality Reduction
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="chapter8.html">
11. Basic ideas of the Principal Component Analysis (PCA)
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="clustering.html">
12. Clustering and Unsupervised Learning
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Deep Learning Methods
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="chapter9.html">
13. Neural networks
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter10.html">
14. Building a Feed Forward Neural Network
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter11.html">
15. Solving Differential Equations with Deep Learning
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter12.html">
16. Convolutional Neural Networks
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter13.html">
17. Recurrent neural networks: Overarching view
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Weekly material, notes and exercises
</span>
</p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="exercisesweek34.html">
Exercises week 34
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="week34.html">
Week 34: Introduction to the course, Logistics and Practicalities
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="exercisesweek35.html">
Exercises week 35
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="week35.html">
Week 35: From Ordinary Linear Regression to Ridge and Lasso Regression
</a>
</li>
<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 Statistical interpretations
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="exercisesweek37.html">
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 current active">
<a class="current reference internal" href="#">
Exercises week 38
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Projects
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="project1.html">
Project 1 on Machine Learning, deadline October 7 (midnight), 2024
</a>
</li>
</ul>
</div>
</nav> <!-- To handle the deprecated key -->
<div class="navbar_extra_footer">
Powered by <a href="https://jupyterbook.org">Jupyter Book</a>
</div>
</div>
<main class="col py-md-3 pl-md-4 bd-content overflow-auto" role="main">
<div class="topbar container-xl fixed-top">
<div class="topbar-contents row">
<div class="col-12 col-md-3 bd-topbar-whitespace site-navigation show"></div>
<div class="col pl-md-4 topbar-main">
<button id="navbar-toggler" class="navbar-toggler ml-0" type="button" data-toggle="collapse"
data-toggle="tooltip" data-placement="bottom" data-target=".site-navigation" aria-controls="navbar-menu"
aria-expanded="true" aria-label="Toggle navigation" aria-controls="site-navigation"
title="Toggle navigation" data-toggle="tooltip" data-placement="left">
<i class="fas fa-bars"></i>
<i class="fas fa-arrow-left"></i>
<i class="fas fa-arrow-up"></i>
</button>
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn" aria-label="Download this page"><i
class="fas fa-download"></i></button>
<div class="dropdown-buttons">
<!-- ipynb file if we had a myst markdown file -->
<!-- Download raw file -->
<a class="dropdown-buttons" href="_sources/exercisesweek38.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Download source file" data-toggle="tooltip"
data-placement="left">.ipynb</button></a>
<!-- Download PDF via print -->
<button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="Print to PDF"
onclick="printPdf(this)" data-toggle="tooltip" data-placement="left">.pdf</button>
</div>
</div>
<!-- Source interaction buttons -->
<!-- Full screen (wrap in <a> to have style consistency -->
<a class="full-screen-button"><button type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip"
data-placement="bottom" onclick="toggleFullScreen()" aria-label="Fullscreen mode"
title="Fullscreen mode"><i
class="fas fa-expand"></i></button></a>
<!-- Launch buttons -->
</div>
<!-- Table of contents -->
<div class="d-none d-md-block col-md-2 bd-toc show noprint">
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i> Contents
</div>
<nav id="bd-toc-nav" aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#overarching-aims-of-the-exercises-this-week">
Overarching aims of the exercises this week
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div id="main-content" class="row">
<div class="col-12 col-md-9 pl-md-3 pr-md-0">
<!-- Table of contents that is only displayed when printing the page -->
<div id="jb-print-docs-body" class="onlyprint">
<h1>Exercises week 38</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#overarching-aims-of-the-exercises-this-week">
Overarching aims of the exercises this week
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div>
<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)
doconce format html exercisesweek38.do.txt -->
<!-- dom:TITLE: Exercises week 38 --><div class="tex2jax_ignore mathjax_ignore section" id="exercises-week-38">
<h1>Exercises week 38<a class="headerlink" href="#exercises-week-38" title="Permalink to this headline"></a></h1>
<p><strong>September 16-20, 2024</strong></p>
<p>Date: <strong>Deadline is Friday September 20 at midnight</strong></p>
<div class="section" id="overarching-aims-of-the-exercises-this-week">
<h2>Overarching aims of the exercises this week<a class="headerlink" href="#overarching-aims-of-the-exercises-this-week" title="Permalink to this headline"></a></h2>
<p>The aim of the exercises this week is to derive the equations for the bias-variance tradeoff to be used in project 1 as well as testing this for a simpler function using the bootstrap method. The exercises here can be reused in project 1 as well.</p>
<p>Consider a
dataset <span class="math notranslate nohighlight">\(\mathcal{L}\)</span> consisting of the data
<span class="math notranslate nohighlight">\(\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}\)</span>.</p>
<p>We assume that the true data is generated from a noisy model</p>
<div class="math notranslate nohighlight">
\[
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
\]</div>
<p>Here <span class="math notranslate nohighlight">\(\epsilon\)</span> is normally distributed with mean zero and standard
deviation <span class="math notranslate nohighlight">\(\sigma^2\)</span>.</p>
<p>In our derivation of the ordinary least squares method we defined
an approximation to the function <span class="math notranslate nohighlight">\(f\)</span> in terms of the parameters
<span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> and the design matrix <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span> which embody our model,
that is <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta}\)</span>.</p>
<p>The parameters <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> are in turn found by optimizing the mean
squared error via the so-called cost function</p>
<div class="math notranslate nohighlight">
\[
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>Here the expected value <span class="math notranslate nohighlight">\(\mathbb{E}\)</span> 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>
<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,
\]</div>
<p>with</p>
<div class="math notranslate nohighlight">
\[
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right],
\]</div>
<p>and</p>
<div class="math notranslate nohighlight">
\[
\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.
\]</div>
<p>Explain what the terms mean and discuss their interpretations.</p>
<p>Perform then a bias-variance analysis of a simple one-dimensional (or other models of your choice) function by
studying the MSE value as function of the complexity of your model. Use ordinary least squares only.</p>
<p>Discuss the bias and variance trade-off as function
of your model complexity (the degree of the polynomial) and the number
of data points, and possibly also your training and test data using the <strong>bootstrap</strong> resampling method.
You can follow the code example in the jupyter-book at <a class="reference external" href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff">https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff</a>.</p>
</div>
</div>
<script type="text/x-thebe-config">
{
requestKernel: true,
binderOptions: {
repo: "binder-examples/jupyter-stacks-datascience",
ref: "master",
},
codeMirrorConfig: {
theme: "abcdef",
mode: "python"
},
kernelOptions: {
kernelName: "python3",
path: "./."
},
predefinedOutput: true
}
</script>
<script>kernelName = 'python3'</script>
</div>
<!-- Previous / next buttons -->
<div class='prev-next-area'>
<a class='left-prev' id="prev-link" href="week37.html" title="previous page">
<i class="fas fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">Week 37: Statistical interpretations and Resampling Methods</p>
</div>
</a>
<a class='right-next' id="next-link" href="project1.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 7 (midnight), 2024</p>
</div>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>
</div>
<footer class="footer">
<p>
By Morten Hjorth-Jensen<br/>
&copy; Copyright 2021.<br/>
</p>
</footer>
</main>
</div>
</div>
<script src="_static/js/index.be7d3bbb2ef33a8344ce.js"></script>
</body>
</html>
@@ -284,6 +284,11 @@ const thebe_selector_output = ".output, .cell_output"
Week 37: Statistical interpretations and Resampling Methods
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="exercisesweek38.html">
Exercises week 38
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
+5
View File
@@ -285,6 +285,11 @@ const thebe_selector_output = ".output, .cell_output"
Week 37: Statistical interpretations and Resampling Methods
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="exercisesweek38.html">
Exercises week 38
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Binary file not shown.
+5
View File
@@ -290,6 +290,11 @@ const thebe_selector_output = ".output, .cell_output"
Week 37: Statistical interpretations and Resampling Methods
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="exercisesweek38.html">
Exercises week 38
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
File diff suppressed because one or more lines are too long
+25 -16
View File
@@ -55,7 +55,7 @@ const thebe_selector_output = ".output, .cell_output"
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></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 7 (midnight), 2024" href="project1.html" />
<link rel="next" title="Exercises week 38" href="exercisesweek38.html" />
<link rel="prev" title="Exercises week 37" href="exercisesweek37.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="None">
@@ -288,6 +288,11 @@ const thebe_selector_output = ".output, .cell_output"
Week 37: Statistical interpretations and Resampling Methods
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="exercisesweek38.html">
Exercises week 38
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
@@ -961,9 +966,9 @@ doconce format html week37.do.txt --no_mako -->
<!-- todo add link to videos and add link to Van Wieringens notes --><div class="section" id="plans-for-week-37-lecture-monday">
<h2>Plans for week 37, lecture Monday<a class="headerlink" href="#plans-for-week-37-lecture-monday" title="Permalink to this headline"></a></h2>
<p><strong>Material for the lecture on Monday September 9.</strong></p>
<!-- * [Video of Lecture](https://youtu.be/YOBBr_toYxc) -->
<!-- * [Whiteboard notes](https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2023/NotesSep14.pdf) -->
<ul class="simple">
<li><p><a class="reference external" href="https://youtu.be/omLmp_kkie0">Video of Lecture</a></p></li>
<li><p><a class="reference external" href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesSeptember9.pdf">Whiteboard notes</a></p></li>
<li><p>Statistical interpretation of Ridge and Lasso regression, see also slides from last week</p></li>
<li><p>Resampling techniques, Bootstrap and cross validation and bias-variance tradeoff (this may partly be discussed during the exercise sessions as well.</p></li>
<li><p>Readings and Videos:</p>
@@ -1618,7 +1623,7 @@ theorem.</p>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Bootstrap Statistics :
original bias std. error
100.057 14.8058 100.061 0.148629
100.106 15.0037 100.104 0.149019
</pre></div>
</div>
</div>
@@ -1843,7 +1848,9 @@ Error: 0.10398646080125035
Bias^2: 0.1007711427354898
Var: 0.0032153180657605116
0.10398646080125035 &gt;= 0.1007711427354898 + 0.0032153180657605116 = 0.10398646080125032
Polynomial degree: 3
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 3
Error: 0.06547790180152355
Bias^2: 0.06208238634231949
Var: 0.0033955154592040936
@@ -1875,7 +1882,9 @@ Error: 0.017355848195593347
Bias^2: 0.010331721306655127
Var: 0.007024126888938232
0.017355848195593347 &gt;= 0.010331721306655127 + 0.007024126888938232 = 0.01735584819559336
Polynomial degree: 9
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 9
Error: 0.02660572763718093
Bias^2: 0.010018312644137363
Var: 0.016587414993043573
@@ -1904,7 +1913,7 @@ Var: 0.20867052175034223
0.22842468702219465 &gt;= 0.01975416527185249 + 0.20867052175034223 = 0.2284246870221947
</pre></div>
</div>
<img alt="_images/week37_139_3.png" src="_images/week37_139_3.png" />
<img alt="_images/week37_139_5.png" src="_images/week37_139_5.png" />
</div>
</div>
</div>
@@ -2300,12 +2309,12 @@ Mean squared error on test data: 1.07641937
Degree of polynomial: 12
Mean squared error on training data: 0.00805074
Mean squared error on test data: 0.04295757
Degree of polynomial: 13
Mean squared error on training data: 0.00781918
Mean squared error on test data: 0.56965674
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 14
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 13
Mean squared error on training data: 0.00781918
Mean squared error on test data: 0.56965674
Degree of polynomial: 14
Mean squared error on training data: 0.00465099
Mean squared error on test data: 0.28443039
Degree of polynomial: 15
@@ -2363,9 +2372,9 @@ Mean squared error on training data: 0.00063866
Mean squared error on test data: 3099.60342978
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_88787/626635268.py:73: RuntimeWarning: divide by zero encountered in log10
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_92606/626635268.py:73: RuntimeWarning: divide by zero encountered in log10
plt.plot(polynomial, np.log10(trainingerror), label=&#39;Training Error&#39;)
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_88787/626635268.py:74: RuntimeWarning: divide by zero encountered in log10
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_92606/626635268.py:74: RuntimeWarning: divide by zero encountered in log10
plt.plot(polynomial, np.log10(testerror), label=&#39;Test Error&#39;)
</pre></div>
</div>
@@ -2450,7 +2459,7 @@ Mean squared error on test data: 3099.60342978
</div>
</div>
<div class="cell_output docutils container">
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_88787/3817475779.py:63: RuntimeWarning: divide by zero encountered in log10
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_92606/3817475779.py:63: RuntimeWarning: divide by zero encountered in log10
plt.plot(polynomial, np.log10(estimated_mse_sklearn), label=&#39;Test Error&#39;)
</pre></div>
</div>
@@ -2644,10 +2653,10 @@ This means the variance we obtain with the standard OLS will always for <span cl
<p class="prev-next-title">Exercises week 37</p>
</div>
</a>
<a class='right-next' id="next-link" href="project1.html" title="next page">
<a class='right-next' id="next-link" href="exercisesweek38.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 7 (midnight), 2024</p>
<p class="prev-next-title">Exercises week 38</p>
</div>
<i class="fas fa-angle-right"></i>
</a>
@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f9bcf943",
"metadata": {
"editable": true
},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html exercisesweek38.do.txt -->\n",
"<!-- dom:TITLE: Exercises week 38 -->"
]
},
{
"cell_type": "markdown",
"id": "ead6d6d6",
"metadata": {
"editable": true
},
"source": [
"# Exercises week 38\n",
"**September 16-20, 2024**\n",
"\n",
"Date: **Deadline is Friday September 20 at midnight**"
]
},
{
"cell_type": "markdown",
"id": "b6ce9344",
"metadata": {
"editable": true
},
"source": [
"## Overarching aims of the exercises this week\n",
"\n",
"The aim of the exercises this week is to derive the equations for the bias-variance tradeoff to be used in project 1 as well as testing this for a simpler function using the bootstrap method. The exercises here can be reused in project 1 as well.\n",
"\n",
"Consider a\n",
"dataset $\\mathcal{L}$ consisting of the data\n",
"$\\mathbf{X}_\\mathcal{L}=\\{(y_j, \\boldsymbol{x}_j), j=0\\ldots n-1\\}$.\n",
"\n",
"We assume that the true data is generated from a noisy model"
]
},
{
"cell_type": "markdown",
"id": "29371f21",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\boldsymbol{y}=f(\\boldsymbol{x}) + \\boldsymbol{\\epsilon}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "36a8765e",
"metadata": {
"editable": true
},
"source": [
"Here $\\epsilon$ is normally distributed with mean zero and standard\n",
"deviation $\\sigma^2$.\n",
"\n",
"In our derivation of the ordinary least squares method we defined \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",
"\n",
"The parameters $\\boldsymbol{\\beta}$ are in turn found by optimizing the mean\n",
"squared error via the so-called cost function"
]
},
{
"cell_type": "markdown",
"id": "68dd52df",
"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",
"$$"
]
},
{
"cell_type": "markdown",
"id": "68a07606",
"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",
"That is, show that"
]
},
{
"cell_type": "markdown",
"id": "fbbb3fd7",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right]=\\mathrm{Bias}[\\tilde{y}]+\\mathrm{var}[\\tilde{y}]+\\sigma^2,\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "5ecc0f22",
"metadata": {
"editable": true
},
"source": [
"with"
]
},
{
"cell_type": "markdown",
"id": "8c17bd0a",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathrm{Bias}[\\tilde{y}]=\\mathbb{E}\\left[\\left(\\boldsymbol{y}-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right]\\right)^2\\right],\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "55ab4f0c",
"metadata": {
"editable": true
},
"source": [
"and"
]
},
{
"cell_type": "markdown",
"id": "98f93c68",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\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.\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "d6d099d4",
"metadata": {
"editable": true
},
"source": [
"Explain what the terms mean and discuss their interpretations.\n",
"\n",
"Perform then a bias-variance analysis of a simple one-dimensional (or other models of your choice) function by\n",
"studying the MSE value as function of the complexity of your model. Use ordinary least squares only.\n",
"\n",
"Discuss the bias and variance trade-off as function\n",
"of your model complexity (the degree of the polynomial) and the number\n",
"of data points, and possibly also your training and test data using the **bootstrap** resampling method.\n",
"You can follow the code example in the jupyter-book at <https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff>."
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
File diff suppressed because one or more lines are too long
@@ -16,14 +16,15 @@
#
# **Material for the lecture on Monday September 9.**
#
# <!-- * [Video of Lecture](https://youtu.be/YOBBr_toYxc) -->
# <!-- * [Whiteboard notes](https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2023/NotesSep14.pdf) -->
# * [Video of Lecture](https://youtu.be/omLmp_kkie0)
#
# * [Whiteboard notes](https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2024/NotesSeptember9.pdf)
#
# * Statistical interpretation of Ridge and Lasso regression, see also slides from last week
#
# * Resampling techniques, Bootstrap and cross validation and bias-variance tradeoff (this may partly be discussed during the exercise sessions as well.
#
# * Readings and Videos:
#
# * Raschka et al, pages 175-192
#
# * Hastie et al Chapter 7, here we recommend 7.1-7.5 and 7.10 (cross-validation) and 7.11 (bootstrap). See <https://link.springer.com/book/10.1007/978-0-387-84858-7>.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

+1
View File
@@ -49,6 +49,7 @@ parts:
- file: week36.ipynb
- file: exercisesweek37.ipynb
- file: week37.ipynb
- file: exercisesweek38.ipynb
- caption: Projects
numbered: false
chapters:
+181
View File
@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f9bcf943",
"metadata": {
"editable": true
},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html exercisesweek38.do.txt -->\n",
"<!-- dom:TITLE: Exercises week 38 -->"
]
},
{
"cell_type": "markdown",
"id": "ead6d6d6",
"metadata": {
"editable": true
},
"source": [
"# Exercises week 38\n",
"**September 16-20, 2024**\n",
"\n",
"Date: **Deadline is Friday September 20 at midnight**"
]
},
{
"cell_type": "markdown",
"id": "b6ce9344",
"metadata": {
"editable": true
},
"source": [
"## Overarching aims of the exercises this week\n",
"\n",
"The aim of the exercises this week is to derive the equations for the bias-variance tradeoff to be used in project 1 as well as testing this for a simpler function using the bootstrap method. The exercises here can be reused in project 1 as well.\n",
"\n",
"Consider a\n",
"dataset $\\mathcal{L}$ consisting of the data\n",
"$\\mathbf{X}_\\mathcal{L}=\\{(y_j, \\boldsymbol{x}_j), j=0\\ldots n-1\\}$.\n",
"\n",
"We assume that the true data is generated from a noisy model"
]
},
{
"cell_type": "markdown",
"id": "29371f21",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\boldsymbol{y}=f(\\boldsymbol{x}) + \\boldsymbol{\\epsilon}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "36a8765e",
"metadata": {
"editable": true
},
"source": [
"Here $\\epsilon$ is normally distributed with mean zero and standard\n",
"deviation $\\sigma^2$.\n",
"\n",
"In our derivation of the ordinary least squares method we defined \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",
"\n",
"The parameters $\\boldsymbol{\\beta}$ are in turn found by optimizing the mean\n",
"squared error via the so-called cost function"
]
},
{
"cell_type": "markdown",
"id": "68dd52df",
"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",
"$$"
]
},
{
"cell_type": "markdown",
"id": "68a07606",
"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",
"That is, show that"
]
},
{
"cell_type": "markdown",
"id": "fbbb3fd7",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right]=\\mathrm{Bias}[\\tilde{y}]+\\mathrm{var}[\\tilde{y}]+\\sigma^2,\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "5ecc0f22",
"metadata": {
"editable": true
},
"source": [
"with"
]
},
{
"cell_type": "markdown",
"id": "8c17bd0a",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\mathrm{Bias}[\\tilde{y}]=\\mathbb{E}\\left[\\left(\\boldsymbol{y}-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right]\\right)^2\\right],\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "55ab4f0c",
"metadata": {
"editable": true
},
"source": [
"and"
]
},
{
"cell_type": "markdown",
"id": "98f93c68",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\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.\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "d6d099d4",
"metadata": {
"editable": true
},
"source": [
"Explain what the terms mean and discuss their interpretations.\n",
"\n",
"Perform then a bias-variance analysis of a simple one-dimensional (or other models of your choice) function by\n",
"studying the MSE value as function of the complexity of your model. Use ordinary least squares only.\n",
"\n",
"Discuss the bias and variance trade-off as function\n",
"of your model complexity (the degree of the polynomial) and the number\n",
"of data points, and possibly also your training and test data using the **bootstrap** resampling method.\n",
"You can follow the code example in the jupyter-book at <https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff>."
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
File diff suppressed because it is too large Load Diff
+2 -7
View File
@@ -1,6 +1,6 @@
TITLE: Exercises week 38
AUTHOR: September 18-22, 2023
DATE: Deadline is Sunday September 24 at midnight
AUTHOR: September 16-20, 2024
DATE: Deadline is Friday September 20 at midnight
===== Overarching aims of the exercises this week =====
@@ -59,7 +59,6 @@ and
!et
Explain what the terms mean and discuss their interpretations.
Perform then a bias-variance analysis of a simple one-dimensional (or other models of your choice) function by
@@ -71,7 +70,3 @@ of data points, and possibly also your training and test data using the _bootstr
You can follow the code example in the jupyter-book at URL:"https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff".
See also the whiteboard notes from week 37 at URL:"https://github.com/CompPhysics/MachineLearning/blob/master/doc/HandWrittenNotes/2023/NotesSep14.pdf"