<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.04774307, 0.04370215, 0.0450328 , 0.04647857, 0.05284639,
       0.04907481, 0.04852994, 0.05373958, 0.04539454, 0.05360737,
       0.05178488, 0.05137988, 0.0492705 , 0.05375749, 0.05112782,
       0.05417595, 0.04755101, 0.04988012, 0.05199328, 0.05292985])
```

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.04135491, 0.03210918, 0.03372616, 0.03784991, 0.0610532 ,
       0.0443272 , 0.04252063, 0.06593451, 0.03451906, 0.06469335,
       0.05418982, 0.05209335, 0.04535122, 0.06568127, 0.05104022,
       0.06894491, 0.0404655 , 0.04667832, 0.05627135, 0.06119597])
```

<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.921041997950763</td>
      <td>18.995841455293334</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.661 seconds)

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