linear algebra, matrices (and why they matter to (f)mri!) methods for dummies fil october 2008 nick...

25
Linear Algebra, Linear Algebra, Matrices (and why they Matrices (and why they matter to (f)MRI!) matter to (f)MRI!) Methods for Dummies Methods for Dummies FIL FIL October 2008 October 2008 Nick Henriquez & Nick Wright Nick Henriquez & Nick Wright Theory & Application Theory & Application

Upload: cecil-ralph-simmons

Post on 23-Dec-2015

226 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Linear Algebra, Matrices (and Linear Algebra, Matrices (and why they matter to (f)MRI!)why they matter to (f)MRI!)

Methods for DummiesMethods for DummiesFILFIL

October 2008October 2008

Nick Henriquez & Nick WrightNick Henriquez & Nick Wright

Theory & ApplicationTheory & Application

Page 2: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Sources and further information Previous FIL slides! YES we copied some.... SPM course

http://www.fil.ion.ucl.ac.uk/spm/course/ Web Guides

– http://linear.ups.edu/download.html– http://joshua.smcvt.edu/linalg.html/

(Formal Modelling in Cognitive Science course)– http://www.wikipedia.org

Page 3: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

fMRI and Linear Algebra(fMRI) measures signal changes in the brain that are due to changing neural activity.

Increases in neural activity cause changes in the MR signal via T2* changes; this mechanism is referred to as the BOLD (blood-oxygen-level dependent) effect.

To estimate the MAGNITUDE of signal changes we need to measureMR INTENSITY levels for each “volumetric pixel”=VOXEL at various TIME POINTS.

In SPM each VOXEL is observed/considered independently over time

Any relationship between EXPERIMENT and VOXEL CHANGE is established using standard statistics

Calculation and notation require Linear Algebra and MATRICES

Page 4: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Defining scalars, vectors and matrices Scalar: Variable described by a single

number – e.g. Image intensity (pixel value)

Vector: Variable described by magnitude and direction – e.g. pixel value+(relative) location

Matrix: Rectangular array of vectors defined by number of rows and columns – e.g. Intensities of several voxels or same voxel at different times

Square (3 x 3) Rectangular (3 x 2) d r c : rth row, cth column

3

2

(Roman Catholic)

VE

VN

Volumetric pixel (VOXEL) intensity is expressed as a VECTOR. The size (=> MAGNITUDE) is determined by its direction.V=

VN

VE

Page 5: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Matrices in Matlab Vector formation: [1 2 3] Matrix formation:

X = [1 2 3; 4 5 6; 7 8 9] =

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) =

‘;’ is used to signal end of a row

‘:’ is used to signify all rows or columns

987

8

5

“Special” matrix commands:

• zeros(3,1) =

• ones(2) =

• magic(3)

more to come…

11

11

0

0

0

Page 6: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Addition (matrix of same size)– Commutative: A+B=B+A– Associative: (A+B)+C=A+(B+C)

Subtraction (consider as the addition of a negative matrix)

Matrix addition

Page 7: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Matrix multiplication

aa1111 aa1212 aa1313 bb1111 bb1212

aa2121 aa2222 aa2323 XX bb2121 bb2222

aa3131 aa3232 aa3333 bb3131 bb3232

aa4141 aa4242 aa4343

• Scalar multiplication:

• Multiplication of vectors/matrices:

bb1111 bb1212 aa1111 aa1212 aa1313

bb2121 bb2222 xx aa2121 aa2222 aa2323

bb3131 bb3232 aa3131 aa3232 aa3333

aa4141 aa4242 aa4343

Matrix multiplication rule:

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

n l

mk

Page 8: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

• Sum over product of respective rows and columns

• For larger matrices, following method might be helpful:

Multiplication method

32

01

13

12

2221

1211

cc

cc

m

l

m

l

3294

0102

513

12

X =

=

=

Define output matrix

Sum over crc

• Matlab does all this for you!

• Simply type: C = A * B

• N.B. If you want to do element-wise multiplication, use: A .* B

r c

Page 9: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Defining the identity matrix Is there a matrix which plays a similar role as

the number 1 in number multiplication? Consider the nxn matrix:

A square nxn matrix A has one A In = In A = A

An nxm matrix A has two!! In A = A & A Im = A

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 In = A

for a 3x3 matrix:

100

010

001

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

Page 10: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Inverse matrices Definition. A matrix A is nonsingular or invertible if there exists a

matrix B such that: worked example:

Common notation for the inverse of a matrix A is A-1

If A is an invertible matrix, then (AT)-1 = (A-1)T

The inverse matrix A-1 is unique when it exists. If A is invertible, A-1 is also invertible A is the inverse matrix of A-1.

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 = AB-1

Page 11: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Determinants

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

A matrix A has an inverse matrix A-1 if and only if det(A)≠0 (see next slide)

http://people.richland.edu/james/lecture/m116/matrices/determinant.html

The determinant of a 2×2 matrix is the product of the elements on the main diagonal minus the product of the elements off the main diagonal.

Page 12: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Calculation of inversion using determinants

ax1 = 1-cx2 => x1 = (1-cx2)

Etc.

thus Note: det(A)≠0

Or you can just typeinv(A)!

bx1 +dx2 =0 => b(1-cx2)/a = -dx2

http://people.richland.edu/james/lecture/m116/matrices/determinant.html

Page 13: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Transposition

column → row row → column

Mrc = Mcr

• In Matlab: AT = A’

T

Page 14: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Outer product = matrix

Inner product = scalar

Two vectors:

Outer and inner products of vectors

(1xn)(nx1) (1X1)

(nx1)(1xn) (nXn)

Page 15: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Applications

Page 16: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Recap!

Why matrices? Lots of data and calculation Why algebra? Allows you to find unknowns.

To do matrix algebra you need to use the matrix manipulations you’ve just learnt, e.g.– Addition and subtraction– Multiplication e.g. Inner / outer– “Division” e.g. Inverse– “Powers” e.g. Transpose

Page 17: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Scalar and vector algebra Algebra with scalars (“Normal school algebra”):

e.g. y = x

– Solve for : = y / x or = y x-1

But our experiments get more data, which we want to represent vectors or matrices. We still want to do algebra with matrices.

e.g. Y = X

– Solve for : = X-1 Y

Examples of using matrix algebra to solve equations:– Simultaneous equations with 2 unknowns and 2 equations– Many equations and unknowns – real world GLM and fMRI

Page 18: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Example: Using matrices to solve simultaneous equations

A pair of simultaneous equations:

In matrix form

We want to rearrange to find the unknowns

0.4p + 0.2q = 40.6p + 0.8q = 11

11

4

8.06.0

2.04.0

q

p

Page 19: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Need to find the inverse of A

Multiply both sides by A-1 of our first matrix by A-1

Get the answer!

A =

8.06.0

2.04.0

Adet

1

4.06.0

2.08.0

23

14A-1 = =

q

p

11

4A-1 A = A-1

q

p

23

14

11

4

10

5 =

= So…p = 5 and q = 10

Page 20: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Analysing data from one voxel

Getting the data as a vector of intensities

Intensity at time 1

Intensity at time 2

Intensity at time 3

Time

Page 21: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

++

== ++YY XX

data v

ecto

r (Vox

el)

data v

ecto

r (Vox

el)

design

mat

rix

design

mat

rix

param

eters

param

eters

erro

r vec

tor

erro

r vec

tor

××

==

One voxel: The GLM

Our aim: Solve equation for β – tells us how much BOLD signal is explained by X

Page 22: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Some matrix algebra

Initially can think of this as a system of simultaneous equations.

But, there are more equations (y = x ) than unknowns ()

So … clever maths e.g.Y = X XT Y = XT X (XT X)-1 XT Y =

= (XT X)-1 XT Y^

Page 23: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

++

== ++YY XX

data v

ecto

r (Vox

el)

data v

ecto

r (Vox

el)

design

mat

rix

design

mat

rix

param

eters

param

eters

erro

r vec

tor

erro

r vec

tor

××

==

One voxel: The GLM

Our aim: Solve equation for β – tells us how much BOLD signal is explained by X

Page 24: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Take home messages

1. Matrices are at the core of SPM – it is how the data and design “matrix” are built and manipulated.

2. You need to be able to manipulate matrices to do matrix algebra and find unknowns – the basic results for the experiment!

3. Simple example: use matrix algebra to solve 2 simultaneous equations with 2 unknowns.

4. Use matrix algebra to solve bigger problems, e.g. the GLM.

Page 25: Linear Algebra, Matrices (and why they matter to (f)MRI!) Methods for Dummies FIL October 2008 Nick Henriquez & Nick Wright Theory & Application Theory

Thanks Justin.

The End…