Math for Causal Inference

Day 17. Matrix Multiplication, Transpose & the Design Matrix

Multiplying matrices, transposing products, and the cross-product matrices X′X and X′y that every regression is built from.

Video coming soon
Learning outcomes
  • ●Multiply conformable matrices and read Ax as a linear combination of the columns of A.
  • ●Prove (AB)′ = B′A′ and tr(AB) = tr(BA).
  • ●Build X′X and X′y from data and interpret their entries as sums of squares and cross-products.
  • ●Use the centring matrix M = I − (1/n)11′ to compute deviations from the mean.
2-HOUR SESSION PLAN
Theory 25′Warm-up (1–5) 20′Proofs (6–10) 35′Applied (11–15) 25′Python & Lab 15′
Colab notebook (.ipynb) Jump to the simulator

Theory

The product

If AA is m×nm \times n and BB is n×pn \times p, the product C=ABC = AB is m×pm \times p with

cij=∑ℓ=1naiℓbℓj=(row i of A)⋅(column j of B).c_{ij} = \sum_{\ell=1}^n a_{i\ell}b_{\ell j} = (\text{row } i \text{ of } A) \cdot (\text{column } j \text{ of } B).

The inner dimensions must match ("conformable"). Matrix multiplication is associative and distributive but not commutative: in general AB≠BAAB \ne BA, and one of them may not even exist.

Two ways to read Ax

Ax=∑jxj a⋅jAx = \sum_{j} x_j\,a_{\cdot j}

is a linear combination of the columns of AA with weights xjx_j. So XβX\beta, the vector of fitted values, is "β0\beta_0 times the intercept column plus β1\beta_1 times the education column plus …". Alternatively, entry ii of AxAx is row ii of AA dotted with xx: unit ii's prediction.

A matrix also acts as a linear transformation: it maps vectors to vectors, preserving sums and scalar multiples. In 2D you can watch it stretch, rotate, shear or flip the plane (Simulation Lab).

Transpose and trace

(AB)⊤=B⊤A⊤,tr⁡(A)=∑iaii,tr⁡(AB)=tr⁡(BA).(AB)^{\top} = B^{\top}A^{\top}, \qquad \operatorname{tr}(A) = \sum_i a_{ii}, \qquad \operatorname{tr}(AB) = \operatorname{tr}(BA).

The cross-product matrices

For a design matrix XX (n×kn \times k) and outcome yy (n×1n \times 1):

X⊤X=[∑ixijxiℓ]j,ℓ,X⊤y=[∑ixijyi]j.X^{\top}X = \Big[\sum_i x_{ij}x_{i\ell}\Big]_{j,\ell}, \qquad X^{\top}y = \Big[\sum_i x_{ij}y_i\Big]_{j}.

With an intercept and one regressor,

X⊤X=(n∑xi∑xi∑xi2),X⊤y=(∑yi∑xiyi).X^{\top}X = \begin{pmatrix} n & \sum x_i \\ \sum x_i & \sum x_i^2 \end{pmatrix}, \qquad X^{\top}y = \begin{pmatrix} \sum y_i \\ \sum x_iy_i \end{pmatrix}.

On Day 27 the OLS estimator will be β^=(X⊤X)−1X⊤y\hat\beta = (X^{\top}X)^{-1}X^{\top}y: everything it needs is in these two objects.

The centring matrix

M1=In−1n11⊤,M1y=y−yˉ1.M_{\mathbf 1} = I_n - \tfrac1n\mathbf{1}\mathbf{1}^{\top}, \qquad M_{\mathbf 1}y = y - \bar y\mathbf{1}.

Multiplying by M1M_{\mathbf 1} subtracts the mean, the matrix version of "demeaning", which becomes fixed-effects estimation on Day 28.

Part 1 · Warm-up

Exercise 1 · Order matters

Let A=(1234)A = \begin{pmatrix}1 & 2\\3 & 4\end{pmatrix} and B=(0110)B = \begin{pmatrix}0 & 1\\1 & 0\end{pmatrix}. Compute ABAB and BABA.

Show solution

AB=(2143)AB = \begin{pmatrix}2 & 1\\4 & 3\end{pmatrix} (columns of AA swapped), BA=(3412)BA = \begin{pmatrix}3 & 4\\1 & 2\end{pmatrix} (rows swapped). They differ.

Exercise 2 · Conformability

AA is 2×32 \times 3 and BB is 3×43 \times 4. Which of ABAB, BABA, A⊤AA^{\top}A, B⊤A⊤B^{\top}A^{\top} exist, and what shapes are they?

Show solution

Step 1. ABAB: 2×42 \times 4.

Step 2. BABA: not defined (4≠24 \ne 2).

Step 3. A⊤AA^{\top}A: 3×33 \times 3.

Step 4. B⊤A⊤B^{\top}A^{\top}: 4×24 \times 2 (it equals (AB)⊤(AB)^{\top}).

Exercise 3 · Columns, combined

A=(1231)A = \begin{pmatrix}1 & 2\\3 & 1\end{pmatrix}, x=(1,1)⊤x = (1, 1)^{\top}. Compute AxAx both row-by-row and as a combination of columns.

Show solution

Rows: (1+2,  3+1)=(3,4)(1 + 2,\; 3 + 1) = (3, 4). Columns: 1⋅(1,3)+1⋅(2,1)=(3,4)1\cdot(1, 3) + 1\cdot(2, 1) = (3, 4).

Exercise 4 · Transpose of a product

Check (AB)⊤=B⊤A⊤(AB)^{\top} = B^{\top}A^{\top} for the matrices in Exercise 1.

Show solution

(AB)⊤=(2413)(AB)^{\top} = \begin{pmatrix}2 & 4\\1 & 3\end{pmatrix}. B⊤A⊤=(0110)(1324)=(2413)B^{\top}A^{\top} = \begin{pmatrix}0 & 1\\1 & 0\end{pmatrix}\begin{pmatrix}1 & 3\\2 & 4\end{pmatrix} = \begin{pmatrix}2 & 4\\1 & 3\end{pmatrix}. ✓

Exercise 5 · Trace

Compute tr⁡(A)\operatorname{tr}(A), tr⁡(AB)\operatorname{tr}(AB) and tr⁡(BA)\operatorname{tr}(BA) for Exercise 1.

Show solution

tr⁡(A)=5\operatorname{tr}(A) = 5, tr⁡(AB)=2+3=5\operatorname{tr}(AB) = 2 + 3 = 5, tr⁡(BA)=3+2=5\operatorname{tr}(BA) = 3 + 2 = 5. Equal even though AB≠BAAB \ne BA.

Part 2 · Proofs

Exercise 6 · Transpose of a product

Prove (AB)⊤=B⊤A⊤(AB)^{\top} = B^{\top}A^{\top}.

Show solution

[(AB)⊤]ij=(AB)ji=∑ℓajℓbℓi=∑ℓ(B⊤)iℓ(A⊤)ℓj=(B⊤A⊤)ij[(AB)^{\top}]_{ij} = (AB)_{ji} = \sum_\ell a_{j\ell}b_{\ell i} = \sum_\ell (B^{\top})_{i\ell}(A^{\top})_{\ell j} = (B^{\top}A^{\top})_{ij}.

Exercise 7 · Cyclic trace

Prove tr⁡(AB)=tr⁡(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA) for AA (m×nm \times n) and BB (n×mn \times m).

Show solution

tr⁡(AB)=∑i∑ℓaiℓbℓi=∑ℓ∑ibℓiaiℓ=tr⁡(BA)\operatorname{tr}(AB) = \sum_i\sum_\ell a_{i\ell}b_{\ell i} = \sum_\ell\sum_i b_{\ell i}a_{i\ell} = \operatorname{tr}(BA). Day 20 uses this to show tr⁡(P)=k\operatorname{tr}(P) = k.

Exercise 8 · The structure of X′X

Show that X⊤XX^{\top}X is symmetric, that its diagonal holds the sums of squares of the columns, and derive its form with an intercept and one regressor.

Show solution

Step 1. (X⊤X)⊤=X⊤(X⊤)⊤=X⊤X(X^{\top}X)^{\top} = X^{\top}(X^{\top})^{\top} = X^{\top}X.

Step 2. Entry (j,ℓ)(j, \ell) is column jj dotted with column ℓ\ell, so the diagonal is ∑ixij2\sum_i x_{ij}^2.

Step 3. With columns 1\mathbf 1 and xx: 1⊤1=n\mathbf 1^{\top}\mathbf 1 = n, 1⊤x=∑xi\mathbf 1^{\top}x = \sum x_i, x⊤x=∑xi2x^{\top}x = \sum x_i^2.

Exercise 9 · Quadratic forms

Show that u⊤Au=∑i∑jaijuiuju^{\top}Au = \sum_i\sum_j a_{ij}u_iu_j, and write u⊤Auu^{\top}Au for A=(2113)A = \begin{pmatrix}2 & 1\\1 & 3\end{pmatrix}.

Show solution

(Au)i=∑jaijuj(Au)_i = \sum_j a_{ij}u_j, so u⊤Au=∑iui∑jaijuju^{\top}Au = \sum_i u_i\sum_j a_{ij}u_j. Here u⊤Au=2u12+2u1u2+3u22u^{\top}Au = 2u_1^2 + 2u_1u_2 + 3u_2^2 (the off-diagonal 1 appears twice).

Exercise 10 · X′X is positive semi-definite

Show that v⊤X⊤Xv≥0v^{\top}X^{\top}Xv \ge 0 for every vv.

Show solution

Step 1. v⊤X⊤Xv=(Xv)⊤(Xv)=∥Xv∥2≥0v^{\top}X^{\top}Xv = (Xv)^{\top}(Xv) = \|Xv\|^2 \ge 0.

Step 2. It is zero only if Xv=0Xv = 0, i.e. if some combination of the columns vanishes: the collinearity problem of Day 18.

Part 3 · Applied

Exercise 11 · Cross-products from data

An agricultural extension service records, for four farms, the number of advisory visits x=(1,2,3,4)x = (1, 2, 3, 4) and maize yield y=(2,3,5,6)y = (2, 3, 5, 6) tonnes per hectare. For a regression of yield on an intercept and visits, build X⊤XX^{\top}X and X⊤yX^{\top}y. (Days 20 and 24 reuse these four farms.)

Show solution

Step 1. The design matrix has a column of ones and the visits column, so we need nn, ∑x\sum x, ∑x2\sum x^2, ∑y\sum y and ∑xy\sum xy.

Step 2. n=4n = 4, ∑x=10\sum x = 10, ∑x2=1+4+9+16=30\sum x^2 = 1 + 4 + 9 + 16 = 30.

Step 3. ∑y=16\sum y = 16, ∑xy=1(2)+2(3)+3(5)+4(6)=2+6+15+24=47\sum xy = 1(2) + 2(3) + 3(5) + 4(6) = 2 + 6 + 15 + 24 = 47.

Answer:

X⊤X=(4101030),X⊤y=(1647).X^{\top}X = \begin{pmatrix}4 & 10\\10 & 30\end{pmatrix}, \qquad X^{\top}y = \begin{pmatrix}16\\47\end{pmatrix}.

Exercise 12 · Fitted yields

For the four farms, use β=(0.5,1.4)⊤\beta = (0.5, 1.4)^{\top} (intercept 0.5 t/ha, 1.4 t/ha per visit). Compute fitted yields XβX\beta and the residuals y−Xβy - X\beta.

Show solution

Step 1. Fitted yield =0.5+1.4x= 0.5 + 1.4x: 1.9,  3.3,  4.7,  6.11.9,\; 3.3,\; 4.7,\; 6.1.

Step 2. Residuals =y−y^= y - \hat y: 2−1.9=0.12 - 1.9 = 0.1, 3−3.3=−0.33 - 3.3 = -0.3, 5−4.7=0.35 - 4.7 = 0.3, 6−6.1=−0.16 - 6.1 = -0.1.

Step 3. Checks: ∑e=0\sum e = 0 and ∑xiei=0.1−0.6+0.9−0.4=0\sum x_ie_i = 0.1 - 0.6 + 0.9 - 0.4 = 0.

Answer: y^=(1.9,3.3,4.7,6.1)\hat y = (1.9, 3.3, 4.7, 6.1) and e=(0.1,−0.3,0.3,−0.1)e = (0.1, -0.3, 0.3, -0.1). The zero sums are no accident: (0.5,1.4)(0.5, 1.4) is the OLS solution (Day 27).

Exercise 13 · A treatment dummy

A sample has n=10n = 10 units of which n1=4n_1 = 4 are treated. Write X⊤XX^{\top}X for a regression on an intercept and the treatment dummy TT.

Show solution

Step 1. Entries: n=10n = 10; ∑Ti=4\sum T_i = 4; ∑Ti2=4\sum T_i^2 = 4 (because T2=TT^2 = T for a 0/1 variable).

Answer:

X⊤X=(10444).X^{\top}X = \begin{pmatrix}10 & 4\\4 & 4\end{pmatrix}.

Exercise 14 · Demeaning household incomes

Three households have monthly incomes y=(1,2,6)y = (1, 2, 6) (thousand dollars). Write the centring matrix M1M_{\mathbf 1} for n=3n = 3 and compute M1yM_{\mathbf 1}y.

Show solution

Step 1. M1=I−1311⊤M_{\mathbf 1} = I - \frac13\mathbf 1\mathbf 1^{\top}: diagonal entries 1−13=231 - \frac13 = \frac23, off-diagonal −13-\frac13:

M1=13(2−1−1−12−1−1−12).M_{\mathbf 1} = \frac13\begin{pmatrix}2 & -1 & -1\\-1 & 2 & -1\\-1 & -1 & 2\end{pmatrix}.

Step 2. First entry: 13(2⋅1−2−6)=−2\frac13(2 \cdot 1 - 2 - 6) = -2; similarly the others.

Step 3. Equivalently, subtract the mean yˉ=3\bar y = 3: (1−3,2−3,6−3)(1 - 3, 2 - 3, 6 - 3).

Answer: M1y=(−2,−1,3)M_{\mathbf 1}y = (-2, -1, 3): each household's income relative to the average.

Exercise 15 · Computing X′y for a large survey

A survey has n=1000n = 1000 households and k=3k = 3 regressors, so X⊤X^{\top} is 3×10003 \times 1000, XX is 1000×31000 \times 3 and yy is 1000×11000 \times 1. Software must compute X(X⊤y)X(X^{\top}y) (needed for fitted values). Compare the multiplications needed for (XX⊤)y(XX^{\top})y and X(X⊤y)X(X^{\top}y).

Show solution

Step 1. (XX⊤)y(XX^{\top})y: forming XX⊤XX^{\top} (1000×10001000 \times 1000) costs 1000×1000×3=3,000,0001000 \times 1000 \times 3 = 3{,}000{,}000 multiplications.

Step 2. Multiplying that by yy costs another 1000×1000=1,000,0001000 \times 1000 = 1{,}000{,}000: total about 4 million.

Step 3. X(X⊤y)X(X^{\top}y): X⊤yX^{\top}y (3×13 \times 1) costs 3×1000=3,0003 \times 1000 = 3{,}000.

Step 4. Then XX times a 3×13 \times 1 vector costs 1000×3=3,0001000 \times 3 = 3{,}000: total 6,000.

Answer: same result, about 700 times cheaper, which is why software never builds an n×nn \times n matrix when it can avoid it.

Verify in Python

import numpy as np

A = np.array([[1, 2], [3, 4]]); B = np.array([[0, 1], [1, 0]])
print("AB =\n", A @ B, "\nBA =\n", B @ A)
print("(AB)' == B'A':", np.array_equal((A @ B).T, B.T @ A.T), " tr(AB), tr(BA):", np.trace(A @ B), np.trace(B @ A))

x = np.array([1, 2, 3, 4.]); y = np.array([2, 3, 5, 6.])
X = np.column_stack([np.ones(4), x])
print("X'X =\n", X.T @ X, "\nX'y =", X.T @ y)
beta = np.array([0.5, 1.4]); e = y - X @ beta
print("residuals:", e.round(2), " X'e =", (X.T @ e).round(10))

n = 3; M = np.eye(n) - np.ones((n, n)) / n
print("M y =", M @ np.array([1, 2, 6.]))

Key takeaways

  • AxAx is a weighted combination of the columns of AA; XβX\beta is the vector of fitted values.
  • (AB)⊤=B⊤A⊤(AB)^{\top} = B^{\top}A^{\top} and tr⁡(AB)=tr⁡(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA) are used constantly in proofs.
  • X⊤XX^{\top}X and X⊤yX^{\top}y hold all the sums of squares and cross-products that regression needs.

Simulation Lab: A Matrix as a Transformation

Open full screen →

Edit a 2×2 matrix and see the plane stretch, rotate, shear or collapse; the determinant is the area of the image square.

AîAĵAx
det A
-5.00
flips orientation
Ax
(3.0, 4.0)
= 1·col₁ + 1·col₂

The faint grid is the original plane; the coloured grid is its image under A. The unit square (grey) becomes the parallelogram spanned by the columns Aî and Aĵ, with area |det A|. Drag x (blue) to see Ax (orange).

Day 16Day 18