Trying to understand rejection sampling on this example with a rough solution

136 Views Asked by At

I find rejection sampling very hard to understand and there is also not much about it on the internet, imo. So I found an example with a solution and can you explain the steps I don't understand / wrong please?

Create random numbers from this density by using rejection sampling

$$f(x)=\begin{cases}\frac{3}{4}(2x-x^2)&\mbox{if } x \in(0,2) \\ 0 & \mbox{else}\end{cases}$$

The algorithm works like that: We have that $M:= \sup_{x}\frac{f(x)}{g(x)}<\infty$

  1. Simulate $U \sim U(0,1)$

  2. Simulate $Y \sim g$

  3. Accept $X=Y \text{ if } U \leq \frac{1}{M}\frac{f(Y)}{g(Y)} \;\text{ else to to 1. }$

Solution: Find a function $g$ that is easier to sample from, such that there is a $M \in \mathbb{R}$, such that $Mg(x) \geq f(x) \; \forall x \in (0,2)$.

Pick $g \sim U(0,2)$, then we have that $M:= \sup_{x \in (0,2)}f(x)=\frac{3}{4}$

$\color{red}{\text{Now sample from $U(0,1)$ for which you get $U=0.32$}}$ and then sample from $\color{red}{Y \sim q \Rightarrow 1.28}$. Now the sampled $y$ value from step $2.$ needs to be accepted if $U \leq \frac{1}{M} \frac{f(Y)}{g(Y)}$, so we have that $0.32 \leq \frac{4}{3}f(1.29)=0.92$ and thus we accept $X=1.28$


I especially don't understand the red market text, how do you get $U=0.32$ and what is $\frac{f(Y)}{g(Y)}$. I really hope you can tell me because I looked this up on several pages and it's just too complicated and I think an example is much better for understanding.

1

There are 1 best solutions below

2
On BEST ANSWER

The part in red is just an example. It's saying suppose we sample from $U(0,1)$ and the result is $0.32$. Next, suppose we sample from $U(0,2)$ and the result is $1.28$. So $0.32$ and $1.28$ are assumed to be two random numbers drawn from the appropriate distributions for the purpose of illustration.

For $0 \le Y \le 2$ we have $f(Y) = (3/4) (2Y-Y^2)$ and $g(Y) = 1/2$, so $$\frac{f(Y)}{g(Y)} = \frac{(3/4) (2Y-Y^2)}{1/2}$$

However, I do not understand the part of the example where it says $M = 3/4$. It appears to me that $M = 3/2$. The equation $M = \sup_{x \in (0,2)} f(x)$ should also be $$M = \sup_{x \in (0,2)} \frac{f(x)}{1/2}$$ because $g(x) = 1/2$ for $x \in (0,2)$. So I think there is an error in the solution.