.. 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_02_creditrisk_plus.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_02_creditrisk_plus.py: CreditRisk+ and Panjer's recursion ================================== Credit Suisse First Boston's CreditRisk+ (1997) treated defaults like insurance claims: Poisson events with rates driven by a gamma-distributed sector factor, and exposures rounded to multiples of a loss unit. The default count is then negative binomial, and Panjer's (1981) recursion produces the whole loss distribution exactly, without simulation. The factor's volatility :math:`\sigma` fattens the tail without changing the expected loss. .. GENERATED FROM PYTHON SOURCE LINES 15-35 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.credit import creditrisk_plus from financialkit.credit.visualizers import plot_loss_distribution rng = np.random.default_rng(0) exposures = np.round(rng.lognormal(np.log(2e5), 0.9, 300), -4) pds = np.clip(rng.lognormal(np.log(0.01), 0.8, 300), 1e-4, 0.2) unit = 50_000.0 fig, ax = plt.subplots(1, 2, figsize=(12, 4)) for a, sigma in zip(ax, (0.0, 1.0), strict=True): dist = creditrisk_plus(exposures, pds, sigma=sigma, unit=unit) plot_loss_distribution(dist, alpha=0.999, ax=a, log=True) a.set_title(f"sector volatility sigma = {sigma}") a.set_xlim(0, 2.5e7) a.set_ylim(1e-8, 1) print(f"sigma = {sigma}: EL {dist.mean:,.0f}, sd {dist.std:,.0f}, 99.9% UL {dist.unexpected_loss(0.999):,.0f}") .. image-sg:: /api/gallery/credit/portfolio_loss/images/sphx_glr_plot_02_creditrisk_plus_001.png :alt: sector volatility sigma = 0.0, sector volatility sigma = 1.0 :srcset: /api/gallery/credit/portfolio_loss/images/sphx_glr_plot_02_creditrisk_plus_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none sigma = 0.0: EL 1,168,615, sd 892,123, 99.9% UL 4,581,385 sigma = 1.0: EL 1,168,614, sd 1,470,212, 99.9% UL 8,981,386 .. GENERATED FROM PYTHON SOURCE LINES 36-38 Tail probabilities ------------------ .. GENERATED FROM PYTHON SOURCE LINES 38-50 .. code-block:: Python fig, ax = plt.subplots(figsize=(7, 4)) for sigma in (0.0, 0.5, 1.0, 1.5): d = creditrisk_plus(exposures, pds, sigma=sigma, unit=unit) ax.semilogy(d.losses, 1 - np.cumsum(d.pmf), label=f"sigma = {sigma}") ax.set_xlim(0, 3e7) ax.set_ylim(1e-6, 1) ax.set_xlabel("loss") ax.set_ylabel("P(loss > x)") ax.legend() plt.show() .. image-sg:: /api/gallery/credit/portfolio_loss/images/sphx_glr_plot_02_creditrisk_plus_002.png :alt: plot 02 creditrisk plus :srcset: /api/gallery/credit/portfolio_loss/images/sphx_glr_plot_02_creditrisk_plus_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.318 seconds) .. _sphx_glr_download_api_gallery_credit_portfolio_loss_plot_02_creditrisk_plus.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_02_creditrisk_plus.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_creditrisk_plus.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_creditrisk_plus.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_creditrisk_plus.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_