1. Use Pandas to read the HW3.xlsx (provided) into Dataframes ‘equity’ and ‘factor’. Convert them into numpy
arrays and construct the corresponding simple returns.
2. Perform a PCA analysis on the factors’ returns using numpy linalg.eig, find the minimum number of principal
components to cover a required explanatory power (set as 0.8). Extract the principal components using
statsmodels.sandbox.tools.tools_pca.
3. Run a multivariate linear regression of the standardized equity index returns over the PCs from Q2 using
sklearn.linear_model, output all the beta coefficients into dataframe and save it as coef.csv.
4. Run a multiple regression for each standardized equity index return over the PCs: the OLS (statsmodels.api) with
intercept (‘add_constant’ function). retrieve the beta, t-value and the R-square. Output all of them into beta.csv,
tvalue.csv, Rsq.csv.
5. Use the HSI stocks monthly return series from HW2 to find the weight vector using the following smart beta
schemes. Use an upper bound weight of 10% for each stock
a. Maximum Diversification Ratio (MDR)
b. Global Minimum Variance (GMV)
c. 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):
cov = data.cov()
n = cov.shape[0]
weights = np.ones(n) /n
cons = ({'type': 'eq', 'fun': lambda x: 1 - sum(x)})
bnds = [(0,0.1) for i in weights]
res = minimize(risk_parity_function, x0 = weights, args = (cov), method = 'SLSQP', constraints = cons,
bounds = bnds, tol = 1e-30)
return res.x