-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoislindll.py
More file actions
366 lines (336 loc) · 12.3 KB
/
poislindll.py
File metadata and controls
366 lines (336 loc) · 12.3 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import numpy as np
import scipy.stats as st
import scipy.optimize as opt
import pandas as pd
def length(x=None):
try:
return len(x)
except:
if type(x) == float or type(x) == int or type(x) == np.int32 or type(x) == np.float64 or type(x) == np.float32 or type(x) == np.int64:
return 1
else:
return 0
def dpoislind(x, theta, log = False):
'''
Discrete Poisson-Lindley Distribution
Description
Density (mass) for the Poisson-Lindley distribution.
Usage
dpoislind(x, theta, log = False)
Parameters
----------
x: list
Vector of quantiles.
theta: float
The shape parameter, which must be greater than 0.
log: bool
Logical vectors. If True, then the probabilities are given as log(p).
Details
The Poisson-Lindley distribution has mass
p(x) = (θ^2(x + θ + 2))/(θ + 1)^(x+3),
where x=0,1,… and θ>0 is the shape parameter.
Returns
-------
dpoislind gives the density (mass) for the specified distribution.
References
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Ghitany, M. E. and Al-Mutairi, D. K. (2009), Estimation Methods for the
Discrete Poisson-Lindley Distribution, Journal of Statistical
Computation and Simulation, 79, 1–9.
Sankaran, M. (1970), The Discrete Poisson-Lindley Distribution, Biometrics,
26, 145–149.
Examples
--------
dpoislind(x=[-3,-2,-2,1,-1,0,1,1,1,2,2,3,1], theta = 0.5)
'''
if theta <= 0:
return "theta must be positive!"
if length(x) == 1:
x = [x]
x = np.array(x)
p = (theta**2*(x+theta+2)/(theta+1)**(x+3))*(x>=0)
if log:
p = log(p)
if not log:
p = np.minimum(np.maximum(p,0),1)
return p
def ppoislind(q, theta, lowertail = True, logp = False):
'''
Discrete Poisson-Lindley Distribution
Description
Distribution function for the Poisson-Lindley distribution.
Usage
ppoislind(q, theta, lowertail = True, logp = False)
Parameters
----------
q: list
Vector of quantiles.
theta: float
The shape parameter, which must be greater than 0.
logp: bool
Logical vectors. If True, then the probabilities are given as log(p).
lowertail: bool
Logical vector. If True, then probabilities are P[X≤ x], else P[X>x].
Details
The Poisson-Lindley distribution has mass
p(x) = (θ^2(x + θ + 2))/(θ + 1)^(x+3),
where x=0,1,… and θ>0 is the shape parameter.
Returns
-------
ppoislind gives the distribution function for the specified distribution.
References
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Ghitany, M. E. and Al-Mutairi, D. K. (2009), Estimation Methods for the
Discrete Poisson-Lindley Distribution, Journal of Statistical
Computation and Simulation, 79, 1–9.
Sankaran, M. (1970), The Discrete Poisson-Lindley Distribution, Biometrics,
26, 145–149.
Examples
--------
ppoislind(q = [-3,-2,-2,1,-1,0,1,1,1,2,2,3,1,-1], theta = 0.5,
lowertail=False)
'''
if theta <= 0:
return "theta must be positive!"
if length(q) == 1:
q = [q]
q = np.array(q)
ind = q<0
q = [int(np.floor(a)) for a in q]
temp = []
for i in range(length(q)):
temp.append(np.sum(dpoislind(x = range(q[i]+1),theta=theta,log=False)))
if length(temp) == 1:
temp = [temp]
temp = np.array(temp)
#temp[np.where(temp == 0)] = np.min(temp[np.where(temp != 0)])
if lowertail == False:
temp = 1-temp
if any(ind):
temp[ind] = 0 + 1 *(not lowertail)
if logp:
temp = np.log(temp)
if not logp:
temp = np.minimum(np.maximum(temp,0),1)
return temp
def qpoislind(p, theta, lowertail = True, logp = False):
'''
Discrete Poisson-Lindley Distribution
Description
Quantile function, and random for the Poisson-Lindley distribution.
Usage
qpoislind(p, theta, lowertail = True, logp = False)
Parameters
----------
p: list
Vector of probabilities.
theta: float
The shape parameter, which must be greater than 0.
logp: bool
Logical vectors. If True, then the probabilities are given as log(p).
lowertail: bool
Logical vector. If True, then probabilities are P[X≤ x], else P[X>x].
Details
The Poisson-Lindley distribution has mass
p(x) = (θ^2(x + θ + 2))/(θ + 1)^(x+3),
where x=0,1,… and θ>0 is the shape parameter.
Returns
-------
qpoislind gives the quantile function for the specified distribution.
References
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Ghitany, M. E. and Al-Mutairi, D. K. (2009), Estimation Methods for the
Discrete Poisson-Lindley Distribution, Journal of Statistical
Computation and Simulation, 79, 1–9.
Sankaran, M. (1970), The Discrete Poisson-Lindley Distribution, Biometrics,
26, 145–149.
Examples
--------
qpoislind(p = .2,theta = 0.5,lowertail=(False))
qpoislind(p = [0.80,.2,1,0,1.1,-1,-2,.5,.9999,.9,0,1,-1,0,.32,1,3], theta = 0.5,lowertail = True)
'''
if theta <= 0:
return "theta must be positive!"
if length(p) == 1:
p = [p]
p = np.array(p)
if logp:
p = np.exp(p)
if theta > 0.125:
up = 400
else:
up = 2000
if lowertail:
tmp = ppoislind(range(up+1),theta=theta)
allp = []
for i in range(length(p)):
allp.append(min(np.where(tmp>=p[i])))
allp = np.array([min(a) if length(a) > 0 else np.nan for a in allp])
if length(p) > 1:
allp[np.where(p == 1)] = np.inf
allp[np.where(p == 0)] = 0
allp[np.where(p > 1)] = np.nan
allp[np.where(p < 0)] = np.nan
else:
if(p == 1):
allp = [np.inf]
elif(p == 0):
allp = [0]
elif(p>1 or p<0):
allp = [np.nan]
else:
tmp = ppoislind(range(up+1),theta=theta,lowertail = False)
allp = []
for i in range(length(p)):
allp.append(np.maximum(max(np.where(tmp>p[i])),0)+1)
allp = np.array([max(a) if length(a) > 0 else 0.0 for a in allp])
if length(p) > 1:
if(up ==2000) and any(allp == 2000):
allp[np.where(allp==2000)] = np.inf
allp[np.where(p == 1)] = 0
allp[np.where(p == 0)] = np.inf
allp[np.where(p > 1)] = np.nan
allp[np.where(p < 0)] = np.nan
else:
if(up ==2000) and allp == 2000:
allp = np.inf
if(p == 1):
allp = [0]
elif(p == 0):
allp = [np.inf]
elif(p > 1 or p < 0):
allp = [np.nan]
if any(np.isnan(allp)):
print("Warning message:\n NaN(s) produced")
return allp
def rpoislind(n, theta):
'''
Discrete Poisson-Lindley Distribution
Description
Random generation for the Poisson-Lindley distribution.
Usage
rpoislind(n, theta)
Parameters
----------
n: int
The number of observations. If length>1, then the length of n is used
in place of n.
theta: float
The shape parameter, which must be greater than 0.
Details
The Poisson-Lindley distribution has mass
p(x) = (θ^2(x + θ + 2))/(θ + 1)^(x+3),
where x=0,1,… and θ>0 is the shape parameter.
Returns
-------
rpoislind generates random deviates for the specified distribution.
References
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Ghitany, M. E. and Al-Mutairi, D. K. (2009), Estimation Methods for the
Discrete Poisson-Lindley Distribution, Journal of Statistical
Computation and Simulation, 79, 1–9.
Sankaran, M. (1970), The Discrete Poisson-Lindley Distribution, Biometrics,
26, 145–149.
Examples
--------
rpoislind(n = 150, theta = 0.5)
rpoislind(n = [4,6], theta = 0.5)
'''
if theta <= 0:
return "theta must be positive!"
if length(n) > 1:
n = len(n)
u = st.uniform.rvs(size = n)
p = theta/(theta+1)
ind = u > p
lamb = st.expon.rvs(theta, size = n) + (st.expon.rvs(theta, size = n))*ind
out = st.poisson.rvs(lamb, size = n)
return out
def poislindll(x, theta = None):
'''
Maximum Likelihood Estimation for the Discrete Poisson-Lindley Distribution
Description
Performs maximum likelihood estimation for the parameter of the
Poisson-Lindley distribution.
Usage
poislindll(x, theta = None)
Parameters
----------
x: list
A vector of raw data which is distributed according to a
Poisson-Lindley distribution.
theta: float, optional
Optional starting value for the parameter. If None, then the method of
moments estimator is used.
Details
The discrete Poisson-Lindley distribution is a compound distribution that,
potentially, provides a better fit for count data relative to the
traditional Poisson and negative binomial distributions.
Returns
-------
poislindll returns a dataframe with items:
s:
The estimated coefficient value of theta.
vcov:
The variance-covariance matrix of the main parameters of a fitted
model object. In this case, it's the inverse of the hessian matrix.
References
----------
Derek S. Young (2010). tolerance: An R Package for Estimating Tolerance
Intervals. Journal of Statistical Software, 36(5), 1-39.
URL http://www.jstatsoft.org/v36/i05/.
Ghitany, M. E. and Al-Mutairi, D. K. (2009), Estimation Methods for
the Discrete Poisson-Lindley Distribution, Journal of Statistical
Computation and Simulation, 79, 1–9.
Sankaran, M. (1970), The Discrete Poisson-Lindley Distribution,
Biometrics, 26, 145–149.
Examples
--------
## Maximum likelihood estimation for randomly generated data from the
Poisson-Lindley distribution.
Pdata = [2,1,3,2,0,11,9,0,0,0]
poislindll(x = Pdata)
'''
x = pd.DataFrame(x)
x = pd.DataFrame(x.value_counts()).T
x = x.reindex(sorted(x.columns), axis=1)
N = list(x.iloc[0])
x = [list(x) for x in x.columns.values]
x = [x[0] for x in x]
N = np.array(N)
x = np.array(x)
Ntimesx = []
for i in range(length(N)):
Ntimesx.append(N[i]*x[i])
xbar = sum(Ntimesx)/sum(N)
if theta == None:
theta = (-(xbar-1)+np.sqrt((xbar-1)**2+8*xbar))/(2*xbar)
def llf(theta):
return -2*sum(N)*np.log(theta)-sum(N*(np.log(x+theta+2)-np.log(theta+1)*(x+3)))
try:
s = opt.minimize(llf, x0=theta, method = 'BFGS')['x']
vcov = opt.minimize(llf, x0=theta, method = 'BFGS')['hess_inv'].ravel()
fit = pd.DataFrame({'theta':s,'vcov':vcov})
except:
try:
s = opt.minimize(llf, x0=theta*0.8, method = 'BFGS')['x']
vcov = opt.minimize(llf, x0=theta*0.8, method = 'BFGS')['hess_inv'].ravel()
fit = pd.DataFrame({'theta':s,'vcov':vcov})
except:
return "Difficulty optimizing the MLE -- must try a different starting value for theta."
return fit
print(dpoislind(x=[-3,-2,-2,1,-1,0,1,1,1,2,2,3,1], theta = 0.5))
print(qpoislind(p = .2,theta = 0.5,lowertail=True))
print(qpoislind(p = [0.80,.2,1,0,1.1,-1,-2,.5,.9999,.9,0,1,-1,0,.32,1,3], theta = 0.5,lowertail = False))
print(rpoislind(n = 150, theta = 0.5))
print(rpoislind(n = [4,6], theta = 0.5))
Pdata = [2,1,3,2,0,11,9,0,0,0]
print(poislindll(x = Pdata))