Principle Component Analysis

Principle Component
Analysis
Royi Itzhack
Algorithms in computational
biology
‫אלגברה ליניארית ‪matlab -‬‬
‫הגדרת מטריצה רב מימדית על ידי סוגריים מרובעים ‪ ,‬גישה לאיברים על ידי סוגריים‬
‫עגולים ‪.‬‬
‫חיבור איבר‬
‫איבר‬
‫הכפלה ע"פ כללי‬
‫מטריצות‬
‫פעולות של‬
‫סקלרים הם על‬
‫כל המטריצה‬
‫בסיס‬
‫קבוצה של וקטורים נקראת בסיס אם היא פורשת את כל המרחב והיא בלתי‬
. ‫תלויה ליניארית‬
‫אי תלות לינארית ניתנת לבדיקה‬
0 ‫אם הדטרמיננטה של המטריצה המייצגת את כל וקטורי הבסיס שונה מ‬
.‫או מימד שורות המטריצה נשאר כמימד הוקטורים לאחר דירוג גאוס‬
1
B1  0
0
2
B2   4
1
1
NB  0
0
0 0
1 
0 
0   a 





3
1 0  v  R a 0   b 1   c 0    b 
0 
0 
1   c 
0 1 
0 1
2
0 
1   2a  c 
3 0  v  R 3 a  4   b  3  c  0    4a  3b 
1 
0 
 4   a  4c 
0 4 
2 0
1 
 2
0   a  2b 
0 0  v  R 3 a 0   b  0   c 0    0 
0 1 
0 
 0 
1   c 
‫בסיס אורתוגונאלי‬
‫כל הוקטורים השונים המרכיבים את הבסיס ניצבים אחד לשני – המכפלה‬
‫הפנימית שלהם שווה ל ‪0‬‬
‫‪i  j  vi  v j   0‬‬
‫‪ 0  1 ‬‬
‫‪0 0‬‬
‫‪1   0 ‬‬
‫‪ 1  0   0  0 1    0  0 0   0‬‬
‫‪0  0‬‬
‫‪1  0‬‬
‫‪0 1 ‬‬
‫‪1 0 0 ‬‬
‫‪B1  0 1 0 ‬‬
‫‪0 0 1 ‬‬
‫נרמול של וקטור – חלוקה של איברי הוקטור בנורמה ‪ 2‬של הוקטור כך שהכפלה‬
‫הפנימית של הוקטור בעצמו תהיה שווה ל ‪. 1‬‬
‫‪ 2 / 17 ‬‬
‫‪‬‬
‫‪‬‬
‫‪norm _ v   2 / 17 ‬‬
‫‪‬‬
‫‪‬‬
‫‪ 3 / 17 ‬‬
‫‪ 2‬‬
‫‪ 2   22  22  32  17‬‬
‫‪ ‬‬
‫‪ 3 ‬‬
‫‪2‬‬
‫‪4 4 4‬‬
‫‪ ‬‬
‫‪17 17 17‬‬
‫‪2‬‬
‫‪v   2 ‬‬
‫‪ 3 ‬‬
‫‪ norm _ v, norm _ v) ‬‬
‫הגדרת נורמה –‬
‫)‪norm(vector,k norm‬‬
‫נרמול וקטור – חלוקה איבר איבר‬
‫בסקלר (אופרטור ”‪) “.‬‬
‫מכפלה סקלרית‬
Matrix arithmetic, etc.

Product
A*B
If either factor is 1X1, i.e., a scalar, then this is
scalar multiplication.

Transpose
A’
Conjugate-transpose for complex matrix
A^(-1) or inv(A)

Inverse

Determinant det(A)
There is also a pseudoinverse, pinv, for
nonsquare matrices.
‫משוואה אופינית‬
‫‪ X‬מציין וקטור עצמי‬
‫‪ ‬מציינת ערך עצמי תואם לוקטור‬
‫‪ A  x  x‬‬
‫‪ A  x    I  x  x[A  I]  0‬‬
‫‪A  I  0 or | I  A]  0‬‬
:‫דוגמא‬
x1  2 x2  x1
2 x1  x2  x2
1 2  x1   0   x1 

 
 


2 1  x2   0    x2 
1 2  x1   0   x1  0
2 1  x    0    x   0

 2  
 2   
‫המשך‬
‫‪2   x1  0‬‬
‫‪1  ‬‬
‫‪ 2 1     x   0‬‬
‫‪‬‬
‫‪ 2   ‬‬
‫הדטרמיננטה של המטריצה צריכה להיות שווה לאפס‬
‫‪0‬‬
‫‪2‬‬
‫‪1 ‬‬
‫‪1 ‬‬
‫‪2‬‬
‫חישוב משוואה אופינית על פי‬
‫הדטרמיננטה‬
‫‪ 1     4  0‬‬
‫‪2‬‬
‫‪1 ‬‬
‫‪1 ‬‬
‫‪2‬‬
‫‪2‬‬
‫‪1  2    4  0‬‬
‫‪2‬‬
‫‪‬‬
‫‪‬‬
‫‪3‬‬
‫‪  2  3  0‬‬
‫‪  1‬‬
‫‪2‬‬
‫חישוב הוקטורים העצמיים‬
‫על מנת למצוא את הוקטור המתאים ל ‪ = 3‬מציבים במטריצה האופינית את‬
‫הערך העצמי ובודקים איך יראה הוקטור שמכפלתו במטריצה תהיה שווה לאפס‬
‫‪2   x1  0‬‬
‫‪1  ‬‬
‫‪ 2 2   x1  0‬‬
‫‪ 2 1     x   0   2  2  x   0‬‬
‫‪‬‬
‫‪ 2   ‬‬
‫‪‬‬
‫‪ 2   ‬‬
‫כאשר המטריצה לא רגולרית יש אין סוף אפשרויות וצריך לבחור את אחד‬
‫הבסיסים למרחב הפתרונות‬
‫המשך‪...‬‬
‫אז נציב ‪ t=1‬ונמצא את‬
‫‪ 2 2   1  0 ‬‬
‫‪ 1‬‬
‫‪ 2 2 x   0  x2  t x 1  t x  t  1 ‬‬
‫‪‬‬
‫‪ 2  ‬‬
‫‪ ‬‬
‫ולכן כל וקטור מהצורה שבו שני הרכיבים זהים‬
‫יהווה וקטור עצמי לערך עצמי ‪3‬‬
...‫המשך‬
-1
‫אותו תהליך מתבצע לערך עצמי השני‬
2 2  x 1  0 
 1 
 2 2   x   0  x  t  1 

 2  
 
The dimension problem



Suppose , we want to calculate the
probability to have a hard disease base on N
parameters : age , height , weight ,blood
pressure , country , historical treatments
,genetics ext..
We calculate for each sample M feature ,if we
have N samples we can describe it as MxN
matrix
probably that only few number of features are
important - how can we find them?
The dimension problem



Some features are not informative
Constant feature – the variance of the vector is zero
or close to zero , lets say that in our experiments ,
we check the birth country of the samples ,and 98%
of them was born in Israel while 2% was born in
other country
Feature that are linearly dependent on other
features like blood pressure and weight
Informative features - high variance between
groups and low variance in the group
Algebraic Interpretation – 1D

Given m points in a n dimensional space, for large n, how does
one project on to a 1 dimensional space?

Choose a line that fits the data so the points are spread out well
along the line
Algebraic Interpretation – 1D



Given m points in a n dimensional space, for large n, how does one project on
to a low dimensional space while preserving broad trends in the data and
allowing it to be visualized?
Formally, minimize sum of squares of distances to the line.
Why sum of squares? Because it allows fast minimization, assuming the line
passes through 0
Principal Components
30


25
Wavelength 2
All principal
components (PCs) start
at the origin of the
ordinate axes.
First PC is direction of
maximum variance
from origin
Subsequent PCs are
orthogonal to 1st PC
and describe maximum
residual variance
20
15
PC 1
10
5
0 0
5
10
15
20
Wavelength 1
25
30
10
25
30
30
25
Wavelength 2

20
15
PC 2
10
5
0 0
5
15
20
Wavelength 1
‫שונות ‪ ,‬תלות‬
‫‪ ‬שונות – היא מדד למידת הפיזור סביב הממוצע‬
‫)‪P( A  B‬‬
‫‪P( A | B) ‬‬
‫‪ ‬נוסחה להסתברות מותנית‬
‫)‪P( B‬‬
‫‪ ‬תלות זוג משתנים ‪ B,A‬נקראים בלתי תלויים אם )‪P( A | B)  P( A‬‬
‫או )‪( P( A  B)  P( A)  P( B‬באופן טרויאלי מהמשוואה‪)...‬‬
‫לדוגמא‪ :‬מה הסיכוי שסכום זריקת ‪ 2‬קוביות הוא ‪ 6‬בהנתן שבזריקה‬
‫הראשונה התקבלה התוצאה ‪ , 4‬האם המאורעות תלויים?‬
‫נסמן ב ‪ - A‬סכום ‪ 2‬התוצאות הוא ‪6‬‬
‫נסמן ב ‪ – B‬התוצאה בהטלה הראשונה היא ‪4‬‬
‫‪1‬‬
‫)‪ (4, 2‬‬
‫‪36‬‬
‫‪P( A  B) ‬‬
‫‪1‬‬
‫‪ (4,1), (4, 2),..., (4, 6) ,‬‬
‫‪6‬‬
‫‪5‬‬
‫‪P ( A) ‬‬
‫)‪ (1,5), (5,1), (2, 4), (4, 2), (3,3‬‬
‫‪36‬‬
‫‪1/ 36 1 5‬‬
‫‪P( A | B) ‬‬
‫‪ ‬‬
‫‪1/ 6 6 36‬‬
‫‪P( B) ‬‬
‫המאורעות תלויים!‬
‫התפלגות משותפת ‪ ,‬שונות משותפת‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫כאשר שני משתנים ‪ x,y‬מעורבים יש לחשב טבלה שבה‬
‫כל זוג ערכים (אחד מ ‪ x‬ואחד מ ‪ ) y‬יש הסתברות – ) ‪P ( xi  y j‬‬
‫סכום ערכי הטבלה הינו ‪1‬‬
‫סכום שורה ועמודה מיצגות את ההתפלגות השולית‬
‫שונות משותפת – היא מדד לתיאום בין משתנים (כמה‬
‫הנטייה שלהם להשתנות ביחד )‬
‫)‪cov( x, y)  E( x  E( x)  y  E( y) ‬‬
‫‪‬‬
‫אי תלות => אי תיאום‬
‫‪Covariance matrix‬‬
‫) ‪cov( x, y )  E ( xy )  E ( x) E ( y‬‬
‫‪ .1‬מרכיבים באקראי מס' דו סיפרתי מהספרות ‪.1,2,3,4‬‬
‫יהי ‪ X‬מס' הספרות השונות המופיעות במס' ו ‪ Y‬מס' הפעמים שהספרה ‪ 1‬מופיעה‪.‬‬
‫מצא‪:‬‬
‫א‪.‬ההתפלגות המשותפת של הזוג )‪(X,Y‬‬
‫ב‪ .‬האם ‪ X‬ו ‪ Y‬בת"ל‬
‫ג‪ .‬מצא את השונות המשותפת – )‪.COV(X,Y‬‬
‫)‪P(Y‬‬
‫‪2‬‬
‫‪1‬‬
‫‪Y\X‬‬
‫‪9/16‬‬
‫‪6/16‬‬
‫‪3/16‬‬
‫‪0‬‬
‫‪6/16‬‬
‫‪6/16‬‬
‫‪0‬‬
‫‪1‬‬
‫‪1/16‬‬
‫‪0‬‬
‫‪1/16‬‬
‫‪2‬‬
‫‪1‬‬
‫‪12/16‬‬
‫‪4/16‬‬
‫)‪P(X‬‬
‫)‪P(X|Y)!=P(X‬‬
‫‪E(x)=0.25*1+0.75*2=1.75‬‬
‫‪E(y)=0*9/16+1*6/16+2*1/16=0.5‬‬
‫‪Cov(x,y)=1*2*6/16+1*2*1/16‬‬‫‪1.75*0.5=0‬‬
Covariance Matrix





Each i,j is the cov(xi, xj)
Each i,i is the var(xi)
In the previous
question
V(X)=1*0.25+4*0.751.75*1.75=
V(Y)=1*6/16+4*1/160.5*0.5
0 
 0.1875


0.375 
 0
The Algorithm

Step 1: Calculate the Covariance Matrix of the
observation matrix.

Step 2: Calculate the eigenvalues and the
corresponding eigenvectors.

Step 3: Sort eigenvectors by the magnitude of their
eigenvalues.

Step 4: Project the data points on those vectors.
PCA – Step 1: Covariance Matrix C
1
C
N

X-
N
T
(
X

X
)
(
X

X
)
 n
n
n 1
Data Matrix
 Var ( X 1 )

.

C( X )  
.

 Cov( X , X )
n
1

. . Cov( X 1 , X n ) 

. .
.


. .
.

. .
Var ( X n ) 
The Algorithm

Step 1: Calculate the Covariance Matrix of the
observation matrix.

Step 2: Calculate the eigenvalues and the
corresponding eigenvectors.

Step 3: Sort eigenvectors by the magnitude of their
eigenvalues.

Step 4: Project the data points on those vectors.
Linear Algebra Review – Eigenvalue and
Eigenvector
Cv  v
eigenvector

C - a square nn matrix
eigenvalue
Cv  v
Cv  Iv  0
(C   I ) v  0
C  I  0
PCA – Step 3

Sort eigenvectors by the magnitude
of their eigenvalues
The Algorithm

Step 1: Calculate the Covariance Matrix of the
observation matrix.

Step 2: Calculate the eigenvalues and the
corresponding eigenvectors.

Step 3: Sort eigenvectors by the magnitude of their
eigenvalues.

Step 4: Project the data points on those vectors.
PCA – Step 4
Sc  X V

Project the input data onto the
principal components.

The new data values are generated for each
observation, which are a linear combination as
follows:
Sci , pc  b pc,i  X i ,1  b pc, 2  X i , 2  ..  b pc,k  X i ,k

Sc

i

pc

b

k
score
observation
principal component
loading (-1 to 1)
variable
PCA: General
From k original variables: x1,x2,...,xk:
Produce k new variables: y1,y2,...,yk:
y1 = a11x1 + a12x2 + ... + a1kxk
y2 = a21x1 + a22x2 + ... + a2kxk
...
yk = ak1x1 + ak2x2 + ... + akkxk
PCA: General
From k original variables: x1,x2,...,xk:
Produce k new variables: y1,y2,...,yk:
y1 = a11x1 + a12x2 + ... + a1kxk
y2 = a21x1 + a22x2 + ... + a2kxk
...
yk = ak1x1 + ak2x2 + ... + akkxk
such that:
yk's are uncorrelated (orthogonal)
y1 explains as much as possible of original variance in data set
y2 explains as much as possible of remaining variance
etc.
5
2nd Principal
Component, y2
1st Principal
Component, y1
4
3
2
4.0
4.5
5.0
5.5
6.0
PCA Scores
5
xi2
yi,1
4
yi,2
3
2
4.0
4.5
5.0
xi1
5.5
6.0
PCA Eigenvalues
5
λ2
λ1
4
3
2
4.0
4.5
5.0
5.5
6.0
PCA: Another Explanation
From k original variables: x1,x2,...,xk:
Produce k new variables: y1,y2,...,yk:
y1 = a11x1 + a12x2 + ... + a1kxk
y2 = a21x1 + a22x2 + ... + a2kxk
...
yk = ak1x1 + ak2x2 + ... + akkxk
yk's are
Principal Components
such that:
yk's are uncorrelated (orthogonal)
y1 explains as much as possible of original variance in data set
y2 explains as much as possible of remaining variance
etc.
‫דוגמא ‪matlab +‬‬
‫נתבונן על ה‬
‫‪ microarray‬הבא‬
‫‪ ,‬המכיל מידע של‬
‫‪ 100‬גנים ‪ ,‬ו ‪60‬‬
‫פציינטים ‪30‬‬
‫בריאים ו‪ 30‬חולים‬
‫הפקודה )(‪– imagesc‬מציגה את‬
‫המטריצה בצבעי חום – ערכים‬
‫גבוהים הינם אדומים וערכים‬
‫נמוכים הן כחולים‬
‫)‪[V E] = eig(C‬‬
‫מחזיר שתי מטריצות ‪ ,‬מטריצה ‪E‬‬
‫אלכסונית של הע"ע (אך ורק על‬
‫האלכסון) מסודרים בסדר עולה ‪,‬‬
‫ומטריצה ‪ V‬של הו"ע התואמים‬
‫לע"ע של מטריצה ‪.E‬‬
‫מגדיר מטריצת קוראינס ‪Cov(X)-‬‬
‫מחזיר וקטור המיצג את ‪diag(X)-‬‬
‫האלכסון של המטריצה‬
‫מציאת אינדקסים בוקטור המקיימים את‬
‫התנאי ‪-‬‬
‫)‪find(logic condition on vector‬‬
‫מציאת ה ‪ Pc‬ועל פיהם הגדרת‬
‫מטריצת הסיבוב‬
Example from 2005b
Perform pca for the following data sets
X=(0,0),(1,1),(2,2),(3,3),(-1,-1),(-2,-2),(-3,-3)
Mean(x)=0
 0 0  

 
 1 1  
 2 2  
0
1
2
3

1

2

3
4 




4  t
1 
  4 4 
 

0

3
3

0




 
 4

  4 4 
4

t
7  0 1 2 3 1 2 3    



 1 1  

 
  2 2 
  3 3  
 

(4  t)2  16  0 t 1  8 t 2  0
t1  8
 4 4  0  r2  r1  r2  4
 4 4   0    0

  

0 1 2 3 1 2
[1 1]  
0 1 2 3 1 2
4  0 
1

v

1
0  0 
 
3 
 [ 0 2 4 6  2  4  6]
3 