Docsity

The Matrix Cookbook, Exams of Computer science

Toggle sidebar
bg1
The Matrix Cookbook
Kaare Brandt Petersen
Michael Syskind Pedersen
Version: February 16, 2006
What is this? These pages are a collection of facts (identities, approxima-
tions, inequalities, relations, ...) about matrices and matters relating to them.
It is collected in this form for the convenience of anyone who wants a quick
desktop reference .
Disclaimer: The identities, approximations and relations presented here were
obviously not invented but collected, borrowed and copied from a large amount
of sources. These sources include similar but shorter notes found on the internet
and appendices in books - see the references for a full list.
Errors: Very likely there are errors, typos, and mistakes for which we apolo-
gize and would be grateful to receive corrections at [email protected].
Its ongoing: The project of keeping a large repository of relations involving
matrices is naturally ongoing and the version will be apparent from the date in
the header.
Suggestions: Your suggestion for additional content or elaboration of some
topics is most welcome at [email protected].
Keywords: Matrix algebra, matrix relations, matrix identities, derivative of
determinant, derivative of inverse matrix, differentiate a matrix.
Acknowledgements: We would like to thank the following for contribu-
tions and suggestions: Christian Rishøj, Douglas L. Theobald, Esben Hoegh-
Rasmussen, Lars Christiansen, and Vasile Sima. We would also like thank The
Oticon Foundation for funding our PhD studies.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

Partial preview of the text

Download The Matrix Cookbook and more Exams Computer science in PDF only on Docsity!

The Matrix Cookbook

Kaare Brandt Petersen

Michael Syskind Pedersen

Version: February 16, 2006

What is this? These pages are a collection of facts (identities, approxima- tions, inequalities, relations, ...) about matrices and matters relating to them. It is collected in this form for the convenience of anyone who wants a quick desktop reference.

Disclaimer: The identities, approximations and relations presented here were obviously not invented but collected, borrowed and copied from a large amount of sources. These sources include similar but shorter notes found on the internet and appendices in books - see the references for a full list.

Errors: Very likely there are errors, typos, and mistakes for which we apolo- gize and would be grateful to receive corrections at [email protected].

Its ongoing: The project of keeping a large repository of relations involving matrices is naturally ongoing and the version will be apparent from the date in the header.

Suggestions: Your suggestion for additional content or elaboration of some topics is most welcome at [email protected].

Keywords: Matrix algebra, matrix relations, matrix identities, derivative of determinant, derivative of inverse matrix, differentiate a matrix.

Acknowledgements: We would like to thank the following for contribu- tions and suggestions: Christian Rishøj, Douglas L. Theobald, Esben Hoegh- Rasmussen, Lars Christiansen, and Vasile Sima. We would also like thank The Oticon Foundation for funding our PhD studies.

CONTENTS CONTENTS

CONTENTS CONTENTS

Notation and Nomenclature

A Matrix Aij Matrix indexed for some purpose Ai Matrix indexed for some purpose Aij^ Matrix indexed for some purpose An^ Matrix indexed for some purpose or The n.th power of a square matrix A−^1 The inverse matrix of the matrix A A+^ The pseudo inverse matrix of the matrix A (see Sec. 3.6) A^1 /^2 The square root of a matrix (if unique), not elementwise (A)ij The (i, j).th entry of the matrix A Aij The (i, j).th entry of the matrix A [A]ij The ij-submatrix, i.e. A with i.th row and j.th column deleted a Vector ai Vector indexed for some purpose ai The i.th element of the vector a a Scalar

<z Real part of a scalar <z Real part of a vector <Z Real part of a matrix =z Imaginary part of a scalar =z Imaginary part of a vector =Z Imaginary part of a matrix

det(A) Determinant of A Tr(A) Trace of the matrix A diag(A) Diagonal matrix of the matrix A, i.e. (diag(A))ij = δij Aij vec(A) The vector-version of the matrix A (see Sec. 9.2.2) ||A|| Matrix norm (subscript if any denotes what norm) AT^ Transposed matrix A∗^ Complex conjugated matrix AH^ Transposed and complex conjugated matrix (Hermitian)

A ◦ B Hadamard (elementwise) product A ⊗ B Kronecker product

0 The null matrix. Zero in all entries. I The identity matrix Jij^ The single-entry matrix, 1 at (i, j) and zero elsewhere Σ A positive definite matrix Λ A diagonal matrix

1 BASICS

1 Basics

(AB)−^1 = B−^1 A−^1

(ABC...)−^1 = ...C−^1 B−^1 A−^1

(AT^ )−^1 = (A−^1 )T

(A + B)T^ = AT^ + BT

(AB)T^ = BT^ AT

(ABC...)T^ = ...CT^ BT^ AT

(AH^ )−^1 = (A−^1 )H

(A + B)H^ = AH^ + BH

(AB)H^ = BH^ AH

(ABC...)H^ = ...CH^ BH^ AH

1.1 Trace and Determinants

Tr(A) =

iAii Tr(A) =

iλi,^ λi^ = eig(A) Tr(A) = Tr(AT^ ) Tr(AB) = Tr(BA) Tr(A + B) = Tr(A) + Tr(B) Tr(ABC) = Tr(BCA) = Tr(CAB) det(A) =

iλi^ λi^ = eig(A) det(AB) = det(A) det(B) det(A−^1 ) = 1 / det(A) det(I + uvT^ ) = 1 + uT^ v

1.2 The Special Case 2x

Consider the matrix A

A =

[

A 11 A 12

A 21 A 22

]

Determinant and trace

det(A) = A 11 A 22 − A 12 A 21

Tr(A) = A 11 + A 22

Eigenvalues λ^2 − λ · Tr(A) + det(A) = 0

2 DERIVATIVES

2 Derivatives

This section is covering differentiation of a number of expressions with respect to a matrix X. Note that it is always assumed that X has no special structure, i.e. that the elements of X are independent (e.g. not symmetric, Toeplitz, positive definite). See section 2.5 for differentiation of structured matrices. The basic assumptions can be written in a formula as

∂Xkl ∂Xij

= δikδlj

that is for e.g. vector forms, [ ∂x ∂y

]

i

∂xi ∂y

[

∂x ∂y

]

i

∂x ∂yi

[

∂x ∂y

]

ij

∂xi ∂yj

The following rules are general and very useful when deriving the differential of an expression ([13]):

∂A = 0 (A is a constant) (1) ∂(αX) = α∂X (2) ∂(X + Y) = ∂X + ∂Y (3) ∂(Tr(X)) = Tr(∂X) (4) ∂(XY) = (∂X)Y + X(∂Y) (5) ∂(X ◦ Y) = (∂X) ◦ Y + X ◦ (∂Y) (6) ∂(X ⊗ Y) = (∂X) ⊗ Y + X ⊗ (∂Y) (7) ∂(X−^1 ) = −X−^1 (∂X)X−^1 (8) ∂(det(X)) = det(X)Tr(X−^1 ∂X) (9) ∂(ln(det(X))) = Tr(X−^1 ∂X) (10) ∂XT^ = (∂X)T^ (11) ∂XH^ = (∂X)H^ (12)

2.1 Derivatives of a Determinant

2.1.1 General form

∂ det(Y) ∂x = det(Y)Tr

[

Y−^1

∂Y

∂x

]

2.1.2 Linear forms

∂ det(X) ∂X = det(X)(X−^1 )T

∂ det(AXB) ∂X = det(AXB)(X−^1 )T^ = det(AXB)(XT^ )−^1

2.2 Derivatives of an Inverse 2 DERIVATIVES

2.1.3 Square forms

If X is square and invertible, then

∂ det(XT^ AX) ∂X

= 2 det(XT^ AX)X−T

If X is not square but A is symmetric, then

∂ det(XT^ AX) ∂X

= 2 det(XT^ AX)AX(XT^ AX)−^1

If X is not square and A is not symmetric, then

∂ det(XT^ AX) ∂X

= det(XT^ AX)(AX(XT^ AX)−^1 + AT^ X(XT^ AT^ X)−^1 ) (13)

2.1.4 Other nonlinear forms

Some special cases are (See [8, 7])

∂ ln det(XT^ X)| ∂X

= 2(X+)T

∂ ln det(XT^ X) ∂X+^

= − 2 XT

∂ ln | det(X)| ∂X

= (X−^1 )T^ = (XT^ )−^1

∂ det(Xk) ∂X

= k det(Xk)X−T

2.2 Derivatives of an Inverse

From [19] we have the basic identity

∂Y−^1 ∂x

= −Y−^1

∂Y

∂x

Y−^1

from which it follows

∂(X−^1 )kl ∂Xij

= −(X−^1 )ki(X−^1 )jl

∂aT^ X−^1 b ∂X = −X−T^ abT^ X−T ∂ det(X−^1 ) ∂X

= − det(X−^1 )(X−^1 )T

∂Tr(AX−^1 B) ∂X

= −(X−^1 BAX−^1 )T

2.3 Derivatives of Matrices, Vectors and Scalar Forms 2 DERIVATIVES

Assume W is symmetric, then

∂ ∂s

(x − As)T^ W(x − As) = − 2 AT^ W(x − As) ∂ ∂s

(x − s)T^ W(x − s) = − 2 W(x − s) ∂ ∂x

(x − As)T^ W(x − As) = 2 W(x − As) ∂ ∂A

(x − As)T^ W(x − As) = − 2 W(x − As)sT

2.3.3 Higher order and non-linear

∂X

aT^ Xnb =

n∑− 1

r=

(Xr^ )T^ abT^ (Xn−^1 −r^ )T^ (14)

∂X

aT^ (Xn)T^ Xnb =

n∑− 1

r=

[

Xn−^1 −r^ abT^ (Xn)T^ Xr

+(Xr^ )T^ XnabT^ (Xn−^1 −r^ )T^

]

See B.1.1 for a proof. Assume s and r are functions of x, i.e. s = s(x), r = r(x), and that A is a constant, then

∂ ∂x

sT^ Ar =

[

∂s ∂x

]T

Ar + sT^ A

[

∂r ∂x

]

2.3.4 Gradient and Hessian

Using the above we have for the gradient and the hessian

f = xT^ Ax + bT^ x

∇xf =

∂f ∂x = (A + AT^ )x + b

∂^2 f ∂x∂xT^

= A + AT

2.4 Derivatives of Traces 2 DERIVATIVES

2.4 Derivatives of Traces

2.4.1 First Order

∂X

Tr(X) = I ∂ ∂X

Tr(XA) = AT^ (16) ∂ ∂X

Tr(AXB) = AT^ BT ∂ ∂X

Tr(AXT^ B) = BA ∂ ∂X

Tr(XT^ A) = A ∂ ∂X

Tr(AXT^ ) = A

2.4.2 Second Order

∂X

Tr(X^2 ) = 2 XT ∂ ∂X

Tr(X^2 B) = (XB + BX)T ∂ ∂X

Tr(XT^ BX) = BX + BT^ X ∂ ∂X

Tr(XBXT^ ) = XBT^ + XB ∂ ∂X

Tr(AXBX) = AT^ XT^ BT^ + BT^ XT^ AT ∂ ∂X

Tr(XT^ X) = 2 X ∂ ∂X

Tr(BXXT^ ) = (B + BT^ )X ∂ ∂X

Tr(BT^ XT^ CXB) = CT^ XBBT^ + CXBBT ∂ ∂X

Tr

[

XT^ BXC

]

= BXC + BT^ XCT

∂X

Tr(AXBXT^ C) = AT^ CT^ XBT^ + CAXB ∂ ∂X

Tr

[

(AXb + c)(AXb + c)T^

]

= 2 AT^ (AXb + c)bT

See [7].

2.5 Derivatives of Structured Matrices 2 DERIVATIVES

2.5.1 The Chain Rule

Sometimes the objective is to find the derivative of a matrix which is a function of another matrix. Let U = f (X), the goal is to find the derivative of the function g(U) with respect to X:

∂g(U) ∂X

∂g(f (X)) ∂X

Then the Chain Rule can then be written the following way:

∂g(U) ∂X

∂g(U) ∂xij

∑^ M

k=

∑^ N

l=

∂g(U) ∂ukl

∂ukl ∂xij

Using matrix notation, this can be written as:

∂g(U) ∂Xij

= Tr

[

∂g(U) ∂U

)T^

∂U

∂Xij

]

2.5.2 Symmetric

If A is symmetric, then Sij^ = Jij^ + Jji^ − Jij^ Jij^ and therefore

df dA

[

∂f ∂A

]

[

∂f ∂A

]T

− diag

[

∂f ∂A

]

That is, e.g., ([5], [20]):

∂Tr(AX) ∂X

= A + AT^ − (A ◦ I), see (23) (20) ∂ det(X) ∂X

= det(X)(2X−^1 − (X−^1 ◦ I)) (21) ∂ ln det(X) ∂X

= 2 X−^1 − (X−^1 ◦ I) (22)

2.5.3 Diagonal

If X is diagonal, then ([13]):

∂Tr(AX) ∂X

= A ◦ I (23)

2.5.4 Toeplitz

Like symmetric matrices and diagonal matrices also Toeplitz matrices has a special structure which should be taken into account when the derivative with respect to a matrix with Toeplitz structure.

2.5 Derivatives of Structured Matrices 2 DERIVATIVES

∂Tr(AT) ∂T

∂Tr(TA) ∂T

Tr(A) Tr([AT^ ]n 1 ) Tr([[AT^ ] 1 n]n− 1 , 2 ) · · · An 1 Tr([AT^ ] 1 n)) Tr(A)

.. . .. .

.. . Tr([[AT^ ] 1 n] 2 ,n− 1 )

.. . .. . .. . (^) Tr([[AT^ ] 1 n]n− 1 , 2 ) .. . .. . .. . .. . (^) Tr([AT^ ]n 1 ) A 1 n · · · Tr([[AT^ ] 1 n] 2 ,n− 1 ) Tr([AT^ ] 1 n)) Tr(A)

≡ α(A)

As it can be seen, the derivative α(A) also has a Toeplitz structure. Each value in the diagonal is the sum of all the diagonal valued in A, the values in the diagonals next to the main diagonal equal the sum of the diagonal next to the main diagonal in AT^. This result is only valid for the unconstrained Toeplitz matrix. If the Toeplitz matrix also is symmetric, the same derivative yields

∂Tr(AT) ∂T

∂Tr(TA) ∂T = α(A) + α(A)T^ − α(A) ◦ I (25)

3.2 Exact Relations 3 INVERSES

3.1.5 Condition number

The condition number of a matrix c(A) is the ratio between the largest and the smallest singular value of a matrix (see Section 5.2 on singular values),

c(A) = d+ d−

The condition number can be used to measure how singular a matrix is. If the condition number is large, it indicates that the matrix is nearly singular. The condition number can also be estimated from the matrix norms. Here

c(A) = ‖A‖ · ‖A−^1 ‖, (32)

where ‖ · ‖ is a norm such as e.g the 1-norm, the 2-norm, the ∞-norm or the Frobenius norm (see Sec 9.4 for more on matrix norms).

3.2 Exact Relations

3.2.1 The Woodbury identity

(A + CBCT^ )−^1 = A−^1 − A−^1 C(B−^1 + CT^ A−^1 C)−^1 CT^ A−^1

If P, R are positive definite, then (see [22])

(P−^1 + BT^ R−^1 B)−^1 BT^ R−^1 = PBT^ (BPBT^ + R)−^1

3.2.2 The Kailath Variant

(A + BC)−^1 = A−^1 − A−^1 B(I + CA−^1 B)−^1 CA−^1

See [4] page 153.

3.2.3 The Searle Set of Identities

The following set of identities, can be found in [17], page 151,

(I + A−^1 )−^1 = A(A + I)−^1

(A + BBT^ )−^1 B = A−^1 B(I + BT^ A−^1 B)−^1 (A−^1 + B−^1 )−^1 = A(A + B)−^1 B = B(A + B)−^1 A A − A(A + B)−^1 A = B − B(A + B)−^1 B A−^1 + B−^1 = A−^1 (A + B)B−^1 (I + AB)−^1 = I − A(I + BA)−^1 B (I + AB)−^1 A = A(I + BA)−^1

3.3 Implication on Inverses 3 INVERSES

3.3 Implication on Inverses

(A + B)−^1 = A−^1 + B−^1 ⇒ AB−^1 A = BA−^1 B

See [17].

3.3.1 A PosDef identity

Assume P, R to be positive definite and invertible, then

(P−^1 + BT^ R−^1 B)−^1 BT^ R−^1 = PBT^ (BPBT^ + R)−^1

See [22].

3.4 Approximations

(I + A)−^1 = I − A + A^2 − A^3 + ...

A − A(I + A)−^1 A ∼= I − A−^1 if A large and symmetric

If σ^2 is small then

(Q + σ^2 M)−^1 ∼= Q−^1 − σ^2 Q−^1 MQ−^1

3.5 Generalized Inverse

3.5.1 Definition

A generalized inverse matrix of the matrix A is any matrix A−^ such that (see [18]) AA−A = A

The matrix A−^ is not unique.

3.6 Pseudo Inverse

3.6.1 Definition

The pseudo inverse (or Moore-Penrose inverse) of a matrix A is the matrix A+ that fulfils

I AA+A = A II A+AA+^ = A+ III AA+^ symmetric IV A+A symmetric

The matrix A+^ is unique and does always exist.

4 COMPLEX MATRICES

4 Complex Matrices

4.1 Complex Derivatives

In order to differentiate an expression f (z) with respect to a complex z, the Cauchy-Riemann equations have to be satisfied ([7]):

df (z) dz

∂<(f (z)) ∂<z

  • i ∂=(f (z)) ∂<z

and df (z) dz

= −i

∂<(f (z)) ∂=z

∂=(f (z)) ∂=z

or in a more compact form:

∂f (z) ∂=z

= i

∂f (z) ∂<z

A complex function that satisfies the Cauchy-Riemann equations for points in a region R is said yo be analytic in this region R. In general, expressions involving complex conjugate or conjugate transpose do not satisfy the Cauchy-Riemann equations. In order to avoid this problem, a more generalized definition of complex derivative is used ([16], [6]):

  • Generalized Complex Derivative:

df (z) dz

( (^) ∂f (z) ∂<z

− i ∂f (z) ∂=z

  • Conjugate Complex Derivative

df (z) dz∗^

( (^) ∂f (z) ∂<z

  • i ∂f (z) ∂=z

The Generalized Complex Derivative equals the normal derivative, when f is an analytic function. For a non-analytic function such as f (z) = z∗, the derivative equals zero. The Conjugate Complex Derivative equals zero, when f is an analytic function. The Conjugate Complex Derivative has e.g been used by [14] when deriving a complex gradient. Notice: df (z) dz

∂f (z) ∂<z

  • i ∂f (z) ∂=z
  • Complex Gradient Vector: If f is a real function of a complex vector z, then the complex gradient vector is given by ([11, p. 798])

∇f (z) = 2

df (z) dz∗^

∂f (z) ∂<z

  • i

∂f (z) ∂=z

4.1 Complex Derivatives 4 COMPLEX MATRICES

  • Complex Gradient Matrix: If f is a real function of a complex matrix Z, then the complex gradient matrix is given by ([2])

∇f (Z) = 2

df (Z) dZ∗^

∂f (Z) ∂<Z

  • i

∂f (Z) ∂=Z

These expressions can be used for gradient descent algorithms.

4.1.1 The Chain Rule for complex numbers

The chain rule is a little more complicated when the function of a complex u = f (x) is non-analytic. For a non-analytic function, the following chain rule can be applied ([7])

∂g(u) ∂x

∂g ∂u

∂u ∂x

∂g ∂u∗

∂u∗ ∂x

∂g ∂u

∂u ∂x

( (^) ∂g∗ ∂u

)∗ (^) ∂u∗ ∂x

Notice, if the function is analytic, the second term reduces to zero, and the func- tion is reduced to the normal well-known chain rule. For the matrix derivative of a scalar function g(U), the chain rule can be written the following way:

∂g(U) ∂X

Tr(( ∂g ∂(UU ))T^ ∂U) ∂X

Tr(( ∂g ∂U(U∗ ))T^ ∂U∗) ∂X

4.1.2 Complex Derivatives of Traces

If the derivatives involve complex numbers, the conjugate transpose is often in- volved. The most useful way to show complex derivative is to show the derivative with respect to the real and the imaginary part separately. An easy example is:

∂Tr(X∗) ∂<X

∂Tr(XH^ ) ∂<X

= I (43)

i

∂Tr(X∗) ∂=X

= i

∂Tr(XH^ ) ∂=X

= I (44)

Since the two results have the same sign, the conjugate complex derivative (37) should be used.

∂Tr(X) ∂<X

∂Tr(XT^ ) ∂<X

= I (45)

i

∂Tr(X) ∂=X

= i

∂Tr(XT^ ) ∂=X

= −I (46)

Here, the two results have different signs, the generalized complex derivative (36) should be used. Hereby, it can be seen that (16) holds even if X is a