forked from femisan/spDCM_task
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spm_BMS_F_smpl.m
64 lines (49 loc) · 1.59 KB
/
spm_BMS_F_smpl.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
function [s_samp,s_bound] = spm_BMS_F_smpl (alpha,lme,alpha0)
% Get sample and lower bound approx. for model evidence p(y|r)
% in group BMS; see spm_BMS_F.
%
% FORMAT [s_samp,s_bound] = spm_BMS_F_smpl (alpha,lme,alpha0)
%
% REFERENCE: See appendix in
% Stephan KE, Penny WD, Daunizeau J, Moran RJ, Friston KJ
% Bayesian Model Selection for Group Studies. NeuroImage (under review)
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Will Penny
% $Id: spm_BMS_F_smpl.m 2626 2009-01-20 16:30:08Z maria $
% prevent numerical problems
max_val = log(realmax('double'));
for i=1:size(lme,1),
lme(i,:) = lme(i,:) - mean(lme(i,:));
for k = 1:size(lme,2),
lme(i,k) = sign(lme(i,k)) * min(max_val,abs(lme(i,k)));
end
end
% Number of samples per alpha bin (0.1)
Nsamp = 1e3;
% Sample from univariate gamma densities then normalise
% (see Dirichlet entry in Wikipedia or Ferguson (1973) Ann. Stat. 1,
% 209-230)
Nk = length(alpha);
for k = 1:Nk,
alpha_samp(:,k) = spm_gamrnd(alpha(k),1,Nsamp,1);
end
Ni = size(lme,1);
for i = 1:Ni,
s_approx(i) = sum((alpha./sum(alpha)).*lme(i,:));
s(i) = 0;
for n = 1:Nsamp,
s(i) = s(i) + si_fun(alpha_samp(n,:),lme(i,:));
end
s(i) = s(i)/Nsamp;
end
s_bound = sum(s_approx);
s_samp = sum(s);
return
%=========================================================================
function [si] = si_fun (alpha,lme)
% Check a lower bound
% FORMAT [si] = si_fun (alpha,lme)
esi = sum((exp(lme).*alpha)/sum(alpha));
si = log(esi);
return