% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

      \subsection{gp1d.m}

\begin{par}
\textbf{Summary:} Compute joint predictions (and derivatives) for the FITC sparse approximation to multiple GPs with uncertain inputs. Predictive variances contain uncertainty about the function, but no noise. If gpmodel.nigp exists, individual noise contributions are added.
\end{par} \vspace{1em}
\begin{verbatim}function [M, S, V, dMdm, dSdm, dVdm, dMds, dSds, dVds] = gp1d(gpmodel, m, s)\end{verbatim}
\begin{par}
\textbf{Input arguments:}
\end{par} \vspace{1em}
\begin{verbatim}gpmodel    GP model struct
  hyp      log-hyper-parameters                                  [D+2 x  E ]
  inputs   training inputs                                       [ n  x  D ]
  targets  training targets                                      [ n  x  E ]
  nigp     (optional) individual noise variance terms            [ n  x  E ]
m          mean of the test distribution                         [ D  x  1 ]
s          covariance matrix of the test distribution            [ D  x  D ]\end{verbatim}
\begin{par}
\textbf{Output arguments:}
\end{par} \vspace{1em}
\begin{verbatim}M          mean of pred. distribution                            [ E  x  1 ]
S          covariance of the pred. distribution                  [ E  x  E ]
V          inv(s) times covariance between input and output      [ D  x  E ]
dMdm       output mean by input mean                             [ E  x  D ]
dSdm       output covariance by input mean                       [E*E x  D ]
dVdm       inv(s)*input-output covariance by input mean          [D*E x  D ]
dMds       ouput mean by input covariance                        [ E  x D*D]
dSds       output covariance by input covariance                 [E*E x D*D]
dVds       inv(s)*input-output covariance by input covariance    [D*E x D*D]\end{verbatim}
\begin{par}
Copyright (C) 2008-2013 by Marc Deisenroth, Andrew McHutchon, Joe Hall, and Carl Edward Rasmussen.
\end{par} \vspace{1em}
\begin{par}
Last modified: 2013-05-24
\end{par} \vspace{1em}

\subsection*{High-Level Steps}

\begin{enumerate}
\setlength{\itemsep}{-1ex}
   \item If necessary, compute kernel matrix and cache it
   \item Compute predicted mean and inv(s) times input-output covariance
   \item Compute predictive covariance matrix, non-central moments
   \item Centralize moments
   \item Vectorize derivatives
\end{enumerate}

\begin{lstlisting}
function [M, S, V, dMdm, dSdm, dVdm, dMds, dSds, dVds] = gp1d(gpmodel, m, s)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
% If no derivatives are required, call gp1
if nargout < 4; [M S V] = gp1(gpmodel, m, s); return; end
% If there are no inducing inputs, back off to gp0d (no sparse GP required)
if numel(gpmodel.induce) == 0
  [M S V dMdm dSdm dVdm dMds dSds dVds] = gp0d(gpmodel, m, s); return;
end

% 1) If necessary: re-compute cached variables
persistent iK iK2 beta oldX;
ridge = 1e-6;                        % jitter to make matrix better conditioned
[n, D] = size(gpmodel.inputs);    % number of examples and dimension of inputs
E = size(gpmodel.targets,2);         % number of examples and number of outputs
X = gpmodel.hyp; input = gpmodel.inputs; targets = gpmodel.targets;

[np pD pE] = size(gpmodel.induce);     % number of pseudo inputs per dimension
pinput = gpmodel.induce;                                   % all pseudo inputs

if numel(X) ~= numel(oldX) || isempty(iK) || isempty(iK2) || ... % if necessary
    sum(any(X ~= oldX)) || numel(iK2) ~=E*np^2 || numel(iK) ~= n*np*E
  oldX = X;                                        % compute K, inv(K), inv(K2)
  iK = zeros(np,n,E); iK2 = zeros(np,np,E); beta = zeros(np,E);

  for i = 1:E
    pinp = bsxfun(@rdivide,pinput(:,:,min(i,pE)),exp(X(1:D,i)'));
    inp = bsxfun(@rdivide,input,exp(X(1:D,i)'));
    Kmm = exp(2*X(D+1,i)-maha(pinp,pinp)/2) + ridge*eye(np);  % add small ridge
    Kmn = exp(2*X(D+1,i)-maha(pinp,inp)/2);
    L = chol(Kmm)';
    V = L\Kmn;                                             % inv(sqrt(Kmm))*Kmn
    if isfield(gpmodel,'nigp')
      G = exp(2*X(D+1,i))-sum(V.^2)+gpmodel.nigp(:,i)';
    else
      G = exp(2*X(D+1,i))-sum(V.^2);
    end
    G = sqrt(1+G/exp(2*X(D+2,i)));
    V = bsxfun(@rdivide,V,G);
    Am = chol(exp(2*X(D+2,i))*eye(np) + V*V')';
    At = L*Am;                           % chol(sig*B) [Snelson's thesis, p. 40]
    iAt = At\eye(np);
    % The following is not an inverse matrix, but we'll treat it as such: multiply
    % the targets from right and the cross-covariances left to get predictive mean.
    iK(:,:,i) = ((Am\(bsxfun(@rdivide,V,G)))'*iAt)';
    beta(:,i) = iK(:,:,i)*targets(:,i);
    iB = iAt'*iAt.*exp(2*X(D+2,i));          % inv(B), [Snelson's thesis, p. 40]
    iK2(:,:,i) = Kmm\eye(np) - iB; % covariance matrix for predictive variances
  end
end

k = zeros(np,E); M = zeros(E,1); V = zeros(D,E); S = zeros(E);       % allocate
dMds = zeros(E,D,D); dSdm = zeros(E,E,D); r = zeros(1,D);
dSds = zeros(E,E,D,D); dVds = zeros(D,E,D,D); T = zeros(D);
inp = zeros(np,D,E);

% 2) Compute predicted mean and inv(s) times input-output covariance
for i = 1:E
  inp(:,:,i) = bsxfun(@minus,pinput(:,:,min(i,pE)),m');   % centralize p-inputs

  L = diag(exp(-X(1:D,i)));
  in = inp(:,:,i)*L;
  B = L*s*L+eye(D);  LiBL = L/B*L; % iR = LiBL;

  t = in/B;
  l = exp(-sum(in.*t,2)/2); lb = l.*beta(:,i);
  tL = t*L;
  tlb = bsxfun(@times,tL,lb);
  c = exp(2*X(D+1,i))/sqrt(det(B));
  M(i) = c*sum(lb);
  V(:,i) = tL'*lb*c;                   % inv(s) times input-output covariance
  dMds(i,:,:) = c*tL'*tlb/2 - LiBL*M(i)/2;
  for d = 1:D
    dVds(d,i,:,:) = c*bsxfun(@times,tL,tL(:,d))'*tlb/2 - LiBL*V(d,i)/2 ...
      - (V(:,i)*LiBL(d,:) + LiBL(:,d)*V(:,i)')/2;
  end
  k(:,i) = 2*X(D+1,i)-sum(in.*in,2)/2;
end
dMdm = V'; dVdm = 2*permute(dMds,[2 1 3]);                  % derivatives wrt m

iell2 = exp(-2*gpmodel.hyp(1:D,:));
inpiell2 = bsxfun(@times,inp,permute(iell2,[3,1,2])); % N-by-D-by-E

% 3) Compute predictive covariance matrix, non-central moments
for i=1:E
  ii = inpiell2(:,:,i);

  for j=1:i
    R = s*diag(iell2(:,i)+iell2(:,j))+eye(D); t = 1/sqrt(det(R));
    ij = inpiell2(:,:,j);
    L = exp(bsxfun(@plus,k(:,i),k(:,j)')+maha(ii,-ij,R\s/2));

    if i==j
      iKL = iK2(:,:,i).*L; s1iKL = sum(iKL,1); s2iKL = sum(iKL,2);
      S(i,j) = t*(beta(:,i)'*L*beta(:,i) - sum(s1iKL));
      zi = ii/R;
      bibLi = L'*beta(:,i).*beta(:,i); cbLi = L'*bsxfun(@times, beta(:,i), zi);
      r = (bibLi'*zi*2 - (s2iKL' + s1iKL)*zi)*t;
      for d = 1:D
        T(d,1:d) = 2*(zi(:,1:d)'*(zi(:,d).*bibLi) + ...
          cbLi(:,1:d)'*(zi(:,d).*beta(:,i)) - zi(:,1:d)'*(zi(:,d).*s2iKL) ...
          - zi(:,1:d)'*(iKL*zi(:,d)));
        T(1:d,d) = T(d,1:d)';
      end
    else
      zi = ii/R; zj = ij/R;
      S(i,j) = beta(:,i)'*L*beta(:,j)*t; S(j,i) = S(i,j);

      bibLj = L*beta(:,j).*beta(:,i); bjbLi = L'*beta(:,i).*beta(:,j);
      cbLi = L'*bsxfun(@times, beta(:,i), zi);
      cbLj = L*bsxfun(@times, beta(:,j), zj);

      r = (bibLj'*zi+bjbLi'*zj)*t;
      for d = 1:D
        T(d,1:d) = zi(:,1:d)'*(zi(:,d).*bibLj) + ...
          cbLi(:,1:d)'*(zj(:,d).*beta(:,j)) + zj(:,1:d)'*(zj(:,d).*bjbLi) + ...
          cbLj(:,1:d)'*(zi(:,d).*beta(:,i));
        T(1:d,d) = T(d,1:d)';
      end
    end

    dSdm(i,j,:) = r - M(i)*dMdm(j,:)-M(j)*dMdm(i,:); dSdm(j,i,:) = dSdm(i,j,:);
    T = (t*T-S(i,j)*diag(iell2(:,i)+iell2(:,j))/R)/2;
    T = T - squeeze(M(i)*dMds(j,:,:) + M(j)*dMds(i,:,:));
    dSds(i,j,:,:) = T; dSds(j,i,:,:) = T;
  end

  S(i,i) = S(i,i) + exp(2*X(D+1,i));
end

% 4) Centralize moments
S = S - M*M';

% 5) Vectorize derivatives
dMds = reshape(dMds,[E D*D]);
dSds = reshape(dSds,[E*E D*D]); dSdm = reshape(dSdm,[E*E D]);
dVds = reshape(dVds,[D*E D*D]); dVdm = reshape(dVdm,[D*E D]);
\end{lstlisting}


% Created 2019-08-29 jue 13:06
\documentclass[letterpaper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage{khpreamble}
\usepackage{subfigure}
\usepgfplotslibrary{groupplots}
\author{Kjartan Halvorsen}
\date{\today}
\title{Poles and geometric sequences exercise}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs 25.3.50.2 (Org mode 8.2.10)}}
\begin{document}

\maketitle

\section*{Plot the sequence \(f(k) = a^k\) for \(k=1,2,3,\ldots\)}
\label{sec-1}

\begin{center}
\includegraphics[width=0.38\linewidth]{../../figures/imaginary-plane-seq-1}
\includegraphics[width=0.6\linewidth]{../../figures/timeplot-discrete-empty}\\
\includegraphics[width=0.38\linewidth]{../../figures/imaginary-plane-seq-2}
\includegraphics[width=0.6\linewidth]{../../figures/timeplot-discrete-empty}\\
\includegraphics[width=0.38\linewidth]{../../figures/imaginary-plane-seq-3}
\includegraphics[width=0.6\linewidth]{../../figures/timeplot-discrete-empty}\\
\end{center}

\section*{Pulse response}
\label{sec-2}
\begin{center}
\begin{minipage}[t]{0.5\linewidth}
\centering
\begin{tikzpicture}[node distance=20mm, anchor=north]
\node[coordinate] (input) {};
\node[rectangle, draw, right of=input, inner sep=3mm] (lti) {H};
\node[coordinate, right of=lti] (output) {};
\draw[->] (input) -- node[near start, above] {$u(k)$}  (lti);
\draw[->] (lti) -- node[near end, above] {$y(k)$} (output);
\end{tikzpicture}
\end{minipage}
\includegraphics[0.5\linewidth]{../../figures/timeplot-discrete-impulse-response}
\end{center}

\textbf{Plot the response of the system to the input signal below.}

\includegraphics[0.6\linewidth]{../../figures/timeplot-discrete-simplesignal}

\vspace*{2mm}

\includegraphics[0.6\linewidth]{../../figures/timeplot-discrete-empty-y}

\newpage

\section*{From continuous-time poles to discrete-time poles}
\label{sec-3}
\begin{center}
\begin{tabular}{cc}
\begin{minipage}[c]{0.48\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{../../figures/imaginary-plane-vertical-line}\\
\end{minipage}
&
\begin{minipage}[c]{0.48\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{../../figures/imaginary-plane-empty}\\
\end{minipage} \\

\begin{minipage}[b]{0.48\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{../../figures/imaginary-plane-diagonal-lines}\\
\end{minipage}
&
\begin{minipage}[b]{0.48\linewidth}
\centering
 \includegraphics[width=0.8\linewidth]{../../figures/imaginary-plane-empty}\\
\end{minipage}\\

\begin{minipage}[b]{0.48\linewidth}
\centering
\includegraphics[width=0.8\linewidth]{../../figures/imaginary-plane-horizontal-lines}\\
\end{minipage}
&
\begin{minipage}[b]{0.48\linewidth}
\centering
 \includegraphics[width=0.8\linewidth]{../../figures/imaginary-plane-empty}\\
\end{minipage}

\end{tabular}
\end{center}
% Emacs 25.3.50.2 (Org mode 8.2.10)
\end{document}


\section{Huffman-Codierung}

\begin{frame}{Huffman-Codierung}
\begin{itemize}
\item Übersetzungsfunktion h(x) gesucht
\item $\epsilon$-freier und Präfixfreier Homomorphismus
\item $h(x):A\to \{0,1\}^*$
\item h(w) soll dabei möglichst kurz sein.
\item Zeichen, die häufiger vorkommen, bekommen einen kürzeren Code.
\end{itemize}
\end{frame}

\begin{frame}
\begin{center}
w=aafbcdfbfbeefbcfbbfeb
    \qquad
  \begin{tabular}{ccccccc}
    \toprule
    x & a & b& c& d& e& f \\
    $N_x(w)$ &2 &7 &2 & 1& 3& 6\\
    \bottomrule
  \end{tabular}
\end{center}
\pause
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=20mm}]
    \node {}%$22$}
    child{node{}
    edge from parent[draw=none]
    child { node {}
    edge from parent[draw=none]
      child {node {}
      edge from parent[draw=none]
        child {node {1,d}
        edge from parent[draw=none]
        }
        child {node {2,c}
        edge from parent[draw=none]
        }
      }
      child {node {2,a}
      edge from parent[draw=none]
      }
    }
    child{node{e,3}edge from parent[draw=none]}
    }
    child{node{}
    edge from parent[draw=none]
    child{node{f,6}edge from parent[draw=none]}
    child{node{b,7}edge from parent[draw=none]}
    };
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}
\begin{center}
w=aafbcdfbfbeefbcfbbfeb
    \qquad
  \begin{tabular}{ccccccc}
    \toprule
    x & a & b& c& d& e& f \\
    $N_x(w)$ &2 &7 &2 & 1& 3& 6\\
    \bottomrule
  \end{tabular}
\end{center}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=20mm}]
    \node {}%$22$}
    child{node{}
    edge from parent[draw=none]
    child { node {}
    edge from parent[draw=none]
      child {node {3}
      edge from parent[draw=none]
        child {node {1,d}
             }
        child {node {2,c}
        }
      }
      child {node {2,a}
      edge from parent[draw=none]
      }
    }
    child{node{e,3}edge from parent[draw=none]}
    }
    child{node{}
    edge from parent[draw=none]
    child{node{f,6}edge from parent[draw=none]}
    child{node{b,7}edge from parent[draw=none]}
    };
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}
\begin{center}
w=aafbcdfbfbeefbcfbbfeb
    \qquad
  \begin{tabular}{ccccccc}
    \toprule
    x & a & b& c& d& e& f \\
    $N_x(w)$ &2 &7 &2 & 1& 3& 6\\
    \bottomrule
  \end{tabular}
\end{center}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=20mm}]
    \node {}%$22$}
    child{node{}
    edge from parent[draw=none]
    child { node {5}
    edge from parent[draw=none]
      child {node {3}
        child {node {1,d}
             }
        child {node {2,c}
        }
      }
      child {node {2,a}
      }
    }
    child{node{e,3}edge from parent[draw=none]}
    }
    child{node{}
    edge from parent[draw=none]
    child{node{f,6}edge from parent[draw=none]}
    child{node{b,7}edge from parent[draw=none]}
    };
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}
\begin{center}
w=aafbcdfbfbeefbcfbbfeb
    \qquad
  \begin{tabular}{ccccccc}
    \toprule
    x & a & b& c& d& e& f \\
    $N_x(w)$ &2 &7 &2 & 1& 3& 6\\
    \bottomrule
  \end{tabular}
\end{center}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=20mm}]
    \node {}%$22$}
    child{node{8}
    edge from parent[draw=none]
    child { node {5}
      child {node {3}
        child {node {1,d}
             }
        child {node {2,c}
        }
      }
      child {node {2,a}
      }
    }
    child{node{e,3}}
    }
    child{node{}
    edge from parent[draw=none]
    child{node{f,6}edge from parent[draw=none]}
    child{node{b,7}edge from parent[draw=none]}
    };
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}
\begin{center}
w=aafbcdfbfbeefbcfbbfeb
    \qquad
  \begin{tabular}{ccccccc}
    \toprule
    x & a & b& c& d& e& f \\
    $N_x(w)$ &2 &7 &2 & 1& 3& 6\\
    \bottomrule
  \end{tabular}
\end{center}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=20mm}]
    \node {}%$22$}
    child{node{8}
    edge from parent[draw=none]
    child { node {5}
      child {node {3}
        child {node {1,d}
             }
        child {node {2,c}
        }
      }
      child {node {2,a}
      }
    }
    child{node{e,3}}
    }
    child{node{13}
    edge from parent[draw=none]
    child{node{f,6}}
    child{node{b,7}}
    };
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}
\begin{center}
w=aafbcdfbfbeefbcfbbfeb
    \qquad
  \begin{tabular}{ccccccc}
    \toprule
    x & a & b& c& d& e& f \\
    $N_x(w)$ &2 &7 &2 & 1& 3& 6\\
    \bottomrule
  \end{tabular}
\end{center}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=20mm}]
    \node {21}%$22$}
    child{node{8}
    child { node {5}
      child {node {3}
        child {node {1,d}
             }
        child {node {2,c}
        }
      }
      child {node {2,a}
      }
    }
    child{node{e,3}}
    }
    child{node{13}
    child{node{f,6}}
    child{node{b,7}}
    };
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}
\begin{center}
w=aafbcdfbfbeefbcfbbfeb
    \qquad
  \begin{tabular}{ccccccc}
    \toprule
    x & a & b& c& d& e& f \\
    $N_x(w)$ &2 &7 &2 & 1& 3& 6\\
    \bottomrule
  \end{tabular}
\end{center}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=20mm}]
    \node {21}%$22$}
    child{node{8}
    child { node {5}
      child {node {3}
        child {node {1,d}
        edge from parent node[left] {0}
             }
        child {node {2,c} edge from parent node[right] {1}
        }
        edge from parent node[left] {0}
      }
      child {node {2,a} edge from parent node[right] {1}
      }
      edge from parent node[left] {0}
    }
    child{node{e,3}edge from parent node[right] {1}}
    edge from parent node[left] {0}
    }
    child{node{13}
    child{node{f,6}edge from parent node[left] {0}}
    child{node{b,7}edge from parent node[right] {1}}
    edge from parent node[right] {1}
    };
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=20mm}]
    \node {21}%$22$}
    child{node{8}
    child { node {5}
      child {node {3}
        child {node {1,d}
        edge from parent node[left] {0}
             }
        child {node {2,c} edge from parent node[right] {1}
        }
        edge from parent node[left] {0}
      }
      child {node {2,a} edge from parent node[right] {1}
      }
      edge from parent node[left] {0}
    }
    child{node{e,3}edge from parent node[right] {1}}
    edge from parent node[left] {0}
    }
    child{node{13}
    child{node{f,6}edge from parent node[left] {0}}
    child{node{b,7}edge from parent node[right] {1}}
    edge from parent node[right] {1}
    };
  \end{tikzpicture}
  \end{center}
   \begin{tabular}{ccccccc}
    \toprule
    x & a & b& c& d& e& f \\
    $h(x)$ &001 &11 &0001 & 0000& 01& 10\\
    \bottomrule
  \end{tabular}
\end{frame}

\begin{frame}{Huffman-Codierung}
\begin{itemize}
\item w=aafbcdfbfbeefbcfbbfeb
\item h(w)=00100110110001000010111011010110110001101111100111
\item Die Codierung von w ist 50 Zeichen lang.
\end{itemize}
\end{frame}

\begin{frame}{Klausuraufgabe: SS 2011 A4}
Gegeben sei das Alphabet $A=\{a,b,c,d,e,f,g\}$ und ein Wort $w\in A^*$ in dem die Symbole mit folgenden Häufigkeiten vorkommen:
\begin{center}
\begin{tabular}{ccccccc}
    \toprule
    a & b& c& d& e& f &g\\
    \midrule
    11 &3 &11 & 24& 8& 7&36\\
    \bottomrule
\end{tabular}
\end{center}
\begin{itemize}
\item Zeichnen Sie den Huffman-Baum.
\item Geben Sie die Huffman-Codierung des Wortes $bad$ an.
\end{itemize}
\end{frame}

\begin{frame}{Klausuraufgabe: SS 2011 A4}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=45mm},
    level 2/.style={sibling distance=30mm},
    level 3/.style={sibling distance=15mm}]
    \node {}
    child{node{}
    child { node {}
      child {node {}
        child {node {3,b}
        edge from parent[draw=none]
             }
        child {node {7,f} edge from parent[draw=none]
        }
        edge from parent[draw=none]
      }
      child {node {8,e} edge from parent[draw=none]
      }
      edge from parent[draw=none]
    }
    child{node{}
    	child{node {11,a}edge from parent[draw=none]}
	child{node {11,c}edge from parent[draw=none]}
    edge from parent[draw=none]}
    edge from parent[draw=none]
    }
    child{node{}
    child{node{24,d}edge from parent[draw=none]}
    child{node{36,g}edge from parent[draw=none]}
    edge from parent[draw=none]
    };
  \end{tikzpicture}
  \end{center}
\end{frame}

\begin{frame}{Klausuraufgabe: SS 2011 A4}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=45mm},
    level 2/.style={sibling distance=30mm},
    level 3/.style={sibling distance=15mm}]
    \node {}
    child{node{}
    child { node {}
      child {node {10}
        child {node {3,b}
        edge from parent node[left] {0}
             }
        child {node {7,f} edge from parent node[right] {1}
        }
        edge from parent[draw=none]
      }
      child {node {8,e} edge from parent[draw=none]
      }
      edge from parent[draw=none]
    }
    child{node{}
    	child{node {11,a}edge from parent[draw=none]}
	child{node {11,c}edge from parent[draw=none]}
    edge from parent[draw=none]}
    edge from parent[draw=none]
    }
    child{node{}
    child{node{24,d}edge from parent[draw=none]}
    child{node{36,g}edge from parent[draw=none]}
    edge from parent[draw=none]
    };
  \end{tikzpicture}
  \end{center}
\end{frame}

\begin{frame}{Klausuraufgabe: SS 2011 A4}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=45mm},
    level 2/.style={sibling distance=30mm},
    level 3/.style={sibling distance=15mm}]
    \node {}
    child{node{}
    child { node {18}
      child {node {10}
        child {node {3,b}
        edge from parent node[left] {0}
             }
        child {node {7,f} edge from parent node[right] {1}
        }
        edge from parent node[left] {0}
      }
      child {node {8,e} edge from parent node[right] {1}
      }
      edge from parent[draw=none]
    }
    child{node{}
    	child{node {11,a}edge from parent[draw=none]}
	child{node {11,c}edge from parent[draw=none]}
    edge from parent[draw=none]}
    edge from parent[draw=none]
    }
    child{node{}
    child{node{24,d}edge from parent[draw=none]}
    child{node{36,g}edge from parent[draw=none]}
    edge from parent[draw=none]
    };
  \end{tikzpicture}
  \end{center}
\end{frame}

\begin{frame}{Klausuraufgabe: SS 2011 A4}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=45mm},
    level 2/.style={sibling distance=30mm},
    level 3/.style={sibling distance=15mm}]
    \node {}
    child{node{}
    child { node {18}
      child {node {10}
        child {node {3,b}
        edge from parent node[left] {0}
             }
        child {node {7,f} edge from parent node[right] {1}
        }
        edge from parent node[left] {0}
      }
      child {node {8,e} edge from parent node[right] {1}
      }
      edge from parent[draw=none]
    }
    child{node{22}
    	child{node {11,a}edge from parent node[left] {0}}
	child{node {11,c}edge from parent node[right] {1}}
    edge from parent[draw=none]}
    edge from parent[draw=none]
    }
    child{node{}
    child{node{24,d}edge from parent[draw=none]}
    child{node{36,g}edge from parent[draw=none]}
    edge from parent[draw=none]
    };
  \end{tikzpicture}
  \end{center}
\end{frame}

\begin{frame}{Klausuraufgabe: SS 2011 A4}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=45mm},
    level 2/.style={sibling distance=30mm},
    level 3/.style={sibling distance=15mm}]
    \node {}
    child{node{40}
    child { node {18}
      child {node {10}
        child {node {3,b}
        edge from parent node[left] {0}
             }
        child {node {7,f} edge from parent node[right] {1}
        }
        edge from parent node[left] {0}
      }
      child {node {8,e} edge from parent node[right] {1}
      }
      edge from parent node[left] {0}
    }
    child{node{22}
    	child{node {11,a}edge from parent node[left] {0}}
	child{node {11,c}edge from parent node[right] {1}}
    edge from parent node[right] {1}}
    edge from parent[draw=none]
    }
    child{node{}
    child{node{24,d}edge from parent[draw=none]}
    child{node{36,g}edge from parent[draw=none]}
    edge from parent[draw=none]
    };
  \end{tikzpicture}
  \end{center}
\end{frame}

\begin{frame}{Klausuraufgabe: SS 2011 A4}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=45mm},
    level 2/.style={sibling distance=30mm},
    level 3/.style={sibling distance=15mm}]
    \node {}
    child{node{40}
    child { node {18}
      child {node {10}
        child {node {3,b}
        edge from parent node[left] {0}
             }
        child {node {7,f} edge from parent node[right] {1}
        }
        edge from parent node[left] {0}
      }
      child {node {8,e} edge from parent node[right] {1}
      }
      edge from parent node[left] {0}
    }
    child{node{22}
    	child{node {11,a}edge from parent node[left] {0}}
	child{node {11,c}edge from parent node[right] {1}}
    edge from parent node[right] {1}}
    edge from parent[draw=none]
    }
    child{node{60}
    child{node{24,d}edge from parent node[left] {0}}
    child{node{36,g}edge from parent node[right] {1}}
    edge from parent[draw=none]
    };
  \end{tikzpicture}
  \end{center}
\end{frame}

\begin{frame}{Klausuraufgabe: SS 2011 A4}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=45mm},
    level 2/.style={sibling distance=30mm},
    level 3/.style={sibling distance=15mm},yscale=0.9]
    \node {100}
    child{node{40}
    child { node {18}
      child {node {10}
        child {node {3,b}
        edge from parent node[left] {0}
             }
        child {node {7,f} edge from parent node[right] {1}
        }
        edge from parent node[left] {0}
      }
      child {node {8,e} edge from parent node[right] {1}
      }
      edge from parent node[left] {0}
    }
    child{node{22}
    	child{node {11,a}edge from parent node[left] {0}}
	child{node {11,c}edge from parent node[right] {1}}
    edge from parent node[right] {1}}
    edge from parent node[left] {0}
    }
    child{node{60}
    child{node{24,d}edge from parent node[left] {0}}
    child{node{36,g}edge from parent node[right] {1}}
    edge from parent node[right] {1}
    };
  \end{tikzpicture}
  \end{center}
  $h(bad)=0000 \text{ } 010\text{ } 10$
\end{frame}

\begin{frame}{Klausuraufgabe: SS 2011 A4}
Gegeben seien zwei Codierungen über dem Alphabet $A=\{a,b,c,d,e\}$
\begin{center}
\begin{tabular}{cccccc}
    \toprule
    x & a & b& c& d& e \\
    $h(x)$ &00 &10 &11 & 010& 011\\
    \bottomrule
  \end{tabular}
  \end{center}
  \begin{center}
  \begin{tabular}{cccccc}
    \toprule
    x & a & b& c& d& e \\
    $h(x)$ &10 &11 &001 & 010& 011\\
    \bottomrule
  \end{tabular}
  \end{center}
  Welche der beiden Codierungen ist eine gültige Huffman-Codierung?
\end{frame}

\begin{frame}
\begin{center}
  \begin{tabular}{cccccc}
    \toprule
    x & a & b& c& d& e \\
    $h(x)$ &00 &10 &11 & 010& 011\\
    \bottomrule
  \end{tabular}
\end{center}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=20mm}]
    \node {}%$22$}
    child{node{}
    child { node {a}
      edge from parent node[left] {0}
    }
    child{node{}
    child{ node {d}edge from parent node[left] {0}}
    child{ node {e}edge from parent node[right] {1}}
    edge from parent node[right] {1}}
    edge from parent node[left] {0}
    }
    child{node{}
    child{node{b}edge from parent node[left] {0}}
    child{node{c}edge from parent node[right] {1}}
    edge from parent node[right] {1}
    };
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}
\begin{center}
  \begin{tabular}{cccccc}
    \toprule
    x & a & b& c& d& e \\
    $h(x)$ &10 &11 &001 & 010& 011\\
    \bottomrule
  \end{tabular}
\end{center}
\begin{center}
  \begin{tikzpicture}
    [level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=20mm},
    level 3/.style={sibling distance=10mm}]
    \node {}%$22$}
    child{node{}
    child { node {}
    child{node{}edge from parent[draw=none]}
    child{node{c}edge from parent node[right] {1}}
      edge from parent node[left] {0}
    }
    child{node{}
    child{ node {d}edge from parent node[left] {0}}
    child{ node {e}edge from parent node[right] {1}}
    edge from parent node[right] {1}}
    edge from parent node[left] {0}
    }
    child{node{}
    child{node{a}edge from parent node[left] {0}}
    child{node{b}edge from parent node[right] {1}}
    edge from parent node[right] {1}
    };
  \end{tikzpicture}
\end{center}
\end{frame}


\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\graphicspath{ {./plots/} }

\title{Test Berekeley CS 294-112: Deep Reinforcement Learning Homework 2}
\author{John Dang}
\date{}

\begin{document}
    \maketitle

    \section{State-Dependent Baseline is Unbiased}

    \subsection{Part A}
    We wish to show that subtracting a state-dependent baseline $b(s_t)$
    results in the same policy gradient in expectation:

    \begin{equation*}
        E_{\tau\sim p_\theta(\tau)} \left[ \sum_{t=1}^T{\nabla_\theta \log \pi_\theta(a_t|s_t)} \left( \left( \sum_{t'=1}^T{r(s_{t'},a_{t'})} \right) - b(s_t)\right)\right]
    \end{equation*}

    \begin{equation*}
        = E_{\tau\sim p_\theta(\tau)} \left[ \sum_{t=1}^T{\nabla_\theta \log \pi_\theta(a_t|s_t)} \left( \sum_{t'=1}^T{r(s_{t'},a_{t'})}\right)\right]
    \end{equation*}

   By linearity of expectation, this equality is true only if

    \begin{equation*}
       \sum_{t=1}^{T}{ E_{(s_t,a_t)\sim p_\theta(s_t,a_t)} \left[ \nabla_\theta \log \pi_\theta(a_t|s_t) b(s_t)\right]} = 0
    \end{equation*}

    We can equivalently show this by showing that the  expectation term is zero for all timesteps using the law of iterated expectations

    \begin{equation*}
        E_{(s_t,a_t)\sim p_\theta(s_t,a_t)} \left[ \nabla_\theta \log \pi_\theta(a_t|s_t) b(s_t)\right]
    \end{equation*}

    \begin{equation*}
        E_{s_t\sim p_(s_t)} \left[ E_{a_t\sim \pi_\theta(a_t|s_t)}\left[\nabla_\theta \log \pi_\theta(a_t|s_t) b(s_t)\right | s_t]\right]
    \end{equation*}

    \begin{equation*}
        \int_{s_t} p(s_t) b(s_t) \int_{a_t} \pi_\theta (a_t|s_t)\nabla_\theta \log \pi_\theta(a_t|s_t)da_t ds_t
    \end{equation*}

    \begin{equation*}
        \int_{s_t} p(s_t) b(s_t) \int_{a_t} \pi_\theta (a_t|s_t) \frac{\nabla_\theta \pi_\theta(a_t|s_t)}{\pi_\theta(a_t|s_t)} da_t ds_t
    \end{equation*}

    \begin{equation*}
        \int_{s_t} p(s_t) b(s_t) \nabla_\theta \int_{a_t} \pi_\theta (a_t|s_t) da_t ds_t
    \end{equation*}

    \begin{equation*}
        \int_{s_t} p(s_t) b(s_t) \nabla_\theta 1 ds_t = 0
    \end{equation*}

    Thus, subtracting a state-dependent baseline from the reward results in the same policy gradient
    in expectations and is unbiased.

    \subsection{Part B}

    \subsubsection{Part a}
    The reinforcement learning problem can be formulated as a markov chain
    of state-action tuples $(s_t,a_t)$, which satisfy the Markov Property:

    \begin{equation*}
        p(s_{t+1}, a_{t+1}| s_{t}, a_{t}) = p(s_{t+1}, a_{t+1} | s_{t}, a_{t}, \ldots , s_0, a_0 )
    \end{equation*}

    Thus, the trajectory after timestep $t$ is dependent only on $(s_t, a_t)$
    (independent of what happened before timestep $t$).

    \subsubsection{Part b}

    The trajectory distribution $p_\theta(\tau)$ can be expressed in terms of the
    trajectory before and after time step $t$.

    \begin{equation*}
        p_\theta(\tau) = p_\theta(s_{1:t}, a_{1:t-1})p_\theta(s_{t+1:T}, a_{t:T} | s_{1:t}, a_{1:t-1})
    \end{equation*}

    We can show

    \begin{equation*}
       E_{\tau\sim p_\theta(\tau)} \left[\nabla_\theta \log \pi_\theta(a_t|s_t) b(s_t)\right] = 0
     \end{equation*}

     by breaking up the expectation into an expectation under the trajectory before and after time step $t$. Let
     $\tau_{t:t'} = (s_t,a_t, ..., s_{t'},a_{t'})$ be the trajectory between time steps $t$ and $t'$.

    \begin{equation*}
        E_{\tau_{1:t}\sim p_\theta(\tau_{1:t})} \left[ E_{\tau_{t:T}\sim p_\theta(\tau_{t:T} | \tau_{1:t})} \left[\nabla_\theta \log \pi_\theta(a_t|s_t) b(s_t)\right | \tau_{1:t}]\right]
    \end{equation*}

    The inner expectation evaluates to

    \begin{equation*}
        \hspace*{-4cm}
        \int_{s_t} b(s_t) p(s_t | s_{t-1}, a_{t-1}) \int_{a_t} \pi_\theta(a_t|s_t) \nabla_\theta \log \pi_\theta(a_t|s_t) \int_{s_{t+1}} p(s_{t+1} | s_t, a_t) \int_{a_{t+1}}\pi_\theta(a_{t+1}|s_{t+1}) \ldots \int_{s_T} p(s_T|s_{t-1},a_{t-1})  ds_T ds_{t+1} da_{t+1} da_t ds_t
    \end{equation*}

    \begin{equation*}
        \int_{s_t} b(s_t) p(s_t | s_{t-1}, a_{t-1}) \int_{a_t} \pi_\theta (a_t|s_t) \frac{\nabla_\theta \pi_\theta(a_t|s_t)}{\pi_\theta(a_t|s_t)}  \cdot 1 da_t ds_t
    \end{equation*}

    \begin{equation*}
        \int_{s_t} b(s_t) p(s_t | s_{t-1}, a_{t-1}) \nabla_\theta 1 ds_t = 0
    \end{equation*}

    Thus, have the policy gradient element at time step $t$ is

    \begin{equation*}
        E_{\tau_{1:t}\sim p_\theta(\tau_{1:t})} \left[ 0\right] = 0
    \end{equation*}

    Again, we have shown that subtracting a state-dependent baseline is unbiased
    in expectation.

    \section{Experiments}

    \subsection{Vanilla Policy Gradient}

    \subsubsection{Small Batch Performance (CartPole-v0)}
    \begin{center}
        \noindent\includegraphics[scale=0.29]{smallBatchCartpole}
    \end{center}

    \subsubsection{Large Batch Performance (CartPole-v0)}
    \begin{center}
        \noindent\includegraphics[scale=0.29]{largeBatchCartpole}
    \end{center}

    \subsubsection{Discussion}
    Using reward to go inmproved performance and reduced variance for both
    small and large batch experiments. Advantage-centering does improve performance.
    Using a larger batch size reduces varaince and speeds up learning for the
    first few iterations. Both batch sizes were able to converge to near optimal
    performance, with the exception of the experiment with no variance reduction
    techniques. In this case, the larger batch size improved performance and reduced
    variance significantly.

    \subsection{Hyperparameter Tuning (InvertedPendulum-v2)}
    Using a batch size of 400 and learning rate of 0.02, resulted in reaching
    optimal performance, while minimizing batch size and maximizing learning rate.
    These hyperparameter values were found by grid search over
    batch sizes $[300, 400, 500]$ and learning rates $[0.03, 0.02, 0.01]$.

    \begin{center}
        \noindent\includegraphics[scale=0.3]{hyperParamsInvertedPendulum}
    \end{center}

    \subsection{Policy Gradient with Baselines}

    \subsubsection{Performance (LunarLander-v2)}
    As expected, using policy gradients with a neural network baseline was
    able to achieve desired performance. During each policy gradient outer loop,
    the value network was trained for 100 epochs.
    \begin{center}
        \noindent\includegraphics[scale=0.3]{lunarLander}
    \end{center}

    \subsubsection{Hyperparameter Tuning (HalfCheetah-v2)}
    In this experiment, using a higher learning rate results in better performance.
    Increasing batch size also generally improved performance or at least
    maintained performance. Based on this experiment
    we run the remaining experiments with batch size 50000 and learning rate 0.02.

    \begin{center}
        \noindent\includegraphics[scale=0.3]{hyperParamsCheetah}
    \end{center}

    \subsubsection{ Performance (HalfCheetah-v2)}
    As shown before using reward to go achieves better performance and lower variance. Using a baseline with
    reward to go also achieves better performance and significantlt reduces varaince. Using a baseline alone
    without reward to go performs poorly.
    \begin{center}
        \noindent\includegraphics[scale=0.3]{cheetah}
    \end{center}

    \section{Scripts Used}

    To reproduce these results run the following commands from the hw2 directory.

    \begin{verbatim}
        ./p4.sh
        ./p5.sh
        ./p7.sh
        ./p8HyperParamSearch.sh
        ./p8.sh
    \end{verbatim}
\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS630: Database Management Systems
% Copyright 2014 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 6}

Show the grant diagrams after steps 4 and 5 of the sequence of actions in Table \ref{table51}, where $A$ owns the relation on which the privilege $p$ is assigned.
Can $C$ still exercise privilege $p$ after step 5? What about $E$?

\begin{table}[H]
\centering
\begin{tabular}{|c|c|l|}
\hline
Step & Executed by & Action\\
\hline \hline
1 & A & GRANT \textit{p} TO \textit{B} WITH GRANT OPTION\\
2 & A & GRANT \textit{p} TO \textit{C} \\
3 & B & GRANT \textit{p} TO \textit{D} WITH GRANT OPTION\\
4 & D & GRANT \textit{p} TO \textit{E} \\
5 & B & REVOKE \textit{p} FROM \textit{D} CASCADE\\
\hline
\end{tabular}
\caption{Sequence of System Level Privilege Grants}\label{table51}
\end{table}

\subsection*{Solution}

Figure \ref{fig1} provides grant diagram after step 4.
As is shown, after step 4, $D$ is granted (only by $B$) both exercise and delegation of $\textit{p}$ to $E$.

\begin{figure}[H]\centering
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node
distance=4cm,semithick]
  \tikzstyle{system}=[circle,thick,draw=black,fill=gray!40,text=black]
  \node[state,system] (0) {Sys};
  \node[state] (1) [above of = 0] {A};
  \node[state] (2) [right of = 1] {B};
  \node[state] (3) [below of = 2] {C};
  \node[state] (4) [right of = 2] {D};
  \node[state] (5) [right of = 3] {E};
  \path
  (0) edge [bend left=0] node {AP, Y} (1)
  (1) edge [bend left=0] node {\textit{p}, Y} (2)
      edge [bend left=0] node {\textit{p}, N} (3)
  (2) edge [bend left=0] node {\textit{p}, Y} (4)
  (4) edge [bend left=0] node {\textit{p}, N} (5);
\end{tikzpicture}
\caption{Grant Diagram after step 4}\label{fig1}
\end{figure}

Figure \ref{fig52} provides grant diagram after step 5.
After step 5, when $B$ revokes $\textit{p}$ from $D$ in cascade mode, $D$ is no longer authorized delegation of $p$.
Therefore, grant $\textit{p}$ by $D$ to $E$ is also revoked.
It is perfectly possible for $C$ to exercise $\textit{p}$, however, $E$ would no longer be able to exercise $\textit{p}$.

\begin{figure}[H]\centering
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node
distance=4cm,semithick]
  \tikzstyle{system}=[circle,thick,draw=black,fill=gray!40,text=black]
  \node[state,system] (0) {Sys};
  \node[state] (1) [above of = 0] {A};
  \node[state] (2) [right of = 1] {B};
  \node[state] (3) [below of = 2] {C};
  \node[state] (4) [right of = 2] {D};
  \node[state] (5) [right of = 3] {E};
  \path
  (0) edge [bend left=0] node {AP, Y} (1)
  (1) edge [bend left=0] node {\textit{p}, Y} (2)
      edge [bend left=0] node {\textit{p}, N} (3);
\end{tikzpicture}
\caption{Grant Diagram after step 5}\label{fig52}
\end{figure}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                 %
%  GEANT manual in LaTeX form                                     %
%                                                                 %
%  Version 1.00                                                   %
%                                                                 %
%  Last Mod. 12 June 1993 1800  MG                                %
%                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Origin{L.Urb\'{a}n}
\Version{Geant 3.10}\Routid{PHYS451}
\Submitted{26.10.84}   \Revised{16.12.93}
\Makehead{Simulation of  e+e- pair  production by  muons}
\section{Subroutines}
\Shubr{GPAIRM}{}
\Rind{GPAIRM} generates the \Pep\Pem-pair radiated by a high energetic muon.
It uses the following input and output:
\begin{DLtt}{MMMMMMM}
\item[input:]  via common block \FCind{/GCTRAK/}
\item[output:] via common block \FCind{/GCKING/}
\end{DLtt}
\Rind{GPAIRM} is called automatically from the tracking routine
\Rind{GTMUON} if, and when,
the parent muon reaches its radiation point during the tracking.
\section{Method }
The double differential cross-section for the
process can be written \cite{bib-LOHM}:
 \begin{eqnarray}
\frac {d^2 \sigma}{d\nu d \rho}&=& \alpha^4\frac
     {2} {3 \pi} (Z \lambda)^2 \cdot  \frac {1-\nu}{\nu}\left [\phi_{\rm e} +
     (m/M)^2
     \phi_{\mu} \right ]
\end{eqnarray}
All the quantities in the expression above are defined in {\tt [PHYS450]}.
 By computing this cross-section for different
($\nu,\rho$) points, it can be seen that:
\begin{enumerate}
\item
the {\bf shape} of the functions
$\frac{\Tstm d^2 \sigma}{\Tstm d\nu d\rho}$
and
$\frac{\Tstm d \sigma}{\Tstm d\nu}\int d \rho \frac {\Tstm d^2 \sigma}
{\Tstm d\nu d\rho}$
practically does not depend on $Z$
\item
the dominant contribution comes from {\bf the low $\nu$ region:}
\begin{equation}
 \nu_{\rm min} = (4m/E)  \leq \nu  \leq  100*\nu_{\rm min}
\end{equation}
\item
in this low region ($d^2\sigma/d\nu d\rho$)
{\bf is flat} as a function of $\rho$
\end{enumerate}
Therefore, we propose the following sampling method as a rough approximation:
\begin{enumerate}
\item
In the low $\nu$ region the differential cross-section
\[
\frac{d \sigma}{d\nu}=\int d \rho \frac{d^2 \sigma K} {d\nu d\sigma}
\]
can be approximated as:
\begin{equation}
\frac{d \sigma}{d\nu} \approx  \left[1-\frac{\nu_{\rm min}}{\nu}\right]^{1/2}
      \frac{1}{v^a} \mbox{\qquad with:\quad}
a = 2-\frac{\ln E}{10} \mbox{\qquad($E$ in GeV)}
\end{equation}
We can write:
\begin{equation}
 \frac{d \sigma}{d\nu}\approx f(\nu) g(\nu)
\end{equation}
where,
\begin{equation}
f(\nu) = \frac{(a-1)}{\frac{\Tstm 1}{\Tstm \nu_c^{a- 1}}  -
        \left(\frac{\Tstm 1}{\Tstm \nu_{\rm max}}\right)^{a-1}}
        \frac{1}{\nu^a}
\end{equation}
is the normalised distribution in the interval $[\nu_c,\nu_{\rm max}]$ and
\begin{equation}
g(\nu) = \left[1-\frac{\nu_{\rm min}}{\nu}\right]^{1/2}
\end{equation}
is the rejection function.
\item
$r_1$ and $r_2$ being two uniformly distributed random numbers in the
interval $[0,1]$:
\begin{eqnarray}
& - & \mbox{Sample $\nu$ from the distribution $f(\nu)$ as:}\nonumber      \\
&   & \nu = \left(\frac{1-r_1}{\nu_c^{a-1}} \frac{r_1}{\nu_{\rm max}^{a-1}}
      \right)^\frac{\Sstm 1}{\Sstm 1-a}                                    \\
& - & \mbox{Accept $\nu$ if $r_2 \leq g(\nu)$}
\end{eqnarray}
\item Then compute
\begin{equation}
\rho_{\rm max}(\nu) = \left[1 -\frac{6M^2}
      {E^2(1-\nu)}\right]\left[1-\frac{4m}{\nu E}\right]^{1/2} \\
\end{equation}
and generate $\rho $ uniformly in the range $[-\rho_{\rm max},
+\rho_{\rm max}]$.
\end{enumerate}
After the successful sampling of ($ \nu,\rho $), \Rind{GPAIRM} generates the
polar
angles of the radiated \Pep\Pem-pair with respect to
an axis defined along the parent
muon's momentum. $\Theta$ is assigned the approximate average value:
\begin{equation}
 \Theta =\frac{M}{E}
\end{equation}
$\phi^+$ is generated isotropically and $\phi^- = \phi^+ + \pi$