TL;DR
A quadratic form takes a vector and returns a single number built only from squares and products of its coordinates. Two coordinates and give expressions such as . Written as , the whole expression is controlled by one matrix , and that matrix tells you the shape of the function.
The shape is the useful part. When all the values are positive, the graph is a bowl, and its level sets are ellipses.
Where you'll see it: in the squared length of an error vector in least squares, in the curvature of a loss surface at its minimum, and in the energy of a physical system near equilibrium.
Intuition
Squares and cross terms
Start with the simplest case. The function takes a number and squares it. It is a parabola, a bowl with its bottom at zero.
Now take two coordinates. The function is a bowl in three dimensions, round like a cereal bowl, because the two coordinates contribute in the same way. Change the coefficients to and the bowl stretches: it is steeper in the direction. Add a cross term and the bowl tilts, so its axes are no longer lined up with the coordinate lines.
A quadratic form is any combination of these pieces: squares of each coordinate, and products of pairs of coordinates, with coefficients. Nothing linear is allowed, and there is no constant.
- x₁² + x₂², along either axis
- 4x₁² + x₂², along the x₁ axis
Why a matrix
Writing out every coefficient gets messy as the number of coordinates grows. A matrix holds all of them in one place. The diagonal entries carry the squares, and the off-diagonal entries carry the cross terms. Then one short expression, , describes the whole function, and the rules of matrix algebra can be used on it.
The math
Definition
Read the double sum in words. Multiply every entry by the product , then add everything up. A diagonal term is a square. An off-diagonal pair and together give a cross term.
Only the symmetric part matters
Two different matrices can give the same quadratic form. Since , the entries and only ever appear added together. So you can replace by its symmetric version
which has the same quadratic form. A matrix equal to its own transpose is a Symmetric Matrix. From now on we assume is symmetric, because that is the case where the shape results hold.
Expanding a small example
Take , which is symmetric. Then
The off-diagonal 1s combine into a cross term with coefficient 2.
Gradient
The rate of change of in each direction is collected in the Gradient (the vector of partial derivatives, which points uphill). For a symmetric ,
Check it on the example: and , and these two numbers are the entries of . This is why quadratic forms are so common in optimization. The gradient is linear in , and it is zero exactly where .
Definiteness: the shape
A symmetric matrix is classified by the sign of over all nonzero vectors:
- Positive definite: for every . The graph is a bowl with its only minimum at zero. Level sets are ellipses.
- Positive semidefinite: for all , with equality for some nonzero vectors. The bowl has a flat valley.
- Indefinite: takes both signs. The graph is a saddle.
A symmetric matrix is positive definite (see Positive Definite Matrix) exactly when all of its eigenvalues are positive. The eigenvalues also give the axes of the ellipses: long axes go with small eigenvalues. For a loss function , the matrix of second derivatives (the Hessian) plays the role of near a minimum: looks like a quadratic form there, which is the curvature mentioned in the TL;DR.
Worked examples
Let and .
- First compute : the first entry is , the second is . So .
- Then take the Dot Product with : .
- Check with the expanded form : .
Interpretation: both routes agree. The matrix route is shorter, and it generalises to any number of coordinates.
Let , so . With no cross term, the axes line up with the coordinates.
- Evaluate at : .
- Find the level set . Solve . This is an ellipse with reaching and reaching .
- Check one point on the ellipse: , gives .
- Both diagonal entries are positive, so for every nonzero vector, and is positive definite.
Interpretation: the entry 8 is four times the entry 2, so the bowl is four times steeper in the direction. Its ellipses are narrower in . The diagonal entries are the eigenvalues here, and the level set's axes are for each eigenvalue .
Least squares (Linear Regression) fits a line or plane to data. Suppose the data matrix has one row per example, here rows and , so . For a weight vector , the predictions are , and the squared length of the predictions (the sum of their squares) is . The squared error of a fit has the same shape, with the targets subtracted first.
- Build . Each entry is a dot product of two columns of . The columns are and . Column 1 with itself gives ; column 1 with column 2 gives (twice, by symmetry); column 2 with itself gives . So .
- Then , which is a quadratic form.
- Test : , so the squared length is .
- Through the form: . The two routes agree.
- The form is never negative, because it is a squared length. Its values are zero only when , and here that happens only at , so the matrix is positive definite.
Interpretation: the squared size of a linear model's output, as a function of the weights, is a bowl, and sets how steep it is in each direction. The same matrix controls the bowl of the squared error, whose bottom is the least-squares answer.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| Matrix is not symmetric | Eigenvalue and ellipse rules do not apply directly | Replace by first |
| Zero eigenvalue or zero direction | The bowl has a flat valley, so the minimum is not unique | Add a small multiple of the identity (regularisation) |
| Negative eigenvalue | The form is a saddle, not a bowl; it has no minimum and Gradient Descent can run off along the negative direction | Check definiteness before relying on a minimum |
| Large dimension | Storing and inverting becomes expensive | Use factorisations or iterative solvers |
History
Quadratic forms are among the oldest objects in algebra. Their study goes back to the classification of conic sections and then to the theory of binary forms in the nineteenth century, where mathematicians asked which integers a form can represent. The modern matrix view grew out of that work. The link between definiteness and eigenvalues, the basis of the shape results above, was worked out as linear algebra matured in the nineteenth and early twentieth centuries. Quadratic forms caught on in statistics and optimization because squared errors are quadratic forms.
Recap
A quadratic form collects squares and cross terms into one matrix. Only the symmetric part of matters. Its gradient is . Its sign pattern decides the shape: positive definite gives a bowl and ellipses, indefinite gives a saddle. This shape controls how easy an optimization problem is, which is why the Gradient and definiteness checks keep reappearing in later lessons.
Check yourself
QuestionRecall: what is the difference between a quadratic form and a linear function of a vector?Show answer
A linear function has only first powers of the coordinates. A quadratic form has products of pairs of coordinates, so it includes squares and cross terms. That extra degree is what makes its graph a bowl or a saddle rather than a flat plane.
QuestionComputation: for and , what is ?Show answer
First . Then . Check: .
QuestionWhy: why does replacing by its symmetric part leave the quadratic form unchanged?Show answer
Because , the entries and always multiply the same product and only their sum matters. The symmetric part has the same sum for each pair, so it produces the same value for every vector.
QuestionApplied: a loss function is with having eigenvalues 4 and . Is the origin a minimum, and why does it matter for gradient descent?Show answer
No. A negative eigenvalue means takes negative values along one direction, so the form is indefinite and the origin is a saddle point, not a minimum. Along the eigenvector for , decreases without limit, so gradient descent started anywhere with a component along that direction runs away instead of settling. A minimum is only guaranteed when all the eigenvalues are positive.