.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/options/closed_form/plot_03_black_scholes.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_closed_form_plot_03_black_scholes.py: The Black-Scholes formula ========================= Fischer Black and Myron Scholes (1973) showed that a continuously rebalanced position in the stock and a bond replicates a European option when the stock follows geometric Brownian motion, so the option price cannot depend on investors' expected return: .. math:: c = S N(d_1) - K e^{-rT} N(d_2). The formula equals the discounted expected payoff under the risk-neutral drift :math:`r`, which a Monte Carlo over exact GBM paths confirms. .. GENERATED FROM PYTHON SOURCE LINES 18-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.integrators import gbm_paths from financialkit.options import black_scholes_price from financialkit.options.visualizers import plot_option_value .. GENERATED FROM PYTHON SOURCE LINES 26-28 Risk-neutral Monte Carlo agrees with the formula ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 28-35 .. code-block:: Python S0, K, T, r, sigma = 100.0, 105.0, 1.0, 0.05, 0.2 _, paths = gbm_paths(S0, r, sigma, T, n_steps=1, n_paths=400_000, rng=0) discounted = np.exp(-r * T) * np.maximum(paths[:, -1] - K, 0.0) print(f"Black-Scholes: {black_scholes_price(S0, K, T, r, sigma):.4f}") print(f"Monte Carlo: {discounted.mean():.4f} +/- {1.96 * discounted.std() / np.sqrt(discounted.size):.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Black-Scholes: 8.0214 Monte Carlo: 8.0392 +/- 0.0409 .. GENERATED FROM PYTHON SOURCE LINES 36-38 The price as a function of spot, approaching the payoff at expiry ----------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-53 .. code-block:: Python S = np.linspace(60, 150, 181) fig, axes = plt.subplots(1, 2, figsize=(11, 4)) for tau in (1.0, 0.25, 0.02): plot_option_value(S, black_scholes_price(S, K, tau, r, sigma), K, ax=axes[0], label=f"T = {tau}") axes[0].set_title("call value against spot") for vol in (0.1, 0.2, 0.4): axes[1].plot(S, black_scholes_price(S, K, T, r, vol, option_type="put"), label=f"sigma = {vol}") axes[1].set_xlabel("spot S") axes[1].set_ylabel("put value") axes[1].set_title("higher volatility, higher price") axes[1].legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/options/closed_form/images/sphx_glr_plot_03_black_scholes_001.png :alt: call value against spot, higher volatility, higher price :srcset: /api/gallery/options/closed_form/images/sphx_glr_plot_03_black_scholes_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.092 seconds) .. _sphx_glr_download_api_gallery_options_closed_form_plot_03_black_scholes.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/closed_form/plot_03_black_scholes.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_black_scholes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_black_scholes.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_black_scholes.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_