Counterfactual reasoning: an analysis of in-context emergence

Moritz Miller, Bernhard Schölkopf, Siyuan Guo
6/5/2025
cs.CLcs.AIcs.LG

Official code: mrtzmllr/iccr (3 stars, MIT, last commit 2025-10-22). 1 more implementation below.

Abstract

Large-scale neural language models exhibit remarkable performance in in-context learning: the ability to learn and reason about the input context on the fly. This work studies in-context counterfactual reasoning in language models, that is, the ability to predict consequences of a hypothetical scenario. We focus on a well-defined, synthetic linear regression task that requires noise abduction. Accurate prediction is based on (1) inferring an unobserved latent concept and (2) copying contextual noise from factual observations. We show that language models are capable of counterfactual reasoning. Further, we enhance existing identifiability results and reduce counterfactual reasoning for a broad class of functions to a transformation on in-context observations. In Transformers, we find that self-attention, model depth and pre-training data diversity drive performance. Moreover, we provide mechanistic evidence that the latent concept is linearly represented in the residual stream and we introduce designated \textit{noise abduction heads} central to performing counterfactual reasoning. Lastly, our findings extend to counterfactual reasoning under SDE dynamics and reflect that Transformers can perform noise abduction on sequential data, providing preliminary evidence on the potential for counterfactual story generation. Our code is available under https://github.com/mrtzmllr/iccr.

View on arXivView PDF

Code Implementations(2)

mrtzmllr/iccrOfficial100%
MIT

Maintained repository for our paper "Counterfactual reasoning: an analysis of in-context emergence"

30Shell, PythonOct 20, 202512 months agoMIT
MIT

This repository contains the code and models for our paper: Counterfactual reasoning: an analysis of in-context emergence

21PythonJun 4, 20251 years agoMIT

Similar Papers

Cite this paper

@article{miller2025counterfactual,
  title  = {Counterfactual reasoning: an analysis of in-context emergence},
  author = {Moritz Miller and Bernhard Schölkopf and Siyuan Guo},
  year   = {2025},
  eprint = {2506.05188},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2506.05188v2}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2506.05188.svg)](https://codeofpaper.com/paper/2506.05188)

Discussion