<a id="sphx-glr-auto-examples-maximum-diversification-plot-1-maximum-diversification-py"></a>

<a id="maximum-diversification"></a>

# Maximum Diversification

This tutorial uses the [`MaximumDiversification`](https://skfolio.org/generated/skfolio.optimization.MaximumDiversification.html.md#skfolio.optimization.MaximumDiversification)
optimization to find the portfolio that maximizes the diversification ratio, which is
the ratio of the weighted volatilities over the total volatility.

<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
from skfolio.datasets import load_sp500_dataset
from skfolio.optimization import EqualWeighted, MaximumDiversification
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 maximum diversification model and then fit it on the training set:

```Python
model = MaximumDiversification()
model.fit(X_train)
model.weights_
```

```none
array([8.33459971e-02, 6.74138299e-02, 2.93952123e-02, 8.57558650e-02,
       4.12145074e-02, 8.80360023e-09, 1.53457231e-08, 4.41151909e-02,
       1.70046770e-08, 5.11503226e-02, 6.82590399e-02, 3.02728712e-02,
       3.79430055e-03, 9.95058777e-02, 1.48755617e-02, 1.10849163e-01,
       1.08087391e-01, 9.45176307e-02, 6.51331697e-02, 2.31402810e-03])
```

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

```Python
bench = EqualWeighted()
bench.fit(X_train)
bench.weights_
```

```none
array([0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05,
       0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05])
```

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

## Diversification Analysis

Let’s analyze the diversification ratio of both models on the training set.
As expected, the maximum diversification model has the highest diversification ratio:

```Python
ptf_model_train = model.predict(X_train)
ptf_bench_train = bench.predict(X_train)
print("Diversification Ratio:")
print(f"    Maximum Diversification model: {ptf_model_train.diversification:0.2f}")
print(f"    Equal Weighted model: {ptf_bench_train.diversification:0.2f}")
```

```none
Diversification Ratio:
    Maximum Diversification model: 1.92
    Equal Weighted model: 1.82
```

<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 composition:

```Python
fig = population.plot_composition()
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 can show a full summary of both strategies evaluated on the test set:

```Python
population.plot_cumulative_returns()
```

<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 />

<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>MaximumDiversification</th>
      <th>EqualWeighted</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Mean</th>
      <td>0.072%</td>
      <td>0.069%</td>
    </tr>
    <tr>
      <th>Annualized Mean</th>
      <td>18.17%</td>
      <td>17.30%</td>
    </tr>
    <tr>
      <th>Variance</th>
      <td>0.00012</td>
      <td>0.00012</td>
    </tr>
    <tr>
      <th>Annualized Variance</th>
      <td>3.14%</td>
      <td>2.94%</td>
    </tr>
    <tr>
      <th>Semi-Variance</th>
      <td>0.000062</td>
      <td>0.000060</td>
    </tr>
    <tr>
      <th>Annualized Semi-Variance</th>
      <td>1.55%</td>
      <td>1.52%</td>
    </tr>
    <tr>
      <th>Standard Deviation</th>
      <td>1.12%</td>
      <td>1.08%</td>
    </tr>
    <tr>
      <th>Annualized Standard Deviation</th>
      <td>17.71%</td>
      <td>17.15%</td>
    </tr>
    <tr>
      <th>Semi-Deviation</th>
      <td>0.79%</td>
      <td>0.78%</td>
    </tr>
    <tr>
      <th>Annualized Semi-Deviation</th>
      <td>12.46%</td>
      <td>12.32%</td>
    </tr>
    <tr>
      <th>Mean Absolute Deviation</th>
      <td>0.75%</td>
      <td>0.71%</td>
    </tr>
    <tr>
      <th>CVaR at 95%</th>
      <td>2.51%</td>
      <td>2.51%</td>
    </tr>
    <tr>
      <th>EVaR at 95%</th>
      <td>4.78%</td>
      <td>5.43%</td>
    </tr>
    <tr>
      <th>Worst Realization</th>
      <td>9.07%</td>
      <td>10.77%</td>
    </tr>
    <tr>
      <th>CDaR at 95%</th>
      <td>14.57%</td>
      <td>13.35%</td>
    </tr>
    <tr>
      <th>MAX Drawdown</th>
      <td>29.52%</td>
      <td>34.70%</td>
    </tr>
    <tr>
      <th>Average Drawdown</th>
      <td>3.24%</td>
      <td>2.67%</td>
    </tr>
    <tr>
      <th>EDaR at 95%</th>
      <td>18.59%</td>
      <td>20.49%</td>
    </tr>
    <tr>
      <th>First Lower Partial Moment</th>
      <td>0.37%</td>
      <td>0.35%</td>
    </tr>
    <tr>
      <th>Ulcer Index</th>
      <td>0.051</td>
      <td>0.044</td>
    </tr>
    <tr>
      <th>Gini Mean Difference</th>
      <td>1.12%</td>
      <td>1.06%</td>
    </tr>
    <tr>
      <th>Value at Risk at 95%</th>
      <td>1.59%</td>
      <td>1.54%</td>
    </tr>
    <tr>
      <th>Drawdown at Risk at 95%</th>
      <td>11.66%</td>
      <td>9.48%</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.000028%</td>
      <td>0.000027%</td>
    </tr>
    <tr>
      <th>Fourth Lower Partial Moment</th>
      <td>0.000009%</td>
      <td>0.000013%</td>
    </tr>
    <tr>
      <th>Skew</th>
      <td>45.26%</td>
      <td>-2.79%</td>
    </tr>
    <tr>
      <th>Kurtosis</th>
      <td>1802.21%</td>
      <td>1961.33%</td>
    </tr>
    <tr>
      <th>Sharpe Ratio</th>
      <td>0.065</td>
      <td>0.064</td>
    </tr>
    <tr>
      <th>Annualized Sharpe Ratio</th>
      <td>1.03</td>
      <td>1.01</td>
    </tr>
    <tr>
      <th>Sortino Ratio</th>
      <td>0.092</td>
      <td>0.088</td>
    </tr>
    <tr>
      <th>Annualized Sortino Ratio</th>
      <td>1.46</td>
      <td>1.40</td>
    </tr>
    <tr>
      <th>Mean Absolute Deviation Ratio</th>
      <td>0.096</td>
      <td>0.097</td>
    </tr>
    <tr>
      <th>First Lower Partial Moment Ratio</th>
      <td>0.19</td>
      <td>0.19</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.029</td>
      <td>0.027</td>
    </tr>
    <tr>
      <th>Entropic Risk Measure Ratio at 95%</th>
      <td>0.00024</td>
      <td>0.00023</td>
    </tr>
    <tr>
      <th>EVaR Ratio at 95%</th>
      <td>0.015</td>
      <td>0.013</td>
    </tr>
    <tr>
      <th>Worst Realization Ratio</th>
      <td>0.0079</td>
      <td>0.0064</td>
    </tr>
    <tr>
      <th>Drawdown at Risk Ratio at 95%</th>
      <td>0.0062</td>
      <td>0.0072</td>
    </tr>
    <tr>
      <th>CDaR Ratio at 95%</th>
      <td>0.0049</td>
      <td>0.0051</td>
    </tr>
    <tr>
      <th>Calmar Ratio</th>
      <td>0.0024</td>
      <td>0.0020</td>
    </tr>
    <tr>
      <th>Average Drawdown Ratio</th>
      <td>0.022</td>
      <td>0.026</td>
    </tr>
    <tr>
      <th>EDaR Ratio at 95%</th>
      <td>0.0039</td>
      <td>0.0033</td>
    </tr>
    <tr>
      <th>Ulcer Index Ratio</th>
      <td>0.014</td>
      <td>0.015</td>
    </tr>
    <tr>
      <th>Gini Mean Difference Ratio</th>
      <td>0.065</td>
      <td>0.064</td>
    </tr>
    <tr>
      <th>Effective Number of Assets</th>
      <td>12.684721598180525</td>
      <td>19.999999999999993</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.004 seconds)

<a id="sphx-glr-download-auto-examples-maximum-diversification-plot-1-maximum-diversification-py"></a>
