Home > NoiseTools > nt_cca_crossvalidate_3.m

nt_cca_crossvalidate_3

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,K)

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,K)
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<6; K=3; end
0019 if nargin<5
0020     A0=[]; B0=[]; 
0021 end
0022 if nargin<4; ncomp=[]; end
0023 if nargin<3 || isempty (shifts); shifts=[0]; end
0024 if nargin<2; error('!'); end
0025 if ~iscell(xx) || ~iscell(yy); error('!'); end
0026 if length(xx) ~= length (yy); error('!'); end
0027 %if size(xx{1},1) ~= size(yy{1},1); error('!'); end
0028 if size(xx{1},1) ~= size(yy{1},1); 
0029     for iTrial=1:numel(xx);
0030         tmp=min(size(xx{iTrial},1),size(yy{iTrial},1));
0031         xx{iTrial}=xx{iTrial}(1:tmp,:);
0032         yy{iTrial}=yy{iTrial}(1:tmp,:);
0033     end
0034 end
0035 
0036 nTrials=length(xx);
0037 
0038 if isempty(A0)
0039     % calculate covariance matrices
0040     n=size(xx{1},2)+size(yy{1},2);
0041     C=zeros(n,n,length(shifts),nTrials);
0042     disp('Calculate all covariances...'); tic;
0043     nt_whoss;
0044     for iTrial=1:nTrials
0045         C(:,:,:,iTrial)=nt_cov_lags(xx{iTrial}, yy{iTrial},shifts);
0046     end
0047 
0048     % calculate leave-one-out CCAs
0049     disp('Calculate CCAs...'); tic;
0050     for iTrial=1:nTrials
0051         others=setdiff(1:nTrials,iTrial);
0052         others=others(1:K);
0053         CC=sum(C(:,:,:,others),4); % covariance of all trials except iOut
0054         [A,B,R]=nt_cca([],[],[],CC,size(xx{1},2));  % CCA to apply to that trial (trained on others)
0055         AA{iTrial}=A;
0056         BB{iTrial}=B;
0057     end
0058     clear C CC
0059     toc;
0060 else
0061     % set to given values
0062     for iTrial=1:nTrials
0063         AA{iTrial}=A0;
0064         BB{iTrial}=B0;
0065     end
0066 end
0067 
0068 %%
0069 % calculate leave-one-out correlation coefficients
0070 disp('Calculate cross-correlations...'); tic;
0071 for iShift=1:length(shifts)
0072     xxx={}; yyy={};
0073     % shift, trim to same length, convert to CCs, normalize
0074     for iTrial=1:nTrials
0075         [xxx{iTrial},yyy{iTrial}]=nt_relshift(xx{iTrial},yy{iTrial},shifts(iShift));
0076         xxx{iTrial}=nt_normcol( nt_demean( nt_mmat(xxx{iTrial},AA{iTrial}(:,:,iShift)) ) );
0077         yyy{iTrial}=nt_normcol( nt_demean( nt_mmat(yyy{iTrial},BB{iTrial}(:,:,iShift)) ) );
0078     end
0079     for iTrial=1:nTrials
0080         x=xxx{iTrial};
0081         y=yyy{iTrial};
0082         RR(:,iShift,iTrial)=diag(x'*y) / size(x,1);
0083     end
0084 end
0085 toc;
0086 
0087 if isempty(ncomp); ncomp=size(RR,1); end
0088 [~,iBest]=max(mean(mean(RR(1:ncomp,:,:),3),1)'); 
0089 
0090 disp('done');
0091

Generated on Sat 29-Apr-2023 17:15:46 by m2html © 2005