-
Notifications
You must be signed in to change notification settings - Fork 1
/
analytical_TD.m
328 lines (259 loc) · 8.11 KB
/
analytical_TD.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
function [V_tr1, V_tr2, pre_RPE_tr1, pre_RPE_tr2, post_RPE_tr1, post_RPE_tr2] = analytical_TD(x, do_plot, distr, distr_params, d_dist, frac_pr_tr1, gamma, speed)
% plot stuff for TD model
%
% x(1) = mean rew dist
% x(2) = std rew dist
% x(3) = mean ITI
% x(4) = fraction track 2 (probe and non-probe)
% x(5) = fraction probe (of track 2)
% x(6) = (optional) min rew dist
% x(7) = (optional) max rew dist
% TODO dedupe w/ analytical_optimal
n_tr1 = 1 - x(4); % fraction of track 1 trials
n_tr2 = x(4); % fraction of track 2 trials
frac_pr = x(5); % fraction probe
n_tr2_npr = x(4) * (1 - x(5)); % fraction of track 2 non-probe trials
n_tr2_pr = x(4) * x(5); % fraction of track 2 probe trials
meanITI = x(3);
mu = x(1); % mean of rew dist
sigma = x(2); % std of rew dist
if length(x) <= 5
min_dist = 20;
max_dist = 500;
else
min_dist = x(6);
max_dist = x(7);
end
if ~exist('gamma', 'var')
gamma = 0.95; % TD discount rate
end
if ~exist('speed', 'var')
speed = 5; % AU per second
end
if ~exist('distr', 'var')
distr = 'norm'; % what kind of reward distribution to use
end
if ~exist('distr_params', 'var')
distr_params = [];
end
if ~exist('d_dist', 'var')
d_dist = 10; % accuracy of numerical approximation TODO this matters a lot for the magnitude of the hazard RPEs; must investigate
end
if ~exist('frac_pr_tr1', 'var')
frac_pr_tr1 = 0.01; % assume almost 100%
end
[pdf, cdf, rnd, mea] = get_distr(distr, min_dist, mu, max_dist, sigma, distr_params);
d = 1:d_dist:max(1000, max_dist); % distances
f = pdf(d); % track 1 reward distance PDF = P(rew at d) = track 2 non-probe PDF
F = cdf(d); % track 1 reward distance CDF = P(rew before d) = track 2 non-probe CDF
% track 1 TD value = Q(d0, RUN) = E[ r_t gamma^(t - t0) ]
% = integral f(d | no rew by d0) * gamma^((d - d0)/speed) * r * d_d
%
for i = 1:length(d)
% P(rew at d | no rew by d_i)
% note difference from hazard
%f_cond = f(i:end) ./ (1 - F(i));
f_cond = f(i:end) * (1 - frac_pr_tr1) ./ (1 - F(i));
g = gamma .^ ((d(i:end) - d(i)) / speed);
rew = 1;
V_tr1(i) = sum(f_cond .* g .* rew .* d_dist);
end
V_tr1(V_tr1 > 1) = nanmax(V_tr1); % TODO hack b/c of numerical approximation, values towards the tail get distorted
V_tr1(isnan(V_tr1)) = nanmax(V_tr1); % TODO hack for tail of distr
% track 2 TD value
% same idea as track 1 except accounting for frac_pr
%
for i = 1:length(d)
% P(rew at d | no rew by d_i)
% note difference from hazard; this is (almost) a proper PDF, with the exception of the frac_pr missing probability mass
f_cond = f(i:end) * (1 - frac_pr) ./ (1 - F(i));
% note: to make model-based and consistent w/ belief state, just do:
%f_cond = f(i:end) * (1 - frac_pr) ./ (1 - F(i) * (1 - frac_pr));
% TODO look into it
g = gamma .^ ((d(i:end) - d(i)) / speed);
rew = 1;
V_tr2(i) = sum(f_cond .* g .* rew .* d_dist);
end
V_tr2(V_tr2 > 1) = nanmax(V_tr2); % TODO hack b/c of numerical approximation, values towards the tail get distorted
V_tr2(isnan(V_tr2)) = nanmax(V_tr2); % TODO hack for tail of distr
% TODO dedupe w/ analytical_hazard
% post-reward RPEs
post_RPE_tr1 = 1 - V_tr1;
post_RPE_tr2 = 1 - V_tr2;
% pre-reward RPEs
pre_RPE_tr1 = V_tr1(2:end) * gamma^(d_dist / speed) - V_tr1(1:end-1);
pre_RPE_tr1 = [V_tr1(1) pre_RPE_tr1];
pre_RPE_tr2 = V_tr2(2:end) * gamma^(d_dist / speed) - V_tr2(1:end-1);
pre_RPE_tr2 = [V_tr2(1) pre_RPE_tr2];
if do_plot
figure; % for debugging
subplot(5,2,1);
plot(d, f, 'linewidth', 2);
xlabel('distance');
ylabel('probability density');
title('Reward location PDF, track 1');
ylim([0 0.05]);
xlim([1 100]);
subplot(5,2,2);
plot(d, f * (1 - frac_pr), 'linewidth', 2);
xlabel('distance');
ylabel('probability density');
title('Reward location PDF, track 2');
ylim([0 0.05]);
xlim([1 100]);
subplot(5,2,3);
plot(d, F);
xlabel('distance');
ylabel('cumulative density');
title('Reward location CDF, track 1');
xlim([1 100]);
subplot(5,2,4);
plot(d, F * (1 - frac_pr));
xlabel('distance');
ylabel('cumulative density');
title('Reward location CDF, track 2');
xlim([1 100]);
subplot(5,2,5);
plot(d, V_tr1);
title('Value, track 1');
xlabel('distance');
ylabel('h');
xlim([1 100]);
subplot(5,2,6);
plot(d, V_tr2);
title('Value, track 2');
xlabel('distance');
ylabel('h');
xlim([1 100]);
subplot(5,2,7);
plot(d, post_RPE_tr1);
title('Post-reward RPE, track 1');
xlabel('distance');
ylabel('1 - h');
xlim([1 100]);
subplot(5,2,8);
plot(d, post_RPE_tr2);
title('Post-reward RPE, track 2');
xlabel('distance');
ylabel('1 - h');
xlim([1 100]);
subplot(5,2,9);
plot(d, pre_RPE_tr1);
title('Pre-reward RPE, track 1');
xlabel('distance');
ylabel('h''');
xlim([1 100]);
subplot(5,2,10);
plot(d, pre_RPE_tr2);
title('Pre-reward RPE, track 2');
xlabel('distance');
ylabel('h''');
xlim([1 100]);
figure('pos', [1067 346 560 291]); % for Nao
subplot(4,2,1);
plot(d, f);
xlabel('distance');
ylabel('probability density');
title('Reward location PDF, track 1');
xlim([1 400]);
ylim([0 max(f)*1.2]);
subplot(4,2,2);
plot(d, f * (1 - frac_pr));
xlabel('distance');
ylabel('probability density');
title('Reward location PDF, track 2');
xlim([1 400]);
ylim([0 max(f)*1.2]);
subplot(4,2,3);
plot(d, F);
xlabel('distance');
ylabel('cumulative density');
title('Reward location CDF, track 1');
xlim([1 400]);
ylim([0 1]);
subplot(4,2,4);
plot(d, F * (1 - frac_pr));
xlabel('distance');
ylabel('cumulative density');
title('Reward location CDF, track 2');
xlim([1 400]);
ylim([0 1]);
subplot(2,2,5-4);
plot(d, V_tr1);
title('Value, track 1');
xlabel('distance');
ylabel('Q');
xlim([1 400]);
ylim([0 1]);
subplot(2,2,6-4);
plot(d, V_tr2);
title('Value, track 2');
xlabel('distance');
ylabel('Q');
xlim([1 400]);
ylim([0 1]);
%{
subplot(2,2,7-4);
title('RPE, track 1');
hold on;
plot(d, pre_RPE_tr1, 'color', [0 0 0]);
assert(d_dist == 10, 'sorry it''s hardcoded; below too');
for i = 5:2:30
plot([d(i-1) d(i) d(i+1)], [pre_RPE_tr1(i-1) post_RPE_tr1(i) pre_RPE_tr1(i+1)], 'color', [1-(i-1)/29 (i-1)/29 1]);
end
xlabel('distance');
ylabel('RPE');
xlim([1 400]);
ylim([-0.2 1]);
subplot(2,2,8-4);
title('RPE, track 2');
hold on;
plot(d, pre_RPE_tr2, 'color', [0 0 0]);
for i = 5:2:30
plot([d(i-1) d(i) d(i+1)], [pre_RPE_tr2(i-1) post_RPE_tr2(i) pre_RPE_tr2(i+1)], 'color', [1-(i-1)/29 (i-1)/29 1]);
end
xlabel('distance');
ylabel('RPE');
xlim([1 400]);
ylim([-0.2 1]);
%}
mtit('TD', 'fontsize',16,'color',[0 0 0], 'xoff',-.02,'yoff',.015);
figure('pos', [108 420 560 420]);
cmap = [0.5 0.5 1; ...
1 0.5 0; ...
0.5 1 0];
subplot(2,2,1);
colormap(cmap);
hold on;
plot(d, V_tr1, 'color', cmap(1,:), 'linewidth', 2);
plot(d, V_tr2, 'color', cmap(2,:), 'linewidth', 2);
title('Value');
xlabel('distance');
ylabel('Q(stay)');
legend({'track 1', 'track 2'});
xlim([1 max_dist * 1.2]);
%ylim([-0.1 1]);
subplot(2,2,3);
colormap(cmap);
hold on;
plot(d, pre_RPE_tr1, 'color', cmap(1,:), 'linewidth', 2);
plot(d, pre_RPE_tr2, 'color', cmap(2,:), 'linewidth', 2);
title('pre-reward RPE');
xlabel('distance');
ylabel('RPE');
legend({'track 1', 'track 2'});
xlim([2 max_dist * 1.2]);
%ylim([-0.1 1]);
subplot(2,2,4);
colormap(cmap);
hold on;
plot(d, post_RPE_tr1, 'color', cmap(1,:), 'linewidth', 2);
plot(d, post_RPE_tr2, 'color', cmap(2,:), 'linewidth', 2);
title('post-reward RPE');
xlabel('distance');
ylabel('RPE');
legend({'track 1', 'track 2'});
xlim([2 max_dist * 1.2]);
%ylim([-0.1 1]);
mtit('CSC-TD', 'fontsize',16,'color',[0 0 0], 'xoff',-.02,'yoff',.015);
end