Primer L. Linear algebra from the first course

Data Mining as Observation, draft 0.3, 2026-09-09. Read this primer if any section of chapter 0 stops you, and read it before chapter 0 if a first linear algebra course is more than a year behind you. Nothing here is a result. Each section rebuilds one idea from arithmetic, gives an example that can be checked by hand in a few lines, and says which section of chapter 0 stands on it.

Chapter 0 assumes a first course in linear algebra and starts from the dot product. This primer starts one step earlier, from a row of numbers, and ends at the two ideas the course cannot do without, the eigenvectors of a symmetric matrix and the singular value decomposition. Everything in it is standard and is covered at greater length in TSK Appendix A [1]. The order here is the order the book needs the ideas in, and every example is small enough to check on paper.

Two conventions run through the book and are stated here once. A vector is a column, and a row of a table is written as a vector when it is used in arithmetic. The transpose of a column is a row, so $x^{\top}y$ is the dot product and $xy^{\top}$ is a matrix, and the book writes both.

L.1 Vectors are lists of numbers, and also arrows

A row of a table with $d$ numeric columns is a list of $d$ numbers. Write it as $x=(x_1,\dots,x_d)$. The same list is an arrow from the origin to the point with those coordinates, and the two pictures are the same object. Adding two vectors adds their coordinates, and multiplying a vector by a number, called a scalar, multiplies every coordinate. In the arrow picture, addition places the second arrow at the tip of the first, and scaling stretches an arrow without turning it.

$$x+y=(x_1+y_1,\dots,x_d+y_d),\qquad c\,x=(c\,x_1,\dots,c\,x_d). \tag{L.1}$$

A linear combination of vectors is a sum of scalar multiples of them. The span of a set of vectors is the set of all their linear combinations. The span of one nonzero vector is a line through the origin. The span of two vectors that do not lie on one line is a plane. The standard basis vectors $e_1,\dots,e_d$ have a one in one coordinate and zeros elsewhere, and every vector is the linear combination of them with its own coordinates as the scalars, which is why coordinates and vectors can be used interchangeably.

The length of a vector, also called its norm, comes from Pythagoras. For two coordinates it is the hypotenuse of the triangle with legs $x_1$ and $x_2$, and in $d$ dimensions it is the square root of the sum of the squared coordinates. A unit vector has length one, and any nonzero vector becomes a unit vector when divided by its length. A unit vector is a pure direction.

$$\|x\|=\sqrt{x_1^{2}+\cdots+x_d^{2}},\qquad u=\frac{x}{\|x\|},\qquad \|u\|=1. \tag{L.2}$$

Example. $x=(1,2)$ and $y=(3,-1)$. Then $x+y=(4,1)$, $2x=(2,4)$, and $2x+y=(5,3)$. The vector $(3,4)$ has length $\sqrt{9+16}=5$, and its direction is $(0.6,0.8)$. The vector $(2,4)$ lies in the span of $(1,2)$, since it is $2$ times it, and $(3,-1)$ does not, since no scalar sends $1$ to $3$ and $2$ to $-1$ at once.

Used in. Section 0.1, which begins here.

L.2 The dot product measures agreement, and the cosine is its scale-free form

The dot product of two vectors multiplies matching coordinates and adds. It is a single number. The dot product of a vector with itself is its squared length, which is equation L.2 again. Two vectors whose dot product is zero are orthogonal, which is the word for perpendicular in any number of dimensions.

$$x\cdot y=x^{\top}y=\sum_{i=1}^{d}x_i y_i,\qquad x\cdot x=\|x\|^{2}. \tag{L.3}$$

The dot product has a geometric meaning, and the meaning is the reason the book uses it. Expand the squared length of $x-y$ coordinate by coordinate and the cross terms collect into the dot product, which is the law of cosines with the dot product in the place of the cosine term. That identity is the whole content of the statement that the dot product of two unit vectors is the cosine of the angle between them.

$$\|x-y\|^{2}=\|x\|^{2}+\|y\|^{2}-2\,x\cdot y,\qquad x\cdot y=\|x\|\,\|y\|\cos\theta. \tag{L.4}$$

Three consequences follow at once. The dot product of a vector with a unit vector $u$ is the coordinate of the vector along $u$, the signed length of its shadow on that line. The absolute value of a dot product is at most the product of the two lengths, since a cosine is at most one, which is the Cauchy and Schwarz inequality. And cosine similarity, the dot product divided by the two lengths, depends only on the two directions and not on either length. Scaling either vector by a positive number leaves it unchanged.

Example. $x=(1,2)$ and $y=(3,-1)$ have dot product $3-2=1$. Their lengths are $\sqrt5$ and $\sqrt{10}$, so the cosine of the angle between them is $1/\sqrt{50}$, about $0.141$, and the angle is about $82$ degrees. Check the law of cosines. $x-y=(-2,3)$ has squared length $13$, and $5+10-2=13$. The vectors $(1,2)$ and $(2,-1)$ have dot product $2-2=0$ and are orthogonal.

Used in. Section 0.1, equation 0.1. Chapter 3, where cosine against Euclidean is the first decision of the course.

L.3 A matrix is a table, and also a machine that moves vectors

A matrix with $n$ rows and $d$ columns is a table of numbers, written $A$ with entry $A_{ij}$ in row $i$ and column $j$. A data table with $n$ rows and $d$ numeric columns is such a matrix, and the book writes it $X$. The transpose $A^{\top}$ swaps rows and columns, so it has $d$ rows and $n$ columns. A square matrix is symmetric when it equals its own transpose, and almost every matrix in this book is symmetric. The identity $I$ has ones on the diagonal and zeros elsewhere.

A matrix multiplies a vector, and there are two ways to read the product that give the same answer. Row by row, entry $i$ of $Ax$ is the dot product of row $i$ of $A$ with $x$. Column by column, $Ax$ is the linear combination of the columns of $A$ with the coordinates of $x$ as the scalars. The second reading is the one to hold in mind. A matrix sends a vector to a combination of its columns, so the outputs of $A$ fill the span of its columns and nothing else.

$$(Ax)_i=\sum_{j}A_{ij}x_j,\qquad Ax=x_1\,a_1+\cdots+x_d\,a_d\ \ \text{with } a_j \text{ the } j\text{th column}. \tag{L.5}$$

Two matrices multiply when the first has as many columns as the second has rows, entry $ij$ of the product being the dot product of row $i$ of the first with column $j$ of the second. The order matters, and $AB$ and $BA$ are different matrices in general. The transpose of a product reverses the order, $(AB)^{\top}=B^{\top}A^{\top}$. The identity leaves every vector alone.

The inverse $A^{-1}$ of a square matrix undoes it, so that $A^{-1}Ax=x$. It exists exactly when the columns of $A$ are not redundant, which section L.4 states precisely. For a two by two matrix there is a formula, and the number in its denominator is the determinant. A determinant of zero means no inverse.

$$A=\begin{pmatrix}a&b\\ c&d\end{pmatrix},\qquad \det A=ad-bc,\qquad A^{-1}=\frac{1}{ad-bc}\begin{pmatrix}d&-b\\ -c&a\end{pmatrix}. \tag{L.6}$$

Solving $Ax=b$ asks which combination of the columns of $A$ gives $b$. When the inverse exists the answer is $x=A^{-1}b$ and it is unique. When the determinant is zero, $b$ either lies in the span of the columns, in which case infinitely many combinations reach it, or it does not, in which case none does. The book rarely solves a system, but it inverts covariance matrices, and section 2.4 of chapter 2 is about an instrument that tried to invert one that had no inverse.

Example. Let $A$ have rows $(2,1)$ and $(1,3)$, and $x=(1,2)$. Row by row, $Ax=(2+2,\ 1+6)=(4,7)$. Column by column, $1\cdot(2,1)+2\cdot(1,3)=(4,7)$. The determinant is $6-1=5$, so the inverse has rows $(3,-1)/5$ and $(-1,2)/5$. Multiply $A$ by its inverse and the first row gives $(6-1)/5=1$ and $(-2+2)/5=0$, as an identity row should. Solving $Ax=(4,7)$ gives $x=\big((12-7)/5,\ (-4+14)/5\big)=(1,2)$, which is where we started. The matrix with rows $(1,2)$ and $(2,4)$ has determinant $4-4=0$, its second column is twice its first, and it has no inverse.

Used in. Section 0.2. Section 0.4 and equation 0.6, where a covariance is inverted through its eigenvalues. Section 0.16, the Mahalanobis distance.

L.4 Independence, rank, and the directions a matrix cannot see

A set of vectors is linearly independent when no one of them is a linear combination of the others, which is the same as saying that the only combination of them that gives the zero vector is the one with every scalar zero. Otherwise the set is dependent and at least one vector is redundant.

The rank of a matrix is the number of linearly independent columns it has, which equals the number of linearly independent rows. It is the dimension of the column space, the span of the columns, which by section L.3 is the set of all outputs $Ax$. A matrix with $n$ rows and $d$ columns has rank at most the smaller of $n$ and $d$. That bound is not a technicality. A data table with sixteen rows in one hundred and twenty-eight columns has rank at most sixteen, its centered version has rank at most fifteen, and a covariance computed from it cannot be inverted, which is the failure chapter 2 section 2.4 records.

The null space, also called the kernel, is the set of vectors that $A$ sends to zero. It is the set of directions the matrix cannot see, since adding a null vector to $x$ leaves $Ax$ unchanged. Rank and the dimension of the null space add up to the number of columns.

$$\ker A=\{x:\ Ax=0\},\qquad \operatorname{rank}A+\dim\ker A=d. \tag{L.7}$$

An outer product $uv^{\top}$ of two vectors is the matrix whose entry $ij$ is $u_iv_j$. Every column is a multiple of $u$, so it has rank one, and it sends any $x$ to $(v\cdot x)\,u$, a multiple of $u$. Chapter 0 builds its central matrix, the read operator, as an average of outer products, and rank counts how many directions that average uses.

$$\big(uv^{\top}\big)_{ij}=u_iv_j,\qquad \big(uv^{\top}\big)x=(v\cdot x)\,u. \tag{L.8}$$

Example. The matrix with rows $(1,2)$ and $(2,4)$ has rank one, since the second row is twice the first. Its column space is the line of multiples of $(1,2)$. Its null space is the line of multiples of $(2,-1)$, because $(1,2)\cdot(2,-1)=0$ and $(2,4)\cdot(2,-1)=0$, and one plus one is two columns, as equation L.7 requires. The outer product of $u=(1,2)$ with $v=(3,1)$ has rows $(3,1)$ and $(6,2)$, rank one, and sends $x=(1,1)$ to $(v\cdot x)\,u=4\,(1,2)=(4,8)$.

Used in. Section 0.2, rank. Sections 0.5 and 0.6, where the kernel of the read operator is the quotient, the set of directions a consumer does not read. Section 0.4, effective rank, which is a soft version of rank.

L.5 Projections, orthonormal bases, and rotations

The projection of $x$ onto a unit vector $u$ is the point on the line through $u$ nearest to $x$. It is the coordinate of $x$ along $u$, the dot product $u\cdot x$ of section L.2, times $u$. What is left over, $x$ minus its projection, is the residual, and it is orthogonal to $u$. The squared lengths of the projection and the residual add up to the squared length of $x$, which is Pythagoras once more.

$$\operatorname{proj}_u(x)=(u\cdot x)\,u=\big(uu^{\top}\big)x,\qquad \big(x-\operatorname{proj}_u(x)\big)\cdot u=0. \tag{L.9}$$

The matrix that performs the projection is the outer product $uu^{\top}$, and it equals its own square, since projecting twice is projecting once. A projection onto a subspace of several dimensions works the same way when the subspace has an orthonormal basis, a set of unit vectors orthogonal to each other. The projection is the sum of the projections onto each basis vector, and its matrix is the sum of the outer products.

Any set of independent vectors can be turned into an orthonormal basis for its span by the Gram and Schmidt procedure. Take the first vector and divide by its length. Take the second, subtract its projection onto the first, and divide what remains by its length. Continue, subtracting the projections onto every basis vector already made.

A square matrix whose columns are orthonormal is an orthogonal matrix, written $Q$. Its transpose is its inverse, $Q^{\top}Q=I$, and it preserves every length and every dot product, so it preserves every angle. Orthogonal matrices are the rotations and reflections. Chapter 0 uses them without naming them whenever it changes to the basis of a symmetric matrix's eigenvectors.

$$Q^{\top}Q=I,\qquad \|Qx\|=\|x\|,\qquad (Qx)\cdot(Qy)=x\cdot y. \tag{L.10}$$

Example. Let $u=(0.6,0.8)$ and $x=(2,3)$. The coordinate along $u$ is $1.2+2.4=3.6$, so the projection is $3.6\,u=(2.16,2.88)$ and the residual is $(-0.16,0.12)$. The residual's dot product with $u$ is $-0.096+0.096=0$. Pythagoras holds, $3.6^{2}+(0.16^{2}+0.12^{2})=12.96+0.04=13=2^{2}+3^{2}$. The projection matrix $uu^{\top}$ has rows $(0.36,0.48)$ and $(0.48,0.64)$, and its top-left entry squared plus its top-right entry times its bottom-left entry is $0.1296+0.2304=0.36$, the top-left entry again, as a projection's square should give. Gram and Schmidt on $(1,1)$ and $(1,0)$ gives $q_1=(1,1)/\sqrt2$, then $(1,0)-\tfrac12(1,1)=(0.5,-0.5)$, so $q_2=(1,-1)/\sqrt2$. The matrix with rows $(0,-1)$ and $(1,0)$ rotates the plane by ninety degrees, sends $(1,0)$ to $(0,1)$, and has its transpose as inverse.

Used in. Section 0.2, equation 0.2. Section 0.5, where the read operator of a consumer that reads one direction is the projection matrix onto it.

L.6 Quadratic forms, and what positive semidefinite means

A symmetric matrix $A$ turns a vector into a number through the quadratic form $x^{\top}Ax$, the dot product of $x$ with $Ax$. Written out, it is a sum over all pairs of coordinates of $A_{ij}x_ix_j$. For the identity it is the squared length. For a diagonal matrix it is a weighted sum of squared coordinates.

$$x^{\top}Ax=\sum_{i,j}A_{ij}\,x_i x_j. \tag{L.11}$$

A symmetric matrix is positive semidefinite when its quadratic form is never negative, and positive definite when the form is positive for every nonzero $x$. The word matters because the book's two central matrices are both of this kind. A covariance matrix is the average of the outer products of the centered rows with themselves, and its quadratic form along a unit direction is the average of the squared coordinates of the centered rows along that direction, which is a variance and cannot be negative. The read operator of section 0.5 is the average of outer products of gradients, and the same argument applies.

$$\Sigma=\frac1n\sum_{i=1}^{n}(x_i-\mu)(x_i-\mu)^{\top},\qquad u^{\top}\Sigma u=\frac1n\sum_{i=1}^{n}\big(u\cdot(x_i-\mu)\big)^{2}\ \ge 0. \tag{L.12}$$

Example. Let $A$ have rows $(2,1)$ and $(1,2)$. At $x=(1,0)$ the form is $2$. At $x=(1,1)$, $Ax=(3,3)$ and the form is $6$. At $x=(1,-1)$, $Ax=(1,-1)$ and the form is $2$. This matrix is positive definite, and section L.7 shows why. The matrix with rows $(1,2)$ and $(2,1)$ is not, since at $x=(1,-1)$ its form is $1-2-2+1=-2$.

Used in. Section 0.3, equation 0.4, where the variance along a direction is a quadratic form of the covariance. Section 0.5, where the read distortion is the quadratic form of the read operator averaged over errors.

L.7 Eigenvectors are the directions a symmetric matrix only stretches

Most vectors are turned as well as stretched when a matrix multiplies them. An eigenvector $v$ of a square matrix $A$ is a nonzero vector that is only stretched, so that $Av$ is a multiple of $v$, and the multiple $\lambda$ is its eigenvalue. Along an eigenvector the matrix is a single number.

$$Av=\lambda v,\qquad \det(A-\lambda I)=0. \tag{L.13}$$

The eigenvalues of a small matrix are found by hand from the second equation, which is a polynomial in $\lambda$ of degree equal to the size of the matrix, and each eigenvector is then found by solving $(A-\lambda I)v=0$ for that eigenvalue.

For a symmetric matrix three things are true that make it the only case the book needs. Every eigenvalue is a real number. Eigenvectors with different eigenvalues are orthogonal, so the eigenvectors can be chosen as an orthonormal basis, an orthogonal matrix $V$ in the sense of section L.5. And the matrix is the sum of its eigenvalues times the outer products of its eigenvectors, which is the spectral decomposition. The list of eigenvalues is the spectrum.

$$A=\sum_{i=1}^{d}\lambda_i\,v_iv_i^{\top}=V\Lambda V^{\top},\qquad V^{\top}V=I,\qquad \Lambda=\operatorname{diag}(\lambda_1,\dots,\lambda_d). \tag{L.14}$$

The decomposition makes every question about a symmetric matrix a question about numbers. The trace, the sum of the diagonal entries, equals the sum of the eigenvalues, and the determinant equals their product. Squaring the matrix squares the eigenvalues and keeps the eigenvectors. Inverting it inverts the eigenvalues, so the inverse exists exactly when no eigenvalue is zero, and the rank is the number of nonzero eigenvalues. Taking a square root takes the square root of each eigenvalue, which is how section 0.4 defines the whitening transform $\Sigma^{-1/2}$. The quadratic form of section L.6 becomes a weighted sum of squared coordinates in the eigenvector basis, so the matrix is positive semidefinite exactly when every eigenvalue is at least zero.

$$x^{\top}Ax=\sum_i\lambda_i\,(v_i\cdot x)^{2},\qquad A^{-1}=\sum_i\lambda_i^{-1}v_iv_i^{\top},\qquad A^{1/2}=\sum_i\lambda_i^{1/2}v_iv_i^{\top}. \tag{L.15}$$

One consequence is the whole of principal component analysis in a sentence. Over unit vectors $u$, the quadratic form $u^{\top}\Sigma u$ is largest when $u$ is the eigenvector with the largest eigenvalue, and the largest value is that eigenvalue. Since the form is the variance along $u$ by equation L.12, the top eigenvector of a covariance matrix is the direction of largest variance, the second eigenvector is the direction of largest variance orthogonal to the first, and so on. TSK Appendix B derives the same statement at length [1].

A computer finds the top eigenvector of a symmetric matrix by power iteration. Start from almost any vector, multiply by the matrix, rescale to unit length, and repeat. Each multiplication stretches the component along the top eigenvector more than every other component, so the vector turns toward it.

Example. Let $A$ have rows $(2,1)$ and $(1,2)$. Equation L.13 gives $(2-\lambda)^{2}-1=0$, so $\lambda=3$ or $\lambda=1$. For $\lambda=3$, $A-3I$ has rows $(-1,1)$ and $(1,-1)$, and $v_1=(1,1)/\sqrt2$ solves it. For $\lambda=1$, $A-I$ has rows $(1,1)$ and $(1,1)$, and $v_2=(1,-1)/\sqrt2$. The two are orthogonal. The spectral decomposition is $3$ times the matrix with every entry $\tfrac12$ plus $1$ times the matrix with rows $(\tfrac12,-\tfrac12)$ and $(-\tfrac12,\tfrac12)$, and the sum has rows $(2,1)$ and $(1,2)$. The trace is $4=3+1$ and the determinant is $3=3\cdot1$. The quadratic form at $x=(1,0)$ is $3\cdot\tfrac12+1\cdot\tfrac12=2$, as section L.6 found directly. The square root has diagonal entries $(\sqrt3+1)/2$, about $1.366$, and off-diagonal entries $(\sqrt3-1)/2$, about $0.366$, and $1.366^{2}+0.366^{2}=2.00$ while $2\cdot1.366\cdot0.366=1.00$, so it squares back to $A$. Power iteration from $(1,0)$ gives $(2,1)$, then $(5,4)$, then $(14,13)$, and the ratio of the two coordinates, $2$, then $1.25$, then $1.077$, is closing on the $1$ of the top eigenvector $(1,1)/\sqrt2$.

A symmetric matrix stretches its eigenvectors and turns everything elsethe unit circleits image, semi-axes 3 and 1 along the eigenvectorsA v1 = 3 v1A v2 = 1 v2e1A e1 = (2, 1), turnedunit circleits image under Aeigenvalues 3 and 1, trace 4, determinant 3
Figure L.1. The matrix with rows (2, 1) and (1, 2) sends the unit circle to an ellipse. Its eigenvectors, at 45 and 135 degrees, are the only directions that are stretched without turning, by 3 and by 1.

Matrices that are not symmetric can have complex eigenvalues and eigenvectors that are not orthogonal. The book never needs them.

Used in. Section 0.4 throughout, equations 0.5 to 0.7. Section 0.10, where the eigenvectors of a graph's Laplacian are its smoothest functions. Section 0.16, the Mahalanobis distance. Chapters 4, 9, and 11.

L.8 The singular value decomposition is the eigenvector idea for any matrix

A data matrix $X$ with $n$ rows and $d$ columns is not square and has no eigenvectors of its own. It has the next best thing. The singular value decomposition writes any matrix as a sum of rank-one outer products, ordered by size, with orthonormal vectors on both sides.

$$A=\sum_{i=1}^{r}\sigma_i\,u_iv_i^{\top}=U\Sigma_{\!s}V^{\top},\qquad \sigma_1\ge\sigma_2\ge\cdots\ge\sigma_r>0,\qquad U^{\top}U=I,\ V^{\top}V=I. \tag{L.16}$$

The numbers $\sigma_i$ are the singular values, the $v_i$ are the right singular vectors, living in the space of columns, and the $u_i$ are the left singular vectors, living in the space of rows. The book writes the diagonal matrix of singular values as $\Sigma_{\!s}$ to keep it apart from the covariance $\Sigma$. The count $r$ of nonzero singular values is the rank of section L.4.

The decomposition is computed from section L.7. The matrix $A^{\top}A$ is symmetric and positive semidefinite, its eigenvectors are the $v_i$, and its eigenvalues are the squares of the singular values. Each $u_i$ is then $Av_i$ divided by $\sigma_i$. For a centered data matrix, $A^{\top}A$ divided by $n$ is the covariance matrix of equation L.12, so the right singular vectors of the centered data are the principal components and the eigenvalues of the covariance are $\sigma_i^{2}/n$. Every statement chapter 4 makes about the spectrum of a covariance is a statement about the singular values of the data.

$$A^{\top}A\,v_i=\sigma_i^{2}\,v_i,\qquad u_i=\frac{Av_i}{\sigma_i},\qquad \Sigma=\frac{A^{\top}A}{n}\ \text{for centered } A. \tag{L.17}$$

The decomposition also answers the question of how well a matrix can be approximated by one of lower rank. Keeping the first $k$ terms of equation L.16 gives the best rank-$k$ approximation in the sense of total squared error, and the error is the sum of the squared singular values that were dropped. That is the Eckart and Young theorem, which ESL states with PCA [2]. The total squared error is measured in the Frobenius norm, the square root of the sum of all squared entries, which also equals the square root of the sum of the squared singular values.

$$\|A\|_F^{2}=\sum_{i,j}A_{ij}^{2}=\sum_i\sigma_i^{2},\qquad \min_{\operatorname{rank}B\le k}\|A-B\|_F^{2}=\sum_{i>k}\sigma_i^{2}. \tag{L.18}$$

Chapter 4 is about the fact that the identity reader's best approximation, the one equation L.18 describes, is not the best approximation for a consumer that reads some directions and not others. The theorem is the starting point of that chapter, not its conclusion.

Example. Let $A$ have rows $(1,1)$, $(1,0)$, and $(0,1)$, three rows and two columns. Then $A^{\top}A$ has rows $(2,1)$ and $(1,2)$, the matrix of section L.7, so the singular values are $\sqrt3$ and $1$, and $v_1=(1,1)/\sqrt2$, $v_2=(1,-1)/\sqrt2$. Then $u_1=Av_1/\sqrt3=(2,1,1)/\sqrt6$ and $u_2=Av_2/1=(0,1,-1)/\sqrt2$, which are orthogonal since $0+1-1=0$. The first term $\sigma_1u_1v_1^{\top}$ has rows $(1,1)$, $(0.5,0.5)$, and $(0.5,0.5)$, and the second has rows $(0,0)$, $(0.5,-0.5)$, and $(-0.5,0.5)$, and they add back to $A$. The squared Frobenius norm of $A$ is $4=3+1$. The rank-one approximation is the first term alone, and its squared error is $\sigma_2^{2}=1$, one quarter of the total, so the first component carries three quarters of the squared size of $A$.

Used in. Section 0.4. Chapter 4 throughout, where truncation is a bet on the spectrum. Section 0.17, latent semantic analysis.

L.9 The gradient is the vector of a function's sensitivities

A function $f$ that takes a vector in and gives a number out changes at some rate along each coordinate. The partial derivative with respect to $x_j$ is the rate of change of $f$ when $x_j$ moves and every other coordinate stays fixed. The gradient is the vector of the $d$ partial derivatives, written $\nabla f$. It is the direction in which $f$ increases fastest at that point, and its length is that fastest rate.

$$\nabla f(x)=\Big(\frac{\partial f}{\partial x_1},\dots,\frac{\partial f}{\partial x_d}\Big),\qquad f(x+\delta)\approx f(x)+\nabla f(x)\cdot\delta. \tag{L.19}$$

The second statement is the one the book uses. For a small step $\delta$, the change in $f$ is the dot product of the gradient with the step, to first order. A step along a direction orthogonal to the gradient changes $f$ by nothing to first order. A coordinate the function does not use has a zero entry in the gradient. Chapter 0 calls the gradient of a consumer its sensitivity, and section 0.5 measures it by finite differences, the ratio of the change in $f$ to a small step, coordinate by coordinate.

Three gradients cover most of the book. A linear function $w\cdot x$ has gradient $w$ everywhere. The squared length $\|x\|^{2}$ has gradient $2x$. A quadratic form $x^{\top}Ax$ with $A$ symmetric has gradient $2Ax$. And the chain rule handles a function of a dot product, so $\tanh(w\cdot x)$ has gradient $\big(1-\tanh^{2}(w\cdot x)\big)\,w$, a multiple of $w$ at every point.

$$\nabla(w\cdot x)=w,\qquad \nabla\|x\|^{2}=2x,\qquad \nabla\big(x^{\top}Ax\big)=2Ax,\qquad \nabla g(w\cdot x)=g'(w\cdot x)\,w. \tag{L.20}$$

When the function gives a vector out rather than a number, each output coordinate has its own gradient, and the matrix whose rows are those gradients is the Jacobian. Section 0.5 mentions it once and otherwise works with a single output.

Example. Let $f(x)=x_1^{2}+3x_1x_2$. The partial derivatives are $2x_1+3x_2$ and $3x_1$, so at $(1,2)$ the gradient is $(8,3)$ and $f$ is $7$. Step to $(1.01,2)$ and $f$ becomes $1.0201+6.06=7.0801$, a change of $0.0801$ against the gradient's prediction of $0.08$. Step to $(1,2.01)$ and $f$ becomes $7.03$, a change of $0.03$, as predicted. For $A$ with rows $(2,1)$ and $(1,2)$, the quadratic form's gradient at $(1,0)$ is $2Ax=(4,2)$, and along the first coordinate the form is $2(1+h)^{2}$, whose rate of change at $h=0$ is $4$.

Used in. Section 0.5, equations 0.8 and 0.9, the sensitivity and the read operator. Chapter 6, where a classifier's read operator is measured from its gradients. Chapter 14, attributions.

L.10 Where each tool goes next

The table names, for each section of this primer, the chapter 0 section that builds on it and the first chapter that uses the result. A reader who is stopped by a chapter 0 section can find the primer section to reread in the first column.

Primer sectionBuilds towardFirst used in earnest
L.1 vectors, span, length0.1chapter 3
L.2 dot product, cosine, orthogonality0.1, equation 0.1chapter 3 section 3.1
L.3 matrices, transpose, inverse, determinant0.2, 0.16chapter 2 section 2.4
L.4 independence, rank, kernel, outer products0.2, 0.5, 0.6chapter 2 section 2.2
L.5 projections, orthonormal bases, rotations0.2, equation 0.2, 0.5chapter 3 section 3.2
L.6 quadratic forms, positive semidefinite0.3, equation 0.4chapter 3 section 3.2
L.7 eigenvectors, spectral decomposition, PCA0.4, 0.10, 0.16chapter 4 section 4.1
L.8 singular values, low-rank approximation0.4, 0.17chapter 4
L.9 gradients, the first-order change0.5, equations 0.8 and 0.9chapter 2 section 2.2, chapter 6

Exercises

Warm-up questions are answered in a sentence or two. By-hand problems are computed on paper and checked against the answer key at the end of the book. Programming problems produce a committed notebook.

Warm-up

  1. Is $(2,4,6)$ in the span of $(1,2,3)$? Say why in one sentence.
  2. What does the dot product of two unit vectors measure, and what values can it take?
  3. A square matrix has determinant zero. What does that say about its columns, and about solving $Ax=b$?
  4. A data table has one hundred rows and three columns. What is the largest rank it can have, and why?
  5. Why is a projection matrix equal to its own square?
  6. A symmetric matrix has eigenvalues $5$, $2$, and $0$. Is it positive semidefinite? Does it have an inverse?
  7. How are the singular values of a matrix $A$ related to the eigenvalues of $A^{\top}A$?
  8. The gradient of a function at some point is $(0,0,4)$. Which coordinate does the function respond to there, and how much does $f$ change for a step of $0.01$ along the third coordinate?

By hand

  1. Let $x=(2,-1,2)$ and $y=(1,2,2)$. Compute both lengths, the dot product, the cosine of the angle, and the angle in degrees.
  2. Let $A$ have rows $(3,1)$ and $(1,2)$. Compute $Ax$ for $x=(1,-1)$, the transpose, the determinant, the inverse, and the solution of $Ax=(4,3)$. Check the solution by multiplying.
  3. Find the rank of the matrix with rows $(1,2,3)$ and $(2,4,6)$, and write down two independent vectors in its null space.
  4. Project $x=(1,3)$ onto $u=(0.8,0.6)$. Give the projection and the residual, and check that the residual is orthogonal to $u$.
  5. Run Gram and Schmidt on $(3,4)$ and $(1,0)$, in that order.
  6. Find the eigenvalues and unit eigenvectors of the matrix with rows $(5,2)$ and $(2,5)$, write its spectral decomposition, and evaluate its quadratic form at $(1,0)$ both directly and through equation L.15.
  7. Is the matrix with rows $(1,3)$ and $(3,1)$ positive semidefinite? Give a vector at which its quadratic form is negative, or show that none exists.
  8. Let $A$ have rows $(1,2)$ and $(2,4)$. Compute its singular values, its rank, and write it as a single outer product $\sigma_1u_1v_1^{\top}$.
  9. Let $f(x)=x_1^{2}+3x_1x_2$. Compute the gradient at $(2,1)$, then the actual change in $f$ for a step of $0.01$ along each coordinate, and compare with the gradient's prediction.

Programming

  1. Use numpy.linalg.eigh to check exercise 14. Reconstruct the matrix from its eigenvalues and eigenvectors with equation L.14 and report the largest absolute difference from the original.
  2. Implement power iteration for the matrix with rows $(2,1)$ and $(1,2)$, starting from $(1,0)$, for twenty steps. Report the estimate of the top eigenvalue at each step, the quadratic form of the current unit vector, and the first step at which it is within $0.001$ of $3$.
  3. Generate two hundred rows in five dimensions as a random combination of two fixed directions plus noise of standard deviation $0.05$. Compute the singular values, reconstruct the matrix from the first two terms of equation L.16, and report the relative squared Frobenius error of the reconstruction. State how it compares with the sum of the dropped squared singular values.
  4. For $f(x)=\tanh(w\cdot x)$ with a fixed random $w$ in eight dimensions, compute the gradient by the closed form of equation L.20 and by central finite differences at a random point, at step $10^{-3}$ and at $10^{-6}$. Report the largest absolute difference at each step.

Answers to the warm-up and by-hand problems are collected in the answer key at the end of the book.

next, Primer S. Probability and statistics from the first course