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

Suppose $X$ is a real-valued random variable and let $P_X$ denote the distribution of $X$. Then

The medians of $X$ are defined as any 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 do the medians solve

For every real valued random variable $X$,
hence the function $u:c\mapsto \mathrm E(|X-c|)$ is differentiable almost everywhere and, where $u'(c)$ exists, $u'(c)=\mathrm P(X\leqslant c)-\mathrm P(X\geqslant c)$. Hence $u'(c)\leqslant0$ if $c$ is smaller than every median, $u'(c)=0$ if $c$ is a median, and $u'(c)\geqslant0$ if $c$ is greater than every median.
The formula for $\mathrm E(|X-c|)$ is the integrated version of the relations and $|x-c|=((-x)-(-c))^++(x-c)^+$, which yield, for every $x$ and $c$,