# What justifies this calculation of the derivative of a matrix function?

In Andrew Ng's machine learning course, he uses this formula:

$\nabla_A tr(ABA^TC) = CAB + C^TAB^T$

and he does a quick proof which is shown below:

$\nabla_A tr(ABA^TC) \\ = \nabla_A tr(f(A)A^TC) \\ = \nabla_{\circ} tr(f(\circ)A^TC) + \nabla_{\circ}tr(f(A)\circ^T C)\\ =(A^TC)^Tf'(\circ) + (\nabla_{\circ^T}tr(f(A)\circ^T C)^T \\ = C^TAB^T + (\nabla_{\circ^T}tr(\circ^T)Cf(A))^T \\ =C^TAB^T + ((Cf(A))^T)^T \\ = C^TAB^T + CAB$

The proof seems very dense without any comments and I'm having trouble understanding it. What exactly happened from second to third equality?

• He must be making special assumptions about the dimensions of $A$, $B$, and $C$, for otherwise this formula makes no sense in general. On the left hand side $A$ must be an $i\times j$ matrix, $B$ a $j\times j$ matrix, and $C$ an $i\times m$ matrix for arbitrary non-negative integers $i,j,m$. But then the products on the right would not be defined unless $i=m$.
– whuber
Jan 22 '17 at 17:07
• @whuber I see. Given the assumptions, I still don't understand how the transition happened from second to third line where he introduces $\circ$. Jan 22 '17 at 17:18
• Between the second and third line he's let $f(A)=AB$. Between the second and third line he's used the product rule. later he uses the chain rule to get rid of $f()$. Jan 22 '17 at 23:37

## 1 Answer

There is a subtle but heavy abuse of the notation that renders many of the steps confusing. Let's address this issue by going back to the definitions of matrix multiplication, transposition, traces, and derivatives. For those wishing to omit the explanations, just jump to the last section "Putting It All Together" to see how short and simple a rigorous demonstration can be.

## Notation and Concepts

### Dimensions

For the expression $ABA^\prime C$ to make sense when $A$ is an $m\times n$ matrix, $B$ must be a (square) $n\times n$ matrix and $C$ must be an $m\times p$ matrix, whence the product is an $m\times p$ matrix. In order to take the trace (which is the sum of diagonal elements, $\operatorname{Tr}(X)=\sum_i X_{ii}$), then $p=m$, making $C$ a square matrix.

### Derivatives

The notation "$\nabla_A$" appears to refer to the derivative of an expression with respect to $A$. Ordinarily, differentiation is an operation performed on functions $f:\mathbb{R}^N\to\mathbb{R}^M$. The derivative at a point $x\in \mathbb{R}^N$ is a linear transformation $Df(x):\mathbb{R}^N\to\mathbb{R}^M$. Upon choosing bases for these vector spaces, such a transformation can be represented as an $M\times N$ matrix. That is not the case here!

### Matrices as vectors

Instead, $A$ is being considered as an element of $\mathbb{R}^{mn}$: its coefficients are being unrolled (usually either row by row or column by column) into a vector of length $N=mn$. The function $f(A)=\operatorname{Tr}(ABA^\prime C)$ has real values, whence $M=1$. Consequently, $Df(x)$ must be a $1\times mn$ matrix: it's a row vector representing a linear form on $\mathbb{R}^{mn}$. Howver, the calculations in the question use a different way of representing linear forms: their coefficients are rolled back up into $m\times n$ matrices.

### The trace as a linear form

Let $\omega$ be a constant $m\times n$ matrix. Then, by definition of the trace and of matrix multiplication,

\eqalign{ \operatorname{Tr}(A\omega^\prime) &= \sum_{i=1}^m(A\omega^\prime)_{ii} = \sum_{i=1}^m\left(\sum_{j=1}^n A_{ij}(\omega^\prime)_{ji}\right) = \sum_{i,j} \omega_{ij}A_{ij} }

This expresses the most general possible linear combination of the coefficients of $A$: $\omega$ is a matrix of the same shape as $A$ and its coefficient in row $i$ and column $j$ is the coefficient of $A_{ij}$ in the linear combination. Because $\omega_{ij}A_{ij}=A_{ij}\omega_{ij}$, the roles of $\omega$ and $A$ may switched, giving the equivalent expression

$$\sum_{i,j} \omega_{ij}A_{ij} = \operatorname{Tr}(A\omega^\prime) = \operatorname{Tr}(\omega A^\prime).\tag{1}$$

By identifying a constant matrix $\omega$ with either of the functions $A\to \operatorname{Tr}(A \omega^\prime)$ or $A\to \operatorname{Tr}(\omega A^\prime)$, we may represent linear forms on the space of $m\times n$ matrices as $m\times n$ matrices. (Do not confuse these with derivatives of functions from $\mathbb{R}^n$ to $\mathbb{R}^m$!)

## Computing a Derivative

### The definition

Derivatives of many of the matrix functions encountered in statistics are most easily and reliably computed from the definition: you don't really need to resort to complicated rules of matrix differentiation. This definition says that $f$ is differentiable at $x$ if and only if there is a linear transformation $L$ such that

$$f(x+h) - f(x) = Lh + o(|h|)$$

for arbitrarily small displacements $h\in \mathbb{R}^N$. The little-oh notation means that the error made in approximating the difference $f(x+h)-f(x)$ by $Lh$ is arbitrarily smaller than the size of $h$ for sufficiently small $h$. In particular, we may always ignore errors that are proportional to $|h|^2$.

### The calculation

Let's apply the definition to the function in question. Multiplying, expanding, and ignoring the term with a product of two $h$'s in it,

\eqalign{ f(A+h)-f(A) &= \operatorname{Tr}((A+h)B(A+h)^\prime C) - \operatorname{Tr}(ABA^\prime C) \\ &= \operatorname{Tr}(hBA^\prime C) +\operatorname{Tr}(ABh^\prime C) + o(|h|).\tag{2} }

To identify the derivative $L=Df(A)$, we must get this into the form $(1)$. The first term on the right is already in this form, with $\omega = BA^\prime C$. The other term on the right has the form $\operatorname{Tr}(Xh^\prime C)$ for $X=AB$. Let's write this out:

$$\operatorname{Tr}(Xh^\prime C) = \sum_{i=1}^m\sum_{j=1}^n\sum_{k=1}^m X_{ij} h_{kj} C_{ki} = \sum_{i,j,k}h_{kj} \left(C_{ki}X_{ij}\right) =\operatorname{Tr}((CX)h^\prime).\tag{3}$$

Recalling $X=AB$, $(2)$ can be rewritten

$$f(A+h) - f(A) = \operatorname{Tr}(h\, BA^\prime C\,) + \operatorname{Tr}(CAB\, h^\prime\,)+o(|h|).$$

It is in this sense that we may consider the derivative of $f$ at $A$ to be $$Df(A) = (BA^\prime C)^\prime + CAB = C^\prime A B^\prime + CAB,$$ because these matrices play the roles of $\omega$ in the trace formulas $(1)$.

## Putting It All Together

Here, then, is a complete solution.

Let $A$ be an $m\times n$ matrix, $B$ an $n\times n$ matrix, and $C$ an $m\times m$ matrix. Let $f(A) = \operatorname{Tr}(ABA^\prime C)$. Let $h$ be an $m\times n$ matrix with arbitrarily small coefficients. Because (by identity $(3)$) \eqalign{f(A+h) - f(A) &= \operatorname{Tr}(hBA^\prime C) +\operatorname{Tr}(ABh^\prime C) + o(|h|) \\ &=\operatorname{Tr}(h(C^\prime A B^\prime)^\prime + (CAB)h^\prime) + o(|h|),} $f$ is differentiable and its derivative is the linear form determined by the matrix $$C^\prime A B^\prime + CAB.$$

Because this takes only about half the work and involves only the most basic manipulations of matrices and traces (multiplication and transposition), it has to be considered a simpler--and arguably more perspicuous--demonstration of the result. If you really want to understand the individual steps in the original demonstration, you might find it fruitful to compare them to the calculations shown here.

• It's helpful to know that in general, $\mbox{tr}(ABC)=\mbox{tr}(CAB)$ whenever the matrices are of compatible sizes. Knowing this make (3) a trivial step. Jan 22 '17 at 23:34
• @Amoeba I can't tell whether you are trying to be humorous or not. Neither the question nor the answer have anything directly to do with partial derivatives. The form $(1)$ explicitly is a linear form defined on the vector space $\operatorname{Mat}(m,n)$ of $m\times n$ real matrices. When somebody claims that the derivative of a function $f:\operatorname{Mat}(m,n)\to\mathbb{R}$ at a point $A$ equals some matrix $\omega$, what they mean is that $Df(A)$ is the linear form given by $X:\to\operatorname{Tr}(X\omega^{\,\prime})$.
– whuber
Jan 23 '17 at 22:29
• @Amoeba That's exactly right--it amply justifies the assertions in the first line of this answer. It is why I wrote "in this sense" and, later in the summary, used the phrase "determined by" rather than "equals." I won't deny that the explanation has been challenging; I'll think about how to clarify it and I appreciate all your comments and suggestions.
– whuber
Jan 24 '17 at 19:35
• @user10324 Christensen's Plane Answers to Complex Questions is precise and widely appreciated. It uses a traditional tensor-style, "debauch of indices" notation. For a "coordinate-free" approach, which is closer to the ideas underlying my answer here, Christensen recommends Eaton, M. L. (1983). Multivariate Statistics: A Vector Space Approach (John Wiley and Sons, New York. Reprinted in 2007 by IMS Lecture Notes – Monograph Series.) I have not seen Eaton's book.
– whuber
Jan 25 '17 at 19:03
• Christiansen's book is superbly precise, thanks a lot. But, even though it's more precise than any other book I've read, I've come to believe your answer stats.stackexchange.com/questions/246047/… is the golden standard for precision. Unfortunately, Christiansen's book does not make the distinctions you make how, e.g., a linear model can be understood from the prespective of probability theory. Do you know of any even more precise book, that would come closer to this golden standard? Jan 25 '17 at 21:57