.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/exotic_options/american/plot_01_longstaff_schwartz.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_exotic_options_american_plot_01_longstaff_schwartz.py: Longstaff-Schwartz least-squares Monte Carlo ============================================ Monte Carlo simulates forward, but early exercise is decided backward: whether to exercise today depends on the value of waiting. Francis Longstaff and Eduardo Schwartz (2001) estimated that continuation value by regressing the discounted future cash flows of the in-the-money paths on a few functions of today's price. Working back from expiry, each path is exercised where the payoff beats the fitted value. The method extends to many assets and path-dependent payoffs, where trees and PDEs give up. .. GENERATED FROM PYTHON SOURCE LINES 15-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.exotic_options import longstaff_schwartz, plot_exercise_boundary, regression_basis from financialkit.integrators import gbm_paths from financialkit.options import CRRTree, black_scholes_price S, K, T, r, sigma = 36.0, 40.0, 1.0, 0.06, 0.2 .. GENERATED FROM PYTHON SOURCE LINES 25-30 One regression step ------------------- At t = 0.5 the discounted value of exercising optimally later (approximated here by the European put to expiry) is scattered against the price, with the fitted continuation value. .. GENERATED FROM PYTHON SOURCE LINES 30-44 .. code-block:: Python _, paths = gbm_paths(S, r, sigma, T, 2, 2000, rng=0) St = paths[:, 1] y = np.exp(-r * 0.5) * np.maximum(K - paths[:, 2], 0.0) itm = St < K coef, *_ = np.linalg.lstsq(regression_basis(St[itm] / K), y[itm], rcond=None) grid = np.linspace(St[itm].min(), K, 100) fig, axes = plt.subplots(1, 2, figsize=(12, 4)) axes[0].scatter(St[itm], y[itm], s=3, alpha=0.4, label="discounted cash flow") axes[0].plot(grid, regression_basis(grid / K) @ coef, "k", lw=2, label="fitted continuation") axes[0].plot(grid, K - grid, "C3--", label="exercise value") axes[0].set_xlabel("price at t = 0.5") axes[0].legend() .. image-sg:: /api/gallery/exotic_options/american/images/sphx_glr_plot_01_longstaff_schwartz_001.png :alt: plot 01 longstaff schwartz :srcset: /api/gallery/exotic_options/american/images/sphx_glr_plot_01_longstaff_schwartz_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 45-49 Price and exercise boundary against the binomial tree ----------------------------------------------------- Longstaff and Schwartz's Table 1: the finite-difference value with 50 exercise dates is 4.478. .. GENERATED FROM PYTHON SOURCE LINES 49-61 .. code-block:: Python res = longstaff_schwartz(S, K, T, r, sigma, n_exercise=50, n_paths=20_000, rng=1) tree = CRRTree(500).price(S, K, T, r, sigma, option_type="put", american=True) european = black_scholes_price(S, K, T, r, sigma, option_type="put") print(f"Longstaff-Schwartz {res.estimate:.4f} +/- {res.std_error:.4f}; tree {tree.price:.4f}; European {european:.4f}") print(f"paths exercised early: {res.exercise_probability:.1%}") plot_exercise_boundary(res, ax=axes[1]) axes[1].plot(tree.times, tree.exercise_boundary, "k", lw=1, label="CRR tree") axes[1].set_ylim(30, 41) axes[1].legend() fig.tight_layout() plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none Longstaff-Schwartz 4.4687 +/- 0.0134; tree 4.4864; European 3.8443 paths exercised early: 69.7% .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.624 seconds) .. _sphx_glr_download_api_gallery_exotic_options_american_plot_01_longstaff_schwartz.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/exotic_options/american/plot_01_longstaff_schwartz.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_longstaff_schwartz.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_longstaff_schwartz.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_longstaff_schwartz.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_