Foundations to Frontier
Tests
A. Mathematical Foundations › A3 Calculus and Analysis › Matrix and vector calculus

Common Matrix-Calculus Identities (xᵀAx, tr, log det, inverse)

A short list of identities covers most of machine learning: quadratic forms, traces, log-determinants and inverses. Derive each from differentials and check it numerically. Then use them to fit Gaussian covariances, tune Gaussian-process kernels, and understand normalizing flows and Kalman filters.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A cheat sheet for matrix derivatives

Just as you memorize 7×8=567\times8 = 56 instead of counting every time, engineers keep a short list of matrix-derivative facts. With about six of them, you can derive the training rules for most machine-learning models. This page builds that cheat sheet, and the demo checks each entry with real numbers.

Two of the stars are the log-determinant and the matrix inverse, which are the matrix versions of ln⁡x\ln x and 1/x1/x.

Matrix-calculus explorer: formula vs finite differences
shapes: A: 2×2, x: 2
rule: ∇f = (A + Aᵀ) x (= 2Ax if A is symmetric) · the gradient has the same shape as the input
input (x or X) · 2×1
-1.100
1.600
closed-form derivative · 2×1
1.920
1.810
finite-difference check · 2×1
1.920
1.810
largest difference: 3.4e-10 ✔ the formula is right
Only the symmetric part of A matters (A2.4-2).
Why engineers care

When a robot learns how noisy its sensors are, it fits a covariance matrix to data. The formula that does this comes straight from two cheat-sheet identities: the derivative of log det and the derivative of the inverse.

📝 Quick check (Level 0)
0/3 answered
  1. 1. det of diag(2, 3)?
  2. 2. The matrix inverse is like the number operation…
  3. 3. Log det is the matrix cousin of…

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

The cheat sheet

Function Derivative Scalar analogue
a⊤x\mathbf a^\top\mathbf x a\mathbf a (ax)′=a(ax)' = a
x⊤Ax\mathbf x^\top A\mathbf x (A+A⊤)x(A + A^\top)\mathbf x (ax2)′=2ax(ax^2)' = 2ax
tr⁡(AX)\operatorname{tr}(AX) A⊤A^\top (ax)′=a(ax)' = a
tr⁡(X⊤AX)\operatorname{tr}(X^\top AX) (A+A⊤)X(A + A^\top)X (ax2)′=2ax(ax^2)' = 2ax
log⁡det⁡X\log\det X X−⊤X^{-\top} (ln⁡x)′=1/x(\ln x)' = 1/x
X−1X^{-1} d(X−1)=−X−1 dX X−1d(X^{-1}) = -X^{-1}\,dX\,X^{-1} (1/x)′=−1/x2(1/x)' = -1/x^2
det⁡X\det X det⁡(X) X−⊤\det(X)\,X^{-\top} Jacobi’s formula

Gradients have the same shape as the variable. The scalar analogues are a memory aid and a sanity check.

📝 Quick check (Level 1)
0/3 answered
  1. 1. ∂ log det X/∂X at X = diag(2, 4): the (2, 2) entry?
  2. 2. ∂tr(AX)/∂X = ?
  3. 3. The scalar analogue of d(X⁻¹) = −X⁻¹dX X⁻¹ is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Deriving the identities with differentials

Inverse. Differentiate XX−1=IXX^{-1} = I: dX X−1+X d(X−1)=0dX\,X^{-1} + X\,d(X^{-1}) = 0, so d(X−1)=−X−1dX X−1d(X^{-1}) = -X^{-1}dX\,X^{-1}.

Determinant (Jacobi’s formula). ddet⁡X=det⁡X⋅tr⁡(X−1dX)d\det X = \det X\cdot\operatorname{tr}(X^{-1}dX), proved at Level 3. So dlog⁡det⁡X=tr⁡(X−1dX)=⟨X−⊤,dX⟩d\log\det X = \operatorname{tr}(X^{-1}dX) = \langle X^{-\top}, dX\rangle, which gives ∂log⁡det⁡X/∂X=X−⊤\partial\log\det X/\partial X = X^{-\top}.

Trace of an inverse. dtr⁡(X−1)=−tr⁡(X−1dX X−1)=−tr⁡(X−2dX)d\operatorname{tr}(X^{-1}) = -\operatorname{tr}(X^{-1}dX\,X^{-1}) = -\operatorname{tr}(X^{-2}dX), so ∂/∂X=−(X−2)⊤\partial/\partial X = -(X^{-2})^\top.

Application: the Gaussian covariance MLE

For nn zero-mean samples with sample covariance SS, the log-likelihood is ℓ(Σ)=−n2(log⁡det⁡Σ+tr⁡(Σ−1S))\ell(\Sigma) = -\frac n2\big(\log\det\Sigma + \operatorname{tr}(\Sigma^{-1}S)\big). Using the log det and inverse identities:

∂ℓ∂Σ=−n2(Σ−1−Σ−1SΣ−1)=0  ⟹  Σ=S\frac{\partial\ell}{\partial\Sigma} = -\frac n2\big(\Sigma^{-1} - \Sigma^{-1}S\Sigma^{-1}\big) = 0\implies\Sigma = S

The maximum-likelihood covariance is the sample covariance. This is how robots fit their sensor-noise models.

🐍 Exercise 1: log det and inverse derivatives
Implement ∂ log det X/∂X = X⁻ᵀ and the directional derivative of the inverse, and check both against finite differences.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The MLE of a Gaussian covariance is…
  2. 2. d(X⁻¹) at X = 2I with dX = I: the (1, 1) entry?
  3. 3. Jacobi: d det X = ?

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Proof of Jacobi’s formula

det⁡(X+dX)=det⁡X⋅det⁡(I+X−1dX)\det(X + dX) = \det X\cdot\det(I + X^{-1}dX). For small E=X−1dXE = X^{-1}dX, det⁡(I+E)=∏(1+μi)=1+∑μi+O(∥E∥2)=1+tr⁡E+⋯\det(I + E) = \prod(1 + \mu_i) = 1 + \sum\mu_i + O(\lVert E\rVert^2) = 1 + \operatorname{tr}E + \cdots, where μi\mu_i are the eigenvalues of EE. So ddet⁡X=det⁡Xtr⁡(X−1dX)d\det X = \det X\operatorname{tr}(X^{-1}dX). ∎

Eigenvalue derivatives

For symmetric AA with a simple eigenvalue λi\lambda_i and unit eigenvector qi\mathbf q_i: dλi=qi⊤dA qid\lambda_i = \mathbf q_i^\top dA\,\mathbf q_i, the first-order perturbation formula of A2.4-4. In particular, ∂λmax⁡/∂A=q1q1⊤\partial\lambda_{\max}/\partial A = \mathbf q_1\mathbf q_1^\top, used in spectral-norm regularization.

Concavity of log det

Along X+tDX + tD (symmetric DD, PD XX), d2dt2log⁡det⁡=−tr⁡((X−1D)2)≤0\frac{d^2}{dt^2}\log\det = -\operatorname{tr}\big((X^{-1}D)^2\big)\le0. log det is concave on positive-definite matrices. That is why maximum-likelihood covariance and precision estimation (including the graphical lasso) are well-behaved convex problems.

Exercise 3.1: Show that ∂ det X/∂X = det(X) X⁻ᵀ, and check it on diag(2, 3)

From Jacobi, ddet⁡X=det⁡X ⟨X−⊤,dX⟩d\det X = \det X\,\langle X^{-\top}, dX\rangle. For X=diag⁡(2,3)X = \operatorname{diag}(2,3): 6⋅diag⁡(1/2,1/3)=diag⁡(3,2)6\cdot\operatorname{diag}(1/2, 1/3) = \operatorname{diag}(3,2). Check: ∂(x11x22)/∂x11=x22=3\partial(x_{11}x_{22})/\partial x_{11} = x_{22} = 3. ✓

📝 Quick check (Level 3)
0/3 answered
  1. 1. log det on PD matrices is…
  2. 2. ∂λ_max/∂A (simple eigenvalue) = ?
  3. 3. det(I + E) ≈ ?

Level 4: Expert

★★★★★ Expert⏱ 45 minResearchers: original papers and open problems.

Tuning Gaussian processes by gradient

A Gaussian process’s evidence is log⁡p(y)=−12y⊤K−1y−12log⁡det⁡K−n2log⁡2π\log p(\mathbf y) = -\frac12\mathbf y^\top K^{-1}\mathbf y - \frac12\log\det K - \frac n2\log2\pi. With the cheat sheet, its gradient with respect to any kernel hyperparameter θ\theta is

∂log⁡p∂θ=12tr⁡((αα⊤−K−1)∂K∂θ),α=K−1y\frac{\partial\log p}{\partial\theta} = \frac12\operatorname{tr}\Big((\boldsymbol\alpha\boldsymbol\alpha^\top - K^{-1})\frac{\partial K}{\partial\theta}\Big), \qquad \boldsymbol\alpha = K^{-1}\mathbf y

(Rasmussen and Williams, 2006). This is how GPs used for robot dynamics models and Bayesian optimization learn their length-scales.

🐍 Exercise 2 (research): learn a Gaussian process length-scale
Code the GP log marginal likelihood and its analytic gradient with respect to the RBF length-scale ℓ. Gradient-check it, then run gradient ascent to recover ℓ ≈ 0.5 from data generated with 0.5.

Where these identities appear

  • Normalizing flows and change of variables: log⁡∣det⁡J∣\log\lvert\det J\rvert terms and their gradients (A3.2-5). Architectures make JJ triangular so that log⁡det⁡\log\det is a cheap sum.
  • Kalman filters and SLAM: covariance updates P+=(I−KH)PP^+ = (I - KH)P and information-form updates rely on inverse and determinant identities. Derivatives of the log-likelihood with respect to noise parameters let robots learn their noise models (Barfoot, 2017).
  • Sparse precision estimation: the graphical lasso maximizes log⁡det⁡Θ−tr⁡(SΘ)−ρ∥Θ∥1\log\det\Theta - \operatorname{tr}(S\Theta) - \rho\lVert\Theta\rVert_1 (Friedman, Hastie and Tibshirani, 2008). Concavity of log det makes it tractable.
  • Gaussian policies in RL: the gradient of log⁡N(a;μ,Σ)\log\mathcal N(\mathbf a;\boldsymbol\mu,\Sigma) with respect to Σ\Sigma uses exactly these two identities.

References

  1. C. G. J. Jacobi (1841). “De formatione et proprietatibus determinantium.” Journal für die reine und angewandte Mathematik, 22.
  2. K. B. Petersen, M. S. Pedersen (2012). The Matrix Cookbook.
  3. C. Rasmussen, C. Williams (2006). Gaussian Processes for Machine Learning. MIT Press. Chapter 5.
  4. J. Friedman, T. Hastie, R. Tibshirani (2008). “Sparse inverse covariance estimation with the graphical lasso.” Biostatistics, 9(3).
  5. T. Barfoot (2017). State Estimation for Robotics. Cambridge University Press.
  6. J. Magnus, H. Neudecker (2019). Matrix Differential Calculus, 3rd ed. Wiley.