-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacings.py
512 lines (490 loc) · 20.3 KB
/
spacings.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 20 17:30:45 2017
@author: martin
"""
import numpy as np
import scipy as sp
import scipy.optimize
from scipy.interpolate import interp1d
import sympy
import unittest
import mpmath as mp
mp.extraprec(50)
class interp1d_mpmath(interp1d):
def _prepare_x(self, x):
"""Reshape input x array to 1-D"""
x = sp._lib._util._asarray_validated(x, check_finite=False, as_inexact=True,
objects_ok=True)
x_shape = x.shape
return x.ravel(), x_shape
class LinearCombinationUniformSpacings():
r"""
Random variable representing a linear combination of the spacings obtained
from a uniform distributions
Given :math:`n-1` draws :math:`X_i` from a uniform distributions. Let
:math:`X_{(1)}\leq X_{(2)}\leq\ldots\leq X_{(n-1)}` be the order statistics.
The spacings are defined as:
.. math::
S_i=X_{(i)}-X_{(i-1)},
where :math:`i=1\ldots n` and :math:`X_{n}=1` by convention.
This class represents the distribution of
.. math::
Z=\sum\limits_{i=1}^n a_i S_i.
The class support three backends:
* numpy: for fast computation
* mpmath: for accurate compuation
* sympy: to support the case where some the :math:`a_i` are equal.
The moments can be derived from a flat Dirichlet distribution and are given
by:
* Mean: :math:`\operatorname{E}Z=\frac{1}{n}\sum\limits_{i=1}^n a_i`
* Variance: :math:`\operatorname{Var}Z=\frac{n-1}{n^2(n+1)}\sum\limits_{i=1}^n a_i^2-\frac{2}{n^2(n+1)}\sum\limits_{i\neq j} a_ia_j`
* General moments can be computed analytically, but are currently not implemented
References
----------
[1] Huffer, Lin (2006) - Linear combinations of spacings
"""
def __init__(self, a,
r=None,
backend='numpy'):
"""
Parameters
----------
a : numpy array
(unique) coefficients of the linear combination
r : numpy array (default None)
indicates occurances of each entry of a, only supported for sympy backend
backend : string
numpy (default) or mpmath for higher precision
"""
assert len(np.unique(a))==len(a)
self.a=np.asanyarray(a, dtype=np.float64)
self._backend=backend
assert self._backend in ['numpy', 'mpmath', 'sympy']
self.r=None
if r is not None:
assert backend=='sympy'
self.r=np.asanyarray(r).astype(np.int)
assert np.all(self.r>0)
if self.r is None and backend=='sympy':
self.r=np.array([1 for aa in self.a], dtype=np.int)
self._proda_jni_cache_numpy=None
self._proda_jni_cache_mpmath=None
self._icdf_grid_x=None
self._icdf_grid_y=None
self._icdf_interpolator=None
self._symbolic_pdf_cache=None
self._symbolic_sf_cache=None
self._t_symb=None
@property
def N(self):
return len(self.a) if self.r is None else self.r.sum()
@property
def _a_full(self):
return self.a if self.r is None else np.repeat(self.a,self.r)
@property
def mean(self):
return np.mean(self._a_full)
@property
def var(self):
A=-1*np.ones((self.N,self.N))
np.fill_diagonal(A,self.N-1)
return np.dot(self._a_full, A.dot(self._a_full))/self.N**2/(self.N+1)
@property
def std(self):
return np.sqrt(self.var)
def rvs(self, size=1, random_state=None):
"""
Returns a random sample
"""
return sp.stats.dirichlet.rvs(np.ones(self.N),
size=size,
random_state=random_state).dot(self._a_full)
@property
def _proda_jni(self):
if self._backend == 'numpy':
return self._proda_jni_numpy
elif self._backend == 'mpmath':
return self._proda_jni_mpmath
else:
raise Exception("Invalid backend")
@property
def _proda_jni_numpy(self):
if self._proda_jni_cache_numpy is None:
idx=np.arange(self.N)
self._proda_jni_cache_numpy =[ np.prod(ai-self.a[idx!=i]) for i, ai in enumerate(self.a)]
return self._proda_jni_cache_numpy
@property
def _proda_jni_mpmath(self):
if self._proda_jni_cache_mpmath is None:
idx=np.arange(self.N)
self._proda_jni_cache_mpmath =np.array([ mp.fprod(ai-self.a[idx!=i]) for i, ai in enumerate(self.a)])
return self._proda_jni_cache_mpmath
@property
def _t(self):
if self._t_symb is None:
self._t_symb= sympy.symbols('t')
return self._t_symb
@property
def _symbolic_sf(self):
if self._symbolic_sf_cache is None:
#define some stuff
ais = [sympy.Symbol('a%d'%i) for i in range(len(self.a))]
gamma = sympy.symbols('gamma')
g=sympy.Max(gamma-self._t,0)**(self.N-1)
#symbolic computation
prod=1
for i, ai in enumerate(ais):
prod *= (self._t-ai)**self.r[i]
f = [g.subs(gamma,ai) for i, ai in enumerate(ais)]
for i,ai in enumerate(ais):
f[i]*=((self._t-ai)**self.r[i]/prod).subs(self._t,ai)
f[i]=f[i].diff(ai,self.r[i]-1)
f[i]=f[i].subs(zip(ais,self.a))/sympy.factorial(self.r[i]-1)
self._symbolic_sf_cache=sum(f)
return self._symbolic_sf_cache
@property
def _symbolic_pdf(self):
if self._symbolic_pdf_cache is None:
self._symbolic_pdf_cache=-self._symbolic_sf.diff(self._t)
return self._symbolic_pdf_cache
def pdf(self, t, **kwargs):
"""
Density function
"""
t=np.asanyarray(t)
if t.ndim !=0:
return np.array([self.pdf(yy, **kwargs) for yy in t])
if self._backend == 'numpy':
return self._pdf_numpy(t.item(), **kwargs)
elif self._backend=='mpmath':
return self._pdf_mpmath(t.item(), **kwargs)
elif self._backend=='sympy':
return self._pdf_sympy(t.item(), **kwargs)
else:
raise Exception("Invalid backend")
def _pdf_numpy(self,t):
if t<min(self.a) or t>max(self.a) or\
np.isclose(t, min(self.a), rtol=0) or\
np.isclose(t, max(self.a), rtol=0):
return 0
y=(self.N-1)*np.power(np.maximum(self.a-t,0), self.N-2)
y=y/self._proda_jni
return y.sum()
def _pdf_mpmath(self,t, output='float'):
if output=='float':
return np.float64(self._pdf_mpmath(t,output='mpf'))
if t<min(self.a) or t>max(self.a) or\
mp.almosteq(t, min(self.a), 1e-8) or\
mp.almosteq(t, max(self.a), 1e-8):
return mp.mpf(0)
y=[(self.N-1)*mp.power(x,self.N-2) for x in np.maximum(self.a-t,0)]
y=[y1/y2 for y1, y2 in zip(y, self._proda_jni_mpmath)]
return mp.fsum(y)
def _pdf_sympy(self,t):
if t<min(self.a) or t>max(self.a) or\
np.isclose(t, min(self.a), rtol=0) or\
np.isclose(t, max(self.a), rtol=0):
return 0
return np.float64(self._symbolic_pdf.subs(self._t,t))
def sf(self,t, **kwargs):
"""
Survival function
Paramters
---------
output : string (default)
only used for mpmath, indicates if floats or mpmath floats are returned
"""
t=np.asanyarray(t)
if t.ndim !=0:
return np.array([self.sf(yy, **kwargs) for yy in t])
if self._backend == 'numpy':
return self._sf_numpy(t.item(), **kwargs)
elif self._backend=='mpmath':
return self._sf_mpmath(t.item(), **kwargs)
elif self._backend=='sympy':
return self._sf_sympy(t.item(), **kwargs)
else:
raise Exception("Invalid backend")
def _sf_numpy(self,t):
if t<min(self.a) or np.isclose(t,min(self.a), rtol=0):
return 1
elif t>max(self.a) or np.isclose(t,max(self.a), rtol=0):
return 0
else:
y=np.power(np.maximum(self.a-t,0), self.N-1)/self._proda_jni_numpy
return y.sum()
def _sf_mpmath(self,t, output='float'):
if output=='float':
return np.float64(self._sf_mpmath(t,output='mpf'))
if t<min(self.a) or mp.almosteq(t, min(self.a), 1e-8):
return mp.mpf(1)
elif t>max(self.a) or mp.almosteq(t, max(self.a), 1e-8):
return mp.mpf(0)
else:
y=[mp.power(max(yy,0),self.N-1) for yy in self.a-t]
y=[y1/y2 for y1, y2 in zip(y, self._proda_jni_mpmath)]
return mp.fsum(y)
def _sf_sympy(self,t, output='float'):
if output=='float':
return np.float64(self._sf_sympy(t, output='mpf'))
if t<min(self.a) or mp.almosteq(t,min(self.a),1e-8):
return mp.mpf(1)
elif t>max(self.a) or mp.almosteq(t,max(self.a),1e-8):
return mp.mpf(0)
else:
return self._symbolic_sf.subs(self._t,t)
def cdf(self,t, **kwargs):
"""
Cumulative density function
Paramters
---------
output : string (default)
only used for mpmath, indicates if floats or mpmath floats are returned
"""
return 1-self.sf(t, **kwargs)
def icdf(self,t, **kwargs):
"""
Inverse cumulative density function.
Can be computed using a solver which is fast on individual evaluations or
using interpolation. The interpolation calculates the icdf on a grid and
uses linear interpolation on future evaluations.
Parameters
----------
output : string (default)
only used for mpmath, indicates if floats or mpmath floats are returned
method : string
can be "solver" or "interpolation
N : int (default 10000)
number of interpolation points
remaining kwargs : keyword arguments
are passed to solver
"""
t=np.asanyarray(t)
if t.ndim !=0:
return np.array([self.icdf(yy, **kwargs) for yy in t])
if self._backend == 'numpy':
return self._icdf_numpy(t.item(), **kwargs)
elif self._backend in ['mpmath', 'sympy']:
return self._icdf_mpmath(t.item(), **kwargs)
else:
raise Exception("Invalid backend")
def _icdf_numpy(self, y, method='solver', N=10000):
if y<0 or y>1:
return np.nan
elif np.isclose(y,0, rtol=0):
return np.min(self.a)
elif np.isclose(y,1, rtol=0):
return np.max(self.a)
if method=='solver':
f=lambda t : y-1+self._sf_numpy(t)
return scipy.optimize.bisect(f, np.min(self.a), np.max(self.a))
elif method=='interpolation':
if self._icdf_grid_x is None or len(self._icdf_grid_x) !=N:
self._icdf_grid_y=np.linspace(min(self.a), max(self.a),N)
self._icdf_grid_x=self.cdf(self._icdf_grid_y)
self._icdf_interpolator=interp1d(self._icdf_grid_x, self._icdf_grid_y,
assume_sorted=True, copy=False)
return self._icdf_interpolator(y).item()
else:
raise Exception('Invalid method')
def _icdf_mpmath(self, y,
method='solver',
N=10000,
output='float',
**kwargs):
if output=='float':
return np.float64(self._icdf_mpmath(y, output='mpf',
method=method,
N=N, **kwargs))
if mp.almosteq(y, 0, 1e-8):
a_min=np.min(self.a)
if isinstance(a_min, np.int32):
a_min=int(a_min)
return mp.mpf(a_min)
elif mp.almosteq(y, 1, 1e-8):
a_max=np.max(self.a)
if isinstance(a_max, np.int32):
a_max=int(a_max)
return mp.mpf(a_max)
elif y<0 or y>1:
return mp.nan
if method=='solver':
f=lambda t : y-1+self.sf(t)
x0=(np.min(self.a), np.max(self.a))
kwargs.setdefault('solver','bisect')
return mp.findroot(f, x0, **kwargs)
elif method=='interpolation':
if self._icdf_grid_x is None or len(self._icdf_grid_x) !=N:
self._icdf_grid_y=mp.linspace(min(self.a), max(self.a),N)
self._icdf_grid_x=self.cdf(self._icdf_grid_y)
self._icdf_interpolator=interp1d_mpmath(self._icdf_grid_x, self._icdf_grid_y,
assume_sorted=True, copy=False,
fill_value=mp.nan)
return self._icdf_interpolator(y).item()
else:
raise Exception('Invalid method')
class unittests(unittest.TestCase):
@property
def a(self):
return np.array([1,1.5,2,6,3])
_Znp=None
@property
def Znp(self):
if self._Znp is None:
self._Znp=LinearCombinationUniformSpacings(self.a, backend='numpy')
return self._Znp
_Zmp=None
@property
def Zmp(self):
if self._Zmp is None:
self._Zmp=LinearCombinationUniformSpacings(self.a, backend='mpmath')
return self._Zmp
_Zsp=None
@property
def Zsp(self):
if self._Zsp is None:
self._Zsp = LinearCombinationUniformSpacings(self.a, backend='sympy')
return self._Zsp
_Zsp2=None
@property
def Zsp2(self):
if self._Zsp2 is None:
self._Zsp2=LinearCombinationUniformSpacings(np.array([1,2,3]),
r=[2,3,1],
backend='sympy')
return self._Zsp2
@property
def N(self):
return len(self.a)
def test_consistency_pdf(self):
Znp=self.Znp
Zmp=self.Zmp
Zsp=self.Zsp
self.assertAlmostEqual(Zmp.pdf(3), Znp.pdf(3))
self.assertAlmostEqual(Zsp.pdf(3), Znp.pdf(3))
res=[Zmp.pdf(3), Zmp.pdf(4)]
self.assertTrue(np.allclose(Zmp.pdf([3,mp.mpf(4)]),res))
self.assertTrue(np.allclose(Znp.pdf([3,4]),res))
self.assertTrue(np.allclose(Zsp.pdf([3,4]),res))
self.assertIsInstance(Zmp.pdf([3,4]), np.ndarray)
self.assertIsInstance(Zsp.pdf([3,4]), np.ndarray)
self.assertIsInstance(self.Zsp2.pdf([3,4]), np.ndarray)
def test_consistency_sf(self):
Znp=self.Znp
Zmp=self.Zmp
Zsp=self.Zsp
self.assertAlmostEqual(Zmp.sf(3), Znp.sf(3))
self.assertAlmostEqual(Zsp.sf(3), Znp.sf(3))
res=[Zmp.sf(3), Zmp.sf(4)]
self.assertTrue(np.allclose(Zmp.sf([mp.mpf(3),4]),res))
self.assertTrue(np.allclose(Znp.sf([3,4]),res))
self.assertTrue(np.allclose(Zsp.sf([3,4]),res))
self.assertIsInstance(Zmp.sf([3,4]), np.ndarray)
self.assertIsInstance(Zsp.sf([3,4]), np.ndarray)
self.assertIsInstance(self.Zsp2.sf([3,4]), np.ndarray)
def test_pdf_cdf_sf(self):
for Z in [self.Zmp, self.Znp, self.Zsp, self.Zsp2]:
self.assertAlmostEqual(Z.cdf(np.max(self.a)),1)
self.assertAlmostEqual(Z.cdf(np.min(self.a)),0)
self.assertAlmostEqual(Z.sf(np.min(self.a)),1)
self.assertAlmostEqual(Z.cdf(2*np.max(self.a)),1)
self.assertAlmostEqual(Z.cdf(-2*np.min(self.a)),0)
self.assertAlmostEqual(Z.pdf(2*np.max(self.a)),0)
Zmp=self.Zmp
self.assertAlmostEqual(Zmp.pdf(mp.mpf(-2*np.min(self.a))),0)
self.assertAlmostEqual(Zmp.sf(mp.mpf(np.max(self.a))),0)
def test_cdf_with_sampling(self):
np.random.seed(100)
from statsmodels.distributions.empirical_distribution import ECDF
sample=sp.stats.dirichlet.rvs(np.ones(self.N),100000).dot(self.a)
ecdf=ECDF(sample)
for Z in [self.Zmp, self.Znp, self.Zsp]:
for val in np.linspace(min(self.a), max(self.a), 10):
self.assertAlmostEqual(ecdf(val), Z.cdf(val), places=2)
Z=self.Zsp2
sample=sp.stats.dirichlet.rvs(np.ones(Z.N),100000).dot(Z._a_full)
ecdf=ECDF(sample)
for val in np.linspace(min(Z.a), max(Z.a), 10):
self.assertAlmostEqual(ecdf(val), Z.cdf(val), places=2)
def test_consistency_pdf_cdf(self):
for Z in [self.Zmp, self.Znp, self.Zsp, self.Zsp2]:
for val in np.linspace(min(self.a), max(self.a), 10):
self.assertAlmostEqual(sp.integrate.quad(lambda x: Z.pdf(x),min(self.a), val)[0],
Z.cdf(val), places=3)
def test_special_cases_test_cdf_icdf(self):
for Z in [self.Zmp, self.Znp, self.Zsp, self.Zsp2]:
self.assertAlmostEqual(Z.cdf(max(Z.a)),1,places=10)
self.assertAlmostEqual(Z.cdf(min(Z.a)),0,places=10)
self.assertAlmostEqual(Z.icdf(0),min(Z.a),places=10)
self.assertAlmostEqual(Z.icdf(1),max(Z.a),places=10)
def test_consistency_cdf_icdf(self):
for Z in [self.Zmp, self.Znp, self.Zsp, self.Zsp2]:
for val in np.linspace(min(Z.a), max(Z.a), 25):
self.assertAlmostEqual(Z.icdf(Z.cdf(val)),val, places=10)
self.assertAlmostEqual(Z.icdf(Z.cdf(val),
method='interpolation',
N=1000),val,
places=4)
def test_moments_with_sampling(self):
np.random.seed(100)
sample=sp.stats.dirichlet.rvs(np.ones(self.N),10000000).dot(self.a)
for Z in [self.Zmp, self.Znp, self.Zsp]:
self.assertAlmostEqual(Z.mean, np.mean(sample), places=3)
self.assertAlmostEqual(Z.var, np.var(sample), places=3)
self.assertAlmostEqual(Z.std, np.std(sample), places=3)
Z=self.Zsp2
sample=sp.stats.dirichlet.rvs(np.ones(Z.N),10000000).dot(Z._a_full)
self.assertAlmostEqual(Z.mean, np.mean(sample), places=3)
self.assertAlmostEqual(Z.var, np.var(sample), places=3)
self.assertAlmostEqual(Z.std, np.std(sample), places=3)
def test_rvs(self):
for Z in [self.Zmp, self.Znp, self.Zsp, self.Zsp2]:
shape=[3,4]
sample=Z.rvs(size=shape)
self.assertListEqual(list(sample.shape),shape)
np.random.seed(10)
sample1=sp.stats.dirichlet.rvs(np.ones(Z.N),size=(8,6,3)).dot(Z._a_full)
sample2=Z.rvs(size=(8,6,3), random_state=10)
self.assertTrue(np.allclose(sample1,sample2))
if __name__ == '__main__':
run_unit_tests=True
test_only = list() # if list is empty then test all
#test_only.append('test_consistency_cdf_icdf')
if run_unit_tests:
if len(test_only) > 0:
suite = unittest.TestSuite()
for ut in test_only:
suite.addTest(unittests(ut))
unittest.TextTestRunner().run(suite)
else:
#unittest.main()
suite = unittest.TestLoader().loadTestsFromTestCase(unittests)
unittest.TextTestRunner(verbosity=2).run(suite)
else:
import matplotlib.pyplot as plt
plt.close('all')
#a=np.array([1,1.5,2,6,3])
a=np.array([1,1.5,1.51,6,3])
Zmp=LinearCombinationUniformSpacings(a, backend='mpmath')
Znp=LinearCombinationUniformSpacings(a, backend='numpy')
Zsp=LinearCombinationUniformSpacings(a, backend='sympy')
#print some values
print(Zmp.pdf(3))
print(Znp.pdf(3))
print(Zsp.pdf(3))
#make some plots
x=np.linspace(np.min(a),np.max(a),200)
plt.plot(x,Znp.pdf(x))
plt.plot(x,Znp.cdf(x))
plt.figure()
plt.plot(np.linspace(0,1,1000), Znp.icdf(np.linspace(0,1,1000)))
plt.plot(np.linspace(0,1,1000), Zmp.icdf(np.linspace(0,1,1000)))
plt.plot(np.linspace(0,1,1000), Zsp.icdf(np.linspace(0,1,1000),
method='interpolation'))
#show added value of the extra precision of mpmath
print(Zmp.icdf(mp.mpf('0.999999991'), method='solver'))
print(Zmp.icdf(mp.mpf('0.999999991'), method='interpolation'))
print(Znp.icdf(0.999999991, method = 'solver'))
print(Znp.icdf(0.999999991, method = 'interpolation'))