% This is a simple LaTex sample document that gives a submission format
%   for IEEE PAMI-TC conference submissions.  Use at your own risk.

% Make two column format for LaTex 2e.\
\documentclass[11pt,twocolumn]{article} %,twocolumn

%\usepackage{times,amsmath,amsfonts}

% Use following instead for LaTex 2.09 (may need some other mods as well).
%\documentstyle[times,twocolumn]{article}
\usepackage[dvips]{graphicx,graphics}
% Set dimensions of columns, gap between columns, and paragraph indent
\setlength{\textheight}{10in} \setlength{\textwidth}{7.2in}
%\setlength{\columnsep}{0.3125in} \setlength{\topmargin}{0in}
\setlength{\headheight}{0in} \setlength{\headsep}{-1in}
\setlength{\parindent}{1pc}
\setlength{\oddsidemargin}{-.5in}  % Centers text.
\setlength{\evensidemargin}{-.5in}

% Add the period after section numbers.  Adjust spacing.
\newcommand{\Section}[1]{\vspace{-8pt}\section{\hskip -1em.~~#1}\vspace{-3pt}}
\newcommand{\SubSection}[1]{\vspace{-3pt}\subsection{\hskip -1em.~~#1}
        \vspace{-3pt}}
\newcommand{\bqn}{\begin{eqnarray}}
\newcommand{\eqn}{\end{eqnarray}}
\newcommand {\diff}[1] {\frac{\partial}{\partial #1}}
\newcommand{\jacob}[3]{\frac{\partial^2 #3}{\partial #1 \partial #2}}
\newcommand{\der}[2]{\frac{\partial #2}{\partial #1}}
\begin{document}

% Make title bold and 14 pt font (Latex default is non-bold, 16pt)
\title{Stat 471: Lecture 3\\
Accept-Reject Method}
% For single author (just remove % characters)
\author{Moo K. Chung\\
mchung@stat.wisc.edu}
% For two authors (default example)
\maketitle \thispagestyle{empty}
\begin{enumerate}

\item Generate $X \sim Unif(0,\frac{1}{2})$ using uniform
distribution $U \sim Unif(0,1)$.\\
Step 1. generate $U$.\\
Step 2. if $U < 1/2$, $X=U$, else go to step 1.\\
The method seems intuitively correct. This is the simplest version
of the accept-reject method. But how do you actually show this is
in fact correct? Let's study the general setting.

\item {\em Accept-reject method}. Given known random number
generators $U \sim Unif(0,1)$ and $X \sim g$, we can generate $Y
\sim f$ by the following algorithm. Let $c$ be a constant such
that $f(x)
\leq cg(x)$ for all $x$.\\
Step 1. Generate $X \sim g$, $U \sim Unif(0,1)$.\\
Step 2. Accept $Y = X$ if $U \leq \frac{f(X)}{cg(X)}$ otherwise go
to Step 1.\\
{\em proof.} We show that the conditional distribution $P(X < y |
U \leq \frac{f(X)}{cg(X)}) = P(Y \leq y)$. We see that the
conditional distribution is
$$\frac{P(X \leq y, U \leq \frac{f(X)}{cg(X)})}{P(U \leq
\frac{f(X)}{cg(X)})}=\frac{\int_{-\infty}^y \int_0^{f(x)/cg(x)}
g(x) \;du \;dx}{\int_{-\infty}^{\infty} \int_0^{f(x)/cg(x)} g(x)
\; du \; dx}.$$ Simplifying this, it can be shown to be
$\int_{-\infty}^y f(x) \;dx$.

\item From $U \sim Unif(0,1)$, generate 1000 random numbers that
follow probability density $f(y) = 2y, 0 < y < 1$. {\em solution.}
Let $g$ follows $Uinf(0,1)$. $g=1$. Then $f(y)/g(y)=f(y) = 2y < 2$
for $y \in (0,1)$. So we choose $c=2$. Usually choose the smallest
$c$ that satisfies the inequality.
\begin{verbatim}
i=1; 
while i <= 1000
  U = rand;X = rand;
  if U < X
    Y(i)=X;
    i=i+1;
  end;
end;
\end{verbatim}
\item A simple version of the accept-reject method. Choose $X \sim
Unif(0,1)$.\\
Step 1. Generate $X, U \sim Unif(0,1)$.\\
Step 2. Accept $Y = X$ if $U \leq \frac{1}{c}f(X)$ otherwise go
to Step 1.\\
But this may not be an efficient algorithm. The following example
illustrate this point.
\item Generate 1000 exponential random numbers with parameter
$\lambda=2$ from $U\sim Unif(0,1)$. {\em solution.} Let
$f=e^{-2x}/2, 0 \leq x$ and $g \sim Unif(0,1)$. Since $f(y) \leq
1/2$ for all $0 \leq y$, choose $c=1/2$.
\begin{verbatim}
>> f=inline('exp(-2*x)/2')
f =
     Inline function:
     f(x) = exp(-2*x)/2
i=1; 
while i <= 1000 
   U = rand; X = rand; 
   if (U < 2*f(X)) 
      Y(i)=X;
      i=i+1; 
   end; 
end;
\end{verbatim}
\item In lecture 2, we studied the integral transform based
algorithm for generating exponential random variables, i.e.
{\tt >> U=rand(1000,1); X=-log(U)/2}.
Which one performs better? It is easy to check by computing the
sample mean which should converge to the population mean as the
number of samples increases.
\begin{verbatim}
>> mean(X)
ans = 0.4857
>> mean(Y)
ans = 0.3444
\end{verbatim}
To use the accept-reject method, the distributions $f$ and $g$
should be somewhat similar to have a sufficiently good algorithm.

\end{enumerate}
\end{document}
