def calc_factor_alpha_beta( self, returns: pd.DataFrame = None, demeaned: bool = True, group_adjust: bool = False, equal_weight: bool = False, ): """ 计算因子的 alpha 和 beta 因子加权组合每日收益 = beta x 市场组合每期收益 + alpha 参数 --- :param returns: 构建多空组合的加权收益, 默认为 None, 为 None 时,会调用 performance 的 factor_returns, 根据因子值构建多空组合,计算相应的按日期[或按资产]收益 :param demeaned: 使用超额收益 :param group_adjust: 行业中性 :param equal_weight: 默认为 False, 如果为 True, 分别对因子中位数构建多空组合, 多空组合权重相等 """ return perf.factor_alpha_beta( factor_data=self._clean_factor_data, demeaned=demeaned, group_adjust=group_adjust, )
def create_summary_tear_sheet(factor_data: pd.DataFrame, long_short: bool = True, group_neutral: bool = False): """ """ # Return Analysis mean_quant_ret, std_quantiles = perf.mean_return_by_quantile( factor_data, by_group=False, demeaned=long_short, group_adjust=group_neutral) mean_quant_rateret = mean_quant_ret.apply( utils.rate_of_return, axis=0, base_period=mean_quant_ret.columns[0]) mean_quant_ret_bydatetime, std_quant_bydatetime = perf.mean_return_by_quantile( factor_data, by_datetime=True, by_group=False, demeaned=long_short, group_adjust=group_neutral, ) mean_quant_rateret_bydatetime = mean_quant_ret_bydatetime.apply( utils.rate_of_return, axis=0, base_period=mean_quant_ret_bydatetime.columns[0]) std_quant_bydatetime = std_quant_bydatetime.apply( utils.std_conversion, axis=0, base_period=std_quant_bydatetime.columns[0]) alpha_beta = perf.factor_alpha_beta(factor_data, demeaned=long_short, group_adjust=group_neutral) mean_ret_spread_quant, std_spread_quant = perf.mean_returns_spread( mean_quant_rateret_bydatetime, factor_data["factor_quantile"].max(), factor_data["factor_quantile"].min(), std_err=std_quant_bydatetime, ) periods = utils.get_forward_returns_columns(factor_data.columns) fr_cols = len(periods) vertical_sections = 2 + fr_cols * 3 gr = GridFigure(rows=vertical_sections, cols=1) plot_quantile_statistics_table(factor_data) plot_quantile_returns_bar(mean_quant_rateret, by_group=False, ylim_percentiles=None, ax=gf.next_row())
def create_summary_tear_sheet( factor_data: pd.DataFrame, by_datetime: bool = True, # 按日期计算 by_group: bool = False, # 按分组计算 long_short: bool = True, # 多空组合 group_neutral: bool = False, # 分组中性 periods: Union[int, Tuple[int], List[int]] = 1, frequence: str = '1d'): """ 创建一个小型的汇总表格,包括因子的收益率分析,IC 值,换手率等分析 参数 --- :param factor_data: 因子数据 :param long_short: 是否构建多空组合,在该组合上进行进行分析。 :param group_neutral: 是否进行行业中性 """ if isinstance(periods, int): periods = [ periods, ] # 收益分析 mean_quant_ret, std_quant = perf.mean_return_by_quantile( factor_data, by_group=by_group, demeaned=long_short, group_adjust=group_neutral) mean_quant_rateret = mean_quant_ret.apply( utils.rate_of_return, axis=0, base_period=mean_quant_ret.columns[0]) std_quant_rate = std_quant.apply(utils.std_conversion, axis=0, base_period=std_quant.columns[0]) mean_quant_ret_bydatetime, std_quant_bydatetime = perf.mean_return_by_quantile( factor_data, by_datetime=by_datetime, by_group=by_group, demeaned=long_short, group_adjust=group_neutral) mean_quant_rateret_bydatetime = mean_quant_ret_bydatetime.apply( utils.rate_of_return, axis=0, base_period=mean_quant_ret_bydatetime.columns[0]) std_quant_rate_bydatetime = std_quant_bydatetime.apply( utils.std_conversion, axis=0, base_period=std_quant_bydatetime.columns[0]) alpha_beta = perf.factor_alpha_beta(factor_data=factor_data, demeaned=long_short, group_adjust=group_neutral) mean_ret_spread_quant, std_spread_quant = perf.mean_returns_spread( mean_quant_rateret, upper_quant=factor_data.factor_quantile.max(), lower_quant=factor_data.factor_quantile.min(), std_err=std_quant_rate) fr_cols = utils.get_forward_returns_columns(factor_data.columns) vertical_sections = 2 + len(fr_cols) * 3 gf = GridFigure(rows=vertical_sections, cols=1) plotting.plot_quantile_statistics_table(factor_data) plotting.plot_returns_table(alpha_beta, mean_quant_rateret, mean_ret_spread_quant) plotting.plot_quantile_returns_bar(mean_quant_rateret, by_group=by_group, ylim_percentiles=None, ax=gf.next_row()) # Information Analysis ic = perf.factor_information_coefficient(factor_data) plotting.plot_information_table(ic) # Turnover Analysis # FIXME: 股票是 T+1,意味着频率只能是 Day 及以上频率 quantile_factor = factor_data["factor_quantile"] quantile_turnover = { p: pd.concat( [ perf.quantile_turnover(quantile_factor, q, p) for q in range(1, int(quantile_factor.max()) + 1) ], axis=1, ) for p in periods } autocorrelation = pd.concat( [ perf.factor_rank_autocorrelation(factor_data, period) for period in periods ], axis=1, ) plotting.plot_turnover_table(autocorrelation, quantile_turnover) plt.show() gf.close()