.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/stochastic_volatility/local_vol/plot_01_cox_cev.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_stochastic_volatility_local_vol_plot_01_cox_cev.py: Cox's constant elasticity of variance ===================================== In 1975 John Cox let the volatility depend on the price, :math:`dS = r S\,dt + \sigma S^{\beta}\,dW`. The local volatility is :math:`\sigma S^{\beta - 1}`: for :math:`\beta < 1` it rises as the price falls, the leverage effect Fischer Black had just documented, and the implied volatility skews down in the strike. Schroder (1989) gave the prices in closed form with the noncentral chi-square distribution. Here the closed form is checked against an Euler simulation of the SDE and the skew is shown for several elasticities, each scaled to a 20% at-the-money local volatility. .. GENERATED FROM PYTHON SOURCE LINES 17-27 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.integrators import euler_maruyama, njit from financialkit.options import implied_volatility from financialkit.stochastic_volatility import cev_local_vol, cev_price from financialkit.stochastic_volatility.visualizers import plot_smile S, T, r = 100.0, 1.0, 0.03 .. GENERATED FROM PYTHON SOURCE LINES 28-30 Closed form against simulation ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 30-49 .. code-block:: Python @njit def drift(x, t, p): return p[0] * x @njit def diffusion(x, t, p): return p[1] * np.maximum(x, 0.0) ** p[2] beta = 0.5 sigma = 0.2 * S ** (1 - beta) _, paths = euler_maruyama(drift, diffusion, S, T, 100, 20_000, params=[r, sigma, beta], rng=0) for K in (80.0, 100.0, 120.0): pay = np.exp(-r * T) * np.maximum(paths[:, -1] - K, 0.0) print(f"K = {K:5.0f}: Schroder {cev_price(S, K, T, r, sigma, beta):.4f}, Euler MC {pay.mean():.4f} +/- {2 * pay.std() / np.sqrt(pay.size):.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none K = 80: Schroder 23.4115, Euler MC 23.5832 +/- 0.2600 K = 100: Schroder 9.4167, Euler MC 9.5563 +/- 0.1902 K = 120: Schroder 2.4856, Euler MC 2.5879 +/- 0.1011 .. GENERATED FROM PYTHON SOURCE LINES 50-52 The skew -------- .. GENERATED FROM PYTHON SOURCE LINES 52-67 .. code-block:: Python K = np.linspace(60, 150, 31) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4)) grid = np.linspace(40, 180, 100) for beta in (0.0001, 0.5, 0.8, 1.5): sigma = 0.2 * S ** (1 - beta) ax1.plot(grid, cev_local_vol(grid, sigma, beta), label=f"beta = {beta:g}") vols = implied_volatility(cev_price(S, K, T, r, sigma, beta), S, K, T, r) plot_smile(K, vols, ax=ax2, label=f"beta = {beta:g}") ax1.set_xlabel("spot S") ax1.set_ylabel("local volatility") ax1.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/stochastic_volatility/local_vol/images/sphx_glr_plot_01_cox_cev_001.png :alt: plot 01 cox cev :srcset: /api/gallery/stochastic_volatility/local_vol/images/sphx_glr_plot_01_cox_cev_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.542 seconds) .. _sphx_glr_download_api_gallery_stochastic_volatility_local_vol_plot_01_cox_cev.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/stochastic_volatility/local_vol/plot_01_cox_cev.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_cox_cev.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_cox_cev.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_cox_cev.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_