\documentclass[11 pt,xcolor={dvipsnames,svgnames,x11names,table}]{beamer}

\usepackage[english]{babel} 
\usepackage{polski}         
\usepackage[skins,theorems]{tcolorbox}
\tcbset{highlight math style={enhanced,
  colframe=red,colback=white,arc=0pt,boxrule=1pt}}

\usetheme[
	bullet=circle,		% Other option: square
	bigpagenumber,		% circled page number on lower right
	topline=true,			% colored bar at the top of the frame 
	shadow=false,			% Shading for beamer blocks
	watermark=BG_lower,	% png file for the watermark
	]{Flip}

%\logo{\kern+1.em\includegraphics[height=1cm]{SHiP-3_LightCharcoal}}
                            

\usepackage[lf]{berenis}
\usepackage[LY1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{emerald}
\usefonttheme{professionalfonts}
\usepackage[no-math]{fontspec}	
\usepackage{listings}
\defaultfontfeatures{Mapping=tex-text}	% This seems to be important for mapping glyphs properly

\setmainfont{Gillius ADF}			% Beamer ignores "main font" in favor of sans font
\setsansfont{Gillius ADF}			% This is the font that beamer will use by default
% \setmainfont{Gill Sans Light}		% Prettier, but harder to read

\setbeamerfont{title}{family=\fontspec{Gillius ADF}}

\input t1augie.fd

%\newcommand{\handwriting}{\fontspec{augie}} % From Emerald City, free font
%\newcommand{\handwriting}{\usefont{T1}{fau}{m}{n}} % From Emerald City, free font
% \newcommand{\handwriting}{}	% If you prefer no special handwriting font or don't have augie

%% Gill Sans doesn't look very nice when boldfaced
%% This is a hack to use Helvetica instead
%% Usage: \textbf{\forbold some stuff}
%\newcommand{\forbold}{\fontspec{Arial}}

\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{amsmath, amssymb} 

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{colortbl}
\usepackage{mathrsfs} 			% For Weinberg-esque letters
\usepackage{cancel}				% For "SUSY-breaking" symbol
\usepackage{slashed}            % for slashed characters in math mode
\usepackage{bbm}                % for \mathbbm{1} (unit matrix)
\usepackage{amsthm}				% For theorem environment
\usepackage{multirow}			% For multi row cells in table
\usepackage{arydshln} 			% For dashed lines in arrays and tables
\usepackage{siunitx}
\usepackage{xhfill}
\usepackage{grffile}
\usepackage{textpos}
\usepackage{subfigure}
\usepackage{tikz}
\usepackage{hyperref}
%\usepackage{hepparticles}    
\usepackage[italic]{hepparticles}     

\usepackage{hepnicenames} 

% Drawing a line
\tikzstyle{lw} = [line width=20pt]
\newcommand{\topline}{%
  \tikz[remember picture,overlay] {%
    \draw[crimsonred] ([yshift=-23.5pt]current page.north west)
             -- ([yshift=-23.5pt,xshift=\paperwidth]current page.north west);}}



% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
\usepackage{tikzfeynman}		% For Feynman diagrams
\usetikzlibrary{arrows,shapes}
\usetikzlibrary{trees}
\usetikzlibrary{matrix,arrows} 				% For commutative diagram
% http://www.felixl.de/commu.pdf
\usetikzlibrary{positioning}				% For "above of=" commands
\usetikzlibrary{calc,through}				% For coordinates
\usetikzlibrary{decorations.pathreplacing}  % For curly braces
% http://www.math.ucla.edu/~getreuer/tikz.html
\usepackage{pgffor}							% For repeating patterns

\usetikzlibrary{decorations.pathmorphing}	% For Feynman Diagrams
\usetikzlibrary{decorations.markings}
\tikzset{
	% >=stealth', %%  Uncomment for more conventional arrows
	vector/.style={decorate, decoration={snake}, draw},
	provector/.style={decorate, decoration={snake,amplitude=2.5pt}, draw},
	antivector/.style={decorate, decoration={snake,amplitude=-2.5pt}, draw},
	fermion/.style={draw=gray, postaction={decorate},
		decoration={markings,mark=at position .55 with {\arrow[draw=gray]{>}}}},
	fermionbar/.style={draw=gray, postaction={decorate},
		decoration={markings,mark=at position .55 with {\arrow[draw=gray]{<}}}},
	fermionnoarrow/.style={draw=gray},
	gluon/.style={decorate, draw=black,
		decoration={coil,amplitude=4pt, segment length=5pt}},
	scalar/.style={dashed,draw=black, postaction={decorate},
		decoration={markings,mark=at position .55 with {\arrow[draw=black]{>}}}},
	scalarbar/.style={dashed,draw=black, postaction={decorate},
		decoration={markings,mark=at position .55 with {\arrow[draw=black]{<}}}},
	scalarnoarrow/.style={dashed,draw=black},
	electron/.style={draw=black, postaction={decorate},
		decoration={markings,mark=at position .55 with {\arrow[draw=black]{>}}}},
	bigvector/.style={decorate, decoration={snake,amplitude=4pt}, draw},
}

% TIKZ - for block diagrams, 
% from http://www.texample.net/tikz/examples/control-system-principles/
% \usetikzlibrary{shapes,arrows}
\tikzstyle{block} = [draw, rectangle, 
minimum height=3em, minimum width=6em]

\def\ARROW{{\color{JungleGreen}{$\Rrightarrow$}}\xspace}  



\usetikzlibrary{backgrounds}
\usetikzlibrary{mindmap,trees}	% For mind map
\newcommand{\degree}{\ensuremath{^\circ}}
\newcommand{\E}{\mathrm{E}}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\Cov}{\mathrm{Cov}}
\newcommand\Ts{\rule{0pt}{2.6ex}}       % Top strut
\newcommand\Bs{\rule[-1.2ex]{0pt}{0pt}} % Bottom strut

\graphicspath{{images/}}	% Put all images in this directory. Avoids clutter.

% SOME COMMANDS THAT I FIND HANDY
% \renewcommand{\tilde}{\widetilde} % dinky tildes look silly, dosn't work with fontspec
%\newcommand{\comment}[1]{\textcolor{comment}{\footnotesize{#1}\normalsize}} % comment mild
%\newcommand{\Comment}[1]{\textcolor{Comment}{\footnotesize{#1}\normalsize}} % comment bold
%\newcommand{\COMMENT}[1]{\textcolor{COMMENT}{\footnotesize{#1}\normalsize}} % comment crazy bold
\newcommand{\Alert}[1]{\textcolor{Alert}{#1}} % louder alert
\newcommand{\ALERT}[1]{\textcolor{ALERT}{#1}} % loudest alert
%% "\alert" is already a beamer pre-defined
\newcommand*{\Scale}[2][4]{\scalebox{#1}{$#2$}}%

\def\Put(#1,#2)#3{\leavevmode\makebox(0,0){\put(#1,#2){#3}}}

\usepackage{gmp}
\usepackage[final]{feynmp-auto}

\usepackage[backend=bibtex,style=numeric-comp,firstinits=true]{biblatex}
\bibliography{bib}
\setbeamertemplate{bibliography item}[text]

\makeatletter\let\frametextheight\beamer@frametextheight\makeatother

% suppress frame numbering for backup slides
% you always need the appendix for this!
\newcommand{\backupbegin}{
	\newcounter{framenumberappendix}
	\setcounter{framenumberappendix}{\value{framenumber}}
}
\newcommand{\backupend}{
	\addtocounter{framenumberappendix}{-\value{framenumber}}
	\addtocounter{framenumber}{\value{framenumberappendix}} 
}


\definecolor{links}{HTML}{2A1B81}
%\hypersetup{colorlinks,linkcolor=,urlcolor=links}

% For shapo's formulas:
\def\lsi{\raise0.3ex\hbox{$<$\kern-0.75em\raise-1.1ex\hbox{$\sim$}}}
\def\gsi{\raise0.3ex\hbox{$>$\kern-0.75em\raise-1.1ex\hbox{$\sim$}}}
\newcommand{\lsim}{\mathop{\lsi}}
\newcommand{\gsim}{\mathop{\gsi}}
\newcommand{\wt}{\widetilde}
%\newcommand{\ol}{\overline}
\newcommand{\Tr}{\rm{Tr}}
\newcommand{\tr}{\rm{tr}}
\newcommand{\eqn}[1]{&\hspace{-0.7em}#1\hspace{-0.7em}&}
\newcommand{\vev}[1]{\rm{$\langle #1 \rangle$}}
\newcommand{\abs}[1]{\rm{$\left| #1 \right|$}}
\newcommand{\eV}{\rm{eV}}
\newcommand{\keV}{\rm{keV}}
\newcommand{\GeV}{\rm{GeV}}
\newcommand{\im}{\rm{Im}}
\newcommand{\disp}{\displaystyle}
\def\be{\begin{equation}}
\def\ee{\end{equation}}
\def\ba{\begin{eqnarray}}
\def\ea{\end{eqnarray}}
\def\d{\partial}
\def\l{\left(}
\def\r{\right)}
\def\la{\langle}
\def\ra{\rangle}
\def\e{{\rm e}}
\def\Br{{\rm Br}}
\def\fixme{{\color{red} FIXME!}}
\def\mc{{\color{Magenta}{MC}}}
\def\pdf{{\rm p.d.f.}}
\def\cdf{{\rm c.d.f.}}
\author{ {\fontspec{Trebuchet MS}Marcin Chrz\k{a}szcz} (Universit\"{a}t Z\"{u}rich)}
\institute{UZH}
\title[Arbitrary \pdf~generation]{Arbitrary \pdf~generation}
\date{\fixme}
\newcommand*{\QEDA}{\hfill\ensuremath{\blacksquare}}%
\newcommand*{\QEDB}{\hfill\ensuremath{\square}}%

\begin{document}
\tikzstyle{every picture}+=[remember picture]

{
\setbeamertemplate{sidebar right}{\llap{\includegraphics[width=\paperwidth,height=\paperheight]{bubble2}}}
\begin{frame}[c]%{\phantom{title page}} 
\begin{center}
\begin{center}
	\begin{columns}
		\begin{column}{0.9\textwidth}
			\flushright\fontspec{Trebuchet MS}\bfseries \Huge {Arbitrary \pdf~generation}
		\end{column}
		\begin{column}{0.2\textwidth}
		  %\includegraphics[width=\textwidth]{SHiP-2}
		\end{column}
	\end{columns}
\end{center}
	\quad
	\vspace{3em}
\begin{columns}
\begin{column}{0.44\textwidth}
\flushright \vspace{-1.8em} {\fontspec{Trebuchet MS} \Large Marcin Chrząszcz\\\vspace{-0.1em}\small \href{mailto:mchrzasz@cern.ch}{mchrzasz@cern.ch}}

\end{column}
\begin{column}{0.53\textwidth}
\includegraphics[height=1.3cm]{uzh-transp}
\end{column}
\end{columns}

\vspace{1em}
%		\footnotesize\textcolor{gray}{With N. Serra, B. Storaci\\Thanks to the theory support from M. Shaposhnikov, D. Gorbunov}\normalsize\\
\vspace{0.5em}
	\textcolor{normal text.fg!50!Comment}{Monte Carlo methods, \\ 7 April, 2016}
\end{center}
\end{frame}
}


\begin{frame}\frametitle{Reverting the \cdf}
 \begin{footnotesize}
 \begin{alertblock}{~}
\ARROW Let $U$ be a random variable from $\mathcal{U}(0,1)$\\
\ARROW Now let $F$ be a non decreasing function such that:
\begin{align*}
F(- \infty) =0~~~~F(\infty)=1
\end{align*}
then:
\begin{align*}
X=F^{-1}(U)
\end{align*}
has a \pdf~ distribution with a \cdf~function of F.
 \end{alertblock}
\ARROW Prove:
\begin{align*}
F(x)=\mathcal{P}(U\leq F(x))=\mathcal{P}(F^{-1}(U) \leq x) = \mathcal{P}(X \leq x)  ~~~~  \QEDB
\end{align*}
\ARROW So it looks very simple if $x_1,X_2,...,X_n$ are random variables from $\mathcal{U}(0,1)$ then: $\lbrace X_i=F^{-1}(x_i)\rbrace,i=1,...,n$ is the sequence that has a \cdf~distirbution of $F$.  

 
 \end{footnotesize}

\end{frame}

\begin{frame}\frametitle{Reverting the \cdf, examples}
 \begin{footnotesize}
\ARROW The exponential distribution: $E(0,1)$. \\
\ARROW The \pdf : $\rho(X)=e^{-X},~X \geq 0$.\\
\ARROW The \cdf : $F(x) = \int_0^x e^{-X} dX= 1- e^{-x}.$\\
\ARROW Now let $R \in \mathcal{U}(0,1):~R=F(X)=1-e^{-X} \longrightarrow X = -\ln(1-R)$\\
\ARROW Now we can play a trick: if $R \in \mathcal{U}(0,1)$ then $1-R$ also in $\mathcal{U}(0,1)$. \\
\ARROW In the end we get: $X=-\ln(R)$  \\
 {~}\\
\ARROW Use the reverting to generate the following distributions:
\begin{itemize}
\item E 6.1 $\rho(X)= \dfrac{c}{X}$ on the interval $[a,b]$, where $0<a<b<\infty$.
\item E6.2 The Breit-Wigner function:
\begin{align*}
\rho_{\theta,\lambda}(X)=\dfrac{\lambda}{\pi} \dfrac{1}{(X-\theta)^2 + \lambda^2}
\end{align*}
Hit: First do $C(0,1)$ then transform the variables.
\end{itemize} 
 
 \end{footnotesize}

\end{frame}

\begin{frame}\frametitle{Reverting the \cdf, general case}
 \begin{footnotesize}
 \begin{alertblock}
\ARROW Lets assume: $F$ - non decreasing function such that: $F(-\infty)=0$ and $F(\infty)=1$. Then a random variable $X$:
\begin{align*}
X=\inf \lbrace x: U \leq F(x) \rbrace
\end{align*}
has a distribution with a \cdf~of $F$
\end{alertblock} 


\begin{center}
\includegraphics[width=0.5\textwidth]{images/code.png}
\end{center}
 \ARROW E6.3 Using the above example please generate the \pdf~accordingly to:
 \begin{equation}
 \mathcal{ P}(X=k) =p_k=A \sin (\dfrac{\pi}{10}\left[k+\dfrac{1}{2}\right])\nonumber
 \end{equation}
 where $A$ is the normalization constant (calculate it!). From the generated numbers make a histogram and compare to the exact function.
 \end{footnotesize}

\end{frame}




\begin{frame}\frametitle{Reverting the \cdf, pros and cons}
 \begin{footnotesize}
\ARROW Pros:
\begin{itemize}
\item Very accurate method.
\item Fast and easy.
\item To generate one random number from \cdf~you need one random number from $\mathcal{U}(0,1)$.
\end{itemize}
\ARROW Cons:
\begin{itemize}
\item Usually we require that the \cdf~is revertible analytically( small number of functions).
\item If we use the numerical method of reverting the function, then it's much slower and less accurate.
\item Super hard fro multidimensional distributions.
\end{itemize}

\ARROW Mathematical digression: Numerical reverting the \cdf :
\begin{itemize}
\item We look for the zero of the function: $F_u(X)\equiv F(X)-U$, where $U \in \mathcal{U}(0,1)$.
\item $X_0=F^{-1}(U)$
\end{itemize}

 \end{footnotesize}

\end{frame}

\begin{frame}\frametitle{J. von Neumann elimination method, 1951}
 \begin{footnotesize}
 \ARROW Let $X$ be a random variable with a \pdf~ $f(x)$ on an interval $\left[a,b\right]$, such that 
$=\infty <a<b < \infty$ and $f(x)\leq c,~\forall_{x \in \left[a,b\right]},~c<\infty$\\
\ARROW The algorithm:
\begin{enumerate}
\item We generate a point: $(U_1,U_2):~U_1 \in \mathcal{U}_1(a,b),~\mathcal{U}_2(0,c)$
\item If $U_2 \leq f(U_1)$ then $X=U_1$.
\item If not we eliminate the $(U_1,U_2)$ pair and we start over.
\end{enumerate}
\ARROW Prove:\\
$X$ has a \pdf~of $f(x)$.\\
$\mathcal{P}(X\leq t) =(b-a) c \int_a^t\dfrac{d_{u_1}}{b-a} \int_0^c \dfrac{d_{u_2}}{c}\Theta(f(u_1)-u_2)=\int_a^t du_1 f(u_1)$ \\
\pause
\ARROW We know this already from the Buffon needle:
\begin{columns}
\column{0.1in}
{~}
\column{3.2in}



Generate 2 dim. distribution:
\begin{align*}
(x,y): \mathcal{U}(0,\dfrac{\pi}{2})\times \mathcal{U}(0,1) {\rm{~and~}}
\end{align*}
\begin{align*}
y 
\begin{cases}
  \leq p(x): & \text{hit},\\
> p(x):             & \text{miss}.
\end{cases}
\end{align*}

\column{2.5in}
\includegraphics[width=0.75\textwidth]{images/result.png}



\end{columns}
\ARROW In the end we managed to generate a \pdf : $f(x)=\cos(x)$.
 \end{footnotesize}

\end{frame}

\begin{frame}\frametitle{Elimination method, example for the Buffon needle}
 \begin{footnotesize}
\begin{center}
\includegraphics[width=0.65\textwidth]{images/result.png}
\end{center}
\ARROW We generate the points from $(U_1,U_2)$ from $\mathcal{U}(0,\dfrac{\pi}{2}) \times \mathcal{U}(0,1)$.\\
\ARROW Points that are below the function we keep.\\
\ARROW The ones above we reject.\\
\ARROW The new variable will have the $f(x)$ \pdf.




 \end{footnotesize}

\end{frame}

\begin{frame}\frametitle{Elimination method, multi dimension case}
 \begin{footnotesize}
\ARROW Let $X=(X_1,X_2,...,X_m)$ be a $m$ dimensional random variable with a \pdf of $f(x_1,x_2,...,x_m)$ of the domain of $\Omega \in \mathcal{R}^m$ and $f(x_1,x_2,..x_m)<c<\infty$.\\
\ARROW The algorithm:
\begin{itemize}
\item We generate the point $(U_1,U_2,...,U_m) \in \mathcal{U}(\Omega)$ and $U_{m+1} \in \mathcal{U}(0,c)$
\item If $U_{m+1} \leq f(U_1,U_2,U_3,...U_m)$, then $X=(U_1,U_2,...,U_m)$
\item If not we start over.
\end{itemize}
\ARROW The constructed random variable has the distribution of the \pdf~$f(x_1,x_2,...,x_m)$.\\
\ARROW Now how do get an $\mathcal{U}(\Omega)$?\\
\pause
\ARROW We generate a hypercube $K^m: \Omega \subset K^m$ and accept the points that are within the $\Omega$ domain $(\in \Omega)$. \\
\ARROW E6.4 Using the Elimination method generate the following function:
\begin{align*}
\psi(x,y)=c\sqrt{1-(x^2+y^2)},~~x^2+y^2\leq 1
\end{align*}
where $c$ is a normalization constant(to be calculated). Using ROOT make a 2D histogram and compare with the $\psi(x,y)$ function.



 \end{footnotesize}

\end{frame}



\begin{frame}\frametitle{Elimination method, general case}
 \begin{footnotesize}
\ARROW Let $f(x)$ be a \pdf~accordingly to which we want to generate random variables. ($x$ can be a multi dimension point). \\
\ARROW The algorithm:
\begin{itemize}
\item We find a function $g(x)$ such that generating accordingly to it is fast and easy.
\item We find a constant $c$ such that:
\begin{align*}
f(x) \leq c g(x), \forall_x,~~~{ \rm{ optimal~value~of~c}}~~c=\sup_x \dfrac{f(x)}{g(x)}
\end{align*}
\item We generate $X$ accordingly to $g(x)$ and a random variable $U \in \mathcal{U}(0,c)$
\item If $Ug(x) \leq f(X)$ then we accept $X$.
\item If not the case we start over.
\end{itemize}
\ARROW The alternative way:
\begin{itemize}
\item We calculate the weight: $w(X)=\dfrac{f(X)}{g(X)}$. We find the maximum weight.
\item If $w(X) \geq U w_{max}$ then we accept the $X$.
\item If not go back :)
\end{itemize}
\ARROW Remark:\\
If you are not scared of weighting you might not reject the events but make a weighted histogram.

 \end{footnotesize}

\end{frame}



\begin{frame}\frametitle{Elimination method, limitations}
 \begin{footnotesize}
\ARROW The zeros of the $g(x)$ functions are very dangerous: $f(x) \neq 0$.\\
\ARROW Peaks $f(x)$ are dangerous as they can screw up the efficiency of generating the distributions if they are not correctly approximated by $g(x)$.

\begin{center}
\includegraphics[width=0.85\textwidth]{images/elimin.png}
\end{center}
\begin{columns}
\column{2.5in}
\begin{center}
Zeros of the function.
\end{center}
\column{2.5in}
\begin{center}
Inefficiency of the generation.
\end{center}
\end{columns}

 \end{footnotesize}

\end{frame}



\begin{frame}\frametitle{Elimination method, example}
 \begin{footnotesize}
\ARROW Example: $N(0,1)$:
\begin{align*}
f(x) = \sqrt{\dfrac{2}{\pi}} e^{-\frac{x^2}{2}},~x \geq 0.
\end{align*}
\ARROW We choose the $g(x)$ to simulate the tails:
\begin{align*}
g(x)=e^{-x},~x \geq 0
\end{align*}
\ARROW The weight:
\begin{align*}
w(X)=\frac{f(X()}{g(X)} = \sqrt{\frac{2}{\pi}}e^{\frac{x(2-x)}{2}}
\end{align*}
\ARROW And the maximum weight is:
\begin{align*}
w_{max}=w(1)=\sqrt{\frac{2 e}{\pi}}
\end{align*}
\ARROW E6.5 Code up the above example. Use weighted and non weighted case. Compare the results.

 \end{footnotesize}

\end{frame}



\begin{frame}\frametitle{Superposition of distribution}
 \begin{footnotesize}
\ARROW Continues superposition of distribution.\\
\ARROW Let $X$ be a random variable from the \pdf~$f(x)$:
\begin{align*}
f(x)=\int g_y(x) h(y) dy
\end{align*}
where $g_y(x)$ is a \pdf~depending on $y$ parameter;\\
$h(y)$ is a \pdf .\\
\ARROW The algorithm:
\begin{itemize}
\item  Generate the $Y$ accordingly to $h(y)$.
\item For a given $Y$ generate the events accordingly to $g_Y(x)$.
\end{itemize}
\ARROW Example: $f(x) = n \int_1^{\infty} y^{-n} e^{-xy}dy,~~x,y>0,~n>1.$\\
We can write down the functions: $g_Y(x)=ye^{-xy}$ and $h(y)=ny^{-(n+1)}$.
\begin{enumerate}
\item The numbers $Y$ should be generated from the \pdf $h(y)$. One can use the Reverting \cdf~for example: $Y=(1-U)^{1/n}$, where $U \in \mathcal{U}(0,1)$.
\item The numbers $X$ of a \pdf $g_Y(x)$ are generated from the exponential \pdf~$E(0,\frac{1}{y}):
X=- \frac{1}{Y}\ln V$, where $V \in \mathcal{U}(0,1)$.
\end{enumerate}



 \end{footnotesize}

\end{frame}


\begin{frame}\frametitle{Discrete superposition of distribution}
 \begin{footnotesize}
\ARROW Let's:
\begin{align*}
f(x) = \sum_{i=1}^{\infty} p_i g_i(x)
\end{align*}
where $p_i$ discrete \pdf . $p_i\geq 0$ and $\sum_{i=1}^{\infty} p_1 =1$\\
\ARROW The algorithm:
\begin{enumerate}
\item Generate the $i$ number accordingly to the $p_i$ \pdf . Using Reverting \cdf~method.
\item For a given $i$ generate the number $X$ from the $g_i(x)$.
\end{enumerate}
\ARROW Example: $f(x)=\dfrac{5}{12}\left[ 1+ (x-1)^4\right],~0\leq x \leq 2.$\\
$\rightarrow~f(x)=\dfrac{5}{6}g_1(x)+1/6 g_2(x),$\\
where $g_1(x)=\dfrac{1}{2}$ and $g_2(x)=\dfrac{5}{2}(x-1)^4$, so $p_1=\dfrac{5}{6}$ and $p_2=\dfrac{1}{6}$.
\begin{align*}
X= \begin{cases}
2 U_2~~~~{ \rm if~  } U_1 < \dfrac{5}{6}\\
1+(2U_2-1)^{\frac{1}{5}}~~~~{ \rm if~  } U_1 < \dfrac{5}{6}                                
\end{cases}
\end{align*}
$U_1,U_2 \in \mathcal{U}(0,1)$.

 \end{footnotesize}

\end{frame}

\iffalse
\begin{frame}\frametitle{Discrete superposition of distribution, multidim case}
 \begin{footnotesize}
\fixme

 \end{footnotesize}

\end{frame}
\fi


\begin{frame}\frametitle{Combination of the superposition and elimination method}
 \begin{footnotesize}

\ARROW Let's:
\begin{align*}
f(x) = \sum_{i=1}^{\infty} p_i f_i(x),~~~p_i\leq 0,~~\sum_{i=1}^n p_i=1
\end{align*}
where $f_i(x)$ functions that depend on $i$.\\
For each $f_i$ we find a weight function $g_i(x)$ and a constant $c_i>0$ such as:
\begin{align*}
f_i(x) \leq c_i g_i(x) \forall_x . 
\end{align*}
\ARROW The algorithm:
\begin{enumerate}
\item Generate $i$ accordingly to the distribution $p_i$
\item For a given $i$ generate $X$ from the \pdf~$g_i(x)$.
\item Generate a $U\in \mathcal{U}(0,1)$.
\item If $c_i U g_i(X) \leq f_i(X)$ then we accept the $X$ if not then we start over.
\end{enumerate}
\ARROW Alternative:
\begin{enumerate}
\item One can also calculate the weights: $w(X)=\dfrac{f_i(X)}{g_i(X)}$.
\item Find the maximal weight: $w_i^{max}$.
\item If $w_i(X) \geq U w_i^{max}$ we accept $X$. If not then start over
\end{enumerate}



 \end{footnotesize}

\end{frame}

\begin{frame}\frametitle{Combination, warnings, example}
 \begin{footnotesize}
\ARROW We need a different maximal weight for each ''branch''. This can be done either numerically or analytically.\\
\ARROW Example:
\begin{itemize}
\item Let our \pdf~be in a form of:
\end{itemize}
\begin{align*}
f(x)=\sum_{i=1}^n c_ix^i,~~0 \leq x \leq 1
\end{align*}
but $\exists_{j \in \lbrace 1,...,n \rbrace}: c_j <0$.
We can do a transmigration: 
\begin{align*}
cj=c^+_j -c^-_j: c^+_j, c^-_j>0
\end{align*}
\ARROW So:
\begin{align*}
f(x)=\sum_{i=1}^n c_i x^i \leq \sum_{i=1}^n c_i^+ x^i = g(x)
\end{align*} 
 \ARROW Now we use the weight function:
 \begin{align*}
 \bar{g}(x)=\frac{g(x)}{\sum_{i=1}^n c_i^+}
 \end{align*}
 
 
 \end{footnotesize}

\end{frame}

\begin{frame}\frametitle{Combination, example}
 \begin{footnotesize}
\ARROW The algorithm:
\begin{itemize}
\item Generate $X$ accordingly to $\bar{g}(x)$ \pdf. The same we did in the discrete superposition method.
\item Generate $U \in \mathcal{U}(0,1)$
\item If $Ug(X) \leq f(X)$ then we accept the $X$. If not start over.
\end{itemize}
\ARROW Example $N(0,1)$: 
 \begin{align*}
 f(x) = \sqrt{\dfrac{2}{\pi}} e^{-\frac{x^2}{2}},~x \geq 0.
 \end{align*}
\ARROW We write the function as:
\begin{align*}
f(x) \alpha_1 g_1(x)h_1(x)+\alpha_2 g_2(x)h_2(x)
\end{align*}
where:
\begin{columns}
\column{2.5in}
\begin{align*}
\alpha_1=\sqrt{\frac{2}{\pi}}
\end{align*}
\begin{align*}
g_1=\begin{cases}
1,~0\leq x \leq 1\\
0,~x \geq 1\\
\end{cases}
\end{align*}

\column{2.5in}
\begin{align*}
\alpha_2=\sqrt{\frac{1}{2 \pi}}
\end{align*}
\begin{align*}
g_2=\begin{cases}
2e^{-2(x-1)},~x \geq 1\\
0,~0\leq x \leq 1\\
\end{cases}
\end{align*}


\end{columns}
 
 \end{footnotesize}

\end{frame}
\begin{frame}\frametitle{Combination, example}
 \begin{footnotesize}

\begin{columns}
\column{2.5in}

\begin{align*}
h_1(x)=e^{\frac{-x^2}{2}}
\end{align*}

\column{2.5in}

\begin{align*}
h_2(x)=e^{-\frac{(x-2)^2}{2}}
\end{align*}

\end{columns}
{~}\\{~}\\

\ARROW The variable $X$ is chosen:
\begin{itemize}
\item With a probability: $\alpha_1/(\alpha_1+\alpha_2) =2/3$ from the $g_1$. Then we do the elimination accordingly to $h_1$ function, aka. $h_1(X) \geq U \in \mathcal{U}(0,1)$
\item   With a probability: $\alpha_2/(\alpha_1+\alpha_2) =2/3$ from the $g_2$. Then we do the elimination accordingly to $h_2$ function, aka. $h_2(X) \geq U \in \mathcal{U}(0,1)$
\end{itemize}
\ARROW E6.6 Generate the above distribution using with the above method. Do a histogram and compare with desired \pdf.
 
 \end{footnotesize}

\end{frame}

\begin{frame}\frametitle{Acceptance - complement, Kornal \& Peterson 1981}
 \begin{footnotesize}
\ARROW If $f=f_1+f_2$ is a \pdf~ of a random variable $X$ and $p=\int f_1(x) dx.$\\
\ARROW $f_1/p_1$ and $f_2(1-p)$ are the \pdf of a discrete function.\\
\ARROW Now if the functions $f_1$ and $f_2$ are easy to generate from then:
\begin{itemize}
\item We choose the numbers with the probability distribution $f_1/p_1$ from the \pdf~$f_1$.
\item We choose the numbers with the probability distribution $f_2/(1-p_1)$ from the \pdf~$f_2$.
\end{itemize}
\ARROW The method is super powerful if $f_2= { \rm const}$. 

 
 \end{footnotesize}

\end{frame}


\backupbegin   

\begin{frame}\frametitle{Backup}


\end{frame}

\backupend			

\end{document}
