# Order Statistics

The $i$th Order Statistic of a random sample of size $n$ from a univariate distribution is the $i$th element after sorting in increasing order. As a special case, the first and $n$th order statistics are the minimum and maximum of the sample, while for odd $n$, the $\lceil \frac{n}{2} \rceil$th entry is the sample median.

Given any univariate distribution and the sample size $n$, we can construct the distribution of its $i$th order statistic:

`Distributions.OrderStatistic`

— Type`OrderStatistic{D<:UnivariateDistribution,S<:ValueSupport} <: UnivariateDistribution{S}`

The distribution of an order statistic from IID samples from a univariate distribution.

`OrderStatistic(dist::UnivariateDistribution, n::Int, rank::Int; check_args::Bool=true)`

Construct the distribution of the `rank`

$=i$th order statistic from `n`

independent samples from `dist`

.

The $i$th order statistic of a sample is the $i$th element of the sorted sample. For example, the 1st order statistic is the sample minimum, while the $n$th order statistic is the sample maximum.

If $f$ is the probability density (mass) function of `dist`

with distribution function $F$, then the probability density function $g$ of the order statistic for continuous `dist`

is

\[g(x; n, i) = {n \choose i} [F(x)]^{i-1} [1 - F(x)]^{n-i} f(x),\]

and the probability mass function $g$ of the order statistic for discrete `dist`

is

\[g(x; n, i) = \sum_{k=i}^n {n \choose k} \left( [F(x)]^k [1 - F(x)]^{n-k} - [F(x_-)]^k [1 - F(x_-)]^{n-k} \right),\]

where $x_-$ is the largest element in the support of `dist`

less than $x$.

For the joint distribution of a subset of order statistics, use `JointOrderStatistics`

instead.

**Examples**

```
OrderStatistic(Cauchy(), 10, 1) # distribution of the sample minimum
OrderStatistic(DiscreteUniform(10), 10, 10) # distribution of the sample maximum
OrderStatistic(Gamma(1, 1), 11, 5) # distribution of the sample median
```

If we are interested in more than one order statistic, for continuous univariate distributions we can also construct the joint distribution of order statistics:

`Distributions.JointOrderStatistics`

— Type`JointOrderStatistics <: ContinuousMultivariateDistribution`

The joint distribution of a subset of order statistics from a sample from a continuous univariate distribution.

```
JointOrderStatistics(
dist::ContinuousUnivariateDistribution,
n::Int,
ranks=Base.OneTo(n);
check_args::Bool=true,
)
```

Construct the joint distribution of order statistics for the specified `ranks`

from an IID sample of size `n`

from `dist`

.

The $i$th order statistic of a sample is the $i$th element of the sorted sample. For example, the 1st order statistic is the sample minimum, while the $n$th order statistic is the sample maximum.

`ranks`

must be a sorted vector or tuple of unique `Int`

s between 1 and `n`

.

For a single order statistic, use `OrderStatistic`

instead.

**Examples**

```
JointOrderStatistics(Normal(), 10) # Product(fill(Normal(), 10)) restricted to ordered vectors
JointOrderStatistics(Cauchy(), 10, 2:9) # joint distribution of all but the extrema
JointOrderStatistics(Cauchy(), 10, (1, 10)) # joint distribution of only the extrema
```