AlphalensPlus: A better toolbox for factors' alpha analysis
基于 Quantopian 的 Alphalens 开发的升级版因子分析工具箱,针对 A 股市场数据结构与交易规则做了适配与增强。
安装
# 使用 uv(推荐)
uv pip install -e ".[opt]"
# 或使用 pip
pip install -e ".[opt]"
若不需要组合优化(
opt模块),可省略[opt],无需安装cvxpy。
快速开始
以下示例展示从模拟数据到生成完整分析报告的最小流程:
import pandas as pd
import numpy as np
import alphalens_plus as ap
# 1. 构造模拟行情数据(长面板)
# 索引:(order_book_id, date),必须包含 close 与 open 列
dates = pd.date_range('2023-01-01', periods=20, freq='B')
assets = ['000001.XSHE', '000002.XSHE', '000009.XSHE', '000012.XSHE']
np.random.seed(42)
prices = pd.DataFrame(
{
'close': 100 * np.exp(np.cumsum(np.random.randn(80) * 0.02 + 0.001)),
'open' : 100 * np.exp(np.cumsum(np.random.randn(80) * 0.02 + 0.001)),
},
index=pd.MultiIndex.from_product(
[assets, dates], names=['order_book_id', 'date']
)
)
# 2. 构造模拟因子数据(长面板)
# 索引:(date, order_book_id),列名为 factor
factor = pd.DataFrame(
np.random.randn(80),
index=pd.MultiIndex.from_product(
[dates, assets], names=['date', 'order_book_id']
),
columns=['factor']
)
# 3. 计算清洗后的因子与远期收益率
factor_data = ap.get_clean_factor_and_forward_returns(
factor, prices, periods=(1, 5), method='open-to-open'
)
# 4. 生成完整 Tear Sheet 分析报告
ap.create_full_tear_sheet(factor_data)
核心 API 速览
alphalens_plus 已将最常用函数暴露在包顶层,可直接通过 import alphalens_plus as ap 调用:
| 功能 | 顶层 API |
|---|---|
| 清洗因子 + 计算远期收益 | ap.get_clean_factor_and_forward_returns |
| 因子分位数分层 | ap.quantize_factor |
| 因子排序 | ap.rank_factor |
| 信息系数 IC | ap.factor_information_coefficient |
| 平均 IC | ap.mean_information_coefficient |
| 组合权重 | ap.factor_weights |
| 完整 Tear Sheet | ap.create_full_tear_sheet |
| 最小方差优化 | ap.min_variance(需安装 cvxpy) |
若需更细粒度的控制,仍可按需导入子模块:
from alphalens_plus import utils, performance, plotting, tears
日志与更新记录
项目开发日志已迁移至 LOGGING.md。
References
-
- 原生alphalens项目,主体内容于2015年左右实现,目前可复用,但局部代码需更新。
- 整体用作思路参考。
-
stefan-jansen/alphalens-reloaded
- 大佬Stefan Jansen维护的alphalens项目,对最新版本python进行适配,可复用程度较高。
- 是主要学习和参考的项目。
-
- vnpy的自适应版,更多面向回测系统使用,次要学习和参考项目。
Metadata
Release files for dsf-alphalens 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dsf_alphalens-1.0.3.tar.gz | 29.2 kB | With provenance | Details for dsf_alphalens-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dsf_alphalens-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for dsf_alphalens-1.0.3-py3-none-any.whl |
Total release size: 58.1 kB
Release files / dsf_alphalens-1.0.3.tar.gz
| Download URL | dsf_alphalens-1.0.3.tar.gz |
|---|---|
| Size | 29.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5aa582cbbdb246dfa552efc179f035d2a4d61ee17c120d31cd8c31414c8cf86a
|
|
BLAKE2b-256 checksum How to use checksums |
2add707e51498ae364586bd596566ecebe125dfbde047ce33342bc97b54fc94b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 21, 2026.
Transparency logRelease files / dsf_alphalens-1.0.3-py3-none-any.whl
| Download URL | dsf_alphalens-1.0.3-py3-none-any.whl |
|---|---|
| Size | 29.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9aee7bb02a7b7f6474998581a7d90658b8d088de10cc71122cf1f30559e31115
|
|
BLAKE2b-256 checksum How to use checksums |
c8de622091e54c456139179e9ebe7bed51e455d88686873c13d942221642bca6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 21, 2026.
Transparency logSecurity for dsf-alphalens 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for dsf_alphalens-1.0.3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 21, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.