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).
"PDF"
instead of"Probability"
, then the scaling comes out right... $\endgroup$HistogramDistribution[]
would have been a slightly tidier way to do your attempt. $\endgroup$