Dirichlet: The Dirichlet Distribution

Description Usage Arguments Details Value Author(s) Examples

Description

Density function and random number generation for the Dirichlet distribution

Usage

1
2
3
4
5
rdirichlet(n, alpha)

ddirichlet(x, alpha, log = FALSE, sum.up = FALSE)

ddirichlet_R(x, alpha, log = FALSE, sum.up = FALSE)

Arguments

n

number of random observations to draw

x

a matrix containing observations

alpha

the Dirichlet distribution's parameters. Can be a vector (one set of parameters for all observations) or a matrix (a different set of parameters for each observation), see “Details”

log

if TRUE, logarithmic densities are returned

sum.up

if TRUE, the (log-)likelihood is returned

Details

The Dirichlet distribution is a multidimensional generalization of the Beta distribution where each dimension is governed by an alpha-parameter. Formally this is

D(α)=[Γ(∑α)/∏Γ(α)]∏ y^(α-1)

Usually, alpha is a vector thus the same parameters will be used for all observations. If alpha is a matrix, a complete set of alpha-parameters must be supplied for each observation.

log returns the logarithm of the densities (therefore the log-likelihood) and sum.up returns the product or sum and thereby the likelihood or log-likelihood.

Dirichlet (log-)densities are by default computed using C-routines (ddirichlet_log_vector and ddirichlet_log_matrix), a version only using R is provided by ddirichlet_R. Caution: Although .C() can be used to call the C routines directly, R will crash or produce wrong values, if, e.g., data types are not set properly.

Value

rdirichlet

returns a matrix with random numbers according to the supplied alpha vector or matrix.

ddirichlet

returns a vector of densities (if sum.up = FALSE) or the (log-)likelihood (if sum.up = TRUE) for the given data and alphas. Returns NaN if any element of alpha is <= 0.

ddirichlet_R

as ddirichlet, only implemented purely in R.

Author(s)

Marco J. Maier

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
X1 <- rdirichlet(100, c(5, 5, 10))

a.mat <- cbind(1:10, 5, 10:1)
a.mat
X2 <- rdirichlet(10, a.mat)
# note how the probabilities in the first an last column relate to a.mat
round(X2, 2)

ddirichlet(X1, c(5, 5, 10))
ddirichlet(X2, a.mat)

ddirichlet(X2[1:3,], c(1, 2, -1))
ddirichlet(X2[1:3,], c(1, 2, -1), sum.up = TRUE)

Example output

Loading required package: Formula
      [,1] [,2] [,3]
 [1,]    1    5   10
 [2,]    2    5    9
 [3,]    3    5    8
 [4,]    4    5    7
 [5,]    5    5    6
 [6,]    6    5    5
 [7,]    7    5    4
 [8,]    8    5    3
 [9,]    9    5    2
[10,]   10    5    1
      [,1] [,2] [,3]
 [1,] 0.05 0.43 0.52
 [2,] 0.04 0.54 0.43
 [3,] 0.32 0.28 0.40
 [4,] 0.15 0.30 0.55
 [5,] 0.55 0.22 0.23
 [6,] 0.27 0.38 0.35
 [7,] 0.60 0.16 0.24
 [8,] 0.51 0.22 0.27
 [9,] 0.63 0.22 0.15
[10,] 0.61 0.22 0.17
  [1] 13.23749392  8.55714881  7.20624951 14.53003593  8.44006548  0.79824950
  [7] 12.97954233  5.41570057  5.67899097 15.31450370  4.83245552  1.38251422
 [13] 15.37949971  0.26747991 12.21391301 15.36508379 10.52088810 12.91431226
 [19]  5.48171044  1.61909135  0.92247429  1.65433241  7.80815975 12.33551664
 [25]  3.47250805  3.19958313  2.22812654  8.53657576 11.18593229  7.35560204
 [31] 15.76574761 12.30329725  8.02008085  1.49589760  5.97980745  4.25019980
 [37] 12.42440828 12.27800248 16.41381335 16.35664429  7.30931864  9.78107110
 [43] 10.16945149  8.91959256  5.90570519  4.88788741 14.46794074  0.07173112
 [49] 17.37134959 15.47541561 10.51237367 12.93461633 14.20871780 15.69097623
 [55] 16.17075455  7.64956794  5.03554020  4.29689457 10.76929748  3.57147052
 [61] 17.43857082 17.18201822  7.88882096  1.48185011  2.76592184  9.78135782
 [67] 13.05727680  7.74545052  9.48692440 11.01388614  0.78726652 15.31545856
 [73]  1.63832510  7.14440151  6.44825398 15.37343788  8.82807414  1.14667287
 [79] 12.47874677 14.87713350 11.76692984 14.21308822 10.92325036 16.55990802
 [85]  0.14239123 15.39548170  2.89615235  6.74382001  1.49421203  6.91386742
 [91] 11.15940688  2.85791788  0.54984287  5.63235395 13.63753652 13.16022004
 [97] 11.61301717  0.54532644 17.69970358  3.67897609
 [1] 14.651433  4.550056  5.530697  9.425575  2.684368  8.548482  5.073082
 [8]  8.181497 11.182616  3.995327
[1] NaN NaN NaN
Warning message:
In ddirichlet(X2[1:3, ], c(1, 2, -1)) : all values in alpha must be > 0
[1] NaN
Warning message:
In ddirichlet(X2[1:3, ], c(1, 2, -1), sum.up = TRUE) :
  all values in alpha must be > 0

DirichletReg documentation built on May 18, 2021, 5:06 p.m.