|
37 | 37 | ], |
38 | 38 | "source": [ |
39 | 39 | "from devito import (Eq, Grid, TimeFunction, Operator, solve, Constant, \n", |
40 | | - " SpaceDimension, configuration, SubDomain, centered)\n", |
| 40 | + " SpaceDimension, configuration, centered)\n", |
41 | 41 | "\n", |
42 | 42 | "from mpl_toolkits.mplot3d import Axes3D\n", |
43 | 43 | "from mpl_toolkits.mplot3d.axis3d import Axis\n", |
44 | 44 | "import matplotlib.pyplot as plt\n", |
45 | 45 | "import matplotlib as mpl\n", |
46 | | - "from matplotlib import cm\n", |
47 | 46 | "\n", |
48 | 47 | "from sympy.stats import Normal, cdf\n", |
49 | 48 | "import numpy as np\n", |
|
242 | 241 | "name": "stderr", |
243 | 242 | "output_type": "stream", |
244 | 243 | "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" |
247 | 246 | ] |
248 | 247 | }, |
249 | 248 | { |
|
263 | 262 | "\n", |
264 | 263 | "# Run our operators\n", |
265 | 264 | "startDevito = timer.time()\n", |
266 | | - " \n", |
| 265 | + "\n", |
267 | 266 | "# Apply operator\n", |
268 | 267 | "op.apply(dt=dt0)\n", |
269 | 268 | "\n", |
|
293 | 292 | "#NBVAL_IGNORE_OUTPUT\n", |
294 | 293 | "\n", |
295 | 294 | "# 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", |
297 | 296 | "ymax = max(slice_smax) + 2\n", |
298 | 297 | "\n", |
299 | 298 | "# Plot\n", |
|
477 | 476 | "name": "stdout", |
478 | 477 | "output_type": "stream", |
479 | 478 | "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" |
482 | 481 | ] |
483 | 482 | }, |
484 | 483 | { |
|
501 | 500 | "# https://aaronschlegel.me/black-scholes-formula-python.html\n", |
502 | 501 | "def call_value_bs(S, K, T, r, sigma):\n", |
503 | 502 | " N = Normal('x', 0.0, 1.0)\n", |
504 | | - " \n", |
| 503 | + "\n", |
505 | 504 | " d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))\n", |
506 | 505 | " d2 = (np.log(S / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))\n", |
507 | | - " \n", |
| 506 | + "\n", |
508 | 507 | " call = (S * cdf(N)(d1) - K * np.exp(-r * T) * cdf(N)(d2))\n", |
509 | 508 | " return call\n", |
510 | 509 | "\n", |
|
563 | 562 | { |
564 | 563 | "data": { |
565 | 564 | "text/plain": [ |
566 | | - "[<matplotlib.lines.Line2D at 0x7f5495584640>]" |
| 565 | + "[<matplotlib.lines.Line2D at 0x7fe2669fda50>]" |
567 | 566 | ] |
568 | 567 | }, |
569 | 568 | "execution_count": 9, |
|
587 | 586 | "#NBVAL_IGNORE_OUTPUT\n", |
588 | 587 | "\n", |
589 | 588 | "# 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", |
592 | 591 | "vals = []\n", |
593 | 592 | "\n", |
594 | 593 | "for t in t_range:\n", |
|
600 | 599 | "\n", |
601 | 600 | " rms = np.sqrt(np.float64(l2 / len(x_range)))\n", |
602 | 601 | " vals.append(rms)\n", |
603 | | - " \n", |
| 602 | + "\n", |
604 | 603 | "plt.figure(figsize=(12,10))\n", |
605 | 604 | "plt.plot(t_range, np.array(vals))" |
606 | 605 | ] |
|
613 | 612 | { |
614 | 613 | "data": { |
615 | 614 | "text/plain": [ |
616 | | - "0.00581731890853893" |
| 615 | + "np.float64(0.005885208362743295)" |
617 | 616 | ] |
618 | 617 | }, |
619 | 618 | "execution_count": 10, |
|
0 commit comments