.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/credit/portfolio_loss/plot_01_vasicek_lhp.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_credit_portfolio_loss_plot_01_vasicek_lhp.py: Vasicek's large homogeneous portfolio ===================================== Vasicek (1987) let every loan default when its asset return :math:`\sqrt\rho M + \sqrt{1-\rho}\,\varepsilon_i` falls below :math:`N^{-1}(p)`. Given the common factor :math:`M`, defaults are independent, so in a large pool the default rate is the deterministic :math:`p(M)`, and its distribution is known in closed form. Correlation does not change the expected loss, only how it is spread out: at high correlation most years are quiet and a few are disasters. .. GENERATED FROM PYTHON SOURCE LINES 15-30 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.credit import simulate_one_factor_defaults, vasicek_cdf, vasicek_pdf, vasicek_quantile p = 0.02 x = np.linspace(1e-4, 0.12, 400) fig, ax = plt.subplots(figsize=(7, 4)) for rho in (0.05, 0.12, 0.24): ax.plot(x, vasicek_pdf(x, p, rho), label=f"rho = {rho}, 99.9% quantile {vasicek_quantile(0.999, p, rho):.1%}") ax.axvline(p, color="k", ls=":", label="mean default rate p") ax.set_xlabel("portfolio default rate") ax.set_ylabel("density") ax.legend() .. image-sg:: /api/gallery/credit/portfolio_loss/images/sphx_glr_plot_01_vasicek_lhp_001.png :alt: plot 01 vasicek lhp :srcset: /api/gallery/credit/portfolio_loss/images/sphx_glr_plot_01_vasicek_lhp_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 31-33 Finite pools converge to the limit ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-46 .. code-block:: Python rho = 0.12 fig, ax = plt.subplots(figsize=(7, 4)) for n in (50, 500, 5000): frac = np.sort(simulate_one_factor_defaults(p, rho, n, 20000, rng=0) / n) ax.step(frac, np.arange(1, frac.size + 1) / frac.size, where="post", label=f"{n} loans (simulated)") ax.plot(x, vasicek_cdf(x, p, rho), "k", lw=2, label="Vasicek limit") ax.set_xlim(0, 0.08) ax.set_xlabel("portfolio default rate") ax.set_ylabel("cumulative probability") ax.legend() plt.show() .. image-sg:: /api/gallery/credit/portfolio_loss/images/sphx_glr_plot_01_vasicek_lhp_002.png :alt: plot 01 vasicek lhp :srcset: /api/gallery/credit/portfolio_loss/images/sphx_glr_plot_01_vasicek_lhp_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.089 seconds) .. _sphx_glr_download_api_gallery_credit_portfolio_loss_plot_01_vasicek_lhp.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/credit/portfolio_loss/plot_01_vasicek_lhp.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_vasicek_lhp.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_vasicek_lhp.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_vasicek_lhp.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_