Created
October 29, 2023 10:25
-
-
Save skojaku/44349fd0975f65b964e5395311b98825 to your computer and use it in GitHub Desktop.
One-pass sampling without replacement by Efraimidis and Spirakis algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Efraimidis and Spirakis algorithm | |
# https://www.sciencedirect.com/science/article/abs/pii/S002001900500298 | |
import numpy as np | |
from numba import njit | |
@njit(nogil=True) | |
def one_pass_sampling_without_replacement(n, k, weights): | |
# Draw a uniform random variable for each item | |
u = np.random.rand(n) | |
# Compute a random variable X for each item | |
Z = np.log(u) / weights | |
# Find the indices of the k largest values | |
indices = np.argpartition(Z, -k)[-k:] | |
# Return the indices | |
return indices |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment