Week 03 · Linear systems, rank, and projections
Tell when equations identify a unique answer and find the closest attainable output when the equations disagree.
Subject contents · Week 2 · Week 4
Tell when equations identify a unique answer and find the closest attainable output when the equations disagree.
Ask what the observations can tell you
Imagine two settings that affect a sensor in exactly the same pattern, with the second setting twice as strong. You can observe the final reading accurately and still have no way to tell how much each setting contributed. A model with duplicate or perfectly proportional features has the same problem. Several sets of coefficients can give the same fitted predictions.
Separate three questions in this lesson. Can the model reach the requested output? If it can, are the coefficients unique? If it cannot, what reachable output is closest? These questions lead directly to least squares next week.
The idea
In A x = b, the columns of A tell you which directions you can combine to construct b. The column space is the set of all such combinations. Rank counts how many independent directions those columns supply. If two columns point in the same direction, the output can still be known even when the individual coefficients are not.
A square, full-rank system has one solution for every right-hand side. For a system with dependent columns, there may be many solutions or none. First ask whether b is reachable. If it is, ask whether more than one coefficient vector reaches it.
When b is outside the column space, its orthogonal projection is the closest reachable point in Euclidean distance. The difference between b and its projection is perpendicular to every column of A. This is the geometric setup for least squares.
Separate inputs, outputs, and directions
In A x = b, A is an m-by-n matrix, x contains n unknown coefficients, and b contains m target readings. The column space is every output A x can reach. The null space is every input n with A n = 0. If x is a solution and n lies in the null space, then A(x + n) = Ax + An = b. A nonzero null vector is a direction in coefficient space that the observations cannot distinguish.
For a single reachable direction a ≠ 0, write the candidate output as p = t a. At the nearest point, the residual b − t a is perpendicular to a. Thus a · (b − t a) = 0. Expanding the dot product gives a · b − t(a · a) = 0. Solving for t gives the projection formula.
t = (a · b) / (a · a), and p = t a.
Why is this the closest point? For any other point q on the line, b − q is the sum of b − p and p − q. Those two parts are perpendicular. Its squared length is therefore ‖b − p‖² + ‖p − q‖², which is smallest at q = p.
Work it out
Let the columns of A be a = (1, 1) and 2a = (2, 2). Every output is (x₁ + 2x₂)a. For b = (3, 3), both x = (3, 0) and x = (1, 1) work because each gives x₁ + 2x₂ = 3.
For b = (3, 1), no exact solution exists because every reachable point has equal coordinates. Project b onto a using p = ((a · b)/(a · a))a. That gives (4/2)(1, 1) = (2, 2). The residual b − p = (1, −1) is perpendicular to (1, 1). You can verify this by computing their dot product.
The diagonal is the set of outputs A can produce. The shortest route from b to that line is perpendicular to it.
Run the Python lab
import numpy as np
a = np.array([1., 1.])
A = np.column_stack([a, 2 * a])
b_exact = np.array([3., 3.])
b = np.array([3., 1.])
p = (a @ b) / (a @ a) * a
residual = b - p
print("rank:", np.linalg.matrix_rank(A))
print("projection:", p.tolist())
print("residual:", residual.tolist())
print("dot with column:", float(a @ residual))
np.testing.assert_allclose(A @ [3, 0], b_exact)
np.testing.assert_allclose(A @ [1, 1], b_exact)
np.testing.assert_allclose(A.T @ residual, [0, 0])
Expected output
rank: 1
projection: [2.0, 2.0]
residual: [1.0, -1.0]
dot with column: 0.0
Check the output separately from the coefficients
np.column_stack([a, 2 * a]) builds a matrix whose columns point along the same line. In this exact example, matrix_rank reports one reachable direction. The two coefficient assertions show that different inputs really do give the same output. The final assertion checks A.T @ residual, which computes the residual’s dot product with each column in one multiplication.
With real measurements, proportional columns can be nearly proportional. A numerical rank routine then uses a tolerance, and coefficients can be very sensitive to small changes even when the routine reports full rank. Try replacing (2, 2) with (2, 2.000001). This lets the matrix reach any point in the plane, but reproducing b = (3, 1) demands coefficients with very large opposite signs. Compare that exact fit with the stable projection onto the nearly shared direction.
Try it yourself
- Find a nonzero vector n for which A n = 0. Explain why adding n to an exact solution leaves the output unchanged.
- Replace the second column by (1, −1). Can the new matrix reproduce (3, 1) exactly?
Check your work. One null vector is (−2, 1). With the independent second column, the new coefficients (2, 1) reproduce (3, 1).