<a id="sphx-glr-auto-examples-risk-budgeting-plot-1-risk-parity-variance-py"></a>

<a id="risk-parity-variance"></a>

# Risk Parity - Variance

This tutorial uses the [`RiskBudgeting`](https://skfolio.org/generated/skfolio.optimization.RiskBudgeting.html.md#skfolio.optimization.RiskBudgeting) optimization to
find the risk parity portfolio with variance as the risk measure.

<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.optimization import InverseVolatility, RiskBudgeting
from skfolio.preprocessing import prices_to_returns

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 the risk parity model and then fit it on the training set:

```Python
model = RiskBudgeting(
    risk_measure=RiskMeasure.VARIANCE,
    portfolio_params=dict(name="Risk Parity - Variance"),
)
model.fit(X_train)
model.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])
```

To compare this model, we use an inverse volatility benchmark using
the [`InverseVolatility`](https://skfolio.org/generated/skfolio.optimization.InverseVolatility.html.md#skfolio.optimization.InverseVolatility) estimator.

```Python
bench = InverseVolatility(portfolio_params=dict(name="Inverse Vol"))
bench.fit(X_train)
bench.weights_
```

```none
array([0.03306735, 0.02548697, 0.03551377, 0.0296872 , 0.06358463,
       0.05434705, 0.04742354, 0.07049715, 0.03882539, 0.06697905,
       0.05570808, 0.05576851, 0.04723274, 0.06351213, 0.05581397,
       0.0676481 , 0.02564642, 0.03970752, 0.05744543, 0.06610498])
```

<a id="risk-contribution-analysis"></a>

## Risk Contribution Analysis

Let’s analyze the risk contribution of both models on the training set.
As expected, the risk parity model has the same variance contribution for each asset:

```Python
ptf_model_train = model.predict(X_train)
ptf_model_train.plot_contribution(measure=RiskMeasure.ANNUALIZED_VARIANCE)
```

<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>[plotly figure stripped from llms output]
<br />
<br />

And the inverse volatility model has non-equal variance contribution. This is because
the correlation is not taken into account in an inverse volatility model:

```Python
ptf_bench_train = bench.predict(X_train)
ptf_bench_train.plot_contribution(measure=RiskMeasure.ANNUALIZED_VARIANCE)
```

<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>[plotly figure stripped from llms output]
<br />
<br />

<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)
```

The `predict` method returns a [`Portfolio`](https://skfolio.org/generated/skfolio.portfolio.Portfolio.html.md#skfolio.portfolio.Portfolio) object.

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

## Analysis

For improved analysis, we 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 composition:

```Python
population.plot_composition()
```

<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>[plotly figure stripped from llms output]
<br />
<br />

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 - Variance</th>
      <th>Inverse Vol</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Mean</th>
      <td>0.065%</td>
      <td>0.064%</td>
    </tr>
    <tr>
      <th>Annualized Mean</th>
      <td>16.40%</td>
      <td>16.06%</td>
    </tr>
    <tr>
      <th>Variance</th>
      <td>0.00010</td>
      <td>0.00010</td>
    </tr>
    <tr>
      <th>Annualized Variance</th>
      <td>2.63%</td>
      <td>2.56%</td>
    </tr>
    <tr>
      <th>Semi-Variance</th>
      <td>0.000054</td>
      <td>0.000053</td>
    </tr>
    <tr>
      <th>Annualized Semi-Variance</th>
      <td>1.36%</td>
      <td>1.33%</td>
    </tr>
    <tr>
      <th>Standard Deviation</th>
      <td>1.02%</td>
      <td>1.01%</td>
    </tr>
    <tr>
      <th>Annualized Standard Deviation</th>
      <td>16.22%</td>
      <td>16.00%</td>
    </tr>
    <tr>
      <th>Semi-Deviation</th>
      <td>0.73%</td>
      <td>0.73%</td>
    </tr>
    <tr>
      <th>Annualized Semi-Deviation</th>
      <td>11.66%</td>
      <td>11.54%</td>
    </tr>
    <tr>
      <th>Mean Absolute Deviation</th>
      <td>0.66%</td>
      <td>0.65%</td>
    </tr>
    <tr>
      <th>CVaR at 95%</th>
      <td>2.37%</td>
      <td>2.35%</td>
    </tr>
    <tr>
      <th>EVaR at 95%</th>
      <td>5.23%</td>
      <td>5.29%</td>
    </tr>
    <tr>
      <th>Worst Realization</th>
      <td>10.34%</td>
      <td>10.49%</td>
    </tr>
    <tr>
      <th>CDaR at 95%</th>
      <td>12.39%</td>
      <td>12.22%</td>
    </tr>
    <tr>
      <th>MAX Drawdown</th>
      <td>34.30%</td>
      <td>34.83%</td>
    </tr>
    <tr>
      <th>Average Drawdown</th>
      <td>2.40%</td>
      <td>2.34%</td>
    </tr>
    <tr>
      <th>EDaR at 95%</th>
      <td>19.86%</td>
      <td>20.05%</td>
    </tr>
    <tr>
      <th>First Lower Partial Moment</th>
      <td>0.33%</td>
      <td>0.32%</td>
    </tr>
    <tr>
      <th>Ulcer Index</th>
      <td>0.041</td>
      <td>0.040</td>
    </tr>
    <tr>
      <th>Gini Mean Difference</th>
      <td>1.00%</td>
      <td>0.98%</td>
    </tr>
    <tr>
      <th>Value at Risk at 95%</th>
      <td>1.46%</td>
      <td>1.45%</td>
    </tr>
    <tr>
      <th>Drawdown at Risk at 95%</th>
      <td>8.97%</td>
      <td>8.79%</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.000023%</td>
      <td>0.000022%</td>
    </tr>
    <tr>
      <th>Fourth Lower Partial Moment</th>
      <td>0.000011%</td>
      <td>0.000011%</td>
    </tr>
    <tr>
      <th>Skew</th>
      <td>-5.68%</td>
      <td>-14.95%</td>
    </tr>
    <tr>
      <th>Kurtosis</th>
      <td>2067.02%</td>
      <td>2118.56%</td>
    </tr>
    <tr>
      <th>Sharpe Ratio</th>
      <td>0.064</td>
      <td>0.063</td>
    </tr>
    <tr>
      <th>Annualized Sharpe Ratio</th>
      <td>1.01</td>
      <td>1.00</td>
    </tr>
    <tr>
      <th>Sortino Ratio</th>
      <td>0.089</td>
      <td>0.088</td>
    </tr>
    <tr>
      <th>Annualized Sortino Ratio</th>
      <td>1.41</td>
      <td>1.39</td>
    </tr>
    <tr>
      <th>Mean Absolute Deviation Ratio</th>
      <td>0.098</td>
      <td>0.098</td>
    </tr>
    <tr>
      <th>First Lower Partial Moment Ratio</th>
      <td>0.20</td>
      <td>0.20</td>
    </tr>
    <tr>
      <th>Value at Risk Ratio at 95%</th>
      <td>0.045</td>
      <td>0.044</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.00022</td>
      <td>0.00021</td>
    </tr>
    <tr>
      <th>EVaR Ratio at 95%</th>
      <td>0.012</td>
      <td>0.012</td>
    </tr>
    <tr>
      <th>Worst Realization Ratio</th>
      <td>0.0063</td>
      <td>0.0061</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.0053</td>
      <td>0.0052</td>
    </tr>
    <tr>
      <th>Calmar Ratio</th>
      <td>0.0019</td>
      <td>0.0018</td>
    </tr>
    <tr>
      <th>Average Drawdown Ratio</th>
      <td>0.027</td>
      <td>0.027</td>
    </tr>
    <tr>
      <th>EDaR Ratio at 95%</th>
      <td>0.0033</td>
      <td>0.0032</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>18.9958444521665</td>
      <td>18.460872007821077</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 1.628 seconds)

<a id="sphx-glr-download-auto-examples-risk-budgeting-plot-1-risk-parity-variance-py"></a>
