Draw random samples from multiple Dirichlet distributions for use in transition probability matrices.

rdirichlet_mat(
  n,
  alpha,
  output = c("array", "matrix", "data.frame", "data.table")
)

Arguments

n

Number of samples to draw.

alpha

A matrix where each row is a separate vector of shape parameters.

output

The class of the object returned by the function. Either an array, matrix, data.frame, or data.table.

Value

If output = "array", then an array of matrices is returned where each row of each matrix is a sample from the Dirichlet distribution. If output results in a two dimensional object (i.e., a matrix, data.frame, or data.table, then each row contains all elements of the sampled matrix from the Dirichlet distribution ordered rowwise; that is, each matrix is flattened. In these cases, the number of rows must be less than or equal to the number of columns.

Details

This function is meant for representing the distribution of transition probabilities in a transition matrix. The (i,j) element of alpha is a transition from state i to state j. It is vectorized and written in C++ for speed.

Examples

alpha <- matrix(c(100, 200, 500, 50, 70, 75), ncol = 3, nrow = 2, byrow = TRUE)
samp <- rdirichlet_mat(100, alpha)
print(samp[, , 1:2])
#> , , 1
#> 
#>           [,1]      [,2]      [,3]
#> [1,] 0.1131831 0.2561972 0.6306197
#> [2,] 0.2636402 0.3881489 0.3482110
#> 
#> , , 2
#> 
#>           [,1]      [,2]      [,3]
#> [1,] 0.1426024 0.2262479 0.6311497
#> [2,] 0.2976925 0.3570395 0.3452680
#>