# Inconsistency in Histogram's “Probability” Binsize

Context

Let me define a Probability distribution (following the documentation and with some connection to this question)

 DD = ProbabilityDistribution[(Sqrt[2]/\[Pi]) (1/(1 + x^4)), {x, -\[Infinity], \[Infinity]}];


which is normalized properly CDF[DD,1000]//N (* 1 *), and looks like this

 Plot[Evaluate[PDF[DD, x]], {x, -4, 4}, Filling -> Axis]


I am interested in drawing samples from this distribution

If I let Mathematica do his job:

 Show[RandomVariate[DD, 1500] // Histogram[#, Automatic, "Probability"] &,
Plot[PDF[DD, x], {x, -5, 5}, PlotStyle -> Directive[Red, Thick]]]


I get this

which strongly suggests it got the normalisation wrong.

Question

Is this a known bug or a misunderstanding on how Histogram works on my part?

Attempt

Now let me evaluate by hand its normalized histogram

 hh = RandomVariate[DD, 15000]//BinCounts[#, {-5, 5, 1/4}] &;
h = 4 hh/Total[hh];
h = Transpose[{Table[i - 1/4, {i, -5 + 1/4, 5, 1/4}], h}];


I get this

 Show[ListLinePlot[h, InterpolationOrder -> 0, PlotRange -> All, Filling -> Axis],
Plot[PDF[DD, x], {x, -5, 5}, PlotStyle -> Directive[Red, Thick]]]


Turning back to the Mathematica Automated procedure, if I force the binsize and the corresponding Normalization, I get this

Show[RandomVariate[DD, 150000]//Histogram[#, {-5, 5, 1/4}, "Probability"] &,
Plot[PDF[DD, x]/4, {x, -5, 5}, PlotStyle -> Directive[Red, Thick]]]


But note the 1/4 factor in the PDF (corresponding to the binsize).

-
If you use "PDF" instead of "Probability", then the scaling comes out right... – J. M. Nov 2 '12 at 9:53
@J.M. so it was a trap! You knew the answer: how wicked! Well you can write it as a one-liner now :-) – chris Nov 2 '12 at 9:56
Well, you got a dandy answer from jVincent, so it still turned out pretty darn good, eh? :) – J. M. Nov 2 '12 at 10:01
@J.M. are you guys working as a team? – chris Nov 2 '12 at 10:02
Nope; I'm pretty sure we've never met before... – J. M. Nov 2 '12 at 10:22

It's because of the different meanings of the graphs. For the bar chart, the probability of being within the width for any given bar is equal to the height. So, the density in a bar is given by height/width. So, when comparing to the probability density function (PDF), they shouldn't have the same height; they should be such that the bar chart is at the same height divided by the width of the bars. Notice that your boxes are 1/4 wide and this is where your inconsistency comes from.

Note that histogram can also scale to show the PDF instead; simply use

Show[RandomVariate[DD, 1500] // Histogram[#, Automatic, "PDF"] &,
Plot[PDF[DD, x], {x, -5, 5}, PlotStyle -> Directive[Red, Thick]]]

-
thanks; having suffered a bit on this I ll probably remember it ;-) – chris Nov 2 '12 at 10:00