VectorDistribution: Vectorise Distributions

VectorDistributionR Documentation

Vectorise Distributions

Description

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.

Super classes

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

Active bindings

modelTable

Returns reference table of wrapped Distributions.

distlist

Returns list of constructed wrapped Distributions.

ids

Returns ids of constructed wrapped Distributions.

Methods

Public methods

Inherited methods

Method new()

Creates a new instance of this R6 class.

Usage
VectorDistribution$new(
  distlist = NULL,
  distribution = NULL,
  params = NULL,
  shared_params = NULL,
  name = NULL,
  short_name = NULL,
  decorators = NULL,
  vecdist = NULL,
  ids = 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.

ids

(character())
Optional ids for wrapped distributions in vector, should be unique and of same length as the number of distributions.

...

Unused

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),
  Binomial$new(prob = 0.2, size = 6)
  )
)
}

Method getParameterValue()

Returns the value of the supplied parameter.

Usage
VectorDistribution$getParameterValue(id, ...)
Arguments
id

character()
id of parameter value to return.

...

Unused


Method wrappedModels()

Returns model(s) wrapped by this wrapper.

Usage
VectorDistribution$wrappedModels(model = NULL)
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.

Usage
VectorDistribution$strprint(n = 10)
Arguments
n

(integer(1))
Number of distributions to include when printing.


Method mean()

Returns named vector of means from each wrapped Distribution.

Usage
VectorDistribution$mean(...)
Arguments
...

Passed to CoreStatistics$genExp if numeric.


Method mode()

Returns named vector of modes from each wrapped Distribution.

Usage
VectorDistribution$mode(which = "all")
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.

Usage
VectorDistribution$median()

Method variance()

Returns named vector of variances from each wrapped Distribution.

Usage
VectorDistribution$variance(...)
Arguments
...

Passed to CoreStatistics$genExp if numeric.


Method skewness()

Returns named vector of skewness from each wrapped Distribution.

Usage
VectorDistribution$skewness(...)
Arguments
...

Passed to CoreStatistics$genExp if numeric.


Method kurtosis()

Returns named vector of kurtosis from each wrapped Distribution.

Usage
VectorDistribution$kurtosis(excess = TRUE, ...)
Arguments
excess

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

...

Passed to CoreStatistics$genExp if numeric.


Method entropy()

Returns named vector of entropy from each wrapped Distribution.

Usage
VectorDistribution$entropy(base = 2, ...)
Arguments
base

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

...

Passed to CoreStatistics$genExp if numeric.


Method mgf()

Returns named vector of mgf from each wrapped Distribution.

Usage
VectorDistribution$mgf(t, ...)
Arguments
t

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

...

Passed to CoreStatistics$genExp if numeric.


Method cf()

Returns named vector of cf from each wrapped Distribution.

Usage
VectorDistribution$cf(t, ...)
Arguments
t

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

...

Passed to CoreStatistics$genExp if numeric.


Method pgf()

Returns named vector of pgf from each wrapped Distribution.

Usage
VectorDistribution$pgf(z, ...)
Arguments
z

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

...

Passed to CoreStatistics$genExp if numeric.


Method pdf()

Returns named vector of pdfs from each wrapped Distribution.

Usage
VectorDistribution$pdf(..., log = FALSE, simplify = TRUE, data = NULL)
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.

Usage
VectorDistribution$cdf(
  ...,
  lower.tail = TRUE,
  log.p = FALSE,
  simplify = TRUE,
  data = NULL
)
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.

lower.tail

(logical(1))
If TRUE (default), probabilities are X <= x, otherwise, P(X > x).

log.p

(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.


Method quantile()

Returns named vector of quantiles from each wrapped Distribution. Same usage as $cdf.

Usage
VectorDistribution$quantile(
  ...,
  lower.tail = TRUE,
  log.p = FALSE,
  simplify = TRUE,
  data = NULL
)
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.

lower.tail

(logical(1))
If TRUE (default), probabilities are X <= x, otherwise, P(X > x).

log.p

(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.


Method rand()

Returns data.table::data.table of draws from each wrapped Distribution.

Usage
VectorDistribution$rand(n, simplify = TRUE)
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.

Usage
VectorDistribution$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

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

Examples


## ------------------------------------------------
## Method `VectorDistribution$new`
## ------------------------------------------------

## Not run: 
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)
  )
)

## End(Not run)

## ------------------------------------------------
## Method `VectorDistribution$pdf`
## ------------------------------------------------

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)))

distr6 documentation built on March 28, 2022, 1:05 a.m.