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] (2010-06-17 12:21:00, 2873.9 KB) [[attachment:bioinf.pdf]]
  • [get | view] (2010-05-04 11:11:44, 3591.3 KB) [[attachment:cca_lecture.pdf]]
  • [get | view] (2010-06-08 09:37:55, 209.6 KB) [[attachment:data.tar.gz]]
  • [get | view] (2010-04-19 09:59:41, 65.6 KB) [[attachment:full_sheet01.pdf]]
  • [get | view] (2010-04-20 09:18:53, 61.3 KB) [[attachment:full_sheet02.pdf]]
  • [get | view] (2010-04-27 09:42:10, 70.0 KB) [[attachment:full_sheet03.pdf]]
  • [get | view] (2010-05-04 10:48:12, 75.0 KB) [[attachment:full_sheet04.pdf]]
  • [get | view] (2010-05-11 08:22:55, 91.1 KB) [[attachment:full_sheet05.pdf]]
  • [get | view] (2010-05-18 10:01:06, 61.9 KB) [[attachment:full_sheet06.pdf]]
  • [get | view] (2010-05-27 10:02:14, 76.7 KB) [[attachment:full_sheet07.pdf]]
  • [get | view] (2010-06-01 08:38:57, 70.8 KB) [[attachment:full_sheet08.pdf]]
  • [get | view] (2010-06-08 09:37:48, 58.2 KB) [[attachment:full_sheet09.pdf]]
  • [get | view] (2010-06-15 10:05:24, 120.8 KB) [[attachment:full_sheet10.pdf]]
  • [get | view] (2010-06-22 08:07:29, 71.3 KB) [[attachment:full_sheet11.pdf]]
  • [get | view] (2010-06-29 09:14:44, 76.2 KB) [[attachment:full_sheet12.pdf]]
  • [get | view] (2010-07-06 10:08:39, 83.4 KB) [[attachment:full_sheet13.pdf]]
  • [get | view] (2010-06-01 08:40:12, 1391.7 KB) [[attachment:kld-tutorial.pdf]]
  • [get | view] (2010-05-27 06:38:11, 2850.3 KB) [[attachment:lect-ids.pdf]]
  • [get | view] (2010-05-20 13:07:56, 2099.2 KB) [[attachment:lect-struct.pdf]]
  • [get | view] (2010-04-20 09:19:25, 26591.3 KB) [[attachment:mnist_train.mat]]
  • [get | view] (2010-07-06 10:08:16, 192.5 KB) [[attachment:optim-intro.pdf]]
  • [get | view] (2010-04-20 09:19:00, 1.0 KB) [[attachment:sheet02.m]]
  • [get | view] (2010-05-11 08:23:01, 0.6 KB) [[attachment:sheet05.m]]
  • [get | view] (2010-05-27 10:02:41, 4.3 KB) [[attachment:sheet07.m]]
  • [get | view] (2010-06-01 08:39:07, 0.9 KB) [[attachment:sheet08.m]]
  • [get | view] (2010-06-08 09:38:00, 2.2 KB) [[attachment:sheet09.m]]
  • [get | view] (2010-06-08 09:38:06, 2.3 KB) [[attachment:sheet09.py]]
  • [get | view] (2010-06-22 08:07:55, 1.1 KB) [[attachment:sheet11.m]]
  • [get | view] (2010-06-22 08:07:51, 129.6 KB) [[attachment:splice-test-data.txt]]
  • [get | view] (2010-06-22 08:09:26, 5.4 KB) [[attachment:splice-test-label.txt]]
  • [get | view] (2010-06-22 08:07:41, 59.6 KB) [[attachment:splice-train-data.txt]]
  • [get | view] (2010-06-22 08:07:47, 2.5 KB) [[attachment:splice-train-label.txt]]
  • [get | view] (2010-04-27 08:49:35, 1515.8 KB) [[attachment:ssa_data.mat]]
  • [get | view] (2010-04-27 08:49:39, 585.7 KB) [[attachment:ssa_lecture.pdf]]
  • [get | view] (2010-04-27 08:49:50, 7.4 KB) [[attachment:ssa_simple.m]]
  • [get | view] (2010-05-27 06:34:10, 1217.5 KB) [[attachment:stud-data.mat.gz]]
  • [get | view] (2010-06-08 09:39:24, 1013.6 KB) [[attachment:textmining.pdf]]
  • [get | view] (2010-05-04 10:49:39, 1.0 KB) [[attachment:tkcca_example.m]]
  • [get | view] (2010-05-04 10:48:19, 4.1 KB) [[attachment:tkcca_simple.m]]
  • [get | view] (2010-05-04 10:48:24, 150.9 KB) [[attachment:tkcca_toy_data.mat]]
 All files | Selected Files: delete move to page copy to page

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