diff --git a/doc/pub/week36/ipynb/week36.ipynb b/doc/pub/week36/ipynb/week36.ipynb index ce8118002..6da928fb8 100644 --- a/doc/pub/week36/ipynb/week36.ipynb +++ b/doc/pub/week36/ipynb/week36.ipynb @@ -3,9 +3,7 @@ { "cell_type": "markdown", "id": "66ea8ed4", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "\n", @@ -15,9 +13,7 @@ { "cell_type": "markdown", "id": "79ce282c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "# Week 36: Linear Rgeression and Statistical interpretations\n", "**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n", @@ -28,9 +24,7 @@ { "cell_type": "markdown", "id": "ed85c38c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Plans for week 36\n", "\n", @@ -58,9 +52,7 @@ { "cell_type": "markdown", "id": "faf98f14", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Material for lecture Monday September 2" ] @@ -68,9 +60,7 @@ { "cell_type": "markdown", "id": "203d6afa", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Important technicalities: More on Rescaling data\n", "\n", @@ -125,10 +115,7 @@ "cell_type": "code", "execution_count": 1, "id": "e3f345b9", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", @@ -152,9 +139,7 @@ { "cell_type": "markdown", "id": "d382904b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Let us try to understand what this may imply mathematically when we\n", "subtract the mean values, also known as *zero centering*. For\n", @@ -166,9 +151,7 @@ { "cell_type": "markdown", "id": "9a2d4217", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\beta_0, \\beta_1, ... , \\beta_{p-1}) = \\frac{1}{n}\\sum_{i=0}^{n} \\left(y_i - \\beta_0 - \\sum_{j=1}^{p-1} X_{ij}\\beta_j\\right)^2,.\n", @@ -178,9 +161,7 @@ { "cell_type": "markdown", "id": "1963de4b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Recall also that we use the squared value. This expression can lead to an\n", "increased penalty for higher differences between predicted and\n", @@ -195,9 +176,7 @@ { "cell_type": "markdown", "id": "09a545ff", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\frac{\\partial C}{\\partial \\beta_j} = 0,\n", @@ -207,9 +186,7 @@ { "cell_type": "markdown", "id": "6384894b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "for all $j$. For $\\beta_0$ we have" ] @@ -217,9 +194,7 @@ { "cell_type": "markdown", "id": "b72ca042", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\frac{\\partial C}{\\partial \\beta_0} = -\\frac{2}{n}\\sum_{i=0}^{n-1} \\left(y_i - \\beta_0 - \\sum_{j=1}^{p-1} X_{ij} \\beta_j\\right).\n", @@ -229,9 +204,7 @@ { "cell_type": "markdown", "id": "b193f08c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Multiplying away the constant $2/n$, we obtain" ] @@ -239,9 +212,7 @@ { "cell_type": "markdown", "id": "d05df72f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\sum_{i=0}^{n-1} \\beta_0 = \\sum_{i=0}^{n-1}y_i - \\sum_{i=0}^{n-1} \\sum_{j=1}^{p-1} X_{ij} \\beta_j.\n", @@ -251,9 +222,7 @@ { "cell_type": "markdown", "id": "708ab439", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Let us specialize first to the case where we have only two parameters $\\beta_0$ and $\\beta_1$.\n", "Our result for $\\beta_0$ simplifies then to" @@ -262,9 +231,7 @@ { "cell_type": "markdown", "id": "bfc8322f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "n\\beta_0 = \\sum_{i=0}^{n-1}y_i - \\sum_{i=0}^{n-1} X_{i1} \\beta_1.\n", @@ -274,9 +241,7 @@ { "cell_type": "markdown", "id": "ea8a6fa5", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We obtain then" ] @@ -284,9 +249,7 @@ { "cell_type": "markdown", "id": "db3ef9e5", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\beta_0 = \\frac{1}{n}\\sum_{i=0}^{n-1}y_i - \\beta_1\\frac{1}{n}\\sum_{i=0}^{n-1} X_{i1}.\n", @@ -296,9 +259,7 @@ { "cell_type": "markdown", "id": "0fcdc688", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "If we define" ] @@ -306,9 +267,7 @@ { "cell_type": "markdown", "id": "3e7f32ff", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\mu_{\\boldsymbol{x}_1}=\\frac{1}{n}\\sum_{i=0}^{n-1} X_{i1},\n", @@ -318,9 +277,7 @@ { "cell_type": "markdown", "id": "5803cedf", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and the mean value of the outputs as" ] @@ -328,9 +285,7 @@ { "cell_type": "markdown", "id": "884ca2e2", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\mu_y=\\frac{1}{n}\\sum_{i=0}^{n-1}y_i,\n", @@ -340,9 +295,7 @@ { "cell_type": "markdown", "id": "633c4189", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "we have" ] @@ -350,9 +303,7 @@ { "cell_type": "markdown", "id": "1c60b743", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\beta_0 = \\mu_y - \\beta_1\\mu_{\\boldsymbol{x}_1}.\n", @@ -362,9 +313,7 @@ { "cell_type": "markdown", "id": "cb3025b6", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "In the general case with more parameters than $\\beta_0$ and $\\beta_1$, we have" ] @@ -372,9 +321,7 @@ { "cell_type": "markdown", "id": "01d272fc", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\beta_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}\\beta_j.\n", @@ -384,9 +331,7 @@ { "cell_type": "markdown", "id": "794acece", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We can rewrite the latter equation as" ] @@ -394,9 +339,7 @@ { "cell_type": "markdown", "id": "997ab0ab", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\beta_0 = \\frac{1}{n}\\sum_{i=0}^{n-1}y_i - \\sum_{j=1}^{p-1} \\mu_{\\boldsymbol{x}_j}\\beta_j,\n", @@ -406,9 +349,7 @@ { "cell_type": "markdown", "id": "952c8b2c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "where we have defined" ] @@ -416,9 +357,7 @@ { "cell_type": "markdown", "id": "08835cf9", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\mu_{\\boldsymbol{x}_j}=\\frac{1}{n}\\sum_{i=0}^{n-1} X_{ij},\n", @@ -428,9 +367,7 @@ { "cell_type": "markdown", "id": "416bf355", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "the mean value for all elements of the column vector $\\boldsymbol{x}_j$.\n", "\n", @@ -440,9 +377,7 @@ { "cell_type": "markdown", "id": "c37bccef", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta}) = (\\boldsymbol{\\tilde{y}} - \\tilde{X}\\boldsymbol{\\beta})^T(\\boldsymbol{\\tilde{y}} - \\tilde{X}\\boldsymbol{\\beta}).\n", @@ -452,9 +387,7 @@ { "cell_type": "markdown", "id": "461d332f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "If we minimize with respect to $\\boldsymbol{\\beta}$ we have then" ] @@ -462,9 +395,7 @@ { "cell_type": "markdown", "id": "572cd133", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}} = (\\tilde{X}^T\\tilde{X})^{-1}\\tilde{X}^T\\boldsymbol{\\tilde{y}},\n", @@ -474,9 +405,7 @@ { "cell_type": "markdown", "id": "d73a2b5d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "where $\\boldsymbol{\\tilde{y}} = \\boldsymbol{y} - \\overline{\\boldsymbol{y}}$\n", "and $\\tilde{X}_{ij} = X_{ij} - \\frac{1}{n}\\sum_{k=0}^{n-1}X_{kj}$.\n", @@ -487,9 +416,7 @@ { "cell_type": "markdown", "id": "3da49213", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}} = (\\tilde{X}^T\\tilde{X} + \\lambda I)^{-1}\\tilde{X}^T\\boldsymbol{\\tilde{y}}.\n", @@ -499,9 +426,7 @@ { "cell_type": "markdown", "id": "6e099555", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "What does this mean? And why do we insist on all this? Let us look at some examples.\n", "\n", @@ -513,10 +438,7 @@ "cell_type": "code", "execution_count": 2, "id": "fc066ea4", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", @@ -612,9 +534,7 @@ { "cell_type": "markdown", "id": "8b52bb8a", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "The intercept is the value of our output/target variable\n", "when all our features are zero and our function crosses the $y$-axis (for a one-dimensional case). \n", @@ -633,9 +553,7 @@ { "cell_type": "markdown", "id": "68ac9f59", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\lambda \\vert\\vert \\boldsymbol{\\beta} \\vert\\vert_2^2 = \\lambda \\sum_{j=0}^{p-1}\\beta_j^2,\n", @@ -645,9 +563,7 @@ { "cell_type": "markdown", "id": "b6d9edc5", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "but when we take out the intercept, this equation becomes" ] @@ -655,9 +571,7 @@ { "cell_type": "markdown", "id": "95b2e91e", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\lambda \\vert\\vert \\boldsymbol{\\beta} \\vert\\vert_2^2 = \\lambda \\sum_{j=1}^{p-1}\\beta_j^2.\n", @@ -667,9 +581,7 @@ { "cell_type": "markdown", "id": "e156d9fe", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "For Lasso regression we have" ] @@ -677,9 +589,7 @@ { "cell_type": "markdown", "id": "85a5a3f6", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\lambda \\vert\\vert \\boldsymbol{\\beta} \\vert\\vert_1 = \\lambda \\sum_{j=1}^{p-1}\\vert\\beta_j\\vert.\n", @@ -689,9 +599,7 @@ { "cell_type": "markdown", "id": "229077e1", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "It means that, when scaling the design matrix and the outputs/targets,\n", "by subtracting the mean values, we have an optimization problem which\n", @@ -707,10 +615,7 @@ "cell_type": "code", "execution_count": 3, "id": "7ed86f8e", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", @@ -783,9 +688,7 @@ { "cell_type": "markdown", "id": "30c962cb", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "The results here agree when we force **Scikit-Learn**'s Ridge function to include the first column in our design matrix.\n", "We see that the results agree very well. Here we have thus explicitely included the intercept column in the design matrix.\n", @@ -797,10 +700,7 @@ "cell_type": "code", "execution_count": 4, "id": "a0ae3089", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", @@ -886,9 +786,7 @@ { "cell_type": "markdown", "id": "1c8ba407", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We see here, when compared to the code which includes explicitely the\n", "intercept column, that our MSE value is actually smaller. This is\n", @@ -901,9 +799,7 @@ { "cell_type": "markdown", "id": "b2fd1861", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Test Function for what happens with OLS, Ridge and Lasso\n", "\n", @@ -927,10 +823,7 @@ "cell_type": "code", "execution_count": 5, "id": "fc3a2cef", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", @@ -1000,9 +893,7 @@ { "cell_type": "markdown", "id": "ab2848a3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "How can we understand this?" ] @@ -1010,9 +901,7 @@ { "cell_type": "markdown", "id": "1a39d66b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Linking the regression analysis with a statistical interpretation\n", "\n", @@ -1039,9 +928,7 @@ { "cell_type": "markdown", "id": "c80ed3da", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\begin{align*} \n", @@ -1055,9 +942,7 @@ { "cell_type": "markdown", "id": "bd53a459", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "The randomness of $\\varepsilon_i$ implies that\n", "$\\mathbf{y}_i$ is also a random variable. In particular,\n", @@ -1074,9 +959,7 @@ { "cell_type": "markdown", "id": "9d7404c8", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Assumptions made\n", "\n", @@ -1088,9 +971,7 @@ { "cell_type": "markdown", "id": "113ca56d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{y} = f(\\boldsymbol{x})+\\boldsymbol{\\varepsilon}\n", @@ -1100,9 +981,7 @@ { "cell_type": "markdown", "id": "f8899915", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We approximate this function with our model from the solution of the linear regression equations, that is our\n", "function $f$ is approximated by $\\boldsymbol{\\tilde{y}}$ where we want to minimize $(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2$, our MSE, with" @@ -1111,9 +990,7 @@ { "cell_type": "markdown", "id": "ec411c8d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\tilde{y}} = \\boldsymbol{X}\\boldsymbol{\\beta}.\n", @@ -1123,9 +1000,7 @@ { "cell_type": "markdown", "id": "8158c2fd", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Expectation value and variance\n", "\n", @@ -1135,9 +1010,7 @@ { "cell_type": "markdown", "id": "8040f293", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\begin{align*} \n", @@ -1151,9 +1024,7 @@ { "cell_type": "markdown", "id": "bc6e1bab", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "while\n", "its variance is" @@ -1162,9 +1033,7 @@ { "cell_type": "markdown", "id": "932e6746", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\begin{align*} \\mbox{Var}(y_i) & = \\mathbb{E} \\{ [y_i\n", @@ -1185,9 +1054,7 @@ { "cell_type": "markdown", "id": "ca2c3e6f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Hence, $y_i \\sim \\mathcal{N}( \\mathbf{X}_{i, \\ast} \\, \\boldsymbol{\\beta}, \\sigma^2)$, that is $\\boldsymbol{y}$ follows a normal distribution with \n", "mean value $\\boldsymbol{X}\\boldsymbol{\\beta}$ and variance $\\sigma^2$ (not be confused with the singular values of the SVD)." @@ -1196,9 +1063,7 @@ { "cell_type": "markdown", "id": "bd423094", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Expectation value and variance for $\\boldsymbol{\\beta}$\n", "\n", @@ -1208,9 +1073,7 @@ { "cell_type": "markdown", "id": "78741c91", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\mathbb{E}(\\boldsymbol{\\hat{\\beta}}) = \\mathbb{E}[ (\\mathbf{X}^{\\top} \\mathbf{X})^{-1}\\mathbf{X}^{T} \\mathbf{Y}]=(\\mathbf{X}^{T} \\mathbf{X})^{-1}\\mathbf{X}^{T} \\mathbb{E}[ \\mathbf{Y}]=(\\mathbf{X}^{T} \\mathbf{X})^{-1} \\mathbf{X}^{T}\\mathbf{X}\\boldsymbol{\\beta}=\\boldsymbol{\\beta}.\n", @@ -1220,9 +1083,7 @@ { "cell_type": "markdown", "id": "72a9fdce", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "This means that the estimator of the regression parameters is unbiased.\n", "\n", @@ -1234,9 +1095,7 @@ { "cell_type": "markdown", "id": "02a6f171", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\begin{eqnarray*}\n", @@ -1265,9 +1124,7 @@ { "cell_type": "markdown", "id": "d435171e", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "where we have used that $\\mathbb{E} (\\mathbf{Y} \\mathbf{Y}^{T}) =\n", "\\mathbf{X} \\, \\boldsymbol{\\beta} \\, \\boldsymbol{\\beta}^{T} \\, \\mathbf{X}^{T} +\n", @@ -1287,9 +1144,7 @@ { "cell_type": "markdown", "id": "8c6b6fcf", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\mathbb{E} \\big[ \\boldsymbol{\\beta}^{\\mathrm{Ridge}} \\big]=(\\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I}_{pp})^{-1} (\\mathbf{X}^{\\top} \\mathbf{X})\\boldsymbol{\\beta}^{\\mathrm{OLS}}.\n", @@ -1299,9 +1154,7 @@ { "cell_type": "markdown", "id": "3c3fa21c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We see clearly that \n", "$\\mathbb{E} \\big[ \\boldsymbol{\\beta}^{\\mathrm{Ridge}} \\big] \\not= \\boldsymbol{\\beta}^{\\mathrm{OLS}}$ for any $\\lambda > 0$. We say then that the ridge estimator is biased.\n", @@ -1312,9 +1165,7 @@ { "cell_type": "markdown", "id": "ab5a68b3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\mbox{Var}[\\boldsymbol{\\beta}^{\\mathrm{Ridge}}]=\\sigma^2[ \\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1} \\mathbf{X}^{T} \\mathbf{X} \\{ [ \\mathbf{X}^{\\top} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1}\\}^{T},\n", @@ -1324,9 +1175,7 @@ { "cell_type": "markdown", "id": "cc57cd3e", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and it is easy to see that if the parameter $\\lambda$ goes to infinity then the variance of Ridge parameters $\\boldsymbol{\\beta}$ goes to zero. \n", "\n", @@ -1336,9 +1185,7 @@ { "cell_type": "markdown", "id": "c6790592", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\mbox{Var}[\\boldsymbol{\\beta}^{\\mathrm{OLS}}]-\\mbox{Var}(\\boldsymbol{\\beta}^{\\mathrm{Ridge}})=\\sigma^2 [ \\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1}[ 2\\lambda\\mathbf{I} + \\lambda^2 (\\mathbf{X}^{T} \\mathbf{X})^{-1} ] \\{ [ \\mathbf{X}^{T} \\mathbf{X} + \\lambda \\mathbf{I} ]^{-1}\\}^{T}.\n", @@ -1348,9 +1195,7 @@ { "cell_type": "markdown", "id": "4cedacf8", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "The difference is non-negative definite since each component of the\n", "matrix product is non-negative definite. \n", @@ -1360,9 +1205,7 @@ { "cell_type": "markdown", "id": "8db6c289", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Deriving OLS from a probability distribution\n", "\n", @@ -1383,9 +1226,7 @@ { "cell_type": "markdown", "id": "84ddde11", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "y_i\\sim \\mathcal{N}(\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta}, \\sigma^2)=\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}.\n", @@ -1395,9 +1236,7 @@ { "cell_type": "markdown", "id": "d50973f1", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Independent and Identically Distributed (iid)\n", "\n", @@ -1408,9 +1247,7 @@ { "cell_type": "markdown", "id": "697b0e8d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(y_i, \\boldsymbol{X}\\vert\\boldsymbol{\\beta})=\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]},\n", @@ -1420,9 +1257,7 @@ { "cell_type": "markdown", "id": "8076ed56", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which reads as finding the likelihood of an event $y_i$ with the input variables $\\boldsymbol{X}$ given the parameters (to be determined) $\\boldsymbol{\\beta}$.\n", "\n", @@ -1432,9 +1267,7 @@ { "cell_type": "markdown", "id": "a74b5da5", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(\\boldsymbol{y},\\boldsymbol{X}\\vert\\boldsymbol{\\beta})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}=\\prod_{i=0}^{n-1}p(y_i,\\boldsymbol{X}\\vert\\boldsymbol{\\beta}).\n", @@ -1444,9 +1277,7 @@ { "cell_type": "markdown", "id": "2b6cd0d8", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We will write this in a more compact form reserving $\\boldsymbol{D}$ for the domain of events, including the ouputs (targets) and the inputs. That is\n", "in case we have a simple one-dimensional input and output case" @@ -1455,9 +1286,7 @@ { "cell_type": "markdown", "id": "2e757da3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\\dots, (x_{n-1},y_{n-1})].\n", @@ -1467,9 +1296,7 @@ { "cell_type": "markdown", "id": "6d256b3f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "In the more general case the various inputs should be replaced by the possible features represented by the input data set $\\boldsymbol{X}$. \n", "We can now rewrite the above probability as" @@ -1478,9 +1305,7 @@ { "cell_type": "markdown", "id": "417f2dfa", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}.\n", @@ -1490,9 +1315,7 @@ { "cell_type": "markdown", "id": "8c071fba", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "It is a conditional probability (see below) and reads as the likelihood of a domain of events $\\boldsymbol{D}$ given a set of parameters $\\boldsymbol{\\beta}$." ] @@ -1500,9 +1323,7 @@ { "cell_type": "markdown", "id": "e3190436", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Maximum Likelihood Estimation (MLE)\n", "\n", @@ -1531,9 +1352,7 @@ { "cell_type": "markdown", "id": "90dafea3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## A new Cost Function\n", "\n", @@ -1543,9 +1362,7 @@ { "cell_type": "markdown", "id": "77a99eca", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta}=-\\log{\\prod_{i=0}^{n-1}p(y_i,\\boldsymbol{X}\\vert\\boldsymbol{\\beta})}=-\\sum_{i=0}^{n-1}\\log{p(y_i,\\boldsymbol{X}\\vert\\boldsymbol{\\beta})},\n", @@ -1555,9 +1372,7 @@ { "cell_type": "markdown", "id": "fd43947c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which becomes" ] @@ -1565,9 +1380,7 @@ { "cell_type": "markdown", "id": "94454b77", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta}=\\frac{n}{2}\\log{2\\pi\\sigma^2}+\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}.\n", @@ -1577,9 +1390,7 @@ { "cell_type": "markdown", "id": "25f0b40e", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Taking the derivative of the *new* cost function with respect to the parameters $\\beta$ we recognize our familiar OLS equation, namely" ] @@ -1587,9 +1398,7 @@ { "cell_type": "markdown", "id": "31a7da5a", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right) =0,\n", @@ -1599,9 +1408,7 @@ { "cell_type": "markdown", "id": "340ab07c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which leads to the well-known OLS equation for the optimal paramters $\\beta$" ] @@ -1609,9 +1416,7 @@ { "cell_type": "markdown", "id": "6f9b8d49", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}!\n", @@ -1621,9 +1426,7 @@ { "cell_type": "markdown", "id": "2ba639bc", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Before we make a similar analysis for Ridge and Lasso regression, we need a short reminder on statistics." ] @@ -1631,9 +1434,7 @@ { "cell_type": "markdown", "id": "1ac764de", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## More basic Statistics and Bayes' theorem\n", "\n", @@ -1651,9 +1452,7 @@ { "cell_type": "markdown", "id": "ee7eb0ec", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(X \\cup Y)= p(X)+p(Y)-p(X \\cap Y).\n", @@ -1663,9 +1462,7 @@ { "cell_type": "markdown", "id": "a7c1c442", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "**The product rule (aka joint probability) is given by.**" ] @@ -1673,9 +1470,7 @@ { "cell_type": "markdown", "id": "902c9e42", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(X \\cup Y)= p(X,Y)= p(X\\vert Y)p(Y)=p(Y\\vert X)p(X),\n", @@ -1685,9 +1480,7 @@ { "cell_type": "markdown", "id": "01385522", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "where we read $p(X\\vert Y)$ as the likelihood of obtaining $X$ given $Y$.\n", "\n", @@ -1697,9 +1490,7 @@ { "cell_type": "markdown", "id": "9a3157bc", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Marginal Probability\n", "\n", @@ -1709,9 +1500,7 @@ { "cell_type": "markdown", "id": "8bef4a71", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(X)=\\sum_{i=0}^{n-1}p(X,Y=y_i)=\\sum_{i=0}^{n-1}p(X\\vert Y=y_i)p(Y=y_i)=\\sum_{i=0}^{n-1}p(X\\vert y_i)p(y_i).\n", @@ -1721,9 +1510,7 @@ { "cell_type": "markdown", "id": "3e6fb4d8", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Conditional Probability\n", "\n", @@ -1733,9 +1520,7 @@ { "cell_type": "markdown", "id": "72979501", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(X\\vert Y)= \\frac{p(X,Y)}{p(Y)}=\\frac{p(X,Y)}{\\sum_{i=0}^{n-1}p(Y\\vert X=x_i)p(x_i)}.\n", @@ -1745,9 +1530,7 @@ { "cell_type": "markdown", "id": "1e9dfa57", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Bayes' Theorem\n", "\n", @@ -1757,9 +1540,7 @@ { "cell_type": "markdown", "id": "911e3e33", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(X\\vert Y)= \\frac{p(X,Y)}{p(Y)},\n", @@ -1769,9 +1550,7 @@ { "cell_type": "markdown", "id": "638834de", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which we can rewrite as" ] @@ -1779,9 +1558,7 @@ { "cell_type": "markdown", "id": "bb77868f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(X\\vert Y)= \\frac{p(X,Y)}{\\sum_{i=0}^{n-1}p(Y\\vert X=x_i)p(x_i)}=\\frac{p(Y\\vert X)p(X)}{\\sum_{i=0}^{n-1}p(Y\\vert X=x_i)p(x_i)},\n", @@ -1791,9 +1568,7 @@ { "cell_type": "markdown", "id": "0a7a9efd", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which is Bayes' theorem. It allows us to evaluate the uncertainty in in $X$ after we have observed $Y$. We can easily interchange $X$ with $Y$." ] @@ -1801,9 +1576,7 @@ { "cell_type": "markdown", "id": "d7c9013f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Interpretations of Bayes' Theorem\n", "\n", @@ -1820,9 +1593,7 @@ { "cell_type": "markdown", "id": "58f244e9", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Example of Usage of Bayes' theorem\n", "\n", @@ -1841,9 +1612,7 @@ { "cell_type": "markdown", "id": "174309f3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(X=1\\vert Y=1) =0.8.\n", @@ -1853,9 +1622,7 @@ { "cell_type": "markdown", "id": "e31dfe4b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "This obviously sounds scary since many would conclude that if the test is positive, there is a likelihood of $80\\%$ for having cancer.\n", "It is however not correct, as the following Bayesian analysis shows." @@ -1864,9 +1631,7 @@ { "cell_type": "markdown", "id": "cb4db137", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Doing it correctly\n", "\n", @@ -1877,9 +1642,7 @@ { "cell_type": "markdown", "id": "8b391eda", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(Y=1) =0.004.\n", @@ -1889,9 +1652,7 @@ { "cell_type": "markdown", "id": "99f66158", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We need also to account for the fact that the test may produce a false positive result (false alarm). Let us here assume that we have" ] @@ -1899,9 +1660,7 @@ { "cell_type": "markdown", "id": "f8ae73a6", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(X=1\\vert Y=0) =0.1.\n", @@ -1911,9 +1670,7 @@ { "cell_type": "markdown", "id": "c3e378e4", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Using Bayes' theorem we can then find the posterior probability that the person has breast cancer in case of a positive test, that is we can compute" ] @@ -1921,9 +1678,7 @@ { "cell_type": "markdown", "id": "2f684426", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(Y=1\\vert X=1)=\\frac{p(X=1\\vert Y=1)p(Y=1)}{p(X=1\\vert Y=1)p(Y=1)+p(X=1\\vert Y=0)p(Y=0)}=\\frac{0.8\\times 0.004}{0.8\\times 0.004+0.1\\times 0.996}=0.031.\n", @@ -1933,9 +1688,7 @@ { "cell_type": "markdown", "id": "983982c0", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "That is, in case of a positive test, there is only a $3\\%$ chance of having breast cancer!" ] @@ -1943,9 +1696,7 @@ { "cell_type": "markdown", "id": "88ef6911", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Bayes' Theorem and Ridge and Lasso Regression\n", "\n", @@ -1957,9 +1708,7 @@ { "cell_type": "markdown", "id": "ba0657c3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{D}=[(x_0,y_0), (x_1,y_1),\\dots, (x_{n-1},y_{n-1})],\n", @@ -1969,9 +1718,7 @@ { "cell_type": "markdown", "id": "f8aa4829", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "is given by" ] @@ -1979,9 +1726,7 @@ { "cell_type": "markdown", "id": "52adbfd2", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}.\n", @@ -1991,9 +1736,7 @@ { "cell_type": "markdown", "id": "2178fe65", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "In Bayes' theorem this function plays the role of the so-called likelihood. We could now ask the question what is the posterior probability of a parameter set $\\boldsymbol{\\beta}$ given a domain of events $\\boldsymbol{D}$? That is, how can we define the posterior probability" ] @@ -2001,9 +1744,7 @@ { "cell_type": "markdown", "id": "97ce5044", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(\\boldsymbol{\\beta}\\vert\\boldsymbol{D}).\n", @@ -2013,9 +1754,7 @@ { "cell_type": "markdown", "id": "afae14bb", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Bayes' theorem comes to our rescue here since (omitting the normalization constant)" ] @@ -2023,9 +1762,7 @@ { "cell_type": "markdown", "id": "3a3fea10", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(\\boldsymbol{\\beta}\\vert\\boldsymbol{D})\\propto p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})p(\\boldsymbol{\\beta}).\n", @@ -2035,9 +1772,7 @@ { "cell_type": "markdown", "id": "aedd41ba", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We have a model for $p(\\boldsymbol{D}\\vert\\boldsymbol{\\beta})$ but need one for the **prior** $p(\\boldsymbol{\\beta}$!" ] @@ -2045,9 +1780,7 @@ { "cell_type": "markdown", "id": "fe2f2354", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Ridge and Bayes\n", "\n", @@ -2061,9 +1794,7 @@ { "cell_type": "markdown", "id": "ce5820c0", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(\\boldsymbol{\\beta})=\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\beta_j^2}{2\\tau^2}\\right)}.\n", @@ -2073,9 +1804,7 @@ { "cell_type": "markdown", "id": "186e4fbd", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Our posterior probability becomes then (omitting the normalization factor which is just a constant)" ] @@ -2083,9 +1812,7 @@ { "cell_type": "markdown", "id": "4e3b13c4", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(\\boldsymbol{\\beta\\vert\\boldsymbol{D})}=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\beta_j^2}{2\\tau^2}\\right)}.\n", @@ -2095,9 +1822,7 @@ { "cell_type": "markdown", "id": "501fe4bf", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We can now optimize this quantity with respect to $\\boldsymbol{\\beta}$. As we\n", "did for OLS, this is most conveniently done by taking the negative\n", @@ -2108,9 +1833,7 @@ { "cell_type": "markdown", "id": "37e09a0d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\frac{1}{2\\tau^2}\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_2^2,\n", @@ -2120,9 +1843,7 @@ { "cell_type": "markdown", "id": "e2d62663", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and replacing $1/2\\tau^2$ with $\\lambda$ we have" ] @@ -2130,9 +1851,7 @@ { "cell_type": "markdown", "id": "795eb5be", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_2^2,\n", @@ -2142,9 +1861,7 @@ { "cell_type": "markdown", "id": "be730c19", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which is our Ridge cost function! Nice, isn't it?" ] @@ -2152,9 +1869,7 @@ { "cell_type": "markdown", "id": "755ba46f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Lasso and Bayes\n", "\n", @@ -2164,9 +1879,7 @@ { "cell_type": "markdown", "id": "99493766", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(\\boldsymbol{\\beta})=\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\vert\\beta_j\\vert}{\\tau}\\right)}.\n", @@ -2176,9 +1889,7 @@ { "cell_type": "markdown", "id": "cc738267", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Our posterior probability becomes then (omitting the normalization factor which is just a constant)" ] @@ -2186,9 +1897,7 @@ { "cell_type": "markdown", "id": "fd28fe2f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "p(\\boldsymbol{\\beta}\\vert\\boldsymbol{D})=\\prod_{i=0}^{n-1}\\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp{\\left[-\\frac{(y_i-\\boldsymbol{X}_{i,*}\\boldsymbol{\\beta})^2}{2\\sigma^2}\\right]}\\prod_{j=0}^{p-1}\\exp{\\left(-\\frac{\\vert\\beta_j\\vert}{\\tau}\\right)}.\n", @@ -2198,9 +1907,7 @@ { "cell_type": "markdown", "id": "c4af71ac", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Taking the negative\n", "logarithm of the posterior probability and leaving out the\n", @@ -2210,9 +1917,7 @@ { "cell_type": "markdown", "id": "3ccfd13a", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta}=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\frac{1}{\\tau}\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n", @@ -2222,9 +1927,7 @@ { "cell_type": "markdown", "id": "d3cb4dac", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and replacing $1/\\tau$ with $\\lambda$ we have" ] @@ -2232,9 +1935,7 @@ { "cell_type": "markdown", "id": "6468d431", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta}=\\frac{\\vert\\vert (\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\vert\\vert_2^2}{2\\sigma^2}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n", @@ -2244,9 +1945,7 @@ { "cell_type": "markdown", "id": "4f08cda3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which is our Lasso cost function!" ] @@ -2254,9 +1953,7 @@ { "cell_type": "markdown", "id": "ab2ab549", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Material for the active learning sessions Tuesday and Wednesday\n", "\n", @@ -2266,9 +1963,7 @@ { "cell_type": "markdown", "id": "e8b1b1af", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Linear Regression and the SVD\n", "\n", @@ -2278,9 +1973,7 @@ { "cell_type": "markdown", "id": "7b21a8cd", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{U}^T\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T=\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma}\\boldsymbol{V}^T.\n", @@ -2290,9 +1983,7 @@ { "cell_type": "markdown", "id": "bed0cd60", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Since the matrices here have dimension $p\\times p$, with $p$ corresponding to the singular values, we defined last week the matrix" ] @@ -2300,9 +1991,7 @@ { "cell_type": "markdown", "id": "15e05a44", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma} = \\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0}\\\\ \\end{bmatrix}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} \\\\ \\boldsymbol{0}\\end{bmatrix},\n", @@ -2312,9 +2001,7 @@ { "cell_type": "markdown", "id": "35b02cee", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "where the tilde-matrix $\\tilde{\\boldsymbol{\\Sigma}}$ is a matrix of dimension $p\\times p$ containing only the singular values $\\sigma_i$, that is" ] @@ -2322,9 +2009,7 @@ { "cell_type": "markdown", "id": "3b44d036", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{\\Sigma}}=\\begin{bmatrix} \\sigma_0 & 0 & 0 & \\dots & 0 & 0 \\\\\n", @@ -2339,9 +2024,7 @@ { "cell_type": "markdown", "id": "fb0a23d3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "meaning we can write" ] @@ -2349,9 +2032,7 @@ { "cell_type": "markdown", "id": "11b857c6", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=\\boldsymbol{V}\\tilde{\\boldsymbol{\\Sigma}}^2\\boldsymbol{V}^T.\n", @@ -2361,9 +2042,7 @@ { "cell_type": "markdown", "id": "32df1603", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Multiplying from the right with $\\boldsymbol{V}$ (using the orthogonality of $\\boldsymbol{V}$) we get" ] @@ -2371,9 +2050,7 @@ { "cell_type": "markdown", "id": "1589aacd", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{V}=\\boldsymbol{V}\\tilde{\\boldsymbol{\\Sigma}}^2.\n", @@ -2383,9 +2060,7 @@ { "cell_type": "markdown", "id": "e9b878c6", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## What does it mean?\n", "\n", @@ -2397,9 +2072,7 @@ { "cell_type": "markdown", "id": "bf01dcea", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)\\boldsymbol{v}_i=\\boldsymbol{v}_i\\sigma_i^2.\n", @@ -2409,9 +2082,7 @@ { "cell_type": "markdown", "id": "8d12b2e4", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "In other words, each non-zero singular value of $\\boldsymbol{X}$ is a positive\n", "square root of an eigenvalue of $\\boldsymbol{X}^T\\boldsymbol{X}$. It means also that\n", @@ -2431,9 +2102,7 @@ { "cell_type": "markdown", "id": "1a6ecf55", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{C}[\\boldsymbol{X}]=\\frac{1}{n}\\boldsymbol{X}^T\\boldsymbol{X},\n", @@ -2443,9 +2112,7 @@ { "cell_type": "markdown", "id": "707eceb6", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "meaning that every squared non-singular value of $\\boldsymbol{X}$ divided by $n$ (\n", "the number of samples) are the eigenvalues of the covariance\n", @@ -2458,9 +2125,7 @@ { "cell_type": "markdown", "id": "096cd515", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## And finally $\\boldsymbol{X}\\boldsymbol{X}^T$\n", "\n", @@ -2470,9 +2135,7 @@ { "cell_type": "markdown", "id": "f518ce45", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}\\boldsymbol{X}^T=\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T\\boldsymbol{V}\\boldsymbol{\\Sigma}^T\\boldsymbol{U}^T=\\boldsymbol{U}\\boldsymbol{\\Sigma}^T\\boldsymbol{\\Sigma}\\boldsymbol{U}^T.\n", @@ -2482,9 +2145,7 @@ { "cell_type": "markdown", "id": "3aee5ef2", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Since the matrices here have dimension $n\\times n$, we have" ] @@ -2492,9 +2153,7 @@ { "cell_type": "markdown", "id": "f57a6398", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\Sigma}\\boldsymbol{\\Sigma}^T = \\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} \\\\ \\boldsymbol{0}\\\\ \\end{bmatrix}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} \\boldsymbol{0}\\\\ \\end{bmatrix}=\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0} \\\\ \\boldsymbol{0} & \\boldsymbol{0}\\\\ \\end{bmatrix},\n", @@ -2504,9 +2163,7 @@ { "cell_type": "markdown", "id": "47fbfc39", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "leading to" ] @@ -2514,9 +2171,7 @@ { "cell_type": "markdown", "id": "ab044c32", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}\\boldsymbol{X}^T=\\boldsymbol{U}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0} \\\\ \\boldsymbol{0} & \\boldsymbol{0}\\\\ \\end{bmatrix}\\boldsymbol{U}^T.\n", @@ -2526,9 +2181,7 @@ { "cell_type": "markdown", "id": "887c7081", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Multiplying with $\\boldsymbol{U}$ from the right gives us the eigenvalue problem" ] @@ -2536,9 +2189,7 @@ { "cell_type": "markdown", "id": "aef54d23", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "(\\boldsymbol{X}\\boldsymbol{X}^T)\\boldsymbol{U}=\\boldsymbol{U}\\begin{bmatrix} \\tilde{\\boldsymbol{\\Sigma}} & \\boldsymbol{0} \\\\ \\boldsymbol{0} & \\boldsymbol{0}\\\\ \\end{bmatrix}.\n", @@ -2548,9 +2199,7 @@ { "cell_type": "markdown", "id": "58545dde", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "It means that the eigenvalues of $\\boldsymbol{X}\\boldsymbol{X}^T$ are again given by\n", "the non-zero singular values plus now a series of zeros. The column\n", @@ -2565,9 +2214,7 @@ { "cell_type": "markdown", "id": "a51460b7", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Code for SVD and Inversion of Matrices\n", "\n", @@ -2579,10 +2226,7 @@ "cell_type": "code", "execution_count": 6, "id": "b9e2d8bf", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", @@ -2593,9 +2237,7 @@ { "cell_type": "markdown", "id": "491c3981", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Let us first look at a matrix which does not causes problems and write our own function where we just use the SVD." ] @@ -2604,10 +2246,7 @@ "cell_type": "code", "execution_count": 7, "id": "33f26b11", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", @@ -2644,9 +2283,7 @@ { "cell_type": "markdown", "id": "b95201dc", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Inverse of Rectangular Matrix\n", "\n", @@ -2664,9 +2301,7 @@ { "cell_type": "markdown", "id": "d1b91d55", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{A}_{\\mathrm{PI}}= \\boldsymbol{V}\\boldsymbol{D}_{\\mathrm{PI}}\\boldsymbol{U}^T,\n", @@ -2676,9 +2311,7 @@ { "cell_type": "markdown", "id": "783cf37c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "where $\\boldsymbol{D}_{\\mathrm{PI}}$ can be calculated by creating a diagonal matrix from $\\boldsymbol{\\Sigma}$ where we only keep the singular values (the non-zero values). The following code computes the pseudoinvers of the matrix based on the SVD." ] @@ -2687,10 +2320,7 @@ "cell_type": "code", "execution_count": 8, "id": "15f3c4fd", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", @@ -2721,9 +2351,7 @@ { "cell_type": "markdown", "id": "cf791d68", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "As you can see from this example, our own decomposition based on the SVD agrees with the pseudoinverse algorithm provided by **Numpy**." ] @@ -2731,9 +2359,7 @@ { "cell_type": "markdown", "id": "e0d6867b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Ridge and LASSO Regression\n", "\n", @@ -2744,9 +2370,7 @@ { "cell_type": "markdown", "id": "1a7560d5", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in {\\mathbb{R}}^{p}}}\\frac{1}{n}\\left\\{\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)^T\\left(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\right)\\right\\}.\n", @@ -2756,9 +2380,7 @@ { "cell_type": "markdown", "id": "8a41ec2e", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "or we can state it as" ] @@ -2766,9 +2388,7 @@ { "cell_type": "markdown", "id": "cd9c1584", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", @@ -2779,9 +2399,7 @@ { "cell_type": "markdown", "id": "a1809fd3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "where we have used the definition of a norm-2 vector, that is" ] @@ -2789,9 +2407,7 @@ { "cell_type": "markdown", "id": "37d1dd03", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\vert\\vert \\boldsymbol{x}\\vert\\vert_2 = \\sqrt{\\sum_i x_i^2}.\n", @@ -2801,9 +2417,7 @@ { "cell_type": "markdown", "id": "2e8f56dc", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## From OLS to Ridge and Lasso\n", "\n", @@ -2816,9 +2430,7 @@ { "cell_type": "markdown", "id": "d709e7cc", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", @@ -2829,9 +2441,7 @@ { "cell_type": "markdown", "id": "75ddcb47", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which leads to the Ridge regression minimization problem where we\n", "require that $\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_2^2\\le t$, where $t$ is\n", @@ -2843,9 +2453,7 @@ { "cell_type": "markdown", "id": "15546e46", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\frac{1}{n}\\vert\\vert \\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta}\\vert\\vert_2^2+\\lambda\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_1,\n", @@ -2855,9 +2463,7 @@ { "cell_type": "markdown", "id": "981f9eaf", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "we have a new optimization equation" ] @@ -2865,9 +2471,7 @@ { "cell_type": "markdown", "id": "cd380e8c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "{\\displaystyle \\min_{\\boldsymbol{\\beta}\\in\n", @@ -2878,9 +2482,7 @@ { "cell_type": "markdown", "id": "3675c6af", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator. \n", "\n", @@ -2890,9 +2492,7 @@ { "cell_type": "markdown", "id": "2e6a4788", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\vert\\vert \\boldsymbol{x}\\vert\\vert_1 = \\sum_i \\vert x_i\\vert.\n", @@ -2902,9 +2502,7 @@ { "cell_type": "markdown", "id": "25da0588", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Deriving the Ridge Regression Equations\n", "\n", @@ -2914,9 +2512,7 @@ { "cell_type": "markdown", "id": "e7cc0e8b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\left\\{(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\right\\}+\\lambda\\boldsymbol{\\beta}^T\\boldsymbol{\\beta},\n", @@ -2926,9 +2522,7 @@ { "cell_type": "markdown", "id": "5a9f8c59", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and \n", "taking the derivatives with respect to $\\boldsymbol{\\beta}$ we obtain then\n", @@ -2940,9 +2534,7 @@ { "cell_type": "markdown", "id": "4ba67adb", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{Ridge}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n", @@ -2952,9 +2544,7 @@ { "cell_type": "markdown", "id": "3f707310", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "with $\\boldsymbol{I}$ being a $p\\times p$ identity matrix with the constraint that" ] @@ -2962,9 +2552,7 @@ { "cell_type": "markdown", "id": "aa1900c6", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\sum_{i=0}^{p-1} \\beta_i^2 \\leq t,\n", @@ -2974,9 +2562,7 @@ { "cell_type": "markdown", "id": "81277a42", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "with $t$ a finite positive number." ] @@ -2984,9 +2570,7 @@ { "cell_type": "markdown", "id": "f0a31465", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Note on Scikit-Learn\n", "\n", @@ -2996,9 +2580,7 @@ { "cell_type": "markdown", "id": "15a2f869", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{Ridge}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}+n\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", @@ -3008,9 +2590,7 @@ { "cell_type": "markdown", "id": "56070d0e", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "In our codes where we compare our own codes with **Scikit-Learn**, we do thus not include the $1/n$ factor in the cost function." ] @@ -3018,9 +2598,7 @@ { "cell_type": "markdown", "id": "1bbfe263", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Comparison with OLS\n", "When we compare this with the ordinary least squares result we have" @@ -3029,9 +2607,7 @@ { "cell_type": "markdown", "id": "2f4f4e30", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_{\\mathrm{OLS}} = \\left(\\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y},\n", @@ -3041,9 +2617,7 @@ { "cell_type": "markdown", "id": "4e995e92", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which can lead to singular matrices. However, with the SVD, we can always compute the inverse of the matrix $\\boldsymbol{X}^T\\boldsymbol{X}$.\n", "\n", @@ -3057,9 +2631,7 @@ { "cell_type": "markdown", "id": "9935ba38", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## SVD analysis\n", "\n", @@ -3070,9 +2642,7 @@ { "cell_type": "markdown", "id": "66971309", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}_{\\mathrm{OLS}}=\\boldsymbol{X}\\boldsymbol{\\beta} =\\boldsymbol{U}\\boldsymbol{U}^T\\boldsymbol{y}.\n", @@ -3082,9 +2652,7 @@ { "cell_type": "markdown", "id": "083cb933", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "For Ridge regression this becomes" ] @@ -3092,9 +2660,7 @@ { "cell_type": "markdown", "id": "2393f70a", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\tilde{\\boldsymbol{y}}_{\\mathrm{Ridge}}=\\boldsymbol{X}\\boldsymbol{\\beta}_{\\mathrm{Ridge}} = \\boldsymbol{U\\Sigma V^T}\\left(\\boldsymbol{V}\\boldsymbol{\\Sigma}^2\\boldsymbol{V}^T+\\lambda\\boldsymbol{I} \\right)^{-1}(\\boldsymbol{U\\Sigma V^T})^T\\boldsymbol{y}=\\sum_{j=0}^{p-1}\\boldsymbol{u}_j\\boldsymbol{u}_j^T\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda}\\boldsymbol{y},\n", @@ -3104,9 +2670,7 @@ { "cell_type": "markdown", "id": "2839df85", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "with the vectors $\\boldsymbol{u}_j$ being the columns of $\\boldsymbol{U}$ from the SVD of the matrix $\\boldsymbol{X}$." ] @@ -3114,9 +2678,7 @@ { "cell_type": "markdown", "id": "725d6507", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Interpreting the Ridge results\n", "\n", @@ -3126,9 +2688,7 @@ { "cell_type": "markdown", "id": "3be3ed05", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\frac{\\sigma_j^2}{\\sigma_j^2+\\lambda} \\leq 1.\n", @@ -3138,9 +2698,7 @@ { "cell_type": "markdown", "id": "42ba7d59", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Ridge regression finds the coordinates of $\\boldsymbol{y}$ with respect to the\n", "orthonormal basis $\\boldsymbol{U}$, it then shrinks the coordinates by\n", @@ -3154,9 +2712,7 @@ { "cell_type": "markdown", "id": "a99bda20", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## More interpretations\n", "\n", @@ -3166,9 +2722,7 @@ { "cell_type": "markdown", "id": "efa137df", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}=(\\boldsymbol{X}^T\\boldsymbol{X})^{-1} =\\boldsymbol{I}.\n", @@ -3178,9 +2732,7 @@ { "cell_type": "markdown", "id": "db30277c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "In this case the standard OLS results in" ] @@ -3188,9 +2740,7 @@ { "cell_type": "markdown", "id": "f0bff457", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{OLS}} = \\boldsymbol{X}^T\\boldsymbol{y}=\\sum_{i=0}^{n-1}\\boldsymbol{u}_i\\boldsymbol{u}_i^T\\boldsymbol{y},\n", @@ -3200,9 +2750,7 @@ { "cell_type": "markdown", "id": "a85d40db", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and" ] @@ -3210,9 +2758,7 @@ { "cell_type": "markdown", "id": "24c0cd1c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{Ridge}} = \\left(\\boldsymbol{I}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}=\\left(1+\\lambda\\right)^{-1}\\boldsymbol{\\beta}^{\\mathrm{OLS}},\n", @@ -3222,9 +2768,7 @@ { "cell_type": "markdown", "id": "1ff9f63d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "that is the Ridge estimator scales the OLS estimator by the inverse of a factor $1+\\lambda$, and\n", "the Ridge estimator converges to zero when the hyperparameter goes to\n", @@ -3239,9 +2783,7 @@ { "cell_type": "markdown", "id": "00573cf2", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Deriving the Lasso Regression Equations\n", "\n", @@ -3251,9 +2793,7 @@ { "cell_type": "markdown", "id": "c9fcb445", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{X},\\boldsymbol{\\beta})=\\frac{1}{n}\\left\\{(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})\\right\\}+\\lambda\\vert\\vert\\boldsymbol{\\beta}\\vert\\vert_1,\n", @@ -3263,9 +2803,7 @@ { "cell_type": "markdown", "id": "b9ade8d2", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Taking the derivative with respect to $\\boldsymbol{\\beta}$ and recalling that the derivative of the absolute value is (we drop the boldfaced vector symbol for simplicity)" ] @@ -3273,9 +2811,7 @@ { "cell_type": "markdown", "id": "28ab82f3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\frac{d \\vert \\beta\\vert}{d \\beta}=\\mathrm{sgn}(\\beta)=\\left\\{\\begin{array}{cc} 1 & \\beta > 0 \\\\-1 & \\beta < 0, \\end{array}\\right.\n", @@ -3285,9 +2821,7 @@ { "cell_type": "markdown", "id": "207f6e1a", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "we have that the derivative of the cost function is" ] @@ -3295,9 +2829,7 @@ { "cell_type": "markdown", "id": "02584f63", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{X},\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}}=-\\frac{2}{n}\\boldsymbol{X}^T(\\boldsymbol{y}-\\boldsymbol{X}\\boldsymbol{\\beta})+\\lambda sgn(\\boldsymbol{\\beta})=0,\n", @@ -3307,9 +2839,7 @@ { "cell_type": "markdown", "id": "d493cd1f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and reordering we have" ] @@ -3317,9 +2847,7 @@ { "cell_type": "markdown", "id": "d9e8a09b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}^T\\boldsymbol{X}\\boldsymbol{\\beta}+\\lambda sgn(\\boldsymbol{\\beta})=\\boldsymbol{X}^T\\boldsymbol{y}.\n", @@ -3329,9 +2857,7 @@ { "cell_type": "markdown", "id": "d0a02f65", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "This equation does not lead to a nice analytical equation as in Ridge regression or ordinary least squares. We have absorbed the factor $2/n$ in a redefinition of the parameter $\\lambda$. We will solve this type of problems using libraries like **scikit-learn**." ] @@ -3339,9 +2865,7 @@ { "cell_type": "markdown", "id": "2045b02f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Simple example to illustrate Ordinary Least Squares, Ridge and Lasso Regression\n", "\n", @@ -3354,9 +2878,7 @@ { "cell_type": "markdown", "id": "0adb3c9d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2,\n", @@ -3366,9 +2888,7 @@ { "cell_type": "markdown", "id": "ca893163", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and minimizing we have that" ] @@ -3376,9 +2896,7 @@ { "cell_type": "markdown", "id": "873ca498", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\beta}_i^{\\mathrm{OLS}} = y_i.\n", @@ -3388,9 +2906,7 @@ { "cell_type": "markdown", "id": "f73c2649", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Ridge Regression\n", "\n", @@ -3400,9 +2916,7 @@ { "cell_type": "markdown", "id": "08489c8d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2+\\lambda\\sum_{i=0}^{p-1}\\beta_i^2,\n", @@ -3412,9 +2926,7 @@ { "cell_type": "markdown", "id": "11e25a08", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and minimizing we have that" ] @@ -3422,9 +2934,7 @@ { "cell_type": "markdown", "id": "2eac8387", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\beta}_i^{\\mathrm{Ridge}} = \\frac{y_i}{1+\\lambda}.\n", @@ -3434,9 +2944,7 @@ { "cell_type": "markdown", "id": "3b52a609", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Lasso Regression\n", "\n", @@ -3446,9 +2954,7 @@ { "cell_type": "markdown", "id": "68745c24", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2+\\lambda\\sum_{i=0}^{p-1}\\vert\\beta_i\\vert=\\sum_{i=0}^{p-1}(y_i-\\beta_i)^2+\\lambda\\sum_{i=0}^{p-1}\\sqrt{\\beta_i^2},\n", @@ -3458,9 +2964,7 @@ { "cell_type": "markdown", "id": "c77b8b18", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and minimizing we have that" ] @@ -3468,9 +2972,7 @@ { "cell_type": "markdown", "id": "f6454cc4", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "-2\\sum_{i=0}^{p-1}(y_i-\\beta_i)+\\lambda \\sum_{i=0}^{p-1}\\frac{(\\beta_i)}{\\vert\\beta_i\\vert}=0,\n", @@ -3480,9 +2982,7 @@ { "cell_type": "markdown", "id": "7874a26a", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which leads to" ] @@ -3490,9 +2990,7 @@ { "cell_type": "markdown", "id": "539a5968", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}_i^{\\mathrm{Lasso}} = \\left\\{\\begin{array}{ccc}y_i-\\frac{\\lambda}{2} &\\mathrm{if} & y_i> \\frac{\\lambda}{2}\\\\\n", @@ -3504,9 +3002,7 @@ { "cell_type": "markdown", "id": "b0cd5e30", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Plotting these results shows clearly that Lasso regression suppresses (sets to zero) values of $\\beta_i$ for specific values of $\\lambda$. Ridge regression reduces on the other hand the values of $\\beta_i$ as function of $\\lambda$." ] @@ -3514,9 +3010,7 @@ { "cell_type": "markdown", "id": "da1f1c65", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Yet another Example\n", "\n", @@ -3526,9 +3020,7 @@ { "cell_type": "markdown", "id": "c30c1157", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{y}=\\begin{bmatrix}4 \\\\ 2 \\\\3\\end{bmatrix},\n", @@ -3538,9 +3030,7 @@ { "cell_type": "markdown", "id": "1fc75552", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and our inputs as a $3\\times 2$ design matrix" ] @@ -3548,9 +3038,7 @@ { "cell_type": "markdown", "id": "a955127d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}=\\begin{bmatrix}2 & 0\\\\ 0 & 1 \\\\ 0 & 0\\end{bmatrix},\n", @@ -3560,9 +3048,7 @@ { "cell_type": "markdown", "id": "2c25c0fd", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "meaning that we have two features and two unknown parameters $\\beta_0$ and $\\beta_1$ to be determined either by ordinary least squares, Ridge or Lasso regression." ] @@ -3570,9 +3056,7 @@ { "cell_type": "markdown", "id": "82979bed", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## The OLS case\n", "\n", @@ -3582,9 +3066,7 @@ { "cell_type": "markdown", "id": "ea2f8d9e", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\left( \\boldsymbol{X}^T\\boldsymbol{X}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", @@ -3594,9 +3076,7 @@ { "cell_type": "markdown", "id": "198468c6", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Inserting the above values we obtain that" ] @@ -3604,9 +3084,7 @@ { "cell_type": "markdown", "id": "c54be5ed", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{OLS}}=\\begin{bmatrix}2 \\\\ 2\\end{bmatrix},\n", @@ -3616,9 +3094,7 @@ { "cell_type": "markdown", "id": "6413ac61", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "The code which implements this simpler case is presented after the discussion of Ridge and Lasso." ] @@ -3626,9 +3102,7 @@ { "cell_type": "markdown", "id": "3fc6d520", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## The Ridge case\n", "\n", @@ -3638,9 +3112,7 @@ { "cell_type": "markdown", "id": "97702dcd", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{Ridge}}=\\left( \\boldsymbol{X}^T\\boldsymbol{X}+\\lambda\\boldsymbol{I}\\right)^{-1}\\boldsymbol{X}^T\\boldsymbol{y}.\n", @@ -3650,9 +3122,7 @@ { "cell_type": "markdown", "id": "44edbaef", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Inserting the above values we obtain that" ] @@ -3660,9 +3130,7 @@ { "cell_type": "markdown", "id": "22d02c34", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\hat{\\boldsymbol{\\beta}}^{\\mathrm{Ridge}}=\\begin{bmatrix}\\frac{8}{4+\\lambda} \\\\ \\frac{2}{1+\\lambda}\\end{bmatrix},\n", @@ -3672,9 +3140,7 @@ { "cell_type": "markdown", "id": "12cf8dc6", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "There is normally a constraint on the value of $\\vert\\vert \\boldsymbol{\\beta}\\vert\\vert_2$ via the parameter $\\lambda$.\n", "Let us for simplicity assume that $\\beta_0^2+\\beta_1^2=1$ as constraint. This will allow us to find an expression for the optimal values of $\\beta$ and $\\lambda$.\n", @@ -3685,9 +3151,7 @@ { "cell_type": "markdown", "id": "941de09b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Writing the Cost Function\n", "\n", @@ -3697,9 +3161,7 @@ { "cell_type": "markdown", "id": "715d1ede", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\boldsymbol{X}\\boldsymbol{\\beta}=\\begin{bmatrix} 2\\beta_0 \\\\ \\beta_1 \\\\0 \\end{bmatrix},\n", @@ -3709,9 +3171,7 @@ { "cell_type": "markdown", "id": "a0dd924b", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=(4-2\\beta_0)^2+(2-\\beta_1)^2+\\lambda(\\beta_0^2+\\beta_1^2),\n", @@ -3721,9 +3181,7 @@ { "cell_type": "markdown", "id": "dd40cb2f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and taking the derivative with respect to $\\beta_0$ we get" ] @@ -3731,9 +3189,7 @@ { "cell_type": "markdown", "id": "f257ab54", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\beta_0=\\frac{8}{4+\\lambda},\n", @@ -3743,9 +3199,7 @@ { "cell_type": "markdown", "id": "ceb84ba5", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and for $\\beta_1$ we obtain" ] @@ -3753,9 +3207,7 @@ { "cell_type": "markdown", "id": "d96cf277", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\beta_1=\\frac{2}{1+\\lambda},\n", @@ -3765,9 +3217,7 @@ { "cell_type": "markdown", "id": "27b2d9c9", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Using the constraint for $\\beta_0^2+\\beta_1^2=1$ we can constrain $\\lambda$ by solving" ] @@ -3775,9 +3225,7 @@ { "cell_type": "markdown", "id": "d53c88da", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\left(\\frac{8}{4+\\lambda}\\right)^2+\\left(\\frac{2}{1+\\lambda}\\right)^2=1,\n", @@ -3787,9 +3235,7 @@ { "cell_type": "markdown", "id": "70a82067", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which gives $\\lambda=4.571$ and $\\beta_0=0.933$ and $\\beta_1=0.359$." ] @@ -3797,9 +3243,7 @@ { "cell_type": "markdown", "id": "cda36acb", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Lasso case\n", "\n", @@ -3810,9 +3254,7 @@ { "cell_type": "markdown", "id": "b2faac1c", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "C(\\boldsymbol{\\beta})=(4-2\\beta_0)^2+(2-\\beta_1)^2+\\lambda(\\vert\\beta_0\\vert+\\vert\\beta_1\\vert),\n", @@ -3822,9 +3264,7 @@ { "cell_type": "markdown", "id": "53192e1f", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\beta_0}=-4(4-2\\beta_0)+\\lambda\\mathrm{sgn}(\\beta_0)=0,\n", @@ -3834,9 +3274,7 @@ { "cell_type": "markdown", "id": "67e7c28a", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and" ] @@ -3844,9 +3282,7 @@ { "cell_type": "markdown", "id": "fd95e257", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\frac{\\partial C(\\boldsymbol{\\beta})}{\\partial \\beta_1}=-2(2-\\beta_1)+\\lambda\\mathrm{sgn}(\\beta_1)=0.\n", @@ -3856,9 +3292,7 @@ { "cell_type": "markdown", "id": "29547ea5", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We have now four cases to solve besides the trivial cases $\\beta_0$ and/or $\\beta_1$ are zero, namely\n", "1. $\\beta_0 > 0$ and $\\beta_1 > 0$,\n", @@ -3873,9 +3307,7 @@ { "cell_type": "markdown", "id": "bad8636d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## The first Case\n", "\n", @@ -3885,9 +3317,7 @@ { "cell_type": "markdown", "id": "35d9a208", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "-4(4-2\\beta_0)+\\lambda=0,\n", @@ -3897,9 +3327,7 @@ { "cell_type": "markdown", "id": "b8251035", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and" ] @@ -3907,9 +3335,7 @@ { "cell_type": "markdown", "id": "af16cca9", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "-2(2-\\beta_1)+\\lambda=0.\n", @@ -3919,9 +3345,7 @@ { "cell_type": "markdown", "id": "96723ed8", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "which yields" ] @@ -3929,9 +3353,7 @@ { "cell_type": "markdown", "id": "9a3b9a51", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\beta_0=\\frac{16+\\lambda}{8},\n", @@ -3941,9 +3363,7 @@ { "cell_type": "markdown", "id": "e21b3e4d", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "and" ] @@ -3951,9 +3371,7 @@ { "cell_type": "markdown", "id": "b646e896", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "$$\n", "\\beta_1=\\frac{4+\\lambda}{2}.\n", @@ -3963,9 +3381,7 @@ { "cell_type": "markdown", "id": "12c82826", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "Using the constraint on $\\beta_0$ and $\\beta_1$ we can then find the optimal value of $\\lambda$ for the different cases. We leave this as an exercise to you." ] @@ -3973,9 +3389,7 @@ { "cell_type": "markdown", "id": "98d8fd18", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Simple code for solving the above problem\n", "\n", @@ -3988,10 +3402,7 @@ "cell_type": "code", "execution_count": 9, "id": "2d797bc4", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "import os\n", @@ -4048,9 +3459,7 @@ { "cell_type": "markdown", "id": "9c8dcf60", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "We see here that we reach a plateau. What is actually happening?" ] @@ -4058,9 +3467,7 @@ { "cell_type": "markdown", "id": "9929cadc", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## With Lasso Regression" ] @@ -4069,10 +3476,7 @@ "cell_type": "code", "execution_count": 10, "id": "2154c3ea", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "import os\n", @@ -4136,9 +3540,7 @@ { "cell_type": "markdown", "id": "24a50aa3", - "metadata": { - "editable": true - }, + "metadata": {}, "source": [ "## Another Example, now with a polynomial fit" ] @@ -4147,10 +3549,7 @@ "cell_type": "code", "execution_count": 11, "id": "5436f7d3", - "metadata": { - "collapsed": false, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "import os\n", @@ -4234,7 +3633,25 @@ ] } ], - "metadata": {}, + "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" + } + }, "nbformat": 4, "nbformat_minor": 5 } diff --git a/doc/src/Projects/2024/Project1/Project1.do.txt b/doc/src/Projects/2024/Project1/Project1.do.txt new file mode 100644 index 000000000..4ef5eb238 --- /dev/null +++ b/doc/src/Projects/2024/Project1/Project1.do.txt @@ -0,0 +1,525 @@ +TITLE: Project 1 on Machine Learning, deadline October 9 (midnight), 2024 +AUTHOR: "Data Analysis and Machine Learning FYS-STK3155/FYS4155":"http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" at University of Oslo, Norway +DATE: September 2 + +===== Note on using reference material, AI and other tools ===== + + +===== Regression analysis and resampling methods ===== + + + +The main aim of this project is to study in more detail various +regression methods, including the Ordinary Least Squares (OLS) method. +In addition to the scientific part, in this course we want also to +give you an experience in writing scientific reports. The format for +the delivery of your answers is namely that of a scientific report. At +for example +URL:"https://github.com/CompPhysics/MachineLearning/blob/master/doc/Projects/EvaluationGrading/EvaluationForm.md" +we detail how to write a report. Furthermore, at +URL:"https://github.com/CompPhysics/MachineLearning/blob/master/doc/Projects/ReportExample/" +you can find examples of previous reports. How to write reports will +also be discussed during the various lab sessions. + + +_A small recommendation when developing the codes here_. Instead of +jumping on to the two-dimensional function described below, we +recommend to do the code development and testing with a simpler +one-dimensional function, similar to those discussed in the exercises +of weeks 35 and 36. A simple test, as discussed during the lectures the first +two weeks is to set the design matrix equal to the identity +matrix. Then your model should give a mean square error which is exactly equal to zero. +When you are sure that your codes function well, you can then replace +the one-dimensional test function with the two-dimensional _Franke_ function +discussed here. + +The Franke function serves as a stepping stone towards the analysis of +real topographic data. The latter is the last part of this project. + +May be change the Franke function + + +=== Description of two-dimensional function === + +We will first study how to fit polynomials to a specific +two-dimensional function called "Franke's +function":"http://www.dtic.mil/dtic/tr/fulltext/u2/a081688.pdf". 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. + + +The Franke function, which is a weighted sum of four exponentials reads as follows +!bt +\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*} +!et + +The function will be defined for $x,y\in [0,1]$. In a sense, our data are thus scaled to a particular domain for the input values. + +Our first step will +be to perform an OLS regression analysis of this function, trying out +a polynomial fit with an $x$ and $y$ dependence of the form $[x, y, +x^2, y^2, xy, \dots]$. We will also include bootstrap first as a +resampling technique. After that we will include the cross-validation +technique. + +We can +use a uniform distribution to set up the arrays of values for $x$ and +$y$, or as in the example below just a set of fixed values for $x$ and +$y$ with a given step size. We will fit a function (for example a +polynomial) of $x$ and $y$. Thereafter we will repeat much of the +same procedure using the Ridge and Lasso regression methods, +introducing thus a dependence on the bias (penalty) $\lambda$. + +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. + + +The Python code for the Franke function is included here (it performs also a three-dimensional plot of it) +!bc pycod +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() + +!ec + + +=== Part a) : Ordinary Least Square (OLS) on the Franke function === + +We will generate our own dataset for a function +$\mathrm{FrankeFunction}(x,y)$ with $x,y \in [0,1]$. The function +$f(x,y)$ is the Franke function. You should explore also the addition +of an added stochastic noise to this function using the normal +distribution $N(0,1)$. + +*Write your own code* (using either a matrix inversion or a singular +value decomposition from e.g., _numpy_ ) and perform a standard _ordinary least square regression_ +analysis using polynomials in $x$ and $y$ up to fifth order. + +Evaluate the mean Squared error (MSE) + + +!bt +\[ MSE(\bm{y},\tilde{\bm{y}}) = \frac{1}{n} +\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, +\] +!et + +and the $R^2$ score function. If $\tilde{\bm{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 + +!bt +\[ +R^2(\bm{y}, \tilde{\bm{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, +\] +!et + +where we have defined the mean value of $\bm{y}$ as + +!bt +\[ +\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. +\] +!et + +Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree five). +Plot also the parameters $\beta$ as you increase the order of the polynomial. Comment your results. + +Your code has to include a scaling/centering 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 _Scikit-Learn_ (make +sure you have installed it). This function is called +$train\_test\_split$. _You should present a critical discussion of why and how you have scaled or not scaled the data_. + + +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 $2/3$ to $4/5$ of the data as training data. + + +You can easily reuse the solutions to your exercises from week 35 and week 36. +See also the lecture slides from week 35 and week 36. + + +=== Part b): Adding Ridge regression for the Franke function === + +Write your own code for the Ridge method, either using matrix +inversion or the singular value decomposition as done in the previous +exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the numerical exercise from week 36 is something you can reuse here. + +Perform the same analysis as you did in the previous exercise but now for different values of $\lambda$. Compare and +analyze your results with those obtained in part a) with the ordinary least squares method. Study the +dependence on $\lambda$. + +=== Part c): Adding Lasso for the Franke function === + +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 _Scikit-Learn_ (recommended). Keep in mind that the library _Scikit-Learn_ excludes the intercept by default. +Give a +critical discussion of the three methods and a judgement of which +model fits the data best. + + + +=== Part d): Paper and pencil part === + + +This exercise deals with various mean values and variances in linear regression method (here it may be useful to look up chapter 3, equation (3.8) of "Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer":"https://www.springer.com/gp/book/9780387848570"). The exercise is also part of the weekly exercises for week 37. + +The assumption we have made is +that there exists a continuous function $f(\bm{x})$ and a normal distributed error $\bm{\varepsilon}\sim N(0, \sigma^2)$ +which describes our data +!bt +\[ +\bm{y} = f(\bm{x})+\bm{\varepsilon} +\] +!et + +We then approximate this function $f(\bm{x})$ with our model $\bm{\tilde{y}}$ from the solution of the linear regression equations (ordinary least squares OLS), that is our +function $f$ is approximated by $\bm{\tilde{y}}$ where we minimized $(\bm{y}-\bm{\tilde{y}})^2$, with +!bt +\[ +\bm{\tilde{y}} = \bm{X}\bm{\beta}. +\] +!et +The matrix $\bm{X}$ is the so-called design or feature matrix. + + +Show that the expectation value of $\bm{y}$ for a given element $i$ +!bt +\[ +\mathbb{E}(y_i) =\sum_{j}x_{ij} \beta_j=\mathbf{X}_{i, \ast} \, \bm{\beta}, +\] +!et +and that +its variance is +!bt +\[ +\mbox{Var}(y_i) = \sigma^2. +\] +!et +Hence, $y_i \sim N( \mathbf{X}_{i, \ast} \, \bm{\beta}, \sigma^2)$, that is $\bm{y}$ follows a normal distribution with +mean value $\bm{X}\bm{\beta}$ and variance $\sigma^2$. + +With the OLS expressions for the optimal parameters $\bm{\hat{\beta}}$ show that +!bt +\[ +\mathbb{E}(\bm{\hat{\beta}}) = \bm{\beta}. +\] +!et +Show finally that the variance of $\bm{\beta}$ is +!bt +\[ +\mbox{Var}(\bm{\hat{\beta}}) = \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1}. +\] +!et + + +We can use the last expression when we define a so-called confidence interval for the parameters $\beta$. +A given parameter $\beta_j$ is given by the diagonal matrix element of the above matrix. + + + + +=== Part e): Bias-variance trade-off and resampling techniques === + +Our aim here is to study the bias-variance trade-off by implementing the _bootstrap_ resampling technique. +_We will only use the simpler ordinary least squares here_. + + +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. + +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! + +With this result we move on to the bias-variance trade-off analysis. + +Consider a +dataset $\mathcal{L}$ consisting of the data +$\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}$. + +As in part d), we assume that the true data is generated from a noisy model + +!bt +\[ +\bm{y}=f(\boldsymbol{x}) + \bm{\epsilon}. +\] +!et + +Here $\epsilon$ is normally distributed with mean zero and standard +deviation $\sigma^2$. + +In our derivation of the ordinary least squares method we defined then +an approximation to the function $f$ in terms of the parameters +$\bm{\beta}$ and the design matrix $\bm{X}$ which embody our model, +that is $\bm{\tilde{y}}=\bm{X}\bm{\beta}$. + +The parameters $\bm{\beta}$ are in turn found by optimizing the mean +squared error via the so-called cost function + +!bt +\[ +C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]. +\] +!et +Here the expected value $\mathbb{E}$ is the sample value. + +Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a +term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise. +That is, show that +!bt +\[ +\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2, +\] +!et +with +!bt +\[ +\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\bm{y}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right], +\] +!et +and +!bt +\[ +\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\bm{y}}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2. +\] +!et +The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38. +Explain what the terms mean and discuss their interpretations. + +Perform then a bias-variance analysis of the Franke function by +studying the MSE value as function of the complexity of your model. + +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 _bootstrap_ resampling method. +You can follow the code example in the jupyter-book at URL:"https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff". + + + +=== Part f): Cross-validation as resampling techniques, adding more complexity === + + +The aim here is to write your own code for another widely popular +resampling technique, the so-called cross-validation method. + +Implement the $k$-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 +_Scikit-Learn_ if needed. + +Compare the MSE you get from your cross-validation code with the one +you got from your _bootstrap_ code. Comment your results. Try $5-10$ +folds. You can also compare your own cross-validation code with the +one provided by _Scikit-Learn_. + +In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression. + + +=== Part g): Analysis of real data === + +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 a-f. 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 +URL:"https://earthexplorer.usgs.gov/", + +Or, if you prefer, we have placed selected datafiles at URL:"https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects/2023/Project1/DataFiles" + +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 _SRTM +Arc-Second Global_ and download the data as a _GeoTIF_ file. The +files are then stored in *tif* format which can be imported into a +Python program using + +!bc pycod +scipy.misc.imread +!ec + +Here is a simple part of a Python code which reads and plots the data +from such files + +!bc pycod +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() +!ec + +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. + + +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 "kaggle.com":"https://www.kaggle.com/datasets" for examples. + + +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. + +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). + + + + +===== Background literature ===== + +o For a discussion and derivation of the variances and mean squared errors using linear regression, see the "Lecture notes on ridge regression by Wessel N. van Wieringen":"https://arxiv.org/abs/1509.09169" + +o The textbook of "Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer":"https://www.springer.com/gp/book/9780387848570", chapters 3 and 7 are the most relevant ones for the analysis here. + +===== Introduction to numerical projects ===== + +Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. + + + * Give a short description of the nature of the problem and the eventual numerical methods you have used. + + * Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself. + + * Include the source code of your program. Comment your program properly. You should have the code at your GitHub/GitLab link. You can also place the code in an appendix of your report. + + * If possible, try to find analytic solutions, or known limits in order to test your program when developing the code. + + * Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes. + + * Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc. + + * Try to give an interpretation of you results in your answers to the problems. + + * Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it. + + * Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning. + + + + + +===== Format for electronic delivery of report and programs ===== + +The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report: + + * Use Canvas to hand in your projects, log in at URL:"https://www.uio.no/english/services/it/education/canvas/" with your normal UiO username and password. + + * Upload _only_ the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them. + + * In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters. + + +Finally, +we encourage you to collaborate. Optimal working groups consist of +2-3 students. You can then hand in a common report. + + + +===== Software and needed installations ===== + +If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages, +we recommend that you install the following Python packages via _pip_ as +o pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow +For Python3, replace _pip_ with _pip3_. + +See below for a discussion of _tensorflow_ and _scikit-learn_. + +For OSX users we recommend also, after having installed Xcode, to install _brew_. Brew allows +for a seamless installation of additional software via for example +o brew install python3 + +For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution +you can use _pip_ as well and simply install Python as +o sudo apt-get install python3 (or python for python2.7) +etc etc. + +If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely +o "Anaconda":"https://docs.anaconda.com/" Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system _conda_ +o "Enthought canopy":"https://www.enthought.com/product/canopy/" is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license. + +Popular software packages written in Python for ML are + +* "Scikit-learn":"http://scikit-learn.org/stable/", +* "Tensorflow":"https://www.tensorflow.org/", +* "PyTorch":"http://pytorch.org/" and +* "Keras":"https://keras.io/". +These are all freely available at their respective GitHub sites. They +encompass communities of developers in the thousands or more. And the number +of code developers and contributors keeps increasing. + + diff --git a/doc/src/Projects/2024/Project1/clean.sh b/doc/src/Projects/2024/Project1/clean.sh new file mode 100755 index 000000000..2e5da2c72 --- /dev/null +++ b/doc/src/Projects/2024/Project1/clean.sh @@ -0,0 +1,3 @@ +#!/bin/sh +doconce clean +rm -rf *.pdf *.tex ipynb*.tar.gz *.html ._*.html *~ reveal.js Trash README.txt diff --git a/doc/src/Projects/2024/Project1/make.sh b/doc/src/Projects/2024/Project1/make.sh new file mode 100755 index 000000000..8169333fc --- /dev/null +++ b/doc/src/Projects/2024/Project1/make.sh @@ -0,0 +1,87 @@ +#!/bin/sh +set -x + +function system { + "$@" + if [ $? -ne 0 ]; then + echo "make.sh: unsuccessful command $@" + echo "abort!" + exit 1 + fi +} + +if [ $# -eq 0 ]; then +echo 'bash make.sh slides1|slides2' +exit 1 +fi + +name=$1 +rm -f *.tar.gz + +opt="--encoding=utf-8" +opt= + +rm -f *.aux + + + +# Plain HTML documents +html=${name} +system doconce format html $name --pygments_html_style=default --html_style=bloodish --html_links_in_new_window --html_output=$html $opt +system doconce split_html $html.html --method=space10 + +# Bootstrap style +html=${name}-bs +system doconce format html $name --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=$html $opt +system doconce split_html $html.html --method=split --pagination --nav_button=bottom + +# IPython notebook +system doconce format ipynb $name $opt + + +# Ordinary plain LaTeX document +system doconce format pdflatex $name --print_latex_style=trac --latex_admon=paragraph $opt +system doconce ptex2tex $name envir=verbatim +# Add special packages +doconce subst "% Add user's preamble" "\g<1>\n\\usepackage{simplewick}" $name.tex +doconce replace 'section{' 'section*{' $name.tex +pdflatex -shell-escape $name +pdflatex -shell-escape $name +mv -f $name.pdf ${name}.pdf +cp $name.tex ${name}.tex + +# Publish +dest=../../../../Projects/2024 +if [ ! -d $dest/$name ]; then +mkdir $dest/$name +mkdir $dest/$name/pdf +mkdir $dest/$name/html +mkdir $dest/$name/ipynb +fi +cp ${name}*.tex $dest/$name/pdf +cp ${name}*.pdf $dest/$name/pdf +cp -r ${name}*.html ._${name}*.html $dest/$name/html + +# Figures: cannot just copy link, need to physically copy the files +if [ -d fig-${name} ]; then +if [ ! -d $dest/$name/html/fig-$name ]; then +mkdir $dest/$name/html/fig-$name +fi +cp -r fig-${name}/* $dest/$name/html/fig-$name +fi + +cp ${name}.ipynb $dest/$name/ipynb +ipynb_tarfile=ipynb-${name}-src.tar.gz +if [ ! -f ${ipynb_tarfile} ]; then +cat > README.txt <