Day 16. Vectors, Matrices & the Dot Product
Data as vectors, and the dot product as the engine behind means, covariances and correlations.
- ●Add, scale and take dot products of vectors, and compute norms and angles.
- ●Prove the Cauchy–Schwarz inequality and the Pythagorean identity for orthogonal vectors.
- ●Write means, covariances and correlations as dot products of (centred) vectors.
- ●Lay out a data set as a design matrix with units in rows and variables in columns.
Theory
Why linear algebra now?
Phase 1 worked with one or two variables at a time. Real causal analyses adjust for many covariates at once, and the cleanest language for "many variables at once" is linear algebra. By Day 27 every regression formula you know will be a one-line matrix expression.
Vectors
A vector is an ordered list of numbers, written as a column. In data work, a vector usually holds one variable for all units (e.g. everyone's wage).
- Addition and scalar multiplication work entry by entry: , .
- The dot product (inner product) is
- The norm (length) is , and the angle between and satisfies
- and are orthogonal when .
- Cauchy–Schwarz: , so .
Statistics as dot products
Let be the vector of ones. Then
A correlation is the cosine of the angle between two centred data vectors. That is why it lies in .
Matrices
An matrix is a rectangular array with rows and columns. The transpose swaps rows and columns. Special cases: the identity (ones on the diagonal), diagonal matrices, and symmetric matrices ().
The design matrix stacks the data: row is unit , column is variable , and the first column is usually (the intercept).
Causal link. Matching estimators (Phase 6) compare units whose covariate vectors are close; "close" means a small norm of the difference. Today's norm is the first distance you will use.
Part 1 · Warm-up
Exercise 1 · Vector arithmetic
Let and . Compute and .
Show solution
and .
Exercise 2 · Dot product
Compute .
Show solution
.
Exercise 3 · Norms
Compute and .
Show solution
and .
Exercise 4 · The angle
Find the angle between and .
Show solution
Step 1. .
Step 2. .
Answer: about 97°, slightly more than a right angle (the dot product is slightly negative).
Exercise 5 · Shapes
is . What are the shapes of , and ?
Show solution
is ; is (one row and column per variable); is (one per unit).
Part 2 · Proofs
Exercise 6 · Properties of the dot product
Prove that and .
Show solution
, because real multiplication commutes. And .
Exercise 7 · Pythagoras
Prove , and deduce the Pythagorean theorem for orthogonal vectors.
Show solution
Step 1. Write the squared norm as a dot product and expand:
Step 2. (Exercise 6), so the middle terms combine: .
Step 3. If , the cross term vanishes: (Pythagoras).
This identity is behind the decomposition TSS = ESS + RSS on Day 20.
Exercise 8 · Cauchy–Schwarz
Prove . Hint: for every .
Show solution
Step 1. If both sides are 0.
Step 2. Otherwise for all .
Step 3. A quadratic in that never goes negative has discriminant :
Step 4. (This is Day 5's proof that , written for vectors.)
Exercise 9 · Centred vectors are orthogonal to the constant
Show that and that .
Show solution
Step 1. , so dividing by gives the mean.
Step 2. Then .
Step 3. Deviations from the mean sum to zero; geometrically, the centred vector is perpendicular to .
Exercise 10 · Correlation is a cosine
Show that the sample correlation equals the cosine of the angle between and , and conclude .
Show solution
Step 1.
Step 2. (the factors cancel).
Step 3. Cauchy–Schwarz gives ; exactly when is a multiple of .
Part 3 · Applied
Exercise 11 · Revenue
A firm sells three goods at prices in quantities . Write revenue as a dot product and compute it.
Show solution
Step 1. Revenue is .
Step 2. .
Answer: $500.
Exercise 12 · A weighted average
A programme budget is split across three sites whose returns are percent. Find the weighted return.
Show solution
Step 1. Weighted return .
Step 2. .
Answer: 3.4%. Any weighted average (IPW, synthetic control) is a dot product with weights that sum to one.
Exercise 13 · Tutoring hours and test-score gains
Four pupils received hours of tutoring per week and gained points on a test. Compute the centred vectors, the correlation and the regression slope .
Show solution
Step 1. Means: hours and points.
Step 2. Centred vectors: and .
Step 3. Dot product and squared norms:
Step 4. Correlation (cosine): .
Step 5. Slope: .
Answer: ; each extra weekly hour goes with about 1.6 more points (an association, not yet a causal effect).
Exercise 14 · A design matrix
Three workers have education 12, 16 and 14 years; workers 2 and 3 were trained. Write the design matrix for a regression of wages on an intercept, education and training.
Show solution
Step 1. One row per worker, in the order given.
Step 2. Columns: a column of ones (intercept), years of education, and the training dummy (0 for worker 1, 1 for workers 2 and 3).
Answer:
Exercise 15 · Distance between units
Firm A has covariates (size 10, age 2) and firm B (size 13, age 6). Find their Euclidean distance. What happens if size is measured in hundreds instead of units?
Show solution
Step 1. Difference in covariates: .
Step 2. Distance: .
Step 3. In hundreds, sizes become 0.10 and 0.13, so the size difference is 0.03 and the distance is .
Answer: 5, but about 4.0 after rescaling: the distance is now driven almost entirely by age. Euclidean distance depends on units, which is why matching uses standardised or Mahalanobis distances (Day 21).
Verify in Python
import numpy as np
u, v = np.array([2, -1, 3]), np.array([1, 4, 0])
print("u+v =", u + v, " 2u-v =", 2 * u - v, " u.v =", u @ v)
cos = u @ v / (np.linalg.norm(u) * np.linalg.norm(v))
print(f"norms {np.linalg.norm(u):.3f}, {np.linalg.norm(v):.3f}; angle {np.degrees(np.arccos(cos)):.1f} degrees")
x, y = np.array([1, 2, 3, 6.]), np.array([2, 3, 5, 10.])
xc, yc = x - x.mean(), y - y.mean()
print("r (cosine) =", round(xc @ yc / np.sqrt((xc @ xc) * (yc @ yc)), 4), " numpy:", round(np.corrcoef(x, y)[0, 1], 4))
print("slope =", round(xc @ yc / (xc @ xc), 3), " 1'xc =", xc.sum())
X = np.array([[1, 12, 0], [1, 16, 1], [1, 14, 1]])
print("X'X =\n", X.T @ X)
Key takeaways
- A data set is a matrix; each variable is a vector; most statistics are dot products.
- Correlation is the cosine of the angle between centred vectors, so Cauchy–Schwarz bounds it by 1.
- Orthogonal vectors obey Pythagoras, the identity behind every variance decomposition in regression.
Simulation Lab: Vector Playground
Open full screen →Drag two vectors and watch the dot product, the angle and the projection; the geometry behind correlation and slopes.
With centred data vectors, cos θ is the correlation and the projection coefficient is the regression slope.
Drag the two tips. The dashed orange vector is the projection of u onto v; the dotted segment (the residual) is always perpendicular to v. The dot product is zero exactly when the angle is 90°.