CausalRank
End-to-end Python framework for multi-treatment causal outcome estimation, joint uncertainty quantification, and statistically defensible treatment ranking from observational data.
当前状态:Phase 0–3(v0.3.0,MVP 完成)。 已实现:原始 DataFrame → 多分类 GBM 倾向得分 → Hájek IPTW →
mu_a = E[Y(a)]估计
- 重叠 / ESS / SMD 诊断与质量门控;联合 Bootstrap 协方差、SE / 单项 CI (
n_bootstrap显式开启);失败轮披露与推断阻断;Bonferroni 两两同时区间、 可确认优劣关系、保守排名置信集[1+B_i, K-W_i](result.pairwise / result.ranks)。 尚未实现(后续):max-T/stepdown 排名推断、AIPW/OW、可视化报告(Phase 4)。 点估计排序不是统计上确立的排名;inconclusive不是相等的证据。
⚠️ 因果识别假设(软件无法验证)
只有在以下条件成立时,IPTW 估计值才能解释为因果平均潜在结果 mu_a = E[Y(a)]:
- 一致性(consistency):观测结果等于所接受 Treatment 的潜在结果,Treatment 版本定义一致;
- 条件可交换性(无未观测混杂):给定输入的协变量
X后,Treatment 分配与潜在结果独立。 软件无法检查这一点;遗漏混杂(尤其是 Treatment 前的强混杂)会直接使估计有偏; - Positivity(共同支持):每个
x上每种 Treatment 的倾向得分都为正。弱重叠会导致极端 权重与不稳定估计,诊断会给出警告,但不能修复该问题; - SUTVA / 无干扰:个体之间潜在结果互不影响;
- iid 抽样:观测行独立同分布(存在客户/家庭/机构聚类时本版本不适用,需 cluster bootstrap);
- 协变量必须测量于 Treatment 之前。把 Treatment 后变量(中介、后果)当作混杂调整会引入偏差。
模型 AUC / 准确率 不能 作为因果估计可靠的证据;平衡、重叠、稳定性诊断不通过时, 软件会降级其质量门控并输出警告,不会输出“可靠因果排序”类结论。因果解释始终由使用者负责。
仅用于研究与方法验证,不构成任何业务、医疗或政策决策建议。
安装
要求 Python ≥ 3.11。开发环境使用 uv:
uv sync # 创建 .venv 并安装 runtime + dev 依赖
uv run pytest # 运行测试
uv run ruff check . # lint
uv run mypy # 类型检查
uv build # 打包检查
或在干净环境中安装本包本身:
uv pip install -e . # 或 pip install -e .
Quickstart
import pandas as pd
from causalrank import CausalRank
# df 需包含:treatment 列(互斥 K 类)、outcome 列(0/1)、Treatment 前协变量列
df = pd.read_csv("your_data.csv")
model = CausalRank(
treatment="route",
outcome="converted",
confounders=["age", "income", "gender", "previous_visits"],
estimator="iptw", # Phase 1/2 支持 IPTW
propensity_model="gbm", # 倾向得分预测模型(多分类 GBM)
estimand="population_mean", # mu_a = E[Y(a)],同一目标总体
weighting="hajek", # 组内归一化 IPTW(不改变估计目标)
cross_fit=5, # 可选:out-of-fold 倾向得分
confidence_level=0.95,
n_bootstrap=300, # >= 1 显式开启联合 Bootstrap 推断(SE/CI)
random_state=2026,
)
result = model.fit(df)
print(result.summary()) # 估计值、SE/CI、排名置信集、诊断与质量门控
print(result.effects.to_dataframe()) # mu_a、se、ci、原始均值、权重、ESS
print(result.covariance) # K×K 联合 Bootstrap 协方差
print(result.ranks.to_dataframe()) # 点排名 + 排名置信区间 [1+B_i, K-W_i]
print(result.pairwise.to_dataframe())# 两两同时区间与可确认优劣关系
print(result.diagnostics.summary()) # overlap / 权重 / SMD 诊断
for w in result.diagnostics.warnings:
print(w.level, w.code, w.message)
n_bootstrap 是显式开关:默认 None 只输出点估计与诊断(秒级);设为 300 会每轮
重新拟合全部协议来估计协方差(分钟级,可 n_jobs 并行且结果与串行逐位一致)。
低层排名内核可单独验证(规格 2.2):
from causalrank.inference import rank_from_estimates
ranking = rank_from_estimates(
estimates={"A": 0.25, "B": 0.24, "C": 0.17},
covariance=sigma,
confidence_level=0.95,
method="bonferroni",
)
估计方法(一句话版)
- 倾向得分:
e_a(x) = P(T=a|X=x),由联合多分类 GBM(scikit-learnHistGradientBoostingClassifier)估计;预处理(缺失插补、one-hot)全部包在 Pipeline 内、 只在训练折内 fit,避免信息泄露。 - 估计器:Hájek IPTW,
w_i = 1 / e_{T_i}(X_i),mu_a = Σ_{T_i=a} w_i Y_i / Σ_{T_i=a} w_i,所有 Treatment 在同一目标总体上比较。 - 诊断:权重分位数、有效样本量 ESS、倾向得分分布(overlap)、加权前后标准化均值差 SMD, 以及可配置的质量门控警告。
完整定义、假设与数值边界见 docs/methodology.md 与
docs/assumptions.md。
合成数据与验证
causalrank.simulation 提供已知真实潜在结果均值 mu_true 的可配置 DGP
(独立大规模 Monte Carlo 参考 + MC 标准误),内置 8 个场景(随机化、混杂、强混杂、弱重叠、
稀有 Treatment、效应相等、效应接近、混合类型 + 缺失)。模拟协议见
docs/simulation_protocol.md,端到端示例见
examples/01_gbm_iptw_from_raw_data.ipynb、
examples/02_joint_bootstrap.ipynb、
examples/03_causal_ranking.ipynb。
项目结构
src/causalrank/ 包源码(api / config / validation / estimators / diagnostics / simulation)
tests/ pytest 单元与端到端测试
examples/ 可执行示例 Notebook
docs/ 方法、假设、模拟协议、API、进度报告
许可证
MIT(暂定,见 LICENSE)。参考文献的使用约定见 docs/references.md。
Metadata
Release files for causalrank 0.3.0
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 |
|---|---|---|---|---|
| causalrank-0.3.0.tar.gz | 134.3 kB | With provenance | Details for causalrank-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| causalrank-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for causalrank-0.3.0-py3-none-any.whl |
Total release size: 199.0 kB
Release files / causalrank-0.3.0.tar.gz
| Download URL | causalrank-0.3.0.tar.gz |
|---|---|
| Size | 134.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97cc553903230501e4f3731e9fc9dacf3a77c84ed5d61be3968f9ade0e1d4653
|
|
BLAKE2b-256 checksum How to use checksums |
2ee66a0c5c76482543af31fada4be282170a99d50ce9b20b3838f0ef7db1f17a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 12, 2026.
Transparency logRelease files / causalrank-0.3.0-py3-none-any.whl
| Download URL | causalrank-0.3.0-py3-none-any.whl |
|---|---|
| Size | 64.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
21512aa0b943a3dd470c217e2c48617e04fbb5de02b0b02af6860ed8969fa206
|
|
BLAKE2b-256 checksum How to use checksums |
4356151f9ef29ac17e899fc0dad1a9f11277fcb829cdc7c5955c517855f9c3e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 12, 2026.
Transparency logSecurity for causalrank 0.3.0
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 causalrank-0.3.0.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 Oct 12, 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.