def test_selector(self): index_weight = pd.MultiIndex.from_product([[dt(2014, 1, 30), dt(2014, 2, 28)], ['a', 'b', 'other']], names=INDEX_INDUSTRY_WEIGHT.full_index) industry_weight = pd.DataFrame([0.5, 0.4, 0.1, 0.5, 0.3, 0.2], index=index_weight) index = pd.MultiIndex.from_product([['2014-01-30', '2014-02-28'], ['001', '002', '003', '004', '005']], names=INDEX_FACTOR.full_index) X = pd.DataFrame({'score': [2, 3, 3, 8, 4, 5, 9, 11, 2, 0], 'industry_code': ['a', 'a', 'a', 'b', 'b', 'a', 'a', 'other', 'b', 'b']}, index=index) score = Factor(data=X['score'], name='score', property_dict={'type': FactorType.SCORE}) industry_code = Factor(data=X['industry_code'], name='industry_code', property_dict={'type': FactorType.INDUSTY_CODE}) fc = FactorContainer(start_date='2014-01-30', end_date='2014-02-28') fc.add_factor(score) fc.add_factor(industry_code) calculated = Selector(industry_weight=industry_weight, method=SelectionMethod.INDUSTRY_NEUTRAL).fit(fc).predict(fc) index_exp = pd.MultiIndex.from_arrays( [[dt(2014, 1, 30), dt(2014, 1, 30), dt(2014, 1, 30), dt(2014, 1, 30), dt(2014, 2, 28), dt(2014, 2, 28), dt(2014, 2, 28), dt(2014, 2, 28), dt(2014, 2, 28)], ['002', '003', '004', '005', '002', '001', '004', '005', '003']], names=['trade_date', 'ticker']) expected = pd.DataFrame({'score': [3, 3, 8, 4, 9, 5, 2, 0, 11], 'industry_code': ['a', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'other'], 'weight': [0.25, 0.25, 0.2, 0.2, 0.25, 0.25, 0.15, 0.15, 0.2]}, index=index_exp, dtype=object) expected = expected[['score', 'industry_code', 'weight']] assert_frame_equal(calculated, expected)
def test_imputer_2(self): index = pd.MultiIndex.from_product( [['2014-01-30', '2014-02-28'], ['001', '002', '003', '004']], names=['trade_date', 'ticker']) data = pd.DataFrame(index=index, data=[1.0, 3.0, 3.0, np.nan, 5.0, 5.0, 6.0, 8.0]) factor_test = Factor(data=data, name='test1') index = pd.MultiIndex.from_product( [[dt(2014, 1, 30), dt(2014, 2, 28)], ['001', '002', '003', '004']], names=['trade_date', 'ticker']) fi = FactorImputer(numerical_strategy=NAStrategy.MEAN) calculated = fi.fit_transform(factor_test) expected = pd.DataFrame( {'test1': [1.0, 3.0, 3.0, 2.33333333333, 5.0, 5.0, 6.0, 8.0]}, index=index) assert_frame_equal(calculated, expected) fi.set_out_container(True) calculated = fi.fit_transform(factor_test) expected = FactorContainer(start_date='2014-01-30', end_date='2014-02-28') factor = Factor(data=pd.DataFrame( {'test1': [1.0, 3.0, 3.0, 2.33333333333, 5.0, 5.0, 6.0, 8.0]}, index=index), name='test1') expected.add_factor(factor) assert (isinstance(calculated, FactorContainer)) self.assertEqual(calculated.property, expected.property) assert_frame_equal(calculated.data, expected.data)
def test_selector(self): index_weight = pd.MultiIndex.from_product( [[dt(2014, 1, 30), dt(2014, 2, 28)], ['a', 'b', 'other']], names=INDEX_INDUSTRY_WEIGHT.full_index) industry_weight = pd.DataFrame([0.5, 0.4, 0.1, 0.5, 0.3, 0.2], index=index_weight) index = pd.MultiIndex.from_product( [['2014-01-30', '2014-02-28'], ['001', '002', '003', '004', '005'] ], names=INDEX_FACTOR.full_index) X = pd.DataFrame( { 'score': [2, 3, 3, 8, 4, 5, 9, 11, 2, 0], 'industry_code': ['a', 'a', 'a', 'b', 'b', 'a', 'a', 'other', 'b', 'b'] }, index=index) score = Factor(data=X['score'], name='score', property_dict={'type': FactorType.SCORE}) industry_code = Factor(data=X['industry_code'], name='industry_code', property_dict={'type': FactorType.INDUSTY_CODE}) fc = FactorContainer(start_date='2014-01-30', end_date='2014-02-28') fc.add_factor(score) fc.add_factor(industry_code) calculated = Selector( industry_weight=industry_weight, method=SelectionMethod.INDUSTRY_NEUTRAL).fit(fc).predict(fc) index_exp = pd.MultiIndex.from_arrays([[ dt(2014, 1, 30), dt(2014, 1, 30), dt(2014, 1, 30), dt(2014, 1, 30), dt(2014, 2, 28), dt(2014, 2, 28), dt(2014, 2, 28), dt(2014, 2, 28), dt(2014, 2, 28) ], ['002', '003', '004', '005', '002', '001', '004', '005', '003']], names=['trade_date', 'ticker']) expected = pd.DataFrame( { 'score': [3, 3, 8, 4, 9, 5, 2, 0, 11], 'industry_code': ['a', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'other'], 'weight': [0.25, 0.25, 0.2, 0.2, 0.25, 0.25, 0.15, 0.15, 0.2] }, index=index_exp, dtype=object) expected = expected[['score', 'industry_code', 'weight']] assert_frame_equal(calculated, expected)
def test_factor_container_1(self): index = pd.MultiIndex.from_product([['2014-01-30', '2014-02-28', '2014-03-31'], ['001', '002']], names=['trade_date', 'ticker']) data1 = pd.DataFrame(index=index, data=[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]) factor_test1 = Factor(data=data1, name='test1') data2 = pd.DataFrame(index=index, data=[3.0, 2.0, 3.0, 7.0, 8.0, 9.0]) factor_test2 = Factor(data=data2, name='test2') data3 = pd.DataFrame(index=index, data=[3.0, 4.0, 3.0, 5.0, 6.0, 7.0]) factor_test3 = Factor(data=data3, name='test3') fc = FactorContainer('2014-01-30', '2014-02-28', [factor_test1, factor_test2]) index_exp = pd.MultiIndex.from_product([[dt(2014, 1, 30), dt(2014, 2, 28)], ['001', '002']], names=['trade_date', 'ticker']) data_exp = pd.DataFrame({'test1': [1.0, 2.0, 3.0, 4.0], 'test2': [3.0, 2.0, 3.0, 7.0]}, index=index_exp) assert_frame_equal(fc.data, data_exp) fc.add_factor(factor_test3) data_exp = pd.DataFrame({'test1': [1.0, 2.0, 3.0, 4.0], 'test2': [3.0, 2.0, 3.0, 7.0], 'test3': [3.0, 4.0, 3.0, 5.0]}, index=index_exp) assert_frame_equal(fc.data, data_exp) fc.remove_factor(factor_test2) data_exp = pd.DataFrame({'test1': [1.0, 2.0, 3.0, 4.0], 'test3': [3.0, 4.0, 3.0, 5.0]}, index=index_exp) assert_frame_equal(fc.data, data_exp) property_exp = {'test1': {'type': FactorType.ALPHA_FACTOR, 'data_format': OutputDataFormat.MULTI_INDEX_DF, 'norm_type': FactorNormType.Null, 'freq': FreqType.EOM}, 'test3': {'type': FactorType.ALPHA_FACTOR, 'data_format': OutputDataFormat.MULTI_INDEX_DF, 'norm_type': FactorNormType.Null, 'freq': FreqType.EOM}} self.assertEqual(fc.property, property_exp) fc.replace_data(np.array([[1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 5.0]]).T) assert_frame_equal(fc.data, pd.DataFrame({'test1': [1.0, 2.0, 3.0, 4.0], 'test3': [1.0, 2.0, 3.0, 5.0]}, index=index_exp)) self.assertEqual(list(fc.alpha_factor_col), ['test1', 'test3'])
def test_imputer_1(self): index = pd.MultiIndex.from_product([['2014-01-30', '2014-02-28'], ['001', '002', '003', '004']], names=['trade_date', 'ticker']) data1 = pd.DataFrame(index=index, data=[1.0, 3.0, 3.0, np.nan, 5.0, 5.0, 6.0, 8.0]) factor_test1 = Factor(data=data1, name='test1') data2 = pd.DataFrame(index=index, data=[3.0, 2.0, 3.0, 7.0, 7.0, np.nan, 6.0, 6.0]) factor_test2 = Factor(data=data2, name='test2') data3 = pd.DataFrame(index=index, data=[3.0, 3.0, np.nan, 5.0, 6.0, 7.0, 6.0, 6.0]) factor_test3 = Factor(data=data3, name='test3') fc = FactorContainer('2014-01-30', '2014-02-28', [factor_test1, factor_test2, factor_test3]) index = pd.MultiIndex.from_product([[dt(2014, 1, 30), dt(2014, 2, 28)], ['001', '002', '003', '004']], names=['trade_date', 'ticker']) calculated = FactorImputer(numerical_strategy=NAStrategy.MOST_FREQ).fit_transform(fc) expected = pd.DataFrame({'test1': [1.0, 3.0, 3.0, 3.0, 5.0, 5.0, 6.0, 8.0], 'test2': [3.0, 2.0, 3.0, 7.0, 7.0, 6.0, 6.0, 6.0], 'test3': [3.0, 3.0, 3.0, 5.0, 6.0, 7.0, 6.0, 6.0]}, index=index) assert_frame_equal(calculated, expected) calculated = FactorImputer(numerical_strategy=NAStrategy.MEDIAN).fit_transform(fc) expected = pd.DataFrame({'test1': [1.0, 3.0, 3.0, 3.0, 5.0, 5.0, 6.0, 8.0], 'test2': [3.0, 2.0, 3.0, 7.0, 7.0, 6.0, 6.0, 6.0], 'test3': [3.0, 3.0, 3.0, 5.0, 6.0, 7.0, 6.0, 6.0]}, index=index) assert_frame_equal(calculated, expected) industry = pd.DataFrame(index=index, data=['a', 'a', 'a', 'a', 'a', 'a', np.nan, 'a']) factor_industry = Factor(data=industry, name='industry', property_dict={'type': FactorType.INDUSTY_CODE}) fc.add_factor(factor=factor_industry) calculated = FactorImputer(numerical_strategy=NAStrategy.MEDIAN, categorical_strategy=NAStrategy.CUSTOM, custom_value='other').fit_transform(fc) calculated.sort_index(axis=1, inplace=True) expected = pd.DataFrame({'test1': [1.0, 3.0, 3.0, 3.0, 5.0, 5.0, 6.0, 8.0], 'test2': [3.0, 2.0, 3.0, 7.0, 7.0, 6.0, 6.0, 6.0], 'test3': [3.0, 3.0, 3.0, 5.0, 6.0, 7.0, 6.0, 6.0], 'industry': ['a', 'a', 'a', 'a', 'a', 'a', 'other', 'a']}, index=index, dtype=object) assert_frame_equal(calculated, expected)
def test_imputer_2(self): index = pd.MultiIndex.from_product([['2014-01-30', '2014-02-28'], ['001', '002', '003', '004']], names=['trade_date', 'ticker']) data = pd.DataFrame(index=index, data=[1.0, 3.0, 3.0, np.nan, 5.0, 5.0, 6.0, 8.0]) factor_test = Factor(data=data, name='test1') index = pd.MultiIndex.from_product([[dt(2014, 1, 30), dt(2014, 2, 28)], ['001', '002', '003', '004']], names=['trade_date', 'ticker']) fi = FactorImputer(numerical_strategy=NAStrategy.MEAN) calculated = fi.fit_transform(factor_test) expected = pd.DataFrame({'test1': [1.0, 3.0, 3.0, 2.33333333333, 5.0, 5.0, 6.0, 8.0]}, index=index) assert_frame_equal(calculated, expected) fi.set_out_container(True) calculated = fi.fit_transform(factor_test) expected = FactorContainer(start_date='2014-01-30', end_date='2014-02-28') factor = Factor(data=pd.DataFrame({'test1': [1.0, 3.0, 3.0, 2.33333333333, 5.0, 5.0, 6.0, 8.0]}, index=index), name='test1') expected.add_factor(factor) assert (isinstance(calculated, FactorContainer)) self.assertEqual(calculated.property, expected.property) assert_frame_equal(calculated.data, expected.data)
def test_factor_container_1(self): index = pd.MultiIndex.from_product( [['2014-01-30', '2014-02-28', '2014-03-31'], ['001', '002']], names=['trade_date', 'ticker']) data1 = pd.DataFrame(index=index, data=[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]) factor_test1 = Factor(data=data1, name='test1') data2 = pd.DataFrame(index=index, data=[3.0, 2.0, 3.0, 7.0, 8.0, 9.0]) factor_test2 = Factor(data=data2, name='test2') data3 = pd.DataFrame(index=index, data=[3.0, 4.0, 3.0, 5.0, 6.0, 7.0]) factor_test3 = Factor(data=data3, name='test3') fc = FactorContainer('2014-01-30', '2014-02-28', [factor_test1, factor_test2]) index_exp = pd.MultiIndex.from_product( [[dt(2014, 1, 30), dt(2014, 2, 28)], ['001', '002']], names=['trade_date', 'ticker']) data_exp = pd.DataFrame( { 'test1': [1.0, 2.0, 3.0, 4.0], 'test2': [3.0, 2.0, 3.0, 7.0] }, index=index_exp) assert_frame_equal(fc.data, data_exp) fc.add_factor(factor_test3) data_exp = pd.DataFrame( { 'test1': [1.0, 2.0, 3.0, 4.0], 'test2': [3.0, 2.0, 3.0, 7.0], 'test3': [3.0, 4.0, 3.0, 5.0] }, index=index_exp) assert_frame_equal(fc.data, data_exp) fc.remove_factor(factor_test2) data_exp = pd.DataFrame( { 'test1': [1.0, 2.0, 3.0, 4.0], 'test3': [3.0, 4.0, 3.0, 5.0] }, index=index_exp) assert_frame_equal(fc.data, data_exp) property_exp = { 'test1': { 'type': FactorType.ALPHA_FACTOR, 'data_format': OutputDataFormat.MULTI_INDEX_DF, 'norm_type': FactorNormType.Null, 'freq': FreqType.EOM }, 'test3': { 'type': FactorType.ALPHA_FACTOR, 'data_format': OutputDataFormat.MULTI_INDEX_DF, 'norm_type': FactorNormType.Null, 'freq': FreqType.EOM } } self.assertEqual(fc.property, property_exp) fc.replace_data( np.array([[1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 5.0]]).T) assert_frame_equal( fc.data, pd.DataFrame( { 'test1': [1.0, 2.0, 3.0, 4.0], 'test3': [1.0, 2.0, 3.0, 5.0] }, index=index_exp)) self.assertEqual(list(fc.alpha_factor_col), ['test1', 'test3'])
def test_imputer_1(self): index = pd.MultiIndex.from_product( [['2014-01-30', '2014-02-28'], ['001', '002', '003', '004']], names=['trade_date', 'ticker']) data1 = pd.DataFrame(index=index, data=[1.0, 3.0, 3.0, np.nan, 5.0, 5.0, 6.0, 8.0]) factor_test1 = Factor(data=data1, name='test1') data2 = pd.DataFrame(index=index, data=[3.0, 2.0, 3.0, 7.0, 7.0, np.nan, 6.0, 6.0]) factor_test2 = Factor(data=data2, name='test2') data3 = pd.DataFrame(index=index, data=[3.0, 3.0, np.nan, 5.0, 6.0, 7.0, 6.0, 6.0]) factor_test3 = Factor(data=data3, name='test3') fc = FactorContainer('2014-01-30', '2014-02-28', [factor_test1, factor_test2, factor_test3]) index = pd.MultiIndex.from_product( [[dt(2014, 1, 30), dt(2014, 2, 28)], ['001', '002', '003', '004']], names=['trade_date', 'ticker']) calculated = FactorImputer( numerical_strategy=NAStrategy.MOST_FREQ).fit_transform(fc) expected = pd.DataFrame( { 'test1': [1.0, 3.0, 3.0, 3.0, 5.0, 5.0, 6.0, 8.0], 'test2': [3.0, 2.0, 3.0, 7.0, 7.0, 6.0, 6.0, 6.0], 'test3': [3.0, 3.0, 3.0, 5.0, 6.0, 7.0, 6.0, 6.0] }, index=index) assert_frame_equal(calculated, expected) calculated = FactorImputer( numerical_strategy=NAStrategy.MEDIAN).fit_transform(fc) expected = pd.DataFrame( { 'test1': [1.0, 3.0, 3.0, 3.0, 5.0, 5.0, 6.0, 8.0], 'test2': [3.0, 2.0, 3.0, 7.0, 7.0, 6.0, 6.0, 6.0], 'test3': [3.0, 3.0, 3.0, 5.0, 6.0, 7.0, 6.0, 6.0] }, index=index) assert_frame_equal(calculated, expected) industry = pd.DataFrame( index=index, data=['a', 'a', 'a', 'a', 'a', 'a', np.nan, 'a']) factor_industry = Factor( data=industry, name='industry', property_dict={'type': FactorType.INDUSTY_CODE}) fc.add_factor(factor=factor_industry) calculated = FactorImputer(numerical_strategy=NAStrategy.MEDIAN, categorical_strategy=NAStrategy.CUSTOM, custom_value='other').fit_transform(fc) calculated.sort_index(axis=1, inplace=True) expected = pd.DataFrame( { 'test1': [1.0, 3.0, 3.0, 3.0, 5.0, 5.0, 6.0, 8.0], 'test2': [3.0, 2.0, 3.0, 7.0, 7.0, 6.0, 6.0, 6.0], 'test3': [3.0, 3.0, 3.0, 5.0, 6.0, 7.0, 6.0, 6.0], 'industry': ['a', 'a', 'a', 'a', 'a', 'a', 'other', 'a'] }, index=index, dtype=object) assert_frame_equal(calculated, expected)
# data_mv.set_index(['date', ' ticker'], inplace=True) # 创建Factor实例,储存数据以及相关参数 factor_pb = Factor(data=data_pb, name='PB', property_dict={ 'type': FactorType.ALPHA_FACTOR, 'norm_type': FactorNormType.Industry_Neutral }) factor_mv = Factor(data=data_mv, name='MV', property_dict={'type': FactorType.ALPHA_FACTOR_MV}) # 创建FactorContainer实例,加载所有的因子信息 fc = FactorContainer(start_date='2014-01-01', end_date='2014-03-10') fc.add_factor(factor_pb) fc.add_factor(factor_mv) # 也可以一次性加载所有因子 # # fc = FactorContainer(start_date='2014-01-01', end_date='2014-03-10', factors=[factor_pb, factor_mv]) # print fc.tiaocang_date # # [datetime.datetime(2014, 1, 30, 0, 0), datetime.datetime(2014, 2, 28, 0, 0)] # print fc.alpha_factor_col # # ['PB', 'MV'] # 提取行业数据 data_industry_code = factor_load('2014-01-01', '2014-03-10', 'SW_C1', sec_id='fullA', is_index=True,
# data_pb = pd.read_csv('pb.csv', encoding='gbk') # data_mv = pd.read_csv('mv.csv', encoding='gbk') # data_pb['date'] = pd.to_datetime(data_pb['date']) # data_mv['date'] = pd.to_datetime(data_mv['date']) # data_pb.set_index(['date', ' ticker'], inplace=True) # data_mv.set_index(['date', ' ticker'], inplace=True) # 创建Factor实例,储存数据以及相关参数 factor_pb = Factor(data=data_pb, name='PB', property_dict={'type': FactorType.ALPHA_FACTOR, 'norm_type': FactorNormType.Industry_Neutral}) factor_mv = Factor(data=data_mv, name='MV', property_dict={'type': FactorType.ALPHA_FACTOR_MV}) # 创建FactorContainer实例,加载所有的因子信息 fc = FactorContainer(start_date='2014-01-01', end_date='2014-03-10') fc.add_factor(factor_pb) fc.add_factor(factor_mv) # 也可以一次性加载所有因子 # # fc = FactorContainer(start_date='2014-01-01', end_date='2014-03-10', factors=[factor_pb, factor_mv]) # print fc.tiaocang_date # # [datetime.datetime(2014, 1, 30, 0, 0), datetime.datetime(2014, 2, 28, 0, 0)] # print fc.alpha_factor_col # # ['PB', 'MV'] # 提取行业数据 data_industry_code = factor_load('2014-01-01', '2014-03-10', 'SW_C1', sec_id='fullA', is_index=True, save_file='sw.csv') # data_industry_code = pd.read_csv('sw.csv', encoding='gbk') # data_industry_code['date'] = pd.to_datetime(data_industry_code['date'])