-
Notifications
You must be signed in to change notification settings - Fork 253
examples: Refresh several notebooks in examples/ #2542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
974611f
757cf39
ebd2356
0233701
8208a53
6e2998f
a6f02a8
d6735ea
2be1b7e
e4736a9
5fec3ad
89f4cd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
|
Check failure on line 1 in examples/finance/bs_ivbp.ipynb
|
||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
|
|
@@ -27,18 +27,20 @@ | |
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "dt,tmax,nt; 0.0005 1.0 2001\n", | ||
| "shape; (81,)\n", | ||
| "origin; (60,)\n", | ||
| "spacing; (1.0,)\n", | ||
| "extent; 80\n" | ||
| "dt,tmax,nt: 0.0005, 1.0, 2001\n", | ||
| "shape: (81,)\n", | ||
| "origin: (60,)\n", | ||
| "spacing: (1.0,)\n", | ||
| "extent: 80\n" | ||
| ] | ||
| } | ||
| ], | ||
georgebisbas marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why?
georgebisbas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "source": [ | ||
| "from devito import (Eq, Grid, TimeFunction, Operator, solve, Constant,\n", | ||
| "from devito import (Eq, Grid, TimeFunction, Operator, solve, Constant, \n", | ||
| " SpaceDimension, configuration, centered)\n", | ||
| "\n", | ||
| "from mpl_toolkits.mplot3d import Axes3D\n", | ||
| "from mpl_toolkits.mplot3d.axis3d import Axis\n", | ||
| "import matplotlib.pyplot as plt\n", | ||
| "import matplotlib as mpl\n", | ||
| "\n", | ||
|
|
@@ -51,7 +53,7 @@ | |
| "\n", | ||
| "configuration[\"log-level\"] = 'INFO'\n", | ||
| "\n", | ||
| "# Constants\n", | ||
| "## Constants\n", | ||
| "# The strike price of the option\n", | ||
| "K = 100.0\n", | ||
| "\n", | ||
|
|
@@ -67,42 +69,42 @@ | |
| "\n", | ||
| "# If you want to try some different problems, uncomment these lines\n", | ||
| "\n", | ||
| "# Example 2\n", | ||
| "## Example 2\n", | ||
| "# K = 10.0\n", | ||
| "# r = 0.1\n", | ||
| "# sigma = 0.2\n", | ||
| "# smin = 0.0\n", | ||
| "# smax = 20.0\n", | ||
| "\n", | ||
| "# Example 3\n", | ||
| "## Example 3\n", | ||
| "# K = 100.0\n", | ||
| "# r = 0.05\n", | ||
| "# sigma = 0.25\n", | ||
| "# smin = 50.0\n", | ||
| "# smax = 150.0\n", | ||
| "\n", | ||
| "# Amount of padding to process left/right (hidden from graphs)\n", | ||
| "# Amount of padding to proccess left/right (hidden from graphs)\n", | ||
| "padding = 10\n", | ||
| "smin -= padding\n", | ||
| "smax += padding\n", | ||
| "\n", | ||
| "# Extent calculations\n", | ||
| "tmax = 1.0\n", | ||
| "dt0 = 0.0005\n", | ||
| "ds0 = 1.0\n", | ||
| "ds0 = 1.0\n", | ||
| "nt = (int)(tmax / dt0) + 1\n", | ||
| "ns = int((smax - smin) / ds0) + 1\n", | ||
| "ns = int((smax - smin) / ds0) + 1 \n", | ||
| "\n", | ||
| "shape = (ns, )\n", | ||
| "origin = (smin, )\n", | ||
| "origin =(smin, )\n", | ||
| "spacing = (ds0, )\n", | ||
| "extent = int(ds0 * (ns - 1))\n", | ||
| "\n", | ||
| "print(\"dt,tmax,nt;\", dt0, tmax, nt)\n", | ||
| "print(\"shape; \", shape)\n", | ||
| "print(\"origin; \", origin)\n", | ||
| "print(\"spacing; \", spacing)\n", | ||
| "print(\"extent; \", extent)" | ||
| "print(f\"dt,tmax,nt: {dt0}, {tmax}, {nt}\")\n", | ||
| "print(f\"shape: {shape}\")\n", | ||
| "print(f\"origin: {origin}\")\n", | ||
| "print(f\"spacing: {spacing}\")\n", | ||
| "print(f\"extent: {extent}\")" | ||
| ] | ||
| }, | ||
| { | ||
|
|
@@ -162,10 +164,10 @@ | |
| "grid = Grid(shape=shape, origin=origin, extent=extent, dimensions=(s, ))\n", | ||
| "\n", | ||
| "so = 2\n", | ||
| "v = TimeFunction(name='v', grid=grid, space_order=so, time_order=1, save=nt)\n", | ||
| "v_no_bc = TimeFunction(name='v_no_bc', grid=grid, space_order=so, time_order=1, save=nt)\n", | ||
| "v = TimeFunction(name='v', grid=grid, space_order=so, time_order=1, save=nt)\n", | ||
| "v_no_bc = TimeFunction(name='v_no_bc', grid=grid, space_order=so, time_order=1, save=nt)\n", | ||
| "\n", | ||
| "t, s = v.dimensions\n", | ||
| "t,s = v.dimensions\n", | ||
| "ds = s.spacing\n", | ||
| "dt = t.spacing\n", | ||
| "\n", | ||
|
|
@@ -215,18 +217,18 @@ | |
| "outputs": [], | ||
| "source": [ | ||
| "# Equations with Neumann boundary conditions\n", | ||
| "eq = [Eq(v[t, extent], v[t, extent-1]+(v[t, extent-1]-v[t, extent-2])),\n", | ||
| " Eq(v[t, extent+1], v[t, extent]+(v[t, extent-1]-v[t, extent-2])),\n", | ||
| "eq = [Eq(v[t,extent], v[t,extent-1]+(v[t,extent-1]-v[t,extent-2])),\n", | ||
| " Eq(v[t,extent+1], v[t,extent]+(v[t,extent-1]-v[t,extent-2])),\n", | ||
| " Eq(v.forward, update_centered)]\n", | ||
| "eq_no_bc = [Eq(v.forward, update_centered)]\n", | ||
| "\n", | ||
| "op = Operator(eq, subs=v.grid.spacing_map)\n", | ||
| "op = Operator(eq, subs=v.grid.spacing_map)\n", | ||
| "op_no_bc = Operator(eq_no_bc, subs=v_no_bc.grid.spacing_map)\n", | ||
| "\n", | ||
| "# Initial conditions\n", | ||
| "\n", | ||
| "for i in range(shape[0]):\n", | ||
| " v.data[0, i] = max((smin + ds0 * i) - K, 0)\n", | ||
| " v.data[0, i] = max((smin + ds0 * i) - K, 0)\n", | ||
| " v_no_bc.data[0, i] = max((smin + ds0 * i) - K, 0)" | ||
| ] | ||
| }, | ||
|
|
@@ -239,8 +241,8 @@ | |
| "name": "stderr", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "Operator `Kernel` run in 0.01 s\n", | ||
| "Operator `Kernel` run in 0.01 s\n" | ||
| "Operator `Kernel` ran in 0.01 s\n", | ||
| "Operator `Kernel` ran in 0.01 s\n" | ||
| ] | ||
| }, | ||
| { | ||
|
|
@@ -256,7 +258,7 @@ | |
| } | ||
| ], | ||
| "source": [ | ||
| "# NBVAL_IGNORE_OUTPUT\n", | ||
| "#NBVAL_IGNORE_OUTPUT\n", | ||
| "\n", | ||
| "# Run our operators\n", | ||
| "startDevito = timer.time()\n", | ||
|
|
@@ -287,27 +289,27 @@ | |
| } | ||
| ], | ||
| "source": [ | ||
| "# NBVAL_IGNORE_OUTPUT\n", | ||
| "#NBVAL_IGNORE_OUTPUT\n", | ||
| "\n", | ||
| "# Get an appropriate ylimit\n", | ||
| "slice_smax = v.data[:, int(smax-smin-padding)]\n", | ||
| "ymax = max(slice_smax) + 2\n", | ||
| "\n", | ||
| "# Plot\n", | ||
| "s = np.linspace(smin, smax, shape[0])\n", | ||
| "plt.figure(figsize=(12, 10), facecolor='w')\n", | ||
| "plt.figure(figsize=(12,10), facecolor='w')\n", | ||
| "\n", | ||
| "time = [1*nt//5, 2*nt//5, 3*nt//5, 4*nt//5, 5*nt//5-1]\n", | ||
| "colors = [\"blue\", \"green\", \"gold\", \"darkorange\", \"red\"]\n", | ||
| "\n", | ||
| "# initial conditions\n", | ||
| "plt.plot(s, v_no_bc.data[0, :], '-', color=\"black\", label='initial condition', linewidth=1)\n", | ||
| "plt.plot(s, v_no_bc.data[0,:], '-', color=\"black\", label='initial condition', linewidth=1)\n", | ||
| "\n", | ||
| "for i in range(len(time)):\n", | ||
| " plt.plot(s, v_no_bc.data[time[i], :], '-', color=colors[i], label='t='+str(time[i]*dt0), linewidth=1.5)\n", | ||
| " plt.plot(s, v_no_bc.data[time[i],:], '-', color=colors[i], label='t='+str(time[i]*dt0), linewidth=1.5)\n", | ||
| "\n", | ||
| "plt.xlim([smin+padding, smax-padding])\n", | ||
| "plt.ylim([0, ymax])\n", | ||
| "plt.xlim([smin+padding,smax-padding])\n", | ||
| "plt.ylim([0,ymax])\n", | ||
| "\n", | ||
| "plt.legend(loc=2)\n", | ||
| "plt.grid(True)\n", | ||
|
|
@@ -341,27 +343,27 @@ | |
| } | ||
| ], | ||
| "source": [ | ||
| "# NBVAL_IGNORE_OUTPUT\n", | ||
| "#NBVAL_IGNORE_OUTPUT\n", | ||
| "\n", | ||
| "# Get an appropriate ylimit\n", | ||
| "slice_smax = v.data[:, int(smax-smin-padding)]\n", | ||
| "slice_smax = v.data[:,int(smax-smin-padding)]\n", | ||
| "ymax = max(slice_smax) + 2\n", | ||
| "\n", | ||
| "# Plot\n", | ||
| "s = np.linspace(smin, smax, shape[0])\n", | ||
| "plt.figure(figsize=(12, 10), facecolor='w')\n", | ||
| "plt.figure(figsize=(12,10), facecolor='w')\n", | ||
| "\n", | ||
| "time = [1*nt//5, 2*nt//5, 3*nt//5, 4*nt//5, 5*nt//5-1]\n", | ||
| "colors = [\"blue\", \"green\", \"gold\", \"darkorange\", \"red\"]\n", | ||
| "\n", | ||
| "# initial conditions\n", | ||
| "plt.plot(s, v.data[0, :], '-', color=\"black\", label='initial condition', linewidth=1)\n", | ||
| "plt.plot(s, v.data[0,:], '-', color=\"black\", label='initial condition', linewidth=1)\n", | ||
| "\n", | ||
| "for i in range(len(time)):\n", | ||
| " plt.plot(s, v.data[time[i], :], '-', color=colors[i], label='t='+str(time[i]*dt0), linewidth=1.5)\n", | ||
| " plt.plot(s, v.data[time[i],:], '-', color=colors[i], label='t='+str(time[i]*dt0), linewidth=1.5)\n", | ||
| "\n", | ||
| "plt.xlim([smin+padding, smax-padding])\n", | ||
| "plt.ylim([0, ymax])\n", | ||
| "plt.xlim([smin+padding,smax-padding])\n", | ||
| "plt.ylim([0,ymax])\n", | ||
| "\n", | ||
| "plt.legend(loc=2)\n", | ||
| "plt.grid(True)\n", | ||
|
|
@@ -403,8 +405,9 @@ | |
| } | ||
| ], | ||
| "source": [ | ||
| "# NBVAL_IGNORE_OUTPUT\n", | ||
| "#NBVAL_IGNORE_OUTPUT\n", | ||
| "\n", | ||
| "from mpl_toolkits.mplot3d import Axes3D\n", | ||
| "\n", | ||
| "# Trim the padding off smin and smax\n", | ||
| "trim_data = v.data[:, padding:-padding]\n", | ||
|
|
@@ -413,15 +416,15 @@ | |
| "tt = np.linspace(0.0, dt0*(nt-1), nt)\n", | ||
| "ss = np.linspace(smin+padding, smax-padding, shape[0]-padding*2)\n", | ||
| "\n", | ||
| "hf = plt.figure(figsize=(12, 12))\n", | ||
| "hf = plt.figure(figsize=(12,12))\n", | ||
| "ha = plt.axes(projection='3d')\n", | ||
| "\n", | ||
| "# 45 degree viewpoint\n", | ||
| "ha.view_init(elev=25, azim=-45)\n", | ||
| "\n", | ||
| "ha.set_xlim3d([0.0, 1.0])\n", | ||
| "ha.set_ylim3d([smin+padding, smax-padding])\n", | ||
| "ha.set_zlim3d([0, ymax])\n", | ||
| "ha.set_xlim3d([0.0,1.0])\n", | ||
| "ha.set_ylim3d([smin+padding,smax-padding])\n", | ||
| "ha.set_zlim3d([0,ymax])\n", | ||
| "\n", | ||
| "ha.set_xlabel('Time to expiration', labelpad=12, fontsize=16)\n", | ||
| "ha.set_ylabel('Stock value', labelpad=12, fontsize=16)\n", | ||
|
|
@@ -473,8 +476,8 @@ | |
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "devito pde timesteps: 2000, 0.205647s runtime\n", | ||
| "call_value_bs timesteps: 5, 3.813932s runtime\n" | ||
| "devito pde timesteps: 2000, 0.019737s runtime\n", | ||
| "call_value_bs timesteps: 5, 2.596800s runtime\n" | ||
| ] | ||
| }, | ||
| { | ||
|
|
@@ -491,9 +494,9 @@ | |
| } | ||
| ], | ||
| "source": [ | ||
| "# NBVAL_IGNORE_OUTPUT\n", | ||
| "#NBVAL_IGNORE_OUTPUT\n", | ||
| "\n", | ||
| "# Derived formula for Black Scholes call from\n", | ||
| "# Derived formula for Black Scholes call from \n", | ||
| "# https://aaronschlegel.me/black-scholes-formula-python.html\n", | ||
| "def call_value_bs(S, K, T, r, sigma):\n", | ||
| " N = Normal('x', 0.0, 1.0)\n", | ||
|
|
@@ -504,7 +507,6 @@ | |
| " call = (S * cdf(N)(d1) - K * np.exp(-r * T) * cdf(N)(d2))\n", | ||
| " return call\n", | ||
| "\n", | ||
| "\n", | ||
| "startBF = timer.time()\n", | ||
| "\n", | ||
| "# Calculate truth and compare to our solution\n", | ||
|
|
@@ -519,21 +521,21 @@ | |
| "\n", | ||
| "endBF = timer.time()\n", | ||
| "\n", | ||
| "print(f\"devito pde timesteps: {nt - 1}, {endDevito - startDevito:12.6f}s runtime\")\n", | ||
| "print(f\"call_value_bs timesteps: {len(time)}, {endBF - startBF:12.6f}s runtime\")\n", | ||
| "\n", | ||
| "print(\"devito pde timesteps: %12.6s, %12.6fs runtime\" % (nt-1, endDevito - startDevito))\n", | ||
| "print(\"call_value_bs timesteps: %12.6s, %12.6fs runtime\" % (len(time), endBF - startBF))\n", | ||
| " \n", | ||
| "s2 = np.linspace(smin, smax, shape[0])\n", | ||
| "plt.figure(figsize=(12, 10))\n", | ||
| "plt.figure(figsize=(12,10))\n", | ||
| "\n", | ||
| "colors = [\"blue\", \"green\", \"gold\", \"darkorange\", \"red\"]\n", | ||
| "plt.plot(s2, v.data[0, :], '-', color=\"black\", label='initial condition', linewidth=1)\n", | ||
| "plt.plot(s2, v.data[0,:], '-', color=\"black\", label='initial condition', linewidth=1)\n", | ||
| "\n", | ||
| "for i in range(len(time)):\n", | ||
| " plt.plot(s2, results[i], ':', color=colors[i], label='truth t='+str(time[i]), linewidth=3)\n", | ||
| " plt.plot(s2, v.data[int(time[i]*nt), :], '-', color=colors[i], label='pde t='+str(time[i]), linewidth=1)\n", | ||
| " plt.plot(s2, results[i], ':', color=colors[i], label='truth t='+str(time[i]), linewidth=3)\n", | ||
| " plt.plot(s2, v.data[int(time[i]*nt),:], '-', color=colors[i], label='pde t='+str(time[i]), linewidth=1)\n", | ||
| "\n", | ||
| "plt.xlim([smin+padding, smax-padding])\n", | ||
| "plt.ylim([0, ymax])\n", | ||
| "plt.xlim([smin+padding,smax-padding])\n", | ||
| "plt.ylim([0,ymax])\n", | ||
| "\n", | ||
| "plt.legend()\n", | ||
| "plt.grid(True)\n", | ||
|
|
@@ -560,7 +562,7 @@ | |
| { | ||
| "data": { | ||
| "text/plain": [ | ||
| "[<matplotlib.lines.Line2D at 0x7f5495584640>]" | ||
| "[<matplotlib.lines.Line2D at 0x7fe2669fda50>]" | ||
| ] | ||
| }, | ||
| "execution_count": 9, | ||
|
|
@@ -581,7 +583,7 @@ | |
| } | ||
| ], | ||
| "source": [ | ||
| "# NBVAL_IGNORE_OUTPUT\n", | ||
| "#NBVAL_IGNORE_OUTPUT\n", | ||
| "\n", | ||
| "# Plot the l2 norm of the formula and our solution over time\n", | ||
| "t_range = np.linspace(dt0, 1.0, 50)\n", | ||
|
|
@@ -592,36 +594,25 @@ | |
| " l2 = 0.0\n", | ||
| " for x in x_range:\n", | ||
| " truth = call_value_bs(x+smin, K, t, r, sigma)\n", | ||
| " val = v.data[int(t*(nt-1)), x]\n", | ||
| " l2 += (truth - val)**2\n", | ||
| " val = v.data[int(t*(nt-1)), x]\n", | ||
| " l2 += (truth - val)**2\n", | ||
| "\n", | ||
| " rms = np.sqrt(np.float64(l2 / len(x_range)))\n", | ||
| " vals.append(rms)\n", | ||
| "\n", | ||
| "plt.figure(figsize=(12, 10))\n", | ||
| "plt.figure(figsize=(12,10))\n", | ||
| "plt.plot(t_range, np.array(vals))" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 10, | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "data": { | ||
| "text/plain": [ | ||
| "0.00581731890853893" | ||
| ] | ||
| }, | ||
| "execution_count": 10, | ||
| "metadata": {}, | ||
| "output_type": "execute_result" | ||
| } | ||
| ], | ||
| "outputs": [], | ||
| "source": [ | ||
| "# NBVAL_IGNORE_OUTPUT\n", | ||
| "#NBVAL_IGNORE_OUTPUT\n", | ||
| "\n", | ||
| "np.mean(vals)" | ||
| "assert np.isclose(np.mean(vals), 0.005885, rtol=1e-2)\n" | ||
| ] | ||
| }, | ||
| { | ||
|
|
||
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this notebook be renamed "01_black_scholes" for consistency with other tutorials?
Reply via ReviewNB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be happy with that