forked from aspenyoo/WM_resource_allocation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_single_nLL.m
159 lines (137 loc) · 5.89 KB
/
calc_single_nLL.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
function nLL = calc_single_nLL(model,Theta,data,exppriorityVec,fixparams)
%CALC_NLL calculates negative log-likelihood of parameters given data and
%model.
%
% NLL = CALC_NLL(MODEL, THETA, DATA, EXPPRIORITYVEC) calculates the
% negative likelihood of DATA given MODEL and THETA, for experimental
% probe probabilities EXPPRIORITYVEC.
%
% NLL = CALC_NLL(MODEL, THETA, DATA, EXPPRIORITYVEC, FIXPARAMS)
% calculates the negative likelihood, where THETA are free parameters
% and FIXPARAMS indicates which parameters are fixed and what value
% they are fixed to.
%
% ================= INPUT VARIABLES ======================
%
% MODEL / THETA:
% 'max_points', MAXIMIZING POINTS / [Jbar_total tau (alpha beta)]
% 'flexible', FLEXIBLE / [Jbar_total tau (alpha beta) p_high p_med]
% 'proportional', PROPORTIONAL / [Jbar_total tau (alpha beta)]
% 'min_error', MINIMIZING ERROR / [Jbar_total tau (alpha beta) gamma]
%
% parameter descriptions:
% JBAR_TOTAL: mean total amount of resources across priorities
% TAU: second parameter of gamma noise distribution
% ALPHA: risk preferences for post-decision wager
% BETA: inverse noise temperature on post-decision wager
% GAMMA: exponent for loss in Minimizing Error model
% P_HIGH: proportion allocated to high-priority stimulus
% P_MED: proportion allocated to medium-priority stimulus
%
% DATA: cell of length nPriorities. the ith element of DATA should be
% all data corresponding to EXPPRIORITYVEC(i) condition. the first
% column should contain the magnitude of errors and the second column,
% if available, should contain the corresponding circle wager radius
% size.
%
% FIXPARAMS: (optional). 2 x (number of fixed parameters) matrix. fixed
% parameters, such that the first row corresponds to the index and
% second row corresponds to the value of the fixed parameter.
%
% ================= OUTPUT VARIABLES ================
%
% NLL: negative log-likelihood
% ---------------------
% revised from Aspen H. Yoo, [email protected]
% ---------------------
if nargin < 5; fixparams = []; end
expnumber = size(data{1},2); % experiment number
% exponentiating appropriate parameters
logflag = loadconstraints(model,exppriorityVec,expnumber-1);
% if there are fixed parameters
if ~isempty(fixparams)
nParams = length(Theta) + size(fixparams,2);
nonfixedparamidx = 1:nParams;
nonfixedparamidx(fixparams(1,:)) = [];
temptheta = nan(1,nParams);
temptheta(nonfixedparamidx) = Theta;
temptheta(fixparams(1,:)) = fixparams(2,:);
Theta = temptheta;
end
Theta(logflag) = exp(Theta(logflag));
Jbar_total = Theta(1);
tau = Theta(2);
if (expnumber == 2); alpha = Theta(3); beta = Theta(4); end
% obtain vector of resource allocated
nPriorities = length(exppriorityVec);
switch model
case 'max_points' % maximizing points (exp 2 only)
pVec = calc_pVec_maxpoints(Theta,exppriorityVec);
case 'flexible' % flexible
pp = Theta(end-(nPriorities-2):end);
pVec = [pp 1-sum(pp)];
case 'proportional' % proportional
pVec = exppriorityVec;
case 'min_error' % minimizing error^\gamma
pVec = calc_pVec_minerror(Theta,exppriorityVec);
end
% loading vector of circle wager radii
[rVec] = loadvar('rVec'); % size: (1 x 500)
rVec = rVec(:); % size: (500 x 1)
if any(isinf(pVec))
nLL = Inf;
else
nLL = 0;
ipriority=0;
for ipriority = 1
Jbar = Jbar_total*pVec(ipriority); % Jbar for current priority condition
% clear varaibles used in previous priorities (necessary for code to run)
clear idx1 idx2 data_r_reshaped
% get subject data
if ipriority ==1
data_distance= data{1}(:,1);
else
sprint('error');
end
nTrials = length(data_distance);
% p(J|Jbar,tau)
[JVec] = loadvar('JVec',{Jbar,tau}); % values of J
nJs = length(JVec);
Jpdf = gampdf(JVec,Jbar/tau,tau); % probability of that J value
Jpdf = Jpdf./sum(Jpdf); % normalize
% p(Shat|S,J)
% Y = MVNPDF(X,MU,SIGMA) returns the density of the multivariate normal
% distribution with mean MU and covariance SIGMA, evaluated at each row
% of X.
Sigma = zeros(1,2,nJs*nTrials);
Sigma(1,:,:) = sort(repmat(sqrt(1./JVec(:)),nTrials,2),'descend')'; % SDs of diagonal matrix. sigmas in descending order --> J in ascending order
p_Shat = mvnpdf(repmat([data_distance(:) zeros(nTrials,1)],nJs,1),0,Sigma);
p_Shat = reshape(p_Shat,nTrials,nJs)'; % nJs x nTrials
p_Shat(p_Shat == 0) = 1e-10; % set to arbitrarily small value if zero
% ====== Exp 2: with disc size data ======
if (expnumber == 2)
data_r = data{ipriority}(:,2);
% p(rVec|J,beta) (a range of r to get entire probability dist)
pdf_r = calc_pdf_r(beta, JVec, alpha); % size: (nrs x nJs)
xdiff = diff(rVec(1:2));
p_r = nan(nJs,nTrials);
for iJ = 1:nJs
pdff = pdf_r(:,iJ);
for itrial = 1:nTrials
r = data_r(itrial);
idx1 = find((rVec- r) <= 0,1,'last');
idx2 = find((rVec- r) > 0,1,'first');
p_r(iJ,itrial) = (pdff(idx2)-pdff(idx1))/xdiff.*(r-rVec(idx1)) + pdff(idx1);
end
end
end
if (expnumber == 2) % if there is wager data
% \int p(Shat|S,J) p(r|J) p(J) dJ
pTrials = sum(bsxfun(@times,p_Shat.*p_r,Jpdf')); % 1 x nTrials
else
% \int p(Shat|S,J) p(J) dJ
pTrials = sum(bsxfun(@times,p_Shat,Jpdf')); % 1 x nTrials
end
nLL = nLL - sum(log(pTrials));
end
end