1

This seems simple, but none of the solutions I've tried work well.

For example, for a range [0, 67), how can you split it up into 20 approximately equal sized discrete bins without introducing significant artifacts due to rounding?

I've tried the following solutions, but none of them seems ideal:


A. Round The Bin Size Then Floor

bin i start = floor(size/n*i)
bin i end   = floor(size/n*(i+1))

This results in the following bins:

[0, 3), [3, 6), [6, 10), [10, 13), [13, 16), [16, 20), [20, 23), [23, 26), [26, 30), [30, 33), [33, 36), [36, 40), [40, 43), [43, 46), [46, 50), [50, 53), [53, 56), [56, 60), [60, 63), [63, 67)

With the bin sizes graphed as

jagged

Pros

  • all bins are about the same size, either 3 or 4
  • the entire range is covered by the bins

Cons

  • an artificial pattern is introduced into the data, e.g. so if you graph values broken into these bins then you tend to see a jagged line, which is just an artifact of the rounding method

B. Skip The End

bin i start = floor(size/n)*i
bin i end   = floor(size/n)*(i+1)

This results in the following bins:

[0, 3), [3, 6), [6, 9), [9, 12), [12, 15), [15, 18), [18, 21), [21, 24), [24, 27), [27, 30), [30, 33), [33, 36), [36, 39), [39, 42), [42, 45), [45, 48), [48, 51), [51, 54), [54, 57), [57, 60)

With the bin sizes graphed as

flat

Pros

  • all bins are exactly the same size (3)
  • no artificial pattern is introduced in the data (besides skipping the end)

Cons

  • the end of the range [60, 67) is excluded from the bins

C. Adjust The Last Bin

bin i start = round(size/n)*i
bin i end   = if not last: round(size/n)*(i+1)
              if last:     size

This results in the following bins:

[0, 3), [3, 6), [6, 9), [9, 12), [12, 15), [15, 18), [18, 21), [21, 24), [24, 27), [27, 30), [30, 33), [33, 36), [36, 39), [39, 42), [42, 45), [45, 48), [48, 51), [51, 54), [54, 57), [57, 67)

With the bin sizes graphed as

last bin big

Pros

  • the entire range is covered by the bins
  • no artificial pattern is introduced in the data (besides the last bin)

Cons

  • the last bin is about 3x as large as all the other bins
JDiMatteo
  • 115
  • 8

1 Answers1

1

You have identified the issues well. Assuming you are making a bar graph, you can normalize by the bin width. Use your first suggestion, then divide the number of counts in each bin by the bin width. You are plotting counts/unit range, which may do what you want.

Ross Millikan
  • 374,822
  • Thanks. Please let me know if this adaptation sounds plausible: if I didn't want the values normalized, I could normalize as you suggest, then multiply by (size/n) to un-normalize, then round (since I need the final results to be integers). Seems sort of odd, but I think this might be a viable option for my purposes since it doesn't skip any ranges nor introduce any artificial patterns (at least as far as I can tell). Obviously the sum would be slightly off, but that seems like a relatively small tradeoff. – JDiMatteo Oct 22 '14 at 05:13
  • On second thought, I'll probably just normalize as you suggest. Thanks again! – JDiMatteo Oct 22 '14 at 05:30
  • If $n$ is the total number of observaions, your unnormalize will give you the fraction in each bin, and the wide bins will again be too high. It will be less than one, and probably round to zero, losing all information. – Ross Millikan Oct 22 '14 at 12:59