AI for Econometricians — browser lab

Attention and Transformers: Learned Data-Dependent Weighting

Three deterministic experiments with four token positions. All vectors and head projections here are fixed for illustration; no model is trained. Predict the effect of a control change before inspecting the calculated matrices.

Experiment 1 — Calculate an attention-weighted average

Scores are query–key dot products divided by √2 × temperature. The mask excludes forbidden keys before softmax. The heatmap and table show the same exact weights.

Scaled scores (masked cells show −∞)

Attention weights (rows sum to 1)

What is the fixed-weight comparison?

A uniform average over the permitted value vectors uses a constant 1 / allowed positions weight. Attention instead uses query–key scores to calculate different normalized weights. Neither operation alone estimates an econometric causal effect.

Experiment 2 — Can a future token change earlier outputs?

Change the fourth token's query, key, and value. Compare attention outputs for positions 1–3 with and without a causal mask. The temperature remains the value chosen in Experiment 1.

Next-token targets must also be shifted

Input prefix availablePredict next
Thecentral
The centralbank
The central bankraised
The central bank raisedrates

The current token is visible at its own position; the target is the following token. This is a data-alignment rule, not a second kind of mask.

What counts as causality here?

A causal mask prevents future-token look-ahead. It does not identify cause-and-effect relationships among words or economic variables.

Experiment 3 — Position and transformer components

First test a mathematical property of unmasked content-only attention: reordering token rows reorders output rows. Fixed position vectors break that comparison when token content moves but position vectors stay tied to places.

Block illustration: two different fixed head projections, average the two head outputs as a simple output projection, add an input residual, apply a fixed position-wise ReLU map, then add another residual. Layer normalization is omitted from this toy calculation, though it belongs in the full block discussed in the lesson.

Lesson notes — Course home — Python code — R code