Giving a method for generating random numbers with a cumulative distribution function

894 Views Asked by At

So let's say I have a cumulative distribution function:

$$F(x) = \frac{1}{2} (x + x^2) \space for \space 0 \lt x \lt 1$$

How do I find a method for generating random numbers from this function?

1

There are 1 best solutions below

0
On

There is an alternate mode of generation that is rather natural as well.

Consider a random variable $X$ that has $F$ as its cdf. By definition of a cdf, one can write, for $0 < x \leq 1$:

$$P(X<x)=\dfrac{1}{2}x+\dfrac{1}{2}x^2 \ \ \ (1)$$

We know that $F_1(x)=x$ is the cdf of the uniform distribution on $[0,1]$ (abbreviated as u.d.), and we may know that $F_2(x)=x^2$ is the cdf of the maximum of 2 u.d. (always with the assumption $0 < x \leq 1$).

Consider thus the random variable $Y$ that is built in two steps:

  • a Bernoulli $B_1 \equiv Ber(1/2)$ is drawn.

  • if $B_1=0$, set $Y=R_1$ where $R_1$ is u.d.

  • else (if $B_1=1$) set $Y=\max(R_1,R_2)$ where $R_2$ is u.d.

If we analyze

$$P(Y<y)=P(Y<y\mid B_1=0)P(B_1=0)+P(Y<y\mid B_1=1)P(B_1=1)$$

$$P(Y<y)=P(R_1<y)\frac{1}{2}+P(\max(R_1,R_2)<y)\frac{1}{2}$$

$$P(Y<y)=y\frac{1}{2}+y^2\frac{1}{2} \ \ \ (2)$$

which coincides with (1)

We can simulate this in the following way in Matlab:

for k=1:100000
if rand<0.5
t(k)=rand;
else t(k)=max([rand,rand]);
end
end
hist(t)