uplift评估-聚合层面的“真实性”(RCT)

📅 2026/7/10 4:13:36
uplift评估-聚合层面的“真实性”(RCT)
虽然单个样本的 label 不存在但我们可以评估✅ 1️⃣ 聚合层面的“真实性”RCT用 A/B 实验数据E[Y∣T1]−E[Y∣T0]✅ 这是ATE 的无偏估计✅ 可以用来校验模型的整体方向是否正确这个问题问得非常专业已经踩到工业级 Uplift / 因果建模 pipeline 的“验收标准”了。我分「思想 → 代码结构 → 关键实现细节 → 常见坑」​ 四个层次来讲给你一套能直接落地的代码逻辑。一、先说“在代码层面到底在干什么”你要做的不是“给每个样本算 label”而是用 RCT 数据在「模型预测空间」上验证模型预测的 uplift 均值 ≈ 实验 ATE高 uplift 分桶 → 实际 uplift 为正低 / 负 uplift 分桶 → 实际 uplift ≤ 0本质上是一个Model Calibration Sanity Check。二、最小可用代码示例核心逻辑一、先给一个「具体到数字」的例子1️⃣ 假设我们有 20 个用户RCT 数据user_idTYuplift_pred1110.31200-0.123110.584010.025100.45600-0.207110.72800-0.059110.6010010.1011100.351200-0.1513110.8014010.2515100.501600-0.0817110.651800-0.1819110.5520010.15T1实验组发券T0对照组不发券Y是否转化 / 留存1是0否uplift_pred模型预测的 upliftτ̂(x)2️⃣ Step 1先算整体 ATEGround TruthT1 df[df[T] 1][Y].mean() # 0.70 T0 df[df[T] 0][Y].mean() # 0.66 ATE 0.70 - 0.66 0.04✅ 这就是你说的t1 的 mean 是 0.7t0 的 mean 是 0.663️⃣ Step 2按 uplift_pred 分桶Decile假设我们分3 桶为了手算清晰桶uplift_pred 范围用户桶 0低≤ 0.10user 2, 4, 6, 8, 10, 12, 14, 16, 18, 20桶 1中0.31 ~ 0.58user 1, 3, 5, 11, 15, 19桶 2高≥ 0.60user 7, 9, 13, 17实际代码中用pd.qcut自动等分4️⃣ Step 3✅ 每个桶里分别算「实际 uplift」公式每个桶内独立计算Observed Upliftk​E[Y∣T1,bucketk]−E[Y∣T0,bucketk] 桶 0低 upliftT用户Y 值均值T1无—NaN​T0user 2,6,8,12,16,18,200,0,0,0,0,0,11/7 ≈0.14​⚠️ 桶 0 没有 T1 用户 →无法计算 Observed Uplift记为 NaN 桶 1中 upliftT用户Y 值均值T1user 1,3,5,11,151,1,0,0,02/5 0.40​T0user 4,10,141,1,13/3 1.00​Observed Uplift1​0.40−1.00−0.60⚠️ 负的说明这个桶里「发券反而更差」 桶 2高 upliftT用户Y 值均值T1user 7,9,13,171,1,1,14/4 1.00​T0user 4,10,14,201,1,1,14/4 1.00​Observed Uplift2​1.00−1.000.005️⃣ Step 4汇总成表桶预测 uplift 均值实际 uplift样本数桶 0低≈ -0.10NaN10桶 1中≈ 0.45-0.60​8桶 2高≈ 0.690.00​8⚠️问题暴露了桶 1 实际 uplift 是负的模型方向反了桶 2 实际 uplift 0模型高估了这个模型有问题不能直接上线二、完整画图代码你直接能跑import pandas as pd import matplotlib.pyplot as plt import numpy as np # 1. 构造数据 df pd.DataFrame({ user_id: range(1, 21), T: [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0], Y: [1,0,1,1,0,0,1,0,1,1,0,0,1,1,0,0,1,0,1,1], uplift_pred: [ 0.31, -0.12, 0.58, 0.02, 0.45, -0.20, 0.72, -0.05, 0.60, 0.10, 0.35, -0.15, 0.80, 0.25, 0.50, -0.08, 0.65, -0.18, 0.55, 0.15 ] }) # 2. 分 3 桶 df[bucket] pd.qcut( df[uplift_pred], q3, labels[低, 中, 高], duplicatesdrop ) # 3. 每个桶算实际 uplift records [] for b, g in df.groupby(bucket): t1 g[g[T] 1][Y].mean() t0 g[g[T] 0][Y].mean() obs t1 - t0 if pd.notna(t1) and pd.notna(t0) else np.nan records.append({ bucket: b, pred_uplift: g[uplift_pred].mean(), obs_uplift: obs, n: len(g) }) res pd.DataFrame(records) # 4. 画图 plt.figure(figsize(7, 4)) colors [green if v 0 else red if v 0 else gray for v in res[obs_uplift]] bars plt.bar(res[bucket].astype(str), res[obs_uplift], colorcolors) # 标注数值 for bar, val in zip(bars, res[obs_uplift]): label f{val:.2f} if pd.notna(val) else NaN plt.text(bar.get_x() bar.get_width()/2, bar.get_height() 0.02, label, hacenter, fontsize12, fontweightbold) # ATE 参考线 ate df[df[T]1][Y].mean() - df[df[T]0][Y].mean() plt.axhline(ate, colorblue, linestyle--, labelfATE{ate:.2f}) plt.axhline(0, colorblack, linewidth0.5) plt.ylabel(Observed Uplift (E[Y|T1] - E[Y|T0]), fontsize11) plt.title(Bucketed Uplift Validation, fontsize13, fontweightbold) plt.legend() plt.tight_layout() plt.show() print(res)三、你应该看到什么合格模型✅合格模型的图高桶Observed Uplift 0显著中桶Observed Uplift ≈ 0低桶Observed Uplift ≤ 0整体趋势单调递增ATE 虚线穿过中间桶❌不合格模型如本例高桶实际 uplift ≈ 0中桶实际 uplift 为负说明模型排序方向错误四、一句话总结每个桶的「实际 uplift」 桶内 T1 的 Y 均值 − 桶内 T0 的 Y 均值如果模型靠谱高 uplift 桶的实际 uplift 应该显著为正低 uplift 桶应该 ≤ 0这一步是 Uplift 模型离线验证的终极审判。RCT 数据 ↓ 特征工程 ↓ 训练 Uplift ModelX-Learner / DML ↓ 离线打分uplift_pred ↓ ✅ 聚合 ATE 校验 ✅ 分桶 Observed Uplift 图 ↓ 通过 → 上线 不通过 → 回炉特征 / 模型 / 样本五、常见坑非常重要❌ 坑 1用留白组算 ATE# 错误 ate_wrong exp_Y.mean() - holdout_Y.mean()✅ 正确ate exp_Y.mean() - ctrl_Y.mean()❌ 坑 2桶内 Treatment / Control 样本极度不均衡某些桶只有 T1 或只有 T0实际 uplift 无法计算✅ 解法分桶时限制最小样本数或按percentile 强制平衡❌ 坑 3只看形状不看符号桶图单调上升 ✅但整体 uplift 是负的 ❌模型方向反了✅ 必须同时检查ATE 符号高桶 uplift 0六、面试一句话标准答案在代码层面我们首先用 RCT 数据计算ATE^E[Y∣T1]−E[Y∣T0]作为真实因果锚点然后将模型预测的 uplift 分 10 桶逐桶计算 Treatment 与 Control 的实际响应差得到 Observed Uplift通过对比预测 uplift 均值与 ATE、以及分桶 Observed Uplift 的单调性完成模型在聚合层面的因果校准与方向校验。