P paper2skillsPlaybook
开始业务诊断
访客模式
22-数据采集工程

Data Collection Causal Debiasing — 采集偏差因果修正:爬虫选择性采集对因果分析的去污染

Skill-Data-Collection-Causal-Debiasing

声明类型推断验证状态待复核

某母婴品牌通过爬虫采集 Amazon 评论,分析"打折 coupon 是否提升复购率"

来源
paper2skills-vault/22-数据采集工程/Skill-Data-Collection-Causal-Debiasing.md
适用边界
论文方法到业务问题的项目侧映射,不自动证明客户根因。
声明类型示例验证状态待企业数据验证

180 万

来源
paper2skills-vault/22-数据采集工程/Skill-Data-Collection-Causal-Debiasing.md#business-value
适用边界
方法估算或场景示例,不等于客户结果或收益承诺。

待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源

业务视角
适用角色数据工程师 / 技术负责人 · 运营负责人 · 选品负责人
适用平台Amazon SP API + Keepa · TikTok Shop API · 跨境多平台数据湖
什么情况下用想监控竞品价格/评论/排名但没有稳定采集能力,手动太慢;多平台数据分散整合成本极高;数据管道不稳定经常断
示例目标(需企业数据验证)竞品价格/评论数据每日自动更新,多平台数据统一入仓,数据管道稳定性 >99%,取数时间从小时降到分钟
业务痛点
竞品数据要手动收集太慢平台 API 限制抓不到数据多系统数据整合不起来报表用的数据是过期的

1. 解决的问题

某母婴品牌通过爬虫采集 Amazon 评论,分析"打折 coupon 是否提升复购率"

2. 核心算法逻辑

核心思想

跨境电商数据采集面临一个根本性困境:你能采到的数据,恰恰不是随机样本。Amazon 评论爬虫只能采集到购买且写了评论的买家(约 2-5% 购买者),而这批人的特征(高参与度、使用经验较强、投诉阈值较低)与全体买家截然不同。当你用这批有偏数据做因果推断(如"折扣是否提升复购率"),估计结果会被选择偏差(Selection Bias)严重扭曲。

问题结构:

真实世界:所有买家 (N=100,000)
           ↓ 购买决策 ↓ 评论决策 ↓ 爬虫可访问
可采集样本:N=2,000 ~ 3,000(仅购买且主动写评论者)
           ↑
    选择偏差来源 × 3:
    1. 购买选择偏差(非随机购买)
    2. 评论选择偏差(非随机评论)
    3. 爬虫采集偏差(反爬/限速截断)

本 Skill 的解法:用因果推断框架(IPW + 选择模型 + 双重稳健估计)对采集偏差进行事后修正,使有偏样本的因果效应估计收敛到真实效应。

三篇论文的互补关系

论文解决的核心问题关键机制
CausalDebiasWeb (2407.15392)网页爬取的选择偏差识别与修正倾向性评分 IPW + Heckman 两阶段选择模型
SurveyBiasCorrect (2501.08734)电商调查数据的非应答偏差修正双重稳健估计(DR-Estimator)+ Calibration 加权
CrawlerBiasAudit (2503.12640)爬虫系统性截断的偏差审计方法截断检测(Truncation Test)+ 外推因子估计

核心算法一:逆概率加权(IPW)选择偏差修正

设定:令 $S_i = 1$ 表示样本 $i$ 被爬虫采集到,$T_i$ 为处理变量(如是否打折),$Y_i$ 为结果变量(如复购率)。

选择机制:

P(S_i = 1 \mid X_i) = \sigma\left(\gamma_0 + \gamma_1 X_i^{\text{review\_propensity}} + \gamma_2 X_i^{\text{account\_age}}\right)

IPW 修正的因果效应估计:

\hat{\tau}^{\text{IPW}} = \frac{1}{n} \sum_{i=1}^{n} \frac{S_i}{\hat{P}(S_i=1 \mid X_i)} \cdot \left(\frac{T_i Y_i}{\hat{P}(T_i=1 \mid X_i)} - \frac{(1-T_i) Y_i}{1-\hat{P}(T_i=1 \mid X_i)}\right)

核心算法二:双重稳健估计(DR-Estimator)

双重稳健估计结合了 IPW 和直接回归,任一模型正确即可得到一致估计:

\hat{\tau}^{\text{DR}} = \frac{1}{n}\sum_{i=1}^{n}\left[\hat{\mu}_1(X_i) - \hat{\mu}_0(X_i) + \frac{T_i(Y_i - \hat{\mu}_1(X_i))}{\hat{e}(X_i)} - \frac{(1-T_i)(Y_i - \hat{\mu}_0(X_i))}{1-\hat{e}(X_i)}\right]

其中:

  • $\hat{\mu}_t(X_i) = \mathbb{E}[Y \mid T=t, X=X_i]$:结果模型(Outcome Model)
  • $\hat{e}(X_i) = P(T=1 \mid X=X_i)$:倾向得分模型(Propensity Score Model)

核心算法三:爬虫截断检测(Truncation Test)

基于 Kolmogorov-Smirnov 检验检测评论数分布的截断点:

\text{KS\_Stat} = \sup_x |F_{\text{observed}}(x) - F_{\text{expected}}(x)|

当 KS-Stat 超过阈值时,识别为爬虫截断,并估计外推因子:

\text{Extrapolation Factor} = \frac{N_{\text{true\_estimated}}}{N_{\text{crawled}}}

判读口径(③ 实测后收紧):KS 只证伪「log1p(评论数) 服从对数正态」,不能单独证明截断——未截断的幂律样本同样显著(③ 的对照用例给出 p = 7.8e-04,仍小于 0.05 阈值)。因此 has_truncation 要求 KS 显著 AND 天花板堆积 spike_at_ceiling ≥ 0.05(速率限制会把高端压成钉在同一个值上的尖)。实测:人为把 250 条里的 50 条钉在 500 → 堆积 0.20 → True;同分布未截断样本 → 堆积 0.005 → False;demo 的 clip(…, 1, 500) 只压到 0.1% 质量 → 堆积 0.001 → False(检得出与否取决于堆积规模,不是恒真开关)。

偏差修正效果对比

估计方法折扣→复购率效应估计偏差(真实值 τ=0.12)
原始爬虫样本(无修正)0.31+0.19(过高估计 158%)
简单 OLS 回归0.24+0.12(过高估计 100%)
IPW 修正0.15+0.03
双重稳健 DR(本文)0.13+0.01(偏差 <10%)

③ 实测(seed 42,跑两遍输出一致;与上表的论文口径分开读)

上表是论文报告的场景量级,③ 的夹具并没有复现出那么大的原始偏差,两边不要混引:

  • demo(n=2000 爬虫样本 + 8000 参考总体,地面真值 true_ate = 0.09):naive 0.1299(+0.0399)→ IPW 0.1229(+0.0329)→ DR 0.1220(+0.0320),95% CI [0.1174, 0.1266],相对偏差修正 6.5%,有效样本量 639(= 2000 的 32%,处在 ④ 警戒线 30% 附近)。修正方向单调(naive > IPW > DR 逐步靠近真值)但没有把偏差消掉,因为选择机制只作用在特征上、coupon 分配本身是随机的。
  • 基础用例:IPW 权重范围 [1.47, 3.14](概率被 clip 到 0.05–0.95,故权重上界 20 远未触顶);DR ATE 0.476 vs 真值 0.5,SE 0.0140。
  • 无混淆对照(T 独立于 X):naive 偏差 0.010、DR 偏差 0.018 —— 该夹具下没有混淆可修,重加权只加方差。原实现在这里写着「修正后偏差应小于原始」却没有断言兜着,所以一直没被证伪;现在这条只断言两者都 <0.05,方向性检验挪到有混淆的 demo 上。
  • 截断检测三档实测:堆积 0.20 → has_truncation=True(截断点估到 500);未截断对照堆积 0.005 → False(但它的 KS p = 7.8e-04 依旧显著,证明单看 p 值会恒报截断);demo 的 clip 堆积 0.001 → False。
  • 类型口径:stats.kstest 返回 numpy.float64,p < threshold 得 numpy.bool_,而 numpy.bool_ 不是 bool 的子类——isinstance(..., bool) 直接失败(本轮实测报错),且 numpy 2.x 下 repr 会变成 np.True_,写进 JSON 也会出问题。三个字段一律显式转内置类型。

3. 业务应用场景

业务背景:某母婴品牌通过爬虫采集 Amazon 评论,分析"打折 coupon 是否提升复购率"。原始爬虫样本显示打折组复购率高出对照组 31%,但市场部基于这一结果激进投放 coupon,实际复购提升仅 8%,造成严重的 coupon 成本浪费。

量化收益: - 避免基于虚高 CATE(+0.31)多投放 coupon 预算 $82,000/季 - 实际 CATE(+0.09)仍显著 → coupon 策略保留但降低力度 - 净节省 coupon 预算 ≈ $65,000/年,同时维持真实复购提升

业务背景:为了解目标买家对价格敏感度,通过 Amazon Vine + 站内消息发放价格调查问卷,但高收入买家(对价格不敏感)应答率仅 12%,低收入买家(对价格高度敏感)应答率 31%,导致调查结果严重低估价格弹性。

4. 输入数据要求

未提取到输入条目;请核对原始 Skill 卡片与代码。

5. 输出结果

未提取到输出条目;请核对原始 Skill 卡片与代码。

6. 业务价值与 ROI

180 万

7. 代码模板

代码块数量:4

Python589 行 · 完整显示
"""
Data Collection Causal Debiasing
整合 IPW 选择偏差修正 + 双重稳健估计 + 爬虫截断检测
CausalDebiasWeb (arXiv:2407.15392) + SurveyBiasCorrect (arXiv:2501.08734) + CrawlerBiasAudit (arXiv:2503.12640)
"""

import numpy as np
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple
from scipy import stats
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.preprocessing import StandardScaler
import warnings
warnings.filterwarnings("ignore")


@dataclass
class BiasAuditResult:
    """偏差审计结果"""
    feature_name: str
    sample_mean: float
    population_mean: float
    bias_ratio: float          # sample_mean / population_mean
    is_biased: bool            # |bias_ratio - 1| > 0.2 视为显著偏差


@dataclass
class TruncationTestResult:
    """爬虫截断检验结果"""
    ks_stat: float
    p_value: float
    has_truncation: bool
    estimated_truncation_point: float
    extrapolation_factor: float        # 估计的样本外推因子
    spike_at_ceiling: float = 0.0      # 天花板值上的堆积占比(截断的判别签名)


class CrawlerBiasAuditor:
    """
    爬虫偏差审计器
    arXiv:2503.12640 CrawlerBiasAudit
    检测爬虫系统性截断与样本分布偏差
    """

    def __init__(self, truncation_p_threshold: float = 0.05,
                 min_ceiling_spike: float = 0.05):
        self.truncation_p_threshold = truncation_p_threshold
        # 天花板堆积占比下限:KS 只证明「与对数正态不符」,幂律尾部更重也会显著,
        # 所以必须再要求高端压平成一个尖才判爬虫截断(口径见 test_truncation)
        self.min_ceiling_spike = min_ceiling_spike

    def audit_feature_bias(
        self,
        sample_features: np.ndarray,           # 爬虫样本特征
        population_means: Dict[str, float],     # 已知总体均值(来自平台统计)
        feature_names: List[str],
    ) -> List[BiasAuditResult]:
        """检测特征分布偏差"""
        results = []
        for i, name in enumerate(feature_names):
            if name not in population_means:
                continue
            sample_mean = float(np.mean(sample_features[:, i]))
            pop_mean = population_means[name]
            bias_ratio = sample_mean / max(abs(pop_mean), 1e-6)
            results.append(BiasAuditResult(
                feature_name=name,
                sample_mean=sample_mean,
                population_mean=pop_mean,
                bias_ratio=bias_ratio,
                is_biased=abs(bias_ratio - 1.0) > 0.2,
            ))
        return results

    def test_truncation(
        self,
        review_counts: np.ndarray,             # 每个 ASIN 的评论数
        expected_distribution: str = "powerlaw",
    ) -> TruncationTestResult:
        """
        检测爬虫截断(评论数分布截断检验)
        评论数理论上应服从幂律分布;若高端被截断,则识别为爬虫限制
        """
        x = np.sort(np.log1p(review_counts))
        n = len(x)

        # 理论分布:对数正态(近似幂律)——评论数取 log1p 后应近似正态,
        # 因此对拟合正态做单样本 KS。
        # ⚠ 原实现先 `np.random.lognormal(mu, sigma, 10000)` 再 `ks_2samp(x, np.log1p(theoretical))`,
        # 等于拿「log1p(lognormal)」去比「log1p(counts)」——两边都压缩过、形状并不对应,
        # 检验对象与 ① 写的 F_expected 不是同一个东西,还白消耗一次全局 RNG 流。
        mu, sigma = float(np.mean(x)), float(np.std(x))
        ks_res = stats.kstest(x, "norm", args=(mu, max(sigma, 1e-9)))
        ks_stat = float(ks_res.statistic)
        p_value = float(ks_res.pvalue)

        # 截断的真实签名:天花板处的堆积(速率限制把高端压平成一个尖)
        ceiling = float(np.max(review_counts))
        spike_at_ceiling = float(np.sum(review_counts >= ceiling) / n)

        # 判读口径:KS 显著 **且** 天花板堆积达标。
        # 只用 KS 会退化成「什么都算截断」——实测未截断的幂律样本(n=2000,np.random.pareto(1.5))
        # 对同一条拟合对数正态也给出 p ≈ 3.3e-17,照样显著。
        has_truncation = bool(p_value < self.truncation_p_threshold
                              and spike_at_ceiling >= self.min_ceiling_spike)

        # 估计截断点:判为截断时就是天花板本身(原实现返回 p95,那不是截断点);
        # 未判为截断时退回 p95 作为尾部关注点。
        p95 = float(np.percentile(review_counts, 95))
        truncation_point = ceiling if has_truncation else p95

        # 外推因子:尾部质量补偿(>p95 的实际占比 vs 幂律理论 10%)
        observed_tail = int(np.sum(review_counts > p95))
        expected_tail_ratio = 0.10  # 幂律分布中 >p95 的比例理论值
        current_tail_ratio = observed_tail / n
        extrapolation_factor = expected_tail_ratio / max(current_tail_ratio, 0.001)

        # ks_2samp / kstest 返回 numpy.float64,`p < thr` 得到 numpy.bool_。
        # numpy.bool_ **不是** Python bool 的子类,isinstance 判定与 JSON 序列化都会失效
        # ——本轮实测报错(「截断检测应返回布尔值」)的根因,三个字段一律显式转内置类型。
        return TruncationTestResult(
            ks_stat=ks_stat,
            p_value=p_value,
            has_truncation=has_truncation,
            estimated_truncation_point=truncation_point,
            extrapolation_factor=float(min(extrapolation_factor, 5.0)),  # 上限5倍
            spike_at_ceiling=spike_at_ceiling,
        )


class SelectionModelIPW:
    """
    选择模型 + 逆概率加权(IPW)
    arXiv:2407.15392 CausalDebiasWeb
    修正爬虫选择偏差
    """

    def __init__(self, selection_features: List[str] = None):
        self.selection_model = LogisticRegression(max_iter=1000, C=1.0)
        self.scaler = StandardScaler()
        self.selection_features = selection_features or []
        self._fitted = False

    def fit_selection_model(
        self,
        X_selected: np.ndarray,     # 爬虫采集样本的特征矩阵
        X_population: np.ndarray,   # 总体(或近似总体)样本的特征矩阵
    ) -> "SelectionModelIPW":
        """
        拟合选择模型 P(S=1 | X)
        通过将爬虫样本(S=1)与参考总体(S=0)合并训练二分类模型
        """
        n_selected = len(X_selected)
        n_pop = len(X_population)

        X_combined = np.vstack([X_selected, X_population])
        y_combined = np.array([1] * n_selected + [0] * n_pop)

        X_scaled = self.scaler.fit_transform(X_combined)
        self.selection_model.fit(X_scaled, y_combined)
        self._fitted = True
        return self

    def compute_selection_prob(self, X: np.ndarray) -> np.ndarray:
        """计算样本被选中的概率 P(S=1 | X)"""
        if not self._fitted:
            raise RuntimeError("请先调用 fit_selection_model()")
        X_scaled = self.scaler.transform(X)
        probs = self.selection_model.predict_proba(X_scaled)[:, 1]
        # 概率截断(防止 IPW 权重爆炸)
        return np.clip(probs, 0.05, 0.95)

    def compute_ipw_weights(self, X: np.ndarray) -> np.ndarray:
        """
        计算 IPW 权重 w_i = 1 / P(S=1 | X_i)
        低选择概率样本获得高权重(代表性补偿)
        """
        selection_probs = self.compute_selection_prob(X)
        return 1.0 / selection_probs

    def estimate_ate_ipw(
        self,
        X: np.ndarray,
        T: np.ndarray,
        Y: np.ndarray,
        propensity_scores: Optional[np.ndarray] = None,
    ) -> Tuple[float, float]:
        """
        IPW 修正的 ATE 估计
        Returns: (ate_estimate, standard_error)
        """
        w_selection = self.compute_ipw_weights(X)

        if propensity_scores is None:
            # 用逻辑回归估计倾向得分
            ps_model = LogisticRegression(max_iter=500)
            ps_model.fit(self.scaler.transform(X), T)
            propensity_scores = np.clip(ps_model.predict_proba(self.scaler.transform(X))[:, 1], 0.05, 0.95)

        # Horvitz-Thompson 估计量
        treated = T == 1
        control = T == 0

        ate_t = np.sum(w_selection[treated] * Y[treated] / propensity_scores[treated]) / \
                np.sum(w_selection[treated])
        ate_c = np.sum(w_selection[control] * Y[control] / (1 - propensity_scores[control])) / \
                np.sum(w_selection[control])

        ate = ate_t - ate_c

        # Bootstrap SE(简化:用加权方差估计)
        residuals = Y - (T * ate_t + (1 - T) * ate_c)
        se = np.sqrt(np.mean((w_selection * residuals) ** 2) / len(Y))

        return float(ate), float(se)


class DoubleRobustEstimator:
    """
    双重稳健估计器(DR-Estimator)
    arXiv:2501.08734 SurveyBiasCorrect
    同时使用结果模型 + 倾向得分模型,任一正确则无偏
    """

    def __init__(self):
        self.outcome_model_t1 = LinearRegression()
        self.outcome_model_t0 = LinearRegression()
        self.propensity_model = LogisticRegression(max_iter=1000, C=1.0)
        self.scaler = StandardScaler()
        self._fitted = False

    def fit(
        self,
        X: np.ndarray,
        T: np.ndarray,
        Y: np.ndarray,
        sample_weights: Optional[np.ndarray] = None,
    ) -> "DoubleRobustEstimator":
        """拟合结果模型和倾向得分模型"""
        X_scaled = self.scaler.fit_transform(X)

        # 倾向得分模型
        self.propensity_model.fit(X_scaled, T, sample_weight=sample_weights)

        # 分组结果模型
        treated = T == 1
        control = T == 0

        sw_t = sample_weights[treated] if sample_weights is not None else None
        sw_c = sample_weights[control] if sample_weights is not None else None

        self.outcome_model_t1.fit(X_scaled[treated], Y[treated], sample_weight=sw_t)
        self.outcome_model_t0.fit(X_scaled[control], Y[control], sample_weight=sw_c)

        self._fitted = True
        return self

    def estimate_ate(
        self,
        X: np.ndarray,
        T: np.ndarray,
        Y: np.ndarray,
        sample_weights: Optional[np.ndarray] = None,
    ) -> Tuple[float, float, Dict]:
        """
        双重稳健 ATE 估计

        Returns: (ate_dr, std_error, diagnostics)
        """
        if not self._fitted:
            raise RuntimeError("请先调用 fit()")

        X_scaled = self.scaler.transform(X)
        n = len(Y)
        w = sample_weights if sample_weights is not None else np.ones(n)

        # 倾向得分
        e_hat = np.clip(
            self.propensity_model.predict_proba(X_scaled)[:, 1], 0.05, 0.95
        )

        # 结果预测
        mu1_hat = self.outcome_model_t1.predict(X_scaled)
        mu0_hat = self.outcome_model_t0.predict(X_scaled)

        # DR 估计量(含采样权重)
        dr_scores = (
            (mu1_hat - mu0_hat)
            + T * (Y - mu1_hat) / e_hat
            - (1 - T) * (Y - mu0_hat) / (1 - e_hat)
        )
        ate_dr = float(np.average(dr_scores, weights=w))

        # 标准误(Delta 方法近似)
        se = float(np.sqrt(np.average((dr_scores - ate_dr) ** 2, weights=w) / n))

        # 诊断信息
        diagnostics = {
            "outcome_model_r2_treated": self.outcome_model_t1.score(X_scaled[T == 1], Y[T == 1]),
            "outcome_model_r2_control": self.outcome_model_t0.score(X_scaled[T == 0], Y[T == 0]),
            "propensity_mean": float(np.mean(e_hat)),
            "propensity_std": float(np.std(e_hat)),
            "effective_n": float(np.sum(w) ** 2 / np.sum(w ** 2)),   # 有效样本量
        }

        return ate_dr, se, diagnostics


class CausalDebiasingPipeline:
    """
    因果去偏完整管线
    整合偏差审计 → 选择模型 → DR 估计
    """

    def __init__(self):
        self.auditor = CrawlerBiasAuditor()
        self.selection_model = SelectionModelIPW()
        self.dr_estimator = DoubleRobustEstimator()

    def run(
        self,
        X_crawled: np.ndarray,             # 爬虫样本特征
        T_crawled: np.ndarray,             # 处理变量(0/1)
        Y_crawled: np.ndarray,             # 结果变量
        X_reference: np.ndarray,           # 参考总体特征(可为平台用户统计 mock)
        feature_names: List[str],
        population_means: Optional[Dict[str, float]] = None,
        review_counts: Optional[np.ndarray] = None,
    ) -> Dict:
        """
        完整因果去偏管线

        Returns: 包含原始估计、修正估计、诊断信息的结果字典
        """
        results = {}

        # Step 1: 偏差审计
        if population_means:
            bias_results = self.auditor.audit_feature_bias(
                X_crawled, population_means, feature_names
            )
            biased_features = [r.feature_name for r in bias_results if r.is_biased]
            results["biased_features"] = biased_features
            results["bias_audit"] = [
                {"feature": r.feature_name, "bias_ratio": round(r.bias_ratio, 3), "is_biased": r.is_biased}
                for r in bias_results
            ]

        # Step 2: 截断检测
        if review_counts is not None:
            truncation = self.auditor.test_truncation(review_counts)
            results["truncation_test"] = {
                "has_truncation": truncation.has_truncation,
                "ks_stat": round(truncation.ks_stat, 4),
                "p_value": round(truncation.p_value, 4),
                "spike_at_ceiling": round(truncation.spike_at_ceiling, 4),
                "extrapolation_factor": round(truncation.extrapolation_factor, 2),
            }

        # Step 3: 原始 ATE(无修正)
        naive_model = LinearRegression()
        naive_model.fit(np.column_stack([X_crawled, T_crawled.reshape(-1, 1)]), Y_crawled)
        naive_ate = float(naive_model.coef_[-1])
        results["naive_ate"] = round(naive_ate, 4)

        # Step 4: IPW 修正
        self.selection_model.fit_selection_model(X_crawled, X_reference)
        ipw_weights = self.selection_model.compute_ipw_weights(X_crawled)

        # 用加权 OLS 作为 IPW 参考
        ipw_model = LinearRegression()
        ipw_model.fit(
            np.column_stack([X_crawled, T_crawled.reshape(-1, 1)]),
            Y_crawled,
            sample_weight=ipw_weights,
        )
        ipw_ate = float(ipw_model.coef_[-1])
        results["ipw_ate"] = round(ipw_ate, 4)

        # Step 5: 双重稳健估计(推荐使用)
        self.dr_estimator.fit(X_crawled, T_crawled, Y_crawled, sample_weights=ipw_weights)
        dr_ate, dr_se, diagnostics = self.dr_estimator.estimate_ate(
            X_crawled, T_crawled, Y_crawled, sample_weights=ipw_weights
        )
        results["dr_ate"] = round(dr_ate, 4)
        results["dr_se"] = round(dr_se, 4)
        results["dr_ci_95"] = [round(dr_ate - 1.96 * dr_se, 4), round(dr_ate + 1.96 * dr_se, 4)]
        results["diagnostics"] = {k: round(v, 4) for k, v in diagnostics.items()}

        # 偏差修正量
        results["bias_correction"] = round(naive_ate - dr_ate, 4)
        results["relative_bias_pct"] = round((naive_ate - dr_ate) / max(abs(dr_ate), 0.001) * 100, 1)

        return results


# ── 端到端演示 ────────────────────────────────────────────────────────────

def demo_amazon_review_debiasing():
    """
    模拟 Amazon 评论爬虫偏差修正
    场景:折扣 coupon 对复购率的因果效应估计(存在选择偏差)
    真实 ATE = 0.09(地面真值)
    """
    np.random.seed(42)
    n_crawled = 2000       # 爬虫采集样本
    n_reference = 8000     # 参考总体(非评论买家)

    # 生成有偏爬虫样本(高参与度买家过采样)
    # 特征:[engagement_score, account_age_months, purchase_frequency, price_sensitivity]
    X_crawled = np.column_stack([
        np.random.beta(3, 1, n_crawled),                    # 高参与度(过采样)
        np.random.gamma(2, 12, n_crawled),                  # 账号年龄(月)
        np.random.poisson(8, n_crawled).astype(float),      # 购买频次
        np.random.beta(2, 3, n_crawled),                    # 价格敏感度
    ])

    # 参考总体(更接近真实分布)
    X_reference = np.column_stack([
        np.random.beta(1.5, 2, n_reference),                # 正常参与度分布
        np.random.gamma(3, 10, n_reference),
        np.random.poisson(4, n_reference).astype(float),
        np.random.beta(2, 2, n_reference),
    ])

    # 处理变量(是否收到折扣 coupon,30% 概率)
    T = (np.random.random(n_crawled) < 0.30).astype(float)

    # 结果变量(复购率):真实 ATE = 0.09,但高参与度买家的 coupon 效应被放大
    true_ate = 0.09
    Y = (
        0.25                                    # 基础复购率
        + true_ate * T                          # 真实 coupon 效应
        + 0.18 * X_crawled[:, 0]               # 参与度效应(confounding)
        + 0.05 * (T * X_crawled[:, 0] - 0.3)  # 交互项(高参与度 × 打折,放大效应)
        + np.random.normal(0, 0.05, n_crawled)
    )
    Y = np.clip(Y, 0, 1)

    # Mock 评论数(检测截断)
    review_counts = np.random.pareto(1.5, n_crawled) * 10 + 1
    review_counts = np.clip(review_counts, 1, 500).astype(int)  # 模拟截断

    # 运行去偏管线
    pipeline = CausalDebiasingPipeline()
    feature_names = ["engagement", "account_age", "purchase_freq", "price_sensitivity"]
    population_means = {
        "engagement": 0.43,         # 真实总体均值(低于样本均值)
        "account_age": 30.0,
        "purchase_freq": 4.0,
        "price_sensitivity": 0.50,
    }

    print("=== Amazon 评论偏差修正 Demo ===\n")
    results = pipeline.run(
        X_crawled=X_crawled,
        T_crawled=T,
        Y_crawled=Y,
        X_reference=X_reference,
        feature_names=feature_names,
        population_means=population_means,
        review_counts=review_counts,
    )

    print(f"真实 ATE(地面真值):  {true_ate:.4f}")
    print(f"原始爬虫样本 ATE:    {results['naive_ate']:.4f}  (偏差: {results['naive_ate']-true_ate:+.4f})")
    print(f"IPW 修正 ATE:        {results['ipw_ate']:.4f}  (偏差: {results['ipw_ate']-true_ate:+.4f})")
    print(f"双重稳健 DR ATE:     {results['dr_ate']:.4f}  (偏差: {results['dr_ate']-true_ate:+.4f})")
    print(f"95% CI:              [{results['dr_ci_95'][0]:.4f}, {results['dr_ci_95'][1]:.4f}]")
    print(f"相对偏差修正:        {results['relative_bias_pct']:.1f}%")
    print(f"\n偏差特征: {results.get('biased_features', [])}")
    print(f"截断检测: {results.get('truncation_test', {})}")
    print(f"诊断信息: 有效样本量 = {results['diagnostics']['effective_n']:.0f}")

    return results


def test_causal_debiasing():
    """测试用例"""
    np.random.seed(42)

    # 基础数据
    n = 500
    X = np.random.randn(n, 3)
    T = (X[:, 0] + np.random.randn(n) > 0).astype(float)
    Y = 0.5 * T + 0.3 * X[:, 0] + np.random.randn(n) * 0.1
    X_ref = np.random.randn(n, 3) * 0.8  # 略有差异的参考总体

    # 测试1:IPW 权重非负且有界
    sel_model = SelectionModelIPW()
    sel_model.fit_selection_model(X, X_ref)
    weights = sel_model.compute_ipw_weights(X)
    assert np.all(weights > 0), "IPW 权重应为正数"
    assert np.all(weights <= 20), "IPW 权重不应过大(已截断)"
    print(f"✓ 测试1: IPW 权重范围 [{weights.min():.2f}, {weights.max():.2f}]")

    # 测试2:DR 估计量接近真实值
    dr = DoubleRobustEstimator()
    dr.fit(X, T, Y)
    ate, se, diag = dr.estimate_ate(X, T, Y)
    assert abs(ate - 0.5) < 0.15, f"DR ATE 应接近 0.5,实得 {ate:.3f}"
    assert se > 0, "标准误应为正"
    print(f"✓ 测试2: DR ATE = {ate:.3f}(真实 0.5),SE = {se:.4f}")

    # 测试3:截断检测
    auditor = CrawlerBiasAuditor()
    # 生成有截断的评论数(截断在500以上)
    truncated_counts = np.concatenate([
        np.random.pareto(1.5, 200) * 10 + 1,
        np.full(50, 500)  # 人为截断
    ]).astype(int)
    test_result = auditor.test_truncation(truncated_counts)
    assert isinstance(test_result.has_truncation, bool), "截断检测应返回布尔值"
    assert test_result.extrapolation_factor >= 1.0, "外推因子应 >= 1"
    # 判据本身要成立:250 条里有 50 条钉在天花板 500 → 堆积占比 0.2,应判为截断,
    # 且估计出的截断点就是天花板 500(原实现把 p95 当截断点返回)
    assert test_result.has_truncation is True, test_result
    assert abs(test_result.spike_at_ceiling - 0.2) < 1e-9, test_result.spike_at_ceiling
    assert test_result.estimated_truncation_point == 500.0, test_result
    # 判别力对照:未截断的同分布样本必须判 False。
    # 原实现只按 KS 的 p 值判,而这条检验对「与拟合对数正态不符」一律显著——
    # 下面 clean_result 打印的 p 值就是证据(未截断样本同样远小于 0.05),
    # 于是 has_truncation 恒真、这个分支等于没有判据。
    clean_result = auditor.test_truncation(
        (np.random.pareto(1.5, 200) * 10 + 1).astype(int))
    assert clean_result.has_truncation is False, clean_result
    assert clean_result.spike_at_ceiling < auditor.min_ceiling_spike, clean_result
    print(f"✓ 测试3: 截断检测 KS={test_result.ks_stat:.4f}, "
          f"堆积={test_result.spike_at_ceiling:.2f}→{test_result.has_truncation}, "
          f"外推因子={test_result.extrapolation_factor:.2f};"
          f"未截断对照 堆积={clean_result.spike_at_ceiling:.3f}→{clean_result.has_truncation}"
          f"(但其 KS p={clean_result.p_value:.1e},单看 p 值它也会被报成截断)")

    # 测试4:无混淆夹具(T 独立随机分配)——只验证管线跑通且估计不跑偏,
    # 不验证「修正必然更好」
    # 生成有偏数据(高engagement组过采样)
    n_biased = 300
    X_biased = np.column_stack([
        np.random.beta(4, 1, n_biased),   # 高参与度
        np.random.randn(n_biased),
        np.random.randn(n_biased),
    ])
    X_ref_unbiased = np.column_stack([
        np.random.beta(1, 2, n_biased),   # 低参与度(总体分布)
        np.random.randn(n_biased),
        np.random.randn(n_biased),
    ])
    T_biased = (np.random.random(n_biased) < 0.4).astype(float)
    Y_biased = 0.2 * T_biased + 0.4 * X_biased[:, 0] + np.random.randn(n_biased) * 0.1

    pipeline = CausalDebiasingPipeline()
    res = pipeline.run(
        X_biased, T_biased, Y_biased, X_ref_unbiased,
        feature_names=["engagement", "f1", "f2"],
    )
    naive_bias = abs(res["naive_ate"] - 0.2)
    dr_bias = abs(res["dr_ate"] - 0.2)
    # 该夹具里 T 是独立随机分配的(T ⊥ X),没有混淆可修:重加权只会加方差。
    # 原注释写着「修正后偏差应小于原始」却没有任何断言兜着,实测(seed 42)是
    # 原始 0.010 → DR 0.018,即这条声明在本夹具下不成立。真正的方向性检验放在
    # 测试5(demo 的参与度既进选择机制又进结果方程,才是有混淆的场景)。
    assert naive_bias < 0.05, f"无混淆时原始估计应已接近真值: {naive_bias:.4f}"
    assert dr_bias < 0.05, f"无混淆时 DR 不应跑偏: {dr_bias:.4f}"
    print(f"✓ 测试4: 无混淆夹具(T⊥X)下两个估计都接近真值 "
          f"原始偏差={naive_bias:.3f}, DR修正后偏差={dr_bias:.3f}(不宣称 DR 更好)")

    # 测试5:端到端 demo
    results = demo_amazon_review_debiasing()
    assert "dr_ate" in results, "应包含 DR 估计结果"
    assert results["dr_ci_95"][0] < results["dr_ate"] < results["dr_ci_95"][1], "ATE 应在 CI 区间内"
    # 方向性检验:demo 的参与度同时进选择机制与结果方程(真有混淆),
    # 此时修正必须把估计往地面真值 0.09 拉,且 IPW 与 DR 都要比 naive 更接近真值
    assert abs(results["naive_ate"] - 0.09) > abs(results["ipw_ate"] - 0.09) > abs(results["dr_ate"] - 0.09), \
        (results["naive_ate"], results["ipw_ate"], results["dr_ate"])
    # demo 的评论数只被 clip 到 500(堆积 0.1%),低于判别下限 → 应判「检不出截断」而不是硬报。
    # 这条对照说明检测有边界,不是恒真开关。
    trunc = results["truncation_test"]
    assert trunc["has_truncation"] is False, trunc
    assert trunc["spike_at_ceiling"] < 0.05, trunc
    print(f"✓ 测试5: 端到端演示通过(demo 尾部 clip 仅堆积 {trunc['spike_at_ceiling']:.3f},"
          f"判 has_truncation={trunc['has_truncation']})")

    print("\n=== 全部测试通过 ===")


if __name__ == "__main__":
    np.random.seed(42)
    test_causal_debiasing()
print("[✓] Data Collection Causal De 测试通过")

8. 论文来源

论文链接用于核验方法来源,不自动证明本页商业结果。