diff --git a/M1/Numerical Optimisation/ComputerSession2.ipynb b/M1/Numerical Optimisation/ComputerSession2.ipynb index 9d41b59..2b2a265 100644 --- a/M1/Numerical Optimisation/ComputerSession2.ipynb +++ b/M1/Numerical Optimisation/ComputerSession2.ipynb @@ -1,8 +1,8 @@ { "cells": [ { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "# Computer session 2\n", "\n", @@ -30,13 +30,23 @@ ] }, { + "cell_type": "code", + "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2025-03-18T16:19:14.314484Z", "start_time": "2025-03-18T16:19:13.728014Z" } }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(-2.3283064365386963e-09, 31)\n" + ] + } + ], "source": [ "import numpy as np\n", "\n", @@ -57,21 +67,11 @@ "f = lambda x: np.tanh(x)\n", "aL, aR = -20, 3\n", "print(dichotomy(f, aL, aR))" - ], - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(-2.3283064365386963e-09, 31)\n" - ] - } - ], - "execution_count": 1 + ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## Solving one-dimensional equation with the Newton and the secant method\n", "\n", @@ -93,13 +93,31 @@ ] }, { + "cell_type": "code", + "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2025-03-18T16:19:17.447647Z", "start_time": "2025-03-18T16:19:17.442560Z" } }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(inf, 'Method diverges')\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/var/folders/tp/_ld5_pzs6nx6mv1pbjhq1l740000gn/T/ipykernel_25957/3868809151.py:14: RuntimeWarning: overflow encountered in exp\n", + " f = lambda x: np.log(np.exp(x) + np.exp(-x))\n" + ] + } + ], "source": [ "def Newton(phi, dphi, x0, eps=1e-10):\n", " iter = 0\n", @@ -118,29 +136,11 @@ "x0 = 1.8\n", "df = lambda x: (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))\n", "print(Newton(f, df, x0))" - ], - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(inf, 'Method diverges')\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/var/folders/tp/_ld5_pzs6nx6mv1pbjhq1l740000gn/T/ipykernel_25957/3868809151.py:14: RuntimeWarning: overflow encountered in exp\n", - " f = lambda x: np.log(np.exp(x) + np.exp(-x))\n" - ] - } - ], - "execution_count": 2 + ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "### Secant method\n", "\n", @@ -153,13 +153,25 @@ ] }, { + "cell_type": "code", + "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2025-03-18T16:19:19.649523Z", "start_time": "2025-03-18T16:19:19.456149Z" } }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(inf, 'Method diverges')\n", + "(inf, 'Method diverges')\n", + "(inf, 'Method diverges')\n" + ] + } + ], "source": [ "def Secant(phi, x0, x1, eps=1e-8):\n", " iter = 0\n", @@ -176,23 +188,11 @@ "\n", "for x0, x1 in xx:\n", " print(Secant(f, x0, x1))" - ], - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(inf, 'Method diverges')\n", - "(inf, 'Method diverges')\n", - "(inf, 'Method diverges')\n" - ] - } - ], - "execution_count": 3 + ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## Combining dichotomy and the Newton method\n", "\n", @@ -211,13 +211,31 @@ ] }, { + "cell_type": "code", + "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2025-03-18T16:44:41.592150Z", "start_time": "2025-03-18T16:44:41.584318Z" } }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(inf, 'Method diverges')\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/var/folders/tp/_ld5_pzs6nx6mv1pbjhq1l740000gn/T/ipykernel_25957/1578277506.py:23: RuntimeWarning: overflow encountered in exp\n", + " f = lambda x: np.log(np.exp(x) + np.exp(-x))\n" + ] + } + ], "source": [ "def DichotomyNewton(phi, dphi, aL, aR, s=0.1, eps=1e-10):\n", " iter = 0\n", @@ -244,29 +262,11 @@ "f = lambda x: np.log(np.exp(x) + np.exp(-x))\n", "df = lambda x: (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))\n", "print(DichotomyNewton(f, df, -20, 3))" - ], - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(inf, 'Method diverges')\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/var/folders/tp/_ld5_pzs6nx6mv1pbjhq1l740000gn/T/ipykernel_25957/1578277506.py:23: RuntimeWarning: overflow encountered in exp\n", - " f = lambda x: np.log(np.exp(x) + np.exp(-x))\n" - ] - } - ], - "execution_count": 22 + ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## Solving an optimisation problem using the Newton method\n", "\n", @@ -285,13 +285,28 @@ ] }, { + "cell_type": "code", + "execution_count": 38, "metadata": { "ExecuteTime": { "end_time": "2025-03-18T17:43:43.061916Z", "start_time": "2025-03-18T17:43:43.042625Z" } }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Optimal point (Newton): 0.9299901531755377\n", + "Objective function value at optimal point (Newton): 1.9329918821224974\n", + "Number of iterations (Newton): 100\n", + "Optimal point (Dichotomy): inf\n", + "Objective function value at optimal point (Dichotomy): inf\n", + "Number of iterations (Dichotomy): Method diverges\n" + ] + } + ], "source": [ "import numpy as np\n", "\n", @@ -360,26 +375,11 @@ "print(f\"Optimal point (Dichotomy): {optimal_point_dichotomy}\")\n", "print(f\"Objective function value at optimal point (Dichotomy): {objective_function(optimal_point_dichotomy)}\")\n", "print(f\"Number of iterations (Dichotomy): {iterations_dichotomy}\")" - ], - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Optimal point (Newton): 0.9299901531755377\n", - "Objective function value at optimal point (Newton): 1.9329918821224974\n", - "Number of iterations (Newton): 100\n", - "Optimal point (Dichotomy): inf\n", - "Objective function value at optimal point (Dichotomy): inf\n", - "Number of iterations (Dichotomy): Method diverges\n" - ] - } - ], - "execution_count": 38 + ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## The Newton method to solve boundary value problems\n", "\n", @@ -493,16 +493,16 @@ ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "The result is a system of equations for $\\mathbf{y}=(y_1,\\dots,y_{N-1})$ :\n", "$$G(\\mathbf{y})=0,\\quad G:\\mathbb{R}^{N-1}\\to\\mathbb{R}^{N-1}.$$" ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "This system can be solved using Newton's method. Note that the Jacobian\n", "$\\partial G/\\partial \\mathbb{y}$ is _tridiagonal_.\n", @@ -522,8 +522,8 @@ ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "**Remark:** In the context of numerical optimal control, these two numerical\n", "methods are often called _indirect method_ (for the shooting method) and _direct\n", @@ -531,8 +531,8 @@ ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## Who’s the best?\n", "\n", @@ -541,9 +541,9 @@ ] }, { - "metadata": {}, "cell_type": "markdown", - "source": "" + "metadata": {}, + "source": [] }, { "cell_type": "code", @@ -560,16 +560,18 @@ "source": [] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, - "source": "\n" + "metadata": {}, + "outputs": [], + "source": [ + "\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -583,7 +585,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.21" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/M1/Statistical Learning/TP4_Ridge_Lasso_and_CV.ipynb b/M1/Statistical Learning/TP4_Ridge_Lasso_and_CV.ipynb index 1d85bb2..8db0071 100644 --- a/M1/Statistical Learning/TP4_Ridge_Lasso_and_CV.ipynb +++ b/M1/Statistical Learning/TP4_Ridge_Lasso_and_CV.ipynb @@ -32,85 +32,32 @@ }, { "cell_type": "code", + "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2025-03-26T10:33:52.177602Z", "start_time": "2025-03-26T10:33:52.174201Z" } }, + "outputs": [], "source": [ "import warnings\n", "\n", "warnings.filterwarnings('ignore')" - ], - "outputs": [], - "execution_count": 4 + ] }, { "cell_type": "code", + "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2025-03-26T10:34:01.222808Z", "start_time": "2025-03-26T10:34:01.200754Z" } }, - "source": [ - "import numpy as np\n", - "import pandas as pd # dataframes are in pandas \n", - "import matplotlib.pyplot as plt\n", - "\n", - "hitters = pd.read_csv(\"data/Hitters.csv\", index_col=\"Name\")\n", - "\n", - "hitters" - ], "outputs": [ { "data": { - "text/plain": [ - " AtBat Hits HmRun Runs RBI Walks Years CAtBat CHits \\\n", - "Name \n", - "-Andy Allanson 293 66 1 30 29 14 1 293 66 \n", - "-Alan Ashby 315 81 7 24 38 39 14 3449 835 \n", - "-Alvin Davis 479 130 18 66 72 76 3 1624 457 \n", - "-Andre Dawson 496 141 20 65 78 37 11 5628 1575 \n", - "-Andres Galarraga 321 87 10 39 42 30 2 396 101 \n", - "... ... ... ... ... ... ... ... ... ... \n", - "-Willie McGee 497 127 7 65 48 37 5 2703 806 \n", - "-Willie Randolph 492 136 5 76 50 94 12 5511 1511 \n", - "-Wayne Tolleson 475 126 3 61 43 52 6 1700 433 \n", - "-Willie Upshaw 573 144 9 85 60 78 8 3198 857 \n", - "-Willie Wilson 631 170 9 77 44 31 11 4908 1457 \n", - "\n", - " CHmRun CRuns CRBI CWalks League Division PutOuts \\\n", - "Name \n", - "-Andy Allanson 1 30 29 14 A E 446 \n", - "-Alan Ashby 69 321 414 375 N W 632 \n", - "-Alvin Davis 63 224 266 263 A W 880 \n", - "-Andre Dawson 225 828 838 354 N E 200 \n", - "-Andres Galarraga 12 48 46 33 N E 805 \n", - "... ... ... ... ... ... ... ... \n", - "-Willie McGee 32 379 311 138 N E 325 \n", - "-Willie Randolph 39 897 451 875 A E 313 \n", - "-Wayne Tolleson 7 217 93 146 A W 37 \n", - "-Willie Upshaw 97 470 420 332 A E 1314 \n", - "-Willie Wilson 30 775 357 249 A W 408 \n", - "\n", - " Assists Errors Salary NewLeague \n", - "Name \n", - "-Andy Allanson 33 20 NaN A \n", - "-Alan Ashby 43 10 475.0 N \n", - "-Alvin Davis 82 14 480.0 A \n", - "-Andre Dawson 11 3 500.0 N \n", - "-Andres Galarraga 40 4 91.5 N \n", - "... ... ... ... ... \n", - "-Willie McGee 9 3 700.0 N \n", - "-Willie Randolph 381 20 875.0 A \n", - "-Wayne Tolleson 113 7 385.0 A \n", - "-Willie Upshaw 131 12 960.0 A \n", - "-Willie Wilson 4 3 1000.0 A \n", - "\n", - "[322 rows x 20 columns]" - ], "text/html": [ "