MCPcopy Create free account
hub / github.com/google-research/language / geometric_sample

Function geometric_sample

language/casper/utils/sample_utils.py:27–52  ·  view source on GitHub ↗

Samples k items using geometric sampling without replacement. At each step, item #i will be sampled with probability sample_prob^i (except the last item which gets the remaining probability mass). Item #i will then be removed from the pool. Args: pool: An ordered list of items to sampl

(pool: Sequence[Any], max_num_items: int,
                     sample_prob: float)

Source from the content-addressed store, hash-verified

25
26
27def geometric_sample(pool: Sequence[Any], max_num_items: int,
28 sample_prob: float) -> Sequence[Any]:
29 """Samples k items using geometric sampling without replacement.
30
31 At each step, item #i will be sampled with probability sample_prob^i (except
32 the last item which gets the remaining probability mass). Item #i will then
33 be removed from the pool.
34
35 Args:
36 pool: An ordered list of items to sample.
37 max_num_items: Maximum number of items to sample.
38 sample_prob: Probability for the geometric sampling.
39
40 Returns:
41 A list of sampled items.
42 """
43 pool = list(pool)
44 sampled = []
45 for _ in range(max_num_items):
46 if not pool:
47 break
48 i = 0
49 while random.random() > sample_prob and i < len(pool) - 1:
50 i += 1
51 sampled.append(pool.pop(i))
52 return sampled

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…