Wrapper used to construct a mixture of two or more distributions.

## Details

A mixture distribution is defined by

$$F_P(x) = w_1 F_{X1}(x) * ... * w_n F_{XN}(x)$$ #nolint where $$F_P$$ is the cdf of the mixture distribution, $$X1,...,XN$$ are independent distributions, and $$w1,...,wN$$ are weights for the mixture.

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

## Super classes

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

## Methods

### Public methods

Inherited methods

### Method new()

Creates a new instance of this R6 class.

MixtureDistribution$new( distlist = NULL, weights = "uniform", distribution = NULL, params = NULL, shared_params = NULL, name = NULL, short_name = NULL, decorators = NULL, vecdist = NULL ) #### Arguments distlist (list()) List of Distributions. weights (character(1)|numeric()) Weights to use in the resulting mixture. If all distributions are weighted equally then "uniform" provides a much faster implementation, otherwise a vector of length equal to the number of wrapped distributions, this is automatically scaled internally. 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. #### Examples MixtureDistribution$new(list(Binomial$new(prob = 0.5, size = 10), Binomial$new()),
weights = c(0.2, 0.8)
)


### Method strprint()

Printable string representation of the MixtureDistribution. Primarily used internally.

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

m <- MixtureDistribution$new(list(Binomial$new(prob = 0.5, size = 10), Binomial$new()), weights = c(0.2, 0.8) ) m$pdf(1:5)
m$pdf(1) # also possible but unlikely to be used m$pdf(1, 2)


### Method cdf()

Cumulative distribution function of the mixture distribution. Computed by $$F_M(x) = \sum_i (F_i)(x)*w_i$$ where $$w_i$$ is the vector of weights and $$F_i$$ are the cdfs of the wrapped distributions.

MixtureDistribution$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. @examples m <- MixtureDistribution$new(list(Binomial$new(prob = 0.5, size = 10), Binomial$new()), weights = c(0.2, 0.8) ) m$cdf(1:5) 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() The quantile function is not implemented for mixture distributions. #### Usage MixtureDistribution$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()

Simulation function for mixture distributions. Samples are drawn from a mixture by first sampling Multinomial(probs = weights, size = n), then sampling each distribution according to the samples from the Multinomial, and finally randomly permuting these draws.

MixtureDistribution$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. #### Examples m <- MixtureDistribution$new(distribution = "Normal",
params = data.table::data.table(mean = 1:2), shared_params = list(sd = 1))

#### Arguments

deep

Whether to make a deep clone.

## Examples


## ------------------------------------------------
## Method MixtureDistribution$new ## ------------------------------------------------ MixtureDistribution$new(list(Binomial$new(prob = 0.5, size = 10), Binomial$new()),
weights = c(0.2, 0.8)
)
#> Binom1 wX Binom2
## ------------------------------------------------
## Method MixtureDistribution$pdf ## ------------------------------------------------ m <- MixtureDistribution$new(list(Binomial$new(prob = 0.5, size = 10), Binomial$new()),
weights = c(0.2, 0.8)
)
m$pdf(1:5) #> [1] 0.009765625 0.043945312 0.117187500 0.205078125 0.246093750m$pdf(1)
#> [1] 0.009765625# also possible but unlikely to be used
m$pdf(1, 2) #> [1] 0.03710937 ## ------------------------------------------------ ## Method MixtureDistribution$rand
## ------------------------------------------------

m <- MixtureDistribution$new(distribution = "Normal", params = data.table::data.table(mean = 1:2), shared_params = list(sd = 1)) m$rand(5)
#> [1] 1.9146069 0.8518175 1.3770416 2.9416995 1.4957673