Learn R Programming

animalEKF (version 1.3)

low_var_sample: Sample particles using low-variance sampling.

Description

Sample particles using low-variance sampling.

Usage

low_var_sample(wts, M=length(wts))

Value

A numeric vector of length M.

Arguments

wts

Vector of weights.

M

The number of items to sample. When resampling, should be number of particles npart.

Author

Samuel Ackerman

Details

Low-variance sampling guarantees items will be sampled in proportion to their weights. With random sampling with replacement (sample function), there is some variability in the final proportions of items.

References

James Edward Baker. Reducing bias and inefficiency in the selection algorithm. Proceedings of the Second International Conference on Genetic Algorithms on Genetic Algorithms and Their Application, 1987.

Examples

Run this code
n <- 20
w <- runif(n)

#can use M != to length(wts)
low_var_sample(wts=w, M=15)

lv <- low_var_sample(wts=w, M=n)

#usual resampling with weights
a <- sample(x=1:n, size=n, prob=w, replace=TRUE)

#the first should be more in proportion to weights
table(lv)/n
table(a)/n
#proportions
w/sum(w)

Run the code above in your browser using DataLab