Created
October 30, 2015 04:10
-
-
Save pkaf/547a3ac5a38b9f3cffd6 to your computer and use it in GitHub Desktop.
Weighted Median
This file contains hidden or 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
import numpy as np | |
def weighted_median(x, weights): | |
#Sorting item and weights first | |
sort = np.argsort(x) | |
x = x[sort] | |
weights = weights[sort] | |
#Less than cumulative frequencies | |
cumfreq = np.cumsum(weights) | |
N = np.sum(weights) | |
assert N == cumfreq[-1] | |
#Find minimum cumulative frequency >= N/2. | |
#If equal, average of x corresponding to min and second min is return | |
#otherwise x corresponding to min is reported | |
idx = np.where(cumfreq>= N/2.)[0][0] | |
if cumfreq[idx]==N/2.: | |
Md = 0.5*(x[idx] + x[idx+1]) | |
else: | |
Md = x[idx] | |
return Md | |
if __name__=="__main__": | |
x = np.array([19,28,27,32,37,41,42,24]) | |
freq = np.array([7,12,22,14,13,14,19,19]) | |
#x = np.array([19,28,27,32,37,41,42,24]) | |
#freq = np.array([7,12,25,14,13,14,19,19]) | |
#x = np.array([155,160,165,170.,175,180]) | |
#freq = np.array([4,7,18,11,6,4.]) | |
#freq = np.array([1]*8) | |
print weighted_median( x, freq) | |
print np.median(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment