forked from anne-urai/2019_Urai_choice-history-ddm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schematic.m
273 lines (216 loc) · 8.01 KB
/
schematic.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
function schematic(seed)
% Code to fit the history-dependent drift diffusion models as described in
% Urai AE, de Gee JW, Tsetsos K, Donner TH (2019) Choice history biases subsequent evidence accumulation. eLife, in press.
%
% MIT License
% Copyright (c) Anne Urai, 2019
% anne.urai@gmail.com
close all; clc;
global colors
% colors = [8 141 165; 141 165 8; 150 150 150] ./ 256;
pairedcols = cbrewer('qual', 'Paired', 8);
fz = 5; timefz = 5;
set(groot, 'defaultaxesfontsize', fz, 'defaultaxestitlefontsizemultiplier', 1);
%% random seed will determine how the drifting timecourse looks
if ~exist('seed', 'var'), seed = 100:120; end
disp(seed);
% use code by Peter Murphy to compute RT distributions
addpath('analyticalDDM/DDM');
addpath(genpath('~/code/Tools'));
tmax = 0.7;
%% ===================================== %%
% make 3 sets of RT distributions
% without any bias
% ===================================== %%
pm = [0.02 0 0.1 0.001 0.05]; % standard parameters
[gC_nobias,gE_nobias,ts] = fpt_regular_DDM(pm, tmax);
plot(ts, gC_nobias, ts, gE_nobias);
% biased drift towards option 1
pm(1) = pm(1) * 10;
[gC_dc,gE_dc,ts] = fpt_regular_DDM(pm, tmax);
plot(ts, gC_dc, ts, gE_dc);
% biased starting point towards option 1
pm = [0.02 0 0.1 0.001 0.05];
pm(5) = pm(5) * 1.4;
[gC_z,gE_z,ts] = fpt_regular_DDM(pm, tmax);
plot(ts, gC_z, ts, gE_z);
%% set parameters
cfg.timestep = 0.005; % 100 ms
cfg.time = cfg.timestep:cfg.timestep:tmax;
cfg.a = 1; % bound with, z = 0
cfg.cdW = 0.1; % variance of normally distributed noise
cfg.v = 2.5*cfg.timestep; % drift rate per timestep
cfg.z = 0; % drift rate per timestep
defcfg = cfg;
%% ===================================== %%
% make an overview of the two biasing mechanisms in the DDM
% ===================================== %%
close all; subplot(431); hold on;
for s = 1:length(seed)
cfg.seed = s;
y1 = ddm(cfg);
% plot the drift on top
plot(cfg.time, y1, 'color', [0.5 0.5 0.5], 'linewidth', 0.2);
end
cfg.v = cfg.v+1*cfg.timestep;
for s = 1:length(seed)
cfg.seed = s+100;
y1 = ddm(cfg);
% plot the drift on top
plot(cfg.time, y1, 'color', pairedcols(1, :), 'linewidth', 0.2);
end
% show the unbiased average drift towards two stimuli
cfg.v = 2.5*cfg.timestep;
cfg.cdW = 0;
y = ddm(cfg);
plot(cfg.time, y,'k');
cfg.v = -cfg.v; % flip around drift rate
y = ddm(cfg);
plot(cfg.time, y,'k');
% now with drift criterion bias
cfg.dc = 1*cfg.timestep;
cfg.v = cfg.v+cfg.dc;
y = ddm(cfg);
plot(cfg.time, y, 'color', pairedcols(2, :));
cfg.v = -cfg.v + 2*cfg.dc; % flip around drift rate
y = ddm(cfg);
plot(cfg.time, y,'k', 'color', pairedcols(2, :));
% add distributions at the top!
scaling = 300;
plot(ts, scaling*gC_nobias + cfg.a, 'k');
meanmedianmode(ts, scaling*gC_nobias + cfg.a, 'k');
plot(ts, scaling*gC_dc + cfg.a, 'color', pairedcols(2, :));
meanmedianmode(ts, scaling*gC_dc + cfg.a, pairedcols(2, :));
plot(ts, -scaling*gE_nobias - cfg.a, 'k');
meanmedianmode(ts, -scaling*gE_nobias - cfg.a, 'k');
plot(ts, -scaling*gE_dc - cfg.a, 'color', pairedcols(2, :));
meanmedianmode(ts, -scaling*gE_dc - cfg.a, pairedcols(2, :));
%% layout
%ylim([-cfg.a cfg.a]);
axis tight;
set(gca, 'ytick', [-cfg.a cfg.z cfg.a], 'yticklabel', {'0', 'z', 'a'});
text(0.83*max(cfg.time), -0.2, 'Time', 'fontsize', timefz);
%title({'History shift' 'in drift bias'}, 'color', colors(2, :));
%title({'Drift bias (v_{bias})'}, 'color', colors(2, :));
arrow([cfg.time(1) cfg.z ], [cfg.time(end) cfg.z], 'linestyle', ':', 'linewidth', 0.5, 'length', 4, 'TipAngle', 45);
% add two axes manually
plot([cfg.time(1) cfg.time(end)], [cfg.a cfg.a], 'k', 'linewidth', 0.5);
plot([cfg.time(1) cfg.time(end)], [-cfg.a -cfg.a], 'k', 'linewidth', 0.5);
box off;
ax = gca;
addlistener(ax, 'MarkedClean', @(obj,event)resetVertex(ax));
set(ax, 'xcolor', 'w', 'xtick',[]);
xlim([min(cfg.time) max(cfg.time)]);
tightfig;
print(gcf, '-dpdf', sprintf('~/Data/serialHDDM/DDMschematic_driftbias.pdf'));
%% ===================================== %%
% now change in starting point
% ===================================== %%
cfg = defcfg;
close all; subplot(431); hold on;
for s = 1:length(seed)
cfg.seed = s;
y1 = ddm(cfg);
% plot the drift on top
plot(cfg.time, y1, 'color', [0.5 0.5 0.5], 'linewidth', 0.2);
end
cfg.z = 0.4;
for s = 1:length(seed)
cfg.seed = s+100;
y1 = ddm(cfg);
% plot the drift on top
plot(cfg.time, y1, 'color', pairedcols(3, :), 'linewidth', 0.2);
end
% show the unbiased average drift towards two stimuli
cfg.cdW = 0;
cfg.z = 0;
y = ddm(cfg);
plot(cfg.time, y,'k');
cfg.v = -cfg.v; % flip around drift rate
y = ddm(cfg);
plot(cfg.time, y,'k');
% now with drift criterion bias
cfg.z = 0.4;
y = ddm(cfg);
plot(cfg.time, y, 'color', pairedcols(4, :));
cfg.v = -cfg.v;
y = ddm(cfg);
plot(cfg.time, y,'k', 'color', pairedcols(4, :));
% drifting particle
%plot(cfg.time, y1, 'color', [0.5 0.5 0.5]);
% add distributions at the top!
%plot(ts, scaling*gC_dc + cfg.a, 'color', colors(2, :), 'linestyle', '-.');
%plot(ts, -scaling*gE_dc - cfg.a, 'color', colors(2, :), 'linestyle', '-.');
plot(ts, scaling*gC_nobias + cfg.a, 'k');
meanmedianmode(ts, scaling*gC_nobias + cfg.a, 'k');
plot(ts, scaling*gC_z + cfg.a, 'color', pairedcols(4, :));
meanmedianmode(ts, scaling*gC_z + cfg.a, pairedcols(4, :));
plot(ts, -scaling*gE_nobias - cfg.a, 'k');
meanmedianmode(ts, -scaling*gE_nobias - cfg.a, 'k');
plot(ts, -scaling*gE_z - cfg.a, 'color', pairedcols(4, :));
meanmedianmode(ts, -scaling*gE_z - cfg.a, pairedcols(4, :));
% layout
%ylim([-cfg.a cfg.a]);
axis tight;
set(gca, 'ytick', [-cfg.a 0 cfg.a], 'yticklabel', {'0', 'z', 'a'});
text(0.83*max(cfg.time), -0.2, 'Time', 'fontsize', timefz);
%title({'History shift' 'in starting point'}, 'color', colors(1, :));
plot([cfg.time(1) cfg.time(end)], [cfg.a cfg.a], 'k', 'linewidth', 0.5);
plot([cfg.time(1) cfg.time(end)], [-cfg.a -cfg.a], 'k', 'linewidth', 0.5);
%title({'Starting point (z)'}, 'color', colors(1, :));
arrow([cfg.time(1) 0], [cfg.time(end) 0], 'linestyle', ':', 'linewidth', 0.5, 'length', 4, 'TipAngle', 45);
box off;
ax = gca;
addlistener(ax, 'MarkedClean', @(obj,event)resetVertex(ax));
set(ax, 'xcolor', 'w', 'xtick',[]);
xlim([min(cfg.time) max(cfg.time)]);
tightfig;
print(gcf, '-dpdf', sprintf('~/Data/serialHDDM/DDMschematic_startingpoint.pdf'));
%% now add the equations!
%
% subplot(335);
% xoffset = 0.02;
% text(xoffset, 1.15, 'dy = (s\cdotv+\bf{v_{bias}}\rm{)dt + cdW}', 'fontsize', fz);
% text(xoffset, 1, 'y(0) = z = a/2', 'fontsize', fz);
% axis off;
%
% subplot(334);
% text(xoffset, 1.15, 'dy = s\cdotv\cdotdt + cdW', 'fontsize', fz);
% text(xoffset, 1, 'y(0) = z = a/2 + \bf{z_{bias}}', 'fontsize', fz);
% axis off;
% save
% offsetAxes;
end
function y = ddm(cfg)
% generate a sample of evidence for each moment in time
evidence = cfg.v * ones(size(cfg.time));
% add noise
rng(cfg.seed); % make sure this is the same each time
noise = randn(size(evidence)) .* cfg.cdW;
noise(1) = cfg.z; % start with no noise
noisyevidence = evidence + noise;
integratedevidence = cumsum(noisyevidence);
y = integratedevidence;
firstPassage = min([find(y > cfg.a, 1, 'first') find(y < -cfg.a, 1, 'first')]);
y(firstPassage+1:end) = NaN;
%y(y > cfg.a) = NaN;
%y(y < -cfg.a) = NaN;
end
function resetVertex ( ax )
% extract the x axis vertext data
% repeat for Y (set 2nd row)
ax.YRuler.Axle.VertexData(2,1) = min(get(ax, 'Ytick'));
ax.YRuler.Axle.VertexData(2,2) = max(get(ax, 'Ytick'));
end
function meanmedianmode(x,y, col)
% from cumulative distribution function compute mean, median and mode
%
% plot(x(dsearchn(y', mean(y))), y(dsearchn(y', mean(y))), 'o', 'markerfacecolor', col, 'markeredgecolor', 'w', 'markersize', 4);
% plot(x(dsearchn(y', median(y))), y(dsearchn(y', median(y))), 's', 'markerfacecolor', col, 'markeredgecolor', 'w', 'markersize', 4);
% mode
if mean(y) > 0,
plot(x(dsearchn(y', max(y))), y(dsearchn(y', max(y))), 'o', 'markerfacecolor', col, 'markeredgecolor', col, 'markersize', 1);
else
plot(x(dsearchn(y', min(y))), y(dsearchn(y', min(y))), 'o', 'markerfacecolor', col, 'markeredgecolor', col, 'markersize', 1);
end
end