/
Samples.py
53 lines (48 loc) · 1.6 KB
/
Samples.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
import numpy as np
from scipy.stats import nbinom
from scipy.stats import binom
from scipy.stats import logser
from scipy.stats import boltzmann
def get_samples(distribution, n):
values = np.random.rand(n)
values.sort()
cumulative = np.cumsum(distribution)
z = 0
k = len(distribution)
freq = [0] * k
for x in values:
while x > cumulative[z]:
z+=1
freq[z]+=1
freq_positive = []
D_freq_positive = {}
for j in range(k):
x = freq[j]
if x>0:
freq_positive.append(x)
if x in D_freq_positive:
D_freq_positive[x].append(j)
else:
D_freq_positive[x] = [j]
return [freq_positive, D_freq_positive]
def get_distribution(dist_name, k):
k = int(k)
if dist_name == 'Uniform':
raw_distribution = [1] * k
elif dist_name == 'Two-step':
raw_distribution = [1] * (k//2) + [4] * (k-k//2)
elif dist_name == 'Three-step':
raw_distribution = [1] * (k//3) + [3] * (k//3)+[9]*(k-2*k//3)
elif dist_name == 'Subset-uniform':
raw_distribution = [1] * (k//2) + [0] * (k-k//2)
elif dist_name == 'Log-series':
p = (k-2)/k
raw_distribution = [logser.pmf(j, p) for j in range(1,k+1)]
elif dist_name == 'Geometric':
p = (k-1)*1.0/k
raw_distribution = [(1-p)*p**i for i in range(k)]
elif dist_name == 'Zipf-half':
raw_distribution = [1/(float(i)**(0.5)) for i in range(1,k+1)]
elif dist_name == 'Zipf-one':
raw_distribution = [1/(float(i)**(1)) for i in range(1,k+1)]
return raw_distribution