Skip to content
149 changes: 70 additions & 79 deletions examples/finance/bs_ivbp.ipynb
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

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (E241)

examples/finance/bs_ivbp.ipynb:1:1: E241 Multiple spaces after comma help: Replace with single space

Check failure on line 1 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (E221)

examples/finance/bs_ivbp.ipynb:1:1: E221 Multiple spaces before operator help: Replace with single space

Check failure on line 1 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (E225)

examples/finance/bs_ivbp.ipynb:1:1: E225 Missing whitespace around operator help: Add missing whitespace

Check failure on line 1 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (W291)

examples/finance/bs_ivbp.ipynb:1:1: W291 Trailing whitespace help: Remove trailing whitespace

Check failure on line 1 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (E221)

examples/finance/bs_ivbp.ipynb:1:1: E221 Multiple spaces before operator help: Replace with single space

Check failure on line 1 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (E266)

examples/finance/bs_ivbp.ipynb:1:1: E266 Too many leading `#` before block comment help: Remove leading `#`

Check failure on line 1 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (E266)

examples/finance/bs_ivbp.ipynb:1:1: E266 Too many leading `#` before block comment help: Remove leading `#`

Check failure on line 1 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (E266)

examples/finance/bs_ivbp.ipynb:1:1: E266 Too many leading `#` before block comment help: Remove leading `#`

Check failure on line 1 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (F401)

examples/finance/bs_ivbp.ipynb:1:1: F401 `mpl_toolkits.mplot3d.axis3d.Axis` imported but unused help: Remove unused import: `mpl_toolkits.mplot3d.axis3d.Axis`

Check failure on line 1 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Lint the codebase

ruff (W291)

examples/finance/bs_ivbp.ipynb:1:1: W291 Trailing whitespace help: Remove trailing whitespace
"cells": [
{
"cell_type": "markdown",
Expand Down Expand Up @@ -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"
]
}
],
Copy link
Contributor

@EdCaunt EdCaunt Mar 11, 2025

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

Copy link
Contributor Author

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

Copy link
Contributor

@EdCaunt EdCaunt Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line #19.    plt.plot(t_range, np.array(vals))

Needs a plt.show()


Reply via ReviewNB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

"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",
Expand All @@ -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",
Expand All @@ -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",

Check failure on line 86 in examples/finance/bs_ivbp.ipynb

View workflow job for this annotation

GitHub Actions / Spellcheck everything

typo

`proccess` should be process
"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}\")"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
]
},
Expand All @@ -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"
]
},
{
Expand All @@ -256,7 +258,7 @@
}
],
"source": [
"# NBVAL_IGNORE_OUTPUT\n",
"#NBVAL_IGNORE_OUTPUT\n",
"\n",
"# Run our operators\n",
"startDevito = timer.time()\n",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"
]
},
{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -560,7 +562,7 @@
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x7f5495584640>]"
"[<matplotlib.lines.Line2D at 0x7fe2669fda50>]"
]
},
"execution_count": 9,
Expand All @@ -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",
Expand All @@ -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"
]
},
{
Expand Down
204 changes: 104 additions & 100 deletions examples/seismic/tutorials/03_fwi.ipynb

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions examples/seismic/tutorials/05_staggered_acoustic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"metadata": {},
"outputs": [],
"source": [
"from devito import *\n",
"from devito import (SpaceDimension, Grid, TimeFunction, Constant, solve,\n",
" Operator, Eq, VectorTimeFunction, norm)\n",
"from devito.types import NODE\n",
"from examples.seismic.source import DGaussSource, TimeAxis\n",
"from examples.seismic import plot_image\n",
"import numpy as np\n",
Expand All @@ -28,7 +30,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Initial grid: 1km x 1km, with spacing 100m\n",
"# Set up initial grid: 1km x 1km, with spacing 100m\n",
"extent = (2000., 2000.)\n",
"shape = (81, 81)\n",
"x = SpaceDimension(name='x', spacing=Constant(name='h_x', value=extent[0]/(shape[0]-1)))\n",
Expand Down
Loading
Loading