How can I simulate from the density $f(x)=x^2\phi(x)$? Where $\phi$ is the standard normal density. Preferably directly, without using MCMC.

  • 4
    $\begingroup$ Could you provide more details on where you are stuck? This is a simple density for which standard methods like accept-reject or ratio of uniform work. $\endgroup$
    – Xi'an
    Mar 5 '17 at 9:27
  • $\begingroup$ @Xi'an I was just thinking that there might be a magic trick to simulate directly from this distribution without having to go for less efficient methods. $\endgroup$
    – Patel
    Mar 5 '17 at 9:30
  • 2
    $\begingroup$ accept-reject and ratio of uniform algorithms are "efficient" enough to be the methods behind the standard simulators for most classical distributions. $\endgroup$
    – Xi'an
    Mar 5 '17 at 9:37
  • $\begingroup$ @Xi'an Yes! But not as efficient as direct simulation! $\endgroup$
    – Patel
    Mar 5 '17 at 11:21
  • $\begingroup$ @Glen_b In my case, I just need to be able to simulate from that distribution. An efficient method is desirable. $\endgroup$
    – Patel
    Mar 5 '17 at 11:32

[The cdf of this variable is $\Phi(x)-x\phi(x)$ but that's not likely to yield a convenient way to generate via the inverse-cdf method.]

Transformation is fairly easy -- if $X$ has the specified density, then $Y=\frac12 X^2$ has a simple, well-known distribution that is often available in statistical computing environments already (it is in R for example; it should also be available in many libraries)

If you're unfamiliar with transforming random variables, it's covered in detail in many places - see here for example, or here, and in this question CDF of transformed random variable; the calculations are straightforward substitution in a formula and one very simple differentiation. There's a detailed (and related) example discussed here.

So if you can generate from the appropriate distribution for $Y$, then $X=Z\sqrt{2Y}$ (where $Z$ is a random sign, ie. $\pm 1$ with equal probability) will have the distribution you're after.

(Actually the factor of $2$ could be incorporated in the original generation by changing the scale parameter, which may be slightly more convenient, avoiding the need to double $Y$ before taking the square root - because it would have been doubled at the previous stage)

We can see that this method works; here's a histogram of a large sample generated using this approach, and the functional form of the density you specified drawn over the top:

Histogram and overlaid density

While this is very convenient, if efficiency is an issue it's not likely to be faster than a well-chosen variant of the accept-reject method (ratio-of uniforms counts as one possible variant of accept-reject; it might be suitable particularly if accompanied by its own transformation -- in particular, one not requiring a square root, in the interests of speed)


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.