Skip to content

Instantly share code, notes, and snippets.

@fnneves
Last active July 30, 2021 06:54
Show Gist options
  • Save fnneves/d5d7d1f29016f62b589455390c20aee5 to your computer and use it in GitHub Desktop.
Save fnneves/d5d7d1f29016f62b589455390c20aee5 to your computer and use it in GitHub Desktop.
medium
np.random.seed(42)
num_ports = 6000
all_weights = np.zeros((num_ports, len(stocks.columns)))
ret_arr = np.zeros(num_ports)
vol_arr = np.zeros(num_ports)
sharpe_arr = np.zeros(num_ports)
for x in range(num_ports):
# Weights
weights = np.array(np.random.random(4))
weights = weights/np.sum(weights)
# Save weights
all_weights[x,:] = weights
# Expected return
ret_arr[x] = np.sum( (log_ret.mean() * weights * 252))
# Expected volatility
vol_arr[x] = np.sqrt(np.dot(weights.T, np.dot(log_ret.cov()*252, weights)))
# Sharpe Ratio
sharpe_arr[x] = ret_arr[x]/vol_arr[x]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment