2
$\begingroup$

Suppose I want to generate a histogram of $n$ trials a slightly complicated random variable in Mathematica. Is there an easy way to do this?

For instance, to make a histogram of a normal variable one can simple use

Histogram[RandomVariate[NormalDistribution[0, 1], 200]]

I want to create a histogram of a more complicated random variable. The random variable comes from random matrix theory, but its specific form is probably not so important. If we call it X, running

Histogram[RandomVariate[X, 200]]

returns

RandomVariate::udist: "The specification X is not a random distribution recognized by the system." Indeed, even running

Histogram[RandomVariate[NormalDistribution[0, 1]^2, 200]]

returns this error message, and I think if I understood what's going wrong in this latter case my problem would be resolved also.

There is a Mathematica help page on the topic, but all the examples of random variables given there are for prepackaged probability distributions.

$\endgroup$
4
  • $\begingroup$ Have you looked at ProbabilityDistribution[] $\endgroup$
    – george2079
    Nov 26, 2013 at 21:07
  • 1
    $\begingroup$ What do you mean by NormalDistribution[0, 1]^2? It is square of the probability density function or x^2 where x distributed as NormalDistribution[0, 1]? TransformedDistribution, EmpiricalDistribution and SmoothKernelDistribution can be helpful. Could you provide an example of the distribution? $\endgroup$
    – ybeltukov
    Nov 26, 2013 at 21:09
  • 1
    $\begingroup$ Break your problem down to pieces and find the smallest piece that you can't get working. Do you need to plot the PDF or do you just need to generate a large number of random variates? It seems to me that you need the latter. In this case Histogram is irrelevant to the question. NormalDistribution[0, 1]^2 is not correct syntax. As george said, look at ProbabilityDistribution. $\endgroup$
    – Szabolcs
    Nov 26, 2013 at 21:09
  • 4
    $\begingroup$ You don't have a problem with Histogram, you have a problem defining a distribution. Have a look at Create Your Own Distribution Workshop. $\endgroup$ Nov 26, 2013 at 21:19

1 Answer 1

5
$\begingroup$

I think this is what you are after..

Histogram[
  RandomVariate[
    ProbabilityDistribution[
     2 Sqrt[π] (PDF[NormalDistribution[0, 1]][x])^2, {x, -∞, ∞}], 2000]]

Note that the argument to ProbabilityDistribution[] has to be a proper probability density function in the sense that it integrates to unity. (Thats where that 2 Sqrt[π] comes from)

$\endgroup$
1
  • $\begingroup$ Thanks! Sorry for the delayed response. This does indeed produce a histogram of X^2 where X is a standard normal variable. I'm afraid I oversimplified the problem in this example though. How would one go about creating a histogram, summing a series of independent identically distributed variables for instance? The actual problem I want to solve concerns operations on the eigenvalues of random matrices. Would it be most appropriate to simply create a new question, or to edit the question above to be more precise? $\endgroup$ Nov 29, 2013 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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