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)
| 25 | |
| 26 | |
| 27 | def 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 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…