Post · 11 min read· pp. 148–155

On the Distribution Functions of Order Statistics

Let Z(1)≤…≤Z(n)Z_{(1)} \le \ldots \le Z_{(n)} denote the order statistics of a collection of independent random variables, ZiZ_{i} indexed by i∈Ni \in N where ∣N∣=n|N| = n. Let FiF_i and fif_i be the distribution and density functions for each ii.

To generate, the order statistics we do the following:

Zi∼Fifor i∈NZ(1),…,Z(n)=sort([Zi for i∈N])\begin{align*} & Z_{i} \sim F_i\quad\textbf{for } i \in N \\ & Z_{(1)}, \ldots, Z_{(n)} = \mathrm{sort}\left([ Z_{i} \textbf{ for }i \in N ] \right) \end{align*}

In this post, we work out the distribution and density functions for each Z(k)Z_{(k)}.

There is no shortage of lecture notes and books online that describe the distribution functions for Z(1)Z_{(1)} (min) and Z(n)Z_{(n)} (max) as these cases are particularly simple and more commonly used; similarly, for the case of identically distributed random variables (where the FiF_is are equal). The more general (non-identical, but still independent) case turns out to not be too much harder. Why do I care about this problem? These distribution functions arise when analyzing order-based sampling without replacement designs, such as the Gumbel-top-k design that I have written about many times.

Before continuing, I should note that my derivation and implementation draws heavily on Aires (1999); specifically, Lemma 2.

Without further ado...

Distribution Function

F(k)N(t)=defPr(Z(k)≤t)=Pr(at least k of Z1…Zn are ≤t)=∑Y⊆N∣Y∣≥k(∏i∈YFi(t))⏟ values ≤t (∏i∈N∖Y(1−Fi(t)))⏟ values >t\begin{align*} F^N_{(k)}(t) &\overset{\mathrm{def}}{=} \mathrm{Pr}\left( Z_{(k)} \le t \right) \\ &= \mathrm{Pr}\left( \text{at least } k \text{ of } Z_1 \ldots Z_n \text{ are } \le t \right) \\ &= \sum_{\substack{ Y \subseteq N \\ |Y| \ge k}} \underbrace{\left( \prod_{i \in Y} F_i(t) \right)}_{ \text{ values } \le t} \, \underbrace{\left( \prod_{i \in N \smallsetminus Y } \left( 1-F_i(t) \right) \right)}_{ \text{ values } > t} \end{align*}
It is a sum over all the ways in which tt can be the kthk^{\text{th}} order statistics: there must be at least kk values that are ≤t\le t, and the remaining values that are >t> t. These events are mutually exclusive and exhaustive. Let YY be the set of variables that are ≤t\le t. Clearly, we require ∣Y∣≥k|Y| \ge k. Because the ZiZ_is are independent, the probability of the event that YY is the set of kk smallest elements is simply the product of the FiF_is in YY and (1−Fi)(1-F_i)s in the complement of YY.

It turns out that we can evaluate the exponential-size sum in O(n2)\mathcal{O}(n^2) time with a simple algorithm, which is essentially a probabilistic generalization of the dynamic program for evaluating the binomial coefficients, (nk){n \choose k}. Furthermore, we can simultaneously evaluate the distribution function for all nn order statistics in O(n2)\mathcal{O}(n^2) time. The algorithm for doing this computation efficiently is in the code section of this article.

Density Function

\begin{align} f^N_{(k)}(t) &\overset{\mathrm{def}}{=} \frac{\partial}{\partial t} F^N_{(k)}(t) \ &= \frac{\partial}{\partial t} \left[ \sum_{\substack{ Y \subseteq N: \ |Y| \ge k}} \left( \prod_{i \in Y} F_i(t) \right) \left( \prod_{i \in N \smallsetminus Y } \left(1-F_i(t)\right) \right) \right] \ &= \sum_j \sum_{\substack{ Y \subseteq N: \ |Y| \ge k}} (-1)^{1[j \notin Y]} f_j(t)
\left( \prod_{\substack{i \in Y \ i \ne j}} F_i(t) \right) \left( \prod_{\substack{i \in N \smallsetminus Y \ i \ne j}} \left(1-F_i(t)\right) \right) \ &= \sum_j \left(F^{N - j}{(k-1)}(t) - F^{N - j}{(k)}(t)\right) f_j(t) \end{align} where F(k−1)N−jF^{N - j}_{(k-1)} and F(k)N−jF^{N - j}_{(k)} are the distribution functions for the order statistics k−1k-1 and kk over the set of elements excluding the random variable ZjZ_j.

Code

text
class Ordered:
    def __init__(self, ds: 'list of distributions'):
        self.ds = ds
    def rvs(self, size=None):
        "Generate a sample of the order statistics"
        return np.sort([d.rvs(size) for d in self.ds], axis=0)
    def cdf(self, t):
        "Evaluate the CDF of the order statistics."
        p = np.array([d.cdf(t) for d in self.ds])
        return C(p, 1-p)[1:]
    def pdf(self, K, t):
        "Evaluate the PDF of the Kth order statistics."
        p = np.array([d.cdf(t) for d in self.ds])
        [_, d_p, d_q] = d_C(p, 1-p, K+1)   # off-by-one due to shift in cdf
        return sum((d_p[n] - d_q[n]) * self.ds[n].pdf(t) for n in range(len(self.ds)))

Utility Functions to Efficiently Sum over Weighted Subsets.

Definition: Let pip_i and qiq_i be inclusion and exclusion weights (respectively) for some set of elements i∈Ni \in N. The total weight of subsets of NN of size with size least KK is

CKN=def∑Y⊆N:∣Y∣≥K(∏i∈Ypi)(∏i∈N∖Yqi)C^N_K \overset{\mathrm{def}}{=} \sum_{\substack{Y \subseteq N:\\ |Y|\ge K}} \left(\prod_{i \in Y} p_i \right)\left(\prod_{i \in N \smallsetminus Y} q_i\right)
We can evaluate CKNC^N_K efficiently in O(n2)\mathcal{O}(n^2) with the following algorithms.

text
def B(p, q):
    """
    Compute the sum of weighted sets of size == k.
    p and q are the inclusion and exclusion weights, respectively
    """
    N = len(p)
    F = np.zeros((N+1,N+1), dtype=p.dtype)
    F[:,0] = 1
    for n in range(N):
        F[n+1,0] = q[n] * F[n,0]
    for k in range(1, N+1):
        for n in range(N):
            F[n+1,k] = q[n] * F[n,k] + p[n] * F[n,k-1]
    return F[N,:]
text
def C(p, q):
    """
    Compute the sum of weighted sets of size >= k.
    p and q are the inclusion and exclusion weights, respectively
    """
    N = len(p)
    b = B(p, q)
    c = np.zeros(N+2, dtype=p.dtype)
    for j in reversed(range(N+1)):
        c[j] += c[j+1]
        c[j] += b[j]
    return c

The method below is used to efficiently evaluate the gradient of the C function in time O(n2)\mathcal{O}(n^2). This method is used to evaluated the PDF. It was derived by manually applying algorithmic differentiation, but it could also be evaluated using an automatic differentiation toolkit (read more).

text
def d_C(p, q, K):
    "Evaluate ∇[C(p)[K]]"
    N = len(p)
    assert K > 0

    F = np.zeros((N+1, N+1))
    F[:,0] = 1
    for n in range(N):
        F[n+1,0] = q[n] * F[n,0]
    for k in range(1, N+1):
        for n in range(N):
            F[n+1,k] = q[n] * F[n,k] + p[n] * F[n,k-1]

    c = np.zeros(N+2, dtype=p.dtype)
    for j in reversed(range(N+1)):
        c[j] += F[N,j]
    for j in reversed(range(N+1)):
        c[j] += c[j+1]

    out = c[K]

    d_F = np.zeros_like(F)
    d_p = np.zeros_like(p)
    d_q = np.zeros_like(q)
    d_c = np.zeros_like(c)

    d_c[K] += 1
    for j in range(N+1):
        d_c[j+1] += d_c[j]
    for j in range(N+1):
        d_F[N,j] += d_c[j]

    for k in reversed(range(1, N+1)):
        for n in reversed(range(N)):
            d_F[n,k] += q[n] * d_F[n+1,k]
            d_F[n,k-1] += p[n] * d_F[n+1,k]
            d_q[n] += F[n,k] * d_F[n+1,k]
            d_p[n] += d_F[n+1,k] * F[n,k-1]

    for n in reversed(range(N)):
        d_q[n] += d_F[n+1,0] * F[n,0]
        d_F[n,0] += q[n] * d_F[n+1,0]

    return [out, d_p, d_q]

Tests

Synethic Data

Below, we test out our analytical method for computing the distribution function of independent normal distributions. The method should work for any collection of distributions in scipy.stats. You can even mix and match the distribution types. We will compare the analytical F(k)F_{(k)} to the empirical distribution of Z(k)Z_{(k)} using a large sample size.

text
import scipy.stats as st, numpy as np, pylab as pl
from numpy.random import uniform
np.random.seed(90210)
N = 16
m = Ordered([st.norm(uniform(-1, 1), uniform(0, 1)) for i in range(N)])
text
ts = np.linspace(-2, 2, 1000)
pl.figure(figsize=(12, 6))
for i in range(N):
    pl.plot(ts, m.ds[i].pdf(ts), label=i)
pl.legend(loc='best');

No description has been provided for this image

text
n_samples = 100_000
S = m.rvs(n_samples)

CDF

Below, I use my arsenal library to fit the empirical distribution function and compare it to the analytical solution.

text
from arsenal.maths import Empirical
ts = np.linspace(S.min(), S.max(), 200)
E = [Empirical(S[k]) for k in range(N)]
F = np.array([m.cdf(t) for t in ts]).T
text
ncols = 4
fig, ax = pl.subplots(figsize=(12,8), nrows=int(np.ceil(N/ncols)), ncols=ncols, 
                      sharex=True, sharey=True)
ax = np.atleast_2d(ax)
for k in range(N):
    ax_ = ax[k//ncols,k%ncols]
    ax_.plot(ts, E[k].cdf(ts), c='r', alpha=0.5, 
                      label = r'$F_{(%s)}(t)$' % (k+1))
    ax_.plot(ts, F[k], c='b', alpha=0.5, linestyle=':')
    ax_.legend(loc='best')
fig.tight_layout();

No description has been provided for this image

In the plots above, the red line is the empirical distribution funciton, and the blue dotted line is the analytical distribution function. In all cases, we see that the fit is good.

PDF

Below, we check the PDF against histograms.

text
fig, ax = pl.subplots(figsize=(12,8), nrows=int(np.ceil(N/ncols)), ncols=ncols)
ax = np.atleast_2d(ax)
for k in range(N):
    ax_ = ax[k//ncols,k%ncols]
    ax_.hist(E[k].x, density=True, color='lightcoral', bins=100)
    ts = np.linspace(E[k].x.min(), E[k].x.max(), 100)
    ax_.plot(ts, [m.pdf(k, t) for t in ts], c='k', label=r'$f_{(%s)}(t)$' % (k+1))
    ax_.legend(loc='best')
fig.tight_layout();

No description has been provided for this image

Closing

I would like to thank Lin Zheng and the commenter "Mechanical Turk" for pointing out a mistake in the original version of this article. I would also like to thank Ryan Cotterell for valuable feedback and discussions that greatly improved this article.

This Jupyter notebook is available for download.

Appendix

Symbolic tests for utility methods B and C

I have included some extra material for testing the efficient algorithms for summing over subsets of size =K=K and size ≥K\ge K. This strategy compares the efficient and inefficient algorithms (brute-force enumeration) using symbolic mathematics. Using symbolic mathematics ensures that the tests do not depend on any specific numerical values. Furthermore, it is helpful during debugging as we can see which terms in the output expressions are different.

text
import sympy
from sympy import Symbol
from arsenal.maths.combinatorics import powerset

def C_slow(p, q):
    N = len(p)
    z = [None for _ in range(N+1)]
    for K in range(N+1):
        z[K] = 0
        for Y in powerset(range(N)):
            if len(Y) >= K:
                z[K] += sympy.prod([p[i] for i in Y]) * sympy.prod([q[i] for i in range(N) if i not in Y])
    return z


def B_slow(p, q):
    N = len(p)
    z = [0 for _ in range(N+1)]
    for Y in powerset(range(N)):
        K = len(Y)
        z[K] += sympy.prod([p[i] for i in Y]) * sympy.prod([q[i] for i in range(N) if i not in Y])
    return z


def test_symbolic():
    Xs = np.array([Symbol(x) for x in 'uvwxyz'])
    Ys = np.array([sympy.Symbol(x) for x in 'UVWXYZ'])
    
    # Binomials
    for k in range(len(Xs)):
        have = B(Xs, Ys)[k].expand()
        want = B_slow(Xs, Ys)[k].expand()
        assert have == want
    print('Binomials size = K: passed')

    # Binomials >= size K.
    for k in range(len(Xs)):
        have = C(Xs, Ys)[k].expand()
        want = C_slow(Xs, Ys)[k].expand()
        assert have == want
    print('Binomials size ≥ K: passed')

test_symbolic()
text
Binomials size = K: passed
Binomials size ≥ K: passed

Inspecting the symbolic output

Below, we look at how the expression evaluated the subsets of size =K=K in the B method. As a mnemonic, lower-case symbols represent the weight of inclusion into the set, and an upper-case symbols represent the weight of exclusion into the set. We consider a set with $3$ elements.

text
b = B(np.array([Symbol(x) for x in 'xyz']),
      np.array([Symbol(x) for x in 'XYZ']))
text
sympy.Matrix([b]).T.expand()

[XYZXYz+XZy+YZxXyz+Yxz+Zxyxyz]\displaystyle \left[\begin{matrix}X Y Z\\X Y z + X Z y + Y Z x\\X y z + Y x z + Z x y\\x y z\end{matrix}\right]

The different set sizes are the rows of the vector above. The first row is the weight of sets of size $0$, which is XYZX Y Z because all $3$ elements are excluded. The next line is we see that each term has one lower-case factor; this is because there is one element included in the set. See the pattern?

Finite-difference test

Below, we test gradients of the C function used in the pdf computation via the strategy described here.

text
from arsenal.maths import fdcheck
def test_grad():
    N = 12
    p = np.random.uniform(0,1,size=N)
    for K in range(1, N+1):
        [_, d_p, d_q] = d_C(p, 1-p, K)
        fdcheck(lambda: C(p, 1-p)[K], p, d_p - d_q, quiet=True)
    print('fdcheck passed.')
test_grad()
text
fdcheck passed.

Properties of ∇C\nabla C

Lemma: For any j∈Nj \in N, the following differential identities hold:

∂∂pjCKN=CK−1N∖jand∂∂qjCKN=CKN∖j\frac{\partial}{\partial p_j} C^N_K = C^{N \smallsetminus j}_{K-1} \quad\text{and}\quad \frac{\partial}{\partial q_j} C^N_K = C^{N \smallsetminus j}_{K}

We use these identities in our derivation of the PDF.

text
def test_gradient_properties(m):
    N = len(m.ds)
    for k in range(1, len(m.ds)):
        ts = [m.rvs()[k] for _ in range(10)]
        for t in ts:
            w = m.pdf(k, t)
            ps = np.array([d.cdf(t) for d in m.ds])
            [_, d_p, d_q] = d_C(ps, 1-ps, k+1)
            h = 0.0
            for j in range(N):
                ds = list(m.ds); ds.pop(j)
                F = Ordered(ds).cdf(t)
                # we use this identity in fast method to evaluate the pdf.
                h += (F[k-1] - F[k]) * m.ds[j].pdf(t) 
                assert np.allclose(F[k-1] - F[k], d_p[j] - d_q[j])
                assert np.allclose(F[k-1], d_p[j])
                assert np.allclose(F[k], d_q[j])
            assert np.allclose(h, w)
    print('gradient_properties: pass.')
        
test_gradient_properties(m)
text
gradient_properties: pass.