.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/credit/migration/plot_03_israel_rosenthal_wei.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_migration_plot_03_israel_rosenthal_wei.py: Israel, Rosenthal and Wei: finding a valid generator ==================================================== Going from an annual transition matrix :math:`P` to a generator is not as simple as :math:`\log P`. Israel, Rosenthal and Wei (2001) showed that for empirical rating matrices the principal logarithm usually has negative off-diagonal entries, which would be negative migration intensities. Their fix: set those entries to zero and absorb the change in the diagonal. A weighted variant spreads it over the row instead. Both leave :math:`e^{G}` within a few basis points of :math:`P`. .. GENERATED FROM PYTHON SOURCE LINES 15-28 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.credit import RATINGS, creditmetrics_matrix, generator_from_transition, is_valid_generator, regularize_generator, transition_from_generator from financialkit.credit.visualizers import plot_transition_matrix P = creditmetrics_matrix() L = generator_from_transition(P) off = L - np.diag(np.diag(L)) print(f"log(P) is a valid generator: {is_valid_generator(L)}; {np.sum(off < 0)} negative off-diagonal entries, most negative {off.min():.2e}") ax = plot_transition_matrix(L, labels=RATINGS, fmt="{:.2f}") ax.set_title("log(P) in % per year: negative intensities in red") .. image-sg:: /api/gallery/credit/migration/images/sphx_glr_plot_03_israel_rosenthal_wei_001.png :alt: log(P) in % per year: negative intensities in red :srcset: /api/gallery/credit/migration/images/sphx_glr_plot_03_israel_rosenthal_wei_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none log(P) is a valid generator: False; 7 negative off-diagonal entries, most negative -3.10e-04 Text(0.5, 1.0, 'log(P) in % per year: negative intensities in red') .. GENERATED FROM PYTHON SOURCE LINES 29-31 The two adjustments ------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-42 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(12, 4.5)) for ax, method in zip(axes, ("diagonal", "weighted"), strict=True): G = regularize_generator(L, method) err = transition_from_generator(G) - P plot_transition_matrix(err, labels=RATINGS, ax=ax, fmt="{:.2f}", scale=1e4) ax.set_title(f"{method}: exp(G) - P in bp (max {1e4 * np.abs(err).max():.1f})") print(f"{method:8s}: valid {is_valid_generator(G)}, max |exp(G) - P| = {np.abs(err).max():.2e}") fig.tight_layout() plt.show() .. image-sg:: /api/gallery/credit/migration/images/sphx_glr_plot_03_israel_rosenthal_wei_002.png :alt: diagonal: exp(G) - P in bp (max 2.4), weighted: exp(G) - P in bp (max 2.4) :srcset: /api/gallery/credit/migration/images/sphx_glr_plot_03_israel_rosenthal_wei_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none diagonal: valid True, max |exp(G) - P| = 2.39e-04 weighted: valid True, max |exp(G) - P| = 2.39e-04 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.191 seconds) .. _sphx_glr_download_api_gallery_credit_migration_plot_03_israel_rosenthal_wei.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/migration/plot_03_israel_rosenthal_wei.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_israel_rosenthal_wei.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_israel_rosenthal_wei.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_israel_rosenthal_wei.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_