def refresh_correl(_window=240): _cls = cr_cret.retrieve(univ_ib_eqidx_ext + 'Close') _vol = cr_cret.retrieve(univ_ib_eqidx_ext + 'vol_gk240') tcl = _cls.tick_cols() for i in tcl: _cls[i] = filt.ret(_cls[i].values)/_vol[i].values _corr = dummy_df(_cls) n = _corr.shape[0] # need to make this code parallel for j in range(0, len(tcl)-1): for k in range(j+1, len(tcl)): print('Processing rolling correlation of %s and %s' % (tcl[j], tcl[k])) _corr[tcl[j]+'_'+tcl[k]] = np.empty(n)*np.nan _tmp1 = _cls[tcl[k]].values _tmp2 = _cls[tcl[j]].values ny1 = np.maximum(filt.fst_nan(_tmp1), filt.fst_nan(_tmp2)) for _lbck in range(_window + ny1, n): _corr[_lbck, tcl[j]+'_'+tcl[k]] = bst.kendall_tau(_tmp1[_lbck-_window:_lbck], _tmp2[_lbck-_window:_lbck]) cr_cret.store(univ_ib_eqidx_ext+'Correl', _corr) return None
test1_all = test1.copy() test2_all = test2.copy() else: test1_all.row_bind(test1) test2_all.row_bind(test2) print('Processed ticker %s and size of dataframe is %s' % (i, str(test1_all.shape[0]))) del test1 del test2 fret1 = test1_all['Returns'].values num_col = len(tcl2) correl_vec = np.zeros(num_col) for z in range(0, num_col): correl_vec[z] = bst.kendall_tau(fret1, test2_all[tcl2[z]].values) _penalty = np.ones(num_col) for z in range(0, num_col): # z = 0 _tmp = tcl2[z] _tmp1 = int(_tmp[1:3]) _tmp2 = np.sum(np.array(smth_param(int(_tmp[4:6])))) _tmp3 = int(_tmp[7:10]) _tmp = _tmp1*_tmp2+_tmp3 _penalty[z] = np.sqrt(np.sqrt(_tmp)) for z2 in [0.55, 0.65, 0.75, 0.85]: print('Processing threshold = %s' % str(z2)) _mom_sig = [] _mom_sig_idx = []
# lag the signal test2 = mkt_retrieve(i, 'MovReg', 'Signals') tcl2 = test2.tick_cols() for k in tcl2: test2[k] = filt.lag(test2[k].values)/testv # get the average of returns test1_ = test1[['Date', 'Close']] test1_['Returns'] = filt.ret(test1_['Close'].values) fret1 = test1_['Returns'].values / testv num_col = len(tcl2) correl_vec = np.zeros(num_col) for z in range(0, num_col): correl_vec[z] = bst.kendall_tau(fret1, test2[tcl2[z]].values) _mom_sig = [] _mom_sig_idx = [] _mom_val = [] for z in range(0, 50): # z = 0 if z == 0: _idx = bst.argmax_fixed(correl_vec) _mom_sig.append(tcl2[_idx]) _mom_sig_idx.append(_idx) _mom_val.append(correl_vec[_idx]) else: scorrel_vec = np.ones(num_col, dtype=bool) for j1 in _mom_sig_idx: scorrel_vec[j1] = False
def test2(): return cub.kendall_tau(x, y)