Dirichlet distribution
Random number generation for the Dirichlet distribution D(alpha[1],...,alpha[K]).
rDirichlet(n, alpha=c(1, 1))
n |
number of observations to be sampled. |
alpha |
parameters of the Dirichlet distribution (‘prior sample sizes’). |
Some objects.
A matrix with sampled values.
Arnošt Komárek arnost.komarek[AT]mff.cuni.cz
Devroye, L. (1986). Non-Uniform Random Variate Generation. New York: Springer-Verlag, Chap. XI.
Gelman, A., Carlin, J. B., Stern, H. S., and Rubin, D. B. (2004). Bayesian Data Analysis. Second Edition. Boca Raton: Chapman and Hall/CRC, pp. 576, 582.
set.seed(1977) alpha <- c(1, 2, 3) Mean <- alpha/sum(alpha) Var <- -(alpha %*% t(alpha)) diag(Var) <- diag(Var) + alpha*sum(alpha) Var <- Var/(sum(alpha)^2*(1+sum(alpha))) x <- rDirichlet(1000, alpha=alpha) x[1:5,] apply(x, 1, sum)[1:5] ### should be all ones rbind(Mean, apply(x, 2, mean)) var(x) print(Var)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.