# Aggreatation of random variables

## Sum and Mean of several correlated random variables

`Base.sum`

— Method```
sum(dv::AbstractDistributionVector, ms=PassMissing(); <keyword arguments>)
sum(dv::AbstractDistributionVector, corr, ms=PassMissing(); <keyword arguments>)
sum(dv::AbstractDistributionVector, acf, ms=PassMissing(); <keyword arguments>)
mean(dv::AbstractDistributionVector, ms=PassMissing(); <keyword arguments>)
mean(dv::AbstractDistributionVector, corr, ms=PassMissing(); <keyword arguments>)
mean(dv::AbstractDistributionVector, acf, ms=PassMissing(); <keyword arguments>)
```

Compute the distribution of the sum or mean of correlated random variables.

**Arguments**

`dv`

: The vector of distributions, see`AbstractDistributionVector`

`ms`

:`MissingStrategy`

: set to`SkipMissing()`

or`ExactMissing()`

to consciously care for missing values in`dv`

.

An optional second arguments supports correlation between random variables.

`corr::Symmetric`

: correlation matrix, or`acf::AutoCorrelationFunction`

: coefficients of the`AutoCorrelationFunction`

Keyword arguments:

`isgapfilled::AbstractVector{Bool}`

: set to true for records that should contribute to the sum but not to the decrease of relative uncertainty with increasing number of records, e.g. for missing records that have been estimated (gapfilled).

The sums of correlated variables require extra allocation and support an additional keyword parameter

`storage`

: a mutable`AbstractVector{eltype(D)}`

of length of`dv`

that provides storage space to avoid additional allocations.

When implementing sum and mean for another Distribution `MyDist`

, the `AutoCorrelationFunction`

-based method falls back to the `Symmetric`

correlation based method. Hence one only needs to define methods

```
sum(dv::AbstractDistributionVector{<:MyDist},
ms::MissingStrategy=PassMissing())
sum(dv::AbstractDistributionVector{<:MyDist}, corr::Symmetric,
ms::MissingStrategy=PassMissing())
```

For examples see

- Sum-and-mean-of-correlated-normal-random-variables
- Sum-and-mean-of-correlated-lognormal-random-variables

## Helpers

### AutoCorrelationFunction

The autocorrelation function describes the correlation of random variables of a time series at increasing lags, i.e. $cor(x_{i+2}, x_{i})$ for lag 2. It is a Real-valued vector with the first entry the correlation at lag 0.

To support unambiguous dispatch on an autocorrelation function, type `AutoCorrelationFunction`

wraps such a vector.

`DistributionVectors.AutoCorrelationFunction`

— Type`AutoCorrelationFunction{T}`

A representation of the autocorrelation function.

It supports accessing the coeficients starting from lag 0 by

`coef(acf::AutoCorrelationFunction)`

: implements StatsBase.coef`coef(acf::AutoCorrelationFunction, lag::Integer)`

: coefficient for lag

Wrapping the vector of coefficients into its own type helps avoiding method ambiguities.

**Examples**

```
using StatsBase: coef
acf = AutoCorrelationFunction([1,0.4,0.1])
coef(acf) == [1,0.4,0.1]
coef(acf,1) == 0.4
```