Skip to content

Instantly share code, notes, and snippets.

@skojaku
Created October 29, 2023 10:25
Show Gist options
  • Save skojaku/44349fd0975f65b964e5395311b98825 to your computer and use it in GitHub Desktop.
Save skojaku/44349fd0975f65b964e5395311b98825 to your computer and use it in GitHub Desktop.
One-pass sampling without replacement by Efraimidis and Spirakis algorithm
# 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