% 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}{7in}
%\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 12\\
Importance Sampling II.}
% 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 Cauchy distribution. The general Cauchy distribution with
parameter $\theta$ is defined as $f(x)=\frac{c}{\pi(x^2 + c^2)}
\sim Cauchy(c)$. CDF is $F(x) = \frac{1}{\pi}\arctan(\frac{x}{c})
+ \frac{1}{2}$ and $F^{-1}(x) = -c\cot(\pi x)$. Based on this we
can generate Cauchy.

\begin{verbatim}
cauchypdf
  =inline('c./(pi*(c^2+x.^2))')
>>cauchyrnd
  =inline('-c*cot(pi*rand(n,1))')
cauchyrnd 
=Inline function:
 cauchyrnd(c,n)=-c*cot(pi*rand(n,1))
\end{verbatim}


 \item In the previous
lecture we showed how to estimate
$$p=\int_{-\infty}^{-2} \frac{e^{-x^2/2}}{\sqrt{2\pi}} \; dx$$
using simple Monte-Carlo integration technique.
\begin{verbatim}
n=1000000
X=snorm(n);
index=find(X<-2);
g=zeros(n,1);
g(index)=1;
>>[mean(g) var(g)]
ans =
    0.0228    0.0223
\end{verbatim}

Now we apply importance sampling technique.
$$\mathbb{E}_f \;g(X)= \mathbb{E}_h \Big[g(X)\frac{f(X)}{h(X)}\Big],$$
where $X \sim f$. Let $g(x)$ to be an index function such that $g(x)= 1$ if $x \in
(-\infty,-2)$ and 0 otherwise. The rule of thumb is to choose $h$
such that $f/h$ is bounded and has a thicker tail than $f$.
Further $|g|f/h$ should be almost constant with finite variance. 
 Let $X \sim N(0,1)$. Then $\hat p =
\sum_{i=1}^ng(X_i)/n$. Let $h \sim Cauchy(1)$. Then

\begin{verbatim}
n=1000000; 
X=cauchyrnd(1,n);
srnpdf
 =inline('exp(-(x.^2/2)/sqrt(2*pi)');
g=(X<-2);
Y=g.*srnpdf(X)./cauchypdf(1,X);
 [mean(Y) var(Y)]
ans =
     0.0228    0.0118
\end{verbatim}
Compare the performance 
\begin{verbatim}
n=1000
n=10000
n=100000
n=1000000
[mean(g) var(g)]
ans =
    0.0290    0.0282
    0.0219    0.0214
    0.0227    0.0222
    0.0228    0.0223
[mean(Y) var(Y)]
ans =
    0.0201    0.0104
    0.0231    0.0119
    0.0232    0.0120
    0.0228    0.0118
\end{verbatim}
\item Trivia: the term Monte-Carlo was used by Ulam and von
Neumann as a Los Alamos code word for the stochastic simulations
they applied to building better atomic bombs.
\end{enumerate}

\end{document}
