-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdgp_class.py
310 lines (277 loc) · 10.9 KB
/
dgp_class.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
"""
This file creates the data generating functions necessary to reproduce Table 1
from:
Z. I. Botev, J. F. Grotowski, and D. P. Kroese. Kernel density
estimation via diffusion. The Annals of Statistics, 38(5):2916-2957, 2010.
Daniel B. Smith, PhD
1-29-2013
"""
from __future__ import division
import numpy as np
from random import shuffle
rand = np.random
_samp_doc = """Generates random numbers according to a {dist} distribution
Parameters:
----------
size: d1, ..., dn : `n` ints, optional
The dimensions of the returned array, should be all positive.
{eq}
"""
_pdf_doc = """Calculated probability distribution function according to a
{dist} distribution.
{eq}
"""
_class_doc = """{class_} class to generate data generating function objects.
Each object has two methods:
dgp.sample(size=1): generates array of samples according to shape defined
in size.
dgp.pdf(mesh): calculates pdf on the given mesh
Probability distribution function, N(mu, sigma^2) normal:
{eq}
"""
# Adapted from scipy:
_NORM_PDF_C = np.sqrt(2*np.pi)
def _norm(x, params):
x = np.asarray(x)
return np.exp(-(x-params[0])**2/2.0/params[1]**2) / _NORM_PDF_C / params[1]
def _generate(inputs, counts):
"""
Generates random samples from a sum of normals based on inputs, counts
Parameters
----------
inputs : list of (mean, standard deviation) tuples
counts : list of number of samples to draw from each normal defined in
inputs
"""
out = []
for iC, count in enumerate(counts):
out.extend(inputs[iC][1]*rand.randn(count)+inputs[iC][0])
shuffle(out)
return out
class dgp(object):
__doc__ = _class_doc.format(class_='Default', eq='N/A')
def __str__(self):
# Print first line of documentation
return self.__doc__.split('\n')[0]
def sample(self, size=1):
nsamp = np.prod(size)
out = self._sample(nsamp)
return np.resize(out, size)
def pdf(self, mesh):
return self._pdf(mesh)
def _pdf(self, mesh):
return sum((self._rates[k]*_norm(mesh, input_) for k, input_ in
enumerate(self._inputs)))
def _sample(self, nsamp):
counts = rand.multinomial(nsamp, self._rates, size=1)[0]
return _generate(self._inputs, counts)
def mesh(self, N=None):
"""
Generates default mesh
"""
if N is None:
N = 2**14
maxes = [input_[0]+4*input_[1] for input_ in self._inputs]
mins = [input_[0]-4*input_[1] for input_ in self._inputs]
min_ = min(mins)
max_ = max(maxes)
return np.linspace(min_, max_, num=N)
_inputs = []
_rates = []
class Claw(dgp):
def __init__(self):
dgp.__init__(self)
eq = ' 1/2*N(0,1) + sum_{k=0}^4 1/10*N(k/2-1, (1/10)^2)'
self.sample.__func__.__doc__ = _samp_doc.format(dist='claw', eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist='claw', eq=eq)
self.__doc__ = _class_doc.format(class_='Claw', eq=eq)
_inputs = [(0, 1)]
for k in xrange(5):
_inputs.append((k/2-1, 1/10))
_rates = [1/2] + [1/10]*5
class StronglySkewed(dgp):
def __init__(self):
dgp.__init__(self)
eq = 'sum_{k=0}^7 1/8*N(3*((2/3)^k-1), (2/3)^(2k))'
self.sample.__func__.__doc__ = _samp_doc.format(dist='strongly skewed',
eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist='strongly skewed',
eq=eq)
self.__doc__ = _class_doc.format(class_='Strongly Skewed', eq=eq)
for k in xrange(8):
self._inputs.append((3*((2/3)**k-1), (2/3)**k))
_rates = [1/8]*8
class KurtoticUnimodal(dgp):
def __init__(self):
dgp.__init__(self)
eq = '2/3*N(0,1) + 1/3*N(0,(1/10)^2)'
dist = 'kurtotic unimodal'
self.sample.__func__.__doc__ = _samp_doc.format(dist=dist, eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist=dist, eq=eq)
self.__doc__ = _class_doc.format(class_='Kurtotic Unimodal', eq=eq)
_inputs = [(0, 1), (0, 1/10)]
_rates = [2/3, 1/3]
class DoubleClaw(dgp):
def __init__(self):
dgp.__init__(self)
eq = ('49/100*N(-1, (2/3)^2) + 49/100*N(1, (2/3)^2) + \n' +
' sum_{k=0}^6 1/350*N((k-3)/2, (1/100)^2)')
self.sample.__func__.__doc__ = _samp_doc.format(dist='double claw',
eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist='double claw', eq=eq)
self.__doc__ = _class_doc.format(class_='Double Claw', eq=eq)
_inputs = [(-1, 2/3), (1, 2/3)]
for k in xrange(7):
_inputs.append(((k-3)/2, 1/100))
_rates = [49/100]*2+[1/350]*7
class DiscreteComb(dgp):
def __init__(self):
dgp.__init__(self)
eq = ('2/7*sum_{k=0}^2 N((12*k-15/7), (2/7)^2) + \n' +
' 1/21*sum_{k=8}^10 N(2*k/7, (1/21)^2)')
self.sample.__func__.__doc__ = _samp_doc.format(dist='discrete comb',
eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist='discrete comb',
eq=eq)
self.__doc__ = _class_doc.format(class_='Discrete Comb', eq=eq)
_inputs = []
for k in xrange(3):
_inputs.append(((12*k-15)/7, 2/7))
for k in xrange(8, 11):
_inputs.append((2*k/7, 1/21))
_rates = [2/7]*3 + [1/21]*3
class AsymDoubleClaw(dgp):
def __init__(self):
dgp.__init__(self)
eq = ('46/100*sum_{k=0}^1 N(2*k-1, (2/3)^2) + 1/300*sum_{k=1}^3 ' +
'N(-k/2, (1/100)^2)\n sum_{k=1}^3 N(k/2, (7/100)^2)')
dist='asymmetric double claw'
self.sample.__func__.__doc__ = _samp_doc.format(dist=dist, eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist=dist, eq=eq)
self.__doc__ = _class_doc.format(class_='Asymmetric Double Claw', eq=eq)
_inputs = []
for k in xrange(2):
_inputs.append((2*k-1, 2/3))
for k in xrange(1, 4):
_inputs.append((-k/2, 1/100))
for k in xrange(1, 4):
_inputs.append((k/2, 7/100))
_rates = [46/100]*2 + [1/300]*3 + [7/300]*3
class Outlier(dgp):
def __init__(self):
dgp.__init__(self)
eq = '1/10*N(0, 1)+9/10*N(0, (1/10)^2)'
self.sample.__func__.__doc__ = _samp_doc.format(dist='outlier', eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist='outlier', eq=eq)
self.__doc__ = _class_doc.format(class_='Outlier', eq=eq)
_inputs = [(0, 1), (0, 1/10)]
_rates = [1/10, 9/10]
class SeparatedBimodal(dgp):
def __init__(self):
dgp.__init__(self)
eq = '1/2*N(-12, (1/2)^2) + 1/2*N(12, (1/2)^2)'
dist = 'separated bimodal'
self.sample.__func__.__doc__ = _samp_doc.format(dist=dist, eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist=dist, eq=eq)
self.__doc__ = _class_doc.format(class_='Separated Bimodal', eq=eq)
_inputs = [(-12, 1/2), (12, 1/2)]
_rates = [1/2]*2
class SkewBimodal(dgp):
def __init__(self):
dgp.__init__(self)
eq = '3/4*N(0, 1) + 1/4*N(3/2, (1/3)^2)'
dist = 'skewed bimodal'
self.sample.__func__.__doc__ = _samp_doc.format(dist=dist, eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist=dist, eq=eq)
self.__doc__ = _class_doc.format(class_='Skewed Bimodal', eq=eq)
_inputs = [(0,1), (3/2, 1/3)]
_rates = [3/4, 1/4]
class Bimodal(dgp):
def __init__(self):
dgp.__init__(self)
eq = '1/2*N(0, (1/10)^2) + 1/2*N(5, 1)'
self.sample.__func__.__doc__ = _samp_doc.format(dist='bimodal', eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist='bimodal', eq=eq)
self.__doc__ = _class_doc.format(class_='Bimodal', eq=eq)
_inputs = [(0, 1/10), (5, 1)]
_rates = [1/2]*2
class LogNormal(dgp):
def __init__(self):
dgp.__init__(self)
eq = 'Wrapper for Numpy\'s log normal random generator'
dist = 'log normal'
self.pdf.__func__.__doc__ = _pdf_doc.format(dist=dist, eq=eq)
self.__doc__ = _class_doc.format(class_='Log Normal', eq=eq)
sample = rand.lognormal
def _pdf(self, mesh):
mesh = np.asarray(mesh)
if (mesh<=0).any():
raise ValueError('mesh must be >0')
return np.exp(-np.log(mesh)**2/2)/mesh/_NORM_PDF_C
def mesh(self, N=None):
if N is None:
N = 2**14
mesh, step = np.linspace(0, 10, num=N, retstep=True)
mesh += step
return mesh
class AsymClaw(dgp):
def __init__(self):
dgp.__init__(self)
eq = '1/2*N(0, 1) + sum_{k=-2}^2 2**(1-k)/31*N(k+1/2, (2**-k/10)^2)'
dist = 'asymmetric claw'
self.sample.__func__.__doc__ = _samp_doc.format(dist=dist, eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist=dist, eq=eq)
self.__doc__ = _class_doc.format(class_='Asymmetric Claw', eq=eq)
_inputs = [(0, 1)]
_rates = [1/2]
for k in xrange(-2, 3):
_inputs.append((k+1/2, 2**(-k)/10))
_rates.append(2**(1-k)/31)
class Trimodal(dgp):
def __init__(self):
dgp.__init__(self)
eq = '1/3*sum_{k=0}^2 N(80*k, (k+1)^4)'
self.sample.__func__.__doc__ = _samp_doc.format(dist='trimodal', eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist='trimodal', eq=eq)
self.__doc__ = _class_doc.format(class_='Trimodal', eq=eq)
_inputs = []
for k in xrange(3):
_inputs.append((80*k, (k+1)**2))
_rates = [1/3]*3
class FiveModes(dgp):
def __init__(self):
dgp.__init__(self)
eq = '1/5*sum_{k=0}^4 N(80*k, (k+1)^2)'
dist = 'five modal'
self.sample.__func__.__doc__ = _samp_doc.format(dist=dist, eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist=dist, eq=eq)
self.__doc__ = _class_doc.format(class_='Five Modal', eq=eq)
_inputs = []
for k in xrange(5):
_inputs.append((80*k, k+1))
_rates = [1/5]*5
class TenModes(dgp):
def __init__(self):
dgp.__init__(self)
eq = '1/10*sum_{k=0}^9 N(100*k, (k+1)^2)'
dist = 'ten modal'
self.sample.__func__.__doc__ = _samp_doc.format(dist=dist, eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist=dist, eq=eq)
self.__doc__ = _class_doc.format(class_='Ten Modal', eq=eq)
_inputs = []
for k in xrange(10):
_inputs.append((100*k, k+1))
_rates = [1/10]*10
class SmoothComb(dgp):
def __init__(self):
dgp.__init__(self)
eq = 'sum_{k=0}^5 2**(5-k)/63*N((65-96*2**-k)/21, (32/63*2**(-2*k))^2)'
dist = 'smooth comb'
self.sample.__func__.__doc__ = _samp_doc.format(dist=dist, eq=eq)
self.pdf.__func__.__doc__ = _pdf_doc.format(dist=dist, eq=eq)
self.__doc__ = _class_doc.format(class_='Ten Modal', eq=eq)
_inputs = []
_rates = []
for k in xrange(6):
_inputs.append(((65-96*2**-k)/21, 32/63*2**(-k)))
_rates.append(2**(5-k)/63)