A wrapper for creating a vector of distributions.

## Details

A vector distribution is intented to vectorize distributions more efficiently than storing a list of distributions. To improve speed and reduce memory usage, distributions are only constructed when methods (e.g. d/p/q/r) are called.

Other wrappers: Convolution, DistributionWrapper, HuberizedDistribution, MixtureDistribution, ProductDistribution, TruncatedDistribution

## Super classes

distr6::Distribution -> distr6::DistributionWrapper -> VectorDistribution

## Active bindings

modelTable

Returns reference table of wrapped Distributions.

distlist

Returns list of constructed wrapped Distributions.

## Methods

### Public methods

Inherited methods

### Method new()

Creates a new instance of this R6 class.

VectorDistribution$new( distlist = NULL, distribution = NULL, params = NULL, shared_params = NULL, name = NULL, short_name = NULL, decorators = NULL, vecdist = NULL, ... ) #### Arguments distlist (list()) List of Distributions. distribution (character(1)) Should be supplied with params and optionally shared_params as an alternative to distlist. Much faster implementation when only one class of distribution is being wrapped. distribution is the full name of one of the distributions in listDistributions(), or "Distribution" if constructing custom distributions. See examples in VectorDistribution. params (list()|data.frame()) Parameters in the individual distributions for use with distribution. Can be supplied as a list, where each element is the list of parameters to set in the distribution, or as an object coercable to data.frame, where each column is a parameter and each row is a distribution. See examples in VectorDistribution. shared_params (list()) If any parameters are shared when using the distribution constructor, this provides a much faster implementation to list and query them together. See examples in VectorDistribution. name (character(1)) Optional name of wrapped distribution. short_name (character(1)) Optional short name/ID of wrapped distribution. decorators (character()) Decorators to add to the distribution during construction. vecdist VectorDistribution Alternative constructor to directly create this object from an object inheriting from VectorDistribution. ... ANY Named arguments of parameters to set values for. See examples. #### Examples \dontrun{ VectorDistribution$new(
distribution = "Binomial",
params = list(
list(prob = 0.1, size = 2),
list(prob = 0.6, size = 4),
list(prob = 0.2, size = 6)
)
)

VectorDistribution$new( distribution = "Binomial", params = data.table::data.table(prob = c(0.1, 0.6, 0.2), size = c(2, 4, 6)) ) # Alternatively VectorDistribution$new(
list(
Binomial$new(prob = 0.1, size = 2), Binomial$new(prob = 0.6, size = 4),

#### Arguments

model

(character(1))
id of wrapped Distributions to return. If NULL (default), a list of all wrapped Distributions is returned; if only one Distribution is matched then this is returned, otherwise a list of Distributions.

### Method strprint()

Printable string representation of the VectorDistribution. Primarily used internally.

#### Arguments

...

#### Arguments

which

(character(1) | numeric(1)
Ignored if distribution is unimodal. Otherwise "all" returns all modes, otherwise specifies which mode to return.

### Method median()

Returns named vector of medians from each wrapped Distribution.

#### Arguments

...

#### Arguments

...

#### Arguments

excess

(logical(1))
If TRUE (default) excess kurtosis returned.

...

#### Arguments

base

(integer(1))
Base of the entropy logarithm, default = 2 (Shannon entropy)

...

#### Arguments

t

(integer(1))
t integer to evaluate function at.

...

#### Arguments

t

(integer(1))
t integer to evaluate function at.

...

#### Arguments

z

(integer(1))
z integer to evaluate probability generating function at.

...

#### Arguments

...

(numeric())
Points to evaluate the function at Arguments do not need to be named. The length of each argument corresponds to the number of points to evaluate, the number of arguments corresponds to the number of variables in the distribution. See examples.

log

(logical(1))
If TRUE returns the logarithm of the probabilities. Default is FALSE.

simplify

logical(1)
If TRUE (default) simplifies the return if possible to a numeric, otherwise returns a data.table::data.table.

data

array
Alternative method to specify points to evaluate. If univariate then rows correspond with number of points to evaluate and columns correspond with number of variables to evaluate. In the special case of VectorDistributions of multivariate distributions, then the third dimension corresponds to the distribution in the vector to evaluate.

#### Examples

vd <- VectorDistribution$new( distribution = "Binomial", params = data.frame(size = 9:10, prob = c(0.5,0.6))) vd$pdf(2)
# Equivalently
vd$pdf(2, 2) vd$pdf(1:2, 3:4)
# or as a matrix
vd$pdf(data = matrix(1:4, nrow = 2)) # when wrapping multivariate distributions, arrays are required vd <- VectorDistribution$new(
distribution = "Multinomial",
params = list(
list(size = 5, probs = c(0.1, 0.9)),
list(size = 8, probs = c(0.3, 0.7))
)
)

# evaluates Multinom1 and Multinom2 at (1, 4)
vd$pdf(1, 4) # evaluates Multinom1 at (1, 4) and Multinom2 at (5, 3) vd$pdf(data = array(c(1,4,5,3), dim = c(1,2,2)))

# and the same across many samples
vd$pdf(data = array(c(1,2,4,3,5,1,3,7), dim = c(2,2,2)))  ### Method cdf() Returns named vector of cdfs from each wrapped Distribution. Same usage as $pdf.

#### Arguments

n

(numeric(1))
Number of points to simulate from the distribution. If length greater than $$1$$, then n <- length(n),

simplify

logical(1)
If TRUE (default) simplifies the return if possible to a numeric, otherwise returns a data.table::data.table.

### Method clone()

The objects of this class are cloneable with this method.

VectorDistribution$clone(deep = FALSE) #### Arguments deep Whether to make a deep clone. ## Examples  ## ------------------------------------------------ ## Method VectorDistribution$new
## ------------------------------------------------

if (FALSE) {
VectorDistribution$new( distribution = "Binomial", params = list( list(prob = 0.1, size = 2), list(prob = 0.6, size = 4), list(prob = 0.2, size = 6) ) ) VectorDistribution$new(
distribution = "Binomial",
params = data.table::data.table(prob = c(0.1, 0.6, 0.2), size = c(2, 4, 6))
)

# Alternatively
VectorDistribution$new( list( Binomial$new(prob = 0.1, size = 2),
Binomial$new(prob = 0.6, size = 4), Binomial$new(prob = 0.2, size = 6)
)
)
}

## ------------------------------------------------
## Method VectorDistribution$pdf ## ------------------------------------------------ vd <- VectorDistribution$new(
distribution = "Binomial",
params = data.frame(size = 9:10, prob = c(0.5,0.6)))

vd$pdf(2) #> Binom1 Binom2 #> 1: 0.0703125 0.01061683# Equivalently vd$pdf(2, 2)
#>       Binom1     Binom2
#> 1: 0.0703125 0.01061683
vd$pdf(1:2, 3:4) #> Binom1 Binom2 #> 1: 0.01757813 0.04246733 #> 2: 0.07031250 0.11147674# or as a matrix vd$pdf(data = matrix(1:4, nrow = 2))
#>        Binom1     Binom2
#> 1: 0.01757813 0.04246733
#> 2: 0.07031250 0.11147674
# when wrapping multivariate distributions, arrays are required
vd <- VectorDistribution$new( distribution = "Multinomial", params = list( list(size = 5, probs = c(0.1, 0.9)), list(size = 8, probs = c(0.3, 0.7)) ) ) # evaluates Multinom1 and Multinom2 at (1, 4) vd$pdf(1, 4)
#>    Multinom1 Multinom2
#> 1:   0.32805         0
# evaluates Multinom1 at (1, 4) and Multinom2 at (5, 3)
vd$pdf(data = array(c(1,4,5,3), dim = c(1,2,2))) #> Multinom1 Multinom2 #> 1: 0.32805 0.04667544 # and the same across many samples vd$pdf(data = array(c(1,2,4,3,5,1,3,7), dim = c(2,2,2)))
#>    Multinom1  Multinom2
#> 1:   0.32805 0.04667544
#> 2:   0.07290 0.19765032