因子 - 快速开始
jh_quant.factors 是多因子模型计算框架,支持 11 种主流学术因子模型的因子收益率计算、个股暴露度估计和统计验证。
核心特性
- 11 种因子模型:CAPM、FF3、FF5、Carhart、Novy-Marx、Hou-Xue-Zhang、DHS、CH3、SY4、Reversal、Low Vol
- 两种计算方法:SIMPLE(简化版,等权 + 中位数分组)和 CLASSIC(经典版,市值加权 + 分位数分组)
- 日频/月频:支持月度和日度因子计算
- 高性能:支持 Polars 加速 + joblib 多进程并行
- 暴露度估计:OLS 回归计算个股在因子上的 Beta 暴露
- 因子验证:截距项 t 检验 + Fama-MacBeth 两步回归
核心入口
| 入口 | 适用场景 |
|---|---|
calculate_factor_returns() | 最简洁,一次性计算因子收益率 |
FactorEngine | 需要复用实例、批量计算多个模型 |
GeneralFactorCalculator | 已有股票收益率和基本面数据 |
方式一:便捷函数(推荐入门)
from jh_quant.factors import calculate_factor_returns
# 计算 FF3 月度因子收益率ff3 = calculate_factor_returns( 'ff3', method='simple', period='M', start_date='2020-01-01', end_date='2024-12-31',)print(ff3.head())一次性计算所有因子模型:
all_factors = calculate_factor_returns('all', start_date='2020-01-01', end_date='2024-12-31')# 返回 Dict[str, DataFrame]
for name, df in all_factors.items(): print(f"{name}: {len(df)} 行, 因子: {list(df.columns)}")方式二:FactorEngine(推荐进阶)
from jh_quant.factors import FactorEngine, FactorType, CalculationMethod, TimePeriod
engine = FactorEngine()
ff5 = engine.calculate_factor_returns( factor_type=FactorType.FF5, method=CalculationMethod.CLASSIC, period=TimePeriod.DAILY, start_date='2024-01-01', end_date='2024-12-31', n_jobs=4, use_polars=True,)
# 批量计算results = engine.calculate_all_factors( factor_types=[FactorType.FF3, FactorType.CARHART, FactorType.DHS], method=CalculationMethod.SIMPLE, period=TimePeriod.MONTHLY, start_date='2020-01-01', end_date='2024-12-31',)参数速查
FactorType
| 字符串 | FactorType | 因子组成 |
|---|---|---|
'capm' | CAPM | mkt |
'ff3' | FF3 | mkt, smb, hml |
'ff5' | FF5 | mkt, smb, hml, rmw, cma |
'carhart' | CARHART | mkt, smb, hml, umd |
'novy_marx' | NOVY_MARX | mkt, hml_adj, umd, gp_a |
'hxz' | HOU_XUE_ZHANG | mkt, me, ia, roe |
'dhs' | DHS | mkt, pead, fin |
'ch3' | CH3 | mkt, smb, vmg |
'sy4' | SY4 | mkt, smb, mgmt, perf |
'reversal' | REVERSAL | mkt, smb, rev |
'low_vol' | LOW_VOL | mkt, smb, ivol |
TimePeriod
| 值 | 说明 |
|---|---|
'M' / TimePeriod.MONTHLY | 月度因子 |
'D' / TimePeriod.DAILY | 日度因子 |
CalculationMethod
| 值 | 说明 |
|---|---|
'simple'(默认) | 简化方法,等权+中位数分组 |
'classic' | 经典方法,市值加权+分位数分组 |