Docsity

Chapter 1 Matrix Algebra Review, Summaries of Algebra

Toggle sidebar
bg1
Chapter 1
Matrix Algebra Review
This chapter reviews some basic matrix algebra concepts that we will use
throughout the book.
Updated: August 15, 2013.
1.1 Matrices and Vectors
Amatrix is just an array of numbers. The dimension of a matrix is deter-
mined by the number of its rows and columns. For example, a matrix A
with rows and columns is illustrated below
A
(×)=
⎡
⎢
⎢
⎢
⎢
⎢
⎢
⎣
11 12 
1
21 22 
2
.
.
..
.
. .
.
.
12

⎤
⎥
⎥
⎥
⎥
⎥
⎥
⎦
where  denotes the  row and  column element of A
Avector is simply a matrix with 1column. For example,
x
(×1) =
⎡
⎢
⎢
⎢
⎢
⎢
⎢
⎣
1
2
.
.
.
⎤
⎥
⎥
⎥
⎥
⎥
⎥
⎦
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Chapter 1 Matrix Algebra Review and more Summaries Algebra in PDF only on Docsity!

Chapter 1

Matrix Algebra Review

This chapter reviews some basic matrix algebra concepts that we will use throughout the book.

Updated: August 15, 2013.

1.1 Matrices and Vectors

A matrix is just an array of numbers. The dimension of a matrix is deter- mined by the number of its rows and columns. For example, a matrix A with  rows and  columns is illustrated below

A

(×)

where  denotes the ^ row and ^ column element of A A vector is simply a matrix with 1 column. For example,

x (×1)

2 CHAPTER 1 MATRIX ALGEBRA REVIEW

is an × 1 vector with elements  1   2       Vectors and matrices are often written in bold type (or underlined) to distinguish them from scalars (single elements of vectors or matrices).

Example 1 Matrix creation in R

In R, matrix objects are created using the matrix() function. For example, to create the 2 × 3 matrix

A

(2×3)

use

> matA = matrix(data=c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE) > matA [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 > class(matA) [1] "matrix"

The optional argument byrow=TRUE fills the matrix row by row.^1 The default is byrow=FALSE which fills the matrix column by column:

> matrix(data=c(1,2,3,4,5,6),nrow=2,ncol=3) [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6

Matrix objects have row and column dimension attributes which can be ex- amined with the dim() function:

> dim(matA) [1] 2 3

(^1) When specifying logical variables in R always spell out TRUE and FALSE instead of using T and F. Upon startup R defines the variables T=TRUE and F=FALSE so that T and F can be used as substitutes for TRUE and FALSE, respectively. However, this shortcut is not recommended because the variables T and F could be reassigned during subsequent programming.

4 CHAPTER 1 MATRIX ALGEBRA REVIEW

Example 2 Creating vectors in R

Vectors can be created in R using a variety of methods:

> xvec = c(1,2,3) > xvec [1] 1 2 3 > xvec = 1: > xvec [1] 1 2 3 > xvec = seq(from=1,to=3,by=1) > xvec [1] 1 2 3

Vectors in R are of class numeric and do not have a dimension attribute:

> class(xvec) [1] "numeric" > dim(xvec) NULL

The elements of a vector can be assigned names using the names() function:

> names(xvec) = c("x1", "x2", "x3") > xvec x1 x2 x 1 2 3

To force a dimension attribute onto a vector, coerce it to a matrix object using as.matrix():

> xvec = as.matrix(xvec) > xvec [,1] x1 1 x2 2 x3 3 > class(xvec) [1] "matrix" > dim(xvec) [1] 3 1

1.1 MATRICES AND VECTORS 5

The transpose of an  ×  matrix A is a new matrix with the rows and columns of A interchanged, and is denoted A^0 or A| For example,

A

(2×3)

⎦  A^0

(3×2)

⎤ ⎥ ⎥ ⎥ ⎦ x

(3×1)

 x^0 (1×3)

h 1 2 3

i 

A symmetric matrix A is such that A = A^0  Obviously, this can only occur if A is a square matrix; i.e., the number of rows of A is equal to the number of columns. For example, consider the 2 × 2 matrix

A =

Then,

A^0 = A =

Example 3 Transpose of a matrix in R

To take the transpose of a matrix or vector use the t() function

> matA = matrix(data=c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE) > t(matA) [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > xvec = c(1,2,3) > t(xvec) [,1] [,2] [,3] [1,] 1 2 3

1.2 BASIC MATRIX OPERATIONS 7

[2,] 0 7

> # matrix addition > matC = matA + matB > matC [,1] [,2] [1,] 6 9 [2,] 2 8 > # matrix subtraction > matC = matA - matB > matC [,1] [,2] [1,] 2 9 [2,] 2 -

¥

1.2.2 Scalar Multiplication

Here we refer to the multiplication of a matrix by a scalar number. This is also an element-by-element operation. For example, let  = 2 and

A =

Then

 · A =

Example 5 Scalar multiplication in R

> matA = matrix(c(3,-1,0,5),2,2,byrow=TRUE) > matC = 2*matA > matC [,1] [,2] [1,] 6 - [2,] 0 10

¥

8 CHAPTER 1 MATRIX ALGEBRA REVIEW

1.2.3 Matrix Multiplication

Matrix multiplication only applies to conformable matrices. A and B are conformable matrices if the number of columns in A is equal to the number of rows in B For example, if A is  ×  and B is  ×  then A and B are conformable and the matrix product of A and B has dimension  × . The mechanics of matrix multiplication is best explained by example. Let

A

(2×2)

⎦ (^) and B (2×3)

Then

A

(2×2)

· B

(2×3)

⎦ = C

(2×3)

The resulting matrix C has 2 rows and 3 columns. In general, if A is  ×  and B is  ×  then C = A · B is  × 

As another example, let

A

(2×2)

⎦ (^) and B (2×1)

10 CHAPTER 1 MATRIX ALGEBRA REVIEW

[2,] 3 4 2

> dim(matA) [1] 2 2 > dim(matB) [1] 2 3 > matC = matA%%matB > matC [,1] [,2] [,3] [1,] 7 10 5 [2,] 15 22 11 > # note: B%%A doesn’t work b/c B and A are not comformable > matB%%matA Error in matB %% matA : non-conformable arguments

The next example shows matrix multiplication in R also works on numeric vectors:

> matA = matrix(c(1,2,3,4), 2, 2, byrow=TRUE) > vecB = c(2,6) > matA%%vecB [,1] [1,] 14 [2,] 30 > vecX = c(1,2,3) > vecY = c(4,5,6) > t(vecX)%%vecY [,1] [1,] 32 > crossprod(vecX, vecY) [,1] [1,] 32

¥

1.2.4 The Identity Matrix

The identity matrix plays a similar role as the number 1  Multiplying any number by 1 gives back that number. In matrix algebra, pre or post multi- plying a matrix A by a conformable identity matrix gives back the matrix

1.2 BASIC MATRIX OPERATIONS 11

A To illustrate, let

I 2 =

denote the 2 dimensional identity matrix and let

A =

denote an arbitrary 2 × 2 matrix. Then

I 2 ·A =

⎦ = A

and

A · I 2 =

⎦ = A

Example 7 The identity matrix in R

Use the diag() function to create an identity matrix:

> matI = diag(2) > matI [,1] [,2] [1,] 1 0 [2,] 0 1 > matA = matrix(c(1,2,3,4), 2, 2, byrow=TRUE) > matI%*%matA

1.3 REPRESENTING SUMMATION USING VECTOR NOTATION 13

This sum can be conveniently represented as

x^0 x =

h  1    

i ·

= ^21 + · · · + ^2  =

X^ 

=

^2 

Last, consider the sum of cross products

X^ 

=

This sum can be compactly represented by

x^0 y =

h  1    

i ·

X^ 

=

Note that x^0 y = y^0 x

Example 8 Computing sums in R

In R, summing the elements in a vector can be done using matrix algebra.

create vector of 1’s and a vector x

> onevec = rep(1,3) > onevec [1] 1 1 1 > xvec = c(1,2,3) > xvec [1] 1 2 3

sum elements in x

> t(xvec)%*%onevec [,1] [1,] 6

The functions crossprod() and sum() are generally computationally more efficient:

14 CHAPTER 1 MATRIX ALGEBRA REVIEW

> crossprod(xvec,onevec) [,1] [1,] 6 > sum(xvec) [1] 6

Sums of squares are best computed using

> crossprod(xvec) [,1] [1,] 14 > sum(xvec^2) [1] 14

The dot-product or cross-product of two vectors can be conveniently com- puted using the crossprod() function:

> yvec = 4: > xvec [1] 1 2 3 > yvec [1] 4 5 6 > crossprod(xvec,yvec) [,1] [1,] 32 > crossprod(yvec,xvec) [,1] [1,] 32

¥

1.4 Systems of Linear Equations

Consider the system of two linear equations

16 CHAPTER 1 MATRIX ALGEBRA REVIEW

If there was a (2 × 2) matrix B with elements   such that B · A = I 2  where I 2 is the (2 × 2) identity matrix, then we could solve for the elements in z as follows. In the equation A · z = b, pre-multiply both sides by B to give

B · A · z = B · b =⇒ I · z = B · b =⇒ z = B · b

or (^) ⎡

⎣

If such a matrix B exists it is called the inverse of A and is denoted A−^1  Intuitively, the inverse matrix A−^1 plays a similar role as the inverse of a number. Suppose  is a number; e.g.,  = 2 Then we know that (^) ^1 ·  = −^1  = 1 Similarly, in matrix algebra A−^1 A = I 2 where I 2 is the identity matrix. Now, consider solving the equation  ·  = 1 By simple division we have that  = (^1)   = −^1  Similarly, in matrix algebra if we want to solve the system of linear equations Ax = b we multiply by A−^1 and get the solution x = A−^1 b Using B = A−^1  we may express the solution for z as

z = A−^1 b

As long as we can determine the elements in A−^1 then we can solve for the values of  and  in the vector z The system of linear equations has a solution as long as the two lines intersect, so we can determine the elements in A−^1 provided the two lines are not parallel. If the two lines are parallel, then one of the equations is a multiple of the other. In this case we say that A is not invertible. There are general numerical algorithms for finding the elements of A−^1 (e.g., so-called Gaussian elimination) and matrix programming languages and spreadsheets have these algorithms available. However, if A is a (2 × 2) matrix then there is a simple formula for A−^1  Let A be a (2 × 2) matrix such that

A =

1.4 SYSTEMS OF LINEAR EQUATIONS 17

Then

A−^1 =

det(A)

where det(A) =  11  22 − 21  12 denotes the determinant of A and is assumed to be not equal to zero By brute force matrix multiplication we can verify this formula:

A−^1 A =

 22  11 − 12  21  11  22 − 21  12 0 0 −  1121  2212 −+ 2111  1222

Let’s apply the above rule to find the inverse of A in our example linear system (1.1)-(1.2):

A−^1 =

1 3

1 3 2 3

− 1 3

Notice that

A−^1 A =

1 3

1 3 2 3

− 1 3

1.4 SYSTEMS OF LINEAR EQUATIONS 19

Then we solve the system z = A−^1 b:

> z = matA.inv%*%vecB > z [,1] [1,] 0. [2,] 0.

¥ In general, if we have  linear equations in  unknown variables we may write the system of equations as

 11  1 +  12  2 + · · · +  1  =  1  21  1 +  22  2 + · · · +  2  =  2 ..

. =

which we may then express in matrix form as ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣  11  12 · · ·  1   21  22 · · ·  2  .. .

or A (×)

· x (×1)

= b (×1)

The solution to the system of equations is given by

x = A−^1 b

where A−^1 A = I and I is the ( × ) identity matrix. If the number of equations is greater than two, then we generally use numerical algorithms to find the elements in A−^1 

1.4.1 Partitioned Matrices and Partitioned Inverses

To be completed

20 CHAPTER 1 MATRIX ALGEBRA REVIEW

1.5 Positive Definite Matrices

To be completed

1.6 Multivariate Probability Distributions Us-

ing Matrix Algebra

In this section, we show how matrix algebra can be used to simplify many of the messy expressions concerning expectations and covariances between multiple random variables, and we show how certain multivariate probability distributions (e.g., the multivariate normal distribution) can be expressed using matrix algebra.

1.6.1 Random Vectors

Let  1       denote  random variables for  = 1      let  = [] and ^2  = var() and let  = cov(  ) for  6 =  Define the  × 1 random vector X = ( 1      )^0. Associated with X is the  × 1 vector of expected values

μ × 1

= [X] =

[ 1 ]

[]

1.6.2 Covariance Matrix

The covariance matrix Σ summarizes the variances and covariances of the elements of the random vector X. In general, the covariance matrix of a random vector X (sometimes simply called the variance of the vector X) with mean vector μ is defined as

cov(X) = [(X − μ)(X − μ)^0 ] = Σ