# Why does the median minimize $E(|X-c|)$?

Suppose $X$ is a real-valued random variable.

Let $P$ be the probability measure of $X$. Then $$E(|X-c|) = \int_\mathbb{R} |x-c| dP(x).$$ Its median is defined as a number $m \in \mathbb{R}$ such that $P(X \leq m) \geq \frac{1}{2}$ and $P(X \geq m) \geq \frac{1}{2}$.

Why does its median solve $$\min_{c \in \mathbb{R}} E(|X-c|) \, ?$$

Thanks and regards!

-

For every real valued random variable $X$, $$\mathrm E(|X-c|)=\int\limits_{-c}^{+\infty}\mathrm P(X\leqslant -t)\mathrm dt+\int\limits_c^{+\infty}\mathrm P(X\geqslant t)\mathrm dt,$$ hence the derivative of the function $c\mapsto \mathrm E(|X-c|)$ is $\mathrm P(X\leqslant c)-\mathrm P(X\geqslant c)$, which is negative if $c$ is smaller than every median, null if $c$ is a median, and positive if $c$ is greater than every median.

The formula for $\mathrm E(|X-c|)$ is the integrated version of the relations $(x-y)^+=\int\limits_y^{+\infty}[t\leqslant x]\mathrm dt$ and $|x-c|=((-x)-(-c))^++(x-c)^+$, which yield, for every $x$ and $c$, $$|x-c|=\int\limits_{-c}^{+\infty}[x\leqslant -t]\mathrm dt+\int\limits_c^{+\infty}[x\geqslant t]\mathrm dt.$$

-
Isn't the set of medians convex? I'm asking because you write "... is a median or between two medians,...". –  Rasmus Nov 25 '11 at 8:12
Thanks! (1) By "integrated version", is it to first integrate your last formula wrt $P$ over $\mathbb{R}$, then apply Fubini Thm to exchange the order of the two integrals, and so get the first formula? (2) If temporarily change the notation $P$ to represent the cdf of $X$, is Sivaram's Edit correct? Specifically, do those Riemann-Stieltjes integrals exist? –  Tim Nov 25 '11 at 8:16
@Rasmus, you are right, thanks, misprint corrected. –  Did Nov 25 '11 at 8:21
Tim: Much too complicated... The last equation of my post states that $u(x)=v(x)$ for every $x$, for some functions $u$ and $v$. Then $E(u(X))=E(v(X))$, end of story. (And if you have questions on @Sivaram's post, ask Sivaram.) –  Did Nov 25 '11 at 8:24

Let $f$ be the pdf and let $J(c) = E(|X-c|)$. We want to maximize $J(c)$. Note that $E(|X-c|) = \int_{\mathbb{R}} |x-c| f(x) dx = \int_{-\infty}^{c} (c-x) f(x) dx + \int_c^{\infty} (x-c) f(x) dx.$

To find the maximum, set $\frac{dJ}{dc} = 0$. Hence, we get that, \begin{align} \frac{dJ}{dc} & = (c-x)f(x) | _{x=c} + \int_{-\infty}^{c} f(x) dx + (x-c)f(x) | _{x=c} - \int_c^{\infty} f(x) dx\\ & = \int_{-\infty}^{c} f(x) dx - \int_c^{\infty} f(x) dx = 0 \end{align}

Hence, we get that $c$ is such that $$\int_{-\infty}^{c} f(x) dx = \int_c^{\infty} f(x) dx$$ i.e. $$P(X \leq c) = P(X > c).$$

However, we also know that $P(X \leq c) + P(X > c) = 1$. Hence, we get that $$P(X \leq c) = P(X > c) = \frac12.$$

EDIT

When $X$ doesn't have a density, all you need to do is to make use of integration by parts. We get that $$\displaystyle \int_{-\infty}^{c} (c-x) dP(x) = \lim_{y \rightarrow -\infty} (c-y) P(y) + \displaystyle \int_{c}^{\infty} P(x) dx.$$ Similarly, we also get that $$\displaystyle \int_{c}^{\infty} (x-c) dP(x) = \lim_{y \rightarrow \infty} (y-c) P(y) - \displaystyle \int_{c}^{\infty} P(x) dx.$$

-
Thanks! But does $X$ always have a density? –  Tim Nov 25 '11 at 7:09
@Tim: I don't think it is hard to adapt the same idea for the case when $X$ doesn't have a density. –  user17762 Nov 25 '11 at 7:15
So you are thinking $P$ as cdf of $X$? –  Tim Nov 25 '11 at 7:30