Matrix calculus for data scientists
Take the red pill and learn about matrix calculus!

Prologue
You take the blue pill...the story ends, you wake up in your bed and believe whatever you want to believe. You take the red pill...you stay in Wonderland, and I show you how deep the rabbit hole goes.
Those are the famous words Morpheus said to Neo in the matrix trilogy. You have to make the same choice, do you want to keep using autograd frameworks like pytorch and tensorflow not knowing, what they do? Or do you want to dig deeper into the world of matrix calculus and understand things like how backpropagation works ?
Linear algebra basics

Vectors and matrices
I write scalars (single numbers) with a small and thin letter

While vectors will be denoted by small bold letter. They are column vectors by default.

Row vectors are also denoted by small bold letters, but they have a T superscript. The T superscript stands for transposed.

The symbols representing matrices will be bold capital letters.

We can also transpose a matrix. When doing this, the first column becomes the first row and vice versa.

The dimension of a vector or matrix is a tuple:
( number of rows, number of columns)
Let's consider the following case:

Dot product
The dot product is also defined for vectors and matrices. But the order matters and the number of columns of the left vector/matrix must match the number of rows of the right vector/matrix.

The dimension of the result is: (number of rows of the left input, number of columns of the right input)

If you are interested you can see how the dot product is performed here in greater detail.

To get one element of the output, we multiply and sum up one row of the left and one column of the right vector/matrix.
The importance of the dot product lies in the fact, that it can be used in many different contexts. In mechanics it can used to represent a rotation and stretching of an object. It can also be used to change the coordinate system.
Analysis basics

Derivatives of sums
When we want to take the derivative of a sum, it is equivalent to taking the derivative of each addend.


Product rule
If we want to take the derivative of the product of two functions, both depending on the variable we want to differentiate by, we can use the following rule:

Let's consider the following example:


Then the derivative of π¦ with respect to π₯ is:

Chain rule
We want to differentiate a function π¦. This function depends on π’ and π’ depends on π₯. We can then apply the chain rule.

Let's make a quick example here.


Matrix calculus
After downloading that stuff into your brain, I hope that you are ready for matrix calculus!
There are two conventions in which we can write matrix calculus, the so called "numerator layout" and the "denominator layout". I will use the numerator layout during this whole blogpost.

Scalar by vector derivative
The derivative of a scalar valued function with respect to a vector of variables then is a row vector. This row vector has one column for each variable we want to differentiate by.

The dots just indicate, that there there are not necessarily 3 variables we want to differentiate by, but rather a variable number.
Let's make an example:


We can then apply the "derivative of a sum" rule:

Since the derivative of a variable with respect to another is 0, we get:

Vector by scalar derivative
If we have a function π, which outputs a vector and want to take the derivative of it with respect to a single variable, we get a column vector as result.

Lets consider the following vector valued function we want to take the derivative of.

Vector by vector derivative
When taking the derivative of a vector valued function with respect to a vector of variables, we get a matrix. I use a function with 2 output values and 3 input variables as example. But you can use any number of output values and input variables.

You can think of it like combining the "scalar by vector" and the "vector by scalar derivative". We vary the elements of the function output along the rows and the elements of the variables along the columns.

The resulting matrix has the dimensions:(size of output, size of input)
Now we go through a quick example.

Summary

Vector chain rule
Let's consider the following scenario. Vector π is a function of vector π. Vector π itself is a function of vector π₯. So the derivative of π(π(π₯)) with respect to π₯ is calculated the following way:

We can see that the vector chain rule looks almost the same as the scalar chain rule. The dot product remains in the formula and we have to construct the "vector by vector" derivative matrices.
Here's a little example:

We calculate the partial derivatives.

And now we expand the dot product.

One last simplification and we get the result.

Dot product of Constants
We can treat constant matrices and vectors almost like scalar constants. Factoring them out is allowed. However keep in mind, that the order matters for dot products between two non-scalars.


We can now factor out the matrix π, if it is a constant and does not depend on π€.

Know we construct the vector by vector derivative matrix.

The matrix with ones on the diagonal and zeros elsewhere is called the Identity matrix. Everything multiplied by it remains the same. Therefore we the final result:

You can get the same result, if you do the dot product yourself instead of relying on me telling you about the identity matrix.
What is interesting is that although the whole equation contained vectors and matrices we got the same result we would have expected based on scalar calculus. However this is not always the case since not all rules of scalar calculus also hold for multivariate calculus. There exists no vector product rule to name one example.
Epilogue
I am glad you took the red pill and joined me on my journey deep down into the field of matrix calculus. If this is new for you, then you should practice a lot and go through a few examples yourself. I hope you enjoyed this blog post and wish you all the best.
Vincent MΓΌller
Related Articles from author
Other articles from author
Want to connect and support me?
Linkedin https://www.linkedin.com/in/vincent-m%C3%BCller-6b3542214/ Facebook https://www.facebook.com/profile.php?id=100072095823739 Twitter https://twitter.com/Vincent02770108 Medium https://medium.com/@Vincent.Mueller Become medium member and support me (part of your membership fees go directly to me) https://medium.com/@Vincent.Mueller/membership






