Message Boards Message Boards

0
|
3838 Views
|
5 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Generating random numbers from given continuous distribution

Posted 4 years ago

Hi, I have a given probability density function, which is

0.001257 *x^4 Exp[-0.285714 *x]

and I need to generate 15 random numbers according to this distribution. Here is what I have done:

dist = ProbabilityDistribution[ 0.001257*x^4 Exp[-0.285714*x], {x, 0, 60}]
Plot[PDF[dist, x], {x, 0, 60}]
sample = RandomVariate[dist, 15]
{5.75881, 4.74259, 6.97658, 5.52833, 7.34263, 6.76362, 4.92664, 2.58222, 4.65696, 6.4487, 7.30355, 7.25276, 7.00075, 4.12756, 4.41973}
Show[Histogram[sample, 100, "ProbabilityDensity"], Plot[PDF[dist, x], {x, 0, 60}, PlotStyle -> PointSize[Medium]]]

I think this is pretty straightforward, and I followed several examples that aim at doing the same, but my results are weird. I get a sequence of 15 numbers between 0 and 8, and I don't understand why. All in all, I think I am not getting right how to generate random variates from a custom distribution in the first place. Would you help me?

POSTED BY: elena max
5 Replies
Posted 4 years ago

Hi Elena,

You need to normalize the distribution so its integral is 1.

    dist = ProbabilityDistribution[
      0.001257*x^4 Exp[-0.285714*x],
      {x, 0, 60},
      Method -> "Normalize"]

    RandomVariate[dist, 15]
(*
{27.4943,16.8757,5.30406,6.00207,11.8668,28.2957,21.9066,19.6846,12.266,18.8735,19.3128,
15.404,21.1927,13.8153,19.9035}
*)
POSTED BY: Rohit Namjoshi
Posted 4 years ago

For what I can see the option Method->Normalize was introduced in version 10.2

POSTED BY: Hans Milton
Posted 4 years ago

For a distribution of this form you can compute the normalization factor.

factor = NIntegrate[0.001257*x^4 Exp[-0.285714*x], {x, 0, Infinity}];
dist = ProbabilityDistribution[ 0.001257*x^4 Exp[-0.285714*x] / factor , {x, 0, 60}]
POSTED BY: Rohit Namjoshi
Posted 4 years ago

Thanks a lot. Just a question, does this work on Mathematica 10? Because I can make it work on the web version of Mathematica, but not on my laptop, where I have v10.enter image description here

POSTED BY: elena max
Posted 4 years ago

Thank you very much, I still have a lot of practice to do.

POSTED BY: elena max
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract