\documentclass[10pt, a4paper, twocolumn, oneside]{article}

\usepackage[mono]{codebookpkg}

\team{NCTU\_a}{National Chiao Tung University}
\codetheme{default}

\begin{document}

\fontsize{7.2pt}{9pt}\selectfont

\tableofcontents
\newpage

\section{Misc}
    \subsection{Contest}
        \subsubsection{Debug List}
            \inputminted{md}{content/misc/debug-list.txt}
        \subsubsection{Makefile}
            \inputminted{makefile}{content/misc/makefile}
        \subsubsection{Default Code}
            \cppfile{content/misc/default.cpp}
        \subsubsection{acceleration yes}
            \cppfile{content/misc/pragmas.cpp}
        \subsubsection{C++17 constexpr}
            \cppfile{content/misc/constexpr.cpp}
        \subsubsection{Changing Stack Size}
            \cppfile{content/misc/stack-size.cpp}
    \subsection{Utils}
        \subsubsection{SplitMix64}
            \cppfile{content/misc/splitmix64.cpp}
        \subsubsection{Random}
            \cppfile{content/misc/random.cpp}
        \subsubsection{Bit Twiddling}
            \cppfile{content/misc/bit-hacks.cpp}
        \subsubsection{Floating Point Comparison}
            \cppfile{content/misc/epsilon.cpp}
        \subsubsection{Floating Point Binary Search}
            \cppfile{content/misc/float-binary-search.cpp}
    \subsection{Misc Algorithms}
        \subsubsection{Mo's Algorithm on Tree}
            \cppfile{content/misc/mo-tree.cpp}
        \subsubsection{Longest Increasing Subsequence}
            \cppfile{content/misc/lis.cpp}
        \subsubsection{Aliens Trick}
            \cppfile{content/misc/aliens.cpp}

\section{Data Structures}
    \subsection{GNU PBDS}
        \cppfile{content/ds/pbds.cpp}
    \subsection{Segment Tree}
        \cppfile{content/ds/segtree.cpp}
    \subsection{Line Container}
        \cppfile{content/ds/line-container.cpp}
    \subsection{Heavy-Light Decomposition}
        \cppfile{content/ds/hld.cpp}
    \subsection{Link-cut Tree}
        \cppfile{content/ds/lct.cpp}

\section{Math}
    \subsection{Number Theory}
        \subsubsection{Modular}
            \cppfile{content/math/number-theory/modular.cpp}
        \subsubsection{Extended GCD}
            \cppfile{content/math/number-theory/extgcd.cpp}
        \subsubsection{Chinese Remainder}
            \cppfile{content/math/number-theory/crt.cpp}
        \subsubsection{Sieve}
            \cppfile{content/math/number-theory/sieve.cpp}
        \subsubsection{Miller-Rabin}
            \cppfile{content/math/number-theory/miller-rabin.cpp}
        \subsubsection{Pollard's Rho}
            \cppfile{content/math/number-theory/pollard-rho.cpp}
        \subsubsection{Tonelli-Shanks}
            \cppfile{content/math/number-theory/tonelli-shanks.cpp}
        \subsubsection{Baby-Step Giant-Step}
            \cppfile{content/math/number-theory/bsgs.cpp}
        \subsubsection{Multiplicative Function Sum}
            \cppfile{content/math/number-theory/djs.cpp}
    \subsection{Combinatorics}
        \subsubsection{De Brujin Sequence}
            \cppfile{content/math/combinatorics/de-brujin.cpp}
        \subsubsection{Multinomial}
            \cppfile{content/math/combinatorics/multinomial.cpp}
        \subsubsection{Not Burnside's Lemma}
            \input{content/math/combinatorics/burnside.tex}
        \subsubsection{Matroid Intersection}
            \input{content/math/combinatorics/matroid.tex}
    \subsection{Theorems}
        \input{content/math/theorem.tex}

\section{Numeric}
    \subsection{long long Multiplication}
        \cppfile{content/numeric/ll-mul.cpp}
    \subsection{Barrett Reduction}
        \cppfile{content/numeric/barrett-reduction.cpp}
    \subsection{Polynomial Interpolation}
        \cppfile{content/numeric/interpolation.cpp}
    \subsection{Fast Fourier Transform}
        \cppfile{content/numeric/fft.cpp}
    \subsection{Fast Walsh-Hadamard Transform}
        \cppfile{content/numeric/fwht.cpp}
    \subsection{FFT Convolution}
        \cppfile{content/numeric/fftc.cpp}
    \subsection{Linear Recurrence}
        \subsubsection{Calculation}
            \cppfile{content/numeric/linear-recurrence.cpp}
        \subsubsection{Berlekamp-Massey}
            \cppfile{content/numeric/berlekamp-massey.cpp}
        \subsubsection{Composite Modulus Recurrence}
            \cppfile{content/numeric/min25-rec.cpp}
    \subsection{Matrix Determinant}
        \cppfile{content/numeric/det.cpp}
    \subsection{Matrix Inverse}
        \cppfile{content/numeric/inverse.cpp}
    \subsection{Linear Equations}
        \cppfile{content/numeric/linear-eq.cpp}
    \subsection{Simplex}
        \cppfile{content/numeric/simplex.cpp}

\section{Graph}
    \subsection{Modeling}
        \input{content/graph/model.tex}
    \subsection{Flow}
        \subsubsection{Dinic}
            \cppfile{content/graph/flow/dinic.cpp}
        \subsubsection{Gomory-Hu Tree}
            \cppfile{content/graph/flow/gomory-hu.cpp}
        \subsubsection{Global Minimum Cut}
            \cppfile{content/graph/flow/sw.cpp}
        \subsubsection{Min Cost Max Flow}
            \cppfile{content/graph/flow/mcmf.cpp}
    \subsection{Matching}
        \subsubsection{Kuhn-Munkres}
            \cppfile{content/graph/matching/km.cpp}
        \subsubsection{Bipartite Minimum Vertex Cover}
            \cppfile{content/graph/matching/min-bi-cover.cpp}
        \subsubsection{Maximum General Matching}
            \cppfile{content/graph/matching/edmonds.cpp}
        \subsubsection{Min Weight Perfect Matching}
            \cppfile{content/graph/matching/min-weight-general.cpp}
        \subsubsection{Stable Marriage}
            \cppfile{content/graph/matching/marriage.cpp}
    \subsection{Centroid Decomposition}
        \cppfile{content/graph/centroid-decomposition.cpp}
    \subsection{Minimum Mean Cycle}
        \cppfile{content/graph/min-mean-cycle.cpp}
    \subsection{Bellman-Ford}
        \cppfile{content/graph/bellman-ford.cpp}
    \subsection{Directed Minimum Spanning Tree}
        \cppfile{content/graph/dmst.cpp}
    \subsection{Maximum Clique}
        \cppfile{content/graph/clique.cpp}
    \subsection{Tarjan}
        \subsubsection{Strongly Connected Components}
            \cppfile{content/graph/tarjan-scc.cpp}
        \subsubsection{Articulation Point}
            \cppfile{content/graph/tarjan-ap.cpp}
        \subsubsection{Bridge}
            \cppfile{content/graph/tarjan-b.cpp}
    \subsection{2-SAT}
        \cppfile{content/graph/2sat.cpp}
    \subsection{Dominator Tree}
        \cppfile{content/graph/dominator-tree.cpp}
    \subsection{Biconnected Components}
        \cppfile{content/graph/bcc.cpp}
    \subsection{Edge BCC}
        \cppfile{content/graph/edge-bcc.cpp}
    \subsection{Manhattan MST}
        \cppfile{content/graph/manhattan-mst.cpp}
    \subsection{Notes}
        \inputminted{text}{content/graph/notes.txt}

\section{Geometry}
    \subsection{Basic 2D}
        \cppfile{content/geometry/basic-2d.cpp}
    \subsection{Angular Sort}
        \cppfile{content/geometry/angular-sort.cpp}
    \subsection{Convex Hull}
        \cppfile{content/geometry/convex-hull.cpp}
    \subsection{Convex Polygon Point Inclusion}
        \cppfile{content/geometry/convex-inclusion.cpp}
    \subsection{Convex Polygon Minkowski Sum}
        \cppfile{content/geometry/minkowski-sum.cpp}
    \subsection{Closest Pair}
        \cppfile{content/geometry/closest-pair.cpp}
    \subsection{Minimum Enclosing Circle}
        \cppfile{content/geometry/min-circle.cpp}
    \subsection{Half Plane Intersection}
        \cppfile{content/geometry/half-plane.cpp}
    \subsection{Delaunay Triangulation}
        \cppfile{content/geometry/delaunay.cpp}
    \subsection{Spherical Coordinates}
        \cppfile{content/geometry/spherical.cpp}
    \subsection{Quaternion}
        \cppfile{content/geometry/quaternion.cpp}
    \subsection{3D Convex Hull}
        \cppfile{content/geometry/3d-hull.cpp}

\section{Strings}
    \subsection{Z-value}
        \cppfile{content/strings/z-value.cpp}
    \subsection{Manacher}
        \cppfile{content/strings/manacher.cpp}
    \subsection{Minimum Rotation}
        \cppfile{content/strings/min-rotation.cpp}
    \subsection{Aho-Corasick}
        \cppfile{content/strings/ahocorasick.cpp}
    \subsection{Suffix Array}
        \cppfile{content/strings/suffix-array.cpp}
    \subsection{Suffix Tree}
        \cppfile{content/strings/suffix-tree.cpp}

\end{document}


\documentclass{article} % For LaTeX2e
\usepackage{times}
\usepackage{hyperref}
\usepackage{url}

\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amssymb}

\usepackage[T1]{fontenc}

\title{Efficient Calculation of Polynomial Features on Sparse Matrices}

%\author{Andrew Nystrom \\
%Savvysherpa Inc.\\
%6200 Shingle Creek Pkwy \\
%Suite 400 \\
%Minneapolis, MN 55430, USA \\
%\texttt{awnystrom@gmail.com} \\
%\And
%John F. Hughes \\
%Department of Computer Science \\
%Brown University \\
%Providence, RI \\
%\texttt{jfh@cs.brown.edu} \\
%}

\author{
  Nystrom, Andrew\\
  \texttt{awnystrom@gmail.com}
  \and
  Hughes, John\\
  \texttt{jfh@cs.brown.edu}
}

% The \author macro works with any number of authors. There are two commands
% used to separate the names and addresses of multiple authors: \And and \AND.
%
% Using \And between authors leaves it to \LaTeX{} to determine where to break
% the lines. Using \AND forces a linebreak at that point. So, if \LaTeX{}
% puts 3 of 4 authors names on the first line, and the last on the second
% line, try using \AND instead of \And before the third author name.

%\newcommand{\fix}{\marginpar{FIX}}
%\newcommand{\new}{\marginpar{NEW}}

\begin{document}

\maketitle

\begin{abstract}
We provide an algorithm for polynomial feature expansion that operates directly on a sparse matrix.
For a vector of dimension $D$ and density $d$, the algorithm has time and space complexity $O(d^kD^k)$ where $k$ is the polynomial order.
\end{abstract}

% this is a test-change for Spike to see whether Git seems to work for him. Ignore.

\section{Introduction}

Polynomial feature expansion has long been used in statistics to approximate nonlinear functions~\cite{gergonne1974application, smith1918standard}.
Despite this, we are unaware of any efforts to optimize calculating them.
Here we provide an algorithm for calculating polynomial features for a vector of dimension $D$ and density $d$ with time and space complexity $O(d^kD^k)$ where $k$ is the polynomial order, and $0 \le d \le 1$ is the fraction of elements that are nonzero.
The standard algorithm has time and space complexity $O(D^k)$, so the added factor of $d^k$ represents a significant complexity reduction.
The algorithm avoids densification of the vector, i.e. the vector remains in compressed sparse row form, so the space complexity is also $O(d^kD^k)$ as opposed to $O(D^k)$.

\section{Algorithm}
In the naive computation of polynomial features for a vector $\vec{x}$, we create a new feature for each product (with repetition) of $k$ features in $\vec{x}$ (or without repetition, for ``interaction features'').
This ignores data sparsity and will yield a product of zero any time one of the features involved in the product is zero.
In a sparse matrix, such zero-products are common.
%If $\vec{x}$ has density $d$, the fraction of nonzero elements of the $k$-order expansion will be $\frac{\left(\binom{dD+k-1}{k}\right)}{\left(\binom{D+k-1}{k}\right)} = \frac{[(dD+k-1)(dD+k-2) \dots (dD+k)](D-1)!}{(dD-1)!} = $
If we store vectors in a sparse matrix format, these zero-products need not be computed or stored.

The main idea behind our algorithm is to leverage sparsity by only computing products that do not involve zeros.
In a compressed sparse row matrix, the columns containing nonzero data are the only columns that are stored.
We can therefore iterate over products of combinations with repetition of order $k$ of \emph{only these columns} for each row to calculate $k$-degree polynomial features.

While the idea is straightforward, there is yet an unaddressed challenge:
Given a multiset of column indices whose corresponding nonzero components were multiplied to produce a polynomial feature, where in the augmented polynomial vector does the result of the product belong?
To address this, we give a bijective mapping from the set of possible column index combinations-with-repetition of order $k$ onto the column index space of the polynomial feature matrix. Thus the map has the form

\begin{equation}
(i_0, i_1, \dots, i_{k-1}) \rightarrowtail \hspace{-1.9ex} \twoheadrightarrow p_{i_0i_1 \dots i_{k-1}} \in \{0,1,\dots,\binom{D}{k}\}
\end{equation}
such that $ 0 \le i_0 \le i_1 \le \dots \le i_{k-1} < D$
where $(i_0, i_1, \dots, i_{k-1})$ are column indicies of a row vector $\vec{x}$ of an $N \times D$ input matrix, and $p_{i_0i_1 \dots i_{k-1}}$ is a column index into the polynomial expansion vector for $\vec{x}$ where the product of elements corresponding to indices $i_0, i_1, \dots, i_{k-1}$ will be stored.
%and $\left(\binom{D}{k}\right)$ is the number of combinations with repetition of size $k$ drawn from $D$ objects.
%In general, $\left(\binom{n}{k}\right) = \binom{n+k-1}{n-1} = \binom{n+k-1}{k}$, as given by \cite{stanley1986enumerative}.

\subsection{Construction of Mappings}

For the second degree case, we seek a map from matrix indices $(i, j)$ (with $0 \le i < j < D$ ) to numbers $f(i, j)$ with $0 \le f(i, j) < \frac{D(D-1)}{2}$, one that follows the pattern indicated by
\begin{align}
\begin{bmatrix}
x & 0 & 1 & 3 \\
x & x & 2 & 4 \\
x & x & x & 5 \\
x & x & x & x
\end{bmatrix}
\label{eq:4x4mat}
\end{align}
where the entry in row $i$, column $j$, displays the value $f(i, j)$. We let $T_2(n) = \frac{1}{2} n(n+1)$
be the $n$th triangular number; then in Equation~\ref{eq:4x4mat}, column $j$ (for $j > 0$) contains entries with
$T_2(j-1) \le e < T_2(j)$; the entry in the $i$th row is just $i + T_2(j-1)$. Thus we have
$
f(i, j)
= i + T_2(j-1) =  \frac{1}{2}(2i + j^2-j).$
For instance, in column $j = 2$ in our example (the \emph{third} column), the entry in row $i = 1$ is
$i + T_2(j-1) = 1 + 1 = 2$.

With one-based indexing in both the domain and codomain, the formula above becomes
$f_1(i, j)  = \frac{1}{2}(2i + j^2 - 3j + 2).$

For \emph{polynomial} features, we seek a similar map $g$, one that also handles the case $i = j$. In this case, a similar analysis yields
$ g(i, j) = i + T_2(j) = \frac{1}{2} (2i + j^2 + j + 1).$

To handle \emph{three-way interactions}, we need to map triples of indices in a 3-index array to a flat list, and similarly for higher-order interactions. For this, we'll need the tetrahedral numbers $T_3(n) = \sum_{i=1}^n T_{2}(n) =
\frac{1}{6}(n^3 + 3n^2 + 2n)$.

For three indices, $i,j,k$, with $0 \le i < j < k < D$, we have a similar recurrence. Calling the mapping $h$, we have
\begin{align}
h(i,j,k) = i + T_2(j-1) + T_3(k-2);
\end{align}
if we define $T_1(i) = i$, then this has the very regular form
\begin{align}
h(i,j,k) =  T_1(i) + T_2(j-1) + T_3(k-2);
\end{align}
and from this the generalization to higher dimensions is straightforward. The formulas for ``higher triangular numbers'', i.e., those defined by
\begin{align}
T_k(n) &= \sum_{i=1}^n T_{k-1}(n)
\end{align}
for $k > 1$ can be determined inductively.

The explicit formula for 3-way interactions, with zero-based indexing, is
\begin{align}
h(i, j, k) &= 1 + (i-1) + \frac{(j-1)j}{2} + \\
& \frac{(k-2)^3 + 3(k-2)^2 + 2(k-2)}{6}.
\end{align}

\section{Complexity Analysis}
%\subsection{Analytical}

Calculating $k$-degree polynomial features via our method for a vector of dimensionality $D$ and density $d$ requires $\binom{dD}{k}$ (with repetition) products.
The complexity of the algorithm, for fixed $k \ll dD$, is therefore
\begin{align}
O\left(\binom{dD+k-1}{k}\right) & = O\left(\frac{(dD+k-1)!}{k!(dD-1)!}\right)\\
& = O\left(\frac{(dD+k-1)(dD+k-2) \dots (dD)}{k!}\right)\\
& = O\left((dD+k-1)(dD+k-2) \dots (dD)\right) \mbox{ for } k \ll dD\\
& = O\left(d^kD^k\right)
\end{align}

%\subsection{Empirical}
%\section{Conclusion}

\bibliography{sparse_poly}
\bibliographystyle{iclr2016_workshop}

\end{document}


\documentclass[parskip=half]{scrartcl}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[citestyle=authoryear-icomp,bibstyle=authoryear]{biblatex}
\usepackage{booktabs}
\usepackage[style=british]{csquotes}
\usepackage{fontspec}
\usepackage{siunitx}

\setmainfont[Numbers=Lowercase]{TeX Gyre Pagella}
\newfontfamily\dispfamily{TeX Gyre Pagella}
\addtokomafont{disposition}{\dispfamily}

\MakeAutoQuote{«}{»}
\DeclareMathOperator{\Res}{Res}

\addbibresource{cable.bib}

\title{Analytic solution to passive cable model}
\author{Sam Yates}
\date{October 20, 2016}

\begin{document}

\maketitle

\section{Background}

The Rallpack suite \parencite{bhalla1992} is a set of three tests for cable-based neuronal
simulators, for validation and benchmarking. Validation data is provided with the
distribution of the suite, which at the time of writing can now be found within the
\textsc{Genesis} simulator simulator distribution.\footnote{see \url{http://genesis-sim.org}.}

The first two of the tests are based on passive cable models which admit analytic
solutions, and the Rallpack suite includes code that can generate the reference curves
for these models. The reference code, however, requires hand-tuning of the iterations;
the authors state in the internal documentation that
«the use of 10 terms gives at least six figure accuracy», but this is for the
fixed time increments used in their dataset and is not validated within the code
or by the supplied material.

For flexibility of testing and cross-checking of the Rallpack reference data,
it would be useful to have at hand a robust calculator of the passive cable
potential solution.

\section{The Rallpack 1 model}

The model comprises a cylindrical cable segment with the physical and electrical
properties as described in Table~\ref{tbl:rallpack1}. A current of \SI{0.1}{\nA}
is injected at the left end of the cable from $t=0$, and the initial potential
is the reversal potential.

\begin{table}[ht]
    \centering
    \begin{tabular}{lSl}
        \toprule
        Term & {Value} & Property\\
        \midrule
        $d$    & \SI{1.0}{\um}                 & cable diameter \\
        $L$    & \SI{1.0}{\mm}                 & cable length \\
        $R_A$  & \SI{1.0}{\ohm\m}              & bulk axial resistivity \\
        $R_M$  & \SI{4.0}{\ohm\m\squared}      & areal membrane resistivity \\
        $C_M$  & \SI{0.01}{\F\per\m\squared}   & areal membrane capacitance \\
        $E_M$  & \SI{-65.0}{\mV}               & membrane reversal potential \\
        \bottomrule
    \end{tabular}
    \caption{Cable properties for the Rallpack 1 model.}
    \label{tbl:rallpack1}
\end{table}

The potential on a constant-radius passive cable is governed by the PDE
\begin{equation}
    \lambda^2 \frac{\partial^2 v}{\partial x^2} =
    \tau\frac{\partial v}{\partial t} + v - E,
\end{equation}
where $E$ is the membrane reversal potential, and $\lambda$ and $\tau$ are the
electrotonic length and time constants for the cable. It is convenient
to consider the electrical properties of the cable per unit-length, in terms
of the linear axial resistivity $r$, the linear membrane capacitance $r$,
and the linear membrane capacitance $c$. These determine $\lambda$ and $\tau$ by
\begin{gather*}
    \lambda = \frac{1}{r\cdot g},\\
    \tau = r\cdot c.
\end{gather*}

With the model boundary conditions,
\begin{subequations}
    \begin{align}
        v(x, 0) &= E, \\
        \left.\frac{\partial v}{\partial x}\right\vert_{x=0} & = -Ir, \\
         \left.\frac{\partial v}{\partial x}\right\vert_{x=L} & = 0,
    \end{align}
\end{subequations}
where $I$ is the injected current and $L$ is the cable length.

The solution $v(x, t)$ can be expressed in terms of the solution $g(x, t; L)$
to a normalized version of the cable equation,
\begin{subequations}
    \begin{align}
        \label{eq:normcable}
        \frac{\partial^2 g}{\partial x^2} & =
        \frac{\partial g}{\partial t} + g,
        \\
        \label{eq:normcableinitial}
        g(x, 0) &= 0,
        \\
        \label{eq:normcableleft}
        \left.\frac{\partial g}{\partial x}\right\vert_{x=0} & = 1,
        \\
        \label{eq:normcableright}
        \left.\frac{\partial g}{\partial x}\right\vert_{x=L} & = 0
    \end{align}
\end{subequations}
by
\begin{equation}
    v(x, t)= E - Ir\lambda \cdot g(\frac{x}{\lambda}, \frac{t}{\tau};  \frac{L}{\lambda}).
\end{equation}

\section{Solution to the normalized cable equation}

Let $G(x, s)$ be the Laplace transform of $g(x, t)$ with respect to $t$.
If the inverse transform of $G$ exists, it must agree with $g$ almost everywhere
for $t>0$, or for all $t>0$ given the continuity of $g$.

From
\eqref{eq:normcable} and \eqref{eq:normcableinitial},
\begin{equation}
    \label{eq:lap}
    \frac{\partial^2 G}{\partial x^2} = sG + G.
\end{equation}
The boundary conditions \eqref{eq:normcableleft} and \eqref{eq:normcableright} give
\begin{align}
    \label{eq:lapleft}
    \frac{\partial G}{\partial x}(0, s) & = \frac{1}{s}
    \\
    \label{eq:lapright}
    \frac{\partial G}{\partial x}(L, s) & = 0.
\end{align}

Solutions to \eqref{eq:lap} must be of the form
\begin{equation}
    G(x, s) = A(s) e^{mx} +B(s) e^{-mx}
\end{equation}
where $m=\sqrt{1+s}$. From \eqref{eq:lapleft} and \eqref{eq:lapright},
\begin{align*}
    mA(s) - mB(s) & = \frac{1}{s}
    \\
    mA(s)e^{mL} -mB(s)e^{-mL} & = 0
\end{align*}
and thus
\begin{align*}
    A(s) & = \frac{1}{sm (1-e^{2mL})}
    \\
    B(s) & = \frac{e^{2mL}}{sm (1-e^{2mL})}.
\end{align*}

Consequently,
\begin{equation}
    \begin{aligned}
        G(x, s) &= \frac{1}{ms}\cdot\frac{e^{mx}+e^{2mL-mx}}{1-e^{2mL}}\\
                &= - \frac{1}{ms}\cdot\frac{\cosh m(L-x)}{\sinh mL}.
    \end{aligned}
\end{equation}

\subsection*{Inversion}

Sufficient conditions for the inverse transform of $G(x,s)$ to exist
and be representable in series form are as follows
\parencite[][Theorem 4]{churchill1937}:
\begin{enumerate}
\item
    $G(x, s)$ is analytic in some right half-plane $H$,
\item
    the singularities of $G(x, s)$ are all poles, and
\item for some $k>1$, $|s^k G(x, s)|$ is bounded in $H$ and
    on the circles $|s|=\rho_i$, where $\rho_i$ is an unbounded monotonically
    increasing sequence.
\end{enumerate}
If in addition all the poles $\sigma_j$ of $G$ are simple, then
the series expansion of the inverse transform is given by
\begin{equation}
    g(x,t) = \sum_j e^{\sigma_j t}\, \Res(G;\sigma_j),\quad t>0.
\end{equation}

$G(x,s)$ has poles when $s=0$, $m=0$, or $\sinh mL=0$.
In the following, let
\begin{gather*}
    a_k = k\pi /L\\
    m_k = a_k i\\
    s_k = m_k^2 -1 = -k^2\pi^2/L^2-1.
\end{gather*}
for $k\in\mathbb{Z}$.
The poles are then $0$, $-1$, and $s_k$ for $k\geq 1$.

There are no branch points
arising from $m=\sqrt{1+s}$, as letting $m=-\sqrt{1+s}$ leaves $G$ unchanged.

For $|s+1|>\epsilon$,
\begin{equation}
    \begin{aligned}
        |s^{3/2}G(x,s)|^2
            & \leq (1+\epsilon)^{-1} \left| \frac{\cosh m(L-x)}{\sinh mL} \right|^2
        \\
            & \leq (1+\epsilon)^{-1} (1+|\coth mL|)^2
    \label{eq:gbounds}
    \end{aligned}
\end{equation}
which is bounded in the half-plane $\Re(s) \geq  -1+\epsilon$.
As $\coth u+iv$ is periodic in $v$, \eqref{eq:gbounds} is also bounded outside
the regions $\{s: m\in D_k\}$, where $D_k$ are non-overlapping $\delta$-neighborhoods of $m_k$
for some fixed small $\delta>0$. These are contained within disks of radius
$2k\pi\delta/L+\delta^2$ about $s_k$ for $k\geq 1$, which then are separated by
circles $|s|=\rho_k$ about the origin for some $\rho_k$ in $(|s_k|, |s_k+1|)$.

\textbf{Pole at $s=0$}.\\
Recalling $m=\sqrt{1+s}$, $m$ and $\sinh mL$ are non-zero in
a neighbourhood of $s=0$, and so the pole is simple and
\begin{equation}
    \begin{aligned}
        \Res(G; 0) & = - \frac{1}{m}\cdot\left.\frac{\cosh m(L-x)}{\sinh mL}\right|_{s=0}\\
                   & = - \frac{\cosh (L-x)}{\sinh L}.
    \end{aligned}
\end{equation}

\textbf{Pole at $s=-1$}.\\
Let $G(x,s)=f(x,s)/h(s)$, where
\begin{equation}
    \begin{aligned}
        f(x, s) &= -\frac{1}{s}\cosh m(L-x)\\
        h(s) &= m\sinh mL.
    \end{aligned}
    \label{eq:hf}
\end{equation}
Noting that $dm/ds = \frac{1}{2}m^{-1}$,
\begin{equation}
    \begin{aligned}
        h'(s) &= \frac{1}{2}m^{-1}\sinh mL + \frac{1}{2}L\cosh mL \\
              &= \frac{1}{2}L + \frac{1}{2}L + O(m^2) \quad(m\to 0) \\
              &= L + O(s+1) \quad(s\to -1).
    \label{eq:hprime}
    \end{aligned}
\end{equation}
The pole is therefore simple, and
\begin{equation}
    \Res(G; -1) = f(x, -1)/h'(-1) = -\frac{1}{L}.
\end{equation}

\textbf{Pole at $s=s_k$, $k \geq 1$}.\\
Taking $h$ and $f$ as above \eqref{eq:hf},
$m_k$ is non-zero for $k\geq 1$ and
\[
    h'(s_k) = \frac{1}{2}L\cosh m_kL.
\]
Consequently the pole is simple and
\begin{equation}
    \begin{aligned}
        \Res(G; s_k)
            & = f(x, s_k)/h'(s_k)\\
            & =  -\frac{2}{s_k L}\frac{\cosh m_k(L-x)}{\cosh m_kL} \\
            & =  -\frac{2}{s_k L}\frac{\cosh m_kL\cosh m_kx-\sinh m_kL\sinh m_kL}{\cosh m_kL} \\
            & =  -\frac{2}{s_k L}\cosh m_k x,
    \end{aligned}
\end{equation}
as $\sinh m_k=0$.

In terms of $a_k$,
\begin{equation}
    \Res(G; s_k) = \frac{2}{L}\cdot\frac{1}{1+a_k^2}\cdot\cos a_kx.
\end{equation}

The series exapnsion for $g(x, t)$ therefore is
\begin{equation}
    g(x, t) = -\frac{\cosh(L-x)}{\sinh L} + \frac{1}{L}e^{-t}\left\{
        1+2\sum_{k=1}^\infty \frac{e^{-ta_k^2}}{1+a_k^2}\cos a_k x\right\}.
    \label{eq:theg}
\end{equation}

\section{Computation of series}

As $t$ approaches zero, the series \eqref{eq:theg} converges increasingly slowly.
For computational purposes, an estimate on the series residual allows the determination
of stopping criteria for a given tolerance.

Let $g_n$ be the partial sum
\begin{equation}
    g_n(x, t) = -\frac{\cosh(L-x)}{\sinh L} + \frac{1}{L}e^{-t}\left\{
        1+2\sum_{k=1}^n \frac{e^{-ta_k^2}}{1+a_k^2}\cos a_k x\right\}.
\end{equation}
so that $g(x, t) =\lim_{n\to\infty} g_n(x,t)$. Let $\bar{g}_n = |g-g_n|$ be the
residual. The $a_k$ form an increasing sequence, so
\begin{equation}
    \begin{aligned}
        \bar{g}_n(x,t)
            & \leq \frac{2}{L}e^{-t}\sum_{n+1}^\infty\frac{e^{-ta_k^2}}{1+a_k^2}\\
            & \leq \frac{2}{L}e^{-t}\int_{a_n}^\infty \frac{e^{-tu^2}}{1+u^2}\,du\\
            & < \frac{2}{L}e^{-t}\int_{a_n}^\infty \frac{e^{-tu^2}}{u^2}\,du.
    \end{aligned}
    \label{eq:gbar}
\end{equation}
Substituting $u=\sqrt{v/t}$ gives the identity
\begin{equation}
    \int_{a}^\infty \frac{e^{-tu^2}}{u^2}\,du
    = \frac{1}{2}\sqrt{t}\int_{a^2t}^\infty e^{-tv} v^{\frac{-3}{2}}\,dv
    = \frac{1}{2}\sqrt{t}\,\Gamma(-\frac{1}{2},a^2 t),
\end{equation}
where $\Gamma(\alpha, z)$ is the upper incomplete gamma function.

For real $\alpha<1$ and $z>0$, \textcite[][Theorem 2.3]{borwein2009} give the upper bound
\[
    \Gamma(\alpha, z) \leq z^{\alpha-1} e^{-z}.
\]
Substituting into \eqref{eq:gbar} gives
\begin{equation}
    \begin{aligned}
        \bar{g}_n(x,t)
            & < \frac{1}{L}e^{-t}\sqrt{t}\,\Gamma(-\frac{1}{2},a_n^2 t) \\
            & \leq \frac{1}{L}e^{-t}\sqrt{t}\,(a_n^2 t)^{-\frac{3}{2}}e^{-a_n^2t} \\
            & = \frac{e^{-t(1+a_n^2)}}{L t a_n^3}.
    \end{aligned}
\end{equation}

\printbibliography
\end{document}


\documentclass{beamer}

\mode<presentation> {
  \usetheme{Madison}
%    \usetheme[left,hideallsubsections,width=1cm]{UWThemeB}
  \usefonttheme[onlymath]{serif}
}

% % % % to make navigation two lines
\usepackage{ragged2e}

\makeatletter
\setbeamertemplate{section in head/foot}{%
    \parbox[c][0.33cm][t]{\dimexpr(\textwidth-1.3cm)/\beamer@sectionmax\relax}{%
        \RaggedRight\fontsize{4}{4}\selectfont\insertsectionhead}}
\setbeamertemplate{footline}[frame number]
\makeatother
% % % %
\usepackage{booktabs, calc, rotating}
\usepackage{scalefnt}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
%\usepackage[T1]{fontenc}
\usepackage{alltt}

  \setbeamertemplate{navigation symbols}{}
  \setbeamertemplate{blocks}[rounded][shadow=true]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  CHANGE THE TITLE AND INPUT FILE ACCORDING TO THE CHAPTER THAT YOU WISH TO COMPILE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\title[Severity]{Severity Distributions}

\date[Fall 2017]{Fall 2017}
%Personal definitions - Bayes Regression
\def\bsb{\boldsymbol \beta}
\def\bsa{\boldsymbol \alpha}
\def\bsm{\boldsymbol \mu}
\def\bsS{\boldsymbol \Sigma}
\def\bsx{\boldsymbol \xi}

\begin{document}

\frame{\titlepage}

\begin{frame}
  \frametitle{Outline}
    %\tableofcontents[part=1,pausesections]
     \tableofcontents[part=1]
\end{frame}

\part<presentation>{Main Talk}

\section[Foundations]{Severity Distributions Foundation}

\begin{frame}[shrink=2]
\frametitle{Important Severity Distributions}

Three important loss severity distributions: \vspace{2mm}
\begin{itemize}
\item \textbf{Gamma}
\begin{itemize}
\item Fits medium tail lines like physical damage auto and homeowners well
\item Member of the ``exponential family of distributions''. This means that it is easy to incorporate rating variables into the distribution via generalized linear modeling
(GLMs) \vspace{2mm}
\end{itemize}
\item \textbf{Pareto}
\begin{itemize}
\item Fits longer tail lines like injury liability in auto and workers' compensation well
\item Simple to work with analytically (hence can provide intuition as we develop theory and explain the theory to
others) \vspace{2mm}
\end{itemize}
\item \textbf{GB2 - Generalized Beta of the Second Kind}
\begin{itemize}
\item A four parameter distribution family, complex
\item Yet, many severity distributions can be expressed as a special case of this distribution (good for programming)
\item Some applications have been fit well by the GB2 where others do not seem to
work \vspace{2mm}
\end{itemize}
\end{itemize}
\end{frame}

\begin{frame}%[shrink=2]
\frametitle{Gamma Distribution}

\begin{itemize}\scalefont{0.9}
\item The gamma distribution has two parameters,  $\alpha$ and $\theta$ %\vspace{2mm}

\item The probability density function is 0 for $x \le 0$ and for $x>0$
\begin{eqnarray*}
f(x) &=& \frac{(\frac{x}{\theta})^{\alpha} e^{-x/\theta}}{x\Gamma(\alpha)} = \frac{1}{\theta^{\alpha} \Gamma(\alpha)} x^{\alpha - 1} e^{-x/\theta}
\end{eqnarray*} %\vspace{2mm}

\item If $\alpha=1$, the gamma reduces to the familiar \textit{exponential} distribution
%\vspace{2mm}
\item The function $\Gamma(\cdot)$ is known as the \textit{gamma function}, defined as
\begin{equation*}
\Gamma(\alpha) =  \int_0^{\infty} x^{\alpha-1} e^{-x} ~dx
\end{equation*} %\vspace{2mm}
\item Some important facts about the gamma function: %\vspace{2mm}

\begin{itemize}\scalefont{0.9}
\item For a positive integer $n$, $\Gamma(n) = (n-1)!$ %\vspace{2mm}

\item For more general arguments, one needs to rely on numerical integration to evaluate $\Gamma(\cdot)$. The two main exceptions are:
\begin{itemize}\scalefont{0.9}
\item For any $\alpha>0$, $\Gamma(\alpha+1)=\alpha\Gamma(\alpha)$
\item $\Gamma(0.5)=\sqrt{\pi}$
\end{itemize}\end{itemize}
Thus, for example, $\Gamma(2.5)=1.5 \Gamma(1.5) = 1.5 (0.5)
\Gamma(0.5) = \frac{3}{4} \sqrt{\pi} $
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Exercise} \emph{Example.} Suppose that $X \sim
gamma(\alpha=2, \theta=100)$, that is, the random variable $X$ has a
gamma distribution with parameters $\alpha=2$ and  $\theta=100$
\vspace{2mm}

Determine $\Pr(X \le 60)$

\end{frame}

%\begin{frame}[shrink=2]
%\frametitle{Incomplete Gamma Function}
%\begin{itemize}
%   \item More generally, we define the \textit{incomplete gamma function}
%\begin{equation*}
%\Gamma(\alpha;x) =  \int_0^x t^{\alpha-1} e^{-t} ~dt
%\end{equation*} \vspace{2mm}

%\item With this, we may express the gamma distribution function as
%\begin{equation*}
%F(x) = \Gamma(\alpha; \frac{x}{\theta}),
%\end{equation*}
%so that computing the gamma distribution function is evaluating the
%incomplete gamma function at a rescaled value of $x$ \vspace{2mm}

%\begin{itemize}\item In general, this is done via numerical integration
%\item However, when $\alpha$ is a positive integer, one can use integration by parts as in the preceding example to get:
%\begin{equation*}
%\Gamma(\alpha; x) = 1 - \sum_{j=0}^{\alpha-1} ~ \frac{x^j
%e^{-x}}{j!}
%\end{equation*}
%In their book, KPW have this as Theorem A.1 \vspace{2mm}
%\end{itemize}
%\item \emph{Example - Continued.} Suppose that $X \sim gamma(\alpha=2, \theta=100)$. Determine $\Pr(X \le 60)$.
%\end{itemize}
%\end{frame}

\begin{frame}[shrink=2]
\frametitle{Gamma Moments} Use the gamma function to calculate
moments of a gamma distribution \vspace{2mm}
\begin{itemize}
\item Define the $k$th \textit{raw moment} to be
\begin{eqnarray*}
\mu_k^{\prime} &=& \mathrm{E~} X^k = \int_0^{\infty} x^k f(x) dx
\end{eqnarray*} \vspace{2mm}

\item Using a change of variable, $t=x/\theta$, we have
\begin{eqnarray*}
\mu_k^{\prime}
&=& \frac{1}{\theta^{\alpha} \Gamma(\alpha)} \int_0^{\infty} x^{\alpha+k-1} \exp(-x/\theta) ~dx \\
&=& \frac{\theta^{\alpha+k}}{\theta^{\alpha} \Gamma(\alpha)} \int_0^{\infty} t^{\alpha+k-1} \exp(-t) dt \\
&=& \frac{\theta^k }{\Gamma(\alpha)} \Gamma(\alpha+k).
\end{eqnarray*} \vspace{2mm}
\item With $k=1$, we have $\mu = \mu_1^{\prime}
= \theta \frac{\Gamma(\alpha+1)}{\Gamma(\alpha)} = \alpha \theta$
\vspace{2mm}
\item Check that:
\begin{itemize}
\item $ \mu_2^{\prime} = \theta^2 \alpha (\alpha+1), $ \hspace{0.2in} $ \mathrm{Var}(X)  =\theta^2
\alpha,
$ \hspace{0.2in} $\mu_k^{\prime} = \theta^k(\alpha+k-1) \cdots
\alpha$
\end{itemize}\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Gamma Moment Generating Function}
\begin{itemize}
\item The gamma moment generating function (mgf) is
\begin{eqnarray*}
M(t) = \mathrm{E~} e^{tX} &=& \frac{1}{\theta^{\alpha}\Gamma(\alpha)}\int_0 ^{\infty} x^{\alpha-1} e^{tx} e^{-x/\theta} dx\\
&=& \frac{1}{\theta^{\alpha}\Gamma(\alpha)}\int_0 ^{\infty} x^{\alpha-1} \exp\left\{-x\left(\frac{1-\theta t}{\theta}\right)\right\} dx\\
&=& \frac{1}{\theta^{\alpha}\Gamma(\alpha)} \left(\frac{\theta}{1-\theta t}\right)^{\alpha} \Gamma(\alpha)\\
&=& (1 - \theta t)^{-\alpha} .
\end{eqnarray*}
\item To check this result, first note that $M(0) = (1 - \theta 0)^{-\alpha} = 1$, as anticipated. Next, taking derivatives, we have
\begin{eqnarray*}
M^{\prime}(t) = \frac{\partial}{\partial t} M(t) &=& -\alpha (1 - \theta t)^{-\alpha - 1}(-\theta) = \alpha \theta (1 - \theta t)^{-\alpha - 1}
\end{eqnarray*}
\item Evaluating this at 0 yields $M^{\prime}(0) = \alpha \theta =\mu$, as anticipated
\end{itemize}
\end{frame}

\begin{frame}%[shrink=2]
\frametitle{Pareto Distribution}
\begin{itemize}\scalefont{0.9}
\item The Pareto with parameters $\alpha$ and $\theta$ has probability density function:
\begin{eqnarray*}
\mathrm{f}(x) = \frac{\alpha \theta^{\alpha}}{(x+\theta)^{\alpha+1}}
\end{eqnarray*} %\vspace{2mm}
and moments
\begin{eqnarray*}
\mu_k^{\prime}= \frac{\theta^k k!}{(\alpha-1) \cdots (\alpha-k)}
\end{eqnarray*} %\vspace{2mm}
\item Because moments are not finite when $k \ge \alpha$, the moment generating function is not well-defined %\vspace{2mm}
\item Unlike the gamma, there is a simple expression for the distribution function
\begin{eqnarray*}
\mathrm{F}(x) &=& \int^x_0 \mathrm{f}(y) dy = 1 -
\left(\frac{\theta}{x+\theta}\right)^{\alpha}
\end{eqnarray*} %\vspace{2mm}
\item This means that it is easy to compute quantiles
\begin{itemize}\scalefont{0.9}
\item For example, find $x$ so that 0.95 = F(x) (the 95th percentile) %\vspace{2mm}
\item Easy calculations show that this is $\theta \left[(0.05)^{-1/\alpha} - 1\right]$ %\vspace{2mm}
\item In general, the $p$th percentile/quantile is $\theta \left[(1-p)^{-1/\alpha} - 1\right]$
\end{itemize}\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{GB2 - Generalized Beta of the Second Kind}
\begin{itemize}
\item In KPW, the GB2 is known as a \textit{transformed beta
distribution} \vspace{2mm}

\item The pdf (KPW, Appendix A2.1.1) is
\begin{eqnarray*}
f(x) &=& \frac{\Gamma(\alpha + \tau)}{\Gamma(\alpha)\Gamma(\tau)}
   \frac{\gamma (x/\theta)^{\gamma \tau}}
   {x\left[1+(x/\theta)^{\gamma}\right]^{\alpha + \tau}}
\end{eqnarray*} \vspace{2mm}

with moments
\begin{eqnarray*}
\mathrm{E~}X^k &=& \theta^k \frac{\Gamma(\tau + \frac{k}{\gamma})\Gamma(\alpha-\frac{k}{\gamma})}{\Gamma(\alpha)\Gamma(\tau)} .
\end{eqnarray*} \vspace{2mm}

\item In the text by Frees on Regression, the GB2 distribution is cited but with the parameters
$\alpha_1  = \alpha$, $\alpha_2  =\tau$, $\gamma = 1/\sigma$,
$\theta = e^\mu$
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{GB2 Special Cases} The GB2 a four parameter family of
distributions that captures many other distributions, either as
special cases or as limiting results: \vspace{2mm}

\begin{itemize}
\item \textit{Special Case: Burr Distribution}. Use the GB2 distribution with $\tau =
1$ \vspace{2mm}

\item \textit{Special Case: Pareto Distribution}. Use the GB2 distribution with $\gamma=\tau =
1$ \vspace{2mm}

\item \textit{Limiting Case: Generalized Gamma Distribution} \vspace{2mm}

Replace $\theta$ by $\theta \tau^{1/\gamma}$ \vspace{2mm}

Then, one can show that
\begin{eqnarray*}
\lim_{\tau \to \infty} f_{GB2}(x; \theta \tau^{1/\gamma}, \alpha, \tau, \gamma) = f(x),
\end{eqnarray*}
the pdf of a \textit{generalized gamma}. In KPW, Appendix A.3.1,
p.673, the generalized gamma is called a \emph{transformed gamma}
\end{itemize}
\end{frame}

\section{Creating Distributions Using Transformations}

\begin{frame}[shrink=2]
\frametitle{Creating Distributions Using Transformations} There are
many distributions available to the analyst \vspace{2mm}

\begin{itemize}
\item In this section, we consider distributions that are created by transforming the random variable of a distribution.
Specifically: \vspace{2mm}

\begin{itemize}
\item  Multiplication by a constant ($Y = cX$) \\
\item Raising to a power ($Y = X^\tau$)\\
\item Exponentiation $(Y = e^X)$\\
\end{itemize}\vspace{2mm}

\item In the next section, we consider ways of combining distributions to form a distribution of interest. Specifically: \vspace{2mm}

\begin{itemize}
\item Mixing
\item Splicing
\end{itemize}\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Multiplication by a Constant}
\begin{itemize}
\item Multiplying a random variable by a positive constant is a simple type of
transformation \vspace{2mm}

\item It is also easy to interpret: \vspace{2mm}

\begin{itemize}
\item Think of $X$ as this year's losses and assume that we have an 8\% inflation rate. Then, we can model next year's losses as $Y =
1.08X$ \vspace{2mm}

\item We also want to readily go from dollars to thousands of dollars ($c=1/1000$) or from dollars to Euros (or swapping any set of
currencies) \vspace{2mm}

\end{itemize}
\item More generally, let $Y=cX$ and use
\begin{eqnarray*}
F_Y (y) &=& \Pr( Y \le y) = \Pr \left(X \le \frac{y}{c}\right)= F_X \left(\frac{y}{c}\right)\\
f_Y (y) &=& \frac{1}{c}~f_X\left(\frac{y}{c}\right)
\end{eqnarray*}
\end{itemize}
\end{frame}

\begin{frame}%[shrink=2]
\frametitle{Scale Distributions}
\begin{itemize}\scalefont{0.9}
\item In a location-scale distribution, the transformed variable $Y = c(X-a)$ has a distribution from the same family as the random variable $X$ %\vspace{2mm}
\begin{itemize}\scalefont{0.9}
\item Here, $a$ and $c>0$ are constants
\item The normal distribution is the usual example %\vspace{2mm}
\end{itemize}
\item In a scale distribution,  the transformed variable $Y = c X$ has a distribution from the same family as the random variable $X$ %\vspace{2mm}
\begin{itemize}\scalefont{0.9}
\item Many loss distributions are scale distributions
\item Typically, one uses $\theta$ as the scale parameter. If $X$ comes from a distribution with parameter $\theta$, then $Y = c X$ has the same distribution with scale parameter $\theta^{\ast} = c \theta$ %\vspace{2mm}
\end{itemize}
\item \textit{Example: Special Case - Gamma Distribution}. Suppose that $X$ has a gamma distribution with $\alpha = 4$ and $\theta = 10,000$ %\vspace{2mm}
\begin{itemize}\scalefont{0.9}
\item The mean is $\alpha \theta = 40,000$ and the standard deviation is $\theta \sqrt{\alpha} = 20,000$ %\vspace{2mm}
\item Suppose that $Y = X/1000$
\begin{itemize}\scalefont{0.9}
\item This has mean 40 and standard deviation 20
\item Further $Y$ has a gamma distribution with parameters $\alpha = 4$ and $\theta^{\ast} = \theta/1000 = 10$\end{itemize}
\end{itemize}\end{itemize}
\end{frame}

\begin{frame}%[shrink=.5]
\frametitle{Raising to a Power}
\begin{itemize}\scalefont{0.9}
\item Another type of transformation involves raising the random variable to a power, say, $\tau$ %\vspace{2mm}
\item Consider the transformed random variable $Y = X^{\tau}$. We examine three cases: %\vspace{2mm}
\scalefont{0.9}
\begin{eqnarray*}
&\tau>0&  \text{transformed}\\
&\tau=-1 &  \text{inverse}\\
&\tau<0 & \text{inverse transformed}
\end{eqnarray*}\scalefont{1.1111} %\vspace{2mm}
\item \textit{Special Case: Exponential Distribution}. Suppose that $X$ has an exponential distribution with parameter $\theta^{\ast}$ and consider $Y=1/X$ %\vspace{2mm}
\begin{itemize}\scalefont{0.9}
\item The distribution function of $Y$ is
\begin{eqnarray*}
\Pr(Y \le y) &=& \Pr(\frac{1}{X} \le y) = \Pr(\frac{1}{y} \le X) = \exp\left(-\frac{1}{y \theta^\ast}\right) .
\end{eqnarray*}
\item Now, define a new parameter $\theta = \frac{1}{\theta^{\ast}}$. With this notation,
\begin{eqnarray*}
\Pr(Y \le y) &=& \exp\left(-\frac{\theta}{y}\right).
\end{eqnarray*}
\item This distribution is known as an \textit{inverse exponential distribution} with parameter $\theta$. See the appendix of KPW
\end{itemize}
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Exponential to get a Weibull}
\textit{Example: Transforming an Exponential to get a Weibull}.
\begin{itemize}
\item Start with $X \sim $ exponential distribution with parameter 1. Define the transformed random
variable:
\begin{eqnarray*}
Y = \theta X^{1/\tau}
\end{eqnarray*} \vspace{2mm}

\item This has distribution
\begin{eqnarray*}
F_Y(y) &=& \Pr(Y \le y)\\
&=& \Pr(X^{1/\tau} \le \frac{y}{\theta}) = \Pr(X \le (\frac{y}{\theta})^{\tau})\\
&=& 1 - \exp\left(-(\frac{y}{\theta})^\tau\right) ,
\end{eqnarray*}
known as a \textit{Weibull distribution} \vspace{2mm}

\item This result will be handy if you want to \emph{simulate} outcomes from a Weibull distribution in Excel (exponential simulation is easy, Weibull is not available)
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Transforming the Pareto Distribution} This is from KPW
Exercise 5.3. We assume that $X \sim$  Pareto with parameters
$(\alpha, \theta)$ and consider the transformed variable $Y =
X^{1/\tau}$. We wish to determine the df of $Y$ when $\tau$ is
positive, equal to -1, and negative

\bigskip

\textit{Solution.} Begin by recalling the df of the Pareto
\begin{eqnarray*}
\mathrm{F}_X (x) &=& 1 - \left(\frac{\theta}{x+\theta}\right)^{\alpha} .
\end{eqnarray*}

\textit{Case} \textcircled{1}. Assume $\tau > 0$. Then,
\begin{eqnarray*}
\mathrm{F}_Y (y) &=& \Pr (X^{1/\tau} \le y ) = \Pr (X \le y^\tau ) \\
& =& \mathrm{F}_X (y^\tau)\\
&=& 1 - \left(\frac{\theta}{y^\tau+\theta}\right)^{\alpha} .
\end{eqnarray*}
Now, define the new parameter $\theta^{\ast} = \theta^{1/\tau} $ so that $\theta^{\ast \tau} = \theta$. With this notation, we have
\begin{eqnarray*}
\mathrm{F}_Y (y) &=& 1 - \left(\frac{\theta^{\ast \tau}}{y^{\tau}+\theta^{\ast \tau}}\right)^{\alpha} .
\end{eqnarray*}
This is known as a \emph{Burr distribution} with parameters $
(\alpha, \theta^{\ast}, \tau = \gamma)$
\end{frame}

\begin{frame}%[shrink=2]
\frametitle{Exponentiation}
\begin{itemize}\scalefont{0.9}
\item Another type of transformation involves exponentiating a random variable so that $Y=\exp(X)$ %\vspace{2mm}
\item The main example of this is the normal distribution. If $X \sim$ normal, then $Y=e^{X} \sim $ a \textit{lognormal
distribution} %\vspace{2mm}
\item We can develop the distribution of the new random variable through the relation with the df
\begin{eqnarray*}
F_Y (y) = \Pr ( \exp(X) \le y) = \Pr( X \le \ln y) = F_X (\ln y)
 \end{eqnarray*} %\vspace{2mm}
and the pdf
\begin{eqnarray*}
f_Y (y) = \frac{1}{y} f_X (\ln y) .
 \end{eqnarray*} %\vspace{2mm}
\item Remark. This provides a way to simulate a Pareto distribution, by first simulating an exponential random variable and then transforming it
\end{itemize}

\end{frame}

\section{Creating Distributions by Mixing}

\subsection{Motivation}

\begin{frame}[shrink=2]
\frametitle{Motivation for Mixing}
\begin{itemize}
\item In a mixture distribution,  the outcome (random variable) can be thought of as a random draw from a population of
outcomes \vspace{2mm}

\item \textit{Example: Pareto Distribution}. Consider the Pareto distribution with survival function
\begin{eqnarray*}
S(x) = \left(\frac{\theta}{x+\theta}\right)^\alpha,
\end{eqnarray*}
and mean $ \mathrm{E~}X = \frac{\theta}{\alpha-1}$. Let us think
about two types of populations: \vspace{2mm}

$X_1 \sim \text{Pareto} (\alpha_1, \theta_1)$ "Good Driver"

$X_2 \sim \text{Pareto} (\alpha_2, \theta_2)$ "Bad Driver"
\vspace{2mm}

\item Suppose that with probability $a$ we draw the loss from a good driver, $X_1$, and with probability $1-a$ we draw the loss from a bad driver,
$X_2$:
\begin{eqnarray*}
Y =
\begin{cases}
X_1 & \text{with probability~} a\\
X_2 &  \text{with probability~} 1-a
\end{cases}
\end{eqnarray*} \vspace{2mm}

Our interest is in the distribution of $Y$
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Mixing Moments}
\begin{itemize}
\item To begin, focus on the mean. Using the law of total
expectations: \vspace{2mm}

\begin{eqnarray*}
\mathrm{E~}Y = a\mathrm{E~}X_1 + (1-a)\mathrm{E~}X_2 = a \frac{\theta_1}{\alpha_1-1} +  (1-a) \frac{\theta_2}{\alpha_2-1} .
\end{eqnarray*} \vspace{2mm}

We can also write \vspace{2mm}

\begin{eqnarray*}
\hspace{-0.8in}Y^2 =
\begin{cases}
X_{1}^2 & \text{with probability~} a\\
X_{2}^2 &  \text{with probability~} 1-a
\end{cases}
\end{eqnarray*} \vspace{2mm}

Thus, we have \vspace{2mm}

\begin{eqnarray*}
\mathrm{E~}Y^2 = a\mathrm{E~}X_{1}^2 + (1-a)\mathrm{E~}X_{2}^2 .
\end{eqnarray*} \vspace{2mm}

The same argument holds for any moment
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Mixing Distribution Function}
\begin{itemize}
\item For the distribution function, we have \vspace{2mm}
\begin{eqnarray*}
\Pr(Y \le y) &=& \Pr(Y \le y, \text{Good Driver}) + \Pr(Y \le y, \text{Bad Driver})\\
&=& \Pr(X_1 \le y, \text{Good Driver}) + \Pr(X_2 \le y, \text{Bad Driver})\\
&=& \Pr(X_1 \le y)\Pr(\text{Good Driver}) + \Pr(X_2 \le y)\Pr(\text{Bad Driver})\\
&=& a F_{X_1}(y) + (1-a) F_{X_2}(y)
\end{eqnarray*}
\end{itemize} \vspace{4mm}

\textit{Example from Exam M Spring 05 \#34}. Suppose that $a$ = 0.8,
and $X_1 \sim \text{Pareto}(\alpha = 2, \theta = 100), X_2 \sim
\text{Pareto}(\alpha = 4, \theta = 3000)$ \vspace{2mm}

Determine $\Pr(Y \le 200)$
\end{frame}

\subsection{Finite Mixture Distributions}

\begin{frame}%[shrink=2]
\frametitle{Finite Mixture Distributions}
\begin{itemize}\scalefont{0.9}
\item \emph{Definition.} Let $X_1, \ldots, X_k$ be random variables and define
\begin{eqnarray*}
Y =\left\{
\begin{array}{cc}
X_1 & with~probability~a_1 \\
\vdots & \vdots \\
X_k & with~probability~a_k \\
\end{array}
\right.
\end{eqnarray*}
Here, $a_{j}>0$ and $ a_1+ \cdots + a_k = 1.$ Then, $Y$ is a $k$-point mixture random variable. The df is
\begin{eqnarray*}
F_Y(y) = a_1 F_{X_1}(y)+ \cdots +a_k F_{X_k}(y)
\end{eqnarray*}
with mean
\begin{eqnarray*}
\mathrm{E~}Y = a_1 \mathrm{E~}X_1 + \cdots + a_k \mathrm{E~}X_k.
\end{eqnarray*}
\item If $k$ is unknown (but not random), then this a \emph{variable component mixture
distribution} %\vspace{2mm}
\item We can always select one or more of the underlying $X_j$ variables to be degenerate (that is, equal to a number with probability one). In this way, we can use the finite mixture framework to create discrete and mixed distributions
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Exponential Example}
\textit{Example}. Suppose, for a fixed parameter $\theta$, that
$X|\theta \sim$ exponential with parameter $\theta$. Thus,
\begin{equation*}
\Pr(X \le x | \theta) = 1 - e^{-x/\theta}.
\end{equation*} \vspace{2mm}

Now, think of two populations, each having an exponential
distribution, but with different parameter values. For concreteness,
assume
\begin{equation*}
 \Theta =
\begin{cases}
10 &  \text{with prob~}\alpha\\
200 & \text{with prob~}(1-\alpha)\\
\end{cases}
\end{equation*} \vspace{2mm}

As with the $x$'s, we use an upper case $\Theta$ for a random
variable and a lower case $\theta$ for a realization of the random
variable
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Exponential Example II}
\begin{itemize}
\item Using the discrete mixing framework, we may write the df of $X$ as
\begin{equation*}
\Pr(X \le x) = \alpha(1 - e^{-x/10}) + (1-\alpha)(1-e^{-x/200}) .
\end{equation*} \vspace{2mm}

\item More generally, we can consider $k$ populations, each with the same
form of the distribution function $\mathrm{F}(\cdot|\theta)$ and
allow $\theta$ to vary as
\[
\Theta=\left\{
\begin{array}{ll}
\theta_1  & \text{prob~} \alpha_1  \\
\theta_2  & \text{prob~} \alpha_2  \\
\vdots & \vdots \\
\theta_K & \text{prob~} \alpha_K \\
\end{array}
\right.
\] \vspace{2mm}

This is our \textit{finite mixture} distribution
\end{itemize}
\end{frame}

\subsection{Continuous Mixtures}

\begin{frame}[shrink=2]
\frametitle{Continuous Mixtures}
\begin{itemize}
\item Extend this idea by thinking about an infinite number of
populations, each with a conditional distribution function that has
the same structure $\mathrm{F}(\cdot|\theta)$ (e.g., exponential)
but with a parameter $\theta$ that accounts for population
differences \vspace{2mm}

\item Assume that the random variable $\Theta$ has pdf $f_\Theta
(\theta)$ \vspace{2mm}

\item Then, the df is:
\begin{eqnarray*}
\mathrm{F}_X(x) = \Pr(X \le x) &=& E_{\Theta} \Pr(X \le x|\Theta)\\
&=& \int \Pr(X \le x|\theta)f_{\Theta}(\theta) d\theta =\int
\mathrm{F}(x|\theta)f_{\Theta}(\theta) d\theta
\end{eqnarray*} \vspace{2mm}

\item The pdf is:
\begin{eqnarray*}
f_X (x) &=& \int f_{x | \theta}(x) f_{\Theta}(\theta) d\theta
\end{eqnarray*}
\end{itemize}
\end{frame}

\begin{frame}%[shrink=2]
\frametitle{Special Case: Gamma Mixtures of Exponentials}
\begin{itemize}\scalefont{0.9}
\item \textit{Special case: Gamma Mixtures of Exponentials}. Suppose that each population has an exponential distribution with parameter $1/\theta$, that is, $X|\theta \sim \text{exponential}(\frac{1}{\theta})$:
\begin{eqnarray*}
f_{X|\theta}(x) &=& \theta e^{-\theta x}
\end{eqnarray*} %\vspace{2mm}
\item Suppose that the distribution of population parameters is governed by a gamma distribution such that $\Theta \sim \text{gamma}(\alpha, \beta)$ %\vspace{2mm}
\begin{eqnarray*}
f_{\theta}(\theta) &=&
\frac{1}{\Gamma(\alpha)\beta^{\alpha}}\theta^{\alpha-1}e^{-\theta/\beta}
\end{eqnarray*} %\vspace{2mm}

\item The pdf of $X$ is
\begin{eqnarray*}
f_X (x) &=&\int f_{x | \theta}(x) f_{\Theta}(\theta) d\theta \\
&=& \frac{1}{\Gamma(\alpha)\beta^{\alpha}}\int_0 ^{\infty}
\theta^{\alpha} e^{{-\theta}(x+1/\beta)}d\theta = \frac{\alpha
\beta}{(1+x\beta)^{\alpha+1}}
\end{eqnarray*} %\vspace{2mm}
\item We recognize this as a Pareto distribution with parameters $\alpha$ and $\theta=1/\beta $
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Mixture Expectations}
\begin{itemize}
\item For some mixtures such as the above example, we can compute the
mixture distribution in closed-form \vspace{2mm}

\item However, it is often helpful to
just consider the moments. For the mean function, using the
\textit{law of iterated expectations}, we have
\begin{eqnarray*}
\mathrm{E~}X &=& \mathrm{E}_{\Theta}[\mathrm{E}(X|\Theta)]
\end{eqnarray*} \vspace{2mm}

\item This is easily extended to the $k$th moment
\begin{eqnarray*}
\mathrm{E~}X^k &=& \mathrm{E}_{\Theta}[\mathrm{E}(X^k|\Theta)]
\end{eqnarray*}
\end{itemize}
\end{frame}

\bigskip
\begin{frame}[shrink=2]
\frametitle{Mixture Expectations Example}
\textit{Example. Gamma Mixtures of Exponentials}.
\begin{itemize}
\item Assume that $X|\theta \sim$ exponential with parameter $(\frac{1}{\theta})$. Thus, the mean is $\mathrm{E}(X|\theta) =
1/\theta$, the second raw moment is $\mathrm{E}(X^2|\theta) =
2/\theta^2$, and the variance is $\mathrm{Var}(X|\theta) = 1/
\theta^2$ \vspace{2mm}

\item For the parameter distribution, we have $\theta \sim \text{gamma}(\alpha,
\beta)$ \vspace{2mm}

\item One can check that
\begin{eqnarray*}
\mathrm{E~}X = \frac{1}{\beta(\alpha-1)}
\end{eqnarray*}
and
\begin{eqnarray*}
\mathrm{Var~}X = \frac{\alpha}{\beta^2(\alpha-1)^2(\alpha-2)}
\end{eqnarray*} \vspace{2mm}

\item This is consistent with a Pareto distribution with parameters $\alpha$ and $\theta=1/\beta $ (good practice to check)
\end{itemize}
\end{frame}

\subsection{Splicing}

\begin{frame}[shrink=2]
\frametitle{Splicing}
\begin{itemize}
\item Join (splice) together different probability density functions to form a pdf over the support of a random
variable \vspace{2mm}
\[
f_X (x)=\left\{
\begin{array}{ll}
\alpha_1 f_1 (x) & c_0  < x < c_1  \\
\alpha_2 f_2 (x) & c_1  < x < c_2  \\
\vdots & \vdots \\
\alpha_{k}f_{k}(x) & c_{k-1} < x < c_{k}  \\%
\end{array}%
\right.
\]% \vspace{2mm}

$\alpha_1  + \alpha_2  \cdots + \alpha_{k} = 1$\\ \vspace{2mm}

Each $f_j$ is a pdf, so that $\int_{c_{j-1}}^{c_{j}}f_{j}(x)dx =
1$\\ \vspace{2mm}

$c_{j}$'s are typically known\\ \vspace{2mm}

\item \textit{Example: Life Contingencies}.
\begin{itemize}\item It is common to use an exponential distribution in the early ages, e.g., from $x=5$ to $x=40$. The exponential has a constant hazard rate and is well suited to model mortality from
accidents \vspace{2mm}

\item Beginning at age $x=40$, one use another mortality law, e.g., Gompertz, that reflects mortality that increases with age $x$
\end{itemize}\end{itemize}
\end{frame}

\section{Risk Retention -- Deductibles and Limits}

\begin{frame}[shrink=2]
\frametitle{Risk Retention Framework}
\begin{itemize}
\item Now consider the following framework: \vspace{2mm}

\begin{itemize}
\item The policyholder or insured suffers a loss in the amount $X$ \vspace{2mm}

\item Under the insurance contract, the insurer is obligated to covered a portion of this
amount \vspace{2mm}

\item The insurer may have entered into a separate contract with a reinsurer that relieves the insurer of a portion of its
obligations \vspace{2mm}
\end{itemize}

\item This section introduces standard mechanisms that insurers use to reduce, or mitigate, their risk, including deductibles and policy
limits \vspace{2mm}

\item Further, we examine how the distribution of the insurers obligations depends on these mechanisms
\end{itemize}
\end{frame}

\begin{frame}%[shrink=2]
\frametitle{Risk Retention Function}
\begin{itemize}\scalefont{0.8}
\item Recall that $X$ represents the amount of an insurable loss and use $Y$ to represent the insurer's
obligation %\vspace{2mm}
\item There is a known function $g(\cdot)$ that maps the amount insured to the amount retained by the insurer, that is, $Y=g(X)$ %\vspace{2mm}
\item \textbf{Special Case 1. Deductible (d)} %\vspace{2mm}
\begin{equation*}
g(x) = (x-d)_+ =
\begin{cases}
0 & x \le d\\
x - d & x > d .
\end{cases}
\end{equation*}
The notation ``$(\cdot)_+$'' means ``take the positive part of.''
$Y=g(X)$ as the loss in excess of the deductible $d$ \vspace{2mm}
\item \textbf{Special Case 2. Limit (u)} %\vspace{2mm}
\begin{equation*}
g(x) = x \wedge u =
\begin{cases}
x & x \le u\\
u & x > u .
\end{cases}
\end{equation*}
The notation ``$\wedge$'' means ``take the minimum of.'' In this case, the insurance only pays up to a specified limit $u$. The random variable $Y= X \wedge u = \min(X,u)$ is the claim paid %\vspace{2mm}
\item \textbf{Special Case 3. Coinsurance}. Define $Y = c X$. Typically, $0<c<1$, and so represents the proportion of claims retained by the insurer
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Risk Retention Function II}
\begin{itemize}
\item One handy way of combining the three special cases is through the expression
\begin{equation*}
g(x) =
\begin{cases}
0      & x \le d\\
c(x-d) & d \le x < u \\
c(u-d) & x \ge u .
\end{cases}
\end{equation*} \vspace{2mm}

\item Think about these as parameters in a contract between a policyholder and an insurer and so represent ``modifications'' of the underlying
contract \vspace{2mm}

\item Also interpret the risk retention function as the result from a reinsurance
contract \vspace{2mm}

\begin{itemize}
\item For example, it is common in such a contract for an insurer to retain 50\% of each risk and ``cede'' 50\% to the reinsurer
\end{itemize}
\end{itemize}
\end{frame}

\begin{frame}%[shrink=2]
\frametitle{Information Set for Deductibles}
\begin{itemize}\scalefont{0.9}
\item Specify what type of information, sometimes known as the ``information set,'' that is available to the
insurer %\vspace{2mm}
\item \textbf{Special Case 4. Policyholder Deductible}. Define:
\begin{equation*}
g_P(x) =
\begin{cases}
\text{undefined/not observed} & x \le d\\
x - d & x > d
\end{cases}
\end{equation*} %\vspace{2mm}
\item The insurance only pays amounts in excess of the deductible $d$. If the loss is less than the deductible, then the insurer does not observe the loss. The random variable $Y^P = g(X)$ is the claim that an insurer
observes %\vspace{2mm}
\item We have placed a ``$P$'' subscript to remind ourselves that the retained loss is on what is sometimes known as a ``per payment'' basis %\vspace{2mm}
\item In statistical terms, this retained loss is \emph{truncated} in the sense that values of $X$ below $d$ are not
observed %\vspace{2mm}
 \item To distinguish this from the other case where a zero is observed for losses $X<d$, the terminology \textbf{per loss} is used.  Some sources use the notation $Y^L = (X-d)_+$ for the loss amount on a \textbf{per loss} basis
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Distributions of Retained Risks - Deductible}
\begin{itemize}
\item Consider two types of \textit{ordinary deductible}: \vspace{2mm}

\begin{enumerate}
\item Cost (amount of payment) per loss event
  \begin{eqnarray*}
Y^L=(X-d)_+&=&
\left \{
\begin{array}{cc}
0 & X<d\\
X-d & X\ge d\\
\end{array}
\right.\text{(a censored rv)}
  \end{eqnarray*} \vspace{2mm}

\item Cost (amount of payment) per payment event
  \begin{eqnarray*}
Y^P &=&
\left \{
\begin{array}{cc}
undefined & X<d\\
X-d & x\ge d\\
\end{array}
\right. \text{(a truncated rv)}
  \end{eqnarray*}
\end{enumerate}
\end{itemize} \vspace{2mm}

\textit{Example. Exponential Distribution.} Suppose that the loss
$X$ has distribution function $F(x) = 1-\exp{(-x/1000)}$. Compute
the distribution function and pdf for $Y^L$ and $Y^P$ with $d=250$
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Pareto Per Payment Deductible}
\begin{itemize}
\item Assume a deductible $d = 1,000$ \vspace{2mm}

Then, the claim amount on a per payment basis is
\begin{equation*}
Y^{P} =
\begin{cases}
\text{undefined/not observed} & X < 1000\\
1000 & X \ge 1000
\end{cases}
\end{equation*} \vspace{2mm}

\item Identify the distribution of $Y^P$
\end{itemize}
\end{frame}

\subsection{Expectations of Retained Risks}

\begin{frame}[shrink=2]
\frametitle{Limited Expected Value}
\begin{itemize}
\item Use a generic ``$u$'' for the upper limit. To compute the expected value of the limited loss variable $\min(X, u)$, we have
\begin{eqnarray*}
\mathrm{E~}\min(X, u) = \int_0^u \left(1-F(x)\right) dx =  \int_0^u S(x)dx .
\end{eqnarray*}

\textit{Pareto Policy Limit}. Recall
\begin{equation*}
1-F(x)=  S(x) = \Pr(X > x) = \left(\frac{\theta}{x + \theta}\right)^{\alpha}
\end{equation*}
with mean $\mathrm{E~}(X) = \frac{\theta}{\alpha - 1}$. Thus, the limited expected value is
\begin{eqnarray*}
\mathrm{E~}\min(X, u)  &=& \theta^{\alpha}\int_0^u (x + \theta)^{-\alpha} dx =\theta^{\alpha} \left.\frac{(x + \theta)^{-\alpha+1}}{-\alpha + 1}\right|_0^u\\
&=& \theta^{\alpha}\left(\frac{\theta^{-\alpha+1} - (u+\theta)^{-\alpha+1}}{\alpha - 1}\right)\\
&=& \frac{\theta}{\alpha-1}\left\{1 - \left(\frac{\theta}{u + \theta}\right)^{\alpha-1} \right\}.
\end{eqnarray*}
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Pareto Deductible}
\begin{itemize}
\item The claim amount on a ``per loss'' basis is $Y^L = (X-d)_+$ for a deductible
$d$ \vspace{2mm}

\item To calculate $\mathrm{E~} (X-d)_+$, we can use the relation, $X \wedge d + (X - d)_{+} =
X$ \vspace{2mm}

\item For the Pareto distribution, recall $\mathrm{E~}X = \frac{\theta}{\alpha - 1}$ and
\begin{eqnarray*}
\mathrm{E~}\min(X, d)  &=&  \frac{\theta}{\alpha-1}\left\{1 - \left(\frac{\theta}{d + \theta}\right)^{\alpha-1} \right\}.
\end{eqnarray*} \vspace{2mm}

Thus,
\begin{eqnarray*}
\mathrm{E~}(X-d)_+ &=& \mathrm{E~} X -
\mathrm{E~}\min(X, d)  =  \frac{\theta}{\alpha-1} -\frac{\theta}{\alpha-1}\left\{1 - \left(\frac{\theta}{d + \theta}\right)^{\alpha-1} \right\} \\
&=& \frac{\theta}{\alpha-1} \left\{\left(\frac{\theta}{d + \theta}\right)^{\alpha-1} \right\} .
\end{eqnarray*}
\end{itemize}
\end{frame}

\begin{frame}%[shrink=2]
\frametitle{Mean Residual Life}
\begin{itemize}\scalefont{0.9}
\item For the ``per payment'' random variable associated with the policyholder deductible case,
\begin{equation*}
g_P(x) =
\begin{cases}
\text{undefined/not observed} & x \le d\\
x - d & x > d
\end{cases}
\end{equation*}
we can calculate the expectation as
\begin{eqnarray*}
e_X(d) &=& e(d) = \mathrm{E~}(X - d|X > d)
\end{eqnarray*} %\vspace{2mm}
\item Here, $e_X(d)$ is known as the \textit{mean residual life} %\vspace{2mm}
\item We can write this as
\begin{eqnarray*}
e(d) &=&  \mathrm{E~}(X - d|X > d)\\
&=& \frac{\int_d^{\infty} (x-d) f(x)dx}{S(d)} = \frac{\mathrm{E~}(X - d)_+}{S(d)}
\end{eqnarray*} %\vspace{2mm}
Thus,
\begin{eqnarray*}
e(d) &=& \frac{\int_d^{\infty} S(x)dx}{S(d)}
\end{eqnarray*}
\end{itemize}
\end{frame}

\begin{frame}[shrink=2]
\frametitle{Example} \textit{Example. Exam M Fall 2005, Exercise
26.} For an insurance:

\begin{enumerate}
\item Losses have density function
\begin{eqnarray*}
f_X(x)&=& \left \{
\begin{array}{cc}
0.02x & 0<x<10\\
0 & \text{elsewhere}\\
\end{array}
\right.
\end{eqnarray*} \vspace{2mm}

\item The insurance has an ordinary deductible of $4$ per loss \vspace{2mm}

\item $Y^P$ is the claim payment per payment random variable \vspace{2mm}

\end{enumerate}
Calculate $\mathrm{E~}[Y^P]$\\
\end{frame}

\begin{frame}%[shrink=2]
\frametitle{Summary of Limited Loss Variables}
\scalefont{0.8}
\[
\begin{tabular}{ll}
\hline
\textbf{Random Variable} & \textbf{Expectation} \\ \hline
\emph{Excess loss random variable} & $e_{X}(d)=\mathrm{E}~Y=\mathrm{E}%
(X-d|X>d)$ \\
$Y=X-d$ if $X>d$ & mean excess loss function \\
left \emph{truncated} & mean residual life function \\
& complete expectation of life \\
& $e_{X}^{k}(d)=\mathrm{E}\left[ \left( X-d\right) ^{k}|X>d\right] $ \\
\hline
$\left( X-d\right) _{+}=\left\{
\begin{array}{ll}
0 & X<d \\
X-d & X\geq d
\end{array}
\right. $ & $\mathrm{E~}\left( X-d\right) _{+}=e(d)S(d)$ \\
left-\emph{censored} and shifted variable & $\mathrm{E~}\left(
X-d\right) _{+}^{k}=e^{k}(d)S(d)$ \\ \hline
$\mathrm{min}(X,d)=X\wedge d=\left\{
\begin{array}{ll}
X & X<d \\
d & X\geq d
\end{array}
\right. $ \  & $\mathrm{E}\left( X\wedge d\right) -$ limited expected value
\\ \emph{limited loss variable} - right \emph{censored} &
\\ \hline
\end{tabular}
\]

Note that $\left( X-d\right) _{+}+\left( X\wedge d\right) =X.$ \
Thus, $ \mathrm{E~}\left( X-d\right) _{+}+\mathrm{E}\left( X\wedge
d\right) =\mathrm{E}~X$ %\vspace{2mm}

For nonnegative, continuous random variables,
\begin{equation*}
\mathrm{E}\left( X \wedge d\right) =\int_{0}^{d} S\left( x\right)
dx\text{ \ \ and \ \ \ }\mathrm{E}( X-d) _{+} = \int_{d}^{\infty
}S\left( x \right) dx
\end{equation*}
\end{frame}

\subsection{Loss Elimination Ratio (LER)}

\begin{frame}[shrink=2]
\frametitle{Loss Elimination Ratio (LER)}
\begin{itemize}
\item Consider an ordinary deductible, cost (amount of payment) per loss event
\begin{eqnarray*}
LER&=&\frac{\mathrm{E~}X - (\mathrm{E~}X-\mathrm{E~}(X\wedge
d))}{\mathrm{E~}X}
=\frac{\mathrm{E~}(X\wedge d)}{\mathrm{E~}X}\\
&=&\frac{\text{limited exp value}}{\text{exp value}}
\end{eqnarray*}
What fraction of losses have been eliminated by introducing the deductible?\\
\end{itemize}

\textit{Example.} Losses have a lognormal distribution with $\mu=6$
and $\sigma=2$. There is a deductible of 2,000, and 10 losses are
expected each year \vspace{2mm}

Determine the loss elimination ratio

\end{frame}

\end{document}

\begin{frame}[shrink=2]
\frametitle{Instructor Notes}
\begin{itemize}
\item
\end{itemize}
\end{frame}

\textcolor{blue}{temp}


\subsection{Bertrand competition}

Each player decides what price to sell at.

Firms who price above the lowest have no sales. Prices converge to cost.


%**************************************************************
% Lab 11: Processor
%**************************************************************
\chapter{Processor}

\section{Purpose}

A \acf{CPU} is arguably one of the most important digital logic devices. \acp{CPU} are found in all computers and many other embedded logic devices. They are versatile circuits that can be used to control many processes and peripheral devices. The purpose of this lab is to lay the foundation of \ac{CPU} operation.

\subsection{A Definition}

When asked to define ``\ac{CPU}'' many students offer poetic definitions like ``it is the brain of the computer.'' This may be somewhat artistic but is not very helpful in defining \ac{CPU} for digital logic purposes. Here is a much better definition:

\begin{quote}
	A \acf{CPU} is a hardware device that is designed to translate binary codes stored in software into signals that control hardware. Thus, a \ac{CPU} is the interface between software and hardware.
\end{quote}

The purpose of this lab is to demonstrate how binary codes can be used to manipulate hardware devices, like registers and adders, to move data through a circuit and accomplish a purpose. While the circuit developed in this lab is not a practical start for a \ac{CPU} is does serve as an introduction to the concept of hardware manipulation by software codes.

\section{Procedure}

This processor contains only three subcircuits connected by several bus lines and each of the three subcircuits are reasonably simple to understand.

\subsection{Arithmetic-Logic Unit}

This processor starts with a simple \ac{ALU}, as in Figure \ref{fig:11-01}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/11-01}
	\caption{Simple ALU}
	\label{fig:11-01}
\end{figure}

To be sure, this \ac{ALU} is not very complex but uses the same principles developed in Lab \ref{lab04}, \nameref{lab04}. It contains only three arithmetic functions, increment, add, and negate; four logic functions, \texttt{AND}, \texttt{OR}, \texttt{XOR}, \texttt{NOT}; and one constant zero output. There are two data input ports but note that some of the functions only use the lower input, and one output port. The multiplexer determines which of the functions will be connected to the output and that is controlled by a signal named \textit{ALUCtl}.

The \ac{ALU} is then expanded somewhat to make it usable in a \ac{CPU}. For simplicity, Figure \ref{fig:11-02} shows only the left side of the ALU.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/11-02}
	\caption{Left Side of ALU}
	\label{fig:11-02}
\end{figure}

Figure \ref{fig:11-03} shows the right side of the ALU.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/11-03}
	\caption{Full ALU}
	\label{fig:11-03}
\end{figure}

The simple \ac{ALU} functions are found in the center of Figure \ref{fig:11-02}. However, what started as \textit{DataInA} has been replaced by a register named \textit{ALUBuffer}.\footnote{IMPORTANT NOTE: All registers in this Processor circuit are triggered on the Falling Edge of the clock. The reason for this will become evident when the circuit is tested.} The \textit{ALUBuffer's} inputs are from Tunnels (\textit{Wiring} library) because those inputs are used in more than one location in the subcircuit.\footnote{Tunnels are used extensively in this circuit to simplify the diagrams and aid in tracing signals.}

The \ac{ALU} output is routed through a register named \textit{Acc}, for \textit{Accumulator}, which is the commonly-used name for the \ac{ALU} output in a \ac{CPU} circuit.

On the left side of the subcircuit are the three input ports. \textit{DataIn} is an eight-bit number that is sent to both the \textit{ALUBuffer} and the lower \textit{DataIn} bus. The \textit{ALUCtl} signal is split into two components. Bits 0-2 are sent to the multiplexer to select which of the eight functions will be output. Bit 3 of the \textit{ALUCtl} signal is sent to the \textit{AccEna} tunnel and when that is high the \textit{Acc} register will be enabled but when that signal is low then the \textit{ALUBuffer} register will be enabled. Finally, the clock input is sent to both registers.

\subsection{General Registers}

A \ac{CPU} must have several general registers available to hold data temporarily while an instruction is being carried out. For example, it may be necessary to hold the \textit{Acc} output until it is needed in a later step so that value can be stored in a register and then recovered when needed.

The processor circuit being built in this lab has four general registers. Figure \ref{fig:11-04} illustrates the \lstinline[columns=fixed]|GenReg| subcircuit.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/11-04}
	\caption{General Registers}
	\label{fig:11-04}
\end{figure}

The \lstinline[columns=fixed]|GenReg| subcircuit does not require any novel digital logic concepts. Starting on the left side of the circuit:

\begin{itemize}
	\item \textit{DataIn} is connected to the data bus and is the main input port for the registers. Note that \textit{DataIn} is connected to the \textit{Data} port on all four registers.
	\item The register that actually stores the input data is determined by the Decoder (\textit{Plexers} library) in the lower left corner of the subcircuit. The two low-order bits from the \textit{RegSel} signal activate one of the output lines from the Decoder and that line is tied to the Write Enable port of the register. On the next clock pulse that register will lock in the data present on the \textit{DataIn} port.
	\item The outputs from all of the registers are wired to a Multiplexer (\textit{Plexers} library). The select bits from the Decoder that are used to select the storage register are also used to select the register output line which is, in turn, wired to the \textit{DataOut} port.
	\item The high-order bit from the \textit{RegSel} control signal is used to determine if data are stored to or read from a register. When that bit is high the decoder is active and will select a storage register but when that bit is low the output multiplexer will be activated and send a register's stored value to the output port.
\end{itemize}

\subsection{Control}

The \lstinline[columns=fixed]|Control| subcircuit in this device is very simple and could, in all actuality, be eliminated. However, in a true \ac{CPU} the \lstinline[columns=fixed]|Control| subcircuit is rather complex and critical to the operation of the circuit so a \lstinline[columns=fixed]|Control| subcircuit is included in this lab as an example. Figure \ref{fig:11-05} illustrates the \lstinline[columns=fixed]|Control| subcircuit.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/11-05}
	\caption{Control Subcircuit}
	\label{fig:11-05}
\end{figure}

The \lstinline[columns=fixed]|Control| subcircuit includes a nine-bit input named \textit{mCode} (for ``Microcode''). That input is latched by a register\footnote{Note, as an exception to the other registers in the Processor circuit, the register in the control subcircuit must be set to trigger on the leading edge of the clock rather than the falling edge.} and the output of that register is split into three components.

\begin{description}
	\item[Bits 0-3] These are the \ac{ALU} control bits and they are sent to the \lstinline[columns=fixed]|ALU| subcircuit.
	\item[Bits 4-6] These are the register control bits and are sent to that subcircuit.
	\item[Bits 7-8] These are the \textit{dBus} (``Data Bus'') control bits. The data bus is found in the \lstinline[columns=fixed]|main| circuit and carries the data to each of the subcircuits. The dBus control is just a multiplexer that controls which subcircuit's output has control of the data bus.
\end{description}

\subsection{Main}

The \lstinline[columns=fixed]|main| circuit ties the three subcircuits together with three control busses and one data bus. Figure \ref{fig:11-06} illustrates the \lstinline[columns=fixed]|main| circuit.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/11-06}
	\caption{Main Circuit}
	\label{fig:11-06}
\end{figure}

There are no novel digital logic functions used in this circuit. The first input is \textit{mCode} which is the microcode used to control the flow of data in the dBus (``data bus''). the other input, \textit{LdImm} (``Load Immediate'') can contain an eight-bit number that is to be loaded into one of the registers for processing. In a full \ac{CPU} that input would be wired to a \ac{RAM} device.

\subsection{Testing the Circuit}

The circuit should be tested by inputting these signals and observing the output.

\subsubsection{Copy LdImm To R0}
%Checked
Enter some value in the \textit{LdImm} input port, set the \textit{mCode} input to 101000000 (the first three values in the table below), and then pulse the \textit{clk}. When completed, the \textit{dBus} and \textit{R0} should both contain the value of the \textit{LdImm} port.

\begin{table}[H]
	\sffamily
	\newcommand{\head}[1]{\textcolor{white}{\textbf{#1}}}
	\begin{center}
		\rowcolors{2}{gray!10}{white} % Color every other line a light gray
		\begin{tabular}{ccccl}
			\textbf{dBus} & \textbf{Reg} & \textbf{ALU} & \textbf{dBus} & \textbf{Notes} \\
			10 & 100 & 0000 & LdImm & R0 <- LdImm \\
		\end{tabular}
	\end{center}
	\caption{R0 <- LdImm}
	\label{tab:11-01}
\end{table}

\subsubsection{Copy LdImm To R1}
% Checked
Enter some value in the \textit{LdImm} input port, set the \textit{mCode} input to 101010000 (the first three values in the table below), and then pulse the \textit{clk}. When completed, the \textit{dBus} and \textit{R1} should both contain the value of the \textit{LdImm} port.

\begin{table}[H]
	\sffamily
	\newcommand{\head}[1]{\textcolor{white}{\textbf{#1}}}
	\begin{center}
		\rowcolors{2}{gray!10}{white} % Color every other line a light gray
		\begin{tabular}{ccccl}
			\textbf{dBus} & \textbf{Reg} & \textbf{ALU} & \textbf{dBus} & \textbf{Notes} \\
			10 & 101 & 0000 & LdImm & R1 <- LdImm
		\end{tabular}
	\end{center}
	\caption{R1 <- LdImm}
	\label{tab:11-02}
\end{table}

\subsubsection{Copy LdImm To ALUbuf}
%Checked
Enter some value in the \textit{LdImm} input port, set the \textit{mCode} input to 100000000 (the first three values in the table below), and then pulse the \textit{clk}. When completed, the \textit{dBus} and \textit{ALUbuf} should both contain the value of the \textit{LdImm} port.

\begin{table}[H]
	\sffamily
	\newcommand{\head}[1]{\textcolor{white}{\textbf{#1}}}
	\begin{center}
		\rowcolors{2}{gray!10}{white} % Color every other line a light gray
		\begin{tabular}{ccccl}
			\textbf{dBus} & \textbf{Reg} & \textbf{ALU} & \textbf{dBus} & \textbf{Notes} \\
			10 & 000 & 0000 & LdImm & ALU <- LdImm
		\end{tabular}
	\end{center}
	\caption{ALU <- LdImm}
	\label{tab:11-03}
\end{table}

\subsubsection{Increment R0}

\marginpar{Use the LdImm function to initialize R0.}Incrementing the value in R0 requires two steps. Set the \textit{mCode} input to the first three values in the table below and pulse the \textit{clk} for each of the steps. When completed, \textit{R0} will contain the original value of the \textit{R0}$ +1 $.

\begin{table}[H]
	\sffamily
	\newcommand{\head}[1]{\textcolor{white}{\textbf{#1}}}
	\begin{center}
		\rowcolors{2}{gray!10}{white} % Color every other line a light gray
		\begin{tabular}{ccccl}
			\textbf{dBus} & \textbf{Reg} & \textbf{ALU} & \textbf{dBus} & \textbf{Notes} \\
			01 & 000 & 1000 & R0 & Acc <- R0+1 \\
			00 & 100 & 0000 & Acc & R0 <- Acc \\
		\end{tabular}
	\end{center}
	\caption{R0 <- Inc(R0)}
	\label{tab:11-04}
\end{table}

\subsubsection{Add R0 And R1, Store In R0}

\marginpar{Use the LdImm function to initialize R0 and R1.}Adding the values of \textit{R0} and \textit{R1} and storing the result in \textit{R0} requires three steps. Set the \textit{mCode} input to the first three values in the table below and pulse the \textit{clk} for each of the steps. When completed, the sum of the original values of \textit{R0} and \textit{R1} will be stored in \textit{R0}.

\begin{table}[H]
	\sffamily
	\newcommand{\head}[1]{\textcolor{white}{\textbf{#1}}}
	\begin{center}
		\rowcolors{2}{gray!10}{white} % Color every other line a light gray
		\begin{tabular}{ccccl}
			\textbf{dBus} & \textbf{Reg} & \textbf{ALU} & \textbf{dBus} & \textbf{Notes} \\
			01 & 001 & 0001 & R1 & ALU <- R1 \\
			01 & 000 & 1001 & R0 & Acc <- R0 + R1 \\
			00 & 100 & 0001 & Acc & R0 <- Acc
		\end{tabular}
	\end{center}
	\caption{R0 <- R0 + R1}
	\label{tab:11-05}
\end{table}

\subsubsection{Subtract R1 From R0, Store In R0}

\marginpar{Use the LdImm function to initialize R0 and R1.}Subtracting the value of \textit{R1} from \textit{R0} and storing the result in \textit{R0} requires four steps. Set the \textit{mCode} input to the first three values in the table below and pulse the \textit{clk} for each of the steps. When completed, the difference of the original values of \textit{R0} and \textit{R1} will be stored in \textit{R0}.

\begin{table}[H]
	\sffamily
	\newcommand{\head}[1]{\textcolor{white}{\textbf{#1}}}
	\begin{center}
		\rowcolors{2}{gray!10}{white} % Color every other line a light gray
		\begin{tabular}{ccccl}
			\textbf{dBus} & \textbf{Reg} & \textbf{ALU} & \textbf{dBus} & \textbf{Notes} \\
			01 & 000 & 0010 & R0 & ALUbuf <- R0 \\
			01 & 001 & 1010 & R1 & Acc <- \textasciitilde R1 \\
			00 & 100 & 1001 & R0-R1 & dBus <- Acc \\
			00 & 100 & 0111 & dBus+1 & R0 <- R0 - R1
		\end{tabular}
	\end{center}
	\caption{R0 <- R0 - R1}
	\label{tab:11-06}
\end{table}

\subsubsection{Copy R0 to R1}

\marginpar{Use the LdImm function to initialize R0.}Copying the value of \textit{R0} to \textit{R1} requires four steps. Set the \textit{mCode} input to the first three values in the table below and pulse the \textit{clk} for each of the steps. When completed, the value of \textit{R0} will be stored in \textit{R1}.

\begin{table}[H]
	\sffamily
	\newcommand{\head}[1]{\textcolor{white}{\textbf{#1}}}
	\begin{center}
		\rowcolors{2}{gray!10}{white} % Color every other line a light gray
		\begin{tabular}{ccccl}
			\textbf{dBus} & \textbf{Reg} & \textbf{ALU} & \textbf{dBus} & \textbf{Notes} \\
			00 & 000 & 1111 & 0 & dBus <- 0 \\
			00 & 000 & 0100 & 0 & ALU <- dBus \\
			01 & 000 & 1100 & Acc & Acc <- ALU OR R0 \\
			00 & 101 & 0111 & Acc & R1 <- Acc
		\end{tabular}
	\end{center}
	\caption{R1 <- R0}
	\label{tab:11-07}
\end{table}

\subsubsection{Swap R0 And R1}

\marginpar{Use the LdImm function to initialize R0 and R1.}Swapping the values of \textit{R0} and \textit{R1} requires 12 steps. Set the \textit{mCode} input to the first three values in the table below and pulse the \textit{clk} for each of the steps. When completed, the values of \textit{R0} and \textit{R1} will exchanged.

\begin{table}[H]
	\sffamily
	\newcommand{\head}[1]{\textcolor{white}{\textbf{#1}}}
	\begin{center}
		\rowcolors{2}{gray!10}{white} % Color every other line a light gray
		\begin{tabular}{ccccl}
			\textbf{dBus} & \textbf{Reg} & \textbf{ALU} & \textbf{dBus} & \textbf{Notes} \\
			00 & 000 & 1111 & 0 & dBus <- 0 (Move R0 to R2)\\
			00 & 000 & 0100 & 0 & ALU <- dBus \\
			01 & 000 & 1100 & Acc & Acc <- ALU OR R0 \\
			00 & 110 & 0111 & Acc & R2 <- Acc \\
 			& & & & \\ %Empty line

			00 & 000 & 1111 & 0 & dBus <- 0 (Move R1 to R0)\\
			00 & 000 & 0100 & 0 & ALU <- dBus \\
			01 & 001 & 1100 & Acc & Acc <- ALU OR R1 \\
			00 & 100 & 0111 & Acc & R0 <- Acc \\
 			& & & & \\ %Empty line

			00 & 000 & 1111 & 0 & dBus <- 0 (Move R2 to R1)\\
			00 & 000 & 0100 & 0 & ALU <- dBus \\
			01 & 010 & 1100 & Acc & Acc <- ALU OR R2 \\
			00 & 101 & 0111 & Acc & R1 <- Acc
		\end{tabular}
	\end{center}
	\caption{R0 <-> R1}
	\label{tab:11-08}
\end{table}

\section{About Programming Languages}

The codes that were input for the last example (swap \textit{R0} and \textit{R1}) would create the following program.

\begin{Verbatim}[frame=lines,
xleftmargin=10mm,
xrightmargin=10mm]
000001111
000000100
010001100
001100111
000001111
000000100
010011100
001000111
000001111
000000100
010101100
001010111
\end{Verbatim}

This group of instructions would be considered ``CPU Microcode,'' which is a very highly specialized form of programming. It is the code that is built into a \ac{CPU} circuit and it determines what gates, registers, and other devices are active for each step of the code. When Intel, AMD, Motorola, or other manufacturers create a new \ac{CPU}, one of their main challenges is creating the microcode that will, for example, ``add the contents of register one to the contents of register two and store the result in register zero.'' The microcode must be able to activate and deactivate various devices within the \ac{CPU} so data appear on the appropriate bus at the right time in order to achieve the objective. Normally, microcode steps must be executed over several clock cycles in order to do a single job. For example, in one clock cycle the contents of register one may be placed on the data bus, the next clock cycle will load that data into the ALU register, and so forth until the entire process is complete.

Microcode is usually stored in \ac{ROM} that is built into the \ac{CPU}. This is typically called ``firmware'' since it is a string of ones and zeros, like software, but it cannot be changed, like hardware.

It is important to keep in mind the difference between instructions contained in a software program (like Word) and those contained in microcode. A single instruction in software is interpreted and executed by the \ac{CPU} using, perhaps, dozens of microcode steps. As an example, the software may want to move a single byte from \ac{RAM} to the video card. The \ac{CPU} may process that instruction by first moving the byte from \ac{RAM} to register one and then moving it from there to the video card's input register and then activating the video card input function. Those moves may require several clock cycles as various multiplexers and other devices are activated in the correct sequence to move the data to its destination.

A software program, like Word, is nothing more than a series of ones and zeros, organized into groups, commonly 64 in modern computers. Each group of bits forms a single ``word'' of information; or a single instruction which would then be used by the \ac{CPU} to trigger a microcode sequence. When viewed at the level of ones and zeros, a software program is said to be in ``machine code,'' and could look something like the following (note, only the first 32 bits of each word are shown).

\begin{Verbatim}[frame=lines,
xleftmargin=10mm,
xrightmargin=10mm]
10010100101100101001101011001010
01101001101011000111101011101011
00011011110010000111010111100101
\end{Verbatim}

If a programmer could master machine code, then those programs would be as concise and efficient as possible since they would be written in machine code the \ac{CPU} can execute directly. Of course, as it is easy to imagine, no one actually writes machine code due to its complexity.

The next level higher than machine code is called ``Assembly'' code. Assembly uses easy-to-remember abbreviations to represent the various \ac{CPU} instructions available; and it looks something like this:

\begin{Verbatim}[frame=lines,
xleftmargin=10mm,
xrightmargin=10mm]
INP
STA FIRST
INP
STA SECOND
LDA FIRST
SUB SECOND
OUT
HLT
FIRST DAT
SECOND DAT
\end{Verbatim}

Once the program has been written in Assembly, it must be ``assembled'' into machine code before it can be executed. An assembler is a fairly simply program that converts a file containing assembly codes into machine codes that can be executed by the \ac{CPU}.

Many programming languages have been developed that are considered ``higher'' than Assembly; for example, C++, Java, and Visual Basic. These languages tend to be easy to master and can enable a programmer to quickly create very complex programs. Programs written in each of these languages must be compiled, or changed into machine code, before they can be executed. Here is an example Java program:

\begin{Verbatim}[frame=lines,
xleftmargin=10mm,
xrightmargin=10mm]
public class HelloWorldExample{
  public static void main(String args[]){
  System.out.println("Hello World !");
  }
}
\end{Verbatim}

In the end, while there are dozens of different programming languages, they are all designed to be reduced into a series of machine codes which the \ac{CPU} can then execute.

\section{Challenge}

Using the examples in the ``Testing the Circuit'' section, create the microcode necessary to carry out these functions:

\begin{enumerate}
	\item Store the value contained in \textit{LdImm} in \textit{R2} (\textit{R2} <- \textit{LdImm}). (Assume that \textit{LdImm} is pre-loaded with the value to store.)

	\item Store the value contained in \textit{LdImm} in \textit{R3} (\textit{R3} <- \textit{LdImm}). (Assume that \textit{LdImm} is pre-loaded with the value to store.)

	\item Store the 2s complement of the value in \textit{R0} back into \textit{R0} (\textit{R0} <- \textasciitilde \textit{R0}). The subtraction example will help with this function.

	\item Store the bitwise NOT of the value in \textit{R0} back into \textit{R0} (\textit{R0} <- \textit{R0'}).
\end{enumerate}

\section{Deliverable}

To receive a grade for this lab, build the Processor circuit and then complete the Challenge. Be sure the standard identifying information is at the top left of the Processor \lstinline{main} circuit, similar to:

\bigskip
% The minipage environment keeps the three lines together - no page break.
\begin{minipage}{\linewidth}
	\begin{verbatim}
	George Self
	Lab 11: Processor
	April 5, 2018
	\end{verbatim}
\end{minipage}
\bigskip

Save the Processor circuit in a file with this name: \textit{Lab11\_Processor}. Complete the code required in the Challenge and store that in a text file with the name \textit{Lab11\_Code.txt}. Submit both files for grading.