-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistribution.py
95 lines (76 loc) · 2.76 KB
/
distribution.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
# Copyright 2016 Parinz Ameri, Haipeng Guan
#
# This file is part of Nowog.
#
# Nowog is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Nowog is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Nowog. If not, see <http://www.gnu.org/licenses/>
import numpy as np
class Distribution(object):
"""Draw samples under specific type of distribution
Note:
The sample under "uniform" distribution is actually generated
by numpy.linspace, in order to provide a more evenly distributed
samples
"""
def __init__(self, seed=None):
self.seed(seed)
def seed(self, seed=None):
"""Change the seed in numpy.random"""
np.random.seed(seed)
def drawSamples(self, d_type, low, high, size, *args):
"""Draw samples based on distribution type (d_type)
Note:
It should be the only interface that this generator should use
Args:
d_type (str): distribution type: {uniform, normal}
low (float/int): Lower boundary of the output samples.
All values generated will be greater than or equal to low
high (float/int): Upper boundary of the output samples.
All values generated will be less than high.
size: (int): total amount of output samples
*args: [float]: additional arguments required by different distribution
"""
if d_type == 'uniform':
return self.linspace(low, high, size)
# return self.uniform(low, high, size)
# elif d_type == 'linspace':
# return self.linspace(low, high, size)
elif d_type == 'normal':
return self.normal(low, high, size, args[0])
else:
raise KeyError('Unknown distribution type: [%s]. Available types include: {uniform, normal}' % d_type)
def uniform(self, low, high, size):
return sorted(np.random.uniform(low, high, size))
def linspace(self, low, high, size):
return np.linspace(low, high, size, endpoint=False)
def normal(self, low, high, size, sigma):
"""Draw sample from normal distribution
Note:
mu will be generated automatically. Only sigma is required.
"""
mu = (low + high) / 2.0
res = []
while len(res) < size:
temp = np.random.normal(mu, sigma, size)
temp = filter(lambda x: low <= x < high, temp)
res.extend(temp)
return sorted(res[:size])
def exponential(self):
pass
def linear(self):
pass
def polynomial(self):
pass
_inst = Distribution()
drawSamples = _inst.drawSamples
seed = _inst.seed