diff --git a/doc/pub/week39/html/week39-bs.html b/doc/pub/week39/html/week39-bs.html index 899adc049..27c968c67 100644 --- a/doc/pub/week39/html/week39-bs.html +++ b/doc/pub/week39/html/week39-bs.html @@ -147,6 +147,10 @@ doconce format html week39.do.txt --html_style=bootstrap --pygments_html_style=d 2, None, 'gradient-descent-and-ridge'), + ('The Hessian matrix for Ridge Regression', + 2, + None, + 'the-hessian-matrix-for-ridge-regression'), ('Program example for gradient descent with Ridge Regression', 2, None, @@ -292,36 +296,37 @@ MathJax.Hub.Config({
  • Gradient Descent Example
  • And a corresponding example using scikit-learn
  • Gradient descent and Ridge
  • -
  • Program example for gradient descent with Ridge Regression
  • -
  • Using gradient descent methods, limitations
  • -
  • Challenge yourself
  • -
  • Friday October 1
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Momentum based GD
  • -
  • More on momentum based approaches
  • -
  • Momentum parameter
  • -
  • Second moment of the gradient
  • -
  • RMS prop
  • -
  • ADAM optimizer
  • -
  • Practical tips
  • -
  • Automatic differentiation
  • -
  • Using autograd
  • -
  • Autograd with more complicated functions
  • -
  • More complicated functions using the elements of their arguments directly
  • -
  • Functions using mathematical functions from Numpy
  • -
  • More autograd
  • -
  • And with loops
  • -
  • Using recursion
  • -
  • Unsupported functions
  • -
  • The syntax a.dot(b) when finding the dot product
  • -
  • Recommended to avoid
  • +
  • The Hessian matrix for Ridge Regression
  • +
  • Program example for gradient descent with Ridge Regression
  • +
  • Using gradient descent methods, limitations
  • +
  • Challenge yourself
  • +
  • Friday October 1
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Momentum based GD
  • +
  • More on momentum based approaches
  • +
  • Momentum parameter
  • +
  • Second moment of the gradient
  • +
  • RMS prop
  • +
  • ADAM optimizer
  • +
  • Practical tips
  • +
  • Automatic differentiation
  • +
  • Using autograd
  • +
  • Autograd with more complicated functions
  • +
  • More complicated functions using the elements of their arguments directly
  • +
  • Functions using mathematical functions from Numpy
  • +
  • More autograd
  • +
  • And with loops
  • +
  • Using recursion
  • +
  • Unsupported functions
  • +
  • The syntax a.dot(b) when finding the dot product
  • +
  • Recommended to avoid
  • @@ -351,7 +356,7 @@ MathJax.Hub.Config({
    -

    Nov 2, 2021

    +

    Nov 3, 2021


    @@ -376,7 +381,7 @@ MathJax.Hub.Config({
  • 9
  • 10
  • ...
  • -
  • 75
  • +
  • 76
  • »
  • diff --git a/doc/pub/week39/html/week39-reveal.html b/doc/pub/week39/html/week39-reveal.html index bd6faa596..de1877466 100644 --- a/doc/pub/week39/html/week39-reveal.html +++ b/doc/pub/week39/html/week39-reveal.html @@ -184,7 +184,7 @@ MathJax.Hub.Config({
    -

    Nov 2, 2021

    +

    Nov 3, 2021


    @@ -1760,6 +1760,26 @@ $$

     
    +

    +

    The Hessian matrix for Ridge Regression

    +

    The Hessian matrix of Ridge Regression for our simple example is given by

    +

     
    +$$ +\boldsymbol{H} \equiv \begin{bmatrix} +\frac{\partial^2 C(\beta)}{\partial \beta_0^2} & \frac{\partial^2 C(\beta)}{\partial \beta_0 \partial \beta_1} \\ +\frac{\partial^2 C(\beta)}{\partial \beta_0 \partial \beta_1} & \frac{\partial^2 C(\beta)}{\partial \beta_1^2} & \\ +\end{bmatrix} = \frac{2}{n}X^T X+2\lambda\boldsymbol{I}. +$$ +

     
    + +

    This implies that the Hessian matrix is positive definite, hence the stationary point is a +minimum. +Note that the Ridge loss function is convex, as a sum of two convex +functions. Therefore, the stationary point is a global +minimum of this function. +

    +
    +

    Program example for gradient descent with Ridge Regression

    @@ -1787,14 +1807,21 @@ XT_X = X.T @ X #Ridge parameter lambda lmbda = 0.001 -Id = lmbda* np.eye(XT_X.shape[0]) +Id = n*lmbda* np.eye(XT_X.shape[0]) + +# Hessian matrix +H = (2.0/n)* XT_X+2*lmbda +# Get the eigenvalues +EigValues, EigVectors = np.linalg.eig(H) +print(f"Eigenvalues of Hessian Matrix:{EigValues}") + beta_linreg = np.linalg.inv(XT_X+Id) @ X.T @ y print(beta_linreg) # Start plain gradient descent beta = np.random.randn(2,1) -eta = 0.1 +eta = 1.0/np.max(EigValues) Niterations = 100 for iter in range(Niterations): diff --git a/doc/pub/week39/html/week39-solarized.html b/doc/pub/week39/html/week39-solarized.html index add781ae1..5bdeff9da 100644 --- a/doc/pub/week39/html/week39-solarized.html +++ b/doc/pub/week39/html/week39-solarized.html @@ -174,6 +174,10 @@ div.toc p,a { 2, None, 'gradient-descent-and-ridge'), + ('The Hessian matrix for Ridge Regression', + 2, + None, + 'the-hessian-matrix-for-ridge-regression'), ('Program example for gradient descent with Ridge Regression', 2, None, @@ -278,7 +282,7 @@ MathJax.Hub.Config({
    -

    Nov 2, 2021

    +

    Nov 3, 2021


    @@ -1701,6 +1705,23 @@ $$ $$ +









    +

    The Hessian matrix for Ridge Regression

    +

    The Hessian matrix of Ridge Regression for our simple example is given by

    +$$ +\boldsymbol{H} \equiv \begin{bmatrix} +\frac{\partial^2 C(\beta)}{\partial \beta_0^2} & \frac{\partial^2 C(\beta)}{\partial \beta_0 \partial \beta_1} \\ +\frac{\partial^2 C(\beta)}{\partial \beta_0 \partial \beta_1} & \frac{\partial^2 C(\beta)}{\partial \beta_1^2} & \\ +\end{bmatrix} = \frac{2}{n}X^T X+2\lambda\boldsymbol{I}. +$$ + +

    This implies that the Hessian matrix is positive definite, hence the stationary point is a +minimum. +Note that the Ridge loss function is convex, as a sum of two convex +functions. Therefore, the stationary point is a global +minimum of this function. +

    +









    Program example for gradient descent with Ridge Regression

    @@ -1728,14 +1749,21 @@ XT_X = X.T @ X #Ridge parameter lambda lmbda = 0.001 -Id = lmbda* np.eye(XT_X.shape[0]) +Id = n*lmbda* np.eye(XT_X.shape[0]) + +# Hessian matrix +H = (2.0/n)* XT_X+2*lmbda +# Get the eigenvalues +EigValues, EigVectors = np.linalg.eig(H) +print(f"Eigenvalues of Hessian Matrix:{EigValues}") + beta_linreg = np.linalg.inv(XT_X+Id) @ X.T @ y print(beta_linreg) # Start plain gradient descent beta = np.random.randn(2,1) -eta = 0.1 +eta = 1.0/np.max(EigValues) Niterations = 100 for iter in range(Niterations): diff --git a/doc/pub/week39/html/week39.html b/doc/pub/week39/html/week39.html index 5c0ddf7a9..43c6269bc 100644 --- a/doc/pub/week39/html/week39.html +++ b/doc/pub/week39/html/week39.html @@ -251,6 +251,10 @@ div.toc p,a { 2, None, 'gradient-descent-and-ridge'), + ('The Hessian matrix for Ridge Regression', + 2, + None, + 'the-hessian-matrix-for-ridge-regression'), ('Program example for gradient descent with Ridge Regression', 2, None, @@ -355,7 +359,7 @@ MathJax.Hub.Config({
    -

    Nov 2, 2021

    +

    Nov 3, 2021


    @@ -1778,6 +1782,23 @@ $$ $$ +









    +

    The Hessian matrix for Ridge Regression

    +

    The Hessian matrix of Ridge Regression for our simple example is given by

    +$$ +\boldsymbol{H} \equiv \begin{bmatrix} +\frac{\partial^2 C(\beta)}{\partial \beta_0^2} & \frac{\partial^2 C(\beta)}{\partial \beta_0 \partial \beta_1} \\ +\frac{\partial^2 C(\beta)}{\partial \beta_0 \partial \beta_1} & \frac{\partial^2 C(\beta)}{\partial \beta_1^2} & \\ +\end{bmatrix} = \frac{2}{n}X^T X+2\lambda\boldsymbol{I}. +$$ + +

    This implies that the Hessian matrix is positive definite, hence the stationary point is a +minimum. +Note that the Ridge loss function is convex, as a sum of two convex +functions. Therefore, the stationary point is a global +minimum of this function. +

    +









    Program example for gradient descent with Ridge Regression

    @@ -1805,14 +1826,21 @@ XT_X = X.#Ridge parameter lambda lmbda = 0.001 -Id = lmbda* np.eye(XT_X.shape[0]) +Id = n*lmbda* np.eye(XT_X.shape[0]) + +# Hessian matrix +H = (2.0/n)* XT_X+2*lmbda +# Get the eigenvalues +EigValues, EigVectors = np.linalg.eig(H) +print(f"Eigenvalues of Hessian Matrix:{EigValues}") + beta_linreg = np.linalg.inv(XT_X+Id) @ X.T @ y print(beta_linreg) # Start plain gradient descent beta = np.random.randn(2,1) -eta = 0.1 +eta = 1.0/np.max(EigValues) Niterations = 100 for iter in range(Niterations): diff --git a/doc/pub/week39/ipynb/ipynb-week39-src.tar.gz b/doc/pub/week39/ipynb/ipynb-week39-src.tar.gz index 0b3705d4b..696a61d56 100644 Binary files a/doc/pub/week39/ipynb/ipynb-week39-src.tar.gz and b/doc/pub/week39/ipynb/ipynb-week39-src.tar.gz differ diff --git a/doc/pub/week39/ipynb/week39.ipynb b/doc/pub/week39/ipynb/week39.ipynb index d28a86b34..2dcb4789c 100644 --- a/doc/pub/week39/ipynb/week39.ipynb +++ b/doc/pub/week39/ipynb/week39.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "6263ac2c", + "id": "53fbfa45", "metadata": { "editable": true }, @@ -14,7 +14,7 @@ }, { "cell_type": "markdown", - "id": "814f46e8", + "id": "aa78aae4", "metadata": { "editable": true }, @@ -22,14 +22,14 @@ "# Week 39: Optimization and Gradient Methods\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", "\n", - "Date: **Nov 2, 2021**\n", + "Date: **Nov 3, 2021**\n", "\n", "Copyright 1999-2021, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license" ] }, { "cell_type": "markdown", - "id": "cd34f9be", + "id": "8c52d8d3", "metadata": { "editable": true }, @@ -55,7 +55,7 @@ }, { "cell_type": "markdown", - "id": "68f08321", + "id": "cf70b513", "metadata": { "editable": true }, @@ -67,7 +67,7 @@ }, { "cell_type": "markdown", - "id": "618b34d4", + "id": "7fe5a096", "metadata": { "editable": true }, @@ -86,7 +86,7 @@ { "cell_type": "code", "execution_count": 1, - "id": "ff12897b", + "id": "7b1ebe9a", "metadata": { "collapsed": false, "editable": true @@ -143,7 +143,7 @@ }, { "cell_type": "markdown", - "id": "75fdf948", + "id": "3b07fa34", "metadata": { "editable": true }, @@ -155,7 +155,7 @@ }, { "cell_type": "markdown", - "id": "21b4ddd2", + "id": "79436191", "metadata": { "editable": true }, @@ -170,7 +170,7 @@ { "cell_type": "code", "execution_count": 2, - "id": "0be364ae", + "id": "249e43db", "metadata": { "collapsed": false, "editable": true @@ -223,7 +223,7 @@ }, { "cell_type": "markdown", - "id": "b1705a00", + "id": "35e54cb1", "metadata": { "editable": true }, @@ -238,7 +238,7 @@ }, { "cell_type": "markdown", - "id": "031b93e6", + "id": "e0c927ce", "metadata": { "editable": true }, @@ -258,7 +258,7 @@ { "cell_type": "code", "execution_count": 3, - "id": "ffe995f0", + "id": "dacaf34d", "metadata": { "collapsed": false, "editable": true @@ -312,7 +312,7 @@ }, { "cell_type": "markdown", - "id": "fc03fd95", + "id": "ed245882", "metadata": { "editable": true }, @@ -331,7 +331,7 @@ }, { "cell_type": "markdown", - "id": "2718f706", + "id": "58312afe", "metadata": { "editable": true }, @@ -348,7 +348,7 @@ }, { "cell_type": "markdown", - "id": "3ad8bf69", + "id": "802e4916", "metadata": { "editable": true }, @@ -363,7 +363,7 @@ }, { "cell_type": "markdown", - "id": "b86907b5", + "id": "d4d65855", "metadata": { "editable": true }, @@ -373,7 +373,7 @@ }, { "cell_type": "markdown", - "id": "c03cb57f", + "id": "4c79aec3", "metadata": { "editable": true }, @@ -389,7 +389,7 @@ }, { "cell_type": "markdown", - "id": "27c1a970", + "id": "d629835c", "metadata": { "editable": true }, @@ -401,7 +401,7 @@ }, { "cell_type": "markdown", - "id": "954ac945", + "id": "eecfd699", "metadata": { "editable": true }, @@ -412,7 +412,7 @@ }, { "cell_type": "markdown", - "id": "0463bd79", + "id": "0eccb2e0", "metadata": { "editable": true }, @@ -424,7 +424,7 @@ }, { "cell_type": "markdown", - "id": "7905b925", + "id": "1ed67a6e", "metadata": { "editable": true }, @@ -434,7 +434,7 @@ }, { "cell_type": "markdown", - "id": "25d628e3", + "id": "83839b6d", "metadata": { "editable": true }, @@ -448,7 +448,7 @@ }, { "cell_type": "markdown", - "id": "196e94f7", + "id": "8424c771", "metadata": { "editable": true }, @@ -460,7 +460,7 @@ }, { "cell_type": "markdown", - "id": "31e06821", + "id": "1e793448", "metadata": { "editable": true }, @@ -470,7 +470,7 @@ }, { "cell_type": "markdown", - "id": "49cf7f77", + "id": "e0f3598c", "metadata": { "editable": true }, @@ -482,7 +482,7 @@ }, { "cell_type": "markdown", - "id": "091103af", + "id": "d373502f", "metadata": { "editable": true }, @@ -494,7 +494,7 @@ }, { "cell_type": "markdown", - "id": "1e13788e", + "id": "4a973764", "metadata": { "editable": true }, @@ -514,7 +514,7 @@ }, { "cell_type": "markdown", - "id": "81b72086", + "id": "50a08bc7", "metadata": { "editable": true }, @@ -530,7 +530,7 @@ }, { "cell_type": "markdown", - "id": "154ff7ec", + "id": "9fbcf11a", "metadata": { "editable": true }, @@ -546,7 +546,7 @@ }, { "cell_type": "markdown", - "id": "2dc77c1c", + "id": "ae9f1c74", "metadata": { "editable": true }, @@ -557,7 +557,7 @@ }, { "cell_type": "markdown", - "id": "436d5e10", + "id": "ff816428", "metadata": { "editable": true }, @@ -569,7 +569,7 @@ }, { "cell_type": "markdown", - "id": "eded5059", + "id": "1d8e23e2", "metadata": { "editable": true }, @@ -579,7 +579,7 @@ }, { "cell_type": "markdown", - "id": "0c8f72c4", + "id": "29c27a39", "metadata": { "editable": true }, @@ -591,7 +591,7 @@ }, { "cell_type": "markdown", - "id": "b43097ee", + "id": "be705d27", "metadata": { "editable": true }, @@ -601,7 +601,7 @@ }, { "cell_type": "markdown", - "id": "78cacabf", + "id": "f8b5017b", "metadata": { "editable": true }, @@ -613,7 +613,7 @@ }, { "cell_type": "markdown", - "id": "4f490616", + "id": "80067acb", "metadata": { "editable": true }, @@ -635,7 +635,7 @@ }, { "cell_type": "markdown", - "id": "1e808c2b", + "id": "cc79eb39", "metadata": { "editable": true }, @@ -648,7 +648,7 @@ }, { "cell_type": "markdown", - "id": "1e13adc6", + "id": "02c931ed", "metadata": { "editable": true }, @@ -661,7 +661,7 @@ }, { "cell_type": "markdown", - "id": "df5ed837", + "id": "e8961c31", "metadata": { "editable": true }, @@ -671,7 +671,7 @@ }, { "cell_type": "markdown", - "id": "b1e83c72", + "id": "e5367208", "metadata": { "editable": true }, @@ -689,7 +689,7 @@ }, { "cell_type": "markdown", - "id": "d913e2b4", + "id": "bf37ee7e", "metadata": { "editable": true }, @@ -699,7 +699,7 @@ }, { "cell_type": "markdown", - "id": "c6a3a141", + "id": "1d5b038a", "metadata": { "editable": true }, @@ -714,7 +714,7 @@ }, { "cell_type": "markdown", - "id": "f80cc614", + "id": "188556e8", "metadata": { "editable": true }, @@ -724,7 +724,7 @@ }, { "cell_type": "markdown", - "id": "65dc9a58", + "id": "55eed375", "metadata": { "editable": true }, @@ -738,7 +738,7 @@ }, { "cell_type": "markdown", - "id": "339350ce", + "id": "59672231", "metadata": { "editable": true }, @@ -748,7 +748,7 @@ }, { "cell_type": "markdown", - "id": "b05423ff", + "id": "3b75e9ab", "metadata": { "editable": true }, @@ -762,7 +762,7 @@ }, { "cell_type": "markdown", - "id": "bc4379f3", + "id": "a3943c2e", "metadata": { "editable": true }, @@ -777,7 +777,7 @@ }, { "cell_type": "markdown", - "id": "7ed3a930", + "id": "9c5e71db", "metadata": { "editable": true }, @@ -794,7 +794,7 @@ }, { "cell_type": "markdown", - "id": "a817ecc9", + "id": "5bdabf51", "metadata": { "editable": true }, @@ -806,7 +806,7 @@ }, { "cell_type": "markdown", - "id": "73e4664e", + "id": "42764d94", "metadata": { "editable": true }, @@ -820,7 +820,7 @@ }, { "cell_type": "markdown", - "id": "42b23010", + "id": "d8ef1cc2", "metadata": { "editable": true }, @@ -835,7 +835,7 @@ }, { "cell_type": "markdown", - "id": "b2ce0e1e", + "id": "ae539c7d", "metadata": { "editable": true }, @@ -847,7 +847,7 @@ }, { "cell_type": "markdown", - "id": "8d9c258d", + "id": "51a30d45", "metadata": { "editable": true }, @@ -858,7 +858,7 @@ }, { "cell_type": "markdown", - "id": "75b014b2", + "id": "2d6745ae", "metadata": { "editable": true }, @@ -886,7 +886,7 @@ }, { "cell_type": "markdown", - "id": "2c3e43f6", + "id": "f29e7884", "metadata": { "editable": true }, @@ -908,7 +908,7 @@ }, { "cell_type": "markdown", - "id": "fc987448", + "id": "7afc86fe", "metadata": { "editable": true }, @@ -930,7 +930,7 @@ }, { "cell_type": "markdown", - "id": "2ec7230c", + "id": "9091a450", "metadata": { "editable": true }, @@ -942,7 +942,7 @@ }, { "cell_type": "markdown", - "id": "2f329d26", + "id": "313a6215", "metadata": { "editable": true }, @@ -979,7 +979,7 @@ }, { "cell_type": "markdown", - "id": "b5d52917", + "id": "111fd8a6", "metadata": { "editable": true }, @@ -1007,7 +1007,7 @@ }, { "cell_type": "markdown", - "id": "e504719e", + "id": "90f7dbd3", "metadata": { "editable": true }, @@ -1037,7 +1037,7 @@ }, { "cell_type": "markdown", - "id": "a1649131", + "id": "781da653", "metadata": { "editable": true }, @@ -1057,7 +1057,7 @@ }, { "cell_type": "markdown", - "id": "4ef42fd6", + "id": "b73579d5", "metadata": { "editable": true }, @@ -1069,7 +1069,7 @@ }, { "cell_type": "markdown", - "id": "159fe760", + "id": "d59eb62d", "metadata": { "editable": true }, @@ -1079,7 +1079,7 @@ }, { "cell_type": "markdown", - "id": "29ebbb6f", + "id": "6d519425", "metadata": { "editable": true }, @@ -1091,7 +1091,7 @@ }, { "cell_type": "markdown", - "id": "5f4a35d4", + "id": "c8276c05", "metadata": { "editable": true }, @@ -1103,7 +1103,7 @@ }, { "cell_type": "markdown", - "id": "90f63cc8", + "id": "c756ffe2", "metadata": { "editable": true }, @@ -1115,7 +1115,7 @@ }, { "cell_type": "markdown", - "id": "b7bd384e", + "id": "3074cc74", "metadata": { "editable": true }, @@ -1127,7 +1127,7 @@ }, { "cell_type": "markdown", - "id": "8a95c1a9", + "id": "a41ea068", "metadata": { "editable": true }, @@ -1138,7 +1138,7 @@ }, { "cell_type": "markdown", - "id": "30180639", + "id": "c3f8b8ce", "metadata": { "editable": true }, @@ -1151,7 +1151,7 @@ }, { "cell_type": "markdown", - "id": "09b1c9f1", + "id": "3f6b44a1", "metadata": { "editable": true }, @@ -1163,7 +1163,7 @@ }, { "cell_type": "markdown", - "id": "6fcbfec6", + "id": "65f1cc04", "metadata": { "editable": true }, @@ -1173,7 +1173,7 @@ }, { "cell_type": "markdown", - "id": "d00fcff7", + "id": "ad133279", "metadata": { "editable": true }, @@ -1185,7 +1185,7 @@ }, { "cell_type": "markdown", - "id": "7ff48637", + "id": "4d54b8c5", "metadata": { "editable": true }, @@ -1195,7 +1195,7 @@ }, { "cell_type": "markdown", - "id": "023bb623", + "id": "e76160cd", "metadata": { "editable": true }, @@ -1206,7 +1206,7 @@ }, { "cell_type": "markdown", - "id": "d4cd6b69", + "id": "6633bf53", "metadata": { "editable": true }, @@ -1218,7 +1218,7 @@ }, { "cell_type": "markdown", - "id": "438c0b5a", + "id": "5d15cfa9", "metadata": { "editable": true }, @@ -1230,7 +1230,7 @@ }, { "cell_type": "markdown", - "id": "764cde40", + "id": "7875717f", "metadata": { "editable": true }, @@ -1242,7 +1242,7 @@ }, { "cell_type": "markdown", - "id": "4dd2012f", + "id": "c91ded01", "metadata": { "editable": true }, @@ -1253,7 +1253,7 @@ }, { "cell_type": "markdown", - "id": "e5a711d2", + "id": "f75dbb21", "metadata": { "editable": true }, @@ -1264,7 +1264,7 @@ }, { "cell_type": "markdown", - "id": "e7eec8f6", + "id": "2aebf554", "metadata": { "editable": true }, @@ -1276,7 +1276,7 @@ }, { "cell_type": "markdown", - "id": "ac4bc943", + "id": "b5a287a3", "metadata": { "editable": true }, @@ -1286,7 +1286,7 @@ }, { "cell_type": "markdown", - "id": "fcf6d10d", + "id": "10e8a74e", "metadata": { "editable": true }, @@ -1298,7 +1298,7 @@ }, { "cell_type": "markdown", - "id": "07d74d47", + "id": "bdf8cb92", "metadata": { "editable": true }, @@ -1308,7 +1308,7 @@ }, { "cell_type": "markdown", - "id": "10181a0a", + "id": "9a8e4c96", "metadata": { "editable": true }, @@ -1320,7 +1320,7 @@ }, { "cell_type": "markdown", - "id": "30ddb647", + "id": "4ec42b4e", "metadata": { "editable": true }, @@ -1330,7 +1330,7 @@ }, { "cell_type": "markdown", - "id": "c40c0ed3", + "id": "ae5e2d9d", "metadata": { "editable": true }, @@ -1342,7 +1342,7 @@ }, { "cell_type": "markdown", - "id": "60898dba", + "id": "ffc4bd1a", "metadata": { "editable": true }, @@ -1352,7 +1352,7 @@ }, { "cell_type": "markdown", - "id": "eee23b5f", + "id": "3f2b8bec", "metadata": { "editable": true }, @@ -1364,7 +1364,7 @@ }, { "cell_type": "markdown", - "id": "50fdc629", + "id": "b06f69b3", "metadata": { "editable": true }, @@ -1375,7 +1375,7 @@ { "cell_type": "code", "execution_count": 4, - "id": "defa6ef0", + "id": "db009c5a", "metadata": { "collapsed": false, "editable": true @@ -1406,7 +1406,7 @@ }, { "cell_type": "markdown", - "id": "669108ca", + "id": "7194777f", "metadata": { "editable": true }, @@ -1417,7 +1417,7 @@ { "cell_type": "code", "execution_count": 5, - "id": "206f3608", + "id": "58968407", "metadata": { "collapsed": false, "editable": true @@ -1431,7 +1431,7 @@ }, { "cell_type": "markdown", - "id": "137b9e45", + "id": "0baba595", "metadata": { "editable": true }, @@ -1442,7 +1442,7 @@ { "cell_type": "code", "execution_count": 6, - "id": "d2410c8a", + "id": "494cc3f6", "metadata": { "collapsed": false, "editable": true @@ -1455,7 +1455,7 @@ }, { "cell_type": "markdown", - "id": "de7aa713", + "id": "a2bbf03a", "metadata": { "editable": true }, @@ -1466,7 +1466,7 @@ { "cell_type": "code", "execution_count": 7, - "id": "16efee06", + "id": "88c531ef", "metadata": { "collapsed": false, "editable": true @@ -1484,7 +1484,7 @@ }, { "cell_type": "markdown", - "id": "c8f6e2bd", + "id": "d0eac371", "metadata": { "editable": true }, @@ -1495,7 +1495,7 @@ { "cell_type": "code", "execution_count": 8, - "id": "8540f54d", + "id": "1b47abfb", "metadata": { "collapsed": false, "editable": true @@ -1510,7 +1510,7 @@ }, { "cell_type": "markdown", - "id": "6f483a63", + "id": "f6c7a927", "metadata": { "editable": true }, @@ -1520,7 +1520,7 @@ }, { "cell_type": "markdown", - "id": "2367ba74", + "id": "bd42e48a", "metadata": { "editable": true }, @@ -1534,7 +1534,7 @@ }, { "cell_type": "markdown", - "id": "20788d85", + "id": "6f4210b4", "metadata": { "editable": true }, @@ -1546,7 +1546,7 @@ }, { "cell_type": "markdown", - "id": "ea535aea", + "id": "74bb3e7d", "metadata": { "editable": true }, @@ -1557,7 +1557,7 @@ }, { "cell_type": "markdown", - "id": "165adcbc", + "id": "beb6c7cc", "metadata": { "editable": true }, @@ -1569,7 +1569,7 @@ }, { "cell_type": "markdown", - "id": "f7aaa007", + "id": "4fd3b390", "metadata": { "editable": true }, @@ -1580,7 +1580,7 @@ }, { "cell_type": "markdown", - "id": "23efba9c", + "id": "3eb93ca8", "metadata": { "editable": true }, @@ -1591,7 +1591,7 @@ }, { "cell_type": "markdown", - "id": "d5439727", + "id": "79bedfaf", "metadata": { "editable": true }, @@ -1603,7 +1603,7 @@ }, { "cell_type": "markdown", - "id": "631c623f", + "id": "bd3eca5f", "metadata": { "editable": true }, @@ -1613,7 +1613,7 @@ }, { "cell_type": "markdown", - "id": "4580be44", + "id": "d1cd77fb", "metadata": { "editable": true }, @@ -1625,7 +1625,7 @@ }, { "cell_type": "markdown", - "id": "3fc0d9f5", + "id": "9db15b7e", "metadata": { "editable": true }, @@ -1637,7 +1637,7 @@ }, { "cell_type": "markdown", - "id": "4b0f7db6", + "id": "889d2e82", "metadata": { "editable": true }, @@ -1649,7 +1649,7 @@ }, { "cell_type": "markdown", - "id": "6062951a", + "id": "880fc01f", "metadata": { "editable": true }, @@ -1661,7 +1661,7 @@ }, { "cell_type": "markdown", - "id": "fd0a2f6e", + "id": "19021b89", "metadata": { "editable": true }, @@ -1672,7 +1672,7 @@ }, { "cell_type": "markdown", - "id": "b83ad4fb", + "id": "26f1ca1a", "metadata": { "editable": true }, @@ -1684,7 +1684,7 @@ }, { "cell_type": "markdown", - "id": "0cd2222f", + "id": "b3e4b78f", "metadata": { "editable": true }, @@ -1694,7 +1694,7 @@ }, { "cell_type": "markdown", - "id": "de54cca2", + "id": "7fb33e80", "metadata": { "editable": true }, @@ -1706,7 +1706,7 @@ }, { "cell_type": "markdown", - "id": "a090dc8e", + "id": "06cbd1cb", "metadata": { "editable": true }, @@ -1716,7 +1716,7 @@ }, { "cell_type": "markdown", - "id": "4c5bb87a", + "id": "15442a6c", "metadata": { "editable": true }, @@ -1728,7 +1728,7 @@ }, { "cell_type": "markdown", - "id": "e1dc0a5d", + "id": "dc60e358", "metadata": { "editable": true }, @@ -1748,7 +1748,7 @@ }, { "cell_type": "markdown", - "id": "94780e34", + "id": "a4f542bc", "metadata": { "editable": true }, @@ -1760,7 +1760,7 @@ }, { "cell_type": "markdown", - "id": "7a0f6afb", + "id": "85e4bc78", "metadata": { "editable": true }, @@ -1770,7 +1770,7 @@ }, { "cell_type": "markdown", - "id": "fd14657e", + "id": "a0317fc5", "metadata": { "editable": true }, @@ -1782,7 +1782,7 @@ }, { "cell_type": "markdown", - "id": "ce919c46", + "id": "b78c21b7", "metadata": { "editable": true }, @@ -1792,7 +1792,7 @@ }, { "cell_type": "markdown", - "id": "2af9b699", + "id": "428e7a19", "metadata": { "editable": true }, @@ -1803,7 +1803,7 @@ }, { "cell_type": "markdown", - "id": "312f6dc3", + "id": "0c913ac0", "metadata": { "editable": true }, @@ -1815,7 +1815,7 @@ }, { "cell_type": "markdown", - "id": "e75a06a8", + "id": "57290ae3", "metadata": { "editable": true }, @@ -1827,7 +1827,7 @@ }, { "cell_type": "markdown", - "id": "12232187", + "id": "67f86200", "metadata": { "editable": true }, @@ -1839,7 +1839,7 @@ }, { "cell_type": "markdown", - "id": "e44907f2", + "id": "008d6c1a", "metadata": { "editable": true }, @@ -1852,7 +1852,7 @@ }, { "cell_type": "markdown", - "id": "3297c1f5", + "id": "24748eec", "metadata": { "editable": true }, @@ -1863,7 +1863,7 @@ }, { "cell_type": "markdown", - "id": "abae27bc", + "id": "29ebba58", "metadata": { "editable": true }, @@ -1875,7 +1875,7 @@ }, { "cell_type": "markdown", - "id": "835b302b", + "id": "6f4b4e61", "metadata": { "editable": true }, @@ -1891,7 +1891,7 @@ }, { "cell_type": "markdown", - "id": "f7a21361", + "id": "c9118780", "metadata": { "editable": true }, @@ -1903,7 +1903,7 @@ }, { "cell_type": "markdown", - "id": "5277ae72", + "id": "dffc5b96", "metadata": { "editable": true }, @@ -1914,7 +1914,7 @@ }, { "cell_type": "markdown", - "id": "cd385e88", + "id": "9d1e43aa", "metadata": { "editable": true }, @@ -1926,7 +1926,7 @@ }, { "cell_type": "markdown", - "id": "20361729", + "id": "d78ff3cf", "metadata": { "editable": true }, @@ -1936,7 +1936,7 @@ }, { "cell_type": "markdown", - "id": "dd6b22b4", + "id": "93268844", "metadata": { "editable": true }, @@ -1948,7 +1948,7 @@ }, { "cell_type": "markdown", - "id": "150beb1c", + "id": "5ee06e42", "metadata": { "editable": true }, @@ -1958,7 +1958,7 @@ }, { "cell_type": "markdown", - "id": "77530681", + "id": "c965a705", "metadata": { "editable": true }, @@ -1970,7 +1970,7 @@ }, { "cell_type": "markdown", - "id": "04ba2383", + "id": "3386391c", "metadata": { "editable": true }, @@ -1980,7 +1980,7 @@ }, { "cell_type": "markdown", - "id": "ff1a8f53", + "id": "4ad90598", "metadata": { "editable": true }, @@ -1992,7 +1992,7 @@ }, { "cell_type": "markdown", - "id": "f8596b4e", + "id": "b0799136", "metadata": { "editable": true }, @@ -2016,7 +2016,7 @@ { "cell_type": "code", "execution_count": 9, - "id": "260fdca9", + "id": "db78d5b5", "metadata": { "collapsed": false, "editable": true @@ -2029,7 +2029,7 @@ }, { "cell_type": "markdown", - "id": "8884fffe", + "id": "a0d1b29e", "metadata": { "editable": true }, @@ -2040,7 +2040,7 @@ }, { "cell_type": "markdown", - "id": "7269998b", + "id": "84e218b8", "metadata": { "editable": true }, @@ -2052,7 +2052,7 @@ }, { "cell_type": "markdown", - "id": "f8d3cadb", + "id": "236e48b7", "metadata": { "editable": true }, @@ -2062,7 +2062,7 @@ }, { "cell_type": "markdown", - "id": "3e9fb7fa", + "id": "78b88cb1", "metadata": { "editable": true }, @@ -2074,7 +2074,7 @@ }, { "cell_type": "markdown", - "id": "d25601f3", + "id": "f49f0b6a", "metadata": { "editable": true }, @@ -2088,7 +2088,7 @@ }, { "cell_type": "markdown", - "id": "3a341c25", + "id": "73e5ec0b", "metadata": { "editable": true }, @@ -2104,7 +2104,7 @@ }, { "cell_type": "markdown", - "id": "aa05f072", + "id": "2f1fab67", "metadata": { "editable": true }, @@ -2114,7 +2114,7 @@ }, { "cell_type": "markdown", - "id": "d9d8e0de", + "id": "d05b60d5", "metadata": { "editable": true }, @@ -2126,7 +2126,7 @@ }, { "cell_type": "markdown", - "id": "1d693be2", + "id": "44bf44c6", "metadata": { "editable": true }, @@ -2136,7 +2136,7 @@ }, { "cell_type": "markdown", - "id": "2ea777b4", + "id": "f67dfe60", "metadata": { "editable": true }, @@ -2148,7 +2148,7 @@ }, { "cell_type": "markdown", - "id": "24834799", + "id": "4b5970e0", "metadata": { "editable": true }, @@ -2162,7 +2162,7 @@ }, { "cell_type": "markdown", - "id": "ca03d184", + "id": "9b0183e3", "metadata": { "editable": true }, @@ -2172,7 +2172,7 @@ }, { "cell_type": "markdown", - "id": "76b10741", + "id": "7d6a7718", "metadata": { "editable": true }, @@ -2183,7 +2183,7 @@ }, { "cell_type": "markdown", - "id": "03d8886f", + "id": "cefb20c5", "metadata": { "editable": true }, @@ -2198,7 +2198,7 @@ }, { "cell_type": "markdown", - "id": "97b0ddb8", + "id": "f1769551", "metadata": { "editable": true }, @@ -2208,7 +2208,7 @@ }, { "cell_type": "markdown", - "id": "928eefaf", + "id": "c590024b", "metadata": { "editable": true }, @@ -2220,7 +2220,7 @@ }, { "cell_type": "markdown", - "id": "20ca3352", + "id": "f087d2d1", "metadata": { "editable": true }, @@ -2232,7 +2232,7 @@ }, { "cell_type": "markdown", - "id": "c9e83371", + "id": "3f1e8b75", "metadata": { "editable": true }, @@ -2247,7 +2247,7 @@ }, { "cell_type": "markdown", - "id": "37af4676", + "id": "3257df95", "metadata": { "editable": true }, @@ -2260,7 +2260,7 @@ { "cell_type": "code", "execution_count": 10, - "id": "8a3afc9b", + "id": "bd5fc754", "metadata": { "collapsed": false, "editable": true @@ -2317,7 +2317,7 @@ }, { "cell_type": "markdown", - "id": "fef51920", + "id": "fcea549d", "metadata": { "editable": true }, @@ -2328,7 +2328,7 @@ { "cell_type": "code", "execution_count": 11, - "id": "3421b06e", + "id": "52a2306c", "metadata": { "collapsed": false, "editable": true @@ -2355,7 +2355,7 @@ }, { "cell_type": "markdown", - "id": "9be51010", + "id": "0909287b", "metadata": { "editable": true }, @@ -2367,7 +2367,7 @@ }, { "cell_type": "markdown", - "id": "f749c999", + "id": "d964e991", "metadata": { "editable": true }, @@ -2379,7 +2379,7 @@ }, { "cell_type": "markdown", - "id": "a3e198f9", + "id": "ac0a7e86", "metadata": { "editable": true }, @@ -2389,7 +2389,7 @@ }, { "cell_type": "markdown", - "id": "1d6eb713", + "id": "afa5f4fd", "metadata": { "editable": true }, @@ -2403,7 +2403,7 @@ }, { "cell_type": "markdown", - "id": "91fab79a", + "id": "b7664c73", "metadata": { "editable": true }, @@ -2413,7 +2413,7 @@ }, { "cell_type": "markdown", - "id": "9107f00b", + "id": "6c39dbf3", "metadata": { "editable": true }, @@ -2425,7 +2425,47 @@ }, { "cell_type": "markdown", - "id": "961a44f6", + "id": "e67c5e82", + "metadata": { + "editable": true + }, + "source": [ + "## The Hessian matrix for Ridge Regression\n", + "The Hessian matrix of Ridge Regression for our simple example is given by" + ] + }, + { + "cell_type": "markdown", + "id": "a657b390", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\boldsymbol{H} \\equiv \\begin{bmatrix}\n", + "\\frac{\\partial^2 C(\\beta)}{\\partial \\beta_0^2} & \\frac{\\partial^2 C(\\beta)}{\\partial \\beta_0 \\partial \\beta_1} \\\\\n", + "\\frac{\\partial^2 C(\\beta)}{\\partial \\beta_0 \\partial \\beta_1} & \\frac{\\partial^2 C(\\beta)}{\\partial \\beta_1^2} & \\\\\n", + "\\end{bmatrix} = \\frac{2}{n}X^T X+2\\lambda\\boldsymbol{I}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "8620c333", + "metadata": { + "editable": true + }, + "source": [ + "This implies that the Hessian matrix is positive definite, hence the stationary point is a\n", + "minimum.\n", + "Note that the Ridge loss function is convex, as a sum of two convex\n", + "functions. Therefore, the stationary point is a global\n", + "minimum of this function." + ] + }, + { + "cell_type": "markdown", + "id": "38ce7622", "metadata": { "editable": true }, @@ -2436,7 +2476,7 @@ { "cell_type": "code", "execution_count": 12, - "id": "b1f705b3", + "id": "042531b1", "metadata": { "collapsed": false, "editable": true @@ -2461,14 +2501,21 @@ "\n", "#Ridge parameter lambda\n", "lmbda = 0.001\n", - "Id = lmbda* np.eye(XT_X.shape[0])\n", + "Id = n*lmbda* np.eye(XT_X.shape[0])\n", + "\n", + "# Hessian matrix\n", + "H = (2.0/n)* XT_X+2*lmbda\n", + "# Get the eigenvalues\n", + "EigValues, EigVectors = np.linalg.eig(H)\n", + "print(f\"Eigenvalues of Hessian Matrix:{EigValues}\")\n", + "\n", "\n", "beta_linreg = np.linalg.inv(XT_X+Id) @ X.T @ y\n", "print(beta_linreg)\n", "# Start plain gradient descent\n", "beta = np.random.randn(2,1)\n", "\n", - "eta = 0.1\n", + "eta = 1.0/np.max(EigValues)\n", "Niterations = 100\n", "\n", "for iter in range(Niterations):\n", @@ -2490,7 +2537,7 @@ }, { "cell_type": "markdown", - "id": "b2c5981d", + "id": "c7e50920", "metadata": { "editable": true }, @@ -2512,7 +2559,7 @@ }, { "cell_type": "markdown", - "id": "4c5330ea", + "id": "fd1811d9", "metadata": { "editable": true }, @@ -2524,7 +2571,7 @@ }, { "cell_type": "markdown", - "id": "c15cdad1", + "id": "4e9a09ef", "metadata": { "editable": true }, @@ -2534,7 +2581,7 @@ }, { "cell_type": "markdown", - "id": "fe4260f1", + "id": "3aa85e79", "metadata": { "editable": true }, @@ -2551,7 +2598,7 @@ }, { "cell_type": "markdown", - "id": "6880a7be", + "id": "9e269917", "metadata": { "editable": true }, @@ -2564,7 +2611,7 @@ }, { "cell_type": "markdown", - "id": "da55002d", + "id": "e346c2be", "metadata": { "editable": true }, @@ -2577,7 +2624,7 @@ }, { "cell_type": "markdown", - "id": "291d4f67", + "id": "ed3a06dc", "metadata": { "editable": true }, @@ -2590,7 +2637,7 @@ }, { "cell_type": "markdown", - "id": "b5587460", + "id": "91172463", "metadata": { "editable": true }, @@ -2604,7 +2651,7 @@ }, { "cell_type": "markdown", - "id": "347deeb7", + "id": "6bf5c088", "metadata": { "editable": true }, @@ -2626,7 +2673,7 @@ }, { "cell_type": "markdown", - "id": "4ff76de7", + "id": "71f4f188", "metadata": { "editable": true }, @@ -2641,7 +2688,7 @@ }, { "cell_type": "markdown", - "id": "0dabde37", + "id": "b0fc4db3", "metadata": { "editable": true }, @@ -2653,7 +2700,7 @@ }, { "cell_type": "markdown", - "id": "85053899", + "id": "bcd4ddfe", "metadata": { "editable": true }, @@ -2666,7 +2713,7 @@ }, { "cell_type": "markdown", - "id": "c5d2743e", + "id": "3a184799", "metadata": { "editable": true }, @@ -2680,7 +2727,7 @@ }, { "cell_type": "markdown", - "id": "a3aa78b6", + "id": "7fe2c739", "metadata": { "editable": true }, @@ -2691,7 +2738,7 @@ { "cell_type": "code", "execution_count": 13, - "id": "3e876868", + "id": "90624acc", "metadata": { "collapsed": false, "editable": true @@ -2716,7 +2763,7 @@ }, { "cell_type": "markdown", - "id": "69f96a04", + "id": "844e8019", "metadata": { "editable": true }, @@ -2732,7 +2779,7 @@ }, { "cell_type": "markdown", - "id": "ab8f6541", + "id": "148bcb51", "metadata": { "editable": true }, @@ -2753,7 +2800,7 @@ }, { "cell_type": "markdown", - "id": "d52641ac", + "id": "b4ab6685", "metadata": { "editable": true }, @@ -2776,7 +2823,7 @@ { "cell_type": "code", "execution_count": 14, - "id": "8168bc67", + "id": "69cbdd65", "metadata": { "collapsed": false, "editable": true @@ -2811,7 +2858,7 @@ }, { "cell_type": "markdown", - "id": "2c2826ce", + "id": "f30332cd", "metadata": { "editable": true }, @@ -2822,7 +2869,7 @@ { "cell_type": "code", "execution_count": 15, - "id": "445663c0", + "id": "e01570bc", "metadata": { "collapsed": false, "editable": true @@ -2897,7 +2944,7 @@ }, { "cell_type": "markdown", - "id": "aca2efe7", + "id": "3282e79d", "metadata": { "editable": true }, @@ -2907,7 +2954,7 @@ }, { "cell_type": "markdown", - "id": "c3f489f9", + "id": "4c69c2c8", "metadata": { "editable": true }, @@ -2922,7 +2969,7 @@ }, { "cell_type": "markdown", - "id": "36735fa8", + "id": "52e6dc92", "metadata": { "editable": true }, @@ -2934,7 +2981,7 @@ }, { "cell_type": "markdown", - "id": "8e0fd47e", + "id": "3c9043ac", "metadata": { "editable": true }, @@ -2952,7 +2999,7 @@ }, { "cell_type": "markdown", - "id": "165c45c1", + "id": "6ec6074f", "metadata": { "editable": true }, @@ -2971,7 +3018,7 @@ }, { "cell_type": "markdown", - "id": "34e9d01d", + "id": "409904fd", "metadata": { "editable": true }, @@ -2983,7 +3030,7 @@ }, { "cell_type": "markdown", - "id": "4fcd3fe2", + "id": "7773c363", "metadata": { "editable": true }, @@ -2993,7 +3040,7 @@ }, { "cell_type": "markdown", - "id": "eef12186", + "id": "b138dd03", "metadata": { "editable": true }, @@ -3009,7 +3056,7 @@ }, { "cell_type": "markdown", - "id": "f1b327ae", + "id": "f3a981e7", "metadata": { "editable": true }, @@ -3021,7 +3068,7 @@ }, { "cell_type": "markdown", - "id": "843e5f0e", + "id": "19d4d326", "metadata": { "editable": true }, @@ -3031,7 +3078,7 @@ }, { "cell_type": "markdown", - "id": "0b256db8", + "id": "6f4ec233", "metadata": { "editable": true }, @@ -3043,7 +3090,7 @@ }, { "cell_type": "markdown", - "id": "a486e7aa", + "id": "80b2287a", "metadata": { "editable": true }, @@ -3053,7 +3100,7 @@ }, { "cell_type": "markdown", - "id": "bd91d3e0", + "id": "f83f8752", "metadata": { "editable": true }, @@ -3065,7 +3112,7 @@ }, { "cell_type": "markdown", - "id": "fc17ad1e", + "id": "4babf4af", "metadata": { "editable": true }, @@ -3081,7 +3128,7 @@ }, { "cell_type": "markdown", - "id": "f4f69cbf", + "id": "162875aa", "metadata": { "editable": true }, @@ -3093,7 +3140,7 @@ }, { "cell_type": "markdown", - "id": "bedea29a", + "id": "42da2213", "metadata": { "editable": true }, @@ -3126,7 +3173,7 @@ }, { "cell_type": "markdown", - "id": "ff6d5b2c", + "id": "76c40c68", "metadata": { "editable": true }, @@ -3138,7 +3185,7 @@ }, { "cell_type": "markdown", - "id": "7a21b635", + "id": "6aa27f9a", "metadata": { "editable": true }, @@ -3156,7 +3203,7 @@ }, { "cell_type": "markdown", - "id": "092e7434", + "id": "ab85ddc4", "metadata": { "editable": true }, @@ -3166,7 +3213,7 @@ }, { "cell_type": "markdown", - "id": "ba10fb4a", + "id": "6349d4ac", "metadata": { "editable": true }, @@ -3197,7 +3244,7 @@ }, { "cell_type": "markdown", - "id": "bd7a13b2", + "id": "d414cf4b", "metadata": { "editable": true }, @@ -3212,7 +3259,7 @@ }, { "cell_type": "markdown", - "id": "6ace73dd", + "id": "ae053391", "metadata": { "editable": true }, @@ -3230,7 +3277,7 @@ }, { "cell_type": "markdown", - "id": "33888962", + "id": "8b123165", "metadata": { "editable": true }, @@ -3242,7 +3289,7 @@ }, { "cell_type": "markdown", - "id": "7665c788", + "id": "180086fe", "metadata": { "editable": true }, @@ -3254,7 +3301,7 @@ }, { "cell_type": "markdown", - "id": "02b499b9", + "id": "950c7627", "metadata": { "editable": true }, @@ -3272,7 +3319,7 @@ }, { "cell_type": "markdown", - "id": "9ddf4fca", + "id": "43e6754e", "metadata": { "editable": true }, @@ -3295,7 +3342,7 @@ }, { "cell_type": "markdown", - "id": "e5867cac", + "id": "c233aa5f", "metadata": { "editable": true }, @@ -3313,7 +3360,7 @@ }, { "cell_type": "markdown", - "id": "a89d53a0", + "id": "b2b5fa2e", "metadata": { "editable": true }, @@ -3325,7 +3372,7 @@ }, { "cell_type": "markdown", - "id": "0bdc8b50", + "id": "78f014d3", "metadata": { "editable": true }, @@ -3337,7 +3384,7 @@ }, { "cell_type": "markdown", - "id": "f9edf07c", + "id": "a79f4829", "metadata": { "editable": true }, @@ -3349,7 +3396,7 @@ }, { "cell_type": "markdown", - "id": "1abbdd9c", + "id": "43eb2ee4", "metadata": { "editable": true }, @@ -3361,7 +3408,7 @@ }, { "cell_type": "markdown", - "id": "7b00f144", + "id": "805dd5cc", "metadata": { "editable": true }, @@ -3373,7 +3420,7 @@ }, { "cell_type": "markdown", - "id": "90a0afbf", + "id": "ea389ad8", "metadata": { "editable": true }, @@ -3390,7 +3437,7 @@ }, { "cell_type": "markdown", - "id": "c4f96471", + "id": "29c22980", "metadata": { "editable": true }, @@ -3409,7 +3456,7 @@ }, { "cell_type": "markdown", - "id": "26b924de", + "id": "15ff24a8", "metadata": { "editable": true }, @@ -3421,7 +3468,7 @@ }, { "cell_type": "markdown", - "id": "4d98827e", + "id": "c2050047", "metadata": { "editable": true }, @@ -3441,7 +3488,7 @@ }, { "cell_type": "markdown", - "id": "7aff0f96", + "id": "d07aa491", "metadata": { "editable": true }, @@ -3479,7 +3526,7 @@ }, { "cell_type": "markdown", - "id": "8b61c524", + "id": "f4eb855c", "metadata": { "editable": true }, @@ -3491,7 +3538,7 @@ }, { "cell_type": "markdown", - "id": "a605388f", + "id": "b2b03e57", "metadata": { "editable": true }, @@ -3501,7 +3548,7 @@ }, { "cell_type": "markdown", - "id": "5116fd5f", + "id": "ffb43d34", "metadata": { "editable": true }, @@ -3513,7 +3560,7 @@ }, { "cell_type": "markdown", - "id": "b0b06320", + "id": "643a528e", "metadata": { "editable": true }, @@ -3524,7 +3571,7 @@ { "cell_type": "code", "execution_count": 16, - "id": "972ed554", + "id": "272fd295", "metadata": { "collapsed": false, "editable": true @@ -3569,7 +3616,7 @@ }, { "cell_type": "markdown", - "id": "ff530d43", + "id": "7cdf6f19", "metadata": { "editable": true }, @@ -3586,7 +3633,7 @@ { "cell_type": "code", "execution_count": 17, - "id": "f9c4b9f9", + "id": "dcb74d34", "metadata": { "collapsed": false, "editable": true @@ -3614,7 +3661,7 @@ }, { "cell_type": "markdown", - "id": "5bb16647", + "id": "8628fe47", "metadata": { "editable": true }, @@ -3629,7 +3676,7 @@ { "cell_type": "code", "execution_count": 18, - "id": "f5c25a9a", + "id": "f0cf0e3b", "metadata": { "collapsed": false, "editable": true @@ -3673,7 +3720,7 @@ }, { "cell_type": "markdown", - "id": "8691e0f5", + "id": "65746580", "metadata": { "editable": true }, @@ -3683,7 +3730,7 @@ }, { "cell_type": "markdown", - "id": "f5c1a799", + "id": "6cb2fa5e", "metadata": { "editable": true }, @@ -3694,7 +3741,7 @@ { "cell_type": "code", "execution_count": 19, - "id": "7a630992", + "id": "09981165", "metadata": { "collapsed": false, "editable": true @@ -3722,7 +3769,7 @@ }, { "cell_type": "markdown", - "id": "5b1de1ef", + "id": "06842bac", "metadata": { "editable": true }, @@ -3737,7 +3784,7 @@ }, { "cell_type": "markdown", - "id": "1d6f8533", + "id": "d0a00ca6", "metadata": { "editable": true }, @@ -3748,7 +3795,7 @@ { "cell_type": "code", "execution_count": 20, - "id": "6732b4e4", + "id": "b83d619c", "metadata": { "collapsed": false, "editable": true @@ -3776,7 +3823,7 @@ }, { "cell_type": "markdown", - "id": "903e1d95", + "id": "60667807", "metadata": { "editable": true }, @@ -3787,7 +3834,7 @@ { "cell_type": "code", "execution_count": 21, - "id": "14f83c2d", + "id": "e244e18d", "metadata": { "collapsed": false, "editable": true @@ -3812,7 +3859,7 @@ }, { "cell_type": "markdown", - "id": "8a55e3b3", + "id": "027aa974", "metadata": { "editable": true }, @@ -3823,7 +3870,7 @@ { "cell_type": "code", "execution_count": 22, - "id": "c894ef86", + "id": "458af951", "metadata": { "collapsed": false, "editable": true @@ -3859,7 +3906,7 @@ { "cell_type": "code", "execution_count": 23, - "id": "83b8201a", + "id": "8503a971", "metadata": { "collapsed": false, "editable": true @@ -3879,7 +3926,7 @@ }, { "cell_type": "markdown", - "id": "0f7fe07d", + "id": "c161f0c5", "metadata": { "editable": true }, @@ -3890,7 +3937,7 @@ { "cell_type": "code", "execution_count": 24, - "id": "b1f755f4", + "id": "3ef079f8", "metadata": { "collapsed": false, "editable": true @@ -3928,7 +3975,7 @@ }, { "cell_type": "markdown", - "id": "0a1df639", + "id": "034338e5", "metadata": { "editable": true }, @@ -3938,7 +3985,7 @@ }, { "cell_type": "markdown", - "id": "22ea217a", + "id": "3d49aee1", "metadata": { "editable": true }, @@ -3952,7 +3999,7 @@ { "cell_type": "code", "execution_count": 25, - "id": "0dd3c4cb", + "id": "20fadbf2", "metadata": { "collapsed": false, "editable": true @@ -3974,7 +4021,7 @@ }, { "cell_type": "markdown", - "id": "030639cd", + "id": "ab81f79d", "metadata": { "editable": true }, @@ -3984,7 +4031,7 @@ }, { "cell_type": "markdown", - "id": "90cc352d", + "id": "837788c6", "metadata": { "editable": true }, @@ -3995,7 +4042,7 @@ { "cell_type": "code", "execution_count": 26, - "id": "40ed6032", + "id": "f38c1e61", "metadata": { "collapsed": false, "editable": true @@ -4017,7 +4064,7 @@ }, { "cell_type": "markdown", - "id": "67e6173f", + "id": "6aa5cce6", "metadata": { "editable": true }, @@ -4030,7 +4077,7 @@ { "cell_type": "code", "execution_count": 27, - "id": "cbfc002f", + "id": "c9b0afed", "metadata": { "collapsed": false, "editable": true @@ -4055,7 +4102,7 @@ }, { "cell_type": "markdown", - "id": "dd550ee3", + "id": "945e6e5d", "metadata": { "editable": true }, @@ -4067,7 +4114,7 @@ { "cell_type": "code", "execution_count": 28, - "id": "0ab38cff", + "id": "ebaa43ed", "metadata": { "collapsed": false, "editable": true diff --git a/doc/src/week39/week39.do.txt b/doc/src/week39/week39.do.txt index bdf1ceb4b..239fd85a7 100644 --- a/doc/src/week39/week39.do.txt +++ b/doc/src/week39/week39.do.txt @@ -1164,6 +1164,23 @@ We can easily extend our program to minimize $C_{\text{ridge}}(\beta)$ using gra \] !et +!split +===== The Hessian matrix for Ridge Regression ===== +The Hessian matrix of Ridge Regression for our simple example is given by +!bt +\[ +\bm{H} \equiv \begin{bmatrix} +\frac{\partial^2 C(\beta)}{\partial \beta_0^2} & \frac{\partial^2 C(\beta)}{\partial \beta_0 \partial \beta_1} \\ +\frac{\partial^2 C(\beta)}{\partial \beta_0 \partial \beta_1} & \frac{\partial^2 C(\beta)}{\partial \beta_1^2} & \\ +\end{bmatrix} = \frac{2}{n}X^T X+2\lambda\bm{I}. +\] +!et +This implies that the Hessian matrix is positive definite, hence the stationary point is a +minimum. +Note that the Ridge loss function is convex, as a sum of two convex +functions. Therefore, the stationary point is a global +minimum of this function. + !split ===== Program example for gradient descent with Ridge Regression ===== @@ -1186,14 +1203,21 @@ XT_X = X.T @ X #Ridge parameter lambda lmbda = 0.001 -Id = lmbda* np.eye(XT_X.shape[0]) +Id = n*lmbda* np.eye(XT_X.shape[0]) + +# Hessian matrix +H = (2.0/n)* XT_X+2*lmbda +# Get the eigenvalues +EigValues, EigVectors = np.linalg.eig(H) +print(f"Eigenvalues of Hessian Matrix:{EigValues}") + beta_linreg = np.linalg.inv(XT_X+Id) @ X.T @ y print(beta_linreg) # Start plain gradient descent beta = np.random.randn(2,1) -eta = 0.1 +eta = 1.0/np.max(EigValues) Niterations = 100 for iter in range(Niterations):