Skip to content

Instantly share code, notes, and snippets.

@fnneves
Created October 11, 2018 00:02
Show Gist options
  • Save fnneves/55cd577332af91c3623dd62411b713cb to your computer and use it in GitHub Desktop.
Save fnneves/55cd577332af91c3623dd62411b713cb to your computer and use it in GitHub Desktop.
Markowitz
def get_ret_vol_sr(weights):
weights = np.array(weights)
ret = np.sum(log_ret.mean() * weights) * 252
vol = np.sqrt(np.dot(weights.T, np.dot(log_ret.cov()*252, weights)))
sr = ret/vol
return np.array([ret, vol, sr])
def neg_sharpe(weights):
# the number 2 is the sharpe ratio index from the get_ret_vol_sr
return get_ret_vol_sr(weights)[2] * -1
def check_sum(weights):
#return 0 if sum of the weights is 1
return np.sum(weights)-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment