Lesson 3.2.3 multiplied a row, a square matrix and a column to get one number. Use the same vector on both sides and the result is a quadratic form. For a symmetric n×n matrix A and a vector x in Rn, it is
q(x)=x⊤Ax
The shapes are (1×n)(n×n)(n×1), so q(x) is a single number. It is called quadratic because every term multiplies two components of x.
Write it out for A=(abbc) and x=(x1,x2). First Ax=(ax1+bx2,bx1+cx2), then the dot product with x:
A symmetric matrix A turns each vector x into one number. The diagonal of A gives the squared terms; each off-diagonal pair aij=aji gives the cross term 2aijxixj.
Take lesson 3.4.1's matrix A=(3112), so q(x)=3x12+2x1x2+2x22. At x=(1,−1), Ax=(2,−1) and x⊤Ax=2+1=3; the formula gives 3−2+2=3 too.
x
(1,0)
(0,1)
(1,1)
(1,−1)
(2,−1)
(−1,3)
q(x)
3
2
7
3
10
15
Three facts follow from the formula:
Only the symmetric part counts.B=(3022) is not symmetric, but its cross term is (2+0)x1x2, the same as A's. Any square B gives the form of 21(B+B⊤), which is why quadratic forms use symmetric matrices.
Scaling x by c scales q by c2.q(2,−2)=12=4×q(1,−1), and q(−x)=q(x). So the sign of q depends only on the direction of x.
q(0)=0 for every A.
With three or more components the double sum has a term for every pair (i,j): case study 3.2.5 met it as the portfolio variance w⊤Σw.
Try it
q(A, x) is x @ A @ x. The loop checks the table against 3x12+2x1x2+2x22.
Then the non-symmetric B gives the same value as A at (2,−1), and its symmetric part is A. The last line shows the scaling: q(2x)=4q(x) and q(−x)=q(x).
⌘/Ctrl + Enter runs the cell
Four kinds of symmetric matrix
The question that matters is the sign of q(x) over all x=0. Four matrices show the possibilities:
A=(3112),J=(1111)N=(1221),−S=(−2−1−1−2)
Here S=(2112) is lesson 3.2.3's symmetric matrix, and J has every entry 1.
x
(1,0)
(0,1)
(1,1)
(1,−1)
(2,−1)
A: 3x12+2x1x2+2x22
3
2
7
3
10
J: (x1+x2)2
1
1
4
0
1
N: x12+4x1x2+x22
1
1
6
−2
−3
−S: −2x12−2x1x2−2x22
−2
−2
−6
−2
−6
J's form is x12+2x1x2+x22=(x1+x2)2, a square: never negative, and 0 along the whole line x2=−x1. N is positive at (1,1) and negative at (1,−1).
Rule: Definite, semidefinite, indefinite
positive definite:x⊤Ax>0 for every x=0positive semidefinite:x⊤Ax≥0 for every x
Negative definite and negative semidefinite are the same with <0 and ≤0. A matrix whose form takes both signs is indefinite.
Every positive definite matrix is also positive semidefinite. When this module calls a matrix semidefinite, it means that q is also 0 at some x=0, as for J.
So A looks positive definite, J positive semidefinite, N indefinite and −S negative definite. A table can only rule things out: one negative value proves that N is not semidefinite. No finite table proves that A is positive everywhere; the next two sections give tests that do.
One quick check comes free. q(ei)=aii, the i-th diagonal entry, so a positive definite matrix has every diagonal entry positive. That is needed but not enough: N has 1 and 1 on its diagonal and is indefinite.
Try it
The loop prints each row of the table: q at the five points, for each of the four matrices.
int(…) prints whole numbers; each value is already a whole number.
⌘/Ctrl + Enter runs the cell
The eigenvalue test
Lesson 3.6.3 showed that a symmetric matrix has real eigenvalues λ1,…,λn and perpendicular unit eigenvectors q1,…,qn, with A=QΛQ⊤. Here Q has the eigenvectors as its columns and Λ is the diagonal matrix of eigenvalues.
Measure x along the eigenvectors instead of the axes: yi=qi⋅x, or y=Q⊤x. Then x⊤Ax=x⊤QΛQ⊤x=y⊤Λy, and the cross terms disappear:
x⊤Ax=λ1y12+⋯+λnyn2
Take S=(2112). Its eigenvalues are 3, along q1=21(1,1), and 1, along q2=21(1,−1). So y1=2x1+x2, y2=2x1−x2, and
Expand the right side to check: 1.5+0.5=2 for each square, and 3−1=2 for the cross term. At (2,−1) both sides give 6.
A sum λ1y12+⋯+λnyn2 is positive for every y=0 exactly when every λi>0. And y=0 only when x=0, because Q only turns or reflects the plane.
Rule: The eigenvalue test
every λi>0⟺positive definiteevery λi≥0⟺positive semidefinite
All λi<0 means negative definite. Eigenvalues of both signs mean indefinite: q is positive along an eigenvector with λi>0 and negative along one with λi<0.
matrix
eigenvalues
kind
A
1.382, 3.618
positive definite
J
0, 2
positive semidefinite
N
−1, 3
indefinite
−S
−3, −1
negative definite
The eigenvectors show where the signs are. N has λ=−1 along 21(1,−1), and there q=−1. J has λ=0 along (1,−1), the line where (x1+x2)2=0.
For a unit vector, y12+⋯+yn2=1, so q is an average of the eigenvalues with weights yi2. It therefore runs from the smallest eigenvalue to the largest: from 1 to 3 for S, from −1 to 3 for N.
For 2×2 matrices lesson 3.6.1 gives a shortcut: λ1λ2=detA and λ1+λ2 is the trace, the sum of the diagonal. detA>0 means the two eigenvalues have the same sign, and the trace says which. detN=−3<0 means opposite signs: indefinite.
Try it
np.linalg.eigh (lesson 3.6.3) returns the eigenvalues, smallest first, and the eigenvectors as columns. The second line makes each eigenvector's first entry positive, since their signs are arbitrary.
The loop compares q(x) with ∑λiyi2 and with 1.5(x1+x2)2+0.5(x1−x2)2.
⌘/Ctrl + Enter runs the cell
Testing on a computer
np.linalg.eigvalsh returns just the eigenvalues of a symmetric matrix, smallest first: it is eigh without the eigenvectors. Testing "every λ≥0" needs care. A zero eigenvalue often comes out as a tiny number such as 10−16, positive or negative depending on the computer.
So compare with a small tolerance, scaled to the size of the matrix, as lesson 3.4.1 did for determinants. classify below treats any eigenvalue within 10−10 times the largest one as zero.
Try it
classify applies the eigenvalue test with that tolerance. Each line prints the eigenvalues, the trace, the determinant and the verdict.
Adding 0.0 turns a -0. into 0.. The last line evaluates N's form at the unit vector 21(1,−1): the eigenvalue −1.
⌘/Ctrl + Enter runs the cell
The test by leading minors
Eigenvalues are hard to find by hand beyond 2×2. Determinants give a test you can do on paper.
The leading minors of A are the determinants of its top-left corners (lesson 3.4.1). They run from the 1×1 corner a11, through the 2×2 corner, up to detA itself.
Rule: The leading-minor test
2×2:a>0 and ac−b2>0every leading minor>0
A symmetric matrix is positive definite exactly when all its leading minors are positive. It is negative definite exactly when they alternate in sign, starting negative: −,+,−,…
Why it works for 2×2: complete the square. For a=0,
ax12+2bx1x2+cx22=a(x1+abx2)2+aac−b2x22
If a>0 and ac−b2>0, both coefficients are positive. The sum of the two squares is then 0 only if x2=0 and then x1=0.
For A: 3(x1+31x2)2+35x22, which at (1,−1) is 34+35=3, as in the table. Lesson 3.7.2 turns this completed square into a factorisation of A.
Two 3×3 examples. Lesson 3.2.3's
K=4−10−132025
has leading minors 4, 4⋅3−(−1)(−1)=11 and detK=39, all positive: positive definite.
T=212121211
passes the first two, 2 and 3, but detT=−3. So T is not positive definite, and indeed q(1,0,−1)=2−4+1=−1. Its eigenvalues are −0.5758, 1.1873 and 4.3885: indefinite.
Where the test breaks: semidefinite matrices. Leading minors that are ≥0 do not prove semidefinite. E=(000−1) has leading minors 0 and 0, yet q(x)=−x22, which is negative at (0,1). For semidefinite questions, use the eigenvalues.
Try it
leading_minors takes the determinant of each top-left corner, M[:k, :k]. Each line prints the minors, then the eigenvalues.
Then q(1,0,−1) for T, and the completed square for A at (1,−1).
⌘/Ctrl + Enter runs the cell
Level curves: ellipses and hyperbolas
A level curve of q is the set of points where q(x) takes one fixed value, like a contour line on a map. Module 4.1 draws them for any function of two variables; for a quadratic form, the eigenvalues decide their shape.
Along the eigenvector qi, q=λiyi2, so q=1 where yi=1/λi. A large eigenvalue makes q climb fast, so the curve is close to the origin in that direction.
Level curves of S, J and N. Positive definite gives ellipses, here with semi-axes 1/3≈0.58 and 1 for q=1. A zero eigenvalue gives parallel lines, and eigenvalues of both signs give hyperbolas, with q<0 in the shaded sectors.
Positive definite: each level curve q=k>0 is an ellipse, with its axes along the eigenvectors and semi-axes k/λi. Seen as a height over the plane, q is a bowl with its bottom at the origin.
Semidefinite, one zero eigenvalue:q does not grow along that eigenvector, so the curves are parallel lines. The surface is a trough.
Indefinite: the curves are hyperbolas. N has the same eigenvectors as S, and q=3y12−y22. It is 0 on the two lines y2=±3y1, which separate the positive part from the negative part. The surface is a saddle.
Rule: Level curves of a quadratic form
q=1 meets the axis of qiat distance 1/λi if λi>0
All λi>0: ellipses. One λi=0, the other positive: parallel lines. Eigenvalues of both signs: hyperbolas.
Try it
The first loop checks four points of S's ellipses. They are the ends of the axes of q=1, at distances 1/3 and 1, then of q=4, twice as far.
For N, 31q1 is on q=1 and q2 on q=−1; q1+3q2 is on a line where q=0.
⌘/Ctrl + Enter runs the cell
Matrices that are always semidefinite
For any m×n matrix M, lesson 3.2.3 showed that M⊤M is symmetric. Its form is a squared length:
x⊤M⊤Mx=(Mx)⊤(Mx)=∥Mx∥2≥0
So M⊤M is always positive semidefinite. It is positive definite exactly when Mx=0 only for x=0: when the columns of M are independent, with rank n (lesson 3.3.2).
Lesson 3.2.3's M=(132−104) is 2×3, so its rank is at most 2 and M⊤M is only semidefinite:
M⊤M=10−112−15−412−416
At x=(1,1,−1), Mx=(3,−2) and the form is 9+4=13. At x=(−8,4,7), Mx=0 and the form is 0. The eigenvalues are 0, 4.9525 and 26.0475; the smaller matrix MM⊤ has eigenvalues 4.9525 and 26.0475, both positive.
Covariance matrices. In case study 3.2.5, w⊤Σw is the variance of a portfolio, and a variance is never negative. So a covariance matrix must be positive semidefinite.
A sample covariance matrix is built as M⊤M divided by n−1, from the centred returns, so it always is. The three assets' Σ has eigenvalues 0.011741, 0.022502 and 0.075058: positive definite. Case study 3.7.4 uses these tests to catch correlation matrices that cannot be real.
Try it
M⊤M and its form at two points; at (−8,4,7), Mx=0.
The smallest eigenvalue of M⊤M is 0, which prints as a tiny number unless rounded; the check lam.min() >= -1e-12 allows for that. matrix_rank gives 2. Last come MM⊤ and the three assets' Σ.
⌘/Ctrl + Enter runs the cell
Lesson 4.3.2 applies the same tests to the matrix of second derivatives of a function, to tell a minimum from a maximum or a saddle.
Common mistakes
The cross term is 2b. In 3x12+4x1x2+2x22 the off-diagonal entry is 2, not 4: A=(3222).
Positive entries do not make a matrix positive definite.N=(1221) is indefinite; (2−1−12), with negative entries, is positive definite (eigenvalues 1 and 3).
A positive diagonal is needed, not enough. It rules out matrices with a diagonal entry ≤0, nothing more.
A positive determinant is not enough.−S has det=3 and is negative definite. Check every leading minor, or the trace too for 2×2.
Leading minors ≥0 do not prove semidefinite.E=(000−1) passes and is not. Use the eigenvalues.
Use the symmetric matrix.B=(1041) has eigenvalues 1 and 1, yet its form x12+4x1x2+x22 is −2 at (1,−1). Test 21(B+B⊤), whose eigenvalues are −1 and 3.
Allow for rounding. A zero eigenvalue prints as a tiny number of either sign. Compare with a tolerance, never with exactly 0.
Quadratic forms and positive definiteness
0 of 12 correct
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
How to type answers
Powers with ^: x^7, 2^(1/3), a^-2.
Multiply with * or by writing side by side: 3x^2, 2(x+1).
ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
Your first answer to each question is saved: it counts towards your score and brings the question back for review.
Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.
Summary card
A quadratic form is q(x)=x⊤Ax=∑i∑jaijxixj, with A symmetric; for 2×2, ax12+2bx1x2+cx22. q(cx)=c2q(x).
Positive definite: q>0 for every x=0. Positive semidefinite: q≥0. Indefinite: both signs. Negative (semi)definite: the reverse.
Eigenvalue test: q=λ1y12+⋯+λnyn2 along the eigenvectors, so all λi>0 means positive definite and all λi≥0 semidefinite. On unit vectors q runs from λmin to λmax.
Leading-minor test: positive definite exactly when every top-left determinant is positive (a>0, ac−b2>0 for 2×2). It does not test semidefinite.
Level curves: ellipses with semi-axes 1/λi for q=1 (definite), parallel lines (one zero eigenvalue), hyperbolas (indefinite).
M⊤M is always positive semidefinite, since its form is ∥Mx∥2; so is every covariance matrix. In code, use eigvalsh with a tolerance.