-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweibull.py
More file actions
236 lines (172 loc) · 7.16 KB
/
weibull.py
File metadata and controls
236 lines (172 loc) · 7.16 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
from __future__ import division
import numpy as np
import seaborn as sns
from scipy.optimize import fmin
import matplotlib.pyplot as plt
from scipy.special import gammaln
eps = 2 ** -52
sns.set_style('ticks')
sns.set_context('talk')
def plotDataBernoulli(y, stim_strength, theta_init=[.1, 1]):
"""plotDataBernoulli(y, stim_strength, theta_init = [.1,1])
Fit data to a Weibull function using a Bernoulli LL formulation and plot it
----------------------------------------------------------------------------
y:
array-like (1d) containing a sequence of 0s (wrong) and 1s (correct)
stim_strength:
array-like (1d) containing a sequence of stim_strengtherences
corresponding to the y
theta_init:
list length 2 with initial estimates for alpha and beta
"""
theta = fitDataBernoulli(y, stim_strength, theta_init)
counter = 0
ustim_strength = np.sort(np.unique(stim_strength))
ustim_strength = ustim_strength[ustim_strength > 0]
prop_correct = np.zeros(len(ustim_strength))
for i in ustim_strength:
prop_correct[counter] = np.mean(y[stim_strength == i])
counter += 1
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(
ustim_strength, prop_correct, marker='o', s=50, color='k')
xs = np.linspace(np.min(ustim_strength) * .8,
np.max(ustim_strength) * 1.2, 100)
pred_y = calculateWeibullP(xs, theta[0], theta[1])
ax.add_line(plt.Line2D(xs, pred_y, color='black'))
ax.axvline(theta[0], color='black', linestyle='--', alpha=0.5)
ax.axhline(.816, color='black', linestyle='--', alpha=0.5)
ax.text(.1, .975, r'$\alpha$ = %.2f' %
theta[0], transform=ax.transAxes)
ax.text(.1, .925, r'$\beta$ = %.2f' %
theta[1], transform=ax.transAxes)
ax.set_xlim(np.min(ustim_strength) * .5, np.max(ustim_strength) * 1.1)
ax.set_xlim(.01, 1)
ax.set_ylim((.45, 1.05))
ax.set_xticks([10**np.arange(-3, 0)])
ax.set_xlabel('Stim strength')
ax.set_ylabel('Proportion correct')
# trim won't work with x log axis so just leave it
sns.despine(offset=5)
ax.set_xscale('log')
plt.tight_layout()
def plotDataBinomial(data):
"""plotDataBinomial(data, theta_init = [.1,1])
Fit data to a Weibull function using a Binomial LL formulation and plot it
---------------------------------------------------------------------------
data:
2d array, with n rows and 4 columns
1st column = stim_strengths
2nd column = proportion correct
3rd column = number of successes
4th column = total number of trials
theta_init:
list length 2 with initial estimates for alpha and beta
"""
theta = fitDataBinomial(data)
ustim_strength = data[:, 0]
prop_correct = data[:, 1]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(
ustim_strength, prop_correct, marker='o', s=50, color='k')
xs = np.linspace(np.min(ustim_strength) * .8,
np.max(ustim_strength) * 1.2, 100)
pred_y = calculateWeibullP(xs, theta[0], theta[1])
ax.add_line(plt.Line2D(xs, pred_y, color='black'))
ax.axvline(theta[0], color='black', linestyle='--', alpha=0.5)
ax.axhline(.816, color='black', linestyle='--', alpha=0.5)
ax.text(.1, .975, r'$\alpha$ = %.2f' %
theta[0], transform=ax.transAxes)
ax.text(.1, .925, r'$\beta$ = %.2f' %
theta[1], transform=ax.transAxes)
ax.set_xlim(np.min(ustim_strength) * .5, np.max(ustim_strength) * 1.1)
ax.set_xlim(.01, 1)
ax.set_ylim((.45, 1.05))
ax.set_xticks([10**np.arange(-3, 0)])
ax.set_xlabel('Stim strength')
ax.set_ylabel('Proportion correct')
sns.despine(offset=5, bottom=False)
# trim won't work with x log axis so just leave it
sns.despine(offset=5)
plt.tight_layout()
def fitDataBernoulli(y, stim_strength, theta_init=[.1, 1]):
"""fitDataBernoulli(y, stim_strength, theta_init = [.1,1])
Fits data to a Weibull function using a Bernoulli LL formulation
----------------------------------------------------------------------------
y:
array-like (1d) containing a sequence of 0s (wrong) and 1s (correct)
stim_strength:
array-like (1d) containing a sequence of stim_strengtherences
corresponding to the y
theta_init:
list length 2 with initial estimates for alpha and beta
"""
theta = fmin(
calculateWeibullNegLLBernoulli, theta_init,
args=(y, stim_strength), disp=True)
return(theta)
def fitDataBernoulliWithLapse(y, stim_strength, theta_init=[.1, 1, .05]):
"""fitDataBernoulliWithLapse(y, stim_strength, theta_init = [.1,1,.05])
Fits data to a Weibull function with lapse using a Bernoulli LL formulation
----------------------------------------------------------------------------
y:
array-like (1d) containing a sequence of 0s (wrong) and 1s (correct)
stim_strength:
array-like (1d) containing a sequence of stim_strengths
corresponding to the y
theta_init:
list length 3 with initial estimates for alpha, beta and lambda
"""
theta = fmin(
calculateWeibullNegLLBernoulliLapse, theta_init,
args=(y, stim_strength), disp=True)
return(theta)
def fitDataBinomial(data, theta_init=[.1, 1]):
"""fitDataBinomial(data, theta_init = [.1,1])
Fits data to a Weibull function using a Binomial LL formulation
---------------------------------------------------------------------------
data:
2d array, with n rows and 4 columns
1st column = stim_strengths
2nd column = proportion correct
3rd column = number of successes
4th column = total number of trials
theta_init:
list length 2 with initial estimates for alpha and beta
"""
theta = fmin(calculateWeibullNegLLBinomial, theta_init, args=(data,))
return(theta)
def calculateWeibullP(stim_strength, alpha, beta):
return(1 - .5 * np.exp(-(stim_strength / alpha) ** beta))
def calculateWeibullPLapse(stim_strength, alpha, beta, lambd):
return(.5 + (.5 - lambd) * (1 - np.exp(-(stim_strength / alpha) ** beta)))
def calculateWeibullNegLLBernoulli(theta, y, stim_strength):
alpha, beta = theta
LL = np.sum(
y * np.log(calculateWeibullP(stim_strength, alpha, beta)) +
(1 - y) * np.log(.5 * np.exp(-(stim_strength / alpha) ** beta)))
return(-LL)
def calculateWeibullNegLLBernoulliLapse(theta, y, stim_strength):
alpha, beta, lambd = theta
lambd = 1 - np.mean(y[stim_strength == .512])
LL = np.sum(
y * np.log(
calculateWeibullPLapse(stim_strength, alpha, beta, lambd)) +
(1 - y) * (np.log(
1 - (-lambd * (1 - np.exp(-(stim_strength / alpha) ** beta))))))
return(-LL)
def calculateWeibullNegLLBinomial(theta, data):
alpha, beta = theta
stim_strength = data[:, 0]
n1_observed = data[:, 2]
n_total = data[:, 3]
p_pred = calculateWeibullP(stim_strength, alpha, beta)
LL = np.sum(
gammaln(n_total + 1) -
gammaln(n1_observed + 1) -
gammaln(n_total - n1_observed + 1) +
n1_observed * np.log(p_pred + eps) +
(n_total - n1_observed) * np.log(1 - p_pred + eps))
return(-LL)