MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / create_alias_table

Function create_alias_table

easygraph/utils/alias.py:4–46  ·  view source on GitHub ↗

Parameters --------- area_ratio : sum(area_ratio)=1 Returns ---------- 1. accept 2. alias

(area_ratio)

Source from the content-addressed store, hash-verified

2
3
4def create_alias_table(area_ratio):
5 """
6 Parameters
7 ---------
8 area_ratio :
9 sum(area_ratio)=1
10
11 Returns
12 ----------
13 1. accept
14 2. alias
15
16 """
17 import numpy as np
18
19 l = len(area_ratio)
20 accept, alias = [0] * l, [0] * l
21 small, large = [], []
22 area_ratio_ = np.array(area_ratio) * l
23 for i, prob in enumerate(area_ratio_):
24 if prob < 1.0:
25 small.append(i)
26 else:
27 large.append(i)
28
29 while small and large:
30 small_idx, large_idx = small.pop(), large.pop()
31 accept[small_idx] = area_ratio_[small_idx]
32 alias[small_idx] = large_idx
33 area_ratio_[large_idx] = area_ratio_[large_idx] - (1 - area_ratio_[small_idx])
34 if area_ratio_[large_idx] < 1.0:
35 small.append(large_idx)
36 else:
37 large.append(large_idx)
38
39 while large:
40 large_idx = large.pop()
41 accept[large_idx] = 1
42 while small:
43 small_idx = small.pop()
44 accept[small_idx] = 1
45
46 return accept, alias
47
48
49def alias_sample(accept, alias):

Callers

nothing calls this directly

Calls 2

appendMethod · 0.80
popMethod · 0.80

Tested by

no test coverage detected