Docsity

Matlab Script for Experiment 1 - Matlab Script for Experiment 1 | MAP 6938, Study Guides, Projects, Research of Mathematics

Toggle sidebar
bg1
Subhabrata Bhattacharya MAP 6398 PROJECT 04
1. Theory The method of diffusion maps attempts to provide some parametrization of an abstract mathematical
space bearing a complex global structure whose each data-point has a Euclidean neighborhood associated with
it. In other words, diffusion maps are used to establish relationship between the members of a multidimensional
data-set through fusion of more than one dimensions into one. This is achived through an approximation technique
used by Laplace-Beltrami operator.
The role of the kernelfunction used in this method is to provide an idea about how connected each member in a
data-set is with the others in the same data-set.
2. Application We are required to perform two numerical experiments using diffusion maps. In both of these
experiments we detect the true dimension of given datasets. For both the experiments we synthetically generate
the data-sets as directed.
(a) In this experiment we generate a data-set 3X1000 that has data-points varying linearly. A normally distributed
noise term is then added to the data-points. Then we apply the diffusion map algorithm to find the six most
important eigenvectors. We compare the representation with significant eigenvectors obtained by Principal
Component Analysis. We select the second smallest eigenvalue and its corresponding eigenvectors because the
smallest is the most trivial representation. This representation tallies with the representation derived from
the largest eigenvalue of PCA. We plot both the results.
Listing 1: Matlab script for experiment 1
1 c l e a r a l l ; cl o s e a l l ; c l c ;
2% Con s t r u c t i ng X
3 n = 1 0 0 0 ;
4 s e e d = 1 0 ;
5 A = [ 3 ; 2 ; 1 . 4 ] ;
6 B = [ 0 ; 4 ; 2 ] ;
7 p i = 3 . 1 4 1 5 1 9 ;
8 s ig ma = 1 ;
9
10 T = see d
*
ra n d ( 1 , n ) ;
11 X = z e r o s ( 3 , n ) ;
12 for i c n t =1: n
13 X( : , i c n t )= T( i c n t ) .
*
A + B ;
14 en d
15 % A dd in g t h e Ga u ss i an n o i s e te rm
16 X = X + ran dn ( 3 , n ) ;
17
18 % Pl o t th e r e s u l t s
19 c o l o r l i s t = so r t ( ra nd ( 1 , n ) ,
'
descend
'
) ;
20 f i g u r e ;
21 s u b p l o t ( 1 , 2 , 1 ) ;
22 s c a t t e r 3 ( X( 1 , : )
'
, X ( 2 , : )
'
, X ( 3 , : )
'
, 1 0 ) ;
23 x l a b e l (
'
x
'
) ;
24 y l a b e l (
'
y
'
) ;
25 z l a b e l (
'
z
'
) ;
26
27 s u b p l o t ( 1 , 2 , 2 ) ;
28 s c a t t e r 3 ( X( 1 , : )
'
, X ( 2 , : )
'
, X ( 3 , : )
'
, 1 0 , c o l o r l i s t ) ;
29 x l a b e l (
'
x
'
) ;
30 y l a b e l (
'
y
'
) ;
31 z l a b e l (
'
z
'
) ;
32 c o l o r b a r ;
33
34 % X i s o ur da ta−s e t g e n e r at e d wi t h some l i n e a r e qu a t i o n wi th some a d d i t i v e
35 % Ga us s ia n n o i s e . X i s 3 x 1 0 00 m a tr i x . Our j o b i s t o re p r e s e n t X −>Y su c h
36 % t h at Y is 3 x m wh er e m << 100 0 . T hi s c ou l d be d i v i d e d i n t o t h e f o l l o w i n g
37 % steps.
38
39 % Let u s a s su me X i s a g r ap h o f 10 00 n o d e s . we c r e a te a c o s t a d ja c e n c y
40 % ma tr ix (W) f o r X who se e n t r i e s a r e we i gh t s bet we en e dg e s of n od e s in X.
41 % Wij = e xp (− | | x i −x j | | 2 / e p s i l o n ) . e p s i l o n i s da t a d e pe nd en t , we se l e c t
1
pf3
pf4
pf5

Partial preview of the text

Download Matlab Script for Experiment 1 - Matlab Script for Experiment 1 | MAP 6938 and more Study Guides, Projects, Research Mathematics in PDF only on Docsity!

1. Theory The method of diffusion maps attempts to provide some parametrization of an abstract mathematical

space bearing a complex global structure whose each data-point has a Euclidean neighborhood associated with

it. In other words, diffusion maps are used to establish relationship between the members of a multidimensional

data-set through fusion of more than one dimensions into one. This is achived through an approximation technique

used by Laplace-Beltrami operator.

The role of the kernelfunction used in this method is to provide an idea about how connected each member in a

data-set is with the others in the same data-set.

2. Application We are required to perform two numerical experiments using diffusion maps. In both of these

experiments we detect the true dimension of given datasets. For both the experiments we synthetically generate

the data-sets as directed.

(a) In this experiment we generate a data-set 3X1000 that has data-points varying linearly. A normally distributed

noise term is then added to the data-points. Then we apply the diffusion map algorithm to find the six most

important eigenvectors. We compare the representation with significant eigenvectors obtained by Principal

Component Analysis. We select the second smallest eigenvalue and its corresponding eigenvectors because the

smallest is the most trivial representation. This representation tallies with the representation derived from

the largest eigenvalue of PCA. We plot both the results.

Listing 1: Matlab script for experiment 1

1 c l e a r a l l ; c l o s e a l l ; c l c ; 2 % C o n s t r u c t i n g X 3 n = 1 0 0 0 ; 4 s e e d = 1 0 ; 5 A = [ 3 ; 2 ; 1. 4 ] ; 6 B = [ 0 ; 4 ; 2 ] ; 7 p i = 3. 1 4 1 5 1 9 ; 8 sigma = 1 ; 9 10 T = s e e d * rand ( 1 , n ) ; 11 X = z e r o s ( 3 , n ) ; 12 f o r i c n t =1:n 13 X( : , i c n t )= T( i c n t ). *A + B ; 14 end 15 % Adding t h e Gaussian n o i s e term 16 X = X + randn ( 3 , n ) ; 17 18 % P l o t t h e r e s u l t s 19 c o l o r l i s t = s o r t ( rand ( 1 , n ) , ' d e s c e n d ' ) ; 20 f i g u r e ; 21 s u b p l o t ( 1 , 2 , 1 ) ; 22 s c a t t e r 3 (X( 1 , : ) ' , X( 2 , : ) ' , X( 3 , : ) ' , 1 0 ) ; 23 x l a b e l ( ' x ' ) ; 24 y l a b e l ( ' y ' ) ; 25 z l a b e l ( ' z ' ) ; 26 27 s u b p l o t ( 1 , 2 , 2 ) ; 28 s c a t t e r 3 (X( 1 , : ) ' , X( 2 , : ) ' , X( 3 , : ) ' , 1 0 , c o l o r l i s t ) ; 29 x l a b e l ( ' x ' ) ; 30 y l a b e l ( ' y ' ) ; 31 z l a b e l ( ' z ' ) ; 32 c o l o r b a r ; 33 34 % X i s our data−s e t g e n e r a t e d with some l i n e a r e q u a t i o n with some a d d i t i v e 35 % Gaussian n o i s e. X i s 3 x 1000 m a t r i x. Our j o b i s t o r e p r e s e n t X −> Y such 36 % t h a t Y i s 3 x m where m << 1000. This c o u l d be d i v i d e d i n t o t h e f o l l o w i n g 37 % s t e p s. 38 39 % Let us assume X i s a graph o f 1000 n o d e s. we c r e a t e a c o s t a d j a c e n c y 40 % matrix (W) f o r X whose e n t r i e s a r e w e i g h t s between e d g e s o f nodes i n X. 41 % Wij = exp (− | | x i − x j | | 2 / e p s i l o n ). e p s i l o n i s data dependent , we s e l e c t

42 % e p s i l o n 43 sumW = 0 ; 44 f o r i c n t =1:n 45 f o r j c n t =1:n 46 W( i c n t , j c n t ) = norm (X( : , i c n t ) − X( : , j c n t ) , 2 ) ; 47 end 48 sumW = sumW + min ( n o n z e r o s (W( i c n t , : ) ) ) ; 49 end 50 e p s i l o n = sumW/n ; 51 W = exp(−W/ e p s i l o n ) ; 52 53 % Now we c r e a t e t h e n o r m a l i z a t i o n matrix D and t h e L a p l a c i a n matrix L. D i s 54 % a d i a g o n a l matrix where each o f i t s d i a g o n a l e l e m e n t i s e q u a l t o t h e sum 55 % o f a l l rows i n a p a r t i c u l a r column. 56 v = o n e s ( n , 1 ) ; 57 D = d i a g (Wv ) ; 58 %L = D − W; 59 S = Dˆ−0. 5 WDˆ−0. 5 ; 60 61 % Computing t h e E i g e n v a l u e s and e i g e n v e c t o r s o f t h e g e n e r a l i z e d E i g e n v a l u e 62 % problem Ly = lambda Dy ( y b e i n g column v e c t o r s o f Y). We would need t o 63 % f i n d t h e 6 most i m p o r t a n t e i g e n v e c t o r s i. e , t h e s m a l l e s t 6 e i g e n v e c t o r s 64 % from t h e d i s t r i b u t i o n 65 k = 6 ; 66 %[ Evect Eval ] = e i g s ( S , k , ' sm ' ) ; 67 [ Evect Eval ] = e i g s ( S ) ; 68 Evect = Dˆ−0. 5 * Evect ; 69 Evect = E v e c t. / Evect ( 1 , 1 ) ; 70 71 % Applying a PCA a l g o r i t h m t o f i n d t h e s i x l a r g e s t e i g e n v e c t o r s o f t h e 72 % d a t a s e t 73 data = S ; 74 mData = mean ( data , 1 ) ; 75 76 f o r i c n t = 1 : s i z e ( data , 1 ) 77 data ( i c n t , : ) = data ( i c n t , : ) − mData ; 78 end 79 [ pEvect S ] = svd ( data , ' econ ' ) ; 80 pEvect = p E v e c t. / pEvect ( 1 , 1 ) ; 81 82 % p l o t t h e p o i n t s u s i n g t h e 2nd s m a l l e s t e i g e n v e c t o r 83 c l d i f f = Evect ( : , 2 ) ; 84 f i g u r e ; 85 s u b p l o t ( 1 , 2 , 1 ) ; 86 s c a t t e r 3 (X( 1 , : ) ' , X( 2 , : ) ' , X( 3 , : ) ' , 1 0 , c l d i f f ) ; 87 x l a b e l ( ' x ' ) ; 88 y l a b e l ( ' y ' ) ; 89 z l a b e l ( ' z ' ) ; 90 c o l o r b a r ; 91 92 % p l o t t h e p o i n t s u s i n g t h e l a r g e s t e i g e n v e c t o r o b t a i n e d from PCA 93 c l p c a = pEvect ( : , 1 ) ; 94 95 s u b p l o t ( 1 , 2 , 2 ) ; 96 s c a t t e r 3 (X( 1 , : ) ' , X( 2 , : ) ' , X( 3 , : ) ' , 1 0 , c l p c a ) ; 97 x l a b e l ( ' x ' ) ; 98 y l a b e l ( ' y ' ) ; 99 z l a b e l ( ' z ' ) ; 100 c o l o r b a r ;

(b) Here, we generate a data-set 3X1000 that has data-points varying non-linearly using sinusoidal functions. A

normally distributed noise term is also added to the data-points. With the help of methods similar to that of

problem 1, we compare the representations.

39 sumW = 0 ; 40 f o r i c n t =1:n 41 f o r j c n t =1:n 42 W( i c n t , j c n t ) = norm (X( : , i c n t ) − X( : , j c n t ) , 2 ) ; 43 end 44 sumW = sumW + min ( n o n z e r o s (W( i c n t , : ) ) ) ; 45 end 46 e p s i l o n = sumW/n ; 47 W = exp(−W/ e p s i l o n ) ; 48 49 % Now we c r e a t e t h e n o r m a l i z a t i o n matrix D and t h e L a p l a c i a n matrix L. D i s 50 % a d i a g o n a l matrix where each o f i t s d i a g o n a l e l e m e n t i s e q u a l t o t h e sum 51 % o f a l l rows i n a p a r t i c u l a r column. 52 v = o n e s ( n , 1 ) ; 53 D = d i a g (Wv ) ; 54 %L = D − W; 55 S = Dˆ−0. 5 WDˆ−0. 5 ; 56 57 % Computing t h e E i g e n v a l u e s and e i g e n v e c t o r s o f t h e g e n e r a l i z e d E i g e n v a l u e 58 % problem Ly = lambda Dy ( y b e i n g column v e c t o r s o f Y). We would need t o 59 % f i n d t h e 6 most i m p o r t a n t e i g e n v e c t o r s i. e , t h e s m a l l e s t 6 e i g e n v e c t o r s 60 % from t h e d i s t r i b u t i o n 61 k = 6 ; 62 %[ Evect Eval ] = e i g s ( S , k , ' sm ' ) ; 63 [ Evect Eval ] = e i g s ( S ) ; 64 Evect = Dˆ−0. 5 * Evect ; 65 Evect = E v e c t. / Evect ( 1 , 1 ) ; 66 67 % Applying a PCA a l g o r i t h m t o f i n d t h e s i x l a r g e s t e i g e n v e c t o r s o f t h e 68 % d a t a s e t 69 data = S ; 70 mData = mean ( data , 1 ) ; 71 72 f o r i c n t = 1 : s i z e ( data , 1 ) 73 data ( i c n t , : ) = data ( i c n t , : ) − mData ; 74 end 75 [ pEvect S ] = svd ( data , ' econ ' ) ; 76 pEvect = p E v e c t. / pEvect ( 1 , 1 ) ; 77 78 % p l o t t h e p o i n t s u s i n g t h e 2nd s m a l l e s t e i g e n v e c t o r 79 c l d i f f = Evect ( : , 2 ) ; 80 f i g u r e ; 81 s u b p l o t ( 1 , 2 , 1 ) ; 82 s c a t t e r 3 (X( 1 , : ) ' , X( 2 , : ) ' , X( 3 , : ) ' , 1 0 , c l d i f f ) ; 83 x l a b e l ( ' x ' ) ; 84 y l a b e l ( ' y ' ) ; 85 z l a b e l ( ' z ' ) ; 86 c o l o r b a r ; 87 88 % p l o t t h e p o i n t s u s i n g t h e l a r g e s t e i g e n v e c t o r o b t a i n e d from PCA 89 c l p c a = pEvect ( : , 1 ) ; 90 91 s u b p l o t ( 1 , 2 , 2 ) ; 92 s c a t t e r 3 (X( 1 , : ) ' , X( 2 , : ) ' , X( 3 , : ) ' , 1 0 , c l p c a ) ; 93 x l a b e l ( ' x ' ) ; 94 y l a b e l ( ' y ' ) ; 95 z l a b e l ( ' z ' ) ; 96 c o l o r b a r ;

Figure 2: Demonstration of representation of a non-linear data-set using diffusion maps.