Skip to content

Commit 08c02b8

Browse files
author
George Bisbas
committed
examples: Update bs_ivbp
1 parent b85b2e3 commit 08c02b8

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

examples/finance/bs_ivbp.ipynb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@
3737
],
3838
"source": [
3939
"from devito import (Eq, Grid, TimeFunction, Operator, solve, Constant, \n",
40-
" SpaceDimension, configuration, SubDomain, centered)\n",
40+
" SpaceDimension, configuration, centered)\n",
4141
"\n",
4242
"from mpl_toolkits.mplot3d import Axes3D\n",
4343
"from mpl_toolkits.mplot3d.axis3d import Axis\n",
4444
"import matplotlib.pyplot as plt\n",
4545
"import matplotlib as mpl\n",
46-
"from matplotlib import cm\n",
4746
"\n",
4847
"from sympy.stats import Normal, cdf\n",
4948
"import numpy as np\n",
@@ -242,8 +241,8 @@
242241
"name": "stderr",
243242
"output_type": "stream",
244243
"text": [
245-
"Operator `Kernel` run in 0.01 s\n",
246-
"Operator `Kernel` run in 0.01 s\n"
244+
"Operator `Kernel` ran in 0.01 s\n",
245+
"Operator `Kernel` ran in 0.01 s\n"
247246
]
248247
},
249248
{
@@ -263,7 +262,7 @@
263262
"\n",
264263
"# Run our operators\n",
265264
"startDevito = timer.time()\n",
266-
" \n",
265+
"\n",
267266
"# Apply operator\n",
268267
"op.apply(dt=dt0)\n",
269268
"\n",
@@ -293,7 +292,7 @@
293292
"#NBVAL_IGNORE_OUTPUT\n",
294293
"\n",
295294
"# Get an appropriate ylimit\n",
296-
"slice_smax = v.data[:,int(smax-smin-padding)]\n",
295+
"slice_smax = v.data[:, int(smax-smin-padding)]\n",
297296
"ymax = max(slice_smax) + 2\n",
298297
"\n",
299298
"# Plot\n",
@@ -477,8 +476,8 @@
477476
"name": "stdout",
478477
"output_type": "stream",
479478
"text": [
480-
"devito pde timesteps: 2000, 0.205647s runtime\n",
481-
"call_value_bs timesteps: 5, 3.813932s runtime\n"
479+
"devito pde timesteps: 2000, 0.019737s runtime\n",
480+
"call_value_bs timesteps: 5, 2.596800s runtime\n"
482481
]
483482
},
484483
{
@@ -501,10 +500,10 @@
501500
"# https://aaronschlegel.me/black-scholes-formula-python.html\n",
502501
"def call_value_bs(S, K, T, r, sigma):\n",
503502
" N = Normal('x', 0.0, 1.0)\n",
504-
" \n",
503+
"\n",
505504
" d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))\n",
506505
" d2 = (np.log(S / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))\n",
507-
" \n",
506+
"\n",
508507
" call = (S * cdf(N)(d1) - K * np.exp(-r * T) * cdf(N)(d2))\n",
509508
" return call\n",
510509
"\n",
@@ -563,7 +562,7 @@
563562
{
564563
"data": {
565564
"text/plain": [
566-
"[<matplotlib.lines.Line2D at 0x7f5495584640>]"
565+
"[<matplotlib.lines.Line2D at 0x7fe2669fda50>]"
567566
]
568567
},
569568
"execution_count": 9,
@@ -587,8 +586,8 @@
587586
"#NBVAL_IGNORE_OUTPUT\n",
588587
"\n",
589588
"# Plot the l2 norm of the formula and our solution over time\n",
590-
"t_range = np.linspace(dt0,1.0,50)\n",
591-
"x_range = range(padding, smax-smin-padding*2, 1)\n",
589+
"t_range = np.linspace(dt0, 1.0, 50)\n",
590+
"x_range = range(padding, smax-smin-padding*2, 1)\n",
592591
"vals = []\n",
593592
"\n",
594593
"for t in t_range:\n",
@@ -600,7 +599,7 @@
600599
"\n",
601600
" rms = np.sqrt(np.float64(l2 / len(x_range)))\n",
602601
" vals.append(rms)\n",
603-
" \n",
602+
"\n",
604603
"plt.figure(figsize=(12,10))\n",
605604
"plt.plot(t_range, np.array(vals))"
606605
]
@@ -613,7 +612,7 @@
613612
{
614613
"data": {
615614
"text/plain": [
616-
"0.00581731890853893"
615+
"np.float64(0.005885208362743295)"
617616
]
618617
},
619618
"execution_count": 10,

0 commit comments

Comments
 (0)