Ejemplo n.º 1
0
def gradient_boosting_result(_ticker='SP500'):
    univ_ib_gd = cr_cret.retrieve(univ_ib_eqidx_ext + 'ExchOpen')[_ticker].values
    z = list(np.where(univ_ib_gd.astype('int') == 1)[0])

    _prc_names = ['CLS', 'A2D', 'A3D', 'A4D', 'A2W', 'A3W', 'A4W', 'A2M', 'A3M', 'A4M']
    _trn_names = ['LVL', 'CH0', 'CH1', 'CH2']
    _fil_names = ['LRB', 'QRB', 'QRG']
    _hoz_names = []
    for j in range(5, 305, 5):
        if j < 10:
            _hoz_names.append('00' + str(j))
        elif j < 100:
            _hoz_names.append('0' + str(j))
        else:
            _hoz_names.append(str(j))

    # get volatility forecast
    univ_ib_vl = cr_vol_all_adj.retrieve(univ_ib_eqidx_ext + 'vol_pb_120')[_ticker].values[z]

    # get return
    univ_ib_cl = cr_cret.retrieve(univ_ib_eqidx_ext + 'Close')[_ticker].values[z]
    univ_ib_cl = filt.ret(univ_ib_cl)

    univ_ib_sig_all = None
    for k in _trn_names:
        for i in _prc_names:
            for j in _fil_names:
                for mi, m in enumerate(_hoz_names):
                    fn = univ_ib_eqidx_ext+i+'_'+j+'_'+k+'_'+m
                    univ_ib_sig = cr_sig_mr_sg.retrieve(fn)[_ticker].values[z]
                    if univ_ib_sig_all is None:
                        univ_ib_sig_all = DataFrame({i+'_'+j+'_'+k+'_'+m: univ_ib_sig})
                    else:
                        univ_ib_sig_all.col_bind(DataFrame({i+'_'+j+'_'+k+'_'+m: univ_ib_sig}))

    # just for storage - incase something happens
    # univ_ib_gb = DataFrame({'Close': univ_ib_cl, 'Volatility': univ_ib_vl})
    # univ_ib_gb.col_bind(univ_ib_sig_all)
    # cr_cret.store(univ_ib_eqidx_ext + 'GBM', univ_ib_gb)

    reg_lookback = [120, 240, 360, 480]
    new_col_names = list(univ_ib_sig_all.columns)

    for i in new_col_names:
        # i = new_col_names[0]
        test_ = np.empty(0)
        for j in reg_lookback:
            # j = reg_lookback[0]
            uic = univ_ib_cl
            uiv = univ_ib_vl
            uis = filt.lag(univ_ib_sig_all[i].values, 1)
            uic, uis, uiv = reduce_nonnan(uic, uis, uiv)
            b1 = qreg.roll_e_ladreg_1d(uic, uis, j)
            b2 = qreg.roll_e_ladreg_1d(uic/uiv, uis/uiv, j)

            resid1 = uic - filt.lag(b1)*uis
            resid2 = uic - filt.lag(b2)*uis
            resid1, resid2 = reduce_nonnan(resid1, resid2)
            test_ = np.hstack((test_, np.array([np.median(abs(resid1)), np.median(abs(resid2))])))
        print(i+' : '+np_to_str(test_))
Ejemplo n.º 2
0
def get_vol_adj():
    univ_ib_cl = cr_cret.retrieve(univ_ib_eqidx_ext + 'Close')
    univ_ib_gd = cr_cret.retrieve(univ_ib_eqidx_ext + 'ExchOpen')
    tick_cols = univ_ib_cl.tick_cols()
    for k in range(0, len(vol_list)):
        print('Processing volatility %s' % vol_names[k])
        for j in vol_lookbacks:
            print('Currently working on lookback %s' % str(j))
            if j < 100:
                univ_ib_vol = cr_vol_all.retrieve(univ_ib_eqidx_ext + vol_names[k] + '_0' + str(j))
            else:
                univ_ib_vol = cr_vol_all.retrieve(univ_ib_eqidx_ext + vol_names[k] + '_' + str(j))
            for i in tick_cols:
                z = np.where(univ_ib_gd[i].values.astype('int') == 1)[0]
                if z.shape[0] > 0:
                    univ_ib_vol_ = univ_ib_vol[i].values[z]
                    univ_ib_cl_ = univ_ib_cl[i].values[z]
                    # find the beta
                    univ_ib_vol__ = filt.lag(univ_ib_vol_)
                    univ_ib_cl_ = np.abs(filt.ret(univ_ib_cl_))
                    regp = qreg4.roll_e_ladreg_1d(univ_ib_cl_, univ_ib_vol__, adj_lookback)
                    # get the beta-adjusted volatility
                    vol__ = univ_ib_vol_ * regp * _lapl_mult
                    # smooth out the volatility
                    vol__1 = filt.lrma(vol__, 61, lg=True)
                    vol__2 = filt.lrma(vol__, 7, lg=True)
                    vol__3 = vol__1 + filt.lrma(vol__2 - vol__1, 16)
                    # push the new volatility back
                    univ_ib_vol[i] = np.nan
                    univ_ib_vol[list(z), i] = vol__3
                    univ_ib_vol[i] = filt.fill(univ_ib_vol[i].values)
                else:
                    univ_ib_vol[i] = np.nan
            if j < 100:
                cr_vol_all_adj.store(univ_ib_eqidx_ext + vol_names[k] + '_0' + str(j), univ_ib_vol)
            else:
                cr_vol_all_adj.store(univ_ib_eqidx_ext + vol_names[k] + '_' + str(j), univ_ib_vol)
    return None
Ejemplo n.º 3
0
tickers = ['SP500', 'DAX', 'Nikkei225', 'ESTX50', 'SMI', 'RDX', 'MSCIEM']

i = 'SP500'
univ_ib_cl = mkt_retrieve(i, 'Stats', 'Returns')['Close'].values
univ_ib_vl = mkt_retrieve(i, 'Stats', 'Volatility')['vol_pb240'].values

univ_ib_cl = np.abs(filt.ret(univ_ib_cl))
univ_ib_vl = filt.lag(univ_ib_vl)

univ_ib_cl, univ_ib_vl = reduce_nonnan(univ_ib_cl, univ_ib_vl )

univ_ib_vl *= _lapl_mult
univ_ib_vl2 = np.sqrt(univ_ib_vl)
univ_ib_vl2 = univ_ib_vl2 * med_abs_dev(univ_ib_vl)/med_abs_dev(univ_ib_vl2)

b41 = qreg.roll_e_ladreg_1d(univ_ib_cl, univ_ib_vl, 240)

b51 = qreg.roll_e_ladreg_2d(univ_ib_cl, mcc(univ_ib_vl, univ_ib_vl2), 240)

resid0 = univ_ib_cl - univ_ib_vl
resid1 = univ_ib_cl - univ_ib_vl * filt.lag(b41)
resid2 = univ_ib_cl - univ_ib_vl * filt.lag(np.ascontiguousarray(b51[:, 0])) - univ_ib_vl2 * filt.lag(np.ascontiguousarray(b51[:, 1]))

resid0, resid1, resid2 = reduce_nonnan(resid0, resid1, resid2)

print(np.median(np.abs(resid0)))
print(np.median(np.abs(resid1)))
print(np.median(np.abs(resid2)))

# f = pyl.figure(1)
# pyl.subplot(3, 1, 1)