-
Notifications
You must be signed in to change notification settings - Fork 54
/
spm_MH_reml.m
73 lines (65 loc) · 1.95 KB
/
spm_MH_reml.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
66
67
68
69
70
71
72
73
function [F,P] = spm_MH_reml(YY,X,Q,N,hE);
% Estimation of covariance components from y*y' using sampling
% FORMAT [F,P] = spm_MH_reml(YY,X,Q,N,[hE]);
%
% YY - (m x m) sample covariance matrix Y*Y' {Y = (m x N) data matrix}
% X - (m x p) design matrix
% Q - {1 x q} covariance components
% N - number of samples
%
% hE - prior expectation: log-normal hyper-parameterisation (with hyperpriors)
%
% F - [-ve] free energy F = log evidence = p(Y|X,Q)
% P - smaple of hyperparameters from thier posterioir p(h|YY,X,Q)
%--------------------------------------------------------------------------
%
% This routiens using MCMC sampling (reverible Metropolis-Hastings)
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_MH_reml.m 5033 2012-11-02 20:59:54Z karl $
% assume a single sample if not specified
%--------------------------------------------------------------------------
try
N;
catch
N = 1;
end
% assume OPT = 0
%--------------------------------------------------------------------------
try
hE;
OPT = hE;
catch
OPT = 0;
end
% ortho-normalise X
%--------------------------------------------------------------------------
if isempty(X)
X = sparse(length(Q{1}),1);
else
X = orth(full(X));
end
% remove fixed effects
%--------------------------------------------------------------------------
n = length(Q{1});
m = length(Q);
h = zeros(m,1);
R = speye(n,n) - X*X';
YY = R*YY*R;
M.OPT = OPT;
M.Q = Q;
M.N = N;
% initialise and specify hyperpriors
%--------------------------------------------------------------------------
[C,h,Ph,Fr] = spm_reml(YY,X,Q,N,0,4,OPT);
if M.OPT
M.hE = h - 16;
M.hP = eye(m,m)/32;
else
M.hE = zeros(m,1);
M.hP = speye(m,m)/exp(32);
end
% sample
%--------------------------------------------------------------------------
[P,F] = spm_MH('spm_MH_reml_likelihood',h,YY,M);