Weighted multinomial sampling
Background
Weighted (multinomial) sampling draws categories in proportion to given weights — the basis of importance sampling, weighted bootstrap, and roulette-wheel selection in genetic algorithms. You normalize the weights into a probability distribution, then use inverse-transform sampling: build the cumulative distribution and map uniform random numbers through it.
Problem statement
Implement weighted_sample(weights, n, seed=0) returning n category indices drawn with probability proportional to weights:
Use np.random.default_rng(seed) and side='right'.
Input
weights—np.ndarray(K,), non-negative weights (need not sum to 1).n—int, number of samples.seed—int.
Output
An np.ndarray of n integer indices in [0, K).
Examples
Example 1
Input: weights = [1, 3], n = large
Output: index 0 about 25% of the time, index 1 about 75%
Explanation: normalizing gives , so category 1 (weight 3) is drawn three times as often as category 0.
Constraints
- Normalize the weights to a probability distribution first.
- Sample via the cumulative distribution (
searchsorted), not a per-sample loop. - Indices are integers in
[0, len(weights)).
Notes
- This is inverse-transform sampling applied to a categorical distribution defined by arbitrary weights.
- A weight of 0 means that category is never drawn; its cumulative step has zero width.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference example: proportional draws with seed 1
- •Sample: three categories with seed 9
- •Example: unnormalized weights with seed 2