Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'BinCounts bug' topicStudent Support Forum > General > "BinCounts bug"

< Previous CommentHelp | Reply To Comment | Reply To Topic
Author Comment/Response
Fprum Moderator
11/05/07 12:19pm

I got a lot of help from Peter Pein in understanding what the issues were in your example. It looks like a couple of things are going on.

First V6 counts into different bins than V5. Here is an example of the different behaviors:

---6.0----
In[28]:= BinCounts[{100}, {98, 102, 1}]

Out[28]= {0,0,1,0}

----5.2----
In[2]:=
<<Statistics`

In[3]:=
BinCounts[{100},{98,102,1}]

Out[3]=
{0,1,0,0}

I think that this is documenting in V6.0 as "In BinCounts[data, {{b1,
b2, ...}}], elements are counted in bin i when their values satisfy
b_i <= x < b_i+1."

... although it does not explicitly say that things are different from
V5.

Looks like this is a machine arithmetic issue in the end, I have this from
one of our developers from a discussion of whether the problem was that the trailing 1 shows up in:

In[1]:= Ceiling[98.65051300731969, .2] // InputForm
Out[1]//InputForm= 98.80000000000001


---
0.2 is not exactly representable in binary.
It has a recurring expansion:

In[1]:= RealDigits[1/5, 2]

Out[1]= {{{1, 1, 0, 0}}, -2}

What we obtain in the Ceiling computation is 494 * 0.2

Now 0.2 is rounded up from:

In[1]:= RealDigits[1/5, 2, 54]

Out[1]= {{1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,

> 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
1, 1,

> 0, 0, 1, 1, 0, 0, 1, 1}, -2}

The last 1 causes rounding up and we get:
In[1]:= FromDigits[ {{1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0}, -2}, 2]

3602879701896397
Out[1]= -----------------
18014398509481984

In[2]:= % > 1/5

Out[2]= True

so it is very slightly larger.

Here are the results of floating point numbers just above and below 0.2:

In[1]:= 494 Interval[0.2]

Out[1]= Interval[{98.8, 98.8}]

In[2]:= InputForm[%]

Out[2]//InputForm= Interval[{98.79999999999998, 98.80000000000004}]

So we can expect a number between these bounds, but not necessarily 98.8
in decimal.

Based on this, the position of the middle 1 in the BinCounts example is
an artifact of machine arithmetic and consistent with the docs, and the
user can use an exact step of 1/5 to avoid this.

---------

So, this works:

In[28]:= BinCounts[{101.13528790591918`,100,98.85051300731969`},{98.8,101,1/5}]


Out[28]=
{1,0,0,0,0,0,1,0,0,0,0,1}

Tom Zeller

URL: ,

Subject (listing for 'BinCounts bug')
Author Date Posted
BinCounts bug Humphrey 10/05/07 11:00am
Re: BinCounts bug Peter Pein 10/11/07 2:20pm
Re: Re: BinCounts bug Humphrey 10/12/07 08:09am
Re: BinCounts bug Fprum Modera... 11/05/07 12:19pm
< Previous CommentHelp | Reply To Comment | Reply To Topic