.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde_methods/american/plot_03_penalty_method.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_pde_methods_american_plot_03_penalty_method.py: Forsyth-Vetzal penalty method ============================= Peter Forsyth and Kenneth Vetzal (2002) replaced the complementarity constraint by a penalty: on nodes where the value falls below the payoff, add :math:`\rho\,(g - x)` with a large :math:`\rho`. The resulting nonlinear system is solved by a Newton (policy) iteration that only changes which nodes are penalised, and converges in two or three tridiagonal solves per time step, independently of the grid size -- unlike PSOR, whose sweep count grows as the grid is refined. :func:`~financialkit.pde_methods.penalty_solve` implements it. .. GENERATED FROM PYTHON SOURCE LINES 16-33 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.pde_methods import CrankNicolson S, K, T, r, sigma = 100.0, 100.0, 1.0, 0.06, 0.3 ms = np.array([50, 100, 200, 400]) iters = {"penalty": [], "psor": []} prices = {"penalty": [], "psor": []} for m in ms: for method in iters: res = CrankNicolson(m, 50, rannacher_steps=2, american_method=method).price(S, K, T, r, sigma, option_type="put", american=True) iters[method].append(res.lcp_iterations / 54) # 50 steps, two of them as four half steps prices[method].append(res.price) for m, a, b in zip(ms, prices["penalty"], prices["psor"], strict=True): print(f"M = {m:3d}: penalty {a:.6f}, PSOR {b:.6f}") .. rst-class:: sphx-glr-script-out .. code-block:: none M = 50: penalty 9.432503, PSOR 9.432503 M = 100: penalty 9.510534, PSOR 9.510534 M = 200: penalty 9.523474, PSOR 9.523474 M = 400: penalty 9.526267, PSOR 9.526266 .. GENERATED FROM PYTHON SOURCE LINES 34-36 Iterations per time step as the grid is refined ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 36-44 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 4)) ax.semilogy(ms, iters["penalty"], "o-", label="penalty (Newton)") ax.semilogy(ms, iters["psor"], "s-", label="PSOR") ax.set_xlabel("spot intervals M") ax.set_ylabel("iterations per time step") ax.legend() plt.show() .. image-sg:: /api/gallery/pde_methods/american/images/sphx_glr_plot_03_penalty_method_001.png :alt: plot 03 penalty method :srcset: /api/gallery/pde_methods/american/images/sphx_glr_plot_03_penalty_method_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.068 seconds) .. _sphx_glr_download_api_gallery_pde_methods_american_plot_03_penalty_method.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/pde_methods/american/plot_03_penalty_method.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_penalty_method.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_penalty_method.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_penalty_method.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_