From aac7c1cac6e21e6f2e3f4c140f77dff13d82e198 Mon Sep 17 00:00:00 2001 From: Morten Hjorth-Jensen Date: Mon, 19 Aug 2024 14:47:12 +0200 Subject: [PATCH] added recording --- doc/pub/week34/html/._week34-bs001.html | 2 +- doc/pub/week34/html/week34-reveal.html | 2 +- doc/pub/week34/html/week34-solarized.html | 2 +- doc/pub/week34/html/week34.html | 2 +- doc/pub/week34/ipynb/ipynb-week34-src.tar.gz | Bin 103516 -> 103516 bytes doc/pub/week34/ipynb/week34.ipynb | 1875 ++++++++++++------ doc/src/week34/week34.do.txt | 2 +- 7 files changed, 1258 insertions(+), 627 deletions(-) diff --git a/doc/pub/week34/html/._week34-bs001.html b/doc/pub/week34/html/._week34-bs001.html index 965ab77f6..c34036df4 100644 --- a/doc/pub/week34/html/._week34-bs001.html +++ b/doc/pub/week34/html/._week34-bs001.html @@ -359,7 +359,7 @@ MathJax.Hub.Config({
  • On Mondays we have a regular lecture which will be organized as a mix of active learning sessions and regular lectures. These lectures/active learning sessions start at 1015am and end at 12pm and serve the aims of giving an overview over various topics as well as solving specific problems. These lectures will also be recorded.
  • The labs are also available till 6pm Tuesdays and Wednesdays. Videos and learning material with reading suggestions will be made available before each week starts.

    diff --git a/doc/pub/week34/html/week34-reveal.html b/doc/pub/week34/html/week34-reveal.html index 19a0b31dd..8f79f9e8e 100644 --- a/doc/pub/week34/html/week34-reveal.html +++ b/doc/pub/week34/html/week34-reveal.html @@ -211,7 +211,7 @@ MathJax.Hub.Config({

  • On Mondays we have a regular lecture which will be organized as a mix of active learning sessions and regular lectures. These lectures/active learning sessions start at 1015am and end at 12pm and serve the aims of giving an overview over various topics as well as solving specific problems. These lectures will also be recorded.
  • diff --git a/doc/pub/week34/html/week34-solarized.html b/doc/pub/week34/html/week34-solarized.html index 88f3ee275..6b8c411cf 100644 --- a/doc/pub/week34/html/week34-solarized.html +++ b/doc/pub/week34/html/week34-solarized.html @@ -311,7 +311,7 @@ MathJax.Hub.Config({

  • On Mondays we have a regular lecture which will be organized as a mix of active learning sessions and regular lectures. These lectures/active learning sessions start at 1015am and end at 12pm and serve the aims of giving an overview over various topics as well as solving specific problems. These lectures will also be recorded.
  • The labs are also available till 6pm Tuesdays and Wednesdays. Videos and learning material with reading suggestions will be made available before each week starts.

    diff --git a/doc/pub/week34/html/week34.html b/doc/pub/week34/html/week34.html index 05c9236f0..55d132967 100644 --- a/doc/pub/week34/html/week34.html +++ b/doc/pub/week34/html/week34.html @@ -388,7 +388,7 @@ MathJax.Hub.Config({
  • On Mondays we have a regular lecture which will be organized as a mix of active learning sessions and regular lectures. These lectures/active learning sessions start at 1015am and end at 12pm and serve the aims of giving an overview over various topics as well as solving specific problems. These lectures will also be recorded.
  • The labs are also available till 6pm Tuesdays and Wednesdays. Videos and learning material with reading suggestions will be made available before each week starts.

    diff --git a/doc/pub/week34/ipynb/ipynb-week34-src.tar.gz b/doc/pub/week34/ipynb/ipynb-week34-src.tar.gz index cb4dada3beb1dc49d667b1b7465b088689a2e863..278714b9a7362f4e7bf20cead8eaf117875269e9 100644 GIT binary patch delta 21 dcmcb!g6+-\n", @@ -12,8 +14,10 @@ }, { "cell_type": "markdown", - "id": "45b52799", - "metadata": {}, + "id": "8bab7c7c", + "metadata": { + "editable": true + }, "source": [ "# Week 34: Introduction to the course, Logistics and Practicalities\n", "**Morten Hjorth-Jensen**, Department of Physics and Center for Computing in Science Education, University of Oslo, Norway and Department of Physics and Astronomy and Facility for Rare Isotope Beams, Michigan State University, USA\n", @@ -23,8 +27,10 @@ }, { "cell_type": "markdown", - "id": "ef4cb19e", - "metadata": {}, + "id": "11f9dddd", + "metadata": { + "editable": true + }, "source": [ "## Overview of first week\n", "\n", @@ -40,15 +46,17 @@ "\n", "4. On Mondays we have a regular lecture which will be organized as a mix of active learning sessions and regular lectures. These lectures/active learning sessions start at 1015am and end at 12pm and serve the aims of giving an overview over various topics as well as solving specific problems. These lectures will also be recorded.\n", "\n", - " * [Link to recording of lecture TBA](https://youtu.be/)\n", + " * [Link to recording of lecture](https://youtu.be/82IPtCrzbhs)\n", "\n", "The labs are also available till 6pm Tuesdays and Wednesdays. Videos and learning material with reading suggestions will be made available before each week starts." ] }, { "cell_type": "markdown", - "id": "1e6b1300", - "metadata": {}, + "id": "cf4f78c4", + "metadata": { + "editable": true + }, "source": [ "## Schedule first week\n", "\n", @@ -61,8 +69,10 @@ }, { "cell_type": "markdown", - "id": "31d1f84c", - "metadata": {}, + "id": "c604d1f4", + "metadata": { + "editable": true + }, "source": [ "## Lectures and ComputerLab\n", "\n", @@ -83,8 +93,10 @@ }, { "cell_type": "markdown", - "id": "53b48ed9", - "metadata": {}, + "id": "f2636e32", + "metadata": { + "editable": true + }, "source": [ "## Communication channels\n", "\n", @@ -95,8 +107,10 @@ }, { "cell_type": "markdown", - "id": "b14cbd93", - "metadata": {}, + "id": "2830d8c8", + "metadata": { + "editable": true + }, "source": [ "## Course Format\n", "\n", @@ -115,8 +129,10 @@ }, { "cell_type": "markdown", - "id": "d9cab2bb", - "metadata": {}, + "id": "e60f3b4c", + "metadata": { + "editable": true + }, "source": [ "## Teachers\n", "\n", @@ -143,8 +159,10 @@ }, { "cell_type": "markdown", - "id": "b7eb2935", - "metadata": {}, + "id": "026a9c30", + "metadata": { + "editable": true + }, "source": [ "## Deadlines for projects (tentative)\n", "\n", @@ -159,8 +177,10 @@ }, { "cell_type": "markdown", - "id": "3b3ac4d2", - "metadata": {}, + "id": "c9b44e2c", + "metadata": { + "editable": true + }, "source": [ "## Grading\n", "\n", @@ -185,8 +205,10 @@ }, { "cell_type": "markdown", - "id": "c98a488d", - "metadata": {}, + "id": "f055bfa3", + "metadata": { + "editable": true + }, "source": [ "## Reading material\n", "\n", @@ -201,8 +223,10 @@ }, { "cell_type": "markdown", - "id": "8bd55d29", - "metadata": {}, + "id": "d4570711", + "metadata": { + "editable": true + }, "source": [ "## Main textbooks\n", "\n", @@ -216,8 +240,10 @@ }, { "cell_type": "markdown", - "id": "ff004362", - "metadata": {}, + "id": "1b9f32e0", + "metadata": { + "editable": true + }, "source": [ "## Other popular texts\n", "\n", @@ -238,8 +264,10 @@ }, { "cell_type": "markdown", - "id": "dc1e0ada", - "metadata": {}, + "id": "53bd6717", + "metadata": { + "editable": true + }, "source": [ "## Reading suggestions week 34\n", "\n", @@ -248,8 +276,10 @@ }, { "cell_type": "markdown", - "id": "2c27fef6", - "metadata": {}, + "id": "c9050be0", + "metadata": { + "editable": true + }, "source": [ "## Prerequisites\n", "\n", @@ -266,8 +296,10 @@ }, { "cell_type": "markdown", - "id": "9b95a77b", - "metadata": {}, + "id": "af50e661", + "metadata": { + "editable": true + }, "source": [ "## Topics covered in this course: Statistical analysis and optimization of data\n", "\n", @@ -282,8 +314,10 @@ }, { "cell_type": "markdown", - "id": "40dfedc0", - "metadata": {}, + "id": "f4913bf5", + "metadata": { + "editable": true + }, "source": [ "## Statistical analysis and optimization of data\n", "\n", @@ -305,8 +339,10 @@ }, { "cell_type": "markdown", - "id": "e3e88448", - "metadata": {}, + "id": "53bb230c", + "metadata": { + "editable": true + }, "source": [ "## Machine Learning\n", "\n", @@ -325,8 +361,10 @@ }, { "cell_type": "markdown", - "id": "b6180e4d", - "metadata": {}, + "id": "ae8411cf", + "metadata": { + "editable": true + }, "source": [ "## Deep learning methods\n", "\n", @@ -347,8 +385,10 @@ }, { "cell_type": "markdown", - "id": "5a6d7473", - "metadata": {}, + "id": "8c4ac8e2", + "metadata": { + "editable": true + }, "source": [ "## Extremely useful tools, strongly recommended\n", "\n", @@ -361,8 +401,10 @@ }, { "cell_type": "markdown", - "id": "8d5bc22f", - "metadata": {}, + "id": "466cf1ce", + "metadata": { + "editable": true + }, "source": [ "## Other courses on Data science and Machine Learning at UiO\n", "\n", @@ -381,8 +423,10 @@ }, { "cell_type": "markdown", - "id": "9f5ffd20", - "metadata": {}, + "id": "6a9068e9", + "metadata": { + "editable": true + }, "source": [ "## Other courses on Data science and Machine Learning at UiO, contn\n", "\n", @@ -397,8 +441,10 @@ }, { "cell_type": "markdown", - "id": "b4ca04fe", - "metadata": {}, + "id": "7a29701d", + "metadata": { + "editable": true + }, "source": [ "## Learning outcomes\n", "\n", @@ -425,8 +471,10 @@ }, { "cell_type": "markdown", - "id": "c1f93b8a", - "metadata": {}, + "id": "6c99f65a", + "metadata": { + "editable": true + }, "source": [ "## Types of Machine Learning\n", "\n", @@ -452,8 +500,10 @@ }, { "cell_type": "markdown", - "id": "7d5e895c", - "metadata": {}, + "id": "43c0f6a4", + "metadata": { + "editable": true + }, "source": [ "## Essential elements of ML\n", "\n", @@ -468,8 +518,10 @@ }, { "cell_type": "markdown", - "id": "d4959411", - "metadata": {}, + "id": "1d8c0f5d", + "metadata": { + "editable": true + }, "source": [ "## An optimization/minimization problem\n", "\n", @@ -478,8 +530,10 @@ }, { "cell_type": "markdown", - "id": "d88e8937", - "metadata": {}, + "id": "c187373e", + "metadata": { + "editable": true + }, "source": [ "## A Frequentist approach to data analysis\n", "\n", @@ -511,8 +565,10 @@ }, { "cell_type": "markdown", - "id": "4f31ca57", - "metadata": {}, + "id": "a3c19f0a", + "metadata": { + "editable": true + }, "source": [ "## What is a good model?\n", "\n", @@ -539,8 +595,10 @@ }, { "cell_type": "markdown", - "id": "6ab2b818", - "metadata": {}, + "id": "b5634c69", + "metadata": { + "editable": true + }, "source": [ "## What is a good model? Can we define it?\n", "\n", @@ -568,8 +626,10 @@ }, { "cell_type": "markdown", - "id": "4cca3e62", - "metadata": {}, + "id": "887400b7", + "metadata": { + "editable": true + }, "source": [ "## Software and needed installations\n", "\n", @@ -605,8 +665,10 @@ }, { "cell_type": "markdown", - "id": "848f89b3", - "metadata": {}, + "id": "e846a6ce", + "metadata": { + "editable": true + }, "source": [ "## Python installers\n", "\n", @@ -636,8 +698,10 @@ }, { "cell_type": "markdown", - "id": "30bb69ca", - "metadata": {}, + "id": "d766a84a", + "metadata": { + "editable": true + }, "source": [ "## Useful Python libraries\n", "Here we list several useful Python libraries we strongly recommend (if you use anaconda many of these are already there)\n", @@ -671,8 +735,10 @@ }, { "cell_type": "markdown", - "id": "a77a9aae", - "metadata": {}, + "id": "c9ddae14", + "metadata": { + "editable": true + }, "source": [ "## Installing R, C++, cython or Julia\n", "\n", @@ -693,8 +759,10 @@ }, { "cell_type": "markdown", - "id": "aaeda8ee", - "metadata": {}, + "id": "b58a98f8", + "metadata": { + "editable": true + }, "source": [ "## Installing R, C++, cython, Numba etc\n", "\n", @@ -718,16 +786,20 @@ }, { "cell_type": "markdown", - "id": "2ad20fcd", - "metadata": {}, + "id": "6a36c754", + "metadata": { + "editable": true + }, "source": [ " pycod jupyter nbconvert filename.ipynb --to latex \n" ] }, { "cell_type": "markdown", - "id": "3cc76f8f", - "metadata": {}, + "id": "4ceb128e", + "metadata": { + "editable": true + }, "source": [ "And to add more versatility, the Python package [SymPy](http://www.sympy.org/en/index.html) is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) and is entirely written in Python. \n", "\n", @@ -736,8 +808,10 @@ }, { "cell_type": "markdown", - "id": "9186c180", - "metadata": {}, + "id": "21e2bd89", + "metadata": { + "editable": true + }, "source": [ "## Numpy examples and Important Matrix and vector handling packages\n", "\n", @@ -755,8 +829,10 @@ }, { "cell_type": "markdown", - "id": "fbe7f0de", - "metadata": {}, + "id": "b8594574", + "metadata": { + "editable": true + }, "source": [ "## Numpy and arrays\n", "[Numpy](http://www.numpy.org/) provides an easy way to handle arrays in Python. The standard way to import this library is as" @@ -765,8 +841,11 @@ { "cell_type": "code", "execution_count": 1, - "id": "f4ba7a28", - "metadata": {}, + "id": "aa70673f", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np" @@ -774,8 +853,10 @@ }, { "cell_type": "markdown", - "id": "0f92ea37", - "metadata": {}, + "id": "252006d1", + "metadata": { + "editable": true + }, "source": [ "Here follows a simple example where we set up an array of ten elements, all determined by random numbers drawn according to the normal distribution," ] @@ -783,8 +864,11 @@ { "cell_type": "code", "execution_count": 2, - "id": "e7a41329", - "metadata": {}, + "id": "21352777", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "n = 10\n", @@ -794,8 +878,10 @@ }, { "cell_type": "markdown", - "id": "269bd917", - "metadata": {}, + "id": "5f7e70c4", + "metadata": { + "editable": true + }, "source": [ "We defined a vector $x$ with $n=10$ elements with its values given by the Normal distribution $N(0,1)$.\n", "Another alternative is to declare a vector as follows" @@ -804,8 +890,11 @@ { "cell_type": "code", "execution_count": 3, - "id": "a44ab51e", - "metadata": {}, + "id": "b4beb609", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -815,8 +904,10 @@ }, { "cell_type": "markdown", - "id": "3365c5f4", - "metadata": {}, + "id": "7f214e1e", + "metadata": { + "editable": true + }, "source": [ "Here we have defined a vector with three elements, with $x_0=1$, $x_1=2$ and $x_2=3$. Note that both Python and C++\n", "start numbering array elements from $0$ and on. This means that a vector with $n$ elements has a sequence of entities $x_0, x_1, x_2, \\dots, x_{n-1}$. We could also let (recommended) Numpy to compute the logarithms of a specific array as" @@ -825,8 +916,11 @@ { "cell_type": "code", "execution_count": 4, - "id": "e7fe287c", - "metadata": {}, + "id": "056d9fe3", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -836,8 +930,10 @@ }, { "cell_type": "markdown", - "id": "edbfa557", - "metadata": {}, + "id": "f847f316", + "metadata": { + "editable": true + }, "source": [ "In the last example we used Numpy's unary function $np.log$. This function is\n", "highly tuned to compute array elements since the code is vectorized\n", @@ -851,8 +947,11 @@ { "cell_type": "code", "execution_count": 5, - "id": "66aa46db", - "metadata": {}, + "id": "86f28ab0", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -865,8 +964,10 @@ }, { "cell_type": "markdown", - "id": "618daef7", - "metadata": {}, + "id": "7ee73a04", + "metadata": { + "editable": true + }, "source": [ "We note that our code is much longer already and we need to import the **log** function from the **math** module. \n", "The attentive reader will also notice that the output is $[1, 1, 2]$. Python interprets automagically our numbers as integers (like the **automatic** keyword in C++). To change this we could define our array elements to be double precision numbers as" @@ -875,8 +976,11 @@ { "cell_type": "code", "execution_count": 6, - "id": "30378e7d", - "metadata": {}, + "id": "855646f8", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -886,8 +990,10 @@ }, { "cell_type": "markdown", - "id": "75ecd295", - "metadata": {}, + "id": "736bf09b", + "metadata": { + "editable": true + }, "source": [ "or simply write them as double precision numbers (Python uses 64 bits as default for floating point type variables), that is" ] @@ -895,8 +1001,11 @@ { "cell_type": "code", "execution_count": 7, - "id": "3be0e9f1", - "metadata": {}, + "id": "2e6e3fda", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -906,8 +1015,10 @@ }, { "cell_type": "markdown", - "id": "34db27b7", - "metadata": {}, + "id": "3331092b", + "metadata": { + "editable": true + }, "source": [ "To check the number of bytes (remember that one byte contains eight bits for double precision variables), you can use simple use the **itemsize** functionality (the array $x$ is actually an object which inherits the functionalities defined in Numpy) as" ] @@ -915,8 +1026,11 @@ { "cell_type": "code", "execution_count": 8, - "id": "a7f623de", - "metadata": {}, + "id": "4bb2f543", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -926,8 +1040,10 @@ }, { "cell_type": "markdown", - "id": "74aa5734", - "metadata": {}, + "id": "a7f0a897", + "metadata": { + "editable": true + }, "source": [ "## Matrices in Python\n", "\n", @@ -939,8 +1055,11 @@ { "cell_type": "code", "execution_count": 9, - "id": "511aad98", - "metadata": {}, + "id": "4da90bd2", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -950,8 +1069,10 @@ }, { "cell_type": "markdown", - "id": "1b13f1f5", - "metadata": {}, + "id": "d793cd0a", + "metadata": { + "editable": true + }, "source": [ "If we use the **shape** function we would get $(3, 3)$ as output, that is verifying that our matrix is a $3\\times 3$ matrix. We can slice the matrix and print for example the first column (Python organized matrix elements in a row-major order, see below) as" ] @@ -959,8 +1080,11 @@ { "cell_type": "code", "execution_count": 10, - "id": "727497fc", - "metadata": {}, + "id": "9cdcb676", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -971,8 +1095,10 @@ }, { "cell_type": "markdown", - "id": "5340edcf", - "metadata": {}, + "id": "dfb37c2b", + "metadata": { + "editable": true + }, "source": [ "We can continue this was by printing out other columns or rows. The example here prints out the second column" ] @@ -980,8 +1106,11 @@ { "cell_type": "code", "execution_count": 11, - "id": "b02f087b", - "metadata": {}, + "id": "f46726a9", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -992,8 +1121,10 @@ }, { "cell_type": "markdown", - "id": "08a1816a", - "metadata": {}, + "id": "c2bd0735", + "metadata": { + "editable": true + }, "source": [ "Numpy contains many other functionalities that allow us to slice, subdivide etc etc arrays. We strongly recommend that you look up the [Numpy website for more details](http://www.numpy.org/). Useful functions when defining a matrix are the **np.zeros** function which declares a matrix of a given dimension and sets all elements to zero" ] @@ -1001,8 +1132,11 @@ { "cell_type": "code", "execution_count": 12, - "id": "65630355", - "metadata": {}, + "id": "2c9ef1d8", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -1014,8 +1148,10 @@ }, { "cell_type": "markdown", - "id": "2d900153", - "metadata": {}, + "id": "c82f780e", + "metadata": { + "editable": true + }, "source": [ "or initializing all elements to" ] @@ -1023,8 +1159,11 @@ { "cell_type": "code", "execution_count": 13, - "id": "eb73a41d", - "metadata": {}, + "id": "fd0bfd99", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -1036,8 +1175,10 @@ }, { "cell_type": "markdown", - "id": "475d032f", - "metadata": {}, + "id": "013c1566", + "metadata": { + "editable": true + }, "source": [ "or as unitarily distributed random numbers (see the material on random number generators in the statistics part)" ] @@ -1045,8 +1186,11 @@ { "cell_type": "code", "execution_count": 14, - "id": "75e1338a", - "metadata": {}, + "id": "fce23d83", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -1058,8 +1202,10 @@ }, { "cell_type": "markdown", - "id": "569d5c0b", - "metadata": {}, + "id": "b9d15dcb", + "metadata": { + "editable": true + }, "source": [ "As we will see throughout these lectures, there are several extremely useful functionalities in Numpy.\n", "As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors\n", @@ -1068,8 +1214,10 @@ }, { "cell_type": "markdown", - "id": "7843e50f", - "metadata": {}, + "id": "af1e9127", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\Sigma} = \\begin{bmatrix} \\sigma_{xx} & \\sigma_{xy} & \\sigma_{xz} \\\\\n", @@ -1081,16 +1229,20 @@ }, { "cell_type": "markdown", - "id": "644713ca", - "metadata": {}, + "id": "7edee47e", + "metadata": { + "editable": true + }, "source": [ "where for example" ] }, { "cell_type": "markdown", - "id": "416f7608", - "metadata": {}, + "id": "31fd8ad6", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\sigma_{xy} =\\frac{1}{n} \\sum_{i=0}^{n-1}(x_i- \\overline{x})(y_i- \\overline{y}).\n", @@ -1099,8 +1251,10 @@ }, { "cell_type": "markdown", - "id": "4219192a", - "metadata": {}, + "id": "fd401935", + "metadata": { + "editable": true + }, "source": [ "The Numpy function **np.cov** calculates the covariance elements using the factor $1/(n-1)$ instead of $1/n$ since it assumes we do not have the exact mean values. \n", "The following simple function uses the **np.vstack** function which takes each vector of dimension $1\\times n$ and produces a $3\\times n$ matrix $\\boldsymbol{W}$" @@ -1108,8 +1262,10 @@ }, { "cell_type": "markdown", - "id": "4f5643f8", - "metadata": {}, + "id": "ad16a020", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{W} = \\begin{bmatrix} x_0 & x_1 & x_2 & \\dots & x_{n-2} & x_{n-1} \\\\\n", @@ -1121,8 +1277,10 @@ }, { "cell_type": "markdown", - "id": "f6584cf1", - "metadata": {}, + "id": "5049bef6", + "metadata": { + "editable": true + }, "source": [ "which in turn is converted into into the $3\\times 3$ covariance matrix\n", "$\\boldsymbol{\\Sigma}$ via the Numpy function **np.cov()**. We note that we can also calculate\n", @@ -1134,8 +1292,11 @@ { "cell_type": "code", "execution_count": 15, - "id": "75602f0d", - "metadata": {}, + "id": "22c72388", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# Importing various packages\n", @@ -1158,8 +1319,11 @@ { "cell_type": "code", "execution_count": 16, - "id": "5ad697d4", - "metadata": {}, + "id": "6a8a2910", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "%matplotlib inline\n", @@ -1179,8 +1343,10 @@ }, { "cell_type": "markdown", - "id": "032b26ff", - "metadata": {}, + "id": "bb63303b", + "metadata": { + "editable": true + }, "source": [ "## Meet the Pandas\n", "\n", @@ -1203,8 +1369,11 @@ { "cell_type": "code", "execution_count": 17, - "id": "a787e973", - "metadata": {}, + "id": "492d1fa7", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import pandas as pd\n", @@ -1220,8 +1389,10 @@ }, { "cell_type": "markdown", - "id": "3e2f2ba0", - "metadata": {}, + "id": "0fa528ac", + "metadata": { + "editable": true + }, "source": [ "In the above we have imported **pandas** with the shorthand **pd**, the latter has become the standard way we import **pandas**. We make then a list of various variables\n", "and reorganize the aboves lists into a **DataFrame** and then print out a neat table with specific column labels as *Name*, *place of birth* and *date of birth*.\n", @@ -1232,8 +1403,11 @@ { "cell_type": "code", "execution_count": 18, - "id": "a6e39994", - "metadata": {}, + "id": "8ba2f86d", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "data_pandas = pd.DataFrame(data,index=['Frodo','Bilbo','Aragorn','Sam'])\n", @@ -1242,8 +1416,10 @@ }, { "cell_type": "markdown", - "id": "8ffd3842", - "metadata": {}, + "id": "c3e978a8", + "metadata": { + "editable": true + }, "source": [ "Thereafter we display the content of the row which begins with the index **Aragorn**" ] @@ -1251,8 +1427,11 @@ { "cell_type": "code", "execution_count": 19, - "id": "b3814c6d", - "metadata": {}, + "id": "46eda90b", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "display(data_pandas.loc['Aragorn'])" @@ -1260,8 +1439,10 @@ }, { "cell_type": "markdown", - "id": "35bafbd2", - "metadata": {}, + "id": "9693c106", + "metadata": { + "editable": true + }, "source": [ "We can easily append data to this, for example" ] @@ -1269,8 +1450,11 @@ { "cell_type": "code", "execution_count": 20, - "id": "8db7bed6", - "metadata": {}, + "id": "2f61798f", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "new_hobbit = {'First Name': [\"Peregrin\"],\n", @@ -1284,8 +1468,10 @@ }, { "cell_type": "markdown", - "id": "d9385413", - "metadata": {}, + "id": "6919ce58", + "metadata": { + "editable": true + }, "source": [ "Here are other examples where we use the **DataFrame** functionality to handle arrays, now with more interesting features for us, namely numbers. We set up a matrix \n", "of dimensionality $10\\times 5$ and compute the mean value and standard deviation of each column. Similarly, we can perform mathematial operations like squaring the matrix elements and many other operations." @@ -1294,8 +1480,11 @@ { "cell_type": "code", "execution_count": 21, - "id": "6dce18c2", - "metadata": {}, + "id": "390f6da5", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -1315,8 +1504,10 @@ }, { "cell_type": "markdown", - "id": "97d91af7", - "metadata": {}, + "id": "a16a595c", + "metadata": { + "editable": true + }, "source": [ "Thereafter we can select specific columns only and plot final results" ] @@ -1324,8 +1515,11 @@ { "cell_type": "code", "execution_count": 22, - "id": "e0dca4c1", - "metadata": {}, + "id": "e97df19e", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "df.columns = ['First', 'Second', 'Third', 'Fourth', 'Fifth']\n", @@ -1350,8 +1544,10 @@ }, { "cell_type": "markdown", - "id": "cdc2ab8a", - "metadata": {}, + "id": "bf733440", + "metadata": { + "editable": true + }, "source": [ "We can produce a $4\\times 4$ matrix" ] @@ -1359,8 +1555,11 @@ { "cell_type": "code", "execution_count": 23, - "id": "36ae1ade", - "metadata": {}, + "id": "899958b9", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "b = np.arange(16).reshape((4,4))\n", @@ -1371,8 +1570,10 @@ }, { "cell_type": "markdown", - "id": "c140669f", - "metadata": {}, + "id": "bf5090aa", + "metadata": { + "editable": true + }, "source": [ "and many other operations. \n", "\n", @@ -1386,8 +1587,10 @@ }, { "cell_type": "markdown", - "id": "d458b4bc", - "metadata": {}, + "id": "9117879b", + "metadata": { + "editable": true + }, "source": [ "## Pandas AI\n", "\n", @@ -1396,8 +1599,10 @@ }, { "cell_type": "markdown", - "id": "f9520546", - "metadata": {}, + "id": "a714932a", + "metadata": { + "editable": true + }, "source": [ "### Simple linear regression model using **scikit-learn**\n", "\n", @@ -1425,8 +1630,10 @@ }, { "cell_type": "markdown", - "id": "77976987", - "metadata": {}, + "id": "058e760f", + "metadata": { + "editable": true + }, "source": [ "$$\n", "y = 2x+N(0,1),\n", @@ -1435,8 +1642,10 @@ }, { "cell_type": "markdown", - "id": "8ac95ae4", - "metadata": {}, + "id": "28cf8076", + "metadata": { + "editable": true + }, "source": [ "where $N(0,1)$ represents random numbers generated by the normal\n", "distribution. From **Scikit-Learn** we import then the\n", @@ -1461,8 +1670,11 @@ { "cell_type": "code", "execution_count": 24, - "id": "3327deec", - "metadata": {}, + "id": "be5e4c97", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# Importing various packages\n", @@ -1488,8 +1700,10 @@ }, { "cell_type": "markdown", - "id": "8e39785b", - "metadata": {}, + "id": "8a4f45e1", + "metadata": { + "editable": true + }, "source": [ "This example serves several aims. It allows us to demonstrate several\n", "aspects of data analysis and later machine learning algorithms. The\n", @@ -1503,8 +1717,10 @@ }, { "cell_type": "markdown", - "id": "2a78a280", - "metadata": {}, + "id": "9cb57477", + "metadata": { + "editable": true + }, "source": [ "$$\n", "y = 10x+0.01 \\times N(0,1),\n", @@ -1513,8 +1729,10 @@ }, { "cell_type": "markdown", - "id": "c4864965", - "metadata": {}, + "id": "3132e173", + "metadata": { + "editable": true + }, "source": [ "where $x$ is defined as before. Does the fit look better? Indeed, by\n", "reducing the role of the noise given by the normal distribution we see immediately that\n", @@ -1532,8 +1750,10 @@ }, { "cell_type": "markdown", - "id": "5c0838b1", - "metadata": {}, + "id": "2a099e88", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\chi^2 = \\frac{1}{n}\n", @@ -1543,8 +1763,10 @@ }, { "cell_type": "markdown", - "id": "02383309", - "metadata": {}, + "id": "423110a6", + "metadata": { + "editable": true + }, "source": [ "where $\\sigma_i^2$ is the variance (to be defined later) of the entry\n", "$y_i$. We may not know the explicit value of $\\sigma_i^2$, it serves\n", @@ -1572,8 +1794,10 @@ }, { "cell_type": "markdown", - "id": "eac58725", - "metadata": {}, + "id": "7d400630", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\epsilon_{\\mathrm{relative}}= \\frac{\\vert \\boldsymbol{y} -\\boldsymbol{\\tilde{y}}\\vert}{\\vert \\boldsymbol{y}\\vert}.\n", @@ -1582,8 +1806,10 @@ }, { "cell_type": "markdown", - "id": "827e0ed3", - "metadata": {}, + "id": "e87827d6", + "metadata": { + "editable": true + }, "source": [ "The squared cost function results in an arithmetic mean-unbiased\n", "estimator, and the absolute-value cost function results in a\n", @@ -1598,8 +1824,11 @@ { "cell_type": "code", "execution_count": 25, - "id": "8f20f97a", - "metadata": {}, + "id": "6f0b4be6", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np\n", @@ -1622,8 +1851,10 @@ }, { "cell_type": "markdown", - "id": "40e0f1fb", - "metadata": {}, + "id": "6775b987", + "metadata": { + "editable": true + }, "source": [ "Depending on the parameter in front of the normal distribution, we may\n", "have a small or larger relative error. Try to play around with\n", @@ -1642,8 +1873,11 @@ { "cell_type": "code", "execution_count": 26, - "id": "85fe3b38", - "metadata": {}, + "id": "2cf9597a", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import numpy as np \n", @@ -1677,8 +1911,10 @@ }, { "cell_type": "markdown", - "id": "c5340def", - "metadata": {}, + "id": "05d1604b", + "metadata": { + "editable": true + }, "source": [ "The function **coef** gives us the parameter $\\beta$ of our fit while **intercept** yields \n", "$\\alpha$. Depending on the constant in front of the normal distribution, we get values near or far from $\\alpha =2$ and $\\beta =5$. Try to play around with different parameters in front of the normal distribution. The function **meansquarederror** gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as" @@ -1686,8 +1922,10 @@ }, { "cell_type": "markdown", - "id": "83e89ce5", - "metadata": {}, + "id": "8b6456ba", + "metadata": { + "editable": true + }, "source": [ "$$\n", "MSE(\\boldsymbol{y},\\boldsymbol{\\tilde{y}}) = \\frac{1}{n}\n", @@ -1697,8 +1935,10 @@ }, { "cell_type": "markdown", - "id": "05a2a70c", - "metadata": {}, + "id": "4568378d", + "metadata": { + "editable": true + }, "source": [ "The smaller the value, the better the fit. Ideally we would like to\n", "have an MSE equal zero. The attentive reader has probably recognized\n", @@ -1716,8 +1956,10 @@ }, { "cell_type": "markdown", - "id": "edfe1145", - "metadata": {}, + "id": "14ce11a0", + "metadata": { + "editable": true + }, "source": [ "$$\n", "R^2(\\boldsymbol{y}, \\tilde{\\boldsymbol{y}}) = 1 - \\frac{\\sum_{i=0}^{n - 1} (y_i - \\tilde{y}_i)^2}{\\sum_{i=0}^{n - 1} (y_i - \\bar{y})^2},\n", @@ -1726,16 +1968,20 @@ }, { "cell_type": "markdown", - "id": "b63b854d", - "metadata": {}, + "id": "a5402b39", + "metadata": { + "editable": true + }, "source": [ "where we have defined the mean value of $\\boldsymbol{y}$ as" ] }, { "cell_type": "markdown", - "id": "b72ca737", - "metadata": {}, + "id": "7747ab89", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\bar{y} = \\frac{1}{n} \\sum_{i=0}^{n - 1} y_i.\n", @@ -1744,8 +1990,10 @@ }, { "cell_type": "markdown", - "id": "da0dd094", - "metadata": {}, + "id": "2431d79e", + "metadata": { + "editable": true + }, "source": [ "Another quantity taht we will meet again in our discussions of regression analysis is \n", " the mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the $l1$-norm loss. In our discussion above we presented the relative error.\n", @@ -1754,8 +2002,10 @@ }, { "cell_type": "markdown", - "id": "55ad6e1f", - "metadata": {}, + "id": "38d92292", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\text{MAE}(\\boldsymbol{y}, \\boldsymbol{\\tilde{y}}) = \\frac{1}{n} \\sum_{i=0}^{n-1} \\left| y_i - \\tilde{y}_i \\right|.\n", @@ -1764,8 +2014,10 @@ }, { "cell_type": "markdown", - "id": "307edc23", - "metadata": {}, + "id": "9911396c", + "metadata": { + "editable": true + }, "source": [ "We present the \n", "squared logarithmic (quadratic) error" @@ -1773,8 +2025,10 @@ }, { "cell_type": "markdown", - "id": "ddcee8d8", - "metadata": {}, + "id": "47044124", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\text{MSLE}(\\boldsymbol{y}, \\boldsymbol{\\tilde{y}}) = \\frac{1}{n} \\sum_{i=0}^{n - 1} (\\log_e (1 + y_i) - \\log_e (1 + \\tilde{y}_i) )^2,\n", @@ -1783,8 +2037,10 @@ }, { "cell_type": "markdown", - "id": "a4aa116d", - "metadata": {}, + "id": "9ca2e35a", + "metadata": { + "editable": true + }, "source": [ "where $\\log_e (x)$ stands for the natural logarithm of $x$. This error\n", "estimate is best to use when targets having exponential growth, such\n", @@ -1803,8 +2059,10 @@ }, { "cell_type": "markdown", - "id": "91b0a8f6", - "metadata": {}, + "id": "7dd117f7", + "metadata": { + "editable": true + }, "source": [ "$$\n", "H_{\\delta}(\\boldsymbol{a})=\\left\\{\\begin{array}{cc}\\frac{1}{2} \\boldsymbol{a}^{2}& \\text{for }|\\boldsymbol{a}|\\leq \\delta\\\\ \\delta (|\\boldsymbol{a}|-\\frac{1}{2}\\delta ),&\\text{otherwise}.\\end{array}\\right.\n", @@ -1813,8 +2071,10 @@ }, { "cell_type": "markdown", - "id": "6a89433e", - "metadata": {}, + "id": "fb9f5891", + "metadata": { + "editable": true + }, "source": [ "Here $\\boldsymbol{a}=\\boldsymbol{y} - \\boldsymbol{\\tilde{y}}$.\n", "\n", @@ -1824,8 +2084,10 @@ }, { "cell_type": "markdown", - "id": "8e9db463", - "metadata": {}, + "id": "2bac24dd", + "metadata": { + "editable": true + }, "source": [ "### To our real data: nuclear binding energies. Brief reminder on masses and binding energies\n", "\n", @@ -1839,8 +2101,10 @@ }, { "cell_type": "markdown", - "id": "322bc071", - "metadata": {}, + "id": "7d001c91", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\Delta M(N, Z) = M(N, Z) - uA,\n", @@ -1849,16 +2113,20 @@ }, { "cell_type": "markdown", - "id": "02864f30", - "metadata": {}, + "id": "a4f83b69", + "metadata": { + "editable": true + }, "source": [ "where $u$ is the Atomic Mass Unit" ] }, { "cell_type": "markdown", - "id": "75d42c9b", - "metadata": {}, + "id": "685bf88d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "u = M(^{12}\\mathrm{C})/12 = 931.4940954(57) \\hspace{0.1cm} \\mathrm{MeV}/c^2.\n", @@ -1867,16 +2135,20 @@ }, { "cell_type": "markdown", - "id": "9cd70ab7", - "metadata": {}, + "id": "0d04440c", + "metadata": { + "editable": true + }, "source": [ "The nucleon masses are" ] }, { "cell_type": "markdown", - "id": "b10d687a", - "metadata": {}, + "id": "6ec2f6a9", + "metadata": { + "editable": true + }, "source": [ "$$\n", "m_p = 1.00727646693(9)u,\n", @@ -1885,16 +2157,20 @@ }, { "cell_type": "markdown", - "id": "fadb16df", - "metadata": {}, + "id": "4f4bf59e", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "a784290a", - "metadata": {}, + "id": "23d1af65", + "metadata": { + "editable": true + }, "source": [ "$$\n", "m_n = 939.56536(8)\\hspace{0.1cm} \\mathrm{MeV}/c^2 = 1.0086649156(6)u.\n", @@ -1903,8 +2179,10 @@ }, { "cell_type": "markdown", - "id": "0eac8891", - "metadata": {}, + "id": "8652ebf1", + "metadata": { + "editable": true + }, "source": [ "In the [2016 mass evaluation of by W.J.Huang, G.Audi, M.Wang, F.G.Kondev, S.Naimi and X.Xu](http://nuclearmasses.org/resources_folder/Wang_2017_Chinese_Phys_C_41_030003.pdf)\n", "there are data on masses and decays of 3437 nuclei.\n", @@ -1917,8 +2195,10 @@ }, { "cell_type": "markdown", - "id": "7e0f7c9c", - "metadata": {}, + "id": "c87ca107", + "metadata": { + "editable": true + }, "source": [ "$$\n", "BE(N, Z) = ZM_H c^2 + Nm_n c^2 - M(N, Z)c^2 ,\n", @@ -1927,8 +2207,10 @@ }, { "cell_type": "markdown", - "id": "2cb53b2f", - "metadata": {}, + "id": "948550c3", + "metadata": { + "editable": true + }, "source": [ "where $M_H$ is the mass of the hydrogen atom and $m_n$ is the mass of the neutron.\n", "In terms of the mass excess the binding energy is given by" @@ -1936,8 +2218,10 @@ }, { "cell_type": "markdown", - "id": "baad681e", - "metadata": {}, + "id": "f589a367", + "metadata": { + "editable": true + }, "source": [ "$$\n", "BE(N, Z) = Z\\Delta_H c^2 + N\\Delta_n c^2 -\\Delta(N, Z)c^2 ,\n", @@ -1946,8 +2230,10 @@ }, { "cell_type": "markdown", - "id": "14445fea", - "metadata": {}, + "id": "045aeed0", + "metadata": { + "editable": true + }, "source": [ "where $\\Delta_H c^2 = 7.2890$ MeV and $\\Delta_n c^2 = 8.0713$ MeV.\n", "\n", @@ -1958,8 +2244,10 @@ }, { "cell_type": "markdown", - "id": "24e60de2", - "metadata": {}, + "id": "0b0febe4", + "metadata": { + "editable": true + }, "source": [ "$$\n", "BE(N,Z) = a_1A-a_2A^{2/3}-a_3\\frac{Z^2}{A^{1/3}}-a_4\\frac{(N-Z)^2}{A},\n", @@ -1968,8 +2256,10 @@ }, { "cell_type": "markdown", - "id": "32c6b631", - "metadata": {}, + "id": "ccf72e53", + "metadata": { + "editable": true + }, "source": [ "where $A$ stands for the number of nucleons and the $a_i$s are parameters which are determined by a fit \n", "to the experimental data. \n", @@ -1991,8 +2281,10 @@ }, { "cell_type": "markdown", - "id": "7f3d9e7f", - "metadata": {}, + "id": "c9a39d66", + "metadata": { + "editable": true + }, "source": [ "### Organizing our data\n", "\n", @@ -2006,8 +2298,11 @@ { "cell_type": "code", "execution_count": 27, - "id": "efbc8ac7", - "metadata": {}, + "id": "fb120a66", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# Common imports\n", @@ -2047,8 +2342,10 @@ }, { "cell_type": "markdown", - "id": "06c59468", - "metadata": {}, + "id": "2cdf5b21", + "metadata": { + "editable": true + }, "source": [ "Before we proceed, we define also a function for making our plots. You can obviously avoid this and simply set up various **matplotlib** commands every time you need them. You may however find it convenient to collect all such commands in one function and simply call this function." ] @@ -2056,8 +2353,11 @@ { "cell_type": "code", "execution_count": 28, - "id": "0ae452a1", - "metadata": {}, + "id": "b75db972", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "from pylab import plt, mpl\n", @@ -2075,8 +2375,10 @@ }, { "cell_type": "markdown", - "id": "f2d085b5", - "metadata": {}, + "id": "9c018124", + "metadata": { + "editable": true + }, "source": [ "Our next step is to read the data on experimental binding energies and\n", "reorganize them as functions of the mass number $A$, the number of\n", @@ -2090,8 +2392,11 @@ { "cell_type": "code", "execution_count": 29, - "id": "dc9112d2", - "metadata": {}, + "id": "c7a1bd51", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "\"\"\" \n", @@ -2108,8 +2413,10 @@ }, { "cell_type": "markdown", - "id": "04be6a67", - "metadata": {}, + "id": "bf23ffd4", + "metadata": { + "editable": true + }, "source": [ "The data we are interested in are in columns 2, 3, 4 and 11, giving us\n", "the number of neutrons, protons, mass numbers and binding energies,\n", @@ -2120,8 +2427,11 @@ { "cell_type": "code", "execution_count": 30, - "id": "23c26038", - "metadata": {}, + "id": "1153f595", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# Read the experimental data with Pandas\n", @@ -2146,8 +2456,10 @@ }, { "cell_type": "markdown", - "id": "e16c91b9", - "metadata": {}, + "id": "76d8d291", + "metadata": { + "editable": true + }, "source": [ "We have now read in the data, grouped them according to the variables we are interested in. \n", "We see how easy it is to reorganize the data using **pandas**. If we\n", @@ -2164,8 +2476,11 @@ { "cell_type": "code", "execution_count": 31, - "id": "3a3bf27b", - "metadata": {}, + "id": "f301bb19", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "A = Masses['A']\n", @@ -2178,8 +2493,10 @@ }, { "cell_type": "markdown", - "id": "5aa20571", - "metadata": {}, + "id": "8aacc9dc", + "metadata": { + "editable": true + }, "source": [ "The next step, and we will define this mathematically later, is to set up the so-called **design matrix**. We will throughout call this matrix $\\boldsymbol{X}$.\n", "It has dimensionality $p\\times n$, where $n$ is the number of data points and $p$ are the so-called predictors. In our case here they are given by the number of polynomials in $A$ we wish to include in the fit." @@ -2188,8 +2505,11 @@ { "cell_type": "code", "execution_count": 32, - "id": "e5b9778b", - "metadata": {}, + "id": "2fb6fe86", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# Now we set up the design matrix X\n", @@ -2203,8 +2523,10 @@ }, { "cell_type": "markdown", - "id": "49d1f04a", - "metadata": {}, + "id": "a0506e09", + "metadata": { + "editable": true + }, "source": [ "With **scikitlearn** we are now ready to use linear regression and fit our data." ] @@ -2212,8 +2534,11 @@ { "cell_type": "code", "execution_count": 33, - "id": "3b1fda0f", - "metadata": {}, + "id": "5c9070af", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "clf = skl.LinearRegression().fit(X, Energies)\n", @@ -2222,8 +2547,10 @@ }, { "cell_type": "markdown", - "id": "e74de2ae", - "metadata": {}, + "id": "1eddc7dc", + "metadata": { + "editable": true + }, "source": [ "Pretty simple! \n", "Now we can print measures of how our fit is doing, the coefficients from the fits and plot the final fit together with our data." @@ -2232,8 +2559,11 @@ { "cell_type": "code", "execution_count": 34, - "id": "61296632", - "metadata": {}, + "id": "a36fde4e", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# The mean squared error \n", @@ -2260,8 +2590,10 @@ }, { "cell_type": "markdown", - "id": "5ed4fbbc", - "metadata": {}, + "id": "e0959e43", + "metadata": { + "editable": true + }, "source": [ "### And what about using neural networks?\n", "\n", @@ -2272,8 +2604,11 @@ { "cell_type": "code", "execution_count": 35, - "id": "a8ea26ad", - "metadata": {}, + "id": "48a4a0c6", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "from sklearn.neural_network import MLPRegressor\n", @@ -2314,8 +2649,10 @@ }, { "cell_type": "markdown", - "id": "37036c7b", - "metadata": {}, + "id": "d877dbf2", + "metadata": { + "editable": true + }, "source": [ "## A first summary\n", "\n", @@ -2332,8 +2669,10 @@ }, { "cell_type": "markdown", - "id": "e8cdcf9c", - "metadata": {}, + "id": "4422f72b", + "metadata": { + "editable": true + }, "source": [ "## Why Linear Regression (aka Ordinary Least Squares and family)\n", "\n", @@ -2362,8 +2701,10 @@ }, { "cell_type": "markdown", - "id": "e61fc5f7", - "metadata": {}, + "id": "62392f20", + "metadata": { + "editable": true + }, "source": [ "## Regression analysis, overarching aims\n", "\n", @@ -2382,8 +2723,10 @@ }, { "cell_type": "markdown", - "id": "b9c9eab9", - "metadata": {}, + "id": "9eda07fd", + "metadata": { + "editable": true + }, "source": [ "## Regression analysis, overarching aims II\n", "\n", @@ -2408,8 +2751,10 @@ }, { "cell_type": "markdown", - "id": "2897f610", - "metadata": {}, + "id": "e7f3e3ea", + "metadata": { + "editable": true + }, "source": [ "## Examples\n", "In order to understand the relation among the predictors (or features or properties) $p$, the set of data $n$ and the target (outcome, output etc) $\\boldsymbol{y}$,\n", @@ -2421,8 +2766,10 @@ }, { "cell_type": "markdown", - "id": "7fd627a1", - "metadata": {}, + "id": "4c58ad38", + "metadata": { + "editable": true + }, "source": [ "$$\n", "BE(A) = a_0+a_1A+a_2A^{2/3}+a_3A^{-1/3}+a_4A^{-1},\n", @@ -2431,8 +2778,10 @@ }, { "cell_type": "markdown", - "id": "b62ab8c7", - "metadata": {}, + "id": "ab618c0d", + "metadata": { + "editable": true + }, "source": [ "we have five predictors, that is the intercept, the $A$ dependent term, the $A^{2/3}$ term and the $A^{-1/3}$ and $A^{-1}$ terms.\n", "This gives $p=0,1,2,3,4$. Furthermore we have $n$ entries for each predictor. It means that our design matrix is a \n", @@ -2444,8 +2793,10 @@ }, { "cell_type": "markdown", - "id": "63f8c4ca", - "metadata": {}, + "id": "fa1f1ebf", + "metadata": { + "editable": true + }, "source": [ "## General linear models and linear algebra\n", "Before we proceed let us study a case where we aim at fitting a set of data $\\boldsymbol{y}=[y_0,y_1,\\dots,y_{n-1}]$. We could think of these data as a result of an experiment or a complicated numerical experiment. These data are functions of a series of variables $\\boldsymbol{x}=[x_0,x_1,\\dots,x_{n-1}]$, that is $y_i = y(x_i)$ with $i=0,1,2,\\dots,n-1$. The variables $x_i$ could represent physical quantities like time, temperature, position etc. We assume that $y(x)$ is a smooth function. \n", @@ -2455,8 +2806,10 @@ }, { "cell_type": "markdown", - "id": "fa8ced7c", - "metadata": {}, + "id": "4154c494", + "metadata": { + "editable": true + }, "source": [ "$$\n", "y=y(x) \\rightarrow y(x_i)=\\tilde{y}_i+\\epsilon_i=\\sum_{j=0}^{n-1} \\beta_j x_i^j+\\epsilon_i,\n", @@ -2465,16 +2818,20 @@ }, { "cell_type": "markdown", - "id": "e775cf69", - "metadata": {}, + "id": "38e4e60d", + "metadata": { + "editable": true + }, "source": [ "where $\\epsilon_i$ is the error in our approximation." ] }, { "cell_type": "markdown", - "id": "e9021bcb", - "metadata": {}, + "id": "23bd9b64", + "metadata": { + "editable": true + }, "source": [ "## Rewriting the fitting procedure as a linear algebra problem\n", "For every set of values $y_i,x_i$ we have thus the corresponding set of equations" @@ -2482,8 +2839,10 @@ }, { "cell_type": "markdown", - "id": "3dfca0e1", - "metadata": {}, + "id": "38af1807", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{align*}\n", @@ -2498,8 +2857,10 @@ }, { "cell_type": "markdown", - "id": "d7dc17c7", - "metadata": {}, + "id": "700e1f0b", + "metadata": { + "editable": true + }, "source": [ "## Rewriting the fitting procedure as a linear algebra problem, more details\n", "Defining the vectors" @@ -2507,8 +2868,10 @@ }, { "cell_type": "markdown", - "id": "0c4a0198", - "metadata": {}, + "id": "acad106a", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{y} = [y_0,y_1, y_2,\\dots, y_{n-1}]^T,\n", @@ -2517,16 +2880,20 @@ }, { "cell_type": "markdown", - "id": "60bc7c9b", - "metadata": {}, + "id": "f34588ec", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "4b1f2bbf", - "metadata": {}, + "id": "b1d9eae1", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\beta} = [\\beta_0,\\beta_1, \\beta_2,\\dots, \\beta_{n-1}]^T,\n", @@ -2535,16 +2902,20 @@ }, { "cell_type": "markdown", - "id": "46f9701c", - "metadata": {}, + "id": "8b16f794", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "c5245bf6", - "metadata": {}, + "id": "b4a5cdee", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\epsilon} = [\\epsilon_0,\\epsilon_1, \\epsilon_2,\\dots, \\epsilon_{n-1}]^T,\n", @@ -2553,16 +2924,20 @@ }, { "cell_type": "markdown", - "id": "f6c9d732", - "metadata": {}, + "id": "a7e42f48", + "metadata": { + "editable": true + }, "source": [ "and the design matrix" ] }, { "cell_type": "markdown", - "id": "26e9e345", - "metadata": {}, + "id": "d4c5fbe9", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}=\n", @@ -2578,16 +2953,20 @@ }, { "cell_type": "markdown", - "id": "298a04a5", - "metadata": {}, + "id": "79dc75d5", + "metadata": { + "editable": true + }, "source": [ "we can rewrite our equations as" ] }, { "cell_type": "markdown", - "id": "b1f251cf", - "metadata": {}, + "id": "5789125b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{y} = \\boldsymbol{X}\\boldsymbol{\\beta}+\\boldsymbol{\\epsilon}.\n", @@ -2596,16 +2975,20 @@ }, { "cell_type": "markdown", - "id": "5ce82275", - "metadata": {}, + "id": "878fac5e", + "metadata": { + "editable": true + }, "source": [ "The above design matrix is called a [Vandermonde matrix](https://en.wikipedia.org/wiki/Vandermonde_matrix)." ] }, { "cell_type": "markdown", - "id": "39b1c23a", - "metadata": {}, + "id": "8ceacbbf", + "metadata": { + "editable": true + }, "source": [ "## Generalizing the fitting procedure as a linear algebra problem\n", "\n", @@ -2618,8 +3001,10 @@ }, { "cell_type": "markdown", - "id": "fe707681", - "metadata": {}, + "id": "ec09e4d8", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{align*}\n", @@ -2636,16 +3021,20 @@ }, { "cell_type": "markdown", - "id": "fef248b2", - "metadata": {}, + "id": "af731e33", + "metadata": { + "editable": true + }, "source": [ "**Note that we have $p=n$ here. The matrix is symmetric. This is generally not the case!**" ] }, { "cell_type": "markdown", - "id": "b8239056", - "metadata": {}, + "id": "afad26b8", + "metadata": { + "editable": true + }, "source": [ "## Generalizing the fitting procedure as a linear algebra problem\n", "We redefine in turn the matrix $\\boldsymbol{X}$ as" @@ -2653,8 +3042,10 @@ }, { "cell_type": "markdown", - "id": "b8d8b1e7", - "metadata": {}, + "id": "6b7e18d9", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}=\n", @@ -2670,16 +3061,20 @@ }, { "cell_type": "markdown", - "id": "d6ab5053", - "metadata": {}, + "id": "27458603", + "metadata": { + "editable": true + }, "source": [ "and without loss of generality we rewrite again our equations as" ] }, { "cell_type": "markdown", - "id": "d5a1a4be", - "metadata": {}, + "id": "20b91634", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{y} = \\boldsymbol{X}\\boldsymbol{\\beta}+\\boldsymbol{\\epsilon}.\n", @@ -2688,16 +3083,20 @@ }, { "cell_type": "markdown", - "id": "17fbd184", - "metadata": {}, + "id": "f9b73178", + "metadata": { + "editable": true + }, "source": [ "The left-hand side of this equation is kwown. Our error vector $\\boldsymbol{\\epsilon}$ and the parameter vector $\\boldsymbol{\\beta}$ are our unknow quantities. How can we obtain the optimal set of $\\beta_i$ values?" ] }, { "cell_type": "markdown", - "id": "75f430d1", - "metadata": {}, + "id": "53d85155", + "metadata": { + "editable": true + }, "source": [ "## Optimizing our parameters\n", "We have defined the matrix $\\boldsymbol{X}$ via the equations" @@ -2705,8 +3104,10 @@ }, { "cell_type": "markdown", - "id": "e33f6d7e", - "metadata": {}, + "id": "b308e153", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\begin{align*}\n", @@ -2723,8 +3124,10 @@ }, { "cell_type": "markdown", - "id": "d65e3d00", - "metadata": {}, + "id": "e6924518", + "metadata": { + "editable": true + }, "source": [ "As we noted above, we stayed with a system with the design matrix \n", " $\\boldsymbol{X}\\in {\\mathbb{R}}^{n\\times n}$, that is we have $p=n$. For reasons to come later (algorithmic arguments) we will hereafter define \n", @@ -2733,8 +3136,10 @@ }, { "cell_type": "markdown", - "id": "8a93ab5b", - "metadata": {}, + "id": "4c524746", + "metadata": { + "editable": true + }, "source": [ "## Our model for the nuclear binding energies\n", "\n", @@ -2746,8 +3151,11 @@ { "cell_type": "code", "execution_count": 36, - "id": "a101fe72", - "metadata": {}, + "id": "be9e5ed9", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# Common imports\n", @@ -2823,16 +3231,20 @@ }, { "cell_type": "markdown", - "id": "5092723e", - "metadata": {}, + "id": "53a9c408", + "metadata": { + "editable": true + }, "source": [ "With $\\boldsymbol{\\beta}\\in {\\mathbb{R}}^{p\\times 1}$, it means that we will hereafter write our equations for the approximation as" ] }, { "cell_type": "markdown", - "id": "4f274a13", - "metadata": {}, + "id": "8b44e887", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\tilde{y}}= \\boldsymbol{X}\\boldsymbol{\\beta},\n", @@ -2841,16 +3253,20 @@ }, { "cell_type": "markdown", - "id": "c1597b61", - "metadata": {}, + "id": "7c1e3077", + "metadata": { + "editable": true + }, "source": [ "throughout these lectures." ] }, { "cell_type": "markdown", - "id": "510eda7d", - "metadata": {}, + "id": "8ff130dd", + "metadata": { + "editable": true + }, "source": [ "## Optimizing our parameters, more details\n", "With the above we use the design matrix to define the approximation $\\boldsymbol{\\tilde{y}}$ via the unknown quantity $\\boldsymbol{\\beta}$ as" @@ -2858,8 +3274,10 @@ }, { "cell_type": "markdown", - "id": "e04087f8", - "metadata": {}, + "id": "c5721167", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\tilde{y}}= \\boldsymbol{X}\\boldsymbol{\\beta},\n", @@ -2868,16 +3286,20 @@ }, { "cell_type": "markdown", - "id": "e4a6ff11", - "metadata": {}, + "id": "75b23a26", + "metadata": { + "editable": true + }, "source": [ "and in order to find the optimal parameters $\\beta_i$ instead of solving the above linear algebra problem, we define a function which gives a measure of the spread between the values $y_i$ (which represent hopefully the exact values) and the parameterized values $\\tilde{y}_i$, namely" ] }, { "cell_type": "markdown", - "id": "65ebba11", - "metadata": {}, + "id": "b1e1bda3", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\frac{1}{n}\\sum_{i=0}^{n-1}\\left(y_i-\\tilde{y}_i\\right)^2=\\frac{1}{n}\\left\\{\\left(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}}\\right)^T\\left(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}}\\right)\\right\\},\n", @@ -2886,16 +3308,20 @@ }, { "cell_type": "markdown", - "id": "f5407aa5", - "metadata": {}, + "id": "cca1bd23", + "metadata": { + "editable": true + }, "source": [ "or using the matrix $\\boldsymbol{X}$ and in a more compact matrix-vector notation as" ] }, { "cell_type": "markdown", - "id": "3eb3ac09", - "metadata": {}, + "id": "fd0f6810", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\frac{1}{n}\\left\\{\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)^T\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)\\right\\}.\n", @@ -2904,8 +3330,10 @@ }, { "cell_type": "markdown", - "id": "4948a093", - "metadata": {}, + "id": "43caf632", + "metadata": { + "editable": true + }, "source": [ "This function is one possible way to define the so-called cost function.\n", "\n", @@ -2915,8 +3343,10 @@ }, { "cell_type": "markdown", - "id": "dbf202fc", - "metadata": {}, + "id": "6f009de0", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\frac{1}{2n}\\sum_{i=0}^{n-1}\\left(y_i-\\tilde{y}_i\\right)^2,\n", @@ -2925,16 +3355,20 @@ }, { "cell_type": "markdown", - "id": "4ec3525e", - "metadata": {}, + "id": "d1ac7af2", + "metadata": { + "editable": true + }, "source": [ "since when taking the first derivative with respect to the unknown parameters $\\beta$, the factor of $2$ cancels out." ] }, { "cell_type": "markdown", - "id": "5dcb6438", - "metadata": {}, + "id": "ff0a733b", + "metadata": { + "editable": true + }, "source": [ "## Interpretations and optimizing our parameters\n", "\n", @@ -2943,8 +3377,10 @@ }, { "cell_type": "markdown", - "id": "f8f9b4d6", - "metadata": {}, + "id": "a1d17166", + "metadata": { + "editable": true + }, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\frac{1}{n}\\left\\{\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)^T\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)\\right\\},\n", @@ -2953,8 +3389,10 @@ }, { "cell_type": "markdown", - "id": "7c4159f3", - "metadata": {}, + "id": "d485a1d0", + "metadata": { + "editable": true + }, "source": [ "can be linked to the variance of the quantity $y_i$ if we interpret the latter as the mean value. \n", "When linking (see the discussion below) with the maximum likelihood approach below, we will indeed interpret $y_i$ as a mean value" @@ -2962,8 +3400,10 @@ }, { "cell_type": "markdown", - "id": "e57a1702", - "metadata": {}, + "id": "bf78a285", + "metadata": { + "editable": true + }, "source": [ "$$\n", "y_{i}=\\langle y_i \\rangle = \\beta_0x_{i,0}+\\beta_1x_{i,1}+\\beta_2x_{i,2}+\\dots+\\beta_{n-1}x_{i,n-1}+\\epsilon_i,\n", @@ -2972,8 +3412,10 @@ }, { "cell_type": "markdown", - "id": "e01c891e", - "metadata": {}, + "id": "6e5a0c21", + "metadata": { + "editable": true + }, "source": [ "where $\\langle y_i \\rangle$ is the mean value. Keep in mind also that\n", "till now we have treated $y_i$ as the exact value. Normally, the\n", @@ -2989,8 +3431,10 @@ }, { "cell_type": "markdown", - "id": "5623f65f", - "metadata": {}, + "id": "7bac5436", + "metadata": { + "editable": true + }, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", @@ -3000,16 +3444,20 @@ }, { "cell_type": "markdown", - "id": "9d395c79", - "metadata": {}, + "id": "c5adfccd", + "metadata": { + "editable": true + }, "source": [ "In practical terms it means we will require" ] }, { "cell_type": "markdown", - "id": "6afb3c3e", - "metadata": {}, + "id": "42507986", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\beta_j} = \\frac{\\partial }{\\partial \\beta_j}\\left[ \\frac{1}{n}\\sum_{i=0}^{n-1}\\left(y_i-\\beta_0x_{i,0}-\\beta_1x_{i,1}-\\beta_2x_{i,2}-\\dots-\\beta_{n-1}x_{i,n-1}\\right)^2\\right]=0,\n", @@ -3018,16 +3466,20 @@ }, { "cell_type": "markdown", - "id": "a48cc207", - "metadata": {}, + "id": "babdef77", + "metadata": { + "editable": true + }, "source": [ "which results in" ] }, { "cell_type": "markdown", - "id": "11abf336", - "metadata": {}, + "id": "86162584", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\beta_j} = -\\frac{2}{n}\\left[ \\sum_{i=0}^{n-1}x_{ij}\\left(y_i-\\beta_0x_{i,0}-\\beta_1x_{i,1}-\\beta_2x_{i,2}-\\dots-\\beta_{n-1}x_{i,n-1}\\right)\\right]=0,\n", @@ -3036,16 +3488,20 @@ }, { "cell_type": "markdown", - "id": "ad441456", - "metadata": {}, + "id": "9b10ab72", + "metadata": { + "editable": true + }, "source": [ "or in a matrix-vector form as" ] }, { "cell_type": "markdown", - "id": "9d5471c2", - "metadata": {}, + "id": "1892f0ff", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}} = 0 = \\boldsymbol{X}^T\\left( \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right).\n", @@ -3054,8 +3510,10 @@ }, { "cell_type": "markdown", - "id": "f1267199", - "metadata": {}, + "id": "2ca00dea", + "metadata": { + "editable": true + }, "source": [ "## Interpretations and optimizing our parameters\n", "We can rewrite" @@ -3063,8 +3521,10 @@ }, { "cell_type": "markdown", - "id": "712bc3d4", - "metadata": {}, + "id": "fade073e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}} = 0 = \\boldsymbol{X}^T\\left( \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right),\n", @@ -3073,16 +3533,20 @@ }, { "cell_type": "markdown", - "id": "e5ae3ee2", - "metadata": {}, + "id": "439ab70d", + "metadata": { + "editable": true + }, "source": [ "as" ] }, { "cell_type": "markdown", - "id": "c3957c60", - "metadata": {}, + "id": "88cb6463", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{y} = \\boldsymbol{X}^T\\boldsymbol{X}\\boldsymbol{\\beta},\n", @@ -3091,16 +3555,20 @@ }, { "cell_type": "markdown", - "id": "2104ba73", - "metadata": {}, + "id": "80edf103", + "metadata": { + "editable": true + }, "source": [ "and if the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$ is invertible we have the solution" ] }, { "cell_type": "markdown", - "id": "13616744", - "metadata": {}, + "id": "15fd0db4", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\beta} =\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", @@ -3109,8 +3577,10 @@ }, { "cell_type": "markdown", - "id": "7123c0b3", - "metadata": {}, + "id": "076137a7", + "metadata": { + "editable": true + }, "source": [ "We note also that since our design matrix is defined as $\\boldsymbol{X}\\in\n", "{\\mathbb{R}}^{n\\times p}$, the product $\\boldsymbol{X}^T\\boldsymbol{X} \\in\n", @@ -3128,8 +3598,10 @@ }, { "cell_type": "markdown", - "id": "ca00624d", - "metadata": {}, + "id": "49820b6b", + "metadata": { + "editable": true + }, "source": [ "## Interpretations and optimizing our parameters\n", "The residuals $\\boldsymbol{\\epsilon}$ are in turn given by" @@ -3137,8 +3609,10 @@ }, { "cell_type": "markdown", - "id": "be062840", - "metadata": {}, + "id": "efc1ea2d", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\epsilon} = \\boldsymbol{y}-\\boldsymbol{\\tilde{y}} = \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta},\n", @@ -3147,16 +3621,20 @@ }, { "cell_type": "markdown", - "id": "1b91f21b", - "metadata": {}, + "id": "c7ed6c2a", + "metadata": { + "editable": true + }, "source": [ "and with" ] }, { "cell_type": "markdown", - "id": "99760cdd", - "metadata": {}, + "id": "f395aa42", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}^T\\left( \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)= 0,\n", @@ -3165,16 +3643,20 @@ }, { "cell_type": "markdown", - "id": "b1092a73", - "metadata": {}, + "id": "30535e6b", + "metadata": { + "editable": true + }, "source": [ "we have" ] }, { "cell_type": "markdown", - "id": "9340bc33", - "metadata": {}, + "id": "b58c9b00", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{\\epsilon}=\\boldsymbol{X}^T\\left( \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)= 0,\n", @@ -3183,8 +3665,10 @@ }, { "cell_type": "markdown", - "id": "5155277d", - "metadata": {}, + "id": "f0264e17", + "metadata": { + "editable": true + }, "source": [ "meaning that the solution for $\\boldsymbol{\\beta}$ is the one which minimizes the residuals. Later we will link this with the maximum likelihood approach.\n", "\n", @@ -3193,8 +3677,10 @@ }, { "cell_type": "markdown", - "id": "d198a040", - "metadata": {}, + "id": "5754e169", + "metadata": { + "editable": true + }, "source": [ "## Own code for Ordinary Least Squares\n", "\n", @@ -3205,8 +3691,11 @@ { "cell_type": "code", "execution_count": 37, - "id": "1fd1c624", - "metadata": {}, + "id": "b8559e4e", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# matrix inversion to find beta\n", @@ -3217,8 +3706,10 @@ }, { "cell_type": "markdown", - "id": "0334c969", - "metadata": {}, + "id": "341052df", + "metadata": { + "editable": true + }, "source": [ "Alternatively, you can use the least squares functionality in **Numpy** as" ] @@ -3226,8 +3717,11 @@ { "cell_type": "code", "execution_count": 38, - "id": "ee65f2c6", - "metadata": {}, + "id": "86f991dd", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "fit = np.linalg.lstsq(X, Energies, rcond =None)[0]\n", @@ -3236,8 +3730,10 @@ }, { "cell_type": "markdown", - "id": "6a90caab", - "metadata": {}, + "id": "3ea4e5ac", + "metadata": { + "editable": true + }, "source": [ "And finally we plot our fit with and compare with data" ] @@ -3245,8 +3741,11 @@ { "cell_type": "code", "execution_count": 39, - "id": "be846d9c", - "metadata": {}, + "id": "514424f8", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "Masses['Eapprox'] = ytilde\n", @@ -3265,8 +3764,10 @@ }, { "cell_type": "markdown", - "id": "186d4dfc", - "metadata": {}, + "id": "d3f1dc97", + "metadata": { + "editable": true + }, "source": [ "## Adding error analysis and training set up\n", "\n", @@ -3277,8 +3778,11 @@ { "cell_type": "code", "execution_count": 40, - "id": "6de9e6a2", - "metadata": {}, + "id": "d8f99811", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "def R2(y_data, y_model):\n", @@ -3287,8 +3791,10 @@ }, { "cell_type": "markdown", - "id": "d416f1dc", - "metadata": {}, + "id": "10369234", + "metadata": { + "editable": true + }, "source": [ "and we would be using it as" ] @@ -3296,8 +3802,11 @@ { "cell_type": "code", "execution_count": 41, - "id": "c3d5d9a5", - "metadata": {}, + "id": "8bcc5624", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "print(R2(Energies,ytilde))" @@ -3305,8 +3814,10 @@ }, { "cell_type": "markdown", - "id": "a8793e09", - "metadata": {}, + "id": "37051767", + "metadata": { + "editable": true + }, "source": [ "We can easily add our **MSE** score as" ] @@ -3314,8 +3825,11 @@ { "cell_type": "code", "execution_count": 42, - "id": "777316e8", - "metadata": {}, + "id": "9180df3b", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "def MSE(y_data,y_model):\n", @@ -3327,8 +3841,10 @@ }, { "cell_type": "markdown", - "id": "8395880e", - "metadata": {}, + "id": "ec181977", + "metadata": { + "editable": true + }, "source": [ "and finally the relative error as" ] @@ -3336,8 +3852,11 @@ { "cell_type": "code", "execution_count": 43, - "id": "b32c1466", - "metadata": {}, + "id": "6cd952eb", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "def RelativeError(y_data,y_model):\n", @@ -3347,8 +3866,10 @@ }, { "cell_type": "markdown", - "id": "91a8c376", - "metadata": {}, + "id": "5bc6ff56", + "metadata": { + "editable": true + }, "source": [ "## The $\\chi^2$ function\n", "\n", @@ -3367,8 +3888,10 @@ }, { "cell_type": "markdown", - "id": "e9f97232", - "metadata": {}, + "id": "6976983f", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\chi^2(\\boldsymbol{\\beta})=\\frac{1}{n}\\sum_{i=0}^{n-1}\\frac{\\left(y_i-\\tilde{y}_i\\right)^2}{\\sigma_i^2}=\\frac{1}{n}\\left\\{\\left(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}}\\right)^T\\frac{1}{\\boldsymbol{\\Sigma^2}}\\left(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}}\\right)\\right\\},\n", @@ -3377,16 +3900,20 @@ }, { "cell_type": "markdown", - "id": "6f86f9ff", - "metadata": {}, + "id": "dad338b3", + "metadata": { + "editable": true + }, "source": [ "where the matrix $\\boldsymbol{\\Sigma}$ is a diagonal matrix with $\\sigma_i$ as matrix elements." ] }, { "cell_type": "markdown", - "id": "099e318a", - "metadata": {}, + "id": "d40eea7e", + "metadata": { + "editable": true + }, "source": [ "## The $\\chi^2$ function\n", "\n", @@ -3395,8 +3922,10 @@ }, { "cell_type": "markdown", - "id": "94882de1", - "metadata": {}, + "id": "c54cda06", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial \\chi^2(\\boldsymbol{\\beta})}{\\partial \\beta_j} = \\frac{\\partial }{\\partial \\beta_j}\\left[ \\frac{1}{n}\\sum_{i=0}^{n-1}\\left(\\frac{y_i-\\beta_0x_{i,0}-\\beta_1x_{i,1}-\\beta_2x_{i,2}-\\dots-\\beta_{n-1}x_{i,n-1}}{\\sigma_i}\\right)^2\\right]=0,\n", @@ -3405,16 +3934,20 @@ }, { "cell_type": "markdown", - "id": "4bbe2d6d", - "metadata": {}, + "id": "b6461ba1", + "metadata": { + "editable": true + }, "source": [ "which results in" ] }, { "cell_type": "markdown", - "id": "82224549", - "metadata": {}, + "id": "7248b6d3", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial \\chi^2(\\boldsymbol{\\beta})}{\\partial \\beta_j} = -\\frac{2}{n}\\left[ \\sum_{i=0}^{n-1}\\frac{x_{ij}}{\\sigma_i}\\left(\\frac{y_i-\\beta_0x_{i,0}-\\beta_1x_{i,1}-\\beta_2x_{i,2}-\\dots-\\beta_{n-1}x_{i,n-1}}{\\sigma_i}\\right)\\right]=0,\n", @@ -3423,16 +3956,20 @@ }, { "cell_type": "markdown", - "id": "85de5310", - "metadata": {}, + "id": "fa59cd41", + "metadata": { + "editable": true + }, "source": [ "or in a matrix-vector form as" ] }, { "cell_type": "markdown", - "id": "0c4e69ee", - "metadata": {}, + "id": "c6537426", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial \\chi^2(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}} = 0 = \\boldsymbol{A}^T\\left( \\boldsymbol{b}-\\boldsymbol{A}\\boldsymbol{\\beta}\\right).\n", @@ -3441,16 +3978,20 @@ }, { "cell_type": "markdown", - "id": "2cf42150", - "metadata": {}, + "id": "9949aea3", + "metadata": { + "editable": true + }, "source": [ "where we have defined the matrix $\\boldsymbol{A} =\\boldsymbol{X}/\\boldsymbol{\\Sigma}$ with matrix elements $a_{ij} = x_{ij}/\\sigma_i$ and the vector $\\boldsymbol{b}$ with elements $b_i = y_i/\\sigma_i$." ] }, { "cell_type": "markdown", - "id": "28d8b572", - "metadata": {}, + "id": "15e75646", + "metadata": { + "editable": true + }, "source": [ "## The $\\chi^2$ function\n", "\n", @@ -3459,8 +4000,10 @@ }, { "cell_type": "markdown", - "id": "bd0da12f", - "metadata": {}, + "id": "960fe9bf", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial \\chi^2(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}} = 0 = \\boldsymbol{A}^T\\left( \\boldsymbol{b}-\\boldsymbol{A}\\boldsymbol{\\beta}\\right),\n", @@ -3469,16 +4012,20 @@ }, { "cell_type": "markdown", - "id": "79ab73b9", - "metadata": {}, + "id": "0c92beea", + "metadata": { + "editable": true + }, "source": [ "as" ] }, { "cell_type": "markdown", - "id": "1a8223d3", - "metadata": {}, + "id": "b1f1dd8c", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{A}^T\\boldsymbol{b} = \\boldsymbol{A}^T\\boldsymbol{A}\\boldsymbol{\\beta},\n", @@ -3487,16 +4034,20 @@ }, { "cell_type": "markdown", - "id": "a2f47783", - "metadata": {}, + "id": "32a69808", + "metadata": { + "editable": true + }, "source": [ "and if the matrix $\\boldsymbol{A}^T\\boldsymbol{A}$ is invertible we have the solution" ] }, { "cell_type": "markdown", - "id": "5e3fb86d", - "metadata": {}, + "id": "1587d13c", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{\\beta} =\\left(\\boldsymbol{A}^T\\boldsymbol{A}\\right)^{-1}\\boldsymbol{A}^T\\boldsymbol{b}.\n", @@ -3505,8 +4056,10 @@ }, { "cell_type": "markdown", - "id": "fa454a83", - "metadata": {}, + "id": "57c8525c", + "metadata": { + "editable": true + }, "source": [ "## The $\\chi^2$ function\n", "\n", @@ -3515,8 +4068,10 @@ }, { "cell_type": "markdown", - "id": "24add22b", - "metadata": {}, + "id": "76b411e4", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\boldsymbol{H} = \\left(\\boldsymbol{A}^T\\boldsymbol{A}\\right)^{-1},\n", @@ -3525,16 +4080,20 @@ }, { "cell_type": "markdown", - "id": "8dcd4b60", - "metadata": {}, + "id": "fd7e54b2", + "metadata": { + "editable": true + }, "source": [ "we have then the following expression for the parameters $\\beta_j$ (the matrix elements of $\\boldsymbol{H}$ are $h_{ij}$)" ] }, { "cell_type": "markdown", - "id": "8cf096f9", - "metadata": {}, + "id": "7df78215", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_j = \\sum_{k=0}^{p-1}h_{jk}\\sum_{i=0}^{n-1}\\frac{y_i}{\\sigma_i}\\frac{x_{ik}}{\\sigma_i} = \\sum_{k=0}^{p-1}h_{jk}\\sum_{i=0}^{n-1}b_ia_{ik}\n", @@ -3543,16 +4102,20 @@ }, { "cell_type": "markdown", - "id": "2af41539", - "metadata": {}, + "id": "b130617b", + "metadata": { + "editable": true + }, "source": [ "We state without proof the expression for the uncertainty in the parameters $\\beta_j$ as (we leave this as an exercise)" ] }, { "cell_type": "markdown", - "id": "48bc0d81", - "metadata": {}, + "id": "c420f462", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\sigma^2(\\beta_j) = \\sum_{i=0}^{n-1}\\sigma_i^2\\left( \\frac{\\partial \\beta_j}{\\partial y_i}\\right)^2,\n", @@ -3561,16 +4124,20 @@ }, { "cell_type": "markdown", - "id": "fea98193", - "metadata": {}, + "id": "25ea122c", + "metadata": { + "editable": true + }, "source": [ "resulting in" ] }, { "cell_type": "markdown", - "id": "d95b3640", - "metadata": {}, + "id": "a50deb8c", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\sigma^2(\\beta_j) = \\left(\\sum_{k=0}^{p-1}h_{jk}\\sum_{i=0}^{n-1}a_{ik}\\right)\\left(\\sum_{l=0}^{p-1}h_{jl}\\sum_{m=0}^{n-1}a_{ml}\\right) = h_{jj}!\n", @@ -3579,8 +4146,10 @@ }, { "cell_type": "markdown", - "id": "d1749aad", - "metadata": {}, + "id": "2e3b4ef2", + "metadata": { + "editable": true + }, "source": [ "## The $\\chi^2$ function\n", "The first step here is to approximate the function $y$ with a first-order polynomial, that is we write" @@ -3588,8 +4157,10 @@ }, { "cell_type": "markdown", - "id": "139a6ebe", - "metadata": {}, + "id": "d4255f0a", + "metadata": { + "editable": true + }, "source": [ "$$\n", "y=y(x) \\rightarrow y(x_i) \\approx \\beta_0+\\beta_1 x_i.\n", @@ -3598,16 +4169,20 @@ }, { "cell_type": "markdown", - "id": "89002f52", - "metadata": {}, + "id": "784d87e1", + "metadata": { + "editable": true + }, "source": [ "By computing the derivatives of $\\chi^2$ with respect to $\\beta_0$ and $\\beta_1$ show that these are given by" ] }, { "cell_type": "markdown", - "id": "b9b48df5", - "metadata": {}, + "id": "8c6a5104", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial \\chi^2(\\boldsymbol{\\beta})}{\\partial \\beta_0} = -2\\left[ \\frac{1}{n}\\sum_{i=0}^{n-1}\\left(\\frac{y_i-\\beta_0-\\beta_1x_{i}}{\\sigma_i^2}\\right)\\right]=0,\n", @@ -3616,16 +4191,20 @@ }, { "cell_type": "markdown", - "id": "76413cfe", - "metadata": {}, + "id": "582c9d1b", + "metadata": { + "editable": true + }, "source": [ "and" ] }, { "cell_type": "markdown", - "id": "21843716", - "metadata": {}, + "id": "d1db631e", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\frac{\\partial \\chi^2(\\boldsymbol{\\beta})}{\\partial \\beta_1} = -\\frac{2}{n}\\left[ \\sum_{i=0}^{n-1}x_i\\left(\\frac{y_i-\\beta_0-\\beta_1x_{i}}{\\sigma_i^2}\\right)\\right]=0.\n", @@ -3634,8 +4213,10 @@ }, { "cell_type": "markdown", - "id": "5bccb54a", - "metadata": {}, + "id": "d3d9ce51", + "metadata": { + "editable": true + }, "source": [ "## The $\\chi^2$ function\n", "\n", @@ -3645,8 +4226,10 @@ }, { "cell_type": "markdown", - "id": "65141693", - "metadata": {}, + "id": "5ceac6f8", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\gamma = \\sum_{i=0}^{n-1}\\frac{1}{\\sigma_i^2},\n", @@ -3655,8 +4238,10 @@ }, { "cell_type": "markdown", - "id": "67a57b05", - "metadata": {}, + "id": "e8298ff1", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\gamma_x = \\sum_{i=0}^{n-1}\\frac{x_{i}}{\\sigma_i^2},\n", @@ -3665,8 +4250,10 @@ }, { "cell_type": "markdown", - "id": "4b786ae5", - "metadata": {}, + "id": "303ab451", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\gamma_y = \\sum_{i=0}^{n-1}\\left(\\frac{y_i}{\\sigma_i^2}\\right),\n", @@ -3675,8 +4262,10 @@ }, { "cell_type": "markdown", - "id": "8f90ddfb", - "metadata": {}, + "id": "4908c797", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\gamma_{xx} = \\sum_{i=0}^{n-1}\\frac{x_ix_{i}}{\\sigma_i^2},\n", @@ -3685,8 +4274,10 @@ }, { "cell_type": "markdown", - "id": "669e431f", - "metadata": {}, + "id": "f3d29d24", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\gamma_{xy} = \\sum_{i=0}^{n-1}\\frac{y_ix_{i}}{\\sigma_i^2},\n", @@ -3695,16 +4286,20 @@ }, { "cell_type": "markdown", - "id": "7dfd296c", - "metadata": {}, + "id": "58c62dd2", + "metadata": { + "editable": true + }, "source": [ "we obtain" ] }, { "cell_type": "markdown", - "id": "1a39204c", - "metadata": {}, + "id": "3fc7a84b", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_0 = \\frac{\\gamma_{xx}\\gamma_y-\\gamma_x\\gamma_y}{\\gamma\\gamma_{xx}-\\gamma_x^2},\n", @@ -3713,8 +4308,10 @@ }, { "cell_type": "markdown", - "id": "61cbc812", - "metadata": {}, + "id": "ca249b50", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\beta_1 = \\frac{\\gamma_{xy}\\gamma-\\gamma_x\\gamma_y}{\\gamma\\gamma_{xx}-\\gamma_x^2}.\n", @@ -3723,8 +4320,10 @@ }, { "cell_type": "markdown", - "id": "6b8648b3", - "metadata": {}, + "id": "923635ef", + "metadata": { + "editable": true + }, "source": [ "This approach (different linear and non-linear regression) suffers\n", "often from both being underdetermined and overdetermined in the\n", @@ -3734,8 +4333,10 @@ }, { "cell_type": "markdown", - "id": "c91f9c88", - "metadata": {}, + "id": "045c5990", + "metadata": { + "editable": true + }, "source": [ "## Fitting an Equation of State for Dense Nuclear Matter\n", "\n", @@ -3759,8 +4360,10 @@ }, { "cell_type": "markdown", - "id": "3b614967", - "metadata": {}, + "id": "5ea68129", + "metadata": { + "editable": true + }, "source": [ "## The code" ] @@ -3768,8 +4371,11 @@ { "cell_type": "code", "execution_count": 44, - "id": "2229e29b", - "metadata": {}, + "id": "d399c0d7", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "# Common imports\n", @@ -3861,8 +4467,10 @@ }, { "cell_type": "markdown", - "id": "51cecbe6", - "metadata": {}, + "id": "c7673816", + "metadata": { + "editable": true + }, "source": [ "The above simple polynomial in density $\\rho$ gives an excellent fit\n", "to the data. \n", @@ -3874,8 +4482,10 @@ }, { "cell_type": "markdown", - "id": "cb44fadc", - "metadata": {}, + "id": "c979bd8e", + "metadata": { + "editable": true + }, "source": [ "## Splitting our Data in Training and Test data\n", "\n", @@ -3894,8 +4504,11 @@ { "cell_type": "code", "execution_count": 45, - "id": "41334dd2", - "metadata": {}, + "id": "933f410b", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "import os\n", @@ -3966,8 +4579,10 @@ }, { "cell_type": "markdown", - "id": "cc28353d", - "metadata": {}, + "id": "13999171", + "metadata": { + "editable": true + }, "source": [ "## Exercises\n", "\n", @@ -3976,8 +4591,10 @@ }, { "cell_type": "markdown", - "id": "6c982e21", - "metadata": {}, + "id": "9f208390", + "metadata": { + "editable": true + }, "source": [ "## Exercise 1: Setting up various Python environments\n", "\n", @@ -4043,8 +4660,10 @@ }, { "cell_type": "markdown", - "id": "28de3ce3", - "metadata": {}, + "id": "2010af15", + "metadata": { + "editable": true + }, "source": [ "## Exercise 2: making your own data and exploring scikit-learn\n", "\n", @@ -4055,8 +4674,11 @@ { "cell_type": "code", "execution_count": 46, - "id": "819f05e1", - "metadata": {}, + "id": "59a44b2e", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "x = np.random.rand(100,1)\n", @@ -4065,8 +4687,10 @@ }, { "cell_type": "markdown", - "id": "f8f20749", - "metadata": {}, + "id": "03512f36", + "metadata": { + "editable": true + }, "source": [ "1. Write your own code (following the examples under the [regression notes](https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter1.html)) for computing the parametrization of the data set fitting a second-order polynomial. \n", "\n", @@ -4077,8 +4701,10 @@ }, { "cell_type": "markdown", - "id": "43ca9d86", - "metadata": {}, + "id": "5b48875f", + "metadata": { + "editable": true + }, "source": [ "$$\n", "MSE(\\boldsymbol{y},\\boldsymbol{\\tilde{y}}) = \\frac{1}{n}\n", @@ -4088,8 +4714,10 @@ }, { "cell_type": "markdown", - "id": "0a80a3b2", - "metadata": {}, + "id": "d842fa2d", + "metadata": { + "editable": true + }, "source": [ "and the $R^2$ score function.\n", "If $\\tilde{\\boldsymbol{y}}_i$ is the predicted value of the $i-th$ sample and $y_i$ is the corresponding true value, then the score $R^2$ is defined as" @@ -4097,8 +4725,10 @@ }, { "cell_type": "markdown", - "id": "8cc1e421", - "metadata": {}, + "id": "e7a29f92", + "metadata": { + "editable": true + }, "source": [ "$$\n", "R^2(\\boldsymbol{y}, \\tilde{\\boldsymbol{y}}) = 1 - \\frac{\\sum_{i=0}^{n - 1} (y_i - \\tilde{y}_i)^2}{\\sum_{i=0}^{n - 1} (y_i - \\bar{y})^2},\n", @@ -4107,16 +4737,20 @@ }, { "cell_type": "markdown", - "id": "8d7a5fbf", - "metadata": {}, + "id": "ee02fdd9", + "metadata": { + "editable": true + }, "source": [ "where we have defined the mean value of $\\boldsymbol{y}$ as" ] }, { "cell_type": "markdown", - "id": "fc2e32e9", - "metadata": {}, + "id": "de2127a5", + "metadata": { + "editable": true + }, "source": [ "$$\n", "\\bar{y} = \\frac{1}{n} \\sum_{i=0}^{n - 1} y_i.\n", @@ -4125,8 +4759,10 @@ }, { "cell_type": "markdown", - "id": "70c1826e", - "metadata": {}, + "id": "64146865", + "metadata": { + "editable": true + }, "source": [ "You can use the functionality included in scikit-learn. If you feel for it, you can use your own program and define functions which compute the above two functions. \n", "Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits." @@ -4134,8 +4770,10 @@ }, { "cell_type": "markdown", - "id": "0fba2f0a", - "metadata": {}, + "id": "44a86bc7", + "metadata": { + "editable": true + }, "source": [ "## Exercise 3: Split data in test and training data\n", "\n", @@ -4151,8 +4789,11 @@ { "cell_type": "code", "execution_count": 47, - "id": "ff051f0b", - "metadata": {}, + "id": "557068d0", + "metadata": { + "collapsed": false, + "editable": true + }, "outputs": [], "source": [ "np.random.seed()\n", @@ -4164,16 +4805,20 @@ }, { "cell_type": "markdown", - "id": "d3911470", - "metadata": {}, + "id": "415b904c", + "metadata": { + "editable": true + }, "source": [ "where $y$ is the function we want to fit with a given polynomial." ] }, { "cell_type": "markdown", - "id": "d888f3b2", - "metadata": {}, + "id": "65a43b24", + "metadata": { + "editable": true + }, "source": [ "**a)**\n", "Write a first code which sets up a design matrix $X$ defined by a fifth-order polynomial and split your data set in training and test data." @@ -4181,8 +4826,10 @@ }, { "cell_type": "markdown", - "id": "87329319", - "metadata": {}, + "id": "ca9e41cf", + "metadata": { + "editable": true + }, "source": [ "**b)**\n", "Write thereafter (using either **scikit-learn** or your matrix inversion code using for example **numpy**)\n", @@ -4191,33 +4838,17 @@ }, { "cell_type": "markdown", - "id": "e599eaec", - "metadata": {}, + "id": "c9896faa", + "metadata": { + "editable": true + }, "source": [ "**c)**\n", "Add now a model which allows you to make polynomials up to degree $15$. Perform a standard OLS fitting of the training data and compute the MSE for the training and test data and plot both test and training data MSE as functions of the polynomial degree. Compare what you see with Figure 2.11 of Hastie et al. Comment your results. For which polynomial degree do you find an optimal MSE (smallest value)?" ] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.18" - } - }, + "metadata": {}, "nbformat": 4, "nbformat_minor": 5 } diff --git a/doc/src/week34/week34.do.txt b/doc/src/week34/week34.do.txt index 8ce8ffd59..a3059e304 100644 --- a/doc/src/week34/week34.do.txt +++ b/doc/src/week34/week34.do.txt @@ -14,7 +14,7 @@ o There are four groups: * Wednesdays 815am-12pm and 1215pm-4pm. o On Mondays we have a regular lecture which will be organized as a mix of active learning sessions and regular lectures. These lectures/active learning sessions start at 1015am and end at 12pm and serve the aims of giving an overview over various topics as well as solving specific problems. These lectures will also be recorded. - * "Link to recording of lecture TBA":"https://youtu.be/" + * "Link to recording of lecture":"https://youtu.be/82IPtCrzbhs" The labs are also available till 6pm Tuesdays and Wednesdays. Videos and learning material with reading suggestions will be made available before each week starts.