Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 3

Vector Calculus for Matrix Expressions

Recommended resources

Welcome back. In our last lesson, we built the general machinery for differentiating vector-valued functions by introducing the Jacobian matrix and the multivariate chain rule. These tools are the foundation for understanding how to train complex models.

Now, we will bring these concepts to ground by focusing on a few specific, but critically important, matrix expressions that you will encounter constantly throughout statistical machine learning. This lesson provides a practical "cookbook" for differentiating functions like the quadratic form and the squared norm , which forms the basis of linear regression. Mastering these derivations is essential, as they are the building blocks for deriving the optimisation algorithms for many linear models.

1. The Building Blocks: Gradients of Linear and Quadratic Forms

While the chain rule we learned is powerful, many of the gradients you'll need can be derived directly by writing out the expressions in summation notation and taking partial derivatives element by element. This "first principles" approach is excellent for building intuition and is a good skill to have.

The following document provides clear, step-by-step derivations for the gradients of two fundamental expressions: the linear form and the quadratic form .

Deriving the Gradient and Hessian of Linear and Quadratic ...

This document shows how to derive gradients for linear and quadratic forms from first principles using element-wise calculations. Note that the author uses the variable w instead of x, but the logic is identical.

Please read the first two sections of the document. In Section 1, "Gradient of Linear Function", follow the four steps to see how \nabla (\mathbf{a}^T \mathbf{w}) = \mathbf{a}. In Section 2, "Gradient of Quadratic Function", carefully trace the derivation for \mathbf{w}^T A \mathbf{w}. This one is more involved and relies on applying the product rule to the summation. Pay attention to how the terms are grouped to arrive at the final matrix form.

Let's summarize these crucial results (using for consistency with our course):

  • Linear form: For , the gradient is . This is the direct multi-dimensional analogue of .

  • Quadratic form: For , the gradient is .

    • A very important special case is when the matrix is symmetric (). In this situation, the gradient simplifies to . This is the analogue of , and you will see it frequently.

These rules form the basis of our "cookbook" for matrix calculus.


2. A More Powerful Viewpoint: Calculus Without Coordinates

While the element-wise method is reliable, it can become tedious. A more elegant and often faster approach is to apply calculus rules like the product and chain rule directly to vector and matrix expressions. This "coordinate-free" method treats vectors and matrices as fundamental objects.

The key idea is to find the function's differential, . For a scalar function of a vector, , the differential relates a small change in the input, , to the corresponding change in the output, . It is given by:

If we can manipulate our derivative expression into this form, we can simply read off the gradient .

The following video demonstrates this powerful technique.

Matrix Calculus 2020 04 16 for MIT Linear Algebra 18.06 Spring 2020 (Alan Edelman)

In this excerpt from a lecture by Prof. Alan Edelman, you'll see how to apply the product rule directly to vector expressions to find the gradient.

Watch the segment from . The example covers finding the derivative of f(\mathbf{x}) = \mathbf{x}^T\mathbf{x}. Notice how the product rule yields two terms, and the key step is realizing that since the result is a scalar, the two terms are equivalent (\mathbf{a}^T\mathbf{b} = \mathbf{b}^T\mathbf{a}) and can be combined. This leads to the final form from which the gradient is identified.

As the video shows, for , the differential is . Since , this simplifies to . Comparing this to the definition , we can see that . This matches the result from our "cookbook" rule, since and is symmetric.


3. The Main Event: Gradient of the Least Squares Objective

Now we'll tackle one of the most important expressions in machine learning: the least squares objective function, . Minimising this function is the goal of linear regression. We need its gradient to perform that minimisation.

We can derive this gradient in two ways: by expanding the expression and using our cookbook rules, or by applying the more general differential method.

Method 1: Expansion

First, let's expand the squared norm.

Using the property and distributing the terms, we get:

Note that is a scalar, so it is equal to its own transpose: . This allows us to combine the two middle terms:

Now we can differentiate this term by term using our rules from Section 1:

  1. The first term is a quadratic form with . Note that is always symmetric. Therefore, .
  2. The second term is a linear form . This is with . The gradient is , which is .
  3. The third term, , is a constant with respect to , so its gradient is zero.

Combining these gives the final gradient:

This derivation is valuable because it forces you to practice manipulating matrix expressions, a key skill for this course.

Method 2: The Differential Approach

The differential approach is much more direct and is what you would likely use once you are comfortable with it. The video from Prof. Edelman demonstrates this derivation beautifully.

Matrix Calculus 2020 04 16 for MIT Linear Algebra 18.06 Spring 2020 (Alan Edelman)

This part of the lecture directly derives the gradient for the least squares objective using the product rule on the differential form.

Please watch from . Follow how the expression d(\|\mathbf{A}\mathbf{x} - \mathbf{b}\|^2) is differentiated using the product rule, just like in the \mathbf{x}^T\mathbf{x} example. The key steps are applying the derivative to the inside term (d(\mathbf{A}\mathbf{x} - \mathbf{b}) = \mathbf{A}d\mathbf{x}) and then manipulating the transposes to isolate d\mathbf{x} on the right-hand side.

This second method confirms our result elegantly and showcases a powerful technique for handling more complex derivatives you may encounter later on.


Conclusion

In this lesson, we developed a practical ability to differentiate some of the most common and important expressions in machine learning. While the derivations may seem purely mathematical, they are the key to unlocking the algorithms that train our models.

The key takeaways are:

  • Gradient of a linear form: .
  • Gradient of a quadratic form: , which simplifies to when is symmetric.
  • Gradient of the least squares objective: .
  • There are two complementary methods for these derivations: the explicit element-wise calculation and the more abstract differential or "coordinate-free" approach.

In our next lesson, we will briefly revisit Singular Value Decomposition (SVD), another cornerstone of linear algebra for machine learning. Then, we will shift our focus from pure calculus to optimisation and explore the concept of convexity. You will see why minimising a function like the least squares objective is a well-posed problem and why we can be confident in finding its single, global minimum.