Inferensys

Glossary

Causal Mediation Analysis

A statistical framework adapted for neural networks to quantify the contribution of a specific intermediate variable or neuron to a model's output by measuring the indirect effect through that mediator.
Governance lead reviewing model governance framework on laptop, policy documents visible, executive office setup.
NEURAL NETWORK INTERPRETABILITY

What is Causal Mediation Analysis?

Causal mediation analysis is a statistical framework adapted for neural networks to quantify the contribution of a specific intermediate variable or neuron to a model's output by measuring the indirect effect through that mediator.

Causal mediation analysis decomposes the total effect of an input on a model's output into a direct effect and an indirect effect that flows through a hypothesized mediator, such as a specific neuron, attention head, or activation. By systematically intervening on the mediator while holding other pathways constant, researchers can isolate whether a particular component is causally necessary for a behavior, rather than merely correlated with it. This framework, rooted in the potential outcomes formalism, provides a rigorous statistical basis for testing mechanistic hypotheses about internal model computations.

In practice, the technique involves running three forward passes: a clean baseline, a corrupted input pass, and a patched pass where the mediator's activation is restored to its clean state while the rest of the network remains corrupted. The average indirect effect is computed as the difference in output between the patched and fully corrupted conditions. Tools like activation patching and causal scrubbing operationalize this logic to identify circuits responsible for factual recall, syntactic agreement, and in-context learning, making causal mediation a cornerstone of modern mechanistic interpretability.

CAUSAL MEDIATION ANALYSIS

Core Characteristics

A statistical framework adapted for neural networks to quantify the contribution of a specific intermediate variable or neuron to a model's output by measuring the indirect effect through that mediator.

01

Total Effect Decomposition

The foundational principle of causal mediation analysis is the decomposition of a total causal effect into distinct pathways. For a neural network, this means separating the effect of an input on the output into a direct effect (bypassing the mediator) and an indirect effect (flowing through the target neuron or layer). This is formalized using the Average Causal Effect (ACE) framework, which contrasts counterfactual outcomes under different intervention states. In mechanistic interpretability, this allows researchers to quantify precisely how much a specific attention head contributes to a factual recall task versus the contribution of the residual stream bypassing it.

Direct + Indirect
Effect Components
03

Causal Tracing for Factual Recall

Causal Tracing is a specific causal mediation protocol designed to locate where factual knowledge is stored in transformer models. The process systematically corrupts the input embeddings (e.g., by adding noise to the subject entity) and then iteratively restores clean hidden states at each layer and token position. The indirect effect is measured by observing the probability of the correct factual output. A sharp increase in restoration probability at a specific MLP layer and the last subject token position indicates a causal locus of the stored fact. This method provided the foundational evidence for the Knowledge Neuron hypothesis.

MLP Layers
Primary Factual Locus
04

Natural vs. Counterfactual Mediators

Causal mediation analysis distinguishes between two types of indirect effects to handle complex interactions:

  • Natural Indirect Effect (NIE): The effect transmitted through the mediator when it is set to the value it would naturally take under the treatment condition. This preserves the dependence between the input and the mediator.
  • Controlled Direct Effect (CDE): The effect of the input when the mediator is forcibly fixed to a specific constant value for all samples.

In neural network terms, the NIE corresponds to patching with a clean-run activation (preserving computation), while the CDE corresponds to clamping a neuron to a fixed value (e.g., zero-ablation), which can introduce out-of-distribution artifacts.

05

Path-Specific Effect Isolation

Advanced causal mediation isolates path-specific effects to disentangle complex circuits. Rather than treating a neuron as a single mediator, this approach traces specific computational paths through the network graph. For example, in an Induction Head circuit, one can measure the effect transmitted solely through the path: [Previous Token] -> [K-composition with QK circuit] -> [V-composition with OV circuit] -> [Output]. This is achieved by holding all other downstream paths constant while intervening only on the edges of the hypothesized circuit, providing rigorous validation for circuit analysis.

CAUSAL MEDIATION ANALYSIS

Frequently Asked Questions

Clear, technically precise answers to the most common questions about applying causal mediation analysis to neural networks and transformer architectures.

Causal mediation analysis in neural networks is a statistical framework adapted from epidemiology that quantifies how much of a model's output effect flows through a specific intermediate variable, neuron, or activation pathway. It decomposes the total effect of an input change on the output into a direct effect (bypassing the mediator) and an indirect effect (transmitted through the mediator). In mechanistic interpretability, this allows researchers to measure the causal contribution of a specific attention head, MLP neuron, or residual stream direction to a behavior. The technique relies on counterfactual interventions—running the model with the mediator set to the value it would take under a different input—to isolate the mediated pathway from other computational routes.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.