-
Notifications
You must be signed in to change notification settings - Fork 2
/
rsa_covarianceDist.m
63 lines (60 loc) · 1.94 KB
/
rsa_covarianceDist.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function [V,delta,ksi]=rsa_covarianceDist(partVec,condVec,varargin)
%function [V,delta,ksi]=covariance_dist(partVec,condVec)
%calculates the covariance of distance estimates V, together with the
%signal component (delta) and noise component (ksi)
%
% INPUT:
% - partVec: partition vector (K x 1)
% - condVec: condition vector (K x 1)
%
% OUTPUT:
% - V: covariance of distance estimates
% - delta: signal component
% - ksi: error component
%
% VARARGIN:
% - distType: 'crossval' or 'ncv' (whether distances are
% calculated with crossvalidation or not) - changes V
% formula
% - G: true second moment matrix
% - sigma: true noise matrix
% - data: beta estimates (K x P; K:cond/run, P:voxels)
% - nVox: number of voxels (if data not given, otherwise
% estimated from data)
%
distType = 'crossval'; % crossval or ncv
data = [];
G = [];
sigma = [];
nVox = [];
vararginoptions(varargin,{'distType','G','sigma','data','nVox'});
X = indicatorMatrix('identity_p',condVec); % per condition
C = indicatorMatrix('allpairs',unique(condVec)'); % condition pairs contrast
nPart = numel(unique(partVec));
switch distType % correct denominator in the V equation
case 'crossval'
den = nPart * (nPart-1);
case 'ncv'
den = nPart * nPart;
end
% if G not provided - estimate from data
if isempty(G)
% check if data given
if isempty(data)
error('Provide data or G matrix');
else
nVox = size(data,2);
U = pinv(X)*data; % calculate mean pattern across runs
G = U*U';
end
end
delta = C*G*C'; % signal component
% if sigma not given, estimate from data
if isempty(sigma)
sigma=0;
for i = 1:nPart
sigma = sigma + (data(partVec==i,:)-delta)*(data(partVec==i,:)-delta)'/(nPart-1)*nVox;
end
end
ksi = C*sigma*C'; % noise component
V = 4*(delta.*ksi)/nPart + 2*(ksi.^2)/den;