ACCFIN5277 Chap.9 Matrix Calculus and Least Squares
Matrix Calculus and Least Squares
Matrix Calculus and Least Squares derives the estimator that appears throughout financial statistics. The notes treat differentiation as a search for the parameter values that make an objective as small as possible, under three standing assumptions: continuity, real-valued objects and ordinary matrix products.
For a scalar function of a vector, the partial derivatives form a row and the gradient is that row transposed into a column. Three rules then do most of the work. The derivative of an inner product a'x is a', the derivative of a quadratic form x'Ax is x'(A + A'), which becomes 2x'A for a symmetric matrix, and gradients add term by term.
Applied to the sum of squared residuals, these rules give the first-order condition, known as the normal equations, and solving them yields the familiar coefficient vector built from the inverse of X'X. The second derivative is X'X itself, and when it is positive definite the solution is a true minimum.
A five-month regression of a stock on the market, worked by hand and drawn as a fitted line, ties the algebra to a beta estimate.
What this chapter covers
- 01
Derivative rows and gradient columns
- 02
Transpose of a product and scalar swaps
- 03
Gradients of linear and quadratic forms
- 04
Jacobian of a vector-valued function
- 05
Normal equations from the first-order condition
- 06
Second-order check on X'X
Worked example · free
Estimate a stock's beta from five months of data
- 1X'X has entries 5, 0, 0 and 10 because the market column sums to zero and its squares sum to 10.
- 1X'y = (5, 21), since the stock values sum to 5 and the cross products sum to 21.
- 2The inverse of X'X is diag(0.2, 0.1), so the intercept is 1 and the slope, the beta, is 2.1.
- 2Fitted values -3.2, -1.1, 1.0, 3.1 and 5.2 leave residuals 0.2, 0.1, 0, -1.1 and 0.8, whose squares sum to 1.90.
Key terms
- Gradient vector
- The column of partial derivatives of a scalar function with respect to each element of its vector argument.
- Jacobian matrix
- The array of partial derivatives of a vector-valued function, with one row per function and one column per argument.
- Normal equations
- The first-order condition of least squares, written X'X times the coefficients equals X'y.
- Second-order condition
- The requirement that the second derivative of the objective, here X'X, be positive definite at the solution.
Matrix Calculus and Least Squares FAQ
Is the derivative of a scalar function a row or a column?
In the notes the derivative of a scalar function with respect to a column vector is written as a row, called Df, and the gradient is its transpose, a column. Both carry the same partial derivatives. Keeping the distinction matters when you multiply the result by other matrices, because a row and a column do not conform in the same places.
Why does x'Ax differentiate to x'(A + A')?
The vector appears twice in the form, once on each side of A. Treating each appearance as its own variable and adding the two partial results gives x'A plus x'A'. When A is symmetric the two pieces are equal, so the derivative simplifies to twice x'A, the version used for the least squares objective.
Where do the normal equations come from?
Expanding the sum of squared residuals gives y'y minus twice the cross term plus a quadratic form in the coefficients. Differentiating with respect to the coefficients and setting the result to zero leaves X'X times the coefficients equal to X'y. Solving that linear system, provided X'X can be inverted, gives the least squares estimator.
How do I know the solution is a minimum and not a maximum?
Differentiate the first-order condition once more. The result is 2X'X, a positive multiple of X'X, which is a quadratic form and so never negative. When the regressors have full column rank its determinant is non-zero and it is positive definite, which makes the objective bowl-shaped and the stationary point its lowest value rather than a peak or a saddle.
What goes wrong when two regressors are perfectly related?
If one column of X is an exact combination of others, X'X loses rank and its determinant becomes zero. The inverse then does not exist, and the normal equations have infinitely many solutions rather than one. In practice the fix is to drop or redefine the redundant column before estimating, and a quick determinant check on X'X catches the problem early.
Assessment move
Learn the three differentiation rules by verifying each one on a two-element example: expand the expression into ordinary terms, take partial derivatives, and compare with the matrix formula. Then rederive the normal equations from a blank page, writing the dimensions under every term. Practise one centred and one uncentred regression by hand, and reproduce both in NumPy with a linear solver.
Finally, check the second-order condition each time by confirming that X'X has a positive determinant and positive diagonal. Keep the tested estimator function in your reference notebook, because the same code serves every regression later in the course.
Working through Matrix Calculus and Least Squares in ACCFIN5277? Sia is AskSia’s AI Finance tutor — ask any ACCFIN5277 Matrix Calculus and Least Squares question and get a clear, step-by-step explanation grounded in how ACCFIN5277 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.