robertmartin8 / PyPortfolioOpt

Financial portfolio optimisation in python, including classical efficient frontier, Black-Litterman, Hierarchical Risk Parity
https://pyportfolioopt.readthedocs.io/
MIT License
4.25k stars 928 forks source link

Stock is recommended for Short on Mean-variance optimization but for Long on CAPM #499

Closed Originn closed 1 year ago

Originn commented 1 year ago

I am following the cookbook for testing the results, but after setting it up I have the the issue as mentioned above.

This is my code for minimum volitility portfolio:

S = risk_models.CovarianceShrinkage(prices).ledoit_wolf()
ef = EfficientFrontier(None, S, weight_bounds=(None, None))
 ef.min_volatility()
weights = ef.clean_weights()
nu = pd.Series(weights)
fig = px.bar(nu, orientation='h')
fig.update_layout(width=700, height=500, title_text = "Weights for minimum volatility (long only)", title_x = 0.5, showlegend=False, yaxis_title=None, xaxis_title=None)
da = DiscreteAllocation(weights, latest_prices, total_portfolio_value=10000)

This is the result image

This is my code for L2 regularisation:

mu = pypfopt.expected_returns.capm_return(prices)
S = risk_models.CovarianceShrinkage(prices).ledoit_wolf()
ef = EfficientFrontier(mu, S)
ef.add_objective(objective_functions.L2_reg, gamma=0.25)  # gamme is the tuning parameter
ef.efficient_risk(int(request.form.get("volatility"))/100)
weights = ef.clean_weights()
ef.portfolio_performance()

The result is:

image

I wonder why TQQQ is suggested for buy in the L2 Regularisation but for short in the min_volatility ?

robertmartin8 commented 1 year ago

For the L2 regularisation, you have not passed bounds on the weights so it defaults to (0,1), which is long-only.

Originn commented 1 year ago

Hi,

I am concerned about the min_volatility suggesting to Short it, while CAPM suggesting to Long it. Why is the difference in the suggestions? If min_volatility is only concerned with volatility what is the difference if we Long or Short TQQQ?