3
$\begingroup$

I'd like to share a pernicious problem with BinCounts and therefore also Histogram. The minimized problem is as follows:

BinCounts[ 
 Join[Range[100], {1000000000000000000}],
 {20}
]

This leads to:

SystemException["MemoryAllocationFailure"]

Unfortunately, this is not a standard error, and doesn't provide any further information. Catch[]ing it doesn't really seem to help. What makes it worse is that the error can occur quite sporadically (I was seeing it in automated plotting code). It turns out that the error was happening only when there was an outlier in the data to be sent to Histogram.

$\endgroup$
2
$\begingroup$

I will submit a bug to Wolfram Research. In the meantime, you can circumvent this by actively specifying the range to bin:

BinCounts[
 Join[Range[100], {1000000000000000000}],
 {0, 100, 20}
 ]

which will lead, as expected, to:

{19, 20, 20, 20, 20}

This workaround works for Histogram too, of course.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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