.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fixed_income/curves/plot_05_litterman_scheinkman_pca.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_fixed_income_curves_plot_05_litterman_scheinkman_pca.py: Litterman-Scheinkman: level, slope, curvature ============================================= Litterman and Scheinkman (1991) ran a principal component analysis of Treasury returns and found that three factors explain almost all of the variation. They named them level, steepness (slope) and curvature. The script repeats the analysis on synthetic daily yield changes and shows the three loadings and their shares of variance. .. GENERATED FROM PYTHON SOURCE LINES 13-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.fixed_income import simulate_yield_changes, yield_curve_pca from financialkit.fixed_income.visualizers import plot_pca_loadings tenors = np.array([0.25, 0.5, 1, 2, 3, 5, 7, 10, 20, 30]) changes = simulate_yield_changes(tenors, n_obs=2500, rng=1991) pca = yield_curve_pca(changes, tenors) .. GENERATED FROM PYTHON SOURCE LINES 24-26 Loadings of the first three components -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-29 .. code-block:: Python plot_pca_loadings(pca) .. image-sg:: /api/gallery/fixed_income/curves/images/sphx_glr_plot_05_litterman_scheinkman_pca_001.png :alt: plot 05 litterman scheinkman pca :srcset: /api/gallery/fixed_income/curves/images/sphx_glr_plot_05_litterman_scheinkman_pca_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 30-32 Share of variance explained --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-41 .. code-block:: Python fig, ax = plt.subplots() ax.bar(np.arange(1, tenors.size + 1), 100 * pca.explained_variance_ratio) ax.plot(np.arange(1, tenors.size + 1), 100 * np.cumsum(pca.explained_variance_ratio), "ko-", label="cumulative") ax.set_xlabel("component") ax.set_ylabel("variance explained (%)") ax.legend() print("first three components explain", f"{100 * pca.explained_variance_ratio[:3].sum():.1f}%") plt.show() .. image-sg:: /api/gallery/fixed_income/curves/images/sphx_glr_plot_05_litterman_scheinkman_pca_002.png :alt: plot 05 litterman scheinkman pca :srcset: /api/gallery/fixed_income/curves/images/sphx_glr_plot_05_litterman_scheinkman_pca_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none first three components explain 99.7% .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.047 seconds) .. _sphx_glr_download_api_gallery_fixed_income_curves_plot_05_litterman_scheinkman_pca.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/fixed_income/curves/plot_05_litterman_scheinkman_pca.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_litterman_scheinkman_pca.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_litterman_scheinkman_pca.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_litterman_scheinkman_pca.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_