-
Notifications
You must be signed in to change notification settings - Fork 5
/
spm_BMS.m
192 lines (168 loc) · 6.01 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
function [alpha,exp_r,xp] = spm_BMS(lme, Nsamp, do_plot, sampling, ecp, alpha0)
% Bayesian model selection for group studies
% FORMAT [alpha, exp_r, xp] = 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
%
% REFERENCE:
% Stephan KE, Penny WD, Daunizeau J, Moran RJ, Friston KJ (2009)
% Bayesian Model Selection for Group Studies. NeuroImage 46:1004-1017
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Klaas Enno Stephan & Will Penny
% $Id: spm_BMS.m 4999 2012-10-12 12:44:15Z vladimir $
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
% 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