Attachment 'tkcca_simple.m'

Download

   1 function [c,U,V,c_perc,U_perc,V_perc] = tkcca(X,Y,tau,kappas)
   2 % [c, U, V, c_bstrp, U_bstrp, V_bstrp] = tkcca(X,Y,tau,kappas)
   3 %
   4 % 	temporal kernel CCA
   5 %	
   6 % INPUT
   7 %	X	a nDimensions-by-Samples data matrix
   8 %	Y	a mDimensions-by-Samples data matrix
   9 %	tau	the maximal time lag by which X is shifted with respect to Y
  10 %	kappas	the regularizers
  11 %	
  12 % OUTPUT
  13 %	c	the canonical correlogram, from -tau to tau
  14 %	U	the time resolved canonical variate for X
  15 %	V	the canonical variate for Y
  16 %	
  17 %	USAGE 
  18 %
  19 %>> [c,U,V] = tkcca(X,Y,10,.1)
  20 %		
  21 %	computes canonical correlogram c, a time resolved variate U and canonical 
  22 %	projection V for timelag -10 to 10 with fixed regularizer of .1
  23 %
  24 
  25 if nargin<4, kappas = [10.^-[0:7]]'; end
  26 
  27 % center data
  28 X = X - repmat(mean(X,2),1,size(X,2));
  29 Y = Y - repmat(mean(Y,2),1,size(Y,2));
  30 % embed one signal in time shifted copies of itself
  31 [X, timeidx, tauidx] = embed(X,tau);
  32 % compute the linear kernels
  33 kY = Y(:,timeidx)' * Y(:,timeidx);
  34 kX = X' * X;
  35 % find the right regularizer
  36 kappaOpt	= optimize_kappa(kX,kY,kappas);
  37 % compute kcca using the right regularizer
  38 [r, a, b]	= kcca(kX,kY,kappaOpt);
  39 % reconstruct the canonical variates and compute canonical correlogram
  40 [U, V, c]	= reconstruct(X,Y(:,timeidx),a,b,tauidx);
  41 
  42 
  43 function [eX, timeidx, tauidx] = embed(X,tau)
  44 	% embed the first signal in its temporal context
  45 	[D T] = size(X);
  46 	% in case tau is a scalar, make it a vector from -tau to tau
  47 	if length(tau)==1,tau = -tau:tau;end
  48 	startInd 	= abs(tau(1)) + 1;
  49 	stopInd		= T - abs(tau(end));
  50 	len			= stopInd - startInd + 1;
  51 	% create a column vector that contains the indices of the first segment
  52 	idx = repmat((startInd:stopInd)', 1, length(tau)) + repmat(tau, len, 1); 	
  53 	% create (linear) indices for the different dimensions
  54 	dim_offset = repmat( (0:D-1)*T, length(tau)*len, 1);
  55 	idx = repmat(idx(:), 1, D) + dim_offset;
  56 	% for the linear indices we need column-signals
  57 	X = X';
  58 	% get the data (D channels, segments are concatenated) and reshape it
  59 	eX = reshape(X(idx), len, length(tau)*D)';
  60 	tauidx = repmat(tau',D,1); 
  61 	timeidx = startInd:stopInd;
  62 
  63 function [r,a,b] = kcca(kX,kY,kappa)
  64 	% compute the dual coefficients 
  65 	n = size(kX,1);
  66 	options.disp = 0;
  67 	% force kernel symmetry
  68 	kX = (kX+kX')/2; kY = (kY+kY')/2;
  69 	% normalise the spectral norm of the matrices
  70 	kX = kX./max(eig(kX));
  71 	kY = kY./max(eig(kY));
  72 	% Generate LH
  73 	LH = [zeros(n) kX*kY';kY*kX' zeros(n)];
  74 	% generate RH with regularization ridge
  75 	RH = [kX*kX' zeros(n);zeros(n) kY*kY'] + eye(2*n)*kappa;
  76 	% make sure the matrices are symmetric
  77 	RH=(RH+RH')/2; LH=(LH+LH')/2;
  78 	% Compute the generalized eigenvectors
  79 	[Vs,r]=eigs(LH,RH,1,'LA',options);
  80 	a = Vs(1:n);
  81 	b = Vs(n+1:end);
  82 	
  83 function kappa = optimize_kappa(kX,kY,kappas,iterations)
  84 	if nargin<4, iterations=10; end
  85 	shcors = zeros(length(kappas),iterations);
  86 	skX = kX; skY = kY;
  87 	% try each regularizer
  88 	for iR = 1:length(kappas)
  89 		r(iR,1) = kcca(kX,kY,kappas(iR));
  90 		% for all iterations of the reshuffling procedure
  91 		for iS = 1:iterations
  92 			idx = randperm(size(kX,1));
  93 			skX = kX(idx,idx);		
  94 		    % do cca on shuffled data
  95      		shcors(iR,iS) = kcca(skX,skY,kappas(iR));
  96 		end
  97 	end
  98   	% pick that regularizer that maximizes the distance between 
  99   	% true correlations and shuffled data correlations
 100   	[val,pick]  = max(mean((repmat(r,1,(iterations))-shcors).^2,2));
 101   	fprintf('Picked kappa=[ %2.8f ]\ncorrelations  %0.2f (true) vs. %0.2f (shuffled)\n',...
 102   			kappas(pick),r(pick),mean(shcors(pick,:)))
 103   	kappa = kappas(pick);
 104 
 105 function [U,V,c] = reconstruct(eX,Y,a,b,tidx)
 106 	% the number of time lags
 107 	nTau = length(unique(tidx));
 108 	% the number of dimensions
 109 	D = size(eX,1)/nTau;
 110 	% the time-lag sorted indices
 111 	[sorted, sortInds] = sort(tidx);
 112 	% the zero lag canonical component
 113 	pY = ((Y * b)' * Y)';
 114 	% the time shifted canonical components
 115 	pX = repmat(eX * a, 1, size(eX, 2) ) .* eX;
 116 	if D>1
 117 		pX = squeeze(mean(reshape(pX(sortInds,:), [D,nTau, size(eX,2)] )));
 118 	end
 119 	% the correlations between the zero lag component of Y and the 
 120 	% time shifted components of X (i.e. the canonical correlogram)
 121 	c = corr(pY,pX');
 122 	% the time resolved variate
 123 	U = reshape(eX * a, nTau, D)';
 124 	% the other variate
 125 	V = Y * b;

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-06-22 07:45:04, 2873.9 KB) [[attachment:bio_intro.pdf]]
  • [get | view] (2011-05-02 16:51:13, 1341.1 KB) [[attachment:buchner_chap2007.pdf]]
  • [get | view] (2011-04-19 12:05:37, 3719.0 KB) [[attachment:cca_lecture.pdf]]
  • [get | view] (2011-07-20 11:25:53, 1778.9 KB) [[attachment:chemo_lecture.pdf]]
  • [get | view] (2011-05-16 22:58:50, 723.1 KB) [[attachment:deep_lecture.pdf]]
  • [get | view] (2011-04-11 15:08:58, 95.6 KB) [[attachment:full_sheet01.pdf]]
  • [get | view] (2011-04-19 11:33:46, 130.0 KB) [[attachment:full_sheet02.pdf]]
  • [get | view] (2011-05-02 16:53:54, 123.4 KB) [[attachment:full_sheet03.pdf]]
  • [get | view] (2011-05-10 10:03:36, 112.2 KB) [[attachment:full_sheet04.pdf]]
  • [get | view] (2011-05-17 09:43:04, 34.8 KB) [[attachment:full_sheet05.pdf]]
  • [get | view] (2011-05-23 12:18:47, 95.7 KB) [[attachment:full_sheet06.pdf]]
  • [get | view] (2011-05-30 13:35:32, 98.3 KB) [[attachment:full_sheet07.pdf]]
  • [get | view] (2011-06-07 15:23:13, 125.3 KB) [[attachment:full_sheet08.pdf]]
  • [get | view] (2011-06-21 08:43:34, 164.4 KB) [[attachment:full_sheet09.pdf]]
  • [get | view] (2011-07-05 09:39:18, 100.4 KB) [[attachment:full_sheet10.pdf]]
  • [get | view] (2011-05-31 20:33:20, 2656.3 KB) [[attachment:ids_lect.pdf]]
  • [get | view] (2011-05-24 15:25:30, 2121.4 KB) [[attachment:kern_struct.pdf]]
  • [get | view] (2011-06-14 10:50:39, 1169.4 KB) [[attachment:mkl_intro.pdf]]
  • [get | view] (2011-05-04 09:54:42, 512.3 KB) [[attachment:ml2_bss.pdf]]
  • [get | view] (2011-05-17 09:43:15, 7656.4 KB) [[attachment:mnist.mat]]
  • [get | view] (2011-06-07 18:29:45, 192.5 KB) [[attachment:opt_intro.pdf]]
  • [get | view] (2011-05-17 09:43:21, 1.7 KB) [[attachment:rbm.m]]
  • [get | view] (2011-05-02 16:54:43, 0.7 KB) [[attachment:sfft.m]]
  • [get | view] (2011-05-30 13:37:07, 4.3 KB) [[attachment:sheet07.m]]
  • [get | view] (2011-06-21 08:43:43, 1.1 KB) [[attachment:sheet09.m]]
  • [get | view] (2011-05-17 09:43:25, 0.1 KB) [[attachment:sigmoid.m]]
  • [get | view] (2011-06-21 08:43:50, 129.6 KB) [[attachment:splice-test-data.txt]]
  • [get | view] (2011-06-21 08:43:55, 5.4 KB) [[attachment:splice-test-label.txt]]
  • [get | view] (2011-06-21 08:44:00, 59.6 KB) [[attachment:splice-train-data.txt]]
  • [get | view] (2011-06-21 08:44:05, 2.5 KB) [[attachment:splice-train-label.txt]]
  • [get | view] (2011-04-18 07:30:47, 1515.8 KB) [[attachment:ssa_data.mat]]
  • [get | view] (2011-04-18 07:28:13, 585.7 KB) [[attachment:ssa_lecture.pdf]]
  • [get | view] (2011-04-18 07:30:20, 7.4 KB) [[attachment:ssa_simple.m]]
  • [get | view] (2011-07-05 11:05:27, 1202.4 KB) [[attachment:structured_lecture.pdf]]
  • [get | view] (2011-05-30 13:36:38, 1217.5 KB) [[attachment:stud-data.mat.gz]]
  • [get | view] (2011-04-18 07:29:43, 1.0 KB) [[attachment:tkcca_example.m]]
  • [get | view] (2011-04-18 07:29:54, 4.1 KB) [[attachment:tkcca_simple.m]]
  • [get | view] (2011-04-18 07:30:07, 150.9 KB) [[attachment:tkcca_toy_data.mat]]
  • [get | view] (2011-05-02 16:51:28, 4.5 KB) [[attachment:umfbss.m]]
  • [get | view] (2011-05-02 16:03:05, 531.3 KB) [[attachment:x1.wav]]
  • [get | view] (2011-05-02 16:03:12, 531.3 KB) [[attachment:x2.wav]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.