Beispiel #1
0
def jensenfilter(funddf, indexdf, rf, ratio):

    funddfr = funddf.pct_change().fillna(0.0)
    indexdfr = indexdf.pct_change().fillna(0.0)


    jensen = {}
    cols = funddfr.columns
    for col in cols:
        p = []
        m = []
        rs = funddfr[col].values
        #print col, rs
        indexrs = indexdfr.values
        for i in range(0, len(rs)):
            if isnan(rs[i]):
                continue
            else:
                p.append(rs[i])
                m.append(indexrs[i])


        jensen[col] = fin.jensen(p, m, rf)


    x = jensen
    sorted_x = sorted(x.iteritems(), key=lambda x : x[1], reverse=True)
    sorted_jensen = sorted_x

    result = []
    for i in range(0, (int)(len(sorted_jensen) * ratio)):
        result.append(sorted_jensen[i])

    return result
Beispiel #2
0
def fund_jensen(funddf, indexdf):

    rf = 0.03 / 52

    funddfr = funddf.pct_change().fillna(0.0)
    indexdfr = indexdf.pct_change().fillna(0.0)

    jensen = {}
    cols = funddfr.columns
    for col in cols:
        p = []
        m = []
        rs = funddfr[col].values
        #print col, rs
        indexrs = indexdfr.values
        for i in range(0, len(rs)):
            if isnan(rs[i]):
                continue
            else:
                p.append(rs[i])
                m.append(indexrs[i])

        jensen[col] = fin.jensen(p, m, rf)

    x = jensen
    sorted_x = sorted(x.iteritems(), key=lambda x: x[1], reverse=True)
    sorted_measure = sorted_x

    return sorted_measure
def pool_by_corr_jensen(pool, day, lookback, limit):

    index = base_trade_dates.trade_date_lookback_index(end_date=day, lookback=lookback)

    start_date = index.min().strftime("%Y-%m-%d")
    end_date = day.strftime("%Y-%m-%d")


    ra_index_id = pool['ra_index_id']
    pool_id     = pool['id']
    df_nav_index = base_ra_index_nav.index_value(start_date, end_date, ra_index_id)
    #df_nav_index.index.name = str(df_nav_index.index.name)
    df_nav_index.columns = df_nav_index.columns.astype(str)
    if len(df_nav_index.index) == 0:
        return []
    pool_codes   = asset_ra_pool_sample.load(pool_id)['ra_fund_code'].values

    df_nav_fund  = base_ra_fund_nav.load_daily(start_date, end_date, codes = pool_codes)
    if len(df_nav_fund) == 0:
        return []

    df_nav_fund  = df_nav_fund.reindex(pd.date_range(df_nav_index.index[0], df_nav_index.index[-1]))
    df_nav_fund  = df_nav_fund.fillna(method = 'pad')
    df_nav_fund  = df_nav_fund.dropna(axis = 1)
    df_nav_fund  = df_nav_fund.loc[df_nav_index.index]
    fund_index_df = pd.concat([df_nav_index, df_nav_fund], axis = 1, join_axes = [df_nav_index.index])


    fund_index_corr_df = fund_index_df.pct_change().fillna(0.0).corr().fillna(0.0)

    corr = fund_index_corr_df[ra_index_id][1:]
    corr = corr.sort_values(ascending = False)

    code_jensen = {}
    for code in df_nav_fund.columns:
        jensen = fin.jensen(df_nav_fund[code].pct_change().fillna(0.0), df_nav_index[ra_index_id].pct_change().fillna(0.0) ,Const.rf)
        code_jensen.setdefault(code, jensen)


    if len(code_jensen) == 0:
        logger.info('No FUND')
        return None
    else:
        final_codes = []
        x = code_jensen
        sorted_x = sorted(iter(x.items()), key=lambda x : x[1], reverse=True)
        corr_threshold = np.percentile(corr.values, 80)
        corr_threshold = 0.7 if corr_threshold <= 0.7 else corr_threshold
        corr_threshold = 0.9 if corr_threshold >= 0.9 else corr_threshold
        for i in range(0, len(sorted_x)):
            code, jensen = sorted_x[i]
            if corr[code] >= corr_threshold:
                final_codes.append(code)

        final_codes = final_codes[0 : limit]
        return final_codes
def jensenmeasure(funddf, indexdf, rf):

    funddfr = funddf.pct_change().fillna(0.0)
    indexdfr = indexdf.pct_change().fillna(0.0)

    jensen = {}
    cols = funddfr.columns
    for col in cols:
        p = []
        m = []
        rs = funddfr[col].values
        #print col, rs
        indexrs = indexdfr.values
        for i in range(0, len(rs)):
            if isnan(rs[i]):
                continue
            else:
                p.append(rs[i])
                m.append(indexrs[i])

        jensen[col] = fin.jensen(p, m, rf)

    return jensen
indexrs = []
for i in range(1, len(indexvs)):
    indexrs.append(indexvs[i] / indexvs[i-1] - 1)
    


rf = 0.025 / 52
jensen = {}
sortino = {}
for code in fundrs.keys():
    rs = fundrs[code]

    if len(rs) < len(indexrs):
        continue

    jensen[code] = fin.jensen(rs, indexrs, rf)
    sortino[code] = fin.sortino(rs, rf)
    #print jensen[codes[i]]
    #print sortino[codes[i]]

#print jensen
#print sortino
#print dates


x = jensen
sorted_x = sorted(x.iteritems(), key=lambda x : x[1], reverse=True) 
sorted_jensen = sorted_x


x = sortino