AI因果推断:从相关性到因果性的跨越 📅 2026/7/15 21:00:52 AI因果推断从相关性到因果性的跨越相关性不等于因果性是统计学第一课但在实际决策中我们真正需要的是因果知识。AI因果推断致力于从数据中发现因果关系回答如果...会怎样What if的反事实问题。本文将系统介绍因果推断的核心框架、方法和工具帮助数据科学家建立因果思维。一、为什么需要因果推断1.1 相关性的陷阱import numpy as np import pandas as pd # 经典的混淆变量案例冰淇淋销量与溺水事件 np.random.seed(42) n 1000 # 混淆变量温度 temperature np.random.normal(25, 10, n) # 冰淇淋销量受温度影响 ice_cream_sales 100 5 * temperature np.random.normal(0, 20, n) # 溺水事件也受温度影响 drowning_incidents 2 0.3 * temperature np.random.normal(0, 2, n) # 相关性很高 correlation np.corrcoef(ice_cream_sales, drowning_incidents)[0, 1] print(fCorrelation: {correlation:.3f}) # 输出: ~0.8 (强相关) # 但禁止冰淇淋销售不会减少溺水事件 # 温度是混淆变量同时影响两者1.2 因果推断 vs 传统ML| 维度 | 传统机器学习 | 因果推断 | |------|-------------|----------| | 目标 | 预测 P(Y|X) | 估计 P(Y|do(X)) | | 问题类型 | 关联 | 干预效果 | | 数据需求 | 观测数据 | 可能需要实验数据 | | 可解释性 | 黑盒 | 显式因果结构 | | 应用场景 | 预测、分类 | 决策、政策评估 |二、因果推断的理论框架2.1 潜在结果框架Rubin Causal Modelclass PotentialOutcomesFramework: 潜在结果框架 def __init__(self): 对于每个个体i: - Y_i(1): 接受处理时的结果 - Y_i(0): 未接受处理时的结果 - T_i: 处理指示变量 个体处理效应: τ_i Y_i(1) - Y_i(0) 但只能观测到一个: Y_i T_i * Y_i(1) (1-T_i) * Y_i(0) pass def average_treatment_effect(self, Y, T): ATE E[Y(1) - Y(0)] E[Y|do(T1)] - E[Y|do(T0)] 在随机化实验中: ATE E[Y|T1] - E[Y|T0] 在观测数据中需要调整混淆变量 treated Y[T 1] control Y[T 0] return np.mean(treated) - np.mean(control) def conditional_average_treatment_effect(self, Y, T, X): CATE(x) E[Y(1) - Y(0) | Xx] 异质性处理效应 cate_estimates {} for x_val in np.unique(X): mask X x_val treated Y[(T 1) mask] control Y[(T 0) mask] if len(treated) 0 and len(control) 0: cate_estimates[x_val] np.mean(treated) - np.mean(control) return cate_estimates2.2 结构因果模型Pearl框架class StructuralCausalModel: 结构因果模型 def __init__(self, graph): graph: 有向无环图 (DAG) 每个节点: X f(PA_X, U_X) PA_X: 父节点, U_X: 外生噪声 self.graph graph self.structural_equations {} def add_equation(self, node, equation): 添加结构方程 self.structural_equations[node] equation def intervene(self, node, value): do(Xx):