.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/options/greeks/plot_01_greeks.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_options_greeks_plot_01_greeks.py: The Black-Scholes Greeks ======================== The Black-Scholes argument hedges an option with :math:`\Delta = \partial V/\partial S` shares; the other partial derivatives measure how that hedge and the option's value respond to everything else. The Greeks, named by traders after the 1973 paper, satisfy the pricing PDE itself, .. math:: \Theta + \tfrac12 \sigma^2 S^2 \Gamma + (r - q) S \Delta = r V, so time decay is the price of positive gamma. Each analytic Greek is checked here against a finite difference of the price. .. GENERATED FROM PYTHON SOURCE LINES 18-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.options import black_scholes_greeks, black_scholes_price, finite_difference_greeks from financialkit.options.visualizers import plot_greeks .. GENERATED FROM PYTHON SOURCE LINES 25-27 Hull's Example 19.1, analytic and by finite differences ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-34 .. code-block:: Python args = (49.0, 50.0, 0.3846, 0.05, 0.20) g = black_scholes_greeks(*args) fd = finite_difference_greeks(black_scholes_price, *args) for name in ("delta", "gamma", "vega", "theta", "rho", "epsilon", "vanna", "volga"): print(f"{name:>7}: analytic {getattr(g, name):+10.5f} finite difference {getattr(fd, name):+10.5f}") .. rst-class:: sphx-glr-script-out .. code-block:: none delta: analytic +0.52160 finite difference +0.52160 gamma: analytic +0.06555 finite difference +0.06555 vega: analytic +12.10524 finite difference +12.10524 theta: analytic -4.30539 finite difference -4.30539 rho: analytic +8.90657 finite difference +8.90657 epsilon: analytic -9.82979 finite difference -9.82979 vanna: analytic +0.13914 finite difference +0.13914 volga: analytic -0.22906 finite difference -0.22906 .. GENERATED FROM PYTHON SOURCE LINES 35-37 The PDE holds across spot ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-44 .. code-block:: Python S = np.linspace(50, 150, 201) K, T, r, q, sigma = 100.0, 0.5, 0.05, 0.0, 0.25 g = black_scholes_greeks(S, K, T, r, sigma, q) residual = g.theta + 0.5 * sigma**2 * S**2 * g.gamma + r * S * g.delta - r * black_scholes_price(S, K, T, r, sigma) print(f"max PDE residual: {np.max(np.abs(residual)):.1e}") .. rst-class:: sphx-glr-script-out .. code-block:: none max PDE residual: 2.7e-15 .. GENERATED FROM PYTHON SOURCE LINES 45-47 The Greeks of a call against spot --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 47-54 .. code-block:: Python axes = plot_greeks(S, g) for ax in axes: ax.axvline(K, color="k", lw=0.5) plt.gcf().tight_layout() plt.show() .. image-sg:: /api/gallery/options/greeks/images/sphx_glr_plot_01_greeks_001.png :alt: delta, gamma, vega, theta, rho, vanna :srcset: /api/gallery/options/greeks/images/sphx_glr_plot_01_greeks_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.133 seconds) .. _sphx_glr_download_api_gallery_options_greeks_plot_01_greeks.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/options/greeks/plot_01_greeks.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_greeks.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_greeks.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_greeks.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_