Last active
August 29, 2015 14:01
-
-
Save alpha-beta-soup/a13e6e3c26df40c4227d to your computer and use it in GitHub Desktop.
A function to calculate the interpolated median of a list of values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# interpolatedMedian.py | |
# Author: Richard Law | |
# Calculates the interpolated median of a list, | |
# which is a form of median that retains the | |
# important properties of a median (isn't swayed by | |
# unusually large or small values) but gives a better | |
# indication of the ditstribution of values | |
# See: http://aec.umich.edu/median.php | |
# An alternative: http://www.cad.vuw.ac.nz/wiki/index.php/Student_Feedback_Medians | |
import numpy as np | |
def interpolatedMedian(list_, alternative=False, interval=None): | |
if len(list_) == 0: | |
return None | |
M = float(np.median(list_)) | |
if not alternative: | |
nl = float(len([n for n in list_ if n < M])) | |
ne = float(len([n for n in list_ if n == M])) | |
ng = float(len([n for n in list_ if n > M])) | |
if ne == 0: | |
return M | |
else: | |
return M + (ng - nl) / (2 * ne) | |
elif alternative == True and interval is not None: | |
# Try the alternative method | |
I = interval | |
L = float(M - (interval * 0.5)) | |
N = float(len(list_)) | |
F = float(len([n for n in list_ if n == min(list_)])) | |
f = float(len([n for n in list_ if n == M])) | |
return L + I * (((N / 2.0) - F) / f) | |
# Testing | |
question1 = [5,5,5,5,5,5,5,5,5,4,4,4,4,4,4,4,4,4,4,2] | |
question2 = [5,4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,2,1,1] | |
question3 = [1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3] | |
# These two lists have the same traditional median, | |
# but it is clear that <question1> tends higher than | |
# <question2>. The interpolated median recognises this. | |
print np.median(question1), interpolatedMedian(question1), interpolatedMedian(question1, alternative=True, interval=1) | |
# 4.0 4.4 4.4 | |
print np.median(question2), interpolatedMedian(question2), interpolatedMedian(question2, alternative=True, interval=1) | |
# 4.0 3.6 4.3 | |
print np.median(question3), interpolatedMedian(question3), interpolatedMedian(question3, alternative=True, interval=1) | |
# 2.0 1.7 1.7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment