Quant Memo
Core

Matrix Calculus and Derivatives

Rules for taking derivatives of expressions involving vectors and matrices directly, without unrolling everything into scalar components first — essential for deriving optimizers like linear regression's closed-form solution.

Prerequisites: Derivatives and Differentiation Rules, Quadratic Forms

Deriving the formula for ordinary least squares regression by writing out every term in the sum of squared residuals, then differentiating with respect to each coefficient one at a time, is a slog that gets worse as the number of predictors grows. Matrix calculus lets you skip that entirely: treat the whole regression objective as a single expression in vectors and matrices, differentiate it as one object, and get the answer — the normal equations — in a few lines instead of pages of scalar bookkeeping.

An analogy: assembling furniture with the instruction sheet, not from scratch

You could figure out how to build a bookshelf by reasoning from raw physics about every screw and joint. Or you could use the pre-worked instruction sheet that tells you exactly what step 4 produces, without re-deriving it. Matrix calculus is the instruction sheet for vector and matrix derivatives: a small set of memorized identities (how to differentiate xAxx^\top A x, axa^\top x, and similar building blocks) that let you assemble the derivative of a complicated expression from known pieces, instead of expanding everything into scalars and grinding through the chain rule term by term.

The math, one symbol at a time

For a vector xRnx \in \mathbb{R}^n, the gradient of a scalar function f(x)f(x) is the vector of partial derivatives f(x)=(fx1,,fxn)\nabla f(x) = \left(\frac{\partial f}{\partial x_1}, \ldots, \frac{\partial f}{\partial x_n}\right) — it collects "how ff changes as each coordinate of xx moves" into one vector. Two identities do most of the work in quant finance:

x(ax)=a,x(xAx)=2Ax    (for symmetric A).\nabla_x (a^\top x) = a, \qquad \nabla_x (x^\top A x) = 2Ax \;\; \text{(for symmetric $A$)}.

In words: the gradient of a plain linear expression axa^\top x is just the constant vector aa (the "slope" in every direction, same as ordinary linear functions); the gradient of a quadratic form is twice the matrix times the vector — the direct multivariate analogue of the single-variable rule that the derivative of ax2ax^2 is 2ax2ax.

Worked example 1: deriving the least-squares normal equations

Minimize the sum of squared residuals f(β)=(yXβ)(yXβ)f(\beta) = (y - X\beta)^\top(y - X\beta) over coefficient vector β\beta. Expand: f(β)=yy2βXy+βXXβf(\beta) = y^\top y - 2\beta^\top X^\top y + \beta^\top X^\top X \beta. Applying the two identities term by term: the gradient of 2βXy-2\beta^\top X^\top y (a linear term in β\beta) is 2Xy-2X^\top y, and the gradient of β(XX)β\beta^\top(X^\top X)\beta (a quadratic form with symmetric matrix XXX^\top X) is 2XXβ2X^\top X \beta. Setting the total gradient to zero: 2Xy+2XXβ=0-2X^\top y + 2X^\top X\beta = 0, giving the normal equations XXβ=XyX^\top X \beta = X^\top y, so β^=(XX)1Xy\hat\beta = (X^\top X)^{-1}X^\top y — the closed-form OLS solution, derived in three lines using only the two identities above.

Worked example 2: a numeric gradient check

Let A=(2003)A = \begin{pmatrix}2&0\\0&3\end{pmatrix} and x=(1,2)x = (1,2). By the identity, (xAx)=2Ax=2(2003)(12)=2(26)=(412)\nabla(x^\top A x) = 2Ax = 2\begin{pmatrix}2&0\\0&3\end{pmatrix}\begin{pmatrix}1\\2\end{pmatrix} = 2\begin{pmatrix}2\\6\end{pmatrix} = \begin{pmatrix}4\\12\end{pmatrix}. Checking by brute force: f(x)=2x12+3x22f(x) = 2x_1^2 + 3x_2^2, so f/x1=4x1=4\partial f/\partial x_1 = 4x_1 = 4 and f/x2=6x2=12\partial f/\partial x_2 = 6x_2 = 12 — matching exactly, confirming the identity is just the compact, all-at-once version of ordinary scalar partial differentiation.

Gradient descent
parameter →
position -0.623loss -0.141gradient -0.930converging

Watch the descent path on a bowl-shaped loss surface; each step it takes moves in the direction of f(x)-\nabla f(x), computed at every point using exactly the matrix-calculus identities above.

scalar rule d/dx (ax²) = 2ax matrix rule ∇ₓ(xᵀAx) = 2Ax generalizes
Matrix calculus identities are the direct multivariable generalization of familiar single-variable derivative rules — the quadratic rule 2ax becomes 2Ax.

What this means in practice

Matrix calculus is what makes closed-form solutions to regression, ridge regression, and Kalman filter updates derivable in a few lines rather than a page of scalar algebra, and it's the language in which gradient-based optimizers (used to train models when no closed form exists) express their update rules. Any time an objective function is written using vectors and matrices — a portfolio's variance, a regression's squared error, a regularization penalty — its gradient is built from a small toolkit of identities like the two above.

The gradient of a linear form axa^\top x is aa; the gradient of a quadratic form xAxx^\top Ax (symmetric AA) is 2Ax2Ax — two identities that let you differentiate most objective functions in quant finance directly, without unrolling into scalar sums.

The clean identity x(xAx)=2Ax\nabla_x(x^\top Ax) = 2Ax only holds when AA is symmetric. If you apply it blindly to a non-symmetric AA, you get the wrong answer — the correct general formula is (A+A)x(A + A^\top)x, which only simplifies to 2Ax2Ax because A=AA = A^\top for a symmetric matrix. Since covariance and Gram matrices are always symmetric, this rarely bites in practice, but it's a common source of silent errors when matrix calculus identities are copied without checking the symmetry assumption they rely on.

Related concepts

Practice in interviews

Further reading

  • Petersen & Pedersen, The Matrix Cookbook
ShareTwitterLinkedIn