Learn R Programming

rje (version 1.9)

Dirichlet: The Dirichlet Distribution

Description

Density function and random generation for Dirichlet distribution with parameter vector alpha.

Usage

rdirichlet(n, alpha)
ddirichlet(x, alpha, log = FALSE, tol = 1e-10)

Arguments

n

number of random variables to be generated.

alpha

vector of Dirichlet hyper parameter.

x

vector (or matrix) of points in sample space.

log

logical; if TRUE, natural logarithm of density is returned.

tol

tolerance of vectors not summing to 1 and negative values.

Value

rdirichlet returns a matrix, each row of which is an independent draw from a Dirichlet distribution with parameter vector alpha.

ddirichlet returns a vector, each entry being the density of the corresponding row of x. If x is a vector, then the output will have length 1.

Details

If x is a matrix, each row is taken to be a different point whose density is to be evaluated. If the number of columns in (or length of, in the case of a vector) x is one less than the length of alpha, the remaining column (or entry) is assumed to make the vector sum to 1.

The k-dimensional Dirichlet distribution has density $$\frac{\Gamma\left(\sum_i \alpha_i\right)}{\prod_i \Gamma(\alpha_i)} \prod_{i=1}^k x_i^{\alpha_i-1}$$ assuming that \(x_i > 0\) and \(\sum_i x_i = 1\), and zero otherwise.

If the sum of row entries in x differs from 1 by more than tol, or any entry takes a value less than -tol, the density is assumed to be zero.

References

http://en.wikipedia.org/wiki/Dirichlet_distribution

Examples

Run this code
# NOT RUN {
x = rdirichlet(10, c(1,2,3))
x

# Find densities at random points.
ddirichlet(x, c(1,2,3))
# Last column to be inferred.
ddirichlet(x[,c(1,2)], c(1,2,3))
# }

Run the code above in your browser using DataLab