Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random portfolios very bunched when following cookbook #576

Open
cianryan09 opened this issue Jan 11, 2024 · 2 comments
Open

Random portfolios very bunched when following cookbook #576

cianryan09 opened this issue Jan 11, 2024 · 2 comments
Labels
question Further information is requested

Comments

@cianryan09
Copy link

I am following the cookbook guide to plot the efficient frontier and optimal portfolio and the random portfolios end up becoming very bunched.

rf = 0.054

mu = capm_return(df, df_BM, risk_free_rate=rf)
S = CovarianceShrinkage(df).ledoit_wolf()

# print(mu)
# print(S)

ef = EfficientFrontier(mu, S, )

fig, ax = plt.subplots()
ef_max_sharpe = ef.deepcopy()
plotting.plot_efficient_frontier(ef, ax=ax, show_assets=False)

# Find the tangency portfolio
ef_max_sharpe.max_sharpe()
ret_tangent, std_tangent, _ = ef_max_sharpe.portfolio_performance()
ax.scatter(std_tangent, ret_tangent, marker="*", s=100, c="r", label="Max Sharpe")

# Generate random portfolios
n_samples = 100
w = np.random.dirichlet(np.ones(ef.n_assets), n_samples)
rets = w.dot(ef.expected_returns)
stds = np.sqrt(np.diag(w @ ef.cov_matrix @ w.T))
sharpes = rets / stds
ax.scatter(stds, rets, marker=".", c=sharpes, cmap="viridis_r")

# Output
ax.set_title("Efficient Frontier with random portfolios")
ax.legend()
plt.tight_layout()
plt.savefig("ef_scatter.png", dpi=200)
plt.show()

My data is the constituents of the S&P500 up until YE 2023.
here is how it turns out:
EF

Basically the shape ratios end up very close together (0.69 - 0.71 range) so it doesn't really visually represent the whole exercise very well. Has anyone else had this issue?

Also I would suggest a potential edit for the ReadTheDocs - it doesn't specify which modules you have to import at the start, it would be useful for people less used to Python or coding in general to have these written in the example. I had to figure out that it was matplotlib.plt and plotting myself.

@cianryan09 cianryan09 added the question Further information is requested label Jan 11, 2024
@cianryan09
Copy link
Author

I would also like to amend the axis ranges - the expected return of the optimised portfolio is at the bottom of the frontier, preferably it would be in the middle, currently everything gets bunched in the bottom right corner.

In addition, I would like to plot the tangency line from the risk free rate (5.4% in my case) to the max shapre portfolio. Is there a way of doing this?

@baobach
Copy link

baobach commented Jan 27, 2024

Have you considered increasing the number of samples for your random portfolios? One hundred seems rather small. Typically, in the Monte Carlo simulation approach, starting with 100,000 samples is advisable. Hypothetically, the higher the number, the closer you get to your target weight allocation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants