# Calculating medians and quartiles of arbitrary probability density functions

I have an interval $[0, 1]$ and a probability density function $f(x)$ defined on that interval. I know $f$ at say 1000 unevenly spaced points along that interval, but I don't have an analytic formula for $f$. How can I calculate the median and the quartiles of $f$?

## 1 Answer

The median is the point where 50% of the population lies below that value. Mathematically that definition means that the median $m$ satisfies the integral:

$$\int_{0}^{m}f(x)dx=0.5$$

You can approximate this integral using numerical methods. Since you have 1000 known points you can approximate the integral using the trapezoidal method. Suppose your points are $x_!, x_2, ... x_{1000}$, then the integral approximated up to the $k^{th}$ point is:

$\sum_{i=1}^k (x_{i+1}-x_i)\frac{f(x_{i+1})+f(x_i)}{2}$

Therefore, you want to find the minimum $k$ such that

$\sum_{i=1}^k (x_{i+1}-x_i)\frac{f(x_{i+1})+f(x_i)}{2}=0.5$

Likewise, for the quartiles just use 0.25 and 0.75 instead of 0.5.