-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproportional_sampling.py
41 lines (31 loc) · 1 KB
/
proportional_sampling.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
"""Proportional Sampling."""
##############################################################################
# Write a function to pick a number proportional to its absolute value
##############################################################################
import numpy as np
def proportional_sampling(data: np.array):
"""Proportional Sampling.
Args:
data (np.array): Data
Returns:
One of the element of data
"""
data = np.sort(np.abs(data))
prob = np.cumsum(data) / np.sum(data)
rand = np.random.uniform(size=1)[0]
idx = np.searchsorted(prob, rand, side="right")
if idx >= data.size:
idx = data.size - 1
return data[idx]
if "__main__" == __name__:
arr = np.array([-4, -1, 0, 9, 3, 6, 2])
print(arr)
test = dict()
n_trails = 100000
for _ in range(n_trails):
number = proportional_sampling(arr)
if number not in test.keys():
test[number] = 1
else:
test[number] += 1
print(test)