Determinant and Trace
Two numbers summarize a square matrix surprisingly well. The determinant says how the matrix scales volume (and whether it is invertible); the trace is the sum of the diagonal. Both equal simple functions of the eigenvalues, and both show up in ML formulas: the Gaussian density contains , and the total variance of a dataset is .
The determinant
For and matrices:
where deletes row 1 and column (Minors and Cofactors). In practice, computers use elimination: the determinant of a triangular matrix is the product of its diagonal.
Geometric meaning. is the factor by which scales areas (2D) or volumes; the sign says whether orientation flips. means squashes space into a lower dimension, so it is not invertible.
Rules worth memorizing: , , , for .
import numpy as np
A = np.array([[2.0, 1.0], [1.0, 3.0]])
B = np.array([[1.0, 4.0], [0.0, -1.0]])
print("det A =", round(np.linalg.det(A), 10), " det B =", round(np.linalg.det(B), 10))
print("det(AB) =", round(np.linalg.det(A @ B), 10), " det(3A) =", round(np.linalg.det(3 * A), 10))
# area of the unit square's image = |det A|
square = np.array([[0, 1, 1, 0], [0, 0, 1, 1]], dtype=float)
x, y = A @ square
area = 0.5 * abs(np.dot(x, np.roll(y, -1)) - np.dot(y, np.roll(x, -1))) # shoelace formula
print("area of the transformed square:", area)det A = 5.0 det B = -1.0
det(AB) = -5.0 det(3A) = 45.0
area of the transformed square: 5.0
The trace
The trace is linear, , and cyclic: . The cyclic rule is a workhorse in ML derivations; for example a scalar equals , which turns a sum of quadratic forms into one trace of a matrix with the data's covariance.
Both from the eigenvalues
If has eigenvalues (counted with multiplicity), then
import numpy as np
rng = np.random.default_rng(0)
A = rng.normal(size=(4, 4))
lam = np.linalg.eigvals(A)
print("det:", round(np.linalg.det(A), 6), " product of eigenvalues:", np.round(np.prod(lam).real, 6))
print("trace:", round(np.trace(A), 6), " sum of eigenvalues:", np.round(np.sum(lam).real, 6))
X = rng.normal(size=(100, 4))
S = np.cov(X.T, bias=True)
print("total variance = tr(S):", np.isclose(np.trace(S), X.var(axis=0).sum()))det: -1.884763 product of eigenvalues: -1.884763
trace: -0.868217 sum of eigenvalues: -0.868217
total variance = tr(S): True
The eigenvalues of a real matrix can be complex (in conjugate pairs), but their product and sum are real.
The log-determinant
Densities like the Gaussian need . Computing first and then the log overflows or underflows for large matrices; np.linalg.slogdet returns the sign and the log of the absolute value directly, and for SPD matrices from the Cholesky factor.
In software
np.linalg.detandslogdetfactorize with LU;slogdetis the one to use inside log-likelihoods.- Normalizing flows track how a transformation changes volume through (Change of Variables).
np.traceand the cyclic trick appear in derivations of PCA and of matrix gradients (Gradients of Matrices).
Exercises
Exercise 1: A determinant
Compute .
Answer
Solution
Expand along the first row:
Exercise 2: Eigenvalues from trace and determinant
A matrix has trace 5 and determinant 6. What are its eigenvalues?
Answer
2 and 3.
Solution
The eigenvalues solve , that is .
Exercise 3: Cyclic trace (code)
For random , , , check , and note the matrices have different sizes.
Answer
Equal; is but is .
Solution
import numpy as np
rng = np.random.default_rng(2)
A, B, C = rng.normal(size=(2, 3)), rng.normal(size=(3, 4)), rng.normal(size=(4, 2))
print((A @ B @ C).shape, (C @ A @ B).shape, np.isclose(np.trace(A @ B @ C), np.trace(C @ A @ B)))(2, 2) (4, 4) True
Test yourself
means…
- is the zero matrix
- is not invertible
- is orthogonal
- all eigenvalues are 1
The trace of a matrix equals…
- the product of its eigenvalues
- the sum of its eigenvalues
- its largest eigenvalue
- its rank
For , equals…