# Aggregators

Aggregation is the process of reducing a collection of values to a single value that represents some property of the collection. An aggregate function or aggregator is a function that performs this operation. This section describes predefined aggregators and a simple mechanism to create your own. Examples of aggregators are: count, largest value, mean...

## Aggregator groups and aggregators

A property like the number of elements or the number of elements that are not missing
in a collection is meaningful for all collections regardless of their element type.
The result is always an integer.
A property like the first element is also universally relevant,
but the type of the result depends on the element type. More than that,
the type of the result is *equal to* the element type.
These two examples illustrate the two main kinds of aggregators: those where
the result is of a specific type, and those where the result type is
the same as the element type.

The most common aggregators are defined as static properties or methods of the Aggregators class, and are listed below.

Member | Description |
---|---|

Computes the number of non-missing values. | |

Returns the largest value. | |

Returns the smallest value. | |

Returns the first non-missing value. | |

Returns the last non-missing value. | |

Returns the first non-missing value after skipping the specified number of non-missing values. | |

Returns the mean of the non-missing values. | |

Returns the sum of the non-missing values. | |

Returns the sample variance of the non-missing values. | |

Returns the sample standard deviation of the non-missing values. | |

Returns the skewness of the non-missing values. | |

Returns the kurtosis of the non-missing values. | |

Returns the median of the non-missing values. | |

Returns the 25% quantile of the non-missing values. | |

Returns the 75% quantile of the non-missing values. | |

Returns the quantile at the specified probability of the non-missing values. | |

Returns the sum of the non-missing values preserving the element type. |

The above properties and methods return an *aggregator group*.
An aggregator group represents an aggregation operation
in a way that is independent of the element type. When the aggregator group
is applied to a collection, an aggregator function for the element type
of the collection is selected to perform the actual operation.
AggregatorGroup
is an abstract class that represents an aggregator group.
It has two descendant classes corresponding to the two kinds of aggregators
mentioned above: AggregatorGroup<T>
represents an aggregator group where the generic type argument is the return type,
and TypePreservingAggregatorGroup
represents an aggregator group where the return type is the same as the element type.

Aggregators that compute numerical descriptive statistics, like Mean and Variance always return a Double value regardless of the element type. To preserve the element type in the result, use the 'exact' variant, like ExactSum.

## Aggregating over multiple collections

Some aggregations, such as the correlation between two variables, involve more than one collection. The Aggregators class defines a number of aggregators that operate on more than one collection:

Member | Description |
---|---|

Computes the correlation between two sets of values. | |

Computes the covariance between two sets of values. | |

Computes the mean of one set of values weighted by another. | |

Computes the standard deviation of one set of values weighted by another. | |

Computes the variance of one set of values weighted by another. |

## User-defined aggregators

The simplest way to define an aggregator that is not available from the Aggregators class is to use the Aggregators.Create method. This method takes two arguments: a name for the aggregator, and a delegate that maps a vector to the aggregated value. The following creates an aggregator that computes the geometric mean:

```
var agg = Aggregators.Create("geo.mean",
(Vector<double> x) => Stats.GeometricMean(x));
```