I am using HistogramList to bin the data and get frequency counts to generate probability distributions. I've noticed something surprising and terrible about Mathematica 10.4's binning method: it leaves out any points on the highest bin border.


The results of this is:


You see that the counts for the value 9 are missing from the final bin. If you check:


We see that there are 25 items in the list, but only 23 counts in the HistogramList output. This is clearly a mistake.

The reason is clearly that Mathematica uses a use bin boundaries like this: [a,b),[b,c),...which works up until the last bin. That needs a special handler to include those points too, and Wolfram forgot about it.

So the question is, what is the best way to compensate for this error/bug?

I've padded the min and max by a tiny amount to get it to work, but that introduces error (it's a kludge rather than a fix). One option would be to test the list for points that match the upper bound and manually add them to the counts for that bin. Another would be to bypass the HistogramList function completely and do the correct binning manually. But which of these is the fastest? What's the best way to get accurate bin counts?

  • $\begingroup$ Is your data all integer values, or was that just for the example? If it is, you can do something like bins = ConstantArray[0, 9]; Scan[bins[[#]]++ &, TestData]. $\endgroup$ Oct 31, 2017 at 10:47
  • $\begingroup$ My data is not all integers. My actual data is 2D (pairs of real numbers), if that makes a difference. $\endgroup$ Oct 31, 2017 at 17:13

3 Answers 3


As I mentioned above, one obvious solution is to test the list for points that match the upper bound and manually add them to the counts for that bin. Here is some basic code that does that:


Note that the conditional is there so that the same bins can be used for different lists with values within the same range, but not necessarily reaching the max (i.e., boundaries from a whole time series, but binned separately by time window).

Perhaps this code can be improved, and especially this is easy in one dimension, but seems like an inefficient approach for 2D data. Perhaps there is an entirely different and better way.


Here's a version of the other solution, doing the binning and counting from "scratch".

(* this is just an easy way to convert the 1D series data into 2D data *)

(* these are the bins for one dimension, but they are the same in both dimensions *)


This basically just handles the cases logically separately, but done together in the same loop. I tried it with nested If statements and the Timing was the same. This is very much a brute-force approach, so although it works fine I expect there is a more elegant way to do the same thing.


Using the method of building from scratch totally works in terms of getting the correct frequency data, but it is MUCH slower than the built-in histogram function: Timing = 0.0312 vs Timing = 0..

Since the bug only excludes values exactly equal to the highest bin boundary, an intermediate solution is to specify the bins first, then add padding to the highest bin boundary, then use the built-in function.


I used a tiny amount of padding, but it doesn't matter for me because this only extends the highest bin, which (in my case) is set to the largest value in the data. So in my case it can't erroneously include points outside the desired range.

Of course, if in other applications the upper bound is not the max datapoint then this solution may not work properly, and Wolfram still needs to fix the bug.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.