.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/options/trees/plot_01_crr_convergence.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_trees_plot_01_crr_convergence.py: Cox-Ross-Rubinstein: the binomial tree converges to Black-Scholes ================================================================= John Cox, Stephen Ross and Mark Rubinstein (1979) showed that the Black-Scholes price is the limit of a much simpler model: in each short period the stock moves up by :math:`u = e^{\sigma\sqrt{\Delta t}}` or down by :math:`d = 1/u`, and the option is replicated one step at a time. The risk-neutral probability :math:`p = (e^{r\Delta t} - d)/(u - d)` falls out of the replication, and backward induction prices any payoff. The error shrinks like :math:`1/n`, oscillating as the strike moves between nodes. .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.options import CRRTree, black_scholes_price from financialkit.options.visualizers import plot_tree_convergence .. GENERATED FROM PYTHON SOURCE LINES 23-29 One-period replication ---------------------- A call struck at 100 on a stock at 100 that moves to 110 or 90 in a year, with r = 5%: hold :math:`\Delta` shares and borrow so the portfolio matches the call in both states. .. GENERATED FROM PYTHON SOURCE LINES 29-38 .. code-block:: Python S, K, u, d, r = 100.0, 100.0, 1.1, 0.9, 0.05 cu, cd = max(S * u - K, 0), max(S * d - K, 0) delta = (cu - cd) / (S * u - S * d) bond = (cu - delta * S * u) * np.exp(-r) p = (np.exp(r) - d) / (u - d) print(f"replicate with {delta:.2f} shares and {bond:.3f} in bonds: price {delta * S + bond:.4f}") print(f"risk-neutral expectation with p = {p:.4f}: {np.exp(-r) * (p * cu + (1 - p) * cd):.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none replicate with 0.50 shares and -42.805 in bonds: price 7.1947 risk-neutral expectation with p = 0.7564: 7.1947 .. GENERATED FROM PYTHON SOURCE LINES 39-41 Convergence to Black-Scholes ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-60 .. code-block:: Python S, K, T, r, sigma = 100.0, 95.0, 1.0, 0.05, 0.2 exact = black_scholes_price(S, K, T, r, sigma) steps = np.arange(5, 301, 1) prices = np.array([CRRTree(n).price(S, K, T, r, sigma).price for n in steps]) print(f"Black-Scholes {exact:.5f}; CRR with 300 steps {prices[-1]:.5f}") fig, axes = plt.subplots(1, 2, figsize=(11, 4)) axes[0].plot(steps, prices, lw=1, label="CRR tree") axes[0].axhline(exact, color="k", ls=":", label="Black-Scholes") axes[0].set_xlabel("time steps n") axes[0].set_ylabel("call price") axes[0].legend() plot_tree_convergence(steps, prices, exact, ax=axes[1], label="CRR error") axes[1].loglog(steps, 1.0 / steps, "k:", label="1/n") axes[1].legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/options/trees/images/sphx_glr_plot_01_crr_convergence_001.png :alt: plot 01 crr convergence :srcset: /api/gallery/options/trees/images/sphx_glr_plot_01_crr_convergence_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Black-Scholes 13.34646; CRR with 300 steps 13.34822 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.217 seconds) .. _sphx_glr_download_api_gallery_options_trees_plot_01_crr_convergence.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/trees/plot_01_crr_convergence.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_crr_convergence.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_crr_convergence.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_crr_convergence.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_