.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/credit/structural/plot_02_black_cox.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_structural_plot_02_black_cox.py: Black and Cox: default at the first passage =========================================== Black and Cox (1976) let safety covenants force default the first time the asset value touches a barrier, not only at maturity. The reflection principle gives the first-passage probability in closed form. Here it is checked against simulated asset paths (with a Brownian-bridge correction for crossings between grid points) and compared with Merton's maturity-only default probability. .. GENERATED FROM PYTHON SOURCE LINES 14-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import stats from financialkit.credit import black_cox_default_probability, first_passage_crossing_probability from financialkit.integrators import gbm_paths V0, K, mu, sigma, T = 100.0, 70.0, 0.05, 0.25, 5.0 t, paths = gbm_paths(V0, mu, sigma, T=T, n_steps=100, n_paths=4000, rng=0) .. GENERATED FROM PYTHON SOURCE LINES 25-27 Paths that touch the barrier ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-38 .. code-block:: Python hit_grid = (paths <= K).any(axis=1) fig, ax = plt.subplots(figsize=(7, 4)) for i in range(60): ax.plot(t, paths[i], color="C3" if hit_grid[i] else "C0", lw=0.6, alpha=0.7) ax.axhline(K, color="k", lw=2, label="barrier K") ax.set_xlabel("years") ax.set_ylabel("asset value") ax.set_title("red: defaulted before maturity") ax.legend() .. image-sg:: /api/gallery/credit/structural/images/sphx_glr_plot_02_black_cox_001.png :alt: red: defaulted before maturity :srcset: /api/gallery/credit/structural/images/sphx_glr_plot_02_black_cox_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 39-41 First passage versus Merton --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-57 .. code-block:: Python horizons = np.linspace(0.1, T, 50) bc = black_cox_default_probability(V0, K, horizons, mu, sigma) merton = stats.norm.cdf((np.log(K / V0) - (mu - 0.5 * sigma**2) * horizons) / (sigma * np.sqrt(horizons))) p_bridge = first_passage_crossing_probability(paths, K, sigma, T / 100) fig, ax = plt.subplots(figsize=(7, 4)) ax.plot(horizons, bc, lw=2, label="Black-Cox first passage") ax.plot(horizons, merton, lw=2, ls="--", label="Merton: below K at the horizon") ax.errorbar(T, p_bridge.mean(), yerr=4 * p_bridge.std() / np.sqrt(p_bridge.size), fmt="o", color="k", label="Monte Carlo, bridge-corrected") ax.plot(T, hit_grid.mean(), "x", color="C3", ms=8, label="Monte Carlo, grid only (biased low)") ax.set_xlabel("horizon (years)") ax.set_ylabel("default probability") ax.legend() plt.show() .. image-sg:: /api/gallery/credit/structural/images/sphx_glr_plot_02_black_cox_002.png :alt: plot 02 black cox :srcset: /api/gallery/credit/structural/images/sphx_glr_plot_02_black_cox_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.080 seconds) .. _sphx_glr_download_api_gallery_credit_structural_plot_02_black_cox.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/structural/plot_02_black_cox.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_black_cox.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_black_cox.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_black_cox.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_