def _refresh_fast_backtest(x): _d_bt_bs, _d_bt_fr, _d_flag = x _d_bt_bs_dt = _d_bt_bs.listdir() _d_bt_bs_dt = _d_bt_bs_dt[:-2] # drop the last two ndt = len(_d_bt_bs_dt) nsig = 50 ri1_1 = np.zeros([ndt, nsig]) ri1_2 = np.zeros([ndt, nsig]) ri1_3 = np.zeros([ndt, nsig]) ri1_4 = np.zeros([ndt, nsig]) ri1_5 = np.zeros([ndt, nsig]) gi1_1 = np.zeros([ndt, nsig]) gi1_2 = np.zeros([ndt, nsig]) gi1_3 = np.zeros([ndt, nsig]) gi1_4 = np.zeros([ndt, nsig]) gi1_5 = np.zeros([ndt, nsig]) hi1_1 = np.zeros([ndt, nsig]) hi1_2 = np.zeros([ndt, nsig]) hi1_3 = np.zeros([ndt, nsig]) hi1_4 = np.zeros([ndt, nsig]) hi1_5 = np.zeros([ndt, nsig]) si1_1a = np.zeros([ndt, nsig]) si1_2a = np.zeros([ndt, nsig]) si1_3a = np.zeros([ndt, nsig]) si1_4a = np.zeros([ndt, nsig]) si1_5a = np.zeros([ndt, nsig]) si1_1b = np.zeros([ndt, nsig]) si1_2b = np.zeros([ndt, nsig]) si1_3b = np.zeros([ndt, nsig]) si1_4b = np.zeros([ndt, nsig]) si1_5b = np.zeros([ndt, nsig]) ri2_1 = np.zeros([ndt, nsig]) ri2_2 = np.zeros([ndt, nsig]) ri2_3 = np.zeros([ndt, nsig]) ri2_4 = np.zeros([ndt, nsig]) ri2_5 = np.zeros([ndt, nsig]) gi2_1 = np.zeros([ndt, nsig]) gi2_2 = np.zeros([ndt, nsig]) gi2_3 = np.zeros([ndt, nsig]) gi2_4 = np.zeros([ndt, nsig]) gi2_5 = np.zeros([ndt, nsig]) hi2_1 = np.zeros([ndt, nsig]) hi2_2 = np.zeros([ndt, nsig]) hi2_3 = np.zeros([ndt, nsig]) hi2_4 = np.zeros([ndt, nsig]) hi2_5 = np.zeros([ndt, nsig]) si2_1a = np.zeros([ndt, nsig]) si2_2a = np.zeros([ndt, nsig]) si2_3a = np.zeros([ndt, nsig]) si2_4a = np.zeros([ndt, nsig]) si2_5a = np.zeros([ndt, nsig]) si2_1b = np.zeros([ndt, nsig]) si2_2b = np.zeros([ndt, nsig]) si2_3b = np.zeros([ndt, nsig]) si2_4b = np.zeros([ndt, nsig]) si2_5b = np.zeros([ndt, nsig]) ri3_1 = np.zeros([ndt, nsig]) ri3_2 = np.zeros([ndt, nsig]) ri3_3 = np.zeros([ndt, nsig]) ri3_4 = np.zeros([ndt, nsig]) ri3_5 = np.zeros([ndt, nsig]) gi3_1 = np.zeros([ndt, nsig]) gi3_2 = np.zeros([ndt, nsig]) gi3_3 = np.zeros([ndt, nsig]) gi3_4 = np.zeros([ndt, nsig]) gi3_5 = np.zeros([ndt, nsig]) hi3_1 = np.zeros([ndt, nsig]) hi3_2 = np.zeros([ndt, nsig]) hi3_3 = np.zeros([ndt, nsig]) hi3_4 = np.zeros([ndt, nsig]) hi3_5 = np.zeros([ndt, nsig]) si3_1a = np.zeros([ndt, nsig]) si3_2a = np.zeros([ndt, nsig]) si3_3a = np.zeros([ndt, nsig]) si3_4a = np.zeros([ndt, nsig]) si3_5a = np.zeros([ndt, nsig]) si3_1b = np.zeros([ndt, nsig]) si3_2b = np.zeros([ndt, nsig]) si3_3b = np.zeros([ndt, nsig]) si3_4b = np.zeros([ndt, nsig]) si3_5b = np.zeros([ndt, nsig]) for j, i in enumerate(_d_bt_bs_dt): # j = 0 # i = _d_bt_bs_dt[0] xi_all = _d_bt_bs.load(i) xi = xi_all['arr_0'].astype('float64') si = xi_all['arr_1'].astype('float64') ci1 = xi_all['arr_2'].astype('float64') del xi_all n = ci1.shape[0] si = (si/(0.25+(si/vt)**2)).T for k in range(0, si.shape[1]): si[:, k] *= sigp[k] flev = xi[0, :] x2 = _d_bt_fr.load(i) x2[np.isnan(x2)] = 0 # move this to prepare, also remove the other two correlation matrices to make the loading faster ici1 = np.linalg.inv(ci1) # read the momentum and low volatility scores fscm1 = np.minimum(xi[7, :], 0) # decide the order (of the neutralization!) flev = np.ascontiguousarray(flev) fscm1 = np.ascontiguousarray(fscm1) ei = np.ones(n) ici1_1 = crts.neut_corr_mat(ici1, np.vstack((xi[[2, 3], :], fscm1))) ici1_1 = crts.neut_corr_mat(ici1_1, xi[[9, 10, 11, 12], :]) # S&P 500 ici1_1 = crts.neut_corr_mat(ici1_1, xi[[21, 22, 23, 24], :]) # mom ici1_2 = crts.neut_corr_mat(ici1_1, xi[[57, 58, 59, 60], :], delta=0.4) # rut ici1_3 = crts.neut_corr_mat(ici1_1, xi[[57, 58, 59, 60], :], delta=0.6) # rut ici1_4 = crts.neut_corr_mat(ici1_1, xi[[57, 58, 59, 60], :], delta=0.8) # rut ici1_5 = crts.neut_corr_mat(ici1_1, xi[[57, 58, 59, 60], :]) # rut ici2_a = crts.neut_corr_mat(ici1, np.vstack((xi[[2, 3], :]))) ici2_1 = crts.neut_corr_mat(ici2_a, np.vstack((flev**3, flev**2, flev, ei)), delta=0.35) ici2_1 = crts.neut_corr_mat(ici2_1, xi[[9, 10, 11, 12], :]) # S&P 500 ici2_1 = crts.neut_corr_mat(ici2_1, xi[[21, 22, 23, 24], :]) # mom ici2_2 = crts.neut_corr_mat(ici2_1, xi[[57, 58, 59, 60], :], delta=0.4) # rut ici2_3 = crts.neut_corr_mat(ici2_1, xi[[57, 58, 59, 60], :], delta=0.6) # rut ici2_4 = crts.neut_corr_mat(ici2_1, xi[[57, 58, 59, 60], :], delta=0.8) # rut ici2_5 = crts.neut_corr_mat(ici2_1, xi[[57, 58, 59, 60], :]) # rut ici3_a = crts.neut_corr_mat(ici1, np.vstack((xi[[2, 3], :]))) ici3_1 = crts.neut_corr_mat(ici3_a, np.vstack((flev**3, flev**2, flev, ei)), delta=0.65) ici3_1 = crts.neut_corr_mat(ici3_1, xi[[9, 10, 11, 12], :]) # S&P 500 ici3_1 = crts.neut_corr_mat(ici3_1, xi[[21, 22, 23, 24], :]) # mom ici3_2 = crts.neut_corr_mat(ici3_1, xi[[57, 58, 59, 60], :], delta=0.4) # rut ici3_3 = crts.neut_corr_mat(ici3_1, xi[[57, 58, 59, 60], :], delta=0.6) # rut ici3_4 = crts.neut_corr_mat(ici3_1, xi[[57, 58, 59, 60], :], delta=0.8) # rut ici3_5 = crts.neut_corr_mat(ici3_1, xi[[57, 58, 59, 60], :]) # rut spb1 = xi[59, :] spb2 = xi[60, :] wi1_1 = np.dot(ici1_1, si) wi1_2 = np.dot(ici1_2, si) wi1_3 = np.dot(ici1_3, si) wi1_4 = np.dot(ici1_4, si) wi1_5 = np.dot(ici1_5, si) wi2_1 = np.dot(ici2_1, si) wi2_2 = np.dot(ici2_2, si) wi2_3 = np.dot(ici2_3, si) wi2_4 = np.dot(ici2_4, si) wi2_5 = np.dot(ici2_5, si) wi3_1 = np.dot(ici3_1, si) wi3_2 = np.dot(ici3_2, si) wi3_3 = np.dot(ici3_3, si) wi3_4 = np.dot(ici3_4, si) wi3_5 = np.dot(ici3_5, si) ri1_1[j, :] = np.dot(x2, wi1_1) ri1_2[j, :] = np.dot(x2, wi1_2) ri1_3[j, :] = np.dot(x2, wi1_3) ri1_4[j, :] = np.dot(x2, wi1_4) ri1_5[j, :] = np.dot(x2, wi1_5) ri2_1[j, :] = np.dot(x2, wi2_1) ri2_2[j, :] = np.dot(x2, wi2_2) ri2_3[j, :] = np.dot(x2, wi2_3) ri2_4[j, :] = np.dot(x2, wi2_4) ri2_5[j, :] = np.dot(x2, wi2_5) ri3_1[j, :] = np.dot(x2, wi3_1) ri3_2[j, :] = np.dot(x2, wi3_2) ri3_3[j, :] = np.dot(x2, wi3_3) ri3_4[j, :] = np.dot(x2, wi3_4) ri3_5[j, :] = np.dot(x2, wi3_5) hi1_1[j, :] = np.dot(flev, abs(wi1_1)) hi1_2[j, :] = np.dot(flev, abs(wi1_2)) hi1_3[j, :] = np.dot(flev, abs(wi1_3)) hi1_4[j, :] = np.dot(flev, abs(wi1_4)) hi1_5[j, :] = np.dot(flev, abs(wi1_5)) hi2_1[j, :] = np.dot(flev, abs(wi2_1)) hi2_2[j, :] = np.dot(flev, abs(wi2_2)) hi2_3[j, :] = np.dot(flev, abs(wi2_3)) hi2_4[j, :] = np.dot(flev, abs(wi2_4)) hi2_5[j, :] = np.dot(flev, abs(wi2_5)) hi3_1[j, :] = np.dot(flev, abs(wi3_1)) hi3_2[j, :] = np.dot(flev, abs(wi3_2)) hi3_3[j, :] = np.dot(flev, abs(wi3_3)) hi3_4[j, :] = np.dot(flev, abs(wi3_4)) hi3_5[j, :] = np.dot(flev, abs(wi3_5)) gi1_1[j, :] = abs(np.dot(flev, wi1_1))/hi1_1[j, :] gi1_2[j, :] = abs(np.dot(flev, wi1_2))/hi1_2[j, :] gi1_3[j, :] = abs(np.dot(flev, wi1_3))/hi1_3[j, :] gi1_4[j, :] = abs(np.dot(flev, wi1_4))/hi1_4[j, :] gi1_5[j, :] = abs(np.dot(flev, wi1_5))/hi1_5[j, :] gi2_1[j, :] = abs(np.dot(flev, wi2_1))/hi2_1[j, :] gi2_2[j, :] = abs(np.dot(flev, wi2_2))/hi2_2[j, :] gi2_3[j, :] = abs(np.dot(flev, wi2_3))/hi2_3[j, :] gi2_4[j, :] = abs(np.dot(flev, wi2_4))/hi2_4[j, :] gi2_5[j, :] = abs(np.dot(flev, wi2_5))/hi2_5[j, :] gi3_1[j, :] = abs(np.dot(flev, wi3_1))/hi3_1[j, :] gi3_2[j, :] = abs(np.dot(flev, wi3_2))/hi3_2[j, :] gi3_3[j, :] = abs(np.dot(flev, wi3_3))/hi3_3[j, :] gi3_4[j, :] = abs(np.dot(flev, wi3_4))/hi3_4[j, :] gi3_5[j, :] = abs(np.dot(flev, wi3_5))/hi3_5[j, :] si1_1a[j, :] = np.dot(spb1, wi1_1) si1_2a[j, :] = np.dot(spb1, wi1_2) si1_3a[j, :] = np.dot(spb1, wi1_3) si1_4a[j, :] = np.dot(spb1, wi1_4) si1_5a[j, :] = np.dot(spb1, wi1_5) si2_1a[j, :] = np.dot(spb1, wi2_1) si2_2a[j, :] = np.dot(spb1, wi2_2) si2_3a[j, :] = np.dot(spb1, wi2_3) si2_4a[j, :] = np.dot(spb1, wi2_4) si2_5a[j, :] = np.dot(spb1, wi2_5) si3_1a[j, :] = np.dot(spb1, wi3_1) si3_2a[j, :] = np.dot(spb1, wi3_2) si3_3a[j, :] = np.dot(spb1, wi3_3) si3_4a[j, :] = np.dot(spb1, wi3_4) si3_5a[j, :] = np.dot(spb1, wi3_5) si1_1b[j, :] = np.dot(spb2, wi1_1) si1_2b[j, :] = np.dot(spb2, wi1_2) si1_3b[j, :] = np.dot(spb2, wi1_3) si1_4b[j, :] = np.dot(spb2, wi1_4) si1_5b[j, :] = np.dot(spb2, wi1_5) si2_1b[j, :] = np.dot(spb2, wi2_1) si2_2b[j, :] = np.dot(spb2, wi2_2) si2_3b[j, :] = np.dot(spb2, wi2_3) si2_4b[j, :] = np.dot(spb2, wi2_4) si2_5b[j, :] = np.dot(spb2, wi2_5) si3_1b[j, :] = np.dot(spb2, wi3_1) si3_2b[j, :] = np.dot(spb2, wi3_2) si3_3b[j, :] = np.dot(spb2, wi3_3) si3_4b[j, :] = np.dot(spb2, wi3_4) si3_5b[j, :] = np.dot(spb2, wi3_5) nn = 50 if ((j != 0) and (j % 500 == 0)) or (j == ndt-1): # if j == ndt-1: # # print('Processing %s' % i) print('\n') print('Optimal Model') print('____________') print('Sharpe Ratio') print('O:', _np_str((np.mean(0.5*(1+np.sign(ri1_1[0:j, 0:nn])), axis=0)-0.5)*40)) print('O:', _np_str((np.mean(0.5*(1+np.sign(ri1_2[0:j, 0:nn])), axis=0)-0.5)*40)) print('O:', _np_str((np.mean(0.5*(1+np.sign(ri1_3[0:j, 0:nn])), axis=0)-0.5)*40)) print('O:', _np_str((np.mean(0.5*(1+np.sign(ri1_4[0:j, 0:nn])), axis=0)-0.5)*40)) print('O:', _np_str((np.mean(0.5*(1+np.sign(ri1_5[0:j, 0:nn])), axis=0)-0.5)*40)) print('Average Gross') print('O:', _np_str(np.mean(hi1_1[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(hi1_2[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(hi1_3[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(hi1_4[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(hi1_5[0:j, 0:nn], axis=0))) print('Average Net / Gross') print('O:', _np_str(np.mean(gi1_1[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(gi1_2[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(gi1_3[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(gi1_4[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(gi1_5[0:j, 0:nn], axis=0))) print('Average 3m S&P 500 Beta') print('O:', _np_str(np.mean(si1_1a[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(si1_2a[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(si1_3a[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(si1_4a[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(si1_5a[0:j, 0:nn], axis=0))) print('Average 6m S&P 500 Beta') print('O:', _np_str(np.mean(si1_1b[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(si1_2b[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(si1_3b[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(si1_4b[0:j, 0:nn], axis=0))) print('O:', _np_str(np.mean(si1_5b[0:j, 0:nn], axis=0))) print('\n') print('Tradable Model') print('____________') print('Sharpe Ratio') print('T:', _np_str((np.mean(0.5*(1+np.sign(ri2_1[0:j, 0:nn])), axis=0)-0.5)*40)) print('T:', _np_str((np.mean(0.5*(1+np.sign(ri2_2[0:j, 0:nn])), axis=0)-0.5)*40)) print('T:', _np_str((np.mean(0.5*(1+np.sign(ri2_3[0:j, 0:nn])), axis=0)-0.5)*40)) print('T:', _np_str((np.mean(0.5*(1+np.sign(ri2_4[0:j, 0:nn])), axis=0)-0.5)*40)) print('T:', _np_str((np.mean(0.5*(1+np.sign(ri2_5[0:j, 0:nn])), axis=0)-0.5)*40)) print('Average Gross') print('T:', _np_str(np.mean(hi2_1[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(hi2_2[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(hi2_3[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(hi2_4[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(hi2_5[0:j, 0:nn], axis=0))) print('Average Net / Gross') print('T:', _np_str(np.mean(gi2_1[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(gi2_2[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(gi2_3[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(gi2_4[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(gi2_5[0:j, 0:nn], axis=0))) print('Average 3m S&P 500 Beta') print('T:', _np_str(np.mean(si2_1a[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(si2_2a[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(si2_3a[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(si2_4a[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(si2_5a[0:j, 0:nn], axis=0))) print('Average 6m S&P 500 Beta') print('T:', _np_str(np.mean(si2_1b[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(si2_2b[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(si2_3b[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(si2_4b[0:j, 0:nn], axis=0))) print('T:', _np_str(np.mean(si2_5b[0:j, 0:nn], axis=0))) print('\n') print('Neutral Model') print('____________') print('Sharpe Ratio') print('N:', _np_str((np.mean(0.5*(1+np.sign(ri3_1[0:j, 0:nn])), axis=0)-0.5)*40)) print('N:', _np_str((np.mean(0.5*(1+np.sign(ri3_2[0:j, 0:nn])), axis=0)-0.5)*40)) print('N:', _np_str((np.mean(0.5*(1+np.sign(ri3_3[0:j, 0:nn])), axis=0)-0.5)*40)) print('N:', _np_str((np.mean(0.5*(1+np.sign(ri3_4[0:j, 0:nn])), axis=0)-0.5)*40)) print('N:', _np_str((np.mean(0.5*(1+np.sign(ri3_5[0:j, 0:nn])), axis=0)-0.5)*40)) print('Average Gross') print('N:', _np_str(np.mean(hi3_1[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(hi3_2[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(hi3_3[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(hi3_4[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(hi3_5[0:j, 0:nn], axis=0))) print('Average Net / Gross') print('N:', _np_str(np.mean(gi3_1[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(gi3_2[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(gi3_3[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(gi3_4[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(gi3_5[0:j, 0:nn], axis=0))) print('Average 3m S&P 500 Beta') print('N:', _np_str(np.mean(si3_1a[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(si3_2a[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(si3_3a[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(si3_4a[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(si3_5a[0:j, 0:nn], axis=0))) print('Average 6m S&P 500 Beta') print('N:', _np_str(np.mean(si3_1b[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(si3_2b[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(si3_3b[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(si3_4b[0:j, 0:nn], axis=0))) print('N:', _np_str(np.mean(si3_5b[0:j, 0:nn], axis=0))) print('\n')
def _refresh_initial_weights(x): _d_bt_bs, _d_bt_fr, _d_bt_iw, _d_bt_ig, _d_flag = x _d_bt_bs_dt = _d_bt_bs.listdir() ndt = len(_d_bt_bs_dt) nsig = 50 for j, i in enumerate(_d_bt_bs_dt): # j = 0 # i = _d_bt_bs_dt[j] xi_all = _d_bt_bs.load(i) xi = np.ascontiguousarray(xi_all['arr_0'].astype('float64')) if (_d_flag == 3) or (_d_flag == 5) or (_d_flag == 7): si = xi_all['arr_2'].astype('float64') # using QR else: si = xi_all['arr_1'].astype('float64') # using LR cieq = xi_all['arr_3'].astype('float64') cilq = xi_all['arr_4'].astype('float64') tci = xi_all['arr_5'] del xi_all n = xi.shape[1] ei = np.ones(n) if j < ndt-2: x2 = _d_bt_fr.load(i) x2[np.isnan(x2)] = 0 # get the risk budgets bieq = xi[25, :] bilq = xi[26, :] # adjustments for longer date signals rarely reaching upto potential si_ = si.copy()/vt for k in range(0, nsig): si_[k, :] *= sigp[k] si_ = 0.5*si_/(0.0625+si_**2) lmbeq = np.median(bieq/np.dot(cieq, ei)) lmblq = np.median(bilq/np.dot(cilq, ei)) flev = np.ascontiguousarray(xi[0, :]) # calculate the weights if (_d_flag == 0) or (_d_flag == 1) or (_d_flag == 2): si1_eq = lmbeq*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[9:12], xi[17:21])))) elif _d_flag == 3: si1_eq = lmbeq*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[9:11], xi[17:19])))) elif _d_flag == 4: si1_eq = lmbeq*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[9:12], xi[17:21])))) elif _d_flag == 5: si1_eq = lmbeq*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[9:12], xi[17:19])))) elif _d_flag == 6: si1_eq = lmbeq*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[9:12], xi[17:21])))) else: # _d_flag == 7 si1_eq = lmbeq*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[9:11], xi[17:19])))) if (_d_flag == 0) or (_d_flag == 1) or (_d_flag == 2): si1_lq = lmblq*np.dot(si_, crts.neut_corr_mat(cilq, np.vstack((xi[2:5, :], xi[9:12], xi[17:21])))) elif _d_flag == 3: si1_lq = lmblq*np.dot(si_, crts.neut_corr_mat(cilq, np.vstack((xi[2:5, :], xi[9:11], xi[17:19])))) elif _d_flag == 4: si1_lq = lmblq*np.dot(si_, crts.neut_corr_mat(cilq, np.vstack((xi[2:4, :], xi[9:12], xi[17:21])))) elif _d_flag == 5: si1_lq = lmblq*np.dot(si_, crts.neut_corr_mat(cilq, np.vstack((xi[2:5, :], xi[9:12], xi[17:19])))) elif _d_flag == 6: si1_lq = lmblq*np.dot(si_, crts.neut_corr_mat(cilq, np.vstack((xi[2, :], xi[9:12], xi[17:21])))) else: # _d_flag == 7 si1_lq = lmblq*np.dot(si_, crts.neut_corr_mat(cilq, np.vstack((xi[2, :], xi[9:11], xi[17:19])))) # also store the gross and net hi1_eq = np.dot(abs(si1_eq), flev) hi1_lq = np.dot(abs(si1_lq), flev) gi1_eq = np.dot(si1_eq, flev) gi1_lq = np.dot(si1_lq, flev) _d_bt_iw.save(i, si1_eq, si1_lq, tci) if j < ndt-2: ri1_eq = np.dot(si1_eq, x2) ri1_lq = np.dot(si1_lq, x2) _d_bt_ig.save(i, ri1_eq, ri1_lq, hi1_eq, hi1_lq, gi1_eq, gi1_lq) return None
def _refresh_fast_backtest(x): _d_bt_bs, _d_bt_fr, _d_flag = x _d_bt_bs_dt = _d_bt_bs.listdir() _d_bt_bs_dt = _d_bt_bs_dt[:-2] # drop the last two ndt = len(_d_bt_bs_dt) nsig = 50 ri1_1 = np.zeros([ndt, nsig]) gi1_1 = np.zeros([ndt, nsig]) hi1_1 = np.zeros([ndt, nsig]) for j, i in enumerate(_d_bt_bs_dt): # j = 0 # i = _d_bt_bs_dt[0] xi_all = _d_bt_bs.load(i) xi = np.ascontiguousarray(xi_all['arr_0'].astype('float64')) if (_d_flag == 3) or (_d_flag == 5) or (_d_flag == 7): si = xi_all['arr_2'].astype('float64') # using QR else: si = xi_all['arr_1'].astype('float64') # using LR cieq = xi_all['arr_3'].astype('float64') del xi_all n = xi.shape[1] ei = np.ones(n) x2 = _d_bt_fr.load(i) x2[np.isnan(x2)] = 0 # get the risk budgets bieq = xi[25, :] # adjustments for longer date signals rarely reaching upto potential si_ = si.copy()/vt for k in range(0, nsig): si_[k, :] *= sigp[k] si_ = 0.5*si_/(0.0625+si_**2) lmb0 = np.median(bieq/np.dot(cieq, ei)) # read the momentum and low volatility scores flev = np.ascontiguousarray(xi[0, :]) if (_d_flag == 0) or (_d_flag == 1) or (_d_flag == 2): si1_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[9:12], xi[17:21])))) elif _d_flag == 3: si1_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[9:11], xi[17:19])))) elif _d_flag == 4: si1_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[9:12], xi[17:21])))) elif _d_flag == 5: si1_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[9:12], xi[17:19])))) elif _d_flag == 6: si1_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[9:12], xi[17:21])))) else: # _d_flag == 7 si1_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[9:11], xi[17:19])))) ri1_1[j, :] = np.dot(si1_1, x2) hi1_1[j, :] = np.dot(abs(si1_1), flev) gi1_1[j, :] = abs(np.dot(si1_1, flev))/hi1_1[j, :] nn = 50 # if ((j != 0) and (j % 200 == 0)) or (j == ndt-1): if j == ndt-1: tn_1_1a = np.array(range(10, 510, 10)) v_1_1 = 16*np.diag(np.std(ri1_1[0:j, 0:nn], axis=0)) v_1_1a = np.linalg.inv(np.dot(v_1_1, np.dot(np.corrcoef(ri1_1[0:j, 0:nn].T)+0.1*np.identity(nn), v_1_1))) # v_1_1b = 1/tn_1_1a # v_1_1c = np.identity(nn)-np.dot(np.outer(v_1_1b, v_1_1b), v_1_1a)/np.dot(v_1_1b, np.dot(v_1_1a, v_1_1b)) # v_1_1 = np.dot(np.dot(v_1_1a, v_1_1b), np.mean(ri1_1[0:j, 0:nn], axis=0)) v_1_1 = np.dot(v_1_1a, np.mean(ri1_1[0:j, 0:nn], axis=0)) rc_1_1 = v_1_1/np.sum(v_1_1) rt_1_1 = np.dot(ri1_1, rc_1_1) ht_1_1 = np.dot(hi1_1, rc_1_1) gt_1_1 = np.dot(hi1_1*gi1_1, rc_1_1)/ht_1_1 tn_1_1 = 1/np.sum(abs(rc_1_1)/tn_1_1a) # print('Processing %s' % i) print('\n') print('____________') print('Days') print(_np_str(_np_push_front(tn_1_1, tn_1_1a))) print('Weights') print(_np_str(_np_push_front(1, rc_1_1))) print('Sharpe_Ratio') print(_np_str(16*np.mean(_np_align(rt_1_1[0:j], ri1_1[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_1_1[0:j], ri1_1[0:j, 0:nn]), axis=0))) print('Average_Gross') print(_np_str(np.mean(_np_align(ht_1_1[0:j], hi1_1[0:j, 0:nn]), axis=0))) print('Average_Net_/_Gross') print(_np_str(np.mean(_np_align(gt_1_1[0:j], gi1_1[0:j, 0:nn]), axis=0))) print('Average_Drawdown') print(_np_str(max_drawdown(_np_align(rt_1_1[0:j], ri1_1[0:j, 0:nn]))/np.std( _np_align(rt_1_1[0:j], ri1_1[0:j, 0:nn]))/16)) print('\n')
def _refresh_fast_backtest(x): _d_bt_bs, _d_bt_fr, _d_flag = x _d_bt_bs_dt = _d_bt_bs.listdir() _d_bt_bs_dt = _d_bt_bs_dt[:-2] # drop the last two ndt = len(_d_bt_bs_dt) nsig = 50 ri1_1 = np.zeros([ndt, nsig]) ri1_2 = np.zeros([ndt, nsig]) ri1_3 = np.zeros([ndt, nsig]) ri1_4 = np.zeros([ndt, nsig]) ri1_5 = np.zeros([ndt, nsig]) gi1_1 = np.zeros([ndt, nsig]) gi1_2 = np.zeros([ndt, nsig]) gi1_3 = np.zeros([ndt, nsig]) gi1_4 = np.zeros([ndt, nsig]) gi1_5 = np.zeros([ndt, nsig]) hi1_1 = np.zeros([ndt, nsig]) hi1_3 = np.zeros([ndt, nsig]) hi1_2 = np.zeros([ndt, nsig]) hi1_4 = np.zeros([ndt, nsig]) hi1_5 = np.zeros([ndt, nsig]) si1_1a = np.zeros([ndt, nsig]) si1_2a = np.zeros([ndt, nsig]) si1_3a = np.zeros([ndt, nsig]) si1_4a = np.zeros([ndt, nsig]) si1_5a = np.zeros([ndt, nsig]) si1_1b = np.zeros([ndt, nsig]) si1_2b = np.zeros([ndt, nsig]) si1_3b = np.zeros([ndt, nsig]) si1_4b = np.zeros([ndt, nsig]) si1_5b = np.zeros([ndt, nsig]) ri2_1 = np.zeros([ndt, nsig]) ri2_2 = np.zeros([ndt, nsig]) ri2_3 = np.zeros([ndt, nsig]) ri2_4 = np.zeros([ndt, nsig]) ri2_5 = np.zeros([ndt, nsig]) gi2_1 = np.zeros([ndt, nsig]) gi2_2 = np.zeros([ndt, nsig]) gi2_3 = np.zeros([ndt, nsig]) gi2_4 = np.zeros([ndt, nsig]) gi2_5 = np.zeros([ndt, nsig]) hi2_1 = np.zeros([ndt, nsig]) hi2_2 = np.zeros([ndt, nsig]) hi2_3 = np.zeros([ndt, nsig]) hi2_4 = np.zeros([ndt, nsig]) hi2_5 = np.zeros([ndt, nsig]) si2_1a = np.zeros([ndt, nsig]) si2_2a = np.zeros([ndt, nsig]) si2_3a = np.zeros([ndt, nsig]) si2_4a = np.zeros([ndt, nsig]) si2_5a = np.zeros([ndt, nsig]) si2_1b = np.zeros([ndt, nsig]) si2_2b = np.zeros([ndt, nsig]) si2_3b = np.zeros([ndt, nsig]) si2_4b = np.zeros([ndt, nsig]) si2_5b = np.zeros([ndt, nsig]) for j, i in enumerate(_d_bt_bs_dt): # j = 0 # i = _d_bt_bs_dt[0] # _d_flag = 0 xi_all = _d_bt_bs.load(i) xi = np.ascontiguousarray(xi_all['arr_0'].astype('float64')) if (_d_flag == 3) or (_d_flag == 5) or (_d_flag == 7): si = xi_all['arr_2'].astype('float64') # using QR sia = xi_all['arr_1'][0, :] else: si = xi_all['arr_1'].astype('float64') # using LR sia = xi_all['arr_2'][0, :] cieq = xi_all['arr_3'].astype('float64') del xi_all n = xi.shape[1] ei = np.ones(n) x2 = _d_bt_fr.load(i) x2[np.isnan(x2)] = 0 # get the risk budgets bieq = xi[25, :] # adjustments for longer date signals rarely reaching upto potential si_ = si.copy()/vt sia_ = sia.copy()/vt for k in range(0, nsig): si_[k, :] *= sigp[k] si_ = 0.5*si_/(0.0625+si_**2) # sia_ = (0.5*sia_/(0.0625+sia_**2))*sigp[0] lmb0 = np.median(bieq/np.dot(cieq, ei)) # read the momentum and low volatility scores flev = np.ascontiguousarray(xi[0, :]) fscm = xi[7, :] fscm = np.vstack((fscm**2, fscm**3)) # fscv = np.ascontiguousarray(np.maximum(xi[7, :], 0))**3 if (_d_flag == 0) or (_d_flag == 1) or (_d_flag == 2) or (_d_flag == 3) or (_d_flag == 5): si1_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, xi[2:5, :])) si2_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], fscm)))) elif (_d_flag == 6) or (_d_flag == 7): si1_1 = lmb0*np.dot(si_, crts.neut_corr_vec(cieq, xi[2, :])) si2_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], fscm)))) else: si1_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, xi[2:4, :])) si2_1 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], fscm)))) # calculate the 3m SP500 neutrality if (_d_flag == 0) or (_d_flag == 1) or (_d_flag == 2) or (_d_flag == 3) or (_d_flag == 5): si1_2 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[9:11, :])))) si2_2 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[9:11, :], fscm)))) elif (_d_flag == 6) or (_d_flag == 7): si1_2 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[9:11, :])))) si2_2 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[9:11, :], fscm)))) else: si1_2 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[9:11, :])))) si2_2 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[9:11, :], fscm)))) # calculate the 6m SP500 neutrality if (_d_flag == 0) or (_d_flag == 1) or (_d_flag == 2) or (_d_flag == 3) or (_d_flag == 5): si1_3 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[17:19, :])))) si2_3 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[17:19, :], fscm)))) elif (_d_flag == 6) or (_d_flag == 7): si1_3 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[17:19, :])))) si2_3 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[17:19, :], fscm)))) else: si1_3 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[17:19, :])))) si2_3 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[17:19, :], fscm)))) # calculate the 9m SP500 neutrality if (_d_flag == 0) or (_d_flag == 1) or (_d_flag == 2) or (_d_flag == 3) or (_d_flag == 5): si1_4 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[21:23, :])))) si2_4 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[21:23, :], fscm)))) elif (_d_flag == 6) or (_d_flag == 7): si1_4 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[21:23, :])))) si2_4 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[21:23, :], fscm)))) else: si1_4 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[21:23, :])))) si2_4 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[21:23, :], fscm)))) # calculate the 12m SP500 neutrality if (_d_flag == 0) or (_d_flag == 1) or (_d_flag == 2) or (_d_flag == 3) or (_d_flag == 5): si1_5 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[13:14, :])))) si2_5 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:5, :], xi[13:14, :], fscm)))) elif (_d_flag == 6) or (_d_flag == 7): si1_5 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[13:14, :])))) si2_5 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2, :], xi[13:14, :], fscm)))) else: si1_5 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[13:14, :])))) si2_5 = lmb0*np.dot(si_, crts.neut_corr_mat(cieq, np.vstack((xi[2:4, :], xi[13:14, :], fscm)))) ri1_1[j, :] = np.dot(si1_1, x2) ri1_2[j, :] = np.dot(si1_2, x2) ri1_3[j, :] = np.dot(si1_3, x2) ri1_4[j, :] = np.dot(si1_4, x2) ri1_5[j, :] = np.dot(si1_5, x2) ri2_1[j, :] = np.dot(si2_1, x2) ri2_2[j, :] = np.dot(si2_2, x2) ri2_3[j, :] = np.dot(si2_3, x2) ri2_4[j, :] = np.dot(si2_4, x2) ri2_5[j, :] = np.dot(si2_5, x2) hi1_1[j, :] = np.dot(abs(si1_1), flev) hi1_2[j, :] = np.dot(abs(si1_2), flev) hi1_3[j, :] = np.dot(abs(si1_3), flev) hi1_4[j, :] = np.dot(abs(si1_4), flev) hi1_5[j, :] = np.dot(abs(si1_5), flev) hi2_1[j, :] = np.dot(abs(si2_1), flev) hi2_2[j, :] = np.dot(abs(si2_2), flev) hi2_3[j, :] = np.dot(abs(si2_3), flev) hi2_4[j, :] = np.dot(abs(si2_4), flev) hi2_5[j, :] = np.dot(abs(si2_5), flev) gi1_1[j, :] = abs(np.dot(si1_1, flev))/hi1_1[j, :] gi1_2[j, :] = abs(np.dot(si1_2, flev))/hi1_2[j, :] gi1_3[j, :] = abs(np.dot(si1_3, flev))/hi1_3[j, :] gi1_4[j, :] = abs(np.dot(si1_4, flev))/hi1_4[j, :] gi1_5[j, :] = abs(np.dot(si1_5, flev))/hi1_5[j, :] gi2_1[j, :] = abs(np.dot(si2_1, flev))/hi2_1[j, :] gi2_2[j, :] = abs(np.dot(si2_2, flev))/hi2_2[j, :] gi2_3[j, :] = abs(np.dot(si2_3, flev))/hi2_3[j, :] gi2_4[j, :] = abs(np.dot(si2_4, flev))/hi2_4[j, :] gi2_5[j, :] = abs(np.dot(si2_5, flev))/hi2_5[j, :] nn = 50 # if ((j != 0) and (j % 200 == 0)) or (j == ndt-1): if j == ndt-1: ei_ = np.ones(nn) sh_1_1 = np.diag(1/(-16*np.mean(ri1_1[0:j, 0:nn], axis=0)/max_drawdown(ri1_1[0:j, 0:nn]))) sh_1_2 = np.diag(1/(-16*np.mean(ri1_2[0:j, 0:nn], axis=0)/max_drawdown(ri1_2[0:j, 0:nn]))) sh_1_3 = np.diag(1/(-16*np.mean(ri1_3[0:j, 0:nn], axis=0)/max_drawdown(ri1_3[0:j, 0:nn]))) sh_1_4 = np.diag(1/(-16*np.mean(ri1_4[0:j, 0:nn], axis=0)/max_drawdown(ri1_4[0:j, 0:nn]))) sh_1_5 = np.diag(1/(-16*np.mean(ri1_5[0:j, 0:nn], axis=0)/max_drawdown(ri1_5[0:j, 0:nn]))) sh_2_1 = np.diag(1/(-16*np.mean(ri2_1[0:j, 0:nn], axis=0)/max_drawdown(ri2_1[0:j, 0:nn]))) sh_2_2 = np.diag(1/(-16*np.mean(ri2_2[0:j, 0:nn], axis=0)/max_drawdown(ri2_2[0:j, 0:nn]))) sh_2_3 = np.diag(1/(-16*np.mean(ri2_3[0:j, 0:nn], axis=0)/max_drawdown(ri2_3[0:j, 0:nn]))) sh_2_4 = np.diag(1/(-16*np.mean(ri2_4[0:j, 0:nn], axis=0)/max_drawdown(ri2_4[0:j, 0:nn]))) sh_2_5 = np.diag(1/(-16*np.mean(ri2_5[0:j, 0:nn], axis=0)/max_drawdown(ri2_5[0:j, 0:nn]))) rc_1_1 = 1/np.dot(np.dot(sh_1_1, np.dot(np.corrcoef(ri1_1[0:j, 0:nn].T), sh_1_1)), ei_) rc_1_2 = 1/np.dot(np.dot(sh_1_2, np.dot(np.corrcoef(ri1_2[0:j, 0:nn].T), sh_1_2)), ei_) rc_1_3 = 1/np.dot(np.dot(sh_1_3, np.dot(np.corrcoef(ri1_3[0:j, 0:nn].T), sh_1_3)), ei_) rc_1_4 = 1/np.dot(np.dot(sh_1_4, np.dot(np.corrcoef(ri1_4[0:j, 0:nn].T), sh_1_4)), ei_) rc_1_5 = 1/np.dot(np.dot(sh_1_5, np.dot(np.corrcoef(ri1_5[0:j, 0:nn].T), sh_1_5)), ei_) rc_2_1 = 1/np.dot(np.dot(sh_2_1, np.dot(np.corrcoef(ri2_1[0:j, 0:nn].T), sh_2_1)), ei_) rc_2_2 = 1/np.dot(np.dot(sh_2_2, np.dot(np.corrcoef(ri2_2[0:j, 0:nn].T), sh_2_2)), ei_) rc_2_3 = 1/np.dot(np.dot(sh_2_3, np.dot(np.corrcoef(ri2_3[0:j, 0:nn].T), sh_2_3)), ei_) rc_2_4 = 1/np.dot(np.dot(sh_2_4, np.dot(np.corrcoef(ri2_4[0:j, 0:nn].T), sh_2_4)), ei_) rc_2_5 = 1/np.dot(np.dot(sh_2_5, np.dot(np.corrcoef(ri2_5[0:j, 0:nn].T), sh_2_5)), ei_) rc_1_1 /= np.sum(rc_1_1) rc_1_2 /= np.sum(rc_1_2) rc_1_3 /= np.sum(rc_1_3) rc_1_4 /= np.sum(rc_1_4) rc_1_5 /= np.sum(rc_1_5) rc_2_1 /= np.sum(rc_2_1) rc_2_2 /= np.sum(rc_2_2) rc_2_3 /= np.sum(rc_2_3) rc_2_4 /= np.sum(rc_2_4) rc_2_5 /= np.sum(rc_2_5) rt_1_1 = np.dot(ri1_1, rc_1_1) rt_1_2 = np.dot(ri1_2, rc_1_2) rt_1_3 = np.dot(ri1_3, rc_1_3) rt_1_4 = np.dot(ri1_4, rc_1_4) rt_1_5 = np.dot(ri1_5, rc_1_5) rt_2_1 = np.dot(ri2_1, rc_2_1) rt_2_2 = np.dot(ri2_2, rc_2_2) rt_2_3 = np.dot(ri2_3, rc_2_3) rt_2_4 = np.dot(ri2_4, rc_2_4) rt_2_5 = np.dot(ri2_5, rc_2_5) ht_1_1 = np.dot(hi1_1, rc_1_1) ht_1_2 = np.dot(hi1_2, rc_1_2) ht_1_3 = np.dot(hi1_3, rc_1_3) ht_1_4 = np.dot(hi1_4, rc_1_4) ht_1_5 = np.dot(hi1_5, rc_1_5) ht_2_1 = np.dot(hi2_1, rc_2_1) ht_2_2 = np.dot(hi2_2, rc_2_2) ht_2_3 = np.dot(hi2_3, rc_2_3) ht_2_4 = np.dot(hi2_4, rc_2_4) ht_2_5 = np.dot(hi2_5, rc_2_5) gt_1_1 = np.dot(hi1_1*gi1_1, rc_1_1)/ht_1_1 gt_1_2 = np.dot(hi1_2*gi1_2, rc_1_2)/ht_1_2 gt_1_3 = np.dot(hi1_3*gi1_3, rc_1_3)/ht_1_3 gt_1_4 = np.dot(hi1_4*gi1_4, rc_1_4)/ht_1_4 gt_1_5 = np.dot(hi1_5*gi1_5, rc_1_5)/ht_1_5 gt_2_1 = np.dot(hi2_1*gi2_1, rc_2_1)/ht_2_1 gt_2_2 = np.dot(hi2_2*gi2_2, rc_2_2)/ht_2_2 gt_2_3 = np.dot(hi2_3*gi2_3, rc_2_3)/ht_2_3 gt_2_4 = np.dot(hi2_4*gi2_4, rc_2_4)/ht_2_4 gt_2_5 = np.dot(hi2_5*gi2_5, rc_2_5)/ht_2_5 # print('Processing %s' % i) print('\n') print('Port_Cons:_GLS_Neutralization') print('____________') print('Sharpe_Ratio') print('B0:', _np_str(16*np.mean(_np_align(rt_1_1[0:j], ri1_1[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_1_1[0:j], ri1_1[0:j, 0:nn]), axis=0))) print('B1:', _np_str(16*np.mean(_np_align(rt_1_2[0:j], ri1_2[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_1_2[0:j], ri1_2[0:j, 0:nn]), axis=0))) print('B2:', _np_str(16*np.mean(_np_align(rt_1_3[0:j], ri1_3[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_1_3[0:j], ri1_3[0:j, 0:nn]), axis=0))) print('B3:', _np_str(16*np.mean(_np_align(rt_1_4[0:j], ri1_4[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_1_4[0:j], ri1_4[0:j, 0:nn]), axis=0))) print('B4:', _np_str(16*np.mean(_np_align(rt_1_5[0:j], ri1_5[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_1_5[0:j], ri1_5[0:j, 0:nn]), axis=0))) print('Average_Gross') print('B0:', _np_str(np.mean(_np_align(ht_1_1[0:j], hi1_1[0:j, 0:nn]), axis=0))) print('B1:', _np_str(np.mean(_np_align(ht_1_2[0:j], hi1_2[0:j, 0:nn]), axis=0))) print('B2:', _np_str(np.mean(_np_align(ht_1_3[0:j], hi1_3[0:j, 0:nn]), axis=0))) print('B3:', _np_str(np.mean(_np_align(ht_1_4[0:j], hi1_4[0:j, 0:nn]), axis=0))) print('B4:', _np_str(np.mean(_np_align(ht_1_5[0:j], hi1_5[0:j, 0:nn]), axis=0))) print('Average_Net_/_Gross') print('B0:', _np_str(np.mean(_np_align(gt_1_1[0:j], gi1_1[0:j, 0:nn]), axis=0))) print('B1:', _np_str(np.mean(_np_align(gt_1_2[0:j], gi1_2[0:j, 0:nn]), axis=0))) print('B2:', _np_str(np.mean(_np_align(gt_1_3[0:j], gi1_3[0:j, 0:nn]), axis=0))) print('B3:', _np_str(np.mean(_np_align(gt_1_4[0:j], gi1_4[0:j, 0:nn]), axis=0))) print('B4:', _np_str(np.mean(_np_align(gt_1_5[0:j], gi1_5[0:j, 0:nn]), axis=0))) print('Average_Drawdown') print('B0:', _np_str(max_drawdown(_np_align(rt_1_1[0:j], ri1_1[0:j, 0:nn]))/np.std( _np_align(rt_1_1[0:j], ri1_1[0:j, 0:nn]))/16)) print('B1:', _np_str(max_drawdown(_np_align(rt_1_2[0:j], ri1_2[0:j, 0:nn]))/np.std( _np_align(rt_1_2[0:j], ri1_2[0:j, 0:nn]))/16)) print('B2:', _np_str(max_drawdown(_np_align(rt_1_3[0:j], ri1_3[0:j, 0:nn]))/np.std( _np_align(rt_1_3[0:j], ri1_3[0:j, 0:nn]))/16)) print('B3:', _np_str(max_drawdown(_np_align(rt_1_4[0:j], ri1_4[0:j, 0:nn]))/np.std( _np_align(rt_1_4[0:j], ri1_4[0:j, 0:nn]))/16)) print('B4:', _np_str(max_drawdown(_np_align(rt_1_5[0:j], ri1_5[0:j, 0:nn]))/np.std( _np_align(rt_1_5[0:j], ri1_5[0:j, 0:nn]))/16)) print('\n') print('Port_Cons:_GLS_Neutralization_with_Momentum_Neutrality') print('Sharpe_Ratio') print('B0:', _np_str(16*np.mean(_np_align(rt_2_1[0:j], ri2_1[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_2_1[0:j], ri2_1[0:j, 0:nn]), axis=0))) print('B1:', _np_str(16*np.mean(_np_align(rt_2_2[0:j], ri2_2[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_2_2[0:j], ri2_2[0:j, 0:nn]), axis=0))) print('B2:', _np_str(16*np.mean(_np_align(rt_2_3[0:j], ri2_3[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_2_3[0:j], ri2_3[0:j, 0:nn]), axis=0))) print('B3:', _np_str(16*np.mean(_np_align(rt_2_4[0:j], ri2_4[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_2_4[0:j], ri2_4[0:j, 0:nn]), axis=0))) print('B4:', _np_str(16*np.mean(_np_align(rt_2_5[0:j], ri2_5[0:j, 0:nn]), axis=0)/np.std( _np_align(rt_2_5[0:j], ri2_5[0:j, 0:nn]), axis=0))) print('Average_Gross') print('B0:', _np_str(np.mean(_np_align(ht_2_1[0:j], hi2_1[0:j, 0:nn]), axis=0))) print('B1:', _np_str(np.mean(_np_align(ht_2_2[0:j], hi2_2[0:j, 0:nn]), axis=0))) print('B2:', _np_str(np.mean(_np_align(ht_2_3[0:j], hi2_3[0:j, 0:nn]), axis=0))) print('B3:', _np_str(np.mean(_np_align(ht_2_4[0:j], hi2_4[0:j, 0:nn]), axis=0))) print('B4:', _np_str(np.mean(_np_align(ht_2_5[0:j], hi2_5[0:j, 0:nn]), axis=0))) print('Average_Net_/_Gross') print('B0:', _np_str(np.mean(_np_align(gt_2_1[0:j], gi2_1[0:j, 0:nn]), axis=0))) print('B1:', _np_str(np.mean(_np_align(gt_2_2[0:j], gi2_2[0:j, 0:nn]), axis=0))) print('B2:', _np_str(np.mean(_np_align(gt_2_3[0:j], gi2_3[0:j, 0:nn]), axis=0))) print('B3:', _np_str(np.mean(_np_align(gt_2_4[0:j], gi2_4[0:j, 0:nn]), axis=0))) print('B4:', _np_str(np.mean(_np_align(gt_2_5[0:j], gi2_5[0:j, 0:nn]), axis=0))) print('Average_Drawdown') print('B0:', _np_str(max_drawdown(_np_align(rt_2_1[0:j], ri2_1[0:j, 0:nn]))/np.std( _np_align(rt_2_1[0:j], ri2_1[0:j, 0:nn]))/16)) print('B1:', _np_str(max_drawdown(_np_align(rt_2_2[0:j], ri2_2[0:j, 0:nn]))/np.std( _np_align(rt_2_2[0:j], ri2_2[0:j, 0:nn]))/16)) print('B2:', _np_str(max_drawdown(_np_align(rt_2_3[0:j], ri2_3[0:j, 0:nn]))/np.std( _np_align(rt_2_3[0:j], ri2_3[0:j, 0:nn]))/16)) print('B3:', _np_str(max_drawdown(_np_align(rt_2_4[0:j], ri2_4[0:j, 0:nn]))/np.std( _np_align(rt_2_4[0:j], ri2_4[0:j, 0:nn]))/16)) print('B4:', _np_str(max_drawdown(_np_align(rt_2_5[0:j], ri2_5[0:j, 0:nn]))/np.std( _np_align(rt_2_5[0:j], ri2_5[0:j, 0:nn]))/16)) print('\n')