skip to content
← Scientific Spaces English

An Introduction to the Median

Recently, I revisited the concept of the median, and I will record the key points while they are fresh.

When performing outlier removal or clipping, we often need a "baseline"; for example, for a collection of non-negative data, we might consider values greater than 50 times the baseline to be outliers. How, then, should this baseline be chosen? A commonly used metric is the mean; however, the mean is easily "skewed" by outliers. As a result, using it as a baseline may be biased toward outliers and thus miss some outliers. In such cases, we can consider choosing the median as the baseline.

Basic Properties #

For one-dimensional data points , their mean is defined as

Because all the data points directly participate in calculating the mean, as soon as a few points are exceptionally large, the mean also grows larger accordingly, thereby interfering with outlier detection.

The idea of the median is to find a "neutral party" among the data to serve as the baseline. Specifically, it seeks to find a dividing point such that the amount of data greater than or equal to this dividing point is the same as the amount less than or equal to it; therefore, it is also called the "50% quantile (Quantile-50%)". If is odd, then it equals the -th largest number among these numbers; if is even, then any number between the -th largest and the -th largest can be regarded as the median; in general, we take the average of the two.

From the definition of the median, we can already see its robustness against disturbances: as long as the relative order of each number with respect to the median does not change, the median will not change. Furthermore, we can introduce the "breakdown point" to describe this capability, which is "the minimum proportion of outliers that can cause the result to tend to infinity". For the mean, the answer is approximately 0, because it only takes a single point tending to infinity for the mean to tend to infinity; for the median, however, it is 50%, because more than half of the numbers must tend to infinity for the median to tend to infinity.

The drawback of the median is that it is more complicated to compute than the mean, since it requires at least some degree of sorting on the data, which is especially unfriendly to distributed computing scenarios. Fortunately, most scenarios do not require an extremely precise median, so there is some room for maneuver—for example, computing local medians first and then computing a global mean or median, which can significantly reduce communication.

An Optimization Perspective #

Interestingly, from an optimization perspective, we can unify the mean and the median:

has a relatively simple proof, which is left to the reader; here I briefly outline that of . Without loss of generality, assume that are already sorted, that is, , and let . Taking the derivative directly yields

The notation denotes the number of elements satisfying the condition. To find the minimum point, we want the derivative to be as close to 0 as possible, that is, we want the counts of elements greater than and less than among to be as equal as possible, which already aligns with the idea of the median. A more detailed discussion requires breaking down into cases:

That is, when is an odd number , both and will cause to increase, so ; when is an even number , both and will cause to increase, so , and one can verify that when lies in this interval, remains unchanged throughout, so all numbers in this interval are . In summary, aligns completely with the definition of the median.

High-Dimensional Spaces #

From an optimization perspective, we can also understand why the median is more resistant to the influence of outliers than the mean: suppose there is an exceptionally large , then the loss incurred by the mean is , whereas for the median it is . In general, , so the mean will be skewed more toward the outlier in order to minimize the loss as much as possible.

In addition, another advantage of the optimization perspective is that it readily generalizes to high-dimensional spaces. As we know, the concept of the mean generalizes very easily to high dimensions: for a set of vectors , the mean vector is simply , yet the concept of the median cannot be easily generalized directly, because it relies on ordering, but for vector data it is difficult to define a well-behaved order.

However, from an optimization perspective, this generalization is very natural:

where is the Euclidean distance. It is easy to prove that the average vector defined in this way is precisely , which matches the empirical definition. As for the median vector, we also call it the "Geometric Median". From the objective function, we can see that if , then it is actually the classical "Fermat point", so in many cases we also directly call the median vector the Fermat point.

Unfortunately, the median vector has no analytical solution, and we usually compute it based on the following Weiszfeld iteration:

Further Generalization #

Obviously, we can also consider a more general extension:

Denoting , then

Setting , the equation to be solved can be written as

Substituting into the right-hand side and denoting it as yields the fixed-point iteration. Substituting gives the average vector, while substituting yields the Weiszfeld iteration. Of course, strictly speaking, one still needs to prove convergence and uniqueness; the details are fairly involved, so I will omit them here.

In addition, there is another less commonly used form of the median vector in high-dimensional space, which replaces the Euclidean norm with the L1 norm:

This is called the "Coordinate-wise Median". Because it essentially takes the one-dimensional median component-wise, it is relatively simple to compute; however, since it lacks clear geometric meaning, its use cases are relatively fewer.

Summary #

This article briefly summarizes the concept and properties of the median, as well as its generalization in high-dimensional spaces.