2658 lines
193 KiB
HTML
2658 lines
193 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Week 37: Statistical interpretations and Resampling Methods — 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="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">
|
||
|
||
|
||
<!-- 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 current active">
|
||
<a class="current reference internal" href="#">
|
||
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">
|
||
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/week37.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="#plans-for-week-37-lecture-monday">
|
||
Plans for week 37, lecture Monday
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#plans-for-week-37-lab-sessions">
|
||
Plans for week 37, lab sessions
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#material-for-lecture-monday-september-9">
|
||
Material for lecture Monday September 9
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#deriving-ols-from-a-probability-distribution">
|
||
Deriving OLS from a probability distribution
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#independent-and-identically-distrubuted-iid">
|
||
Independent and Identically Distrubuted (iid)
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#maximum-likelihood-estimation-mle">
|
||
Maximum Likelihood Estimation (MLE)
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#a-new-cost-function">
|
||
A new Cost Function
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#more-basic-statistics-and-bayes-theorem">
|
||
More basic Statistics and Bayes’ theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#marginal-probability">
|
||
Marginal Probability
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#conditional-probability">
|
||
Conditional Probability
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#bayes-theorem">
|
||
Bayes’ Theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#interpretations-of-bayes-theorem">
|
||
Interpretations of Bayes’ Theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#example-of-usage-of-bayes-theorem">
|
||
Example of Usage of Bayes’ theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#doing-it-correctly">
|
||
Doing it correctly
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#bayes-theorem-and-ridge-and-lasso-regression">
|
||
Bayes’ Theorem and Ridge and Lasso Regression
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#ridge-and-bayes">
|
||
Ridge and Bayes
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#lasso-and-bayes">
|
||
Lasso and Bayes
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#why-resampling-methods">
|
||
Why resampling methods
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods">
|
||
Resampling methods
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-approaches-can-be-computationally-expensive">
|
||
Resampling approaches can be computationally expensive
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#id1">
|
||
Why resampling methods ?
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#statistical-analysis">
|
||
Statistical analysis
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#id2">
|
||
Resampling methods
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-bootstrap">
|
||
Resampling methods: Bootstrap
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#the-central-limit-theorem">
|
||
The Central Limit Theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#finding-the-limit">
|
||
Finding the Limit
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#rewriting-the-delta-function">
|
||
Rewriting the
|
||
<span class="math notranslate nohighlight">
|
||
\(\delta\)
|
||
</span>
|
||
-function
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#identifying-terms">
|
||
Identifying Terms
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#wrapping-it-up">
|
||
Wrapping it up
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#confidence-intervals">
|
||
Confidence Intervals
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#standard-approach-based-on-the-normal-distribution">
|
||
Standard Approach based on the Normal Distribution
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-bootstrap-background">
|
||
Resampling methods: Bootstrap background
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-more-bootstrap-background">
|
||
Resampling methods: More Bootstrap background
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-bootstrap-approach">
|
||
Resampling methods: Bootstrap approach
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-bootstrap-steps">
|
||
Resampling methods: Bootstrap steps
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#code-example-for-the-bootstrap-method">
|
||
Code example for the Bootstrap method
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#plotting-the-histogram">
|
||
Plotting the Histogram
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#the-bias-variance-tradeoff">
|
||
The bias-variance tradeoff
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#a-way-to-read-the-bias-variance-tradeoff">
|
||
A way to Read the Bias-Variance Tradeoff
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#example-code-for-bias-variance-tradeoff">
|
||
Example code for Bias-Variance tradeoff
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#understanding-what-happens">
|
||
Understanding what happens
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#summing-up">
|
||
Summing up
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#another-example-from-scikit-learn-s-repository">
|
||
Another Example from Scikit-Learn’s Repository
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#various-steps-in-cross-validation">
|
||
Various steps in cross-validation
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#cross-validation-in-brief">
|
||
Cross-validation in brief
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#code-example-for-cross-validation-and-k-fold-cross-validation">
|
||
Code Example for Cross-validation and
|
||
<span class="math notranslate nohighlight">
|
||
\(k\)
|
||
</span>
|
||
-fold Cross-validation
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#more-examples-on-bootstrap-and-cross-validation-and-errors">
|
||
More examples on bootstrap and cross-validation and errors
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#the-same-example-but-now-with-cross-validation">
|
||
The same example but now with cross-validation
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#material-for-the-lab-sessions">
|
||
Material for the lab sessions
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#linking-the-regression-analysis-with-a-statistical-interpretation">
|
||
Linking the regression analysis with a statistical interpretation
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#assumptions-made">
|
||
Assumptions made
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#expectation-value-and-variance">
|
||
Expectation value and variance
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#expectation-value-and-variance-for-boldsymbol-beta">
|
||
Expectation value and variance for
|
||
<span class="math notranslate nohighlight">
|
||
\(\boldsymbol{\beta}\)
|
||
</span>
|
||
</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>Week 37: Statistical interpretations and Resampling Methods</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="#plans-for-week-37-lecture-monday">
|
||
Plans for week 37, lecture Monday
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#plans-for-week-37-lab-sessions">
|
||
Plans for week 37, lab sessions
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#material-for-lecture-monday-september-9">
|
||
Material for lecture Monday September 9
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#deriving-ols-from-a-probability-distribution">
|
||
Deriving OLS from a probability distribution
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#independent-and-identically-distrubuted-iid">
|
||
Independent and Identically Distrubuted (iid)
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#maximum-likelihood-estimation-mle">
|
||
Maximum Likelihood Estimation (MLE)
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#a-new-cost-function">
|
||
A new Cost Function
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#more-basic-statistics-and-bayes-theorem">
|
||
More basic Statistics and Bayes’ theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#marginal-probability">
|
||
Marginal Probability
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#conditional-probability">
|
||
Conditional Probability
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#bayes-theorem">
|
||
Bayes’ Theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#interpretations-of-bayes-theorem">
|
||
Interpretations of Bayes’ Theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#example-of-usage-of-bayes-theorem">
|
||
Example of Usage of Bayes’ theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#doing-it-correctly">
|
||
Doing it correctly
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#bayes-theorem-and-ridge-and-lasso-regression">
|
||
Bayes’ Theorem and Ridge and Lasso Regression
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#ridge-and-bayes">
|
||
Ridge and Bayes
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#lasso-and-bayes">
|
||
Lasso and Bayes
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#why-resampling-methods">
|
||
Why resampling methods
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods">
|
||
Resampling methods
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-approaches-can-be-computationally-expensive">
|
||
Resampling approaches can be computationally expensive
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#id1">
|
||
Why resampling methods ?
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#statistical-analysis">
|
||
Statistical analysis
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#id2">
|
||
Resampling methods
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-bootstrap">
|
||
Resampling methods: Bootstrap
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#the-central-limit-theorem">
|
||
The Central Limit Theorem
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#finding-the-limit">
|
||
Finding the Limit
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#rewriting-the-delta-function">
|
||
Rewriting the
|
||
<span class="math notranslate nohighlight">
|
||
\(\delta\)
|
||
</span>
|
||
-function
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#identifying-terms">
|
||
Identifying Terms
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#wrapping-it-up">
|
||
Wrapping it up
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#confidence-intervals">
|
||
Confidence Intervals
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#standard-approach-based-on-the-normal-distribution">
|
||
Standard Approach based on the Normal Distribution
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-bootstrap-background">
|
||
Resampling methods: Bootstrap background
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-more-bootstrap-background">
|
||
Resampling methods: More Bootstrap background
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-bootstrap-approach">
|
||
Resampling methods: Bootstrap approach
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#resampling-methods-bootstrap-steps">
|
||
Resampling methods: Bootstrap steps
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#code-example-for-the-bootstrap-method">
|
||
Code example for the Bootstrap method
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#plotting-the-histogram">
|
||
Plotting the Histogram
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#the-bias-variance-tradeoff">
|
||
The bias-variance tradeoff
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#a-way-to-read-the-bias-variance-tradeoff">
|
||
A way to Read the Bias-Variance Tradeoff
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#example-code-for-bias-variance-tradeoff">
|
||
Example code for Bias-Variance tradeoff
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#understanding-what-happens">
|
||
Understanding what happens
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#summing-up">
|
||
Summing up
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#another-example-from-scikit-learn-s-repository">
|
||
Another Example from Scikit-Learn’s Repository
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#various-steps-in-cross-validation">
|
||
Various steps in cross-validation
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#cross-validation-in-brief">
|
||
Cross-validation in brief
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#code-example-for-cross-validation-and-k-fold-cross-validation">
|
||
Code Example for Cross-validation and
|
||
<span class="math notranslate nohighlight">
|
||
\(k\)
|
||
</span>
|
||
-fold Cross-validation
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#more-examples-on-bootstrap-and-cross-validation-and-errors">
|
||
More examples on bootstrap and cross-validation and errors
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#the-same-example-but-now-with-cross-validation">
|
||
The same example but now with cross-validation
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#material-for-the-lab-sessions">
|
||
Material for the lab sessions
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#linking-the-regression-analysis-with-a-statistical-interpretation">
|
||
Linking the regression analysis with a statistical interpretation
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#assumptions-made">
|
||
Assumptions made
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#expectation-value-and-variance">
|
||
Expectation value and variance
|
||
</a>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry">
|
||
<a class="reference internal nav-link" href="#expectation-value-and-variance-for-boldsymbol-beta">
|
||
Expectation value and variance for
|
||
<span class="math notranslate nohighlight">
|
||
\(\boldsymbol{\beta}\)
|
||
</span>
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div>
|
||
|
||
<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)
|
||
doconce format html week37.do.txt --no_mako -->
|
||
<!-- dom:TITLE: Week 37: Statistical interpretations and Resampling Methods --><div class="tex2jax_ignore mathjax_ignore section" id="week-37-statistical-interpretations-and-resampling-methods">
|
||
<h1>Week 37: Statistical interpretations and Resampling Methods<a class="headerlink" href="#week-37-statistical-interpretations-and-resampling-methods" title="Permalink to this headline">¶</a></h1>
|
||
<p><strong>Morten Hjorth-Jensen</strong>, Department of Physics, University of Oslo, Norway</p>
|
||
<p>Date: <strong>September 9, 2024</strong></p>
|
||
<!-- 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>
|
||
<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>
|
||
<ul>
|
||
<li><p>Raschka et al, pages 175-192</p></li>
|
||
<li><p>Hastie et al Chapter 7, here we recommend 7.1-7.5 and 7.10 (cross-validation) and 7.11 (bootstrap). See <a class="reference external" href="https://link.springer.com/book/10.1007/978-0-387-84858-7">https://link.springer.com/book/10.1007/978-0-387-84858-7</a>.</p></li>
|
||
<li><p><a class="reference external" href="https://www.youtube.com/watch?v=fSytzGwwBVw">Video on cross validation</a></p></li>
|
||
<li><p><a class="reference external" href="https://www.youtube.com/watch?v=Xz0x-8-cgaQ">Video on Bootstrapping</a></p></li>
|
||
<li><p><a class="reference external" href="https://www.youtube.com/watch?v=EuBBz3bI-aA">Video on bias-variance tradeoff</a></p></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="plans-for-week-37-lab-sessions">
|
||
<h2>Plans for week 37, lab sessions<a class="headerlink" href="#plans-for-week-37-lab-sessions" title="Permalink to this headline">¶</a></h2>
|
||
<p><strong>Material for the lab sessions on Tuesday and Wednesday.</strong></p>
|
||
<ul class="simple">
|
||
<li><p>Calculations of expectation values</p></li>
|
||
<li><p>Discussion of resampling techniques</p></li>
|
||
<li><p>Exercise set for week 37</p></li>
|
||
<li><p>Work on project 1</p></li>
|
||
<li><p><a class="reference external" href="https://youtu.be/bK4AEcTu-oM">Video of exercise sessions week 37</a></p></li>
|
||
<li><p>For more discussions of Ridge regression and calculation of averages, <a class="reference external" href="https://arxiv.org/abs/1509.09169">Wessel van Wieringen’s</a> article is highly recommended.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="material-for-lecture-monday-september-9">
|
||
<h2>Material for lecture Monday September 9<a class="headerlink" href="#material-for-lecture-monday-september-9" title="Permalink to this headline">¶</a></h2>
|
||
</div>
|
||
<div class="section" id="deriving-ols-from-a-probability-distribution">
|
||
<h2>Deriving OLS from a probability distribution<a class="headerlink" href="#deriving-ols-from-a-probability-distribution" title="Permalink to this headline">¶</a></h2>
|
||
<p>Our basic assumption when we derived the OLS equations was to assume
|
||
that our output is determined by a given continuous function
|
||
<span class="math notranslate nohighlight">\(f(\boldsymbol{x})\)</span> and a random noise <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span> given by the normal
|
||
distribution with zero mean value and an undetermined variance
|
||
<span class="math notranslate nohighlight">\(\sigma^2\)</span>.</p>
|
||
<p>We found above that the outputs <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span> have a mean value given by
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{X}\hat{\boldsymbol{\beta}}\)</span> and variance <span class="math notranslate nohighlight">\(\sigma^2\)</span>. Since the entries to
|
||
the design matrix are not stochastic variables, we can assume that the
|
||
probability distribution of our targets is also a normal distribution
|
||
but now with mean value <span class="math notranslate nohighlight">\(\boldsymbol{X}\hat{\boldsymbol{\beta}}\)</span>. This means that a
|
||
single output <span class="math notranslate nohighlight">\(y_i\)</span> is given by the Gaussian distribution</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
y_i\sim \mathcal{N}(\boldsymbol{X}_{i,*}\boldsymbol{\beta}, \sigma^2)=\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}.
|
||
\]</div>
|
||
</div>
|
||
<div class="section" id="independent-and-identically-distrubuted-iid">
|
||
<h2>Independent and Identically Distrubuted (iid)<a class="headerlink" href="#independent-and-identically-distrubuted-iid" title="Permalink to this headline">¶</a></h2>
|
||
<p>We assume now that the various <span class="math notranslate nohighlight">\(y_i\)</span> values are stochastically distributed according to the above Gaussian distribution.
|
||
We define this distribution as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(y_i, \boldsymbol{X}\vert\boldsymbol{\beta})=\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]},
|
||
\]</div>
|
||
<p>which reads as finding the likelihood of an event <span class="math notranslate nohighlight">\(y_i\)</span> with the input variables <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span> given the parameters (to be determined) <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span>.</p>
|
||
<p>Since these events are assumed to be independent and identicall distributed we can build the probability distribution function (PDF) for all possible event <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span> as the product of the single events, that is we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(\boldsymbol{y},\boldsymbol{X}\vert\boldsymbol{\beta})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}=\prod_{i=0}^{n-1}p(y_i,\boldsymbol{X}\vert\boldsymbol{\beta}).
|
||
\]</div>
|
||
<p>We will write this in a more compact form reserving <span class="math notranslate nohighlight">\(\boldsymbol{D}\)</span> for the domain of events, including the ouputs (targets) and the inputs. That is
|
||
in case we have a simple one-dimensional input and output case</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\dots, (x_{n-1},y_{n-1})].
|
||
\]</div>
|
||
<p>In the more general case the various inputs should be replaced by the possible features represented by the input data set <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span>.
|
||
We can now rewrite the above probability as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(\boldsymbol{D}\vert\boldsymbol{\beta})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}.
|
||
\]</div>
|
||
<p>It is a conditional probability (see below) and reads as the likelihood of a domain of events <span class="math notranslate nohighlight">\(\boldsymbol{D}\)</span> given a set of parameters <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="maximum-likelihood-estimation-mle">
|
||
<h2>Maximum Likelihood Estimation (MLE)<a class="headerlink" href="#maximum-likelihood-estimation-mle" title="Permalink to this headline">¶</a></h2>
|
||
<p>In statistics, maximum likelihood estimation (MLE) is a method of
|
||
estimating the parameters of an assumed probability distribution,
|
||
given some observed data. This is achieved by maximizing a likelihood
|
||
function so that, under the assumed statistical model, the observed
|
||
data is the most probable.</p>
|
||
<p>We will assume here that our events are given by the above Gaussian
|
||
distribution and we will determine the optimal parameters <span class="math notranslate nohighlight">\(\beta\)</span> by
|
||
maximizing the above PDF. However, computing the derivatives of a
|
||
product function is cumbersome and can easily lead to overflow and/or
|
||
underflowproblems, with potentials for loss of numerical precision.</p>
|
||
<p>In practice, it is more convenient to maximize the logarithm of the
|
||
PDF because it is a monotonically increasing function of the argument.
|
||
Alternatively, and this will be our option, we will minimize the
|
||
negative of the logarithm since this is a monotonically decreasing
|
||
function.</p>
|
||
<p>Note also that maximization/minimization of the logarithm of the PDF
|
||
is equivalent to the maximization/minimization of the function itself.</p>
|
||
</div>
|
||
<div class="section" id="a-new-cost-function">
|
||
<h2>A new Cost Function<a class="headerlink" href="#a-new-cost-function" title="Permalink to this headline">¶</a></h2>
|
||
<p>We could now define a new cost function to minimize, namely the negative logarithm of the above PDF</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{\beta}=-\log{\prod_{i=0}^{n-1}p(y_i,\boldsymbol{X}\vert\boldsymbol{\beta})}=-\sum_{i=0}^{n-1}\log{p(y_i,\boldsymbol{X}\vert\boldsymbol{\beta})},
|
||
\]</div>
|
||
<p>which becomes</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{\beta}=\frac{n}{2}\log{2\pi\sigma^2}+\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}.
|
||
\]</div>
|
||
<p>Taking the derivative of the <em>new</em> cost function with respect to the parameters <span class="math notranslate nohighlight">\(\beta\)</span> we recognize our familiar OLS equation, namely</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{X}^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right) =0,
|
||
\]</div>
|
||
<p>which leads to the well-known OLS equation for the optimal paramters <span class="math notranslate nohighlight">\(\beta\)</span></p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}=\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}!
|
||
\]</div>
|
||
<p>Before we make a similar analysis for Ridge and Lasso regression, we need a short reminder on statistics.</p>
|
||
</div>
|
||
<div class="section" id="more-basic-statistics-and-bayes-theorem">
|
||
<h2>More basic Statistics and Bayes’ theorem<a class="headerlink" href="#more-basic-statistics-and-bayes-theorem" title="Permalink to this headline">¶</a></h2>
|
||
<p>A central theorem in statistics is Bayes’ theorem. This theorem plays a similar role as the good old Pythagoras’ theorem in geometry.
|
||
Bayes’ theorem is extremely simple to derive. But to do so we need some basic axioms from statistics.</p>
|
||
<p>Assume we have two domains of events <span class="math notranslate nohighlight">\(X=[x_0,x_1,\dots,x_{n-1}]\)</span> and <span class="math notranslate nohighlight">\(Y=[y_0,y_1,\dots,y_{n-1}]\)</span>.</p>
|
||
<p>We define also the likelihood for <span class="math notranslate nohighlight">\(X\)</span> and <span class="math notranslate nohighlight">\(Y\)</span> as <span class="math notranslate nohighlight">\(p(X)\)</span> and <span class="math notranslate nohighlight">\(p(Y)\)</span> respectively.
|
||
The likelihood of a specific event <span class="math notranslate nohighlight">\(x_i\)</span> (or <span class="math notranslate nohighlight">\(y_i\)</span>) is then written as <span class="math notranslate nohighlight">\(p(X=x_i)\)</span> or just <span class="math notranslate nohighlight">\(p(x_i)=p_i\)</span>.</p>
|
||
<p><strong>Union of events is given by.</strong></p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(X \cup Y)= p(X)+p(Y)-p(X \cap Y).
|
||
\]</div>
|
||
<p><strong>The product rule (aka joint probability) is given by.</strong></p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(X \cup Y)= p(X,Y)= p(X\vert Y)p(Y)=p(Y\vert X)p(X),
|
||
\]</div>
|
||
<p>where we read <span class="math notranslate nohighlight">\(p(X\vert Y)\)</span> as the likelihood of obtaining <span class="math notranslate nohighlight">\(X\)</span> given <span class="math notranslate nohighlight">\(Y\)</span>.</p>
|
||
<p>If we have independent events then <span class="math notranslate nohighlight">\(p(X,Y)=p(X)p(Y)\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="marginal-probability">
|
||
<h2>Marginal Probability<a class="headerlink" href="#marginal-probability" title="Permalink to this headline">¶</a></h2>
|
||
<p>The marginal probability is defined in terms of only one of the set of variables <span class="math notranslate nohighlight">\(X,Y\)</span>. For a discrete probability we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(X)=\sum_{i=0}^{n-1}p(X,Y=y_i)=\sum_{i=0}^{n-1}p(X\vert Y=y_i)p(Y=y_i)=\sum_{i=0}^{n-1}p(X\vert y_i)p(y_i).
|
||
\]</div>
|
||
</div>
|
||
<div class="section" id="conditional-probability">
|
||
<h2>Conditional Probability<a class="headerlink" href="#conditional-probability" title="Permalink to this headline">¶</a></h2>
|
||
<p>The conditional probability, if <span class="math notranslate nohighlight">\(p(Y) > 0\)</span>, is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(X\vert Y)= \frac{p(X,Y)}{p(Y)}=\frac{p(X,Y)}{\sum_{i=0}^{n-1}p(Y\vert X=x_i)p(x_i)}.
|
||
\]</div>
|
||
</div>
|
||
<div class="section" id="bayes-theorem">
|
||
<h2>Bayes’ Theorem<a class="headerlink" href="#bayes-theorem" title="Permalink to this headline">¶</a></h2>
|
||
<p>If we combine the conditional probability with the marginal probability and the standard product rule, we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(X\vert Y)= \frac{p(X,Y)}{p(Y)},
|
||
\]</div>
|
||
<p>which we can rewrite as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(X\vert Y)= \frac{p(X,Y)}{\sum_{i=0}^{n-1}p(Y\vert X=x_i)p(x_i)}=\frac{p(Y\vert X)p(X)}{\sum_{i=0}^{n-1}p(Y\vert X=x_i)p(x_i)},
|
||
\]</div>
|
||
<p>which is Bayes’ theorem. It allows us to evaluate the uncertainty in in <span class="math notranslate nohighlight">\(X\)</span> after we have observed <span class="math notranslate nohighlight">\(Y\)</span>. We can easily interchange <span class="math notranslate nohighlight">\(X\)</span> with <span class="math notranslate nohighlight">\(Y\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="interpretations-of-bayes-theorem">
|
||
<h2>Interpretations of Bayes’ Theorem<a class="headerlink" href="#interpretations-of-bayes-theorem" title="Permalink to this headline">¶</a></h2>
|
||
<p>The quantity <span class="math notranslate nohighlight">\(p(Y\vert X)\)</span> on the right-hand side of the theorem is
|
||
evaluated for the observed data <span class="math notranslate nohighlight">\(Y\)</span> and can be viewed as a function of
|
||
the parameter space represented by <span class="math notranslate nohighlight">\(X\)</span>. This function is not
|
||
necesseraly normalized and is normally called the likelihood function.</p>
|
||
<p>The function <span class="math notranslate nohighlight">\(p(X)\)</span> on the right hand side is called the prior while the function on the left hand side is the called the posterior probability. The denominator on the right hand side serves as a normalization factor for the posterior distribution.</p>
|
||
<p>Let us try to illustrate Bayes’ theorem through an example.</p>
|
||
</div>
|
||
<div class="section" id="example-of-usage-of-bayes-theorem">
|
||
<h2>Example of Usage of Bayes’ theorem<a class="headerlink" href="#example-of-usage-of-bayes-theorem" title="Permalink to this headline">¶</a></h2>
|
||
<p>Let us suppose that you are undergoing a series of mammography scans in
|
||
order to rule out possible breast cancer cases. We define the
|
||
sensitivity for a positive event by the variable <span class="math notranslate nohighlight">\(X\)</span>. It takes binary
|
||
values with <span class="math notranslate nohighlight">\(X=1\)</span> representing a positive event and <span class="math notranslate nohighlight">\(X=0\)</span> being a
|
||
negative event. We reserve <span class="math notranslate nohighlight">\(Y\)</span> as a classification parameter for
|
||
either a negative or a positive breast cancer confirmation. (Short note on wordings: positive here means having breast cancer, although none of us would consider this being a positive thing).</p>
|
||
<p>We let <span class="math notranslate nohighlight">\(Y=1\)</span> represent the the case of having breast cancer and <span class="math notranslate nohighlight">\(Y=0\)</span> as not.</p>
|
||
<p>Let us assume that if you have breast cancer, the test will be positive with a probability of <span class="math notranslate nohighlight">\(0.8\)</span>, that is we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(X=1\vert Y=1) =0.8.
|
||
\]</div>
|
||
<p>This obviously sounds scary since many would conclude that if the test is positive, there is a likelihood of <span class="math notranslate nohighlight">\(80\%\)</span> for having cancer.
|
||
It is however not correct, as the following Bayesian analysis shows.</p>
|
||
</div>
|
||
<div class="section" id="doing-it-correctly">
|
||
<h2>Doing it correctly<a class="headerlink" href="#doing-it-correctly" title="Permalink to this headline">¶</a></h2>
|
||
<p>If we look at various national surveys on breast cancer, the general likelihood of developing breast cancer is a very small number.
|
||
Let us assume that the prior probability in the population as a whole is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(Y=1) =0.004.
|
||
\]</div>
|
||
<p>We need also to account for the fact that the test may produce a false positive result (false alarm). Let us here assume that we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(X=1\vert Y=0) =0.1.
|
||
\]</div>
|
||
<p>Using Bayes’ theorem we can then find the posterior probability that the person has breast cancer in case of a positive test, that is we can compute</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(Y=1\vert X=1)=\frac{p(X=1\vert Y=1)p(Y=1)}{p(X=1\vert Y=1)p(Y=1)+p(X=1\vert Y=0)p(Y=0)}=\frac{0.8\times 0.004}{0.8\times 0.004+0.1\times 0.996}=0.031.
|
||
\]</div>
|
||
<p>That is, in case of a positive test, there is only a <span class="math notranslate nohighlight">\(3\%\)</span> chance of having breast cancer!</p>
|
||
</div>
|
||
<div class="section" id="bayes-theorem-and-ridge-and-lasso-regression">
|
||
<h2>Bayes’ Theorem and Ridge and Lasso Regression<a class="headerlink" href="#bayes-theorem-and-ridge-and-lasso-regression" title="Permalink to this headline">¶</a></h2>
|
||
<p>Using Bayes’ theorem we can gain a better intuition about Ridge and Lasso regression.</p>
|
||
<p>For ordinary least squares we postulated that the maximum likelihood for the doamin of events <span class="math notranslate nohighlight">\(\boldsymbol{D}\)</span> (one-dimensional case)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\dots, (x_{n-1},y_{n-1})],
|
||
\]</div>
|
||
<p>is given by</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(\boldsymbol{D}\vert\boldsymbol{\beta})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}.
|
||
\]</div>
|
||
<p>In Bayes’ theorem this function plays the role of the so-called likelihood. We could now ask the question what is the posterior probability of a parameter set <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> given a domain of events <span class="math notranslate nohighlight">\(\boldsymbol{D}\)</span>? That is, how can we define the posterior probability</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(\boldsymbol{\beta}\vert\boldsymbol{D}).
|
||
\]</div>
|
||
<p>Bayes’ theorem comes to our rescue here since (omitting the normalization constant)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(\boldsymbol{\beta}\vert\boldsymbol{D})\propto p(\boldsymbol{D}\vert\boldsymbol{\beta})p(\boldsymbol{\beta}).
|
||
\]</div>
|
||
<p>We have a model for <span class="math notranslate nohighlight">\(p(\boldsymbol{D}\vert\boldsymbol{\beta})\)</span> but need one for the <strong>prior</strong> <span class="math notranslate nohighlight">\(p(\boldsymbol{\beta})\)</span>!</p>
|
||
</div>
|
||
<div class="section" id="ridge-and-bayes">
|
||
<h2>Ridge and Bayes<a class="headerlink" href="#ridge-and-bayes" title="Permalink to this headline">¶</a></h2>
|
||
<p>With the posterior probability defined by a likelihood which we have
|
||
already modeled and an unknown prior, we are now ready to make
|
||
additional models for the prior.</p>
|
||
<p>We can, based on our discussions of the variance of <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> and the mean value, assume that the prior for the values <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> is given by a Gaussian with mean value zero and variance <span class="math notranslate nohighlight">\(\tau^2\)</span>, that is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(\boldsymbol{\beta})=\prod_{j=0}^{p-1}\exp{\left(-\frac{\beta_j^2}{2\tau^2}\right)}.
|
||
\]</div>
|
||
<p>Our posterior probability becomes then (omitting the normalization factor which is just a constant)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(\boldsymbol{\beta\vert\boldsymbol{D})}=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}\prod_{j=0}^{p-1}\exp{\left(-\frac{\beta_j^2}{2\tau^2}\right)}.
|
||
\]</div>
|
||
<p>We can now optimize this quantity with respect to <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span>. As we
|
||
did for OLS, this is most conveniently done by taking the negative
|
||
logarithm of the posterior probability. Doing so and leaving out the
|
||
constants terms that do not depend on <span class="math notranslate nohighlight">\(\beta\)</span>, we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{\beta})=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\frac{1}{2\tau^2}\vert\vert\boldsymbol{\beta}\vert\vert_2^2,
|
||
\]</div>
|
||
<p>and replacing <span class="math notranslate nohighlight">\(1/2\tau^2\)</span> with <span class="math notranslate nohighlight">\(\lambda\)</span> we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{\beta})=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\lambda\vert\vert\boldsymbol{\beta}\vert\vert_2^2,
|
||
\]</div>
|
||
<p>which is our Ridge cost function! Nice, isn’t it?</p>
|
||
</div>
|
||
<div class="section" id="lasso-and-bayes">
|
||
<h2>Lasso and Bayes<a class="headerlink" href="#lasso-and-bayes" title="Permalink to this headline">¶</a></h2>
|
||
<p>To derive the Lasso cost function, we simply replace the Gaussian prior with an exponential distribution (<a class="reference external" href="https://en.wikipedia.org/wiki/Laplace_distribution">Laplace in this case</a>) with zero mean value, that is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(\boldsymbol{\beta})=\prod_{j=0}^{p-1}\exp{\left(-\frac{\vert\beta_j\vert}{\tau}\right)}.
|
||
\]</div>
|
||
<p>Our posterior probability becomes then (omitting the normalization factor which is just a constant)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
p(\boldsymbol{\beta}\vert\boldsymbol{D})=\prod_{i=0}^{n-1}\frac{1}{\sqrt{2\pi\sigma^2}}\exp{\left[-\frac{(y_i-\boldsymbol{X}_{i,*}\boldsymbol{\beta})^2}{2\sigma^2}\right]}\prod_{j=0}^{p-1}\exp{\left(-\frac{\vert\beta_j\vert}{\tau}\right)}.
|
||
\]</div>
|
||
<p>Taking the negative
|
||
logarithm of the posterior probability and leaving out the
|
||
constants terms that do not depend on <span class="math notranslate nohighlight">\(\beta\)</span>, we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{\beta})=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\frac{1}{\tau}\vert\vert\boldsymbol{\beta}\vert\vert_1,
|
||
\]</div>
|
||
<p>and replacing <span class="math notranslate nohighlight">\(1/\tau\)</span> with <span class="math notranslate nohighlight">\(\lambda\)</span> we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{\beta})=\frac{\vert\vert (\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\vert\vert_2^2}{2\sigma^2}+\lambda\vert\vert\boldsymbol{\beta}\vert\vert_1,
|
||
\]</div>
|
||
<p>which is our Lasso cost function!</p>
|
||
</div>
|
||
<div class="section" id="why-resampling-methods">
|
||
<h2>Why resampling methods<a class="headerlink" href="#why-resampling-methods" title="Permalink to this headline">¶</a></h2>
|
||
<p>Before we proceed, we need to rethink what we have been doing. In our
|
||
eager to fit the data, we have omitted several important elements in
|
||
our regression analysis. In what follows we will</p>
|
||
<ol class="simple">
|
||
<li><p>look at statistical properties, including a discussion of mean values, variance and the so-called bias-variance tradeoff</p></li>
|
||
<li><p>introduce resampling techniques like cross-validation, bootstrapping and jackknife and more</p></li>
|
||
</ol>
|
||
<p>and discuss how to select a given model (one of the difficult parts in machine learning).</p>
|
||
</div>
|
||
<div class="section" id="resampling-methods">
|
||
<h2>Resampling methods<a class="headerlink" href="#resampling-methods" title="Permalink to this headline">¶</a></h2>
|
||
<p>Resampling methods are an indispensable tool in modern
|
||
statistics. They involve repeatedly drawing samples from a training
|
||
set and refitting a model of interest on each sample in order to
|
||
obtain additional information about the fitted model. For example, in
|
||
order to estimate the variability of a linear regression fit, we can
|
||
repeatedly draw different samples from the training data, fit a linear
|
||
regression to each new sample, and then examine the extent to which
|
||
the resulting fits differ. Such an approach may allow us to obtain
|
||
information that would not be available from fitting the model only
|
||
once using the original training sample.</p>
|
||
<p>Two resampling methods are often used in Machine Learning analyses,</p>
|
||
<ol class="simple">
|
||
<li><p>The <strong>bootstrap method</strong></p></li>
|
||
<li><p>and <strong>Cross-Validation</strong></p></li>
|
||
</ol>
|
||
<p>In addition there are several other methods such as the Jackknife and the Blocking methods. We will discuss in particular
|
||
cross-validation and the bootstrap method.</p>
|
||
</div>
|
||
<div class="section" id="resampling-approaches-can-be-computationally-expensive">
|
||
<h2>Resampling approaches can be computationally expensive<a class="headerlink" href="#resampling-approaches-can-be-computationally-expensive" title="Permalink to this headline">¶</a></h2>
|
||
<p>Resampling approaches can be computationally expensive, because they
|
||
involve fitting the same statistical method multiple times using
|
||
different subsets of the training data. However, due to recent
|
||
advances in computing power, the computational requirements of
|
||
resampling methods generally are not prohibitive. In this chapter, we
|
||
discuss two of the most commonly used resampling methods,
|
||
cross-validation and the bootstrap. Both methods are important tools
|
||
in the practical application of many statistical learning
|
||
procedures. For example, cross-validation can be used to estimate the
|
||
test error associated with a given statistical learning method in
|
||
order to evaluate its performance, or to select the appropriate level
|
||
of flexibility. The process of evaluating a model’s performance is
|
||
known as model assessment, whereas the process of selecting the proper
|
||
level of flexibility for a model is known as model selection. The
|
||
bootstrap is widely used.</p>
|
||
</div>
|
||
<div class="section" id="id1">
|
||
<h2>Why resampling methods ?<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
|
||
<p><strong>Statistical analysis.</strong></p>
|
||
<ul class="simple">
|
||
<li><p>Our simulations can be treated as <em>computer experiments</em>. This is particularly the case for Monte Carlo methods which are widely used in statistical analyses.</p></li>
|
||
<li><p>The results can be analysed with the same statistical tools as we would use when analysing experimental data.</p></li>
|
||
<li><p>As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="statistical-analysis">
|
||
<h2>Statistical analysis<a class="headerlink" href="#statistical-analysis" title="Permalink to this headline">¶</a></h2>
|
||
<ul class="simple">
|
||
<li><p>As in other experiments, many numerical experiments have two classes of errors:</p>
|
||
<ul>
|
||
<li><p>Statistical errors</p></li>
|
||
<li><p>Systematical errors</p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p>Statistical errors can be estimated using standard tools from statistics</p></li>
|
||
<li><p>Systematical errors are method specific and must be treated differently from case to case.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="id2">
|
||
<h2>Resampling methods<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2>
|
||
<p>With all these analytical equations for both the OLS and Ridge
|
||
regression, we will now outline how to assess a given model. This will
|
||
lead to a discussion of the so-called bias-variance tradeoff (see
|
||
below) and so-called resampling methods.</p>
|
||
<p>One of the quantities we have discussed as a way to measure errors is
|
||
the mean-squared error (MSE), mainly used for fitting of continuous
|
||
functions. Another choice is the absolute error.</p>
|
||
<p>In the discussions below we will focus on the MSE and in particular since we will split the data into test and training data,
|
||
we discuss the</p>
|
||
<ol class="simple">
|
||
<li><p>prediction error or simply the <strong>test error</strong> <span class="math notranslate nohighlight">\(\mathrm{Err_{Test}}\)</span>, where we have a fixed training set and the test error is the MSE arising from the data reserved for testing. We discuss also the</p></li>
|
||
<li><p>training error <span class="math notranslate nohighlight">\(\mathrm{Err_{Train}}\)</span>, which is the average loss over the training data.</p></li>
|
||
</ol>
|
||
<p>As our model becomes more and more complex, more of the training data tends to used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error.
|
||
For a certain level of complexity the test error will reach minimum, before starting to increase again. The
|
||
training error reaches a saturation.</p>
|
||
</div>
|
||
<div class="section" id="resampling-methods-bootstrap">
|
||
<h2>Resampling methods: Bootstrap<a class="headerlink" href="#resampling-methods-bootstrap" title="Permalink to this headline">¶</a></h2>
|
||
<p>Bootstrapping is a <a class="reference external" href="https://en.wikipedia.org/wiki/Nonparametric_statistics">non-parametric approach</a> to statistical inference
|
||
that substitutes computation for more traditional distributional
|
||
assumptions and asymptotic results. Bootstrapping offers a number of
|
||
advantages:</p>
|
||
<ol class="simple">
|
||
<li><p>The bootstrap is quite general, although there are some cases in which it fails.</p></li>
|
||
<li><p>Because it does not require distributional assumptions (such as normally distributed errors), the bootstrap can provide more accurate inferences when the data are not well behaved or when the sample size is small.</p></li>
|
||
<li><p>It is possible to apply the bootstrap to statistics with sampling distributions that are difficult to derive, even asymptotically.</p></li>
|
||
<li><p>It is relatively simple to apply the bootstrap to complex data-collection plans (such as stratified and clustered samples).</p></li>
|
||
</ol>
|
||
<p>The textbook by <a class="reference external" href="https://www.cambridge.org/core/books/bootstrap-methods-and-their-application/ED2FD043579F27952363566DC09CBD6A">Davison on the Bootstrap Methods and their Applications</a> provides many more insights and proofs. In this course we will take a more practical approach and use the results and theorems provided in the literature. For those interested in reading more about the bootstrap methods, we recommend the above text and the one by <a class="reference external" href="https://www.routledge.com/An-Introduction-to-the-Bootstrap/Efron-Tibshirani/p/book/9780412042317">Efron and Tibshirani</a>.</p>
|
||
<p>Before we proceed however, we need to remind ourselves about a central theorem in statistics, namely the so-called <strong>central limit theorem</strong>.</p>
|
||
</div>
|
||
<div class="section" id="the-central-limit-theorem">
|
||
<h2>The Central Limit Theorem<a class="headerlink" href="#the-central-limit-theorem" title="Permalink to this headline">¶</a></h2>
|
||
<p>Suppose we have a PDF <span class="math notranslate nohighlight">\(p(x)\)</span> from which we generate a series <span class="math notranslate nohighlight">\(N\)</span>
|
||
of averages <span class="math notranslate nohighlight">\(\mathbb{E}[x_i]\)</span>. Each mean value <span class="math notranslate nohighlight">\(\mathbb{E}[x_i]\)</span>
|
||
is viewed as the average of a specific measurement, e.g., throwing
|
||
dice 100 times and then taking the average value, or producing a certain
|
||
amount of random numbers.
|
||
For notational ease, we set <span class="math notranslate nohighlight">\(\mathbb{E}[x_i]=x_i\)</span> in the discussion
|
||
which follows. We do the same for <span class="math notranslate nohighlight">\(\mathbb{E}[z]=z\)</span>.</p>
|
||
<p>If we compute the mean <span class="math notranslate nohighlight">\(z\)</span> of <span class="math notranslate nohighlight">\(m\)</span> such mean values <span class="math notranslate nohighlight">\(x_i\)</span></p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
z=\frac{x_1+x_2+\dots+x_m}{m},
|
||
\]</div>
|
||
<p>the question we pose is which is the PDF of the new variable <span class="math notranslate nohighlight">\(z\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="finding-the-limit">
|
||
<h2>Finding the Limit<a class="headerlink" href="#finding-the-limit" title="Permalink to this headline">¶</a></h2>
|
||
<p>The probability of obtaining an average value <span class="math notranslate nohighlight">\(z\)</span> is the product of the
|
||
probabilities of obtaining arbitrary individual mean values <span class="math notranslate nohighlight">\(x_i\)</span>,
|
||
but with the constraint that the average is <span class="math notranslate nohighlight">\(z\)</span>. We can express this through
|
||
the following expression</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\tilde{p}(z)=\int dx_1p(x_1)\int dx_2p(x_2)\dots\int dx_mp(x_m)
|
||
\delta(z-\frac{x_1+x_2+\dots+x_m}{m}),
|
||
\]</div>
|
||
<p>where the <span class="math notranslate nohighlight">\(\delta\)</span>-function enbodies the constraint that the mean is <span class="math notranslate nohighlight">\(z\)</span>.
|
||
All measurements that lead to each individual <span class="math notranslate nohighlight">\(x_i\)</span> are expected to
|
||
be independent, which in turn means that we can express <span class="math notranslate nohighlight">\(\tilde{p}\)</span> as the
|
||
product of individual <span class="math notranslate nohighlight">\(p(x_i)\)</span>. The independence assumption is important in the derivation of the central limit theorem.</p>
|
||
</div>
|
||
<div class="section" id="rewriting-the-delta-function">
|
||
<h2>Rewriting the <span class="math notranslate nohighlight">\(\delta\)</span>-function<a class="headerlink" href="#rewriting-the-delta-function" title="Permalink to this headline">¶</a></h2>
|
||
<p>If we use the integral expression for the <span class="math notranslate nohighlight">\(\delta\)</span>-function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\delta(z-\frac{x_1+x_2+\dots+x_m}{m})=\frac{1}{2\pi}\int_{-\infty}^{\infty}
|
||
dq\exp{\left(iq(z-\frac{x_1+x_2+\dots+x_m}{m})\right)},
|
||
\]</div>
|
||
<p>and inserting <span class="math notranslate nohighlight">\(e^{i\mu q-i\mu q}\)</span> where <span class="math notranslate nohighlight">\(\mu\)</span> is the mean value
|
||
we arrive at</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\tilde{p}(z)=\frac{1}{2\pi}\int_{-\infty}^{\infty}
|
||
dq\exp{\left(iq(z-\mu)\right)}\left[\int_{-\infty}^{\infty}
|
||
dxp(x)\exp{\left(iq(\mu-x)/m\right)}\right]^m,
|
||
\]</div>
|
||
<p>with the integral over <span class="math notranslate nohighlight">\(x\)</span> resulting in</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\int_{-\infty}^{\infty}dxp(x)\exp{\left(iq(\mu-x)/m\right)}=
|
||
\int_{-\infty}^{\infty}dxp(x)
|
||
\left[1+\frac{iq(\mu-x)}{m}-\frac{q^2(\mu-x)^2}{2m^2}+\dots\right].
|
||
\]</div>
|
||
</div>
|
||
<div class="section" id="identifying-terms">
|
||
<h2>Identifying Terms<a class="headerlink" href="#identifying-terms" title="Permalink to this headline">¶</a></h2>
|
||
<p>The second term on the rhs disappears since this is just the mean and
|
||
employing the definition of <span class="math notranslate nohighlight">\(\sigma^2\)</span> we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\int_{-\infty}^{\infty}dxp(x)e^{\left(iq(\mu-x)/m\right)}=
|
||
1-\frac{q^2\sigma^2}{2m^2}+\dots,
|
||
\]</div>
|
||
<p>resulting in</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\left[\int_{-\infty}^{\infty}dxp(x)\exp{\left(iq(\mu-x)/m\right)}\right]^m\approx
|
||
\left[1-\frac{q^2\sigma^2}{2m^2}+\dots \right]^m,
|
||
\]</div>
|
||
<p>and in the limit <span class="math notranslate nohighlight">\(m\rightarrow \infty\)</span> we obtain</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\tilde{p}(z)=\frac{1}{\sqrt{2\pi}(\sigma/\sqrt{m})}
|
||
\exp{\left(-\frac{(z-\mu)^2}{2(\sigma/\sqrt{m})^2}\right)},
|
||
\]</div>
|
||
<p>which is the normal distribution with variance
|
||
<span class="math notranslate nohighlight">\(\sigma^2_m=\sigma^2/m\)</span>, where <span class="math notranslate nohighlight">\(\sigma\)</span> is the variance of the PDF <span class="math notranslate nohighlight">\(p(x)\)</span>
|
||
and <span class="math notranslate nohighlight">\(\mu\)</span> is also the mean of the PDF <span class="math notranslate nohighlight">\(p(x)\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="wrapping-it-up">
|
||
<h2>Wrapping it up<a class="headerlink" href="#wrapping-it-up" title="Permalink to this headline">¶</a></h2>
|
||
<p>Thus, the central limit theorem states that the PDF <span class="math notranslate nohighlight">\(\tilde{p}(z)\)</span> of
|
||
the average of <span class="math notranslate nohighlight">\(m\)</span> random values corresponding to a PDF <span class="math notranslate nohighlight">\(p(x)\)</span>
|
||
is a normal distribution whose mean is the
|
||
mean value of the PDF <span class="math notranslate nohighlight">\(p(x)\)</span> and whose variance is the variance
|
||
of the PDF <span class="math notranslate nohighlight">\(p(x)\)</span> divided by <span class="math notranslate nohighlight">\(m\)</span>, the number of values used to compute <span class="math notranslate nohighlight">\(z\)</span>.</p>
|
||
<p>The central limit theorem leads to the well-known expression for the
|
||
standard deviation, given by</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\sigma_m=
|
||
\frac{\sigma}{\sqrt{m}}.
|
||
\]</div>
|
||
<p>The latter is true only if the average value is known exactly. This is obtained in the limit
|
||
<span class="math notranslate nohighlight">\(m\rightarrow \infty\)</span> only. Because the mean and the variance are measured quantities we obtain
|
||
the familiar expression in statistics (the so-called Bessel correction)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\sigma_m\approx
|
||
\frac{\sigma}{\sqrt{m-1}}.
|
||
\]</div>
|
||
<p>In many cases however the above estimate for the standard deviation,
|
||
in particular if correlations are strong, may be too simplistic. Keep
|
||
in mind that we have assumed that the variables <span class="math notranslate nohighlight">\(x\)</span> are independent
|
||
and identically distributed. This is obviously not always the
|
||
case. For example, the random numbers (or better pseudorandom numbers)
|
||
we generate in various calculations do always exhibit some
|
||
correlations.</p>
|
||
<p>The theorem is satisfied by a large class of PDFs. Note however that for a
|
||
finite <span class="math notranslate nohighlight">\(m\)</span>, it is not always possible to find a closed form /analytic expression for
|
||
<span class="math notranslate nohighlight">\(\tilde{p}(x)\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="confidence-intervals">
|
||
<h2>Confidence Intervals<a class="headerlink" href="#confidence-intervals" title="Permalink to this headline">¶</a></h2>
|
||
<p>Confidence intervals are used in statistics and represent a type of estimate
|
||
computed from the observed data. This gives a range of values for an
|
||
unknown parameter such as the parameters <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> from linear regression.</p>
|
||
<p>With the OLS expressions for the parameters <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> we found
|
||
<span class="math notranslate nohighlight">\(\mathbb{E}(\boldsymbol{\beta}) = \boldsymbol{\beta}\)</span>, which means that the estimator of the regression parameters is unbiased.</p>
|
||
<p>In the exercises this week we show that the variance of the estimate of the <span class="math notranslate nohighlight">\(j\)</span>-th regression coefficient is
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{\sigma}^2 (\boldsymbol{\beta}_j ) = \boldsymbol{\sigma}^2 [(\mathbf{X}^{T} \mathbf{X})^{-1}]_{jj} \)</span>.</p>
|
||
<p>This quantity can be used to
|
||
construct a confidence interval for the estimates.</p>
|
||
</div>
|
||
<div class="section" id="standard-approach-based-on-the-normal-distribution">
|
||
<h2>Standard Approach based on the Normal Distribution<a class="headerlink" href="#standard-approach-based-on-the-normal-distribution" title="Permalink to this headline">¶</a></h2>
|
||
<p>We will assume that the parameters <span class="math notranslate nohighlight">\(\beta\)</span> follow a normal
|
||
distribution. We can then define the confidence interval. Here we will be using as
|
||
shorthands <span class="math notranslate nohighlight">\(\mu_{\beta}\)</span> for the above mean value and <span class="math notranslate nohighlight">\(\sigma_{\beta}\)</span>
|
||
for the standard deviation. We have then a confidence interval</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\left(\mu_{\beta}\pm \frac{z\sigma_{\beta}}{\sqrt{n}}\right),
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(z\)</span> defines the level of certainty (or confidence). For a normal
|
||
distribution typical parameters are <span class="math notranslate nohighlight">\(z=2.576\)</span> which corresponds to a
|
||
confidence of <span class="math notranslate nohighlight">\(99\%\)</span> while <span class="math notranslate nohighlight">\(z=1.96\)</span> corresponds to a confidence of
|
||
<span class="math notranslate nohighlight">\(95\%\)</span>. A confidence level of <span class="math notranslate nohighlight">\(95\%\)</span> is commonly used and it is
|
||
normally referred to as a <em>two-sigmas</em> confidence level, that is we
|
||
approximate <span class="math notranslate nohighlight">\(z\approx 2\)</span>.</p>
|
||
<p>For more discussions of confidence intervals (and in particular linked with a discussion of the bootstrap method), see chapter 5 of the textbook by <a class="reference external" href="https://www.cambridge.org/core/books/bootstrap-methods-and-their-application/ED2FD043579F27952363566DC09CBD6A">Davison on the Bootstrap Methods and their Applications</a></p>
|
||
<p>In this text you will also find an in-depth discussion of the
|
||
Bootstrap method, why it works and various theorems related to it.</p>
|
||
</div>
|
||
<div class="section" id="resampling-methods-bootstrap-background">
|
||
<h2>Resampling methods: Bootstrap background<a class="headerlink" href="#resampling-methods-bootstrap-background" title="Permalink to this headline">¶</a></h2>
|
||
<p>Since <span class="math notranslate nohighlight">\(\widehat{\beta} = \widehat{\beta}(\boldsymbol{X})\)</span> is a function of random variables,
|
||
<span class="math notranslate nohighlight">\(\widehat{\beta}\)</span> itself must be a random variable. Thus it has
|
||
a pdf, call this function <span class="math notranslate nohighlight">\(p(\boldsymbol{t})\)</span>. The aim of the bootstrap is to
|
||
estimate <span class="math notranslate nohighlight">\(p(\boldsymbol{t})\)</span> by the relative frequency of
|
||
<span class="math notranslate nohighlight">\(\widehat{\beta}\)</span>. You can think of this as using a histogram
|
||
in the place of <span class="math notranslate nohighlight">\(p(\boldsymbol{t})\)</span>. If the relative frequency closely
|
||
resembles <span class="math notranslate nohighlight">\(p(\vec{t})\)</span>, then using numerics, it is straight forward to
|
||
estimate all the interesting parameters of <span class="math notranslate nohighlight">\(p(\boldsymbol{t})\)</span> using point
|
||
estimators.</p>
|
||
</div>
|
||
<div class="section" id="resampling-methods-more-bootstrap-background">
|
||
<h2>Resampling methods: More Bootstrap background<a class="headerlink" href="#resampling-methods-more-bootstrap-background" title="Permalink to this headline">¶</a></h2>
|
||
<p>In the case that <span class="math notranslate nohighlight">\(\widehat{\beta}\)</span> has
|
||
more than one component, and the components are independent, we use the
|
||
same estimator on each component separately. If the probability
|
||
density function of <span class="math notranslate nohighlight">\(X_i\)</span>, <span class="math notranslate nohighlight">\(p(x)\)</span>, had been known, then it would have
|
||
been straightforward to do this by:</p>
|
||
<ol class="simple">
|
||
<li><p>Drawing lots of numbers from <span class="math notranslate nohighlight">\(p(x)\)</span>, suppose we call one such set of numbers <span class="math notranslate nohighlight">\((X_1^*, X_2^*, \cdots, X_n^*)\)</span>.</p></li>
|
||
<li><p>Then using these numbers, we could compute a replica of <span class="math notranslate nohighlight">\(\widehat{\beta}\)</span> called <span class="math notranslate nohighlight">\(\widehat{\beta}^*\)</span>.</p></li>
|
||
</ol>
|
||
<p>By repeated use of the above two points, many
|
||
estimates of <span class="math notranslate nohighlight">\(\widehat{\beta}\)</span> can be obtained. The
|
||
idea is to use the relative frequency of <span class="math notranslate nohighlight">\(\widehat{\beta}^*\)</span>
|
||
(think of a histogram) as an estimate of <span class="math notranslate nohighlight">\(p(\boldsymbol{t})\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="resampling-methods-bootstrap-approach">
|
||
<h2>Resampling methods: Bootstrap approach<a class="headerlink" href="#resampling-methods-bootstrap-approach" title="Permalink to this headline">¶</a></h2>
|
||
<p>But
|
||
unless there is enough information available about the process that
|
||
generated <span class="math notranslate nohighlight">\(X_1,X_2,\cdots,X_n\)</span>, <span class="math notranslate nohighlight">\(p(x)\)</span> is in general
|
||
unknown. Therefore, <a class="reference external" href="https://projecteuclid.org/euclid.aos/1176344552">Efron in 1979</a> asked the
|
||
question: What if we replace <span class="math notranslate nohighlight">\(p(x)\)</span> by the relative frequency
|
||
of the observation <span class="math notranslate nohighlight">\(X_i\)</span>?</p>
|
||
<p>If we draw observations in accordance with
|
||
the relative frequency of the observations, will we obtain the same
|
||
result in some asymptotic sense? The answer is yes.</p>
|
||
</div>
|
||
<div class="section" id="resampling-methods-bootstrap-steps">
|
||
<h2>Resampling methods: Bootstrap steps<a class="headerlink" href="#resampling-methods-bootstrap-steps" title="Permalink to this headline">¶</a></h2>
|
||
<p>The independent bootstrap works like this:</p>
|
||
<ol class="simple">
|
||
<li><p>Draw with replacement <span class="math notranslate nohighlight">\(n\)</span> numbers for the observed variables <span class="math notranslate nohighlight">\(\boldsymbol{x} = (x_1,x_2,\cdots,x_n)\)</span>.</p></li>
|
||
<li><p>Define a vector <span class="math notranslate nohighlight">\(\boldsymbol{x}^*\)</span> containing the values which were drawn from <span class="math notranslate nohighlight">\(\boldsymbol{x}\)</span>.</p></li>
|
||
<li><p>Using the vector <span class="math notranslate nohighlight">\(\boldsymbol{x}^*\)</span> compute <span class="math notranslate nohighlight">\(\widehat{\beta}^*\)</span> by evaluating <span class="math notranslate nohighlight">\(\widehat \beta\)</span> under the observations <span class="math notranslate nohighlight">\(\boldsymbol{x}^*\)</span>.</p></li>
|
||
<li><p>Repeat this process <span class="math notranslate nohighlight">\(k\)</span> times.</p></li>
|
||
</ol>
|
||
<p>When you are done, you can draw a histogram of the relative frequency
|
||
of <span class="math notranslate nohighlight">\(\widehat \beta^*\)</span>. This is your estimate of the probability
|
||
distribution <span class="math notranslate nohighlight">\(p(t)\)</span>. Using this probability distribution you can
|
||
estimate any statistics thereof. In principle you never draw the
|
||
histogram of the relative frequency of <span class="math notranslate nohighlight">\(\widehat{\beta}^*\)</span>. Instead
|
||
you use the estimators corresponding to the statistic of interest. For
|
||
example, if you are interested in estimating the variance of <span class="math notranslate nohighlight">\(\widehat
|
||
\beta\)</span>, apply the etsimator <span class="math notranslate nohighlight">\(\widehat \sigma^2\)</span> to the values
|
||
<span class="math notranslate nohighlight">\(\widehat \beta^*\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="code-example-for-the-bootstrap-method">
|
||
<h2>Code example for the Bootstrap method<a class="headerlink" href="#code-example-for-the-bootstrap-method" title="Permalink to this headline">¶</a></h2>
|
||
<p>The following code starts with a Gaussian distribution with mean value
|
||
<span class="math notranslate nohighlight">\(\mu =100\)</span> and variance <span class="math notranslate nohighlight">\(\sigma=15\)</span>. We use this to generate the data
|
||
used in the bootstrap analysis. The bootstrap analysis returns a data
|
||
set after a given number of bootstrap operations (as many as we have
|
||
data points). This data set consists of estimated mean values for each
|
||
bootstrap operation. The histogram generated by the bootstrap method
|
||
shows that the distribution for these mean values is also a Gaussian,
|
||
centered around the mean value <span class="math notranslate nohighlight">\(\mu=100\)</span> but with standard deviation
|
||
<span class="math notranslate nohighlight">\(\sigma/\sqrt{n}\)</span>, where <span class="math notranslate nohighlight">\(n\)</span> is the number of bootstrap samples (in
|
||
this case the same as the number of original data points). The value
|
||
of the standard deviation is what we expect from the central limit
|
||
theorem.</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="o">%</span><span class="k">matplotlib</span> inline
|
||
|
||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>
|
||
<span class="kn">from</span> <span class="nn">scipy.stats</span> <span class="kn">import</span> <span class="n">norm</span>
|
||
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
|
||
|
||
<span class="c1"># Returns mean of bootstrap samples </span>
|
||
<span class="c1"># Bootstrap algorithm</span>
|
||
<span class="k">def</span> <span class="nf">bootstrap</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">datapoints</span><span class="p">):</span>
|
||
<span class="n">t</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">datapoints</span><span class="p">)</span>
|
||
<span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
|
||
<span class="c1"># non-parametric bootstrap </span>
|
||
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">datapoints</span><span class="p">):</span>
|
||
<span class="n">t</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">n</span><span class="p">,</span><span class="n">n</span><span class="p">)])</span>
|
||
<span class="c1"># analysis </span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Bootstrap Statistics :"</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"original bias std. error"</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="si">%8g</span><span class="s2"> </span><span class="si">%8g</span><span class="s2"> </span><span class="si">%14g</span><span class="s2"> </span><span class="si">%15g</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">data</span><span class="p">),</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">t</span><span class="p">),</span><span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">t</span><span class="p">)))</span>
|
||
<span class="k">return</span> <span class="n">t</span>
|
||
|
||
<span class="c1"># We set the mean value to 100 and the standard deviation to 15</span>
|
||
<span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">15</span>
|
||
<span class="n">datapoints</span> <span class="o">=</span> <span class="mi">10000</span>
|
||
<span class="c1"># We generate random numbers according to the normal distribution</span>
|
||
<span class="n">x</span> <span class="o">=</span> <span class="n">mu</span> <span class="o">+</span> <span class="n">sigma</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="n">datapoints</span><span class="p">)</span>
|
||
<span class="c1"># bootstrap returns the data sample </span>
|
||
<span class="n">t</span> <span class="o">=</span> <span class="n">bootstrap</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">datapoints</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<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
|
||
99.7217 14.8193 99.7194 0.147557
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>We see that our new variance and from that the standard deviation, agrees with the central limit theorem.</p>
|
||
</div>
|
||
<div class="section" id="plotting-the-histogram">
|
||
<h2>Plotting the Histogram<a class="headerlink" href="#plotting-the-histogram" title="Permalink to this headline">¶</a></h2>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># the histogram of the bootstrapped data (normalized data if density = True)</span>
|
||
<span class="n">n</span><span class="p">,</span> <span class="n">binsboot</span><span class="p">,</span> <span class="n">patches</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="n">density</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">facecolor</span><span class="o">=</span><span class="s1">'red'</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.75</span><span class="p">)</span>
|
||
<span class="c1"># add a 'best fit' line </span>
|
||
<span class="n">y</span> <span class="o">=</span> <span class="n">norm</span><span class="o">.</span><span class="n">pdf</span><span class="p">(</span><span class="n">binsboot</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">t</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
|
||
<span class="n">lt</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">binsboot</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">'x'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">'Probability'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<img alt="_images/week37_121_0.png" src="_images/week37_121_0.png" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="the-bias-variance-tradeoff">
|
||
<h2>The bias-variance tradeoff<a class="headerlink" href="#the-bias-variance-tradeoff" title="Permalink to this headline">¶</a></h2>
|
||
<p>We will discuss the bias-variance tradeoff in the context of
|
||
continuous predictions such as regression. However, many of the
|
||
intuitions and ideas discussed here also carry over to classification
|
||
tasks. Consider a dataset <span class="math notranslate nohighlight">\(\mathcal{D}\)</span> consisting of the data
|
||
<span class="math notranslate nohighlight">\(\mathbf{X}_\mathcal{D}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}\)</span>.</p>
|
||
<p>Let us 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>where <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 then
|
||
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>Thereafter we found the parameters <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> by optimizing the means 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>We can rewrite this as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2.
|
||
\]</div>
|
||
<p>The three terms represent the square of the bias of the learning
|
||
method, which can be thought of as the error caused by the simplifying
|
||
assumptions built into the method. The second term represents the
|
||
variance of the chosen model and finally the last terms is variance of
|
||
the error <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span>.</p>
|
||
<p>To derive this equation, we need to recall that the variance of <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span> and <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span> are both equal to <span class="math notranslate nohighlight">\(\sigma^2\)</span>. The mean value of <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span> is by definition equal to zero. Furthermore, the function <span class="math notranslate nohighlight">\(f\)</span> is not a stochastics variable, idem for <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}}\)</span>.
|
||
We use a more compact notation in terms of the expectation value</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}})^2\right],
|
||
\]</div>
|
||
<p>and adding and subtracting <span class="math notranslate nohighlight">\(\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\)</span> we get</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}}+\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right],
|
||
\]</div>
|
||
<p>which, using the abovementioned expectation values can be rewritten as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right]+\mathrm{Var}\left[\boldsymbol{\tilde{y}}\right]+\sigma^2,
|
||
\]</div>
|
||
<p>that is the rewriting in terms of the so-called bias, the variance of the model <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}}\)</span> and the variance of <span class="math notranslate nohighlight">\(\boldsymbol{\epsilon}\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="a-way-to-read-the-bias-variance-tradeoff">
|
||
<h2>A way to Read the Bias-Variance Tradeoff<a class="headerlink" href="#a-way-to-read-the-bias-variance-tradeoff" title="Permalink to this headline">¶</a></h2>
|
||
<!-- dom:FIGURE: [figures/BiasVariance.png, width=600 frac=0.9] -->
|
||
<!-- begin figure -->
|
||
<p><img src="figures/BiasVariance.png" width="600"><p style="font-size: 0.9em"><i>Figure 1: </i></p></p>
|
||
<!-- end figure --></div>
|
||
<div class="section" id="example-code-for-bias-variance-tradeoff">
|
||
<h2>Example code for Bias-Variance tradeoff<a class="headerlink" href="#example-code-for-bias-variance-tradeoff" title="Permalink to this headline">¶</a></h2>
|
||
<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">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
|
||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</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="p">,</span> <span class="n">Ridge</span><span class="p">,</span> <span class="n">Lasso</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">PolynomialFeatures</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.pipeline</span> <span class="kn">import</span> <span class="n">make_pipeline</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.utils</span> <span class="kn">import</span> <span class="n">resample</span>
|
||
|
||
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">2018</span><span class="p">)</span>
|
||
|
||
<span class="n">n</span> <span class="o">=</span> <span class="mi">500</span>
|
||
<span class="n">n_boostraps</span> <span class="o">=</span> <span class="mi">100</span>
|
||
<span class="n">degree</span> <span class="o">=</span> <span class="mi">18</span> <span class="c1"># A quite high value, just to show.</span>
|
||
<span class="n">noise</span> <span class="o">=</span> <span class="mf">0.1</span>
|
||
|
||
<span class="c1"># Make data set.</span>
|
||
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
||
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mf">1.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Hold out some test data that is never used in training.</span>
|
||
<span class="n">x_train</span><span class="p">,</span> <span class="n">x_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">test_size</span><span class="o">=</span><span class="mf">0.2</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Combine x transformation and model into one operation.</span>
|
||
<span class="c1"># Not neccesary, but convenient.</span>
|
||
<span class="n">model</span> <span class="o">=</span> <span class="n">make_pipeline</span><span class="p">(</span><span class="n">PolynomialFeatures</span><span class="p">(</span><span class="n">degree</span><span class="o">=</span><span class="n">degree</span><span class="p">),</span> <span class="n">LinearRegression</span><span class="p">(</span><span class="n">fit_intercept</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
|
||
|
||
<span class="c1"># The following (m x n_bootstraps) matrix holds the column vectors y_pred</span>
|
||
<span class="c1"># for each bootstrap iteration.</span>
|
||
<span class="n">y_pred</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="n">y_test</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">n_boostraps</span><span class="p">))</span>
|
||
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_boostraps</span><span class="p">):</span>
|
||
<span class="n">x_</span><span class="p">,</span> <span class="n">y_</span> <span class="o">=</span> <span class="n">resample</span><span class="p">(</span><span class="n">x_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Evaluate the new model on the same test data each time.</span>
|
||
<span class="n">y_pred</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x_</span><span class="p">,</span> <span class="n">y_</span><span class="p">)</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x_test</span><span class="p">)</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
|
||
|
||
<span class="c1"># Note: Expectations and variances taken w.r.t. different training</span>
|
||
<span class="c1"># data sets, hence the axis=1. Subsequent means are taken across the test data</span>
|
||
<span class="c1"># set in order to obtain a total value, but before this we have error/bias/variance</span>
|
||
<span class="c1"># calculated per data point in the test set.</span>
|
||
<span class="c1"># Note 2: The use of keepdims=True is important in the calculation of bias as this </span>
|
||
<span class="c1"># maintains the column vector form. Dropping this yields very unexpected results.</span>
|
||
<span class="n">error</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">((</span><span class="n">y_test</span> <span class="o">-</span> <span class="n">y_pred</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="p">)</span>
|
||
<span class="n">bias</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span> <span class="p">(</span><span class="n">y_test</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">y_pred</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span> <span class="p">)</span>
|
||
<span class="n">variance</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="n">y_pred</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Error:'</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Bias^2:'</span><span class="p">,</span> <span class="n">bias</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Var:'</span><span class="p">,</span> <span class="n">variance</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'</span><span class="si">{}</span><span class="s1"> >= </span><span class="si">{}</span><span class="s1"> + </span><span class="si">{}</span><span class="s1"> = </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="n">bias</span><span class="p">,</span> <span class="n">variance</span><span class="p">,</span> <span class="n">bias</span><span class="o">+</span><span class="n">variance</span><span class="p">))</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">[::</span><span class="mi">5</span><span class="p">,</span> <span class="p">:],</span> <span class="n">y</span><span class="p">[::</span><span class="mi">5</span><span class="p">,</span> <span class="p">:],</span> <span class="n">label</span><span class="o">=</span><span class="s1">'f(x)'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">x_test</span><span class="p">,</span> <span class="n">y_test</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'Data points'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">x_test</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">y_pred</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">'Pred'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Error: 0.013121574015585152
|
||
Bias^2: 0.012073649446193166
|
||
Var: 0.0010479245693919886
|
||
0.013121574015585152 >= 0.012073649446193166 + 0.0010479245693919886 = 0.013121574015585155
|
||
</pre></div>
|
||
</div>
|
||
<img alt="_images/week37_137_1.png" src="_images/week37_137_1.png" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="understanding-what-happens">
|
||
<h2>Understanding what happens<a class="headerlink" href="#understanding-what-happens" title="Permalink to this headline">¶</a></h2>
|
||
<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">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
|
||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</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="p">,</span> <span class="n">Ridge</span><span class="p">,</span> <span class="n">Lasso</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">PolynomialFeatures</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.pipeline</span> <span class="kn">import</span> <span class="n">make_pipeline</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.utils</span> <span class="kn">import</span> <span class="n">resample</span>
|
||
|
||
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">2018</span><span class="p">)</span>
|
||
|
||
<span class="n">n</span> <span class="o">=</span> <span class="mi">40</span>
|
||
<span class="n">n_boostraps</span> <span class="o">=</span> <span class="mi">100</span>
|
||
<span class="n">maxdegree</span> <span class="o">=</span> <span class="mi">14</span>
|
||
|
||
|
||
<span class="c1"># Make data set.</span>
|
||
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
||
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mf">1.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
|
||
<span class="n">error</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">maxdegree</span><span class="p">)</span>
|
||
<span class="n">bias</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">maxdegree</span><span class="p">)</span>
|
||
<span class="n">variance</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">maxdegree</span><span class="p">)</span>
|
||
<span class="n">polydegree</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">maxdegree</span><span class="p">)</span>
|
||
<span class="n">x_train</span><span class="p">,</span> <span class="n">x_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">test_size</span><span class="o">=</span><span class="mf">0.2</span><span class="p">)</span>
|
||
|
||
<span class="k">for</span> <span class="n">degree</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">maxdegree</span><span class="p">):</span>
|
||
<span class="n">model</span> <span class="o">=</span> <span class="n">make_pipeline</span><span class="p">(</span><span class="n">PolynomialFeatures</span><span class="p">(</span><span class="n">degree</span><span class="o">=</span><span class="n">degree</span><span class="p">),</span> <span class="n">LinearRegression</span><span class="p">(</span><span class="n">fit_intercept</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
|
||
<span class="n">y_pred</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="n">y_test</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">n_boostraps</span><span class="p">))</span>
|
||
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_boostraps</span><span class="p">):</span>
|
||
<span class="n">x_</span><span class="p">,</span> <span class="n">y_</span> <span class="o">=</span> <span class="n">resample</span><span class="p">(</span><span class="n">x_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
|
||
<span class="n">y_pred</span><span class="p">[:,</span> <span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x_</span><span class="p">,</span> <span class="n">y_</span><span class="p">)</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x_test</span><span class="p">)</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
|
||
|
||
<span class="n">polydegree</span><span class="p">[</span><span class="n">degree</span><span class="p">]</span> <span class="o">=</span> <span class="n">degree</span>
|
||
<span class="n">error</span><span class="p">[</span><span class="n">degree</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">((</span><span class="n">y_test</span> <span class="o">-</span> <span class="n">y_pred</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="p">)</span>
|
||
<span class="n">bias</span><span class="p">[</span><span class="n">degree</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span> <span class="p">(</span><span class="n">y_test</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">y_pred</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span> <span class="p">)</span>
|
||
<span class="n">variance</span><span class="p">[</span><span class="n">degree</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="n">y_pred</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Polynomial degree:'</span><span class="p">,</span> <span class="n">degree</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Error:'</span><span class="p">,</span> <span class="n">error</span><span class="p">[</span><span class="n">degree</span><span class="p">])</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Bias^2:'</span><span class="p">,</span> <span class="n">bias</span><span class="p">[</span><span class="n">degree</span><span class="p">])</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Var:'</span><span class="p">,</span> <span class="n">variance</span><span class="p">[</span><span class="n">degree</span><span class="p">])</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'</span><span class="si">{}</span><span class="s1"> >= </span><span class="si">{}</span><span class="s1"> + </span><span class="si">{}</span><span class="s1"> = </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">error</span><span class="p">[</span><span class="n">degree</span><span class="p">],</span> <span class="n">bias</span><span class="p">[</span><span class="n">degree</span><span class="p">],</span> <span class="n">variance</span><span class="p">[</span><span class="n">degree</span><span class="p">],</span> <span class="n">bias</span><span class="p">[</span><span class="n">degree</span><span class="p">]</span><span class="o">+</span><span class="n">variance</span><span class="p">[</span><span class="n">degree</span><span class="p">]))</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">polydegree</span><span class="p">,</span> <span class="n">error</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'Error'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">polydegree</span><span class="p">,</span> <span class="n">bias</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'bias'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">polydegree</span><span class="p">,</span> <span class="n">variance</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">'Variance'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 0
|
||
Error: 0.32149601703519115
|
||
Bias^2: 0.3123314713548606
|
||
Var: 0.009164545680330616
|
||
0.32149601703519115 >= 0.3123314713548606 + 0.009164545680330616 = 0.3214960170351912
|
||
Polynomial degree: 1
|
||
Error: 0.08426840630693411
|
||
Bias^2: 0.0796891867672603
|
||
Var: 0.004579219539673834
|
||
0.08426840630693411 >= 0.0796891867672603 + 0.004579219539673834 = 0.08426840630693413
|
||
Polynomial degree: 2
|
||
Error: 0.10398646080125035
|
||
Bias^2: 0.1007711427354898
|
||
Var: 0.0032153180657605116
|
||
0.10398646080125035 >= 0.1007711427354898 + 0.0032153180657605116 = 0.10398646080125032
|
||
</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
|
||
0.06547790180152355 >= 0.06208238634231949 + 0.0033955154592040936 = 0.06547790180152359
|
||
Polynomial degree: 4
|
||
Error: 0.06844519414009445
|
||
Bias^2: 0.06453579006728324
|
||
Var: 0.003909404072811226
|
||
0.06844519414009445 >= 0.06453579006728324 + 0.003909404072811226 = 0.06844519414009446
|
||
</pre></div>
|
||
</div>
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree: 5
|
||
Error: 0.05227921801205686
|
||
Bias^2: 0.0481872773043029
|
||
Var: 0.004091940707753939
|
||
0.05227921801205686 >= 0.0481872773043029 + 0.004091940707753939 = 0.052279218012056844
|
||
Polynomial degree: 6
|
||
Error: 0.037813671417389005
|
||
Bias^2: 0.033657685071527665
|
||
Var: 0.00415598634586135
|
||
0.037813671417389005 >= 0.033657685071527665 + 0.00415598634586135 = 0.03781367141738902
|
||
Polynomial degree: 7
|
||
Error: 0.02760977349102253
|
||
Bias^2: 0.022999498260366312
|
||
Var: 0.004610275230656212
|
||
0.02760977349102253 >= 0.022999498260366312 + 0.004610275230656212 = 0.027609773491022525
|
||
Polynomial degree: 8
|
||
Error: 0.017355848195593347
|
||
Bias^2: 0.010331721306655127
|
||
Var: 0.007024126888938232
|
||
0.017355848195593347 >= 0.010331721306655127 + 0.007024126888938232 = 0.01735584819559336
|
||
Polynomial degree: 9
|
||
Error: 0.02660572763718093
|
||
Bias^2: 0.010018312644137363
|
||
Var: 0.016587414993043573
|
||
0.02660572763718093 >= 0.010018312644137363 + 0.016587414993043573 = 0.026605727637180936
|
||
</pre></div>
|
||
</div>
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Polynomial degree:
|
||
</pre></div>
|
||
</div>
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 10
|
||
Error: 0.021592704588025025
|
||
Bias^2: 0.010516485576645508
|
||
Var: 0.011076219011379514
|
||
0.021592704588025025 >= 0.010516485576645508 + 0.011076219011379514 = 0.021592704588025022
|
||
Polynomial degree: 11
|
||
Error: 0.07160048164233104
|
||
Bias^2: 0.014436800088904942
|
||
Var: 0.05716368155342608
|
||
0.07160048164233104 >= 0.014436800088904942 + 0.05716368155342608 = 0.07160048164233102
|
||
Polynomial degree: 12
|
||
Error: 0.11547777218872497
|
||
Bias^2: 0.01628578269596628
|
||
Var: 0.09919198949275869
|
||
0.11547777218872497 >= 0.01628578269596628 + 0.09919198949275869 = 0.11547777218872497
|
||
Polynomial degree: 13
|
||
Error: 0.22842468702219465
|
||
Bias^2: 0.01975416527185249
|
||
Var: 0.20867052175034223
|
||
0.22842468702219465 >= 0.01975416527185249 + 0.20867052175034223 = 0.2284246870221947
|
||
</pre></div>
|
||
</div>
|
||
<img alt="_images/week37_139_5.png" src="_images/week37_139_5.png" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="summing-up">
|
||
<h2>Summing up<a class="headerlink" href="#summing-up" title="Permalink to this headline">¶</a></h2>
|
||
<p>The bias-variance tradeoff summarizes the fundamental tension in
|
||
machine learning, particularly supervised learning, between the
|
||
complexity of a model and the amount of training data needed to train
|
||
it. Since data is often limited, in practice it is often useful to
|
||
use a less-complex model with higher bias, that is a model whose asymptotic
|
||
performance is worse than another model because it is easier to
|
||
train and less sensitive to sampling noise arising from having a
|
||
finite-sized training dataset (smaller variance).</p>
|
||
<p>The above equations tell us that in
|
||
order to minimize the expected test error, we need to select a
|
||
statistical learning method that simultaneously achieves low variance
|
||
and low bias. Note that variance is inherently a nonnegative quantity,
|
||
and squared bias is also nonnegative. Hence, we see that the expected
|
||
test MSE can never lie below <span class="math notranslate nohighlight">\(Var(\epsilon)\)</span>, the irreducible error.</p>
|
||
<p>What do we mean by the variance and bias of a statistical learning
|
||
method? The variance refers to the amount by which our model would change if we
|
||
estimated it using a different training data set. Since the training
|
||
data are used to fit the statistical learning method, different
|
||
training data sets will result in a different estimate. But ideally the
|
||
estimate for our model should not vary too much between training
|
||
sets. However, if a method has high variance then small changes in
|
||
the training data can result in large changes in the model. In general, more
|
||
flexible statistical methods have higher variance.</p>
|
||
<p>You may also find this recent <a class="reference external" href="https://www.pnas.org/content/116/32/15849">article</a> of interest.</p>
|
||
</div>
|
||
<div class="section" id="another-example-from-scikit-learn-s-repository">
|
||
<h2>Another Example from Scikit-Learn’s Repository<a class="headerlink" href="#another-example-from-scikit-learn-s-repository" title="Permalink to this headline">¶</a></h2>
|
||
<p>This example demonstrates the problems of underfitting and overfitting and
|
||
how we can use linear regression with polynomial features to approximate
|
||
nonlinear functions. The plot shows the function that we want to approximate,
|
||
which is a part of the cosine function. In addition, the samples from the
|
||
real function and the approximations of different models are displayed. The
|
||
models have polynomial features of different degrees. We can see that a
|
||
linear function (polynomial with degree 1) is not sufficient to fit the
|
||
training samples. This is called <strong>underfitting</strong>. A polynomial of degree 4
|
||
approximates the true function almost perfectly. However, for higher degrees
|
||
the model will <strong>overfit</strong> the training data, i.e. it learns the noise of the
|
||
training data.
|
||
We evaluate quantitatively overfitting and underfitting by using
|
||
cross-validation. We calculate the mean squared error (MSE) on the validation
|
||
set, the higher, the less likely the model generalizes correctly from the
|
||
training data.</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="c1">#print(__doc__)</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.pipeline</span> <span class="kn">import</span> <span class="n">Pipeline</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">PolynomialFeatures</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.model_selection</span> <span class="kn">import</span> <span class="n">cross_val_score</span>
|
||
|
||
|
||
<span class="k">def</span> <span class="nf">true_fun</span><span class="p">(</span><span class="n">X</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mf">1.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">X</span><span class="p">)</span>
|
||
|
||
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||
|
||
<span class="n">n_samples</span> <span class="o">=</span> <span class="mi">30</span>
|
||
<span class="n">degrees</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">15</span><span class="p">]</span>
|
||
|
||
<span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">n_samples</span><span class="p">))</span>
|
||
<span class="n">y</span> <span class="o">=</span> <span class="n">true_fun</span><span class="p">(</span><span class="n">X</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="n">n_samples</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.1</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">14</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>
|
||
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">degrees</span><span class="p">)):</span>
|
||
<span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">degrees</span><span class="p">),</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">setp</span><span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">xticks</span><span class="o">=</span><span class="p">(),</span> <span class="n">yticks</span><span class="o">=</span><span class="p">())</span>
|
||
|
||
<span class="n">polynomial_features</span> <span class="o">=</span> <span class="n">PolynomialFeatures</span><span class="p">(</span><span class="n">degree</span><span class="o">=</span><span class="n">degrees</span><span class="p">[</span><span class="n">i</span><span class="p">],</span>
|
||
<span class="n">include_bias</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
|
||
<span class="n">linear_regression</span> <span class="o">=</span> <span class="n">LinearRegression</span><span class="p">()</span>
|
||
<span class="n">pipeline</span> <span class="o">=</span> <span class="n">Pipeline</span><span class="p">([(</span><span class="s2">"polynomial_features"</span><span class="p">,</span> <span class="n">polynomial_features</span><span class="p">),</span>
|
||
<span class="p">(</span><span class="s2">"linear_regression"</span><span class="p">,</span> <span class="n">linear_regression</span><span class="p">)])</span>
|
||
<span class="n">pipeline</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">],</span> <span class="n">y</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Evaluate the models using crossvalidation</span>
|
||
<span class="n">scores</span> <span class="o">=</span> <span class="n">cross_val_score</span><span class="p">(</span><span class="n">pipeline</span><span class="p">,</span> <span class="n">X</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">],</span> <span class="n">y</span><span class="p">,</span>
|
||
<span class="n">scoring</span><span class="o">=</span><span class="s2">"neg_mean_squared_error"</span><span class="p">,</span> <span class="n">cv</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
|
||
|
||
<span class="n">X_test</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">X_test</span><span class="p">,</span> <span class="n">pipeline</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]),</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Model"</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">X_test</span><span class="p">,</span> <span class="n">true_fun</span><span class="p">(</span><span class="n">X_test</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s2">"True function"</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">edgecolor</span><span class="o">=</span><span class="s1">'b'</span><span class="p">,</span> <span class="n">s</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"Samples"</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s2">"x"</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s2">"y"</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">((</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s2">"best"</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">"Degree </span><span class="si">{}</span><span class="se">\n</span><span class="s2">MSE = </span><span class="si">{:.2e}</span><span class="s2">(+/- </span><span class="si">{:.2e}</span><span class="s2">)"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
|
||
<span class="n">degrees</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="o">-</span><span class="n">scores</span><span class="o">.</span><span class="n">mean</span><span class="p">(),</span> <span class="n">scores</span><span class="o">.</span><span class="n">std</span><span class="p">()))</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<img alt="_images/week37_142_0.png" src="_images/week37_142_0.png" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="various-steps-in-cross-validation">
|
||
<h2>Various steps in cross-validation<a class="headerlink" href="#various-steps-in-cross-validation" title="Permalink to this headline">¶</a></h2>
|
||
<p>When the repetitive splitting of the data set is done randomly,
|
||
samples may accidently end up in a fast majority of the splits in
|
||
either training or test set. Such samples may have an unbalanced
|
||
influence on either model building or prediction evaluation. To avoid
|
||
this <span class="math notranslate nohighlight">\(k\)</span>-fold cross-validation structures the data splitting. The
|
||
samples are divided into <span class="math notranslate nohighlight">\(k\)</span> more or less equally sized exhaustive and
|
||
mutually exclusive subsets. In turn (at each split) one of these
|
||
subsets plays the role of the test set while the union of the
|
||
remaining subsets constitutes the training set. Such a splitting
|
||
warrants a balanced representation of each sample in both training and
|
||
test set over the splits. Still the division into the <span class="math notranslate nohighlight">\(k\)</span> subsets
|
||
involves a degree of randomness. This may be fully excluded when
|
||
choosing <span class="math notranslate nohighlight">\(k=n\)</span>. This particular case is referred to as leave-one-out
|
||
cross-validation (LOOCV).</p>
|
||
</div>
|
||
<div class="section" id="cross-validation-in-brief">
|
||
<h2>Cross-validation in brief<a class="headerlink" href="#cross-validation-in-brief" title="Permalink to this headline">¶</a></h2>
|
||
<p>For the various values of <span class="math notranslate nohighlight">\(k\)</span></p>
|
||
<ol class="simple">
|
||
<li><p>shuffle the dataset randomly.</p></li>
|
||
<li><p>Split the dataset into <span class="math notranslate nohighlight">\(k\)</span> groups.</p></li>
|
||
<li><p>For each unique group:</p></li>
|
||
</ol>
|
||
<p>a. Decide which group to use as set for test data</p>
|
||
<p>b. Take the remaining groups as a training data set</p>
|
||
<p>c. Fit a model on the training set and evaluate it on the test set</p>
|
||
<p>d. Retain the evaluation score and discard the model</p>
|
||
<ol class="simple">
|
||
<li><p>Summarize the model using the sample of model evaluation scores</p></li>
|
||
</ol>
|
||
</div>
|
||
<div class="section" id="code-example-for-cross-validation-and-k-fold-cross-validation">
|
||
<h2>Code Example for Cross-validation and <span class="math notranslate nohighlight">\(k\)</span>-fold Cross-validation<a class="headerlink" href="#code-example-for-cross-validation-and-k-fold-cross-validation" title="Permalink to this headline">¶</a></h2>
|
||
<p>The code here uses Ridge regression with cross-validation (CV) resampling and <span class="math notranslate nohighlight">\(k\)</span>-fold CV in order to fit a specific polynomial.</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.model_selection</span> <span class="kn">import</span> <span class="n">KFold</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <span class="n">Ridge</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">cross_val_score</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">PolynomialFeatures</span>
|
||
|
||
<span class="c1"># A seed just to ensure that the random numbers are the same for every run.</span>
|
||
<span class="c1"># Useful for eventual debugging.</span>
|
||
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">3155</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Generate the data.</span>
|
||
<span class="n">nsamples</span> <span class="o">=</span> <span class="mi">100</span>
|
||
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="n">nsamples</span><span class="p">)</span>
|
||
<span class="n">y</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="n">nsamples</span><span class="p">)</span>
|
||
|
||
<span class="c1">## Cross-validation on Ridge regression using KFold only</span>
|
||
|
||
<span class="c1"># Decide degree on polynomial to fit</span>
|
||
<span class="n">poly</span> <span class="o">=</span> <span class="n">PolynomialFeatures</span><span class="p">(</span><span class="n">degree</span> <span class="o">=</span> <span class="mi">6</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Decide which values of lambda to use</span>
|
||
<span class="n">nlambdas</span> <span class="o">=</span> <span class="mi">500</span>
|
||
<span class="n">lambdas</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">logspace</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="n">nlambdas</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Initialize a KFold instance</span>
|
||
<span class="n">k</span> <span class="o">=</span> <span class="mi">5</span>
|
||
<span class="n">kfold</span> <span class="o">=</span> <span class="n">KFold</span><span class="p">(</span><span class="n">n_splits</span> <span class="o">=</span> <span class="n">k</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Perform the cross-validation to estimate MSE</span>
|
||
<span class="n">scores_KFold</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">nlambdas</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
|
||
|
||
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
|
||
<span class="k">for</span> <span class="n">lmb</span> <span class="ow">in</span> <span class="n">lambdas</span><span class="p">:</span>
|
||
<span class="n">ridge</span> <span class="o">=</span> <span class="n">Ridge</span><span class="p">(</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">lmb</span><span class="p">)</span>
|
||
<span class="n">j</span> <span class="o">=</span> <span class="mi">0</span>
|
||
<span class="k">for</span> <span class="n">train_inds</span><span class="p">,</span> <span class="n">test_inds</span> <span class="ow">in</span> <span class="n">kfold</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||
<span class="n">xtrain</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="n">train_inds</span><span class="p">]</span>
|
||
<span class="n">ytrain</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="n">train_inds</span><span class="p">]</span>
|
||
|
||
<span class="n">xtest</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="n">test_inds</span><span class="p">]</span>
|
||
<span class="n">ytest</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="n">test_inds</span><span class="p">]</span>
|
||
|
||
<span class="n">Xtrain</span> <span class="o">=</span> <span class="n">poly</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">xtrain</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">])</span>
|
||
<span class="n">ridge</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">Xtrain</span><span class="p">,</span> <span class="n">ytrain</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">])</span>
|
||
|
||
<span class="n">Xtest</span> <span class="o">=</span> <span class="n">poly</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">xtest</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">])</span>
|
||
<span class="n">ypred</span> <span class="o">=</span> <span class="n">ridge</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">Xtest</span><span class="p">)</span>
|
||
|
||
<span class="n">scores_KFold</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">((</span><span class="n">ypred</span> <span class="o">-</span> <span class="n">ytest</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">])</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="n">np</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">ypred</span><span class="p">)</span>
|
||
|
||
<span class="n">j</span> <span class="o">+=</span> <span class="mi">1</span>
|
||
<span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
|
||
|
||
|
||
<span class="n">estimated_mse_KFold</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">scores_KFold</span><span class="p">,</span> <span class="n">axis</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span>
|
||
|
||
<span class="c1">## Cross-validation using cross_val_score from sklearn along with KFold</span>
|
||
|
||
<span class="c1"># kfold is an instance initialized above as:</span>
|
||
<span class="c1"># kfold = KFold(n_splits = k)</span>
|
||
|
||
<span class="n">estimated_mse_sklearn</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">nlambdas</span><span class="p">)</span>
|
||
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
|
||
<span class="k">for</span> <span class="n">lmb</span> <span class="ow">in</span> <span class="n">lambdas</span><span class="p">:</span>
|
||
<span class="n">ridge</span> <span class="o">=</span> <span class="n">Ridge</span><span class="p">(</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">lmb</span><span class="p">)</span>
|
||
|
||
<span class="n">X</span> <span class="o">=</span> <span class="n">poly</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">x</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">])</span>
|
||
<span class="n">estimated_mse_folds</span> <span class="o">=</span> <span class="n">cross_val_score</span><span class="p">(</span><span class="n">ridge</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">],</span> <span class="n">scoring</span><span class="o">=</span><span class="s1">'neg_mean_squared_error'</span><span class="p">,</span> <span class="n">cv</span><span class="o">=</span><span class="n">kfold</span><span class="p">)</span>
|
||
|
||
<span class="c1"># cross_val_score return an array containing the estimated negative mse for every fold.</span>
|
||
<span class="c1"># we have to the the mean of every array in order to get an estimate of the mse of the model</span>
|
||
<span class="n">estimated_mse_sklearn</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="o">-</span><span class="n">estimated_mse_folds</span><span class="p">)</span>
|
||
|
||
<span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
|
||
|
||
<span class="c1">## Plot and compare the slightly different ways to perform cross-validation</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">lambdas</span><span class="p">),</span> <span class="n">estimated_mse_sklearn</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="s1">'cross_val_score'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">lambdas</span><span class="p">),</span> <span class="n">estimated_mse_KFold</span><span class="p">,</span> <span class="s1">'r--'</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="s1">'KFold'</span><span class="p">)</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">'log10(lambda)'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">'mse'</span><span class="p">)</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<img alt="_images/week37_146_0.png" src="_images/week37_146_0.png" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="more-examples-on-bootstrap-and-cross-validation-and-errors">
|
||
<h2>More examples on bootstrap and cross-validation and errors<a class="headerlink" href="#more-examples-on-bootstrap-and-cross-validation-and-errors" title="Permalink to this headline">¶</a></h2>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Common imports</span>
|
||
<span class="kn">import</span> <span class="nn">os</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">pandas</span> <span class="k">as</span> <span class="nn">pd</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.linear_model</span> <span class="kn">import</span> <span class="n">LinearRegression</span><span class="p">,</span> <span class="n">Ridge</span><span class="p">,</span> <span class="n">Lasso</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="nn">sklearn.metrics</span> <span class="kn">import</span> <span class="n">mean_squared_error</span>
|
||
<span class="c1"># Where to save the figures and data files</span>
|
||
<span class="n">PROJECT_ROOT_DIR</span> <span class="o">=</span> <span class="s2">"Results"</span>
|
||
<span class="n">FIGURE_ID</span> <span class="o">=</span> <span class="s2">"Results/FigureFiles"</span>
|
||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"DataFiles/"</span>
|
||
|
||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">):</span>
|
||
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">)</span>
|
||
|
||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">FIGURE_ID</span><span class="p">):</span>
|
||
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">FIGURE_ID</span><span class="p">)</span>
|
||
|
||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">DATA_ID</span><span class="p">):</span>
|
||
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">DATA_ID</span><span class="p">)</span>
|
||
|
||
<span class="k">def</span> <span class="nf">image_path</span><span class="p">(</span><span class="n">fig_id</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">FIGURE_ID</span><span class="p">,</span> <span class="n">fig_id</span><span class="p">)</span>
|
||
|
||
<span class="k">def</span> <span class="nf">data_path</span><span class="p">(</span><span class="n">dat_id</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">DATA_ID</span><span class="p">,</span> <span class="n">dat_id</span><span class="p">)</span>
|
||
|
||
<span class="k">def</span> <span class="nf">save_fig</span><span class="p">(</span><span class="n">fig_id</span><span class="p">):</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="n">image_path</span><span class="p">(</span><span class="n">fig_id</span><span class="p">)</span> <span class="o">+</span> <span class="s2">".png"</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s1">'png'</span><span class="p">)</span>
|
||
|
||
<span class="n">infile</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">data_path</span><span class="p">(</span><span class="s2">"EoS.csv"</span><span class="p">),</span><span class="s1">'r'</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Read the EoS data as csv file and organize the data into two arrays with density and energies</span>
|
||
<span class="n">EoS</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="n">names</span><span class="o">=</span><span class="p">(</span><span class="s1">'Density'</span><span class="p">,</span> <span class="s1">'Energy'</span><span class="p">))</span>
|
||
<span class="n">EoS</span><span class="p">[</span><span class="s1">'Energy'</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">EoS</span><span class="p">[</span><span class="s1">'Energy'</span><span class="p">],</span> <span class="n">errors</span><span class="o">=</span><span class="s1">'coerce'</span><span class="p">)</span>
|
||
<span class="n">EoS</span> <span class="o">=</span> <span class="n">EoS</span><span class="o">.</span><span class="n">dropna</span><span class="p">()</span>
|
||
<span class="n">Energies</span> <span class="o">=</span> <span class="n">EoS</span><span class="p">[</span><span class="s1">'Energy'</span><span class="p">]</span>
|
||
<span class="n">Density</span> <span class="o">=</span> <span class="n">EoS</span><span class="p">[</span><span class="s1">'Density'</span><span class="p">]</span>
|
||
<span class="c1"># The design matrix now as function of various polytrops</span>
|
||
|
||
<span class="n">Maxpolydegree</span> <span class="o">=</span> <span class="mi">30</span>
|
||
<span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">Density</span><span class="p">),</span><span class="n">Maxpolydegree</span><span class="p">))</span>
|
||
<span class="n">X</span><span class="p">[:,</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
|
||
<span class="n">testerror</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">Maxpolydegree</span><span class="p">)</span>
|
||
<span class="n">trainingerror</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">Maxpolydegree</span><span class="p">)</span>
|
||
<span class="n">polynomial</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">Maxpolydegree</span><span class="p">)</span>
|
||
|
||
<span class="n">trials</span> <span class="o">=</span> <span class="mi">100</span>
|
||
<span class="k">for</span> <span class="n">polydegree</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">Maxpolydegree</span><span class="p">):</span>
|
||
<span class="n">polynomial</span><span class="p">[</span><span class="n">polydegree</span><span class="p">]</span> <span class="o">=</span> <span class="n">polydegree</span>
|
||
<span class="k">for</span> <span class="n">degree</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">polydegree</span><span class="p">):</span>
|
||
<span class="n">X</span><span class="p">[:,</span><span class="n">degree</span><span class="p">]</span> <span class="o">=</span> <span class="n">Density</span><span class="o">**</span><span class="p">(</span><span class="n">degree</span><span class="o">/</span><span class="mf">3.0</span><span class="p">)</span>
|
||
|
||
<span class="c1"># loop over trials in order to estimate the expectation value of the MSE</span>
|
||
<span class="n">testerror</span><span class="p">[</span><span class="n">polydegree</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span>
|
||
<span class="n">trainingerror</span><span class="p">[</span><span class="n">polydegree</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.0</span>
|
||
<span class="k">for</span> <span class="n">samples</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">trials</span><span class="p">):</span>
|
||
<span class="n">x_train</span><span class="p">,</span> <span class="n">x_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Energies</span><span class="p">,</span> <span class="n">test_size</span><span class="o">=</span><span class="mf">0.2</span><span class="p">)</span>
|
||
<span class="n">model</span> <span class="o">=</span> <span class="n">LinearRegression</span><span class="p">(</span><span class="n">fit_intercept</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
|
||
<span class="n">ypred</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x_train</span><span class="p">)</span>
|
||
<span class="n">ytilde</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x_test</span><span class="p">)</span>
|
||
<span class="n">testerror</span><span class="p">[</span><span class="n">polydegree</span><span class="p">]</span> <span class="o">+=</span> <span class="n">mean_squared_error</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">ytilde</span><span class="p">)</span>
|
||
<span class="n">trainingerror</span><span class="p">[</span><span class="n">polydegree</span><span class="p">]</span> <span class="o">+=</span> <span class="n">mean_squared_error</span><span class="p">(</span><span class="n">y_train</span><span class="p">,</span> <span class="n">ypred</span><span class="p">)</span>
|
||
|
||
<span class="n">testerror</span><span class="p">[</span><span class="n">polydegree</span><span class="p">]</span> <span class="o">/=</span> <span class="n">trials</span>
|
||
<span class="n">trainingerror</span><span class="p">[</span><span class="n">polydegree</span><span class="p">]</span> <span class="o">/=</span> <span class="n">trials</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Degree of polynomial: </span><span class="si">%3d</span><span class="s2">"</span><span class="o">%</span> <span class="n">polynomial</span><span class="p">[</span><span class="n">polydegree</span><span class="p">])</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Mean squared error on training data: </span><span class="si">%.8f</span><span class="s2">"</span> <span class="o">%</span> <span class="n">trainingerror</span><span class="p">[</span><span class="n">polydegree</span><span class="p">])</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Mean squared error on test data: </span><span class="si">%.8f</span><span class="s2">"</span> <span class="o">%</span> <span class="n">testerror</span><span class="p">[</span><span class="n">polydegree</span><span class="p">])</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">polynomial</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">trainingerror</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">'Training Error'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">polynomial</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">testerror</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">'Test Error'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">'Polynomial degree'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">'log10[MSE]'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="cell_output docutils container">
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 1
|
||
Mean squared error on training data: 446033.51374050
|
||
Mean squared error on test data: 455173.80460179
|
||
Degree of polynomial: 2
|
||
Mean squared error on training data: 114550.54637219
|
||
Mean squared error on test data: 129963.83146596
|
||
Degree of polynomial: 3
|
||
Mean squared error on training data: 9054.61775176
|
||
Mean squared error on test data: 10572.87627342
|
||
Degree of polynomial: 4
|
||
Mean squared error on training data: 302.15313054
|
||
Mean squared error on test data: 433.26292364
|
||
</pre></div>
|
||
</div>
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 5
|
||
Mean squared error on training data: 3.64316192
|
||
Mean squared error on test data: 7.23528337
|
||
Degree of polynomial: 6
|
||
Mean squared error on training data: 3.56589683
|
||
Mean squared error on test data: 10.50427787
|
||
Degree of polynomial: 7
|
||
Mean squared error on training data: 0.47313680
|
||
Mean squared error on test data: 1.53738247
|
||
Degree of polynomial: 8
|
||
Mean squared error on training data: 0.04926746
|
||
Mean squared error on test data: 0.14629156
|
||
</pre></div>
|
||
</div>
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 9
|
||
Mean squared error on training data: 0.02546675
|
||
Mean squared error on test data: 0.11202337
|
||
Degree of polynomial: 10
|
||
Mean squared error on training data: 0.02424794
|
||
Mean squared error on test data: 0.22467274
|
||
Degree of polynomial: 11
|
||
Mean squared error on training data: 0.01594452
|
||
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
|
||
</pre></div>
|
||
</div>
|
||
<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
|
||
Mean squared error on training data: 0.00420072
|
||
Mean squared error on test data: 568.47051432
|
||
Degree of polynomial: 16
|
||
Mean squared error on training data: 0.00325450
|
||
Mean squared error on test data: 48.97630233
|
||
</pre></div>
|
||
</div>
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 17
|
||
Mean squared error on training data: 0.00242954
|
||
Mean squared error on test data: 2.52780600
|
||
Degree of polynomial: 18
|
||
Mean squared error on training data: 0.00219195
|
||
Mean squared error on test data: 429.25695398
|
||
Degree of polynomial: 19
|
||
Mean squared error on training data: 0.00154853
|
||
Mean squared error on test data: 239.97065359
|
||
Degree of polynomial: 20
|
||
Mean squared error on training data: 0.00140846
|
||
Mean squared error on test data: 1350.24493666
|
||
</pre></div>
|
||
</div>
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 21
|
||
Mean squared error on training data: 0.00119688
|
||
Mean squared error on test data: 1840.50530832
|
||
Degree of polynomial: 22
|
||
Mean squared error on training data: 0.00092898
|
||
Mean squared error on test data: 1184.60929685
|
||
Degree of polynomial: 23
|
||
Mean squared error on training data: 0.00089193
|
||
Mean squared error on test data: 3892.17483760
|
||
Degree of polynomial: 24
|
||
Mean squared error on training data: 0.00083355
|
||
Mean squared error on test data: 1332.46736215
|
||
</pre></div>
|
||
</div>
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 25
|
||
Mean squared error on training data: 0.00079904
|
||
Mean squared error on test data: 7577.76690383
|
||
Degree of polynomial: 26
|
||
Mean squared error on training data: 0.00075590
|
||
Mean squared error on test data: 1079.36895644
|
||
Degree of polynomial: 27
|
||
Mean squared error on training data: 0.00068091
|
||
Mean squared error on test data: 3207.25343155
|
||
Degree of polynomial: 28
|
||
Mean squared error on training data: 0.00063362
|
||
Mean squared error on test data: 674.79633065
|
||
</pre></div>
|
||
</div>
|
||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Degree of polynomial: 29
|
||
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_405/626635268.py:73: RuntimeWarning: divide by zero encountered in log10
|
||
plt.plot(polynomial, np.log10(trainingerror), label='Training Error')
|
||
/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_405/626635268.py:74: RuntimeWarning: divide by zero encountered in log10
|
||
plt.plot(polynomial, np.log10(testerror), label='Test Error')
|
||
</pre></div>
|
||
</div>
|
||
<img alt="_images/week37_148_9.png" src="_images/week37_148_9.png" />
|
||
</div>
|
||
</div>
|
||
<p>Note that we kept the intercept column in the fitting here. This means that we need to set the <strong>intercept</strong> in the call to the <strong>Scikit-Learn</strong> function as <strong>False</strong>. Alternatively, we could have set up the design matrix <span class="math notranslate nohighlight">\(X\)</span> without the first column of ones.</p>
|
||
</div>
|
||
<div class="section" id="the-same-example-but-now-with-cross-validation">
|
||
<h2>The same example but now with cross-validation<a class="headerlink" href="#the-same-example-but-now-with-cross-validation" title="Permalink to this headline">¶</a></h2>
|
||
<p>In this example we keep the intercept column again but add cross-validation in order to estimate the best possible value of the means 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="c1"># Common imports</span>
|
||
<span class="kn">import</span> <span class="nn">os</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">pandas</span> <span class="k">as</span> <span class="nn">pd</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.linear_model</span> <span class="kn">import</span> <span class="n">LinearRegression</span><span class="p">,</span> <span class="n">Ridge</span><span class="p">,</span> <span class="n">Lasso</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">KFold</span>
|
||
<span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">cross_val_score</span>
|
||
|
||
|
||
<span class="c1"># Where to save the figures and data files</span>
|
||
<span class="n">PROJECT_ROOT_DIR</span> <span class="o">=</span> <span class="s2">"Results"</span>
|
||
<span class="n">FIGURE_ID</span> <span class="o">=</span> <span class="s2">"Results/FigureFiles"</span>
|
||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"DataFiles/"</span>
|
||
|
||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">):</span>
|
||
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">)</span>
|
||
|
||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">FIGURE_ID</span><span class="p">):</span>
|
||
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">FIGURE_ID</span><span class="p">)</span>
|
||
|
||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">DATA_ID</span><span class="p">):</span>
|
||
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">DATA_ID</span><span class="p">)</span>
|
||
|
||
<span class="k">def</span> <span class="nf">image_path</span><span class="p">(</span><span class="n">fig_id</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">FIGURE_ID</span><span class="p">,</span> <span class="n">fig_id</span><span class="p">)</span>
|
||
|
||
<span class="k">def</span> <span class="nf">data_path</span><span class="p">(</span><span class="n">dat_id</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">DATA_ID</span><span class="p">,</span> <span class="n">dat_id</span><span class="p">)</span>
|
||
|
||
<span class="k">def</span> <span class="nf">save_fig</span><span class="p">(</span><span class="n">fig_id</span><span class="p">):</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="n">image_path</span><span class="p">(</span><span class="n">fig_id</span><span class="p">)</span> <span class="o">+</span> <span class="s2">".png"</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s1">'png'</span><span class="p">)</span>
|
||
|
||
<span class="n">infile</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">data_path</span><span class="p">(</span><span class="s2">"EoS.csv"</span><span class="p">),</span><span class="s1">'r'</span><span class="p">)</span>
|
||
|
||
<span class="c1"># Read the EoS data as csv file and organize the data into two arrays with density and energies</span>
|
||
<span class="n">EoS</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="n">names</span><span class="o">=</span><span class="p">(</span><span class="s1">'Density'</span><span class="p">,</span> <span class="s1">'Energy'</span><span class="p">))</span>
|
||
<span class="n">EoS</span><span class="p">[</span><span class="s1">'Energy'</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">EoS</span><span class="p">[</span><span class="s1">'Energy'</span><span class="p">],</span> <span class="n">errors</span><span class="o">=</span><span class="s1">'coerce'</span><span class="p">)</span>
|
||
<span class="n">EoS</span> <span class="o">=</span> <span class="n">EoS</span><span class="o">.</span><span class="n">dropna</span><span class="p">()</span>
|
||
<span class="n">Energies</span> <span class="o">=</span> <span class="n">EoS</span><span class="p">[</span><span class="s1">'Energy'</span><span class="p">]</span>
|
||
<span class="n">Density</span> <span class="o">=</span> <span class="n">EoS</span><span class="p">[</span><span class="s1">'Density'</span><span class="p">]</span>
|
||
<span class="c1"># The design matrix now as function of various polytrops</span>
|
||
|
||
<span class="n">Maxpolydegree</span> <span class="o">=</span> <span class="mi">30</span>
|
||
<span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">Density</span><span class="p">),</span><span class="n">Maxpolydegree</span><span class="p">))</span>
|
||
<span class="n">X</span><span class="p">[:,</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
|
||
<span class="n">estimated_mse_sklearn</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">Maxpolydegree</span><span class="p">)</span>
|
||
<span class="n">polynomial</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">Maxpolydegree</span><span class="p">)</span>
|
||
<span class="n">k</span> <span class="o">=</span><span class="mi">5</span>
|
||
<span class="n">kfold</span> <span class="o">=</span> <span class="n">KFold</span><span class="p">(</span><span class="n">n_splits</span> <span class="o">=</span> <span class="n">k</span><span class="p">)</span>
|
||
|
||
<span class="k">for</span> <span class="n">polydegree</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">Maxpolydegree</span><span class="p">):</span>
|
||
<span class="n">polynomial</span><span class="p">[</span><span class="n">polydegree</span><span class="p">]</span> <span class="o">=</span> <span class="n">polydegree</span>
|
||
<span class="k">for</span> <span class="n">degree</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">polydegree</span><span class="p">):</span>
|
||
<span class="n">X</span><span class="p">[:,</span><span class="n">degree</span><span class="p">]</span> <span class="o">=</span> <span class="n">Density</span><span class="o">**</span><span class="p">(</span><span class="n">degree</span><span class="o">/</span><span class="mf">3.0</span><span class="p">)</span>
|
||
<span class="n">OLS</span> <span class="o">=</span> <span class="n">LinearRegression</span><span class="p">(</span><span class="n">fit_intercept</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
|
||
<span class="c1"># loop over trials in order to estimate the expectation value of the MSE</span>
|
||
<span class="n">estimated_mse_folds</span> <span class="o">=</span> <span class="n">cross_val_score</span><span class="p">(</span><span class="n">OLS</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">Energies</span><span class="p">,</span> <span class="n">scoring</span><span class="o">=</span><span class="s1">'neg_mean_squared_error'</span><span class="p">,</span> <span class="n">cv</span><span class="o">=</span><span class="n">kfold</span><span class="p">)</span>
|
||
<span class="c1">#[:, np.newaxis]</span>
|
||
<span class="n">estimated_mse_sklearn</span><span class="p">[</span><span class="n">polydegree</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="o">-</span><span class="n">estimated_mse_folds</span><span class="p">)</span>
|
||
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">polynomial</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">estimated_mse_sklearn</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">'Test Error'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">'Polynomial degree'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">'log10[MSE]'</span><span class="p">)</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
|
||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
</pre></div>
|
||
</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_405/3817475779.py:63: RuntimeWarning: divide by zero encountered in log10
|
||
plt.plot(polynomial, np.log10(estimated_mse_sklearn), label='Test Error')
|
||
</pre></div>
|
||
</div>
|
||
<img alt="_images/week37_151_1.png" src="_images/week37_151_1.png" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="material-for-the-lab-sessions">
|
||
<h2>Material for the lab sessions<a class="headerlink" href="#material-for-the-lab-sessions" title="Permalink to this headline">¶</a></h2>
|
||
</div>
|
||
<div class="section" id="linking-the-regression-analysis-with-a-statistical-interpretation">
|
||
<h2>Linking the regression analysis with a statistical interpretation<a class="headerlink" href="#linking-the-regression-analysis-with-a-statistical-interpretation" title="Permalink to this headline">¶</a></h2>
|
||
<p>We will now couple the discussions of ordinary least squares, Ridge
|
||
and Lasso regression with a statistical interpretation, that is we
|
||
move from a linear algebra analysis to a statistical analysis. In
|
||
particular, we will focus on what the regularization terms can result
|
||
in. We will amongst other things show that the regularization
|
||
parameter can reduce considerably the variance of the parameters
|
||
<span class="math notranslate nohighlight">\(\beta\)</span>.</p>
|
||
<p>The
|
||
advantage of doing linear regression is that we actually end up with
|
||
analytical expressions for several statistical quantities.<br />
|
||
Standard least squares and Ridge regression allow us to
|
||
derive quantities like the variance and other expectation values in a
|
||
rather straightforward way.</p>
|
||
<p>It is assumed that <span class="math notranslate nohighlight">\(\varepsilon_i
|
||
\sim \mathcal{N}(0, \sigma^2)\)</span> and the <span class="math notranslate nohighlight">\(\varepsilon_{i}\)</span> are
|
||
independent, i.e.:</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\begin{align*}
|
||
\mbox{Cov}(\varepsilon_{i_1},
|
||
\varepsilon_{i_2}) & = \left\{ \begin{array}{lcc} \sigma^2 & \mbox{if}
|
||
& i_1 = i_2, \\ 0 & \mbox{if} & i_1 \not= i_2. \end{array} \right.
|
||
\end{align*}
|
||
\end{split}\]</div>
|
||
<p>The randomness of <span class="math notranslate nohighlight">\(\varepsilon_i\)</span> implies that
|
||
<span class="math notranslate nohighlight">\(\mathbf{y}_i\)</span> is also a random variable. In particular,
|
||
<span class="math notranslate nohighlight">\(\mathbf{y}_i\)</span> is normally distributed, because <span class="math notranslate nohighlight">\(\varepsilon_i \sim
|
||
\mathcal{N}(0, \sigma^2)\)</span> and <span class="math notranslate nohighlight">\(\mathbf{X}_{i,\ast} \, \boldsymbol{\beta}\)</span> is a
|
||
non-random scalar. To specify the parameters of the distribution of
|
||
<span class="math notranslate nohighlight">\(\mathbf{y}_i\)</span> we need to calculate its first two moments.</p>
|
||
<p>Recall that <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span> is a matrix of dimensionality <span class="math notranslate nohighlight">\(n\times p\)</span>. The
|
||
notation above <span class="math notranslate nohighlight">\(\mathbf{X}_{i,\ast}\)</span> means that we are looking at the
|
||
row number <span class="math notranslate nohighlight">\(i\)</span> and perform a sum over all values <span class="math notranslate nohighlight">\(p\)</span>.</p>
|
||
</div>
|
||
<div class="section" id="assumptions-made">
|
||
<h2>Assumptions made<a class="headerlink" href="#assumptions-made" title="Permalink to this headline">¶</a></h2>
|
||
<p>The assumption we have made here can be summarized as (and this is going to be useful when we discuss the bias-variance trade off)
|
||
that there exists a function <span class="math notranslate nohighlight">\(f(\boldsymbol{x})\)</span> and a normal distributed error <span class="math notranslate nohighlight">\(\boldsymbol{\varepsilon}\sim \mathcal{N}(0, \sigma^2)\)</span>
|
||
which describe our data</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{y} = f(\boldsymbol{x})+\boldsymbol{\varepsilon}
|
||
\]</div>
|
||
<p>We approximate this function with our model from the solution of the linear regression equations, that is our
|
||
function <span class="math notranslate nohighlight">\(f\)</span> is approximated by <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}}\)</span> where we want to minimize <span class="math notranslate nohighlight">\((\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\)</span>, our MSE, with</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{\tilde{y}} = \boldsymbol{X}\boldsymbol{\beta}.
|
||
\]</div>
|
||
</div>
|
||
<div class="section" id="expectation-value-and-variance">
|
||
<h2>Expectation value and variance<a class="headerlink" href="#expectation-value-and-variance" title="Permalink to this headline">¶</a></h2>
|
||
<p>We can calculate the expectation value of <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span> for a given element <span class="math notranslate nohighlight">\(i\)</span></p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{align*}
|
||
\mathbb{E}(y_i) & =
|
||
\mathbb{E}(\mathbf{X}_{i, \ast} \, \boldsymbol{\beta}) + \mathbb{E}(\varepsilon_i)
|
||
\, \, \, = \, \, \, \mathbf{X}_{i, \ast} \, \beta,
|
||
\end{align*}
|
||
\]</div>
|
||
<p>while
|
||
its variance is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\begin{align*} \mbox{Var}(y_i) & = \mathbb{E} \{ [y_i
|
||
- \mathbb{E}(y_i)]^2 \} \, \, \, = \, \, \, \mathbb{E} ( y_i^2 ) -
|
||
[\mathbb{E}(y_i)]^2 \\ & = \mathbb{E} [ ( \mathbf{X}_{i, \ast} \,
|
||
\beta + \varepsilon_i )^2] - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 \\ &
|
||
= \mathbb{E} [ ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2 \varepsilon_i
|
||
\mathbf{X}_{i, \ast} \, \boldsymbol{\beta} + \varepsilon_i^2 ] - ( \mathbf{X}_{i,
|
||
\ast} \, \beta)^2 \\ & = ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2
|
||
\mathbb{E}(\varepsilon_i) \mathbf{X}_{i, \ast} \, \boldsymbol{\beta} +
|
||
\mathbb{E}(\varepsilon_i^2 ) - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2
|
||
\\ & = \mathbb{E}(\varepsilon_i^2 ) \, \, \, = \, \, \,
|
||
\mbox{Var}(\varepsilon_i) \, \, \, = \, \, \, \sigma^2.
|
||
\end{align*}
|
||
\end{split}\]</div>
|
||
<p>Hence, <span class="math notranslate nohighlight">\(y_i \sim \mathcal{N}( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta}, \sigma^2)\)</span>, that is <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span> follows a normal distribution with
|
||
mean value <span class="math notranslate nohighlight">\(\boldsymbol{X}\boldsymbol{\beta}\)</span> and variance <span class="math notranslate nohighlight">\(\sigma^2\)</span> (not be confused with the singular values of the SVD).</p>
|
||
</div>
|
||
<div class="section" id="expectation-value-and-variance-for-boldsymbol-beta">
|
||
<h2>Expectation value and variance for <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span><a class="headerlink" href="#expectation-value-and-variance-for-boldsymbol-beta" title="Permalink to this headline">¶</a></h2>
|
||
<p>With the OLS expressions for the optimal parameters <span class="math notranslate nohighlight">\(\boldsymbol{\hat{\beta}}\)</span> we can evaluate the expectation value</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathbb{E}(\boldsymbol{\hat{\beta}}) = \mathbb{E}[ (\mathbf{X}^{\top} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbb{E}[ \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1} \mathbf{X}^{T}\mathbf{X}\boldsymbol{\beta}=\boldsymbol{\beta}.
|
||
\]</div>
|
||
<p>This means that the estimator of the regression parameters is unbiased.</p>
|
||
<p>We can also calculate the variance</p>
|
||
<p>The variance of the optimal value <span class="math notranslate nohighlight">\(\boldsymbol{\hat{\beta}}\)</span> is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\begin{eqnarray*}
|
||
\mbox{Var}(\boldsymbol{\hat{\beta}}) & = & \mathbb{E} \{ [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})] [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})]^{T} \}
|
||
\\
|
||
& = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{y} - \boldsymbol{\beta}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{y} - \boldsymbol{\beta}]^{T} \}
|
||
\\
|
||
% & = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{y}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{y}]^{T} \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
|
||
% \\
|
||
% & = & \mathbb{E} \{ (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{y} \, \mathbf{y}^{T} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
|
||
% \\
|
||
& = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \mathbb{E} \{ \mathbf{y} \, \mathbf{y}^{T} \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
|
||
\\
|
||
& = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \{ \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \mathbf{X}^{T} + \sigma^2 \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
|
||
% \\
|
||
% & = & (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^T \, \mathbf{X}^T \, \mathbf{X} \, (\mathbf{X}^T % \mathbf{X})^{-1}
|
||
% \\
|
||
% & & + \, \, \sigma^2 \, (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, (\mathbf{X}^T \mathbf{X})^{-1} - \boldsymbol{\beta} \boldsymbol{\beta}^T
|
||
\\
|
||
& = & \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} + \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T}
|
||
\, \, \, = \, \, \, \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1},
|
||
\end{eqnarray*}
|
||
\end{split}\]</div>
|
||
<p>where we have used that <span class="math notranslate nohighlight">\(\mathbb{E} (\mathbf{y} \mathbf{y}^{T}) =
|
||
\mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \mathbf{X}^{T} +
|
||
\sigma^2 \, \mathbf{I}_{nn}\)</span>. From <span class="math notranslate nohighlight">\(\mbox{Var}(\boldsymbol{\beta}) = \sigma^2
|
||
\, (\mathbf{X}^{T} \mathbf{X})^{-1}\)</span>, one obtains an estimate of the
|
||
variance of the estimate of the <span class="math notranslate nohighlight">\(j\)</span>-th regression coefficient:
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{\sigma}^2 (\boldsymbol{\beta}_j ) = \boldsymbol{\sigma}^2 [(\mathbf{X}^{T} \mathbf{X})^{-1}]_{jj} \)</span>. This may be used to
|
||
construct a confidence interval for the estimates.</p>
|
||
<p>In a similar way, we can obtain analytical expressions for say the
|
||
expectation values of the parameters <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> and their variance
|
||
when we employ Ridge regression, allowing us again to define a confidence interval.</p>
|
||
<p>It is rather straightforward to show that</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathbb{E} \big[ \hat{\boldsymbol{\beta}}^{\mathrm{Ridge}} \big]=(\mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} (\mathbf{X}^{\top} \mathbf{X})\boldsymbol{\beta}.
|
||
\]</div>
|
||
<p>We see clearly that
|
||
<span class="math notranslate nohighlight">\(\mathbb{E} \big[ \hat{\boldsymbol{\beta}}^{\mathrm{Ridge}} \big] \not= \hat{\boldsymbol{\beta}}^{\mathrm{OLS}}\)</span> for any <span class="math notranslate nohighlight">\(\lambda > 0\)</span>.</p>
|
||
<p>We can also compute the variance as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mbox{Var}[\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}}]=\sigma^2[ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1} \mathbf{X}^{T} \mathbf{X} \{ [ \mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T},
|
||
\]</div>
|
||
<p>and it is easy to see that if the parameter <span class="math notranslate nohighlight">\(\lambda\)</span> goes to infinity then the variance of Ridge parameters <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> goes to zero.</p>
|
||
<p>With this, we can compute the difference</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mbox{Var}[\hat{\boldsymbol{\beta}}^{\mathrm{OLS}}]-\mbox{Var}(\hat{\boldsymbol{\beta}}^{\mathrm{Ridge}})=\sigma^2 [ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}[ 2\lambda\mathbf{I} + \lambda^2 (\mathbf{X}^{T} \mathbf{X})^{-1} ] \{ [ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T}.
|
||
\]</div>
|
||
<p>The difference is non-negative definite since each component of the
|
||
matrix product is non-negative definite.
|
||
This means the variance we obtain with the standard OLS will always for <span class="math notranslate nohighlight">\(\lambda > 0\)</span> be larger than the variance of <span class="math notranslate nohighlight">\(\boldsymbol{\beta}\)</span> obtained with the Ridge estimator. This has interesting consequences when we discuss the so-called bias-variance trade-off below.</p>
|
||
<p>For more discussions of Ridge regression and calculation of averages, <a class="reference external" href="https://arxiv.org/abs/1509.09169">Wessel van Wieringen’s</a> article is highly recommended.</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="exercisesweek37.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">Exercises week 37</p>
|
||
</div>
|
||
</a>
|
||
<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">Exercises week 38</p>
|
||
</div>
|
||
<i class="fas fa-angle-right"></i>
|
||
</a>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
<footer class="footer">
|
||
<p>
|
||
|
||
By Morten Hjorth-Jensen<br/>
|
||
|
||
© Copyright 2021.<br/>
|
||
</p>
|
||
</footer>
|
||
</main>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<script src="_static/js/index.be7d3bbb2ef33a8344ce.js"></script>
|
||
|
||
</body>
|
||
</html> |