Created
May 11, 2022 20:55
-
-
Save Zmey56/24a8ddb3de7045bd0649878e6198e16c 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
port_return = [] | |
# Initialize an empty list for storing the portfolio volatility | |
port_volatility = [] | |
# Initialize an empty list for storing the portfolio weights | |
port_weights = [] | |
num_assets = len(data.columns) | |
num_portfolio = 1000000 | |
individual_rets = data.resample('Y').last().pct_change().mean() | |
for port in range(num_portfolio): | |
# Randomly generate weigh combination | |
weights = np.random.random(num_assets) | |
# Normalize weight so that they sum to 1 | |
weights = weights/np.sum(weights) | |
port_weights.append(weights) | |
# Return are the dot product of individual expected returns of asset and its weights | |
returns = np.dot(weights, individual_rets) | |
port_return.append(returns) | |
# Computing Portfolio Volatility | |
portfolio_volatility = np.sqrt(np.dot(weights.T,np.dot(var_matrix*252,weights))) | |
port_volatility.append(portfolio_volatility) | |
portfolio = {'Returns': port_return,'Volatility': port_volatility} | |
for counter, symbol in enumerate(data.columns.tolist()): | |
portfolio[symbol] = [w[counter] for w in port_weights] | |
portfolios_V1 = pd.DataFrame(portfolio) | |
portfolios_V1.head() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment