def factor_uqer_one_day(factor_name, ref_date, use_only_index_components=False, risk_neutral=True): risk_factors = ','.join( ['risk_factor_300.' + name for name in risk_factors_300]) sql = "select factor_uqer.Code, factor_uqer.{factor_name}, factor_data.申万一级行业, {risk_factors}," \ " return_300.D1LogReturn, return_300.isTradable from factor_uqer".format(factor_name=factor_name, risk_factors=risk_factors) sql += " join factor_data on factor_uqer.Date = factor_data.Date and factor_uqer.Code = factor_data.Code" \ " join risk_factor_300 on factor_uqer.Date = risk_factor_300.Date and factor_uqer.Code = risk_factor_300.Code" \ " join return_300 on factor_uqer.Date = return_300.Date and factor_uqer.Code = return_300.Code" \ " where factor_uqer.Date = '{ref_date}'".format(ref_date=ref_date) df1 = pd.read_sql(sql, engine).dropna() if df1.empty: return None, None df2 = pd.read_sql( "select Code, 300Weight / 100. as benchmark from index_components " "where Date ='{ref_date}'".format(ref_date=ref_date), engine) df = pd.merge(df1, df2, on=['Code'], how='left').fillna(0.) if use_only_index_components: df = df[df.benchmark != 0.] factors = df[['Code', factor_name]].set_index('Code')[factor_name] industry = df['申万一级行业'].values d1returns = df['D1LogReturn'].values is_tradable = df['isTradable'].values benchmark = df['benchmark'].values risk_exp = df[risk_factors_300].values risk_exp = np.concatenate([risk_exp, np.ones((len(risk_exp), 1))], axis=1) if risk_neutral: weights, analysis = factor_analysis(factors=factors, industry=industry, d1returns=d1returns, detail_analysis=True, benchmark=benchmark, risk_exp=risk_exp, is_tradable=is_tradable) else: weights, analysis = factor_analysis(factors=factors, industry=industry, d1returns=d1returns, detail_analysis=True, risk_exp=risk_exp, is_tradable=is_tradable) return weights, analysis
def test_factor_analysis_with_several_factors(self): benchmark = np.random.randint(50, size=1000) benchmark = benchmark / benchmark.sum() industry = np.random.randint(30, size=1000) factor_df = pd.DataFrame(np.random.randn(1000, 2), index=range(len(self.raw_factor))) factor_weights = np.array([0.2, 0.8]) constraints = Constraints() names = np.array(['a', 'b', 'c']) constraints.add_exposure(names, self.risk_factor) targets = self.risk_factor.T @ benchmark for i, name in enumerate(names): constraints.set_constraints(name, targets[i], targets[i]) weight_table, analysis_table = factor_analysis(factor_df, factor_weights, d1returns=self.d1returns, industry=industry, benchmark=benchmark, risk_exp=self.risk_factor, constraints=constraints) weight = weight_table.weight self.assertEqual(analysis_table['er'].sum() / analysis_table['er'].iloc[-1], 2.0) np.testing.assert_array_almost_equal(weight @ self.risk_factor, benchmark @ self.risk_factor)
def test_factor_analysis(self): benchmark = np.random.randint(50, size=1000) benchmark = benchmark / benchmark.sum() industry = np.random.randint(30, size=1000) factor_series = pd.Series(self.raw_factor.flatten(), index=range(len(self.raw_factor))) weight_table, analysis_table = factor_analysis( factor_series, d1returns=self.d1returns, industry=industry, benchmark=benchmark, risk_exp=self.risk_factor) weight = weight_table.weight self.assertEqual(analysis_table['er'].sum() / analysis_table['er'][-1], 2.0) np.testing.assert_array_almost_equal(weight @ self.risk_factor, benchmark @ self.risk_factor) self.assertTrue( weight @ factor_series.values > benchmark @ factor_series.values)