Matrix calculus explained

In mathematics, matrix calculus is a specialized notation for doing multivariable calculus, especially over spaces of matrices. It collects the various partial derivatives of a single function with respect to many variables, and/or of a multivariate function with respect to a single variable, into vectors and matrices that can be treated as single entities. This greatly simplifies operations such as finding the maximum or minimum of a multivariate function and solving systems of differential equations. The notation used here is commonly used in statistics and engineering, while the tensor index notation is preferred in physics.

Two competing notational conventions split the field of matrix calculus into two separate groups. The two groups can be distinguished by whether they write the derivative of a scalar with respect to a vector as a column vector or a row vector. Both of these conventions are possible even when the common assumption is made that vectors should be treated as column vectors when combined with matrices (rather than row vectors). A single convention can be somewhat standard throughout a single field that commonly uses matrix calculus (e.g. econometrics, statistics, estimation theory and machine learning). However, even within a given field different authors can be found using competing conventions. Authors of both groups often write as though their specific conventions were standard. Serious mistakes can result when combining results from different authors without carefully verifying that compatible notations have been used. Definitions of these two conventions and comparisons between them are collected in the layout conventions section.

Scope

Matrix calculus refers to a number of different notations that use matrices and vectors to collect the derivative of each component of the dependent variable with respect to each component of the independent variable. In general, the independent variable can be a scalar, a vector, or a matrix while the dependent variable can be any of these as well. Each different situation will lead to a different set of rules, or a separate calculus, using the broader sense of the term. Matrix notation serves as a convenient way to collect the many derivatives in an organized way.

As a first example, consider the gradient from vector calculus. For a scalar function of three independent variables,

f(x1,x2,x3)

, the gradient is given by the vector equation

\nablaf=

\partialf
\partialx1

\hat{x}1+

\partialf
\partialx2

\hat{x}2+

\partialf
\partialx3

\hat{x}3,

where

\hat{x}i

represents a unit vector in the

xi

direction for

1\lei\le3

. This type of generalized derivative can be seen as the derivative of a scalar, f, with respect to a vector,

x

, and its result can be easily collected in vector form.

\nablaf=\left(

\partialf
\partialx

\right)T= \begin{bmatrix} \dfrac{\partialf}{\partialx1}& \dfrac{\partialf}{\partialx2}& \dfrac{\partialf}{\partialx3}\\ \end{bmatrix}sf{T}.

More complicated examples include the derivative of a scalar function with respect to a matrix, known as the gradient matrix, which collects the derivative with respect to each matrix element in the corresponding position in the resulting matrix. In that case the scalar must be a function of each of the independent variables in the matrix. As another example, if we have an -vector of dependent variables, or functions, of independent variables we might consider the derivative of the dependent vector with respect to the independent vector. The result could be collected in an matrix consisting of all of the possible derivative combinations.

There are a total of nine possibilities using scalars, vectors, and matrices. Notice that as we consider higher numbers of components in each of the independent and dependent variables we can be left with a very large number of possibilities. The six kinds of derivatives that can be most neatly organized in matrix form are collected in the following table.

Types! scope="col"
ScalarVectorMatrix
Scalar
\partialy
\partialx

\partialy
\partialx

\partialY
\partialx

Vector
\partialy
\partialx

\partialy
\partialx

Matrix
\partialy
\partialX

Here, we have used the term "matrix" in its most general sense, recognizing that vectors are simply matrices with one column (and scalars are simply vectors with one row). Moreover, we have used bold letters to indicate vectors and bold capital letters for matrices. This notation is used throughout.

Notice that we could also talk about the derivative of a vector with respect to a matrix, or any of the other unfilled cells in our table. However, these derivatives are most naturally organized in a tensor of rank higher than 2, so that they do not fit neatly into a matrix. In the following three sections we will define each one of these derivatives and relate them to other branches of mathematics. See the layout conventions section for a more detailed table.

Relation to other derivatives

The matrix derivative is a convenient notation for keeping track of partial derivatives for doing calculations. The Fréchet derivative is the standard way in the setting of functional analysis to take derivatives with respect to vectors. In the case that a matrix function of a matrix is Fréchet differentiable, the two derivatives will agree up to translation of notations. As is the case in general for partial derivatives, some formulae may extend under weaker analytic conditions than the existence of the derivative as approximating linear mapping.

Usages

Matrix calculus is used for deriving optimal stochastic estimators, often involving the use of Lagrange multipliers. This includes the derivation of:

Notation

The vector and matrix derivatives presented in the sections to follow take full advantage of matrix notation, using a single variable to represent a large number of variables. In what follows we will distinguish scalars, vectors and matrices by their typeface. We will let denote the space of real matrices with rows and columns. Such matrices will be denoted using bold capital letters:,,, etc. An element of, that is, a column vector, is denoted with a boldface lowercase letter:,,, etc. An element of is a scalar, denoted with lowercase italic typeface:,,, etc. denotes matrix transpose, is the trace, and or is the determinant. All functions are assumed to be of differentiability class unless otherwise noted. Generally letters from the first half of the alphabet (a, b, c, ...) will be used to denote constants, and from the second half (t, x, y, ...) to denote variables.

NOTE: As mentioned above, there are competing notations for laying out systems of partial derivatives in vectors and matrices, and no standard appears to be emerging yet. The next two introductory sections use the numerator layout convention simply for the purposes of convenience, to avoid overly complicating the discussion. The section after them discusses layout conventions in more detail. It is important to realize the following:

  1. Despite the use of the terms "numerator layout" and "denominator layout", there are actually more than two possible notational choices involved. The reason is that the choice of numerator vs. denominator (or in some situations, numerator vs. mixed) can be made independently for scalar-by-vector, vector-by-scalar, vector-by-vector, and scalar-by-matrix derivatives, and a number of authors mix and match their layout choices in various ways.
  2. The choice of numerator layout in the introductory sections below does not imply that this is the "correct" or "superior" choice. There are advantages and disadvantages to the various layout types. Serious mistakes can result from carelessly combining formulas written in different layouts, and converting from one layout to another requires care to avoid errors. As a result, when working with existing formulas the best policy is probably to identify whichever layout is used and maintain consistency with it, rather than attempting to use the same layout in all situations.

Alternatives

The tensor index notation with its Einstein summation convention is very similar to the matrix calculus, except one writes only a single component at a time. It has the advantage that one can easily manipulate arbitrarily high rank tensors, whereas tensors of rank higher than two are quite unwieldy with matrix notation. All of the work here can be done in this notation without use of the single-variable matrix notation. However, many problems in estimation theory and other areas of applied mathematics would result in too many indices to properly keep track of, pointing in favor of matrix calculus in those areas. Also, Einstein notation can be very useful in proving the identities presented here (see section on differentiation) as an alternative to typical element notation, which can become cumbersome when the explicit sums are carried around. Note that a matrix can be considered a tensor of rank two.

Derivatives with vectors

See main article: Vector calculus. Because vectors are matrices with only one column, the simplest matrix derivatives are vector derivatives.

The notations developed here can accommodate the usual operations of vector calculus by identifying the space of -vectors with the Euclidean space, and the scalar is identified with . The corresponding concept from vector calculus is indicated at the end of each subsection.

NOTE: The discussion in this section assumes the numerator layout convention for pedagogical purposes. Some authors use different conventions. The section on layout conventions discusses this issue in greater detail. The identities given further down are presented in forms that can be used in conjunction with all common layout conventions.

Vector-by-scalar

y=\begin{bmatrix} y1& y2& & ym \end{bmatrix}T

, by a scalar is written (in numerator layout notation) as
dy
dx

= \begin{bmatrix}

dy1
dx

\\

dy2
dx

\\ \vdots\\

dym
dx

\\ \end{bmatrix}.

In vector calculus the derivative of a vector with respect to a scalar is known as the tangent vector of the vector,

\partialy
\partialx
. Notice here that .

Example Simple examples of this include the velocity vector in Euclidean space, which is the tangent vector of the position vector (considered as a function of time). Also, the acceleration is the tangent vector of the velocity.

Scalar-by-vector

The derivative of a scalar by a vector

x=\begin{bmatrix} x1& x2& & xn \end{bmatrix}T

, is written (in numerator layout notation) as
\partialy
\partialx

= \begin{bmatrix} \dfrac{\partialy}{\partialx1}& \dfrac{\partialy}{\partialx2}& & \dfrac{\partialy}{\partialxn} \end{bmatrix}.

In vector calculus, the gradient of a scalar field (whose independent coordinates are the components of) is the transpose of the derivative of a scalar by a vector.

\nablaf=\begin{bmatrix}

\partialf
\partialx1

\\vdots\

\partialf
\partialxn

\end{bmatrix}=\left(

\partialf
\partialx

\right)T

By example, in physics, the electric field is the negative vector gradient of the electric potential.

The directional derivative of a scalar function of the space vector in the direction of the unit vector (represented in this case as a column vector) is defined using the gradient as follows.

\nablau{f}(x)=\nablaf(x)u

Using the notation just defined for the derivative of a scalar with respect to a vector we can re-write the directional derivative as

\nablauf=

\partialf
\partialx

u.

This type of notation will be nice when proving product rules and chain rules that come out looking similar to what we are familiar with for the scalar derivative.

Vector-by-vector

Each of the previous two cases can be considered as an application of the derivative of a vector with respect to a vector, using a vector of size one appropriately. Similarly we will find that the derivatives involving matrices will reduce to derivatives involving vectors in a corresponding way.

The derivative of a vector function (a vector whose components are functions)

y=\begin{bmatrix} y1& y2& & ym \end{bmatrix}T

, with respect to an input vector,

x=\begin{bmatrix} x1& x2& & xn \end{bmatrix}T

, is written (in numerator layout notation) as
\partialy
\partialx

= \begin{bmatrix}

\partialy1
\partialx1

&

\partialy1
\partialx2

&&

\partialy1
\partialxn

\\

\partialy2
\partialx1

&

\partialy2
\partialx2

&&

\partialy2
\partialxn

\\ \vdots&\vdots&\ddots&\vdots\\

\partialym
\partialx1

&

\partialym
\partialx2

&&

\partialym
\partialxn

\\ \end{bmatrix}.

In vector calculus, the derivative of a vector function with respect to a vector whose components represent a space is known as the pushforward (or differential), or the Jacobian matrix.

The pushforward along a vector function with respect to vector in is given by

df(v)=

\partialf
\partialv

dv.

Derivatives with matrices

There are two types of derivatives with matrices that can be organized into a matrix of the same size. These are the derivative of a matrix by a scalar and the derivative of a scalar by a matrix. These can be useful in minimization problems found in many areas of applied mathematics and have adopted the names tangent matrix and gradient matrix respectively after their analogs for vectors.

Note: The discussion in this section assumes the numerator layout convention for pedagogical purposes. Some authors use different conventions. The section on layout conventions discusses this issue in greater detail. The identities given further down are presented in forms that can be used in conjunction with all common layout conventions.

Matrix-by-scalar

The derivative of a matrix function by a scalar is known as the tangent matrix and is given (in numerator layout notation) by

\partialY= \begin{bmatrix}
\partialx
\partialy11
\partialx

&

\partialy12
\partialx

&&

\partialy1n\\
\partialx
\partialy21
\partialx

&

\partialy22
\partialx

&&

\partialy2n
\partialx

\\ \vdots&\vdots&\ddots&\vdots\\

\partialym1
\partialx

&

\partialym2
\partialx

&&

\partialymn
\partialx

\\ \end{bmatrix}.

Scalar-by-matrix

The derivative of a scalar function, with respect to a matrix of independent variables, is given (in numerator layout notation) by

\partialy= \begin{bmatrix}
\partialX
\partialy
\partialx11

&

\partialy
\partialx21

&&

\partialy\\
\partialxp1
\partialy
\partialx12

&

\partialy
\partialx22

&&

\partialy
\partialxp2

\\ \vdots&\vdots&\ddots&\vdots\\

\partialy
\partialx1q

&

\partialy
\partialx2q

&&

\partialy
\partialxpq

\\ \end{bmatrix}.

Important examples of scalar functions of matrices include the trace of a matrix and the determinant.

In analog with vector calculus this derivative is often written as the following.

\nablaXy(X)=

\partialy(X)
\partialX

Also in analog with vector calculus, the directional derivative of a scalar of a matrix in the direction of matrix is given by

\nablaYf=\operatorname{tr}\left(

\partialf
\partialX

Y\right).

It is the gradient matrix, in particular, that finds many uses in minimization problems in estimation theory, particularly in the derivation of the Kalman filter algorithm, which is of great importance in the field.

Other matrix derivatives

The three types of derivatives that have not been considered are those involving vectors-by-matrices, matrices-by-vectors, and matrices-by-matrices. These are not as widely considered and a notation is not widely agreed upon.

Layout conventions

This section discusses the similarities and differences between notational conventions that are used in the various fields that take advantage of matrix calculus. Although there are largely two consistent conventions, some authors find it convenient to mix the two conventions in forms that are discussed below. After this section, equations will be listed in both competing forms separately.

The fundamental issue is that the derivative of a vector with respect to a vector, i.e.

\partialy
\partialx
, is often written in two competing ways. If the numerator is of size and the denominator of size n, then the result can be laid out as either an matrix or matrix, i.e. the elements of laid out in rows and the elements of laid out in columns, or vice versa. This leads to the following possibilities:
  1. Numerator layout, i.e. lay out according to and (i.e. contrarily to). This is sometimes known as the Jacobian formulation. This corresponds to the layout in the previous example, which means that the row number of
\partialy
\partialx
equals to the size of the numerator

y

and the column number of
\partialy
\partialx
equals to the size of .
  1. Denominator layout, i.e. lay out according to and (i.e. contrarily to y). This is sometimes known as the Hessian formulation. Some authors term this layout the gradient, in distinction to the Jacobian (numerator layout), which is its transpose. (However, gradient more commonly means the derivative
\partialy
\partialx

,

regardless of layout.). This corresponds to the n×m layout in the previous example, which means that the row number of
\partialy
\partialx
equals to the size of (the denominator).
  1. A third possibility sometimes seen is to insist on writing the derivative as
\partialy
\partialx'

,

(i.e. the derivative is taken with respect to the transpose of) and follow the numerator layout. This makes it possible to claim that the matrix is laid out according to both numerator and denominator. In practice this produces results the same as the numerator layout.
\partialy
\partialx
and the opposite case
\partialy
\partialx

,

we have the same issues. To be consistent, we should do one of the following:
  1. If we choose numerator layout for
\partialy
\partialx

,

we should lay out the gradient
\partialy
\partialx
as a row vector, and
\partialy
\partialx
as a column vector.
  1. If we choose denominator layout for
\partialy
\partialx

,

we should lay out the gradient
\partialy
\partialx
as a column vector, and
\partialy
\partialx
as a row vector.
  1. In the third possibility above, we write
\partialy
\partialx'
and
\partialy
\partialx

,

and use numerator layout.

Not all math textbooks and papers are consistent in this respect throughout. That is, sometimes different conventions are used in different contexts within the same book or paper. For example, some choose denominator layout for gradients (laying them out as column vectors), but numerator layout for the vector-by-vector derivative

\partialy
\partialx

.

Similarly, when it comes to scalar-by-matrix derivatives

\partialy
\partialX
and matrix-by-scalar derivatives
\partialY
\partialx

,

then consistent numerator layout lays out according to and, while consistent denominator layout lays out according to and . In practice, however, following a denominator layout for
\partialY
\partialx

,

and laying the result out according to, is rarely seen because it makes for ugly formulas that do not correspond to the scalar formulas. As a result, the following layouts can often be found:
  1. Consistent numerator layout, which lays out
\partialY
\partialx
according to and
\partialy
\partialX
according to .
  1. Mixed layout, which lays out
\partialY
\partialx
according to and
\partialy
\partialX
according to .
  1. Use the notation
\partialy
\partialX'

,

with results the same as consistent numerator layout.

In the following formulas, we handle the five possible combinations

\partialy
\partialx

,

\partialy
\partialx

,

\partialy
\partialx

,

\partialy
\partialX
and
\partialY
\partialx
separately. We also handle cases of scalar-by-scalar derivatives that involve an intermediate vector or matrix. (This can arise, for example, if a multi-dimensional parametric curve is defined in terms of a scalar variable, and then a derivative of a scalar function of the curve is taken with respect to the scalar that parameterizes the curve.) For each of the various combinations, we give numerator-layout and denominator-layout results, except in the cases above where denominator layout rarely occurs. In cases involving matrices where it makes sense, we give numerator-layout and mixed-layout results. As noted above, cases where vector and matrix denominators are written in transpose notation are equivalent to numerator layout with the denominators written without the transpose.

Keep in mind that various authors use different combinations of numerator and denominator layouts for different types of derivatives, and there is no guarantee that an author will consistently use either numerator or denominator layout for all types. Match up the formulas below with those quoted in the source to determine the layout used for that particular type of derivative, but be careful not to assume that derivatives of other types necessarily follow the same kind of layout.

When taking derivatives with an aggregate (vector or matrix) denominator in order to find a maximum or minimum of the aggregate, it should be kept in mind that using numerator layout will produce results that are transposed with respect to the aggregate. For example, in attempting to find the maximum likelihood estimate of a multivariate normal distribution using matrix calculus, if the domain is a k×1 column vector, then the result using the numerator layout will be in the form of a 1×k row vector. Thus, either the results should be transposed at the end or the denominator layout (or mixed layout) should be used.

! colspan=2
Scalar Column vector (size)Matrix (size)
Notation TypeNotation TypeNotation Type
Scalar Numerator
\partialy
\partialx
Scalar
\partialy
\partialx
Size- column vector
\partialY
\partialx
matrix
DenominatorSize-m row vector
Column vector
(size)
Numerator
\partialy
\partialx
Size- row vector
\partialy
\partialx
matrix
\partialY
\partialx
DenominatorSize- column vector matrix
Matrix
(size)
Numerator
\partialy
\partialX
matrix
\partialy
\partialX
\partialY
\partialX
Denominator matrix

The results of operations will be transposed when switching between numerator-layout and denominator-layout notation.

Numerator-layout notation

Using numerator-layout notation, we have:[1]

\begin{align}

\partialy
\partialx

&=\begin{bmatrix}

\partialy
\partialx1

&

\partialy
\partialx2

& &

\partialy
\partialxn

\end{bmatrix}.\\

\partialy
\partialx

&=\begin{bmatrix}

\partialy1
\partialx

\\

\partialy2
\partialx

\\ \vdots\\

\partialym
\partialx

\\ \end{bmatrix}.\\

\partialy
\partialx

&=\begin{bmatrix}

\partialy1
\partialx1

&

\partialy1
\partialx2

&&

\partialy1
\partialxn

\\

\partialy2
\partialx1

&

\partialy2
\partialx2

&&

\partialy2
\partialxn

\\ \vdots&\vdots&\ddots&\vdots\\

\partialym
\partialx1

&

\partialym
\partialx2

&&

\partialym
\partialxn

\\ \end{bmatrix}.\\

\partialy
\partialX

&=\begin{bmatrix}

\partialy
\partialx11

&

\partialy
\partialx21

&&

\partialy
\partialxp1

\\

\partialy
\partialx12

&

\partialy
\partialx22

&&

\partialy
\partialxp2

\\ \vdots&\vdots&\ddots&\vdots\\

\partialy
\partialx1q

&

\partialy
\partialx2q

&&

\partialy
\partialxpq

\\ \end{bmatrix}. \end{align}

The following definitions are only provided in numerator-layout notation:

\begin{align}

\partialY
\partialx

&=\begin{bmatrix}

\partialy11
\partialx

&

\partialy12
\partialx

&&

\partialy1n
\partialx

\\

\partialy21
\partialx

&

\partialy22
\partialx

&&

\partialy2n
\partialx

\\ \vdots&\vdots&\ddots&\vdots\\

\partialym1
\partialx

&

\partialym2
\partialx

&&

\partialymn
\partialx

\\ \end{bmatrix}.\\ dX&=\begin{bmatrix} dx11&dx12&&dx1n\\ dx21&dx22&&dx2n\\ \vdots&\vdots&\ddots&\vdots\\ dxm1&dxm2&&dxmn\\ \end{bmatrix}. \end{align}

Denominator-layout notation

Using denominator-layout notation, we have:[2]

\begin{align}

\partialy
\partialx

&=\begin{bmatrix}

\partialy
\partialx1

\\

\partialy
\partialx2

\\ \vdots\\

\partialy
\partialxn

\\ \end{bmatrix}.\\

\partialy
\partialx

&=\begin{bmatrix}

\partialy1
\partialx

&

\partialy2
\partialx

& &

\partialym
\partialx

\end{bmatrix}.\\

\partialy
\partialx

&=\begin{bmatrix}

\partialy1
\partialx1

&

\partialy2
\partialx1

&&

\partialym
\partialx1

\\

\partialy1
\partialx2

&

\partialy2
\partialx2

&&

\partialym
\partialx2

\\ \vdots&\vdots&\ddots&\vdots\\

\partialy1
\partialxn

&

\partialy2
\partialxn

&&

\partialym
\partialxn

\\ \end{bmatrix}.\\

\partialy
\partialX

&=\begin{bmatrix}

\partialy
\partialx11

&

\partialy
\partialx12

&&

\partialy
\partialx1q

\\

\partialy
\partialx21

&

\partialy
\partialx22

&&

\partialy
\partialx2q

\\ \vdots&\vdots&\ddots&\vdots\\

\partialy
\partialxp1

&

\partialy
\partialxp2

&&

\partialy
\partialxpq

\\ \end{bmatrix}. \end{align}

Identities

As noted above, in general, the results of operations will be transposed when switching between numerator-layout and denominator-layout notation.

To help make sense of all the identities below, keep in mind the most important rules: the chain rule, product rule and sum rule. The sum rule applies universally, and the product rule applies in most of the cases below, provided that the order of matrix products is maintained, since matrix products are not commutative. The chain rule applies in some of the cases, but unfortunately does not apply in matrix-by-scalar derivatives or scalar-by-matrix derivatives (in the latter case, mostly involving the trace operator applied to matrices). In the latter case, the product rule can't quite be applied directly, either, but the equivalent can be done with a bit more work using the differential identities.

The following identities adopt the following conventions:

Vector-by-vector identities

This is presented first because all of the operations that apply to vector-by-vector differentiation apply directly to vector-by-scalar or scalar-by-vector differentiation simply by reducing the appropriate vector in the numerator or denominator to a scalar.

Condition! scope="col" width="10"
ExpressionNumerator layout, i.e. by and Denominator layout, i.e. by and
is not a function of
\partiala
\partialx

=

0

\partialx
\partialx

=

I

is not a function of
\partialAx
\partialx

=

A

A\top

is not a function of
\partialx\topA
\partialx

=

A\top

A

is not a function of,
\partialau
\partialx

=

a\partialu
\partialx
,
is not a function of
\partialva
\partialx

=

a\partialv
\partialx

\partialv
\partialx

a\top

,
\partialvu
\partialx

=

v

\partialu
\partialx

+u

\partialv
\partialx

v\partialu
\partialx

+

\partialv
\partialx

u\top

is not a function of,
\partialAu
\partialx

=

A\partialu
\partialx
\partialu
\partialx

A\top

,
\partial(u+v)
\partialx

=

\partialu
\partialx

+

\partialv
\partialx
\partialg(u)
\partialx

=

\partialg(u)
\partialu
\partialu
\partialx
\partialu
\partialx
\partialg(u)
\partialu
\partialf(g(u))
\partialx

=

\partialf(g)
\partialg
\partialg(u)
\partialu
\partialu
\partialx
\partialu
\partialx
\partialg(u)
\partialu
\partialf(g)
\partialg

Scalar-by-vector identities

The fundamental identities are placed above the thick black line.

Condition! scope="col" width="200"
ExpressionNumerator layout,
i.e. by ; result is row vector
Denominator layout,
i.e. by ; result is column vector
is not a function of
\partiala
\partialx

=

0\top

[3]

0

is not a function of,
\partialau
\partialx

=

a\partialu
\partialx
,
\partial(u+v)
\partialx

=

\partialu
\partialx

+

\partialv
\partialx

,
\partialuv
\partialx

=

u\partialv
\partialx

+v

\partialu
\partialx

\partialg(u)
\partialx

=

\partialg(u)
\partialu
\partialu
\partialx

\partialf(g(u))
\partialx

=

\partialf(g)
\partialg
\partialg(u)
\partialu
\partialu
\partialx

,
\partial(uv)
\partialx

=

\partialu\topv
\partialx

=

\top\partialv
\partialx
u

+

\top\partialu
\partialx
v

\partialu
\partialx

,

\partialv
\partialx
in numerator layout
\partialu
\partialx

v+

\partialv
\partialx

u

\partialu
\partialx

,

\partialv
\partialx
in denominator layout
,,
is not a function of
\partial(uAv)
\partialx

=

\partialu\topAv
\partialx

=

\topA\partialv
\partialx
u

+v\top

\top\partialu
\partialx
A

\partialu
\partialx

,

\partialv
\partialx
in numerator layout
\partialu
\partialx

Av+

\partialv
\partialx

A\topu

\partialu
\partialx

,

\partialv
\partialx
in denominator layout
\partial2f
\partialx\partialx\top

=

H\top

H

, the Hessian matrix
is not a function of
\partial(ax)
\partialx

=

\partial(xa)
\partialx

=



\partiala\topx
\partialx

=

\partialx\topa
\partialx

=

a\top

a

is not a function of
is not a function of
\partialb\topAx
\partialx

=

b\topA

A\topb

is not a function of
\partialx\topAx
\partialx

=

x\top\left(A+A\top\right)

\left(A+A\top\right)x

\partialx\topAx
\partialx

=

2x\topA

2Ax

is not a function of
\partial2x\topAx
\partialx\partialx\top

=

A+A\top

\partial2x\topAx
\partialx\partialx\top

=

2A

\partial(xx)
\partialx

=

\partialx\topx
\partialx

=

\partial\left\Vertx\right\Vert2
\partialx

=

2x\top

2x

is not a function of,
\partial(au)
\partialx

=

\partiala\topu
\partialx

=

\top\partialu
\partialx
a
\partialu
\partialx
in numerator layout
\partialu
\partialx

a

\partialu
\partialx
in denominator layout
, are not functions of
\partialbf{a
\topbf{x}bf{x}

\topbf{b}}{\partialbf{x}}=

bf{x}\top\left(bf{a}bf{b}\top+bf{b}bf{a}\top\right)

\left(bf{a}bf{b}\top+bf{b}bf{a}\top\right)bf{x}

,,,, are not functions of
\partial(bf{A
bf{x}+

bf{b})\topbf{C}(bf{D}bf{x}+bf{e})}{\partialbf{x}}=

(bf{D}bf{x}+bf{e})\topbf{C}\topbf{A}+(bf{A}bf{x}+bf{b})\topbf{C}bf{D}

bf{D}\topbf{C}\top(bf{A}bf{x}+bf{b})+bf{A}\topbf{C}(bf{D}bf{x}+bf{e})

is not a function of
\partial\|x-a\|
\partialx

=

(x-a)\top
\|x-a\|
x-a
\|x-a\|

Vector-by-scalar identities

Condition! scope="col" width="100"
ExpressionNumerator layout, i.e. by,
result is column vector
Denominator layout, i.e. by,
result is row vector
is not a function of
\partiala
\partialx

=

0

is not a function of,
\partialau
\partialx

=

a\partialu
\partialx
is not a function of x,
\partialAu
\partialx

=

A\partialu
\partialx
\partialu
\partialx

A\top

\partialu\top
\partialx

=

\left(\partialu
\partialx

\right)\top

,
\partial(u+v)
\partialx

=

\partialu
\partialx

+

\partialv
\partialx
,
\partial(u\top x v)
\partialx

=

\left(\partialu
\partialx

\right)\top x v+u\top x

\partialv
\partialx
\partialu
\partialx

x v+u\top x \left(

\partialv
\partialx

\right)\top

\partialg(u)
\partialx

=

\partialg(u)
\partialu
\partialu
\partialx
\partialu
\partialx
\partialg(u)
\partialu
Assumes consistent matrix layout; see below.
\partialf(g(u))
\partialx

=

\partialf(g)
\partialg
\partialg(u)
\partialu
\partialu
\partialx
\partialu
\partialx
\partialg(u)
\partialu
\partialf(g)
\partialg
Assumes consistent matrix layout; see below.
,
\partial(U x v)
\partialx

=

\partialU
\partialx

x v+U x

\partialv
\partialx

v\top x \left(

\partialU
\partialx

\right)+

\partialv
\partialx

x U\top

NOTE: The formulas involving the vector-by-vector derivatives

\partialg(u)
\partialu
and
\partialf(g)
\partialg
(whose outputs are matrices) assume the matrices are laid out consistent with the vector layout, i.e. numerator-layout matrix when numerator-layout vector and vice versa; otherwise, transpose the vector-by-vector derivatives.

Scalar-by-matrix identities

Note that exact equivalents of the scalar product rule and chain rule do not exist when applied to matrix-valued functions of matrices. However, the product rule of this sort does apply to the differential form (see below), and this is the way to derive many of the identities below involving the trace function, combined with the fact that the trace function allows transposing and cyclic permutation, i.e.:

\begin{align} \operatorname{tr}(A)&=

\operatorname{tr}\left(A\top\right)

\\ \operatorname{tr}(ABCD)&=\operatorname{tr}(BCDA)=\operatorname{tr}(CDAB)=\operatorname{tr}(DABC) \end{align}

For example, to compute

\partial\operatorname{tr
(AXBX\topC)}{\partial

X

}:\begin d\operatorname(\mathbf) &= d\operatorname\left(\mathbf\right) = \operatorname\left(d\left(\mathbf\right)\right) \\[1ex] &= \operatorname\left(\mathbf d(\mathbf\right) + d\left(\mathbf)\mathbf\right) \\[1ex] &= \operatorname\left(\mathbf d\left(\mathbf\right)\right) + \operatorname\left(d(\mathbf)\mathbf\right) \\[1ex] &= \operatorname\left(\mathbf d\left(\mathbf\right)\right) + \operatorname\left(\mathbf(d\mathbf)\mathbf\right) \\[1ex] &= \operatorname\left(\mathbf (d\mathbf)^\top\right) + \operatorname(\mathbf\left(d\mathbf)\mathbf\right) \\[1ex] &= \operatorname\left(\left(\mathbf (d\mathbf)^\top\right)^\top\right) + \operatorname\left(\mathbf(d\mathbf)\mathbf\right) \\[1ex] &= \operatorname\left((d\mathbf)\mathbf\right) + \operatorname\left(\mathbf(d\mathbf)\mathbf\right) \\[1ex] &= \operatorname\left(\mathbf(d\mathbf)\right) + \operatorname\left(\mathbf\mathbf(d\mathbf)\right) \\[1ex] &= \operatorname\left(\left(\mathbf + \mathbf\mathbf\right)d\mathbf\right) \\[1ex] &= \operatorname\left(\left(\mathbf + \mathbf\right)^\top d\mathbf\right)\end

Therefore,

\partial\operatorname{tr
\left(AXBX\topC\right)}{\partial

X

} = \mathbf + \mathbf. (numerator layout)
\partial\operatorname{tr
\left(AXBX\topC\right)}{\partial

X

} = \mathbf + \mathbf. (denominator layout)

(For the last step, see the Conversion from differential to derivative form section.)

Condition! scope="col" width="10"
ExpressionNumerator layout, i.e. by Denominator layout, i.e. by
is not a function of
\partiala
\partialX

=

0\top

[4]

0

is not a function of,
\partialau
\partialX

=

a\partialu
\partialX
,
\partial(u+v)
\partialX

=

\partialu
\partialX

+

\partialv
\partialX

,
\partialuv
\partialX

=

u\partialv
\partialX

+v

\partialu
\partialX

\partialg(u)
\partialX

=

\partialg(u)
\partialu
\partialu
\partialX

\partialf(g(u))
\partialX

=

\partialf(g)
\partialg
\partialg(u)
\partialu
\partialu
\partialX

    
\partialg(U)
\partialXij

=

\operatorname{tr}\left(

\partialg(U)
\partialU
\partialU
\partialXij

\right)

\operatorname{tr}\left(\left(

\partialg(U)
\partialU

\right)\top

\partialU
\partialXij

\right)

Both forms assume numerator layout for
\partialU
\partialXij

,


i.e. mixed layout if denominator layout for is being used.
and are not functions of
\partiala\topXb
\partialX

=

ba\top

ab\top

and are not functions of
\partiala\topX\topb
\partialX

=

ab\top

ba\top

, and are not functions of
\partial(Xa+b)\topC(Xa+b)
\partialX

=

\left(\left(C+C\top\right)(Xa+b)a\top\right)\top

\left(C+C\top\right)(Xa+b)a\top

, and are not functions of
\partial(Xa)\topC(Xb)
\partialX

=

\left(CXba\top+C\topXab\top\right)\top

CXba\top+C\topXab\top

\partial\operatorname{tr
(X)}{\partial

X

} =

I

,
\partial\operatorname{tr
(U

+V)}{\partialX

} =
\partial\operatorname{tr
(U)}{\partial

X

} + \frac
is not a function of,
\partial\operatorname{tr
(aU)}{\partial

X

} =
a\partial\operatorname{tr
(U)}{\partial

X

}
is any polynomial with scalar coefficients, or any matrix function defined by an infinite polynomial series (e.g.,,,, etc. using a Taylor series); is the equivalent scalar function, is its derivative, and is the corresponding matrix function
\partial\operatorname{tr
(g(X))}{\partial

X

} =

g'(X)

\left(g'(X)\right)\top

is not a function of [5]     
\partial\operatorname{tr
(AX)}{\partial

X

} = \frac =

A

A\top

is not a function of     
\partial\operatorname{tr
\left(AX\top\right)}{\partial

X

} = \frac =

A\top

A

is not a function of     
\partial\operatorname{tr
\left(X\topAX\right)}{\partial

X

} =

X\top\left(A+A\top\right)

\left(A+A\top\right)X

is not a function of     
\partial\operatorname{tr
(X-1A)}{\partial

X

} =

-X-1AX-1

-\left(X-1\right)\topA\top\left(X-1\right)\top

, are not functions of
\partial\operatorname{tr
(AXB)}{\partial

X

} = \frac =

BA

A\topB\top
,, are not functions of
\partial\operatorname{tr
\left(AXBX\topC\right)}{\partial

X

} =
BX\topCA

+

B\topX\topA\topC\top
A\topC\topXB\top

+CAXB

is a positive integer     
\partial\operatorname{tr
\left(X

n\right)}{\partialX

} =

nXn-1

n\left(Xn-1\right)\top

is not a function of,
is a positive integer
    
\partial\operatorname{tr
\left(AX

n\right)}{\partialX

} =
n-1
\sum
i=0

XiAXn-i-1

n-1
\sum
i=0

\left(XiAXn-i-1\right)\top

    
\partial\operatorname{tr
\left(e

X\right)}{\partialX

} =

eX

\left(eX\right)\top

    
\partial\operatorname{tr
(\sin(X))}{\partial

X

} =

\cos(X)

(\cos(X))\top

[6]     
\partial|X|
\partialX

=

\operatorname{cofactor}(X)\top=

\mathbf\mathbf^

\operatorname{cofactor}(X)=

\mathbf\left(\mathbf^\right)^\top
is not a function of
\partialln|aX|
\partialX

=

[7]

X-1

\left(X-1\right)\top

, are not functions of     
\partial|AXB|
\partialX

=

\mathbf\mathbf^
\mathbf\left(\mathbf^\right)^\top
is a positive integer     
\partial\left|Xn\right|
\partialX

=

n\left

\mathbf^n\right\mathbf^

n\left

\mathbf^n\right\left(\mathbf^\right)^\top
(see pseudo-inverse)     
\partialln\left|X\topX\right|
\partialX

=

2X+

2\left(X+\right)\top

(see pseudo-inverse)     
\partialln\left|X\topX\right|
\partialX+

=

-2X

-2X\top

is not a function of,
is square and invertible
\partial
\left|X\topAX\right|
\partialX

=

2\left

\mathbf\mathbf\mathbf\right\mathbf^ = 2\left\mathbf\right\mathbf\mathbf\mathbf^

2\left

\mathbf\mathbf\mathbf\right\left(\mathbf^\right)^\top
is not a function of,
is non-square,
is symmetric
\partial
\left|X\topAX\right|
\partialX

=

2\left

\mathbf\mathbf\mathbf\right\left(\mathbf\right)^\mathbf

2\left

\mathbf\mathbf\mathbf\right\mathbf\left(\mathbf\right)^
is not a function of,
is non-square,
is non-symmetric
\partial
|X\topAX|
\partialX

=

\begin{align} \left

\mathbf\mathbf\mathbf\right\Big(&\left(\mathbf\right)^\mathbf + \\ &\left(\mathbf\right)^\mathbf\Big)\end

\begin{align} \left

\mathbf\mathbf\mathbf\right\Big(&\mathbf\left(\mathbf\right)^ + \\ &\mathbf\left(\mathbf\right)^\Big)\end

Matrix-by-scalar identities

Condition! scope="col" width="100"
ExpressionNumerator layout, i.e. by
\partialaU
\partialx

=

a\partialU
\partialx
, are not functions of x,
\partialAUB
\partialx

=

A\partialU
\partialx

B

,
\partial(U+V)
\partialx

=

\partialU
\partialx

+

\partialV
\partialx
,
\partial(UV)
\partialx

=

U\partialV
\partialx

+

\partialU
\partialx

V

,
\partial(UV)
\partialx

=

U

\partialV
\partialx

+

\partialU
\partialx

V

,
\partial(U\circV)
\partialx

=

U\circ

\partialV
\partialx

+

\partialU
\partialx

\circV

\partialU-1
\partialx

=

-U-1

\partialU
\partialx

U-1

\partial2U-1
\partialx\partialy

=

U-1\left(

\partialU
\partialx

U-1

\partialU
\partialy

-

\partial2U
\partialx\partialy

+

\partialU
\partialy

U-1

\partialU
\partialx

\right)U-1

is not a function of, is any polynomial with scalar coefficients, or any matrix function defined by an infinite polynomial series (e.g.,,,, etc.); is the equivalent scalar function, is its derivative, and is the corresponding matrix function
\partialg(xA)
\partialx

=

Ag'(xA)=g'(xA)A

is not a function of
\partialexA
\partialx

=

AexA=exAA

See also: Derivative of the exponential map.

Scalar-by-scalar identities

With vectors involved

Condition! scope="col" width="10"
ExpressionAny layout (assumes dot product ignores row vs. column layout)
\partialg(u)
\partialx

=

\partialg(u)
\partialu

\partialu
\partialx
,
\partial(uv)
\partialx

=

u

\partialv
\partialx

+

\partialu
\partialx

v

With matrices involved

Condition! scope="col" width="100"
ExpressionConsistent numerator layout,
i.e. by and
Mixed layout,
i.e. by and
\partial|U|
\partialx

=

\mathbf\operatorname\left(\mathbf^\frac\right)
\partialln|U|
\partialx

=

\operatorname{tr}\left(U-1

\partialU
\partialx

\right)

\partial2|U|
\partialx2

=

\left

\mathbf\right\left[\operatorname{tr}\left(\mathbf{U}^{-1}\frac{\partial^2 \mathbf{U}}{\partial x^2}\right) + \operatorname{tr}^2\left(\mathbf{U}^{-1}\frac{\partial \mathbf{U}}{\partial x}\right) - \operatorname{tr}\left(\left(\mathbf{U}^{-1}\frac{\partial \mathbf{U}}{\partial x}\right)^2\right) \right]
\partialg(U)
\partialx

=

\operatorname{tr}\left(

\partialg(U)
\partialU
\partialU
\partialx

\right)

\operatorname{tr}\left(\left(

\partialg(U)
\partialU

\right)\top

\partialU
\partialx

\right)

is not a function of, is any polynomial with scalar coefficients, or any matrix function defined by an infinite polynomial series (e.g.,,,, etc.); is the equivalent scalar function, is its derivative, and is the corresponding matrix function.
\partial\operatorname{tr
(g(xA))}{\partial

x}=

\operatorname{tr}\left(Ag'(xA)\right)

is not a function of
\partial\operatorname{tr
\left(e

xA\right)}{\partialx}=

\operatorname{tr}\left(AexA\right)

Identities in differential form

It is often easier to work in differential form and then convert back to normal derivatives. This only works well using the numerator layout. In these rules, is a scalar.

Differential identities: scalar involving matrix! Expression !! Result (numerator layout)

d(\operatorname{tr}(X))=

\operatorname{tr}(dX)

d(

\mathbf) =
\mathbf\operatorname\left(\mathbf^d\mathbf\right) = \operatorname(\operatorname(\mathbf)d\mathbf)

d(ln

\mathbf) =

\operatorname{tr}\left(X-1dX\right)

Differential identities: matrix[8] [9] ! Condition !! Expression !! Result (numerator layout)
A is not a function of

d(A)=

0

a is not a function of

d(aX)=

adX

d(X+Y)=

dX+dY

d(XY)=

(dX)Y+X(dY)

(Kronecker product)

d(XY)=

(dX)Y+X(dY)

(Hadamard product)

d(X\circY)=

(dX)\circY+X\circ(dY)

d\left(X\top\right)=

(dX)\top

d\left(X-1\right)=

-X-1\left(dX\right)X-1

(conjugate transpose)

d\left(XH\right)=

(dX)H

is a positive integer

d\left(Xn\right)=

n-1
\sum
i=0

Xi(dX)Xn-i-1

d\left(eX\right)=

1
\int
0

eaX(dX)e(1-a)Xda

d\left(log{X}\right)=

infty
\int
0

(X+zI)-1(dX)(X+zI)-1dz

X=\sumiλiPi

is diagonalizable

PiPj=\deltaijPi


is differentiable at every eigenvalue

λi

d\left(f(X)\right)=

\sumijPi(dX)Pj\begin{cases} f'(λi)&λi=λj\\

f(λi)-f(λj)
λij

&λiλj \end{cases}

In the last row,

\deltaij

is the Kronecker delta and

(Pk)ij=(Q)ik(Q-1)kj

is the set of orthogonal projection operators that project onto the -th eigenvector of . is the matrix of eigenvectors of

X=Q\boldsymbol{Λ}Q-1

, and

(\boldsymbol{Λ})ii=λi

are the eigenvalues.The matrix function

f(X)

is defined in terms of the scalar function

f(x)

for diagonalizable matrices by f(\mathbf) = \sum_i f(\lambda_i) \mathbf_i where \mathbf = \sum_i \lambda_i \mathbf_i with

To convert to normal derivative form, first convert it to one of the following canonical forms, and then use these identities:

Conversion from differential to derivative form! Canonical differential form !! Equivalent derivative form (numerator layout)

dy=adx

dy
dx

=a

dy=a\topdx

dy
dx

=a\top

dy=\operatorname{tr}(AdX)

dy
dX

=A

dy=adx

dy
dx

=a

dy=Adx

dy
dx

=A

dY=Adx

dY
dx

=A

Applications

Matrix differential calculus is used in statistics and econometrics, particularly for the statistical analysis of multivariate distributions, especially the multivariate normal distribution and other elliptical distributions.[10] [11] [12]

It is used in regression analysis to compute, for example, the ordinary least squares regression formula for the case of multiple explanatory variables.[13] It is also used in random matrices, statistical moments, local sensitivity and statistical diagnostics.[14] [15]

See also

Further reading

External links

Software

Information

Notes and References

  1. Web site: Minka . Thomas P. . Old and New Matrix Algebra Useful for Statistics . MIT Media Lab note (1997; revised 12/00) . December 28, 2000 . 5 February 2016.
  2. Book: http://www.colorado.edu/engineering/cas/courses.d/IFEM.d/IFEM.AppD.d/IFEM.AppD.pdf . Appendix D, Linear Algebra: Determinants, Inverses, Rank . Carlos A. . Felippa . ASEN 5007: Introduction To Finite Element Methods . University of Colorado . Boulder, Colorado . 5 February 2016. Uses the Hessian (transpose to Jacobian) definition of vector and matrix derivatives.
  3. Here,

    0

    refers to a column vector of all 0's, of size, where is the length of .
  4. Here,

    0

    refers to a matrix of all 0's, of the same shape as .
  5. Web site: Duchi. John C. Properties of the Trace and Matrix Derivatives. Stanford University . 5 February 2016.
  6. See for the derivation.
  7. The constant disappears in the result. This is intentional. In general,

    \frac = \frac\frac = \fraca\frac = \frac \frac = \frac.or, also

    \frac = \frac = \frac + \frac = \frac.

  8. Giles . Mike B. . Bischof . Christian H. . Bücker . H. Martin . Hovland . Paul . Naumann . Uwe . Utke . Jean . Collected matrix derivative results for forward and reverse mode algorithmic differentiation . 10.1007/978-3-540-68942-3_4 . 978-3-540-68935-5 . Berlin . 2531677 . 35–44 . Springer . Lecture Notes in Computational Science and Engineering . Advances in Automatic Differentiation . 64 . 2008.
  9. https://www.ias.edu/sites/default/files/sns/files/1-matrixlog_tex(1).pdf Unpublished memo
  10. Book: Generalized multivariate analysis . Kai-Tai . Fang . Kai-Tai Fang. Yao-Ting . Zhang . Science Press (Beijing) and Springer-Verlag (Berlin) . 1990 . 3540176519 . 9783540176510.
  11. Book: Pan . Jianxin . Fang . Kaitai . Kaitai Fang . Growth curve models and statistical diagnostics . Science Press . Beijing . 2007 . 9780387950532 .
  12. Book: Kollo . Tõnu . von Rosen . Dietrich . Advanced multivariate statistics with matrices . Dordrecht . Springer . 2005 . 978-1-4020-3418-3 .
  13. Book: Magnus . Jan . Neudecker . Heinz . Matrix differential calculus with applications in statistics and econometrics . John Wiley . New York . 2019 . 9781119541202 .
  14. Liu. Shuangzhe. Leiva. Victor. Zhuang. Dan. Ma. Tiefeng. Figueroa-Zúñiga. Jorge I.. 2022. Matrix differential calculus with applications in the multivariate linear model and its diagnostics. Journal of Multivariate Analysis. en. 188. 104849. 10.1016/j.jmva.2021.104849 . free.
  15. Liu. Shuangzhe. Trenkler. Götz. Kollo. Tõnu. von Rosen. Dietrich. Baksalary. Oskar Maria. 2023. Professor Heinz Neudecker and matrix differential calculus. Statistical Papers. en. 10.1007/s00362-023-01499-w. 263661094 .