Home > NoiseTools > nt_cca_crossvalidate_old.m

# nt_cca_crossvalidate_old

## PURPOSE

[AA,BB,RR,SD]=nt_cca_crossvalidate(xx,yy,shifts) - CCA with cross-validation

## SYNOPSIS

function [AA,BB,RR,SD]=nt_cca_crossvalidate(xx,yy,shifts)

## DESCRIPTION

    [AA,BB,RR,SD]=nt_cca_crossvalidate(xx,yy,shifts) - CCA with cross-validation

      AA, BB: cell arrays of transform matrices
      RR: r scores (2D)
      SD: standard deviation of correlation over non-matching pairs (2D)

      xx,yy: cell arrays of column matrices
      shifts: array of shifts to apply to y relative to x (can be negative)

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_cca [A,B,R]=nt_cca(x,y,shifts,C,m,thresh,demeanflag) - canonical correlation
- nt_cov_lags [C,tw,m]=nt_cov_lags(x,y,shifts,nodemeanflag) - covariance of [x,y] with lags
- nt_normcol [y,norm]=nt_normcol(x,w) - normalize each column so its weighted msq is 1
- nt_relshift [xx,yy]=nt_relshift(x,y,shift,flag) - delay x relative to y
- nt_whoss size=nt_whoss - total Gbytes used by variables

This function is called by:

## SOURCE CODE

    0001 function [AA,BB,RR,SD]=nt_cca_crossvalidate(xx,yy,shifts)
    0002 %[AA,BB,RR,SD]=nt_cca_crossvalidate(xx,yy,shifts) - CCA with cross-validation
    0003 %
    0004 %  AA, BB: cell arrays of transform matrices
    0005 %  RR: r scores (2D)
    0006 %  SD: standard deviation of correlation over non-matching pairs (2D)
    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
    0011 if nargin<3; shifts=[0]; end
    0012 if nargin<2; error('!'); end
    0013 if ~iscell(xx) || ~iscell(yy); error('!'); end
    0014 if length(xx) ~= length (yy); error('!'); end
    0015 if size(xx{1},1) ~= size(yy{1},1); error('!'); end
    0016
    0017 if nargout==0 || nargout==4; doSurrogate=1; else doSurrogate=0; end
    0018
    0019 %%
    0020 % calculate covariance matrices
    0021 nTrials=length(xx);
    0022 n=size(xx{1},2)+size(yy{1},2);
    0023 C=zeros(n,n,length(shifts),nTrials);
    0024 disp('Calculate all covariances...');
    0025 nt_whoss;
    0026 for iTrial=1:nTrials
    0027     C(:,:,:,iTrial)=nt_cov_lags(xx{iTrial}, yy{iTrial},shifts);
    0028 end
    0029
    0030 %%
    0031 % calculate leave-one-out CCAs
    0032 disp('Calculate CCAs...');
    0033 for iOut=1:nTrials
    0034     CC=sum(C(:,:,:,setdiff(1:nTrials,iOut)),4); % covariance of all trials except iOut
    0035     [A,B,R]=nt_cca([],[],[],CC,size(xx{1},2));  % corresponding CCA
    0036     AA{iOut}=A;
    0037     BB{iOut}=B;
    0038 end
    0039 clear C CC
    0040
    0041 %%
    0042 % calculate leave-one-out correlation coefficients
    0043 disp('Calculate cross-correlations...');
    0044 for iOut=1:nTrials
    0045     iNext=mod(iOut,nTrials)+1; % correlate with next in list
    0046     A=AA{iOut};
    0047     B=BB{iOut};
    0048     for iShift=1:length(shifts)
    0049         [x,y]=nt_relshift(xx{iOut},yy{iOut},shifts(iShift));
    0050         a=A(:,:,iShift);
    0051         b=B(:,:,iShift);
    0052         r(:,iShift)=diag( nt_normcol(x*a)' * nt_normcol(y*b )) / size(x,1);
    0053     end
    0054     RR(:,:,iOut)=r;
    0055     if doSurrogate
    0056         for iShift=1:length(shifts)
    0057             [x,y]=nt_relshift(xx{iOut},yy{iNext},shifts(iShift));
    0058             a=A(:,:,iShift);
    0059             b=B(:,:,iShift);
    0060             mn=min(size(x,1),size(y,1));
    0061             s(:,iShift)=diag( nt_normcol(x(1:mn,:)*a)' * nt_normcol(y(1:mn,:)*b )) / mn;
    0062         end
    0063         ss(:,:,iOut)=s;
    0064     end
    0065 end
    0066 if doSurrogate
    0067     VAR=(sum(ss.^2,3)-sum(ss,3).^2/nTrials) / (nTrials-1);
    0068     SD(:,:)=sqrt(VAR);
    0069 end
    0070 disp('done');
    0071
    0072 %%
    0073 % If no output arguments, plot something informative
    0074
    0075 if nargout==0
    0076     figure(1); clf;
    0077     if length(shifts)>1;
    0078         plot(mean(RR,3)'); title('correlation for each CC'); xlabel('shift'); ylabel('correlation');
    0079         hold on;
    0080         plot(SD', ':r');
    0081         legend('correlation','standard error'); legend boxoff
    0082     else
    0083         plot(squeeze(mean(RR,3))); title ('correlation for each CC'); xlabel('CC'); ylabel('correlation');
    0084         plot(SD', ':r');
    0085     end
    0086     figure(2); clf;
    0087     size(RR)
    0088     for k=1:min(4,size(RR,1))
    0089         subplot(2,2,k);
    0090         [~,idx]=max(mean(RR(k,:,:),3));
    0091         [x,y]=nt_relshift(xx{1},yy{1},shifts(idx));
    0092         plot([x*A(:,k,idx), y*B(:,k,idx)]);
    0093         disp(corr(nt_normcol([x*A(:,k,idx), y*B(:,k,idx)])));
    0094         title(['CC ',num2str(k)]); xlabel('sample');
    0095     end
    0096 end
    0097