





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















This chapter reviews some basic matrix algebra concepts that we will use throughout the book.
Updated: August 15, 2013.
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
(×)
where denotes the ^ row and ^ column element of A A vector is simply a matrix with 1 column. For example,
x (×1)
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
(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.
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
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,
(2×3)
(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
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
> # 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 -
¥
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
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
¥
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
(2×2)
⎦ (^) and B (2×3)
Then
(2×2)
(2×3)
(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
(2×2)
⎦ (^) and B (2×1)
> 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
¥
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
A To illustrate, let
I 2 =
denote the 2 dimensional identity matrix and let
denote an arbitrary 2 × 2 matrix. Then
and
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
This sum can be conveniently represented as
x^0 x =
h 1
i ·
=
Last, consider the sum of cross products
X^
=
This sum can be compactly represented by
x^0 y =
h 1
i ·
=
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.
> onevec = rep(1,3) > onevec [1] 1 1 1 > xvec = c(1,2,3) > xvec [1] 1 2 3
> t(xvec)%*%onevec [,1] [1,] 6
The functions crossprod() and sum() are generally computationally more efficient:
> 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
¥
Consider the system of two linear equations
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 =
Then
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:
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):
1 3
1 3 2 3
− 1 3
Notice that
1 3
1 3 2 3
− 1 3
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
To be completed
To be completed
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.
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
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 ] = Σ