.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/risk/copulas/plot_01_sklar_gaussian_copula.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_risk_copulas_plot_01_sklar_gaussian_copula.py: Sklar's theorem and the Gaussian copula ======================================= Sklar (1959) showed that every joint distribution is its marginals glued together by a copula :math:`C`: .. math:: F(x_1, x_2) = C\big(F_1(x_1), F_2(x_2)\big). Risk managers can therefore model each asset's return on its own and choose the dependence separately. Here a Gaussian copula with correlation 0.7 joins a fat-tailed Student-t equity return to a skewed credit loss, and the copula is recovered from the joint sample by ranks alone. .. GENERATED FROM PYTHON SOURCE LINES 18-28 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import stats from financialkit.risk import apply_marginals, copula_kendall_tau, gaussian_copula_sample, pseudo_observations from financialkit.risk.visualizers import plot_copula corr = np.array([[1.0, 0.7], [0.7, 1.0]]) u = gaussian_copula_sample(corr, 3000, rng=0) .. GENERATED FROM PYTHON SOURCE LINES 29-31 One copula, any marginals ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-45 .. code-block:: Python equity = stats.t(4, scale=0.01) credit = stats.lognorm(0.8, scale=0.005) x = apply_marginals(u, [equity.ppf, credit.ppf]) fig, axes = plt.subplots(1, 3, figsize=(13, 4)) plot_copula(u, ax=axes[0], title="Gaussian copula, rho = 0.7") axes[1].scatter(x[:, 0], x[:, 1], s=2, alpha=0.4) axes[1].set_xlabel("equity return (t, 4 df)") axes[1].set_ylabel("credit loss (lognormal)") axes[1].set_title("joint distribution") plot_copula(pseudo_observations(x), ax=axes[2], title="copula recovered from ranks") fig.tight_layout() .. image-sg:: /api/gallery/risk/copulas/images/sphx_glr_plot_01_sklar_gaussian_copula_001.png :alt: Gaussian copula, rho = 0.7, joint distribution, copula recovered from ranks :srcset: /api/gallery/risk/copulas/images/sphx_glr_plot_01_sklar_gaussian_copula_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-51 Rank correlation is a property of the copula -------------------------------------------- Pearson correlation changes with the marginals; Kendall's tau does not, and for the Gaussian copula it equals :math:`\tfrac{2}{\pi}\arcsin\rho`. .. GENERATED FROM PYTHON SOURCE LINES 51-57 .. code-block:: Python print(f"Pearson, uniforms {np.corrcoef(u.T)[0, 1]:.3f} vs Pearson, t/lognormal {np.corrcoef(x.T)[0, 1]:.3f}") print(f"Kendall, uniforms {stats.kendalltau(*u.T).statistic:.3f} vs Kendall, t/lognormal {stats.kendalltau(*x.T).statistic:.3f}") print(f"theory 2/pi arcsin(0.7) = {copula_kendall_tau(0.7):.3f}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none Pearson, uniforms 0.677 vs Pearson, t/lognormal 0.570 Kendall, uniforms 0.486 vs Kendall, t/lognormal 0.486 theory 2/pi arcsin(0.7) = 0.494 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.082 seconds) .. _sphx_glr_download_api_gallery_risk_copulas_plot_01_sklar_gaussian_copula.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/risk/copulas/plot_01_sklar_gaussian_copula.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_sklar_gaussian_copula.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_sklar_gaussian_copula.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_sklar_gaussian_copula.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_