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

E(|X-c|) = \int_\mathbb{R} |x-c| dP_X(x).

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

\min_{c \in \mathbb{R}} E(|X-c|) \, ?

**Answer**

For **every** real valued random variable X,

\mathrm E(|X-c|)=\int_{-\infty}^c\mathrm P(X\leqslant t)\,\mathrm dt+\int_c^{+\infty}\mathrm P(X\geqslant t)\,\mathrm dt

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 (x-y)^+=\int_y^{+\infty}[t\leqslant x]\,\mathrm dt and |x-c|=((-x)-(-c))^++(x-c)^+, which yield, for every x and c,

|x-c|=\int_{-\infty}^c[x\leqslant t]\,\mathrm dt+\int_c^{+\infty}[x\geqslant t]\,\mathrm dt

**Attribution***Source : Link , Question Author : Tim , Answer Author : Did*