9
$\begingroup$

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]

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.

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

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).

$\endgroup$
8
  • 3
    $\begingroup$ If you use "PDF" instead of "Probability", then the scaling comes out right... $\endgroup$ – J. M.'s torpor Nov 2 '12 at 9:53
  • $\begingroup$ @J.M. so it was a trap! You knew the answer: how wicked! Well you can write it as a one-liner now :-) $\endgroup$ – chris Nov 2 '12 at 9:56
  • $\begingroup$ Well, you got a dandy answer from jVincent, so it still turned out pretty darn good, eh? :) $\endgroup$ – J. M.'s torpor Nov 2 '12 at 10:01
  • $\begingroup$ @J.M. are you guys working as a team? $\endgroup$ – chris Nov 2 '12 at 10:02
  • 1
    $\begingroup$ FWIW: HistogramDistribution[] would have been a slightly tidier way to do your attempt. $\endgroup$ – J. M.'s torpor Nov 2 '12 at 11:02
8
$\begingroup$

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

Mathematica graphics

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

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.