-
Notifications
You must be signed in to change notification settings - Fork 1
/
vdditer.py
178 lines (143 loc) · 4.41 KB
/
vdditer.py
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
from numba import njit
import numpy as np
import matplotlib.pyplot as plt
@njit
def simulate_activation(tau, noise, std, smoothing, scale, tau_threshold, *args):
act = 0.0
for i in range(len(tau)):
act = act*smoothing + np.arctan((tau[i] - tau_threshold)*scale) + noise[i]*std
yield act
@njit
def simulate_time(tau, noise, std, smoothing, scale, tau_threshold, act_threshold):
acts = simulate_activation(tau, noise, std, smoothing, scale, tau_threshold)
prev = 0.0
if prev > act_threshold:
return 0.0
for i, act in enumerate(acts):
if act < act_threshold:
prev = act
continue
t = (act_threshold - prev)/(act - prev)
return i + t
return np.nan
@njit
def simulate_times(tau, noise_bank, std, smoothing, scale, tau_threshold, act_threshold):
out = np.empty(len(noise_bank))
for i in range(len(noise_bank)):
out[i] = simulate_time(tau, noise_bank[i], std, smoothing, scale, tau_threshold, act_threshold)
return out
@njit
def stdnormpdf(x):
return np.exp(-x**2/2)/np.sqrt(2*np.pi)
@njit
def sample_lik(vals, sample, dt):
liks = np.empty_like(vals)
n = len(sample)
std = 0.1
bw = std*n**(-1/(1+4))
for i, val in enumerate(vals):
lik = 0.0
for s in sample:
# TODO: Handle non-responders
# TODO: Make a discrete PDF from the empirical CDF?
lik += stdnormpdf((s*dt - val)/bw)
liks[i] = lik/(n*bw)
return liks
from kwopt import minimizer, logbarrier, logitbarrier
def vdd_loss(trials, dt, N=1000):
taus, rts = zip(*trials)
hacktaus = []
for tau in taus:
hacktau = tau.copy()
hacktau[hacktau < 0] = 1e5
hacktaus.append(hacktau)
noises = [np.random.randn(N, len(tau)) for (tau, rts) in trials]
def loss(**kwargs):
lik = 0
for tau, rt, noise in zip(hacktaus, rts, noises):
sample = simulate_times(tau, noise, **kwargs)
lik += np.sum(np.log(sample_lik(rt, sample, dt)))
return -lik
return loss
def fit_vdd(trials, dt, N=1000, init=None):
if init is None:
init = dict(
std=1.0*np.sqrt(dt),
smoothing=0.5,
scale=1.0,
tau_threshold=4.0,
act_threshold=1.0
)
spec = dict(
std= (init['std'], logbarrier),
smoothing= (init['smoothing'], logitbarrier),
scale= (init['scale'], logbarrier),
tau_threshold= (init['tau_threshold'], logbarrier),
act_threshold= (init['act_threshold'], logbarrier)
)
loss = vdd_loss(trials, dt, N)
return minimizer(loss, method='nelder-mead')(**spec)
def gridtest():
def fittingtest():
N = 20
dt = 1/90
dur = 20
ts = np.arange(0, dur, dt)
param = dict(
std=1.0*np.sqrt(dt),
smoothing=0.5,
scale=1.0,
tau_threshold=2.0,
act_threshold=1.0
)
trials = []
for tau0 in (2.0, 3.0, 4.0, 5.0):
tau0 = 4.5
speed = 30.0
dist = tau0*speed - ts*speed
tau = dist/speed
np.random.seed(0)
noise_bank = np.random.randn(N, len(tau))
hacktau = tau.copy()
hacktau[hacktau < 0] = 1e5
sample = simulate_times(hacktau, noise_bank, **param)*dt
trials.append((tau, sample))
result = fit_vdd(trials, dt)
print(result)
#plt.hist(sample)
#plt.show()
def samplingtest():
N = 1000
dt = 1/90
dur = 20
ts = np.arange(0, dur, dt)
tau0 = 4.5
speed = 30.0
dist = tau0*speed - ts*speed
tau = dist/speed
np.random.seed(0)
noise_bank = np.random.randn(N, len(tau))
tau[tau < 0] = 4.5
param = dict(
std=1.0*np.sqrt(dt),
smoothing=0.5,
scale=1.0,
tau_threshold=4.0,
act_threshold=1.0
)
#print("Simulating")
sample = simulate_times(tau, noise_bank, **param)
#responders = np.isfinite(sample)
#print(np.sum(responders)/len(responders)*100)
#plt.hist(sample[responders]*dt, bins=100)
plt.hist(sample*dt, bins=100, density=True)
est = sample_lik(ts, sample, dt)
#from scipy.stats.kde import gaussian_kde
#est = gaussian_kde(sample, bw_method=0.1)(ts)
#plt.plot(ts, est, color='green')
#plt.twinx()
plt.plot(ts, est, color='red')
plt.show()
if __name__ == '__main__':
fittingtest()
#samplingtest()