Skip to content Mastodon

Notes and Tones

The Case for Differentials

Every student studying a field related to optimization should be able to work with differentials. They are simpler to apply than (partial) derivatives and strictly more expressive, especially when generalizing to matrix functions. We will motivate them in this post, following the book Matrix Differential Calculus.

In university of any engineering degree, they teach you the partial derivatives of a function. It tells you the rate of change between an input and an output of the function and uses the convention to represent them as βˆ‚π‘¦βˆ•βˆ‚π‘₯. That works for functions with scalar input and scalar output, but does not generalize well for vector/matrix calculus. Also this convention is rather weird, as it introduces a fraction notation, while not being a real fraction (your are not allowed to move βˆ‚π‘₯ out).

Instead, differentials use a different approach. They represent how a change of the input dx should be transformed to arrive at the linear part of the output dy. Linear, because the term is only allowed to depend on dx once, for example for Ο•(x) = x2

Ο•(x + dx) = (x + dx)2 = x2 + 2xdx + (dx)2

the first differential is given as dΟ• = 2xdx.

The function parameters may be scalar, vector or matrix, for demonstration purpose we will concentrate on scalar functions with matrix parameters, often encountered in machine learning context and denote them as Ο•(F).

Differentiating inverse function

For the beginning, we start with a motivational example. Let’s consider the differential of the matrix inverse function dXβˆ’1 for an invertible matrix X. We work with the identity Xβˆ’1X = I and differentiate

d(Xβˆ’1X) = (dXβˆ’1)X + Xβˆ’1dX = d(I) = 0.

Hence, bringing the part of equation involving dX to the RHS and right-multiplying Xβˆ’1, we do get

dXβˆ’1 = βˆ’Xβˆ’1(dX)Xβˆ’1

So this tells us, that increasing by dX, increases the output by βˆ’Xβˆ’1(dX)Xβˆ’1 for the linear part. We cannot hope to represent this relation with derivatives so easily, as we would have to represent the partial derivates for all input/output combinations.

Identification of Jacobian

Another way to express the relation between first-order derivatives and first-order differentials is the following identity

df = J(x)dxβŸΊβˆ‚π‘“(x) βˆ‚π‘₯ = J(x)

with the Jacobian J(x) describing the change at some coordinate x. To rewrite a differential to a derivative, we need to order elements such that we can express the Jacobian in the LHS relation above.

Comparing this to the approach of many machine learning frameworks, we see the similarity to vector-Jacobian product (vJp) to transform a gradient vector in a chain of Jacobian transformations. Sometimes, respresenting the transformation as a linear map is hard, and we can represent the vJp in a more general form for differentials.

Introducing useful calculus rules

We will directly go in media res and introduce differentials of matrix calculus. Many rules are straightforward and known from derivative rules. We have that for A constant dA = 0, and that the differential is a linear operator

d(𝛼𝑋) = Ξ±dX and d(X + Y ) = dX + dY.

The matrix product follows from product rule

d(π‘‹π‘Œ ) = (dX)Y + X(dY ).

Further, one can also prove that

ddet ⁑ X = det ⁑ Xtr ⁑ Xβˆ’1dX

and hence (following from log rule)

dlog⁑det ⁑ X = ddet ⁑ X det ⁑ X = tr ⁑ Xβˆ’1dX.

Folowing the identification theorem, we can define the chain rules for two functions Z = f(Y ) and Y = g(X) as

dZ = A(Y )B(X)dX

resulting from dZ = A(Y )dY and dY = B(X)dX.

Going to Second Differentials

Producing more rules, we can derive Hessian from the second differential. It is straightforward defines as taking the differential, of a differential: d(df(X)). The third differential is again defines, as taking the differential of the second differential, and so on ….

Similar to the identification of the Jacobian matrix, we can also identify the Hessian from the second differential. We formally define the Hessian through the differential of the Jacobian matrix parametrized by x. Say dΟ• = J⊺(x)dx and dJ = H(x)⊺dx, then we have

d2Ο• = dJ⊺dx + J⊺d2x = ⋆ dJ⊺dx = (dx)⊺H(x)dx

We used in (⋆) that d2x = 0.

Hence, if we arrive (after a length derivation) at some representation d2Ο• = (dx)⊺B(x)dx, then we know that the Hessian is H = (B(x) + B(x)⊺)βˆ•2.

Three Examples of Matrix Functions

We finish with three examples of matrix functions taken from the book. The functions take all matrices as input:

1.
Let Ο•(X) = tr ⁑ XβŠΊπ΄π‘‹, then we have dΟ• = d(tr ⁑ XβŠΊπ΄π‘‹) = ⋆ tr ⁑ d(XβŠΊπ΄π‘‹) = ⋆⋆ tr ⁑ (dX)βŠΊπ΄π‘‹ + tr ⁑ X⊺AdX = tr ⁑ X⊺(A + A⊺)dX

We use the trace rule from previous section in

(⋆), and the product rule in (⋆⋆). The second differentials follow from

d2Ο• = dtr ⁑ X⊺(A + A⊺)dX = tr ⁑ {(dX)⊺(A + A⊺)dX }
2.
Let Ο•(X) = tr ⁑ Xk, then for k β‰₯ 1 dΟ• = ⋆ tr ⁑ (dX)Xkβˆ’1 + tr ⁑ X(dX)Xkβˆ’2 + β‹― + tr ⁑ Xkβˆ’1dX = ⋆⋆ ktr ⁑ Xkβˆ’1dX

We again apply the product rule for k-packets in (⋆), and the known reordering in trace argument for (⋆⋆). For k β‰₯ 2

d2Ο• = ktr ⁑ (dXkβˆ’1)dX = kβˆ‘ k=0Kβˆ’2 tr ⁑ Xk(dX)XKβˆ’2βˆ’kdX.
3.
Let F(X) = AXβˆ’1B, then dF = ⋆ A(dXβˆ’1)B = βˆ’AXβˆ’1(dX)Xβˆ’1B

We applied the differential of an inverse in (⋆), we have derived it in the first section.

d2F = ⋆ βˆ’ A(dXβˆ’1)(dX)Xβˆ’1B βˆ’ AXβˆ’1(dX)(dXβˆ’1)B = ⋆⋆ 2AXβˆ’1(dX)Xβˆ’1(dX)Xβˆ’1B.

We cannot identify the Hessian matrix from second differentials, because the vec and Kronecker operator are missing.

Conclusion

I hope you got an idea why differentials are a handy tool when working with optimization or Machine Learning. Personally I found them helpful many times when deriving some algorithm for machine learning.

We showcased only some calculus rules to highlight the advantage of differentials compared to derivatives. For actual applications take a look at Chapter 11 for inequalities and Chapter 15 for Maximum Likelihood Estimation. I recommend the whole book, there is also a condensed overview in the last chapter.