As far as I know, Apache Commons does not have a good histogram class. I finished writing my own. If all you need is linearly distributed silos from min to max, then it's pretty easy to write.
Maybe something like this:
public static int[] calcHistogram(double[] data, double min, double max, int numBins) { final int[] result = new int[numBins]; final double binSize = (max - min)/numBins; for (double d : data) { int bin = (int) ((d - min) / binSize); if (bin < 0) { } else if (bin >= numBins) { } else { result[bin] += 1; } } return result; }
Change Here is an example.
double[] data = { 2, 4, 6, 7, 8, 9 }; int[] histogram = calcHistogram(data, 0, 10, 4); // This is a histogram with 4 bins, 0-2.5, 2.5-5, 5-7.5, 7.5-10. assert histogram[0] == 1; // one point (2) in range 0-2.5 assert histogram[1] == 1; // one point (4) in range 2.5-5. // etc..
source share