.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde_methods/schemes/plot_07_barrier_pde.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_pde_methods_schemes_plot_07_barrier_pde.py: Zvan-Forsyth-Vetzal barrier options =================================== A knock-out option is a boundary condition: the value is zero on the barrier. Zvan, Forsyth and Vetzal (2000) showed that finite differences price barriers accurately when a grid node sits exactly on the barrier and the solver handles the discontinuous data smoothly, rather than letting the barrier fall between nodes. :meth:`~financialkit.pde_methods.ThetaScheme.price_barrier` starts the grid at the barrier, and the result matches the method of images, :math:`C_{do}(S) = C(S) - (H/S)^{2\lambda - 2} C(H^2/S)`. .. GENERATED FROM PYTHON SOURCE LINES 16-34 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.options import black_scholes_price from financialkit.pde_methods import CrankNicolson S, K, H, T, r, sigma, q = 100.0, 100.0, 90.0, 1.0, 0.05, 0.25, 0.0 lam = (r - q + 0.5 * sigma**2) / sigma**2 def images(s): """Down-and-out call by the method of images (Hull, 10th ed., Section 26.9).""" return black_scholes_price(s, K, T, r, sigma, q) - (H / s) ** (2 * lam - 2) * black_scholes_price(H**2 / s, K, T, r, sigma, q) res = CrankNicolson(400, 200, rannacher_steps=2).price_barrier(S, K, H, T, r, sigma, q, "call", "down-and-out") print(f"PDE {res.price:.4f}, method of images {images(S):.4f}, vanilla {black_scholes_price(S, K, T, r, sigma, q):.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none PDE 9.1106, method of images 9.1112, vanilla 12.3360 .. GENERATED FROM PYTHON SOURCE LINES 35-41 A node on the barrier, against a barrier between nodes ------------------------------------------------------ On a grid that ignores the barrier, the knock-out condition can only be imposed at the nearest node, typically half a spacing away. Moving the barrier by h/2 mimics that, and costs an error of order h; the aligned grid keeps the scheme's second order. .. GENERATED FROM PYTHON SOURCE LINES 41-64 .. code-block:: Python ms = np.array([50, 100, 200, 400]) aligned, between = [], [] for m in ms: cn = CrankNicolson(m, 200, rannacher_steps=2) h = (400.0 - H) / m aligned.append(abs(cn.price_barrier(S, K, H, T, r, sigma).price - images(S))) between.append(abs(cn.price_barrier(S, K, H + 0.5 * h, T, r, sigma).price - images(S))) fig, axes = plt.subplots(1, 2, figsize=(11, 4)) near = res.S < 160 axes[0].plot(res.S[near], res.V[near], label="down-and-out call (PDE)") axes[0].plot(res.S[near], black_scholes_price(res.S[near], K, T, r, sigma, q), "--", label="vanilla call") axes[0].axvline(H, color="k", lw=0.8) axes[0].set_xlabel("spot S") axes[0].legend() axes[1].loglog(ms, aligned, "o-", label="node on the barrier") axes[1].loglog(ms, between, "s-", label="barrier half a node away") axes[1].set_xlabel("spot intervals M") axes[1].set_ylabel("absolute error") axes[1].legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/pde_methods/schemes/images/sphx_glr_plot_07_barrier_pde_001.png :alt: plot 07 barrier pde :srcset: /api/gallery/pde_methods/schemes/images/sphx_glr_plot_07_barrier_pde_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.130 seconds) .. _sphx_glr_download_api_gallery_pde_methods_schemes_plot_07_barrier_pde.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/pde_methods/schemes/plot_07_barrier_pde.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_07_barrier_pde.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_07_barrier_pde.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_07_barrier_pde.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_