#### NOTE
[Go to the end](#sphx-glr-download-auto-examples-risk-budgeting-plot-3-risk-parity-ledoit-wolf-py)
to download the full example code or to run this example in your browser via JupyterLite.

<a id="sphx-glr-auto-examples-risk-budgeting-plot-3-risk-parity-ledoit-wolf-py"></a>

<a id="risk-parity-covariance-shrinkage"></a>

# Risk Parity - Covariance shrinkage

This tutorial shows how to incorporate covariance shrinkage in the
[`RiskBudgeting`](https://skfolio.org/generated/skfolio.optimization.RiskBudgeting.html.md#skfolio.optimization.RiskBudgeting) optimization.

<a id="data"></a>

## Data

We load the S&P 500 [dataset](https://skfolio.org/user_guide/datasets.html.md#datasets) composed of the daily prices of 20
assets from the S&P 500 Index composition starting from 1990-01-02 up to 2022-12-28:

```Python
from plotly.io import show
from sklearn.model_selection import train_test_split

from skfolio import Population, RiskMeasure
from skfolio.datasets import load_sp500_dataset
from skfolio.moments import ShrunkCovariance
from skfolio.optimization import RiskBudgeting
from skfolio.preprocessing import prices_to_returns
from skfolio.prior import EmpiricalPrior

prices = load_sp500_dataset()

X = prices_to_returns(prices)
X_train, X_test = train_test_split(X, test_size=0.33, shuffle=False)
```

<a id="model"></a>

## Model

We create a risk parity model by using [`ShrunkCovariance`](https://skfolio.org/generated/skfolio.moments.ShrunkCovariance.html.md#skfolio.moments.ShrunkCovariance) as
the covariance estimator then fit it on the training set:

```Python
model = RiskBudgeting(
    risk_measure=RiskMeasure.VARIANCE,
    prior_estimator=EmpiricalPrior(
        covariance_estimator=ShrunkCovariance(shrinkage=0.9)
    ),
    portfolio_params=dict(name="Risk Parity - Covariance Shrinkage"),
)
model.fit(X_train)
model.weights_
```

```none
array([0.04774404, 0.04370342, 0.04503321, 0.04647533, 0.05284521,
       0.04907493, 0.04852809, 0.05373997, 0.04539475, 0.05360793,
       0.05178586, 0.0513793 , 0.04927117, 0.05375824, 0.05112857,
       0.05417612, 0.0475513 , 0.04988039, 0.05199224, 0.05292994])
```

To compare this model, we use a basic risk parity without covariance shrinkage:

```Python
bench = RiskBudgeting(
    risk_measure=RiskMeasure.VARIANCE,
    portfolio_params=dict(name="Risk Parity - Basic"),
)
bench.fit(X_train)
bench.weights_
```

```none
array([0.04135318, 0.03210824, 0.0337272 , 0.03785076, 0.06105329,
       0.04432756, 0.04252214, 0.06593567, 0.03451845, 0.06469326,
       0.05418886, 0.05209518, 0.04535298, 0.0656811 , 0.0510388 ,
       0.06894497, 0.04046537, 0.04667777, 0.05626949, 0.06119573])
```

<a id="prediction"></a>

## Prediction

We predict the model and the benchmark on the test set:

```Python
ptf_model_test = model.predict(X_test)
ptf_bench_test = bench.predict(X_test)
```

<a id="analysis"></a>

## Analysis

For improved analysis, it’s possible to load both predicted portfolios into a
[`Population`](https://skfolio.org/generated/skfolio.population.Population.html.md#skfolio.population.Population):

```Python
population = Population([ptf_model_test, ptf_bench_test])
```

Let’s plot each portfolio cumulative returns:

```Python
fig = population.plot_cumulative_returns()
show(fig)
```

[plotly figure stripped from llms output]<style>html[data-theme="dark"] div.output_subarea:has(.plotly-graph-div){background:#fff;border-radius:0.25rem;padding:0.5rem}@media (prefers-color-scheme: dark){html:not([data-theme="light"]) div.output_subarea:has(.plotly-graph-div){background:#fff;border-radius:0.25rem;padding:0.5rem}}</style><script>if (!window.plotlySphinxGalleryResize) {window.plotlySphinxGalleryResize = true;window.addEventListener("load", function () {document.querySelectorAll(".plotly-graph-div").forEach(function (gd) { Plotly.Plots.resize(gd); });});}</script>

<br/>

Finally, we print a full summary of both strategies evaluated on the test set:

```Python
population.summary()
```

<div class="output_subarea output_html rendered_html output_result">
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Risk Parity - Covariance Shrinkage</th>
      <th>Risk Parity - Basic</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Mean</th>
      <td>0.068%</td>
      <td>0.065%</td>
    </tr>
    <tr>
      <th>Annualized Mean</th>
      <td>17.04%</td>
      <td>16.40%</td>
    </tr>
    <tr>
      <th>Variance</th>
      <td>0.00011</td>
      <td>0.00010</td>
    </tr>
    <tr>
      <th>Annualized Variance</th>
      <td>2.84%</td>
      <td>2.63%</td>
    </tr>
    <tr>
      <th>Semi-Variance</th>
      <td>0.000058</td>
      <td>0.000054</td>
    </tr>
    <tr>
      <th>Annualized Semi-Variance</th>
      <td>1.47%</td>
      <td>1.36%</td>
    </tr>
    <tr>
      <th>Standard Deviation</th>
      <td>1.06%</td>
      <td>1.02%</td>
    </tr>
    <tr>
      <th>Annualized Standard Deviation</th>
      <td>16.86%</td>
      <td>16.22%</td>
    </tr>
    <tr>
      <th>Semi-Deviation</th>
      <td>0.76%</td>
      <td>0.73%</td>
    </tr>
    <tr>
      <th>Annualized Semi-Deviation</th>
      <td>12.11%</td>
      <td>11.66%</td>
    </tr>
    <tr>
      <th>Mean Absolute Deviation</th>
      <td>0.69%</td>
      <td>0.66%</td>
    </tr>
    <tr>
      <th>CVaR at 95%</th>
      <td>2.46%</td>
      <td>2.37%</td>
    </tr>
    <tr>
      <th>EVaR at 95%</th>
      <td>5.37%</td>
      <td>5.23%</td>
    </tr>
    <tr>
      <th>Worst Realization</th>
      <td>10.66%</td>
      <td>10.34%</td>
    </tr>
    <tr>
      <th>CDaR at 95%</th>
      <td>13.02%</td>
      <td>12.39%</td>
    </tr>
    <tr>
      <th>MAX Drawdown</th>
      <td>34.58%</td>
      <td>34.30%</td>
    </tr>
    <tr>
      <th>Average Drawdown</th>
      <td>2.58%</td>
      <td>2.40%</td>
    </tr>
    <tr>
      <th>EDaR at 95%</th>
      <td>20.30%</td>
      <td>19.86%</td>
    </tr>
    <tr>
      <th>First Lower Partial Moment</th>
      <td>0.35%</td>
      <td>0.33%</td>
    </tr>
    <tr>
      <th>Ulcer Index</th>
      <td>0.043</td>
      <td>0.041</td>
    </tr>
    <tr>
      <th>Gini Mean Difference</th>
      <td>1.04%</td>
      <td>1.00%</td>
    </tr>
    <tr>
      <th>Value at Risk at 95%</th>
      <td>1.51%</td>
      <td>1.46%</td>
    </tr>
    <tr>
      <th>Drawdown at Risk at 95%</th>
      <td>9.30%</td>
      <td>8.97%</td>
    </tr>
    <tr>
      <th>Entropic Risk Measure at 95%</th>
      <td>3.00</td>
      <td>3.00</td>
    </tr>
    <tr>
      <th>Fourth Central Moment</th>
      <td>0.000025%</td>
      <td>0.000023%</td>
    </tr>
    <tr>
      <th>Fourth Lower Partial Moment</th>
      <td>0.000012%</td>
      <td>0.000011%</td>
    </tr>
    <tr>
      <th>Skew</th>
      <td>-3.68%</td>
      <td>-5.68%</td>
    </tr>
    <tr>
      <th>Kurtosis</th>
      <td>1998.27%</td>
      <td>2067.02%</td>
    </tr>
    <tr>
      <th>Sharpe Ratio</th>
      <td>0.064</td>
      <td>0.064</td>
    </tr>
    <tr>
      <th>Annualized Sharpe Ratio</th>
      <td>1.01</td>
      <td>1.01</td>
    </tr>
    <tr>
      <th>Sortino Ratio</th>
      <td>0.089</td>
      <td>0.089</td>
    </tr>
    <tr>
      <th>Annualized Sortino Ratio</th>
      <td>1.41</td>
      <td>1.41</td>
    </tr>
    <tr>
      <th>Mean Absolute Deviation Ratio</th>
      <td>0.097</td>
      <td>0.098</td>
    </tr>
    <tr>
      <th>First Lower Partial Moment Ratio</th>
      <td>0.19</td>
      <td>0.20</td>
    </tr>
    <tr>
      <th>Value at Risk Ratio at 95%</th>
      <td>0.045</td>
      <td>0.045</td>
    </tr>
    <tr>
      <th>CVaR Ratio at 95%</th>
      <td>0.027</td>
      <td>0.027</td>
    </tr>
    <tr>
      <th>Entropic Risk Measure Ratio at 95%</th>
      <td>0.00023</td>
      <td>0.00022</td>
    </tr>
    <tr>
      <th>EVaR Ratio at 95%</th>
      <td>0.013</td>
      <td>0.012</td>
    </tr>
    <tr>
      <th>Worst Realization Ratio</th>
      <td>0.0063</td>
      <td>0.0063</td>
    </tr>
    <tr>
      <th>Drawdown at Risk Ratio at 95%</th>
      <td>0.0073</td>
      <td>0.0073</td>
    </tr>
    <tr>
      <th>CDaR Ratio at 95%</th>
      <td>0.0052</td>
      <td>0.0053</td>
    </tr>
    <tr>
      <th>Calmar Ratio</th>
      <td>0.0020</td>
      <td>0.0019</td>
    </tr>
    <tr>
      <th>Average Drawdown Ratio</th>
      <td>0.026</td>
      <td>0.027</td>
    </tr>
    <tr>
      <th>EDaR Ratio at 95%</th>
      <td>0.0033</td>
      <td>0.0033</td>
    </tr>
    <tr>
      <th>Ulcer Index Ratio</th>
      <td>0.016</td>
      <td>0.016</td>
    </tr>
    <tr>
      <th>Gini Mean Difference Ratio</th>
      <td>0.065</td>
      <td>0.065</td>
    </tr>
    <tr>
      <th>Effective Number of Assets</th>
      <td>19.921039413395736</td>
      <td>18.9958444521665</td>
    </tr>
    <tr>
      <th>Assets Number</th>
      <td>20</td>
      <td>20</td>
    </tr>
  </tbody>
</table>
</div>
</div>
<br />
<br />

**Total running time of the script:** (0 minutes 0.665 seconds)

<a id="sphx-glr-download-auto-examples-risk-budgeting-plot-3-risk-parity-ledoit-wolf-py"></a>
[![Launch JupyterLite](auto_examples/risk_budgeting/images/jupyterlite_badge_logo.svg)](../../lite/lab/index.html?path=auto_examples/risk_budgeting/plot_3_risk_parity_ledoit_wolf.ipynb)

[`Download Jupyter notebook: plot_3_risk_parity_ledoit_wolf.ipynb`](https://skfolio.org/auto_examples/risk_budgeting/_downloads/746ce70ca7a977777ffdd7b87f0461fd/plot_3_risk_parity_ledoit_wolf.ipynb)

[`Download Python source code: plot_3_risk_parity_ledoit_wolf.py`](https://skfolio.org/auto_examples/risk_budgeting/_downloads/45e8717f871fb406c62266ae0664b6c6/plot_3_risk_parity_ledoit_wolf.py)

[`Download zipped: plot_3_risk_parity_ledoit_wolf.zip`](https://skfolio.org/auto_examples/risk_budgeting/_downloads/660c9fcd68961f87309951468e2ed12c/plot_3_risk_parity_ledoit_wolf.zip)

[Gallery generated by Sphinx-Gallery](https://sphinx-gallery.github.io)
