Matrix inverse and eigendecomposition
The whole book is also in the PDF.
This chapter introduces two of the most powerful concepts in advanced linear algebra: the matrix inverse and the eigendecomposition. We begin by tackling the fundamental problem of solving a system of linear equations, \(\mathbf{A}\mathbf{x} = \mathbf{b}\), which naturally leads to the definition of the matrix inverse as the operator that “undoes” a linear transformation. We establish the conditions for a matrix to be invertible, introducing the concept of matrix rank as a measure of linear independence. We then introduce the main topic, eigendecomposition, with a brief but essential interlude on the geometry of orthogonal matrices. We thoroughly explore the geometric implications of different eigenvalue structures and conclude by demonstrating the power of these concepts in two key applications: solving linear systems and the foundational AI technique of Principal Component Analysis (PCA).
Keywords. Matrix Inverse | Linear Systems | Rank | Full Rank | Singular Matrix | Orthogonal Matrix | Eigenvalue | Eigenvector | Eigendecomposition | Spectral Theorem | Principal Component Analysis (PCA)
4.1 The Matrix Inverse and the Solution to \(\mathbf{A}\mathbf{x} = \mathbf{b}\)
A central problem in science and engineering is to solve a system of linear equations. In matrix notation, this is expressed as \(\mathbf{A}\mathbf{x} = \mathbf{b}\). Here, we can view the matrix \(\mathbf{A} \in \mathbb{R}^{m \times p}\) as a linear transformation that maps an unknown vector \(\mathbf{x} \in \mathbb{R}^p\) to a known output vector \(\mathbf{b} \in \mathbb{R}^m\). The core question is: given the transformation \(\mathbf{A}\) and the output \(\mathbf{b}\), can we uniquely determine the input \(\mathbf{x}\)?
This is akin to asking if we can reverse, or “undo,” the transformation represented by \(\mathbf{A}\). The operator that achieves this reversal is the matrix (pseudo)inverse.
4.1.1 Defining the Inverse (square matrix)
Just as the inverse of a scalar number \(a\) is \(a^{-1}\) such that \(a \cdot a^{-1} = 1\), the inverse of a square matrix \(\mathbf{A} \in \mathbb{R}^{p \times p}\), denoted \(\mathbf{A}^{-1} \in \mathbb{R}^{p \times p}\), is a matrix that, when multiplied by \(\mathbf{A}\), yields the identity matrix \(\mathbf{I}\).
Definition 4.1 (Matrix Inverse). A square matrix \(\mathbf{A} \in \mathbb{R}^{p \times p}\) is said to be invertible (or non-singular) if there exists a matrix \(\mathbf{A}^{-1} \in \mathbb{R}^{p \times p}\) such that:
\[ \mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}_p. \]The matrix \(\mathbf{A}^{-1}\) is called the inverse of \(\mathbf{A}\).
Before we continue though, an interlude to remember some material from linear algebra.
4.1.2 Interlude: The Determinant of a Matrix
The determinant of a matrix provides a more nuanced, geometric measure of how a square matrix transforms space. It is a single scalar value that encapsulates two critical pieces of information: the volume scaling factor of the transformation and its effect on the orientation of space.
Definition 4.2 (Determinant). The determinant of a square matrix \(\mathbf{A} \in \mathbb{R}^{p \times p}\), denoted \(\det(\mathbf{A})\) or \(|\mathbf{A}|\), is a scalar value computed from its elements. While the general recursive formula (Laplace expansion) is complex, the results for small matrices are straightforward:
- For a \(1 \times 1\) matrix \(\mathbf{A} = [a]\), \(\det(\mathbf{A}) = a\).
- For a \(2 \times 2\) matrix \(\mathbf{A} = \left[\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right]\), \(\det(\mathbf{A}) = ad - bc\).
- For a \(3 \times 3\) matrix, the formula involves a sum of six products of its elements.
A fundamental property is that the determinant of a matrix is equal to the product of all its eigenvalues; what is an eigenvalue? One of the topics of this chapter! Stay tuned :)!
4.1.3 Geometric Interpretation: Volume and Orientation
Imagine the standard unit vectors in \(\mathbb{R}^p\). These vectors form a “unit box” (a square in 2D, a cube in 3D) with a volume of 1. When we apply the transformation matrix \(\mathbf{A}\) to every point in this box, it gets mapped to a new shape: a parallelepiped (a parallelogram in 2D) spanned by the columns of \(\mathbf{A}\).
Volume Scaling Factor: The absolute value of the determinant, \(|\det(\mathbf{A})|\), is the volume of this new parallelepiped. It tells us how much the transformation scales, stretches, or squashes space.
- If \(|\det(\mathbf{A})| > 1\), the transformation expands volume.
- If \(|\det(\mathbf{A})| < 1\), the transformation contracts volume.
- If \(|\det(\mathbf{A})| = 1\), the transformation is volume-preserving (like a pure rotation).
Orientation: The sign of the determinant tells us whether the transformation preserves the “handedness” or orientation of space.
- If \(\det(\mathbf{A}) > 0\), the orientation is preserved. In 2D, if you curl your fingers from the first basis vector to the second in a counter-clockwise motion, their transformed versions will also maintain a counter-clockwise relationship.
- If \(\det(\mathbf{A}) < 0\), the orientation is flipped. The transformation includes a reflection, like looking in a mirror. A counter-clockwise system becomes a clockwise one.
4.1.4 The Critical Case: A Determinant of Zero
The most important geometric insight comes when \(\det(\mathbf{A}) = 0\).
This means the volume of the transformed parallelepiped is zero. This can only happen if the column vectors of \(\mathbf{A}\) are linearly dependent. When the vectors are linearly dependent, they do not span the full \(p\)-dimensional space; instead, they lie on a lower-dimensional subspace (e.g., three vectors in 3D lying on the same plane, or two vectors in 2D lying on the same line).
Geometrically, a determinant of zero means the transformation \(\mathbf{A}\) squashes the entire space into a lower-dimensional subspace. For instance, a \(3 \times 3\) matrix with a zero determinant maps the entire 3D space onto a plane or a line. This “collapse” of a dimension is an irreversible, lossy process. You cannot uniquely determine which 3D point was mapped to a given point on the plane, just as you cannot know the depth of an object from its 2D shadow.
An example of such a collapse is shown in Figure 4.1, where a singular matrix transforms a 2D space (a square) into a 1D space (a line segment). This provides the crucial geometric intuition behind the link between the determinant and invertibility:
- If \(\det(\mathbf{A}) \neq 0\), the transformation maps the entire space \(\mathbb{R}^p\) back onto itself without collapse. The mapping is one-to-one, and therefore an inverse transformation exists.
- If \(\det(\mathbf{A}) = 0\), the transformation collapses the space. The mapping is many-to-one, and it is impossible to uniquely reverse it. The matrix is singular, and no inverse exists.
Going back to our “story”:
4.1.5 A Special Case: The Closed-Form Inverse of a 2x2 Matrix
While finding the inverse of a large matrix requires sophisticated numerical algorithms, for the simple and common case of a \(2 \times 2\) matrix, an elegant closed-form solution exists. This is a situation analogous to finding the roots of a univariate quadratic function. Instead of relying on iterative optimization methods to search for a solution, we have a direct formula that provides the exact answer with absolute accuracy.
For a general \(2 \times 2\) matrix \(\mathbf{A} = \left[\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right]\), its inverse is given by the formula:
\[ \mathbf{A}^{-1} = \frac{1}{ad-bc} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix} = \frac{1}{\det(\mathbf{A})} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix} \]This formula also makes it explicitly clear why the inverse only exists if the determinant is non-zero; a zero determinant would lead to division by zero, rendering the operation undefined.
This direct analytical solution is a powerful tool for small-scale problems and theoretical derivations. For larger matrices, however, such simple formulas do not exist, and we must rely on more general computational methods, many of which are themselves rooted in optimization.
Equipped with this expression, to make the concept of an inverse concrete, let’s verify the relationship \(\mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}\) with specific examples.
Example 4.1 (The 2x2 Case). Consider the following \(2 \times 2\) matrix:
\[ \mathbf{A} = \begin{bmatrix} 4 & 7 \\ 2 & 6 \end{bmatrix} \]A matrix is invertible if its determinant is non-zero. For a \(2 \times 2\) matrix \(\left[\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right]\), the determinant is \(ad-bc\). Here, \(\det(\mathbf{A}) = (4)(6) - (7)(2) = 24 - 14 = 10\). Since \(10 \neq 0\), the matrix is invertible.
Applying the \(2 \times 2\) inverse formula to our matrix \(\mathbf{A}\):
\[ \mathbf{A}^{-1} = \frac{1}{10} \begin{bmatrix} 6 & -7 \\ -2 & 4 \end{bmatrix} = \begin{bmatrix} 0.6 & -0.7 \\ -0.2 & 0.4 \end{bmatrix} \]Now, we verify the inverse property by computing both products.
First, we check \(\mathbf{A}\mathbf{A}^{-1}\):
\[ \begin{aligned} \mathbf{A}\mathbf{A}^{-1} &= \begin{bmatrix} 4 & 7 \\ 2 & 6 \end{bmatrix} \begin{bmatrix} 0.6 & -0.7 \\ -0.2 & 0.4 \end{bmatrix} \\ &= \begin{bmatrix} (4)(0.6) + (7)(-0.2) & (4)(-0.7) + (7)(0.4) \\ (2)(0.6) + (6)(-0.2) & (2)(-0.7) + (6)(0.4) \end{bmatrix} \\ &= \begin{bmatrix} 2.4 - 1.4 & -2.8 + 2.8 \\ 1.2 - 1.2 & -1.4 + 2.4 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} = \mathbf{I}_2. \end{aligned} \]Next, we check \(\mathbf{A}^{-1}\mathbf{A}\):
\[ \begin{aligned} \mathbf{A}^{-1}\mathbf{A} &= \begin{bmatrix} 0.6 & -0.7 \\ -0.2 & 0.4 \end{bmatrix} \begin{bmatrix} 4 & 7 \\ 2 & 6 \end{bmatrix} \\ &= \begin{bmatrix} (0.6)(4) + (-0.7)(2) & (0.6)(7) + (-0.7)(6) \\ (-0.2)(4) + (0.4)(2) & (-0.2)(7) + (0.4)(6) \end{bmatrix} \\ &= \begin{bmatrix} 2.4 - 1.4 & 4.2 - 4.2 \\ -0.8 + 0.8 & -1.4 + 2.4 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} = \mathbf{I}_2. \end{aligned} \]Both products yield the \(2 \times 2\) identity matrix, confirming that we have found the correct inverse.
Example 4.2 (The 3x3 Case). Consider the following \(3 \times 3\) matrix:
\[ \mathbf{B} = \begin{bmatrix} 1 & 3 & 3 \\ 1 & 4 & 3 \\ 1 & 3 & 4 \end{bmatrix} \]While the formula for a \(3 \times 3\) inverse is more complex and beyond the scope of this course to derive, here we are given its inverse and verify the property. The inverse of \(\mathbf{B}\) is:
\[ \mathbf{B}^{-1} = \begin{bmatrix} 7 & -3 & -3 \\ -1 & 1 & 0 \\ -1 & 0 & 1 \end{bmatrix} \]Let’s verify that \(\mathbf{B}\mathbf{B}^{-1} = \mathbf{I}_3\). We compute the element at position \((i,j)\) by taking the inner product of the \(i\)-th row of \(\mathbf{B}\) and the \(j\)-th column of \(\mathbf{B}^{-1}\).
- Element (1,1): \(7 - 3 - 3 = 1\).
- Element (1,2): \(-3 + 3 + 0 = 0\).
- Element (1,3): \(-3 + 0 + 3 = 0\).
- Element (2,1): \(7 - 4 - 3 = 0\).
- Element (2,2): \(-3 + 4 + 0 = 1\).
- Element (2,3): \(-3 + 0 + 3 = 0\).
- Element (3,1): \(7 - 3 - 4 = 0\).
- Element (3,2): \(-3 + 3 + 0 = 0\).
- Element (3,3): \(-3 + 0 + 4 = 1\).
Assembling these results gives:
\[ \mathbf{B}\mathbf{B}^{-1} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} = \mathbf{I}_3. \]A similar calculation would show that \(\mathbf{B}^{-1}\mathbf{B} = \mathbf{I}_3\) as well, thus verifying the inverse property for this \(3 \times 3\) case.
Example 4.3 (The Trivial Case: Inverting a Diagonal Matrix). While inverting general matrices requires complex algorithms, the inverse of a diagonal matrix is remarkably simple to compute, provided it exists.
Consider the diagonal matrix:
\[ \boldsymbol{\Lambda} = \begin{bmatrix} 2 & 0 & 0 \\ 0 & 5 & 0 \\ 0 & 0 & -4 \end{bmatrix} \]A diagonal matrix is invertible if and only if all of its diagonal entries are non-zero. If this condition holds, its inverse is simply the diagonal matrix containing the reciprocals of the original diagonal entries. Therefore, the inverse of \(\boldsymbol{\Lambda}\) is:
\[ \boldsymbol{\Lambda}^{-1} = \begin{bmatrix} 1/2 & 0 & 0 \\ 0 & 1/5 & 0 \\ 0 & 0 & -1/4 \end{bmatrix} = \begin{bmatrix} 0.5 & 0 & 0 \\ 0 & 0.2 & 0 \\ 0 & 0 & -0.25 \end{bmatrix} \]Let’s verify the property \(\boldsymbol{\Lambda}\boldsymbol{\Lambda}^{-1} = \mathbf{I}_3\):
\[ \begin{aligned} \boldsymbol{\Lambda}\boldsymbol{\Lambda}^{-1} &= \begin{bmatrix} 2 & 0 & 0 \\ 0 & 5 & 0 \\ 0 & 0 & -4 \end{bmatrix} \begin{bmatrix} 0.5 & 0 & 0 \\ 0 & 0.2 & 0 \\ 0 & 0 & -0.25 \end{bmatrix} \\ &= \begin{bmatrix} (2)(0.5) & 0 & 0 \\ 0 & (5)(0.2) & 0 \\ 0 & 0 & (-4)(-0.25) \end{bmatrix} \\ &= \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} = \mathbf{I}_3. \end{aligned} \]The structure of diagonal matrices makes this possible. When multiplying two diagonal matrices, the resulting matrix is also diagonal, and its diagonal entries are simply the products of the corresponding diagonal entries from the original matrices. For the inverse, this product is always \(d_{ii} \times (1/d_{ii}) = 1\).
4.1.6 Going back to \(\mathbf{A} \mathbf{x} = \mathbf{b}\)
If such an inverse exists and we have access to it, we can solve the system \(\mathbf{A}\mathbf{x} = \mathbf{b}\) directly! (Remember, inverses exist only for square matrices!) No optimization, no iterative methods! By pre-multiplying both sides of the equation by \(\mathbf{A}^{-1}\), we get:
\[ \begin{aligned} \mathbf{A}^{-1}(\mathbf{A}\mathbf{x}) &= \mathbf{A}^{-1}\mathbf{b} \\ (\mathbf{A}^{-1}\mathbf{A})\mathbf{x} &= \mathbf{A}^{-1}\mathbf{b} \\ \mathbf{I}\mathbf{x} &= \mathbf{A}^{-1}\mathbf{b} \\ \mathbf{x} &= \mathbf{A}^{-1}\mathbf{b}. \end{aligned} \]This provides a unique and elegant solution. However, this entire procedure hinges on a critical question: when does the inverse of a matrix exist? And, how does one compute such an inverse? Finally, what are some alternative options and how do they compare to finding the matrix inverse?
4.1.7 More Conditions for Invertibility: Rank and Linear Independence
Not all square matrices have an inverse. For an inverse to exist, the transformation it represents must be a one-to-one mapping, meaning every input \(\mathbf{x}\) maps to a unique output \(\mathbf{b}\), and every output \(\mathbf{b}\) comes from exactly one input \(\mathbf{x}\).
From a geometric standpoint, the transformation \(\mathbf{y} = \mathbf{A}\mathbf{x}\) is a linear combination of the columns of \(\mathbf{A}\). For a unique solution to exist for any \(\mathbf{b} \in \mathbb{R}^p\), the columns of \(\mathbf{A}\) must be able to “reach” every point in the space \(\mathbb{R}^p\). This requires the columns to be linearly independent. If the columns were linearly dependent, they would span only a subspace (like a plane within a 3D space), and any vector \(\mathbf{b}\) outside that subspace would be unreachable, leading to no solution. Remember also the example from the determinant!
This concept of spanning ability is formalized by the rank of a matrix.
Definition 4.3 (Rank of a Matrix). The rank of a matrix \(\mathbf{A}\), denoted \(\text{rank}(\mathbf{A})\), is the maximum number of linearly independent columns (or, equivalently, rows) in \(\mathbf{A}\).
Connections to the determinant. Notice that both the rank of a matrix and its determinant can indicate whether the matrix has an inverse or not. However, each one of these quantities convey also different messages: for the case of determinant, a non-zero determinant also informs us about the type of transformation (just rotation, or expansion/squeeze of the volume?), but a zero-determinant does not inform us whether one or multiple spaces have collapsed (does the 3D shape collapse to a 2D shape or a line? no idea!). On the other hand, the rank of a matrix does not tell us anything about how the transformation looks like, but it informs us about how many subspaces we lost: if a \(p \times p\) matrix is rank-1, then the determinant is zero, but knowing that it is rank-1 means that we lost \(p-1\) subspaces.
For a square matrix to be invertible, its columns must be able to span the entire space, which requires the matrix to have full rank. However, many practical problems, especially in AI, involve non-square matrices where the number of variables does not match the number of constraints. This is true even for problems such as linear regression:
4.1.8 Beyond Square Matrices
Case 1: Underdetermined Systems. This scenario occurs when the matrix \(\mathbf{A} \in \mathbb{R}^{m \times p}\) is “fat,” meaning it has more columns than rows (\(p > m\)). This corresponds to having more variables (\(p\)) than equations (\(m\)). In AI, this is often called an overparameterized system. Example:
\[ \begin{bmatrix} A_{11} & A_{12} & A_{13} \\ A_{21} & A_{22} & A_{23} \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} = \begin{bmatrix} b_1 \\ b_2 \end{bmatrix} \quad (m=2, p=3). \]This is a many-to-one mapping from a higher-dimensional space (\(\mathbb{R}^p\)) to a lower-dimensional space (\(\mathbb{R}^m\)). Imagine projecting 3D space onto a 2D plane. An infinite number of points in 3D can map to the same single point on the plane. Consequently, if a solution to \(\mathbf{A}\mathbf{x} = \mathbf{b}\) exists, there are generally infinitely many solutions, unless further constraints are imposed.
Example 4.4 (An Underdetermined System). Consider the system defined by:
\[ \begin{bmatrix} 1 & 1 & 1 \\ 0 & 1 & 2 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} = \begin{bmatrix} 3 \\ 4 \end{bmatrix} \]This translates to the equations:
\[ \begin{aligned} x_1 + x_2 + x_3 &= 3 \\ x_2 + 2x_3 &= 4 \end{aligned} \]From the second equation, we can express \(x_2\) in terms of \(x_3\): \(x_2 = 4 - 2x_3\). Substituting this into the first equation gives \(x_1 + (4 - 2x_3) + x_3 = 3\), which simplifies to \(x_1 - x_3 = -1\), or \(x_1 = x_3 - 1\).
Since both \(x_1\) and \(x_2\) depend on \(x_3\), we can treat \(x_3\) as a free parameter, say \(t\). The family of solutions is then:
\[ \mathbf{x} = \begin{bmatrix} t-1 \\ 4-2t \\ t \end{bmatrix} \]Let’s check two possible solutions:
- If \(t=0\), \(\mathbf{x} = [-1, 4, 0]^\top\). This is a valid solution.
- If \(t=2\), \(\mathbf{x} = [1, 0, 2]^\top\). This is also a valid solution.
Since we can choose any real number for \(t\), there are infinitely many solutions.
Case 2: Overdetermined Systems. This scenario occurs when the matrix \(\mathbf{A} \in \mathbb{R}^{m \times p}\) is “tall,” meaning it has more rows than columns (\(m > p\)). This corresponds to having more equations (constraints) than variables (\(p\)). In AI, this is the standard underparameterized setup for regression. Example:
\[ \begin{bmatrix} A_{11} & A_{12} \\ A_{21} & A_{22} \\ A_{31} & A_{32} \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \begin{bmatrix} b_1 \\ b_2 \\ b_3 \end{bmatrix} \quad (m=3, p=2). \]We are trying to find a linear combination of \(p\) column vectors to perfectly hit a target vector \(\mathbf{b}\) that lives in a higher-dimensional space \(\mathbb{R}^m\). The \(p\) columns of \(\mathbf{A}\) can span a subspace of at most \(p\) dimensions (e.g., a plane in 3D space). If the target vector \(\mathbf{b}\) does not happen to lie perfectly within this subspace, no exact solution exists.
Example 4.5 (An Overdetermined System). Consider the system:
\[ \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \mathbf{b} \]This system gives three constraints on two variables:
\[ \begin{aligned} x_1 &= b_1 \\ x_2 &= b_2 \\ x_1 + x_2 &= b_3 \end{aligned} \]An exact solution is only possible if the target vector \(\mathbf{b}\) is consistent, meaning \(b_1 + b_2 = b_3\).
- If \(\mathbf{b} =[2, 3, 5]^\top\), the system is consistent. The unique solution is \(x_1=2, x_2=3\). Geometrically, the vector \([2, 3, 5]^\top\) lies on the plane spanned by the columns of \(\mathbf{A}\).
- If \(\mathbf{b} =[2, 3, 6]^\top\), the system is inconsistent. The first two equations demand \(x_1=2\) and \(x_2=3\), but then \(x_1+x_2 = 5 \neq 6\). No solution can satisfy all three equations simultaneously. Geometrically, the vector \([2, 3, 6]^\top\) lies outside the plane spanned by the columns of \(\mathbf{A}\).
The second case (no solution) is the norm in real-world data, which is always noisy. It is almost never possible to find a simple linear model that perfectly fits every data point. This is precisely why we do not try to solve \(\mathbf{A}\mathbf{x} = \mathbf{b}\) directly. Instead, we reframe it as an optimization problem: find the vector \(\mathbf{x}\) that makes \(\mathbf{A}\mathbf{x}\) as close as possible to \(\mathbf{b}\). This leads directly to the least-squares method, which seeks to minimize the distance \(\|\mathbf{A}\mathbf{x} - \mathbf{b}\|_2\), a central theme we will return to repeatedly.
Note that the two cases considered above are cases where the inverse of \(\mathbf{A}\) is not well-defined. These are provided to highlight that linear systems of equations is not only the case of square systems, plus the fact that there are instances where we can have from no solution to infinite solutions.
Now, going back to our discussion where \(\mathbf{A} \in \mathbb{R}^{p \times p}\) is a square matrix, the maximum possible rank is \(p\). If \(\text{rank}(\mathbf{A}) = p\), we say the matrix has full rank. If \(\text{rank}(\mathbf{A}) < p\), the matrix is rank-deficient or singular. A singular matrix’s columns are linearly dependent, meaning the transformation collapses the space into a lower-dimensional subspace. This collapse is irreversible; you cannot uniquely determine the original vector from its lower-dimensional projection.
4.1.9 Invertibility conditions
We can now summarize the conditions for a square matrix to be invertible.
Theorem 4.1 (Invertibility Conditions). For a square matrix \(\mathbf{A} \in \mathbb{R}^{p \times p}\), the following statements are equivalent:
- \(\mathbf{A}\) is invertible.
- \(\mathbf{A}\) has full rank, i.e., \(\text{rank}(\mathbf{A}) = p\).
- The columns of \(\mathbf{A}\) are linearly independent.
- The rows of \(\mathbf{A}\) are linearly independent.
- The system of equations \(\mathbf{A}\mathbf{x} = \mathbf{0}\) has only the trivial solution \(\mathbf{x} = \mathbf{0}\).
- The determinant of \(\mathbf{A}\) is non-zero, i.e., \(\det(\mathbf{A}) \neq 0\).
4.2 Methods for Computing the Matrix Inverse
Knowing that an inverse exists is one thing; computing it is another. For matrices larger than \(3 \times 3\), we rely on systematic algorithms rather than direct formulas. These methods are not just of theoretical interest: their computational cost and numerical stability are critical considerations in practice.
4.2.1 Known Method to Solve Systems of Linear Equations
This is the classic textbook algorithm for finding an inverse by hand and is the basis for many computational implementations: Gauss-Jordan Elimination. The method involves augmenting the matrix \(\mathbf{A}\) with the identity matrix and performing elementary row operations to transform \(\mathbf{A}\) into \(\mathbf{I}\).
Algorithm. Follow the next steps:
- Create the augmented matrix \(\,\,[\mathbf{A} \,\,|\,\, \mathbf{I}]\).
- Use elementary row operations (scaling, swapping, and adding multiples of rows) to transform the left side of the augmented matrix into the identity matrix, \(\mathbf{I}\).
- The same sequence of operations will simultaneously transform the right side of the augmented matrix from \(\mathbf{I}\) into \(\mathbf{A}^{-1}\). The final state will be \(\,\,[\mathbf{I} \,\,|\,\, \mathbf{A}^{-1}]\).
Complexity. The Gauss-Jordan elimination algorithm requires a nested loop structure. For each of the \(p\) columns (and corresponding rows), we perform operations on all \(p\) rows. This leads to a computational complexity of \(O(p^3)\) arithmetic operations, which is the same as other standard inversion methods like LU decomposition (out of scope).
Example 4.6 (Inversion via Gauss-Jordan Elimination). Let’s find the inverse of \(\mathbf{A} = \begin{bmatrix} 2 & 1 \\ 1 & 1 \end{bmatrix}\).
Start with the augmented matrix:
\[ \left[ \begin{array}{cc|cc} 2 & 1 & 1 & 0 \\ 1 & 1 & 0 & 1 \end{array} \right] \]Perform row operations:
1. Swap Row 1 and Row 2 to get a 1 in the top-left corner:
2. Subtract 2 times Row 1 from Row 2 (\(R_2 \to R_2 - 2R_1\)):
\[ \left[ \begin{array}{cc|cc} 1 & 1 & 0 & 1 \\ 0 & -1 & 1 & -2 \end{array} \right] \]3. Multiply Row 2 by -1 (\(R_2 \to -R_2\)):
\[ \left[ \begin{array}{cc|cc} 1 & 1 & 0 & 1 \\ 0 & 1 & -1 & 2 \end{array} \right] \]4. Subtract Row 2 from Row 1 (\(R_1 \to R_1 - R_2\)):
\[ \left[ \begin{array}{cc|cc} 1 & 0 & 1 & -1 \\ 0 & 1 & -1 & 2 \end{array} \right] \]Result: The left side is now the identity matrix. The right side is the inverse:
\[ \mathbf{A}^{-1} = \begin{bmatrix} 1 & -1 \\ -1 & 2 \end{bmatrix} \]4.2.2 The Perils of Inversion: Numerical Instability
Although we have algorithms to compute the inverse, in practice, direct computation of \(\mathbf{A}^{-1}\) is often avoided in numerical applications. The reason is that the process of inversion can be highly sensitive to small changes or errors in the input matrix \(\mathbf{A}\), a property known as numerical instability.
The Condition Number. The sensitivity of a matrix to inversion is captured by its condition number, \(\kappa(\mathbf{A})\). Intuitively, the condition number measures how much the output of a problem (like solving \(\mathbf{A}\mathbf{x}=\mathbf{b}\)) can change for a small change in the input data (\(\mathbf{A}\) or \(\mathbf{b}\)).
- A matrix with a low condition number (close to 1) is well-conditioned. Small perturbations in the input lead to similarly small perturbations in the solution.
- A matrix with a high condition number is ill-conditioned. A tiny, almost imperceptible perturbation in the input can lead to a massive, dramatic change in the solution.
Geometrically, an ill-conditioned matrix is one that transforms space in a very non-uniform way, squashing it severely in some directions while stretching it greatly in others. Such a matrix is “nearly singular.”
Example 4.7 (A Perturbation Theory Perspective). Let’s see the catastrophic effect of inverting an ill-conditioned matrix with a simple example. Consider a matrix that is nearly singular:
\[ \mathbf{A} = \begin{bmatrix} 1 & 1 \\ 1 & 1.0001 \end{bmatrix} \]Its determinant is tiny: \(\det(\mathbf{A}) = (1)(1.0001) - (1)(1) = 0.0001\). Using the 2x2 inverse formula, we get:
\[ \mathbf{A}^{-1} = \frac{1}{0.0001} \begin{bmatrix} 1.0001 & -1 \\ -1 & 1 \end{bmatrix} = \begin{bmatrix} 10001 & -10000 \\ -10000 & 10000 \end{bmatrix} \]Now, let’s introduce a tiny perturbation to \(\mathbf{A}\), on the order of \(10^{-4}\), which could easily arise from measurement noise or floating-point representation error. Let our new matrix be:
\[ \tilde{\mathbf{A}} = \begin{bmatrix} 1 & 1 \\ 1 & 1.0002 \end{bmatrix} \]Its determinant is now \(\det(\tilde{\mathbf{A}}) = (1)(1.0002) - (1)(1) = 0.0002\). Its inverse is:
\[ \tilde{\mathbf{A}}^{-1} = \frac{1}{0.0002} \begin{bmatrix} 1.0002 & -1 \\ -1 & 1 \end{bmatrix} = \begin{bmatrix} 5001 & -5000 \\ -5000 & 5000 \end{bmatrix} \]A minuscule change of \(0.0001\) in a single entry of \(\mathbf{A}\) (a relative change of less than 0.01%) caused the entries of its inverse to change by approximately 5000, a massive 50% change! If we were using this inverse to solve a system of equations, our solution would be wildly inaccurate.
One entry changes, the answer jumps
Example 4.7's matrix A and its copy with one entry moved by 0.0001: their inverses, and the solution of A x = b for a b chosen so that the answer is (1, 1).
1A = np.array([[1.0, 1.0], [1.0, 1.0001]])
2A_changed = np.array([[1.0, 1.0], [1.0, 1.0002]]) # one entry moved by 0.0001
3b = np.array([2.0, 2.0001]) # so that A x = b has x = (1, 1)
4print("inverse of A:", np.linalg.inv(A).round(1).tolist())
5print("inverse after the change:", np.linalg.inv(A_changed).round(1).tolist())
6print("solution:", np.linalg.solve(A, b).round(4).tolist(),
7 "after the change:", np.linalg.solve(A_changed, b).round(4).tolist())
8print("condition number of A:", round(float(np.linalg.cond(A))))
9entries = np.linspace(1.00005, 1.0003, 200) # the bottom-right entry
10x2 = [np.linalg.solve(np.array([[1.0, 1.0], [1.0, e]]), b)[1] for e in entries]
inverse of A: [[10001.0, -10000.0], [-10000.0, 10000.0]] inverse after the change: [[5001.0, -5000.0], [-5000.0, 5000.0]] solution: [1.0, 1.0] after the change: [1.5, 0.5] condition number of A: 40002
The second coordinate of the solution as the bottom-right entry of A moves from 1.00005 to 1.0003. Moving that entry by 0.0001, from 1.0001 to 1.0002, moves the solution from (1, 1) to (1.5, 0.5) and changes the entries of the inverse by about 5000, half their size. NumPy puts the condition number of A at about 40,000, the value Section 4.3.2 computes from the eigenvalues; a large condition number is the warning sign for this behavior.
Recorded on an Apple M1 Max laptop, macOS 26.6.2, Python 3.13.7, NumPy 2.4.6, 2026-11-30.
4.3 Eigenvalues and Eigenvectors
Instead of viewing inversion as a series of row operations, we can approach it from a more fundamental, geometric perspective. A matrix represents a complex transformation, but are there special vectors that are only scaled by the matrix, without changing their direction?
Definition 4.4 (Eigenvector and Eigenvalue). For a square matrix \(\mathbf{A} \in \mathbb{R}^{p \times p}\), a non-zero vector \(\mathbf{v} \in \mathbb{R}^p\) is an eigenvector of \(\mathbf{A}\) if it satisfies the equation:
\[ \mathbf{A}\mathbf{v} = \lambda\mathbf{v} \]for some scalar \(\lambda\). The scalar \(\lambda\) is called the eigenvalue corresponding to the eigenvector \(\mathbf{v}\).
Eigenvectors are the “axes of transformation” for a matrix, representing directions that are left invariant (up to scaling).
4.3.1 From Eigenvectors to Eigendecomposition
To find these special pairs, we rearrange the equation:
\[ (\mathbf{A} - \lambda\mathbf{I})\mathbf{v} = \mathbf{0} \]For this equation to have a non-zero solution for \(\mathbf{v}\), the matrix \((\mathbf{A} - \lambda\mathbf{I})\) must be singular, which means its determinant must be zero: \(\det(\mathbf{A} - \lambda\mathbf{I}) = 0\). This is the characteristic equation, and its roots are the eigenvalues.
If a matrix \(\mathbf{A} \in \mathbb{R}^{p \times p}\) has \(p\) linearly independent eigenvectors, we say it is diagonalizable. Let’s collect these eigenvectors as the columns of a matrix \(\mathbf{V} = [\mathbf{v}_1, \dots, \mathbf{v}_p]\) and their corresponding eigenvalues into a diagonal matrix \(\boldsymbol{\Lambda}\) where \((\boldsymbol{\Lambda})_{ii} = \lambda_i\). The set of all eigenvector equations \(\mathbf{A}\mathbf{v}_i = \lambda_i\mathbf{v}_i\) can then be written compactly in matrix form:
\[ \begin{aligned} \mathbf{A}\mathbf{V} &= \mathbf{A}[\mathbf{v}_1, \dots, \mathbf{v}_p] = [\mathbf{A}\mathbf{v}_1, \dots, \mathbf{A}\mathbf{v}_p] \\ &= [\lambda_1\mathbf{v}_1, \dots, \lambda_p\mathbf{v}_p] = \mathbf{V}\boldsymbol{\Lambda}. \end{aligned} \]Since the eigenvectors are linearly independent, \(\mathbf{V}\) is invertible. We can right-multiply by \(\mathbf{V}^{-1}\) to obtain the eigendecomposition (EVD) of \(\mathbf{A}\):
\[ \mathbf{A} = \mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{-1}. \]4.3.2 Inversion and Complexity via Eigendecomposition
This decomposition provides a new way to understand matrix inversion. Using the property \((\mathbf{XYZ})^{-1} = \mathbf{Z}^{-1}\mathbf{Y}^{-1}\mathbf{X}^{-1}\), we get:
\[ \mathbf{A}^{-1} = (\mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{-1})^{-1} = (\mathbf{V}^{-1})^{-1} \boldsymbol{\Lambda}^{-1} \mathbf{V}^{-1} = \mathbf{V}\boldsymbol{\Lambda}^{-1}\mathbf{V}^{-1} \]This formula reveals a critical fact: the inverse \(\mathbf{A}^{-1}\) exists if and only if all eigenvalues of \(\mathbf{A}\) are non-zero, because only then can we compute \(\boldsymbol{\Lambda}^{-1}\) by taking the reciprocal of each diagonal entry.
Using this decomposition, solving the linear system \(\mathbf{A}\mathbf{x} = \mathbf{b}\) becomes a process of “unwrapping” the transformation:
\[ \begin{aligned} \mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{-1} \mathbf{x} &= \mathbf{b} \\ \boldsymbol{\Lambda}\mathbf{V}^{-1} \mathbf{x} &= \mathbf{V}^{-1}\mathbf{b} \quad (\text{multiplying by } \mathbf{V}^{-1}) \\ \mathbf{V}^{-1} \mathbf{x} &= \boldsymbol{\Lambda}^{-1}\mathbf{V}^{-1}\mathbf{b} \quad (\text{multiplying by } \boldsymbol{\Lambda}^{-1}) \\ \mathbf{x} &= \mathbf{V}\boldsymbol{\Lambda}^{-1}\mathbf{V}^{-1}\mathbf{b} \quad (\text{multiplying by } \mathbf{V}) \end{aligned} \]From this, we can see that the solution is \(\mathbf{x} = (\mathbf{V}\boldsymbol{\Lambda}^{-1}\mathbf{V}^{-1})\mathbf{b}\). This implies we have found a formula for the inverse of \(\mathbf{A}\).
Complexity. While this formula is elegant, it is not a free lunch. Finding the eigenvalues by solving the characteristic polynomial is numerically unstable and inefficient for large matrices. Practical EVD algorithms (like the iterative QR algorithm) are used instead. These advanced methods do not rely on determinants and have a computational complexity of roughly \(O(p^3)\) for a dense \(p \times p\) matrix, similar to direct inversion methods. For the purposes of this class, we will be discussing a more optimization approach in the next chapters, the power method!
Revisiting Numerical Stability through Eigendecomposition. Eigendecomposition gives us a powerful lens through which to understand the numerical instability we observed earlier. Let’s revisit the ill-conditioned matrix:
\[ \mathbf{A} = \begin{bmatrix} 1 & 1 \\ 1 & 1.0001 \end{bmatrix} \]Its eigenvalues can be calculated to be approximately:
\[ \lambda_1 \approx 2.00005 \quad \text{and} \quad \lambda_2 \approx 0.00005 \]Notice the huge disparity. The condition number for a symmetric matrix (that we left as a “mystery” quantity) is a quantity \(\kappa(\mathbf{A}) \geq 1\) and the larger it is, the more unstable the use of \(\mathbf{A}\) becomes. In particular, \(\kappa(\mathbf{A})\) is the ratio of the absolute largest to the absolute smallest eigenvalue:
\[ \kappa(\mathbf{A}) = \frac{\max_i |\lambda_i|}{\min_i |\lambda_i|} \approx \frac{2.00005}{0.00005} \approx 40{,}000 \]This extremely high condition number quantifies the matrix’s instability. The reason is clear from the eigendecomposition formula for the inverse. The inverse matrix \(\mathbf{A}^{-1}\) will have eigenvalues that are the reciprocals of the original eigenvalues:
\[ \frac{1}{\lambda_1} \approx 0.5 \quad \text{and} \quad \frac{1}{\lambda_2} \approx \frac{1}{0.00005} \approx 20{,}000 \]A tiny eigenvalue in \(\mathbf{A}\) becomes an enormous eigenvalue in \(\mathbf{A}^{-1}\). When we perturbed \(\mathbf{A}\) slightly, it caused a tiny shift in \(\lambda_2\). However, because the function \(f(x) = 1/x\) is extremely steep for small \(x\), this tiny shift in \(\lambda_2\) resulted in a massive, explosive change in its reciprocal \(1/\lambda_2\). This explains why the elements of the inverse matrix changed so dramatically.
4.3.3 Geometric Interpretation: A Change of Coordinate System
The most profound way to understand eigendecomposition is to see it as revealing the “natural” coordinate system of a linear transformation. The transformation \(\mathbf{A}\mathbf{x}\) can be interpreted as a three-step process (see also Figure 4.2):
- Change of Basis (\(\mathbf{V}^{-1}\mathbf{x}\)): The multiplication by \(\mathbf{V}^{-1}\) projects the input vector \(\mathbf{x}\) onto the basis formed by the eigenvectors of \(\mathbf{A}\). It computes the coordinates of \(\mathbf{x}\) in this new coordinate system.
- Simple Scaling (\(\boldsymbol{\Lambda}(\mathbf{V}^{-1}\mathbf{x})\)): In this new eigenvector basis, the transformation is incredibly simple. The diagonal matrix \(\boldsymbol{\Lambda}\) just stretches or shrinks the new coordinates along each eigenvector axis by the corresponding eigenvalue.
- Change Basis Back (\(\mathbf{V}(\boldsymbol{\Lambda}\mathbf{V}^{-1}\mathbf{x})\)): Finally, the multiplication by \(\mathbf{V}\) converts these scaled coordinates back into the original standard basis.
Essentially, eigendecomposition tells us that for any diagonalizable matrix, there exists a special set of axes (the eigenvectors) along which the complex action of the matrix simplifies to pure scaling.
4.3.4 Examples of Eigendecomposition
Example 4.8 (The Identity Matrix). The identity matrix \(\mathbf{I} = \left[\begin{smallmatrix} 1 & 0 \\ 0 & 1 \end{smallmatrix}\right]\) leaves every vector unchanged. Its transformation is trivial.
- Eigenvalues: Both are \(\lambda_1 = 1, \lambda_2 = 1\). The transformation scales every direction by 1.
- Eigenvectors: Any non-zero vector is an eigenvector, because \(\mathbf{I}\mathbf{v} = \mathbf{v} = 1 \cdot \mathbf{v}\). The simplest choice is to choose the standard basis vectors \(\mathbf{e}_1 = [1, 0]^\top\) and \(\mathbf{e}_2 =[0, 1]^\top\) as our eigenvector basis.
- Decomposition: \(\mathbf{V} = \mathbf{I}\), \(\boldsymbol{\Lambda} = \mathbf{I}\). So, \(\mathbf{I} = \mathbf{I}\mathbf{I}\mathbf{I}^{-1}\). The “natural” coordinate system is the standard one, and the scaling is unity in all directions.
Example 4.9 (A Diagonal Matrix). Consider \(\mathbf{A} = \left[\begin{smallmatrix} 3 & 0 \\ 0 & 0.5 \end{smallmatrix}\right]\). This matrix scales the x-axis by 3 and the y-axis by 0.5.
- Eigenvalues: \(\lambda_1 = 3, \lambda_2 = 0.5\).
- Eigenvectors: The standard basis vectors are the eigenvectors. For \(\mathbf{e}_1 =[1, 0]^\top\), \(\mathbf{A}\mathbf{e}_1 = 3\mathbf{e}_1\). For \(\mathbf{e}_2 =[0, 1]^\top\), \(\mathbf{A}\mathbf{e}_2 = [0, 0.5]^\top = 0.5\mathbf{e}_2\).
- Decomposition: \(\mathbf{V} = \mathbf{I}\), and \(\boldsymbol{\Lambda} = \mathbf{A}\). The decomposition is simply \(\mathbf{A} = \mathbf{I}\mathbf{A}\mathbf{I}^{-1}\). The natural coordinate system aligns with the standard axes, and the transformation is a pure, non-uniform stretch along these axes.
4.4 Orthogonal and Orthonormal Sets
Before diving into the more useful eigenvalue decomposition, and to be properly equipped for the Singular Value Decomposition (Chapter 5, § From Eigendecomposition to the Singular Value Decomposition), we must introduce a special class of matrices that represent pure rotations and reflections. These matrices are fundamental to understanding the geometry of many AI algorithms because they manipulate vectors without changing their intrinsic properties like length or the angles between them. To build this concept, we first need to be precise about the properties of the vectors that form these matrices.
4.4.1 Orthogonal vs. Orthonormal Vectors
The terms “orthogonal” and “orthonormal” are related but distinct.
- A set of vectors is orthogonal if every vector in the set is mutually perpendicular to every other vector. I.e., their inner product is zero.
- A set of vectors is orthonormal if it is an orthogonal set and every vector has a norm of 1 (i.e., they are all unit vectors).
Example 4.10 (Orthogonal vs. Orthonormal Sets). Consider the vectors \(\mathbf{v}_1 = \begin{bmatrix} 3 \\ 0 \end{bmatrix}\) and \(\mathbf{v}_2 = \begin{bmatrix} 0 \\ -2 \end{bmatrix}\). Their inner product is \(\langle \mathbf{v}_1, \mathbf{v}_2 \rangle = (3)(0) + (0)(-2) = 0\), so the set \(\{\mathbf{v}_1, \mathbf{v}_2\}\) is orthogonal. However, their norms are \(\|\mathbf{v}_1\|_2 = 3\) and \(\|\mathbf{v}_2\|_2 = 2\), so they are not unit vectors.
To make this set orthonormal, we normalize each vector by dividing it by its norm:
\[ \mathbf{u}_1 = \frac{\mathbf{v}_1}{\|\mathbf{v}_1\|_2} = \begin{bmatrix} 1 \\ 0 \end{bmatrix}, \quad \mathbf{u}_2 = \frac{\mathbf{v}_2}{\|\mathbf{v}_2\|_2} = \begin{bmatrix} 0 \\ -1 \end{bmatrix} \]The new set \(\{\mathbf{u}_1, \mathbf{u}_2\}\) is orthonormal. The vectors are still orthogonal, and now both have a norm of 1.
4.4.2 The Orthonormal Matrix (Officially Called the “Orthogonal Matrix”)
This brings us to a common point of confusion in linear algebra terminology. The matrices that perform rigid transformations are built from an orthonormal set of basis vectors, but for historical reasons, they are called orthogonal matrices.
Definition 4.5 (Orthogonal Matrix). A square matrix \(\mathbf{Q} \in \mathbb{R}^{p \times p}\) is orthogonal if its columns (and, equivalently, its rows) form an orthonormal set. This is equivalent to the condition:
\[ \mathbf{Q}^\top\mathbf{Q} = \mathbf{Q}\mathbf{Q}^\top = \mathbf{I}. \]This definition immediately implies a remarkable property: the inverse of an orthogonal matrix is simply its transpose (\(\mathbf{Q}^{-1} = \mathbf{Q}^\top\)). This makes inversion, a computationally expensive operation (\(O(p^3)\)), virtually free.
4.4.3 Geometric Properties and Energy Preservation
Orthogonal matrices have profound geometric properties because their constituent vectors are both perpendicular and of unit length.
- Length Preservation: For any vector \(\mathbf{x}\), the length (or norm) of the transformed vector \(\mathbf{Q}\mathbf{x}\) is the same as the length of \(\mathbf{x}\).
\[ \|\mathbf{Q}\mathbf{x}\|_2^2 = (\mathbf{Q}\mathbf{x})^\top(\mathbf{Q}\mathbf{x}) = \mathbf{x}^\top\mathbf{Q}^\top\mathbf{Q}\mathbf{x} = \mathbf{x}^\top\mathbf{I}\mathbf{x} = \|\mathbf{x}\|_2^2. \]
In many scientific domains, the squared norm of a vector, \(\|\mathbf{x}\|_2^2\), is referred to as its energy. Therefore, orthogonal transformations are energy-preserving.
- Angle Preservation: The angle between two vectors \(\mathbf{x}\) and \(\mathbf{y}\) is the same as the angle between \(\mathbf{Q}\mathbf{x}\) and \(\mathbf{Q}\mathbf{y}\). This follows from the fact that inner products are preserved: \[ (\mathbf{Q}\mathbf{x})^\top(\mathbf{Q}\mathbf{y}) = \mathbf{x}^\top\mathbf{Q}^\top\mathbf{Q}\mathbf{y} = \mathbf{x}^\top\mathbf{y}. \]
Because they preserve all lengths and angles, transformations by orthogonal matrices are rigid transformations: They correspond to pure rotations and reflections of space, without any stretching, shearing, or compressing.
Example 4.11 (A 2D Rotation Matrix). The matrix that rotates a vector counter-clockwise by an angle \(\theta\) is a classic orthogonal matrix:
\[ \mathbf{Q} = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} \]Let’s verify its properties for \(\theta = 30^\circ\) (\(\cos30^\circ = \frac{\sqrt{3}}{2}, \sin30^\circ = \frac{1}{2}\)):
\[ \mathbf{Q} = \begin{bmatrix} \frac{\sqrt{3}}{2} & -\frac{1}{2} \\ \frac{1}{2} & \frac{\sqrt{3}}{2} \end{bmatrix} \]The columns are orthogonal: \((\frac{\sqrt{3}}{2})(-\frac{1}{2}) + (\frac{1}{2})(\frac{\sqrt{3}}{2}) = 0\). The columns are unit vectors: \((\frac{\sqrt{3}}{2})^2 + (\frac{1}{2})^2 = \frac{3}{4} + \frac{1}{4} = 1\). Thus, the columns form an orthonormal set, and \(\mathbf{Q}\) is an orthogonal matrix.
Let’s apply it to a vector \(\mathbf{x} = \begin{bmatrix} 2 \\ 0 \end{bmatrix}\). Its energy is \(\|\mathbf{x}\|_2^2 = 4\).
\[ \mathbf{Qx} = \begin{bmatrix} \frac{\sqrt{3}}{2} & -\frac{1}{2} \\ \frac{1}{2} & \frac{\sqrt{3}}{2} \end{bmatrix} \begin{bmatrix} 2 \\ 0 \end{bmatrix} = \begin{bmatrix} \sqrt{3} \\ 1 \end{bmatrix} \]The energy of the transformed vector is \(\|\mathbf{Qx}\|_2^2 = (\sqrt{3})^2 + 1^2 = 3 + 1 = 4\). The energy is preserved, as expected.
Example 4.12 (Orthogonal, but not Orthonormal). It is crucial to understand why the unit norm property is essential. Consider the matrix \(\mathbf{A}\) formed by our earlier orthogonal-but-not-orthonormal vectors:
\[ \mathbf{A} = \begin{bmatrix} 3 & 0 \\ 0 & -2 \end{bmatrix} \]The columns are orthogonal. However, \(\mathbf{A}^\top\mathbf{A} = \left[\begin{smallmatrix} 9 & 0 \\ 0 & 4 \end{smallmatrix}\right] \neq \mathbf{I}\). Let’s apply this matrix to the vector \(\mathbf{x} = \begin{bmatrix} 1 \\ 1 \end{bmatrix}\). The initial energy is \(\|\mathbf{x}\|_2^2 = 1^2+1^2 = 2\).
\[ \mathbf{Ax} = \begin{bmatrix} 3 & 0 \\ 0 & -2 \end{bmatrix} \begin{bmatrix} 1 \\ 1 \end{bmatrix} = \begin{bmatrix} 3 \\ -2 \end{bmatrix} \]The energy of the transformed vector is \(\|\mathbf{Ax}\|_2^2 = 3^2 + (-2)^2 = 9+4=13\). The energy is clearly not preserved. This transformation stretches space (by 3x along the first axis and 2x along the second; Note that these are the absolute values of the eigenvalues of the matrix \(\mathbf{A}\)!) and is therefore not a rigid transformation. This highlights that orthogonality of columns alone is not sufficient; the unit norm condition is what guarantees energy and length preservation.
We now turn to a special case of the eigendecomposition of \(\mathbf{A}\).
4.4.4 The Special Case: Eigendecomposition of Symmetric Matrices
The eigendecomposition becomes particularly elegant and powerful for symmetric matrices, which are ubiquitous in AI (e.g., covariance matrices, Hessians, kernel matrices).
Theorem 4.2 (The Spectral Theorem). Every real symmetric matrix \(\mathbf{A} \in \mathbb{R}^{p \times p}\) is diagonalizable. Furthermore, its eigenvalues are all real, and its eigenvectors can be chosen to form an orthonormal basis.
This is a profound result. Because the eigenvectors form an orthonormal basis, the general eigenvector matrix \(\mathbf{V}\) becomes a special orthogonal matrix \(\mathbf{Q}\)! Recall that their key property is that their inverse is simply their transpose: \(\mathbf{Q}^{-1} = \mathbf{Q}^\top\).
This simplifies the general eigendecomposition formula \(\mathbf{A} = \mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{-1}\) wonderfully. By replacing \(\mathbf{V}\) with \(\mathbf{Q}\) and \(\mathbf{V}^{-1}\) with \(\mathbf{Q}^\top\), we arrive at the standard form for symmetric matrices:
\[ \mathbf{A} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top. \]This is one of the most important and useful factorizations in all of linear algebra, as it avoids the costly and potentially unstable computation of a general matrix inverse.
4.5 A Deeper Look: EVD as a Sum of Outer Products
The factorization \(\mathbf{A} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top\) is not just a compact formula; it reveals that any symmetric matrix can be constructed as a weighted sum of simple, rank-1 matrices. This is one of the most powerful and intuitive ways to understand the structure of a linear transformation.
If we write out the matrices explicitly, with \(\mathbf{q}_i\) being the \(i\)-th orthonormal eigenvector and \(\lambda_i\) its corresponding eigenvalue, the product can be expanded:
\[ \begin{aligned} \mathbf{A} &= \begin{bmatrix} | & & | \\ \mathbf{q}_1 & \cdots & \mathbf{q}_p \\ | & & | \end{bmatrix} \begin{bmatrix} \lambda_1 & & 0 \\ & \ddots & \\ 0 & & \lambda_p \end{bmatrix} \begin{bmatrix} \text{--- } \mathbf{q}_1^\top \text{ ---} \\ \vdots \\ \text{--- } \mathbf{q}_p^\top \text{ ---} \end{bmatrix} \\ &= \begin{bmatrix} | & & | \\ \lambda_1\mathbf{q}_1 & \cdots & \lambda_p\mathbf{q}_p \\ | & & | \end{bmatrix} \begin{bmatrix} \text{--- } \mathbf{q}_1^\top \text{ ---} \\ \vdots \\ \text{--- } \mathbf{q}_p^\top \text{ ---} \end{bmatrix} \\ &= \lambda_1 (\mathbf{q}_1\mathbf{q}_1^\top) + \lambda_2 (\mathbf{q}_2\mathbf{q}_2^\top) + \dots + \lambda_p (\mathbf{q}_p\mathbf{q}_p^\top) = \sum_{i=1}^p \lambda_i \mathbf{q}_i\mathbf{q}_i^\top \end{aligned} \]This expansion is matrix multiplication done column by row, so it holds for any matrix in place of \(\mathbf{Q}\); the orthonormality of the \(\mathbf{q}_i\) is what makes each piece a projection, as we see next. This is the spectral decomposition or outer product expansion of \(\mathbf{A}\). It expresses the matrix \(\mathbf{A}\) as a linear combination of simpler matrices, where each matrix is an outer product of an eigenvector with itself, and the weighting is the corresponding eigenvalue.
4.5.1 The Building Blocks: Rank-1 Eigenvector Matrices
Each term \(\mathbf{q}_i\mathbf{q}_i^\top\) in the sum is a matrix with a special structure.
- It is a Rank-1 Matrix: The outer product of any two non-zero vectors creates a rank-1 matrix. Geometrically, consider the effect of this matrix on any vector \(\mathbf{x}\):
\[ (\mathbf{q}_i\mathbf{q}_i^\top)\mathbf{x} = \mathbf{q}_i(\mathbf{q}_i^\top\mathbf{x}) \]
The term in parentheses, \(\mathbf{q}_i^\top\mathbf{x}\), is a scalar: it’s the projection of \(\mathbf{x}\) onto the direction of \(\mathbf{q}_i\). Therefore, the entire transformation maps every vector \(\mathbf{x}\) to a scalar multiple of the single eigenvector \(\mathbf{q}_i\). The entire output space of this matrix is just the line spanned by \(\mathbf{q}_i\), which is a 1-dimensional subspace. Hence, its rank is 1.
- It is a Projection Matrix: Since \(\mathbf{q}_i\) is a unit vector, the matrix \(\mathbf{P}_i = \mathbf{q}_i\mathbf{q}_i^\top\) is the orthogonal projection matrix onto the subspace (the line) spanned by \(\mathbf{q}_i\). Applying it twice has no further effect: \(\mathbf{P}_i^2 = (\mathbf{q}_i\mathbf{q}_i^\top)(\mathbf{q}_i\mathbf{q}_i^\top) = \mathbf{q}_i(\mathbf{q}_i^\top\mathbf{q}_i)\mathbf{q}_i^\top = \mathbf{q}_i(1)\mathbf{q}_i^\top = \mathbf{P}_i\).
4.5.2 Orthogonality of the Building Blocks
These rank-1 projection matrices are not just simple; they are also mutually orthogonal in the space of matrices. We can show this using the matrix inner product:
\[ \langle \mathbf{q}_i\mathbf{q}_i^\top, \mathbf{q}_j\mathbf{q}_j^\top \rangle = \text{Tr}\left((\mathbf{q}_i\mathbf{q}_i^\top)^\top (\mathbf{q}_j\mathbf{q}_j^\top)\right) = \text{Tr}\left((\mathbf{q}_i\mathbf{q}_i^\top) (\mathbf{q}_j\mathbf{q}_j^\top)\right) \]Using the associative property and the fact that the eigenvectors are orthogonal (\(\mathbf{q}_i^\top\mathbf{q}_j = 0\) for \(i \neq j\)):
\[ = \text{Tr}\left(\mathbf{q}_i (\mathbf{q}_i^\top \mathbf{q}_j) \mathbf{q}_j^\top\right) = \text{Tr}\left(\mathbf{q}_i (0) \mathbf{q}_j^\top\right) = \text{Tr}(\mathbf{0}) = 0. \]This means each matrix \(\mathbf{q}_i\mathbf{q}_i^\top\) represents a unique, independent piece of the total transformation.
4.5.3 From Rank-1 Sums to Full Rank
The spectral decomposition expresses the full matrix \(\mathbf{A}\) as a sum of these orthogonal, rank-1 building blocks.
- If all eigenvalues \(\lambda_i\) are non-zero, then we are summing \(p\) independent rank-1 matrices. This constructs a matrix of full rank, i.e., \(\text{rank}(\mathbf{A}) = p\).
- This provides a powerful intuition: a full-rank symmetric matrix embodies a transformation that acts non-trivially along \(p\) mutually orthogonal directions.
Example 4.13 (Decomposing a 2x2 Matrix). Consider the symmetric matrix \(\mathbf{A} = \left[\begin{smallmatrix} 2.5 & -1.5 \\ -1.5 & 2.5 \end{smallmatrix}\right]\). Its normalized eigenvectors and eigenvalues are:
\[ \lambda_1 = 4, \quad \mathbf{q}_1 = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 \\ -1 \end{bmatrix} \quad \text{and} \quad \lambda_2 = 1, \quad \mathbf{q}_2 = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 \\ 1 \end{bmatrix} \]Let’s compute the rank-1 outer product matrices:
\[ \mathbf{q}_1\mathbf{q}_1^\top = \frac{1}{2}\begin{bmatrix} 1 \\ -1 \end{bmatrix}\begin{bmatrix} 1 & -1 \end{bmatrix} = \begin{bmatrix} 0.5 & -0.5 \\ -0.5 & 0.5 \end{bmatrix} \] \[ \mathbf{q}_2\mathbf{q}_2^\top = \frac{1}{2}\begin{bmatrix} 1 \\ 1 \end{bmatrix}\begin{bmatrix} 1 & 1 \end{bmatrix} = \begin{bmatrix} 0.5 & 0.5 \\ 0.5 & 0.5 \end{bmatrix} \]Now, let’s reconstruct \(\mathbf{A}\) using the weighted sum:
\[ \begin{aligned} \mathbf{A} &= \lambda_1 \mathbf{q}_1\mathbf{q}_1^\top + \lambda_2 \mathbf{q}_2\mathbf{q}_2^\top \\ &= 4 \begin{bmatrix} 0.5 & -0.5 \\ -0.5 & 0.5 \end{bmatrix} + 1 \begin{bmatrix} 0.5 & 0.5 \\ 0.5 & 0.5 \end{bmatrix} \\ &= \begin{bmatrix} 2 & -2 \\ -2 & 2 \end{bmatrix} + \begin{bmatrix} 0.5 & 0.5 \\ 0.5 & 0.5 \end{bmatrix} = \begin{bmatrix} 2.5 & -1.5 \\ -1.5 & 2.5 \end{bmatrix}. \end{aligned} \]The original matrix is perfectly reconstructed.
4.5.4 The Impact of Zero Eigenvalues and the Condensed EVD
What happens if one or more eigenvalues are zero? If an eigenvalue \(\lambda_k = 0\), its corresponding term \(\lambda_k \mathbf{q}_k\mathbf{q}_k^\top\) in the sum is just the zero matrix. It contributes nothing to the final matrix \(\mathbf{A}\).
This means that if a symmetric matrix has only \(r < p\) non-zero eigenvalues, its spectral decomposition is a sum of only \(r\) rank-1 matrices:
\[ \mathbf{A} = \sum_{i=1}^r \lambda_i \mathbf{q}_i\mathbf{q}_i^\top \quad (\text{where } \lambda_i \neq 0 \text{ for } i \le r) \]This directly implies that the rank of \(\mathbf{A}\) is exactly \(r\). The transformation represented by \(\mathbf{A}\) collapses any information in the directions of the eigenvectors corresponding to the zero eigenvalues.
This observation allows us to write the eigendecomposition in a more compact and efficient form. We can discard the eigenvectors corresponding to zero eigenvalues.
Definition 4.6 (Condensed Eigendecomposition). Let \(\mathbf{A}\) be a symmetric matrix of rank \(r\). Let \(\mathbf{Q}_r \in \mathbb{R}^{p \times r}\) be the matrix whose columns are the \(r\) orthonormal eigenvectors corresponding to the \(r\) non-zero eigenvalues, and let \(\boldsymbol{\Lambda}_r \in \mathbb{R}^{r \times r}\) be the diagonal matrix of these non-zero eigenvalues. The condensed eigendecomposition is:
\[ \mathbf{A} = \mathbf{Q}_r \boldsymbol{\Lambda}_r \mathbf{Q}_r^\top \]This form is computationally more efficient to store and work with, as it discards the irrelevant zero-eigenvalue components. It is the basis for dimensionality reduction techniques like Principal Component Analysis (PCA).
Example 4.14 (A Rank-Deficient Matrix). Consider the matrix \(\mathbf{A} = \left[\begin{smallmatrix} 1 & 1 \\ 1 & 1 \end{smallmatrix}\right]\). It is symmetric. Its eigenvalues are \(\lambda_1 = 2\) and \(\lambda_2 = 0\). The rank is 1. The corresponding orthonormal eigenvectors are \(\mathbf{q}_1 = \frac{1}{\sqrt{2}}\left[\begin{smallmatrix} 1 \\ 1 \end{smallmatrix}\right]\) and \(\mathbf{q}_2 = \frac{1}{\sqrt{2}}\left[\begin{smallmatrix} 1 \\ -1 \end{smallmatrix}\right]\).
The full spectral decomposition is:
\[ \mathbf{A} = 2 \left(\frac{1}{2}\begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix}\right) + 0 \left(\frac{1}{2}\begin{bmatrix} 1 & -1 \\ -1 & 1 \end{bmatrix}\right) = \begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix} + \begin{bmatrix} 0 & 0 \\ 0 & 0 \end{bmatrix} \]The term for \(\lambda_2=0\) vanishes.
The condensed eigendecomposition only uses the non-zero components:
- \(\mathbf{Q}_r = \mathbf{Q}_1 = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 \\ 1 \end{bmatrix}\) (a \(2 \times 1\) matrix)
- \(\boldsymbol{\Lambda}_r = \boldsymbol{\Lambda}_1 = [2]\) (a \(1 \times 1\) matrix)
This compact form perfectly reconstructs the rank-1 matrix using only its single essential dimension of transformation.
4.6 Eigendecomposition in Action: PCA
We now arrive at one of the most fundamental and widely used algorithms in AI and data science: Principal Component Analysis (PCA). PCA is a technique for dimensionality reduction, but it is more than just a way to save memory or computation. At its heart, PCA is a method for finding the most meaningful basis to represent your data. It answers the question: “In which directions does my data vary the most?”
The solution to this question, as we will see, is found directly through the eigendecomposition of a special symmetric matrix derived from the data.
4.6.1 The Core Idea: Finding the Axes of Maximum Variance
Imagine a dataset of points plotted in 2D space. If the points form a random, circular cloud, then the standard \(x_1\) and \(x_2\) axes are a perfectly good description of the data’s spread. But what if the data forms a tilted, elliptical cloud?
As shown in the figure, the data is much more spread out along the red arrow PC1 than along any other direction. The direction PC2, orthogonal to PC1, captures the next largest amount of spread. PCA is the method that finds these “natural” axes of the data, which we call the principal components. (Here variance means the average squared distance from the mean; for numbers whose mean is zero, it is the average of their squares.)
4.6.2 PCA Formalization
Let’s formalize this. We want to find a direction, represented by a unit vector \(\mathbf{v}\), such that when we project our data onto this direction, the variance of the projected data is maximized (i.e., we capture most of the data’s behavior).
The Data: We start with a data matrix \(\mathbf{X} \in \mathbb{R}^{n \times p}\), where \(n\) is the number of data points and \(p\) is the number of features. Each row \(\mathbf{x}_i^\top\) is a single data point.
Center the Data: First, we must center the data by subtracting the mean of each feature. This ensures we are measuring variance (spread around the center) rather than the location of the data cloud. Let \(\mathbf{X}_c\) be the centered data matrix.
Project the Data: The projection of a single centered data point (row \(i\) of \(\mathbf{X}_c\)) onto the direction \(\mathbf{v}\) is a scalar given by the inner product of that row with \(\mathbf{v}\). The collection of all \(n\) projections can be computed in one go with the matrix-vector product \(\mathbf{X}_c\mathbf{v}\). This results in an \(n\)-dimensional vector of the projected coordinates.
Maximize the Variance: The variance of these projected points is proportional to the sum of their squares (since their mean is zero). This is simply the squared \(\ell_2\)-norm of the projected vector. Our objective is to maximize this quantity. This leads us to the formal optimization problem for the first principal component:
\[ \underset{\mathbf{v}}{\text{maximize}} \quad \|\mathbf{X}_c\mathbf{v}\|_2^2 \quad \text{subject to} \quad \|\mathbf{v}\|_2^2 = 1 \]The constraint \(\|\mathbf{v}\|_2^2 = 1\) is crucial; without it, we could achieve infinite variance simply by making \(\mathbf{v}\) infinitely long. We are interested only in the direction.
4.6.3 The Solution: Eigendecomposition Emerges
Let’s rewrite the objective function using matrix properties:
\[ \|\mathbf{X}_c\mathbf{v}\|_2^2 = (\mathbf{X}_c\mathbf{v})^\top(\mathbf{X}_c\mathbf{v}) = \mathbf{v}^\top\mathbf{X}_c^\top\mathbf{X}_c\mathbf{v} \]Let’s define the matrix \(\mathbf{S} = \mathbf{X}_c^\top\mathbf{X}_c\). This \(p \times p\) matrix is called the scatter matrix. Notice that \(\mathbf{S}\) is symmetric, because \(\mathbf{S}^\top = (\mathbf{X}_c^\top\mathbf{X}_c)^\top = \mathbf{X}_c^\top(\mathbf{X}_c^\top)^\top = \mathbf{X}_c^\top\mathbf{X}_c = \mathbf{S}\). Our optimization problem is now:
\[ \underset{\mathbf{v}}{\text{maximize}} \quad \mathbf{v}^\top\mathbf{S}\mathbf{v} \quad \text{subject to} \quad \mathbf{v}^\top\mathbf{v} = 1 \]This is a classic norm-constrained quadratic optimization. But how do we solve it? The answer lies in viewing the scatter matrix \(\mathbf{S}\) not as a block of numbers, but as a sum of its fundamental components, as revealed by its eigendecomposition.
4.6.4 The Solution Revealed by Spectral Decomposition
Since the scatter matrix \(\mathbf{S}\) is real and symmetric, the Spectral Theorem guarantees it has an eigendecomposition \(\mathbf{S} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top\). As we’ve seen, this can be expanded into a sum of rank-1 outer product matrices:
\[ \mathbf{S} = \sum_{i=1}^p \lambda_i \mathbf{q}_i\mathbf{q}_i^\top \]where the \(\lambda_i\) are the eigenvalues and the \(\mathbf{q}_i\) are the corresponding orthonormal eigenvectors which form a basis for \(\mathbb{R}^p\). Let’s substitute this expansion into our objective function:
\[ \begin{aligned} \mathbf{v}^\top\mathbf{S}\mathbf{v} &= \mathbf{v}^\top \left( \sum_{i=1}^p \lambda_i \mathbf{q}_i\mathbf{q}_i^\top \right) \mathbf{v} \\ &= \sum_{i=1}^p \lambda_i \mathbf{v}^\top (\mathbf{q}_i\mathbf{q}_i^\top) \mathbf{v} \\ &= \sum_{i=1}^p \lambda_i (\mathbf{v}^\top\mathbf{q}_i)(\mathbf{q}_i^\top\mathbf{v}) \\ &= \sum_{i=1}^p \lambda_i (\langle \mathbf{v}, \mathbf{q}_i \rangle)^2 \end{aligned} \]The term \((\langle \mathbf{v}, \mathbf{q}_i \rangle)^2\) is the squared projection of our chosen direction \(\mathbf{v}\) onto the eigenvector \(\mathbf{q}_i\). Let’s call the projection coefficient \(\alpha_i = \langle \mathbf{v}, \mathbf{q}_i \rangle\). Our optimization problem is now:
\[ \underset{\alpha_1, \dots, \alpha_p}{\text{maximize}} \quad \sum_{i=1}^p \lambda_i \alpha_i^2 \quad \text{subject to} \quad \sum_{i=1}^p \alpha_i^2 = 1 \]The constraint \(\sum \alpha_i^2 = 1\) comes from the fact that \(\|\mathbf{v}\|_2^2=1\) and the \(\mathbf{q}_i\) form an orthonormal basis.
The objective is now a weighted average of the eigenvalues, where the weights \(\alpha_i^2\) must sum to 1. To make this sum as large as possible, we should put all of our “weight” on the largest term. By convention, we sort the eigenvalues such that \(\lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_p\). The maximum value is achieved by setting the weight for the largest eigenvalue, \(\lambda_1\), to 1, and all other weights to 0.
- Set \(\alpha_1^2 = 1\) (so \(\alpha_1 = \pm 1\))
- Set \(\alpha_i^2 = 0\) for all \(i > 1\).
This choice of coefficients means that our optimal vector \(\mathbf{v}\) must be perfectly aligned with the eigenvector \(\mathbf{q}_1\), i.e., \(\mathbf{v} = \mathbf{q}_1\) (or \(-\mathbf{q}_1\), which represents the same direction). The maximum value of the objective function is, in fact, that largest eigenvalue itself:
\[ \mathbf{q}_1^\top\mathbf{S}\mathbf{q}_1 = \sum_{i=1}^p \lambda_i (\langle \mathbf{q}_1, \mathbf{q}_i \rangle)^2 = \lambda_1(1)^2 + \lambda_2(0)^2 + \dots = \lambda_1 \]Example 4.15 (The Suboptimality of the Second Eigenvector). Let’s illustrate why just picking any direction is a bad idea. Suppose after centering our data, we compute the scatter matrix:
\[ \mathbf{S} = \begin{bmatrix} 6.8 & -2.4 \\ -2.4 & 3.2 \end{bmatrix} \]The eigendecomposition yields:
- \(\lambda_1 = 8\), with eigenvector \(\mathbf{q}_1 = \frac{1}{\sqrt{5}}\begin{bmatrix} 2 \\ -1 \end{bmatrix}\)
- \(\lambda_2 = 2\), with eigenvector \(\mathbf{q}_2 = \frac{1}{\sqrt{5}}\begin{bmatrix} 1 \\ 2 \end{bmatrix}\)
(Check: \(\mathbf{S}\left[\begin{smallmatrix} 2 \\ -1 \end{smallmatrix}\right] = \left[\begin{smallmatrix} 16 \\ -8 \end{smallmatrix}\right] = 8\left[\begin{smallmatrix} 2 \\ -1 \end{smallmatrix}\right]\) and \(\mathbf{S}\left[\begin{smallmatrix} 1 \\ 2 \end{smallmatrix}\right] = \left[\begin{smallmatrix} 2 \\ 4 \end{smallmatrix}\right] = 2\left[\begin{smallmatrix} 1 \\ 2 \end{smallmatrix}\right]\).) The total “variance” in the data is the sum of the eigenvalues, \(8+2=10\).
If we choose the first principal component, \(\mathbf{v} = \mathbf{q}_1\), the variance we capture is \(\lambda_1 = 8\). This single direction explains \(8/10 = 80\%\) of the total variance in the data.
If, for some reason, we were to choose the second principal component, \(\mathbf{v} = \mathbf{q}_2\), the variance we capture would be only \(\lambda_2 = 2\). This direction explains only \(2/10 = 20\%\) of the total variance.
Projecting the data onto \(\mathbf{q}_2\) would create a 1D representation that throws away the primary axis of variation, losing the majority of the information about how the data points differ from each other.
4.6.5 The Betting Game Analogy
Imagine you are playing a game where your goal is to “capture” as much information about a dataset as possible.
- The Game: Represent a \(p\)-dimensional dataset using a single line.
- Your Bet: You get to choose one direction (one unit vector \(\mathbf{v}\)) to define that line.
- The Payoff: Your score is the variance of the data projected onto your chosen line. A high score means you’ve captured the data’s main spread; a low score means the data looks like a compressed blob on your line.
The PCA optimization problem, \(\text{maximize } \mathbf{v}^\top\mathbf{S}\mathbf{v}\), is exactly this game. The eigendecomposition of \(\mathbf{S}\) gives you the perfect strategy guide. The eigenvalues are the potential payoffs, and the eigenvectors are the directions you can bet on.
Why is the leading eigenvector the best bet? The proof shows that the maximum possible payoff is \(\lambda_1\), and you can only achieve this payoff by betting on the direction \(\mathbf{q}_1\). Placing your bet on any other direction \(\mathbf{v}\) is equivalent to spreading your bet across multiple eigenvectors, and the payoff will be a weighted average of the eigenvalues, which will always be less than the maximum, \(\lambda_1\).
Why not bet on two directions? If the rules change and you’re allowed to pick two orthogonal directions to form a plane, the game becomes maximizing the total variance captured by that plane. The optimal strategy is to choose the two directions with the highest individual payoffs: \(\mathbf{q}_1\) and \(\mathbf{q}_2\). Your total score would be \(\lambda_1 + \lambda_2\). PCA provides the optimal strategy for any number of “bets” you are allowed to make; that is, for any target dimensionality \(k\). You always choose the top \(k\) eigenvectors to capture the maximum possible information.
4.6.6 Finding Subsequent Components
To find the second principal component, we seek the direction \(\mathbf{v}_2\) that maximizes the variance, with the additional constraint that it must be orthogonal to the first component \(\mathbf{v}_1\). This ensures we are capturing new, uncorrelated information. The problem becomes:
\[ \underset{\mathbf{v}_2}{\text{maximize}} \quad \mathbf{v}_2^\top\mathbf{S}\mathbf{v}_2 \quad \text{subject to} \quad \mathbf{v}_2^\top\mathbf{v}_2 = 1 \quad \text{and} \quad \mathbf{v}_2^\top\mathbf{v}_1 = 0. \]The solution to this is the eigenvector of \(\mathbf{S}\) corresponding to the second-largest eigenvalue.
This pattern continues: the \(k\)-th principal component is the eigenvector of the scatter matrix \(\mathbf{S}\) corresponding to the \(k\)-th largest eigenvalue. The principal components are the orthonormal eigenvectors of \(\mathbf{X}_c^\top\mathbf{X}_c\), sorted by the magnitude of their corresponding eigenvalues.
4.6.7 The PCA “meta”-algorithm: A Step-by-Step Implementation
We described the following procedure:
- Start with your data matrix \(\mathbf{X} \in \mathbb{R}^{n \times p}\).
- Center the data: For each feature (column), compute its mean and subtract it from all entries in that column. This yields the centered matrix \(\mathbf{X}_c\).
- Form the scatter matrix: Compute the symmetric matrix \(\mathbf{S} = \mathbf{X}_c^\top\mathbf{X}_c\).
- Perform eigendecomposition: Find the eigenvalues and orthonormal eigenvectors of \(\mathbf{S}\). This gives \(\mathbf{S} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top\), where the columns of \(\mathbf{Q}\) are the eigenvectors and the diagonal of \(\boldsymbol{\Lambda}\) contains the eigenvalues. By convention, sort the columns of \(\mathbf{Q}\) and the entries of \(\boldsymbol{\Lambda}\) in descending order of eigenvalue magnitude.
- Identify Principal Components: The sorted eigenvectors (the columns of \(\mathbf{Q}\)) are the principal components. The corresponding eigenvalues in \(\boldsymbol{\Lambda}\) represent the variance captured by each component.
- Reduce Dimensionality (Optional): To reduce the data from \(p\) dimensions to \(k\) dimensions (\(k < p\)), select the first \(k\) columns of \(\mathbf{Q}\) to form a matrix \(\mathbf{Q}_k \in \mathbb{R}^{p \times k}\).
- Project the Data: The new, lower-dimensional representation of the data is obtained by projecting the centered data onto these components:
\[ \mathbf{Z} = \mathbf{X}_c \mathbf{Q}_k \]
The resulting matrix \(\mathbf{Z} \in \mathbb{R}^{n \times k}\) contains the new coordinates for each of the \(n\) data points in the \(k\)-dimensional space of principal components.
Example 4.16 (A Hand-Calculated PCA). Let’s find the first principal component for a simple dataset:
\[ \mathbf{X} = \begin{bmatrix} 1 & 1 \\ 3 & 2 \\ 5 & 3 \end{bmatrix} \]1. Center the data: Per column, the mean is \(\boldsymbol{\mu} =[3, 2]^\top\).
\[ \mathbf{X}_c = \begin{bmatrix} 1-3 & 1-2 \\ 3-3 & 2-2 \\ 5-3 & 3-2 \end{bmatrix} = \begin{bmatrix} -2 & -1 \\ 0 & 0 \\ 2 & 1 \end{bmatrix} \]2. Form the scatter matrix:
\[ \mathbf{S} = \mathbf{X}_c^\top\mathbf{X}_c = \begin{bmatrix} -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix} \begin{bmatrix} -2 & -1 \\ 0 & 0 \\ 2 & 1 \end{bmatrix} = \begin{bmatrix} 8 & 4 \\ 4 & 2 \end{bmatrix} \]3. Perform Eigendecomposition: The eigenvalues are \(\lambda_1 = 10\) and \(\lambda_2 = 0\). The normalized eigenvector is \(\mathbf{q}_1 = \frac{1}{\sqrt{5}}\left[\begin{smallmatrix} 2 \\ 1 \end{smallmatrix}\right]\).
The first principal component (the direction of maximum variance) is \(\mathbf{q}_1 = \frac{1}{\sqrt{5}}\left[\begin{smallmatrix} 2 \\ 1 \end{smallmatrix}\right]\). The second eigenvalue is 0, which means all the centered data points lie perfectly on the line defined by \(\mathbf{q}_1\). The intrinsic dimensionality of this dataset is only 1.
Principal components: Example 4.16, then a cloud
Example 4.16's three points, then 200 random points from a stretched cloud: center the data, form the scatter matrix, and take its eigenvectors.
1X = np.array([[1.0, 1.0], [3.0, 2.0], [5.0, 3.0]]) # Example 4.16
2Xc = X - X.mean(axis=0)
3lam, Q = np.linalg.eigh(Xc.T @ Xc) # ascending eigenvalues
4q1 = Q[:, -1] * np.sign(Q[0, -1]) # sign chosen as in the example
5print("eigenvalues:", np.round(lam, 6) + 0.0, " first component:", q1.round(4))
6rng = np.random.default_rng(0)
7cloud = rng.normal(size=(200, 2)) @ np.array([[3.0, 1.0], [0.0, 0.5]])
8C = cloud - cloud.mean(axis=0)
9lamC, QC = np.linalg.eigh(C.T @ C)
10q1C = QC[:, -1] * np.sign(QC[0, -1])
11print("cloud of 200: eigenvalues", lamC.round(1), " first component", q1C.round(3))
eigenvalues: [ 0. 10.] first component: [0.8944 0.4472] cloud of 200: eigenvalues [ 45.1 1934.3] first component [0.943 0.332]
In the first panel, Example 4.16's three points lie on one line, so the second eigenvalue is 0 and the first component, (2, 1)/√5 ≈ (0.894, 0.447), describes them exactly. The second panel shows the cloud with its two principal axes. The first eigenvalue is about 43 times the second, so the first component carries 98% of the variance.
Recorded on an Apple M1 Max laptop, macOS 26.6.2, Python 3.13.7, NumPy 2.4.6, 2026-11-30.
4.6.8 Computational Complexity of PCA
The PCA algorithm involves several steps, each with its own computational cost. For a data matrix \(\mathbf{X} \in \mathbb{R}^{n \times p}\):
- Centering the data: Requires computing the mean of each of the \(p\) columns (\(O(np)\)) and then subtracting it (\(O(np)\)). Total: \(O(np)\).
- Forming the scatter matrix \(\mathbf{S} = \mathbf{X}_c^\top\mathbf{X}_c\): This is a matrix multiplication of a \(p \times n\) matrix with an \(n \times p\) matrix, resulting in a \(p \times p\) matrix. The cost is \(O(p^2n)\).
- Eigendecomposition of \(\mathbf{S}\): As we’ve seen, this costs \(O(p^3)\).
The overall complexity is therefore \(O(p^2n + p^3)\).
- If we have many data points but few features (\(n \gg p\)), the cost is dominated by forming the scatter matrix, \(O(p^2n)\).
- If we have many features but few data points (\(p \gg n\)), the cost is dominated by the eigendecomposition, \(O(p^3)\).
4.7 Eigendecomposition in Action: Linear Systems
Eigendecomposition provides a powerful theoretical tool for understanding the solution to a linear system \(\mathbf{A}\mathbf{x} = \mathbf{b}\) when \(\mathbf{A}\) is a symmetric matrix. More than just a formula, it offers a “change of perspective” that simplifies a complex problem into a sequence of trivial ones.
In this eigenvector basis, “undoing” the transformation (inversion) is as simple as “undoing” the scaling (taking the reciprocal of each scaling factor). The eigendecomposition formula for the inverse, \(\mathbf{A}^{-1} = \mathbf{Q}\boldsymbol{\Lambda}^{-1}\mathbf{Q}^\top\), is the recipe for this process:
- Go into the special basis (\(\mathbf{Q}^\top\)): First, project the problem from the standard coordinate system into the eigenvector coordinate system.
- Perform the simple inverse (\(\boldsymbol{\Lambda}^{-1}\)): In this new basis, invert the simple scaling operation. This is easy, as it just involves taking the reciprocal of each eigenvalue.
- Go back to the original basis (\(\mathbf{Q}\)): Convert the solution from the eigenvector basis back to the standard coordinate system.
Let’s walk through this process with a concrete example.
Example 4.17 (Solving a Linear System with EVD). Suppose we want to solve the linear system \(\mathbf{A}\mathbf{x} = \mathbf{b}\) where:
\[ \mathbf{A} = \begin{bmatrix} 5 & -3 \\ -3 & 5 \end{bmatrix} \quad \text{and} \quad \mathbf{b} = \begin{bmatrix} 6 \\ 2 \end{bmatrix} \]We will solve this by changing to the natural coordinate system of \(\mathbf{A}\).
First, find the eigendecomposition of \(\mathbf{A}\); this is a step that we have not discussed in detail, but we will use optimization in the next chapter to show a way to do that: The matrix is symmetric. Its eigenvalues and orthonormal eigenvectors are:
\[ \lambda_1 = 8 \quad \text{with} \quad \mathbf{q}_1 = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 \\ -1 \end{bmatrix} \] \[ \lambda_2 = 2 \quad \text{with} \quad \mathbf{q}_2 = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 \\ 1 \end{bmatrix} \]This gives us the matrices for the decomposition \(\mathbf{A} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top\):
\[ \mathbf{Q} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ -1 & 1 \end{bmatrix}, \quad \boldsymbol{\Lambda} = \begin{bmatrix} 8 & 0 \\ 0 & 2 \end{bmatrix} \]Now, we solve for \(\mathbf{x}\) using the three-step process:
Step 1. We compute the coordinates of \(\mathbf{b}\) in the new system defined by \(\mathbf{q}_1\) and \(\mathbf{q}_2\). This is done by multiplying with \(\mathbf{Q}^\top\).
This vector \(\mathbf{c}\) tells us that to form \(\mathbf{b}\), we need to take \(2\sqrt{2}\) units of \(\mathbf{q}_1\) and \(4\sqrt{2}\) units of \(\mathbf{q}_2\).
Step 2. In the eigenvector basis, the equation is effectively \(\boldsymbol{\Lambda}\mathbf{c}_x = \mathbf{c}\), where \(\mathbf{c}_x = \mathbf{Q}^\top\mathbf{x}\) are the unknown coordinates of \(\mathbf{x}\). The solution is simply \(\mathbf{c}_x = \boldsymbol{\Lambda}^{-1}\mathbf{c}\). The inverse of the diagonal matrix \(\boldsymbol{\Lambda}\) is trivial:
Now we solve for the coordinates of our solution:
\[ \mathbf{c}_x = \boldsymbol{\Lambda}^{-1}\mathbf{c} = \begin{bmatrix} 1/8 & 0 \\ 0 & 1/2 \end{bmatrix} \begin{bmatrix} 2\sqrt{2} \\ 4\sqrt{2} \end{bmatrix} = \begin{bmatrix} \frac{\sqrt{2}}{4} \\ 2\sqrt{2} \end{bmatrix} \]
Step 3. We now have the solution in the eigenvector basis. To get the final answer \(\mathbf{x}\), we convert these coordinates back to the standard basis using the matrix \(\mathbf{Q}\).
Let’s check our solution:
\[ \mathbf{A}\mathbf{x} = \begin{bmatrix} 5 & -3 \\ -3 & 5 \end{bmatrix} \begin{bmatrix} 2.25 \\ 1.75 \end{bmatrix} = \begin{bmatrix} 11.25 - 5.25 \\ -6.75 + 8.75 \end{bmatrix} = \begin{bmatrix} 6 \\ 2 \end{bmatrix} = \mathbf{b} \]The solution is correct. This example demonstrates how eigendecomposition transforms a coupled linear system into a decoupled one that can be solved element by element, providing both a practical solution method and a deep geometric understanding of matrix inversion.
4.7.1 The Moore-Penrose Pseudoinverse: A General Solution
We’ve seen that non-square or singular matrices do not have a true inverse. However, we often still need a way to find a “best” solution to \(\mathbf{A}\mathbf{x} = \mathbf{b}\). The Moore-Penrose Pseudoinverse, denoted \(\mathbf{A}^\dagger\), provides this generalized capability.
Definition 4.7 (Moore-Penrose Pseudoinverse). For any matrix \(\mathbf{A} \in \mathbb{R}^{m \times p}\), its pseudoinverse \(\mathbf{A}^\dagger \in \mathbb{R}^{p \times m}\) is a unique matrix that provides a “best-fit” solution to \(\mathbf{A}\mathbf{x} = \mathbf{b}\) in the form \(\mathbf{x}^\star = \mathbf{A}^\dagger\mathbf{b}\). When \(\mathbf{A}\) has full column rank, \(\mathbf{A}^\dagger = (\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top\). When \(\mathbf{A}\) does not have full column rank (dependent columns, or more columns than rows), this formula breaks down and the general definition requires the Singular Value Decomposition (Chapter 5, § From Eigendecomposition to the Singular Value Decomposition).
The pseudoinverse provides a powerful theoretical tool that unifies the concept of solving linear systems, handling all cases with a single operator. It is often computed via the Singular Value Decomposition (SVD), the topic that opens the next chapter (Chapter 5, § From Eigendecomposition to the Singular Value Decomposition).
Exercises
Below is a set of exercises related to the content of Chapter 4. The exercises are partitioned by difficulty. Programming questions should be completed in Python using the NumPy library.
EASY EXERCISES
- E1.2x2 Matrix Inverse. Using the closed-form formula, compute the inverse of the matrix \(\mathbf{A} = \begin{bmatrix} 3 & 1 \\ 4 & 2 \end{bmatrix}\). Verify your result by computing \(\mathbf{A}\mathbf{A}^{-1}\).
- E2.Identifying Invertibility. For each of the following matrices, determine whether it is invertible or singular without computing the inverse. Explain your reasoning. \[ \mathbf{M}_1 = \begin{bmatrix} 1 & 2 & 3 \\ 0 & 4 & 5 \\ 0 & 0 & 6 \end{bmatrix}, \,\, \mathbf{M}_2 = \begin{bmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}, \,\, \mathbf{M}_3 = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}. \]
- E3.Eigenvalue-Eigenvector Verification. Verify that \(\mathbf{v} = \begin{bmatrix} 1 \\ -2 \end{bmatrix}\) is an eigenvector of the matrix \(\mathbf{A} = \begin{bmatrix} 3 & 1 \\ 2 & 2 \end{bmatrix}\) and find the corresponding eigenvalue.
- E4.Properties of Orthogonal Matrices. Given that \(\mathbf{Q}\) is an orthogonal matrix, what is its inverse, \(\mathbf{Q}^{-1}\)? If \(\mathbf{x} = \begin{bmatrix} 3 \\ 4 \end{bmatrix}\), what is the \(\ell_2\)-norm of the vector \(\mathbf{y} = \mathbf{Qx}\)?
- E5.Rank of a Matrix. What is the rank of the matrix \(\mathbf{A} = \begin{bmatrix} 1 & 2 & 3 \\ 2 & 4 & 6 \end{bmatrix}\)? Explain your reasoning.
- E6.Geometric Interpretation of Eigenvalues. A 2x2 symmetric matrix \(\mathbf{A}\) has eigenvalues \(\lambda_1 = 3\) and \(\lambda_2 = 0.5\). Describe geometrically what this transformation does to the unit circle in \(\mathbb{R}^2\).
- E7.Underdetermined System. Write down a simple \(1 \times 2\) linear system of equations that has infinite solutions. Provide two distinct solutions.
- E8.Overdetermined System. Write down a simple \(3 \times 1\) linear system of equations that has no solution.
- E9.Python: Inverse and Eigenvalues. Using NumPy, create the matrix \(\mathbf{A} = \begin{bmatrix} 5 & 1 \\ 1 & 3 \end{bmatrix}\). Compute its:
- a)inverse using
numpy.linalg.inv(). - b)eigenvalues and eigenvectors using
numpy.linalg.eig().
- a)
- E10.Determinant and Invertibility. A matrix has eigenvalues \(\lambda_1 = 4, \lambda_2 = -2, \lambda_3 = 0\). What is its determinant? Is the matrix invertible?
MEDIUM EXERCISES
- M1.Finding Eigenvalues and Eigenvectors. Find all eigenvalues and the corresponding normalized eigenvectors for the matrix \(\mathbf{A} = \begin{bmatrix} 2 & -1 \\ -1 & 2 \end{bmatrix}\).
- M2.Properties of Inverses. Prove that for any two invertible \(p \times p\) matrices \(\mathbf{A}\) and \(\mathbf{B}\), the following property holds: \((\mathbf{A}\mathbf{B})^{-1} = \mathbf{B}^{-1}\mathbf{A}^{-1}\).
- M3.Eigenvalues of the Inverse. If an invertible matrix \(\mathbf{A}\) has an eigenvalue \(\lambda\) with corresponding eigenvector \(\mathbf{v}\), show that its inverse \(\mathbf{A}^{-1}\) has an eigenvalue of \(1/\lambda\) with the same eigenvector \(\mathbf{v}\).
- M4.Spectral Decomposition. For the matrix \(\mathbf{A}\) from exercise M1, write its spectral decomposition as the weighted sum of outer products, \(\mathbf{A} = \sum \lambda_i \mathbf{q}_i\mathbf{q}_i^\top\). Verify that the sum correctly reconstructs the original matrix \(\mathbf{A}\).
- M5.Orthogonality Check. Is the matrix \(\mathbf{Q} = \begin{bmatrix} \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \end{bmatrix}\) orthogonal? Justify your answer by computing \(\mathbf{Q}^\top\mathbf{Q}\).
- M6.Solving a System with EVD. Using the eigendecomposition of the matrix \(\mathbf{A} = \begin{bmatrix} 2 & -1 \\ -1 & 2 \end{bmatrix}\) (from exercise M1), solve the linear system \(\mathbf{A}\mathbf{x} = \mathbf{b}\) for \(\mathbf{b} = \begin{bmatrix} 1 \\ 5 \end{bmatrix}\). Follow the three-step process described in the chapter.
- M7.PCA: Scatter Matrix. Given the following centered data matrix \(\mathbf{X}_c\): \[ \mathbf{X}_c = \begin{bmatrix} 1 & -1 \\ -2 & 2 \\ 1 & -1 \end{bmatrix} \]
- a)Compute the scatter matrix \(\mathbf{S} = \mathbf{X}_c^\top\mathbf{X}_c\).
- b)Without computing the eigenvalues, what can you say about the rank of \(\mathbf{S}\)?
- a)
- M8.Condition Number and Eigenvalues. A symmetric matrix \(\mathbf{A}\) has eigenvalues \(\lambda_1=1000\) and \(\lambda_2=0.01\). Another symmetric matrix \(\mathbf{B}\) has eigenvalues \(\lambda_1=5\) and \(\lambda_2=4\). Which matrix is better conditioned? Explain your reasoning.
- M9.Python: Verifying Orthogonality. Write a Python function
is_orthogonal(matrix, tol=1e-8)that takes a square NumPy array and returnsTrueif it is an orthogonal matrix andFalseotherwise. Use a small tolerancetolfor floating-point comparisons.
HARD EXERCISES
- H1.Proof of Orthogonal Eigenvectors. Prove that for any real symmetric matrix \(\mathbf{A}\), eigenvectors corresponding to distinct eigenvalues are orthogonal.
- H2.Rank of the Scatter Matrix. Prove that for any centered data matrix \(\mathbf{X}_c \in \mathbb{R}^{n \times p}\), the rank of the scatter matrix \(\mathbf{S} = \mathbf{X}_c^\top\mathbf{X}_c\) is the same as the rank of \(\mathbf{X}_c\). (Hint: prove that \(\mathbf{X}_c\mathbf{v} = \mathbf{0}\) if and only if \(\mathbf{X}_c^\top\mathbf{X}_c\mathbf{v} = \mathbf{0}\)).
- H3.Eigenvalues of Projection Matrices. Let \(\mathbf{q}\) be a unit vector in \(\mathbb{R}^p\). The matrix \(\mathbf{P} = \mathbf{q}\mathbf{q}^\top\) is the orthogonal projection matrix onto the line spanned by \(\mathbf{q}\).
- a)Show that \(\mathbf{q}\) is an eigenvector of \(\mathbf{P}\). What is its eigenvalue?
- b)Now consider any non-zero vector \(\mathbf{v}\) that is orthogonal to \(\mathbf{q}\). Show that \(\mathbf{v}\) is also an eigenvector of \(\mathbf{P}\). What is its eigenvalue?
- c)Based on this, what are the \(p\) eigenvalues of the matrix \(\mathbf{P}\)?
- a)
- H4.Python: PCA Implementation. Write a Python function
pca(X, k)that implements the Principal Component Analysis algorithm. The function should:- a)Take a data matrix \(\mathbf{X}\) (rows are data points) and a target dimension
kas input. - b)Center the data.
- c)Compute the scatter matrix.
- d)Compute the eigendecomposition of the scatter matrix.
- e)Sort the eigenvalues and eigenvectors.
- f)Select the top
keigenvectors. - g)Project the centered data onto these
keigenvectors. - h)Return the projected data, the top
keigenvectors, and the corresponding eigenvalues.
- a)
- H5.Invariance of the Trace. The trace of a matrix is the sum of its diagonal elements, \(\text{Tr}(\mathbf{A}) = \sum_i A_{ii}\). Show that the trace of a diagonalizable matrix is also equal to the sum of its eigenvalues. (Hint: Use the property that the trace is invariant under cyclic permutations, \(\text{Tr}(\mathbf{A}\mathbf{B}) = \text{Tr}(\mathbf{B}\mathbf{A})\), and the eigendecomposition \(\mathbf{A} = \mathbf{V}\boldsymbol{\Lambda}\mathbf{V}^{-1}\)).