Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


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]

Mathematica graphics

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

Mathematica graphics

which strongly suggests it got the normalisation wrong.


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


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]]]

Mathematica graphics

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]]]

Mathematica graphics

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

share|improve this question
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
up vote 6 down vote accepted

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]]]
share|improve this answer
thanks; having suffered a bit on this I ll probably remember it ;-) – chris Nov 2 '12 at 10:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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