# Thread: Sampling - inverse transform & rejection method

1. ## Sampling - inverse transform & rejection method

Hello,

I was wondering if someone could help me with a question given in the tutorial at uni.

The probability density function is

f(x) = (1/2)(x+1) 1 < x <= 2
= (1/2) 2 < x <= 3
= 0 elsewhere

Now to generate values of the random variable, we have to use the inverse transform method and the rejection method.

If anyone, could show me how to solve this equations or equations like the one above, that would be appreciated.

2. (*) $\displaystyle \int_1^x f(t) dt = \frac{1}{4}(x+1)^2 - 1$
(**)$\displaystyle \int_1^2 f(t) dt + \int_2^x f(t) dt = \frac{1}{2}x - \frac{1}{4}$

Note that (*) is 1-1 over [1,2]

Now, the inverses are:

(*) $\displaystyle F^{-1}(u) = 2\sqrt{u+1} - 1$
(**) $\displaystyle F^{-1}(u) = 2\left(u+\frac{1}{4}\right)$

R code

n <- 1000
u <- runif(n)
x1 <- 2*sqrt(u+1) - 1
x2 <- 2*(u+1/4)
par(mfrow=c(1,2))
hist(x1)
hist(x2)
par(mfrow=c(1,1))

Now, for the rejection method, note that for g uniform [0,1], we have

$\displaystyle \frac{f(x)}{g(x)} \leq \frac{3}{2}$ for $\displaystyle x \in [1,2]$. This gives you $\displaystyle c=\frac{3}{2}$

So, a uniformly generated x will be accepted if $\displaystyle \frac{1}{3} (x+1) > u$

This should get you started, I hope...

P.S. This is only my 2nd post, so please don't bash me too much...

3. so I make this clear,

If I was to do the inverse method on the following equation:

f(x) = (2/3)(x-1) 1 < x <= 2 (equation 1)
= (2/3) 2 < x <= 3 (equation 2)
= 0 elsewhere

I'm also not sure about my c values, can you double check???

Equation 1

By integration it would be
(1/3)(x-1)^2 + 1

The inverse is
sqrt(3)sqrt(u-1) + 1

Equation 2

By integration it would be
(2/3)x + (1/3)
The inverse is
(3/2)(x-(1/3))

Also, don't understand the rejection method there, is that just the first equation is f(x) and the second equation is g(x).

4. CDF:

(*) $\displaystyle \int_1^x f(t) dt = \frac{1}{3}(x-1)^2$
(**)$\displaystyle \int_1^2 f(t) dt + \int_2^x f(t) dt = \frac{2}{3}x - 1$

Inverses:

(*) $\displaystyle F^{-1}(u) = \sqrt{3u} + 1$
(**) $\displaystyle F^{-1}(u) = \frac{3}{2}\left(u+1\right)$

I hope it helps

For more on rejection sampling, see

Statistical Computing with R by Maria L. Rizzo

Also, here's some helpful R code to go with her book:
http://personal.bgsu.edu/~mrizzo/SCR/SCRch3.R

Also, see the post by Moo:
http://www.mathhelpforum.com/math-he...on-method.html