From b8b72f6b8bb7630c96a4ab5346410123abb78d5a Mon Sep 17 00:00:00 2001 From: George Bisbas Date: Thu, 6 Mar 2025 16:27:33 +0200 Subject: [PATCH] examples: Update bs_ivbp --- examples/finance/bs_ivbp.ipynb | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/examples/finance/bs_ivbp.ipynb b/examples/finance/bs_ivbp.ipynb index e59d6ec2b0..3ccac619f6 100644 --- a/examples/finance/bs_ivbp.ipynb +++ b/examples/finance/bs_ivbp.ipynb @@ -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", @@ -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" ] }, { @@ -263,7 +262,7 @@ "\n", "# Run our operators\n", "startDevito = timer.time()\n", - " \n", + "\n", "# Apply operator\n", "op.apply(dt=dt0)\n", "\n", @@ -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", @@ -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" ] }, { @@ -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", @@ -563,7 +562,7 @@ { "data": { "text/plain": [ - "[]" + "[]" ] }, "execution_count": 9, @@ -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", @@ -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))" ] @@ -613,7 +612,7 @@ { "data": { "text/plain": [ - "0.00581731890853893" + "np.float64(0.005885208362743295)" ] }, "execution_count": 10,