Ejemplo n.º 1
0
    def testGetMultiIndexData(self):
        index = pd.MultiIndex.from_arrays([[
            datetime(2015, 1, 1),
            datetime(2015, 1, 2),
            datetime(2015, 1, 2),
            datetime(2015, 2, 4),
            datetime(2015, 2, 4)
        ], ['A', 'B', 'C', 'A', 'C']],
                                          names=['date', 'category'])
        multi = pd.Series([1.0, 2.0, 3.0, 4.0, 3.0], index=index)

        calculated = get_multi_index_data(multi, 'date', datetime(2015, 1, 2))
        expected = pd.Series([2.0, 3.0],
                             index=pd.MultiIndex.from_arrays(
                                 [[datetime(2015, 1, 2),
                                   datetime(2015, 1, 2)], ['B', 'C']],
                                 names=['date', 'category']))
        pd.util.testing.assert_series_equal(calculated, expected)

        calculated = get_multi_index_data(multi, 'date',
                                          [datetime(2015, 1, 2)])
        pd.util.testing.assert_series_equal(calculated, expected)

        calculated = get_multi_index_data(multi, 'date', datetime(2015, 1, 2),
                                          'category', 'B')
        expected = pd.Series([2.0],
                             index=pd.MultiIndex.from_tuples(
                                 [(datetime(2015, 1, 2), 'B')],
                                 names=['date', 'category']))
        pd.util.testing.assert_series_equal(calculated, expected)

        calculated = get_multi_index_data(
            multi, 'date',
            [datetime(2015, 1, 2), datetime(2015, 2, 4)], 'category', 'C')
        expected = pd.Series([3.0, 3.0],
                             index=pd.MultiIndex.from_product(
                                 [[datetime(2015, 1, 2),
                                   datetime(2015, 2, 4)], ['C']],
                                 names=['date', 'category']))
        pd.util.testing.assert_series_equal(calculated, expected)

        calculated = get_multi_index_data(
            multi, 'date',
            [datetime(2015, 1, 2), datetime(2015, 2, 4)], 'category',
            ['B', 'C'])
        expected = pd.Series([2.0, 3.0, 3.0],
                             index=pd.MultiIndex.from_arrays(
                                 [[
                                     datetime(2015, 1, 2),
                                     datetime(2015, 1, 2),
                                     datetime(2015, 2, 4)
                                 ], ['B', 'C', 'C']],
                                 names=['date', 'category']))
        pd.util.testing.assert_series_equal(calculated, expected)
Ejemplo n.º 2
0
 def get_sec_return_on_date(cls, sec_return, sec_ids, date):
     """
     :param sec_return: pd.Series, multindex = [tiaoCangdate, seciD]
     :param sec_ids: list of sec ids
     :param date: datetime, 调仓日
     :return: pd.Series, index = sec ID
     给定某一时间和股票代码列表, 返回前一个调仓日至当前调仓日的股票收益
     """
     data = get_multi_index_data(sec_return, 'tiaoCangDate', date, 'secID',
                                 sec_ids)
     data = data.reset_index().drop('tiaoCangDate', axis=1)
     data = data.set_index('secID')
     return data
Ejemplo n.º 3
0
 def seperate_sec_group(cls, factor, date):
     """
     :param date: datetime, 调仓日
     :param factor: multi index pd.Series, 情景分层因子
     :return: list
     给定某一时间,按因子把股票分为数量相同的两组(大/小)
     """
     data = get_multi_index_data(factor, 'tiaoCangDate', date)
     # 按分层因子值从小到大排序
     data.sort_values(ascending=True, inplace=True)
     sec_ids = data.index.get_level_values('secID').tolist()
     # 分组,因子值小的哪一组股票为low,高的为high
     group_low = sec_ids[:np.round(len(data)) / 2]
     group_high = sec_ids[np.round(len(data)) / 2:]
     return group_low, group_high
Ejemplo n.º 4
0
    def testUpdateWeightAfterFilter(self):
        filtered = self.get_filtered_result('secID_1', 'filters_1', 'secID',
                                            'filters')
        weight = get_multi_index_data(self.portfolio._sec_selected,
                                      'tiaoCangDate', datetime(2012, 8, 31),
                                      'secID', filtered.index.values)
        weight = weight.reset_index().set_index('secID')
        weight = weight[['weight', 'INDUSTRY']]
        calculated = self.portfolio._update_weight_after_filter(
            weight, filtered)

        expected = self.get_filtered_result(
            'secID_4', ['weight_4', 'INDUSTRY', 'filters_4'], 'secID',
            ['weight', 'INDUSTRY', 'filters'])
        assert_frame_equal(calculated, expected)
Ejemplo n.º 5
0
 def calc_layer_factor_quantile_on_date(self, date):
     """
     :param date: datetime, 调仓日
     :return: pd.DataFrame, index=secid, col = layerFactorNames
     """
     ret = pd.DataFrame()
     for layerFactor in self._layerFactor:
         data = get_multi_index_data(layerFactor, 'tiaoCangDate', date)
         sec_ids = data.index.get_level_values('secID').tolist()
         # 由低至高排序
         rank = data.rank(ascending=True)
         rank = rank.divide(len(sec_ids))
         ret = pd.concat([ret, pd.Series(rank.values, index=sec_ids)],
                         axis=1)
     ret.columns = self._layerFactorNames
     return ret
Ejemplo n.º 6
0
 def get_factor_on_date(cls, factors, sec_ids, date):
     """
     :param factors: list, element = pd.Series, multiindex = [tiaoCangdate, seci]
     :param sec_ids: list of sec ids
     :param date: datetime, 调仓日
     :return: pd.DataFrame, index = sec_ids, col = [factor]
     给定某一时间, 和股票代码列表, 返回因子列表
     """
     ret = pd.DataFrame()
     for i in range(len(factors)):
         data = get_multi_index_data(factors[i], 'tiaoCangDate', date,
                                     'secID', sec_ids)
         data = data.reset_index().drop('tiaoCangDate', axis=1)
         data = data.set_index('secID')
         ret = pd.concat([ret, data], axis=1)
     ret.columns = [factor.name for factor in factors]
     return ret
Ejemplo n.º 7
0
    def testGetQuantity(self):
        init_ptf_value = 10000000
        filtered = self.filtered[['filters_1',
                                  'secID_1']].dropna().set_index('secID_1')
        weight = get_multi_index_data(self.portfolio._sec_selected,
                                      'tiaoCangDate', datetime(2012, 8, 31),
                                      'secID', filtered.index.values)
        weight = weight.reset_index().set_index('secID')
        weight = weight[['weight', 'INDUSTRY']]
        price = self.price_data.loc[datetime(2012, 8, 6)]
        price.name = 'price'
        calculated = self.portfolio._get_quantity(init_ptf_value, weight,
                                                  price)

        expected = self.get_filtered_result('secID_3', 'quantity', 'secID',
                                            'quantity')
        expected = expected.astype('int64')
        assert_series_equal(calculated, expected)
Ejemplo n.º 8
0
    def calc_sec_score_on_date(self, date):
        """
        :param date: datetime, tiaoCangDate
        :return: pd.Series, index = secID, cols = score, industry
        给定调仓日, 返回股票打分列表
        """
        alpha_weight_low, alpha_weight_high = self.calc_alpha_factor_weight_on_date(
            date)
        alpha_factor_rank = self.calc_alpha_factor_rank_on_date(
            date, alpha_weight_low, alpha_weight_high)
        layer_factor_quantile = self.calc_layer_factor_quantile_on_date(date)
        sec_ids = layer_factor_quantile.index.tolist()
        ret = pd.Series(index=sec_ids, name=date)
        for secID in sec_ids:
            # 提取secID对应的alphaFactorRank DataFrame, index = [layerFactor, high/low], col = alphaFactor
            factor_rank_matrix = get_multi_index_data(alpha_factor_rank,
                                                      'secID', secID)
            weighted_rank = 0.0
            for layerFactor in factor_rank_matrix.index.get_level_values(
                    'layerFactor'):
                factor_rank_on_layer_factor = factor_rank_matrix.loc[
                    factor_rank_matrix.index.get_level_values(
                        'layerFactor') == layerFactor]
                rank = factor_rank_on_layer_factor.values.flatten()
                low_high = factor_rank_on_layer_factor.index.get_level_values(
                    'low_high')
                # 权重取绝对值
                weight = abs(alpha_weight_low.loc[layerFactor].values
                             ) if low_high == 'low' else abs(
                                 alpha_weight_high.loc[layerFactor].values)
                # normalize
                # weight = [i / weight.sum() for i in weight]
                layer_factor_quantile_to_use = layer_factor_quantile[
                    layerFactor][secID]
                weighted_rank += np.dot(weight, rank) * abs(
                    self.calc_layer_factor_distance(
                        layer_factor_quantile_to_use))
            ret[secID] = weighted_rank

        return ret