Last active
July 28, 2020 15:32
-
-
Save srikumarks/9a3908afefc948633cfaf773c858779d to your computer and use it in GitHub Desktop.
Freedman-Diaconis thumb rule for number of bins of a histogram
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
// metric = array of real numbers (like > 100 or something) | |
// IQR = inter-quaartile-range | |
function numBins(metric, defaultBins) { | |
var h = binWidth(metric), ulim = Math.max.apply(Math, metric), llim = Math.min.apply(Math, metric); | |
if (h <= (ulim - llim) / metric.length) { | |
return defaultBins || 10; // Fix num bins if binWidth yields too small a value. | |
} | |
return Math.ceil((ulim - llim) / h); | |
} | |
function binWidth(metric) { | |
return 2 * iqr(metric) * Math.pow(metric.length, -1/3); | |
} | |
function iqr(metric) { | |
var sorted = metric.slice(0).sort(function (a, b) { return a - b; }); | |
var q1 = sorted[Math.floor(sorted.length / 4)]; | |
var q3 = sorted[Math.floor(sorted.length * 3 / 4)]; | |
return q3-q1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python 3 compatible: