forked from AlexanderFengler/nn_likelihoods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddm_data_simulation.py
More file actions
349 lines (295 loc) · 14.7 KB
/
ddm_data_simulation.py
File metadata and controls
349 lines (295 loc) · 14.7 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Functions for DDM data simulation
import numpy as np
import pandas as pd
import time
import inspect
# Simulate (rt, choice) tuples from: SIMPLE DDM -----------------------------------------------
# Simplest algorithm
def ddm_simulate(v = 0, # drift by timestep 'delta_t'
a = 1, # boundary separation
w = 0.5, # between -1 and 1
s = 1, # noise sigma
delta_t = 0.001, # timesteps fraction of seconds
max_t = 20, # maximum rt allowed
n_samples = 20000, # number of samples considered
print_info = True # timesteps fraction of seconds
):
rts = np.zeros((n_samples, 1))
choices = np.zeros((n_samples, 1))
delta_t_sqrt = np.sqrt(delta_t)
for n in range(0, n_samples, 1):
y = w*a
t = 0
while y <= a and y >= 0 and t <= max_t:
y += v * delta_t + delta_t_sqrt * np.random.normal(loc = 0,
scale = s,
size = 1)
t += delta_t
# Store choice and reaction time
rts[n] = t
# Note that for purposes of consistency with Navarro and Fuss, the choice corresponding the lower barrier is +1, higher barrier is -1
choices[n] = (-1) * np.sign(y)
if print_info == True:
if n % 1000 == 0:
print(n, ' datapoints sampled')
print('finished:', {'v': v,
'a': a,
'w': w,
's': s,
'delta_t': delta_t,
'max_t': max_t,
'n_samples': n_samples,
'simulator': 'ddm',
'boundary_fun_type': 'constant'})
return (rts, choices, {'v': v,
'a': a,
'w': w,
's': s,
'delta_t': delta_t,
'max_t': max_t,
'n_samples': n_samples,
'simulator': 'ddm',
'boundary_fun_type': 'constant',
'possible_choices': [-1, 1]})
# -----------------------------------------------------------------------------------------------
# Simulate (rt, choice) tuples from: DDM WITH ARBITRARY BOUNDARY --------------------------------
# For the flexbound (and in fact for all other dd)
# We expect the boundary function to have the following general shape:
# 1. An initial separation (using the a parameter) with lower bound 0
# (this allows interpretation of the w parameter as usual)
# 2. No touching of boundaries
# 3. It return upper and lower bounds for every t as a list [upper, lower]
def ddm_flexbound_simulate(v = 0,
a = 1,
w = 0.5,
s = 1,
delta_t = 0.001,
max_t = 20,
n_samples = 20000,
print_info = True,
boundary_fun = None, # function of t (and potentially other parameters) that takes in (t, *args)
boundary_multiplicative = True,
boundary_params = {'p1': 0, 'p2':0}
):
# Initializations
#print({'boundary_fun': boundary_fun})
rts = np.zeros((n_samples,1)) #rt storage
choices = np.zeros((n_samples,1)) # choice storage
delta_t_sqrt = np.sqrt(delta_t) # correct scalar so we can use standard normal samples for the brownian motion
# Boundary storage:
boundaries = np.zeros(((int((max_t/delta_t) + 1), 2)))
b_tmp = 0.0
if boundary_multiplicative:
for i in range(0, int((max_t/delta_t) + 1), 1):
b_tmp = a * boundary_fun(t = i * delta_t, **boundary_params)
if b_tmp > 0:
boundaries[i, 1] = b_tmp
boundaries[i, 0] = - boundaries[i, 1]
else:
for i in range(0, int((max_t/delta_t) + 1), 1):
b_tmp = a + boundary_fun(t = i * delta_t, **boundary_params)
if b_tmp > 0:
boundaries[i, 1] = b_tmp
boundaries[i, 0] = - boundaries[i, 1]
#print(boundaries.shape)
# Outer loop over n - number of samples
for n in range(0, n_samples, 1):
# initialize y, t, and time_counter
y = boundaries[0, 0] + (w * (boundaries[0, 1] - boundaries[0, 0]))
t = 0
cnt = 0
# Inner loop (trajection simulation)
while y <= boundaries[cnt, 1] and y >= boundaries[cnt, 0] and t < max_t:
# Increment y position (particle position)
y += v * delta_t + delta_t_sqrt * np.random.normal(loc = 0,
scale = s,
size = 1)
# Increment time
t += delta_t
# increment count
cnt += 1
# Store choice and reaction time
rts[n] = t
# Note that for purposes of consistency with Navarro and Fuss, the choice corresponding the lower barrier is +1, higher barrier is -1
# This is kind of a legacy issue at this point (plan is to flip this around, after appropriately reformulating navarro fuss wfpd function)
choices[n] = np.sign(y)
if print_info == True:
if n % 1000 == 0:
print(n, ' datapoints sampled')
return (rts, choices, {'v': v,
'a': a,
'w': w,
's': s,
**boundary_params,
'delta_t': delta_t,
'max_t': max_t,
'n_samples': n_samples,
'simulator': 'ddm_flexbound',
'boundary_fun_type': boundary_fun.__name__,
'possible_choices': [-1, 1]})
# -----------------------------------------------------------------------------------------------
# return ({'rts': rts,
# 'choices': choices,
# 'process_params': {'v': v, 'a': a, 'w': w, 's': s},
# 'boundary_params': {**boundary_params},
# 'simulator_params': {'delta_t': delta_t,
# 'max_t': max_t,
# 'n_samples': n_samples,
# 'simulator': 'ddm_flexbound',
# 'possible_choices': [-1, 1],
# 'boundary_fun_type': boundary_fun.__name__},
# 'simulation_stats': {'choice_proportions': [len(choices[choices == -1]) / len(choices), len(choices[choices == 1]) / len(choices)]})
# Simulate (rt, choice) tuples from: RACE MODEL WITH N SAMPLES ----------------------------------
def race_model(v = [0, 0, 0], # np.array expected in fact, one column of floats
w = [0, 0, 0], # np.array expected in fact, one column of floats
s = [1, 1, 1], # np.array expected in fact, one column of floats
delta_t = 0.001,
max_t = 20,
n_samples = 2000,
print_info = True,
boundary_fun = None,
**boundary_params):
# Initializations
n_particles = len(v)
rts = np.zeros((n_samples, 1))
choices = np.zeros((n_samples, 1))
delta_t_sqrt = np.sqrt(delta_t)
particles = np.zeros((n_particles, 1))
# We just care about an upper boundary here: (more complicated things possible)
boundaries = np.zeros((int(max_t / delta_t), 1))
for i in range(0, int(max_t / delta_t), 1):
boundaries[i] = boundary_fun(t = i * delta_t, **boundary_params)
for n in range(0, n_samples, 1):
# initialize y, t and time_counter
particles = w * boundaries[0]
t = 0
cnt = 0
while np.less_equal(particles, boundaries[cnt]).all() and t <= max_t:
particles += (v * delta_t) + (delta_t_sqrt * np.random.normal(loc = 0, scale = s, size = (n_particles, 1)))
t += delta_t
cnt += 1
rts[n] = t
choices[n] = particles.argmax()
if print_info == True:
if n % 1000 == 0:
print(n, ' datapoints sampled')
return (rts, choices)
# -------------------------------------------------------------------------------------------------
# Simulate (rt, choice) tuples from: Ornstein-Uhlenbeck -------------------------------------------
def ornstein_uhlenbeck(v = 1, # drift parameter
a = 1, # boundary separation parameter
w = 0.5, # starting point bias
g = 0.1, # decay parameter
s = 1, # standard deviation
delta_t = 0.001, # size of timestamp
max_t = 20, # maximal time in trial
n_samples = 2000, # number of samples from process
print_info = True): # whether or not ot print periodic update on number of samples generated
# Initializations
rts = np.zeros((n_samples, 1))
choices = np.zeros((n_samples, 1))
delta_t_sqrt = np.sqrt(delta_t)
for n in range(0, n_samples, 1):
y = w*a
t = 0
while y <= a and y >= 0 and t <= max_t:
y += ((v * delta_t) - (delta_t * g * y)) + delta_t_sqrt * np.random.normal(loc = 0,
scale = s,
size = 1)
t += delta_t
# Store choice and reaction time
rts[n] = t
# Note that for purposes of consistency with Navarro and Fuss, the choice corresponding the lower barrier is +1, higher barrier is -1
choices[n] = (-1) * np.sign(y)
if print_info == True:
if n % 1000 == 0:
print(n, ' datapoints sampled')
return (rts, choices)
# -------------------------------------------------------------------------------------------------
# Simulate (rt, choice) tuples from: Onstein-Uhlenbeck with flexible bounds -----------------------
def ornstein_uhlenbeck_flexbnd(v = 0, # drift parameter
w = 0.5, # starting point bias
g = 0.1, # decay parameter
s = 1, # standard deviation
delta_t = 0.001, # size of timestep
max_t = 20, # maximal time in trial
n_samples = 20000, # number of samples from process
print_info = True, # whether or not to print periodic update on number of samples generated
boundary_fun = None, # function of t (and potentially other parameters) that takes in (t, *args)
**boundary_params):
# Initializations
rts = np.zeros((n_samples,1)) # rt storage
choices = np.zeros((n_samples,1)) # choice storage
delta_t_sqrt = np.sqrt(delta_t) # correct scalar so we can use standard normal samples for the brownian motion
# Boundary storage:
boundaries = np.zeros(((int(max_t/delta_t), 2)))
for i in range(0, int(max_t/delta_t), 1):
boundaries[i, :] = boundary_fun(t = i * delta_t, **boundary_params)
# Outer loop over n - number of samples
for n in range(0, n_samples, 1):
# initialize y, t, and time_counter
y = boundaries[0, 0] + (w * (boundaries[0, 1] - boundaries[0, 0]))
t = 0
cnt = 0
# Inner loop (trajection simulation)
while y <= boundaries[cnt, 1] and y >= boundaries[cnt, 0] and t <= max_t:
# Increment y position (particle position)
y += ((v * delta_t) - (delta_t * g * y)) + delta_t_sqrt * np.random.normal(loc = 0,
scale = s,
size = 1)
# Increment time
t += delta_t
# increment count
cnt += 1
# Store choice and reaction time
rts[n] = t
# Note that for purposes of consistency with Navarro and Fuss, the choice corresponding the lower barrier is +1, higher barrier is -1
# This is kind of a legacy issue at this point (plan is to flip this around, after appropriately reformulating navarro fuss wfpd function)
choices[n] = (-1) * np.sign(y)
if print_info == True:
if n % 1000 == 0:
print(n, ' datapoints sampled')
return (rts, choices)
# --------------------------------------------------------------------------------------------------
# Simulate (rt, choice) tuples from: Leaky Competing Accumulator Model -----------------------------
def lca(v = [0, 0, 0], # drift parameters (np.array expect: one column of floats)
w = [0, 0, 0], # initial bias parameters (np.array expect: one column of floats)
a = 1, # criterion height
g = 0, # decay parameter
b = 1, # inhibition parameter
s = 1, # variance (can be one value or np.array of size as v and w)
delta_t = 0.001, # time-step size in simulator
max_t = 20, # maximal time
n_samples = 2000, # number of samples to produce
print_info = True): # whether or not to periodically report the number of samples generated thus far
# Initializations
n_particles = len(v)
rts = np.zeros((n_samples, 1))
choices = np.zeros((n_samples, 1))
delta_t_sqrt = np.sqrt(delta_t)
particles = np.zeros((n_particles, 1))
for n in range(0, n_samples, 1):
# initialize y, t and time_counter
particles_reduced_sum = particles
particles = w * a
t = 0
while np.less_equal(particles, a).all() and t <= max_t:
particles_reduced_sum[:,] = - particles + np.sum(particles)
particles += ((v - (g * particles) - (b * particles_reduced_sum)) * delta_t) + \
(delta_t_sqrt * np.random.normal(loc = 0, scale = s, size = (n_particles, 1)))
particles = np.maximum(particles, 0.0)
t += delta_t
rts[n] = t
choices[n] = particles.argmax()
if print_info == True:
if n % 1000 == 0:
print(n, ' datapoints sampled')
return (rts, choices)
# --------------------------------------------------------------------------------------------------
if __name__ == "__main__":
start_time = time.time()
#ddm_simulate(n_samples = 1000)
#print("--- %s seconds ---" % (time.time() - start_time))
#start_time = time.time()
#ddm_simulate_fast(n_samples = 1000)
#print("--- %s seconds ---" % (time.time() - start_time))