• API
• FAQ
• Tools
• Archive
SHARE
TWEET Untitled a guest Jun 27th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def neg_sharpe_ratio_m(weights, mean_returns, cov_matrix, risk_free_rate, alpha):
2.     p_var, p_ret = portfolio_annualised_performance(weights, mean_returns, cov_matrix)
3.     m_risk = equal_risk_contributon(weights, cov_matrix)
4.     return -(p_ret - risk_free_rate)/p_var*alpha + (1-alpha)*m_risk
5.
6. def max_sharpe_ratio_m(mean_returns, cov_matrix, risk_free_rate, alpha):
7.     num_assets = mean_returns.shape
8.     args = (mean_returns, cov_matrix, risk_free_rate, alpha)
9.     constraints = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
10.     bound = (0.0,1.0)
11.     bounds = tuple(bound for asset in range(num_assets))
12.     result = minimize(neg_sharpe_ratio_m, num_assets*[1./num_assets,], args=args,
13.                         method='SLSQP', bounds=bounds, constraints=constraints)
14.     return result.x
15.
16. weights_alpha =[]
17. blended_list = []
18. length = etf_return.shape//66
19. for alpha in 1/np.exp2(np.arange(0,10,1)):
20.     i= 0
21.     risk_free_rate =0.0
22.     weights = np.empty([0,6])
23.     while i<length:
24.         distance = min(66,etf_return.shape-i*66)
25.         cov_matrix = np.cov(etf_return.iloc[i*66: (i+1)*min(distance, 66) , :].values,
26.                                             rowvar = False)
27.         rt = np.mean(etf_return.iloc[i*66: (i+1)*min(distance, 66) , :].values, axis=0)
28.         result = np.around(max_sharpe_ratio_m(rt, cov_matrix, risk_free_rate, alpha), 3)
29.         weights = np.vstack((weights, result))
30.         i+=1
31.     weights_alpha.append(weights)
32.     blended = portfolio_performance(weights)
33.     blended_list.append(blended)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top