2
$\begingroup$

Given a list of $N$ numbers I need to compute the median.

The book Numerical Recipes says that:

When $N$ is odd, the median is the $k$th element, with $k=\frac{N+1}{2}$. When $N$ is even, statistics books define the median as the arithmetic mean of the elements $k=\frac{N}{2}$ and $k=1+\frac{N}{2}$ (that is, $\frac{N}{2}$ from the bottom and $\frac{N}{2}$ from the top). If you accept such pedantry, you must perform two separate selections to find these elements. For $N > 100$ we usually define $k=\frac{N}{2}$ to be the median element, pedants be damned.

I can define myself as pedant and so when I need to code an (exact) median algorithm I usually consider the two cases for $N$ but what could possibly go wrong defining $k=\frac{N}{2}$ to be the median element when $N$ is not odd?

My use of the median value is usually just for summary purposes, like in the following R snippet:

v=rnorm(10)
summary(v)
$\endgroup$
7
  • $\begingroup$ What alternative definition do you have in mind? $\endgroup$ – whuber Aug 5 '20 at 13:35
  • $\begingroup$ @whuber I have no alternative definition in mind. I just follow the definition that you can find for example at mathworld.wolfram.com/StatisticalMedian.html Tangentially I could wonder why Numerical Recipes choose 100 and not 1000 or 10 in order to not accept the pedantry... $\endgroup$ – Alessandro Jacopson Aug 5 '20 at 14:02
  • $\begingroup$ By asking "what could go wrong" you seem to imply there is some alternative where things go "right." $\endgroup$ – whuber Aug 5 '20 at 14:57
  • $\begingroup$ @whuber method one ("right"): compute the median according to mathworld.wolfram.com/StatisticalMedian.html, method two ("wrong"): compute the median according to Numerical Recipes as described in the question. $\endgroup$ – Alessandro Jacopson Aug 5 '20 at 15:57
  • 1
    $\begingroup$ Right. I had forgotten that NR wasn't simply ignoring the cases of even N--it was using $k=N/2.$ One way to view this would be to suppose they are randomly removing one observation in the top half and then applying the formula for the median of an odd sample size. That's obviously biased--but not very much so for large N. An alternative would be to remove a random observation (not necessarily in the top half). Contemplating these two cases can help us analyze the differences among the approaches. $\endgroup$ – whuber Aug 5 '20 at 18:29
1
$\begingroup$

For practical purposes, things will rarely go wrong, for most reasonable values of "wrong", unless you have very strange distributions.

Note that this recipe is just one possibility. Hyndman & Fan (The American Statistician, 1996) give no less than nine different definitions of sample quantiles and recommend number 8. R's quantile() function follows their tabulation and implements all of them, with the default type=7. I would very much recommend that article, which also discusses possibilities for things to go "wrong".

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.