Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Adding up the diagonal
Number a 3 × 3 board 1 to 9, row by row. Walk diagonally from the top-left corner to the bottom-right and add what you step on: 1 + 5 + 9 = 15. That sum is called the trace.
A sum that behaves nicely
If you add two boards square by square, their traces add too. Double every number, and the trace doubles. And here’s the surprise: even when two matrices give different answers in different orders (), their traces agree, . Small facts like this turn out to be very useful.
- 1. A 3 × 3 board numbered 1–9 row by row. Sum of the top-left to bottom-right diagonal?
- 2. Board A has diagonal sum 4 and board B has 6. The diagonal sum of A + B?
- 3. Does the trace use the numbers off the diagonal?
Level 1: Beginner
★★☆☆☆ Basic⏱ 10 minSecondary-school mathematics: arithmetic and elementary algebra.Definition
For a square matrix, . For example, , and .
Basic rules
So the trace is a linear function of a matrix.
- 1. tr I₇ = ?
- 2. tr A = 5. tr(3A) = ?
- 3. Trace is defined for…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.The cyclic property
This holds even when and aren’t square, as long as both products are square. You can rotate a product cyclically, but you can’t reorder it arbitrarily: in general. With and , , but both traces are 69.
Useful identities
| Identity | Why it matters |
|---|---|
| a scalar is its own trace, so the “trace trick” applies | |
| Frobenius norm: multi-output squared losses, weight decay | |
| the sum of the eigenvalues (A2.3) | |
| the inner product of matrices |
The trace trick in ML and statistics
A scalar like is a matrix, so it equals its own trace. Then . Taking an expectation gives . This one-line move appears in derivations of the multivariate Gaussian MLE, PCA, the bias–variance decomposition, and Kalman filters.
The Kalman gain is chosen to minimize , the trace of the state covariance, which is the total expected squared estimation error (T1). Satellites, drones and rovers all estimate their state this way.
- 1. tr(ABC) always equals…
- 2. tr(x xᵀ) for x = (1, 2, 3)?
- 3. ‖A‖²_F for A = [[1, 2], [3, 4]]?
Level 3: Advanced
★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.Proof of the cyclic property
For and : .
Proof. . Swapping finite sums is allowed (A1-3). ∎ Applying it to and gives .
Trace is basis-independent
Similar matrices have the same trace: .
Proof. . ∎ So the trace belongs to the linear map, not to the coordinates. That’s why it equals the sum of the eigenvalues (see A2.3), and why physical quantities built from it don’t depend on the frame.
Consequences
- No matrices satisfy : the left side has trace 0 and the right side has trace . (In quantum mechanics, this is why position and momentum need infinite-dimensional spaces.)
- Projections: if , then . Projecting onto a 3-dimensional subspace has trace 3.
- Rotations: a 3D rotation by angle has , whatever its axis. So . This is the standard way to measure the attitude error between two orientations in robotics and spacecraft (R2).
Exercise 3.1: A 3D rotation has trace 0. What is its angle?
, so and .
- 1. tr(AB − BA) = ?
- 2. A projection onto a 4-dimensional subspace of ℝ¹⁰ has trace…
- 3. A 2D rotation by 60° has trace…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Estimating a trace without the matrix
In large-scale ML you often can’t form a matrix, such as the Hessian of a network with a billion parameters, but you can multiply it by a vector. Hutchinson’s estimator (1989) uses random vectors with independent ±1 entries:
That’s the trace trick again. Averaging over probes gives an error that shrinks like . Improvements such as Hutch++ (Meyer et al., 2021) do better. Uses:
- Hessian traces to measure sharpness and curvature of the loss landscape (B4), and in second-order optimizers.
- Log-determinants, , for Gaussian processes (I2) and normalizing flows (H5), via stochastic Lanczos quadrature.
- FFJORD / continuous normalizing flows, which need at every step. Grathwohl et al. (2019) use exactly the Hutchinson estimator.
Traces in matrix calculus
Many gradients are easiest to derive by writing the loss as a trace and using together with the cyclic property. For example, the gradient of with respect to is , which is the backbone of linear regression and of backprop through a linear layer (A3.3, Practice Code 5).
References
- M. Hutchinson (1989). “A stochastic estimator of the trace of the influence matrix for Laplacian smoothing splines.” Communications in Statistics: Simulation and Computation, 18(3).
- K. B. Petersen and M. S. Pedersen (2012). The Matrix Cookbook. Technical University of Denmark. (Trace identities and derivatives.)
- W. Grathwohl, R. Chen, J. Bettencourt, I. Sutskever, D. Duvenaud (2019). “FFJORD: Free-form continuous dynamics for scalable reversible generative models.” ICLR.
- R. Meyer, C. Musco, C. Musco, D. Woodruff (2021). “Hutch++: Optimal stochastic trace estimation.” SIAM Symposium on Simplicity in Algorithms (SOSA).
- R. Kalman (1960). “A new approach to linear filtering and prediction problems.” Journal of Basic Engineering, 82(1).