.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/options/implied_vol/plot_01_implied_volatility.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_implied_vol_plot_01_implied_volatility.py: Latané-Rendleman implied volatility =================================== Every input to Black-Scholes is observable except the volatility. Henry Latané and Richard Rendleman (1976) turned the formula around: given a market price, solve for the volatility that reproduces it. The price is strictly increasing in :math:`\sigma` between Merton's bounds, so the root is unique; it exists only for prices strictly inside them. .. GENERATED FROM PYTHON SOURCE LINES 13-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.options import black_scholes_price, implied_volatility, no_arbitrage_bounds .. GENERATED FROM PYTHON SOURCE LINES 19-21 Hull's example (Section 15.11) ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 21-24 .. code-block:: Python print(f"implied vol of a 1.875 call (S = 21, K = 20, r = 10%, 3 months): {implied_volatility(1.875, 21.0, 20.0, 0.25, 0.10):.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none implied vol of a 1.875 call (S = 21, K = 20, r = 10%, 3 months): 0.2345 .. GENERATED FROM PYTHON SOURCE LINES 25-27 The price is monotone in volatility, so the inverse is unique ------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-48 .. code-block:: Python S, K, T, r = 100.0, 110.0, 0.5, 0.03 sigmas = np.linspace(0.001, 2.0, 400) prices = black_scholes_price(S, K, T, r, sigmas) lo, hi = no_arbitrage_bounds(S, K, T, r) target = 4.0 iv = implied_volatility(target, S, K, T, r) print(f"a {target} call has implied volatility {iv:.4f}") fig, ax = plt.subplots(figsize=(7, 4)) ax.plot(sigmas, prices, label="Black-Scholes price") ax.axhline(lo, color="k", ls=":", label="Merton bounds") ax.axhline(hi, color="k", ls=":") ax.plot([0, iv, iv], [target, target, 0], "r--", lw=1) ax.plot(iv, target, "ro", label=f"implied vol = {iv:.3f}") ax.set_xlabel("volatility") ax.set_ylabel("call price") ax.set_ylim(0, 60) ax.legend() fig.tight_layout() .. image-sg:: /api/gallery/options/implied_vol/images/sphx_glr_plot_01_implied_volatility_001.png :alt: plot 01 implied volatility :srcset: /api/gallery/options/implied_vol/images/sphx_glr_plot_01_implied_volatility_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none a 4.0 call has implied volatility 0.2538 .. GENERATED FROM PYTHON SOURCE LINES 49-51 Prices outside the bounds have no implied volatility ---------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-58 .. code-block:: Python try: implied_volatility(0.5 * lo, S, 80.0, T, r) except ValueError as err: print("ValueError:", err) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none ValueError: price 0 violates the no-arbitrage bounds (21.191, 100); no implied volatility exists .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.048 seconds) .. _sphx_glr_download_api_gallery_options_implied_vol_plot_01_implied_volatility.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/implied_vol/plot_01_implied_volatility.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_implied_volatility.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_implied_volatility.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_implied_volatility.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_