Transcript Matrix

Linear Algebra, Matrices (and
why they matter to (f)MRI!)
Methods for Dummies
FIL
October 2008
Nick Henriquez & Nick Wright
Theory & Application
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
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 measure
MR 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
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
V=
VN
VE
VN
Volumetric pixel (VOXEL) intensity is
expressed as a VECTOR. The size (=>
MAGNITUDE) is determined by its direction.
VE
 Matrix: Rectangular array of vectors defined by number of rows and
columns – e.g. Intensities of several voxels or same voxel at different
times
2
(Roman Catholic)
3
Square (3 x 3)
Rectangular (3 x 2)
d r c : rth row, cth column
Matrices in Matlab
 Vector formation:
 Matrix formation:
X = [1 2 3; 4 5 6; 7 8 9]
=
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, :) =
7
8
‘;’ is used to signal
end of a row
[1 2 3]
9
5
X( [2 3], 2) =  
8
 
1

4
7

2
5
8
3

6
9 
‘:’ is used to
signify all rows or
columns
“Special” matrix commands:
• zeros(3,1) =
• ones(2) =
• magic(3)
more to come…
0
 
0
0
 
1

1
1

1
Matrix addition
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 multiplication
• Scalar multiplication:
n
• Multiplication of
vectors/matrices:
m
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”
l
a11
a12
a13
a21
a22
a23
a31
a32
a33
a41
a42
a43
b11
b12
b21
b22
b31
b32
x
b11
b12
b21
b22
b31
b32
a11
a12
a13
a21
a22
a23
a31
a32
a33
a41
a42
a43
X
k
Multiplication methodl
l
• Sum over
product of
respective rows
and columns
• For larger
matrices, following
method might be
helpful:
m
m
0
2
 X 
3
3
1

2
r
1

1
 c11

 c 21
=
c
=
• Matlab does all this for you!
• Simply type: C = A * B
• N.B. If you want to do element-wise
multiplication, use: A .* B
=
2  0

49
2

13
c12 

c 22 
Define
output
matrix
1  0  Sum
 over
2  3  crc
1

5
Defining the identity matrix
 Is there a matrix which plays a similar role as
the number 1 in number multiplication?
Consider the nxn matrix:
1

0
0

0
1
0
Worked example
A In = A
for a 3x3 matrix:
A square nxn matrix A has one
A I n = In A = A
0

0
1 
An nxm matrix A has two!!
In A = A & A I m = A
1
2
3
4
5
6
7
8
9
X
1
0
0
0
1
0
0
0
1
=
1+0+0
0+2+0
0+0+3
4+0+0
0+5+0
0+0+6
7+0+0
0+8+0
0+0+9
• In Matlab: eye(r, c) produces
an r x c identity matrix
Inverse matrices
 Definition. A matrix A is nonsingular or invertible if there exists a
matrix B such that:
worked example:
1
1
-1
2
X
2
3
-1
3
1
3
1
3
=
2+1
3 3
-1 + 1
3 3
-2+ 2
3 3
1+2
3 3
=
1
0
0
1
 Common notation for the inverse of a matrix A is A-1
 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.
• Matrix division:
 If A is an invertible matrix, then (AT)-1 = (A-1)T
• In Matlab: A-1 = inv(A)
A/B = AB-1
Determinants
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.
 A matrix A has an inverse matrix A-1 if and only if det(A)≠0 (see next slide)
• In Matlab: det(A) = det(A)
Calculation of inversion using
determinants
ax1 = 1-cx2 => x1 = (1-cx2)
bx1 +dx2 =0 => b(1-cx2)/a = -dx2
Or you can just type
inv(A)!
Etc.
thus
Note: det(A)≠0
http://people.richland.edu/james/lecture/m116/matrices/determinant.html
Transposition
column
→
row
row
→
column
T
Mrc = Mcr
• In Matlab: AT = A’
Outer and inner products of
vectors
Two vectors:
Inner product = scalar
Outer product = matrix
(1xn)(nx1)  (1X1)
(nx1)(1xn)  (nXn)
Applications
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
“Division”
“Powers”
e.g. Inner / outer
e.g. Inverse
e.g. Transpose
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
Example: Using matrices to solve
simultaneous equations
 A pair of simultaneous equations:
0.4p + 0.2q = 4
0.6p + 0.8q = 11
 In matrix form
 0.4 0.2  p   4 

    
 0.6 0.8  q  11
 We want to rearrange to find the unknowns
 Need to find the inverse of A
A =  0 .4 0 .2 


 0 .6 0 .8 
1
det A
A-1 =
 0.8  0.2 


  0.6 0.4 
=
 4  1


 3 2 
 Multiply both sides by A-1 of our first matrix by A-1
A-1 A
 p  = A-1
 
q
4
 
11
 Get the answer!
 p =
 
q
 4  1  4  =

  
  3 2  11
5
 
10 
So…
p = 5 and q = 10
Analysing data from one voxel
 Getting the data as a vector of intensities
Intensity at time 1
Time
Intensity at time 2
Intensity at time 3
One voxel: The GLM
Our aim: Solve equation for β – tells us how much BOLD signal is explained by X
a
m
3
4
5
=
6
+
7
8
9
Y
=
X
×

+
e
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
One voxel: The GLM
Our aim: Solve equation for β – tells us how much BOLD signal is explained by X
a
m
3
4
5
=
6
+
7
8
9
Y
=
X
×

+
e
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.
Thanks Justin.
The End…