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.
- ●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.
Theory
The product
If is and is , the product is with
The inner dimensions must match ("conformable"). Matrix multiplication is associative and distributive but not commutative: in general , and one of them may not even exist.
Two ways to read Ax
is a linear combination of the columns of with weights . So , the vector of fitted values, is " times the intercept column plus times the education column plus …". Alternatively, entry of is row of dotted with : unit '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
The cross-product matrices
For a design matrix () and outcome ():
With an intercept and one regressor,
On Day 27 the OLS estimator will be : everything it needs is in these two objects.
The centring matrix
Multiplying by 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 and . Compute and .
Show solution
(columns of swapped), (rows swapped). They differ.
Exercise 2 · Conformability
is and is . Which of , , , exist, and what shapes are they?
Show solution
Step 1. : .
Step 2. : not defined ().
Step 3. : .
Step 4. : (it equals ).
Exercise 3 · Columns, combined
, . Compute both row-by-row and as a combination of columns.
Show solution
Rows: . Columns: .
Exercise 4 · Transpose of a product
Check for the matrices in Exercise 1.
Show solution
. . ✓
Exercise 5 · Trace
Compute , and for Exercise 1.
Show solution
, , . Equal even though .
Part 2 · Proofs
Exercise 6 · Transpose of a product
Prove .
Show solution
.
Exercise 7 · Cyclic trace
Prove for () and ().
Show solution
. Day 20 uses this to show .
Exercise 8 · The structure of X′X
Show that 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. .
Step 2. Entry is column dotted with column , so the diagonal is .
Step 3. With columns and : , , .
Exercise 9 · Quadratic forms
Show that , and write for .
Show solution
, so . Here (the off-diagonal 1 appears twice).
Exercise 10 · X′X is positive semi-definite
Show that for every .
Show solution
Step 1. .
Step 2. It is zero only if , 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 and maize yield tonnes per hectare. For a regression of yield on an intercept and visits, build and . (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 , , , and .
Step 2. , , .
Step 3. , .
Answer:
Exercise 12 · Fitted yields
For the four farms, use (intercept 0.5 t/ha, 1.4 t/ha per visit). Compute fitted yields and the residuals .
Show solution
Step 1. Fitted yield : .
Step 2. Residuals : , , , .
Step 3. Checks: and .
Answer: and . The zero sums are no accident: is the OLS solution (Day 27).
Exercise 13 · A treatment dummy
A sample has units of which are treated. Write for a regression on an intercept and the treatment dummy .
Show solution
Step 1. Entries: ; ; (because for a 0/1 variable).
Answer:
Exercise 14 · Demeaning household incomes
Three households have monthly incomes (thousand dollars). Write the centring matrix for and compute .
Show solution
Step 1. : diagonal entries , off-diagonal :
Step 2. First entry: ; similarly the others.
Step 3. Equivalently, subtract the mean : .
Answer: : each household's income relative to the average.
Exercise 15 · Computing X′y for a large survey
A survey has households and regressors, so is , is and is . Software must compute (needed for fitted values). Compare the multiplications needed for and .
Show solution
Step 1. : forming () costs multiplications.
Step 2. Multiplying that by costs another : total about 4 million.
Step 3. : () costs .
Step 4. Then times a vector costs : total 6,000.
Answer: same result, about 700 times cheaper, which is why software never builds an 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
- is a weighted combination of the columns of ; is the vector of fitted values.
- and are used constantly in proofs.
- and 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.
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).