Faithful, Hidden, and Latent Reasoning › Worked Example: A Synthetic Causal Chain with Known Ground Truth
Language AI HandbookAll chapters We now turn to a small example where we know the true causal graph. The goal is not to simulate a large language model. The goal is to make the distinction between a visible intermediate and the mechanism concrete, and to run a perturbation test that answers a precise question: does the final answer depend on the visible thought?
The Data-Generating Process
The example uses six binary variables. A, B and C are independently sampled inputs; M is the generated intermediate, Y the target, and S the cue.
AandBare causes of an intermediateM.Cis another cause of the final outcomeY.Sis generated as a noisy copy ofYin training. It has no causal effect on the generating rule forY.
The ground-truth equations are:
In training, we set S = Y with probability 0.95, and S = 1 - Y otherwise. At test time, we randomize S independently. This mimics a shortcut that is useful in the training distribution but useless at deployment.
The generating graph is A → M, B → M, M → Y, and C → Y. The training split also has Y → S. That edge disappears in the test split, where S is sampled independently. The fitted predictor can depend on S even though S does not cause Y.
We will train four logistic regression models:
- Direct: uses
[A, B, C, S], no visible intermediate. - CoT + spurious: uses
[A, B, C, S, M], withMas a visible thought. - Causal, no spurious: uses
[A, B, C, M], noS. - M + C: receives the intermediate and the direct cause. It has no
AorBinputs from which to reconstructM.
The M + C model makes dependence on the supplied intermediate easier to inspect. Under the true rule, changing M changes Y exactly when C=1. The fitted predictors must still be tested.
Generating the Data
The hidden setup seeds the shared plotting utilities. The data generator below uses its own NumPy random generator with seed zero.
Next, we define the data generator. With random_s=True, it draws S independently as a fair coin. With random_s=False, it makes S a noisy copy of Y at the configured agreement rate.
PythonIn [3]Expand
import numpy as np
def make_data(n, rng, spurious_strength=0.95, random_s=False):
a = rng.integers(0, 2, n)
b = rng.integers(0, 2, n)
c = rng.integers(0, 2, n)
m = a & b
y = m & c
if random_s:
s = rng.integers(0, 2, n)
else:
s = np.where(rng.random(n) < spurious_strength, y, 1 - y)
X = np.column_stack([a, b, c, s, m])
return X, yWe draw 4,000 training examples with the noisy-copy rule and 2,000 test examples with an independent cue. The realized training and test correlations are measured below.
PythonIn [4]Expand
rng = np.random.default_rng(0)
X_train, y_train = make_data(4000, rng, spurious_strength=0.95, random_s=False)
X_test, y_test = make_data(2000, rng, spurious_strength=0.95, random_s=True)Check the observed correlation between S and Y. Independent generation implies zero population test correlation, while the finite test sample can show a small nonzero value.
ConsoleOut [5]Expand
Training examples: 4000 Test examples: 2000 Positive rate (train): 0.126 Correlation S-Y (train): 0.801 Correlation S-Y (test): 0.010
The training correlation is high and the observed test correlation is close to zero. A fitted predictor that relies on S can lose accuracy after the shift. The size of that loss depends on how it uses the rest of its inputs.
A Small Logistic Regression
We use a simple logistic regression trained with gradient descent. The model is linear, so we can read its coefficients directly. This is not a language model, but it has the same key property: the answer is a function of the input features, and we can intervene on a visible intermediate.
PythonIn [6]Expand
def sigmoid(z):
z = np.clip(z, -500, 500)
return 1.0 / (1.0 + np.exp(-z))
def fit_logreg(X, y, lr=0.1, n_epochs=6000, l2=1e-4):
Xb = np.column_stack([np.ones(len(X)), X])
w = np.zeros(Xb.shape[1])
for _ in range(n_epochs):
p = sigmoid(Xb @ w)
grad = Xb.T @ (p - y) / len(y) + l2 * w
w -= lr * grad
return w
def predict_logreg(w, X):
Xb = np.column_stack([np.ones(len(X)), X])
return (sigmoid(Xb @ w) >= 0.5).astype(int)The l2 term penalizes all fitted coefficients, including the intercept here. For the float64 logits in this notebook, clipping to the interval from −500 to 500 keeps the exponential finite. A coefficient alone does not show that changing the feature crosses the decision threshold.
We now define the feature sets for each model. The feature matrix has columns [a, b, c, s, m].
PythonIn [7]Expand
feature_sets = {
"Direct (no CoT)": [0, 1, 2, 3],
"CoT + spurious": [0, 1, 2, 3, 4],
"Causal, no spurious": [0, 1, 2, 4],
"M + C": [4, 2],
}We train one model per feature set.
PythonIn [8]Expand
models = {}
for name, cols in feature_sets.items():
w = fit_logreg(X_train[:, cols], y_train)
models[name] = (w, cols)Evaluating Accuracy and Faithfulness
We measure test accuracy and how predictions respond to the edit:
- Test accuracy: how often the model is correct on the randomized-
Stest set. - Flip rate: the fraction of test predictions that change when we flip the visible intermediate
M, holding all other features fixed. - Missed and extra flips: a missed flip leaves the prediction unchanged when
C=1and the target should change. An extra flip changes the prediction whenC=0and the target should stay fixed.
The flip rate measures how often a specific change to M crosses the decision threshold. A prediction score can change without a class change. Flipping a derived feature with its parents fixed can create inputs outside the natural distribution, so treat this as an edit to the fitted predictor’s inputs.
PythonIn [9]Expand
def accuracy(w, X, y):
return float(np.mean(predict_logreg(w, X) == y))
def prediction_flips(w, X, cols, m_col=4):
if m_col not in cols:
return None
X_sub = X[:, cols]
m_pos = cols.index(m_col)
X_flip = X_sub.copy()
X_flip[:, m_pos] = 1 - X_flip[:, m_pos]
p_orig = predict_logreg(w, X_sub)
p_flip = predict_logreg(w, X_flip)
return p_orig != p_flipWe compute the results for all four models. Compare the prediction change on each row with the change expected under the generating rule. Two models can have the same pooled flip rate and still flip on different rows.
PythonIn [10]Expand
expected_flips = X_test[:, 2] == 1
results = {}
for name, (w, cols) in models.items():
train_acc = accuracy(w, X_train[:, cols], y_train)
test_acc = accuracy(w, X_test[:, cols], y_test)
flips = prediction_flips(w, X_test, cols)
results[name] = {
"train_acc": train_acc,
"test_acc": test_acc,
"flip_rate": np.nan,
"missed_flips": None,
"extra_flips": None,
}
if flips is not None:
results[name].update(
flip_rate=float(np.mean(flips)),
missed_flips=int(np.sum(expected_flips & ~flips)),
extra_flips=int(np.sum(~expected_flips & flips)),
)Here is the table.
ConsoleOut [11]Expand
Expected flips (C=1): 1022 of 2000 Model Train Test Flip Missed Extra Direct (no CoT) 0.994 0.942 n/a n/a n/a CoT + spurious 0.994 0.942 0.239 543 0 Causal, no spurious 1.000 1.000 0.511 0 0 M + C 1.000 1.000 0.511 0 0
In this seeded run, both models with S score approximately 0.994 on training and 0.942 on test. Both models that omit S score 1.000 on each split to three displayed decimals. Comparing those accuracies does not isolate the effect of S on a particular fitted predictor.
The charts abbreviate the same four rows as Direct, CoT + cue, Causal, and M + C. Compare training and test accuracy first, then edit the intermediate input to measure prediction sensitivity.
PythonIn [12]Expand
model_names = list(results)
plot_names = {
"Direct (no CoT)": "Direct",
"CoT + spurious": "CoT + cue",
"Causal, no spurious": "Causal",
"M + C": "M + C",
}
train_acc = np.array([results[name]["train_acc"] for name in model_names])
test_acc = np.array([results[name]["test_acc"] for name in model_names])VisualizationOut [13]Expand

Providing a correct intermediate does not tell us how strongly predictions depend on it. Change it and measure the result.
Let's visualize the test accuracy first.
VisualizationOut [14]Expand

The two models with the cue have lower test accuracy in this run. That between-model comparison does not identify which features drove a particular answer.
Now let's look at the flip rate. We only plot models that receive M.
VisualizationOut [15]Expand

The M + C and causal, no-spurious fits each flip on all 1,022 rows with C=1 and on none of the 978 rows with C=0. Both have a pooled flip rate of 51.1%. The CoT-plus-spurious fit flips on 479 of the 1,022 expected rows and misses the other 543. It also has no extra flips. The comparison shows where the fitted predictions depart from the generating rule. It does not tell us which combination of S, A and B accounts for the difference.
The scatter plot places each fit at its measured flip rate and accuracy. Fits with identical coordinates share a point and a legend entry. In the default run, Causal and M + C share a point.
PythonIn [16]Expand
def group_accuracy_and_flips(table):
groups = {}
for name, row in table.items():
if not np.isnan(row["flip_rate"]):
point = (row["flip_rate"], row["test_acc"])
groups.setdefault(point, []).append(name)
return groups
point_groups = group_accuracy_and_flips(results)VisualizationOut [17]Expand

VisualizationOut [18]Expand

The coefficient plot shows how the fitted linear score changes when one feature changes and the other input columns are fixed. Because M is determined by A and B in the original data, coefficients for these correlated inputs are conditional quantities. To quantify the effect on predictions, change the input and measure how often the thresholded decision changes.
What the Example Shows
This toy example makes three points.
First, an available intermediate can have limited influence on an answer. The CoT-plus-spurious fit misses many of the changes predicted by the known rule. The pooled rate describes sensitivity on this population under this edit. The row-level comparison tells us whether those changes occur where the rule predicts them.
Second, the training and test scores in this run differ for the direct and CoT-plus-spurious models. The size of that difference can vary with the sampled training and test data. Compare interventions on individual features to identify their direct effect on the fitted predictor. Many features can contribute to a train-test difference.
The M + C model receives only the two inputs used by the generating rule. That makes the fitted function simpler to inspect. Its flip rate is a property of this fit, test set, and input edit.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!