if (FALSE) {
# a scalar variable following a strange bimodal distibution
weights <- uniform(0, 1, dim = 3)
a <- mixture(normal(-3, 0.5),
normal(3, 0.5),
normal(0, 3),
weights = weights
)
m <- model(a)
plot(mcmc(m, n_samples = 500))
# simulate a mixture of poisson random variables and try to recover the
# parameters with a Bayesian model
x <- c(
rpois(800, 3),
rpois(200, 10)
)
weights <- uniform(0, 1, dim = 2)
rates <- normal(0, 10, truncation = c(0, Inf), dim = 2)
distribution(x) <- mixture(poisson(rates[1]),
poisson(rates[2]),
weights = weights
)
m <- model(rates)
draws_rates <- mcmc(m, n_samples = 500)
# check the mixing probabilities after fitting using calculate()
# (you could also do this within the model)
normalized_weights <- weights / sum(weights)
draws_weights <- calculate(normalized_weights, draws_rates)
# get the posterior means
summary(draws_rates)$statistics[, "Mean"]
summary(draws_weights)$statistics[, "Mean"]
# weights can also be an array, giving different mixing weights
# for each observation (first dimension must be number of components)
dim <- c(5, 4)
weights <- uniform(0, 1, dim = c(2, dim))
b <- mixture(normal(1, 1, dim = dim),
normal(-1, 1, dim = dim),
weights = weights
)
}
Run the code above in your browser using DataLab