Skip to main content
`Ctrl`+`K`
scipy.stats.

# order_statistic

scipy.stats.order_statistic(X, /, *, r, n)

  Probability distribution of an order statistic.

  Returns a random variable that follows the distribution of the \(r^{\text{th}}\) order statistic of a sample of \(n\) observations of a random variable \(X\).

  Parameters:
    X ContinuousDistribution

      The random variable \(X\).

    r array_like

      The (positive integer) rank of the order statistic \(r\), satisfying `1 <= r <= n`.

    n array_like

      The (positive integer) sample size \(n\).

  Returns:
    Y ContinuousDistribution

      A random variable that follows the distribution of the prescribed order statistic.

  Notes

  If we make \(n\) observations of a continuous random variable \(X\) and sort them in increasing order \(X_{(1)}, \dots, X_{(r)}, \dots, X_{(n)}\), then \(X_{(r)}\) is known as the \(r^{\text{th}}\) order statistic.

  If the PDF, CDF, and CCDF of \(X\) are denoted by \(f\), \(F\), and \(G = 1 - F\), respectively, then the PDF of \(X_{(r)}\) is given by:

  \[f_r(x) = \frac{n!}{(r-1)! (n-r)!} f(x) F(x)^{r-1} G(x)^{n - r}\]

  The CDF and other methods of the distribution of \(X_{(r)}\) are calculated using the fact that \(X = F^{-1}(U)\), where \(U\) is a standard uniform random variable, together with the fact that the order statistics of i.i.d. uniform random variables follow a beta distribution \(B(r, n - r + 1)\).

  References

  [1]

  Order statistic. Wikipedia. https://en.wikipedia.org/wiki/Order_statistic

  Examples

  Suppose we are interested in order statistics of samples of size five drawn from the standard normal distribution. Plot the PDF of each order statistic and compare with a normalized histogram from simulation.

      >>> import numpy as np
      >>> import matplotlib.pyplot as plt
      >>> from scipy import stats
      >>>
      >>> X = stats.Normal()
      >>> data = X.sample(shape=(10000, 5))
      >>> sorted_data = np.sort(data, axis=1)
      >>> Y = stats.order_statistic(X, r=[1, 2, 3, 4, 5], n=5)
      >>>
      >>> ax = plt.gca()
      >>> colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
      >>> for i in range(5):
      ...     y = sorted_data[:, i]
      ...     ax.hist(y, density=True, bins=30, alpha=0.1, color=colors[i])
      >>> Y.plot(ax=ax)
      >>> plt.show()