-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_dcm_csd_Q.m
66 lines (61 loc) · 2.34 KB
/
spm_dcm_csd_Q.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
64
65
function Q = spm_dcm_csd_Q(csd)
% Precision of cross spectral density
% FORMAT Q = spm_dcm_csd_Q(csd)
%
% csd{i} - [cell] Array of complex cross spectra
% Q - normalised precision
%--------------------------------------------------------------------------
% This routine returns the precision of complex cross spectra based upon
% the asymptotic results described in Camba-Mendez & Kapetanios (2005):
% In particular, the scaled difference between the sample spectral density
% (g) and the predicted density (G);
%
% e = vec(g - G)
%
% is asymptotically complex normal, where the covariance between e(i,j) and
% e(u,v) is given by Q/h and:
%
% Q = G(i,u)*G(j,u): h = 2*m + 1
%
% Here m represent the number of averages from a very long time series.The
% inverse of the covariance is thus a scaled precision, where the
% hyperparameter (h) plays the role of the degrees of freedom (e.g., the
% number of averages comprising the estimate). In this routine, we use the
% sample spectral density to create a frequency specific precision matrix
% for the vectorised spectral densities - under the assumption that the
% former of this sample spectral density resembles the predicted spectral
% density (which will become increasingly plausible with convergence).
%
% Camba-Mendez, G., & Kapetanios, G. (2005). Estimating the Rank of the
% Spectral Density Matrix. Journal of Time Series Analysis, 26(1), 37-48.
% doi: 10.1111/j.1467-9892.2005.00389.x
%__________________________________________________________________________
% Copyright (C) 2018 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_dcm_csd_Q.m 7751 2019-12-06 11:59:09Z peter $
%-Check for cell arrays
%--------------------------------------------------------------------------
if iscell(csd)
CSD = spm_zeros(csd{1});
n = numel(csd);
for i = 1:n
CSD = CSD + csd{i};
end
Q = spm_dcm_csd_Q(CSD/n);
Q = kron(eye(n,n),Q);
return
end
%-Get precision
%--------------------------------------------------------------------------
SIZ = size(csd);
Qn = spm_length(csd);
Q = spalloc(Qn,Qn,SIZ(1)*prod(SIZ(2:end))^2);
[w,i,j] = ind2sub(SIZ,1:Qn);
for Qi = 1:Qn
for Qj = 1:Qn
if w(Qi) == w(Qj)
Q(Qi,Qj) = csd(w(Qi),i(Qi),i(Qj))*csd(w(Qi),j(Qi),j(Qj));
end
end
end
Q = inv(Q + norm(Q,1)*speye(size(Q))/32);