Skip to content

Commit

Permalink
examples: Update bs_ivbp
Browse files Browse the repository at this point in the history
  • Loading branch information
georgebisbas committed Mar 6, 2025
1 parent 19771e2 commit b8b72f6
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions examples/finance/bs_ivbp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@
],
"source": [
"from devito import (Eq, Grid, TimeFunction, Operator, solve, Constant, \n",
" SpaceDimension, configuration, SubDomain, centered)\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",
"from matplotlib import cm\n",
"\n",
"from sympy.stats import Normal, cdf\n",
"import numpy as np\n",
Expand Down Expand Up @@ -242,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 @@ -263,7 +262,7 @@
"\n",
"# Run our operators\n",
"startDevito = timer.time()\n",
" \n",
"\n",
"# Apply operator\n",
"op.apply(dt=dt0)\n",
"\n",
Expand Down Expand Up @@ -293,7 +292,7 @@
"#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",
Expand Down Expand Up @@ -477,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 @@ -501,10 +500,10 @@
"# 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",
" \n",
"\n",
" d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))\n",
" d2 = (np.log(S / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))\n",
" \n",
"\n",
" call = (S * cdf(N)(d1) - K * np.exp(-r * T) * cdf(N)(d2))\n",
" return call\n",
"\n",
Expand Down Expand Up @@ -563,7 +562,7 @@
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x7f5495584640>]"
"[<matplotlib.lines.Line2D at 0x7fe2669fda50>]"
]
},
"execution_count": 9,
Expand All @@ -587,8 +586,8 @@
"#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",
"x_range = range(padding, smax-smin-padding*2, 1)\n",
"t_range = np.linspace(dt0, 1.0, 50)\n",
"x_range = range(padding, smax-smin-padding*2, 1)\n",
"vals = []\n",
"\n",
"for t in t_range:\n",
Expand All @@ -600,7 +599,7 @@
"\n",
" rms = np.sqrt(np.float64(l2 / len(x_range)))\n",
" vals.append(rms)\n",
" \n",
"\n",
"plt.figure(figsize=(12,10))\n",
"plt.plot(t_range, np.array(vals))"
]
Expand All @@ -613,7 +612,7 @@
{
"data": {
"text/plain": [
"0.00581731890853893"
"np.float64(0.005885208362743295)"
]
},
"execution_count": 10,
Expand Down

0 comments on commit b8b72f6

Please sign in to comment.