# Plot the cdf and simulate a random variable (rv) with this cdf using the inversion method.

Consider the continuous random variable with pdf given by:

$$f(x) = 2(x − 1)^2;\quad 1 < x ≤ 2$$ $$f(x) = 0;\quad \text{otherwise}$$

Plot the cdf for this random variable. Show how to simulate a rv with this cdf using the inversion method.

I tried this as

$$F(x) =\int_{1}^{x} 2(t − 1)^2 dt=\frac{2}{3}(x − 1)^3;\quad 1 < x ≤ 2$$

I plot the cdf using R.

 x<-seq(1,2,length=10)
plot(x,(2/3)*(x-1)^3)


to simulate a rv with this cdf using the inversion method

let

$$S=\frac{2}{3}(x − 1)^3$$

$$\frac{3S}{2}=(x − 1)^3$$

$$X=1+{(\frac{3S}{2})}^{\frac{1}{3}}$$

Using R

  X.sim <- function(S)1+(3*S/2)^(1/3)
S <- runif(100)

x = X.sim(S)


But i got the value of $x$ more than $2$ . But $1 < x ≤ 2$ .

How can i solve the problem ?

I noticed if i integrate the pdf under the range $1 < x ≤ 2$ it is not equal to $1$.

• Maybe you should revisit the source of this problem? As you point out, that is not a probability density function since it doesn't integrate to 1. – hejseb Sep 24 '13 at 6:36
• @hejseb This is an exercise of Introduction of scientific programing and simulation using R exercise 18.6,number 13 – ABC Sep 24 '13 at 6:42

## 1 Answer

The issue is that the normalizing constant has been incorrectly specified in the exercise. It says 2 when it should in fact be 3. See below.

$$\int_1^2c(x-1)^2dx=c\left[\frac{(x-1)^3}{3}\right]_1^2=\frac{c}{3}=1\Leftrightarrow c=3$$

If you change this and run the following code:

X.sim <- function(S)1+(S)^(1/3)
S <- runif(100000)
x <- X.sim(S)
max(x)


you will see that you get very close to 2, but you won't exceed it. Hope this helps!

• Thank you very much. Would you please check the command of my plotting cdf? is it correct? – ABC Sep 24 '13 at 8:43
• @harry: You probably want to plot it as a continuous function and not just ten values from the cdf. Like this for example: CDF <- function(x) {(x-1)^3}; curve(CDF, from = 1, to = 2) – hejseb Sep 24 '13 at 9:03
• Thank you very,very,very much. – ABC Sep 24 '13 at 9:09