Esempio n. 1
0
def _update_pca(x):
    window = 200
    _d_comb, _d_pca_rw = x
    comb_ret = _d_comb.retrieve('Returns')
    n = comb_ret.shape[0]
    tick_cols = comb_ret.tick_cols()
    f_list_cur = _d_pca_rw.listdir()
    f_dates_new = [i.__str__()[0:10].replace('-', '') for i in comb_ret['Date', list]]
    f_dates_new = [i for i in f_dates_new if i > max(f_list_cur)]
    f_dates_new_idx = [i for i, j in enumerate(comb_ret['Date', list]) if j.__str__()[0:10].replace('-', '') in
                       f_dates_new]
    for i in f_dates_new_idx:
        comb_ret_ext = comb_ret[i-window+1:i, :]
        y1 = np.all(~np.isnan(comb_ret_ext[tick_cols].values), axis=0).tolist()
        ticks = [k for k, j in zip(tick_cols, y1) if j]
        # handle cases where the return are zero all throughout
        comb_ret_ext__ = comb_ret_ext[ticks].values
        comb_ret_ext__sn = np.std(comb_ret_ext__, axis=0)
        bad_ticks_idx = list(np.where(comb_ret_ext__sn < 0.00001)[0])
        ticks = [j for k, j in enumerate(ticks) if k not in bad_ticks_idx]
        # now do the pca
        comb_ret_ext_ = np.transpose(comb_ret_ext[ticks].values)
        pca_wgt, pca_ret = crtp.compute_fast_pca(comb_ret_ext_)
        f_name = comb_ret_ext['Date', list][-1]
        f_name = f_name.__str__()[0:10].replace('-', '')
        _d_pca_rw.save(f_name, ticks, pca_wgt, pca_ret)
Esempio n. 2
0
def _refresh_pca(x):
    window = 200
    _d_comb, _d_pca_rw = x
    comb_ret = _d_comb.retrieve('Returns')
    n = comb_ret.shape[0]
    tick_cols = comb_ret.tick_cols()
    for i in range(window+1, n+1):  # new change here
        # i = 744
        comb_ret_ext = comb_ret[i-window:i-1, :]
        y1 = np.all(~np.isnan(comb_ret_ext[tick_cols].values), axis=0).tolist()
        ticks = [k for k, j in zip(tick_cols, y1) if j]
        # handle cases where the return are zero all throughout
        comb_ret_ext__ = comb_ret_ext[ticks].values
        comb_ret_ext__sn = np.std(comb_ret_ext__, axis=0)
        bad_ticks_idx = list(np.where(comb_ret_ext__sn < 0.00001)[0])
        ticks = [j for k, j in enumerate(ticks) if k not in bad_ticks_idx]
        if len(ticks) >= 50:
            # now do the pca
            comb_ret_ext_ = np.transpose(comb_ret_ext[ticks].values)
            pca_wgt, pca_ret = crtp.compute_fast_pca(comb_ret_ext_)
            f_name = comb_ret_ext['Date', list][-1]
            f_name = f_name.__str__()[0:10].replace('-', '')
            _d_pca_rw.save(f_name, ticks, pca_wgt, pca_ret)