• 0. Introduction
  • 1. Weighted means
  • 2. Geometry of weighted sums
  • 3. Matrix as a function
  • 4. Rank and reversibility
  • 5. Barycentric coordinates
  • 6. Matrix-matrix multiplication
  • 7. Linear transformations
  • 8. Polynomials are vectors in disguise
  • 9. Bezier curves
  • 10. Dot product
  • 11. Matrix transpose
  • 12. Rotations
  • 13. Normal vectors
  • 14. Eigenvectors and eigenvalues
  • 15. Eigendecomposition
  • 16. Eigenvectors of symmetric matrices
  • 17. Powers of a matrix
  • 18. Fibonacci numbers
  • 19. Markov matrices
  • 20. Projection matrix
  • 21. Symmetric matrix-vector product
  • 22. Positive symmetric definite matrices
  • 23. SVD
  • 24. Dimension reduction
  • 25. What next
unitgridOpacitycanvasColororiginXoriginY

22. Positive symmetric definite matrices

  • 22.1 Angle between the input and output vectors of a symmetric matrix
  • 22.2 Properties of symmetric PSD matrices
    • 22.2.1 All eigenvalues are non-negative
    • 22.2.2 Sum of two symmetric PSD matrices is a symmetric PSD matrix
    • 22.2.3 Product of a symmetric PSD matrix and a non-negative scalar is a symmetric PSD matrix
    • 22.2.4 ATAA^TAATA is a symmetric PSD matrix for any matrix AAA
    • 22.2.5 Diagonal elements of a symmetric PSD matrix are non-negative
  • 22.3 Symmetric PSD matrices induce a partial order over matrices
  • 22.4 Visualizing xTMxx^TMxxTMx
  • 22.5 Decomposition of a symmetric PSD matrix
  • 22.6 PSD matrices as analogues of positive numbers
    • 22.6.1 Product
    • 22.6.2 Square roots
    • 22.6.3 Positive scalars become symmetric PSD matrices in higher dimensions
  • 22.7 Applications of symmetric PSD matrices
    • 22.7.1 Symmetric PSD matrices are everywhere
    • 22.7.2 Optimization
    • 22.7.3 It defines a metric
Review

Symmetric matrices are the good kind of square matrices: they always have real eigenvalues and their eigenvectors are all perpendicular to each other. The previous chapter showed that multiplying a symmetric matrix with a vector amounts to projecting the vector on the eigenvectors and scaling each projection by its eigenvalue.

There is in fact an even better kind of symmetric matrix, a positive semi-definite matrix, in the sense that its eigenvalues are always non-negative.


22.1 Angle between the input and output vectors of a symmetric matrix

Consider a matrix-vector product MxMxMx for a symmetric matrix MMM and a vector xxx. We are interested in examining the angle between the input vector xxx and the output vector MxMxMx.

  • If the angle between xxx and MxMxMx is less than or equal to 90°, the dot product ⟨x,Mx⟩\langle x, Mx \rangle⟨x,Mx⟩ would be non-negative. In other words, xT(Mx)≥0x^T(Mx) \ge 0xT(Mx)≥0 or simply xTMx≥0x^TMx \ge 0xTMx≥0.
  • If the angle between the input and output vectors is more than 90°, we have xTMx<0x^TMx < 0xTMx<0.

A symmetric matrix MMM for which we have xTMx≥0x^TMx \ge 0xTMx≥0 for every xxx is called a symmetric positive semi-definite matrix.

Let's see an example of a matrix that is symmetric but not positive semi-definite, and then an example of a positive semi-definite matrix.

First take M1=[1.5−1.5−1.5−0.9]M_1 = \begin{bmatrix} 1.5 & -1.5 \\ -1.5 & -0.9 \end{bmatrix}M1​=[1.5−1.5​−1.5−0.9​].

  • Draw the unit circle and its map under M1M_1M1​.
import math

pie.clear()
pie.scalar(60, out='unit')
GRAY, ROSE, GOLD = '#71717a', '#e11d48', '#ca8a04'
M1 = [[1.5, -1.5], [-1.5, -0.9]]
p0 = pie.point(0, 0, out='p0', hidden=True)
pie.circle(p0, 1, out='circ', stroke=GRAY)
pie.la_mat_to_ellipse(M1, p0, out='elli', stroke=GRAY)
  • Now pick one input vector xxx on the circle shown in gold, and its output vector M1xM_1xM1​x in rose.
tdeg = pie.scalar(0, out='tdega')
# x is the unit vector at tdeg degrees; Mx is M times it, and k scales either one
def input_output(tdeg, k, which, axis):
    x, y = math.cos(math.radians(tdeg)), math.sin(math.radians(tdeg))
    if which == 'out':
        x, y = M1[0][0] * x + M1[0][1] * y, M1[1][0] * x + M1[1][1] * y
    return k * (x if axis == 'x' else y)

coord = pie.lift(input_output, pie.Scalar)
pie.la_vec2d(coord(tdeg, 1, 'in', 'x'), coord(tdeg, 1, 'in', 'y'), out='xveca', stroke=GOLD)
pie.la_vec2d(coord(tdeg, 1, 'out', 'x'), coord(tdeg, 1, 'out', 'y'), out='yveca', stroke=ROSE)
pie.text('x', coord(tdeg, 1.2, 'in', 'x'), coord(tdeg, 1.2, 'in', 'y'), 14, out='labx_box_a', stroke=GOLD)
pie.text('M₁x', coord(tdeg, 1.12, 'out', 'x'), coord(tdeg, 1.12, 'out', 'y'), 14, out='laby_box_a', stroke=ROSE)
pie.text('Watch the readout: it goes negative once the angle between x and M₁x passes 90°, so M₁ is not PSD.', 3.6, -3.2, 12, 2.4, -1, out='capa_box', stroke=GRAY)
  • Show a live readout of xTMxx^TMxxTMx at the top left.
def quadratic_form(tdeg):
    x, y = math.cos(math.radians(tdeg)), math.sin(math.radians(tdeg))
    q = x * (M1[0][0] * x + M1[0][1] * y) + y * (M1[1][0] * x + M1[1][1] * y)
    return f'xᵀMx = {q:.2f}'

pie.text(pie.lift(quadratic_form, pie.Text)(tdeg), -2.6, 2.8, 14, out='lbla', stroke=GRAY)

Now rotate the input vector through a full turn and watch the readout: xTMxx^TMxxTMx dips below zero whenever the output swings more than 90° away from the input. So M1M_1M1​ is not positive semi-definite.

pie.translate_scalar_to(tdeg, 360, animate=True)

You can reset the input vector and replay.

pie.translate_scalar_to(tdeg, 0, animate=True)

Now take M2=[1.22−0.43−0.431.865]M_2 = \begin{bmatrix} 1.22 & -0.43 \\ -0.43 & 1.865 \end{bmatrix}M2​=[1.22−0.43​−0.431.865​].

  • Draw the unit circle and its map under M2M_2M2​.
import math

pie.clear()
pie.scalar(60, out='unit')
GRAY, ROSE, GOLD = '#71717a', '#e11d48', '#ca8a04'
M2 = [[1.22, -0.43], [-0.43, 1.865]]
p0 = pie.point(0, 0, out='p0', hidden=True)
pie.circle(p0, 1, out='circ', stroke=GRAY)
pie.la_mat_to_ellipse(M2, p0, out='elli', stroke=GRAY)
  • Then an input vector xxx in gold and its output M2xM_2xM2​x in rose.
tdeg = pie.scalar(0, out='tdegb')
# x is the unit vector at tdeg degrees; Mx is M times it, and k scales either one
def input_output(tdeg, k, which, axis):
    x, y = math.cos(math.radians(tdeg)), math.sin(math.radians(tdeg))
    if which == 'out':
        x, y = M2[0][0] * x + M2[0][1] * y, M2[1][0] * x + M2[1][1] * y
    return k * (x if axis == 'x' else y)

coord = pie.lift(input_output, pie.Scalar)
pie.la_vec2d(coord(tdeg, 1, 'in', 'x'), coord(tdeg, 1, 'in', 'y'), out='xvecb', stroke=GOLD)
pie.la_vec2d(coord(tdeg, 1, 'out', 'x'), coord(tdeg, 1, 'out', 'y'), out='yvecb', stroke=ROSE)
pie.text('x', coord(tdeg, 1.2, 'in', 'x'), coord(tdeg, 1.2, 'in', 'y'), 14, out='labx_box_b', stroke=GOLD)
pie.text('M₂x', coord(tdeg, 1.15, 'out', 'x'), coord(tdeg, 1.15, 'out', 'y'), 14, out='laby_box_b', stroke=ROSE)
  • Show the live readout of xTMxx^TMxxTMx again.
def quadratic_form(tdeg):
    x, y = math.cos(math.radians(tdeg)), math.sin(math.radians(tdeg))
    q = x * (M2[0][0] * x + M2[0][1] * y) + y * (M2[1][0] * x + M2[1][1] * y)
    return f'xᵀMx = {q:.2f}'

pie.text(pie.lift(quadratic_form, pie.Text)(tdeg), -2.6, 2.8, 14, out='lblb', stroke=GRAY)
pie.text('Watch the readout: it stays positive at every angle between x and M₂x, so M₂ is PSD.', 3.6, -3.2, 12, 2.4, -1, out='capb_box', stroke=GRAY)
  • Rotate the input vector through a full turn: this time xTMxx^TMxxTMx stays positive no matter where xxx points, so M2M_2M2​ is positive semi-definite.
pie.translate_scalar_to(tdeg, 360, animate=True)
  • Reset the input vector to replay.
pie.translate_scalar_to(tdeg, 0, animate=True)

22.2 Properties of symmetric PSD matrices

Just from one property, xTMx≥0x^TMx \ge 0xTMx≥0 for all xxx, we can deduce other properties of symmetric positive semi-definite (PSD) matrices.


22.2.1 All eigenvalues are non-negative

Since MMM is symmetric, we know that its eigenvalues are real. Now for any eigenvalue λ\lambdaλ and its corresponding eigenvector vvv, we have:

vTMv≥0⇒ vT(λv⏟Mv)≥0⇒ λ⋅∥v∥2≥0\begin{aligned} v^TMv &\ge 0 \\ \Rightarrow \, v^T(\underbrace{\lambda v}_{Mv}) &\ge 0 \\ \\ \Rightarrow \, \lambda \cdot \|v\|^2 &\ge 0 \end{aligned}vTMv⇒vT(Mvλv​​)⇒λ⋅∥v∥2​≥0≥0≥0​

Since ∥v∥2\|v\|^2∥v∥2 is always positive, we have λ≥0\lambda \ge 0λ≥0.


22.2.2 Sum of two symmetric PSD matrices is a symmetric PSD matrix

This also follows directly from the definition. Given two PSD matrices AAA and BBB:

  • xTAx≥0x^TAx \ge 0xTAx≥0 and xTBx≥0x^TBx \ge 0xTBx≥0, so xTAx+xTBx≥0x^TAx + x^TBx \ge 0xTAx+xTBx≥0
  • which gives us xT(A+B)x≥0x^T(A+B)x \ge 0xT(A+B)x≥0

Thus, A+BA+BA+B is also PSD.


22.2.3 Product of a symmetric PSD matrix and a non-negative scalar is a symmetric PSD matrix

Given a scalar c≥0c \ge 0c≥0 and a PSD matrix MMM, we have:

c⋅(xTMx)≥0⇒ xT(cM)x≥0\begin{aligned} c \cdot (x^TMx) &\ge 0 \\ \Rightarrow \, x^T(cM)x &\ge 0 \end{aligned}c⋅(xTMx)⇒xT(cM)x​≥0≥0​

In other words, cMcMcM is also PSD.


22.2.4 ATAA^TAATA is a symmetric PSD matrix for any matrix AAA

This comes from the observation that:

xT(ATA)x=(xTAT)(Ax)=(Ax)T(Ax)=∥Ax∥2⏟squared norm≥0\begin{aligned} x^T(A^TA)x &= (x^TA^T)(Ax) \\ &= (\textcolor{#0d9488}{Ax})^T(\textcolor{#0d9488}{Ax}) \\ &= \underbrace{\|\textcolor{#0d9488}{Ax}\|^2}_{\text{squared norm}} \ge 0 \end{aligned}xT(ATA)x​=(xTAT)(Ax)=(Ax)T(Ax)=squared norm∥Ax∥2​​≥0​

Question

Does this also imply that for any matrix AAA, the matrix AATAA^TAAT is PSD?

Yes. Apply the result above to the matrix B=ATB = A^TB=AT: the matrix BTB=(AT)TAT=AATB^TB = (A^T)^TA^T = AA^TBTB=(AT)TAT=AAT is PSD.


22.2.5 Diagonal elements of a symmetric PSD matrix are non-negative

To prove that the iii-th diagonal element MiiM_{ii}Mii​, the element in the iii-th row and iii-th column, is non-negative, we just choose a special vector xxx whose iii-th value is 1 and every other value is 0.

  • We have x=[0⋯10⋯]Tx = \begin{bmatrix} 0 & \cdots & 1 & 0 & \cdots \end{bmatrix}^Tx=[0​⋯​1​0​⋯​]T. This gives us Mx=i-th column of MMx = i\text{-th column of } MMx=i-th column of M.
  • This means that xTMx=xT(i-th column of M)=Miix^TMx = x^T(i\text{-th column of } M) = M_{ii}xTMx=xT(i-th column of M)=Mii​.
  • Since xTMx≥0x^TMx \ge 0xTMx≥0, we have Mii≥0M_{ii} \ge 0Mii​≥0.

22.3 Symmetric PSD matrices induce a partial order over matrices

We can compare (and thus order) real numbers (eg 5>25 > 25>2) but there is no good way to compare matrices. PSD matrices provide a way to compare some pairs of matrices AAA and BBB.

Given matrices AAA and BBB, we say that A≥BA \ge BA≥B if A−BA-BA−B is a PSD matrix.

In other words, A≥BA \ge BA≥B if xT(A−B)x≥0x^T(A-B)x \ge 0xT(A−B)x≥0 for all xxx.


22.4 Visualizing xTMxx^TMxxTMx

We can visualize how a matrix MMM maps points on a circle to points on the real number line via the function xTMxx^TMxxTMx. To show this mapping, we draw a line from the input, a point on the circle, to the output, the point (xTMx,0)(x^TMx, 0)(xTMx,0) on the x-axis.

First for the non-PSD matrix M1=[1.5−1.5−1.5−0.9]M_1 = \begin{bmatrix} 1.5 & -1.5 \\ -1.5 & -0.9 \end{bmatrix}M1​=[1.5−1.5​−1.5−0.9​]:

import math
import numpy as np

pie.clear()
pie.scalar(100, out='unit')
GRAY, ROSE = '#71717a', '#e11d48'
M1 = [[1.5, -1.5], [-1.5, -0.9]]
pie.circle(pie.point(0, 0, out='p0', hidden=True), 1, out='circ', stroke=GRAY)
# xᵀMx for each column (x, y) of points on the unit circle
def quadratic(m, x, y):
    return m[0][0] * x * x + (m[0][1] + m[1][0]) * x * y + m[1][1] * y * y

... draw a line from each point xxx on the unit circle to the number xTM1xx^TM_1xxTM1​x on the real line.

angles = np.radians(np.arange(50) * 360 / 50)
px, py = np.cos(angles), np.sin(angles)
q = quadratic(M1, px, py)
pie.la_vec2d((q - px).tolist(), (-py).tolist(), px.tolist(), py.tolist(), out='famc', stroke=GRAY)

Note how the lines land on both sides of the origin.

To follow one pair, show the input vector xxx and its output xTM1xx^TM_1xxTM1​x on the real line in rose.

tdeg = pie.scalar(40, out='tdegc')
cos_d = pie.lift(lambda d: math.cos(math.radians(d)), pie.Scalar)(tdeg)
sin_d = pie.lift(lambda d: math.sin(math.radians(d)), pie.Scalar)(tdeg)
pie.la_vec2d(cos_d, sin_d, out='xvc', stroke=ROSE)
pie.la_vec2d(pie.lift(lambda d: quadratic(M1, math.cos(math.radians(d)), math.sin(math.radians(d))), pie.Scalar)(tdeg), 0, out='qvc', stroke=ROSE)
pie.text('Each line sends a point x on the unit circle to the number xᵀM₁x on the real line. Lines landing left of the origin mean xᵀM₁x < 0, so M₁ is not PSD.', 0, -2, 12, 4, -1, out='sumboxc', stroke=GRAY)

Then rotate the input around the circle: the output arrow keeps crossing to the negative side of the real line, confirming that xTM1x<0x^TM_1x < 0xTM1​x<0 for some inputs.

pie.translate_scalar_to(tdeg, 40 + 360, animate=True)

Reset to replay.

pie.translate_scalar_to(tdeg, 40, animate=True)

Now the same picture for the PSD matrix M2=[1.22−0.43−0.431.865]M_2 = \begin{bmatrix} 1.22 & -0.43 \\ -0.43 & 1.865 \end{bmatrix}M2​=[1.22−0.43​−0.431.865​]:

import math
import numpy as np

pie.clear()
pie.scalar(100, out='unit')
GRAY, ROSE = '#71717a', '#e11d48'
M2 = [[1.22, -0.43], [-0.43, 1.865]]
# xᵀMx for each column (x, y) of points on the unit circle
def quadratic(m, x, y):
    return m[0][0] * x * x + (m[0][1] + m[1][0]) * x * y + m[1][1] * y * y

... draw a line from each point xxx on the unit circle to the number xTM2xx^TM_2xxTM2​x on the real line.

angles = np.radians(np.arange(24) * 360 / 24)
px, py = np.cos(angles), np.sin(angles)
q = quadratic(M2, px, py)
pie.la_vec2d((q - px).tolist(), (-py).tolist(), px.tolist(), py.tolist(), out='famd', stroke=GRAY)

This time every line lands on the positive side. Show the input vector xxx and its output xTM2xx^TM_2xxTM2​x in rose.

tdeg = pie.scalar(40, out='tdegd')
cos_d = pie.lift(lambda d: math.cos(math.radians(d)), pie.Scalar)(tdeg)
sin_d = pie.lift(lambda d: math.sin(math.radians(d)), pie.Scalar)(tdeg)
pie.la_vec2d(cos_d, sin_d, out='xvd', stroke=ROSE)
pie.la_vec2d(pie.lift(lambda d: quadratic(M2, math.cos(math.radians(d)), math.sin(math.radians(d))), pie.Scalar)(tdeg), 0, out='qvd', stroke=ROSE)
pie.text('Each line sends a point x on the unit circle to the number xᵀM₂x on the real line. Every line lands on the positive side, so M₂ is PSD.', 0, -2, 12, 4, -1, out='sumboxd', stroke=GRAY)

Rotate the input around the circle: the output arrow never leaves the positive real line, since xTM2x>0x^TM_2x > 0xTM2​x>0 for every xxx.

pie.translate_scalar_to(tdeg, 40 + 360, animate=True)

Reset to replay.

pie.translate_scalar_to(tdeg, 40, animate=True)

22.5 Decomposition of a symmetric PSD matrix

The fact that the eigenvalues of a PSD matrix are all non-negative allows one to decompose a PSD matrix MMM as M=AATM = AA^TM=AAT for some matrix AAA.

Since MMM is symmetric, we can write it as M=VDVTM = VDV^TM=VDVT. Here, the columns of VVV are (unit) eigenvectors of MMM and the diagonal values of the diagonal matrix DDD are (non-negative) eigenvalues of MMM. Since the diagonal values of DDD are all non-negative, we can write D=D1/2D1/2D = D^{1/2}D^{1/2}D=D1/2D1/2 where D1/2D^{1/2}D1/2 is also a diagonal matrix such that the iii-th diagonal value of D1/2D^{1/2}D1/2 is the square root of the iii-th diagonal value of DDD.

Also note that since D1/2D^{1/2}D1/2 is a diagonal matrix, it is also symmetric. Thus (D1/2)T=D1/2(D^{1/2})^T = D^{1/2}(D1/2)T=D1/2. This gives us:

M=VDVT=VD1/2D1/2VT=VD1/2(D1/2)TVT=(VD1/2)⏟A(VD1/2)T⏟AT\begin{aligned} M &= \textcolor{#1d4ed8}{V}D\textcolor{#be185d}{V^T} \\ &= \textcolor{#1d4ed8}{V}\textcolor{#1d4ed8}{D^{1/2}}\textcolor{#be185d}{D^{1/2}}\textcolor{#be185d}{V^T} \\ &= \textcolor{#1d4ed8}{V}\textcolor{#1d4ed8}{D^{1/2}}\textcolor{#be185d}{(D^{1/2})^T}\textcolor{#be185d}{V^T} \\ &= \underbrace{\textcolor{#1d4ed8}{(VD^{1/2})}}_{\textcolor{#1d4ed8}{A}}\underbrace{\textcolor{#be185d}{(VD^{1/2})^T}}_{\textcolor{#be185d}{A^T}} \end{aligned}M​=VDVT=VD1/2D1/2VT=VD1/2(D1/2)TVT=A(VD1/2)​​AT(VD1/2)T​​​

Writing VD1/2=A\textcolor{#1d4ed8}{VD^{1/2}} = \textcolor{#1d4ed8}{A}VD1/2=A, we get M=AATM = \textcolor{#1d4ed8}{A}\textcolor{#be185d}{A^T}M=AAT.

Note that here each column of A\textcolor{#1d4ed8}{A}A is an eigenvector of MMM scaled by the square root of its corresponding eigenvalue.


22.6 PSD matrices as analogues of positive numbers

A common way to introduce symmetric PSD matrices is to compare them with positive numbers: they are high-dimensional analogues of positive real numbers. I didn't want to start out using this analogy but the points below justify the comparison.


22.6.1 Product

Given a real number ccc and a positive number ppp, the value c⋅p⋅cc \cdot p \cdot cc⋅p⋅c is always non-negative. Similarly for any vector xxx and a PSD matrix MMM, the dot product ⟨x,Mx⟩\langle x, Mx \rangle⟨x,Mx⟩ is always non-negative.


22.6.2 Square roots

A square root exists for a positive number ppp. In other words, p=c⋅cp = c \cdot cp=c⋅c for some real number ccc. Similarly we can write M=AATM = AA^TM=AAT for a PSD matrix MMM. The reverse is also true: the product of any number with itself is non-negative, c⋅c≥0c \cdot c \ge 0c⋅c≥0. Similarly for any matrix AAA, the matrix AATAA^TAAT is always PSD.


22.6.3 Positive scalars become symmetric PSD matrices in higher dimensions

Variance of a random variable is always non-negative. In higher dimensions, the covariance matrix is always PSD. The diagonal values in a covariance matrix represent variances of the random variables in those corresponding dimensions.


22.7 Applications of symmetric PSD matrices

PSD matrices have a lot of applications in both pure and applied math. This is a rich area of math and this chapter only provides a basic introduction to symmetric positive semi-definite matrices.


22.7.1 Symmetric PSD matrices are everywhere

  • The Laplacian matrix is a PSD matrix. It forms the basis of spectral geometry.
  • As we mentioned earlier, the covariance matrix is also PSD.
  • In machine learning, the kernel trick induces a similarity matrix over points in a dataset that is also PSD.

22.7.2 Optimization

There is a rich connection between convex optimization and PSD matrices. In fact, given a PSD matrix AAA, the Hessian of the multi-variable function xTAxx^TAxxTAx, where xxx is the vector of variables of the function, is AAA itself (up to a scale).

Semi-definite programming is an area of convex optimization where the properties of PSD matrices are used to optimize a linear function.


22.7.3 It defines a metric

Roughly speaking, a metric defined on a space (a set of points) is a function ddd that takes two elements from that space and returns a non-negative number. The most common example of a metric is the notion of distance that we use in our daily lives. A metric has these properties:

  • d(x,x)=0d(x,x) = 0d(x,x)=0
  • d(x,y)>0d(x,y) > 0d(x,y)>0 if x≠yx \ne yx=y
  • d(x,z)≤d(x,y)+d(y,z)d(x,z) \le d(x,y) + d(y,z)d(x,z)≤d(x,y)+d(y,z)

It is easy to define a metric using a (positive definite) matrix MMM: given two elements u,vu, vu,v the metric is defined as d(u,v)=(u−v)TM(u−v)d(u,v) = (u-v)^TM(u-v)d(u,v)=(u−v)TM(u−v).


← 21. Symmetric matrix-vector product · 23. SVD →