python 金融代写

Use the 11 stock return series from HW2 to find the weight vector using the following smart beta schemes. Use an upper bound weight of 50% for each stock

  1. Maximum Diversification Ratio (MDR)
  2. Global Minimum Variance (GMV)
  3. Maximum Sharpe Ratio (MSR)

You will need to import ‘minimize’ from scipy.optimize. To help you understand how to use an optimizer, a sample code for the objective function and the optimization for Equal Risk Contribution (ERC) is provided for you (you need to understand and change the code).

def risk_parity_function(weights, cov):
temp = np.dot(cov,weights) / np.dot(np.dot(weights, cov),weights)**(1/2)
MC = weights[:] * temp[:]
return (sum(((np.dot(np.dot(weights,cov),weights))**(1/2) / len(weights) -MC)**2))

def risk_parity(data, long = 1):
cov = data.cov()
n = cov.shape[0]
weights = np.ones(n) /n
cons = ({‘type’: ‘eq’, ‘fun’: lambda x: 1 – sum(x)}) bnds = [(0,1) for i in weights]

if long == 1:
res = minimize(risk_parity_function, x0 = weights, args = (cov), method = ‘SLSQP’, constraints = cons,

bounds = bnds, tol = 1e-30)

else:

res = minimize(risk_parity_function, x0 = weights, args = (cov), method = ‘SLSQP’, constraints = cons, tol = 1e-30)

return res.x