Asiman Data School

Module 2, topic 15, week 8, Tuesday

Eigenvalues and eigenvectors; vector norms and their role in machine learning

The vectors that a matrix does not turn: eigenvalues, eigenvectors, diagonalization and symmetric matrices, with PCA and PageRank as uses. Then the L1, L2 and max norm and what they do in machine learning. Worked by hand.

Introduction: the last lesson on linear algebra

The third lesson of Module 2, and the last one on linear algebra. The last lesson showed that a matrix moves vectors: it stretches, turns and squeezes the plane. This lesson starts with a simple question that has deep consequences: are there vectors that a matrix does not turn? Such vectors are called eigenvectors, and the factors by which they are stretched are the eigenvalues. They show what a matrix really does, and they are behind Principal Component Analysis, Google's PageRank and many other methods of data science. The second subject of the lesson is the norm of a vector: the different ways to measure how large a vector is. The length of Topic 13 is one norm. Machine learning uses two more of them every day: to measure the error of a model, to decide which data points are neighbours and to keep the weights of a model small.

This guide first builds the idea of an eigenvector with a picture. Then it teaches how to find eigenvalues and eigenvectors by hand, which special cases can happen, the rules worth knowing, how a matrix is taken apart into its eigenvectors (diagonalization), and why symmetric matrices are especially friendly. After the uses of eigenvectors come the norms: the three norms of machine learning, their rules, the distances they give, and their role in machine learning. Every idea comes as a definition and worked examples calculated by hand. Then the guide walks through 26 solved problems from the easiest to the hardest, and at the end there are 40 practice problems with a hint and a full solution for each.

You need the last two lessons: the dot product, the length of a vector and perpendicular vectors (Topic 13), and a matrix times a vector, the determinant, the inverse and systems of equations (Topic 14). From school you need quadratic equations. As in the whole module, the work is done by hand; Python appears only in one short part near the end.

What you will learn

  1. 1.What an eigenvector and an eigenvalue are, and what the eigenvalue says about the vector
  2. 2.How to check an eigenvector, and why an eigenvector is a direction, not one single vector
  3. 3.Finding eigenvalues with the characteristic equation, the trace and the determinant
  4. 4.Finding the eigenvectors that belong to an eigenvalue
  5. 5.The special cases: an eigenvalue 0, a repeated eigenvalue, no real eigenvalues, a 3 x 3 matrix
  6. 6.The rules for powers, inverses and multiples of a matrix, and the dominant eigenvalue
  7. 7.Diagonalization: a matrix written with its eigenvectors and eigenvalues, and powers of a matrix
  8. 8.Symmetric matrices: perpendicular eigenvectors, orthogonal matrices, positive definite matrices
  9. 9.Where eigenvectors are used: Principal Component Analysis, and the long-run state of a process (PageRank)
  10. 10.The L1, L2 and max norm of a vector, the rules of a norm, and the distances that come from norms
  11. 11.Norms in machine learning: the error of a model, regularization (Ridge and Lasso), neighbours, and a first look at norms of matrices and singular values
Notation used in this lesson
WrittenMeaning
Av=λvA\vv = \lambda \vvthe eigenvalue equation
λ\lambdaan eigenvalue (the Greek letter lambda)
v\vvan eigenvector
tr⁡(A)\tr(A)the trace of AA: the sum of the entries on its main diagonal
det⁡(A−λI)=0\det(A - \lambda I) = 0the characteristic equation
PP, DDthe matrix of the eigenvectors, and the diagonal matrix of the eigenvalues
QQan orthogonal matrix
∥x∥1\norm{\vx}_1, ∥x∥2\norm{\vx}_2, ∥x∥∞\norm{\vx}_\inftythe L1 norm, the L2 norm and the max norm of the vector x\vx
∥A∥F\norm{A}_Fthe Frobenius norm of the matrix AA
∣c∣\len{c}the absolute value of the number cc

1The idea of an eigenvector

1.1Most vectors are turned, a few are not

In the last lesson the matrix [2112]\mat{2 & 1 \\ 1 & 2} turned the unit square into a parallelogram. Look at what it does to three single vectors:

A=[2112]A = \mat{2 & 1 \\ 1 & 2}
A[10]=[21]A[11]=[33]=3[11]A[1−1]=[1−1]\begin{aligned} A \mat{1 \\ 0} &= \mat{2 \\ 1} \\[6pt] A \mat{1 \\ 1} &= \mat{3 \\ 3} = 3 \mat{1 \\ 1} \\[6pt] A \mat{1 \\ -1} &= \mat{1 \\ -1} \end{aligned}
  • (1,0)(1, 0) becomes (2,1)(2, 1). The vector points in a new direction: it was turned.
  • (1,1)(1, 1) becomes (3,3)(3, 3), which is 3 (1,1)3\,(1, 1). The direction is the same; the vector is 3 times as long.
  • (1,−1)(1, -1) becomes (1,−1)(1, -1). It is not changed at all.
-2-11234-2-11234A vvw = A wA uu
Three vectors and what the matrix A does to them. On the two dotted lines a vector keeps its direction: v = (1, 1) becomes A v = (3, 3), three times as long, and w = (1, −1) stays as it is. The vector u = (1, 0) is turned: A u = (2, 1).

Most vectors behave like (1,0)(1, 0): the matrix turns them. But on two lines through the origin (the dotted lines of the picture) the vectors keep their direction. These are the special vectors of this matrix.

1.2The definition

A vector v\vv that is not the zero vector is an eigenvector of the square matrix AA if

Av=λvA\vv = \lambda \vv

for some number λ\lambda. The number λ\lambda is the eigenvalue that belongs to v\vv. (The word comes from German: "eigen" means "own". The eigenvectors are the matrix's own directions.)

For the matrix above:

  • (1,1)(1, 1) is an eigenvector with the eigenvalue 3, because A (1,1)=3 (1,1)A\,(1, 1) = 3\,(1, 1).
  • (1,−1)(1, -1) is an eigenvector with the eigenvalue 1, because A (1,−1)=1 (1,−1)A\,(1, -1) = 1\,(1, -1).
  • (1,0)(1, 0) is not an eigenvector: (2,1)(2, 1) is not a multiple of (1,0)(1, 0).

Read the equation once more. On the left a matrix multiplies the vector, on the right only a number does. For an eigenvector, the whole matrix acts like one single number.

1.3What the eigenvalue says

The eigenvalue and what happens to the eigenvector
EigenvalueWhat happens to the eigenvector
λ\lambda larger than 1it is stretched
λ=1\lambda = 1it stays as it is
λ\lambda between 0 and 1it shrinks
λ=0\lambda = 0it is sent to the zero vector
λ\lambda negativeit is flipped to the opposite direction (and stretched or shrunk)

A flipped vector still lies on the same line through the origin. So the exact statement is: an eigenvector stays on its own line.

Choose a matrix and a vector v. Does A v stay on the dotted line of v?

-5-4-3-2-112345-5-4-3-2-112345
A =

v = (1, 0)
A v = (2 × 1 + 1 × 0, 1 × 1 + 2 × 0) = (2, 1)

A v is not on the line of v: the vector was turned. It is not an eigenvector.

Try all eight vectors for every matrix. How many lines of eigenvectors does each matrix have?

1.4Why this matters

A matrix is a complicated object: a 2 x 2 matrix has 4 numbers, a 1000 x 1000 matrix has a million. Along its eigenvectors it is simple: it only multiplies by a number. Knowing the eigenvectors and the eigenvalues means knowing the simplest description of the matrix. Many methods of data science are built on this:

Where eigenvectors and eigenvalues are used
WhereWhat the eigenvectors and eigenvalues give
Principal Component Analysis (Topic 44)the directions in which the data varies most, and how much it varies along each
Google's PageRankthe importance of every web page: an eigenvector with the eigenvalue 1
A step that is repeated many timesthe largest eigenvalue decides what happens in the long run
Clustering with graphs (spectral clustering)groups of points, found from the eigenvectors of a matrix of similarities
Training neural networkshow steep and how flat the loss is in different directions

1.5Only square matrices

AvA\vv and λv\lambda \vv must have the same number of components, so AA must be square. All matrices in the first nine parts of this lesson are square.

Try it yourself

For A=[2112]A = \mat{2 & 1 \\ 1 & 2} calculate A (2,2)A\,(2, 2) and A (2,0)A\,(2, 0). Which of the two vectors is an eigenvector, and what is its eigenvalue?

Show a hint
One dot product for every row of the matrix. A vector is an eigenvector when the result is a multiple of the vector itself.
Show the solution

Answer: A (2,2)=(6,6)A\,(2, 2) = (6, 6) and A (2,0)=(4,2)A\,(2, 0) = (4, 2). The vector (2,2)(2, 2) is an eigenvector with the eigenvalue 3.

A[22]=[4+22+4]=[66]=3[22]A[20]=[4+02+0]=[42]\begin{aligned} A \mat{2 \\ 2} &= \mat{4 + 2 \\ 2 + 4} = \mat{6 \\ 6} = 3 \mat{2 \\ 2} \\[6pt] A \mat{2 \\ 0} &= \mat{4 + 0 \\ 2 + 0} = \mat{4 \\ 2} \end{aligned}

(6,6)(6, 6) is 3 times (2,2)(2, 2): the vector kept its direction. (4,2)(4, 2) is not a multiple of (2,0)(2, 0). Notice that (2,2)(2, 2) lies on the same line as (1,1)(1, 1) and has the same eigenvalue.

2Checking an eigenvector, and the first facts

2.1How to check

To test whether a vector is an eigenvector, multiply and compare: AvA\vv must be a multiple of v\vv.

B=[4123]B = \mat{4 & 1 \\ 2 & 3}
B[11]=[55]=5[11]B[12]=[68]B[1−2]=[2−4]=2[1−2]\begin{aligned} B \mat{1 \\ 1} &= \mat{5 \\ 5} = 5 \mat{1 \\ 1} \\[6pt] B \mat{1 \\ 2} &= \mat{6 \\ 8} \\[6pt] B \mat{1 \\ -2} &= \mat{2 \\ -4} = 2 \mat{1 \\ -2} \end{aligned}

(1,1)(1, 1) is an eigenvector with λ=5\lambda = 5, and (1,−2)(1, -2) is an eigenvector with λ=2\lambda = 2. The vector (1,2)(1, 2) is not an eigenvector: 6=6×16 = 6 \times 1 but 8=4×28 = 4 \times 2.

Every component of AvA\vv must be the same multiple of the matching component of v\vv. In the second test the first component was multiplied by 6 and the second by 4, so the vector was turned.

2.2An eigenvector is a direction

If v\vv is an eigenvector, then every multiple c vc\,\vv (with cc not 0) is an eigenvector too, with the same eigenvalue:

A (c v)=c (Av)=c (λv)=λ (c v)A\,(c\,\vv) = c\,(A\vv) = c\,(\lambda \vv) = \lambda\,(c\,\vv)

For the matrix A=[2112]A = \mat{2 & 1 \\ 1 & 2} the vectors (1,1)(1, 1), (2,2)(2, 2), (−1,−1)(-1, -1) and (0.5,0.5)(0.5, 0.5) are all eigenvectors with the eigenvalue 3. So "the eigenvector" is really a whole line through the origin, and we are free to choose one vector on it. Usually we take the one with small whole numbers, or the one with the length 1.

2.3The zero vector does not count, the eigenvalue zero does

A 0=λ 0A\,\vzero = \lambda\,\vzero is true for every matrix and every number λ\lambda, so it tells nothing. That is why the zero vector is excluded in the definition. But the number 0 is a perfectly good eigenvalue: it means Av=0A\vv = \vzero for a vector v\vv that is not zero (Part 5).

2.4Matrices whose eigenvalues can be read off

For a diagonal matrix the eigenvalues are the entries on the diagonal, and the eigenvectors are the standard unit vectors:

[300−2][10]=[30]=3[10][300−2][01]=[0−2]=−2[01]\begin{aligned} \mat{3 & 0 \\ 0 & -2} \mat{1 \\ 0} &= \mat{3 \\ 0} = 3 \mat{1 \\ 0} \\[6pt] \mat{3 & 0 \\ 0 & -2} \mat{0 \\ 1} &= \mat{0 \\ -2} = -2 \mat{0 \\ 1} \end{aligned}

For a triangular matrix the eigenvalues are also the entries on the diagonal (Part 3 shows why). Its eigenvectors have to be calculated.

Try it yourself

Test the vectors (1,1)(1, 1), (1,2)(1, 2) and (3,−2)(3, -2) for the matrix A=[1322]A = \mat{1 & 3 \\ 2 & 2}. Give the eigenvalue of each vector that is an eigenvector.

Show a hint
Multiply AA by each vector. Is the result the same multiple in both components?
Show the solution

Answer: (1,1)(1, 1) is an eigenvector with the eigenvalue 4, (3,−2)(3, -2) is an eigenvector with the eigenvalue −1-1, and (1,2)(1, 2) is not an eigenvector.

A[11]=[1+32+2]=[44]=4[11]A[12]=[1+62+4]=[76]A[3−2]=[3−66−4]=[−32]=−1[3−2]\begin{aligned} A \mat{1 \\ 1} &= \mat{1 + 3 \\ 2 + 2} = \mat{4 \\ 4} = 4 \mat{1 \\ 1} \\[6pt] A \mat{1 \\ 2} &= \mat{1 + 6 \\ 2 + 4} = \mat{7 \\ 6} \\[6pt] A \mat{3 \\ -2} &= \mat{3 - 6 \\ 6 - 4} = \mat{-3 \\ 2} = -1 \mat{3 \\ -2} \end{aligned}

(7,6)(7, 6) is not a multiple of (1,2)(1, 2). The eigenvalue −1-1 flips (3,−2)(3, -2) to the opposite vector.

3Finding the eigenvalues

3.1From the definition to an equation

Bring everything in Av=λvA\vv = \lambda \vv to one side:

Av−λv=0(A−λI) v=0\begin{aligned} A\vv - \lambda \vv &= \vzero \\ (A - \lambda I)\,\vv &= \vzero \end{aligned}

The identity matrix II is needed here: λv\lambda \vv is the same as λIv\lambda I \vv, and a number cannot be subtracted from a matrix. A−λIA - \lambda I is the matrix AA with λ\lambda subtracted from every entry of the main diagonal.

A − λ I =
Subtracting λ I changes only the main diagonal (filled). The other entries stay as they are.

(A−λI) v=0(A - \lambda I)\,\vv = \vzero is a system of equations with zeros on the right side. The zero vector always solves it, but we are looking for another solution. From the last lesson: a system has exactly one solution when the determinant of its matrix is not 0. We want more than one solution, so the determinant must be 0:

det⁡(A−λI)=0\det(A - \lambda I) = 0

This is the characteristic equation of AA. Its solutions are the eigenvalues.

3.2The characteristic equation of a 2 x 2 matrix

A=[abcd]A−λI=[a−λbcd−λ]A = \mat{a & b \\ c & d} \qquad A - \lambda I = \mat{a - \lambda & b \\ c & d - \lambda}
det⁡(A−λI)=(a−λ)(d−λ)−bc=λ2−(a+d) λ+(ad−bc)\begin{aligned} \det(A - \lambda I) &= (a - \lambda)(d - \lambda) - bc \\ &= \lambda^2 - (a + d)\,\lambda + (ad - bc) \end{aligned}

The number a+da + d, the sum of the main diagonal, is called the trace of AA, written tr⁡(A)\tr(A). The number ad−bcad - bc is the determinant. So for every 2 x 2 matrix:

λ2−tr⁡(A) λ+det⁡(A)=0\lambda^2 - \tr(A)\,\lambda + \det(A) = 0

3.3Example 1

A=[2112]A = \mat{2 & 1 \\ 1 & 2}
tr⁡(A)=2+2=4det⁡(A)=2×2−1×1=3λ2−4λ+3=0(λ−1)(λ−3)=0λ=1orλ=3\begin{aligned} \tr(A) &= 2 + 2 = 4 \\ \det(A) &= 2 \times 2 - 1 \times 1 = 3 \\[6pt] \lambda^2 - 4\lambda + 3 &= 0 \\ (\lambda - 1)(\lambda - 3) &= 0 \\ \lambda = 1 \quad &\text{or} \quad \lambda = 3 \end{aligned}

These are the two eigenvalues that Part 1 found by trying vectors.

3.4Solving the quadratic equation

Two ways, both from school:

  • Factoring. Look for two numbers whose sum is the trace and whose product is the determinant. For the sum 4 and the product 3 they are 1 and 3.
  • The formula.
λ=tr⁡(A)±tr⁡(A)2−4det⁡(A)2\lambda = \frac{\tr(A) \pm \sqrt{\tr(A)^2 - 4\det(A)}}{2}

3.5Example 2

B=[4123]B = \mat{4 & 1 \\ 2 & 3}
tr⁡(B)=7det⁡(B)=4×3−1×2=10λ2−7λ+10=0λ=7±49−402=7±32λ=5orλ=2\begin{aligned} \tr(B) &= 7 \\ \det(B) &= 4 \times 3 - 1 \times 2 = 10 \\[6pt] \lambda^2 - 7\lambda + 10 &= 0 \\[4pt] \lambda &= \frac{7 \pm \sqrt{49 - 40}}{2} = \frac{7 \pm 3}{2} \\[4pt] \lambda = 5 \quad &\text{or} \quad \lambda = 2 \end{aligned}

3.6Two checks that cost nothing

For every square matrix:

sum of the eigenvalues=traceproduct of the eigenvalues=determinant\begin{aligned} \text{sum of the eigenvalues} &= \text{trace} \\ \text{product of the eigenvalues} &= \text{determinant} \end{aligned}

For AA: 1+3=41 + 3 = 4 and 1×3=31 \times 3 = 3. For BB: 5+2=75 + 2 = 7 and 5×2=105 \times 2 = 10. Use these two checks after every calculation of eigenvalues.

The second check has an important consequence. The determinant is 0 exactly when one of the eigenvalues is 0. So one more statement joins the list of the last lesson (rank smaller than nn, determinant 0, no inverse): a matrix is singular exactly when 0 is one of its eigenvalues.

3.7Triangular matrices

U=[570−1]U = \mat{5 & 7 \\ 0 & -1}
det⁡(U−λI)=(5−λ)(−1−λ)−7×0=(5−λ)(−1−λ)\begin{aligned} \det(U - \lambda I) &= (5 - \lambda)(-1 - \lambda) - 7 \times 0 \\ &= (5 - \lambda)(-1 - \lambda) \end{aligned}

The product is 0 for λ=5\lambda = 5 and for λ=−1\lambda = -1: the entries of the diagonal. The zero below the diagonal removes the second part of the determinant. The same happens for triangular matrices of every size.

3.8Larger matrices

For an n×nn \times n matrix the characteristic equation has the degree nn, so there are nn eigenvalues (some may be repeated, and some may not be real numbers, see Part 5). By hand this is practical for 2 x 2 matrices and for 3 x 3 matrices with some zeros. For larger matrices a computer finds the eigenvalues.

Move the sliders: the characteristic equation and the eigenvalues of your matrix.

A =

tr(A) = 2 + 2 = 4
det(A) = 2 × 2 − 1 × 1 = 3

λ² − 4λ + 3 = 0

tr(A)² − 4 det(A) = 16 − 12 = 4

It is positive: two different eigenvalues, λ = 3 and λ = 1.

Check: the sum 3 + 1 is the trace, and the product is the determinant.

Try it yourself

Find the trace, the determinant and the eigenvalues of A=[3241]A = \mat{3 & 2 \\ 4 & 1}.

Show a hint
Write λ2−tr⁡(A) λ+det⁡(A)=0\lambda^2 - \tr(A)\,\lambda + \det(A) = 0 and factor it: two numbers with the sum 4 and the product −5-5.
Show the solution

Answer: tr⁡(A)=4\tr(A) = 4, det⁡(A)=−5\det(A) = -5, and the eigenvalues are 5 and −1-1.

tr⁡(A)=3+1=4det⁡(A)=3×1−2×4=−5λ2−4λ−5=0(λ−5)(λ+1)=0λ=5orλ=−1\begin{aligned} \tr(A) &= 3 + 1 = 4 \\ \det(A) &= 3 \times 1 - 2 \times 4 = -5 \\[6pt] \lambda^2 - 4\lambda - 5 &= 0 \\ (\lambda - 5)(\lambda + 1) &= 0 \\ \lambda = 5 \quad &\text{or} \quad \lambda = -1 \end{aligned}

The checks: 5+(−1)=45 + (-1) = 4 and 5×(−1)=−55 \times (-1) = -5.

4Finding the eigenvectors

4.1One small system for every eigenvalue

Put an eigenvalue into (A−λI) v=0(A - \lambda I)\,\vv = \vzero and solve the system. Because A−λIA - \lambda I is singular, its two rows are multiples of each other: both equations say the same thing. One equation is left for two unknowns, so one unknown can be chosen freely. That is exactly the freedom of Part 2: an eigenvector is a whole line.

4.2Example 1

A=[2112]A = \mat{2 & 1 \\ 1 & 2} has the eigenvalues 3 and 1.

λ=3:A−3I=[−111−1]−x+y=0andx−y=0y=x,choose x=1:v=(1,1)\begin{aligned} \lambda = 3: \quad & A - 3I = \mat{-1 & 1 \\ 1 & -1} \\[4pt] & -x + y = 0 \quad \text{and} \quad x - y = 0 \\ & y = x, \quad \text{choose } x = 1: \quad \vv = (1, 1) \end{aligned}

The two equations are the same equation. For the second eigenvalue:

λ=1:A−I=[1111]x+y=0y=−x,choose x=1:v=(1,−1)\begin{aligned} \lambda = 1: \quad & A - I = \mat{1 & 1 \\ 1 & 1} \\[4pt] & x + y = 0 \\ & y = -x, \quad \text{choose } x = 1: \quad \vv = (1, -1) \end{aligned}

4.3Example 2

B=[4123]B = \mat{4 & 1 \\ 2 & 3} has the eigenvalues 5 and 2.

λ=5:B−5I=[−112−2]−x+y=0  ⇒  y=x,v=(1,1)λ=2:B−2I=[2121]2x+y=0  ⇒  y=−2x,v=(1,−2)\begin{aligned} \lambda = 5: \quad & B - 5I = \mat{-1 & 1 \\ 2 & -2} \\[4pt] & -x + y = 0 \;\Rightarrow\; y = x, \quad \vv = (1, 1) \\[8pt] \lambda = 2: \quad & B - 2I = \mat{2 & 1 \\ 2 & 1} \\[4pt] & 2x + y = 0 \;\Rightarrow\; y = -2x, \quad \vv = (1, -2) \end{aligned}

The check, for the second one: B (1,−2)=(4−2,  2−6)=(2,−4)=2 (1,−2)B\,(1, -2) = (4 - 2,\; 2 - 6) = (2, -4) = 2\,(1, -2).

4.4The recipe

  1. Calculate the trace and the determinant, and write the characteristic equation λ2−tr⁡(A) λ+det⁡(A)=0\lambda^2 - \tr(A)\,\lambda + \det(A) = 0.
  2. Solve it. Check: the sum of the eigenvalues is the trace, their product is the determinant.
  3. For every eigenvalue write the matrix A−λIA - \lambda I, take one of its rows as an equation and choose a simple solution.
  4. Check every eigenvector: AvA\vv must be λv\lambda \vv.

A warning for step 3: the two rows of A−λIA - \lambda I must be multiples of each other. If they are not, the only solution is the zero vector, and that means the eigenvalue is wrong. Go back to step 2.

All the eigenvectors of one eigenvalue, together with the zero vector, form the eigenspace of that eigenvalue. In the two examples every eigenspace is a line through the origin.

Try it yourself

The matrix A=[3241]A = \mat{3 & 2 \\ 4 & 1} has the eigenvalues 5 and −1-1. Find an eigenvector for each of them.

Show a hint
Subtract the eigenvalue from the main diagonal and read one row as an equation. Subtracting −1-1 means adding 1.
Show the solution

Answer: (1,1)(1, 1) for the eigenvalue 5, and (1,−2)(1, -2) for the eigenvalue −1-1 (or any multiples of them).

λ=5:A−5I=[−224−4]−2x+2y=0  ⇒  y=xv=(1,1)λ=−1:A+I=[4242]4x+2y=0  ⇒  y=−2xv=(1,−2)\begin{aligned} \lambda = 5: \quad & A - 5I = \mat{-2 & 2 \\ 4 & -4} \\[4pt] & -2x + 2y = 0 \;\Rightarrow\; y = x \\ & \vv = (1, 1) \\[8pt] \lambda = -1: \quad & A + I = \mat{4 & 2 \\ 4 & 2} \\[4pt] & 4x + 2y = 0 \;\Rightarrow\; y = -2x \\ & \vv = (1, -2) \end{aligned}

The checks: A (1,1)=(5,5)A\,(1, 1) = (5, 5) and A (1,−2)=(3−4,  4−2)=(−1,2)=−1 (1,−2)A\,(1, -2) = (3 - 4,\; 4 - 2) = (-1, 2) = -1\,(1, -2).

5Special cases

5.1An eigenvalue 0

C=[1224]C = \mat{1 & 2 \\ 2 & 4}
tr⁡(C)=5,det⁡(C)=4−4=0λ2−5λ=0λ (λ−5)=0λ=0orλ=5\begin{aligned} \tr(C) &= 5, \quad \det(C) = 4 - 4 = 0 \\[6pt] \lambda^2 - 5\lambda &= 0 \\ \lambda\,(\lambda - 5) &= 0 \\ \lambda = 0 \quad &\text{or} \quad \lambda = 5 \end{aligned}
λ=0:Cv=0:x+2y=0,v=(2,−1)λ=5:C−5I=[−422−1]−4x+2y=0  ⇒  y=2x,v=(1,2)\begin{aligned} \lambda = 0: \quad & C\vv = \vzero: \quad x + 2y = 0, \quad \vv = (2, -1) \\[8pt] \lambda = 5: \quad & C - 5I = \mat{-4 & 2 \\ 2 & -1} \\[4pt] & -4x + 2y = 0 \;\Rightarrow\; y = 2x, \quad \vv = (1, 2) \end{aligned}

The checks: C (2,−1)=(0,0)C\,(2, -1) = (0, 0) and C (1,2)=(5,10)=5 (1,2)C\,(1, 2) = (5, 10) = 5\,(1, 2).

The determinant is 0, so the matrix squeezes the plane flat onto a line (Topic 14). Now both directions have a name: the line of (1,2)(1, 2) is where everything lands, and (2,−1)(2, -1) is the direction that is squeezed to nothing.

5.2A repeated eigenvalue

The characteristic equation can have one solution twice. Then two things can happen.

[2002]:(2−λ)2=0,λ=2 twice\mat{2 & 0 \\ 0 & 2}: \qquad (2 - \lambda)^2 = 0, \quad \lambda = 2 \text{ twice}

Every vector is an eigenvector: this matrix doubles all vectors.

[2102]:(2−λ)2=0,λ=2 twiceA−2I=[0100]  ⇒  y=0\begin{aligned} \mat{2 & 1 \\ 0 & 2}: \qquad & (2 - \lambda)^2 = 0, \quad \lambda = 2 \text{ twice} \\[6pt] & A - 2I = \mat{0 & 1 \\ 0 & 0} \;\Rightarrow\; y = 0 \end{aligned}

Only the direction (1,0)(1, 0). The first matrix has eigenvectors in every direction, the second one only on a single line. A repeated eigenvalue needs this extra look.

5.3No real eigenvalues

R=[0−110]R = \mat{0 & -1 \\ 1 & 0}
tr⁡(R)=0,det⁡(R)=1λ2+1=0\begin{aligned} \tr(R) &= 0, \quad \det(R) = 1 \\ \lambda^2 + 1 &= 0 \end{aligned}

No real number solves this equation. RR is the rotation by 90 degrees of the last lesson. A rotation turns every vector, so no vector keeps its direction, and the calculation agrees with the picture. (With complex numbers the equation has solutions. They are not needed in this course: the symmetric matrices of machine learning always have real eigenvalues, see Part 8.)

The number under the root of the formula decides which case it is:

The three cases of a 2 x 2 matrix
tr⁡(A)2−4det⁡(A)\tr(A)^2 - 4\det(A)Eigenvalues
positivetwo different real eigenvalues
zeroone eigenvalue, repeated
negativeno real eigenvalues

5.4A 3 x 3 matrix

A=[200034049]A = \mat{2 & 0 & 0 \\ 0 & 3 & 4 \\ 0 & 4 & 9}

The determinant of A−λIA - \lambda I along the first row (Topic 14). The two zeros leave only one part:

det⁡(A−λI)=(2−λ)det⁡[3−λ449−λ]=(2−λ) ((3−λ)(9−λ)−16)=(2−λ) (λ2−12λ+11)=(2−λ)(λ−1)(λ−11)\begin{aligned} \det(A - \lambda I) &= (2 - \lambda) \det \mat{3 - \lambda & 4 \\ 4 & 9 - \lambda} \\[6pt] &= (2 - \lambda)\,\big((3 - \lambda)(9 - \lambda) - 16\big) \\ &= (2 - \lambda)\,(\lambda^2 - 12\lambda + 11) \\ &= (2 - \lambda)(\lambda - 1)(\lambda - 11) \end{aligned}

The eigenvalues are 2, 1 and 11. The checks: their sum is 14, and the trace is 2+3+9=142 + 3 + 9 = 14. Their product is 22, and the determinant is 2×(27−16)=222 \times (27 - 16) = 22.

The eigenvector of λ=11\lambda = 11:

A−11I=[−9000−8404−2]−9x=0⇒x=0−8y+4z=0⇒z=2yv=(0,1,2)\begin{aligned} A - 11I &= \mat{-9 & 0 & 0 \\ 0 & -8 & 4 \\ 0 & 4 & -2} \\[6pt] -9x = 0 \quad &\Rightarrow \quad x = 0 \\ -8y + 4z = 0 \quad &\Rightarrow \quad z = 2y \\ \vv &= (0, 1, 2) \end{aligned}

The third row, 4y−2z=04y - 2z = 0, says the same as the second. The check: A (0,1,2)=(0,  3+8,  4+18)=(0,11,22)A\,(0, 1, 2) = (0,\; 3 + 8,\; 4 + 18) = (0, 11, 22).

In the same way λ=2\lambda = 2 gives (1,0,0)(1, 0, 0) and λ=1\lambda = 1 gives (0,2,−1)(0, 2, -1).

Try it yourself

Which case is it? Decide for A=[1230]A = \mat{1 & 2 \\ 3 & 0}, B=[4−112]B = \mat{4 & -1 \\ 1 & 2} and C=[02−20]C = \mat{0 & 2 \\ -2 & 0} whether the matrix has two different real eigenvalues, one repeated eigenvalue, or no real eigenvalues.

Show a hint
Calculate tr⁡2−4det⁡\tr^2 - 4\det for each matrix and look at its sign.
Show the solution

Answer: AA has two different real eigenvalues, BB has one repeated eigenvalue, and CC has no real eigenvalues.

A:tr⁡=1,  det⁡=−61+24=25>0B:tr⁡=6,  det⁡=8+1=936−36=0C:tr⁡=0,  det⁡=0+4=40−16=−16<0\begin{aligned} A: \quad & \tr = 1, \;\det = -6 \\ & 1 + 24 = 25 > 0 \\[4pt] B: \quad & \tr = 6, \;\det = 8 + 1 = 9 \\ & 36 - 36 = 0 \\[4pt] C: \quad & \tr = 0, \;\det = 0 + 4 = 4 \\ & 0 - 16 = -16 < 0 \end{aligned}

AA has the eigenvalues 3 and −2-2. BB has the eigenvalue 3 twice. CC is a rotation by 90 degrees with a stretch by 2, so it turns every vector.

6Rules worth knowing

6.1What happens to the eigenvalues when the matrix changes

Let v\vv be an eigenvector of AA with the eigenvalue λ\lambda. Then v\vv is also an eigenvector of the following matrices:

Matrices made from A and their eigenvalues
MatrixIts eigenvalueWhy
A2A^2λ2\lambda^2A (Av)=A (λv)=λ (Av)=λ2vA\,(A\vv) = A\,(\lambda \vv) = \lambda\,(A\vv) = \lambda^2 \vv
AkA^kλk\lambda^kthe same step, kk times
A−1A\inv (if it exists)1λ\dfrac{1}{\lambda}multiply Av=λvA\vv = \lambda \vv by A−1A\inv: v=λA−1v\vv = \lambda A\inv \vv
cAcAcλc\lambda(cA) v=c (Av)(cA)\,\vv = c\,(A\vv)
A+cIA + cIλ+c\lambda + c(A+cI) v=λv+c v(A + cI)\,\vv = \lambda \vv + c\,\vv

One more rule: ATA\T has the same eigenvalues as AA, because a matrix and its transpose have the same determinant. Its eigenvectors are in general different.

Example. B=[4123]B = \mat{4 & 1 \\ 2 & 3} has the eigenvector v=(1,1)\vv = (1, 1) with λ=5\lambda = 5.

Bv=(5,5),B (5,5)=(25,25)=52 (1,1)B−1=110[3−1−24]B−1v=110 (2,2)=(0.2,0.2)=15 (1,1)\begin{aligned} B\vv &= (5, 5), \quad B\,(5, 5) = (25, 25) = 5^2\,(1, 1) \\[6pt] B\inv &= \frac{1}{10} \mat{3 & -1 \\ -2 & 4} \\[6pt] B\inv \vv &= \frac{1}{10}\,(2, 2) = (0.2, 0.2) = \frac{1}{5}\,(1, 1) \end{aligned}

6.2Eigenvectors of different eigenvalues

Eigenvectors that belong to different eigenvalues are linearly independent: they never lie on the same line. A 2 x 2 matrix with two different eigenvalues therefore has two independent eigenvectors, and every vector of the plane is a linear combination of them.

6.3Applying a matrix many times

This makes a hard calculation easy. Take A=[2112]A = \mat{2 & 1 \\ 1 & 2} with the eigenvectors (1,1)(1, 1) (eigenvalue 3) and (1,−1)(1, -1) (eigenvalue 1), and the vector u=(3,1)\vu = (3, 1):

u=(3,1)=2 (1,1)+1 (1,−1)\vu = (3, 1) = 2\,(1, 1) + 1\,(1, -1)

The matrix multiplies each part by its eigenvalue:

Au=2×3 (1,1)+1×1 (1,−1)=(6,6)+(1,−1)=(7,5)A10u=2×310 (1,1)+1×110 (1,−1)=(118098,118098)+(1,−1)=(118099,118097)\begin{aligned} A\vu &= 2 \times 3\,(1, 1) + 1 \times 1\,(1, -1) \\ &= (6, 6) + (1, -1) = (7, 5) \\[8pt] A^{10}\vu &= 2 \times 3^{10}\,(1, 1) + 1 \times 1^{10}\,(1, -1) \\ &= (118098, 118098) + (1, -1) \\ &= (118099, 118097) \end{aligned}

The first result can be checked directly: A (3,1)=(6+1,  3+2)=(7,5)A\,(3, 1) = (6 + 1,\; 3 + 2) = (7, 5). The second would need ten matrix multiplications without eigenvectors.

Look at the result: after ten steps the vector points almost exactly in the direction (1,1)(1, 1). The part with the largest eigenvalue grows fastest and takes over. The eigenvalue with the largest size is called the dominant eigenvalue, and its eigenvector is where repeated multiplication leads. PageRank (Part 9) is built on this fact.

Try it yourself

A matrix AA has the eigenvalues 3 and −2-2. Give the eigenvalues of A3A^3, of A−1A\inv and of A+4IA + 4I.

Show a hint
Raise them to the third power, take one over them, add 4.
Show the solution

Answer: A3A^3: 27 and −8-8. A−1A\inv: 13\tfrac{1}{3} and −12-\tfrac{1}{2}. A+4IA + 4I: 7 and 2.

A3:33=27,(−2)3=−8A−1:13≈0.333,1−2=−0.5A+4I:3+4=7,−2+4=2\begin{aligned} A^3: \quad & 3^3 = 27, \quad (-2)^3 = -8 \\ A\inv: \quad & \tfrac{1}{3} \approx 0.333, \quad \tfrac{1}{-2} = -0.5 \\ A + 4I: \quad & 3 + 4 = 7, \quad -2 + 4 = 2 \end{aligned}

The eigenvectors are the same for all four matrices. A third power keeps the sign of a negative eigenvalue.

7Diagonalization

7.1Everything in two matrices

Take B=[4123]B = \mat{4 & 1 \\ 2 & 3} again. Put its eigenvectors into the columns of a matrix PP, and its eigenvalues, in the same order, on the diagonal of a matrix DD:

P=[111−2],D=[5002]P = \mat{1 & 1 \\ 1 & -2}, \quad D = \mat{5 & 0 \\ 0 & 2}

Multiply BPBP. Every column of the product is BB times a column of PP (Topic 14), and that is an eigenvalue times the column:

BP=[525−4]PD=[111−2][5002]=[525−4]\begin{aligned} BP &= \mat{5 & 2 \\ 5 & -4} \\[6pt] PD &= \mat{1 & 1 \\ 1 & -2} \mat{5 & 0 \\ 0 & 2} = \mat{5 & 2 \\ 5 & -4} \end{aligned}

So BP=PDBP = PD. Multiply both sides from the right by P−1P\inv:

B=PDP−1B = P D P\inv

This is the diagonalization of BB: the matrix written with its eigenvectors and eigenvalues.

The check, with the inverse of PP:

det⁡(P)=1×(−2)−1×1=−3P−1=1−3[−2−1−11]=13[211−1]PDP−1=[525−4]⋅13[211−1]=13[12369]=[4123]\begin{aligned} \det(P) &= 1 \times (-2) - 1 \times 1 = -3 \\[6pt] P\inv &= \frac{1}{-3} \mat{-2 & -1 \\ -1 & 1} = \frac{1}{3} \mat{2 & 1 \\ 1 & -1} \\[6pt] P D P\inv &= \mat{5 & 2 \\ 5 & -4} \cdot \frac{1}{3} \mat{2 & 1 \\ 1 & -1} \\[6pt] &= \frac{1}{3} \mat{12 & 3 \\ 6 & 9} = \mat{4 & 1 \\ 2 & 3} \end{aligned}

7.2What it means

Read Bv=PDP−1vB\vv = P D P\inv \vv from right to left:

  1. P−1P\inv writes v\vv as a combination of the eigenvectors.
  2. DD multiplies each part by its eigenvalue.
  3. PP puts the parts together again.

It is the calculation of Part 6 ("applying a matrix many times") written with matrices. Seen from its eigenvectors, the matrix is diagonal: a pure stretch along two directions.

7.3Powers of a matrix

B2=(PDP−1)(PDP−1)=PD (P−1P) DP−1=PD2P−1Bk=PDkP−1\begin{aligned} B^2 &= (P D P\inv)(P D P\inv) \\ &= P D\,(P\inv P)\,D P\inv = P D^2 P\inv \\[6pt] B^k &= P D^k P\inv \end{aligned}

A power of a diagonal matrix is easy: only the diagonal entries are raised to the power.

D3=[125008]B3=PD3P−1=[1258125−16]⋅13[211−1]=13[258117234141]=[86397847]\begin{aligned} D^3 &= \mat{125 & 0 \\ 0 & 8} \\[6pt] B^3 = P D^3 P\inv &= \mat{125 & 8 \\ 125 & -16} \cdot \frac{1}{3} \mat{2 & 1 \\ 1 & -1} \\[6pt] &= \frac{1}{3} \mat{258 & 117 \\ 234 & 141} = \mat{86 & 39 \\ 78 & 47} \end{aligned}

7.4When it is not possible

PP must have an inverse, so the matrix needs as many independent eigenvectors as it has rows.

  • A 2 x 2 matrix with two different eigenvalues can always be diagonalized.
  • [2102]\mat{2 & 1 \\ 0 & 2} of Part 5 has only one line of eigenvectors: it cannot be diagonalized.
  • A symmetric matrix can always be diagonalized, and in an especially good way. That is the next part.

Try it yourself

A=[3241]A = \mat{3 & 2 \\ 4 & 1} has the eigenvector (1,1)(1, 1) with the eigenvalue 5 and the eigenvector (1,−2)(1, -2) with the eigenvalue −1-1. Write PP and DD in this order, and find A2A^2 as PD2P−1P D^2 P\inv.
DD
A2A^2

Show a hint
P=[111−2]P = \mat{1 & 1 \\ 1 & -2} is the matrix of the lesson, so P−1=13[211−1]P\inv = \tfrac{1}{3} \mat{2 & 1 \\ 1 & -1}. Square the two entries of DD.
Show the solution

Answer: D=[500−1]D = \mat{5 & 0 \\ 0 & -1} and A2=[178169]A^2 = \mat{17 & 8 \\ 16 & 9}.

P=[111−2],D=[500−1]D2=[25001],PD2=[25125−2]A2=[25125−2]⋅13[211−1]=13[51244827]=[178169]\begin{aligned} P &= \mat{1 & 1 \\ 1 & -2}, \quad D = \mat{5 & 0 \\ 0 & -1} \\[6pt] D^2 &= \mat{25 & 0 \\ 0 & 1}, \quad P D^2 = \mat{25 & 1 \\ 25 & -2} \\[6pt] A^2 &= \mat{25 & 1 \\ 25 & -2} \cdot \frac{1}{3} \mat{2 & 1 \\ 1 & -1} \\[6pt] &= \frac{1}{3} \mat{51 & 24 \\ 48 & 27} = \mat{17 & 8 \\ 16 & 9} \end{aligned}

The check without eigenvectors: A2=[9+86+212+48+1]A^2 = \mat{9 + 8 & 6 + 2 \\ 12 + 4 & 8 + 1}, the same matrix.

8Symmetric matrices

8.1Why they get a part of their own

The matrices of data science are very often symmetric. A covariance matrix, which says how the features of a data set vary together, is symmetric. For every matrix AA the product ATAA\T A is symmetric (a practice problem of the last lesson). Matrices of distances and of similarities are symmetric. And symmetric matrices have the best eigenvectors there are.

8.2Three facts

For every symmetric matrix:

  1. All eigenvalues are real numbers. The case "no real eigenvalues" cannot happen.
  2. Eigenvectors of different eigenvalues are perpendicular.
  3. It can always be diagonalized, with eigenvectors that are perpendicular to each other and have the length 1.

Together these facts are called the spectral theorem.

Fact 2 with the examples of this lesson: A=[2112]A = \mat{2 & 1 \\ 1 & 2} is symmetric, and its eigenvectors have the dot product (1,1)⋅(1,−1)=1−1=0(1, 1) \cdot (1, -1) = 1 - 1 = 0. B=[4123]B = \mat{4 & 1 \\ 2 & 3} is not symmetric, and its eigenvectors are not perpendicular: (1,1)⋅(1,−2)=−1(1, 1) \cdot (1, -2) = -1.

-2-112-1123(1, 1)(−1, 1)

symmetric: [2 1; 1 2]

(1, 1) · (−1, 1) = 0: perpendicular

-2-112-1123(1, 1)(−1, 2)

not symmetric: [4 1; 2 3]

(1, 1) · (−1, 2) = 1: not perpendicular

The eigenvectors of the two example matrices. (−1, 1) is a multiple of (1, −1), and (−1, 2) is a multiple of (1, −2): the same eigenvectors, drawn upwards.

Why fact 2 is true: for a symmetric matrix (Av)⋅w=v⋅(Aw)(A\vv) \cdot \vw = \vv \cdot (A\vw). With two eigenvectors v\vv and w\vw and their eigenvalues λ1\lambda_1 and λ2\lambda_2 this says λ1 (v⋅w)=λ2 (v⋅w)\lambda_1\,(\vv \cdot \vw) = \lambda_2\,(\vv \cdot \vw). When the two eigenvalues are different, that is only possible with v⋅w=0\vv \cdot \vw = 0.

8.3Orthonormal vectors and orthogonal matrices

Vectors are called orthonormal when each of them has the length 1 and they are perpendicular to each other. The eigenvectors of AA become orthonormal when each is divided by its length (Topic 13):

(1,1):length 212 (1,1)≈(0.707,0.707)(1,−1):length 212 (1,−1)≈(0.707,−0.707)\begin{aligned} (1, 1): \quad & \text{length } \sqrt{2} \\ & \tfrac{1}{\sqrt{2}}\,(1, 1) \approx (0.707, 0.707) \\[6pt] (1, -1): \quad & \text{length } \sqrt{2} \\ & \tfrac{1}{\sqrt{2}}\,(1, -1) \approx (0.707, -0.707) \end{aligned}

A square matrix QQ whose columns are orthonormal is called an orthogonal matrix. It has a remarkable property:

QTQ=IsoQ−1=QTQ\T Q = I \qquad \text{so} \qquad Q\inv = Q\T

Its inverse is simply its transpose. The reason: the entries of QTQQ\T Q are the dot products of the columns of QQ, and those are 1 (a column with itself) or 0 (two different columns).

Q=12[111−1]QTQ=12[111−1][111−1]=12[2002]=[1001]\begin{aligned} Q &= \frac{1}{\sqrt{2}} \mat{1 & 1 \\ 1 & -1} \\[6pt] Q\T Q &= \frac{1}{2} \mat{1 & 1 \\ 1 & -1} \mat{1 & 1 \\ 1 & -1} \\[6pt] &= \frac{1}{2} \mat{2 & 0 \\ 0 & 2} = \mat{1 & 0 \\ 0 & 1} \end{aligned}

An orthogonal matrix changes no length and no angle: it only turns or mirrors the plane. The rotation RR of the last lesson is an orthogonal matrix.

8.4The diagonalization of a symmetric matrix

With orthonormal eigenvectors in the columns of QQ, the formula A=PDP−1A = P D P\inv becomes

A=QDQTA = Q D Q\T

No inverse has to be calculated. For A=[2112]A = \mat{2 & 1 \\ 1 & 2} with D=[3001]D = \mat{3 & 0 \\ 0 & 1}:

QDQT=12[111−1][3001][111−1]=12[313−1][111−1]=12[4224]=[2112]\begin{aligned} Q D Q\T &= \frac{1}{2} \mat{1 & 1 \\ 1 & -1} \mat{3 & 0 \\ 0 & 1} \mat{1 & 1 \\ 1 & -1} \\[6pt] &= \frac{1}{2} \mat{3 & 1 \\ 3 & -1} \mat{1 & 1 \\ 1 & -1} \\[6pt] &= \frac{1}{2} \mat{4 & 2 \\ 2 & 4} = \mat{2 & 1 \\ 1 & 2} \end{aligned}

The picture behind the formula: QTQ\T turns the plane, DD stretches it along the axes, QQ turns it back. A symmetric matrix stretches the plane along perpendicular directions, and nothing else.

8.5Positive definite matrices: a first look

A symmetric matrix whose eigenvalues are all positive is called positive definite. If they are all positive or zero, it is called positive semidefinite.

There is a second way to say the same: AA is positive definite when the number xTAx\vx\T A \vx is positive for every vector x\vx that is not zero. For A=[2112]A = \mat{2 & 1 \\ 1 & 2} and x=(a,b)\vx = (a, b):

xTAx=(a,b)⋅(2a+b,  a+2b)=2a2+2ab+2b2x=(1,1):2+2+2=6x=(1,−1):2−2+2=2\begin{aligned} \vx\T A \vx &= (a, b) \cdot (2a + b,\; a + 2b) \\ &= 2a^2 + 2ab + 2b^2 \\[6pt] \vx = (1, 1): \quad & 2 + 2 + 2 = 6 \\ \vx = (1, -1): \quad & 2 - 2 + 2 = 2 \end{aligned}

Both eigenvalues of AA (3 and 1) are positive, so the result is positive for every x\vx. The matrix [1221]\mat{1 & 2 \\ 2 & 1} has the eigenvalues 3 and −1-1, so it is not positive definite: for x=(1,−1)\vx = (1, -1) the result is 1−4+1=−21 - 4 + 1 = -2.

You will meet the word in machine learning. Covariance matrices and all matrices of the form ATAA\T A are positive semidefinite. And a loss function whose matrix of second derivatives is positive definite has the shape of a bowl, with exactly one lowest point: the best case for training a model.

Try it yourself

Find the eigenvalues and the eigenvectors of the symmetric matrix A=[6229]A = \mat{6 & 2 \\ 2 & 9}. Is the matrix positive definite?

Show a hint
The trace is 15 and the determinant is 50. The two eigenvectors must come out perpendicular.
Show the solution

Answer: The eigenvalues are 10 and 5, with the eigenvectors (1,2)(1, 2) and (2,−1)(2, -1). Both eigenvalues are positive, so the matrix is positive definite.

λ2−15λ+50=0(λ−10)(λ−5)=0\begin{aligned} \lambda^2 - 15\lambda + 50 &= 0 \\ (\lambda - 10)(\lambda - 5) &= 0 \end{aligned}
λ=10:A−10I=[−422−1]−4x+2y=0  ⇒  y=2xv=(1,2)λ=5:A−5I=[1224]x+2y=0,v=(2,−1)\begin{aligned} \lambda = 10: \quad & A - 10I = \mat{-4 & 2 \\ 2 & -1} \\[4pt] & -4x + 2y = 0 \;\Rightarrow\; y = 2x \\ & \vv = (1, 2) \\[8pt] \lambda = 5: \quad & A - 5I = \mat{1 & 2 \\ 2 & 4} \\[4pt] & x + 2y = 0, \quad \vv = (2, -1) \end{aligned}

The dot product is (1,2)⋅(2,−1)=2−2=0(1, 2) \cdot (2, -1) = 2 - 2 = 0: perpendicular, as a symmetric matrix promises. Both eigenvalues are positive.

9Where eigenvectors are used

9.1Principal Component Analysis

A data set has two features. Its covariance matrix is

C=[5222]C = \mat{5 & 2 \\ 2 & 2}

On the diagonal are the variances of the two features (how much each of them varies), outside the diagonal is their covariance (how they vary together). How these numbers are calculated from data is a subject of the statistics lessons; here the matrix is given.

-7-6-5-4-3-2-11234567-5-4-3-2-112345direction (2, 1): variance 6direction (1, −2): variance 1
A cloud of data with the covariance matrix C. The data is spread most along the direction (2, 1), the eigenvector of the largest eigenvalue, and least along the perpendicular direction (1, −2).

The eigenvalues and eigenvectors of CC:

tr⁡(C)=7,det⁡(C)=10−4=6λ2−7λ+6=0(λ−6)(λ−1)=0λ=6orλ=1\begin{aligned} \tr(C) &= 7, \quad \det(C) = 10 - 4 = 6 \\[6pt] \lambda^2 - 7\lambda + 6 &= 0 \\ (\lambda - 6)(\lambda - 1) &= 0 \\ \lambda = 6 \quad &\text{or} \quad \lambda = 1 \end{aligned}
λ=6:C−6I=[−122−4]−x+2y=0,v=(2,1)λ=1:C−I=[4221]2x+y=0,v=(1,−2)\begin{aligned} \lambda = 6: \quad & C - 6I = \mat{-1 & 2 \\ 2 & -4} \\[4pt] & -x + 2y = 0, \quad \vv = (2, 1) \\[8pt] \lambda = 1: \quad & C - I = \mat{4 & 2 \\ 2 & 1} \\[4pt] & 2x + y = 0, \quad \vv = (1, -2) \end{aligned}

What they say about the data:

  • The data varies most along the direction (2,1)(2, 1). The variance in this direction is the eigenvalue, 6.
  • Perpendicular to it, along (1,−2)(1, -2), the variance is only 1. (The two directions are perpendicular because CC is symmetric.)
  • The total variance is 5+2=75 + 2 = 7, the trace, and also 6+1=76 + 1 = 7. The first direction carries 67\tfrac{6}{7}, about 86 percent, of it.

Now the useful step. Replace every data point by one number: its scalar projection onto the unit vector 15 (2,1)\tfrac{1}{\sqrt{5}}\,(2, 1) (Topic 13). Two numbers for every point become one, and 86 percent of the variance is kept. With 100 features the same idea keeps, say, the 10 best directions. This is Principal Component Analysis (Topic 44): the eigenvectors of the covariance matrix are the principal components, and the eigenvalues say how important each one is.

9.2The long run: from two shops to PageRank

Two shops share the customers of a town. Every month 10 percent of the customers of shop 1 change to shop 2, and 20 percent of the customers of shop 2 change to shop 1. If x\vx holds the share of shop 1 and the share of shop 2, then next month's shares are PxP\vx:

P=[0.90.20.10.8]P = \mat{0.9 & 0.2 \\ 0.1 & 0.8}

(Each column adds up to 1: every customer goes somewhere.) Starting with equal shares:

The shares of the two shops, month after month
MonthShare of shop 1Share of shop 2
00.50.5
10.550.45
20.5850.415
30.60950.3905
50.63870.3613
100.66200.3380
200.66650.3335

The shares settle down. Where? At a vector that the matrix does not change any more: Px=xP\vx = \vx. That is an eigenvector with the eigenvalue 1.

tr⁡(P)=1.7det⁡(P)=0.72−0.02=0.7λ2−1.7λ+0.7=0(λ−1)(λ−0.7)=0λ=1orλ=0.7\begin{aligned} \tr(P) &= 1.7 \\ \det(P) &= 0.72 - 0.02 = 0.7 \\[6pt] \lambda^2 - 1.7\lambda + 0.7 &= 0 \\ (\lambda - 1)(\lambda - 0.7) &= 0 \\ \lambda = 1 \quad &\text{or} \quad \lambda = 0.7 \end{aligned}
λ=1:P−I=[−0.10.20.1−0.2]−0.1x+0.2y=0  ⇒  x=2yv=(2,1)\begin{aligned} \lambda = 1: \quad & P - I = \mat{-0.1 & 0.2 \\ 0.1 & -0.2} \\[4pt] & -0.1x + 0.2y = 0 \;\Rightarrow\; x = 2y \\ & \vv = (2, 1) \end{aligned}

Shares must add up to 1, so divide by 2+1=32 + 1 = 3: the long-run shares are (23,13)\left(\tfrac{2}{3}, \tfrac{1}{3}\right), about (0.667,0.333)(0.667, 0.333). The table was on its way there. The other eigenvalue, 0.7, belongs to the part of the starting vector that fades away: it is multiplied by 0.7 every month. Whatever the starting shares are, the result is the same.

Choose the starting share of shop 1, then let the months pass.

Share of shop 1 at the start

Months that have passed

The vertical line marks two thirds, the long-run share of shop 1.

Month 0: shares (0.5000, 0.5000)

Distance to the long-run share: 0.1667. It is multiplied by 0.7, the second eigenvalue, every month.

Change the start: the shares always settle at (0.6667, 0.3333), the eigenvector of the eigenvalue 1.

Such a process, with fixed chances of moving from one state to another, is called a Markov chain. Google's PageRank is the same calculation with web pages instead of shops: a surfer clicks from page to page, and the long-run share of time spent on each page is its rank. It is the eigenvector with the eigenvalue 1 of a matrix with billions of rows, found exactly as in the table: by multiplying again and again.

9.3More places

More places where eigenvalues appear
WhereThe eigen idea
Vibrations of a bridge or a guitar stringthe eigenvalues are the natural frequencies
Stability of a systemall eigenvalues smaller than 1 in size: repeated steps die out
Face recognition with "eigenfaces"the eigenvectors of the covariance matrix of many face images
Compressing an imagekeeping only the directions with the largest values (see the singular values in Part 11)

Try it yourself

A city has two bike stations. Every day half of the bikes of station 1 end at station 2, and a quarter of the bikes of station 2 end at station 1. So tomorrow's shares are PxP\vx with P=[0.50.250.50.75]P = \mat{0.5 & 0.25 \\ 0.5 & 0.75}. Find the shares of the two stations in the long run.

Show a hint
Find the eigenvector of the eigenvalue 1 from P−IP - I, then divide it by the sum of its components.
Show the solution

Answer: The long-run shares are (13,23)\left(\tfrac{1}{3}, \tfrac{2}{3}\right), about (0.333,0.667)(0.333, 0.667).

P−I=[−0.50.250.5−0.25]−0.5x+0.25y=0  ⇒  y=2xv=(1,2)shares:13 (1,2)=(13,23)\begin{aligned} P - I &= \mat{-0.5 & 0.25 \\ 0.5 & -0.25} \\[6pt] -0.5x + 0.25y &= 0 \;\Rightarrow\; y = 2x \\ \vv &= (1, 2) \\[6pt] \text{shares:} \quad & \tfrac{1}{3}\,(1, 2) = \left(\tfrac{1}{3}, \tfrac{2}{3}\right) \end{aligned}

The check: P(13,23)=(16+16,  16+12)=(13,23)P \left(\tfrac{1}{3}, \tfrac{2}{3}\right) = \left(\tfrac{1}{6} + \tfrac{1}{6},\; \tfrac{1}{6} + \tfrac{1}{2}\right) = \left(\tfrac{1}{3}, \tfrac{2}{3}\right). The second eigenvalue is 1.25−1=0.251.25 - 1 = 0.25, so the shares settle very quickly.

10Vector norms

10.1More than one way to measure size

"How large is this vector?" "How far apart are these two data points?" "How big is the error of the model?" Each of these questions wants one number as an answer. A norm gives it: a norm turns a vector into one number that is never negative, the size of the vector. It is written with double bars: ∥x∥\norm{\vx}.

You know one norm already: the length of Topic 13. There are others, and each of them answers the question "how large?" in a slightly different way.

10.2The three norms of machine learning

Take x=(3,−4)\vx = (3, -4).

The L2 norm (Euclidean norm). Square the components, add, take the root. It is the length of Topic 13, where it was written ∣a∣\len{\va}.

∥x∥2=x12+x22+⋯+xn2∥(3,−4)∥2=9+16=5\begin{aligned} \norm{\vx}_2 &= \sqrt{x_1^2 + x_2^2 + \dots + x_n^2} \\[4pt] \norm{(3, -4)}_2 &= \sqrt{9 + 16} = 5 \end{aligned}

The L1 norm (Manhattan norm). Add the absolute values of the components.

∥x∥1=∣x1∣+∣x2∣+⋯+∣xn∣∥(3,−4)∥1=3+4=7\begin{aligned} \norm{\vx}_1 &= \len{x_1} + \len{x_2} + \dots + \len{x_n} \\[4pt] \norm{(3, -4)}_1 &= 3 + 4 = 7 \end{aligned}

The max norm (L∞ norm, "L infinity"). Take the largest absolute value.

∥x∥∞=max⁡(∣x1∣,∣x2∣,…,∣xn∣)∥(3,−4)∥∞=4\begin{aligned} \norm{\vx}_\infty &= \max\big(\len{x_1}, \len{x_2}, \dots, \len{x_n}\big) \\[4pt] \norm{(3, -4)}_\infty &= 4 \end{aligned}
The three norms of four vectors
VectorL1 normL2 normMax norm
(3,−4)(3, -4)754
(1,2,−2)(1, 2, -2)532
(1,1,1,1)(1, 1, 1, 1)421
(0,−6,0)(0, -6, 0)666

Two things to notice. For every vector

∥x∥∞  ≤  ∥x∥2  ≤  ∥x∥1\norm{\vx}_\infty \;\le\; \norm{\vx}_2 \;\le\; \norm{\vx}_1

and the three norms are equal when the vector has only one component that is not 0.

Move the vector. Each shape shows all vectors with the same norm as yours.

-8-6-4-22468-8-6-4-22468
  • L1 norm (diamond)
  • L2 norm (circle)
  • max norm (square)

L1 norm: |3| + |−4| = 7

L2 norm: √(9 + 16) = √25 = 5

Max norm: the larger of 3 and 4 = 4

4 ≤ 5 ≤ 7: the max norm is never larger than the L2 norm, and the L2 norm never larger than the L1 norm. On an axis all three are equal.

10.3The family behind them

The three norms belong to one family, the Lp norms:

∥x∥p=(∣x1∣p+∣x2∣p+⋯+∣xn∣p)1/p\norm{\vx}_p = \big(\len{x_1}^p + \len{x_2}^p + \dots + \len{x_n}^p\big)^{1/p}

for p=1,2,3,…p = 1, 2, 3, \dots The values p=1p = 1 and p=2p = 2 give the first two norms. The larger pp is, the more the largest component counts, and for very large pp only the largest one is left: that is why the max norm is called L∞. For x=(3,−4)\vx = (3, -4):

The Lp norm of (3, -4) for growing p
pp123410100
∥x∥p\norm{\vx}_p754.4984.2854.0224.000

Two relatives that you will meet:

  • The squared L2 norm, ∥x∥22=x12+⋯+xn2=x⋅x\norm{\vx}_2^2 = x_1^2 + \dots + x_n^2 = \vx \cdot \vx. It has no root, so it is easier to calculate with, and most formulas of machine learning use it.
  • The "L0 norm", the number of components that are not 0. It is not a real norm (it breaks rule 2 below), but the name is used when people speak about sparse vectors: vectors with many zeros.

10.4The three rules of every norm

A way of measuring size is called a norm only if it obeys three rules:

The three rules of a norm
RuleIn symbolsIn words
1∥x∥≥0\norm{\vx} \ge 0, and ∥x∥=0\norm{\vx} = 0 only for the zero vectorsizes are not negative
2∥c x∥=∣c∣ ∥x∥\norm{c\,\vx} = \len{c}\,\norm{\vx}a vector 3 times as long has 3 times the size
3∥x+y∥≤∥x∥+∥y∥\norm{\vx + \vy} \le \norm{\vx} + \norm{\vy}the triangle inequality: a detour is never shorter

A check with the L1 norm, for x=(3,−4)\vx = (3, -4) and y=(1,2)\vy = (1, 2):

rule 2:−2x=(−6,8)∥−2x∥1=6+8=14=2×7rule 3:x+y=(4,−2),∥x+y∥1=6∥x∥1+∥y∥1=7+3=10,6≤10\begin{aligned} \text{rule 2:} \quad & -2\vx = (-6, 8) \\ & \norm{-2\vx}_1 = 6 + 8 = 14 = 2 \times 7 \\[6pt] \text{rule 3:} \quad & \vx + \vy = (4, -2), \quad \norm{\vx + \vy}_1 = 6 \\ & \norm{\vx}_1 + \norm{\vy}_1 = 7 + 3 = 10, \quad 6 \le 10 \end{aligned}

10.5What "size 1" looks like

Draw all the vectors whose norm is exactly 1. For the L2 norm they form a circle. For the L1 norm they form a square standing on its corner (a diamond), and for the max norm a square.

-2-112-2-112
  • L1 norm (diamond)
  • L2 norm (circle)
  • max norm (square)
All vectors with the norm 1, for the three norms. The corners of the diamond lie on the axes, where one component is 0.

Some points to test the picture: (1,0)(1, 0) lies on all three shapes. (0.6,0.8)(0.6, 0.8) lies on the circle (0.36+0.64=10.36 + 0.64 = 1). (0.5,0.5)(0.5, 0.5) lies on the diamond (0.5+0.5=10.5 + 0.5 = 1). (1,1)(1, 1) lies on the square (the largest component is 1). The corners of the diamond lie on the axes. Remember this for Part 11: it is the reason why the L1 norm produces zeros.

10.6Distances

Every norm gives a distance: the distance between a\va and b\vb is the norm of their difference, ∥a−b∥\norm{\va - \vb}. For a=(1,2)\va = (1, 2) and b=(4,6)\vb = (4, 6) the difference is (−3,−4)(-3, -4):

Three distances between the same two points
DistanceNormValue
Euclidean distanceL29+16=5\sqrt{9 + 16} = 5
Manhattan distanceL13+4=73 + 4 = 7
Chebyshev distancemax44
1234561234567a = (1, 2)b = (4, 6)Euclidean: 534Manhattan:3 + 4 = 7
From a to b: the Euclidean distance is the straight line, the Manhattan distance follows the streets of a grid.

The Euclidean distance is the straight line. The Manhattan distance is the way of a taxi in a city with a grid of streets: 3 blocks along and 4 blocks up. The Chebyshev distance is the number of moves a king needs on a chess board, where one move can change both coordinates at once.

10.7Normalizing a vector

Dividing a vector by its norm gives a vector with the norm 1.

  • Dividing by the L2 norm gives the unit vector of Topic 13: the direction of the vector without its length. (3,−4)(3, -4) becomes (0.6,−0.8)(0.6, -0.8).
  • Dividing by the L1 norm gives a vector whose absolute values add up to 1. For a vector of counts these are shares: (2,3,5)(2, 3, 5) has the L1 norm 10 and becomes (0.2,0.3,0.5)(0.2, 0.3, 0.5). This is how counts are turned into proportions and probabilities.

Try it yourself

Find the L1 norm, the L2 norm and the max norm of x=(−6,0,8)\vx = (-6, 0, 8). Then find the Manhattan distance between (2,7)(2, 7) and (5,3)(5, 3).

Show a hint
Absolute values for the L1 norm and the max norm, squares and a root for the L2 norm. A distance is the norm of the difference.
Show the solution

Answer: The norms are 14, 10 and 8. The Manhattan distance is 7.

∥x∥1=6+0+8=14∥x∥2=36+0+64=100=10∥x∥∞=8(2,7)−(5,3)=(−3,4),3+4=7\begin{aligned} \norm{\vx}_1 &= 6 + 0 + 8 = 14 \\ \norm{\vx}_2 &= \sqrt{36 + 0 + 64} = \sqrt{100} = 10 \\ \norm{\vx}_\infty &= 8 \\[6pt] (2, 7) - (5, 3) &= (-3, 4), \quad 3 + 4 = 7 \end{aligned}

The order 8≤10≤148 \le 10 \le 14 is as it must be: max norm, L2 norm, L1 norm.

11Norms in machine learning

11.1Measuring the error of a model

A model predicts four values. The true values are y=(3,5,2,8)\vy = (3, 5, 2, 8), the predictions are y^=(2,5,4,4)\hat{\vy} = (2, 5, 4, 4). The vector of the errors is

r=y−y^=(1,0,−2,4)\vr = \vy - \hat{\vy} = (1, 0, -2, 4)

How good is the model? Every usual answer is a norm of r\vr:

Four measures of the error, each a norm
MeasureFormulaValue
Mean absolute error (MAE)∥r∥1n\dfrac{\norm{\vr}_1}{n}1+0+2+44=1.75\dfrac{1 + 0 + 2 + 4}{4} = 1.75
Mean squared error (MSE)∥r∥22n\dfrac{\norm{\vr}_2^2}{n}1+0+4+164=5.25\dfrac{1 + 0 + 4 + 16}{4} = 5.25
Root mean squared error (RMSE)MSE\sqrt{\text{MSE}}5.25≈2.29\sqrt{5.25} \approx 2.29
Largest error∥r∥∞\norm{\vr}_\infty44

The two main measures treat a large error differently. In the MAE the error 4 is 4 of the 7 points, 57 percent. In the MSE it is 16 of the 21 points, 76 percent: squaring makes large errors count much more. So a model that is trained with the squared L2 norm works hard to avoid single large errors and is sensitive to outliers, and a model trained with the L1 norm is more robust against them. Linear regression (Topic 22) minimizes the MSE.

11.2Keeping the weights small: regularization

A model with very large weights reacts strongly to small changes of its input, and often it has learned the noise of the training data (overfitting, Topic 23). The cure is to make large weights expensive. A penalty is added to the loss:

Ridge regression:loss+α ∥w∥22Lasso regression:loss+α ∥w∥1\begin{aligned} \text{Ridge regression:} \quad & \text{loss} + \alpha\,\norm{\vw}_2^2 \\ \text{Lasso regression:} \quad & \text{loss} + \alpha\,\norm{\vw}_1 \end{aligned}

α\alpha is a number that we choose: the larger it is, the stronger the penalty. (Many books write λ\lambda for this number. This lesson uses α\alpha, because λ\lambda is an eigenvalue here. scikit-learn calls it alpha too.)

For the weights w=(0.5,−3,0,2)\vw = (0.5, -3, 0, 2) and α=0.1\alpha = 0.1:

∥w∥1=0.5+3+0+2=5.5penalty of Lasso:0.1×5.5=0.55∥w∥22=0.25+9+0+4=13.25penalty of Ridge:0.1×13.25=1.325\begin{aligned} \norm{\vw}_1 &= 0.5 + 3 + 0 + 2 = 5.5 \\ \text{penalty of Lasso:} \quad & 0.1 \times 5.5 = 0.55 \\[6pt] \norm{\vw}_2^2 &= 0.25 + 9 + 0 + 4 = 13.25 \\ \text{penalty of Ridge:} \quad & 0.1 \times 13.25 = 1.325 \end{aligned}

The two penalties push the weights in different ways:

  • In ∥w∥22\norm{\vw}_2^2 the weight −3-3 costs 9, and the weight 0.50.5 costs only 0.25. Large weights are punished hard, small ones hardly at all. Ridge makes all weights smaller, the large ones most, but it rarely makes a weight exactly 0.
  • In ∥w∥1\norm{\vw}_1 every bit of a weight costs the same, whether the weight is large or small. So it pays to push small weights all the way to 0. Lasso produces weights that are exactly 0: it switches features off, and the model becomes sparse. The corners of the diamond in the picture of Part 10, which lie on the axes, are the geometric reason.

Topic 26 teaches both methods. Their whole difference is the choice of the norm.

11.3Which points are neighbours?

k-Nearest Neighbors (Topics 32 and 33) and k-means (Topic 43) work with distances, and the answer can depend on the norm. Which of A=(3,3)A = (3, 3) and B=(0,5)B = (0, 5) is nearer to the point (0,0)(0, 0)?

The same two points in three distances
Distanceto A=(3,3)A = (3, 3)to B=(0,5)B = (0, 5)Nearer
Euclidean (L2)18≈4.24\sqrt{18} \approx 4.2455AA
Manhattan (L1)6655BB
Chebyshev (max)3355AA

The usual choice is the Euclidean distance. In every norm a feature with large numbers (a salary next to an age) decides the distance almost alone. That is why features are scaled before such methods are used (Topic 24).

11.4Unit vectors everywhere

The cosine similarity of Topic 13 divides a dot product by two L2 norms. In practice the vectors are normalized once, to the L2 norm 1, and stored. Then the cosine similarity is simply the dot product. Word embeddings and the vectors of a vector database are kept in this form.

11.5Gradient clipping

A neural network learns by taking steps along a vector called the gradient (Topic 53). Sometimes this vector becomes far too long, and one wild step destroys what was learned. The remedy: if the norm is larger than a limit, shrink the vector to the limit and keep its direction.

g=(6,8),∥g∥2=10,limit 510>5:g×510=(3,4)\begin{aligned} \vg &= (6, 8), \quad \norm{\vg}_2 = 10, \quad \text{limit } 5 \\[4pt] 10 > 5: \quad & \vg \times \tfrac{5}{10} = (3, 4) \end{aligned}

The new vector has the same direction and the norm 5.

-11234567-1123456789g = (6, 8), norm 10(3, 4), norm 5limit 5
Gradient clipping: the vector g is too long. It is shrunk to the limit, the circle of the norm 5, and keeps its direction.

11.6Norms of matrices

A matrix has a size too. The most used one is the Frobenius norm: treat the matrix as one long vector and take the L2 norm.

A=[1234]∥A∥F=1+4+9+16=30≈5.48\begin{aligned} A &= \mat{1 & 2 \\ 3 & 4} \\[6pt] \norm{A}_F &= \sqrt{1 + 4 + 9 + 16} = \sqrt{30} \approx 5.48 \end{aligned}

It measures how different two matrices are, for example an image and its compressed version: ∥A−B∥F\norm{A - B}_F.

11.7A look ahead: singular values

Here the two halves of the lesson meet. For any matrix AA, even one that is not square, the matrix ATAA\T A is symmetric and positive semidefinite, so its eigenvalues are real and not negative. Their square roots are called the singular values of AA.

A=[3045]ATA=[3405][3045]=[25202025]tr⁡=50,det⁡=625−400=225λ2−50λ+225=0(λ−45)(λ−5)=0λ=45orλ=5\begin{aligned} A &= \mat{3 & 0 \\ 4 & 5} \\[6pt] A\T A &= \mat{3 & 4 \\ 0 & 5} \mat{3 & 0 \\ 4 & 5} = \mat{25 & 20 \\ 20 & 25} \\[6pt] \tr &= 50, \quad \det = 625 - 400 = 225 \\[6pt] \lambda^2 - 50\lambda + 225 &= 0 \\ (\lambda - 45)(\lambda - 5) &= 0 \\ \lambda = 45 \quad &\text{or} \quad \lambda = 5 \end{aligned}

The singular values are 45≈6.71\sqrt{45} \approx 6.71 and 5≈2.24\sqrt{5} \approx 2.24. They are the stretch factors of the matrix: the largest one, 6.71, is the most that AA can stretch a vector of the length 1. They also give the Frobenius norm: ∥A∥F2=9+0+16+25=50=45+5\norm{A}_F^2 = 9 + 0 + 16 + 25 = 50 = 45 + 5.

Writing a matrix with its singular values is called the singular value decomposition (SVD). It does for every matrix what diagonalization does for symmetric ones. Libraries use it to calculate a PCA, and it is behind recommendation systems and the compression of images. In this course you will use it as a ready tool; what you need to remember is where it comes from: the eigenvalues of ATAA\T A.

Try it yourself

The true values are y=(12,9,15,6)\vy = (12, 9, 15, 6) and a model predicts y^=(10,9,16,11)\hat{\vy} = (10, 9, 16, 11). Find the MAE, the MSE and the largest error. Then find the penalty of Lasso and of Ridge for the weights w=(2,−1,0,3)\vw = (2, -1, 0, 3) and α=0.5\alpha = 0.5.

Show a hint
First the error vector r=y−y^\vr = \vy - \hat{\vy}. The MAE uses its L1 norm, the MSE its squared L2 norm, each divided by 4. Lasso uses the L1 norm of the weights, Ridge the squared L2 norm.
Show the solution

Answer: MAE 2, MSE 7.5, largest error 5. The Lasso penalty is 3 and the Ridge penalty is 7.

r=(2,0,−1,−5)MAE=2+0+1+54=84=2MSE=4+0+1+254=304=7.5∥r∥∞=5∥w∥1=2+1+0+3=6,0.5×6=3∥w∥22=4+1+0+9=14,0.5×14=7\begin{aligned} \vr &= (2, 0, -1, -5) \\ \text{MAE} &= \frac{2 + 0 + 1 + 5}{4} = \frac{8}{4} = 2 \\[4pt] \text{MSE} &= \frac{4 + 0 + 1 + 25}{4} = \frac{30}{4} = 7.5 \\[4pt] \norm{\vr}_\infty &= 5 \\[8pt] \norm{\vw}_1 &= 2 + 1 + 0 + 3 = 6, \quad 0.5 \times 6 = 3 \\ \norm{\vw}_2^2 &= 4 + 1 + 0 + 9 = 14, \quad 0.5 \times 14 = 7 \end{aligned}

The single error 5 is 25 of the 30 points of the squared L2 norm: one bad prediction decides the MSE.

12Common mistakes

Accepting the zero vector. A 0=λ 0A\,\vzero = \lambda\,\vzero is always true and says nothing. An eigenvector is never the zero vector. If solving (A−λI) v=0(A - \lambda I)\,\vv = \vzero gives only v=0\vv = \vzero, the eigenvalue is wrong.

Subtracting λ\lambda from every entry. A−λIA - \lambda I changes only the main diagonal. The other entries stay as they are.

The wrong signs in the characteristic equation. It is λ2−tr⁡(A) λ+det⁡(A)=0\lambda^2 - \tr(A)\,\lambda + \det(A) = 0: minus the trace, plus the determinant.

Thinking that there is one right eigenvector. (1,1)(1, 1), (2,2)(2, 2) and (−3,−3)(-3, -3) are the same answer. Compare directions, not numbers.

Mixing up "eigenvalue 0" and "no eigenvalue". An eigenvalue 0 is a real eigenvalue with real eigenvectors: the matrix is singular. "No real eigenvalues" means that the characteristic equation has no real solution.

Using row operations before looking for eigenvalues. Elimination keeps the solutions of a system, but it changes the eigenvalues. Work with the matrix as it is.

Adding eigenvalues of two matrices. The eigenvalues of A+BA + B are in general not the sums, and those of ABAB not the products, of the eigenvalues of AA and BB. The rules of Part 6 are about one matrix.

A different order in PP and DD. The first column of PP must belong to the first entry of DD.

Writing P−1DPP\inv D P. The diagonalization is A=PDP−1A = P D P\inv, with the inverse at the end.

Expecting perpendicular eigenvectors from every matrix. Only symmetric matrices promise them.

Forgetting the absolute values in the L1 norm. ∥(3,−4)∥1\norm{(3, -4)}_1 is 7, not 3−4=−13 - 4 = -1. A norm is never negative.

Taking the largest component for the max norm. It is the largest absolute value: ∥(2,−9)∥∞=9\norm{(2, -9)}_\infty = 9.

Mixing up ∥x∥2\norm{\vx}_2 and ∥x∥22\norm{\vx}_2^2. The MSE and the Ridge penalty use the square, without the root. The RMSE and a distance use the root.

Adding norms. ∥x+y∥\norm{\vx + \vy} is in general not ∥x∥+∥y∥\norm{\vx} + \norm{\vy}. Only "smaller or equal" is true.

Common mistakes and what is right
MistakeWhat is right
the zero vector as an eigenvectoran eigenvector is not the zero vector
A−λA - \lambda in every entryA−λIA - \lambda I: only the main diagonal
λ2+tr⁡(A) λ+det⁡(A)=0\lambda^2 + \tr(A)\,\lambda + \det(A) = 0λ2−tr⁡(A) λ+det⁡(A)=0\lambda^2 - \tr(A)\,\lambda + \det(A) = 0
"(1,1)(1, 1) is right, (2,2)(2, 2) is wrong"both are the same eigenvector
eigenvalues after row operationsrow operations change the eigenvalues
A=P−1DPA = P\inv D PA=PDP−1A = P D P\inv
∥(3,−4)∥1=−1\norm{(3, -4)}_1 = -1∥(3,−4)∥1=7\norm{(3, -4)}_1 = 7
∥(2,−9)∥∞=2\norm{(2, -9)}_\infty = 2∥(2,−9)∥∞=9\norm{(2, -9)}_\infty = 9
MSE=∥r∥2n\text{MSE} = \dfrac{\norm{\vr}_2}{n}MSE=∥r∥22n\text{MSE} = \dfrac{\norm{\vr}_2^2}{n}
∥x+y∥=∥x∥+∥y∥\norm{\vx + \vy} = \norm{\vx} + \norm{\vy}∥x+y∥≤∥x∥+∥y∥\norm{\vx + \vy} \le \norm{\vx} + \norm{\vy}

13Solved problems (from the easiest to the hardest)

Each problem has the question, the idea behind it with the part of the lesson it uses, and the calculation step by step. Cover the solution and try first.

Level 1: The basics

Problem 1: Is it an eigenvector?

Which of the vectors v=(1,0)\vv = (1, 0), w=(1,1)\vw = (1, 1) and u=(1,−1)\vu = (1, -1) are eigenvectors of A=[3102]A = \mat{3 & 1 \\ 0 & 2}? Give the eigenvalue where there is one.

Idea. Multiply and compare: AvA\vv must be a multiple of v\vv (Part 2).

Av=[3×1+1×00×1+2×0]=[30]=3[10]Aw=[3×1+1×10×1+2×1]=[42]Au=[3×1+1×(−1)0×1+2×(−1)]=[2−2]=2[1−1]\begin{aligned} A\vv &= \mat{3 \times 1 + 1 \times 0 \\ 0 \times 1 + 2 \times 0} = \mat{3 \\ 0} = 3 \mat{1 \\ 0} \\[6pt] A\vw &= \mat{3 \times 1 + 1 \times 1 \\ 0 \times 1 + 2 \times 1} = \mat{4 \\ 2} \\[6pt] A\vu &= \mat{3 \times 1 + 1 \times (-1) \\ 0 \times 1 + 2 \times (-1)} = \mat{2 \\ -2} = 2 \mat{1 \\ -1} \end{aligned}

v\vv is an eigenvector with λ=3\lambda = 3 and u\vu is an eigenvector with λ=2\lambda = 2. w\vw is not an eigenvector: (4,2)(4, 2) is not a multiple of (1,1)(1, 1).

Why: the matrix is triangular, so its eigenvalues are the diagonal entries 3 and 2 (Part 3): the test found both. For w\vw the two components were multiplied by different numbers, 4 and 2, and that is a turn.

Problem 2: Eigenvalues that can be read off

Give the eigenvalues of D=[400−2]D = \mat{4 & 0 \\ 0 & -2} and of U=[1506]U = \mat{1 & 5 \\ 0 & 6} without a long calculation. Which eigenvectors of DD do you know at once?

Idea. For a diagonal and for a triangular matrix the eigenvalues are the entries of the diagonal (Parts 2 and 3).

D:eigenvalues 4 and −2D (1,0)=(4,0),D (0,1)=(0,−2)U:det⁡(U−λI)=(1−λ)(6−λ)−5×0λ=1orλ=6\begin{aligned} D: \quad & \text{eigenvalues } 4 \text{ and } -2 \\ & D\,(1, 0) = (4, 0), \quad D\,(0, 1) = (0, -2) \\[8pt] U: \quad & \det(U - \lambda I) = (1 - \lambda)(6 - \lambda) - 5 \times 0 \\ & \lambda = 1 \quad \text{or} \quad \lambda = 6 \end{aligned}

The eigenvectors of DD are (1,0)(1, 0) and (0,1)(0, 1). The check for UU: 1+6=7=tr⁡(U)1 + 6 = 7 = \tr(U) and 1×6=6=det⁡(U)1 \times 6 = 6 = \det(U).

Why: the entry 5 above the diagonal has no influence on the eigenvalues of UU. It does change the eigenvectors: (0,1)(0, 1) is not an eigenvector of UU, because U (0,1)=(5,6)U\,(0, 1) = (5, 6).

Problem 3: The characteristic equation

Find the eigenvalues of A=[1232]A = \mat{1 & 2 \\ 3 & 2}.

Idea. For a 2 x 2 matrix the characteristic equation is λ2−tr⁡(A) λ+det⁡(A)=0\lambda^2 - \tr(A)\,\lambda + \det(A) = 0 (Part 3).

tr⁡(A)=1+2=3det⁡(A)=1×2−2×3=−4λ2−3λ−4=0(λ−4)(λ+1)=0λ=4orλ=−1\begin{aligned} \tr(A) &= 1 + 2 = 3 \\ \det(A) &= 1 \times 2 - 2 \times 3 = -4 \\[6pt] \lambda^2 - 3\lambda - 4 &= 0 \\ (\lambda - 4)(\lambda + 1) &= 0 \\ \lambda = 4 \quad &\text{or} \quad \lambda = -1 \end{aligned}

The check: 4+(−1)=34 + (-1) = 3 and 4×(−1)=−44 \times (-1) = -4.

Why: the determinant is negative, so the two eigenvalues must have different signs: their product is the determinant. One direction is stretched, the other one is flipped.

Problem 4: The eigenvectors

Find the eigenvectors of the matrix A=[1232]A = \mat{1 & 2 \\ 3 & 2} of Problem 3.

Idea. For every eigenvalue solve (A−λI) v=0(A - \lambda I)\,\vv = \vzero (Part 4).

λ=4:A−4I=[−323−2]−3x+2y=0x=2,  y=3:v=(2,3)λ=−1:A+I=[2233]2x+2y=0x=1,  y=−1:v=(1,−1)\begin{aligned} \lambda = 4: \quad & A - 4I = \mat{-3 & 2 \\ 3 & -2} \\[4pt] & -3x + 2y = 0 \\ & x = 2, \; y = 3: \quad \vv = (2, 3) \\[8pt] \lambda = -1: \quad & A + I = \mat{2 & 2 \\ 3 & 3} \\[4pt] & 2x + 2y = 0 \\ & x = 1, \; y = -1: \quad \vv = (1, -1) \end{aligned}

The checks:

A (2,3)=(2+6,  6+6)=(8,12)=4 (2,3)A (1,−1)=(1−2,  3−2)=(−1,1)=−1 (1,−1)\begin{aligned} A\,(2, 3) &= (2 + 6,\; 6 + 6) = (8, 12) \\ &= 4\,(2, 3) \\[4pt] A\,(1, -1) &= (1 - 2,\; 3 - 2) = (-1, 1) \\ &= -1\,(1, -1) \end{aligned}

Why: for λ=4\lambda = 4 the choice x=2x = 2 avoids a fraction; (1,1.5)(1, 1.5) would be the same eigenvector. Subtracting the eigenvalue −1-1 means adding 1 to the diagonal.

Problem 5: The two checks

A student says that A=[6223]A = \mat{6 & 2 \\ 2 & 3} has the eigenvalues 3 and 5. Show without solving an equation that this is wrong. Then find the right eigenvalues.

Idea. The sum of the eigenvalues is the trace, their product is the determinant (Part 3).

tr⁡(A)=9,det⁡(A)=18−4=14the student:3+5=8≠9,3×5=15≠14λ2−9λ+14=0(λ−2)(λ−7)=0λ=2orλ=7\begin{aligned} \tr(A) &= 9, \quad \det(A) = 18 - 4 = 14 \\[4pt] \text{the student:} \quad & 3 + 5 = 8 \neq 9, \quad 3 \times 5 = 15 \neq 14 \\[8pt] \lambda^2 - 9\lambda + 14 &= 0 \\ (\lambda - 2)(\lambda - 7) &= 0 \\ \lambda = 2 \quad &\text{or} \quad \lambda = 7 \end{aligned}

The check: 2+7=92 + 7 = 9 and 2×7=142 \times 7 = 14.

Why: the two checks need ten seconds and catch almost every slip. One failed check is enough to know that an answer is wrong.

Problem 6: Three norms

Find the L1 norm, the L2 norm and the max norm of x=(2,−3,6)\vx = (2, -3, 6).

Idea. Add the absolute values, or square, add and take the root, or take the largest absolute value (Part 10).

∥x∥1=2+3+6=11∥x∥2=4+9+36=49=7∥x∥∞=6\begin{aligned} \norm{\vx}_1 &= 2 + 3 + 6 = 11 \\ \norm{\vx}_2 &= \sqrt{4 + 9 + 36} = \sqrt{49} = 7 \\ \norm{\vx}_\infty &= 6 \end{aligned}

The check: 6≤7≤116 \le 7 \le 11.

Why: the minus sign of −3-3 disappears in all three norms: in an absolute value, in a square, in an absolute value. The order max norm, L2 norm, L1 norm from small to large is a quick test of the three results.

Problem 7: A distance in three norms

Find the Manhattan, the Euclidean and the Chebyshev distance between a=(1,5)\va = (1, 5) and b=(7,−3)\vb = (7, -3).

Idea. A distance is the norm of the difference (Part 10).

a−b=(1−7,  5−(−3))=(−6,8)Manhattan:6+8=14Euclidean:36+64=100=10Chebyshev:8\begin{aligned} \va - \vb &= (1 - 7,\; 5 - (-3)) = (-6, 8) \\[4pt] \text{Manhattan:} \quad & 6 + 8 = 14 \\ \text{Euclidean:} \quad & \sqrt{36 + 64} = \sqrt{100} = 10 \\ \text{Chebyshev:} \quad & 8 \end{aligned}

Why: one subtraction, then three norms. The order of the subtraction does not matter for a distance: b−a=(6,−8)\vb - \va = (6, -8) gives the same three numbers.

Problem 8: Normalizing in two ways

Normalize x=(4,0,−3)\vx = (4, 0, -3) with the L2 norm, and c=(6,3,1)\vc = (6, 3, 1) with the L1 norm.

Idea. Divide the vector by its norm (Part 10).

∥x∥2=16+0+9=515 x=(0.8,  0,  −0.6)∥c∥1=6+3+1=10110 c=(0.6,  0.3,  0.1)\begin{aligned} \norm{\vx}_2 &= \sqrt{16 + 0 + 9} = 5 \\ \tfrac{1}{5}\,\vx &= (0.8,\; 0,\; -0.6) \\[6pt] \norm{\vc}_1 &= 6 + 3 + 1 = 10 \\ \tfrac{1}{10}\,\vc &= (0.6,\; 0.3,\; 0.1) \end{aligned}

The checks: 0.64+0+0.36=10.64 + 0 + 0.36 = 1 and 0.6+0.3+0.1=10.6 + 0.3 + 0.1 = 1.

Why: the first result is a direction: a unit vector. The second is a list of shares: if c\vc counts how often three words appear in a text, the result says that the first word makes up 60 percent.

Level 2: Eigenvalues, eigenvectors and norms at work

Problem 9: A symmetric matrix

Find the eigenvalues and eigenvectors of A=[344−3]A = \mat{3 & 4 \\ 4 & -3}, and check that the eigenvectors are perpendicular.

Idea. The recipe of Part 4. For a symmetric matrix the eigenvectors of different eigenvalues must be perpendicular (Part 8).

tr⁡(A)=0,det⁡(A)=−9−16=−25λ2−25=0λ=5orλ=−5\begin{aligned} \tr(A) &= 0, \quad \det(A) = -9 - 16 = -25 \\ \lambda^2 - 25 &= 0 \\ \lambda = 5 \quad &\text{or} \quad \lambda = -5 \end{aligned}
λ=5:A−5I=[−244−8]−2x+4y=0  ⇒  x=2yv=(2,1)λ=−5:A+5I=[8442]8x+4y=0  ⇒  y=−2xv=(1,−2)\begin{aligned} \lambda = 5: \quad & A - 5I = \mat{-2 & 4 \\ 4 & -8} \\[4pt] & -2x + 4y = 0 \;\Rightarrow\; x = 2y \\ & \vv = (2, 1) \\[8pt] \lambda = -5: \quad & A + 5I = \mat{8 & 4 \\ 4 & 2} \\[4pt] & 8x + 4y = 0 \;\Rightarrow\; y = -2x \\ & \vv = (1, -2) \end{aligned}

The dot product is (2,1)⋅(1,−2)=2−2=0(2, 1) \cdot (1, -2) = 2 - 2 = 0: perpendicular.

Why: with the trace 0 the equation has no middle term and is solved by a square root. The eigenvalues 5 and −5-5 say what the matrix does: it stretches the direction (2,1)(2, 1) by 5 and flips the perpendicular direction.

Problem 10: A singular matrix

Find the eigenvalues and eigenvectors of A=[4−2−21]A = \mat{4 & -2 \\ -2 & 1}. What does the matrix do to the plane?

Idea. A determinant 0 gives an eigenvalue 0 (Parts 3 and 5).

tr⁡(A)=5,det⁡(A)=4−4=0λ2−5λ=0λ=0orλ=5\begin{aligned} \tr(A) &= 5, \quad \det(A) = 4 - 4 = 0 \\ \lambda^2 - 5\lambda &= 0 \\ \lambda = 0 \quad &\text{or} \quad \lambda = 5 \end{aligned}
λ=0:4x−2y=0  ⇒  y=2xv=(1,2)λ=5:A−5I=[−1−2−2−4]−x−2y=0  ⇒  x=−2yv=(2,−1)\begin{aligned} \lambda = 0: \quad & 4x - 2y = 0 \;\Rightarrow\; y = 2x \\ & \vv = (1, 2) \\[8pt] \lambda = 5: \quad & A - 5I = \mat{-1 & -2 \\ -2 & -4} \\[4pt] & -x - 2y = 0 \;\Rightarrow\; x = -2y \\ & \vv = (2, -1) \end{aligned}

The checks:

A (1,2)=(4−4,  −2+2)=(0,0)A (2,−1)=(8+2,  −4−1)=(10,−5)=5 (2,−1)\begin{aligned} A\,(1, 2) &= (4 - 4,\; -2 + 2) = (0, 0) \\[4pt] A\,(2, -1) &= (8 + 2,\; -4 - 1) = (10, -5) \\ &= 5\,(2, -1) \end{aligned}

Why: the matrix squeezes the plane flat onto the line of (2,−1)(2, -1) and stretches by 5 along that line. Everything in the direction (1,2)(1, 2) is sent to zero. A determinant 0 always shows up as an eigenvalue 0.

Problem 11: A repeated eigenvalue

Find the eigenvalues and all directions of eigenvectors of A=[3103]A = \mat{3 & 1 \\ 0 & 3}. Compare with the matrix [3003]\mat{3 & 0 \\ 0 & 3}.

Idea. When the characteristic equation has one solution twice, look at A−λIA - \lambda I to see how many directions there are (Part 5).

det⁡(A−λI)=(3−λ)2=0,λ=3 twiceA−3I=[0100]  ⇒  y=0,v=(1,0)\begin{aligned} \det(A - \lambda I) &= (3 - \lambda)^2 = 0, \quad \lambda = 3 \text{ twice} \\[6pt] A - 3I &= \mat{0 & 1 \\ 0 & 0} \;\Rightarrow\; y = 0, \quad \vv = (1, 0) \end{aligned}

Only one direction. For the second matrix:

[3003]−3I=[0000]\mat{3 & 0 \\ 0 & 3} - 3I = \mat{0 & 0 \\ 0 & 0}

Every vector solves this system: all directions.

Why: the two matrices have the same eigenvalue, the same trace and the same determinant, but not the same eigenvectors. [3103]\mat{3 & 1 \\ 0 & 3} has too few eigenvectors and cannot be diagonalized (Part 7).

Problem 12: No real eigenvalues

Show that A=[1−111]A = \mat{1 & -1 \\ 1 & 1} has no real eigenvalues. What does that say about the matrix?

Idea. The sign of tr⁡(A)2−4det⁡(A)\tr(A)^2 - 4\det(A) decides (Part 5).

tr⁡(A)=2,det⁡(A)=1+1=2λ2−2λ+2=0tr⁡(A)2−4det⁡(A)=4−8=−4\begin{aligned} \tr(A) &= 2, \quad \det(A) = 1 + 1 = 2 \\ \lambda^2 - 2\lambda + 2 &= 0 \\[4pt] \tr(A)^2 - 4\det(A) &= 4 - 8 = -4 \end{aligned}

The number is negative: the equation has no real solution.

Why: no vector keeps its direction. In the last lesson this matrix turned every vector to the left by half a right angle and made it longer: a rotation with a stretch, and a rotation leaves no direction where it was.

Problem 13: Four multiplications at once

B=[4123]B = \mat{4 & 1 \\ 2 & 3} has the eigenvector (1,1)(1, 1) with the eigenvalue 5 and the eigenvector (1,−2)(1, -2) with the eigenvalue 2. Find B4uB^4 \vu for u=(3,0)\vu = (3, 0) without calculating B4B^4.

Idea. Write u\vu as a combination of the eigenvectors. The matrix multiplies each part by its eigenvalue, four times (Part 6).

u=(3,0)=2 (1,1)+1 (1,−2)B4u=2×54 (1,1)+1×24 (1,−2)=1250 (1,1)+16 (1,−2)=(1250+16,  1250−32)=(1266,1218)\begin{aligned} \vu = (3, 0) &= 2\,(1, 1) + 1\,(1, -2) \\[6pt] B^4 \vu &= 2 \times 5^4\,(1, 1) + 1 \times 2^4\,(1, -2) \\ &= 1250\,(1, 1) + 16\,(1, -2) \\ &= (1250 + 16,\; 1250 - 32) \\ &= (1266, 1218) \end{aligned}

The check of the first line: 2+1=32 + 1 = 3 and 2−2=02 - 2 = 0.

Why: the numbers 2 and 1 of the combination come from two small equations, c1+c2=3c_1 + c_2 = 3 and c1−2c2=0c_1 - 2c_2 = 0. After four steps the part of the dominant eigenvalue 5 is already about 78 times as large as the other part (1250 against 16).

Problem 14: Eigenvalues of related matrices

A matrix AA has the eigenvalues 2 and 5. Give the eigenvalues of A2A^2, A−1A\inv, 3A3A, A+2IA + 2I and ATA\T, and the determinant of A2A^2.

Idea. The rules of Part 6: the eigenvectors stay, the eigenvalues change in a simple way.

A2:22=4   and   52=25A−1:12=0.5   and   15=0.23A:6   and   15A+2I:4   and   7AT:2   and   5det⁡(A2)=4×25=100\begin{aligned} A^2: \quad & 2^2 = 4 \;\text{ and }\; 5^2 = 25 \\ A\inv: \quad & \tfrac{1}{2} = 0.5 \;\text{ and }\; \tfrac{1}{5} = 0.2 \\ 3A: \quad & 6 \;\text{ and }\; 15 \\ A + 2I: \quad & 4 \;\text{ and }\; 7 \\ A\T: \quad & 2 \;\text{ and }\; 5 \\[6pt] \det(A^2) &= 4 \times 25 = 100 \end{aligned}

Why: none of these answers needs the matrix itself. The determinant of A2A^2 is the product of its eigenvalues. It is also det⁡(A)2=102\det(A)^2 = 10^2, as the rule det⁡(AB)=det⁡(A)×det⁡(B)\det(AB) = \det(A) \times \det(B) of the last lesson says.

Problem 15: A diagonalization

Diagonalize A=[1232]A = \mat{1 & 2 \\ 3 & 2} (Problems 3 and 4): give PP and DD, and check that AP=PDAP = PD.

Idea. The eigenvectors go into the columns of PP, the eigenvalues in the same order onto the diagonal of DD (Part 7).

P=[213−1],D=[400−1]P = \mat{2 & 1 \\ 3 & -1}, \quad D = \mat{4 & 0 \\ 0 & -1}
AP=[1×2+2×31×1+2×(−1)3×2+2×33×1+2×(−1)]=[8−1121]PD=[2×41×(−1)3×4−1×(−1)]=[8−1121]\begin{aligned} AP &= \mat{1 \times 2 + 2 \times 3 & 1 \times 1 + 2 \times (-1) \\ 3 \times 2 + 2 \times 3 & 3 \times 1 + 2 \times (-1)} \\[4pt] &= \mat{8 & -1 \\ 12 & 1} \\[8pt] PD &= \mat{2 \times 4 & 1 \times (-1) \\ 3 \times 4 & -1 \times (-1)} = \mat{8 & -1 \\ 12 & 1} \end{aligned}

Why: checking AP=PDAP = PD avoids the inverse of PP. The first column of PDPD is 4 times the first eigenvector and the second column is −1-1 times the second eigenvector: exactly what AA does to them.

Problem 16: The error of a model

The true values are y=(20,14,9,30,12)\vy = (20, 14, 9, 30, 12) and a model predicts y^=(18,15,9,24,13)\hat{\vy} = (18, 15, 9, 24, 13). Find the MAE, the MSE, the RMSE and the largest error.

Idea. All four are norms of the error vector r=y−y^\vr = \vy - \hat{\vy} (Part 11).

r=(2,−1,0,6,−1)∥r∥1=2+1+0+6+1=10MAE=105=2∥r∥22=4+1+0+36+1=42MSE=425=8.4RMSE=8.4≈2.90∥r∥∞=6\begin{aligned} \vr &= (2, -1, 0, 6, -1) \\[4pt] \norm{\vr}_1 &= 2 + 1 + 0 + 6 + 1 = 10 \\ \text{MAE} &= \tfrac{10}{5} = 2 \\[4pt] \norm{\vr}_2^2 &= 4 + 1 + 0 + 36 + 1 = 42 \\ \text{MSE} &= \tfrac{42}{5} = 8.4 \\[4pt] \text{RMSE} &= \sqrt{8.4} \approx 2.90 \\[4pt] \norm{\vr}_\infty &= 6 \end{aligned}

Why: one bad prediction, the error 6, is 60 percent of the L1 norm but 86 percent of the squared L2 norm (36 of 42). The RMSE has the same unit as the data, so it can be compared with the MAE: here it is clearly larger, a sign that the errors are uneven.

Problem 17: The nearest neighbour

Which of the points A=(5,4)A = (5, 4) and B=(1,7)B = (1, 7) is nearer to q=(1,1)\vq = (1, 1): with the Manhattan distance, with the Euclidean distance and with the Chebyshev distance?

Idea. Three norms of the two differences (Parts 10 and 11).

The distances from q to A and to B
DifferenceL1L2max
A−q=(4,3)A - \vq = (4, 3)716+9=5\sqrt{16 + 9} = 54
B−q=(0,6)B - \vq = (0, 6)666

With the Manhattan distance BB is nearer (6<76 < 7). With the Euclidean distance AA is nearer (5<65 < 6), and with the Chebyshev distance too (4<64 < 6).

Why: BB differs from q\vq in one coordinate only, so all its norms are equal. AA differs in both, and the L1 norm adds the two differences in full. A k-Nearest Neighbors model with the Manhattan distance and one with the Euclidean distance would choose different neighbours here.

Problem 18: Two penalties, two opinions

Two models fit the data equally well. Their weights are w=(4,0,0)\vw = (4, 0, 0) and u=(2,2,2)\vu = (2, 2, 2). Which model does a Lasso penalty prefer, and which a Ridge penalty?

Idea. Lasso uses the L1 norm of the weights, Ridge the squared L2 norm. The smaller penalty wins (Part 11).

The two penalties for the two models
Penaltyw=(4,0,0)\vw = (4, 0, 0)u=(2,2,2)\vu = (2, 2, 2)Smaller
L1 norm (Lasso)46w\vw
squared L2 norm (Ridge)1612u\vu

Lasso prefers w\vw, Ridge prefers u\vu.

Why: the L1 norm likes one large weight and many zeros: a sparse model. The squared L2 norm dislikes a single large weight (16) and prefers the weight to be spread over several features (4+4+44 + 4 + 4). The same two models, the opposite verdict.

Level 3: Putting ideas together

Problem 19: A 3 x 3 matrix

Find the eigenvalues of A=[210030045]A = \mat{2 & 1 & 0 \\ 0 & 3 & 0 \\ 0 & 4 & 5}, and an eigenvector of the middle eigenvalue.

Idea. The determinant of A−λIA - \lambda I along the first row (Part 5), then a system of three equations for the eigenvector.

det⁡(A−λI)=(2−λ)det⁡[3−λ045−λ]−1det⁡[0005−λ]+0=(2−λ)(3−λ)(5−λ)−1×0\begin{aligned} \det(A - \lambda I) = {} & (2 - \lambda) \det \mat{3 - \lambda & 0 \\ 4 & 5 - \lambda} \\[4pt] & - 1 \det \mat{0 & 0 \\ 0 & 5 - \lambda} + 0 \\[6pt] = {} & (2 - \lambda)(3 - \lambda)(5 - \lambda) - 1 \times 0 \end{aligned}

The eigenvalues are 2, 3 and 5. The check: 2+3+5=10=tr⁡(A)2 + 3 + 5 = 10 = \tr(A). For the middle one:

A−3I=[−110000042]−x+y=0⇒y=x4y+2z=0⇒z=−2yx=1:v=(1,1,−2)\begin{aligned} A - 3I &= \mat{-1 & 1 & 0 \\ 0 & 0 & 0 \\ 0 & 4 & 2} \\[6pt] -x + y = 0 \quad &\Rightarrow \quad y = x \\ 4y + 2z = 0 \quad &\Rightarrow \quad z = -2y \\ x = 1: \quad & \vv = (1, 1, -2) \end{aligned}

The check: A (1,1,−2)=(2+1,  3,  4−10)=(3,3,−6)=3 (1,1,−2)A\,(1, 1, -2) = (2 + 1,\; 3,\; 4 - 10) = (3, 3, -6) = 3\,(1, 1, -2).

Why: the eigenvalues are the diagonal entries although the matrix is not triangular: the zeros made the other parts of the determinant vanish. In A−3IA - 3I one row is zero, as a singular matrix must show somewhere; the two other rows give the eigenvector.

Problem 20: Orthonormal eigenvectors

The symmetric matrix A=[533−3]A = \mat{5 & 3 \\ 3 & -3} has the eigenvalues 6 and −4-4. Find orthonormal eigenvectors, write the matrices QQ and DD, and check that QDQT=AQ D Q\T = A.

Idea. Find the eigenvectors, divide each by its length, and put them into the columns of QQ (Part 8).

λ=6:A−6I=[−133−9]−x+3y=0,v=(3,1)length 9+1=10λ=−4:A+4I=[9331]9x+3y=0,v=(1,−3)length 1+9=10\begin{aligned} \lambda = 6: \quad & A - 6I = \mat{-1 & 3 \\ 3 & -9} \\[4pt] & -x + 3y = 0, \quad \vv = (3, 1) \\ & \text{length } \sqrt{9 + 1} = \sqrt{10} \\[8pt] \lambda = -4: \quad & A + 4I = \mat{9 & 3 \\ 3 & 1} \\[4pt] & 9x + 3y = 0, \quad \vv = (1, -3) \\ & \text{length } \sqrt{1 + 9} = \sqrt{10} \end{aligned}

The dot product is (3,1)⋅(1,−3)=3−3=0(3, 1) \cdot (1, -3) = 3 - 3 = 0.

Q=110[311−3],D=[600−4]Q = \frac{1}{\sqrt{10}} \mat{3 & 1 \\ 1 & -3}, \quad D = \mat{6 & 0 \\ 0 & -4}
QDQT=110[311−3][600−4][311−3]=110[18−4612][311−3]=110[503030−30]=[533−3]\begin{aligned} Q D Q\T &= \frac{1}{10} \mat{3 & 1 \\ 1 & -3} \mat{6 & 0 \\ 0 & -4} \mat{3 & 1 \\ 1 & -3} \\[6pt] &= \frac{1}{10} \mat{18 & -4 \\ 6 & 12} \mat{3 & 1 \\ 1 & -3} \\[6pt] &= \frac{1}{10} \mat{50 & 30 \\ 30 & -30} = \mat{5 & 3 \\ 3 & -3} \end{aligned}

Why: the two factors 110\tfrac{1}{\sqrt{10}} of QQ and of QTQ\T multiply to 110\tfrac{1}{10}, so no root is left in the calculation. Because QQ is orthogonal, its transpose did the work of the inverse.

Problem 21: A power of a matrix

Find A4A^4 for A=[1232]A = \mat{1 & 2 \\ 3 & 2} with the diagonalization of Problem 15.

Idea. A4=PD4P−1A^4 = P D^4 P\inv, and the power of a diagonal matrix is the power of its entries (Part 7).

det⁡(P)=2×(−1)−1×3=−5P−1=1−5[−1−1−32]=15[113−2]D4=[4400(−1)4]=[256001]PD4=[5121768−1]\begin{aligned} \det(P) &= 2 \times (-1) - 1 \times 3 = -5 \\[6pt] P\inv &= \frac{1}{-5} \mat{-1 & -1 \\ -3 & 2} = \frac{1}{5} \mat{1 & 1 \\ 3 & -2} \\[6pt] D^4 &= \mat{4^4 & 0 \\ 0 & (-1)^4} = \mat{256 & 0 \\ 0 & 1} \\[6pt] P D^4 &= \mat{512 & 1 \\ 768 & -1} \end{aligned}
A4=PD4P−1=15[512+3512−2768−3768+2]=15[515510765770]=[103102153154]\begin{aligned} A^4 = P D^4 P\inv &= \frac{1}{5} \mat{512 + 3 & 512 - 2 \\ 768 - 3 & 768 + 2} \\[6pt] &= \frac{1}{5} \mat{515 & 510 \\ 765 & 770} = \mat{103 & 102 \\ 153 & 154} \end{aligned}

Why: three matrix multiplications are replaced by the fourth powers of two numbers. A check with little effort: A2=[76910]A^2 = \mat{7 & 6 \\ 9 & 10}, and A2A^2 times A2A^2 gives the same matrix.

Problem 22: Principal components

The covariance matrix of a data set with two features is C=[4227]C = \mat{4 & 2 \\ 2 & 7}. Find the two principal directions and the share of the variance that the first one carries. Then give the new value of the data point (3,4)(3, 4) on the first direction.

Idea. The principal directions are the eigenvectors of CC, the largest eigenvalue first. The new value of a point is its scalar projection onto the unit vector of the direction (Part 9, and Topic 13).

tr⁡(C)=11,det⁡(C)=28−4=24λ2−11λ+24=0(λ−8)(λ−3)=0\begin{aligned} \tr(C) &= 11, \quad \det(C) = 28 - 4 = 24 \\ \lambda^2 - 11\lambda + 24 &= 0 \\ (\lambda - 8)(\lambda - 3) &= 0 \end{aligned}
λ=8:C−8I=[−422−1]−4x+2y=0  ⇒  y=2x,v=(1,2)λ=3:C−3I=[1224]x+2y=0,v=(2,−1)\begin{aligned} \lambda = 8: \quad & C - 8I = \mat{-4 & 2 \\ 2 & -1} \\[4pt] & -4x + 2y = 0 \;\Rightarrow\; y = 2x, \quad \vv = (1, 2) \\[8pt] \lambda = 3: \quad & C - 3I = \mat{1 & 2 \\ 2 & 4} \\[4pt] & x + 2y = 0, \quad \vv = (2, -1) \end{aligned}
share:88+3=811≈0.727unit vector:15 (1,2)new value:(3,4)⋅(1,2)5=115≈4.92\begin{aligned} \text{share:} \quad & \frac{8}{8 + 3} = \frac{8}{11} \approx 0.727 \\[6pt] \text{unit vector:} \quad & \tfrac{1}{\sqrt{5}}\,(1, 2) \\[6pt] \text{new value:} \quad & \frac{(3, 4) \cdot (1, 2)}{\sqrt{5}} = \frac{11}{\sqrt{5}} \approx 4.92 \end{aligned}

The share of the first direction, about 73 percent of the variance, its unit vector, and the new value of the point (3,4)(3, 4) are in the three lines above.

Why: the first principal direction belongs to the largest eigenvalue. The second feature has the larger variance (7 against 4), and the direction (1,2)(1, 2) leans towards it. The new value is a scalar projection, the tool of Topic 13.

Problem 23: The long run

Every year 30 percent of the customers of phone company 1 change to company 2, and 40 percent of the customers of company 2 change to company 1. Write the matrix PP of the changes and find the shares in the long run.

Idea. The long-run shares are the eigenvector of the eigenvalue 1, scaled so that its components add up to 1 (Part 9).

P=[0.70.40.30.6]P = \mat{0.7 & 0.4 \\ 0.3 & 0.6}

The first column says where the customers of company 1 go, the second column where the customers of company 2 go.

tr⁡(P)=1.3det⁡(P)=0.42−0.12=0.3λ2−1.3λ+0.3=0(λ−1)(λ−0.3)=0\begin{aligned} \tr(P) &= 1.3 \\ \det(P) &= 0.42 - 0.12 = 0.3 \\[6pt] \lambda^2 - 1.3\lambda + 0.3 &= 0 \\ (\lambda - 1)(\lambda - 0.3) &= 0 \end{aligned}
λ=1:P−I=[−0.30.40.3−0.4]−0.3x+0.4y=0  ⇒  3x=4yv=(4,3)shares:17 (4,3)=(47,37)≈(0.571,0.429)\begin{aligned} \lambda = 1: \quad & P - I = \mat{-0.3 & 0.4 \\ 0.3 & -0.4} \\[4pt] & -0.3x + 0.4y = 0 \;\Rightarrow\; 3x = 4y \\ & \vv = (4, 3) \\[6pt] \text{shares:} \quad & \tfrac{1}{7}\,(4, 3) = \left(\tfrac{4}{7}, \tfrac{3}{7}\right) \\ & \approx (0.571, 0.429) \end{aligned}

The check: P(47,37)=(2.8+1.27,  1.2+1.87)=(47,37)P \left(\tfrac{4}{7}, \tfrac{3}{7}\right) = \left(\tfrac{2.8 + 1.2}{7},\; \tfrac{1.2 + 1.8}{7}\right) = \left(\tfrac{4}{7}, \tfrac{3}{7}\right).

Why: dividing by 7 is an L1 normalization: shares must add up to 1. The second eigenvalue, 0.3, is small, so the shares settle quickly: every year the distance to the final shares shrinks to 30 percent of what it was.

Problem 24: Positive definite or not?

Which of the symmetric matrices A=[2−1−12]A = \mat{2 & -1 \\ -1 & 2} and B=[2332]B = \mat{2 & 3 \\ 3 & 2} is positive definite? Check your answer with the number xTMx\vx\T M \vx for x=(1,1)\vx = (1, 1) and for x=(1,−1)\vx = (1, -1).

Idea. A symmetric matrix is positive definite when all its eigenvalues are positive. Then xTMx\vx\T M \vx is positive for every vector that is not zero (Part 8).

A:tr⁡=4,  det⁡=4−1=3λ2−4λ+3=0,λ=1 and λ=3B:tr⁡=4,  det⁡=4−9=−5λ2−4λ−5=0,λ=5 and λ=−1\begin{aligned} A: \quad & \tr = 4, \;\det = 4 - 1 = 3 \\ & \lambda^2 - 4\lambda + 3 = 0, \quad \lambda = 1 \text{ and } \lambda = 3 \\[6pt] B: \quad & \tr = 4, \;\det = 4 - 9 = -5 \\ & \lambda^2 - 4\lambda - 5 = 0, \quad \lambda = 5 \text{ and } \lambda = -1 \end{aligned}

AA is positive definite, BB is not. With x=(a,b)\vx = (a, b):

xTAx=2a2−2ab+2b2(1,1):2−2+2=2(1,−1):2+2+2=6xTBx=2a2+6ab+2b2(1,1):2+6+2=10(1,−1):2−6+2=−2\begin{aligned} \vx\T A \vx &= 2a^2 - 2ab + 2b^2 \\ (1, 1): \quad & 2 - 2 + 2 = 2 \\ (1, -1): \quad & 2 + 2 + 2 = 6 \\[6pt] \vx\T B \vx &= 2a^2 + 6ab + 2b^2 \\ (1, 1): \quad & 2 + 6 + 2 = 10 \\ (1, -1): \quad & 2 - 6 + 2 = -2 \end{aligned}

Why: for a 2 x 2 matrix a negative determinant already decides the question: the product of the eigenvalues is negative, so one of them is. The vector (1,−1)(1, -1) is the eigenvector of BB with the eigenvalue −1-1, and that is exactly where xTBx\vx\T B \vx becomes negative.

Problem 25: Frobenius norm and singular values

For A=[22−11]A = \mat{2 & 2 \\ -1 & 1} find the Frobenius norm and the singular values, and check that the squares of the singular values add up to the square of the Frobenius norm.

Idea. The singular values are the roots of the eigenvalues of ATAA\T A (Part 11).

∥A∥F=4+4+1+1=10≈3.16ATA=[2−121][22−11]=[4+14−14−14+1]=[5335]tr⁡=10,det⁡=25−9=16λ2−10λ+16=0λ=8orλ=2\begin{aligned} \norm{A}_F &= \sqrt{4 + 4 + 1 + 1} = \sqrt{10} \approx 3.16 \\[6pt] A\T A &= \mat{2 & -1 \\ 2 & 1} \mat{2 & 2 \\ -1 & 1} \\[4pt] &= \mat{4 + 1 & 4 - 1 \\ 4 - 1 & 4 + 1} = \mat{5 & 3 \\ 3 & 5} \\[6pt] \tr &= 10, \quad \det = 25 - 9 = 16 \\ \lambda^2 - 10\lambda + 16 &= 0 \\ \lambda = 8 \quad &\text{or} \quad \lambda = 2 \end{aligned}

The singular values are 8≈2.83\sqrt{8} \approx 2.83 and 2≈1.41\sqrt{2} \approx 1.41. The check: 8+2=10=∥A∥F28 + 2 = 10 = \norm{A}_F^2.

Why: ATAA\T A is symmetric, so its eigenvalues are real, and they are never negative, so the roots exist. The sum of the eigenvalues of ATAA\T A is its trace, and the trace of ATAA\T A is the sum of the squares of all entries of AA: that is why the check must work.

Problem 26: A matrix from its eigenvectors

A matrix AA has the eigenvalue 2 with the eigenvector (1,1)(1, 1) and the eigenvalue 3 with the eigenvector (1,2)(1, 2). Find AA.

Idea. Read the diagonalization backwards: A=PDP−1A = P D P\inv (Part 7).

P=[1112],D=[2003]det⁡(P)=2−1=1,P−1=[2−1−11]PD=[2326]\begin{aligned} P &= \mat{1 & 1 \\ 1 & 2}, \quad D = \mat{2 & 0 \\ 0 & 3} \\[6pt] \det(P) &= 2 - 1 = 1, \quad P\inv = \mat{2 & -1 \\ -1 & 1} \\[6pt] PD &= \mat{2 & 3 \\ 2 & 6} \end{aligned}
A=PDP−1=[2326][2−1−11]=[4−3−2+34−6−2+6]=[11−24]\begin{aligned} A = P D P\inv &= \mat{2 & 3 \\ 2 & 6} \mat{2 & -1 \\ -1 & 1} \\[6pt] &= \mat{4 - 3 & -2 + 3 \\ 4 - 6 & -2 + 6} = \mat{1 & 1 \\ -2 & 4} \end{aligned}

The checks: A (1,1)=(2,2)A\,(1, 1) = (2, 2) and A (1,2)=(3,6)A\,(1, 2) = (3, 6). Also tr⁡(A)=5=2+3\tr(A) = 5 = 2 + 3 and det⁡(A)=4+2=6=2×3\det(A) = 4 + 2 = 6 = 2 \times 3.

Why: the eigenvalues and the eigenvectors together contain the whole matrix: the diagonalization, read backwards, builds it again. Four checks confirm the result.

14Practice problems

Forty problems in three levels. Solve each one on paper, type your result into the answer boxes and press Check. Under every problem there is a hint and a full solution: look at the hint only when you are stuck, and at the solution only after you have an answer of your own. For an eigenvector every multiple of the vector given is right too.

Level 1: The basics

  1. 1.Is v=(2,1)\vv = (2, 1) an eigenvector of A=[122−2]A = \mat{1 & 2 \\ 2 & -2}? If it is, give the eigenvalue.

    Show a hint
    Multiply AvA\vv and compare the result with v\vv.
    Show one possible solution

    Answer: Yes, with the eigenvalue 2.

    Av=[1×2+2×12×2−2×1]=[42]=2[21]A\vv = \mat{1 \times 2 + 2 \times 1 \\ 2 \times 2 - 2 \times 1} = \mat{4 \\ 2} = 2 \mat{2 \\ 1}

    Both components were multiplied by 2, so the vector kept its direction.

  2. 2.Is v=(1,3)\vv = (1, 3) an eigenvector of A=[2103]A = \mat{2 & 1 \\ 0 & 3}?

    Show a hint
    Is AvA\vv a multiple of v\vv? Compare the two components.
    Show one possible solution

    Answer: No.

    Av=[2+30+9]=[59]A\vv = \mat{2 + 3 \\ 0 + 9} = \mat{5 \\ 9}

    The first component was multiplied by 5 and the second by 3, so the vector was turned. (The eigenvectors of this matrix are (1,0)(1, 0) with the eigenvalue 2 and (1,1)(1, 1) with the eigenvalue 3.)

  3. 3.Give the eigenvalues and the eigenvectors of the diagonal matrix [700−3]\mat{7 & 0 \\ 0 & -3}.

    Show a hint
    For a diagonal matrix nothing has to be calculated.
    Show one possible solution

    Answer: 7 with the eigenvector (1,0)(1, 0), and −3-3 with the eigenvector (0,1)(0, 1).

    The matrix sends (1,0)(1, 0) to (7,0)=7 (1,0)(7, 0) = 7\,(1, 0) and (0,1)(0, 1) to (0,−3)=−3 (0,1)(0, -3) = -3\,(0, 1). The eigenvalues of a diagonal matrix are its diagonal entries.

  4. 4.Give the eigenvalues of [−1406]\mat{-1 & 4 \\ 0 & 6}.

    Show a hint
    The matrix is triangular.
    Show one possible solution

    Answer: −1-1 and 6.

    det⁡(A−λI)=(−1−λ)(6−λ)−4×0\det(A - \lambda I) = (-1 - \lambda)(6 - \lambda) - 4 \times 0

    It is 0 for λ=−1\lambda = -1 and for λ=6\lambda = 6: the diagonal entries. Check: the sum is 5, the trace; the product is −6-6, the determinant.

  5. 5.Find the trace and the determinant of A=[351−2]A = \mat{3 & 5 \\ 1 & -2}, and write its characteristic equation.

    Show a hint
    The equation is λ2−tr⁡(A) λ+det⁡(A)=0\lambda^2 - \tr(A)\,\lambda + \det(A) = 0.
    Show one possible solution

    Answer: tr⁡(A)=1\tr(A) = 1, det⁡(A)=−11\det(A) = -11, and the equation is λ2−λ−11=0\lambda^2 - \lambda - 11 = 0.

    tr⁡(A)=3+(−2)=1det⁡(A)=3×(−2)−5×1=−11λ2−λ−11=0\begin{aligned} \tr(A) &= 3 + (-2) = 1 \\ \det(A) &= 3 \times (-2) - 5 \times 1 = -11 \\ \lambda^2 - \lambda - 11 &= 0 \end{aligned}

    Minus the trace, plus the determinant: λ2−1λ+(−11)=0\lambda^2 - 1\lambda + (-11) = 0.

  6. 6.Find the eigenvalues of A=[2341]A = \mat{2 & 3 \\ 4 & 1}.

    Show a hint
    The trace is 3 and the determinant is −10-10.
    Show one possible solution

    Answer: 5 and −2-2.

    tr⁡(A)=3,det⁡(A)=2−12=−10λ2−3λ−10=0(λ−5)(λ+2)=0\begin{aligned} \tr(A) &= 3, \quad \det(A) = 2 - 12 = -10 \\ \lambda^2 - 3\lambda - 10 &= 0 \\ (\lambda - 5)(\lambda + 2) &= 0 \end{aligned}

    Check: 5+(−2)=35 + (-2) = 3 and 5×(−2)=−105 \times (-2) = -10.

  7. 7.Find the eigenvalues of A=[5412]A = \mat{5 & 4 \\ 1 & 2}.

    Show a hint
    Look for two numbers with the sum 7 and the product 6.
    Show one possible solution

    Answer: 6 and 1.

    tr⁡(A)=7,det⁡(A)=10−4=6λ2−7λ+6=0(λ−6)(λ−1)=0\begin{aligned} \tr(A) &= 7, \quad \det(A) = 10 - 4 = 6 \\ \lambda^2 - 7\lambda + 6 &= 0 \\ (\lambda - 6)(\lambda - 1) &= 0 \end{aligned}
  8. 8.A 2 x 2 matrix has the eigenvalues 3 and −1-1. What are its trace and its determinant? Is the matrix invertible?

    Show a hint
    The sum and the product of the eigenvalues.
    Show one possible solution

    Answer: The trace is 2 and the determinant is −3-3. The matrix is invertible.

    The trace is 3+(−1)=23 + (-1) = 2, the determinant is 3×(−1)=−33 \times (-1) = -3. The determinant is not 0 (no eigenvalue is 0), so the matrix has an inverse.

  9. 9.Find the L1 norm, the L2 norm and the max norm of x=(−5,12)\vx = (-5, 12).

    Show a hint
    Absolute values for the L1 norm and the max norm, squares for the L2 norm.
    Show one possible solution

    Answer: 17, 13 and 12.

    ∥x∥1=5+12=17∥x∥2=25+144=169=13∥x∥∞=12\begin{aligned} \norm{\vx}_1 &= 5 + 12 = 17 \\ \norm{\vx}_2 &= \sqrt{25 + 144} = \sqrt{169} = 13 \\ \norm{\vx}_\infty &= 12 \end{aligned}
  10. 10.Find the L1 norm, the L2 norm and the max norm of x=(1,−2,2,4)\vx = (1, -2, 2, 4).

    Show a hint
    Four components, the same three recipes.
    Show one possible solution

    Answer: 9, 5 and 4.

    ∥x∥1=1+2+2+4=9∥x∥2=1+4+4+16=25=5∥x∥∞=4\begin{aligned} \norm{\vx}_1 &= 1 + 2 + 2 + 4 = 9 \\ \norm{\vx}_2 &= \sqrt{1 + 4 + 4 + 16} = \sqrt{25} = 5 \\ \norm{\vx}_\infty &= 4 \end{aligned}

    The order 4≤5≤94 \le 5 \le 9 is as it must be.

  11. 11.Find the Manhattan, the Euclidean and the Chebyshev distance between a=(2,3)\va = (2, 3) and b=(10,9)\vb = (10, 9).

    Show a hint
    First the difference of the two vectors, then its three norms.
    Show one possible solution

    Answer: 14, 10 and 8.

    a−b=(−8,−6)Manhattan:8+6=14Euclidean:64+36=100=10Chebyshev:8\begin{aligned} \va - \vb &= (-8, -6) \\ \text{Manhattan:} \quad & 8 + 6 = 14 \\ \text{Euclidean:} \quad & \sqrt{64 + 36} = \sqrt{100} = 10 \\ \text{Chebyshev:} \quad & 8 \end{aligned}
  12. 12.For x=(2,−1,2)\vx = (2, -1, 2) find ∥−4x∥2\norm{-4\vx}_2 and ∥−4x∥1\norm{-4\vx}_1.

    Show a hint
    Rule 2 of a norm: a number comes out of the norm as its absolute value.
    Show one possible solution

    Answer: 12 and 20.

    ∥x∥2=4+1+4=3∥−4x∥2=4×3=12∥x∥1=2+1+2=5∥−4x∥1=4×5=20\begin{aligned} \norm{\vx}_2 &= \sqrt{4 + 1 + 4} = 3 \\ \norm{-4\vx}_2 &= 4 \times 3 = 12 \\[6pt] \norm{\vx}_1 &= 2 + 1 + 2 = 5 \\ \norm{-4\vx}_1 &= 4 \times 5 = 20 \end{aligned}

    Directly: −4x=(−8,4,−8)-4\vx = (-8, 4, -8), with 64+16+64=144=12\sqrt{64 + 16 + 64} = \sqrt{144} = 12 and 8+4+8=208 + 4 + 8 = 20.

  13. 13.Four words appear 3, 1, 4 and 2 times in a text. Normalize the vector of counts (3,1,4,2)(3, 1, 4, 2) with the L1 norm.

    Show a hint
    Divide every component by the sum of the components.
    Show one possible solution

    Answer: (0.3,0.1,0.4,0.2)(0.3, 0.1, 0.4, 0.2).

    The L1 norm is 3+1+4+2=103 + 1 + 4 + 2 = 10. Dividing gives (0.3,0.1,0.4,0.2)(0.3, 0.1, 0.4, 0.2): the share of every word. The shares add up to 1.

Level 2: Eigenvalues, eigenvectors and norms at work

  1. 14.Find the eigenvectors of A=[5412]A = \mat{5 & 4 \\ 1 & 2}. Its eigenvalues are 6 and 1 (Problem 7).

    Show a hint
    For each eigenvalue subtract it from the main diagonal and solve one equation.
    Show one possible solution

    Answer: (4,1)(4, 1) for the eigenvalue 6 and (1,−1)(1, -1) for the eigenvalue 1 (or any multiples of them).

    λ=6:A−6I=[−141−4]−x+4y=0  ⇒  x=4yv=(4,1)λ=1:A−I=[4411]x+y=0,v=(1,−1)\begin{aligned} \lambda = 6: \quad & A - 6I = \mat{-1 & 4 \\ 1 & -4} \\[4pt] & -x + 4y = 0 \;\Rightarrow\; x = 4y \\ & \vv = (4, 1) \\[8pt] \lambda = 1: \quad & A - I = \mat{4 & 4 \\ 1 & 1} \\[4pt] & x + y = 0, \quad \vv = (1, -1) \end{aligned}

    Check: A (4,1)=(20+4,  4+2)=(24,6)=6 (4,1)A\,(4, 1) = (20 + 4,\; 4 + 2) = (24, 6) = 6\,(4, 1), and A (1,−1)=(5−4,  1−2)=(1,−1)A\,(1, -1) = (5 - 4,\; 1 - 2) = (1, -1).

  2. 15.Find the eigenvalues and the eigenvectors of the symmetric matrix A=[222−1]A = \mat{2 & 2 \\ 2 & -1}. Are the eigenvectors perpendicular?

    Show a hint
    The trace is 1 and the determinant is −6-6.
    Show one possible solution

    Answer: 3 with (2,1)(2, 1), and −2-2 with (1,−2)(1, -2). They are perpendicular.

    λ2−λ−6=0(λ−3)(λ+2)=0λ=3:A−3I=[−122−4]−x+2y=0,v=(2,1)λ=−2:A+2I=[4221]2x+y=0,v=(1,−2)\begin{aligned} \lambda^2 - \lambda - 6 &= 0 \\ (\lambda - 3)(\lambda + 2) &= 0 \\[8pt] \lambda = 3: \quad & A - 3I = \mat{-1 & 2 \\ 2 & -4} \\[4pt] & -x + 2y = 0, \quad \vv = (2, 1) \\[8pt] \lambda = -2: \quad & A + 2I = \mat{4 & 2 \\ 2 & 1} \\[4pt] & 2x + y = 0, \quad \vv = (1, -2) \end{aligned}

    The dot product is 2−2=02 - 2 = 0, as it must be for a symmetric matrix.

  3. 16.Find the eigenvalues and the eigenvectors of A=[6321]A = \mat{6 & 3 \\ 2 & 1}. Is the matrix invertible?

    Show a hint
    The determinant is 0.
    Show one possible solution

    Answer: 0 with (1,−2)(1, -2), and 7 with (3,1)(3, 1). The matrix is not invertible.

    tr⁡(A)=7,det⁡(A)=6−6=0λ2−7λ=0λ=0orλ=7\begin{aligned} \tr(A) &= 7, \quad \det(A) = 6 - 6 = 0 \\ \lambda^2 - 7\lambda &= 0 \\ \lambda = 0 \quad &\text{or} \quad \lambda = 7 \end{aligned}
    λ=0:6x+3y=0  ⇒  y=−2xv=(1,−2)λ=7:A−7I=[−132−6]−x+3y=0,v=(3,1)\begin{aligned} \lambda = 0: \quad & 6x + 3y = 0 \;\Rightarrow\; y = -2x \\ & \vv = (1, -2) \\[8pt] \lambda = 7: \quad & A - 7I = \mat{-1 & 3 \\ 2 & -6} \\[4pt] & -x + 3y = 0, \quad \vv = (3, 1) \end{aligned}

    Check: A (3,1)=(18+3,  6+1)=(21,7)A\,(3, 1) = (18 + 3,\; 6 + 1) = (21, 7). An eigenvalue 0 means that the determinant is 0: there is no inverse.

  4. 17.The matrices A=[5005]A = \mat{5 & 0 \\ 0 & 5} and B=[5105]B = \mat{5 & 1 \\ 0 & 5} both have the eigenvalue 5 twice. Which of them has eigenvectors in every direction?

    Show a hint
    Look at A−5IA - 5I and at B−5IB - 5I.
    Show one possible solution

    Answer: AA. The matrix BB has only the direction (1,0)(1, 0).

    A−5I=[0000]B−5I=[0100]A - 5I = \mat{0 & 0 \\ 0 & 0} \qquad B - 5I = \mat{0 & 1 \\ 0 & 0}

    Every vector solves (A−5I) v=0(A - 5I)\,\vv = \vzero. For BB the first row gives y=0y = 0: only the vectors on the line of (1,0)(1, 0).

  5. 18.Which of the matrices A=[2−51−2]A = \mat{2 & -5 \\ 1 & -2} and B=[1411]B = \mat{1 & 4 \\ 1 & 1} has real eigenvalues? Find them.

    Show a hint
    Look at the sign of tr⁡2−4det⁡\tr^2 - 4\det for each matrix.
    Show one possible solution

    Answer: BB, with the eigenvalues 3 and −1-1. AA has no real eigenvalues.

    A:tr⁡=0,  det⁡=−4+5=1λ2+1=0,0−4=−4<0B:tr⁡=2,  det⁡=1−4=−3λ2−2λ−3=0(λ−3)(λ+1)=0\begin{aligned} A: \quad & \tr = 0, \;\det = -4 + 5 = 1 \\ & \lambda^2 + 1 = 0, \quad 0 - 4 = -4 < 0 \\[6pt] B: \quad & \tr = 2, \;\det = 1 - 4 = -3 \\ & \lambda^2 - 2\lambda - 3 = 0 \\ & (\lambda - 3)(\lambda + 1) = 0 \end{aligned}
  6. 19.A matrix AA has the eigenvector (1,1)(1, 1) with the eigenvalue 2 and the eigenvector (1,−1)(1, -1) with the eigenvalue −1-1. Find AuA\vu and A3uA^3\vu for u=(3,1)\vu = (3, 1).

    Show a hint
    u=2 (1,1)+1 (1,−1)\vu = 2\,(1, 1) + 1\,(1, -1). The matrix multiplies each part by its eigenvalue.
    Show one possible solution

    Answer: Au=(3,5)A\vu = (3, 5) and A3u=(15,17)A^3\vu = (15, 17).

    Au=2×2 (1,1)+1×(−1) (1,−1)=(4,4)+(−1,1)=(3,5)A3u=2×23 (1,1)+1×(−1)3 (1,−1)=(16,16)+(−1,1)=(15,17)\begin{aligned} A\vu &= 2 \times 2\,(1, 1) + 1 \times (-1)\,(1, -1) \\ &= (4, 4) + (-1, 1) = (3, 5) \\[6pt] A^3\vu &= 2 \times 2^3\,(1, 1) + 1 \times (-1)^3\,(1, -1) \\ &= (16, 16) + (-1, 1) = (15, 17) \end{aligned}
  7. 20.A matrix AA has the eigenvalues 4 and −2-2. Give the eigenvalues of A2A^2, of A−1A\inv and of A−3IA - 3I.

    Show a hint
    Square them, take one over them, subtract 3.
    Show one possible solution

    Answer: A2A^2: 16 and 4. A−1A\inv: 0.25 and −0.5-0.5. A−3IA - 3I: 1 and −5-5.

    A2:42=16,(−2)2=4A−1:14=0.25,1−2=−0.5A−3I:4−3=1,−2−3=−5\begin{aligned} A^2: \quad & 4^2 = 16, \quad (-2)^2 = 4 \\ A\inv: \quad & \tfrac{1}{4} = 0.25, \quad \tfrac{1}{-2} = -0.5 \\ A - 3I: \quad & 4 - 3 = 1, \quad -2 - 3 = -5 \end{aligned}

    The eigenvectors are the same in all three cases.

  8. 21.One matrix has the eigenvalues 0 and 3, another one has the eigenvalues 2 and −7-7. Which of them is invertible, and what is its determinant?

    Show a hint
    The determinant is the product of the eigenvalues.
    Show one possible solution

    Answer: The second one. Its determinant is −14-14.

    First matrix: 0×3=00 \times 3 = 0, so it is singular. Second matrix: 2×(−7)=−142 \times (-7) = -14, not 0, so it has an inverse.

  9. 22.Diagonalize A=[5412]A = \mat{5 & 4 \\ 1 & 2} (Problems 7 and 14): give PP and DD, with the larger eigenvalue first, and check that AP=PDAP = PD.
    DD

    Show a hint
    The eigenvectors are the columns of PP, the eigenvalues stand in the same order in DD.
    Show one possible solution

    Answer: P=[411−1]P = \mat{4 & 1 \\ 1 & -1} and D=[6001]D = \mat{6 & 0 \\ 0 & 1}.

    AP=[5×4+4×15×1+4×(−1)1×4+2×11×1+2×(−1)]=[2416−1]PD=[4×61×11×6−1×1]=[2416−1]\begin{aligned} AP &= \mat{5 \times 4 + 4 \times 1 & 5 \times 1 + 4 \times (-1) \\ 1 \times 4 + 2 \times 1 & 1 \times 1 + 2 \times (-1)} \\[4pt] &= \mat{24 & 1 \\ 6 & -1} \\[8pt] PD &= \mat{4 \times 6 & 1 \times 1 \\ 1 \times 6 & -1 \times 1} = \mat{24 & 1 \\ 6 & -1} \end{aligned}

    The two products are equal.

  10. 23.The true values are y=(5,8,6,10)\vy = (5, 8, 6, 10) and a model predicts y^=(7,8,3,9)\hat{\vy} = (7, 8, 3, 9). Find the MAE, the MSE and the largest error.

    Show a hint
    First the error vector r=y−y^\vr = \vy - \hat{\vy}, then its norms.
    Show one possible solution

    Answer: MAE 1.5, MSE 3.5, largest error 3.

    r=(−2,0,3,1)MAE=2+0+3+14=64=1.5MSE=4+0+9+14=144=3.5∥r∥∞=3\begin{aligned} \vr &= (-2, 0, 3, 1) \\ \text{MAE} &= \frac{2 + 0 + 3 + 1}{4} = \frac{6}{4} = 1.5 \\[4pt] \text{MSE} &= \frac{4 + 0 + 9 + 1}{4} = \frac{14}{4} = 3.5 \\[4pt] \norm{\vr}_\infty &= 3 \end{aligned}
  11. 24.Which of the points A=(4,4)A = (4, 4) and B=(7,0)B = (7, 0) is nearer to the origin: with the Manhattan distance, and with the Euclidean distance?

    Show a hint
    Calculate the L1 norm and the L2 norm of both points.
    Show one possible solution

    Answer: Manhattan: BB (7 against 8). Euclidean: AA (about 5.66 against 7).

    L1:A:  4+4=8,B:  7L2:A:  16+16=32≈5.66,B:  7\begin{aligned} \text{L1:} \quad & A: \; 4 + 4 = 8, \qquad B: \; 7 \\ \text{L2:} \quad & A: \; \sqrt{16 + 16} = \sqrt{32} \approx 5.66, \qquad B: \; 7 \end{aligned}

    The two distances disagree about the nearer point.

  12. 25.A model has the weights w=(3,−4,0,0)\vw = (3, -4, 0, 0). Find the penalty of Lasso and the penalty of Ridge for α=0.1\alpha = 0.1. How many weights are not 0?

    Show a hint
    Lasso uses the L1 norm, Ridge the squared L2 norm.
    Show one possible solution

    Answer: 0.7 for Lasso, 2.5 for Ridge. Two weights are not 0.

    ∥w∥1=3+4=7,0.1×7=0.7∥w∥22=9+16=25,0.1×25=2.5\begin{aligned} \norm{\vw}_1 &= 3 + 4 = 7, \quad 0.1 \times 7 = 0.7 \\ \norm{\vw}_2^2 &= 9 + 16 = 25, \quad 0.1 \times 25 = 2.5 \end{aligned}

    The weights 3 and −4-4 are not 0.

  13. 26.A gradient g=(9,12)\vg = (9, 12) is clipped to the L2 norm 5. What is the new vector?

    Show a hint
    Find the L2 norm of g\vg. If it is larger than 5, multiply g\vg by 5 divided by the norm.
    Show one possible solution

    Answer: (3,4)(3, 4).

    ∥g∥2=81+144=225=15>5g×515=13 (9,12)=(3,4)\begin{aligned} \norm{\vg}_2 &= \sqrt{81 + 144} = \sqrt{225} = 15 > 5 \\ \vg \times \tfrac{5}{15} &= \tfrac{1}{3}\,(9, 12) = (3, 4) \end{aligned}

    The new vector has the norm 5 and the direction of g\vg.

  14. 27.Check the triangle inequality in the L1 norm for a=(3,−1)\va = (3, -1) and b=(−2,4)\vb = (-2, 4): give ∥a+b∥1\norm{\va + \vb}_1 and ∥a∥1+∥b∥1\norm{\va}_1 + \norm{\vb}_1.

    Show a hint
    Add the vectors first for the left side.
    Show one possible solution

    Answer: 4 and 10.

    a+b=(1,3),∥a+b∥1=4∥a∥1+∥b∥1=4+6=10\begin{aligned} \va + \vb &= (1, 3), \quad \norm{\va + \vb}_1 = 4 \\ \norm{\va}_1 + \norm{\vb}_1 &= 4 + 6 = 10 \end{aligned}

    Indeed 4≤104 \le 10: the components of a\va and b\vb partly cancel each other.

  15. 28.Find the Frobenius norm of A=[1−224]A = \mat{1 & -2 \\ 2 & 4}.

    Show a hint
    Square all four entries, add, take the root.
    Show one possible solution

    Answer: 5.

    ∥A∥F=1+4+4+16=25=5\norm{A}_F = \sqrt{1 + 4 + 4 + 16} = \sqrt{25} = 5

Level 3: Putting ideas together

  1. 29.Find the eigenvalues of A=[12003000−2]A = \mat{1 & 2 & 0 \\ 0 & 3 & 0 \\ 0 & 0 & -2}, and an eigenvector for λ=3\lambda = 3.

    Show a hint
    The matrix is triangular. For the eigenvector subtract 3 from the diagonal.
    Show one possible solution

    Answer: 1, 3 and −2-2. An eigenvector for 3 is (1,1,0)(1, 1, 0).

    The eigenvalues of a triangular matrix are its diagonal entries.

    A−3I=[−22000000−5]−2x+2y=0⇒y=x−5z=0⇒z=0v=(1,1,0)\begin{aligned} A - 3I &= \mat{-2 & 2 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & -5} \\[6pt] -2x + 2y = 0 \quad &\Rightarrow \quad y = x \\ -5z = 0 \quad &\Rightarrow \quad z = 0 \\ \vv &= (1, 1, 0) \end{aligned}

    Check: A (1,1,0)=(1+2,  3,  0)=(3,3,0)A\,(1, 1, 0) = (1 + 2,\; 3,\; 0) = (3, 3, 0).

  2. 30.Find the eigenvalues of A=[100042021]A = \mat{1 & 0 & 0 \\ 0 & 4 & 2 \\ 0 & 2 & 1}. Is the matrix invertible?

    Show a hint
    The determinant of A−λIA - \lambda I along the first row has only one part.
    Show one possible solution

    Answer: 1, 0 and 5. The matrix is not invertible.

    det⁡(A−λI)=(1−λ) ((4−λ)(1−λ)−4)=(1−λ) (λ2−5λ)=(1−λ) λ (λ−5)\begin{aligned} \det(A - \lambda I) &= (1 - \lambda)\,\big((4 - \lambda)(1 - \lambda) - 4\big) \\ &= (1 - \lambda)\,(\lambda^2 - 5\lambda) \\ &= (1 - \lambda)\,\lambda\,(\lambda - 5) \end{aligned}

    The eigenvalues are 1, 0 and 5. Check: their sum is 6=1+4+16 = 1 + 4 + 1, the trace. One eigenvalue is 0, so the determinant is 0 and there is no inverse.

  3. 31.For the symmetric matrix A=[222−1]A = \mat{2 & 2 \\ 2 & -1} of Problem 15 give orthonormal eigenvectors and the matrices QQ and DD with A=QDQTA = Q D Q\T.

    Show a hint
    Divide each eigenvector by its length, 5\sqrt{5}.
    Show one possible solution

    Answer: 15 (2,1)≈(0.894,0.447)\tfrac{1}{\sqrt{5}}\,(2, 1) \approx (0.894, 0.447) and 15 (1,−2)≈(0.447,−0.894)\tfrac{1}{\sqrt{5}}\,(1, -2) \approx (0.447, -0.894). Q=15[211−2]Q = \tfrac{1}{\sqrt{5}} \mat{2 & 1 \\ 1 & -2} and D=[300−2]D = \mat{3 & 0 \\ 0 & -2}.

    Both eigenvectors have the length 4+1=5\sqrt{4 + 1} = \sqrt{5}. The check:

    QD=15[6−234]QDQT=15[6−234][211−2]=15[12−26+46+43−8]=15[101010−5]=[222−1]\begin{aligned} Q D &= \frac{1}{\sqrt{5}} \mat{6 & -2 \\ 3 & 4} \\[6pt] Q D Q\T &= \frac{1}{5} \mat{6 & -2 \\ 3 & 4} \mat{2 & 1 \\ 1 & -2} \\[6pt] &= \frac{1}{5} \mat{12 - 2 & 6 + 4 \\ 6 + 4 & 3 - 8} \\[6pt] &= \frac{1}{5} \mat{10 & 10 \\ 10 & -5} = \mat{2 & 2 \\ 2 & -1} \end{aligned}
  4. 32.Find A3A^3 for A=[5412]A = \mat{5 & 4 \\ 1 & 2} with the diagonalization of Problem 22.
    A3A^3

    Show a hint
    A3=PD3P−1A^3 = P D^3 P\inv, and det⁡(P)=−5\det(P) = -5.
    Show one possible solution

    Answer: A3=[1731724344]A^3 = \mat{173 & 172 \\ 43 & 44}.

    P−1=1−5[−1−1−14]=15[111−4]D3=[216001],PD3=[8641216−1]A3=15[864+1864−4216−1216+4]=15[865860215220]=[1731724344]\begin{aligned} P\inv &= \frac{1}{-5} \mat{-1 & -1 \\ -1 & 4} = \frac{1}{5} \mat{1 & 1 \\ 1 & -4} \\[6pt] D^3 &= \mat{216 & 0 \\ 0 & 1}, \quad P D^3 = \mat{864 & 1 \\ 216 & -1} \\[6pt] A^3 &= \frac{1}{5} \mat{864 + 1 & 864 - 4 \\ 216 - 1 & 216 + 4} \\[6pt] &= \frac{1}{5} \mat{865 & 860 \\ 215 & 220} = \mat{173 & 172 \\ 43 & 44} \end{aligned}
  5. 33.The covariance matrix of a data set is C=[10665]C = \mat{10 & 6 \\ 6 & 5}. Find the first principal direction and the share of the variance that it carries.

    Show a hint
    The eigenvector of the largest eigenvalue. The share is that eigenvalue divided by the trace.
    Show one possible solution

    Answer: The direction (3,2)(3, 2), with 1415\tfrac{14}{15}, about 93 percent, of the variance.

    tr⁡(C)=15det⁡(C)=50−36=14λ2−15λ+14=0λ=14orλ=1\begin{aligned} \tr(C) &= 15 \\ \det(C) &= 50 - 36 = 14 \\[6pt] \lambda^2 - 15\lambda + 14 &= 0 \\ \lambda = 14 \quad &\text{or} \quad \lambda = 1 \end{aligned}
    λ=14:C−14I=[−466−9]−4x+6y=0  ⇒  2x=3yv=(3,2)share:1414+1=1415≈0.933\begin{aligned} \lambda = 14: \quad & C - 14I = \mat{-4 & 6 \\ 6 & -9} \\[4pt] & -4x + 6y = 0 \;\Rightarrow\; 2x = 3y \\ & \vv = (3, 2) \\[8pt] \text{share:} \quad & \frac{14}{14 + 1} = \frac{14}{15} \approx 0.933 \end{aligned}

    The second direction is (2,−3)(2, -3), with the variance 1.

  6. 34.Every week 20 percent of the viewers of channel 1 change to channel 2, and 30 percent of the viewers of channel 2 change to channel 1. Find the shares of the two channels in the long run.

    Show a hint
    Write the matrix with columns that add up to 1, and find the eigenvector of the eigenvalue 1.
    Show one possible solution

    Answer: (0.6,0.4)(0.6, 0.4).

    P=[0.80.30.20.7]P−I=[−0.20.30.2−0.3]−0.2x+0.3y=0  ⇒  2x=3yv=(3,2)shares:15 (3,2)=(0.6,0.4)\begin{aligned} P &= \mat{0.8 & 0.3 \\ 0.2 & 0.7} \\[6pt] P - I &= \mat{-0.2 & 0.3 \\ 0.2 & -0.3} \\[6pt] -0.2x + 0.3y &= 0 \;\Rightarrow\; 2x = 3y \\ \vv &= (3, 2) \\[6pt] \text{shares:} \quad & \tfrac{1}{5}\,(3, 2) = (0.6, 0.4) \end{aligned}

    Check: P (0.6,0.4)=(0.48+0.12,  0.12+0.28)=(0.6,0.4)P\,(0.6, 0.4) = (0.48 + 0.12,\; 0.12 + 0.28) = (0.6, 0.4). The other eigenvalue is 1.5−1=0.51.5 - 1 = 0.5.

  7. 35.Which of the symmetric matrices A=[3113]A = \mat{3 & 1 \\ 1 & 3}, B=[1331]B = \mat{1 & 3 \\ 3 & 1} and C=[4221]C = \mat{4 & 2 \\ 2 & 1} are positive definite?

    Show a hint
    Find the eigenvalues of each. All of them must be positive.
    Show one possible solution

    Answer: Only AA.

    A:tr⁡=6,  det⁡=8,λ=2 and 4B:tr⁡=2,  det⁡=−8,λ=4 and −2C:tr⁡=5,  det⁡=0,λ=0 and 5\begin{aligned} A: \quad & \tr = 6, \;\det = 8, \quad \lambda = 2 \text{ and } 4 \\ B: \quad & \tr = 2, \;\det = -8, \quad \lambda = 4 \text{ and } -2 \\ C: \quad & \tr = 5, \;\det = 0, \quad \lambda = 0 \text{ and } 5 \end{aligned}

    AA has two positive eigenvalues. BB has a negative one. CC has an eigenvalue 0, so it is positive semidefinite but not positive definite.

  8. 36.A matrix has the eigenvalue 4 with the eigenvector (1,1)(1, 1) and the eigenvalue 1 with the eigenvector (1,−2)(1, -2). Find the matrix.
    AA

    Show a hint
    A=PDP−1A = P D P\inv, with det⁡(P)=−3\det(P) = -3.
    Show one possible solution

    Answer: A=[3122]A = \mat{3 & 1 \\ 2 & 2}.

    P=[111−2],D=[4001]P−1=1−3[−2−1−11]=13[211−1]PD=[414−2]A=13[8+14−18−24+2]=13[9366]=[3122]\begin{aligned} P &= \mat{1 & 1 \\ 1 & -2}, \quad D = \mat{4 & 0 \\ 0 & 1} \\[6pt] P\inv &= \frac{1}{-3} \mat{-2 & -1 \\ -1 & 1} = \frac{1}{3} \mat{2 & 1 \\ 1 & -1} \\[6pt] PD &= \mat{4 & 1 \\ 4 & -2} \\[6pt] A &= \frac{1}{3} \mat{8 + 1 & 4 - 1 \\ 8 - 2 & 4 + 2} \\[6pt] &= \frac{1}{3} \mat{9 & 3 \\ 6 & 6} = \mat{3 & 1 \\ 2 & 2} \end{aligned}

    Check: A (1,1)=(4,4)A\,(1, 1) = (4, 4) and A (1,−2)=(1,−2)A\,(1, -2) = (1, -2).

  9. 37.Find the singular values of A=[403−5]A = \mat{4 & 0 \\ 3 & -5}, and check them with the Frobenius norm.

    Show a hint
    First ATAA\T A, then its eigenvalues, then their roots.
    Show one possible solution

    Answer: 40≈6.32\sqrt{40} \approx 6.32 and 10≈3.16\sqrt{10} \approx 3.16.

    ATA=[430−5][403−5]=[16+90−150−150+25]=[25−15−1525]tr⁡=50det⁡=625−225=400λ2−50λ+400=0λ=40orλ=10\begin{aligned} A\T A &= \mat{4 & 3 \\ 0 & -5} \mat{4 & 0 \\ 3 & -5} \\[4pt] &= \mat{16 + 9 & 0 - 15 \\ 0 - 15 & 0 + 25} \\[4pt] &= \mat{25 & -15 \\ -15 & 25} \\[6pt] \tr &= 50 \\ \det &= 625 - 225 = 400 \\[6pt] \lambda^2 - 50\lambda + 400 &= 0 \\ \lambda = 40 \quad &\text{or} \quad \lambda = 10 \end{aligned}

    The singular values are 40\sqrt{40} and 10\sqrt{10}. Check: ∥A∥F2=16+0+9+25=50=40+10\norm{A}_F^2 = 16 + 0 + 9 + 25 = 50 = 40 + 10.

  10. 38.Find the three norms of x=(2,2,2,2)\vx = (2, 2, 2, 2) and of y=(8,0,0,0)\vy = (8, 0, 0, 0). For which kind of vector are all three norms equal?

    Show a hint
    Calculate the six numbers and compare the two vectors.
    Show one possible solution

    Answer: x\vx: 8, 4 and 2. y\vy: 8, 8 and 8. The norms are equal for vectors with only one component that is not 0.

    x:∥x∥1=8,∥x∥2=16=4∥x∥∞=2y:∥y∥1=8,∥y∥2=64=8∥y∥∞=8\begin{aligned} \vx: \quad & \norm{\vx}_1 = 8, \quad \norm{\vx}_2 = \sqrt{16} = 4 \\ & \norm{\vx}_\infty = 2 \\[4pt] \vy: \quad & \norm{\vy}_1 = 8, \quad \norm{\vy}_2 = \sqrt{64} = 8 \\ & \norm{\vy}_\infty = 8 \end{aligned}

    When the size sits in one single component, the three norms agree. The more evenly it is spread, the further apart they are.

  11. 39.Two models fit the data equally well, with the weights w=(5,0)\vw = (5, 0) and u=(3,3)\vu = (3, 3). Which one has the smaller Lasso penalty, and which the smaller Ridge penalty?

    Show a hint
    Compare the L1 norms, then the squared L2 norms.
    Show one possible solution

    Answer: Lasso: w\vw (5 against 6). Ridge: u\vu (18 against 25).

    ∥w∥1=5,∥u∥1=3+3=6∥w∥22=25,∥u∥22=9+9=18\begin{aligned} \norm{\vw}_1 &= 5, \quad \norm{\vu}_1 = 3 + 3 = 6 \\ \norm{\vw}_2^2 &= 25, \quad \norm{\vu}_2^2 = 9 + 9 = 18 \end{aligned}

    Lasso prefers the sparse model, Ridge the model with the weight spread over both features.

  12. 40.Show that an eigenvector v\vv of AA with the eigenvalue λ\lambda is also an eigenvector of A2+AA^2 + A, and give its eigenvalue. What are the eigenvalues of A2+AA^2 + A for A=[2112]A = \mat{2 & 1 \\ 1 & 2}?

    Show a hint
    Apply A2+AA^2 + A to v\vv and use Av=λvA\vv = \lambda \vv twice.
    Show one possible solution

    Answer: The eigenvalue is λ2+λ\lambda^2 + \lambda. For the matrix given: 12 and 2.

    (A2+A) v=A2v+Av=λ2v+λv=(λ2+λ) v\begin{aligned} (A^2 + A)\,\vv &= A^2\vv + A\vv \\ &= \lambda^2 \vv + \lambda \vv \\ &= (\lambda^2 + \lambda)\,\vv \end{aligned}

    The matrix [2112]\mat{2 & 1 \\ 1 & 2} has the eigenvalues 3 and 1, so A2+AA^2 + A has 9+3=129 + 3 = 12 and 1+1=21 + 1 = 2. Check:

    A2+A=[5445]+[2112]=[7557]A^2 + A = \mat{5 & 4 \\ 4 & 5} + \mat{2 & 1 \\ 1 & 2} = \mat{7 & 5 \\ 5 & 7}

    Its trace is 14=12+214 = 12 + 2 and its determinant is 49−25=24=12×249 - 25 = 24 = 12 \times 2.

15Check yourself

Twelve quick questions. Your answers are saved in this browser, so you can leave the page and continue later. Each finished round is kept as an attempt below the questions; you can delete the attempts at any time.

Question 1

1.Which vector is an eigenvector of [2005]\mat{2 & 0 \\ 0 & 5}?

Question 2

2.v\vv is an eigenvector of AA with the eigenvalue −2-2. What does AA do to v\vv?

Question 3

3.What is the characteristic equation of [3124]\mat{3 & 1 \\ 2 & 4}?

Question 4

4.A 2 x 2 matrix has the eigenvalues 2 and 6. What is its determinant?

Question 5

5.Which of these matrices is singular (has no inverse)?

Question 6

6.v\vv is an eigenvector of AA with the eigenvalue 3. Which eigenvalue does v\vv have for A2A^2?

Question 7

7.For a symmetric matrix, two eigenvectors that belong to different eigenvalues are always

Question 8

8.What is ∥(4,−3)∥1\norm{(4, -3)}_1?

Question 9

9.What is the max norm of (2,−9,4)(2, -9, 4)?

Question 10

10.Which norm of the error vector does the mean squared error (MSE) use?

Question 11

11.Lasso regression makes some weights exactly 0. Which penalty does it add to the loss?

Question 12

12.In the diagonalization A=PDP−1A = P D P\inv, what are the columns of PP?

0 of 12 answered.

16Checking your work with NumPy

This is the only code of the lesson. After you have solved a problem by hand, NumPy can confirm the result.

Python
import numpy as np

B = np.array([[4, 1], [2, 3]])
values, vectors = np.linalg.eig(B)
print(values)
print(vectors)

x = np.array([3, -4])
print(np.linalg.norm(x, 1), np.linalg.norm(x), np.linalg.norm(x, np.inf))

Output

[5. 2.]
[[ 0.70710678 -0.4472136 ]
 [ 0.70710678  0.89442719]]
7.0 5.0 4.0

Change it and run: put a matrix of a problem you solved by hand into B, and a vector into x, and compare the results.

The eigenvalues come first: 5 and 2. The eigenvectors are the columns of the second result, and NumPy gives each of them the length 1. The first column (0.707,0.707)(0.707, 0.707) is (1,1)(1, 1) divided by 2\sqrt{2}. The second column (−0.447,0.894)(-0.447, 0.894) is (1,−2)(1, -2) divided by −5-\sqrt{5}: a multiple of our eigenvector, so the same answer. The last line shows the L1 norm, the L2 norm and the max norm of (3,−4)(3, -4).

One trap: np.linalg.norm(x) without a second value is the L2 norm of a vector. For a matrix the same call gives the Frobenius norm.

By hand and in NumPy
By handIn NumPy
the eigenvalues and eigenvectors of AAnp.linalg.eig(A)
the same for a symmetric matrixnp.linalg.eigh(A)
tr⁡(A)\tr(A)np.trace(A)
∥x∥1\norm{\vx}_1np.linalg.norm(x, 1)
∥x∥2\norm{\vx}_2np.linalg.norm(x)
∥x∥∞\norm{\vx}_\inftynp.linalg.norm(x, np.inf)
∥A∥F\norm{A}_Fnp.linalg.norm(A)
the singular values of AAnp.linalg.svd(A, compute_uv=False)

17Quick summary

Every idea of the lesson with its definition or rule
IdeaDefinition or rule
Eigenvector and eigenvalueAv=λvA\vv = \lambda \vv, with v\vv not the zero vector
What it meansthe matrix only stretches v\vv, by the factor λ\lambda
Characteristic equationdet⁡(A−λI)=0\det(A - \lambda I) = 0; for a 2 x 2 matrix λ2−tr⁡(A) λ+det⁡(A)=0\lambda^2 - \tr(A)\,\lambda + \det(A) = 0
Tracethe sum of the main diagonal = the sum of the eigenvalues
Determinantthe product of the eigenvalues
Eigenvectorssolve (A−λI) v=0(A - \lambda I)\,\vv = \vzero for every eigenvalue
Eigenvalue 0the matrix is singular
Diagonal or triangular matrixthe eigenvalues are the diagonal entries
Powers and inverseAkA^k has the eigenvalues λk\lambda^k, A−1A\inv has 1λ\dfrac{1}{\lambda}, with the same eigenvectors
Dominant eigenvaluethe largest in size; it decides the long run
DiagonalizationA=PDP−1A = P D P\inv, and Ak=PDkP−1A^k = P D^k P\inv
Symmetric matrixreal eigenvalues, perpendicular eigenvectors, A=QDQTA = Q D Q\T
Orthogonal matrixorthonormal columns; QTQ=IQ\T Q = I and Q−1=QTQ\inv = Q\T
Positive definite matrixsymmetric, with only positive eigenvalues
Principal Component Analysisthe eigenvectors of the covariance matrix; share of a direction = its eigenvalue divided by the trace
Long-run statethe eigenvector of the eigenvalue 1, with components that add up to 1
L1 normthe sum of the absolute values
L2 normthe root of the sum of the squares (the length)
Max normthe largest absolute value
Rules of a normnever negative; a number comes out as its absolute value; the triangle inequality
Distance∥a−b∥\norm{\va - \vb}: Manhattan (L1), Euclidean (L2), Chebyshev (max)
MAE and MSE∥r∥1n\dfrac{\norm{\vr}_1}{n} and ∥r∥22n\dfrac{\norm{\vr}_2^2}{n} for the error vector r\vr
Lasso and Ridgethe penalties α ∥w∥1\alpha\,\norm{\vw}_1 and α ∥w∥22\alpha\,\norm{\vw}_2^2
Frobenius normthe root of the sum of the squares of all entries of a matrix
Singular valuesthe roots of the eigenvalues of ATAA\T A