Returns a function that draws samples from the mixture by first selecting a component according to the mixing weights, then sampling from the selected component.

# S3 method for class 'mixture'
sampler(x, ...)

Arguments

x

A mixture object.

...

Additional arguments (not used).

Value

A function function(n = 1, ...) returning a numeric vector of length n.

Examples

m <- mixture(list(normal(0, 1), normal(5, 1)), c(0.5, 0.5))
s <- sampler(m)
set.seed(42)
s(6)
#> [1]  0.63286260  0.40426832  7.01842371 -0.10612452  1.51152200 -0.09465904