Is there a faster way to calculate the median??
Finding median of list in Python - Stack Overflow
ELI5: How Medians Are Calculated
Besides the other information here, the way to calculate median is very simple. When you have a huge set of data, you can use probability theory to find a density function (A function where, given your input value X, you get an output that is the probability that a randomly selected person in your data will, in this case, earn X income), and then from there you can easily find the value for which 50% of the function relies on either side of that point.
More on reddit.comCalculating Median Value - Age Groups
What is the median of two numbers?
What is the Median? Give Example.
What is the difference between mean and median?
Videos
After some extra research it seems there is no solution for my desire. The best bet probably is to define a function, say , which is defined as
$m\left(x\right) =\begin{cases} x_\frac{n+1}{2} & n\text{ odd}\\ \frac {1}{2}\left(x_{\frac{n}{2}} + x_{\frac{n}{2} + 1}\right) & n \text{ even} \end{cases} $
For a given sample by which I mean a collection of independent, identically distributed random variables
the median of the sample is either the
or
order-statistic, depending on whether
is odd or even respectively. The sample mean does not give a sufficient amount of information to uniquely determine what these order-statistics are.
Mature student here relearning my maths GCSE..
Currently I’m writing them down in order and cancelling them one by one.. until I get to the median.
Python 3.4 has statistics.median:
Return the median (middle value) of numeric data.
When the number of data points is odd, return the middle data point. When the number of data points is even, the median is interpolated by taking the average of the two middle values:
>>> median([1, 3, 5]) 3 >>> median([1, 3, 5, 7]) 4.0
Usage:
import statistics
items = [6, 1, 8, 2, 3]
statistics.median(items)
#>>> 3
It's pretty careful with types, too:
statistics.median(map(float, items))
#>>> 3.0
from decimal import Decimal
statistics.median(map(Decimal, items))
#>>> Decimal('3')
(Works with python-2.x):
def median(lst):
n = len(lst)
s = sorted(lst)
return (s[n//2-1]/2.0+s[n//2]/2.0, s[n//2])[n % 2] if n else None
>>> median([-5, -5, -3, -4, 0, -1])
-3.5
numpy.median():
>>> from numpy import median
>>> median([1, -4, -1, -1, 1, -3])
-1.0
For python-3.x, use statistics.median:
>>> from statistics import median
>>> median([5, 2, 3, 8, 9, -2])
4.0