robertmartin8 / PyPortfolioOpt

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

Plotting efficient frontier and optimal port #147

Closed renzonuz closed 4 years ago

renzonuz commented 4 years ago

Hi im using the following code to graph the efficient frontier, however the graph i get its setting the optimal portfolio at a point that does not make sense

with this optimal port:

Expected annual return: 7.3%
Annual volatility: 4.8%
Sharpe Ratio: 1.10

i get the following frontier and optimal port: image

code used:

mu = expected_returns.mean_historical_return(df1, frequency=12)
Sigma = risk_models.sample_cov(df1, frequency=12)
ef = EfficientFrontier(mu, Sigma,  weight_bounds=(0, 0.15))
ef.max_sharpe()
cleaned_weights = ef.clean_weights(cutoff=0.01, rounding = 2)
ef.portfolio_performance(verbose=True)

cla = CLA(mu, Sigma)
ax = plotting.plot_efficient_frontier(cla, showfig = False)
ax.xaxis.set_major_formatter(FuncFormatter(lambda x, _: '{:.0%}'.format(x)))
ax.yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
arbarbera commented 4 years ago

Hi, In the case of a limited number of assets, Is there a way to put labels in plotted point? Thx a lot Roberto

robertmartin8 commented 4 years ago

@renzonuz what is wrong with the optimal portfolio?

@arbarbera Unfortunately not, however you might be able to take a look at the plotting code here and modify it to suit your needs.

renzonuz commented 4 years ago

@robertmartin8 the problem is that the optimizator gives me a Expected annual return: 7.3% and Annual volatility: 4.8%

however as you can see on the the optimal portfolio signaled with a red x is at Expected annual return: 0% and Annual volatility: 0%, do you know what the problem could be?

image

regards

Renzo

robertmartin8 commented 4 years ago

Hmm my mistake, that is indeed a bug. I wonder if it's got something to do with the time period - I've never tested the plots with a different frequency.

On Wed, 15 Jul 2020, 19:16 renzonuz, notifications@github.com wrote:

@robertmartin8 https://github.com/robertmartin8 the problem is that the optimizator gives me a Expected annual return: 7.3% and Annual volatility: 4.8%

however as you can see on the the optimal portfolio signaled with a red x is at Expected annual return: 0% and Annual volatility: 0%, do you know what the problem could be?

[image: image] https://user-images.githubusercontent.com/67790786/87538967-784be580-c673-11ea-9585-f5d1d9e6c417.png

regards

Renzo

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/robertmartin8/PyPortfolioOpt/issues/147#issuecomment-658709106, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACZ7ECOYZR4NO5234DD3FGTR3WF2FANCNFSM4OPZTTBA .

robertmartin8 commented 4 years ago

I think this has got to do with the numerical differences between CLA and max_sharpe. Your max_sharpe is currently plotting the constrained efficient frontier (since you have weight bounds at 15%), while CLA is unconstrained.

If you try the below snippet, I think you'll see that it gives you something different to ef.portfolio_performance. I suspect the Sharpe will be higher because it is unconstrained.

cla = CLA(mu, Sigma)
cla.max_sharpe()
cla.portfolio_performance(verbose=True)

I am still working on a way to plot the efficient frontier directly from the EfficientFrontier object rather than CLA, so in future these should correspond.