The median minimizes the sum of absolute deviations (the $ {\ell}_{1} $ norm)

117.4k Views Asked by At

Suppose we have a set $S$ of real numbers. Show that $$\sum_{s\in S}|s-x| $$ is minimal if $x$ is equal to the median.

This is a sample exam question of one of the exams that I need to take and I don't know how to proceed.

5

There are 5 best solutions below

0
On BEST ANSWER

Introduction: The solution below is essentially the same as the solution given by Brian M. Scott, but it will take a lot longer. You are expected to assume that $S$ is a finite set. with say $k$ elements. Line them up in order, as $s_1<s_2<\cdots <s_k$.

The situation is a little different when $k$ is odd than when $k$ is even. In particular, if $k$ is even there are (depending on the exact definition of median) many medians. We tell the story first for $k$ odd.
Recall that $|x-s_i|$ is the distance between $x$ and $s_i$, so we are trying to minimize the sum of the distances. For example, we have $k$ people who live at various points on the $x$-axis. We want to find the point(s) $x$ such that the sum of the travel distances of the $k$ people to $x$ is a minimum.

The story: Imagine that the $s_i$ are points on the $x$-axis. For clarity, take $k=7$. Start from well to the left of all the $s_i$, and take a tiny step, say of length $\epsilon$, to the right. Then you have gotten $\epsilon$ closer to every one of the $s_i$, so the sum of the distances has decreased by $7\epsilon$.

Keep taking tiny steps to the right, each time getting a decrease of $7\epsilon$. This continues until you hit $s_1$. If you now take a tiny step to the right, then your distance from $s_1$ increases by $\epsilon$, and your distance from each of the remaining $s_i$ decreases by $\epsilon$. What has happened to the sum of the distances? There is a decrease of $6\epsilon$, and an increase of $\epsilon$, for a net decrease of $5\epsilon$ in the sum.

This continues until you hit $s_2$. Now, when you take a tiny step to the right, your distance from each of $s_1$ and $s_2$ increases by $\epsilon$, and your distance from each of the five others decreases by $\epsilon$, for a
net decrease of $3\epsilon$.

This continues until you hit $s_3$. The next tiny step gives an increase of $3\epsilon$, and a decrease of $4\epsilon$, for a net decrease of $\epsilon$.

This continues until you hit $s_4$. The next little step brings a total increase of $4\epsilon$, and a total decrease of $3\epsilon$, for an increase of $\epsilon$. Things get even worse when you travel further to the right. So the minimum sum of distances is reached at $s_4$, the median.

The situation is quite similar if $k$ is even, say $k=6$. As you travel to the right, there is a net decrease at every step, until you hit $s_3$. When you are between $s_3$ and $s_4$, a tiny step of $\epsilon$ increases your distance from each of $s_1$, $s_2$, and $s_3$ by $\epsilon$. But it decreases your distance from each of the three others, for no net gain. Thus any $x$ in the interval from $s_3$ to $s_4$, including the endpoints, minimizes the sum of the distances. In the even case, I prefer to say that any point between the two "middle" points is a median. So the conclusion is that the points that minimize the sum are the medians. But some people prefer to define the median in the even case to be the average of the two "middle" points. Then the median does minimize the sum of the distances, but some other points also do.

4
On

Suppose that the set $S$ has $n$ elements, $s_1<s_2<\dots<s_n$. If $x<s_1$, then $$f(x)=\sum_{s\in S}|s-x|=\sum_{s\in S}(s-x)=\sum_{k=1}^n(s_k-x)\;.\tag{1}$$ As $x$ increases, each term of $(1)$ decreases until $x$ reaches $s_1$, therefore $f(s_1)<f(x)$ for all $x<s_1$.

Now suppose that $s_k\le x\le x+d\le s_{k+1}$. Then

$$\begin{align*}f(x+d)&=\sum_{i=1}^k\Big(x+d-s_i\Big)+\sum_{i=k+1}^n\Big(s_i-(x+d)\Big)\\ &=dk+\sum_{i=1}^k(x-s_i)-d(n-k)+\sum_{i=k+1}^n(s_i-x)\\ &=d(2k-n)+\sum_{i=1}^k(x-s_i)+\sum_{i=k+1}^n(s_i-x)\\ &=d(2k-n)+f(x)\;, \end{align*}$$

so $f(x+d)-f(x)=d(2k-n)$. This is negative if $2k<n$, zero if $2k=n$, and positive if $2k>n$. Thus, on the interval $[s_k,s_{k+1}]$

$$f(x)\text{ is }\begin{cases} \text{decreasing},&\text{if }2k<n\\ \text{constant},&\text{if }2k=n\\ \text{increasing},&\text{if }2k>n\;. \end{cases}$$

From here it shouldn’t be too hard to show that $f(x)$ is minimal when $x$ is the median of $S$.

0
On

You want the median of $n$ numbers. Say $x$ is bigger than $12$ of them and smaller than $8$ of them (so $n=20$). If $x$ increases, it's getting closer to $8$ of the numbers and farther from $12$ of them, so the sum of the distances gets greater. And if $x$ decreases, it's getting closer to $12$ of them and farther from $8$ of them, so the sum of the distances gets smaller.

A similar thing happens if $x$ is smaller than more of the $n$ numbers than $x$ is bigger than.

But if $x$ is smaller than $10$ of them and bigger than $10$ of them, then when $x$ moves, it's getting farther from $10$ of them and closer to just as many of them, so the sum of the distances is not changing.

So the sum of the distances is smallest when the number of data points less than $x$ is the same as the number of data points bigger than $x$.

0
On

As a start, i will define the median of a set with an even cardinality to be one of the two elements in the mid, for example {1,2,3,4} the median is either 2 or 3. and for a set with odd cardinality the median is the middle element.

Suppose that the set has elements, and 1<2<⋯<, we will start by showing that the median gets min sum for sets of cardinality (size) 1,2, and that any problem can be reduced to a set of cardinality 1 or 2.

For set {}, the median is , and the sum is zero.
For set {1,2}, the median is either 1,2, and the sum is |1 - 2| always.
Obvioulsy its easy to see and prove for sets of sizes 1,2 or any other size, that if x is not one of the set elements then the sum bigger than than if x was an element in the set.

So we have proved that the median works for sets of sizes 1,2. Now lets consider set of size 3 where its sorted: {1,2,3}, to get the minimum its easy to see and prove, that we have to pick x such that x is between s1,s3. but for such an x, |s1 - x| + |s3 - x| is always the same and equals |1 - 3| and so x that acheives min for set {1,2,3} is the same for set {s1} which is s1, which is the median.

Its also not hard to show that the same logic applies for set of four elements, the element that achieves min for set of 4 elements is the same element that achieves min for a set of 2 elements which is the median. And using the same logic reduce the problem from a set of 7 elements to set of 5 elements, and from a set of 6 elements to a set of 4 elements,and so on.

0
On

Let $S=\{X_1,X_2,\ldots X_n\}$, w.l.o.g. assume $X_1\leq X_2 \leq \ldots \leq X_n$

$\implies D=\sum\limits_{k=1}^{n}|X_k-x|=|X_1-x|+|X_2-x|+\ldots +|X_{n-1}-x|+|X_n-x|$

Let $n \in \mathbb{Z}_{odd}^{+}$, i.e., $n=2m+1$ for some $m \in \mathbb{Z}^{+}$, for $m=0$ it's trivial.

Also, let's notice that $D=|X_{m+1}-x|+\sum\limits_{k=1}^{m}\left(|X_k-x| + |X_{n-k+1}-x|\right)$

Now, let's consider the sum $D_1=|X_1-x|+|X_n-x|$ and consider the following cases:

$(1) \quad x \leq X_1 \implies D_1=X_1+X_n-2x \geq X_n-X_1$

$(2) \quad x \geq X_n \implies D_1=2x-X_1-X_n \geq X_n-X_1$

$(3) \quad X_1 \leq x \leq X_n \implies D_1=X_n-X_1$

$\implies D_1$ is minimum when $X_1\leq x \leq X_n$

Similarly, $D_2=|X_2-x|+|X_{n-1}-x|$ is minimum when $X_2 \leq x \leq X_{n-1}$

$\ldots$

Likewise, $D_m=|X_m-x|+|X_{m+2}-x|$ is minimum when $X_m \leq x \leq X_{m+2}$

Finally, $D_{m+1}=|X_{m+1}-x|$ is minimum when $x=X_{m+1}$

Hence, $D$ is minimum when $\sum\limits_{i=1}^{m+1}D_i$ is minimum, when each $D_i$ has minimum possible value for $i=1,2,\ldots m+1$

$\implies x=X_{m+1}$, i.e., at the median value $D$ is minimum.

Similarly, we can show the same result for $n \in \mathbb{Z_{even}^{+}}$ too.