-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbm_spm_BMS.m
221 lines (192 loc) · 6.37 KB
/
cbm_spm_BMS.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
function [alpha,exp_r,xp,pxp,bor,g] = cbm_spm_BMS(lme, Nsamp, do_plot, sampling, ecp, alpha0)
% Bayesian model selection for group studies
% FORMAT [alpha,exp_r,xp,pxp,bor] = spm_BMS (lme, Nsamp, do_plot, sampling, ecp, alpha0)
%
% INPUT:
% lme - array of log model evidences
% rows: subjects
% columns: models (1..Nk)
% Nsamp - number of samples used to compute exceedance probabilities
% (default: 1e6)
% do_plot - 1 to plot p(r|y)
% sampling - use sampling to compute exact alpha
% ecp - 1 to compute exceedance probability
% alpha0 - [1 x Nk] vector of prior model counts
%
% OUTPUT:
% alpha - vector of model probabilities
% exp_r - expectation of the posterior p(r|y)
% xp - exceedance probabilities
% pxp - protected exceedance probabilities
% bor - Bayes Omnibus Risk (probability that model frequencies
% are equal)
%
% REFERENCES:
%
% Stephan KE, Penny WD, Daunizeau J, Moran RJ, Friston KJ (2009)
% Bayesian Model Selection for Group Studies. NeuroImage 46:1004-1017
%
% Rigoux, L, Stephan, KE, Friston, KJ and Daunizeau, J. (2014)
% Bayesian model selection for group studies—Revisited.
% NeuroImage 84:971-85. doi: 10.1016/j.neuroimage.2013.08.065
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Klaas Enno Stephan, Will Penny
% $Id: spm_BMS.m 6442 2015-05-21 09:13:44Z will $
if nargin < 2 || isempty(Nsamp)
Nsamp = 1e6;
end
if nargin < 3 || isempty(do_plot)
do_plot = 0;
end
if nargin < 4 || isempty(sampling)
sampling = 0;
end
if nargin < 5 || isempty(ecp)
ecp = 1;
end
Ni = size(lme,1); % number of subjects
Nk = size(lme,2); % number of models
c = 1;
cc = 10e-4;
% prior observations
%--------------------------------------------------------------------------
if nargin < 6 || isempty(alpha0)
alpha0 = ones(1,Nk);
end
alpha = alpha0;
% iterative VB estimation
%--------------------------------------------------------------------------
while c > cc,
% compute posterior belief g(i,k)=q(m_i=k|y_i) that model k generated
% the data for the i-th subject
for i = 1:Ni,
for k = 1:Nk,
% integrate out prior probabilities of models (in log space)
log_u(i,k) = lme(i,k) + psi(alpha(k))- psi(sum(alpha));
end
% exponentiate (to get back to non-log representation)
u(i,:) = exp(log_u(i,:)-max(log_u(i,:)));
% normalisation: sum across all models for i-th subject
u_i = sum(u(i,:));
g(i,:) = u(i,:)/u_i;
end
% expected number of subjects whose data we believe to have been
% generated by model k
for k = 1:Nk,
beta(k) = sum(g(:,k));
end
% update alpha
prev = alpha;
for k = 1:Nk,
alpha(k) = alpha0(k) + beta(k);
end
% convergence?
c = norm(alpha - prev);
end
% Compute expectation of the posterior p(r|y)
%--------------------------------------------------------------------------
exp_r = alpha./sum(alpha);
% Compute exceedance probabilities p(r_i>r_j)
%--------------------------------------------------------------------------
if ecp
xp = cbm_dirichlet_exceedance(alpha,Nsamp);
else
xp = [];
end
posterior.a=alpha;
posterior.r=g';
priors.a=alpha0;
bor = spm_BMS_bor (lme',posterior,priors);
% Compute protected exceedance probs - Eq 7 in Rigoux et al.
pxp=(1-bor)*xp+bor/Nk;
end
function [bor,F0,F1] = spm_BMS_bor(L,posterior,priors,C)
% Compute Bayes Omnibus Risk
% FORMAT [bor,F0,F1] = spm_BMS_bor(L,posterior,priors,C)
%
% L Log model evidence table (models x subjects)
% posterior .a model counts, .r model-subject probs
% priors .a model counts
% C if this field is specified then BOR under family prior
% is computed, otherwise BOR under model prior is computed.
% C(k,f) = 1 if model k belongs to family f (0 otherwise)
%
% REFERENCES:
%
% Rigoux, L, Stephan, KE, Friston, KJ and Daunizeau, J. (2014)
% Bayesian model selection for group studies - Revisited.
% NeuroImage 84:971-85. doi: 10.1016/j.neuroimage.2013.08.065
%__________________________________________________________________________
% Copyright (C) 2015 Wellcome Trust Centre for Neuroimaging
% Will Penny
% $Id: spm_BMS_bor.m 6444 2015-05-21 11:15:48Z guillaume $
if nargin < 4
options.families = 0;
% Evidence of null (equal model freqs)
F0 = FE_null(L,options);
else
options.families = 1;
options.C = C;
% Evidence of null (equal model freqs) under family prior
[~,F0] = FE_null(L,options);
end
% Evidence of alternative
F1 = FE(L,posterior,priors);
% Implied by Eq 5 (see also p39) in Rigoux et al.
% See also, last equation in Appendix 2
bor = 1/(1+exp(F1-F0));
end
function [F,ELJ,Sqf,Sqm] = FE(L,posterior,priors)
% derives the free energy for the current approximate posterior
% This routine has been copied from the VBA_groupBMC function
% of the VBA toolbox http://code.google.com/p/mbb-vb-toolbox/
% and was written by Lionel Rigoux and J. Daunizeau
%
% See equation A.20 in Rigoux et al. (should be F1 on LHS)
[K,n] = size(L);
a0 = sum(posterior.a);
Elogr = psi(posterior.a) - psi(sum(posterior.a));
Sqf = sum(gammaln(posterior.a)) - gammaln(a0) - sum((posterior.a-1).*Elogr);
Sqm = 0;
for i=1:n
Sqm = Sqm - sum(posterior.r(:,i).*log(posterior.r(:,i)+eps));
end
ELJ = gammaln(sum(priors.a)) - sum(gammaln(priors.a)) + sum((priors.a-1).*Elogr);
for i=1:n
for k=1:K
ELJ = ELJ + posterior.r(k,i).*(Elogr(k)+L(k,i));
end
end
F = ELJ + Sqf + Sqm;
end
function [F0m,F0f] = FE_null (L,options)
% Free energy of the 'null' (H0: equal frequencies)
%
% F0m Evidence for null (ie. equal probs) over models
% F0f Evidence for null (ie. equal probs) over families
%
% This routine derives from the VBA_groupBMC function
% of the VBA toolbox http://code.google.com/p/mbb-vb-toolbox/
% written by Lionel Rigoux and J. Daunizeau
%
% See Equation A.17 in Rigoux et al.
[K,n] = size(L);
if options.families
f0 = options.C*sum(options.C,1)'.^-1/size(options.C,2);
F0f = 0;
else
F0f = [];
end
F0m = 0;
for i=1:n
tmp = L(:,i) - max(L(:,i));
g = exp(tmp)./sum(exp(tmp));
for k=1:K
F0m = F0m + g(k).*(L(k,i)-log(K)-log(g(k)+eps));
if options.families
F0f = F0f + g(k).*(L(k,i)-log(g(k)+eps)+log(f0(k)));
end
end
end
end