DEV Community

Cover image for Find the Mode of Intervals
Jackson Reeves
Jackson Reeves

Posted on • Edited on

Find the Mode of Intervals

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]
view raw mqr.py hosted with ❤ by GitHub

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more