# 反事实模拟

反事实模拟是一种计算建模方法，它通过从因果模型模拟结果，来估计在未曾发生的干预或条件下本会发生什么。它被用于[流行病学](https://www.edgepedia.org/zh/epidemiology)、认知科学和机器学习，并以潜在结果框架为基础——在该框架中，每个单元在受处理下有结果 \( Y_{i}(1) \)，在对照下有结果 \( Y_{i}(0) \)，而两者中至多只有一个能被观测到。<sup>[1](https://arxiv.org/html/2604.01325)</sup><sup> • </sup><sup>[2](https://ftp.cs.ucla.edu/pub/stat_ser/r485.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 它产生什么 | 在未施加的干预下预测的结果或结果分布；个体效应 \( \tau_{i} = Y_{i}(1) - Y_{i}(0) \) 永远无法直接观测。<sup>[1](https://arxiv.org/html/2604.01325)</sup> |
| 结构因果模型中的核心计算 | 三个步骤：溯因（根据证据更新背景变量）、行动（替换被干预的方程）、预测。<sup>[2](https://ftp.cs.ucla.edu/pub/stat_ser/r485.pdf)</sup> |
| 主要的观察性算法 | g计算（参数化g公式）流程：拟合结果模型与混杂模型，插补反事实，向前模拟，再作差。<sup>[3](https://marginaleffects.com/chapters/gcomputation.html)</sup> |
| 所需假设 | 条件可交换性、正值性、一致性、无干扰，以及模型设定正确。<sup>[4](https://jech.bmj.com/content/76/11/960)</sup> |
| 关键起源 | Robins 1986年的参数化g公式与 Pearl 1995年的do演算。<sup>[5](https://doi.org/10.1016/0270-0255%2886%2990088-6)</sup><sup> • </sup><sup>[6](https://doi.org/10.1093/biomet/82.4.669)</sup> |
| 比较准确性 | 在一项四方法模拟研究中，g计算的偏差与方差最低，平均绝对偏差接近于零（最大值为 −0.028）。<sup>[7](https://www.nature.com/articles/s41598-020-65917-x)</sup> |
| 主要失效模式 | 未测量的混杂：遗漏一个混杂变量后，所有情景下偏差均上升，g计算的最低值为 0.456。<sup>[7](https://www.nature.com/articles/s41598-020-65917-x)</sup> |

## 工作原理

反事实是相对于因果模型而非仅相对于数据集定义的。在结构因果模型（SCM）中，每个变量是其父变量与一个误差项的函数；干预 \( \mathrm{do}(X = x) \) 会诱导出一个子模型 \( M_{x} \)，其中决定 \( X \) 的方程被替换为常数 \( X = x \)，而反事实 \( Y_{x}(u) \) 就是该子模型中 \( Y \) 的解。<sup>[2](https://ftp.cs.ucla.edu/pub/stat_ser/r485.pdf)</sup><sup> • </sup><sup>[8](https://jair.org/index.php/jair/article/download/15579/27053/39852)</sup> [计算](https://www.edgepedia.org/zh/computing) \( P(Y_{x} = y \mid e) \) 随后分三步进行：溯因，即在给定观测证据 \( e \) 的情况下更新背景变量 \( U \) 的分布；行动，即把 \( X \) 的方程替换为 \( X = x \)；预测，即计算修改后模型中 \( Y = y \) 的概率。<sup>[2](https://ftp.cs.ucla.edu/pub/stat_ser/r485.pdf)</sup>

在潜在结果表示下，SUTVA 下的观测结果为 \( Y_{i}^{\text{obs}} = D_{i} \cdot Y_{i}(1) + (1 - D_{i}) \cdot Y_{i}(0) \)，因此模拟必须补上这一对中缺失的那一个。<sup>[1](https://arxiv.org/html/2604.01325)</sup>

## 如何实施

观察性数据上的g计算工作流有三个核心动作：拟合一个控制混杂的统计模型；用拟合好的模型预测或插补每个个体在替代处理方案下的结果；比较聚合后的反事实预测以估计处理效应。<sup>[3](https://marginaleffects.com/chapters/gcomputation.html)</sup> 对于二值处理，g公式推广了经典标准化，给出平均处理效应为 \( \sum_{w} \left[ P(Y = 1 \mid A = 1, W = w) - P(Y = 1 \mid A = 0, W = w) \right] P(W = w) \)。<sup>[9](https://scholar.harvard.edu/files/malf/files/2012.09920.pdf)</sup>

在时变混杂下，该流程扩展为五步：为每个时变混杂拟合一个模型；拟合结果模型；在固定方案下向前模拟（对每个受试者，将处理设为所评估的方案，从拟合的混杂模型抽取混杂值，再抽取结果）；对比较方案重复上述过程；对模拟结果的均值作差。<sup>[10](https://casrai.org/guides/g-computation-time-varying-confounding)</sup> g公式要求一致性、正值性和序贯可交换性，以及混杂模型与结果模型设定正确；g计算需要这些模型正确，而逆概率加权需要处理（倾向性）模型正确。<sup>[10](https://casrai.org/guides/g-computation-time-varying-confounding)</sup> 由于g计算没有显式的方差公式，不确定性通常通过自助法获得。<sup>[11](https://link.springer.com/article/10.1186/s12874-023-01835-6)</sup><sup> • </sup><sup>[9](https://scholar.harvard.edu/files/malf/files/2012.09920.pdf)</sup> 当因果模型以参数形式完全已知时，带有离散与连续变量条件的反事实分布可以直接通过一种可解释为粒子滤波的算法来模拟，并具有渐近有效的推断。<sup>[8](https://jair.org/index.php/jair/article/download/15579/27053/39852)</sup>

## 起源

潜在结果框架最早由 Rubin 于1974年在《教育心理学杂志》上应用于观察性研究中的统计推断。<sup>[12](https://doi.org/10.1037/h0037350)</sup> Robins 于1986年在《数学建模》上关于健康工人幸存者效应的研究中引入了参数化g公式。<sup>[5](https://doi.org/10.1016/0270-0255%2886%2990088-6)</sup> Pearl 1995年发表于《[Biometrika](https://www.edgepedia.org/zh/biometrika)》的论文引入了do算子与do演算，通过从非参数结构模型中移除方程来定义干预。<sup>[6](https://doi.org/10.1093/biomet/82.4.669)</sup>

## 变体

**边际结构模型。** 边际结构模型（MSM）由 Robins、Hernán 和 Brumback 于2000年在《流行病学》上提出。<sup>[13](https://doi.org/10.1097/00001648-200009000-00011)</sup> MSM 是一类因果模型，用于估计当时变协变量同时既是混杂又是中间变量时时变暴露的效应；其参数由逆处理概率加权（IPTW）估计量一致估计，后者通过权重而非作为回归变量来调整时变协变量。<sup>[13](https://doi.org/10.1097/00001648-200009000-00011)</sup>

**双重稳健与目标估计量。** TMLE 是一种半参数、双重稳健的替换估计量，允许用灵活的机器学习方法估计冗余函数，并且只要结果模型或处理模型之一被一致估计，它便一致；与相关的 AIPTW 估计量不同，它使估计值保持在参数范围内。<sup>[14](https://pmc.ncbi.nlm.nih.gov/articles/PMC6032875/)</sup> 增广双重稳健估计量 \( E[Y(a)] = E[Y(a) - \mu(a)(L)] + E[\mu(a)(L)] \) 在倾向性模型或建模均值之一被正确设定时一致。<sup>[15](https://discovery.ucl.ac.uk/id/eprint/10111286/1/sim.8741.pdf)</sup> 在流行病建模中，单世界方法通过潜在流行病图，将受控流行病的模拟与其精确的未受控反事实相匹配。Valentyn Melnychuk、Dennis Frauen 和 Stefan Feuerriegel（2022，arXiv）的因果 Transformer 将深度序列模型应用于反事实结果估计。<sup>[16](https://doi.org/10.48550/arxiv.2204.07258)</sup>

## 应用

在流行病学中，g计算和 MSM 用于估计队列中持续处理策略的效应，这些队列存在时变混杂，例如 HIV 队列数据结构中的 HAART 与无 HAART 对比。<sup>[13](https://doi.org/10.1097/00001648-200009000-00011)</sup><sup> • </sup><sup>[17](https://people.maths.bris.ac.uk/~maxvd/WH_VD_SIM.pdf)</sup> 流行病政策模拟使用匹配的受控与未受控流行病实现来评估干预。在因果认知中，Tobias Gerstenberg（2024）的反事实模拟模型将结构因果模型与力动力学模型相结合，通过在诸如物理引擎的生成模型上计算反事实对比，来预测人们的因果判断与责任判断。<sup>[18](https://doi.org/10.31234/osf.io/72scr)</sup> 在机器学习中，反事实模拟支持信用评分中的公平性分析，其中预测模型可能是一个不透明的 AI 系统，而反事实如今被常规性地用于理解复杂 AI 系统如何运作。<sup>[8](https://jair.org/index.php/jair/article/download/15579/27053/39852)</sup>

## 局限与替代方法

**外推与模型依赖。** 关于离观测数据越远的反事实的推断越依赖于模型，这一结果是在不假设特定函数形式的情况下证明的；位于协变量空间凸包内的反事实涉及插值，位于其外的涉及外推，而观察性数据偏差可分解为遗漏变量、处理后、插值与外推四个分量。<sup>[19](https://gking.harvard.edu/files/counterft.pdf)</sup> 当稀疏性和接近非正值性造成可识别性问题时，仅靠g计算便依赖外推。<sup>[9](https://scholar.harvard.edu/files/malf/files/2012.09920.pdf)</sup>

**假设违背。** 正值性要求每个个体被分配到任一处理组的概率都非零；倾向性评分方法对违背、甚至接近违背尤为敏感，而g计算似乎对非正值性更为稳健。<sup>[4](https://jech.bmj.com/content/76/11/960)</sup><sup> • </sup><sup>[20](https://pmc.ncbi.nlm.nih.gov/articles/PMC10107671/)</sup> 来自未测量混杂的偏差无法完全校正，除非该变量被测量并纳入。<sup>[11](https://link.springer.com/article/10.1186/s12874-023-01835-6)</sup> 在个体级模拟模型中，参数化所需的直接效应往往定义不清，例如在保持 CD4 计数不变时处理对死亡的直接效应，因为没有任何可行的干预能固定 CD4 计数；当干预定义不清时，混杂因素也定义不清。<sup>[21](https://journals.sagepub.com/doi/full/10.1177/0272989X19894940)</sup>

**比较表现。** 在一项比较g计算、IPTW、全匹配和 TMLE 的二值处理模拟中，无论协变量集如何，g计算的偏差与方差最低；在2,000名个体下，所有方法的偏差都接近零，功效高于95%。<sup>[7](https://www.nature.com/articles/s41598-020-65917-x)</sup> 在外部对照试验中，g计算在大多数情形下具有最小的 RMSE。<sup>[11](https://link.springer.com/article/10.1186/s12874-023-01835-6)</sup>

**替代方法。** 观察性方法，包括匹配、逆概率加权、工具变量、双重差分、回归断点以及合成控制，用假设来替代缺失的反事实数据。<sup>[1](https://arxiv.org/html/2604.01325)</sup> 倾向性评分匹配和敏感性分析限制观察性数据中的偏差，但敏感性分析只揭示在指定偏差参数下的结果范围。<sup>[22](https://www.annualreviews.org/content/journals/10.1146/annurev.publhealth.21.1.121)</sup><sup> • </sup><sup>[23](https://link.springer.com/article/10.1186/1471-2288-5-28)</sup> IV 分析估计依从者的局部平均处理效应，而依从者群体无法被精确识别，这可能限制政策相关性；两阶段最小二乘法对连续结果表现良好。<sup>[4](https://jech.bmj.com/content/76/11/960)</sup><sup> • </sup><sup>[20](https://pmc.ncbi.nlm.nih.gov/articles/PMC10107671/)</sup> 一项对127项模拟研究的范围综述发现，处理混杂没有单一表现最佳的方法。<sup>[20](https://pmc.ncbi.nlm.nih.gov/articles/PMC10107671/)</sup>

## References

1. [The Digital Twin Counterfactual Framework: A Validation Architecture for Simulated Potential Outcomes (arXiv)](https://arxiv.org/html/2604.01325)
2. [Causal and Counterfactual Inference (Pearl, UCLA technical report R-485)](https://ftp.cs.ucla.edu/pub/stat_ser/r485.pdf)
3. [Causal inference with G-computation – Model to Meaning](https://marginaleffects.com/chapters/gcomputation.html)
4. [Causal inference and effect estimation using observational data (JECH glossary)](https://jech.bmj.com/content/76/11/960)
5. [A new approach to causal inference in mortality studies with a sustained exposure period—application to control of the healthy worker survivor effect (Mathematical Modelling, 1986)](https://doi.org/10.1016/0270-0255%2886%2990088-6)
6. [JUDEA PEARL (1995). Causal diagrams for empirical research. Biometrika.](https://doi.org/10.1093/biomet/82.4.669)
7. [G-computation, propensity score-based methods, and TMLE for causal inference with different covariates sets: a comparative simulation study (Scientific Reports)](https://www.nature.com/articles/s41598-020-65917-x)
8. [Counterfactual simulation in structural causal models (JAIR; arXiv 2306.15328)](https://jair.org/index.php/jair/article/download/15579/27053/39852)
9. [Causal inference methods for observational studies: G-formula, IPTW, and double robust estimators (tutorial)](https://scholar.harvard.edu/files/malf/files/2012.09920.pdf)
10. [G-Computation: Simulating Counterfactual Outcomes for Time-Varying Confounding (CASRAI guide)](https://casrai.org/guides/g-computation-time-varying-confounding)
11. [Comparing g-computation, PS-based weighting, and TMLE for externally controlled trials with measured and unmeasured confounders: a simulation study (BMC Medical Research Methodology)](https://link.springer.com/article/10.1186/s12874-023-01835-6)
12. [Donald B. Rubin (1974). Estimating causal effects of treatments in randomized and nonrandomized studies.. Journal of Educational Psychology.](https://doi.org/10.1037/h0037350)
13. [James M. Robins, Miguel Ángel Hernán, Babette Brumback (2000). Marginal Structural Models and Causal Inference in Epidemiology. Epidemiology.](https://doi.org/10.1097/00001648-200009000-00011)
14. [Targeted maximum likelihood estimation for a binary treatment: A tutorial (PMC)](https://pmc.ncbi.nlm.nih.gov/articles/PMC6032875/)
15. [Formulating causal questions and principled statistical answers (Statistics in Medicine tutorial, UCL repository)](https://discovery.ucl.ac.uk/id/eprint/10111286/1/sim.8741.pdf)
16. [Melnychuk, Valentyn, Frauen, Dennis, Feuerriegel, Stefan (2022). Causal Transformer for Estimating Counterfactual Outcomes. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2204.07258)
17. [Simulating from Marginal Structural Models with Time-Dependent Confounding](https://people.maths.bris.ac.uk/~maxvd/WH_VD_SIM.pdf)
18. [Tobias Gerstenberg (2024). Counterfactual simulation in causal cognition. .](https://doi.org/10.31234/osf.io/72scr)
19. [The Dangers of Extreme Counterfactuals (King & Zeng)](https://gking.harvard.edu/files/counterft.pdf)
20. [Dealing with confounding in observational studies: scoping review of simulation studies (PMC)](https://pmc.ncbi.nlm.nih.gov/articles/PMC10107671/)
21. [The Challenges of Parameterizing Direct Effects in Individual-Level Simulation Models (Medical Decision Making)](https://journals.sagepub.com/doi/full/10.1177/0272989X19894940)
22. [Causal Effects in Clinical and Epidemiological Studies Via Potential Outcomes (Little & Rubin, 2000)](https://www.annualreviews.org/content/journals/10.1146/annurev.publhealth.21.1.121)
23. [Causal inference based on counterfactuals (BMC Medical Research Methodology)](https://link.springer.com/article/10.1186/1471-2288-5-28)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/counterfactual-simulation

English original: https://www.edgepedia.org/counterfactual-simulation
