forked from neurodebian/spm12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spm_BMS.m
271 lines (235 loc) · 8.28 KB
/
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
function [alpha,exp_r,xp,pxp,bor] = 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, Lionel Rigoux and J. Daunizeau
% $Id: spm_BMS.m 5842 2014-01-20 10:53:17Z 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
max_val = log(realmax('double'));
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
log_u(i,:) = log_u(i,:) - mean(log_u(i,:));
% prevent numerical problems for badly scaled posteriors
for k = 1:Nk,
log_u(i,k) = sign(log_u(i,k)) * min(max_val,abs(log_u(i,k)));
end
% exponentiate (to get back to non-log representation)
u(i,:) = exp(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
if Nk == 2
% comparison of 2 models
xp(1) = spm_Bcdf(0.5,alpha(2),alpha(1));
xp(2) = spm_Bcdf(0.5,alpha(1),alpha(2));
else
% comparison of >2 models: use sampling approach
xp = spm_dirichlet_exceedance(alpha,Nsamp);
end
else
xp = [];
end
% Compute Bayes Omnibus Risk - use functions from VBA toolbox
posterior.a=alpha;
posterior.r=g';
priors.a=alpha0;
F1 = FE(lme',posterior,priors); % Evidence of alternative
options.families=[];
F0 = FE_null(lme',options); % Evidence of null (equal model freqs)
% Implied by Eq 5 (see also p39) in Rigoux et al.
% See also, last equation in Appendix 2
bor=1/(1+exp(F1-F0));
% Compute protected exceedance probs - Eq 7 in Rigoux et al.
pxp=(1-bor)*xp+bor/Nk;
% Graphics output (currently for 2 models only)
%--------------------------------------------------------------------------
if do_plot && Nk == 2
% plot Dirichlet pdf
%----------------------------------------------------------------------
if alpha(1)<=alpha(2)
alpha_now =sort(alpha,1,'descend');
winner_inx=2;
else
alpha_now =alpha;
winner_inx=1;
end
x1 = [0:0.0001:1];
for i = 1:length(x1),
p(i) = spm_Dpdf([x1(i) 1-x1(i)],alpha_now);
end
fig1 = figure;
axes1 = axes('Parent',fig1,'FontSize',14);
plot(x1,p,'k','LineWidth',1);
% cumulative probability: p(r1>r2)
i = find(x1 >= 0.5);
hold on
fill([x1(i) fliplr(x1(i))],[i*0 fliplr(p(i))],[1 1 1]*.8)
v = axis;
plot([0.5 0.5],[v(3) v(4)],'k--','LineWidth',1.5);
xlim([0 1.05]);
xlabel(sprintf('r_%d',winner_inx),'FontSize',18);
ylabel(sprintf('p(r_%d|y)',winner_inx),'FontSize',18);
title(sprintf('p(r_%d>%1.1f | y) = %1.3f',winner_inx,0.5,xp(winner_inx)),'FontSize',18);
legend off
end
% Sampling approach ((currently implemented for 2 models only):
% plot F as a function of alpha_1
%--------------------------------------------------------------------------
if sampling
if Nk == 2
% Compute lower bound on F by sampling
%------------------------------------------------------------------
alpha_max = size(lme,1) + Nk*alpha0(1);
dx = 0.1;
a = [1:dx:alpha_max];
Na = length(a);
for i=1:Na,
alpha_s = [a(i),alpha_max-a(i)];
[F_samp(i),F_bound(i)] = spm_BMS_F(alpha_s,lme,alpha0);
end
if do_plot
% graphical display
%------------------------------------------------------------------
fig2 = figure;
axes2 = axes('Parent',fig2,'FontSize',14);
plot(a,F_samp,'Parent',axes2,'LineStyle','-','DisplayName','Sampling Approach',...
'Color',[0 0 0]);
hold on;
yy = ylim;
plot([alpha(1),alpha(1)],[yy(1),yy(2)],'Parent',axes2,'LineStyle','--',...
'DisplayName','Variational Bayes','Color',[0 0 0]);
legend2 = legend(axes2,'show');
set(legend2,'Position',[0.15 0.8 0.2 0.1],'FontSize',14);
xlabel('\alpha_1','FontSize',18);
ylabel('F','FontSize',18);
end
else
fprintf('\n%s\n','Verification of alpha estimates by sampling not available.')
fprintf('%s\n','This approach is currently only implemented for comparison of 2 models.');
end
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;
function [F0m,F0f] = FE_null(L,options)
% derives the free energy of the 'null' (H0: equal model frequencies)
% 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.17 in Rigoux et al.
[K,n] = size(L);
if ~isempty(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 ~isempty(options.families)
F0f = F0f + g(k).*(L(k,i)-log(g(k))+log(f0(k)));
end
end
end