Rejection method

58 Views Asked by At

I need to simulate a distribution using the rejection method $$p(x) = c\begin{cases} \frac{1}{1+x^2} ,& x \leq 0\\ 1, & 0<x<2\\ e^{-x}, & x \geq 2\\ \end{cases} $$


CDF is: $$ F\left(x\right)=c \begin{cases}\frac{\pi}{2}+\arctan x,& x\leq0\\ \frac{\pi}{2}+x,& 0<x<2\\ \frac{\pi}{2}+2+e^{-2}-e^{-x},& x\geq2 \end{cases} $$

As a distribution, we take the Cauchy distribution $$q\left(x\right)=\frac{1}{\pi\left(1+x^2\right)},\quad Q\left(x\right)=\frac{1}{\pi}\arctan{x}+\frac{1}{2} $$

It can be obtained by the method of inverse functions as $$-cot({\pi}u), \quad u \sim {U}\left(0,1\right)$$

Finding $$r\left(x\right) = \frac{p\left(x\right)}{q\left(x\right)}=c\begin{cases} \pi,& x\leq 0\\ \pi(1+x^2),& 0<x<2\\ \pi(1+x^2)e^{-x},& x\geq2 \end{cases} $$ $$M=\max_\mathbb{R}\frac{p\left(x\right)}{q\left(x\right)}= c \cdot \max{\left(\pi,\sup_{[0,2]}\frac{1}{\frac{1}{\pi\left(1+x^2\right)}},\sup_{(2,+\infty)}\frac{e^{-x}}{\frac{1}{\pi\left(1+x^2\right)}}\right)}=5c\pi $$

We do according to the rejection scheme: $$u\sim U(0,1),\quad \eta \sim {Q} $$

If $r\left(\eta\right)\le Mu$, continue.

Else $\xi=\eta$.


After modeling in Python (code), I got the following results.

But there is a gap near 1 (I suppose because of using $-cot(\pi u))$. How that can be fixed?