.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/credit/migration/plot_02_generator_matrix.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_02_generator_matrix.py: Jarrow, Lando and Turnbull: generator matrices ============================================== Jarrow, Lando and Turnbull (1997) put rating migration in continuous time. A generator :math:`\Lambda`, with non-negative off-diagonal intensities and rows summing to zero, gives the transition matrix for every horizon, :math:`P(t) = e^{\Lambda t}`. Default probabilities by rating, and risky bond prices with recovery of treasury, follow for any maturity, including fractions of a year that an annual matrix cannot provide. .. GENERATED FROM PYTHON SOURCE LINES 15-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.credit import RATINGS, creditmetrics_matrix, generator_from_transition, jlt_risky_zero, regularize_generator, transition_from_generator from financialkit.credit.visualizers import plot_transition_matrix G = regularize_generator(generator_from_transition(creditmetrics_matrix())) plot_transition_matrix(G, labels=RATINGS, fmt="{:.1f}") plt.title("generator (% per year)") .. image-sg:: /api/gallery/credit/migration/images/sphx_glr_plot_02_generator_matrix_001.png :alt: generator (% per year) :srcset: /api/gallery/credit/migration/images/sphx_glr_plot_02_generator_matrix_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'generator (% per year)') .. GENERATED FROM PYTHON SOURCE LINES 26-28 The term structure of default probabilities ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-38 .. code-block:: Python t = np.linspace(0.0, 10.0, 81) Pt = transition_from_generator(G, t) fig, ax = plt.subplots(figsize=(7, 4)) for i in range(7): ax.semilogy(t[1:], Pt[1:, i, -1], label=RATINGS[i]) ax.set_xlabel("years") ax.set_ylabel("cumulative default probability") ax.legend(ncol=2) .. image-sg:: /api/gallery/credit/migration/images/sphx_glr_plot_02_generator_matrix_002.png :alt: plot 02 generator matrix :srcset: /api/gallery/credit/migration/images/sphx_glr_plot_02_generator_matrix_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 39-41 exp(G t) interpolates the annual matrix powers ---------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-48 .. code-block:: Python P1 = transition_from_generator(G, 1.0) years = np.arange(1, 11) powers = [np.linalg.matrix_power(P1, n)[3, -1] for n in years] ax.plot(years, powers, "kx", label="P(1)^n, BBB") ax.legend(ncol=2) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 49-51 Risky zero-coupon spreads by rating ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-63 .. code-block:: Python T = np.linspace(0.25, 10, 40) fig, ax = plt.subplots(figsize=(7, 4)) for i in range(1, 7): P = jlt_risky_zero(T, 0.03, G, i, recovery=0.4) ax.plot(T, 1e4 * (-np.log(P) / T - 0.03), label=RATINGS[i]) ax.set_xlabel("maturity") ax.set_ylabel("spread (bp)") ax.set_yscale("log") ax.legend() plt.show() .. image-sg:: /api/gallery/credit/migration/images/sphx_glr_plot_02_generator_matrix_003.png :alt: plot 02 generator matrix :srcset: /api/gallery/credit/migration/images/sphx_glr_plot_02_generator_matrix_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.138 seconds) .. _sphx_glr_download_api_gallery_credit_migration_plot_02_generator_matrix.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_02_generator_matrix.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_generator_matrix.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_generator_matrix.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_generator_matrix.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_