1

I'm not a smart guy, but I recently reading a book about programming where it said that: $|a_1 - x|^c + |a_2 - x|^c + ... + |a_n - x|^c$, in the case of $c$ = 1, the median minimizes the sum.

The example used was [1,2,9,2,6], so we have: $|1−2| +|2−2| +|9−2| +|2−2| +|6−2| = 12$

Although it is true that if you make $x$ any bigger, you could make the sum smaller by reducing it, and vice versa also holds true.

The big question is why? Is this some mysterious mathematical property that laymen cannot understand? Or is there some intuitive explanation. For context I have completed first year of a math degree and understand the basics of proof and number theory (really really basic stuff though).

herophant
  • 149

1 Answers1

2

Intuitively if you consider $f(x)=|a_1 - x| + |a_2 - x| + \ldots + |a_n - x|$ then you are looking for the $x$ which minimises $f(x)$

For any $x$ consider $f(x+\delta x)-f(x)$ for some very small positive $\delta x$, sothis will be $\delta x$ times the difference between the number of $a_i$ less than or equal to $x$ and the number of $a_i$ greater than $x$. For this to be non-negative, you need the number of $a_i$ less than or equal to $x$ to be at least the number of $a_i$ greater than $x$

Then consider $f(x-\delta x)-f(x)$ for some very small positive $\delta x$, so this will be $\delta x$ times the difference between the number of $a_i$ greater than or equal to $x$ and the number of $a_i$ less than than $x$. For this to be non-negative, you need the number of $a_i$ greater than or equal to $x$ to be at least the number of $a_i$ less than than $x$

So to have both these differences non-negative in order to have $f(x)$ minimised, you need $x$ to be the middle ranked value when $n$ is odd or in the range of the two nearest-middle ranked values when $n$ is even. This is a definition of the median of the $a_i$

Henry
  • 157,058
  • For any $x$ consider $f(x+\delta x)-f(x)$ for some very small positive $\delta x$, sothis will be $\delta x$ times the difference between the number of $a_i$ less than or equal to $x$ and the number of $a_i$ greater than $x$. For this to be non-negative, you need the number of $a_i$ less than or equal to $x$ to be at least the number of $a_i$ greater than $x$

    • Can you clarify the above? I tried an example using $a_1$ and $a_2$, and the result I ended up getting is $2\delta x$, so I'm not sure how the greater vs. smaller thing comes into play here.
    – herophant May 26 '19 at 23:03
  • @herophant suppose as an example $a_1=4$ and $a_2=6$. Then when $x\ge 6$ you have $f(x+\delta x)-f(x) = 2\delta x$ which is $\delta x$ times $2-0$, but when $4 \le x \lt 6$ you have $f(x+\delta x)-f(x) = 0$ which is $\delta x$ times $1-1$, and when $x \lt 4$ you have $f(x+\delta x)-f(x) = -2\delta x$ which is $\delta x$ times $0-2$. Does that help? – Henry May 27 '19 at 08:05