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 , , 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 , the gradient of a scalar function is the vector of partial derivatives — it collects "how changes as each coordinate of moves" into one vector. Two identities do most of the work in quant finance:
In words: the gradient of a plain linear expression is just the constant vector (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 is .
Worked example 1: deriving the least-squares normal equations
Minimize the sum of squared residuals over coefficient vector . Expand: . Applying the two identities term by term: the gradient of (a linear term in ) is , and the gradient of (a quadratic form with symmetric matrix ) is . Setting the total gradient to zero: , giving the normal equations , so — the closed-form OLS solution, derived in three lines using only the two identities above.
Worked example 2: a numeric gradient check
Let and . By the identity, . Checking by brute force: , so and — matching exactly, confirming the identity is just the compact, all-at-once version of ordinary scalar partial differentiation.
Watch the descent path on a bowl-shaped loss surface; each step it takes moves in the direction of , computed at every point using exactly the matrix-calculus identities above.
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 is ; the gradient of a quadratic form (symmetric ) is — two identities that let you differentiate most objective functions in quant finance directly, without unrolling into scalar sums.
The clean identity only holds when is symmetric. If you apply it blindly to a non-symmetric , you get the wrong answer — the correct general formula is , which only simplifies to because 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