5
$\begingroup$

I want to generate a random real number between 0 and 1 but not distributed uniformly but with a probability distribution say, 2 Sin[ pi x ]^2 which integrates to 1 and peaks at $x = 1/2$.

I tried looking for Biased random variables in the HELP tab, but it did not show any hits.

This is my PDF:

pdf = 2 Sin[Pi x]^2
$\endgroup$
5
  • 5
    $\begingroup$ Have you tried a RandomVariate of a ProbabilityDistribution with your chosen PDF function? $\endgroup$
    – kirma
    Apr 25 at 11:32
  • 3
    $\begingroup$ Please give actual code .. $\endgroup$ Apr 25 at 11:34
  • 4
    $\begingroup$ This is a low-quality question. In the future, please make an effort to show due diligence. Provide a minimum working example of the code you have, tell us what have you tried, how did you search, and why the results were not satisfactory. $\endgroup$
    – rhermans
    Apr 25 at 12:10
  • $\begingroup$ It's hard to provide code when you cannot spot the command that does the trick in the help menu. I tried looking for Biased random variables in the HELP tab, it did not show any hits ... $\endgroup$ Apr 25 at 12:22
  • 5
    $\begingroup$ @QuasarSupernova if I Google "Mathematica" How do you generate a random number with a pre-specified probability distribution? (i.e the title you wrote) the first hit brings me to Random Number Generation, which contain in the second paragraph already an explicit mention of RandomVariate, which could have been a good start. $\endgroup$
    – rhermans
    Apr 25 at 12:48

2 Answers 2

11
$\begingroup$
dist = ProbabilityDistribution[2 Sin[Pi x]^2, {x, 0, 1}];

SeedRandom[1]
sample = RandomVariate[dist, 500];

Show[Histogram[sample, 20, "PDF"], Plot[PDF[dist, x], {x, 0, 1}]]

enter image description here

$\endgroup$
8
  • $\begingroup$ If I am not mistaken, PearsonChiSquareTest[sample, dist, "TestDataTable"] results in $$\begin{array}{l|ll} \text{} & \text{Statistic} & \text{P-Value} \\ \hline \text{Pearson }\chi ^2 & 20.6 & 0.662195 \\ \end{array}$$ and does not confirm the accordance between sample and dist. $\endgroup$
    – user64494
    Apr 25 at 13:20
  • 1
    $\begingroup$ @user64494 Try comparing the P-value obtained using dist as the test distribution with that obtained using another test distribution on the same data (e.g. NormalDistribution[] or UniformDistribution[]) and see how the latter attempts result in much, much lower P-values. $\endgroup$
    – MarcoB
    Apr 25 at 13:39
  • 3
    $\begingroup$ @user64494 No. I suggest that the P-value you obtained is actually compatible with reasonable fit between the data and the proposed distribution. As a counterexample, I recommended running PearsonChiSquareTest[sample, UniformDistribution[], "TestDataTable"] to see an example of the kind of P-value that would be associated with a poor fit between data and test distribution. $\endgroup$
    – MarcoB
    Apr 25 at 14:14
  • 1
    $\begingroup$ @user64494, check PearsonChiSquareTest[sample, dist, "TestConclusion"] $\endgroup$
    – kglr
    Apr 25 at 14:23
  • 1
    $\begingroup$ 500 samples is not enough to reject any similar distribution, try dist2=NormalDistribution[Mean[dist],StandardDeviation[dist] ], PearsonChiSquareTest[smallsample, dist2, "ShortTestConclusion"] gives "Do not reject". You need around 5000 samples to reject a NormalDistribution of the same Mean and StandardDeviation. $\endgroup$
    – rhermans
    Apr 25 at 15:20
6
$\begingroup$

Since your PDF already integrates to one over the range this is pretty straight-forward:

RandomVariate[ProbabilityDistribution[2 Sin[Pi x]^2, {x, 0, 1}]]

(* 0.163801 *)
$\endgroup$

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.