sample.weighted: Weighted random sampling with a reservoir

View source: R/extras.R

sample.weightedR Documentation

Weighted random sampling with a reservoir

Description

Implementation of the Weighted random sampling with a reservoir (without replacement) (Efraimidis & Spirakis, 2006) algorithm.

Usage

sample.weighted(x, size, prob)

Arguments

x

a vector of one or more elements from which to choose.

size

a non-negative integer giving the number of items to choose.

prob

a vector of weights for obtaining the elements of the vector being sampled.

Value

A vector of length size with elements drawn from x

References

Efraimidis & Spirakis (2006). Weighted random sampling with a reservoir

Examples

N = 1000
x = seq_len(N)
prob = c(rep(0.1, N/2), rep(1, N/2))
x_sample = sample.weighted(x=x, prob=prob, size=N/2)
hist(x_sample)

roliveros-ramos/kali documentation built on March 30, 2025, 12:55 a.m.