|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "hide_input": false, |
| 7 | + "slideshow": { |
| 8 | + "slide_type": "skip" |
| 9 | + } |
| 10 | + }, |
| 11 | + "source": [ |
| 12 | + "<table>\n", |
| 13 | + " <tr align=left><td><img align=left src=\"./images/CC-BY.png\">\n", |
| 14 | + " <td>Text provided under a Creative Commons Attribution license, CC-BY. All code is made available under the FSF-approved MIT license. (c) Kyle T. Mandli</td>\n", |
| 15 | + "</table>" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "metadata": { |
| 22 | + "hide_input": false, |
| 23 | + "slideshow": { |
| 24 | + "slide_type": "skip" |
| 25 | + } |
| 26 | + }, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "from __future__ import print_function\n", |
| 30 | + "\n", |
| 31 | + "%matplotlib inline\n", |
| 32 | + "import numpy\n", |
| 33 | + "import matplotlib.pyplot as plt" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": { |
| 39 | + "slideshow": { |
| 40 | + "slide_type": "skip" |
| 41 | + } |
| 42 | + }, |
| 43 | + "source": [ |
| 44 | + "Note to lecturers: This notebook is designed to work best as a classic Jupyter Notebook with nbextensions \n", |
| 45 | + "* hide_input: to hide selected python cells particularly for just plotting\n", |
| 46 | + "* RISE: Interactive js slide presentations" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": { |
| 52 | + "slideshow": { |
| 53 | + "slide_type": "skip" |
| 54 | + } |
| 55 | + }, |
| 56 | + "source": [ |
| 57 | + "## Why should I care?" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": { |
| 63 | + "slideshow": { |
| 64 | + "slide_type": "skip" |
| 65 | + } |
| 66 | + }, |
| 67 | + "source": [ |
| 68 | + "*Because I have to be here for a requirement...*" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "metadata": { |
| 74 | + "slideshow": { |
| 75 | + "slide_type": "skip" |
| 76 | + } |
| 77 | + }, |
| 78 | + "source": [ |
| 79 | + "*or I might actually need to solve something important...like can I ever retire?*" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "metadata": { |
| 85 | + "slideshow": { |
| 86 | + "slide_type": "skip" |
| 87 | + } |
| 88 | + }, |
| 89 | + "source": [ |
| 90 | + "### An example: The Retirement Problem\n", |
| 91 | + "\n", |
| 92 | + "$$A = \\frac{P}{r} \\left((1+r)^n - 1 \\right)$$\n", |
| 93 | + "\n", |
| 94 | + "$A$ is the total amount after n payments\n", |
| 95 | + "\n", |
| 96 | + "$P$ is the incremental payment\n", |
| 97 | + "\n", |
| 98 | + "$r$ is the interest rate per payment period\n", |
| 99 | + "\n", |
| 100 | + "$n$ is the number of payments\n", |
| 101 | + "\n", |
| 102 | + "\n", |
| 103 | + "\n", |
| 104 | + "Note that these can all be functions of $r$, $n$, and time" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "markdown", |
| 109 | + "metadata": { |
| 110 | + "hide_input": true, |
| 111 | + "slideshow": { |
| 112 | + "slide_type": "subslide" |
| 113 | + } |
| 114 | + }, |
| 115 | + "source": [ |
| 116 | + "What is the minimum interest rate $r$ required to accrue \\$1M over 20 years saving \\\\$1000 per month?\n", |
| 117 | + "\n", |
| 118 | + "$$\n", |
| 119 | + "A = \\frac{P}{r} \\left((1+r)^n - 1 \\right)\n", |
| 120 | + "$$" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": null, |
| 126 | + "metadata": { |
| 127 | + "hide_input": false, |
| 128 | + "jupyter": { |
| 129 | + "source_hidden": true |
| 130 | + }, |
| 131 | + "slideshow": { |
| 132 | + "slide_type": "skip" |
| 133 | + } |
| 134 | + }, |
| 135 | + "outputs": [], |
| 136 | + "source": [ |
| 137 | + "def A(P, r, n):\n", |
| 138 | + " return P / r * ((1 + r)**n - 1)\n", |
| 139 | + "\n", |
| 140 | + "P = 1000.\n", |
| 141 | + "years = numpy.linspace(0,20,241)\n", |
| 142 | + "n = 12*years\n", |
| 143 | + "target = 1.e6" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": null, |
| 149 | + "metadata": { |
| 150 | + "hide_input": false, |
| 151 | + "jupyter": { |
| 152 | + "source_hidden": true |
| 153 | + }, |
| 154 | + "slideshow": { |
| 155 | + "slide_type": "skip" |
| 156 | + } |
| 157 | + }, |
| 158 | + "outputs": [], |
| 159 | + "source": [ |
| 160 | + "# find the root using scipy's brentq method\n", |
| 161 | + "from scipy.optimize import brentq\n", |
| 162 | + "n_max = n[-1]\n", |
| 163 | + "f = lambda r : target - A(P,r/12., n_max)\n", |
| 164 | + "r_target = brentq(f,0.1,0.15)" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "code", |
| 169 | + "execution_count": null, |
| 170 | + "metadata": { |
| 171 | + "hide_input": true, |
| 172 | + "jupyter": { |
| 173 | + "source_hidden": true |
| 174 | + }, |
| 175 | + "slideshow": { |
| 176 | + "slide_type": "fragment" |
| 177 | + } |
| 178 | + }, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "fig = plt.figure(figsize=(8,6))\n", |
| 182 | + "fig.set_figwidth(fig.get_figwidth() * 2)\n", |
| 183 | + "\n", |
| 184 | + "axes = fig.add_subplot(1, 2, 1)\n", |
| 185 | + "for r in [0.02, 0.05, 0.08, 0.1, 0.12, 0.15]:\n", |
| 186 | + " axes.plot(years, A(P, r/12., n),label='r = {}'.format(r))\n", |
| 187 | + "axes.plot(years, numpy.ones(years.shape) * target, 'k--',linewidth=2,label=\"Target\")\n", |
| 188 | + "axes.legend(loc='best')\n", |
| 189 | + "axes.set_xlabel(\"Years\",fontsize=18)\n", |
| 190 | + "axes.set_ylabel(\"Annuity Value (Dollars)\",fontsize=18)\n", |
| 191 | + "axes.set_title(\"The Forward Problem $A(P,r,n)$\",fontsize=20)\n", |
| 192 | + "axes.grid()\n", |
| 193 | + "\n", |
| 194 | + "axes = fig.add_subplot(1, 2, 2)\n", |
| 195 | + "r = numpy.linspace(1.e-6,0.15,100)\n", |
| 196 | + "target = 1.e6\n", |
| 197 | + "axes.plot(A(P,r/12.,n_max),r,linewidth=2)\n", |
| 198 | + "axes.plot(numpy.ones(r.shape) * target, r,'k--',linewidth=2)\n", |
| 199 | + "axes.scatter(A(P,r_target/12,n_max),r_target,s=100,c='r')\n", |
| 200 | + "axes.set_xlabel('Annuity Value (Dollars)',fontsize=18)\n", |
| 201 | + "axes.set_title('The Inverse Problem $r(A,P,n)$, $r\\geq$ {:.3f}'.format(r_target),fontsize=20)\n", |
| 202 | + "axes.grid()\n", |
| 203 | + "plt.show()" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "markdown", |
| 208 | + "metadata": { |
| 209 | + "slideshow": { |
| 210 | + "slide_type": "fragment" |
| 211 | + } |
| 212 | + }, |
| 213 | + "source": [ |
| 214 | + "This is a rootfinding problem for a function of a single variable" |
| 215 | + ] |
| 216 | + }, |
| 217 | + { |
| 218 | + "cell_type": "markdown", |
| 219 | + "metadata": { |
| 220 | + "slideshow": { |
| 221 | + "slide_type": "skip" |
| 222 | + } |
| 223 | + }, |
| 224 | + "source": [ |
| 225 | + "### Another Example: Boat race (numerical quadrature of arc length)\n", |
| 226 | + "Given a river with coordinates $(x,f(x))$ find the total length actually rowed over a given interval $x\\in [0,X]$\n", |
| 227 | + "\n", |
| 228 | + "Example: a sinusoidal river $$f(x) = A \\sin x$$" |
| 229 | + ] |
| 230 | + }, |
| 231 | + { |
| 232 | + "cell_type": "code", |
| 233 | + "execution_count": null, |
| 234 | + "metadata": { |
| 235 | + "hide_input": true, |
| 236 | + "slideshow": { |
| 237 | + "slide_type": "skip" |
| 238 | + } |
| 239 | + }, |
| 240 | + "outputs": [], |
| 241 | + "source": [ |
| 242 | + "A=.5\n", |
| 243 | + "fig = plt.figure(figsize=(8,6))\n", |
| 244 | + "x = numpy.linspace(0,10,100)\n", |
| 245 | + "plt.plot(x,A*numpy.sin(x),'r',linewidth=2,)\n", |
| 246 | + "plt.xlabel('distance (km)')\n", |
| 247 | + "plt.grid()\n", |
| 248 | + "plt.title('The Boat Race: $f(x)=A\\sin(x)')\n", |
| 249 | + "plt.show()" |
| 250 | + ] |
| 251 | + }, |
| 252 | + { |
| 253 | + "cell_type": "markdown", |
| 254 | + "metadata": { |
| 255 | + "slideshow": { |
| 256 | + "slide_type": "skip" |
| 257 | + } |
| 258 | + }, |
| 259 | + "source": [ |
| 260 | + "We need to calculate the function $f(x)$'s arc-length from $[0, X]$ e.g.\n", |
| 261 | + "\n", |
| 262 | + "\\begin{align}\n", |
| 263 | + " L(X) &= \\int_0^{X} \\sqrt{1 + |f'(x)|^2} dx\\\\\n", |
| 264 | + " &= \\int_0^{X} \\sqrt{1 + A^2\\cos^2(x)} dx\\\\\n", |
| 265 | + "\\end{align}\n", |
| 266 | + "\n", |
| 267 | + "In general the value of this integral cannot be solved analytically (the answer is actually an elliptic integral). We usually need to approximate the integral using a numerical quadrature." |
| 268 | + ] |
| 269 | + }, |
| 270 | + { |
| 271 | + "cell_type": "markdown", |
| 272 | + "metadata": { |
| 273 | + "hide_input": true, |
| 274 | + "slideshow": { |
| 275 | + "slide_type": "skip" |
| 276 | + } |
| 277 | + }, |
| 278 | + "source": [ |
| 279 | + "## Why is this exciting?\n", |
| 280 | + "\n", |
| 281 | + "Computers have revolutionized our ability to explore...\n", |
| 282 | + "\n", |
| 283 | + "[Top 10 Algorithms of the 20th Century](http://www.siam.org/pdf/news/637.pdf?t=1&cn=ZmxleGlibGVfcmVjcw%3D%3D&refsrc=email&iid=658bdab6af614c83a8df1f8e02035eae&uid=755271476&nid=244+285282312)\n", |
| 284 | + "\n", |
| 285 | + "...and there is more to come!" |
| 286 | + ] |
| 287 | + } |
| 288 | + ], |
| 289 | + "metadata": { |
| 290 | + "celltoolbar": "Slideshow", |
| 291 | + "kernelspec": { |
| 292 | + "display_name": "Python 3", |
| 293 | + "language": "python", |
| 294 | + "name": "python3" |
| 295 | + }, |
| 296 | + "language_info": { |
| 297 | + "codemirror_mode": { |
| 298 | + "name": "ipython", |
| 299 | + "version": 3 |
| 300 | + }, |
| 301 | + "file_extension": ".py", |
| 302 | + "mimetype": "text/x-python", |
| 303 | + "name": "python", |
| 304 | + "nbconvert_exporter": "python", |
| 305 | + "pygments_lexer": "ipython3", |
| 306 | + "version": "3.7.5" |
| 307 | + }, |
| 308 | + "latex_envs": { |
| 309 | + "bibliofile": "biblio.bib", |
| 310 | + "cite_by": "apalike", |
| 311 | + "current_citInitial": 1, |
| 312 | + "eqLabelWithNumbers": true, |
| 313 | + "eqNumInitial": 0 |
| 314 | + } |
| 315 | + }, |
| 316 | + "nbformat": 4, |
| 317 | + "nbformat_minor": 4 |
| 318 | +} |
0 commit comments