Basic matrix algebra Raphael Mrode Training in quantitative genetics and genomics 3 May June 26 ILRI, Nairobi Partner Logo Partner Logo
Matrix definition A matrix is a rectangular array of numbers set in rows and columns. These elements are called the elements of a matrix: Matrix B with 2 rows and 3 columns or B 2x3 or in general B nxm 2 b b b 2 3 B= B = b2 b22 b23 6 8 9 4 5
Matrix definition A matrix is a simple way to order information Pen size Breed A Breed B Breed C Pen 4. 8. 6. Pen 2 5. 9. 8. Pen 3 35.. 2.
Matrix definition A matrix consisting of a single row of elements is called a row vector. y = 2 6-4
Matrix definition A matrix consisting of a single column is called a column vector. - 2 6 d = 8 2 A scalar is matrix with one row and one column.
6 Special matrices Square : equal number of rows and columns Diagonal: A square matrix having zero for all of its offdiagonal elements is referred to as a diagonal matrix (B). When all the diagonal elements of a diagonal matrix are one, it is referred to as an identity matrix (I). In animal breeding, relationship matrix for a group of animals which are unrelated is an identity matrix 8 4 3 B = I =
7 Special matrices Triangular matrices A square matrix with all elements above the diagonal being zero is called a lower triangular matrix (D). In animal breeding, we come across this sort of matrices when we trace a flow of genes from parents to progeny when parents precede progeny in the pedigree. When all the elements below the diagonal that are zeros, it is referred to as an upper triangular matrix (E). Note: D = transpose of E and vice versa 6 8 4 9 3 E 9 7-2 3 4 D = ; =
Symmetric matrix A symmetric matrix is a square matrix with the elements above the diagonal equal to the corresponding elements below the diagonal, that is, element ij is equal to element ji. An example in animal breeding is the relationship matrix that describes the relatedness among animals 2-4 A = - 4 6 3 3 7 8
Basic matrix operations Transpose The transpose of a matrix A usually written as A ' or A T, is the matrix whose ji elements are the ij elements of the original matrix, that is a' ji = a ij. Therefore the transpose of a (m x n) matrix A is the (n x m) matrix where the rows and columns have been swapped. 9
Basic matrix operations Matrix addition and subtraction. Two matrices can be added together only if they have the same number of rows and columns, that is, they are of the same order and they are said to be conformable for addition. Given that W is the sum of the matrices X and Y, then w ij = x ij + y ij. 4 X = 39 ; - 25-2 2 Y= 4 4 4 ( 2) + 2 38 3 W = = 39 + 4-25 + 4 43 5
Basic matrix operations Matrix multiplication Two matrices can be multiplied only if the number of columns in the first matrix equals the number of rows in the second. The order of the product matrix is equal to the number of rows of the first matrix by the number of columns in the second. Given that C = AB, then C c ij m n z ji k a ik b kj where m = number of columns in B, n = number of rows in A and z = number of rows in B.
Basic matrix operations Direct product of matrices Given a matrix G of order 2 by 2 and A of order t by s, the direct product is g G A= g 2 A A g g 2 22 A A G = 5 5 and A= 2 2 4 2 4 2 G A= 5 4 5 2 2 4 2 5 5 2 2 5 2 2 8 2 5 4 8 2 Also known as the Kronecker product. It is useful when you carry out multi-trait analysis 2
Basic matrix operations Matrix inversion An inverse matrix is one which when multiplied by the original matrix gives an identity matrix. The inverse of a matrix A is usually denoted as A -, therefore A - A = I, Only square matrices can be inverted diagonal matrix the inverse is calculated simply as the reciprocal of the diagonal elements. For a 2x2 matrix the inverse is easy to calculate A = a a 2 a a 2 22 a - a - 22 2 A = aa22 - a2a2 - a2 a If (a a 22 a 2 a 2 ) is zero, then an inverse does not exist and matrix is singular 3
Basic matrix operations TRANSPOSES AND INVERSES OF PRODUCTS If we have a product AB, then (AB) T = B T A T If we have a product AB then, assuming the inverses exist, (AB) - = B - A -. 4
Some examples Assume we have 3 animals each with 2 records for milk yield We want to calculate total litres of milk for each animal using matrix algebra Animal Milk yield 2.5 25. 2 3.5 2 5. 3. 3 6. 5
6 Some examples Set up an incidence matrix for the animals X= Set a column vector containing the yield of each animal Finally compute total as follows 6.. 5. 3.5 25. 2.5 y 26. 28.5 37.5 6.. 5. 3.5 25. 2.5 X'Y
7 Some examples Simple simultaneous equations (Eg. Selection index). We want solve for x, y and z 4x + 2y + z = 8 x + 2y z = 2 x y + 2z = 5 Then this set of equations can be described as Cb = y, where C is the (3 x 3) matrix, b =(3x) and y = (3,) vectors C = ; b = ; y = 2 2 4 z y x 5 2 8
Some examples We compute that the inverse of C. Then we solve: Cb = y to get b b = C - y =.5.5.5 8.5.5.67.8332 2.5.5.833.675 3.5 8
Suppose we do a -way ANOVA with fixed effects, on 5 data points with a factor of 3 treatment levels. Data point 2 3 4 5 Factor level (herds) 2 2 3 Observation (y vector) 3 4 2 6 Model: y ij = m + a i + e ij for i =,,5. To fit the model choose a constraint among the treatment levels to allow us to fit the mean, so we shall assume the first treatment level is. We can then write the model as y = Xb + e 9
Some examples Here y is the (5 x ) column vector of data points y T = (3, 4, 2,, 6) e is the (5 x ) column vector of residuals, b is the (3 x ) vector of parameters that need to be estimated; b T = (µ,a 2,a 3 ) X is called the design matrix which relates the model parameters to the data points and has dimension (5,3): 2
Some examples Solve for b as b = (X X) - X y 5 b 2 2 2 65.5.5 26.5..5.5.565.5 26.5 4.5.5 4.5 2
better lives through livestock ilri.org The presentation has a Creative Commons licence. You are free to re-use or distribute this work, provided credit is given to ILRI.
CRP and CG logos