Last active
July 18, 2021 11:24
-
-
Save rian-dolphin/51d9a72b650c7a085388b0d5a26dc9bb to your computer and use it in GitHub Desktop.
MPT_Medium_1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#- How many assests to include in each portfolio | |
n_assets = 5 | |
#-- How many portfolios to generate | |
n_portfolios = 1000 | |
#-- Initialize empty list to store mean-variance pairs for plotting | |
mean_variance_pairs = [] | |
np.random.seed(75) | |
#-- Loop through and generate lots of random portfolios | |
for i in range(n_portfolios): | |
#- Choose assets randomly without replacement | |
assets = np.random.choice(list(daily_returns.columns), n_assets, replace=False) | |
#- Choose weights randomly | |
weights = np.random.rand(n_assets) | |
#- Ensure weights sum to 1 | |
weights = weights/sum(weights) | |
#-- Loop over asset pairs and compute portfolio return and variance | |
#- https://quant.stackexchange.com/questions/43442/portfolio-variance-explanation-for-equation-investments-by-zvi-bodie | |
portfolio_E_Variance = 0 | |
portfolio_E_Return = 0 | |
for i in range(len(assets)): | |
portfolio_E_Return += weights[i] * mus.loc[assets[i]] | |
for j in range(len(assets)): | |
#-- Add variance/covariance for each asset pair | |
#- Note that when i==j this adds the variance | |
portfolio_E_Variance += weights[i] * weights[j] * cov.loc[assets[i], assets[j]] | |
#-- Add the mean/variance pairs to a list for plotting | |
mean_variance_pairs.append([portfolio_E_Return, portfolio_E_Variance]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment