# RandomVariate[LogNormalDistribution] works not as expected

I currently simulate data with a lognormal distributed covariate. I used the following code to check the generated random numbers (with Mathematica 9).

slogn2 = Select[RandomVariate[LogNormalDistribution[6, 9], 10000000], # < 10 &];
Show[
Histogram[slogn2, 200, "ProbabilityDensity"],
Plot[PDF[LogNormalDistribution[6, 9], x], {x, 0.0001, 10},
PlotRange -> All, PlotStyle -> Thick],
PlotRange -> {{0, 10}, Automatic}


!]

I found an unexpected deviation from the PDF. The histogram data are well above the density (see image). Does this indicate an error in the random number generation or how can this be explained?

The reason the curves do not match is because you have truncated the simulated data above $X > 10$, so the empirical density histogram is scaled incorrectly. $\Pr[X \le 10] \approx 0.34$, so almost 66% of your simulated observations are truncated.

Try plotting this:

Show[Histogram[Select[RandomVariate[LogNormalDistribution[6, 9], 10^6], # <= 10 &],
200, "ProbabilityDensity"],
Plot[PDF[LogNormalDistribution[6, 9], x] / CDF[LogNormalDistribution[6, 9], 10],
{x, 0.0001, 10}, PlotRange -> All, PlotStyle -> Thick],
PlotRange -> {{0, 10}, Automatic}]


You can also censor the distribution instead of truncating it:

s = Min[#, 20] & /@ RandomVariate[LogNormalDistribution[6, 9], 10^6];
Show[Histogram[s, 200, "ProbabilityDensity"],
Plot[PDF[LogNormalDistribution[6, 9], x], {x, 0.0001, 20}, PlotRange -> All,
PlotStyle -> Thick],
PlotRange -> {{0, 20}, Automatic}]

• Thanks for the quick reply. I just noticed this stupid error myself. Should I simply delete the question? I think it will be of no help to anyone. – user11881 Jan 21 '14 at 14:19
• Leave it--others might find it instructive. If the moderators don't think so, that's up to them, so I wouldn't worry. – heropup Jan 21 '14 at 14:20
• Since version 8 Mathematica has a CensoredDistribution and a TruncatedDistribution, so you can use that instead. There's one catch: in version 9 there is a bug wrt the PDF of CensoredDistribution, which returns unevaluated. This worked in v8. It is confirmed by WRI that this is indeed a bug. The CDF of CensoredDistribution does work in v9. – Sjoerd C. de Vries Jan 21 '14 at 14:34