-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCap_inversion.m
More file actions
217 lines (184 loc) · 7.88 KB
/
Copy pathCap_inversion.m
File metadata and controls
217 lines (184 loc) · 7.88 KB
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
%% Model inversion script used to estimate model parameters
function [DCM] = Cap_inversion(DCM)
% MDP inversion using Variational Bayes
% FORMAT [DCM] = spm_dcm_mdp(DCM)
% If simulating - comment out section on line 196
% If not simulating - specify subject data file in this section
%
% Expects:
%--------------------------------------------------------------------------
% DCM.MDP % MDP structure specifying a generative model
% DCM.field % parameter (field) names to optimise
% DCM.U % cell array of outcomes (stimuli)
% DCM.Y % cell array of responses (action)
%
% Returns:
%--------------------------------------------------------------------------
% DCM.M % generative model (DCM)
% DCM.Ep % Conditional means (structure)
% DCM.Cp % Conditional covariances
% DCM.F % (negative) Free-energy bound on log evidence
%
% This routine inverts (cell arrays of) trials specified in terms of the
% stimuli or outcomes and subsequent choices or responses. It first
% computes the prior expectations (and covariances) of the free parameters
% specified by DCM.field. These parameters are log scaling parameters that
% are applied to the fields of DCM.MDP.
%
% If there is no learning implicit in multi-trial games, only unique trials
% (as specified by the stimuli), are used to generate (subjective)
% posteriors over choice or action. Otherwise, all trials are used in the
% order specified. The ensuing posterior probabilities over choices are
% used with the specified choices or actions to evaluate their log
% probability. This is used to optimise the MDP (hyper) parameters in
% DCM.field using variational Laplace (with numerical evaluation of the
% curvature).
%
%__________________________________________________________________________
% Copyright (C) 2005 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_dcm_mdp.m 7120 2017-06-20 11:30:30Z spm $
% OPTIONS
%--------------------------------------------------------------------------
ALL = false;
% specify prior expectations and covariance
%--------------------------------------------------------------------------
prior_variance = 2^-2;
for i = 1:length(DCM.field)
field = DCM.field{i};
try
param = DCM.MDP.(field);
param = double(~~param);
catch
param = 1;
end
if ALL
pE.(field) = zeros(size(param));
pC{i,i} = diag(param);
else
if strcmp(field,'IP')
pE.(field) = log(0.95/(1-0.95)); % in logit-space - bounded between 0 and 1
pC{i,i} = prior_variance;
elseif strcmp(field,'pV')
pE.(field) = log(0.5/(1-0.5)); % in logit-space - bounded between 0 and 1
pC{i,i} = prior_variance;
elseif strcmp(field,'etaA')
pE.(field) = log(0.5/(1-0.5)); % in logit-space - bounded between 0 and 1
pC{i,i} = prior_variance;
elseif strcmp(field,'etaAV')
pE.(field) = log(0.5/(1-0.5)); % in logit-space - bounded between 0 and 1
pC{i,i} = prior_variance;
elseif strcmp(field,'etaANV')
pE.(field) = log(0.5/(1-0.5)); % in logit-space - bounded between 0 and 1
pC{i,i} = prior_variance;
elseif strcmp(field,'etaB')
pE.(field) = log(0.5/(1-0.5)); % in logit-space - bounded between 0 and 1
pC{i,i} = prior_variance;
elseif strcmp(field,'etaBV')
pE.(field) = log(0.5/(1-0.5)); % in logit-space - bounded between 0 and 1
pC{i,i} = prior_variance;
elseif strcmp(field,'etaBNV')
pE.(field) = log(0.5/(1-0.5)); % in logit-space - bounded between 0 and 1
pC{i,i} = prior_variance;
elseif strcmp(field,'IPdiff')
pE.(field) = log(0.2/(1-0.2)); % in logit-space - bounded between 0 and 1
pC{i,i} = prior_variance;
else
pE.(field) = 0;
pC{i,i} = prior_variance;
end
end
end
pC = spm_cat(pC);
% model specification
%--------------------------------------------------------------------------
M.L = @(P,M,U,Y)spm_mdp_L(P,M,U,Y); % log-likelihood function
M.pE = pE; % prior means (parameters)
M.pC = pC; % prior variance (parameters)
M.mdp = DCM.MDP; % MDP structure
% SMT -------------+
M.noprint = true;
M.nograph = true;
% end SMT ---------+
% Variational Laplace
%--------------------------------------------------------------------------
[Ep,Cp,F] = spm_nlsi_Newton(M,DCM.U,DCM.Y);
% Store posterior densities and log evidnce (free energy)
%--------------------------------------------------------------------------
DCM.M = M;
DCM.Ep = Ep;
DCM.Cp = Cp;
DCM.F = F;
return
function L = spm_mdp_L(P,M,U,Y)
% log-likelihood function
% FORMAT L = spm_mdp_L(P,M,U,Y)
% P - parameter structure
% M - generative model
% U - inputs
% Y - observed repsonses
%__________________________________________________________________________
if ~isstruct(P); P = spm_unvec(P,M.pE); end
% multiply parameters in MDP
%--------------------------------------------------------------------------
mdp = M.mdp;
field = fieldnames(M.pE);
for i = 1:length(field)
if strcmp(field{i},'IP')
mdp.(field{i}) = 1/(1+exp(-P.(field{i})));
elseif strcmp(field{i},'pV')
mdp.(field{i}) = 1/(1+exp(-P.(field{i})));
elseif strcmp(field{i},'etaA')
mdp.(field{i}) = 1/(1+exp(-P.(field{i})));
elseif strcmp(field{i},'etaAV')
mdp.(field{i}) = 1/(1+exp(-P.(field{i})));
elseif strcmp(field{i},'etaANV')
mdp.(field{i}) = 1/(1+exp(-P.(field{i})));
elseif strcmp(field{i},'etaB')
mdp.(field{i}) = 1/(1+exp(-P.(field{i})));
elseif strcmp(field{i},'etaBV')
mdp.(field{i}) = 1/(1+exp(-P.(field{i})));
elseif strcmp(field{i},'etaBNV')
mdp.(field{i}) = 1/(1+exp(-P.(field{i})));
elseif strcmp(field{i},'IPdiff')
mdp.(field{i}) = 1/(1+exp(-P.(field{i})));
else
mdp.(field{i}) = exp(P.(field{i}));
end
end
% discern whether learning is enabled - and identify unique trials if not
%--------------------------------------------------------------------------
if any(ismember(fieldnames(mdp),{'a','b','d','c','d','e'}))
j = 1:numel(U);
k = 1:numel(U);
else
% find unique trials (up until the last outcome)
%----------------------------------------------------------------------
u = spm_cat(U');
[i,j,k] = unique(u(:,1:(end - 1)),'rows');
end
L = 0;
mdp_temp = Cap_gen_mdp(mdp, mdp.fit_options);
[MDP(1:mdp.TpB)] = deal(mdp_temp);
for idx_trial = 1:size(MDP,2)
MDP(idx_trial).o = [U{1}(:,:,idx_trial)];
MDP(idx_trial).u = [Y{1}(:,:,idx_trial)];
end
for i = 1:mdp.TpB
MDP(i).action = mdp(1).action(:,:,i);
% if normal trial and using pVdiff
if mdp(1).block(:, :, i) == 4 && mdp.fit_options.IPdiff_on == 1
MDP(i).A{1}(:,:) = [1 0 0; % start
0 (mdp.IP - mdp.IPdiff) ((1-mdp.IP) + mdp.IPdiff); % no vib
0 ((1-mdp.IP) + mdp.IPdiff) (mdp.IP - mdp.IPdiff)]; % vib
end
end
% solve MDP and accumulate log-likelihood
%--------------------------------------------------------------------------
MDP = spm_MDP_VB_X_Cap(MDP);
for j = 1:mdp.TpB
L = L + log(MDP(j).X{1,1}(MDP(j).action,2) + eps);
end
clear('MDP')
% end
fprintf('LL: %f \n',L)