Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

33
Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini

Transcript of Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Page 1: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Linear Algebra and Matrices

Methods for Dummies21st October, 2009

Elvina Chu & Flavia Mancini

Page 2: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Talk Outline

• Scalars, vectors and matrices• Vector and matrix calculations• Identity, inverse matrices &

determinants• Solving simultaneous equations• Relevance to SPM

Linear Algebra & Matrices, MfD 2009

Page 3: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Scalar

• Variable described by a single number

e.g. Intensity of each voxel in an MRI scan

Linear Algebra & Matrices, MfD 2009

Page 4: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Vector• Not a physics vector (magnitude, direction)• Column of numbers e.g. intensity of same

voxel at different time points

Linear Algebra & Matrices, MfD 2009

3

2

1

x

x

x

Page 5: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Matrices

• Rectangular display of vectors in rows and columns

• Can inform about the same vector intensity at different times or different voxels at the same time

• Vector is just a n x 1 matrix

476

145

321

A

Square (3 x 3) Rectangular (3 x 2) d i j : ith row, jth column

Defined as rows x columns (R x C)

83

72

41

C

333231

232221

131211

ddd

ddd

ddd

D

Linear Algebra & Matrices, MfD 2009

Page 6: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Matrices in Matlab

• X=matrix • ;=end of a row• :=all row or column

987

654

321

Subscripting – each element of a matrix can be addressed with a pair of numbers; row first, column second (Roman Catholic)

e.g. X(2,3) = 6

X(3, :) =

X( [2 3], 2) =

987

8

5

“Special” matrix commands:

• zeros(3,1) =

• ones(2) =

• magic(3) =

11

11

0

0

0

Linear Algebra & Matrices, MfD 2009

294

753

618

Page 7: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Design matrix

=

+

= +Y X

data

vecto

rdes

ign

mat

rixpar

amete

rs

erro

r

vecto

r

= the b

etas

(her

e : 1

to 9)

Linear Algebra & Matrices, MfD 2009

Page 8: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Transposition

2

1

1

b 211Tb 943d

column row row column

476

145

321

A

413

742

651TA

Linear Algebra & Matrices, MfD 2009

9

4

3Td

Page 9: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Matrix Calculations

Addition – Commutative: A+B=B+A– Associative: (A+B)+C=A+(B+C)

Subtraction- By adding a negative matrix

65

43

1532

0412

13

01

52

42BA

Linear Algebra & Matrices, MfD 2009

Page 10: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Scalar multiplication

• Scalar x matrix = scalar multiplication

Linear Algebra & Matrices, MfD 2009

Page 11: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Matrix Multiplication

“When A is a mxn matrix & B is a kxl matrix, AB is only possible if n=k. The result will be an mxl matrix”

A1 A2 A3

A4 A5 A6

A7 A8 A9

A10 A11 A12

m

n

x

B13 B14

B15 B16

B17 B18

l

k

Number of columns in A = Number of rows in B

= m x l matrix

Linear Algebra & Matrices, MfD 2009

Page 12: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Matrix multiplication

• Multiplication method:Sum over product of respective rows and columns

1 0

2 3

13

12

2221

1211

cc

cc

1)(31)(23)(32)(2

1)(01)(13)(02)(1

513

12

X =

=

=

Define output matrixA B

• Matlab does all this for you!

• Simply type: C = A * B

Linear Algebra & Matrices, MfD 2009

Page 13: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Matrix multiplication

• Matrix multiplication is NOT commutative

• AB≠BA• Matrix multiplication IS associative• A(BC)=(AB)C• Matrix multiplication IS distributive• A(B+C)=AB+AC• (A+B)C=AC+BC

Linear Algebra & Matrices, MfD 2009

Page 14: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Vector Products

Inner product XTY is a scalar(1xn) (nx1)

Outer product XYOuter product XYTT is a matrix is a matrix

(nx1) (1xn) (nx1) (1xn)

xyT x1

x2

x3

y1 y2 y3 x1y1 x1y2 x1y3

x2y1 x2y2 x2y3

x3y1 x3y2 x3y3

ii

iT yxyxyxyx

y

y

y

xxx

3

1332211

3

2

1

321yx

Inner product = scalar

Two vectors:

3

2

1

x

x

x

x

3

2

1

y

y

y

y

Outer product = matrix

Linear Algebra & Matrices, MfD 2009

Page 15: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Identity matrix

Is there a matrix which plays a similar role as the number 1 in number multiplication?

Consider the nxn matrix:

For any nxn matrix A, we have A In = In A = A

For any nxm matrix A, we have In A = A, and A Im = A (so 2 possible matrices)

Linear Algebra & Matrices, MfD 2009

Page 16: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Identity matrix

11 22 33 11 00 00 1+0+01+0+0 0+2+00+2+0 0+0+30+0+3

44 55 66 XX 00 11 00 == 4+0+04+0+0 0+5+00+5+0 0+0+60+0+6

77 88 99 00 00 11 7+0+07+0+0 0+8+00+8+0 0+0+90+0+9

Worked exampleA I3 = A for a 3x3 matrix:

• In Matlab: eye(r, c) produces an r x c identity matrix

Linear Algebra & Matrices, MfD 2009

Page 17: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Matrix inverse

• Definition. A matrix A is called nonsingular or invertible if there exists a matrix B such that:

• Notation. A common notation for the inverse of a matrix A is A-1. So:

• The inverse matrix is unique when it exists. So if A is invertible, then A-1 is also invertible and then (AT)-1 = (A-1)T

11 11 XX2 2 33

-1-1 33

==22 + + 11 3 33 3

-1-1 + + 11 3 33 3 == 11 00

-1-1 22 1 1 33

1 1 33

-2-2+ + 22 3 3

33

11 + + 22 3 3 3 3 00 11

• In Matlab: A-1 = inv(A) •Matrix division: A/B= A*B-1

Linear Algebra & Matrices, MfD 2009

Page 18: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Matrix inverse

• For a XxX square matrix:

• The inverse matrix is:

Linear Algebra & Matrices, MfD 2009

• E.g.: 2x2 matrix

Page 19: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Determinants

• Determinants are mathematical objects that are very useful in the analysis and solution of systems of linear equations (i.e. GLMs).

• The determinant is a function that associates a scalar det(A) to every square matrix A.

– Input is nxn matrix– Output is a single

number (real or complex) called the determinant

Linear Algebra & Matrices, MfD 2009

Page 20: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Determinants

•Determinants can only be found for square matrices.•For a 2x2 matrix A, det(A) = ad-bc. Lets have at closer look at that:

• A matrix A has an inverse matrix A-1 if and only if det(A)≠0.

• In Matlab: det(A) = det(A)

Linear Algebra & Matrices, MfD 2009

a bc d

det(A) = = ad - bc [ ]

Page 21: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Solving simultaneous equations

For one linear equation ax=b where the unknown is x and a and b are constants,

3 possibilities:

If 0a then baa

bx 1 thus there is single solution

If 0a , 0b then the equation bax becomes 00 and any value of x will do If 0a , 0b then bax becomes b0 which is a contradiction

Linear Algebra & Matrices, MfD 2009

Page 22: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

With >1 equation and >1 unknown

• Can use solution from the single equation to solve

• For example

• In matrix form

bax 1

12

532

21

21

xx

xx

A X = B

1

5

21

32

2

1

x

x

X =A-1BLinear Algebra & Matrices, MfD 2009

4

1

Page 23: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

• X =A-1B• To find A-1

• Need to find determinant of matrix A

• From earlier

(2 -2) – (3 1) = -4 – 3 = -7

• So determinant is -7

bcaddc

baA )det(

21

32

Linear Algebra & Matrices, MfD 2009

A 1 1

det(A)

d b c a

Page 24: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

21

32

7

1

21

32

)7(

11A

1

2

7

14

7

1

4

1

21

32

7

1

X

4

1if B isif B is

SoSo

bax 1

Linear Algebra & Matrices, MfD 2009

Page 25: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Linear Algebra & Matrices, MfD 2009

How are matrices relevant to fMRI data?

Page 26: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

NormalisationNormalisation

Statistical Parametric MapStatistical Parametric MapImage time-seriesImage time-series

Parameter estimatesParameter estimates

General Linear ModelGeneral Linear ModelRealignmentRealignment SmoothingSmoothing

Design matrix

AnatomicalAnatomicalreferencereference

Spatial filterSpatial filter

StatisticalStatisticalInferenceInference

RFTRFT

p <0.05p <0.05

Linear Algebra & Matrices, MfD 2009

Page 27: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Linear Algebra & Matrices, MfD 2009

Time

BOLD signal

Tim

e

single voxeltime series

single voxeltime series

Voxel-wise time series analysis

Modelspecificati

on

Modelspecificati

onParameterestimationParameterestimation

HypothesisHypothesis

StatisticStatistic

SPMSPM

Page 28: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

How are matrices relevant to fMRI data?

=

+

= +Y X

data

vecto

rdes

ign

mat

rixpar

amete

rs

erro

r

vecto

r

Linear Algebra & Matrices, MfD 2009

GLM equation

Page 29: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

How are matrices relevant to fMRI data?

Y

data

vecto

r

Response variable

e.g BOLD signal at a particular voxel

A single voxel sampled at successive time points. Each voxel is considered as independent observation.

Preprocessing ...

Intensity

Ti

me

YT

ime

Y = X . β + εLinear Algebra & Matrices, MfD 2009

Page 30: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

How are matrices relevant to fMRI data?

X

design

mat

rixpar

amete

rs

Explanatory variables

– These are assumed to be measured without error.

– May be continuous;– May be dummy,

indicating levels of an experimental factor.

Y = X . β + ε

Solve equation for β – tells us how much of the BOLD signal is explained by X

Linear Algebra & Matrices, MfD 2009

Page 31: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

In Practice

• Estimate MAGNITUDE of signal changes

• MR INTENSITY levels for each voxel at various time points

• Relationship between experiment and voxel changes are established

• Calculation and notation require linear algebra

Linear Algebra & Matrices, MfD 2009

Page 32: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

Summary

• SPM builds up data as a matrix.• Manipulation of matrices enables

unknown values to be calculated.

Y = X . β + εObserved = Predictors * Parameters +

ErrorBOLD = Design Matrix * Betas + Error

Linear Algebra & Matrices, MfD 2009

Page 33: Linear Algebra and Matrices Methods for Dummies 21 st October, 2009 Elvina Chu & Flavia Mancini.

References

• SPM course http://www.fil.ion.ucl.ac.uk/spm/course/• Web Guideshttp://mathworld.wolfram.com/LinearAlgebra.htmlhttp://www.maths.surrey.ac.uk/explore/emmaspages/

option1.htmlhttp://www.inf.ed.ac.uk/teaching/courses/fmcs1/(Formal Modelling in Cognitive Science course)• http://www.wikipedia.org• Previous MfD slides

Linear Algebra & Matrices, MfD 2009