Home > NoiseTools > nt_cca_crossvalidate.m

nt_cca_crossvalidate

PURPOSE ^

[AA,BB,RR,iBest]=nt_cca_crossvalidate(xx,yy,shifts,ncomp,A0,B0) - CCA with cross-validation

SYNOPSIS ^

function [AA,BB,RR,iBest]=nt_cca_crossvalidate(xx,yy,shifts,ncomp,A0,B0)

DESCRIPTION ^

[AA,BB,RR,iBest]=nt_cca_crossvalidate(xx,yy,shifts,ncomp,A0,B0) - CCA with cross-validation

  AA, BB: cell arrays of transform matrices
  RR: r scores (3D) for all components, shifts and trials
  iBest: index of best shift

  xx,yy: cell arrays of column matrices
  shifts: array of shifts to apply to y relative to x (can be negative)
  ncomp: number of components to consider for iBest [default: all]
  A0,B0: if present, use these CCA transform matrices 

  Plot correlation re shifts for matching trials
    plot(shifts, mean(RR,3)');
  Plot mean correlation re shifts for mismatched trials
    plot(shifts, mean(mean(RR,4),3)');

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [AA,BB,RR,iBest]=nt_cca_crossvalidate(xx,yy,shifts,ncomp,A0,B0)
0002 %[AA,BB,RR,iBest]=nt_cca_crossvalidate(xx,yy,shifts,ncomp,A0,B0) - CCA with cross-validation
0003 %
0004 %  AA, BB: cell arrays of transform matrices
0005 %  RR: r scores (3D) for all components, shifts and trials
0006 %  iBest: index of best shift
0007 %
0008 %  xx,yy: cell arrays of column matrices
0009 %  shifts: array of shifts to apply to y relative to x (can be negative)
0010 %  ncomp: number of components to consider for iBest [default: all]
0011 %  A0,B0: if present, use these CCA transform matrices
0012 %
0013 %  Plot correlation re shifts for matching trials
0014 %    plot(shifts, mean(RR,3)');
0015 %  Plot mean correlation re shifts for mismatched trials
0016 %    plot(shifts, mean(mean(RR,4),3)');
0017 
0018 if nargin<5
0019     A0=[]; B0=[]; 
0020 end
0021 if nargin<4; ncomp=[]; end
0022 if nargin<3 || isempty (shifts); shifts=[0]; end
0023 if nargin<2; error('!'); end
0024 if ~iscell(xx) || ~iscell(yy); error('!'); end
0025 if length(xx) ~= length (yy); error('!'); end
0026 if size(xx{1},1) ~= size(yy{1},1); error('!'); end
0027 
0028 nTrials=length(xx);
0029 
0030 if isempty(A0)
0031     % calculate covariance matrices
0032     n=size(xx{1},2)+size(yy{1},2);
0033     C=zeros(n,n,length(shifts),nTrials);
0034     disp('Calculate all covariances...'); tic;
0035     nt_whoss;
0036     for iTrial=1:nTrials
0037         C(:,:,:,iTrial)=nt_cov_lags(xx{iTrial}, yy{iTrial},shifts);
0038     end
0039 
0040     % calculate leave-one-out CCAs
0041     disp('Calculate CCAs...'); tic;
0042     for iTrial=1:nTrials
0043         CC=sum(C(:,:,:,setdiff(1:nTrials,iTrial)),4); % covariance of all trials except iOut
0044         [A,B,R]=nt_cca([],[],[],CC,size(xx{1},2));  % CCA to apply to that trial (trained on others)
0045         AA{iTrial}=A;
0046         BB{iTrial}=B;
0047     end
0048     clear C CC
0049     toc;
0050 else
0051     % set to given values
0052     for iTrial=1:nTrials
0053         AA{iTrial}=A0;
0054         BB{iTrial}=B0;
0055     end
0056 end
0057 
0058 %%
0059 % calculate leave-one-out correlation coefficients
0060 disp('Calculate cross-correlations...'); tic;
0061 for iShift=1:length(shifts)
0062     xxx={}; yyy={};
0063     % shift, trim to same length, convert to CCs, normalize
0064     for iTrial=1:nTrials
0065         [xxx{iTrial},yyy{iTrial}]=nt_relshift(xx{iTrial},yy{iTrial},shifts(iShift));
0066         xxx{iTrial}=nt_normcol( nt_demean( nt_mmat(xxx{iTrial},AA{iTrial}(:,:,iShift)) ) );
0067         yyy{iTrial}=nt_normcol( nt_demean( nt_mmat(yyy{iTrial},BB{iTrial}(:,:,iShift)) ) );
0068     end
0069     for iTrial=1:nTrials
0070         x=xxx{iTrial};
0071         y=yyy{iTrial};
0072         RR(:,iShift,iTrial)=diag(x'*y) / size(x,1);
0073     end
0074 end
0075 toc;
0076 
0077 if isempty(ncomp); ncomp=size(RR,1); end
0078 [~,iBest]=max(mean(mean(RR(1:ncomp,:,:),3),1)'); 
0079 
0080 disp('done');
0081

Generated on Tue 18-Feb-2020 11:23:12 by m2html © 2005