Maximum Diversification#

This tutorial uses the MaximumDiversification optimization to find the portfolio that maximizes the diversification ratio, which is the ratio of the weighted volatilities over the total volatility.

Data#

We load the S&P 500 dataset 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:

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)

Model#

We create the maximum diversification model and then fit it on the training set:

model = MaximumDiversification()
model.fit(X_train)
model.weights_
array([8.33456624e-02, 6.74138185e-02, 2.93952573e-02, 8.57560762e-02,
       4.11892006e-02, 7.98154300e-11, 1.50727312e-10, 4.41147667e-02,
       1.90340137e-10, 5.11478008e-02, 6.82592031e-02, 3.02737024e-02,
       3.79050966e-03, 9.95048460e-02, 1.48699105e-02, 1.10847215e-01,
       1.08086158e-01, 9.45171911e-02, 6.51323607e-02, 2.35632022e-03])

To compare this model, we use an equal weighted benchmark using the EqualWeighted estimator:

bench = EqualWeighted()
bench.fit(X_train)
bench.weights_
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])

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:

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}")
Diversification Ratio:
    Maximum Diversification model: 1.92
    Equal Weighted model: 1.82

Prediction#

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

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

Analysis#

For improved analysis, it’s possible to load both predicted portfolios into a Population:

population = Population([ptf_model_test, ptf_bench_test])

Let’s plot each portfolio composition:

fig = population.plot_composition()
show(fig)

Finally we can show a full summary of both strategies evaluated on the test set:

population.plot_cumulative_returns()



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

population.summary()
MaximumDiversification EqualWeighted
Mean 0.072% 0.069%
Annualized Mean 18.17% 17.30%
Variance 0.012% 0.012%
Annualized Variance 3.14% 2.94%
Semi-Variance 0.0062% 0.0060%
Annualized Semi-Variance 1.55% 1.52%
Standard Deviation 1.12% 1.08%
Annualized Standard Deviation 17.71% 17.15%
Semi-Deviation 0.79% 0.78%
Annualized Semi-Deviation 12.46% 12.32%
Mean Absolute Deviation 0.75% 0.71%
CVaR at 95% 2.51% 2.51%
EVaR at 95% 4.78% 5.43%
Worst Realization 9.07% 10.77%
CDaR at 95% 14.57% 13.35%
MAX Drawdown 29.52% 34.70%
Average Drawdown 3.24% 2.67%
EDaR at 95% 18.59% 20.49%
First Lower Partial Moment 0.37% 0.35%
Ulcer Index 0.051 0.044
Gini Mean Difference 1.12% 1.06%
Value at Risk at 95% 1.59% 1.54%
Drawdown at Risk at 95% 11.66% 9.48%
Entropic Risk Measure at 95% 3.00 3.00
Fourth Central Moment 0.000028% 0.000027%
Fourth Lower Partial Moment 0.000009% 0.000013%
Skew 45.23% -2.79%
Kurtosis 1800.83% 1959.90%
Sharpe Ratio 0.065 0.064
Annualized Sharpe Ratio 1.03 1.01
Sortino Ratio 0.092 0.088
Annualized Sortino Ratio 1.46 1.40
Mean Absolute Deviation Ratio 0.096 0.097
First Lower Partial Moment Ratio 0.19 0.19
Value at Risk Ratio at 95% 0.045 0.045
CVaR Ratio at 95% 0.029 0.027
Entropic Risk Measure Ratio at 95% 0.00024 0.00023
EVaR Ratio at 95% 0.015 0.013
Worst Realization Ratio 0.0079 0.0064
Drawdown at Risk Ratio at 95% 0.0062 0.0072
CDaR Ratio at 95% 0.0049 0.0051
Calmar Ratio 0.0024 0.0020
Average Drawdown Ratio 0.022 0.026
EDaR Ratio at 95% 0.0039 0.0033
Ulcer Index Ratio 0.014 0.015
Gini Mean Difference Ratio 0.065 0.064
Effective Number of Assets 12.685271602584908 19.999999999999993
Assets Number 20 20


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

Gallery generated by Sphinx-Gallery