Last active
July 18, 2021 11:05
-
-
Save rian-dolphin/99847bf073ddf0dd9c1815bfbdc0c981 to your computer and use it in GitHub Desktop.
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
#-- Create random portfolio weights and indexes | |
#- How many assests in the portfolio | |
n_assets = 5 | |
mean_variance_pairs = [] | |
#-- Store weights and tickers for labelling | |
weights_list=[] | |
tickers_list=[] | |
for i in tqdm(range(1000)): | |
next_i = False | |
while True: | |
#- Choose assets randomly without replacement | |
assets = np.random.choice(tickers, n_assets, replace=False) | |
#- Choose weights randomly ensuring they sum to one | |
weights = np.random.rand(n_assets) | |
weights = weights/sum(weights) | |
#-- Loop over asset pairs and compute portfolio return and variance | |
portfolio_E_Variance = 0 | |
portfolio_E_Return = 0 | |
for i in range(len(assets)): | |
portfolio_E_Return += weights[i] * df.mu.loc[assets[i]] | |
for j in range(len(assets)): | |
portfolio_E_Variance += weights[i] * weights[j] * cov.loc[assets[i], assets[j]] | |
#-- Skip over dominated portfolios | |
for R,V in mean_variance_pairs: | |
if (R > portfolio_E_Return) & (V < portfolio_E_Variance): | |
next_i = True | |
break | |
if next_i: | |
break | |
#-- Add the mean/variance pairs to a list for plotting | |
mean_variance_pairs.append([portfolio_E_Return, portfolio_E_Variance]) | |
weights_list.append(weights) | |
tickers_list.append(assets) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment