This process is commonly used in particle resampling during particle filtering. Here's an example to illustrate the steps:
We have 5 particles with the following weights:
We want to resample these particles based on their weights to produce 5 new particles.
Ensure the weights sum to 1 (if not already). In this case, they are already normalized:
The cumulative sum creates intervals that represent the range of selection for each particle:
This means:
Generate 5 random numbers between 0 and 1 to select particles. For example:
Check each random number against the cumulative weights to determine the corresponding particle:
The resampled particles based on this example would be:
import numpy as np
# Initial weights
weights = np.array([0.1, 0.2, 0.4, 0.2, 0.1])
# Cumulative sum of weights
cumulative_weights = np.cumsum(weights)
# Generate random numbers
random_numbers = np.random.uniform(0, 1, size=5)
# Resample particles
resampled_indices = np.searchsorted(cumulative_weights, random_numbers)
print("Random Numbers:", random_numbers)
print("Resampled Indices:", resampled_indices)
This will output a similar result, resampling particles based on their weights.