Skip to contents

The function ddirichlet() computes the density of a Dirichlet distribution.

The function rdirichlet() samples from a Dirichlet distribution.

The functions with suffix _cpp perform no input checks, hence are faster.

Usage

ddirichlet_cpp(x, concentration, log = FALSE)

rdirichlet_cpp(concentration)

ddirichlet(x, concentration, log = FALSE)

rdirichlet(n = 1, concentration)

Arguments

x

[numeric()]
A probability vector.

concentration

[numeric()]
A concentration vector of the same length as x.

log

[logical(1)]
Return the logarithm of the density value?

n

[integer(1)]
The number of samples.

Value

For ddirichlet(): The density value.

For rdirichlet(): If n = 1 a vector of length p, else a matrix of dimension n times p with samples as rows.

Examples

x <- c(0.5, 0.3, 0.2)
concentration <- 1:3

# compute density
ddirichlet(x = x, concentration = concentration)
#> [1] 0.72
ddirichlet(x = x, concentration = concentration, log = TRUE)
#> [1] -0.3285041

# sample
rdirichlet(concentration = 1:3)
#> [1] 0.07023854 0.75917746 0.17058400
rdirichlet(n = 4, concentration = 1:2)
#>           [,1]      [,2]
#> [1,] 0.2227488 0.7772512
#> [2,] 0.3041696 0.6958304
#> [3,] 0.1435577 0.8564423
#> [4,] 0.2562422 0.7437578