Recently, I have been re-learning the concept of the median and decided to record the key points while they are fresh in my mind.

When performing outlier removal or clipping, we often need a "baseline." For instance, given a set of non-negative data, we might consider any value greater than 50 times the baseline to be an outlier. How should this baseline be selected? A commonly used metric is the mean; however, the mean is easily "skewed" by outliers. Therefore, using it as a baseline might lean toward the outliers, causing us to miss some results. In such cases, we can consider choosing the median as the baseline.

Basic Properties #

For one-dimensional data points $x_1, x_2, \cdots, x_n$, their mean is defined as: \begin{equation}\newcommand{mean}{\mathop{\text{mean}}}\mean(x_1,x_2,\cdots,x_n) = \frac{1}{n}\sum_{i=1}^n x_i\end{equation} Since all data points participate directly in the calculation of the average, if even a few points are exceptionally large, the mean will increase accordingly, thereby interfering with the judgment of outliers.

The idea of the median is to find a "neutral observer" in the data to serve as the baseline. Specifically, it seeks to find a partition point such that the amount of data greater than or equal to the point is equal to the amount of data less than or equal to it; thus, it is also called the "50% Quantile." If $n$ is odd, it is equal to the $(n+1)/2$-th largest number in the set. If $n$ is even, any number between the $n/2$-th and $(n/2+1)$-th largest numbers can be considered the median; typically, we take the average of the two.

The robustness of the median against interference is evident from its definition: as long as the relationship between each number and the median (greater than or less than) does not change, the median remains unchanged. Furthermore, we can introduce the "Breakdown Point" to describe this capability, which is "the minimum proportion of outliers required to make the result tend toward infinity." For the mean, the answer is approximately 0, because it only takes one point tending to infinity for the mean to do the same; but for the median, it is 50%, because more than half of the numbers must tend to infinity for the median to tend to infinity.

The disadvantage of the median is that its calculation is more complex than that of the mean, as it requires some degree of sorting. This is particularly unfriendly for distributed computing scenarios. Fortunately, most scenarios do not require a perfectly precise median, so there is room for maneuver—for example, calculating local medians first and then calculating a global mean or median, which can significantly reduce communication overhead.

Optimization Perspective #

Interestingly, we can unify the mean and the median from an optimization perspective: \begin{gather}\newcommand{argmin}{\mathop{\text{argmin}}}\newcommand{median}{\mathop{\text{median}}} \mean(x_1,x_2,\cdots,x_n) = \argmin_{\mu} \sum_{i=1}^n (x_i - \mu)^2 \\ \median(x_1,x_2,\cdots,x_n) = \argmin_{\mu} \sum_{i=1}^n |x_i - \mu| \\ \end{gather} The proof for $\mean$ is relatively simple and left to the reader. Here, I will briefly introduce the proof for $\median$. Without loss of generality, assume the $x_i$ are sorted, i.e., $x_1\leq x_2\leq\cdots\leq x_n$. Let $f(\mu) = \sum_{i=1}^n |x_i - \mu|$. Taking the derivative directly gives: \begin{equation}\newcommand{sign}{\mathop{\text{sign}}} f'(\mu) = \sum_{i=1}^n \sign(\mu - x_i) = \#\{x_i < \mu\} - \#\{x_i > \mu\}\\ \end{equation} The symbol $\#$ denotes the count of elements satisfying the condition. To find the minimum point, we want the derivative to be as close to 0 as possible, meaning we want the number of $x_i$ greater than $\mu$ and the number of $x_i$ less than $\mu$ to be as equal as possible, which matches the idea of the median. A more detailed discussion requires looking at cases: \begin{equation}f'(\mu) = \left\{\begin{aligned}&\left.\begin{aligned} &\underbrace{\#\{x_i < \mu\}}_{\leq k} - \underbrace{\#\{x_i > \mu\}}_{\geq k+1} < 0, &\mu < x_{k+1} \\ &\underbrace{\#\{x_i < \mu\}}_{\geq k+1} - \underbrace{\#\{x_i > \mu\}}_{\leq k} > 0, &\mu > x_{k+1} \\\end{aligned}\right\} & n = 2k+1 \\ &\left.\begin{aligned} &\underbrace{\#\{x_i < \mu\}}_{\leq k-1} - \underbrace{\#\{x_i > \mu\}}_{\geq k+1} < 0, &\mu < x_{k\phantom{+1}} \\ &\underbrace{\#\{x_i < \mu\}}_{\geq k+1} - \underbrace{\#\{x_i > \mu\}}_{\leq k-1} > 0, &\mu > x_{k+1} \\ \end{aligned}\right\} & n = 2k \end{aligned}\right.\end{equation} That is, when $n$ is an odd number $2k+1$, both $\mu < x_{k+1}$ and $\mu > x_{k+1}$ will cause $f(\mu)$ to increase, so $\mu^*=x_{k+1}$. When $n$ is an even number $2k$, both $\mu < x_k$ and $\mu > x_{k+1}$ will cause $f(\mu)$ to increase; one can verify that when $\mu^*$ is located within the interval $[x_k, x_{k+1}]$, $f(\mu^*)$ remains constant, so any number in this interval is a valid $\mu^*$. In summary, $\mu^*$ perfectly coincides with the definition of the median.

High-Dimensional Space #

From the optimization perspective, we can also understand why the median resists outlier interference better than the mean: if there is an exceptionally large $x_i$, the loss contributed to the mean is $(x_i - \mu)^2$, while for the median it is $|x_i - \mu|$. Generally, $(x_i - \mu)^2 \gg |x_i - \mu|$, so the mean will lean more toward the outlier to minimize the loss as much as possible.

Furthermore, the optimization perspective has another advantage: it is naturally generalizable to high-dimensional spaces. We know that the concept of the mean is easily extended to high dimensions; for a set of vectors $\boldsymbol{x}_1, \boldsymbol{x}_2, \cdots, \boldsymbol{x}_n$, the average vector is $\frac{1}{n}\sum_{i=1}^n \boldsymbol{x}_i$. However, the concept of the median is not directly extendable because it relies on ordering, and it is difficult to define a meaningful order for vector data.

However, under the optimization perspective, this extension is natural: \begin{gather}\mean(\boldsymbol{x}_1,\boldsymbol{x}_2,\cdots,\boldsymbol{x}_n) = \argmin_{\boldsymbol{\mu}} \sum_{i=1}^n \Vert\boldsymbol{x}_i - \boldsymbol{\mu}\Vert_2^2 \\ \median(\boldsymbol{x}_1,\boldsymbol{x}_2,\cdots,\boldsymbol{x}_n) = \argmin_{\boldsymbol{\mu}} \sum_{i=1}^n \Vert\boldsymbol{x}_i - \boldsymbol{\mu}\Vert_2 \\ \end{gather} where $\Vert\Vert_2$ is the Euclidean distance. It is easy to prove that the average vector defined this way is exactly $\frac{1}{n}\sum_{i=1}^n \boldsymbol{x}_i$, 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 $n=3$, it is actually the classic "Fermat point," so oftentimes we directly refer to the median vector as the Fermat point.

Unfortunately, the median vector has no analytical solution. We usually compute it based on the following Weiszfeld iteration: \begin{equation}\boldsymbol{\mu}_{t+1} = \frac{\sum_{i=1}^n \boldsymbol{x}_i / \Vert\boldsymbol{x}_i - \boldsymbol{\mu}_t\Vert_2}{\sum_{i=1}^n 1 / \Vert\boldsymbol{x}_i - \boldsymbol{\mu}_t\Vert_2}\end{equation}

Further Generalization #

Clearly, we can consider more general generalizations: \begin{equation}\newcommand{average}{\mathop{\text{average}}}\average(\boldsymbol{x}_1,\boldsymbol{x}_2,\cdots,\boldsymbol{x}_n;\alpha) = \argmin_{\boldsymbol{\mu}} \sum_{i=1}^n \Vert\boldsymbol{x}_i - \boldsymbol{\mu}\Vert_2^{\alpha}\end{equation} Let $f(\boldsymbol{\mu}) = \sum_{i=1}^n \Vert\boldsymbol{x}_i - \boldsymbol{\mu}\Vert_2^{\alpha}$, then: \begin{equation}\nabla_{\boldsymbol{\mu}} f(\boldsymbol{\mu}) = \alpha\sum_{i=1}^n \Vert\boldsymbol{x}_i - \boldsymbol{\mu}\Vert_2^{\alpha - 2}(\boldsymbol{\mu} - \boldsymbol{x}_i)\end{equation} Setting $\nabla_{\boldsymbol{\mu}} f(\boldsymbol{\mu})=\boldsymbol{0}$, the equation to be solved can be written as: \begin{equation}\boldsymbol{\mu} = \frac{\sum_{i=1}^n \Vert\boldsymbol{x}_i - \boldsymbol{\mu}\Vert_2^{\alpha - 2}\boldsymbol{x}_i}{\sum_{i=1}^n\Vert\boldsymbol{x}_i - \boldsymbol{\mu}\Vert_2^{\alpha - 2}}\end{equation} By substituting $\boldsymbol{\mu}_t$ into the right side and denoting it as $\boldsymbol{\mu}_{t+1}$, we obtain the fixed-point iteration method. Substituting $\alpha=2$ yields the mean vector, and substituting $\alpha=1$ yields the Weiszfeld iteration. Of course, strictly speaking, one must prove convergence and uniqueness; the details are quite complex and are omitted here.

Additionally, there is another less commonly used form of the high-dimensional median vector, which involves replacing the Euclidean norm with the L1 norm: \begin{equation}\argmin_{\boldsymbol{\mu}} \sum_{i=1}^n \Vert\boldsymbol{x}_i - \boldsymbol{\mu}\Vert_1\end{equation} This is called the "Coordinate-wise Median," because it is essentially taking the one-dimensional median of each component independently. It is simpler to calculate, but because it lacks a clear geometric meaning, its use cases are relatively fewer.

Summary #

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