Chapter 8
25 min read
Section 57 of 175

Order Statistics

Transformations of Random Variables

Learning Objectives

By the end of this section, you will be able to:

  1. Define order statistics and understand their role as the sorted values of a random sample.
  2. Derive the PDF of the k-th order statistic for any continuous distribution using the multinomial argument.
  3. Apply the Uniform-Beta theorem to compute exact distributions, expected values, and variances for order statistics of uniform samples.
  4. Understand joint distributions of order statistics and why they are never independent.
  5. Connect order statistics to ML applications: max pooling, top-K selection, quantile regression, and extreme value problems.
  6. Implement order statistic computations in Python for statistical inference and machine learning applications.
Why This Matters for AI/ML Engineers: Order statistics are everywhere in deep learning. Max pooling selects the maximum (the n-th order statistic). Beam search keeps the top-K candidates (the K largest order statistics). Quantile regression estimates conditional quantiles. Robust statistics uses medians and trimmed means. Understanding order statistics gives you a theoretical foundation for these fundamental operations.

The Story: Ranking and Extremes

Why Do We Need Order Statistics?

Consider these questions from different domains:

  • Quality Control: What is the probability that the weakest component in a batch of 100 will fail within the first year?
  • Climate Science: What is the expected maximum temperature over the next decade?
  • Finance: What is the Value-at-Risk (5th percentile of losses)?
  • Deep Learning: In max pooling, what is the distribution of the pooled output?

All these questions ask about ranked values—the minimum, maximum, median, or specific percentiles of a sample. Order statistics provide the mathematical framework to answer them.

Historical Context

The study of order statistics emerged from practical problems in the late 19th and early 20th centuries. Francis Galton developed methods for studying medians and percentiles in his work on heredity. Karl Pearson used order statistics for goodness-of-fit tests. The comprehensive theory was developed by statisticians including S.S. Wilks, Harald Cramér, and Herbert David.

Key Insight: Order statistics transform the problem from "what values might we observe?" to "what is the distribution of ranked values?" This shift in perspective is powerful: it lets us study extremes, quantiles, and robust estimators with precision.

Definitions and Notation

What Are Order Statistics?

Let X1,X2,,XnX_1, X_2, \ldots, X_n be i.i.d. random variables with continuous CDF F(x)F(x) and PDF f(x)f(x).

Definition: The order statistics are the random variables X(1),X(2),,X(n)X_{(1)}, X_{(2)}, \ldots, X_{(n)} obtained by sorting the sample in ascending order:X(1)X(2)X(n)X_{(1)} \leq X_{(2)} \leq \cdots \leq X_{(n)}where:
  • X(1)=min{X1,,Xn}X_{(1)} = \min\{X_1, \ldots, X_n\} is the minimum
  • X(n)=max{X1,,Xn}X_{(n)} = \max\{X_1, \ldots, X_n\} is the maximum
  • X(k)X_{(k)} is the k-th smallest value (k-th order statistic)

Notation: We use parentheses in subscripts X(k)X_{(k)} to distinguish order statistics from the original sample XkX_k. This convention is standard in statistics.

Crucial Point: The original observations X1,,XnX_1, \ldots, X_n are i.i.d., but the order statistics X(1),,X(n)X_{(1)}, \ldots, X_{(n)} are not independent. The ordering constraint X(1)X(2)X_{(1)} \leq X_{(2)} \leq \cdotsinduces strong dependence between them.

Interactive: PDF Explorer

Explore how the distribution of order statistics changes with sample size n and order k. Compare the order statistic PDF to the parent distribution:


Marginal Distributions

PDF of the k-th Order Statistic

The key result for order statistics is the formula for the marginal PDF of X(k)X_{(k)}:

Theorem: For i.i.d. continuous random variables with CDF F(x)F(x) and PDF f(x)f(x), the PDF of the k-th order statistic is:fX(k)(x)=n!(k1)!(nk)![F(x)]k1[1F(x)]nkf(x)f_{X_{(k)}}(x) = \frac{n!}{(k-1)!(n-k)!} [F(x)]^{k-1} [1-F(x)]^{n-k} f(x)

Intuitive derivation: For X(k)X_{(k)} to be at value xx:

  1. Exactly k1k-1 observations must be less than xx (probability [F(x)]k1[F(x)]^{k-1})
  2. Exactly nkn-k observations must be greater than xx (probability [1F(x)]nk[1-F(x)]^{n-k})
  3. One observation must be at xx (density f(x)f(x))
  4. The multinomial coefficient n!(k1)!(nk)!\frac{n!}{(k-1)!(n-k)!} counts the ways to arrange these

Special Cases: Minimum and Maximum

The minimum and maximum have particularly simple forms:

StatisticPDF FormulaCDF Formula
Minimum X₍₁₎n × [1 - F(x)]^(n-1) × f(x)1 - [1 - F(x)]ⁿ
Maximum X₍ₙ₎n × [F(x)]^(n-1) × f(x)[F(x)]ⁿ
Why these make sense:
  • Maximum CDF: P(max ≤ x) = P(all ≤ x) = [F(x)]ⁿ (independence)
  • Minimum CDF: P(min ≤ x) = 1 - P(all > x) = 1 - [1 - F(x)]ⁿ

Interactive: Monte Carlo Simulator

Generate random samples and observe how the empirical distributions of order statistics match the theoretical predictions:


The Uniform-Beta Connection

Theorem: Order Statistics of Uniform Samples

A beautiful and immensely useful result emerges for Uniform(0,1) samples:

Theorem (Uniform-Beta): If X1,,XniidUniform(0,1)X_1, \ldots, X_n \stackrel{iid}{\sim} \text{Uniform}(0, 1), then:X(k)Beta(k,nk+1)X_{(k)} \sim \text{Beta}(k, n-k+1)The k-th order statistic follows a Beta distribution with parameters α=k\alpha = k and β=nk+1\beta = n - k + 1.

Proof sketch: For Uniform(0,1), we have F(x)=xF(x) = x and f(x)=1f(x) = 1 for x[0,1]x \in [0, 1]. Substituting into the order statistic PDF formula:

fX(k)(x)=n!(k1)!(nk)!xk1(1x)nkf_{X_{(k)}}(x) = \frac{n!}{(k-1)!(n-k)!} x^{k-1} (1-x)^{n-k}

This is exactly the Beta(kk, nk+1n-k+1) PDF!

Why This Matters: The Uniform-Beta connection means we can use all the machinery of the Beta distribution (closed-form moments, mode, etc.) for uniform order statistics. And since any continuous distribution can be transformed to uniform via its CDF (probability integral transform), this result has broad applications.

Expected Values and Variances

For Uniform(0,1) order statistics, the moments have elegant closed forms:

QuantityFormulaInterpretation
E[X₍ₖ₎]k / (n + 1)Order statistics are evenly spaced in expectation
Var(X₍ₖ₎)k(n-k+1) / [(n+1)²(n+2)]Middle order statistics have highest variance
E[X₍ₙ₎] (max)n / (n + 1)Expected max approaches 1 as n → ∞
E[X₍₁₎] (min)1 / (n + 1)Expected min approaches 0 as n → ∞
E[Range](n - 1) / (n + 1)Expected range approaches 1 as n → ∞
Intuition for E[X₍ₖ₎] = k/(n+1): Imagine placing n random points on [0,1]. They divide the interval into n+1 gaps. The k-th point, on average, sits after k gaps out of n+1, so its expected position is k/(n+1).

Joint Distributions

Joint PDF of Two Order Statistics

For two order statistics X(i)X_{(i)} and X(j)X_{(j)} where i<ji < j:

Theorem: The joint PDF of (X(i),X(j))(X_{(i)}, X_{(j)}) is:fX(i),X(j)(x,y)=n!(i1)!(ji1)!(nj)![F(x)]i1[F(y)F(x)]ji1[1F(y)]njf(x)f(y)f_{X_{(i)}, X_{(j)}}(x, y) = \frac{n!}{(i-1)!(j-i-1)!(n-j)!} [F(x)]^{i-1} [F(y) - F(x)]^{j-i-1} [1-F(y)]^{n-j} f(x) f(y)for x<yx < y (and zero otherwise).

Interpretation: The joint PDF factors into:

  • [F(x)]i1[F(x)]^{i-1}: probability that (i-1) values are below x
  • [F(y)F(x)]ji1[F(y) - F(x)]^{j-i-1}: probability that (j-i-1) values are between x and y
  • [1F(y)]nj[1-F(y)]^{n-j}: probability that (n-j) values are above y
  • f(x)f(y)f(x) f(y): densities at the two points

Distribution of the Range

A particularly important joint distribution is between the minimum and maximum, which determines the range:

R=X(n)X(1)R = X_{(n)} - X_{(1)}

For Uniform(0,1), the range has the simple PDF:

fR(r)=n(n1)rn2(1r),0<r<1f_R(r) = n(n-1) r^{n-2}(1-r), \quad 0 < r < 1

Interactive: Joint Distribution Explorer

Visualize the joint distribution of order statistics and the range distribution:


Sample Quantiles

Quantiles and Percentiles

Order statistics provide the foundation for sample quantiles. The p-th quantile (or 100p-th percentile) is a value below which approximately proportion p of the data falls.

Definition: For 0<p<10 < p < 1, the p-th sample quantile Q^p\hat{Q}_p is typically defined as:Q^p=X(np)\hat{Q}_p = X_{(\lceil np \rceil)}or with linear interpolation between adjacent order statistics for smoother behavior.

Common quantiles have special names:

Namep-valueOrder Statistic (for n=100)
Median0.5X₍₅₀₎ or average of X₍₅₀₎ and X₍₅₁₎
First Quartile (Q1)0.25X₍₂₅₎
Third Quartile (Q3)0.75X₍₇₅₎
5th Percentile0.05X₍₅₎
95th Percentile0.95X₍₉₅₎

Asymptotic Distribution

Sample quantiles have a well-known asymptotic distribution:

Theorem (Asymptotic Normality of Sample Quantiles): For i.i.d. samples from a distribution with density ff at the p-th quantile ξp\xi_p:n(Q^pξp)dN(0,p(1p)[f(ξp)]2)\sqrt{n}(\hat{Q}_p - \xi_p) \xrightarrow{d} N\left(0, \frac{p(1-p)}{[f(\xi_p)]^2}\right)
Implications:
  • The sample median has variance proportional to 1/[f(median)]² — flat regions give high variance
  • Extreme quantiles (p near 0 or 1) also have higher variance due to the p(1-p) term
  • This is the theoretical foundation for quantile regression inference

AI/ML Applications

Order statistics appear throughout machine learning, often in disguised forms. Let's explore the key connections.

Max Pooling in CNNs

Max pooling is perhaps the most visible application of order statistics in deep learning:

Max Pooling = Maximum Order StatisticIn a 2×2 max pooling layer with stride 2, each output value is X(4)X_{(4)} of the 4 input values in that window—the maximum (4th order statistic of a sample of size 4).

Why max pooling works:

  • Translation invariance: If a feature detector fires anywhere in the pooling window, the maximum captures it regardless of exact position.
  • Sparsity: Only the maximum gets gradient during backpropagation, acting as a form of sparse attention.
  • Dimensionality reduction: Output size is reduced while preserving the strongest activations.

Top-K Selection

Top-K selection finds the K largest order statistics:

Top-K={X(n),X(n1),,X(nK+1)}\text{Top-K} = \{X_{(n)}, X_{(n-1)}, \ldots, X_{(n-K+1)}\}

Applications in ML:

ApplicationWhat It DoesOrder Statistic View
Top-K AccuracyCorrect if true label is in top K predictionsCheck if true label ∈ {X₍ₙ₎, ..., X₍ₙ₋ₖ₊₁₎}
Beam SearchKeep top K candidates at each stepSelect K largest order statistics
Sparse AttentionAttend only to top K keysSoftmax over top K = truncated attention
Hard Negative MiningSelect K hardest examplesTop K by loss = K largest losses

Interactive: ML Applications

Explore max pooling and top-K selection interactively:

Other Applications


Python Implementation

Here is a comprehensive Python implementation of order statistic computations and their ML applications:

Order Statistics: Complete Python Implementation
🐍order_statistics.py
1NumPy Import

NumPy provides efficient array operations for order statistics computations and simulations.

5Order Statistic PDF

The core formula for the PDF of the k-th order statistic. This is the fundamental result that tells us how order statistics are distributed.

EXAMPLE
For n=5, k=1 (minimum): f(x) = 5 × [1-F(x)]^4 × f(x)
22Log-Space Computation

We use log-space to avoid numerical underflow when computing factorials and high powers. This is essential for large n.

31Uniform-Beta Connection

For Uniform(0,1) samples, the k-th order statistic follows Beta(k, n-k+1). This elegant result makes computations much simpler!

37Expected Value Formula

For Uniform(0,1): E[X_(k)] = k/(n+1). This simple formula shows that order statistics are evenly spaced in expectation.

EXAMPLE
For n=4: E[X_(1)] = 0.2, E[X_(2)] = 0.4, E[X_(3)] = 0.6, E[X_(4)] = 0.8
50Range Distribution

The range R = X_(n) - X_(1) measures the spread of the sample. For Uniform(0,1), E[R] = (n-1)/(n+1) approaches 1 as n → ∞.

58Joint PDF

The joint distribution of two order statistics X_(i) and X_(j) where i < j. Note: order statistics are NOT independent!

83Sample Quantiles

Sample quantiles are directly related to order statistics. The p-th quantile is approximately X_(ceil(np)).

90All Order Statistics

Getting all order statistics is simple: just sort the data! X_(1) is the minimum, X_(n) is the maximum.

96Monte Carlo Simulation

Validate theoretical formulas by simulation. Generate many samples, compute order statistics, and compare empirical to theoretical.

125Max Pooling

Max pooling in CNNs is exactly the maximum order statistic! Each output is the max of a window, providing translation invariance.

EXAMPLE
2×2 max pool: output = X_(4) of 4 values = maximum
147Top-K Selection

Top-K returns the K largest order statistics: X_(n), X_(n-1), ..., X_(n-k+1). Used in beam search, top-K accuracy, and attention.

207 lines without explanation
1import numpy as np
2from scipy import stats, special
3from typing import Tuple, List
4import matplotlib.pyplot as plt
5
6def order_statistic_pdf(x: float, k: int, n: int,
7                        parent_cdf: callable,
8                        parent_pdf: callable) -> float:
9    """
10    Compute PDF of the k-th order statistic at point x.
11
12    Parameters:
13    -----------
14    x : float - Point at which to evaluate PDF
15    k : int - Order of the statistic (1 = minimum, n = maximum)
16    n : int - Sample size
17    parent_cdf : callable - CDF of parent distribution F(x)
18    parent_pdf : callable - PDF of parent distribution f(x)
19
20    Returns:
21    --------
22    PDF value at x
23
24    Formula: f_{X_(k)}(x) = n!/(k-1)!(n-k)! [F(x)]^{k-1} [1-F(x)]^{n-k} f(x)
25    """
26    F = parent_cdf(x)
27    f = parent_pdf(x)
28
29    # Use log-space for numerical stability
30    log_coeff = (special.gammaln(n + 1) -
31                 special.gammaln(k) -
32                 special.gammaln(n - k + 1))
33    log_pdf = (log_coeff +
34               (k - 1) * np.log(np.maximum(F, 1e-300)) +
35               (n - k) * np.log(np.maximum(1 - F, 1e-300)) +
36               np.log(np.maximum(f, 1e-300)))
37
38    return np.exp(log_pdf)
39
40def uniform_order_statistic_pdf(x: float, k: int, n: int) -> float:
41    """
42    PDF of k-th order statistic from Uniform(0,1) samples.
43    This is the Beta(k, n-k+1) distribution!
44    """
45    if x < 0 or x > 1:
46        return 0.0
47    return stats.beta.pdf(x, k, n - k + 1)
48
49def expected_order_statistic_uniform(k: int, n: int) -> Tuple[float, float]:
50    """
51    Expected value and variance of X_(k) for Uniform(0,1).
52
53    E[X_(k)] = k / (n + 1)
54    Var(X_(k)) = k(n - k + 1) / ((n + 1)^2 (n + 2))
55    """
56    mean = k / (n + 1)
57    var = k * (n - k + 1) / ((n + 1)**2 * (n + 2))
58    return mean, var
59
60def expected_range_uniform(n: int) -> Tuple[float, float]:
61    """
62    Expected value and variance of range R = X_(n) - X_(1) for Uniform(0,1).
63
64    E[R] = (n - 1) / (n + 1)
65    Var(R) = 2(n-1) / ((n+1)^2 (n+2))
66    """
67    mean = (n - 1) / (n + 1)
68    var = 2 * (n - 1) / ((n + 1)**2 * (n + 2))
69    return mean, var
70
71def joint_order_statistic_pdf(x: float, y: float,
72                              i: int, j: int, n: int) -> float:
73    """
74    Joint PDF of (X_(i), X_(j)) for Uniform(0,1), where i < j.
75
76    f(x, y) = n! / ((i-1)!(j-i-1)!(n-j)!)
77              * x^{i-1} * (y-x)^{j-i-1} * (1-y)^{n-j}
78
79    Only valid for 0 < x < y < 1.
80    """
81    if x < 0 or x > 1 or y < 0 or y > 1 or x >= y:
82        return 0.0
83    if i < 1 or j > n or i >= j:
84        return 0.0
85
86    log_coeff = (special.gammaln(n + 1) -
87                 special.gammaln(i) -
88                 special.gammaln(j - i) -
89                 special.gammaln(n - j + 1))
90
91    log_pdf = (log_coeff +
92               (i - 1) * np.log(max(x, 1e-300)) +
93               (j - i - 1) * np.log(max(y - x, 1e-300)) +
94               (n - j) * np.log(max(1 - y, 1e-300)))
95
96    return np.exp(log_pdf)
97
98def sample_quantile(data: np.ndarray, p: float,
99                    method: str = 'linear') -> float:
100    """
101    Compute the p-th sample quantile.
102
103    This is the order statistic X_(ceil(n*p)) with interpolation.
104    """
105    return np.quantile(data, p, method=method)
106
107def order_statistics_from_sample(data: np.ndarray) -> np.ndarray:
108    """
109    Return all order statistics X_(1), X_(2), ..., X_(n).
110    Simply the sorted sample!
111    """
112    return np.sort(data)
113
114def monte_carlo_order_statistic(n: int, k: int,
115                                 num_simulations: int = 10000,
116                                 distribution: str = 'uniform'
117                                 ) -> dict:
118    """
119    Monte Carlo estimation of order statistic distribution.
120
121    Returns empirical mean, std, and samples for plotting.
122    """
123    samples = []
124
125    for _ in range(num_simulations):
126        if distribution == 'uniform':
127            data = np.random.uniform(0, 1, n)
128        elif distribution == 'exponential':
129            data = np.random.exponential(1, n)
130        elif distribution == 'normal':
131            data = np.random.normal(0, 1, n)
132        else:
133            raise ValueError(f"Unknown distribution: {distribution}")
134
135        sorted_data = np.sort(data)
136        samples.append(sorted_data[k - 1])  # k-th order statistic
137
138    samples = np.array(samples)
139
140    return {
141        'mean': np.mean(samples),
142        'std': np.std(samples),
143        'samples': samples,
144        'n': n,
145        'k': k,
146        'num_simulations': num_simulations
147    }
148
149# Example: Max pooling as order statistics
150def max_pool_2d(feature_map: np.ndarray,
151                pool_size: int = 2,
152                stride: int = 2) -> np.ndarray:
153    """
154    2D max pooling - each output is the maximum (n-th order statistic)
155    of a pooling window.
156
157    In deep learning, this provides translation invariance by
158    selecting the strongest activation regardless of exact position.
159    """
160    h, w = feature_map.shape
161    out_h = (h - pool_size) // stride + 1
162    out_w = (w - pool_size) // stride + 1
163
164    output = np.zeros((out_h, out_w))
165
166    for i in range(out_h):
167        for j in range(out_w):
168            window = feature_map[i*stride:i*stride+pool_size,
169                                j*stride:j*stride+pool_size]
170            output[i, j] = np.max(window)  # Maximum order statistic
171
172    return output
173
174# Top-K selection
175def top_k(scores: np.ndarray, k: int) -> Tuple[np.ndarray, np.ndarray]:
176    """
177    Select top-K scores (the K largest order statistics).
178
179    Used in:
180    - Classification: top-K accuracy
181    - Beam search: keep top-K candidates
182    - Attention: sparse attention with top-K
183    """
184    indices = np.argsort(scores)[::-1][:k]
185    values = scores[indices]
186    return values, indices
187
188# Example usage
189if __name__ == "__main__":
190    # Example 1: Compare theoretical vs empirical
191    print("=== Order Statistics: Theory vs Simulation ===")
192    n, k = 10, 3  # 3rd order statistic from sample of 10
193
194    # Theoretical values for Uniform(0,1)
195    theory_mean, theory_var = expected_order_statistic_uniform(k, n)
196    print(f"Uniform(0,1), n={n}, k={k}")
197    print(f"Theoretical E[X_({k})] = {theory_mean:.4f}")
198    print(f"Theoretical Var(X_({k})) = {theory_var:.6f}")
199
200    # Monte Carlo simulation
201    mc_result = monte_carlo_order_statistic(n, k)
202    print(f"Monte Carlo E[X_({k})] = {mc_result['mean']:.4f}")
203    print(f"Monte Carlo Std(X_({k})) = {mc_result['std']:.4f}")
204
205    # Example 2: Range statistics
206    print("\n=== Range Statistics ===")
207    n = 20
208    range_mean, range_var = expected_range_uniform(n)
209    print(f"For n={n} Uniform(0,1) samples:")
210    print(f"E[Range] = {range_mean:.4f}")
211    print(f"Std[Range] = {np.sqrt(range_var):.4f}")
212
213    # Example 3: Max pooling demonstration
214    print("\n=== Max Pooling Demo ===")
215    feature_map = np.random.rand(4, 4)
216    pooled = max_pool_2d(feature_map, pool_size=2, stride=2)
217    print(f"Input shape: {feature_map.shape}")
218    print(f"Output shape: {pooled.shape}")
219    print("Each output cell is the MAX of a 2x2 window (4th order statistic of 4)")
Numerical Stability: When computing order statistic PDFs for large n, always work in log-space to avoid overflow/underflow. The log of the binomial coefficient is scipy.special.gammaln(n+1) - gammaln(k) - gammaln(n-k+1).

Common Mistakes


Test Your Understanding


Summary

Key Takeaways
  • Order statistics X(1),,X(n)X_{(1)}, \ldots, X_{(n)}are the sorted sample values: minimum to maximum
  • The PDF of X(k)X_{(k)} involves the multinomial counting argument: n!(k1)!(nk)![F(x)]k1[1F(x)]nkf(x)\frac{n!}{(k-1)!(n-k)!}[F(x)]^{k-1}[1-F(x)]^{n-k}f(x)
  • For Uniform(0,1): X(k)Beta(k,nk+1)X_{(k)} \sim \text{Beta}(k, n-k+1), with E[X(k)]=k/(n+1)E[X_{(k)}] = k/(n+1)
  • Order statistics are not independent, even when original samples are i.i.d.
  • Sample quantiles are order statistics: the p-th quantile is approximately X(np)X_{(\lceil np \rceil)}
  • ML applications: max pooling = maximum order statistic, top-K = K largest order statistics, quantile regression, robust estimators

Order statistics provide a powerful lens for understanding ranked data, extremes, and quantiles. From max pooling in CNNs to robust statistical inference, they appear throughout machine learning and statistics. The elegant Uniform-Beta connection gives us closed-form solutions for many practical problems.

Connection to Next Section: The next section covers Convolutions of random variables—what happens when we add random variables together. While order statistics study ranked values, convolutions study sums. Together, these transformations form the foundation of probability theory.
Loading comments...