.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/portfolio/risk_based/plot_02_most_diversified.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_portfolio_risk_based_plot_02_most_diversified.py: Choueifaty's most-diversified portfolio ======================================= Yves Choueifaty and Yves Coignard (2008) defined the diversification ratio of a long-only portfolio as its weighted average volatility over its actual volatility, .. math:: D(w) = \frac{w'\sigma}{\sqrt{w'\Sigma w}}, which equals one for a single asset and :math:`\sqrt{d}` for :math:`d` uncorrelated, equally risky assets held equally. The portfolio that maximizes it has a neat property: every asset it holds has the same correlation with it, and every asset it leaves out has a higher one. .. GENERATED FROM PYTHON SOURCE LINES 20-42 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.portfolio import diversification_ratio, inverse_volatility_portfolio, min_variance_portfolio, most_diversified_portfolio from financialkit.portfolio.visualizers import plot_weights names = ["govt", "IG credit", "HY credit", "US eq", "EU eq", "EM eq", "gold", "REITs"] vol = np.array([0.05, 0.06, 0.10, 0.16, 0.18, 0.22, 0.16, 0.20]) corr = np.array( [ [1.0, 0.7, 0.1, -0.2, -0.2, -0.1, 0.2, 0.1], [0.7, 1.0, 0.5, 0.2, 0.2, 0.3, 0.2, 0.3], [0.1, 0.5, 1.0, 0.6, 0.6, 0.6, 0.1, 0.6], [-0.2, 0.2, 0.6, 1.0, 0.85, 0.75, 0.0, 0.7], [-0.2, 0.2, 0.6, 0.85, 1.0, 0.8, 0.05, 0.65], [-0.1, 0.3, 0.6, 0.75, 0.8, 1.0, 0.15, 0.6], [0.2, 0.2, 0.1, 0.0, 0.05, 0.15, 1.0, 0.1], [0.1, 0.3, 0.6, 0.7, 0.65, 0.6, 0.1, 1.0], ] ) cov = corr * np.outer(vol, vol) .. GENERATED FROM PYTHON SOURCE LINES 43-45 Three risk-based portfolios --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 45-56 .. code-block:: Python ports = { "minimum variance": min_variance_portfolio(cov, long_only=True), "inverse volatility": inverse_volatility_portfolio(cov), "most diversified": most_diversified_portfolio(cov), } fig, axes = plt.subplots(3, 1, figsize=(7, 7), sharex=True) for ax, (label, p) in zip(axes, ports.items(), strict=True): plot_weights(p.weights, names, ax=ax).set_title(f"{label}: D = {diversification_ratio(p.weights, cov):.2f}, vol {p.volatility:.2%}") fig.tight_layout() .. image-sg:: /api/gallery/portfolio/risk_based/images/sphx_glr_plot_02_most_diversified_001.png :alt: minimum variance: D = 1.53, vol 4.40%, inverse volatility: D = 1.54, vol 6.98%, most diversified: D = 1.78, vol 5.12% :srcset: /api/gallery/portfolio/risk_based/images/sphx_glr_plot_02_most_diversified_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 57-59 The core property ----------------- .. GENERATED FROM PYTHON SOURCE LINES 59-68 .. code-block:: Python mdp = ports["most diversified"] rho = (cov @ mdp.weights) / (vol * mdp.volatility) fig, ax = plt.subplots(figsize=(7, 3)) ax.bar(names, rho, color=["C0" if w > 1e-6 else "0.7" for w in mdp.weights]) ax.set_ylabel("correlation with the MDP") ax.set_title("held assets (blue) share one correlation; excluded ones (grey) exceed it") fig.tight_layout() plt.show() .. image-sg:: /api/gallery/portfolio/risk_based/images/sphx_glr_plot_02_most_diversified_002.png :alt: held assets (blue) share one correlation; excluded ones (grey) exceed it :srcset: /api/gallery/portfolio/risk_based/images/sphx_glr_plot_02_most_diversified_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.123 seconds) .. _sphx_glr_download_api_gallery_portfolio_risk_based_plot_02_most_diversified.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/portfolio/risk_based/plot_02_most_diversified.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_most_diversified.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_most_diversified.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_most_diversified.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_