A simple Python function for calculating the MQR, a nifty statistical measure of spread that I came up with one night.
I was annoyed that every measure of central tendency had a corresponding measure of spread—except mode. Mean has standard deviation; median has interquartile range. But what about mode? So for the sake of parity, I made one up.
I call it the Minimal Quintile Range (or MQR), which provides the length of the smallest interval that contains at least a fifth of the data in a set. In other words, the mode of intervals.
def mqr(raw_data): | |
sorted_data = sorted(raw_data) | |
count = len(sorted_data) | |
fifth = math.ceil(count / 5.0) | |
contenders = [] | |
num = 0 | |
while num < count - fifth: | |
contenders.append(sorted_data[num + fifth - 1] - sorted_data[num]) | |
num += 1 | |
return contenders | |
sorted_contenders = sorted(contenders) | |
return sorted_contenders[0] |
Top comments (0)