Calculating the median in data sets of odd (above) and even (below) observations
Themedian of a set of numbers is the value separating the higher half from the lower half of adata sample, apopulation, or aprobability distribution. For adata set, it may be thought of as the “middle" value. The basic feature of the median in describing data compared to themean (often simply described as the "average") is that it is notskewed by a small proportion of extremely large or small values, and therefore provides a better representation of the center.Median income, for example, may be a better way to describe the center of the income distribution because increases in the largest incomes alone have no effect on the median. For this reason, the median is of central importance inrobust statistics.
The median of a finite list of numbers is the "middle" number, when those numbers are listed in order from smallest to greatest.
If the data set has an odd number of observations, the middle one is selected (after arranging in ascending order). For example, the following list of seven numbers,
1, 3, 3,6, 7, 8, 9
has the median of6, which is the fourth value.
If the data set has an even number of observations, there is no distinct middle value and the median is usually defined to be thearithmetic mean of the two middle values.[1][2] For example, this data set of 8 numbers
1, 2, 3,4, 5, 6, 8, 9
has a median value of4.5, that is. (In more technical terms, this interprets the median as the fullytrimmedmid-range).
In general, with this convention, the median can be defined as follows: For a data set of elements, ordered from smallest to greatest,
if is odd,
if is even,
Comparison of commonaverages of values [ 1, 2, 2, 3, 4, 7, 9 ]
Formally, a median of apopulation is any value such that at least half of the population is less than or equal to the proposed median and at least half is greater than or equal to the proposed median. As seen above, medians may not be unique. If each set contains more than half the population, then some of the population is exactly equal to the unique median.
The median is well-defined for anyordered (one-dimensional) data and is independent of anydistance metric. The median can thus be applied to school classes which are ranked but not numerical (e.g. working out a median grade when student test scores are graded from F to A), although the result might be halfway between classes if there is an even number of classes. (For odd number classes, one specific class is determined as the median.)
Ageometric median, on the other hand, is defined in any number of dimensions. A related concept, in which the outcome is forced to correspond to a member of the sample, is themedoid.
There is no widely accepted standard notation for the median, but some authors represent the median of a variablex as med(x),x͂,[3] asμ1/2,[1] or asM.[3][4] In any of these cases, the use of these or other symbols for the median needs to be explicitly defined when they are introduced.
The median can be used as a measure oflocation when one attaches reduced importance to extreme values, typically because a distribution isskewed, extreme values are not known, oroutliers are untrustworthy, i.e., may be measurement or transcription errors.
The median is 2 in this case, as is themode, and it might be seen as a better indication of thecenter than thearithmetic mean of 4, which is larger than all but one of the values. However, the widely cited empirical relationship that the mean is shifted "further into the tail" of a distribution than the median is not generally true. At most, one can say that the two statistics cannot be "too far" apart; see§ Inequality relating means and medians below.[5]
As a median is based on the middle data in a set, it is not necessary to know the value of extreme results in order to calculate it. For example, in a psychology test investigating the time needed to solve a problem, if a small number of people failed to solve the problem at all in the given time a median can still be calculated.[6]
For practical purposes, different measures of location and dispersion are often compared on the basis of how well the corresponding population values can be estimated from a sample of data. The median, estimated using the sample median, has good properties in this regard. While it is not usually optimal if a given population distribution is assumed, its properties are always reasonably good. For example, a comparison of theefficiency of candidate estimators shows that the sample mean is more statistically efficientwhen—and only when— data is uncontaminated by data from heavy-tailed distributions or from mixtures of distributions.[citation needed] Even then, the median has a 64% efficiency compared to the minimum-variance mean (for large normal samples), which is to say the variance of the median will be ~50% greater than the variance of the mean.[7][8]
Anyprobability distribution on the real number set has at least one median, but in pathological cases there may be more than one median: ifF is constant 1/2 on an interval (so thatf = 0 there), then any value of that interval is a median.
The medians of certain types of distributions can be easily calculated from their parameters; furthermore, they exist even for some distributions lacking a well-defined mean, such as theCauchy distribution:
Themean absolute error of a real variablec with respect to therandom variableX isProvided that the probability distribution ofX is such that the above expectation exists, thenm is a median ofX if and only ifm is a minimizer of the mean absolute error with respect toX.[11] In particular, ifm is a sample median, then it minimizes the arithmetic mean of the absolute deviations.[12] Note, however, that in cases where the sample contains an even number of elements, this minimizer is not unique.
More generally, a median is defined as a minimum ofas discussed below in the section onmultivariate medians (specifically, thespatial median).
This optimization-based definition of the median is useful in statistical data-analysis, for example, ink-medians clustering.
If the distribution has finite variance, then the distance between the median and the mean is bounded by onestandard deviation.
This bound was proved by Book and Sher in 1979 for discrete samples,[13] and more generally by Page and Murty in 1982.[14] In a comment on a subsequent proof by O'Cinneide,[15] Mallows in 1991 presented a compact proof that usesJensen's inequality twice,[16] as follows. Using |·| for theabsolute value, we have
The first and third inequalities come from Jensen's inequality applied to the absolute-value function and the square function, which are each convex. The second inequality comes from the fact that a median minimizes theabsolute deviation function.
Mallows's proof can be generalized to obtain a multivariate version of the inequality[17] simply by replacing the absolute value with anorm:
wherem is aspatial median, that is, a minimizer of the function The spatial median is unique when the data-set's dimension is two or more.[18][19]
A typical heuristic is that positively skewed distributions have mean > median. This is true for all members of thePearson distribution family. However this is not always true. For example, theWeibull distribution family has members with positive mean, but mean < median. Violations of the rule are particularly common for discrete distributions. For example, any Poisson distribution has positive skew, but its mean < median whenever.[22] See[23] for a proof sketch.
When the distribution has a monotonically decreasing probability density, then the median is less than the mean, as shown in the figure.
Jensen's inequality states that for any random variableX with a finite expectationE[X] and for any convex functionf
This inequality generalizes to the median as well. We say a functionf:R →R is aC function if, for anyt,
is aclosed interval (allowing the degenerate cases of asingle point or anempty set). Every convex function is a C function, but the reverse does not hold. Iff is a C function, then
If the medians are not unique, the statement holds for the corresponding suprema.[24]
This section discusses the theory of estimating a population median from a sample. To calculate the median of a sample "by hand," see§ Finite data set of numbers above.
Selection algorithms still have the downside of requiringΩ(n) memory, that is, they need to have the full sample (or a linear-sized portion of it) in memory. Because this, as well as the linear time requirement, can be prohibitive, several estimation procedures for the median have been developed. A simple one is the median of three rule, which estimates the median as the median of a three-element subsample; this is commonly used as a subroutine in thequicksort sorting algorithm, which uses an estimate of its input's median. A morerobust estimator isTukey'sninther, which is the median of three rule applied with limited recursion:[26] ifA is the sample laid out as anarray, and
The distributions of both the sample mean and the sample median were determined byLaplace.[28] The distribution of the sample median from a population with a density function is asymptotically normal with mean and variance[29]
We take the sample size to be an odd number and assume our variable continuous; the formula for the case of discrete variables is given below in§ Empirical local density. The sample can be summarized as "below median", "at median", and "above median", which corresponds to a trinomial distribution with probabilities, and. For a continuous variable, the probability of multiple sample values being exactly equal to the median is 0, so one can calculate the density of at the point directly from the trinomial distribution:
Now we introduce the beta function. For integer arguments and, this can be expressed as. Also, recall that. Using these relationships and setting both and equal to allows the last expression to be written as
Hence the density function of the median is a symmetric beta distributionpushed forward by. Its mean, as we would expect, is 0.5 and its variance is. By thechain rule, the corresponding variance of the sample median is
In practice, the functions and above are often not known or assumed. However, they can be estimated from an observed frequency distribution. In this section, we give an example. Consider the following table, representing a sample of 3,800 (discrete-valued) observations:
v
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
f(v)
0.000
0.008
0.010
0.013
0.083
0.108
0.328
0.220
0.202
0.023
0.005
F(v)
0.000
0.008
0.018
0.031
0.114
0.222
0.550
0.770
0.972
0.995
1.000
Because the observations are discrete-valued, constructing the exact distribution of the median is not an immediate translation of the above expression for; one may (and typically does) have multiple instances of the median in one's sample. So we must sum over all these possibilities:
Here,i is the number of points strictly less than the median andk the number strictly greater.
Using these preliminaries, it is possible to investigate the effect of sample size on the standard errors of the mean and median. The observed mean is 3.16, the observed raw median is 3 and the observed interpolated median is 3.174. The following table gives some comparison statistics.
Sample size
Statistic
3
9
15
21
Expected value of median
3.198
3.191
3.174
3.161
Standard error of median (above formula)
0.482
0.305
0.257
0.239
Standard error of median (asymptotic approximation)
0.879
0.508
0.393
0.332
Standard error of mean
0.421
0.243
0.188
0.159
The expected value of the median falls slightly as sample size increases while, as would be expected, the standard errors of both the median and the mean are proportionate to the inverse square root of the sample size. The asymptotic approximation errs on the side of caution by overestimating the standard error.
The value of—the asymptotic value of where is the population median—has been studied by several authors. The standard "delete one"jackknife method producesinconsistent results.[30] An alternative—the "delete k" method—where grows with the sample size has been shown to be asymptotically consistent.[31] This method may be computationally expensive for large data sets. A bootstrap estimate is known to be consistent,[32] but converges very slowly (order of).[33] Other methods have been proposed but their behavior may differ between large and small samples.[34]
Theefficiency of the sample median, measured as the ratio of the variance of the mean to the variance of the median, depends on the sample size and on the underlying population distribution. For a sample of size from thenormal distribution, the efficiency for large N is
If data is represented by astatistical model specifying a particular family ofprobability distributions, then estimates of the median can be obtained by fitting that family of probability distributions to the data and calculating the theoretical median of the fitted distribution.Pareto interpolation is an application of this when the population is assumed to have aPareto distribution.
Previously, this article discussed the univariate median, when the sample or population had one-dimension. When the dimension is two or higher, there are multiple concepts that extend the definition of the univariate median; each such multivariate median agrees with the univariate median when the dimension is exactly one.[36][37][38][39]
The marginal median is defined for vectors defined with respect to a fixed set of coordinates. A marginal median is defined to be the vector whose components are univariate medians. The marginal median is easy to compute, and its properties were studied by Puri and Sen.[36][40]
If the marginal medians for all coordinate systems coincide, then their common location may be termed the "median in all directions".[42] This concept is relevant to voting theory on account of themedian voter theorem. When it exists, the median in all directions coincides with the geometric median (at least for discrete distributions).
Instatistics andcomputational geometry, the notion ofcenterpoint is a generalization of the median to data in higher-dimensionalEuclidean space. Given a set of points ind-dimensional space, a centerpoint of the set is a point such that any hyperplane that goes through that point divides the set of points in two roughly equal subsets: the smaller part should have at least a 1/(d + 1) fraction of the points. Like the median, a centerpoint need not be one of the data points. Every non-empty set of points (with no duplicates) has at least one centerpoint.
The conditional median occurs in the setting where we seek to estimate a random variable from a random variable, which is a noisy version of. The conditional median in this setting is given by
where is the inverse of the conditional cdf (i.e., conditional quantile function) of. For example, a popular model is where is standard normal independent of. The conditional median is the optimal Bayesian estimator:
It is known that for the model where is standard normal independent of, the estimator is linear if and only if is Gaussian.[43]
When dealing with a discrete variable, it is sometimes useful to regard the observed values as being midpoints of underlying continuous intervals. An example of this is aLikert scale, on which opinions or preferences are expressed on a scale with a set number of possible responses. If the scale consists of the positive integers, an observation of 3 might be regarded as representing the interval from 2.50 to 3.50. It is possible to estimate the median of the underlying variable. If, say, 22% of the observations are of value 2 or below and 55.0% are of 3 or below (so 33% have the value 3), then the median is 3 since the median is the smallest value of for which is greater than a half. But the interpolated median is somewhere between 2.50 and 3.50. First we add half of the interval width to the median to get the upper bound of the median interval. Then we subtract that proportion of the interval width which equals the proportion of the 33% which lies above the 50% mark. In other words, we split up the interval width pro rata to the numbers of observations. In this case, the 33% is split into 28% below the median and 5% above it so we subtract 5/33 of the interval width from the upper bound of 3.50 to give an interpolated median of 3.35. More formally, if the values are known, the interpolated median can be calculated from
Alternatively, if in an observed sample there are scores above the median category, scores in it and scores below it then the interpolated median is given by
For univariate distributions that aresymmetric about one median, theHodges–Lehmann estimator is a robust and highly efficient estimator of the population median; for non-symmetric distributions, the Hodges–Lehmann estimator is a robust and highly efficient estimator of the populationpseudo-median, which is the median of a symmetrized distribution and which is close to the population median.[44] The Hodges–Lehmann estimator has been generalized to multivariate distributions.[45]
Incluster analysis, thek-medians clustering algorithm provides a way of defining clusters, in which the criterion of maximising the distance between cluster-means that is used ink-means clustering, is replaced by maximising the distance between cluster-medians.
This is a method of robust regression. The idea dates back toWald in 1940 who suggested dividing a set of bivariate data into two halves depending on the value of the independent parameter: a left half with values less than the median and a right half with values greater than the median.[47] He suggested taking the means of the dependent and independent variables of the left and the right halves and estimating the slope of the line joining these two points. The line could then be adjusted to fit the majority of the points in the data set.
Nair and Shrivastava in 1942 suggested a similar idea but instead advocated dividing the sample into three equal parts before calculating the means of the subsamples.[48] Brown and Mood in 1951 proposed the idea of using the medians of two subsamples rather the means.[49] Tukey combined these ideas and recommended dividing the sample into three equal size subsamples and estimating the line based on the medians of the subsamples.[50]
The theory of median-unbiased estimators was revived by George W. Brown in 1947:[51]
An estimate of a one-dimensional parameter θ will be said to be median-unbiased if, for fixed θ, the median of the distribution of the estimate is at the value θ; i.e., the estimate underestimates just as often as it overestimates. This requirement seems for most purposes to accomplish as much as the mean-unbiased requirement and has the additional property that it is invariant under one-to-one transformation.
— page 584
Further properties of median-unbiased estimators have been reported.[52][53][54][55]
There are methods of constructing median-unbiased estimators that are optimal (in a sense analogous to the minimum-variance property for mean-unbiased estimators). Such constructions exist for probability distributions havingmonotone likelihood-functions.[56][57] One such procedure is an analogue of theRao–Blackwell procedure for mean-unbiased estimators: The procedure holds for a smaller class of probability distributions than does the Rao—Blackwell procedure but for a larger class ofloss functions.[58]
Scientific researchers in the ancient near east appear not to have used summary statistics altogether, instead choosing values that offered maximal consistency with a broader theory that integrated a wide variety of phenomena.[59] Within the Mediterranean (and, later, European) scholarly community, statistics like the mean are fundamentally a medieval and early modern development. (The history of the median outside Europe and its predecessors remains relatively unstudied.)
The idea of the median appeared in the 6th century in theTalmud, in order to fairly analyze divergentappraisals.[60][61] However, the concept did not spread to the broader scientific community.
Instead, the closest ancestor of the modern median is themid-range, invented byAl-Biruni[62]: 31 [63] Transmission of his work to later scholars is unclear. He applied his technique toassaying currency metals, but, after he published his work, most assayers still adopted the most unfavorable value from their results, lest they appear tocheat.[62]: 35–8 [64] However, increased navigation at sea during theAge of Discovery meant that ship's navigators increasingly had to attempt to determine latitude in unfavorable weather against hostile shores, leading to renewed interest in summary statistics. Whether rediscovered or independently invented, the mid-range is recommended to nautical navigators in Harriot's "Instructions for Raleigh's Voyage to Guiana, 1595".[62]: 45–8
The idea of the median may have first appeared inEdward Wright's 1599 bookCertaine Errors in Navigation on a section aboutcompass navigation.[65] Wright was reluctant to discard measured values, and may have felt that the median — incorporating a greater proportion of the dataset than themid-range — was more likely to be correct. However, Wright did not give examples of his technique's use, making it hard to verify that he described the modern notion of median.[59][63][b] The median (in the context of probability) certainly appeared in the correspondence ofChristiaan Huygens, but as an example of a statistic that was inappropriate foractuarial practice.[59]
The earliest recommendation of the median dates to 1757, whenRoger Joseph Boscovich developed a regression method based on theL1 norm and therefore implicitly on the median.[59][66] In 1774,Laplace made this desire explicit: he suggested the median be used as the standard estimator of the value of a posteriorPDF. The specific criterion was to minimize the expected magnitude of the error; where is the estimate and is the true value. To this end, Laplace determined the distributions of both the sample mean and the sample median in the early 1800s.[28][67] However, a decade later,Gauss andLegendre developed theleast squares method, which minimizes to obtain the mean; the strong justification of this estimator by reference tomaximum likelihood estimation based on anormal distribution means it has mostly replaced Laplace's original suggestion.[68]
Antoine Augustin Cournot in 1843 was the first[69] to use the termmedian (valeur médiane) for the value that divides a probability distribution into two equal halves.Gustav Theodor Fechner used the median (Centralwerth) in sociological and psychological phenomena.[70] It had earlier been used only in astronomy and related fields.Gustav Fechner popularized the median into the formal analysis of data, although it had been used previously by Laplace,[70] and the median appeared in a textbook byF. Y. Edgeworth.[71]Francis Galton used the termmedian in 1881,[72][73] having earlier used the termsmiddle-most value in 1869, and themedium in 1880.[74][75]
Median graph – Graph with a median for each three vertices
Median of medians – Fast approximate median algorithm – Algorithm to calculate the approximate median in linear time
Median search – Method for finding kth smallest valuePages displaying short descriptions of redirect targets
Median slope – Statistical method for fitting a linePages displaying short descriptions of redirect targets
Median voter theory – Theorem in political sciencePages displaying short descriptions of redirect targets
Medoid – representative objects of a data set or a cluster within a data set whose sum of dissimilarities to all the objects in the cluster is minimalPages displaying wikidata descriptions as a fallbacks – Generalization of the median in higher dimensions
^The geometric median is unique unless the sample is collinear.[41]
^Subsequent scholars appear to concur with Eisenhart that Boroughs' 1580 figures, while suggestive of the median, in fact describe an arithmetic mean.;[62]: 62–3 Boroughs is mentioned in no other work.
^Mallows, Colin (August 1991). "Another comment on O'Cinneide".The American Statistician.45 (3): 257.doi:10.1080/00031305.1991.10475815.
^Piché, Robert (2012).Random Vectors and Random Sequences. Lambert Academic Publishing.ISBN978-3659211966.
^Kemperman, Johannes H. B. (1987). Dodge, Yadolah (ed.). "The median of a finite measure on a Banach space: Statistical data analysis based on the L1-norm and related methods".Papers from the First International Conference Held at Neuchâtel, August 31–September 4, 1987. Amsterdam: North-Holland Publishing Co.:217–230.MR0949228.
^Basu, S.; Dasgupta, A. (1997). "The Mean, Median, and Mode of Unimodal Distributions:A Characterization".Theory of Probability and Its Applications.41 (2):210–223.doi:10.1137/S0040585X97975447.S2CID54593178.
^abcHettmansperger, Thomas P.; McKean, Joseph W. (1998).Robust nonparametric statistical methods. Kendall's Library of Statistics. Vol. 5. London: Edward Arnold.ISBN0-340-54937-8.MR1604954.
^Small, Christopher G. "A survey of multidimensional medians." International Statistical Review/Revue Internationale de Statistique (1990): 263–277.doi:10.2307/1403809JSTOR1403809
^Niinimaa, A., and H. Oja. "Multivariate median." Encyclopedia of statistical sciences (1999).
^Mosler, Karl. Multivariate Dispersion, Central Regions, and Depth: The Lift Zonoid Approach. Vol. 165. Springer Science & Business Media, 2012.
^Puri, Madan L.; Sen, Pranab K.;Nonparametric Methods in Multivariate Analysis, John Wiley & Sons, New York, NY, 1971. (Reprinted by Krieger Publishing)
^Barnes, Leighton; Dytso, Alex J.; Jingbo, Liu; Poor, H.Vincent (2024-08-22). "L1 Estimation: On the Optimality of Linear Estimators".IEEE Transactions on Information Theory.70 (11):8026–8039.doi:10.1109/TIT.2024.3440929.
^Pratt, William K.; Cooper, Ted J.; Kabir, Ihtisham (1985-07-11). Corbett, Francis J (ed.). "Pseudomedian Filter".Architectures and Algorithms for Digital Image Processing II.0534: 34.Bibcode:1985SPIE..534...34P.doi:10.1117/12.946562.S2CID173183609.
^Oja, Hannu (2010).Multivariate nonparametric methods with R: An approach based on spatial signs and ranks. Lecture Notes in Statistics. Vol. 199. New York, NY: Springer. pp. xiv+232.doi:10.1007/978-1-4419-0468-3.ISBN978-1-4419-0467-6.MR2598854.
^Nair, K. R.; Shrivastava, M. P. (1942). "On a Simple Method of Curve Fitting".Sankhyā: The Indian Journal of Statistics.6 (2):121–132.JSTOR25047749.
^Brown, G. W.; Mood, A. M. (1951). "On Median Tests for Linear Hypotheses".Proc Second Berkeley Symposium on Mathematical Statistics and Probability. Berkeley, CA: University of California Press. pp. 159–166.Zbl0045.08606.
^Adler, Dan (31 December 2014)."Talmud and Modern Economics".Jewish American and Israeli Issues. Archived fromthe original on 6 December 2015. Retrieved22 February 2020.
^David, H. A. (1995). "First (?) Occurrence of Common Terms in Mathematical Statistics".The American Statistician.49 (2):121–133.doi:10.2307/2684625.ISSN0003-1305.JSTOR2684625.