Using below sample data :
List<Double> sampleData = Arrays.asList(1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.);
We know the binWidth or binSize is 2 using the formula:
double binWidth = (max-min) / numberOfBins;
Where the numberOfBins are 5 using the Sturge’s formula:
int numbetOfBins = (int)Math.ceil((1.0 + (3.3 * (Math.log10(exampleData.size())))));
Now, I am trying to count the number of data in each bin of histogram. Using below code:
public void calculateBinsNumberOfData() {
double binWidth = (max-min) / numberOfBins;
System.out.println("Bin Width is: " + binWidth);
double binMin = min, binMax, binDataPoints;
for (int i = 1; i <= numberOfBins; i++) {
binMax = binMin + binWidth;
binDataPoints = 0;
for (Double double1 : sampleData) {
if(double1 >= binMin && double1 <= binMax) {
binDataPoints++;
}
}
System.out.println("Bin "+i+" : ["+binMin+" to "+binMax+"] = " + binDataPoints);
binMin = binMax;
}
}
The output prints the results when including the last edge of a bin double1 <= binMax like:
The output prints the results when not including the last edge of a bin double1 < binMax like:
And, you can observe in both above cases we are missing the value 11 of sample data. Now, I don't know how to fix this problem in this special case so that we can effectively count the last value of the data.

