2839 lines
145 KiB
HTML
2839 lines
145 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
|
||
<html lang="en" data-content_root="./" >
|
||
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
|
||
<title>5. Resampling Methods — Applied Data Analysis and Machine Learning</title>
|
||
|
||
|
||
|
||
<script data-cfasync="false">
|
||
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
|
||
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
|
||
</script>
|
||
|
||
<!-- Loaded before other Sphinx assets -->
|
||
<link href="_static/styles/theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
|
||
<link href="_static/styles/bootstrap.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
|
||
<link href="_static/styles/pydata-sphinx-theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
|
||
|
||
|
||
<link href="_static/vendor/fontawesome/6.5.2/css/all.min.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
|
||
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-solid-900.woff2" />
|
||
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-brands-400.woff2" />
|
||
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-regular-400.woff2" />
|
||
|
||
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=03e43079" />
|
||
<link rel="stylesheet" type="text/css" href="_static/styles/sphinx-book-theme.css?v=eba8b062" />
|
||
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css?v=13237357" />
|
||
<link rel="stylesheet" type="text/css" href="_static/copybutton.css?v=76b2166b" />
|
||
<link rel="stylesheet" type="text/css" href="_static/mystnb.8ecb98da25f57f5357bf6f572d296f466b2cfe2517ffebfabe82451661e28f02.css?v=6644e6bb" />
|
||
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css?v=4fa983c6" />
|
||
<link rel="stylesheet" type="text/css" href="_static/sphinx-design.min.css?v=95c83b7e" />
|
||
|
||
<!-- Pre-loaded scripts that we'll load fully later -->
|
||
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=dfe6caa3a7d634c4db9b" />
|
||
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=dfe6caa3a7d634c4db9b" />
|
||
<script src="_static/vendor/fontawesome/6.5.2/js/all.min.js?digest=dfe6caa3a7d634c4db9b"></script>
|
||
|
||
<script src="_static/documentation_options.js?v=9eb32ce0"></script>
|
||
<script src="_static/doctools.js?v=9a2dae69"></script>
|
||
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
|
||
<script src="_static/clipboard.min.js?v=a7894cd8"></script>
|
||
<script src="_static/copybutton.js?v=f281be69"></script>
|
||
<script src="_static/scripts/sphinx-book-theme.js?v=887ef09a"></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?v=4a39c7ea"></script>
|
||
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
|
||
<script src="_static/design-tabs.js?v=f930bc37"></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?v=c100c467"></script>
|
||
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</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>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>
|
||
<script>DOCUMENTATION_OPTIONS.pagename = 'chapter3';</script>
|
||
<link rel="index" title="Index" href="genindex.html" />
|
||
<link rel="search" title="Search" href="search.html" />
|
||
<link rel="next" title="6. Logistic Regression" href="chapter4.html" />
|
||
<link rel="prev" title="4. Ridge and Lasso Regression" href="chapter2.html" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||
<meta name="docsearch:language" content="en"/>
|
||
</head>
|
||
|
||
|
||
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
|
||
|
||
|
||
|
||
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
|
||
|
||
<div id="pst-scroll-pixel-helper"></div>
|
||
|
||
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
|
||
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
|
||
|
||
|
||
<input type="checkbox"
|
||
class="sidebar-toggle"
|
||
id="pst-primary-sidebar-checkbox"/>
|
||
<label class="overlay overlay-primary" for="pst-primary-sidebar-checkbox"></label>
|
||
|
||
<input type="checkbox"
|
||
class="sidebar-toggle"
|
||
id="pst-secondary-sidebar-checkbox"/>
|
||
<label class="overlay overlay-secondary" for="pst-secondary-sidebar-checkbox"></label>
|
||
|
||
<div class="search-button__wrapper">
|
||
<div class="search-button__overlay"></div>
|
||
<div class="search-button__search-container">
|
||
<form class="bd-search d-flex align-items-center"
|
||
action="search.html"
|
||
method="get">
|
||
<i class="fa-solid fa-magnifying-glass"></i>
|
||
<input type="search"
|
||
class="form-control"
|
||
name="q"
|
||
id="search-input"
|
||
placeholder="Search this book..."
|
||
aria-label="Search this book..."
|
||
autocomplete="off"
|
||
autocorrect="off"
|
||
autocapitalize="off"
|
||
spellcheck="false"/>
|
||
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
|
||
</form></div>
|
||
</div>
|
||
|
||
<div class="pst-async-banner-revealer d-none">
|
||
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
|
||
</div>
|
||
|
||
|
||
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
|
||
</header>
|
||
|
||
|
||
<div class="bd-container">
|
||
<div class="bd-container__inner bd-page-width">
|
||
|
||
|
||
|
||
<div class="bd-sidebar-primary bd-sidebar">
|
||
|
||
|
||
|
||
<div class="sidebar-header-items sidebar-primary__section">
|
||
|
||
|
||
|
||
|
||
</div>
|
||
|
||
<div class="sidebar-primary-items__start sidebar-primary__section">
|
||
<div class="sidebar-primary-item">
|
||
|
||
|
||
|
||
|
||
|
||
<a class="navbar-brand logo" href="intro.html">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<img src="_static/logo.png" class="logo__image only-light" alt="Applied Data Analysis and Machine Learning - Home"/>
|
||
<script>document.write(`<img src="_static/logo.png" class="logo__image only-dark" alt="Applied Data Analysis and Machine Learning - Home"/>`);</script>
|
||
|
||
|
||
</a></div>
|
||
<div class="sidebar-primary-item">
|
||
|
||
<script>
|
||
document.write(`
|
||
<button class="btn search-button-field search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<i class="fa-solid fa-magnifying-glass"></i>
|
||
<span class="search-button__default-text">Search</span>
|
||
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
|
||
</button>
|
||
`);
|
||
</script></div>
|
||
<div class="sidebar-primary-item"><nav class="bd-links bd-docs-nav" aria-label="Main">
|
||
<div class="bd-toc-item navbar-nav active">
|
||
|
||
<ul class="nav bd-sidenav bd-sidenav__home-link">
|
||
<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">Course setting</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="current 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 current active"><a class="current reference internal" href="#">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="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 Gradient descent</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek37.html">Exercises week 37</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="week37.html">Week 37: Gradient descent methods</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek38.html">Exercises week 38</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="exercisesweek39.html">Exercises week 39</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 6 (midnight), 2025</a></li>
|
||
</ul>
|
||
|
||
</div>
|
||
</nav></div>
|
||
</div>
|
||
|
||
|
||
<div class="sidebar-primary-items__end sidebar-primary__section">
|
||
</div>
|
||
|
||
<div id="rtd-footer-container"></div>
|
||
|
||
|
||
</div>
|
||
|
||
<main id="main-content" class="bd-main" role="main">
|
||
|
||
|
||
|
||
<div class="sbt-scroll-pixel-helper"></div>
|
||
|
||
<div class="bd-content">
|
||
<div class="bd-article-container">
|
||
|
||
<div class="bd-header-article d-print-none">
|
||
<div class="header-article-items header-article__inner">
|
||
|
||
<div class="header-article-items__start">
|
||
|
||
<div class="header-article-item"><button class="sidebar-toggle primary-toggle btn btn-sm" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<span class="fa-solid fa-bars"></span>
|
||
</button></div>
|
||
|
||
</div>
|
||
|
||
|
||
<div class="header-article-items__end">
|
||
|
||
<div class="header-article-item">
|
||
|
||
<div class="article-header-buttons">
|
||
|
||
|
||
|
||
|
||
|
||
<div class="dropdown dropdown-download-buttons">
|
||
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Download this page">
|
||
<i class="fas fa-download"></i>
|
||
</button>
|
||
<ul class="dropdown-menu">
|
||
|
||
|
||
|
||
<li><a href="_sources/chapter3.ipynb" target="_blank"
|
||
class="btn btn-sm btn-download-source-button dropdown-item"
|
||
title="Download source file"
|
||
data-bs-placement="left" data-bs-toggle="tooltip"
|
||
>
|
||
|
||
|
||
<span class="btn__icon-container">
|
||
<i class="fas fa-file"></i>
|
||
</span>
|
||
<span class="btn__text-container">.ipynb</span>
|
||
</a>
|
||
</li>
|
||
|
||
|
||
|
||
|
||
<li>
|
||
<button onclick="window.print()"
|
||
class="btn btn-sm btn-download-pdf-button dropdown-item"
|
||
title="Print to PDF"
|
||
data-bs-placement="left" data-bs-toggle="tooltip"
|
||
>
|
||
|
||
|
||
<span class="btn__icon-container">
|
||
<i class="fas fa-file-pdf"></i>
|
||
</span>
|
||
<span class="btn__text-container">.pdf</span>
|
||
</button>
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
|
||
|
||
|
||
<button onclick="toggleFullScreen()"
|
||
class="btn btn-sm btn-fullscreen-button"
|
||
title="Fullscreen mode"
|
||
data-bs-placement="bottom" data-bs-toggle="tooltip"
|
||
>
|
||
|
||
|
||
<span class="btn__icon-container">
|
||
<i class="fas fa-expand"></i>
|
||
</span>
|
||
|
||
</button>
|
||
|
||
|
||
|
||
<script>
|
||
document.write(`
|
||
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light"></i>
|
||
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark"></i>
|
||
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto"></i>
|
||
</button>
|
||
`);
|
||
</script>
|
||
|
||
|
||
<script>
|
||
document.write(`
|
||
<button class="btn btn-sm pst-navbar-icon search-button search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
|
||
</button>
|
||
`);
|
||
</script>
|
||
<button class="sidebar-toggle secondary-toggle btn btn-sm" title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
|
||
<span class="fa-solid fa-list"></span>
|
||
</button>
|
||
</div></div>
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
|
||
|
||
<div id="jb-print-docs-body" class="onlyprint">
|
||
<h1>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="#introduction">5.1. Introduction</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reminder-on-statistics">5.2. Reminder on Statistics</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#id1">5.3. Resampling methods</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#bootstrap">5.3.1. Bootstrap</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-bias-variance-tradeoff">5.4. The bias-variance tradeoff</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#cross-validation">5.5. Cross-validation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-on-rescaling-data">5.6. More on Rescaling data</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-complicated-example-the-ising-model">5.7. More complicated Example: The Ising model</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercises-and-projects">5.8. Exercises and Projects</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-ordinary-least-square-ols-on-the-franke-function">5.8.1. Exercise: Ordinary Least Square (OLS) on the Franke function</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-bias-variance-trade-off-and-resampling-techniques">5.8.2. Exercise: Bias-variance trade-off and resampling techniques</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-cross-validation-as-resampling-techniques-adding-more-complexity">5.8.3. Exercise: Cross-validation as resampling techniques, adding more complexity</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-ridge-regression-on-the-franke-function-with-resampling">5.8.4. Exercise: Ridge Regression on the Franke function with resampling</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-lasso-regression-on-the-franke-function-with-resampling">5.8.5. Exercise: Lasso Regression on the Franke function with resampling</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-analysis-of-real-data">5.8.6. Exercise: Analysis of real data</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
|
||
<div id="searchbox"></div>
|
||
<article class="bd-article">
|
||
|
||
<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)
|
||
doconce format html chapter3.do.txt --><section class="tex2jax_ignore mathjax_ignore" id="resampling-methods">
|
||
<h1><span class="section-number">5. </span>Resampling Methods<a class="headerlink" href="#resampling-methods" title="Link to this heading">#</a></h1>
|
||
<section id="introduction">
|
||
<h2><span class="section-number">5.1. </span>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">#</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="arabic 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>
|
||
<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>
|
||
<ul class="simple">
|
||
<li><p>Our simulations can be treated as <em>computer experiments</em>. This is particularly the case for Monte Carlo methods</p></li>
|
||
<li><p>The results can be analysed with the same statistical tools as we would use 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>
|
||
</section>
|
||
<section id="reminder-on-statistics">
|
||
<h2><span class="section-number">5.2. </span>Reminder on Statistics<a class="headerlink" href="#reminder-on-statistics" title="Link to this heading">#</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>
|
||
<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{\theta}\)</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>
|
||
<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{\theta}.
|
||
\]</div>
|
||
<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{\theta}) + \mathbb{E}(\varepsilon_i)
|
||
\, \, \, = \, \, \, \mathbf{X}_{i, \ast} \, \theta,
|
||
\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} \,
|
||
\theta + \varepsilon_i )^2] - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\theta})^2 \\ &
|
||
= \mathbb{E} [ ( \mathbf{X}_{i, \ast} \, \boldsymbol{\theta})^2 + 2 \varepsilon_i
|
||
\mathbf{X}_{i, \ast} \, \boldsymbol{\theta} + \varepsilon_i^2 ] - ( \mathbf{X}_{i,
|
||
\ast} \, \theta)^2 \\ & = ( \mathbf{X}_{i, \ast} \, \boldsymbol{\theta})^2 + 2
|
||
\mathbb{E}(\varepsilon_i) \mathbf{X}_{i, \ast} \, \boldsymbol{\theta} +
|
||
\mathbb{E}(\varepsilon_i^2 ) - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\theta})^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{\theta}, \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{\theta}\)</span> and variance <span class="math notranslate nohighlight">\(\sigma^2\)</span> (not be confused with the singular values of the SVD).</p>
|
||
<p>With the OLS expressions for the parameters <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span> we can evaluate the expectation value</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mathbb{E}(\boldsymbol{\theta}) = \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{\theta}=\boldsymbol{\theta}.
|
||
\]</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 <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span> is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\begin{eqnarray*}
|
||
\mbox{Var}(\boldsymbol{\theta}) & = & \mathbb{E} \{ [\boldsymbol{\theta} - \mathbb{E}(\boldsymbol{\theta})] [\boldsymbol{\theta} - \mathbb{E}(\boldsymbol{\theta})]^{T} \}
|
||
\\
|
||
& = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\theta}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\theta}]^{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{\theta} \, \boldsymbol{\theta}^{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{\theta} \, \boldsymbol{\theta}^{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{\theta} \, \boldsymbol{\theta}^{T}
|
||
\\
|
||
& = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \{ \mathbf{X} \, \boldsymbol{\theta} \, \boldsymbol{\theta}^{T} \, \mathbf{X}^{T} + \sigma^2 \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\theta} \, \boldsymbol{\theta}^{T}
|
||
% \\
|
||
% & = & (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, \boldsymbol{\theta} \, \boldsymbol{\theta}^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{\theta} \boldsymbol{\theta}^T
|
||
\\
|
||
& = & \boldsymbol{\theta} \, \boldsymbol{\theta}^{T} + \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\theta} \, \boldsymbol{\theta}^{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{\theta} \, \boldsymbol{\theta}^{T} \, \mathbf{X}^{T} +
|
||
\sigma^2 \, \mathbf{I}_{nn}\)</span>. From <span class="math notranslate nohighlight">\(\mbox{Var}(\boldsymbol{\theta}) = \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{\theta}_j ) = \boldsymbol{\sigma}^2 \sqrt{
|
||
[(\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{\theta}\)</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[ \boldsymbol{\theta}^{\mathrm{Ridge}} \big]=(\mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} (\mathbf{X}^{\top} \mathbf{X})\boldsymbol{\theta}^{\mathrm{OLS}}.
|
||
\]</div>
|
||
<p>We see clearly that
|
||
<span class="math notranslate nohighlight">\(\mathbb{E} \big[ \boldsymbol{\theta}^{\mathrm{Ridge}} \big] \not= \boldsymbol{\theta}^{\mathrm{OLS}}\)</span> for any <span class="math notranslate nohighlight">\(\lambda > 0\)</span>. We say then that the ridge estimator is biased.</p>
|
||
<p>We can also compute the variance as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mbox{Var}[\boldsymbol{\theta}^{\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{\theta}\)</span> goes to zero.</p>
|
||
<p>With this, we can compute the difference</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mbox{Var}[\boldsymbol{\theta}^{\mathrm{OLS}}]-\mbox{Var}(\boldsymbol{\theta}^{\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{\theta}\)</span> obtained with the Ridge estimator. This has interesting consequences when we discuss the so-called bias-variance trade-off below.</p>
|
||
</section>
|
||
<section id="id1">
|
||
<h2><span class="section-number">5.3. </span>Resampling methods<a class="headerlink" href="#id1" title="Link to this heading">#</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 us 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="arabic 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 be 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>
|
||
<p>Two famous
|
||
resampling methods are the <strong>independent bootstrap</strong> and <strong>the jackknife</strong>.</p>
|
||
<p>The jackknife is a special case of the independent bootstrap. Still, the jackknife was made
|
||
popular prior to the independent bootstrap. And as the popularity of
|
||
the independent bootstrap soared, new variants, such as <strong>the dependent bootstrap</strong>.</p>
|
||
<p>The Jackknife and independent bootstrap work for
|
||
independent, identically distributed random variables.
|
||
If these conditions are not
|
||
satisfied, the methods will fail. Yet, it should be said that if the data are
|
||
independent, identically distributed, and we only want to estimate the
|
||
variance of <span class="math notranslate nohighlight">\(\overline{X}\)</span> (which often is the case), then there is no
|
||
need for bootstrapping.</p>
|
||
<p>The Jackknife works by making many replicas of the estimator <span class="math notranslate nohighlight">\(\widehat{\theta}\)</span>.
|
||
The jackknife is a resampling method where we systematically leave out one observation from the vector of observed values <span class="math notranslate nohighlight">\(\boldsymbol{x} = (x_1,x_2,\cdots,X_n)\)</span>.
|
||
Let <span class="math notranslate nohighlight">\(\boldsymbol{x}_i\)</span> denote the vector</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{x}_i = (x_1,x_2,\cdots,x_{i-1},x_{i+1},\cdots,x_n),
|
||
\]</div>
|
||
<p>which equals the vector <span class="math notranslate nohighlight">\(\boldsymbol{x}\)</span> with the exception that observation
|
||
number <span class="math notranslate nohighlight">\(i\)</span> is left out. Using this notation, define
|
||
<span class="math notranslate nohighlight">\(\widehat{\theta}_i\)</span> to be the estimator
|
||
<span class="math notranslate nohighlight">\(\widehat{\theta}\)</span> computed using <span class="math notranslate nohighlight">\(\vec{X}_i\)</span>.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>from numpy import *
|
||
from numpy.random import randint, randn
|
||
from time import time
|
||
|
||
def jackknife(data, stat):
|
||
n = len(data);t = zeros(n); inds = arange(n); t0 = time()
|
||
## 'jackknifing' by leaving out an observation for each i
|
||
for i in range(n):
|
||
t[i] = stat(delete(data,i) )
|
||
|
||
# analysis
|
||
print("Runtime: %g sec" % (time()-t0)); print("Jackknife Statistics :")
|
||
print("original bias std. error")
|
||
print("%8g %14g %15g" % (stat(data),(n-1)*mean(t)/n, (n*var(t))**.5))
|
||
|
||
return t
|
||
|
||
|
||
# Returns mean of data samples
|
||
def stat(data):
|
||
return mean(data)
|
||
|
||
|
||
mu, sigma = 100, 15
|
||
datapoints = 10000
|
||
x = mu + sigma*random.randn(datapoints)
|
||
# jackknife returns the data sample
|
||
t = jackknife(x, stat)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<section id="bootstrap">
|
||
<h3><span class="section-number">5.3.1. </span>Bootstrap<a class="headerlink" href="#bootstrap" title="Link to this heading">#</a></h3>
|
||
<p>Bootstrapping is a nonparametric approach to statistical inference
|
||
that substitutes computation for more traditional distributional
|
||
assumptions and asymptotic results. Bootstrapping offers a number of
|
||
advantages:</p>
|
||
<ol class="arabic 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>Since <span class="math notranslate nohighlight">\(\widehat{\theta} = \widehat{\theta}(\boldsymbol{X})\)</span> is a function of random variables,
|
||
<span class="math notranslate nohighlight">\(\widehat{\theta}\)</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{\theta}\)</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>
|
||
<p>In the case that <span class="math notranslate nohighlight">\(\widehat{\theta}\)</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 straight forward to do this by:</p>
|
||
<ol class="arabic 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{\theta}\)</span> called <span class="math notranslate nohighlight">\(\widehat{\theta}^*\)</span>.</p></li>
|
||
</ol>
|
||
<p>By repeated use of (1) and (2), many
|
||
estimates of <span class="math notranslate nohighlight">\(\widehat{\theta}\)</span> could have been obtained. The
|
||
idea is to use the relative frequency of <span class="math notranslate nohighlight">\(\widehat{\theta}^*\)</span>
|
||
(think of a histogram) as an estimate of <span class="math notranslate nohighlight">\(p(\boldsymbol{t})\)</span>.</p>
|
||
<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>; 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>
|
||
<p>Instead of generating the histogram for the relative
|
||
frequency of the observation <span class="math notranslate nohighlight">\(X_i\)</span>, just draw the values
|
||
<span class="math notranslate nohighlight">\((X_1^*,X_2^*,\cdots,X_n^*)\)</span> with replacement from the vector
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span>.</p>
|
||
<p>The independent bootstrap works like this:</p>
|
||
<ol class="arabic 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{\theta}^*\)</span> by evaluating <span class="math notranslate nohighlight">\(\widehat \theta\)</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 \theta^*\)</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{\theta}^*\)</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
|
||
\theta\)</span>, apply the estimator <span class="math notranslate nohighlight">\(\widehat \sigma^2\)</span> to the values
|
||
<span class="math notranslate nohighlight">\(\widehat \theta^*\)</span>.</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>.
|
||
This theorem plays a central role in understanding why the Bootstrap
|
||
(and other resampling methods) work so well on independent and
|
||
identically distributed variables.</p>
|
||
<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">\(\langle x_i \rangle\)</span>. Each mean value <span class="math notranslate nohighlight">\(\langle x_i \rangle\)</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">\(\langle x_i \rangle=x_i\)</span> in the discussion
|
||
which follows.</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>
|
||
<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>
|
||
<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>
|
||
<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>
|
||
<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</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>
|
||
<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-none notranslate"><div class="highlight"><pre><span></span>%matplotlib inline
|
||
|
||
import numpy as np
|
||
from time import time
|
||
from scipy.stats import norm
|
||
import matplotlib.pyplot as plt
|
||
|
||
# Returns mean of bootstrap samples
|
||
# Bootstrap algorithm
|
||
def bootstrap(data, datapoints):
|
||
t = np.zeros(datapoints)
|
||
n = len(data)
|
||
# non-parametric bootstrap
|
||
for i in range(datapoints):
|
||
t[i] = np.mean(data[np.random.randint(0,n,n)])
|
||
# analysis
|
||
print("Bootstrap Statistics :")
|
||
print("original bias std. error")
|
||
print("%8g %8g %14g %15g" % (np.mean(data), np.std(data),np.mean(t),np.std(t)))
|
||
return t
|
||
|
||
# We set the mean value to 100 and the standard deviation to 15
|
||
mu, sigma = 100, 15
|
||
datapoints = 10000
|
||
# We generate random numbers according to the normal distribution
|
||
x = mu + sigma*np.random.randn(datapoints)
|
||
# bootstrap returns the data sample
|
||
t = bootstrap(x, datapoints)
|
||
</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>
|
||
<p>We plot then the histogram together with a best fit for the data set.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># the histogram of the bootstrapped data (normalized data if density = True)
|
||
n, binsboot, patches = plt.hist(t, 50, density=True, facecolor='red', alpha=0.75)
|
||
# add a 'best fit' line
|
||
y = norm.pdf(binsboot, np.mean(t), np.std(t))
|
||
lt = plt.plot(binsboot, y, 'b', linewidth=1)
|
||
plt.xlabel('x')
|
||
plt.ylabel('Probability')
|
||
plt.grid(True)
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="the-bias-variance-tradeoff">
|
||
<h2><span class="section-number">5.4. </span>The bias-variance tradeoff<a class="headerlink" href="#the-bias-variance-tradeoff" title="Link to this heading">#</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{L}\)</span> consisting of the data
|
||
<span class="math notranslate nohighlight">\(\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}\)</span>.</p>
|
||
<p>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{\theta}\)</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{\theta}\)</span>.</p>
|
||
<p>Thereafter we found the parameters <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span> by optimizing the means squared error via the so-called cost function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{X},\boldsymbol{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
|
||
\]</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 first term represents 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 stochastic 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 class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>import matplotlib.pyplot as plt
|
||
import numpy as np
|
||
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
||
from sklearn.preprocessing import PolynomialFeatures
|
||
from sklearn.model_selection import train_test_split
|
||
from sklearn.pipeline import make_pipeline
|
||
from sklearn.utils import resample
|
||
|
||
np.random.seed(2018)
|
||
|
||
n = 500
|
||
n_boostraps = 100
|
||
degree = 18 # A quite high value, just to show.
|
||
noise = 0.1
|
||
|
||
# Make data set.
|
||
x = np.linspace(-1, 3, n).reshape(-1, 1)
|
||
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2) + np.random.normal(0, 0.1, x.shape)
|
||
|
||
# Hold out some test data that is never used in training.
|
||
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
|
||
|
||
# Combine x transformation and model into one operation.
|
||
# Not neccesary, but convenient.
|
||
model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
|
||
|
||
# The following (m x n_bootstraps) matrix holds the column vectors y_pred
|
||
# for each bootstrap iteration.
|
||
y_pred = np.empty((y_test.shape[0], n_boostraps))
|
||
for i in range(n_boostraps):
|
||
x_, y_ = resample(x_train, y_train)
|
||
|
||
# Evaluate the new model on the same test data each time.
|
||
y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel()
|
||
|
||
# Note: Expectations and variances taken w.r.t. different training
|
||
# data sets, hence the axis=1. Subsequent means are taken across the test data
|
||
# set in order to obtain a total value, but before this we have error/bias/variance
|
||
# calculated per data point in the test set.
|
||
# Note 2: The use of keepdims=True is important in the calculation of bias as this
|
||
# maintains the column vector form. Dropping this yields very unexpected results.
|
||
error = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
|
||
bias = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
|
||
variance = np.mean( np.var(y_pred, axis=1, keepdims=True) )
|
||
print('Error:', error)
|
||
print('Bias^2:', bias)
|
||
print('Var:', variance)
|
||
print('{} >= {} + {} = {}'.format(error, bias, variance, bias+variance))
|
||
|
||
plt.plot(x[::5, :], y[::5, :], label='f(x)')
|
||
plt.scatter(x_test, y_test, label='Data points')
|
||
plt.scatter(x_test, np.mean(y_pred, axis=1), label='Pred')
|
||
plt.legend()
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>import matplotlib.pyplot as plt
|
||
import numpy as np
|
||
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
||
from sklearn.preprocessing import PolynomialFeatures
|
||
from sklearn.model_selection import train_test_split
|
||
from sklearn.pipeline import make_pipeline
|
||
from sklearn.utils import resample
|
||
|
||
np.random.seed(2018)
|
||
|
||
n = 40
|
||
n_boostraps = 100
|
||
maxdegree = 14
|
||
|
||
|
||
# Make data set.
|
||
x = np.linspace(-3, 3, n).reshape(-1, 1)
|
||
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
|
||
error = np.zeros(maxdegree)
|
||
bias = np.zeros(maxdegree)
|
||
variance = np.zeros(maxdegree)
|
||
polydegree = np.zeros(maxdegree)
|
||
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
|
||
|
||
for degree in range(maxdegree):
|
||
model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
|
||
y_pred = np.empty((y_test.shape[0], n_boostraps))
|
||
for i in range(n_boostraps):
|
||
x_, y_ = resample(x_train, y_train)
|
||
y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel()
|
||
|
||
polydegree[degree] = degree
|
||
error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
|
||
bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
|
||
variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )
|
||
print('Polynomial degree:', degree)
|
||
print('Error:', error[degree])
|
||
print('Bias^2:', bias[degree])
|
||
print('Var:', variance[degree])
|
||
print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))
|
||
|
||
plt.plot(polydegree, error, label='Error')
|
||
plt.plot(polydegree, bias, label='bias')
|
||
plt.plot(polydegree, variance, label='Variance')
|
||
plt.legend()
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<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 class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>"""
|
||
============================
|
||
Underfitting vs. Overfitting
|
||
============================
|
||
|
||
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 **underfitting**. A polynomial of degree 4
|
||
approximates the true function almost perfectly. However, for higher degrees
|
||
the model will **overfit** the training data, i.e. it learns the noise of the
|
||
training data.
|
||
We evaluate quantitatively **overfitting** / **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.
|
||
"""
|
||
|
||
print(__doc__)
|
||
|
||
import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
from sklearn.pipeline import Pipeline
|
||
from sklearn.preprocessing import PolynomialFeatures
|
||
from sklearn.linear_model import LinearRegression
|
||
from sklearn.model_selection import cross_val_score
|
||
|
||
|
||
def true_fun(X):
|
||
return np.cos(1.5 * np.pi * X)
|
||
|
||
np.random.seed(0)
|
||
|
||
n_samples = 30
|
||
degrees = [1, 4, 15]
|
||
|
||
X = np.sort(np.random.rand(n_samples))
|
||
y = true_fun(X) + np.random.randn(n_samples) * 0.1
|
||
|
||
plt.figure(figsize=(14, 5))
|
||
for i in range(len(degrees)):
|
||
ax = plt.subplot(1, len(degrees), i + 1)
|
||
plt.setp(ax, xticks=(), yticks=())
|
||
|
||
polynomial_features = PolynomialFeatures(degree=degrees[i],
|
||
include_bias=False)
|
||
linear_regression = LinearRegression()
|
||
pipeline = Pipeline([("polynomial_features", polynomial_features),
|
||
("linear_regression", linear_regression)])
|
||
pipeline.fit(X[:, np.newaxis], y)
|
||
|
||
# Evaluate the models using crossvalidation
|
||
scores = cross_val_score(pipeline, X[:, np.newaxis], y,
|
||
scoring="neg_mean_squared_error", cv=10)
|
||
|
||
X_test = np.linspace(0, 1, 100)
|
||
plt.plot(X_test, pipeline.predict(X_test[:, np.newaxis]), label="Model")
|
||
plt.plot(X_test, true_fun(X_test), label="True function")
|
||
plt.scatter(X, y, edgecolor='b', s=20, label="Samples")
|
||
plt.xlabel("x")
|
||
plt.ylabel("y")
|
||
plt.xlim((0, 1))
|
||
plt.ylim((-2, 2))
|
||
plt.legend(loc="best")
|
||
plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".format(
|
||
degrees[i], -scores.mean(), scores.std()))
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># Common imports
|
||
import os
|
||
import numpy as np
|
||
import pandas as pd
|
||
import matplotlib.pyplot as plt
|
||
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
||
from sklearn.model_selection import train_test_split
|
||
from sklearn.utils import resample
|
||
from sklearn.metrics import mean_squared_error
|
||
# Where to save the figures and data files
|
||
PROJECT_ROOT_DIR = "Results"
|
||
FIGURE_ID = "Results/FigureFiles"
|
||
DATA_ID = "DataFiles/"
|
||
|
||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||
os.mkdir(PROJECT_ROOT_DIR)
|
||
|
||
if not os.path.exists(FIGURE_ID):
|
||
os.makedirs(FIGURE_ID)
|
||
|
||
if not os.path.exists(DATA_ID):
|
||
os.makedirs(DATA_ID)
|
||
|
||
def image_path(fig_id):
|
||
return os.path.join(FIGURE_ID, fig_id)
|
||
|
||
def data_path(dat_id):
|
||
return os.path.join(DATA_ID, dat_id)
|
||
|
||
def save_fig(fig_id):
|
||
plt.savefig(image_path(fig_id) + ".png", format='png')
|
||
|
||
infile = open(data_path("EoS.csv"),'r')
|
||
|
||
# Read the EoS data as csv file and organize the data into two arrays with density and energies
|
||
EoS = pd.read_csv(infile, names=('Density', 'Energy'))
|
||
EoS['Energy'] = pd.to_numeric(EoS['Energy'], errors='coerce')
|
||
EoS = EoS.dropna()
|
||
Energies = EoS['Energy']
|
||
Density = EoS['Density']
|
||
# The design matrix now as function of various polytrops
|
||
|
||
Maxpolydegree = 30
|
||
X = np.zeros((len(Density),Maxpolydegree))
|
||
X[:,0] = 1.0
|
||
testerror = np.zeros(Maxpolydegree)
|
||
trainingerror = np.zeros(Maxpolydegree)
|
||
polynomial = np.zeros(Maxpolydegree)
|
||
|
||
trials = 100
|
||
for polydegree in range(1, Maxpolydegree):
|
||
polynomial[polydegree] = polydegree
|
||
for degree in range(polydegree):
|
||
X[:,degree] = Density**(degree/3.0)
|
||
|
||
# loop over trials in order to estimate the expectation value of the MSE
|
||
testerror[polydegree] = 0.0
|
||
trainingerror[polydegree] = 0.0
|
||
for samples in range(trials):
|
||
x_train, x_test, y_train, y_test = train_test_split(X, Energies, test_size=0.2)
|
||
model = LinearRegression(fit_intercept=False).fit(x_train, y_train)
|
||
ypred = model.predict(x_train)
|
||
ytilde = model.predict(x_test)
|
||
testerror[polydegree] += mean_squared_error(y_test, ytilde)
|
||
trainingerror[polydegree] += mean_squared_error(y_train, ypred)
|
||
|
||
testerror[polydegree] /= trials
|
||
trainingerror[polydegree] /= trials
|
||
print("Degree of polynomial: %3d"% polynomial[polydegree])
|
||
print("Mean squared error on training data: %.8f" % trainingerror[polydegree])
|
||
print("Mean squared error on test data: %.8f" % testerror[polydegree])
|
||
|
||
plt.plot(polynomial, np.log10(trainingerror), label='Training Error')
|
||
plt.plot(polynomial, np.log10(testerror), label='Test Error')
|
||
plt.xlabel('Polynomial degree')
|
||
plt.ylabel('log10[MSE]')
|
||
plt.legend()
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="cross-validation">
|
||
<h2><span class="section-number">5.5. </span>Cross-validation<a class="headerlink" href="#cross-validation" title="Link to this heading">#</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>
|
||
<ul class="simple">
|
||
<li><p>Define a range of interest for the penalty parameter.</p></li>
|
||
<li><p>Divide the data set into training and test set comprising samples <span class="math notranslate nohighlight">\(\{1, \ldots, n\} \setminus i\)</span> and <span class="math notranslate nohighlight">\(\{ i \}\)</span>, respectively.</p></li>
|
||
<li><p>Fit the linear regression model by means of ridge estimation for each <span class="math notranslate nohighlight">\(\lambda\)</span> in the grid using the training set, and the corresponding estimate of the error variance <span class="math notranslate nohighlight">\(\boldsymbol{\sigma}_{-i}^2(\lambda)\)</span>, as</p></li>
|
||
</ul>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{align*}
|
||
\boldsymbol{\theta}_{-i}(\lambda) & = ( \boldsymbol{X}_{-i, \ast}^{T}
|
||
\boldsymbol{X}_{-i, \ast} + \lambda \boldsymbol{I}_{pp})^{-1}
|
||
\boldsymbol{X}_{-i, \ast}^{T} \boldsymbol{y}_{-i}
|
||
\end{align*}
|
||
\]</div>
|
||
<ul class="simple">
|
||
<li><p>Evaluate the prediction performance of these models on the test set by <span class="math notranslate nohighlight">\(\log\{L[y_i, \boldsymbol{X}_{i, \ast}; \boldsymbol{\theta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\}\)</span>. Or, by the prediction error <span class="math notranslate nohighlight">\(|y_i - \boldsymbol{X}_{i, \ast} \boldsymbol{\theta}_{-i}(\lambda)|\)</span>, the relative error, the error squared or the R2 score function.</p></li>
|
||
<li><p>Repeat the first three steps such that each sample plays the role of the test set once.</p></li>
|
||
<li><p>Average the prediction performances of the test sets at each grid point of the penalty bias/parameter. It is an estimate of the prediction performance of the model corresponding to this value of the penalty parameter on novel data. It is defined as</p></li>
|
||
</ul>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{align*}
|
||
\frac{1}{n} \sum_{i = 1}^n \log\{L[y_i, \mathbf{X}_{i, \ast}; \boldsymbol{\theta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\}.
|
||
\end{align*}
|
||
\]</div>
|
||
<p>For the various values of <span class="math notranslate nohighlight">\(k\)</span></p>
|
||
<ol class="arabic 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="arabic simple" start="5">
|
||
<li><p>Summarize the model using the sample of model evaluation scores</p></li>
|
||
</ol>
|
||
<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-none notranslate"><div class="highlight"><pre><span></span>import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
from sklearn.model_selection import KFold
|
||
from sklearn.linear_model import Ridge
|
||
from sklearn.model_selection import cross_val_score
|
||
from sklearn.preprocessing import PolynomialFeatures
|
||
|
||
# A seed just to ensure that the random numbers are the same for every run.
|
||
# Useful for eventual debugging.
|
||
np.random.seed(3155)
|
||
|
||
# Generate the data.
|
||
nsamples = 100
|
||
x = np.random.randn(nsamples)
|
||
y = 3*x**2 + np.random.randn(nsamples)
|
||
|
||
## Cross-validation on Ridge regression using KFold only
|
||
|
||
# Decide degree on polynomial to fit
|
||
poly = PolynomialFeatures(degree = 6)
|
||
|
||
# Decide which values of lambda to use
|
||
nlambdas = 500
|
||
lambdas = np.logspace(-3, 5, nlambdas)
|
||
|
||
# Initialize a KFold instance
|
||
k = 5
|
||
kfold = KFold(n_splits = k)
|
||
|
||
# Perform the cross-validation to estimate MSE
|
||
scores_KFold = np.zeros((nlambdas, k))
|
||
|
||
i = 0
|
||
for lmb in lambdas:
|
||
ridge = Ridge(alpha = lmb)
|
||
j = 0
|
||
for train_inds, test_inds in kfold.split(x):
|
||
xtrain = x[train_inds]
|
||
ytrain = y[train_inds]
|
||
|
||
xtest = x[test_inds]
|
||
ytest = y[test_inds]
|
||
|
||
Xtrain = poly.fit_transform(xtrain[:, np.newaxis])
|
||
ridge.fit(Xtrain, ytrain[:, np.newaxis])
|
||
|
||
Xtest = poly.fit_transform(xtest[:, np.newaxis])
|
||
ypred = ridge.predict(Xtest)
|
||
|
||
scores_KFold[i,j] = np.sum((ypred - ytest[:, np.newaxis])**2)/np.size(ypred)
|
||
|
||
j += 1
|
||
i += 1
|
||
|
||
|
||
estimated_mse_KFold = np.mean(scores_KFold, axis = 1)
|
||
|
||
## Cross-validation using cross_val_score from sklearn along with KFold
|
||
|
||
# kfold is an instance initialized above as:
|
||
# kfold = KFold(n_splits = k)
|
||
|
||
estimated_mse_sklearn = np.zeros(nlambdas)
|
||
i = 0
|
||
for lmb in lambdas:
|
||
ridge = Ridge(alpha = lmb)
|
||
|
||
X = poly.fit_transform(x[:, np.newaxis])
|
||
estimated_mse_folds = cross_val_score(ridge, X, y[:, np.newaxis], scoring='neg_mean_squared_error', cv=kfold)
|
||
|
||
# cross_val_score return an array containing the estimated negative mse for every fold.
|
||
# we have to the the mean of every array in order to get an estimate of the mse of the model
|
||
estimated_mse_sklearn[i] = np.mean(-estimated_mse_folds)
|
||
|
||
i += 1
|
||
|
||
## Plot and compare the slightly different ways to perform cross-validation
|
||
|
||
plt.figure()
|
||
|
||
plt.plot(np.log10(lambdas), estimated_mse_sklearn, label = 'cross_val_score')
|
||
plt.plot(np.log10(lambdas), estimated_mse_KFold, 'r--', label = 'KFold')
|
||
|
||
plt.xlabel('log10(lambda)')
|
||
plt.ylabel('mse')
|
||
|
||
plt.legend()
|
||
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>More examples of the application of cross-validation follow here.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span># Common imports
|
||
import os
|
||
import numpy as np
|
||
import pandas as pd
|
||
import matplotlib.pyplot as plt
|
||
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
||
from sklearn.metrics import mean_squared_error
|
||
from sklearn.model_selection import KFold
|
||
from sklearn.model_selection import cross_val_score
|
||
|
||
|
||
# Where to save the figures and data files
|
||
PROJECT_ROOT_DIR = "Results"
|
||
FIGURE_ID = "Results/FigureFiles"
|
||
DATA_ID = "DataFiles/"
|
||
|
||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||
os.mkdir(PROJECT_ROOT_DIR)
|
||
|
||
if not os.path.exists(FIGURE_ID):
|
||
os.makedirs(FIGURE_ID)
|
||
|
||
if not os.path.exists(DATA_ID):
|
||
os.makedirs(DATA_ID)
|
||
|
||
def image_path(fig_id):
|
||
return os.path.join(FIGURE_ID, fig_id)
|
||
|
||
def data_path(dat_id):
|
||
return os.path.join(DATA_ID, dat_id)
|
||
|
||
def save_fig(fig_id):
|
||
plt.savefig(image_path(fig_id) + ".png", format='png')
|
||
|
||
infile = open(data_path("EoS.csv"),'r')
|
||
|
||
# Read the EoS data as csv file and organize the data into two arrays with density and energies
|
||
EoS = pd.read_csv(infile, names=('Density', 'Energy'))
|
||
EoS['Energy'] = pd.to_numeric(EoS['Energy'], errors='coerce')
|
||
EoS = EoS.dropna()
|
||
Energies = EoS['Energy']
|
||
Density = EoS['Density']
|
||
# The design matrix now as function of various polytrops
|
||
|
||
Maxpolydegree = 30
|
||
X = np.zeros((len(Density),Maxpolydegree))
|
||
X[:,0] = 1.0
|
||
estimated_mse_sklearn = np.zeros(Maxpolydegree)
|
||
polynomial = np.zeros(Maxpolydegree)
|
||
k =5
|
||
kfold = KFold(n_splits = k)
|
||
|
||
for polydegree in range(1, Maxpolydegree):
|
||
polynomial[polydegree] = polydegree
|
||
for degree in range(polydegree):
|
||
X[:,degree] = Density**(degree/3.0)
|
||
OLS = LinearRegression(fit_intercept=False)
|
||
# loop over trials in order to estimate the expectation value of the MSE
|
||
estimated_mse_folds = cross_val_score(OLS, X, Energies, scoring='neg_mean_squared_error', cv=kfold)
|
||
#[:, np.newaxis]
|
||
estimated_mse_sklearn[polydegree] = np.mean(-estimated_mse_folds)
|
||
|
||
plt.plot(polynomial, np.log10(estimated_mse_sklearn), label='Test Error')
|
||
plt.xlabel('Polynomial degree')
|
||
plt.ylabel('log10[MSE]')
|
||
plt.legend()
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Note that we have kept the intercept in the first column of design matrix <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span>. When we call the corresponding <strong>Scikit-Learn</strong> function we need thus to set the intercept to <strong>False</strong>. Libraries like <strong>Scikit-Learn</strong> normally scale the design matrix and do not fit intercept. See the discussions below.</p>
|
||
</section>
|
||
<section id="more-on-rescaling-data">
|
||
<h2><span class="section-number">5.6. </span>More on Rescaling data<a class="headerlink" href="#more-on-rescaling-data" title="Link to this heading">#</a></h2>
|
||
<p>We end this chapter by adding some words on scaling and how to deal with the intercept for regression cases.</p>
|
||
<p>When you are comparing your own code with for example <strong>Scikit-Learn</strong>’s
|
||
library, there are some technicalities to keep in mind. The examples
|
||
here demonstrate some of these aspects with potential pitfalls.</p>
|
||
<p>The discussion here focuses on the role of the intercept, how we can
|
||
set up the design matrix, what scaling we should use and other topics
|
||
which tend confuse us.</p>
|
||
<p>The intercept can be interpreted as the expected value of our
|
||
target/output variables when all other predictors are set to zero.
|
||
Thus, if we cannot assume that the expected outputs/targets are zero
|
||
when all predictors are zero (the columns in the design matrix), it
|
||
may be a bad idea to implement a model which penalizes the intercept.
|
||
Furthermore, in for example Ridge and Lasso regression, the default solutions
|
||
from the library <strong>Scikit-Learn</strong> (when not shrinking <span class="math notranslate nohighlight">\(\theta_0\)</span>) for the unknown parameters
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span>, are derived under the assumption that both <span class="math notranslate nohighlight">\(\boldsymbol{y}\)</span> and
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span> are zero centered, that is we subtract the mean values.</p>
|
||
<p>If our predictors represent different scales, then it is important to
|
||
standardize the design matrix <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span> by subtracting the mean of each
|
||
column from the corresponding column and dividing the column with its
|
||
standard deviation. Most machine learning libraries do this as a default. This means that if you compare your code with the results from a given library,
|
||
the results may differ.</p>
|
||
<p>The
|
||
<a class="reference external" href="https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html">Standardscaler</a>
|
||
function in <strong>Scikit-Learn</strong> does this for us. For the data sets we
|
||
have been studying in our various examples, the data are in many cases
|
||
already scaled and there is no need to scale them. You as a user of different machine learning algorithms, should always perform a
|
||
survey of your data, with a critical assessment of them in case you need to scale the data.</p>
|
||
<p>If you need to scale the data, not doing so will give an <em>unfair</em>
|
||
penalization of the parameters since their magnitude depends on the
|
||
scale of their corresponding predictor.</p>
|
||
<p>Suppose as an example that you
|
||
you have an input variable given by the heights of different persons.
|
||
Human height might be measured in inches or meters or
|
||
kilometers. If measured in kilometers, a standard linear regression
|
||
model with this predictor would probably give a much bigger
|
||
coefficient term, than if measured in millimeters.
|
||
This can clearly lead to problems in evaluating the cost/loss functions.</p>
|
||
<p>Keep in mind that when you transform your data set before training a model, the same transformation needs to be done
|
||
on your eventual new data set before making a prediction. If we translate this into a Python code, it would could be implemented as follows</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>"""
|
||
#Model training, we compute the mean value of y and X
|
||
y_train_mean = np.mean(y_train)
|
||
X_train_mean = np.mean(X_train,axis=0)
|
||
X_train = X_train - X_train_mean
|
||
y_train = y_train - y_train_mean
|
||
|
||
# The we fit our model with the training data
|
||
trained_model = some_model.fit(X_train,y_train)
|
||
|
||
|
||
#Model prediction, we need also to transform our data set used for the prediction.
|
||
X_test = X_test - X_train_mean #Use mean from training data
|
||
y_pred = trained_model(X_test)
|
||
y_pred = y_pred + y_train_mean
|
||
"""
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Let us try to understand what this may imply mathematically when we
|
||
subtract the mean values, also known as <em>zero centering</em>. For
|
||
simplicity, we will focus on ordinary regression, as done in the above example.</p>
|
||
<p>The cost/loss function for regression is</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\theta_0, \theta_1, ... , \theta_{p-1}) = \frac{1}{n}\sum_{i=0}^{n} \left(y_i - \theta_0 - \sum_{j=1}^{p-1} X_{ij}\theta_j\right)^2,.
|
||
\]</div>
|
||
<p>Recall also that we use the squared value. This expression can lead to an
|
||
increased penalty for higher differences between predicted and
|
||
output/target values.</p>
|
||
<p>What we have done is to single out the <span class="math notranslate nohighlight">\(\theta_0\)</span> term in the
|
||
definition of the mean squared error (MSE). The design matrix <span class="math notranslate nohighlight">\(X\)</span>
|
||
does in this case not contain any intercept column. When we take the
|
||
derivative with respect to <span class="math notranslate nohighlight">\(\theta_0\)</span>, we want the derivative to obey</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial \theta_j} = 0,
|
||
\]</div>
|
||
<p>for all <span class="math notranslate nohighlight">\(j\)</span>. For <span class="math notranslate nohighlight">\(\theta_0\)</span> we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\frac{\partial C}{\partial \theta_0} = -\frac{2}{n}\sum_{i=0}^{n-1} \left(y_i - \theta_0 - \sum_{j=1}^{p-1} X_{ij} \theta_j\right).
|
||
\]</div>
|
||
<p>Multiplying away the constant <span class="math notranslate nohighlight">\(2/n\)</span>, we obtain</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\sum_{i=0}^{n-1} \theta_0 = \sum_{i=0}^{n-1}y_i - \sum_{i=0}^{n-1} \sum_{j=1}^{p-1} X_{ij} \theta_j.
|
||
\]</div>
|
||
<p>Let us specialize first to the case where we have only two parameters <span class="math notranslate nohighlight">\(\theta_0\)</span> and <span class="math notranslate nohighlight">\(\theta_1\)</span>.
|
||
Our result for <span class="math notranslate nohighlight">\(\theta_0\)</span> simplifies then to</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
n\theta_0 = \sum_{i=0}^{n-1}y_i - \sum_{i=0}^{n-1} X_{i1} \theta_1.
|
||
\]</div>
|
||
<p>We obtain then</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\theta_0 = \frac{1}{n}\sum_{i=0}^{n-1}y_i - \theta_1\frac{1}{n}\sum_{i=0}^{n-1} X_{i1}.
|
||
\]</div>
|
||
<p>If we define</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mu_{\boldsymbol{x}_1}=\frac{1}{n}\sum_{i=0}^{n-1} X_{i1},
|
||
\]</div>
|
||
<p>and the mean value of the outputs as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mu_y=\frac{1}{n}\sum_{i=0}^{n-1}y_i,
|
||
\]</div>
|
||
<p>we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\theta_0 = \mu_y - \theta_1\mu_{\boldsymbol{x}_1}.
|
||
\]</div>
|
||
<p>In the general case with more parameters than <span class="math notranslate nohighlight">\(\theta_0\)</span> and <span class="math notranslate nohighlight">\(\theta_1\)</span>, we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\theta_0 = \frac{1}{n}\sum_{i=0}^{n-1}y_i - \frac{1}{n}\sum_{i=0}^{n-1}\sum_{j=1}^{p-1} X_{ij}\theta_j.
|
||
\]</div>
|
||
<p>We can rewrite the latter equation as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\theta_0 = \frac{1}{n}\sum_{i=0}^{n-1}y_i - \sum_{j=1}^{p-1} \mu_{\boldsymbol{x}_j}\theta_j,
|
||
\]</div>
|
||
<p>where we have defined</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\mu_{\boldsymbol{x}_j}=\frac{1}{n}\sum_{i=0}^{n-1} X_{ij},
|
||
\]</div>
|
||
<p>the mean value for all elements of the column vector <span class="math notranslate nohighlight">\(\boldsymbol{x}_j\)</span>.</p>
|
||
<p>Replacing <span class="math notranslate nohighlight">\(y_i\)</span> with <span class="math notranslate nohighlight">\(y_i - y_i - \overline{\boldsymbol{y}}\)</span> and centering also our design matrix results in a cost function (in vector-matrix disguise)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{\theta}) = (\boldsymbol{\tilde{y}} - \tilde{X}\boldsymbol{\theta})^T(\boldsymbol{\tilde{y}} - \tilde{X}\boldsymbol{\theta}).
|
||
\]</div>
|
||
<p>If we minimize with respect to <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span> we have then</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\hat{\boldsymbol{\theta}} = (\tilde{X}^T\tilde{X})^{-1}\tilde{X}^T\boldsymbol{\tilde{y}},
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(\boldsymbol{\tilde{y}} = \boldsymbol{y} - \overline{\boldsymbol{y}}\)</span>
|
||
and <span class="math notranslate nohighlight">\(\tilde{X}_{ij} = X_{ij} - \frac{1}{n}\sum_{k=0}^{n-1}X_{kj}\)</span>.</p>
|
||
<p>For Ridge regression we need to add <span class="math notranslate nohighlight">\(\lambda \boldsymbol{\theta}^T\boldsymbol{\theta}\)</span> to the cost function and get then</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\hat{\boldsymbol{\theta}} = (\tilde{X}^T\tilde{X} + \lambda I)^{-1}\tilde{X}^T\boldsymbol{\tilde{y}}.
|
||
\]</div>
|
||
<p>What does this mean? And why do we insist on all this? Let us look at some examples.</p>
|
||
<p>This code shows a simple first-order fit to a data set using the above transformed data, where we consider the role of the intercept first, by either excluding it or including it (<em>code example thanks to Øyvind Sigmundson Schøyen</em>). Here our scaling of the data is done by subtracting the mean values only.
|
||
Note also that we do not split the data into training and test.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
|
||
from sklearn.linear_model import LinearRegression
|
||
|
||
|
||
np.random.seed(2021)
|
||
|
||
def MSE(y_data,y_model):
|
||
n = np.size(y_model)
|
||
return np.sum((y_data-y_model)**2)/n
|
||
|
||
|
||
def fit_theta(X, y):
|
||
return np.linalg.pinv(X.T @ X) @ X.T @ y
|
||
|
||
|
||
true_theta = [2, 0.5, 3.7]
|
||
|
||
x = np.linspace(0, 1, 11)
|
||
y = np.sum(
|
||
np.asarray([x ** p * b for p, b in enumerate(true_theta)]), axis=0
|
||
) + 0.1 * np.random.normal(size=len(x))
|
||
|
||
degree = 3
|
||
X = np.zeros((len(x), degree))
|
||
|
||
# Include the intercept in the design matrix
|
||
for p in range(degree):
|
||
X[:, p] = x ** p
|
||
|
||
theta = fit_theta(X, y)
|
||
|
||
# Intercept is included in the design matrix
|
||
skl = LinearRegression(fit_intercept=False).fit(X, y)
|
||
|
||
print(f"True theta: {true_theta}")
|
||
print(f"Fitted theta: {theta}")
|
||
print(f"Sklearn fitted theta: {skl.coef_}")
|
||
ypredictOwn = X @ theta
|
||
ypredictSKL = skl.predict(X)
|
||
print(f"MSE with intercept column")
|
||
print(MSE(y,ypredictOwn))
|
||
print(f"MSE with intercept column from SKL")
|
||
print(MSE(y,ypredictSKL))
|
||
|
||
|
||
plt.figure()
|
||
plt.scatter(x, y, label="Data")
|
||
plt.plot(x, X @ theta, label="Fit")
|
||
plt.plot(x, skl.predict(X), label="Sklearn (fit_intercept=False)")
|
||
|
||
|
||
# Do not include the intercept in the design matrix
|
||
X = np.zeros((len(x), degree - 1))
|
||
|
||
for p in range(degree - 1):
|
||
X[:, p] = x ** (p + 1)
|
||
|
||
# Intercept is not included in the design matrix
|
||
skl = LinearRegression(fit_intercept=True).fit(X, y)
|
||
|
||
# Use centered values for X and y when computing coefficients
|
||
y_offset = np.average(y, axis=0)
|
||
X_offset = np.average(X, axis=0)
|
||
|
||
theta = fit_theta(X - X_offset, y - y_offset)
|
||
intercept = np.mean(y_offset - X_offset @ theta)
|
||
|
||
print(f"Manual intercept: {intercept}")
|
||
print(f"Fitted theta (wiothout intercept): {theta}")
|
||
print(f"Sklearn intercept: {skl.intercept_}")
|
||
print(f"Sklearn fitted theta (without intercept): {skl.coef_}")
|
||
ypredictOwn = X @ theta
|
||
ypredictSKL = skl.predict(X)
|
||
print(f"MSE with Manual intercept")
|
||
print(MSE(y,ypredictOwn+intercept))
|
||
print(f"MSE with Sklearn intercept")
|
||
print(MSE(y,ypredictSKL))
|
||
|
||
plt.plot(x, X @ theta + intercept, "--", label="Fit (manual intercept)")
|
||
plt.plot(x, skl.predict(X), "--", label="Sklearn (fit_intercept=True)")
|
||
plt.grid()
|
||
plt.legend()
|
||
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>The intercept is the value of our output/target variable
|
||
when all our features are zero and our function crosses the <span class="math notranslate nohighlight">\(y\)</span>-axis (for a one-dimensional case).</p>
|
||
<p>Printing the MSE, we see first that both methods give the same MSE, as
|
||
they should. However, when we move to for example Ridge regression,
|
||
the way we treat the intercept may give a larger or smaller MSE,
|
||
meaning that the MSE can be penalized by the value of the
|
||
intercept. Not including the intercept in the fit, means that the
|
||
regularization term does not include <span class="math notranslate nohighlight">\(\theta_0\)</span>. For different values
|
||
of <span class="math notranslate nohighlight">\(\lambda\)</span>, this may lead to different MSE values.</p>
|
||
<p>To remind the reader, the regularization term, with the intercept in Ridge regression, is given by</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\lambda \vert\vert \boldsymbol{\theta} \vert\vert_2^2 = \lambda \sum_{j=0}^{p-1}\theta_j^2,
|
||
\]</div>
|
||
<p>but when we take out the intercept, this equation becomes</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\lambda \vert\vert \boldsymbol{\theta} \vert\vert_2^2 = \lambda \sum_{j=1}^{p-1}\theta_j^2.
|
||
\]</div>
|
||
<p>For Lasso regression we have</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\lambda \vert\vert \boldsymbol{\theta} \vert\vert_1 = \lambda \sum_{j=1}^{p-1}\vert\theta_j\vert.
|
||
\]</div>
|
||
<p>It means that, when scaling the design matrix and the outputs/targets,
|
||
by subtracting the mean values, we have an optimization problem which
|
||
is not penalized by the intercept. The MSE value can then be smaller
|
||
since it focuses only on the remaining quantities. If we however bring
|
||
back the intercept, we will get a MSE which then contains the
|
||
intercept.</p>
|
||
<p>Armed with this wisdom, we attempt first to simply set the intercept equal to <strong>False</strong> in our implementation of Ridge regression for our well-known vanilla data set.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>import numpy as np
|
||
import pandas as pd
|
||
import matplotlib.pyplot as plt
|
||
from sklearn.model_selection import train_test_split
|
||
from sklearn import linear_model
|
||
|
||
def MSE(y_data,y_model):
|
||
n = np.size(y_model)
|
||
return np.sum((y_data-y_model)**2)/n
|
||
|
||
|
||
# A seed just to ensure that the random numbers are the same for every run.
|
||
# Useful for eventual debugging.
|
||
np.random.seed(3155)
|
||
|
||
n = 100
|
||
x = np.random.rand(n)
|
||
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)
|
||
|
||
Maxpolydegree = 20
|
||
X = np.zeros((n,Maxpolydegree))
|
||
#We include explicitely the intercept column
|
||
for degree in range(Maxpolydegree):
|
||
X[:,degree] = x**degree
|
||
# We split the data in test and training data
|
||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
||
|
||
p = Maxpolydegree
|
||
I = np.eye(p,p)
|
||
# Decide which values of lambda to use
|
||
nlambdas = 6
|
||
MSEOwnRidgePredict = np.zeros(nlambdas)
|
||
MSERidgePredict = np.zeros(nlambdas)
|
||
lambdas = np.logspace(-4, 2, nlambdas)
|
||
for i in range(nlambdas):
|
||
lmb = lambdas[i]
|
||
OwnRidgeTheta = np.linalg.pinv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train
|
||
# Note: we include the intercept column and no scaling
|
||
RegRidge = linear_model.Ridge(lmb,fit_intercept=False)
|
||
RegRidge.fit(X_train,y_train)
|
||
# and then make the prediction
|
||
ytildeOwnRidge = X_train @ OwnRidgeTheta
|
||
ypredictOwnRidge = X_test @ OwnRidgeTheta
|
||
ytildeRidge = RegRidge.predict(X_train)
|
||
ypredictRidge = RegRidge.predict(X_test)
|
||
MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge)
|
||
MSERidgePredict[i] = MSE(y_test,ypredictRidge)
|
||
print("Theta values for own Ridge implementation")
|
||
print(OwnRidgeTheta)
|
||
print("Theta values for Scikit-Learn Ridge implementation")
|
||
print(RegRidge.coef_)
|
||
print("MSE values for own Ridge implementation")
|
||
print(MSEOwnRidgePredict[i])
|
||
print("MSE values for Scikit-Learn Ridge implementation")
|
||
print(MSERidgePredict[i])
|
||
|
||
# Now plot the results
|
||
plt.figure()
|
||
plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE own Ridge Test')
|
||
plt.plot(np.log10(lambdas), MSERidgePredict, 'g', label = 'MSE Ridge Test')
|
||
|
||
plt.xlabel('log10(lambda)')
|
||
plt.ylabel('MSE')
|
||
plt.legend()
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>The results here agree when we force <strong>Scikit-Learn</strong>’s Ridge function to include the first column in our design matrix.
|
||
We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix.
|
||
What happens if we do not include the intercept in our fit?
|
||
Let us see how we can change this code by zero centering.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>import numpy as np
|
||
import pandas as pd
|
||
import matplotlib.pyplot as plt
|
||
from sklearn.model_selection import train_test_split
|
||
from sklearn import linear_model
|
||
from sklearn.preprocessing import StandardScaler
|
||
|
||
def MSE(y_data,y_model):
|
||
n = np.size(y_model)
|
||
return np.sum((y_data-y_model)**2)/n
|
||
# A seed just to ensure that the random numbers are the same for every run.
|
||
# Useful for eventual debugging.
|
||
np.random.seed(315)
|
||
|
||
n = 100
|
||
x = np.random.rand(n)
|
||
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)
|
||
|
||
Maxpolydegree = 20
|
||
X = np.zeros((n,Maxpolydegree-1))
|
||
|
||
for degree in range(1,Maxpolydegree): #No intercept column
|
||
X[:,degree-1] = x**(degree)
|
||
|
||
# We split the data in test and training data
|
||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
||
|
||
#For our own implementation, we will need to deal with the intercept by centering the design matrix and the target variable
|
||
X_train_mean = np.mean(X_train,axis=0)
|
||
#Center by removing mean from each feature
|
||
X_train_scaled = X_train - X_train_mean
|
||
X_test_scaled = X_test - X_train_mean
|
||
#The model intercept (called y_scaler) is given by the mean of the target variable (IF X is centered)
|
||
#Remove the intercept from the training data.
|
||
y_scaler = np.mean(y_train)
|
||
y_train_scaled = y_train - y_scaler
|
||
|
||
p = Maxpolydegree-1
|
||
I = np.eye(p,p)
|
||
# Decide which values of lambda to use
|
||
nlambdas = 6
|
||
MSEOwnRidgePredict = np.zeros(nlambdas)
|
||
MSERidgePredict = np.zeros(nlambdas)
|
||
|
||
lambdas = np.logspace(-4, 2, nlambdas)
|
||
for i in range(nlambdas):
|
||
lmb = lambdas[i]
|
||
OwnRidgeTheta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ (y_train_scaled)
|
||
intercept_ = y_scaler - X_train_mean@OwnRidgeTheta #The intercept can be shifted so the model can predict on uncentered data
|
||
#Add intercept to prediction
|
||
ypredictOwnRidge = X_test_scaled @ OwnRidgeTheta + y_scaler
|
||
RegRidge = linear_model.Ridge(lmb)
|
||
RegRidge.fit(X_train,y_train)
|
||
ypredictRidge = RegRidge.predict(X_test)
|
||
MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge)
|
||
MSERidgePredict[i] = MSE(y_test,ypredictRidge)
|
||
print("Theta values for own Ridge implementation")
|
||
print(OwnRidgeTheta) #Intercept is given by mean of target variable
|
||
print("Theta values for Scikit-Learn Ridge implementation")
|
||
print(RegRidge.coef_)
|
||
print('Intercept from own implementation:')
|
||
print(intercept_)
|
||
print('Intercept from Scikit-Learn Ridge implementation')
|
||
print(RegRidge.intercept_)
|
||
print("MSE values for own Ridge implementation")
|
||
print(MSEOwnRidgePredict[i])
|
||
print("MSE values for Scikit-Learn Ridge implementation")
|
||
print(MSERidgePredict[i])
|
||
|
||
|
||
# Now plot the results
|
||
plt.figure()
|
||
plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'b--', label = 'MSE own Ridge Test')
|
||
plt.plot(np.log10(lambdas), MSERidgePredict, 'g--', label = 'MSE SL Ridge Test')
|
||
plt.xlabel('log10(lambda)')
|
||
plt.ylabel('MSE')
|
||
plt.legend()
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>We see here, when compared to the code which includes explicitely the
|
||
intercept column, that our MSE value is actually smaller. This is
|
||
because the regularization term does not include the intercept value
|
||
<span class="math notranslate nohighlight">\(\theta_0\)</span> in the fitting. This applies to Lasso regularization as
|
||
well. It means that our optimization is now done only with the
|
||
centered matrix and/or vector that enter the fitting procedure. Note
|
||
also that the problem with the intercept occurs mainly in these type
|
||
of polynomial fitting problem.</p>
|
||
<p>The next example is indeed an example where all these discussions about the role of intercept are not present.</p>
|
||
</section>
|
||
<section id="more-complicated-example-the-ising-model">
|
||
<h2><span class="section-number">5.7. </span>More complicated Example: The Ising model<a class="headerlink" href="#more-complicated-example-the-ising-model" title="Link to this heading">#</a></h2>
|
||
<p>The one-dimensional Ising model with nearest neighbor interaction, no
|
||
external field and a constant coupling constant <span class="math notranslate nohighlight">\(J\)</span> is given by</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto1"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
H = -J \sum_{k}^L s_k s_{k + 1},
|
||
\label{_auto1} \tag{1}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(s_i \in \{-1, 1\}\)</span> and <span class="math notranslate nohighlight">\(s_{N + 1} = s_1\)</span>. The number of spins
|
||
in the system is determined by <span class="math notranslate nohighlight">\(L\)</span>. For the one-dimensional system
|
||
there is no phase transition.</p>
|
||
<p>We will look at a system of <span class="math notranslate nohighlight">\(L = 40\)</span> spins with a coupling constant of
|
||
<span class="math notranslate nohighlight">\(J = 1\)</span>. To get enough training data we will generate 10000 states
|
||
with their respective energies.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
from mpl_toolkits.axes_grid1 import make_axes_locatable
|
||
import seaborn as sns
|
||
import scipy.linalg as scl
|
||
from sklearn.model_selection import train_test_split
|
||
import tqdm
|
||
sns.set(color_codes=True)
|
||
cmap_args=dict(vmin=-1., vmax=1., cmap='seismic')
|
||
|
||
L = 40
|
||
n = int(1e4)
|
||
|
||
spins = np.random.choice([-1, 1], size=(n, L))
|
||
J = 1.0
|
||
|
||
energies = np.zeros(n)
|
||
|
||
for i in range(n):
|
||
energies[i] = - J * np.dot(spins[i], np.roll(spins[i], 1))
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Here we use ordinary least squares
|
||
regression to predict the energy for the nearest neighbor
|
||
one-dimensional Ising model on a ring, i.e., the endpoints wrap
|
||
around. We will use linear regression to fit a value for
|
||
the coupling constant to achieve this.</p>
|
||
<p>A more general form for the one-dimensional Ising model is</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto2"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
H = - \sum_j^L \sum_k^L s_j s_k J_{jk}.
|
||
\label{_auto2} \tag{2}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>Here we allow for interactions beyond the nearest neighbors and a state dependent
|
||
coupling constant. This latter expression can be formulated as
|
||
a matrix-product</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto3"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
\boldsymbol{H} = \boldsymbol{X} J,
|
||
\label{_auto3} \tag{3}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(X_{jk} = s_j s_k\)</span> and <span class="math notranslate nohighlight">\(J\)</span> is a matrix which consists of the
|
||
elements <span class="math notranslate nohighlight">\(-J_{jk}\)</span>. This form of writing the energy fits perfectly
|
||
with the form utilized in linear regression, that is</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto4"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
\boldsymbol{y} = \boldsymbol{X}\boldsymbol{\theta} + \boldsymbol{\epsilon},
|
||
\label{_auto4} \tag{4}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>We split the data in training and test data as discussed in the previous example</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>X = np.zeros((n, L ** 2))
|
||
for i in range(n):
|
||
X[i] = np.outer(spins[i], spins[i]).ravel()
|
||
y = energies
|
||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>In the ordinary least squares method we choose the cost function</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto5"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
C(\boldsymbol{X}, \boldsymbol{\theta})= \frac{1}{n}\left\{(\boldsymbol{X}\boldsymbol{\theta} - \boldsymbol{y})^T(\boldsymbol{X}\boldsymbol{\theta} - \boldsymbol{y})\right\}.
|
||
\label{_auto5} \tag{5}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>We then find the extremal point of <span class="math notranslate nohighlight">\(C\)</span> by taking the derivative with respect to <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span> as discussed above.
|
||
This yields the expression for <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span> to be</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{\theta} = \frac{\boldsymbol{X}^T \boldsymbol{y}}{\boldsymbol{X}^T \boldsymbol{X}},
|
||
\]</div>
|
||
<p>which immediately imposes some requirements on <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span> as there must exist
|
||
an inverse of <span class="math notranslate nohighlight">\(\boldsymbol{X}^T \boldsymbol{X}\)</span>. If the expression we are modeling contains an
|
||
intercept, i.e., a constant term, we must make sure that the
|
||
first column of <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span> consists of <span class="math notranslate nohighlight">\(1\)</span>. We do this here</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>X_train_own = np.concatenate(
|
||
(np.ones(len(X_train))[:, np.newaxis], X_train),
|
||
axis=1
|
||
)
|
||
X_test_own = np.concatenate(
|
||
(np.ones(len(X_test))[:, np.newaxis], X_test),
|
||
axis=1
|
||
)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Doing the inversion directly turns out to be a bad idea since the matrix
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{X}^T\boldsymbol{X}\)</span> is singular. An alternative approach is to use the <strong>singular
|
||
value decomposition</strong>. Using the definition of the Moore-Penrose
|
||
pseudoinverse we can write the equation for <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span> as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{\theta} = \boldsymbol{X}^{+}\boldsymbol{y},
|
||
\]</div>
|
||
<p>where the pseudoinverse of <span class="math notranslate nohighlight">\(\boldsymbol{X}\)</span> is given by</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\boldsymbol{X}^{+} = \frac{\boldsymbol{X}^T}{\boldsymbol{X}^T\boldsymbol{X}}.
|
||
\]</div>
|
||
<p>Using singular value decomposition we can decompose the matrix <span class="math notranslate nohighlight">\(\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma} \boldsymbol{V}^T\)</span>,
|
||
where <span class="math notranslate nohighlight">\(\boldsymbol{U}\)</span> and <span class="math notranslate nohighlight">\(\boldsymbol{V}\)</span> are orthogonal(unitary) matrices and <span class="math notranslate nohighlight">\(\boldsymbol{\Sigma}\)</span> contains the singular values (more details below).
|
||
where <span class="math notranslate nohighlight">\(X^{+} = V\Sigma^{+} U^T\)</span>. This reduces the equation for
|
||
<span class="math notranslate nohighlight">\(\omega\)</span> to</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto6"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
\boldsymbol{\theta} = \boldsymbol{V}\boldsymbol{\Sigma}^{+} \boldsymbol{U}^T \boldsymbol{y}.
|
||
\label{_auto6} \tag{6}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>Note that solving this equation by actually doing the pseudoinverse
|
||
(which is what we will do) is not a good idea as this operation scales
|
||
as <span class="math notranslate nohighlight">\(\mathcal{O}(n^3)\)</span>, where <span class="math notranslate nohighlight">\(n\)</span> is the number of elements in a
|
||
general matrix. Instead, doing <span class="math notranslate nohighlight">\(QR\)</span>-factorization and solving the
|
||
linear system as an equation would reduce this down to
|
||
<span class="math notranslate nohighlight">\(\mathcal{O}(n^2)\)</span> operations.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>def ols_svd(x: np.ndarray, y: np.ndarray) -> np.ndarray:
|
||
u, s, v = scl.svd(x)
|
||
return v.T @ scl.pinv(scl.diagsvd(s, u.shape[0], v.shape[0])) @ u.T @ y
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>theta = ols_svd(X_train_own,y_train)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>When extracting the <span class="math notranslate nohighlight">\(J\)</span>-matrix we need to make sure that we remove the intercept, as is done here</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>J = theta[1:].reshape(L, L)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>A way of looking at the coefficients in <span class="math notranslate nohighlight">\(J\)</span> is to plot the matrices as images.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>fig = plt.figure(figsize=(20, 14))
|
||
im = plt.imshow(J, **cmap_args)
|
||
plt.title("OLS", fontsize=18)
|
||
plt.xticks(fontsize=18)
|
||
plt.yticks(fontsize=18)
|
||
cb = fig.colorbar(im)
|
||
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>It is interesting to note that OLS
|
||
considers both <span class="math notranslate nohighlight">\(J_{j, j + 1} = -0.5\)</span> and <span class="math notranslate nohighlight">\(J_{j, j - 1} = -0.5\)</span> as
|
||
valid matrix elements for <span class="math notranslate nohighlight">\(J\)</span>.
|
||
In our discussion below on hyperparameters and Ridge and Lasso regression we will see that
|
||
this problem can be removed, partly and only with Lasso regression.</p>
|
||
<p>In this case our matrix inversion was actually possible. The obvious question now is what is the mathematics behind the SVD?</p>
|
||
<p>Let us now
|
||
focus on Ridge and Lasso regression as well. We repeat some of the
|
||
basic parts of the Ising model and the setup of the training and test
|
||
data. The one-dimensional Ising model with nearest neighbor
|
||
interaction, no external field and a constant coupling constant <span class="math notranslate nohighlight">\(J\)</span> is
|
||
given by</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto7"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
H = -J \sum_{k}^L s_k s_{k + 1},
|
||
\label{_auto7} \tag{7}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(s_i \in \{-1, 1\}\)</span> and <span class="math notranslate nohighlight">\(s_{N + 1} = s_1\)</span>. The number of spins in the system is determined by <span class="math notranslate nohighlight">\(L\)</span>. For the one-dimensional system there is no phase transition.</p>
|
||
<p>We will look at a system of <span class="math notranslate nohighlight">\(L = 40\)</span> spins with a coupling constant of <span class="math notranslate nohighlight">\(J = 1\)</span>. To get enough training data we will generate 10000 states with their respective energies.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
from mpl_toolkits.axes_grid1 import make_axes_locatable
|
||
import seaborn as sns
|
||
import scipy.linalg as scl
|
||
from sklearn.model_selection import train_test_split
|
||
import sklearn.linear_model as skl
|
||
import tqdm
|
||
sns.set(color_codes=True)
|
||
cmap_args=dict(vmin=-1., vmax=1., cmap='seismic')
|
||
|
||
L = 40
|
||
n = int(1e4)
|
||
|
||
spins = np.random.choice([-1, 1], size=(n, L))
|
||
J = 1.0
|
||
|
||
energies = np.zeros(n)
|
||
|
||
for i in range(n):
|
||
energies[i] = - J * np.dot(spins[i], np.roll(spins[i], 1))
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>A more general form for the one-dimensional Ising model is</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto8"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
H = - \sum_j^L \sum_k^L s_j s_k J_{jk}.
|
||
\label{_auto8} \tag{8}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>Here we allow for interactions beyond the nearest neighbors and a more
|
||
adaptive coupling matrix. This latter expression can be formulated as
|
||
a matrix-product on the form</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto9"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
H = X J,
|
||
\label{_auto9} \tag{9}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>where <span class="math notranslate nohighlight">\(X_{jk} = s_j s_k\)</span> and <span class="math notranslate nohighlight">\(J\)</span> is the matrix consisting of the
|
||
elements <span class="math notranslate nohighlight">\(-J_{jk}\)</span>. This form of writing the energy fits perfectly
|
||
with the form utilized in linear regression, viz.</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto10"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
\boldsymbol{y} = \boldsymbol{X}\boldsymbol{\theta} + \boldsymbol{\epsilon}.
|
||
\label{_auto10} \tag{10}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>We organize the data as we did above</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>X = np.zeros((n, L ** 2))
|
||
for i in range(n):
|
||
X[i] = np.outer(spins[i], spins[i]).ravel()
|
||
y = energies
|
||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.96)
|
||
|
||
X_train_own = np.concatenate(
|
||
(np.ones(len(X_train))[:, np.newaxis], X_train),
|
||
axis=1
|
||
)
|
||
|
||
X_test_own = np.concatenate(
|
||
(np.ones(len(X_test))[:, np.newaxis], X_test),
|
||
axis=1
|
||
)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>We will do all fitting with <strong>Scikit-Learn</strong>,</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>clf = skl.LinearRegression().fit(X_train, y_train)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>When extracting the <span class="math notranslate nohighlight">\(J\)</span>-matrix we make sure to remove the intercept</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>J_sk = clf.coef_.reshape(L, L)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>And then we plot the results</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>fig = plt.figure(figsize=(20, 14))
|
||
im = plt.imshow(J_sk, **cmap_args)
|
||
plt.title("LinearRegression from Scikit-learn", fontsize=18)
|
||
plt.xticks(fontsize=18)
|
||
plt.yticks(fontsize=18)
|
||
cb = fig.colorbar(im)
|
||
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>The results agree perfectly with our previous discussion where we used our own code.</p>
|
||
<p>Having explored the ordinary least squares we move on to ridge
|
||
regression. In ridge regression we include a <strong>regularizer</strong>. This
|
||
involves a new cost function which leads to a new estimate for the
|
||
weights <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span>. This results in a penalized regression problem. The
|
||
cost function is given by</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto11"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
C(\boldsymbol{X}, \boldsymbol{\theta}; \lambda) = (\boldsymbol{X}\boldsymbol{\theta} - \boldsymbol{y})^T(\boldsymbol{X}\boldsymbol{\theta} - \boldsymbol{y}) + \lambda \boldsymbol{\theta}^T\boldsymbol{\theta}.
|
||
\label{_auto11} \tag{11}
|
||
\end{equation}
|
||
\]</div>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>_lambda = 0.1
|
||
clf_ridge = skl.Ridge(alpha=_lambda).fit(X_train, y_train)
|
||
J_ridge_sk = clf_ridge.coef_.reshape(L, L)
|
||
fig = plt.figure(figsize=(20, 14))
|
||
im = plt.imshow(J_ridge_sk, **cmap_args)
|
||
plt.title("Ridge from Scikit-learn", fontsize=18)
|
||
plt.xticks(fontsize=18)
|
||
plt.yticks(fontsize=18)
|
||
cb = fig.colorbar(im)
|
||
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
|
||
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>In the <strong>Least Absolute Shrinkage and Selection Operator</strong> (LASSO)-method we get a third cost function.</p>
|
||
<!-- Equation labels as ordinary links -->
|
||
<div id="_auto12"></div>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\begin{equation}
|
||
C(\boldsymbol{X}, \boldsymbol{\theta}; \lambda) = (\boldsymbol{X}\boldsymbol{\theta} - \boldsymbol{y})^T(\boldsymbol{X}\boldsymbol{\theta} - \boldsymbol{y}) + \lambda \sqrt{\boldsymbol{\theta}^T\boldsymbol{\theta}}.
|
||
\label{_auto12} \tag{12}
|
||
\end{equation}
|
||
\]</div>
|
||
<p>Finding the extremal point of this cost function is not so straight-forward as in least squares and ridge. We will therefore rely solely on the function <code class="docutils literal notranslate"><span class="pre">Lasso</span></code> from <strong>Scikit-Learn</strong>.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>clf_lasso = skl.Lasso(alpha=_lambda).fit(X_train, y_train)
|
||
J_lasso_sk = clf_lasso.coef_.reshape(L, L)
|
||
fig = plt.figure(figsize=(20, 14))
|
||
im = plt.imshow(J_lasso_sk, **cmap_args)
|
||
plt.title("Lasso from Scikit-learn", fontsize=18)
|
||
plt.xticks(fontsize=18)
|
||
plt.yticks(fontsize=18)
|
||
cb = fig.colorbar(im)
|
||
cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=18)
|
||
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>It is quite striking how LASSO breaks the symmetry of the coupling
|
||
constant as opposed to ridge and OLS. We get a sparse solution with
|
||
<span class="math notranslate nohighlight">\(J_{j, j + 1} = -1\)</span>.</p>
|
||
<p>We see how the different models perform for a different set of values for <span class="math notranslate nohighlight">\(\lambda\)</span>.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>lambdas = np.logspace(-4, 5, 10)
|
||
|
||
train_errors = {
|
||
"ols_sk": np.zeros(lambdas.size),
|
||
"ridge_sk": np.zeros(lambdas.size),
|
||
"lasso_sk": np.zeros(lambdas.size)
|
||
}
|
||
|
||
test_errors = {
|
||
"ols_sk": np.zeros(lambdas.size),
|
||
"ridge_sk": np.zeros(lambdas.size),
|
||
"lasso_sk": np.zeros(lambdas.size)
|
||
}
|
||
|
||
plot_counter = 1
|
||
|
||
fig = plt.figure(figsize=(32, 54))
|
||
|
||
for i, _lambda in enumerate(tqdm.tqdm(lambdas)):
|
||
for key, method in zip(
|
||
["ols_sk", "ridge_sk", "lasso_sk"],
|
||
[skl.LinearRegression(), skl.Ridge(alpha=_lambda), skl.Lasso(alpha=_lambda)]
|
||
):
|
||
method = method.fit(X_train, y_train)
|
||
|
||
train_errors[key][i] = method.score(X_train, y_train)
|
||
test_errors[key][i] = method.score(X_test, y_test)
|
||
|
||
omega = method.coef_.reshape(L, L)
|
||
|
||
plt.subplot(10, 5, plot_counter)
|
||
plt.imshow(omega, **cmap_args)
|
||
plt.title(r"%s, $\lambda = %.4f$" % (key, _lambda))
|
||
plot_counter += 1
|
||
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>We see that LASSO reaches a good solution for low
|
||
values of <span class="math notranslate nohighlight">\(\lambda\)</span>, but will “wither” when we increase <span class="math notranslate nohighlight">\(\lambda\)</span> too
|
||
much. Ridge is more stable over a larger range of values for
|
||
<span class="math notranslate nohighlight">\(\lambda\)</span>, but eventually also fades away.</p>
|
||
<p>To determine which value of <span class="math notranslate nohighlight">\(\lambda\)</span> is best we plot the accuracy of
|
||
the models when predicting the training and the testing set. We expect
|
||
the accuracy of the training set to be quite good, but if the accuracy
|
||
of the testing set is much lower this tells us that we might be
|
||
subject to an overfit model. The ideal scenario is an accuracy on the
|
||
testing set that is close to the accuracy of the training set.</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>fig = plt.figure(figsize=(20, 14))
|
||
|
||
colors = {
|
||
"ols_sk": "r",
|
||
"ridge_sk": "y",
|
||
"lasso_sk": "c"
|
||
}
|
||
|
||
for key in train_errors:
|
||
plt.semilogx(
|
||
lambdas,
|
||
train_errors[key],
|
||
colors[key],
|
||
label="Train {0}".format(key),
|
||
linewidth=4.0
|
||
)
|
||
|
||
for key in test_errors:
|
||
plt.semilogx(
|
||
lambdas,
|
||
test_errors[key],
|
||
colors[key] + "--",
|
||
label="Test {0}".format(key),
|
||
linewidth=4.0
|
||
)
|
||
plt.legend(loc="best", fontsize=18)
|
||
plt.xlabel(r"$\lambda$", fontsize=18)
|
||
plt.ylabel(r"$R^2$", fontsize=18)
|
||
plt.tick_params(labelsize=18)
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>From the above figure we can see that LASSO with <span class="math notranslate nohighlight">\(\lambda = 10^{-2}\)</span>
|
||
achieves a very good accuracy on the test set. This by far surpasses the
|
||
other models for all values of <span class="math notranslate nohighlight">\(\lambda\)</span>.</p>
|
||
</section>
|
||
<section id="exercises-and-projects">
|
||
<h2><span class="section-number">5.8. </span>Exercises and Projects<a class="headerlink" href="#exercises-and-projects" title="Link to this heading">#</a></h2>
|
||
<p>The main aim of this project is to study in more detail various
|
||
regression methods, including the Ordinary Least Squares (OLS) method,
|
||
The total score is <strong>100</strong> points. Each subtask has its own final score.</p>
|
||
<p>We will first study how to fit polynomials to a specific
|
||
two-dimensional function called <a class="reference external" href="http://www.dtic.mil/dtic/tr/fulltext/u2/a081688.pdf">Franke’s
|
||
function</a>. This
|
||
is a function which has been widely used when testing various
|
||
interpolation and fitting algorithms. Furthermore, after having
|
||
established the model and the method, we will employ resamling
|
||
techniques such as cross-validation and/or bootstrap in order to perform a
|
||
proper assessment of our models. We will also study in detail the
|
||
so-called Bias-Variance trade off.</p>
|
||
<p>The Franke function, which is a weighted sum of four exponentials reads as follows</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[\begin{split}
|
||
\begin{align*}
|
||
f(x,y) &= \frac{3}{4}\exp{\left(-\frac{(9x-2)^2}{4} - \frac{(9y-2)^2}{4}\right)}+\frac{3}{4}\exp{\left(-\frac{(9x+1)^2}{49}- \frac{(9y+1)}{10}\right)} \\
|
||
&+\frac{1}{2}\exp{\left(-\frac{(9x-7)^2}{4} - \frac{(9y-3)^2}{4}\right)} -\frac{1}{5}\exp{\left(-(9x-4)^2 - (9y-7)^2\right) }.
|
||
\end{align*}
|
||
\end{split}\]</div>
|
||
<p>The function will be defined for <span class="math notranslate nohighlight">\(x,y\in [0,1]\)</span>. Our first step will
|
||
be to perform an OLS regression analysis of this function, trying out
|
||
a polynomial fit with an <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> dependence of the form <span class="math notranslate nohighlight">\([x, y,
|
||
x^2, y^2, xy, \dots]\)</span>. We will also include bootstrap first as
|
||
a resampling technique. After that we will include the cross-validation technique. As in homeworks 1 and 2, we can use a uniform
|
||
distribution to set up the arrays of values for <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span>, or as in
|
||
the example below just a set of fixed
|
||
values for <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> with a given step
|
||
size. We will fit a
|
||
function (for example a polynomial) of <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span>. Thereafter we
|
||
will repeat much of the same procedure using the Ridge and Lasso
|
||
regression methods, introducing thus a dependence on the bias
|
||
(penalty) <span class="math notranslate nohighlight">\(\lambda\)</span>.</p>
|
||
<p>Finally we are going to use (real) digital terrain data and try to
|
||
reproduce these data using the same methods. We will also try to go
|
||
beyond the second-order polynomials metioned above and explore
|
||
which polynomial fits the data best.</p>
|
||
<p>The Python code for the Franke function is included here (it performs also a three-dimensional plot of it)</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>from mpl_toolkits.mplot3d import Axes3D
|
||
import matplotlib.pyplot as plt
|
||
from matplotlib import cm
|
||
from matplotlib.ticker import LinearLocator, FormatStrFormatter
|
||
import numpy as np
|
||
from random import random, seed
|
||
|
||
fig = plt.figure()
|
||
ax = fig.gca(projection='3d')
|
||
|
||
# Make data.
|
||
x = np.arange(0, 1, 0.05)
|
||
y = np.arange(0, 1, 0.05)
|
||
x, y = np.meshgrid(x,y)
|
||
|
||
|
||
def FrankeFunction(x,y):
|
||
term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
|
||
term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
|
||
term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
|
||
term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
|
||
return term1 + term2 + term3 + term4
|
||
|
||
|
||
z = FrankeFunction(x, y)
|
||
|
||
# Plot the surface.
|
||
surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm,
|
||
linewidth=0, antialiased=False)
|
||
|
||
# Customize the z axis.
|
||
ax.set_zlim(-0.10, 1.40)
|
||
ax.zaxis.set_major_locator(LinearLocator(10))
|
||
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
|
||
|
||
# Add a color bar which maps values to colors.
|
||
fig.colorbar(surf, shrink=0.5, aspect=5)
|
||
|
||
plt.show()
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<section id="exercise-ordinary-least-square-ols-on-the-franke-function">
|
||
<h3><span class="section-number">5.8.1. </span>Exercise: Ordinary Least Square (OLS) on the Franke function<a class="headerlink" href="#exercise-ordinary-least-square-ols-on-the-franke-function" title="Link to this heading">#</a></h3>
|
||
<p>We will generate our own dataset for a function
|
||
<span class="math notranslate nohighlight">\(\mathrm{FrankeFunction}(x,y)\)</span> with <span class="math notranslate nohighlight">\(x,y \in [0,1]\)</span>. The function
|
||
<span class="math notranslate nohighlight">\(f(x,y)\)</span> is the Franke function. You should explore also the addition
|
||
of an added stochastic noise to this function using the normal
|
||
distribution <span class="math notranslate nohighlight">\(N(0,1)\)</span>.</p>
|
||
<p><em>Write your own code</em> (using either a matrix inversion or a singular
|
||
value decomposition from e.g., <strong>numpy</strong> ) or use your code from
|
||
homeworks 1 and 2 and perform a standard least square regression
|
||
analysis using polynomials in <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> up to fifth order. Find the
|
||
<a class="reference external" href="https://en.wikipedia.org/wiki/Confidence_interval">confidence intervals</a> of the parameters (estimators) <span class="math notranslate nohighlight">\(\theta\)</span> by computing their
|
||
variances, evaluate the Mean Squared error (MSE)</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n}
|
||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||
\]</div>
|
||
<p>and the <span class="math notranslate nohighlight">\(R^2\)</span> score function. If <span class="math notranslate nohighlight">\(\tilde{\hat{y}}_i\)</span> is the predicted
|
||
value of the <span class="math notranslate nohighlight">\(i-th\)</span> sample and <span class="math notranslate nohighlight">\(y_i\)</span> is the corresponding true value,
|
||
then the score <span class="math notranslate nohighlight">\(R^2\)</span> is defined as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||
\]</div>
|
||
<p>where we have defined the mean value of <span class="math notranslate nohighlight">\(\hat{y}\)</span> as</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||
\]</div>
|
||
<p>Your code has to include a scaling of the data (for example by
|
||
subtracting the mean value), and
|
||
a split of the data in training and test data. For this exercise you can
|
||
either write your own code or use for example the function for
|
||
splitting training data provided by the library <strong>Scikit-Learn</strong> (make
|
||
sure you have installed it). This function is called
|
||
<span class="math notranslate nohighlight">\(train\_test\_split\)</span>. <strong>You should present a critical discussion of why and how you have scaled or not scaled the data</strong>.</p>
|
||
<p>It is normal in essentially all Machine Learning studies to split the
|
||
data in a training set and a test set (eventually also an additional
|
||
validation set). There
|
||
is no explicit recipe for how much data should be included as training
|
||
data and say test data. An accepted rule of thumb is to use
|
||
approximately <span class="math notranslate nohighlight">\(2/3\)</span> to <span class="math notranslate nohighlight">\(4/5\)</span> of the data as training data.</p>
|
||
<p>You can easily reuse the solutions to your exercises from week 35 and week 36.</p>
|
||
</section>
|
||
<section id="exercise-bias-variance-trade-off-and-resampling-techniques">
|
||
<h3><span class="section-number">5.8.2. </span>Exercise: Bias-variance trade-off and resampling techniques<a class="headerlink" href="#exercise-bias-variance-trade-off-and-resampling-techniques" title="Link to this heading">#</a></h3>
|
||
<p>Our aim here is to study the bias-variance trade-off by implementing the <strong>bootstrap</strong> resampling technique.</p>
|
||
<p>With a code which does OLS and includes resampling techniques,
|
||
we will now discuss the bias-variance trade-off in the context of
|
||
continuous predictions such as regression. However, many of the
|
||
intuitions and ideas discussed here also carry over to classification
|
||
tasks and basically all Machine Learning algorithms.</p>
|
||
<p>Before you perform an analysis of the bias-variance trade-off on your test data, make
|
||
first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and
|
||
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
|
||
indicate possible regions of low/high bias and variance. You will most likely not get an
|
||
equally smooth curve!</p>
|
||
<p>With this result we move on to the bias-variance trade-off analysis.</p>
|
||
<p>Consider a
|
||
dataset <span class="math notranslate nohighlight">\(\mathcal{L}\)</span> consisting of the data
|
||
<span class="math notranslate nohighlight">\(\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}\)</span>.</p>
|
||
<p>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>Here <span class="math notranslate nohighlight">\(\epsilon\)</span> is normally distributed with mean zero and standard
|
||
deviation <span class="math notranslate nohighlight">\(\sigma^2\)</span>.</p>
|
||
<p>In our derivation of the ordinary least squares method we defined then
|
||
an approximation to the function <span class="math notranslate nohighlight">\(f\)</span> in terms of the parameters
|
||
<span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</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{\theta}\)</span>.</p>
|
||
<p>The parameters <span class="math notranslate nohighlight">\(\boldsymbol{\theta}\)</span> are in turn found by optimizing the means
|
||
squared error via the so-called cost function</p>
|
||
<div class="math notranslate nohighlight">
|
||
\[
|
||
C(\boldsymbol{X},\boldsymbol{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
|
||
\]</div>
|
||
<p>Here the expected value <span class="math notranslate nohighlight">\(\mathbb{E}\)</span> is the sample value.</p>
|
||
<p>Show that you can rewrite this 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>Explain what the terms mean, which one is the bias and which one is
|
||
the variance and discuss their interpretations.</p>
|
||
<p>Perform then a bias-variance analysis of the Franke function by
|
||
studying the MSE value as function of the complexity of your model.</p>
|
||
<p>Discuss the bias and variance trade-off as function
|
||
of your model complexity (the degree of the polynomial) and the number
|
||
of data points, and possibly also your training and test data using the <strong>bootstrap</strong> resampling method.</p>
|
||
<p>Note also that when you calculate the bias, in all applications you don’t know the function values <span class="math notranslate nohighlight">\(f_i\)</span>. You would hence replace them with the actual data points <span class="math notranslate nohighlight">\(y_i\)</span>.</p>
|
||
</section>
|
||
<section id="exercise-cross-validation-as-resampling-techniques-adding-more-complexity">
|
||
<h3><span class="section-number">5.8.3. </span>Exercise: Cross-validation as resampling techniques, adding more complexity<a class="headerlink" href="#exercise-cross-validation-as-resampling-techniques-adding-more-complexity" title="Link to this heading">#</a></h3>
|
||
<p>The aim here is to write your own code for another widely popular
|
||
resampling technique, the so-called cross-validation method. Again,
|
||
before you start with cross-validation approach, you should scale your
|
||
data.</p>
|
||
<p>Implement the <span class="math notranslate nohighlight">\(k\)</span>-fold cross-validation algorithm (write your own
|
||
code) and evaluate again the MSE function resulting
|
||
from the test folds. You can compare your own code with that from
|
||
<strong>Scikit-Learn</strong> if needed.</p>
|
||
<p>Compare the MSE you get from your cross-validation code with the one
|
||
you got from your <strong>bootstrap</strong> code. Comment your results. Try <span class="math notranslate nohighlight">\(5-10\)</span>
|
||
folds. You can also compare your own cross-validation code with the
|
||
one provided by <strong>Scikit-Learn</strong>.</p>
|
||
</section>
|
||
<section id="exercise-ridge-regression-on-the-franke-function-with-resampling">
|
||
<h3><span class="section-number">5.8.4. </span>Exercise: Ridge Regression on the Franke function with resampling<a class="headerlink" href="#exercise-ridge-regression-on-the-franke-function-with-resampling" title="Link to this heading">#</a></h3>
|
||
<p>Write your own code for the Ridge method, either using matrix
|
||
inversion or the singular value decomposition as done in the previous
|
||
exercise. Perform the same bootstrap analysis as in the
|
||
Exercise 2 (for the same polynomials) and the cross-validation in exercise 3 but now for different values of <span class="math notranslate nohighlight">\(\lambda\)</span>. Compare and
|
||
analyze your results with those obtained in exercises 1-3. Study the
|
||
dependence on <span class="math notranslate nohighlight">\(\lambda\)</span>.</p>
|
||
<p>Study also the bias-variance trade-off as function of various values of
|
||
the parameter <span class="math notranslate nohighlight">\(\lambda\)</span>. For the bias-variance trade-off, use the <strong>bootstrap</strong> resampling method. Comment your results.</p>
|
||
</section>
|
||
<section id="exercise-lasso-regression-on-the-franke-function-with-resampling">
|
||
<h3><span class="section-number">5.8.5. </span>Exercise: Lasso Regression on the Franke function with resampling<a class="headerlink" href="#exercise-lasso-regression-on-the-franke-function-with-resampling" title="Link to this heading">#</a></h3>
|
||
<p>This exercise is essentially a repeat of the previous two ones, but now
|
||
with Lasso regression. Write either your own code (difficult and optional) or, in this case,
|
||
you can also use the functionalities of <strong>Scikit-Learn</strong> (recommended).
|
||
Give a
|
||
critical discussion of the three methods and a judgement of which
|
||
model fits the data best. Perform here as well an analysis of the bias-variance trade-off using the <strong>bootstrap</strong> resampling technique and an analysis of the mean squared error using cross-validation.</p>
|
||
</section>
|
||
<section id="exercise-analysis-of-real-data">
|
||
<h3><span class="section-number">5.8.6. </span>Exercise: Analysis of real data<a class="headerlink" href="#exercise-analysis-of-real-data" title="Link to this heading">#</a></h3>
|
||
<p>With our codes functioning and having been tested properly on a
|
||
simpler function we are now ready to look at real data. We will
|
||
essentially repeat in this exercise what was done in exercises 1-5. However, we
|
||
need first to download the data and prepare properly the inputs to our
|
||
codes. We are going to download digital terrain data from the website
|
||
<a class="reference external" href="https://earthexplorer.usgs.gov/">https://earthexplorer.usgs.gov/</a>,</p>
|
||
<p>Or, if you prefer, we have placed selected datafiles at <a class="github reference external" href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2021/Project1/DataFiles">CompPhysics/MachineLearning</a></p>
|
||
<p>In order to obtain data for a specific region, you need to register as
|
||
a user (free) at this website and then decide upon which area you want
|
||
to fetch the digital terrain data from. In order to be able to read
|
||
the data properly, you need to specify that the format should be <strong>SRTM
|
||
Arc-Second Global</strong> and download the data as a <strong>GeoTIF</strong> file. The
|
||
files are then stored in <em>tif</em> format which can be imported into a
|
||
Python program using</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>scipy.misc.imread
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>Here is a simple part of a Python code which reads and plots the data
|
||
from such files</p>
|
||
<div class="cell docutils container">
|
||
<div class="cell_input docutils container">
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>"""
|
||
import numpy as np
|
||
from imageio import imread
|
||
import matplotlib.pyplot as plt
|
||
from mpl_toolkits.mplot3d import Axes3D
|
||
from matplotlib import cm
|
||
|
||
# Load the terrain
|
||
terrain1 = imread('SRTM_data_Norway_1.tif')
|
||
# Show the terrain
|
||
plt.figure()
|
||
plt.title('Terrain over Norway 1')
|
||
plt.imshow(terrain1, cmap='gray')
|
||
plt.xlabel('X')
|
||
plt.ylabel('Y')
|
||
plt.show()
|
||
"""
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>If you should have problems in downloading the digital terrain data,
|
||
we provide two examples under the data folder of project 1. One is
|
||
from a region close to Stavanger in Norway and the other Møsvatn
|
||
Austfjell, again in Norway.
|
||
Feel free to produce your own terrain data.</p>
|
||
<p>Alternatively, if you would like to use another data set, feel free to do so. This could be data close to your reseach area or simply a data set you found interesting. See for example <a class="reference external" href="https://www.kaggle.com/datasets">kaggle.com</a> for examples.</p>
|
||
<p>Our final part deals with the parameterization of your digital terrain
|
||
data (or your own data). We will apply all three methods for linear regression, the same type (or higher order) of polynomial
|
||
approximation and cross-validation as resampling technique to evaluate which
|
||
model fits the data best.</p>
|
||
<p>At the end, you should present a critical evaluation of your results
|
||
and discuss the applicability of these regression methods to the type
|
||
of data presented here (either the terrain data we propose or other data sets).</p>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
|
||
<script type="text/x-thebe-config">
|
||
{
|
||
requestKernel: true,
|
||
binderOptions: {
|
||
repo: "binder-examples/jupyter-stacks-datascience",
|
||
ref: "master",
|
||
},
|
||
codeMirrorConfig: {
|
||
theme: "abcdef",
|
||
mode: "python"
|
||
},
|
||
kernelOptions: {
|
||
name: "python3",
|
||
path: "./."
|
||
},
|
||
predefinedOutput: true
|
||
}
|
||
</script>
|
||
<script>kernelName = 'python3'</script>
|
||
|
||
</article>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<footer class="prev-next-footer d-print-none">
|
||
|
||
<div class="prev-next-area">
|
||
<a class="left-prev"
|
||
href="chapter2.html"
|
||
title="previous page">
|
||
<i class="fa-solid fa-angle-left"></i>
|
||
<div class="prev-next-info">
|
||
<p class="prev-next-subtitle">previous</p>
|
||
<p class="prev-next-title"><span class="section-number">4. </span>Ridge and Lasso Regression</p>
|
||
</div>
|
||
</a>
|
||
<a class="right-next"
|
||
href="chapter4.html"
|
||
title="next page">
|
||
<div class="prev-next-info">
|
||
<p class="prev-next-subtitle">next</p>
|
||
<p class="prev-next-title"><span class="section-number">6. </span>Logistic Regression</p>
|
||
</div>
|
||
<i class="fa-solid fa-angle-right"></i>
|
||
</a>
|
||
</div>
|
||
</footer>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<div class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
|
||
|
||
|
||
<div class="sidebar-secondary-item">
|
||
<div class="page-toc tocsection onthispage">
|
||
<i class="fa-solid fa-list"></i> Contents
|
||
</div>
|
||
<nav class="bd-toc-nav page-toc">
|
||
<ul class="visible nav section-nav flex-column">
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#introduction">5.1. Introduction</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#reminder-on-statistics">5.2. Reminder on Statistics</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#id1">5.3. Resampling methods</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#bootstrap">5.3.1. Bootstrap</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-bias-variance-tradeoff">5.4. The bias-variance tradeoff</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#cross-validation">5.5. Cross-validation</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-on-rescaling-data">5.6. More on Rescaling data</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-complicated-example-the-ising-model">5.7. More complicated Example: The Ising model</a></li>
|
||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercises-and-projects">5.8. Exercises and Projects</a><ul class="nav section-nav flex-column">
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-ordinary-least-square-ols-on-the-franke-function">5.8.1. Exercise: Ordinary Least Square (OLS) on the Franke function</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-bias-variance-trade-off-and-resampling-techniques">5.8.2. Exercise: Bias-variance trade-off and resampling techniques</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-cross-validation-as-resampling-techniques-adding-more-complexity">5.8.3. Exercise: Cross-validation as resampling techniques, adding more complexity</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-ridge-regression-on-the-franke-function-with-resampling">5.8.4. Exercise: Ridge Regression on the Franke function with resampling</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-lasso-regression-on-the-franke-function-with-resampling">5.8.5. Exercise: Lasso Regression on the Franke function with resampling</a></li>
|
||
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-analysis-of-real-data">5.8.6. Exercise: Analysis of real data</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</nav></div>
|
||
|
||
</div></div>
|
||
|
||
|
||
</div>
|
||
<footer class="bd-footer-content">
|
||
|
||
<div class="bd-footer-content__inner container">
|
||
|
||
<div class="footer-item">
|
||
|
||
<p class="component-author">
|
||
By Morten Hjorth-Jensen
|
||
</p>
|
||
|
||
</div>
|
||
|
||
<div class="footer-item">
|
||
|
||
|
||
<p class="copyright">
|
||
|
||
© Copyright 2023.
|
||
<br/>
|
||
|
||
</p>
|
||
|
||
</div>
|
||
|
||
<div class="footer-item">
|
||
|
||
</div>
|
||
|
||
<div class="footer-item">
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</footer>
|
||
|
||
|
||
</main>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Scripts loaded after <body> so the DOM is not blocked -->
|
||
<script src="_static/scripts/bootstrap.js?digest=dfe6caa3a7d634c4db9b"></script>
|
||
<script src="_static/scripts/pydata-sphinx-theme.js?digest=dfe6caa3a7d634c4db9b"></script>
|
||
|
||
<footer class="bd-footer">
|
||
</footer>
|
||
</body>
|
||
</html> |