.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/interest_rate_models/short_rate/plot_04_vasicek.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_interest_rate_models_short_rate_plot_04_vasicek.py: Vasicek's mean-reverting short rate =================================== Oldrich Vasicek (1977) derived the term structure from a short rate pulled back towards a long-run mean, .. math:: dr = \kappa(\theta - r)\,dt + \sigma\,dW, an Ornstein-Uhlenbeck process, together with the market price of risk that makes bond prices consistent. Bonds are exponential-affine in the rate, :math:`P = A(\tau) e^{-B(\tau) r}`, and the curve is upward sloping, humped or inverted depending on where :math:`r_0` stands against the long yield :math:`\theta - \sigma^2/(2\kappa^2)`. .. GENERATED FROM PYTHON SOURCE LINES 18-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.interest_rate_models import VasicekModel, monte_carlo_bond_prices from financialkit.interest_rate_models.visualizers import plot_model_yields, plot_short_rate_paths .. GENERATED FROM PYTHON SOURCE LINES 25-27 Three curve shapes from one model --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-35 .. code-block:: Python fig, axes = plt.subplots(1, 3, figsize=(14, 4)) for r0, shape in ((0.01, "normal"), (0.0555, "humped"), (0.09, "inverted")): m = VasicekModel(r0, 0.25, 0.06, 0.02) plot_model_yields(m, t_max=30, ax=axes[0], label=f"r0 = {100 * r0:.2f}% ({shape})") axes[0].axhline(100 * m.long_yield, color="k", ls=":", lw=1) axes[0].set_title("Vasicek yield curves") .. image-sg:: /api/gallery/interest_rate_models/short_rate/images/sphx_glr_plot_04_vasicek_001.png :alt: Vasicek yield curves :srcset: /api/gallery/interest_rate_models/short_rate/images/sphx_glr_plot_04_vasicek_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Vasicek yield curves') .. GENERATED FROM PYTHON SOURCE LINES 36-38 Exact simulation and mean reversion ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-45 .. code-block:: Python model = VasicekModel(0.01, 0.25, 0.06, 0.02) times, r = model.simulate(20.0, 200, 5000, rng=0) plot_short_rate_paths(times, r, n_show=12, ax=axes[1], label="simulated") axes[1].plot(times, 100 * (0.06 + (0.01 - 0.06) * np.exp(-0.25 * times)), "w--", lw=1, label="E[r(t)]") axes[1].legend() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 46-48 Closed form against Monte Carlo ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: Python res = monte_carlo_bond_prices(model, np.arange(1.0, 21.0), n_steps=200, n_paths=8000, rng=1) axes[2].errorbar(res.maturities, res.z_scores, yerr=1, fmt="o", ms=3) axes[2].axhspan(-2, 2, color="0.9") axes[2].set_xlabel("maturity (years)") axes[2].set_ylabel("(MC - exact) / SE") axes[2].set_title(f"max |z| = {np.abs(res.z_scores).max():.2f}") fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.116 seconds) .. _sphx_glr_download_api_gallery_interest_rate_models_short_rate_plot_04_vasicek.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/interest_rate_models/short_rate/plot_04_vasicek.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_vasicek.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_vasicek.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_vasicek.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_