\section{More inductive types}

Analogous to the type of natural numbers, many types can be specified as inductive types. In this lecture we introduce some further examples of inductive types: the unit type, the empty type, the booleans, coproducts, dependent pair types, and cartesian products. We also introduce the type of integers.

\subsection{The idea of general inductive types}

Just like the type of natural numbers, other inductive types are also specified by their \emph{constructors}, an \emph{induction principle}, and their \emph{computation rules}:
\begin{enumerate}
\item The constructors tell what structure the inductive type comes equipped with. There may any finite number of constructors, even no constructors at all, in the specification of an inductive type.
\item The induction principle specifies the data that should be provided in order to construct a section of an arbitrary type family over the inductive type.
\item The computation rules assert that the inductively defined section agrees on the constructors with the data that was used to define the section. Thus, there is a computation rule for every constructor.
\end{enumerate}
The induction principle and computation rules can be generated automatically once the constructors are specified, but it goes beyond the scope of our course to describe general inductive types.
%For a more general treatment of inductive types, we refer to Chapter 5 of \cite{hottbook}.

\subsection{The unit type}
A straightforward example of an inductive type is the \emph{unit type}, which has just one constructor.
Its induction principle is analogous to just the base case of induction on the natural numbers.

\begin{defn}
We define the \define{unit type}\index{1@{$\unit$}|see {unit type}}\index{unit type|textbf} to be a closed type $\unit$ equipped with a closed term\index{unit type!star@{$\ttt$}}
\begin{equation*}
\ttt:\unit,
\end{equation*}
satisfying the induction principle\index{induction principle!of unit type} that for any type family of types $P(x)$ indexed by $x:\unit$, there is a term
\begin{equation*}
\indunit : P(\ttt)\to\prd{x:\unit}P(x)
\end{equation*}
for which the computation rule\index{computation rules!of unit type}
\begin{equation*}
\indunit(p,\ttt) \jdeq p
\end{equation*}
holds. Sometimes we write $\lam{\ttt}p$ for $\indunit(p)$.
\end{defn}

The induction principle can also be used to define ordinary functions out of the unit type. Indeed, given a type $A$ we can first weaken it to obtain the constant family over $\unit$, with value $A$. Then the induction principle of the unit type provides a function
\begin{equation*}
  \indunit : A \to (\unit\to A).
\end{equation*}
In other words, by the induction principle for the unit type we obtain for every $x:A$ a function $\mathsf{pt}_x\defeq\indunit(x):\unit\to A$.

\subsection{The empty type}
The empty type is a degenerate example of an inductive type. It does \emph{not} come equipped with any constructors, and therefore there are also no computation rules. The induction principle merely asserts that any type family has a section. In other words: if we assume the empty type has a term, then we can prove anything.

\begin{defn}
We define the \define{empty type}\index{0@{$\emptyt$}|see {empty type}}\index{empty type|textbf} to be a type $\emptyt$ satisfying the induction principle\index{induction principle!of empty type} that for any family of types $P(x)$ indexed by $x:\empty$, there is a term
\begin{equation*}
\indempty : \prd{x:\emptyt}P(x).
\end{equation*}
\end{defn}

The induction principle for the empty type can also be used to construct a function
\begin{equation*}
  \emptyt\to A
\end{equation*}
for any type $A$. Indeed, to obtain this function one first weakens $A$ to obtain the constant family over $\emptyt$ with value $A$, and then the induction principle gives the desired function.

Thus we see that from the empty type anything follows. Therefore, we we see that anything follows from $A$, if we have a function from $A$ to the empty type. This motivates the following definition.

\begin{defn}
  For any type $A$ we define \define{negation} of $A$ by
  \begin{equation*}
    \neg A\defeq A\to\emptyt.
  \end{equation*}
\end{defn}

Since $\neg A$ is the type of functions from $A$ to $\emptyt$, a proof of $\neg A$ is given by assuming that $A$ holds, and then deriving a contradiction. This proof technique is called \define{proof of negation}. Proofs of negation are not to be confused with \emph{proofs by contradiction}. In type theory there is no way of obtaining a term of type $A$ from a term of type $(A\to \emptyt)\to\emptyt$.

\subsection{The booleans}
\begin{defn}
We define the \define{booleans}\index{booleans|textbf}\index{2@{$\bool$}|see {booleans}} to be a type $\bool$ that comes equipped with
\begin{align*}
\bfalse & : \bool \\
\btrue & : \bool
\end{align*}
satisfying the induction principle\index{induction principle!of booleans} that for any family of types $P(x)$ indexed by $x:\bool$, there is a term
\begin{equation*}
\indbool : P(\bfalse)\to \Big(P(\btrue)\to \prd{x:\bool}P(x)\Big)
\end{equation*}
for which the computation rules\index{computation rules!of booleans}
\begin{align*}
\indbool(p_0,p_1,\bfalse) & \jdeq p_0 \\
\indbool(p_0,p_1,\btrue) & \jdeq p_1
\end{align*}
hold.
\end{defn}

Just as in the cases for the unit type and the empty type, the induction principle for the booleans can also be used to construct an ordinary function $\bool\to A$, provided that we can construct two terms of type $A$. Indeed, by the induction principle for the booleans there is a function
\begin{equation*}
  \indbool : A \to (A\to A^\bool)
\end{equation*}
for any type $A$.

\begin{eg}
  Using the induction principle of $\bool$ we can define all the operations of Boolean algebra. For example, the \define{boolean negation} operation $\neg : \bool \to \bool$ is defined by
  \begin{align*}
    \neg\btrue & \defeq \bfalse & \neg\bfalse & \defeq \btrue.
  \end{align*}
  The \define{boolean conjunction} operation $\blank\land\blank : \bool \to (\bool\to \bool)$ is defined by
  \begin{align*}
    \btrue\land\btrue & \defeq \btrue & \bfalse\land\btrue & \defeq \bfalse \\
    \btrue\land\bfalse & \defeq \bfalse & \bfalse\land\bfalse & \defeq \bfalse.
  \end{align*}
  The \define{boolean disjunction} operation $\blank\lor\blank : \bool \to (\bool\to \bool)$ is defined by
  \begin{align*}
    \btrue\lor\btrue & \defeq \btrue & \bfalse\lor\btrue & \defeq \btrue \\
    \btrue\lor\bfalse & \defeq \btrue & \bfalse\lor\bfalse & \defeq \bfalse.
  \end{align*}
  We leave the definitions of some of the other boolean operations as \cref{ex:boolean-operation}. Note that the method of defing the boolean operations by the induction principle of $\bool$ is not that different from defining them by truth tables.

  Boolean logic is important, but it won't be very prominent in this course. The reason is simple: in type theory it is more natural to use the `logic' of types that is provided by the inference rules.
\end{eg}

\subsection{Coproducts and the type of integers}
\begin{defn}
Let $A$ and $B$ be types. We define the \define{coproduct}\index{coproduct}\index{disjoint sum|see {coproduct}} $A+B$\index{plus ($+$)|see {coproduct}} to be a type that comes equipped with
\begin{align*}
\inl & : A \to A+B \\
\inr & : B \to A+B
\end{align*}
satisfying the induction principle\index{induction principle!of coproduct} that for any family of types $P(x)$ indexed by $x:A+B$, there is a term
\begin{equation*}
\ind{+} : \Big(\prd{x:A}P(\inl(x))\Big)\to\Big(\prd{y:B}P(\inr(y))\Big)\to\prd{z:A+B}P(z)
\end{equation*}
for which the computation rules\index{computation rules!of coproduct}
\begin{align*}
\ind{+}(f,g,\inl(x)) & \jdeq f(x) \\
\inr{+}(f,g,\inr(y)) & \jdeq g(y)
\end{align*}
hold. Sometimes we write $[f,g]$ for $\ind{+}(f,g)$.
\end{defn}

The coproduct of two types is sometimes also called the \define{disjoint sum}. By the induction principle of coproducts it follows that we have a function
\begin{equation*}
  (A\to X) \to \big((B\to X) \to (A+B\to X)\big)
\end{equation*}
for any type $X$. Note that this special case of the induction principle of coproducts is very much like the elimination rule of disjunction in first order logic: if $P$, $P'$, and $Q$ are propositions, then we have
\begin{equation*}
  (P\to Q)\to \big((P'\to Q)\to (P\lor P'\to Q)\big).
\end{equation*}
Indeed, we can think of \emph{propositions as types} and of terms as their constructive proofs. Under this interpretation of type theory the coproduct is indeed the disjunction.

An important example of a type that can be defined using coproducts is the type $\Z$ of integers.

\begin{defn}
  We define the \define{integers}\index{integers|see Z@{$\Z$}} to be the type $\Z\defeq\nat+(\unit+\nat)$. The type of integers comes equipped with inclusion functions of the positive and negative integers
  \begin{align*}
    \mathsf{in\usc{}pos} & \defeq \inr\circ\inr \\
    \mathsf{in\usc{}neg} & \defeq \inl,
  \end{align*}
  which are both of type $\N\to\Z$, and the constants
  \begin{align*}
    -1_\Z & \defeq \mathsf{in\usc{}neg}(0)\\
    0_\Z & \defeq \inr(\inl(\ttt))\\
    1_\Z & \defeq \mathsf{in\usc{}pos}(0).
  \end{align*}
\end{defn}

In the following lemma we derive an induction principle\index{induction principle!of Z@{of $\Z$}} for $\Z$, which can be used in many familiar constructions on $\Z$, such as in the definitions of addition and multiplication.

\begin{lem}\label{lem:Z_ind}
  Consider a type family $P$ over $\Z$. If we are given
  \begin{align*}
    p_{-1} & :P(-1_\Z) \\
    p_{-S} & : \prd{n:\N}P(\mathsf{in\usc{}neg}(n))\to P(\mathsf{in\usc{}neg}(\succN(n)))\\
    p_{0} & : P(0_\Z) \\
    P_{1} & : P(1_\Z) \\
    P_{S} & : \prd{n:\N}P(\mathsf{in\usc{}pos}(n))\to P(\mathsf{in\usc{}pos}(\succN(n))),
  \end{align*}
  then we can construct a dependent function $f:\prd{k:\Z}P(k)$ for which the following judgmental equalities hold:
  \begin{align*}
    f(-1_\Z) & \jdeq p_{-1} \\
    f(\mathsf{in\usc{}neg}(\mathsf{succ}_\N(n))) & \jdeq p_{-S}(n,f(\mathsf{in\usc{}neg}(n))) \\
    f(0_\Z) & \jdeq p_{0} \\
    f(1_\Z) & \jdeq p_{1} \\
    f(\mathsf{in\usc{}pos}(\mathsf{succ}_\N(n))) & \jdeq p_S(n,f(\mathsf{in\usc{}pos}(n))).
  \end{align*}
\end{lem}

\begin{proof}
  Since $\Z$ is the coproduct of $\N$ and $\unit+\N$, it suffices to define
  \begin{align*}
    p_{inl} & : \prd{n:\N}P(\inl(n)) \\
    p_{inr} & : \prd{t:\unit+\N}P(\inr(t)).
  \end{align*}
  Note that $\mathsf{in\usc{}neg}\jdeq\inl$ and $-1_\Z\jdeq \mathsf{in\usc{}neg}(\zeroN)$. In order to define $p_{inl}$ we use induction on the natural numbers, so it suffices to define
  \begin{align*}
    p_{-1} & : P(-1) \\
    p_{-S} & : \prd{n:\N} P(\mathsf{in\usc{}neg}(n))\to P(\mathsf{in\usc{}neg}(\succN(n))).
  \end{align*}
  Similarly, we proceed by coproduct induction, followed by induction on $\unit$ in the left case and induction on $\N$ on the right case, in order to define $p_{inr}$.
\end{proof}

As an application we define the successor function on the integers.

\begin{defn}
We define the \define{successor function}\index{successor function!on Z@{on $\Z$}|textbf} on the integers $\mathsf{succ}_\Z:\Z\to\Z$ using the induction principle of \cref{lem:Z_ind}, taking
\begin{align*}
\mathsf{succ}_\Z(-1_\Z) & \defeq 0_\N \\
\mathsf{succ}_\Z(\mathsf{in\usc{}neg}(\mathsf{succ}_\N(n))) & \defeq \mathsf{in\usc{}neg}(n) \\
\mathsf{succ}_\Z(0_\Z) & \defeq 1_\N \\
\mathsf{succ}_\Z(1_\Z) & \defeq \mathsf{in\usc{}pos}(1_\N) \\
\mathsf{succ}_\Z(\mathsf{in\usc{}pos}(\mathsf{succ}_\N(n))) & \defeq \mathsf{in\usc{}pos}(\mathsf{succ}_\N(\mathsf{succ}_\N(n))).
\end{align*}
\end{defn}

\subsection{Dependent pair types}
The \emph{dependent pair type}\index{dependent pair type|see {$\Sigma$-type}} (or $\Sigma$-type) can be thought of as a `type indexed' disjoint sum.
However, this intuition for the dependent pair type can be counterproductive once we start to do homotopy theory in type theory.
It is better to think of the $\Sigma$-type as the total space of a family of types depending continuously on a base type, just like one can have a family of spaces depending continuously on a base space (i.e., a fibration).

\begin{defn}
Let $A$ be a type in context $\Gamma$, and let $\Gamma,x:A\vdash B(x)~\mathrm{type}$ be a type family over $A$.
The \define{dependent pair type}\index{Sigma type@{$\Sigma$-type}|textbf} is defined to be the inductive type $\sm{x:A}B(x)$ in context $\Gamma$ equipped with a \define{pairing function}\index{pairing function}
\begin{equation*}
(\blank,\blank):\prd{x:A} \Big(B(x)\to \sm{y:A}B(y)\Big).
\end{equation*}
The induction principle\index{induction principle|of Sigma types@{of $\Sigma$-types}} for $\sm{x:A}B(x)$ asserts that for any family of types $P(p)$ indexed by $p:\sm{x:A}B(x)$, there is a function
\begin{equation*}
\ind{\Sigma}:\Big(\prd{x:A}{y:B(x)}P(x,y)\Big)\to\Big(\prd{p:\sm{x:A}B(x)}P(p)\Big).
\end{equation*}
satisfying the computation rule\index{computation rules!of Sigma types@{of $\Sigma$-types}}
\begin{equation*}
\ind{\Sigma}(f,(x,y))\jdeq f(x,y).
\end{equation*}
Sometimes we write $\lam{(x,y)}f(x,y)$ for $\ind{\Sigma}(\lam{x}{y}f(x,y))$.
\end{defn}

\begin{defn}
Given a type $A$ and a type family $B$ over $A$, the \define{first projection map}\index{first projection map|textbf}\index{projection maps!first projection|textbf}
\begin{equation*}
\proj 1:\Big(\sm{x:A}B(x)\Big)\to A
\end{equation*}
is defined by induction as
\begin{equation*}
\proj 1\defeq \lam{(x,y)}x.
\end{equation*}
The \define{second projection map}\index{second projection map|textbf}\index{projection map!second projection|textbf} is a dependent function
\begin{equation*}
\proj 2 : \prd{p:\sm{x:A}B(x)} B(\proj 1(p))
\end{equation*}
defined by induction as
\begin{equation*}
\proj 2\defeq \lam{(x,y)}y.
\end{equation*}
By the computation rule we have
\begin{align*}
\proj 1 (x,y) & \jdeq x \\
\proj 2 (x,y) & \jdeq y.
\end{align*}
\end{defn}

\subsection{Cartesian products}
A special case of the $\Sigma$-type occurs when the $B$ is a type in context $\Gamma$ weakened by $A$ (i.e., $B$ is not actually depending on $A$). In this case, a term of $\sm{x:A}B$ is given as a pair consisting of a term of $A$ and a term of $B$. Thus, $\sm{x:A}B$ is the \emph{(cartesian) product} $A\times B$. Since the cartesian product is so common (just like $A\to B$ is a common special case of the dependent product), we provide its specification along with the induction principle for cartesian products.

\begin{defn}
Let $A$ and $B$ be types in context $\Gamma$. The \define{(cartesian) product}\index{cartesian product|textbf}\index{product!of types|textbf} of $A$ and $B$ is defined as the inductive type $A\times B$\index{times ($\times$)|see {cartesian product}} with constructor
\begin{equation*}
(\blank,\blank):A\to (B\to A\times B).
\end{equation*}
The induction principle\index{induction principle!of cartesian products} for $A\times B$ asserts that for any type family $P$ over $A\times B$, one has
\begin{equation*}
\ind{\times} : \Big(\prd{x:A}{y:B}P(a,b)\Big)\to\Big(\prd{p:A\times B} P(p)\Big)
\end{equation*}
satisfying the computation rule\index{computation rules!of cartesian product} that
\begin{align*}
\ind{\times}(f,(x,y)) & \jdeq f(x,y).
\end{align*}
\end{defn}

The projection maps are defined similarly to the projection maps of $\Sigma$-types. When one thinks of types as propositions\index{propositions as types!conjunction}, then $A\times B$ is interpreted as the conjunction of $A$ and $B$.

\begin{comment}
\subsection{Overview of the inductive types}
The following table gives an overview of the 'primitive' inductive types that we have introduced so far. Many types of interest can be defined in terms of these inductive types, along with the identity type which we will introduce in \cref{chap:identity}.
\begin{center}
\begin{tabular}{llll}
\toprule
name & type & constructors \\
\midrule
\define{natural numbers} & $\N$ & $\zeroN:\N$ \\
& & $\succN:\N\to \N$ & \\
\define{empty type} & $\emptyt$ & {\color{black!20}(no constructors)}\\
\define{unit type} & $\unit$ & $\ttt:\unit$ \\
\define{booleans} & $\bool$ & $\bfalse:\bool$ \\
& & $\btrue : \bool$ \\
\define{coproduct} & $A+B$ & $\inl : A \to A+B$ \\
& & $\inr : B\to A+B$ & \\
\define{product} & $A\times B$ & $(\blank,\blank):A\to (B\to A\times B)$ \\
\define{$\Sigma$-type} & $\sm{x:A}B(x)$ & $(\blank,\blank):\prd{y:A} \big(B(y)\to \sm{x:A}B(x)\big)$ \\
\bottomrule
\end{tabular}
\end{center}
\end{comment}

\begin{exercises}
\item \label{ex:boolean-operation}Define the following operations of Boolean algebra:
  \begin{center}
    \begin{tabular}{ll}
      exclusive disjunction & $p \oplus q$ \\
      implication & $p \Rightarrow q$ \\
      if and only if & $p \Leftrightarrow q$ \\
      Peirce's arrow (neither \dots{} nor) & $p \downarrow q$ \\
      Sheffer stroke (not both) & $p\mid q$.
    \end{tabular}
  \end{center}
  Here $p$ and $q$ range over $\bool$.
\item \label{ex:int_pred}\index{predecessor function|textbf}Define the predecessor function $\mathsf{pred}_\Z:\Z\to \Z$.
\item \label{ex:int_group_ops}\index{group operations!on Z@{on $\Z$}}Define group operations
  \begin{align*}
    k,l & \mapsto k+l \\
    k & \mapsto -k
  \end{align*}
  on $\Z$.
\item Define multiplication on $\Z$.
\item Construct a function $F:\Z\to\Z$ that extends the Fibonacci sequence to the left
  \begin{equation*}
    \ldots,5,-3,2,-1,1,0,1,1,2,3,5,8,13,\ldots
  \end{equation*}
  in the expected way.
\item \label{ex:one_plus_one} Show that $\unit+\unit$ satisfies the same induction principle\index{induction principle!of booleans} as $\bool$, i.e., define
  \begin{align*}
    t_0 & : \unit + \unit \\
    t_1 & : \unit + \unit,
  \end{align*}
  and show that for any type family $P$ over $\unit+\unit$ there is a function
  \begin{align*}
    \ind{\unit+\unit}:P(t_0)\to \Big(P(t_1)\to \prd{t:\unit+\unit}P(t)\Big)
  \end{align*}
  satisfying
  \begin{align*}
    \ind{\unit+\unit}(p_0,p_1,t_0) & \jdeq p_0 \\
    \ind{\unit+\unit}(p_0,p_1,t_1) & \jdeq p_1.
  \end{align*}
  In other words, \emph{type theory cannot distinguish between the types $\bool$ and $\unit+\unit$.}
\item For any type $A$ we can define the type $\mathsf{list}_A$ of \define{lists} elements of $A$ as the inductive type with constructors
  \begin{align*}
    \mathsf{empty\usc{}list} & : \mathsf{list}_A \\
    \mathsf{append\usc{}list} & : A \to (\mathsf{list}_A \to \mathsf{list}_A).
  \end{align*}
  \begin{subexenum}
  \item Write down the induction principle and the computation rules for $\mathsf{list}_A$.
  \item Define a function $\mathsf{length}:\mathsf{list}_A\to\N$.
  \item Define a function $\mathsf{reverse} : \mathsf{list}_A \to \mathsf{list}_A$ that reverses the order of the elements in any list.
  \item Define a function
    \begin{equation*}
      \mathsf{concat\usc{}list} : \mathsf{list}_A \to (\mathsf{list}_A \to \mathsf{list}_A)
    \end{equation*}
    that concatenates any two lists of elements in $A$.
  \item Define a function
    \begin{equation*}
      \mathsf{sum} : \mathsf{list}_\N \to \N
    \end{equation*}
    that adds all the elements in a list of natural numbers.
  \end{subexenum}
\end{exercises}


\documentclass{article}

\begin{document}

\section{Senstivity Analysis of PETPT (Priestley-Taylor) model}

Sensitivity analysis of any theoretical model involves the computation of Sobol indices which measures the variance in the output as a
result of the fluctuations in the input parameters.
%has been performed for the
The Priestley-Taylor (PETPT) model is an example of an
evapotranspiration (EO) model where the daily EO rate of a crop depends on the following independent
variables - the maximum (TMAX) and minimum (TMIN) observable temperatures within a
given day, daily solar radiation (SRAD), leaf area index (XHLAI), and soil
albedo coefficient (MSALB). For computational purposes,
the SALib library in python allows one to perform a
sensitivity analysis for a given model. In our work, we have implemented three
different sensitivity methods, namely, Sobol, Fourier Amplitude Sensitivity Test or
FAST, and finally, Random Balance Design-Fourier Amplitude Sensitivity Test or
RBD-FAST and compared the first order indices (S$_i$; $i$ is the variable) across the different models.
Our results indicate that for reasonable approximations of the input parameter
bounds, S$_i$ values are maximum for TMAX and minimum (zero) for
XHLAI in all three methods. SRAD followed by MSALB have the most significant
S$_i$'s after TMAX while the sobol index for TMIN in the PETPT model is
negligible. The Sobol method can compute the second order index (S$_{ij}$; for
the variables $i$ and $j$) as
well. Interestingly, we find that while S$_{XHLAI}$ is vanishingly small,
the second order index of XHLAI and TMAX is significantly large. Again,
S$_{ij}$ for $(i, j)=$ (TMAX, SRAD) is non-negligible. In addition, the runtime for each of
the sensitivity analysis methods is computed and compared for different sample
sizes. It becomes evident that for large sample sizes ($\sim$ 10$^6$), Sobol is
two orders of magnitude slower than both FAST and RBD-FAST methods. In summary,
TMAX is the most significant parameter and any significant change in its value will
have the maximum effect on the EO rate. Our future goal is to extend our
discussion of sensitivity analysis to the PETASCE model in which the EO rate
depends on the
independent variables discussed above as well as a new set of input parameters.

\section{Parameter Estimation}

A necessary question that arises from the previous section is how reliable are
the bounds of our input variables that have been used in estimating the Sobol
indices. While such an
information can be extracted after having parsed through hundreds of documents on
various evapotranspiration models and other experimental data, we would like to
adopt a more fundamental approach to determine the domain of a high
dimensional space spanned by our input
parameters. Given our previous choice of the bounds imposed on the input parameters, it is easy
to see that there exist a subset of parameter sets which yield similar
evapotranspiration rates in both PETPT and PETASCE models. However, such an
{\it ad-hoc} choice generates only a slice of a larger domain of possible values in
the configuration space of our parameters. A more rigorous approach then
requires us to minimize the square of the difference between the EO rates of the PETPT and
PETASCE models which is defined as the Loss.
Typically, the Loss is a non-convex, non-linear, non-separable function with
multiple minima and hence any optimizaiton procedure is non-trivial.
We invoke the standard gradient descent algorithm that is ideal for
convex optimzation problems and apply it over here. Moreover, the mathematical
expression for the EO rates vary based on the domains of the input variables.
Hence, domain constraints need to be imposed while minimizing the Loss.
This work is currently  ongoing and we hope to develop a more efficient
algorithm that can precisely identify a list of parameter sets that optimize
the output in both models.

\end{document}


% appendices.tex

\section{Appendices}

% Not yet ready for prime time; may not include it at any rate because
% of the "negative-zero" problem.

\subsection*{Higher Order Exponents}

You can multiply a number times itself as many times as you want. Understanding a little more about exponents (the number of times you multiply a number times itself) will make understanding the language we use to discuss computers quite a bit easier. Just as we mention kilobytes and megabytes as units of storage, there are also less common units of storage that invent new multiplier terms for bits and bytes, because of the ``powers of 2.'' So let's learn about base-2 exponents-- the ``powers of two".

\bigskip
\newcommand{\expline}[2]{
$2^{#1}$ & = & #2
}

\begin{footnotesize}
\begin{tabular}{l c l p{3.5in} }

\multicolumn{3}{c}{\textbf{Powers of Two}} & \textbf{Notes} \\
\hline\\[\negsep]

\expline{0}{1}& This one is strange, sort of. But it works! Any number ``to the zeroth power" is equal to 1. See below for more. \\
\expline{1}{2} \\
\expline{2}{4} \\
\expline{3}{8} \\
\expline{4}{16} & Since $2^2 = 4$, $2^2 \times 2^2 = 16 $   \\
\expline{5}{32} &  This is why you can count to 31 on one hand. \\
\expline{6}{64} \\
\expline{7}{128} \\
\expline{8}{256} & 8-bit computers were the first machines really adopted by consumers. Also, 8 bits makes up one \emph{byte} of computer memory, so each byte can take on up to 256 values. \\
\expline{9}{512} \\
\expline{10}{1,024} & 1,000 usually gets the prefix \emph{kilo-}, like a kilogram is 1000 grams. A \emph{kilobyte} is 1024 bytes.\\
\expline{16}{65,536} \\
\expline{20}{1,048,576} & $2^{10}$ bytes is a kilobyte; ($2^{10} \times 2^{10}$) bytes is a \emph{megabyte}.\\
\expline{24}{16,777,216} & Most computer displays can show up to 16 million colors, using red, green, and blue, all in combination. Each piece of the color can have 256 ($2^8$) levels, from zero (black) to 255 (100\% red, or green, or blue)\\
\expline{30}{1,073,741,824} & $2^{30}$ bytes is a \emph{gibibyte}, or a bit more than a \emph{gigabyte}, which is 1000 megabytes. \\
\expline{32}{4,294,967,296} \\
\expline{40}{1,099,511,627,776} & $2^{40}$ bytes is a \emph{tebibyte}, or a few percent more than 1000 gigabytes -- a \emph{terabyte.} \\
\expline{50}{1,125,899,906,842,624} & $2^{50}$ bytes is \emph{pebibyte}. A petabyte is so large that one pebibyte is enough to store the DNA of the entire population of the USA\ldots{}and then clone them, \emph{twice.} \\[\sep]
\hline
\end{tabular}

\end{footnotesize}
% $10^2 & = & 100 & \\
%$10^3 & = & 1000 & \\
%$10^4 & = & 10000 & \\

\vfill

\stbox{6.0in}{\emph{Explanation:} The reason that any number to the zeroth power is equal to one comes from the way we subtract exponents when dividing. You know that 8 divided by 4 equals 2; written another way, $2^3 \div 2^2 = 2^1$. Notice that the exponents change by subtraction, but the equation is the same! You can \emph{divide} base-exponent numbers by \emph{subtracting} the exponents (\emph{extra-special historical trivia: this is how slide rules work}; see Figure \ref{fig:sliderule} for a picture). And since any number divided by itself equals one, as in $2^3 \div 2^3 = 1 $, subtracting the exponents gives $2^0$.}

%\subsection*{Negative Numbers}
%
% If we had told the computer that it was using \emph{signed} numbers, it would count upwards from -32,768, and the first column would be a one, to indicate it was negative, with the rest of them zeros. That is, the format for storing negative numbers subtracts 32,768 from 0 -- the range is still the same (65,536 numbers), but the starting point is different.

\subsection*{Numbers Below Zero?}

Representing \emph{positive} numbers is easy for a computer: it counts upwards from zero. Representing \emph{negative} numbers is harder, because \emph{taking away values by adding them} is a little awkward. To represent a negative, designers use the leading (left-most) bit to declare ``positive'' (zero) or ``negative'' (one). Note that the \emph{range} of numbers that can be represented does not change -- for a 4-digit binary number, whether counting from zero to 15, or from -8 to 7, the total space used on the number line is still 16 numbers, in order.

\stbox{6.0in}{
\emph{Problem:} If the computer doesn't know it is supposed to use that first number to determine whether a number is negative or not, how does it evaluate the number?
}

\subsection*{Complement of a Number}

In mathematics, the \emph{complement} of a binary number is ``the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa)." This number is called the \emph{ones' complement} of the number.\footnote{{\color{webblue}\href{https://en.wikipedia.org/wiki/Ones\%27_complement}{Wikipedia page on Ones' Complement.}}}

Here's an example:

\bigskip

\begin{tabular} {c c c}
 Number &  $+$   &   $-$ \\[\sep]
 \hline\\[\negsep]
 0  &  0000 &  1111  \\
\grr
 1  & 0001  & 1110  \\
 2  & 0010  & 1101\\
\grr
 3  & 0011  & 1100\\
 4  & 0100  & 1011\\
\grr
 5  & 0101  & 1010\\
 6  & 0110  & 1001\\
\grr
 7  & 0111  & 1000\\
 \hline

\end{tabular}

\subsection*{Subtraction}

Subtraction is similar to addition -- very similar, since the process is ``adding a negative number". To this point, we haven't seen negative numbers, because numbers below zero are harder to create or see, compared to numbers between zero and 15, or some other positive number. To get a negative number, computer scientists decided to create a pattern for describing negative numbers, that computers can correctly interpret. They decided to use the first bit (1 or 0, just a ``yes'' or ``no'' indicator) of the number as the \emph{sign bit}: that is, if the first bit, in a ``signed integer", is 1, then the number is a negative number. So if we have a four bit number, and the first bit is only for the ``is this number negative'' indicator, the number represents numbers in the range $[-7,8]$ -- still 16 values, but counting from $-7$ instead of zero. Also note that if you don't tell the computer the number is signed, it will happily assume the number is \emph{unsigned} and give you the wrong answer\footnote{Well, really, it will give you the \emph{right} answer to a question that is different from the question you thought you were asking!}.

To subtract, we first create the \emph{two's complement} of the number we are subtracting (the ``subtrahend"). We leave the number from which we are subtracting (the ``minuend") alone.

Two's complement works like this: you take the \emph{complement} of the number, and add one. \emph{Complement} means you swap all the zeroes for ones, and all the ones for zeroes. Put another way, you run each bit through a NOT gate, then add a one to the result, using a set of full adders.

Let's take the two's complement of 7.

\begin{verbatim}
       0111      7

       1000           ones' complement of 7
       0001           add one to get two's complement of 7
===========   ====
       1001     -8 + 1 = -7

\end{verbatim}

So here's a simple example of subtraction:

\begin{verbatim}
       0110      6
-      0011      3
===========   ====

       1100           ones' complement of 3
       0001           add one to get two's complement
===========
       1101           (-8 + 1 + 0 + 4) = -3 ... good. Now we can add:

       0110      6
+      1101     -3
===========   ====
       0011      3      (see how the sign bit was converted to positive?)
                        (the overflowed bit doesn't matter in this case)

\end{verbatim}

\newpage
And here's a slightly more interesting one, with 8 bit numbers:

\begin{verbatim}
  0000 0110      6
- 0001 0011     19
===========   ====

  0001 0011     19

  1110 1100             ones' complement
  0000 0001             add one
===========   ====
  1110 1101     (-128 + 64 + 32 + 0 + 8 + 4 + 0 + 1) = -19

  0000 0110      6
+ 1110 1101    -19
===========   ====
  1111 0011    -13   (-128 + 64 + 32 + 16 + 0 + 0 + 2 + 1) = BOOM!

\end{verbatim}

Designing logic circuits for subtraction using the twos' complement is surprisingly elegant. Let's review what happens to execute a twos' complement subtraction operation:

\bi
\+ Get the ones' complement of the subtrahend (invert each bit with a NOT function)
\+ Add 1
\+ Add the resulting value to the minuend
\ei

Building a flexible subtraction logic circuit depends on two features of our existing gates: first, that full adders can accept an incoming ``carry'' bit; and second, that XOR gates can be used as NOT gates by tying one of the inputs to positive voltage. Therefore, the logic circuit works like this:

\bi
\+ Pass each bit of the subtrahend through XOR gates with one of the inputs pulled up to positive voltage level; this takes the ones' complement of the number
\+ The result passes into one side of the full adder line we use for adding numbers
\+ Set the carry bit on the right-most full adder (i.e. the zero or 1 register) to high -- \emph{this action adds 1 to the incoming number}
\ei

The result of the line of adders is the answer to the subtraction question.

\begin{figure}[ht!]
\begin{center}

\input{./include/real_subtraction_circuit.tex}

\caption{How addition and subtraction gets implemented with logic gates to either subtract {\color{red}$A$}, a four digit binary number (something like \texttt{0010}, or ``2"), from {\color{blue}$B$} (perhaps \texttt{1000}, or ``8''), or add the two numbers instead. Each pair of bits goes into a full adder (the trapezoid-shaped symbol). To use subtraction, the M-line is held to a positive voltage, effectively turning the $XOR$ gate into a $NOT$ gate.}
\end{center}
\end{figure}

\newpage
\section*{A Bit More About Capacitors}

The Leyden jar was one of the first capacitors invented: metal foil was placed inside a glass jar, and wrapped around the outside of a glass jar, but neither foil gets near the top of the jar. The glass barrier between the foil sheets (inside and outside) allows a charge to build up between the foil without allowing the electrical charge to move through the glass. Leyden jars didn't hold much charge, but the concept it demonstrated has not changed.

Capacitors are usually made with two metal plates that are on top of each other (or wrapped around each other), but that do not actually touch. When powered, they allow energy to be stored inside an electrical field. Because the plates need a lot of area to store even a small amount of charge, the plates are usually rolled up into some other shape, such as a cylinder. Sometimes, other shapes of capacitors are used for special purposes.

\bigskip
\stbox{6.0in}{
\emph{Experiment:} make a variable capacitor from aluminum foil, paper, and a paper towel tube. The paper should go around the roll only once. Cut the foil one-quarter or one-half inch smaller than the paper on all sides. Tape a small wire to one corner of the foil, if possible using metallic/conductive tape\footnote{While it is possible to solder to aluminum foil, really there are much better and safer ways to attach a wire to the foil for the purposes of this workshop.}. Tape the foil to the paper. Tape one edge of the paper, foil side down, against the cardboard tube. Make another paper/foil combination. Wrap the paper/foil around the cardboard tube, but tape the paper only to itself, and just loose enough to slide a little.

Another example, using a coke can, can be found {\color{webblue}\href{http://www.tompolk.com/crystalradios/cokecancapacitor.html}{here}}.

\bigskip

It is also possible to make a regular capacitor with the paper-foil layers, separated by flat sheets of cardboard. Keep track of the ``up'' and ``down'' sides! Note that it is pretty easy to gang each ``plate'' together and make capacitor with a larger value; tying all the anodes together, and all the cathodes together, makes a larger capacitor.
}

%---------------------------
\clearpage
\newpage
\section*{Why NAND Gates and NOR Gates are ``Universal"}
%---------------------------

It turns out that it is possible to make \emph{any} gate from NAND gates. The same is true for NOR gates. A very simple example can be seen, where we can create an OR gate from two NOR gates.

\input{./include/nor-to-or-gate.tex}

\input{./include/3-input-nand.tex}

%---------------------------
\newpage
\section*{Memory}
%---------------------------

Logic circuits need to store the numbers they are working with in order to do more than one thing. Central Processing Units (CPUs), Arithmetic Logic Units (ALUs) or Floating-Point Units (FPUs) each work with numbers, and need to fetch them from somewhere and put them somewhere when they are done. The ``somewhere" is \emph{memory}. Keeping with the use of binary math and binary logic, a high voltage would be a 1 and a low voltage would be a 0. Through the use of these little stored charges, computers can keep track of many, many pieces of information. No matter what the circuits are doing, everything being stored is composed of some count of zeros and ones.

There have been \emph{many} forms of memory through the years, including punched paper cards, and even a wiggly wire! The most common memory that a CPU uses these days is usually called ``dynamic RAM" (DRAM), and each bit consists of a circuit that is primarily a single capacitor and a single transistor, though it requires supporting circuitry and cannot be read without erasing the memory cell. Computers read eight, sixteen, 32, or 64 bits at a time from a row of memory and pass the information (numbers or instruction codes) from the memory on to be processed by the CPU.

One type of computer memory, called \emph{static RAM}, uses at least six transistors per bit. Since  DRAM is simpler (it uses only one transistor and one capacitor per bit), it is also cheaper. Therefore, to store one bit with 6 or more transistors is more expensive. So SRAM is mostly used for very important memory, like the memory very close to the core of the computer processor. Have a look at Figure \ref{fig:sram}\footnote{Diagram and supporting information adapted from {\color{webblue}\href{https://en.wikipedia.org/wiki/Static_random-access_memory}{Wikipedia}} and {\color{webblue}\href{https://www.entner.net/sites/default/files/diss-entner-final-v1.pdf}{Robert Entner's dissertation}}.},
which is pretty complicated, but if you understand how the two types of transistors are turned on and off, it will make sense. To keep this diagram simple, no resistors are shown. If you look closely at the $Q_1$/$Q_2$ and $Q_3$/$Q_4$ transistors, you can see that they are acting like inverters (that is, each pair makes a NOT gate). When WL (the ``word line") goes high, $Q_5$ and $Q_6$ open up, allowing access to the single bit stored in $BL$ and the inverse of that bit in $\overline{BL}$. Whether the word line is active (that is, whether or not you can write to the memory bit), it is possible to read the value of the bit--making SRAM very fast, because the system does not wait on the word line to activate.

\begin{figure}[h!]
\begin{center}
\input{./include/sramcell.tex}
\caption{A static ram cell. The bit line ($BL$) on the right is the value of the bit, and the bit line on the left ($\low{BL}$) is the complement (NOT) the value of that bit.} % With a bit of careful circuitry, it is possible to write using only one of the bit lines, but it is safer to have two inputs (one positive, and one complement) to ensure the gates both change and lock in the value of the bit.
\label{fig:sram}
\end{center}
\end{figure}

SRAM uses 6 transistors per bit, making it very expensive compared to dynamic RAM,
but each bit of SRAM can stand alone, with no other supporting circuitry except
for the word line transistors. So, we use SRAM here because you can see the
circuit, and see how it works. As a bonus, you can construct a bit of SRAM with
opposing NOT gates, making it fairly easy to see what is happening.

\begin{figure}
  \begin{center}
  \input{./include/JK-flip-flop.tex}
  \caption{A gate-level schematic of one bit of memory, using a JK flip-flop circuit. Note that the two left-most NAND gates have \emph{three} inputs.}
  \end{center}
\end{figure}

% JK flip-flop:
% Flip-flop storage using NOR gates is better than my previous attempt using NOT gates.
% I had trouble with the word/line controller circuit resistor values for positive
% clearing and setting of the gate states.

We are going to implement some RAM using a {\color{webblue}\href{https://www.electronics-tutorials.ws/sequential/seq_2.html}{\emph{flip-flop}}} circuit, made of NAND gates.
It uses even more transistors than the SRAM circuit, but it is simpler to create and uses logic gates with which you are already familiar. This ``JK Flip-Flop" circuit uses
two input NAND gates and also \emph{three}-input NAND gates. 3-input NAND operates mostly
the same way as a regular NAND gate, except that the inputs also include the value of the
Q and not-Q output (value) lines.
These lines are required before the output state (value) of the gate will change. And so is a ``clock" signal line. By using a signal that turns on or turns off everywhere at once, the computer memory can be more reliable. The clock line ticks on and off like the second hand of a clock, allowing the computer to read or write to the memory without wondering if the values are changing.
So, the set- and reset- functions for the memory cell
only activate when the clock signal is also high (has positive voltage on the line).
This extra feature allows for certainty of reads and writes: it helps
avoid rapid oscillation / instability or lock-ups should a circuit (or a third
grader!) try to both set and reset the
flip-flop at the same time. Furthermore, when powering up a simple SR flip-flop,
there is no inherent guarantee of what the value would be (of course, it's
possible to put a pull-up or pull-down resistor on the signal lines to create
a natural ``base level'' of a set of inputs, though care must be taken to
not accidentally create incompatible base conditions (such as accidentally setting
both set and reset to high).

\begin{figure}
  \begin{center}
    \includegraphics[scale=0.10]{twoflipflops.jpg}
  \caption{Two bits of data storage, using JK flip-flop gates.}
  \end{center}
\end{figure}

%---------------------------
\clearpage
\newpage
\section*{Resistor Chart}
%---------------------------

Resistors are marked with colored bands to make it easy to determine what
resistance value they have, and how close each individual part's value is
guaranteed to be to that stated value.

\begin{figure}[!ht]
\begin{center}
\fbox{
\includegraphics[scale=0.60]{resistorchart.png}
}
\caption{A resistor value color band chart. The `tolerance' stripe (how close the value is guaranteed to be to the stated value) is on the right side. }
\label{fig:resistorchart}
\end{center}
\end{figure}


\subsubsection{Quadratic Factors}

If a polynomial has complex roots, then it has a quadratic factors.
Here, we'll assume that the quadratic factor isn't repeated.
So,
\begin{equation*}
	Q(x) = R(x)(ax^2+bx+c)\text{, }b^2-4ac < 0\text{, and }R(x)\text{ is not divisible by }ax^2+bx+c
\end{equation*}
In this case, we say
\begin{equation*}
	\frac{P(x)}{R(x)(ax^2+bx+c)} = \left(\text{Decomposition of }R(x)\right)+\frac{A_1x+B_1}{ax^2+bx+c}
\end{equation*}
and then solve for the constants in the numerator, possibly having to solve a system of equations or using previous results and less convenient values for $x$.

\input{./backgroundReview/algebraPreCalc/quadraticFactors_example.tex}


\subsection{Expressions derived from \( \pi \):}
One may easily derive the average number of individuals that are at any given state
using \( pi \).
The average number of individuals in state \( i \) can be calculated by multiplying
the number of individuals that are present in state \( i \) with the probability
of being at that particular state (i.e \(\pi_i (u_i + v_i)\)).
Using this logic it is possible to calculate any performance measures that are related
to the mean number of individuals in the system.

Average number of people in the system:
\begin{equation}
    L = \sum_{i=1}^{|\pi|} \pi_i (u_i + v_i)
\end{equation}

Average number of people in the service centre:
\begin{equation}
    L_H = \sum_{i=1}^{|\pi|} \pi_i v_i
\end{equation}

Average number of people in the buffer centre:
\begin{equation}
    L_A = \sum_{i=1}^{|\pi|} \pi_i u_i
\end{equation}

Consequently getting the performance measures that are related to the duration of
time is not as straightforward.
Such performance measures are the mean waiting time in the system and the mean time
blocked in the system.
Under the scope of this study three approaches have been considered to calculate these
performance measures; a direct approach, a recursive algorithm and consequently a
closed-form formula.

The research question that needs to be answered here is: ``When a class 1/2
individuals enters the system, what is the expected time that they will have to
wait?''.
In order to formulate the answer to that question one needs to consider all possible
scenarios of what state the system can be in when an individual arrives.
Furthermore, different formulas arises for class 1 individuals
and a different one for class 2 individuals.

\subsection{Mean waiting time}
\subsubsection{Recursive formula for mean waiting time of class 1 individuals}
\label{sec:recursive-waiting-time-others}

To calculate the mean waiting time of class 1 individuals one must first identify
the set of states \((u, v)\) that will imply that a wait will occur.
For this particular Markov chain, this points to all states that satisfy \(v > C\)
i.e. all states where the number of individuals in the service centre exceed the
number of servers.
The set of such states is defined as \textit{waiting states} and can be denoted
as a subset of all the states, where:

\begin{equation} \label{eq:waiting_states}
    S_w = \{(u, v) \in S \; | \; v > C \}
\end{equation}

Additionally, there are certain states in the model where arrivals cannot occur.
A class 1 individual cannot arrive whenever the model is at any state
\((u, N)\)
for all \(u\) where \(N\) is the system capacity.
Therefore the set of all such states that an arrival may occur are defined as
\textit{accepting states} and are denoted as:

\begin{equation}\label{eq:accepting_states_class_1}
    S_A^{(1)} = \{(u, v) \in S \; | \; v < N \}
\end{equation}

Moreover, another element that needs to be considered is the expected waiting time
in each state \( c(u,v) \), otherwise known as sojourn time \cite{Raghunandanan}.
In order to do so a variation of the Markov model has to be considered where when
the individual arrives at any of the states of the model no more arrivals can
occur after that.

\begin{figure}[h]
    \centering
    \begin{tikzpicture}[-, node distance = 1.4cm, auto]

        \tikzmath{
            let \minsz = 1.8cm;
        }

        \node[draw=none, minimum size=\minsz] (one) {};
        \node[state, minimum size=\minsz, right=of one] (two) {(0,T-1)};
        \node[state, minimum size=\minsz, right=of two] (three) {(0,T)};
        \node[state, minimum size=\minsz, right=of three] (four) {(0,T+1)};
        \node[draw=none, minimum size=\minsz, right=of four] (five) {};

        \node[state, draw=red, line width=0.5mm, minimum size=\minsz,
        below=of three] (three_one) {(1,T)};
        \node[state, draw=red, line width=0.5mm, minimum size=\minsz,
        below=of three_one] (three_two) {(2,T)};
        \node[state, minimum size=\minsz, below=of four] (four_one) {(1,T+1)};
        \node[state, minimum size=\minsz, below=of four_one] (four_two) {(2,T+1)};
        \node[draw=none, minimum size=\minsz, right=of four_one] (five_one) {};
        \node[draw=none, minimum size=\minsz, right=of four_two] (five_two) {};
        \node[draw=none, minimum size=\minsz, below=of three_two] (three_three) {};

        \draw[every loop]
            (two) edge node {\((T-1) \mu\)} (one)
            (three) edge node {\(T \mu\)} (two)
            (four) edge node {\((T+1) \mu\)} (three)
            (five) edge node {\((T+1) \mu\)} (four)
            (three_one) edge node {\(T \mu\)} (three)

            (four_one) edge node {\((T+1) \mu\)} (three_one)
            (five_one) edge node {\((T+1) \mu\)} (four_one)
            (three_two) edge node {\(T \mu\)} (three_one)
            (three_three) edge node {\(T \mu\)} (three_two)
            (four_two) edge node {\((T+1) \mu\)} (three_two)
            (five_two) edge node {\((T+1) \mu\)} (four_two)
            ;

        \draw[->, red, ultra thick] (three_two) edge node {} (two);
        \draw[->, red, ultra thick] (three_one) edge node {} (two);
    \end{tikzpicture}
    \caption{Markov chain - ignoring any arrivals}
    \label{other_patients_trip}
\end{figure}

As illustrated in figure \ref{other_patients_trip} a class 1 individual,
when in the threshold column, only visits one of the nodes since they are not
affected by class 2 individuals.
Thus, one may acquire the desired time by calculating the inverse of the sum of
the out-flow rate of that state.
Since we are ignoring arrivals though the only way to exit the state will only be
via a service.
In essence this notion can be expressed as:

\begin{equation} \label{eq:sojourn_others}
    c^{(1)}(u,v) =
    \begin{cases}
        0, & \textbf{if } u > 0 \textbf{ and } v = T \\
        \frac{1}{\text{min}(v,C)\mu}, & \textbf{otherwise}
    \end{cases}
\end{equation}

Note that whenever any class 1 individual is at a state \((u,v)\) where
\(u > 0\)
and \(v = T\) (i.e. all states \((1,T), (2,T) \dots, (M,T)\)) the sojourn time is
set to \(0\).
This is done to capture the trip thorough the Markov chain from the perspective
of class 1 individuals.
Meaning that they will visit all states of the threshold column but only the one
in the first row will return a non-zero sojourn time.

Now, using the above equations, and considering all sates that belong in \(S_w\)
the following recursive formula can be used to get the mean waiting time spent in
each state in the Markov model.
For class 1 individuals, whenever the model is at state \( (u,v) \), any
incoming
individual will proceed to arrive at state \( (u, v+1) \).
individuals will then proceed to visit all other states until they reach one which
has less than \(C\) servers occupied (i.e. until a server becomes available).
The formula goes through all states from right to left recursively and adds the
sojourn times of all these states together until it reaches a state that is not
in the set of waiting states.
Thus, the expected waiting time of a class 1 individual when they arrive at
state \( (u,v) \) can be given by:

\begin{equation} \label{eq:waiting-time-at-each-state-other}
    w^{(1)}(u,v) =
    \begin{cases}
        0, \hspace{4.85cm} & \textbf{if } (u,v) \notin S_w \\
        c^{(1)}(u,v) + w^{(1)}(u-1, v), & \textbf{if } u > 0 \textbf{ and } v = T \\
        c^{(1)}(u,v) + w^{(1)}(u, v-1), & \textbf{otherwise}
    \end{cases}
\end{equation}

Finally, the mean waiting time can be calculated by summing over all expected
waiting times of accepting states multiplied by the probability of being at that
state and dividing by the probability of being in any accepting state.

\begin{equation} \label{eq:recursive-waiting-time-others}
    W^{(1)} = \frac{\sum_{(u,v) \in S_A^{(1)}} w^{(1)}(u,v)
    \pi_{(u,v)}}{\sum_{(u,v) \in S_A^{(1)}} \pi_{(u,v)}}
\end{equation}

\subsubsection{Recursive formula for mean waiting time of
class 2 individuals} \label{sec:recursive-waiting-time-ambulance}

Equivalently the mean waiting time for class 2 individuals can be calculated
in a similar manner.
The set of waiting states is the same as before but there is a slight modification
in the set of accepting states.

\[
    S_w = \{(u, v) \in S \; | \; v > C \}
\]

\begin{equation}\label{eq:accepting_states_class_2}
    S_A^{(2)}=
    \begin{cases}
        \{(u, v) \in S \; | \; u < M \} & \textbf{if } T \leq N\\
        \{(u, v) \in S \; | \; v < N \} & \textbf{otherwise}
    \end{cases}
\end{equation}

The set of accepting states is modified in such a way such that a class 2
individual cannot arrive in the model when the model is at any state \((M, v)\)
for
all \(v \geq T\) where \(M\) is the buffer centre capacity and \(T\) is the threshold.
An odd situation here is when the threshold is set to a very high number that is
more than the actual system capacity.
In such cases the set of accepting states is defined in the same way as the
class 1 individuals case (\ref{eq:accepting_states_class_1}).
That is because whenever \(T > N\) no class 2 individual will ever be blocked in
the model
(since that threshold can never be reached) and thus the last accepting state of
the model will be state \( (0,N-1)\).

Now just like in the class 1 individuals case the sojourn time is needed.
For class 2 individuals whenever individuals are at any row apart from the
first one they automatically get a wait time of \(0\) since they are essentially
blocked.

\begin{equation} \label{eq:sojourn_ambulance}
    c^{(2)}(u,v) =
    \begin{cases}
        0, & \textbf{if } u > 0 \\
        \frac{1}{\text{min}(v,C)\mu}, & \textbf{otherwise}
    \end{cases}
\end{equation}

Finally, the recursive formula and the mean waiting time equation are identical
to the ones described above with the exception that they now use \(c^{(2)}(u,v)\)
instead of \(c^{(1)}(u,v)\).
\begin{equation} \label{eq:waiting-time-at-each-state-ambulance}
    w^{(2)}(u,v) =
    \begin{cases}
        0, \hspace{4.85cm} & \textbf{if } (u,v) \notin S_w \\
        c^{(2)}(u,v) + w^{(2)}(u-1, v), & \textbf{if } u > 0 \textbf{ and } v = T \\
        c^{(2)}(u,v) + w^{(2)}(u, v-1), & \textbf{otherwise}
    \end{cases}
\end{equation}

\begin{equation}\label{eq:recursive-waiting-time-ambulance}
    W^{(2)} = \frac{\sum_{(u,v) \in S_A^{(2)}} w^{(2)}(u,v) \pi_{(u,v)}}
    {\sum_{(u,v) \in S_A^{(2)}} \pi_{(u,v)}}
\end{equation}

\subsubsection{Mean Waiting Time - Closed-form}
Upon closer inspection of the recursive formula a more compact formula can arise.
The equivalent closed-form formula eliminates the need for recursion and thus makes
the computation of waiting times much more efficient.
Just like in the recursive part there are two formulas; one for \textit{class 1}
and one for class 2 individuals.
The formulas are given by:

\begin{equation} \label{eq:closed_form_waiting_others}
    W^{(1)} = \frac{\sum_{\substack{(u,v) \, \in S_A^{(1)} \\ v \geq C}}
    \frac{1}{C \mu} \times (v-C+1) \times \pi(u,v)}{\sum_{(u,v) \,
    \in S_A^{(1)}} \pi(u,v)}
\end{equation}

\begin{equation}\label{eq:closed_form_waiting_ambulance}
    W^{(2)} = \frac{\sum_{\substack{(u,v) \, \in S_A^{(2)} \\ min(v,T) \geq C}}
    \frac{1}{C \mu} \times (\min(v+1,T)-C) \times \pi(u,v)}{\sum_{(u,v) \,
    \in S_A^{(2)}} \pi(u,v)}
\end{equation}

Note here that the summation, in both equations \ref{eq:closed_form_waiting_others}
and \ref{eq:closed_form_waiting_ambulance}, goes through all states in the set of
accepting
states of either class 1 or class 2 individuals respectively, where a wait
incurs.
In equation \ref{eq:closed_form_waiting_others} that includes all states \((u,v)\)
in the set of accepting states of class 1 individuals such that \( v \geq C\); i.e.
whenever an arrival occurs and the system is at a state where the number of individuals
in the system is more than or equal to $C$.
Consequently, for the states that are included in the summation the expression
\( v-C+1 \) indicates the amount of people in service one would have to wait for
upon arrival at the hospital.

Additionally, the minimisation function in equation
\ref{eq:closed_form_waiting_ambulance}
ensures that when a class 2 individual arrives at any state
that is greater than the predetermined threshold, the wait that the individual will
have to endure remains the same.
In essence, the expression \(\min(v+1,T) - C\) returns the number of people in line
in front of a particular individual upon arrival.

\subsubsection{Overall Waiting Time}

Consequently, the overall waiting time should can be estimated by a linear combination
of the waiting times of class 1 and class 2 individuals.
The overall waiting time can be then given by the following equation where \(c_1\)
and \(c_2\) are the coefficients of each individual's type waiting time:

\begin{equation}\label{eq:overall_waiting_time_coeff}
    W = c_1 W^{(1)} + c_2 W^{(2)}
\end{equation}

The two coefficients represent the proportion of individuals of each type that
traversed through the model.
Theoretically, getting these percentages should be as simple as looking at the arrival
rates of each type but in practise if the service centre or the buffer centre
is full, some individuals may be lost to the system.
Thus, one should account for the probability that an individual is lost to the system.
This probability can be easily calculated by using the two sets of accepting states
\(S_A^{(2)}\) and \(S_A^{(1)}\) defined earlier in equations
\ref{eq:accepting_states_class_1}
and \ref{eq:accepting_states_class_2}.
Let us define here the probability, for either class type, that an individual
is not lost in the system by:

\begin{equation*}
    P(L'_1) = \sum_{(u,v) \, \in S_A^{(1)}} \pi(u,v) \hspace{2cm}
    P(L'_2) = \sum_{(u,v) \, \in S_A^{(2)}} \pi(u,v)
\end{equation*}

Having defined these probabilities one may combine them with the arrival rates of
each class type in such a way to get the expected proportions of class 1 and
class 2 individuals in the model.
Thus, by using these values as the coefficient of equation
\ref{eq:overall_waiting_time_coeff}
the resultant equation can be used to get the overall waiting time.
Note here that the equation below gets the overall waiting time for both the recursive
and the closed-form formula.

\begin{equation}\label{eq:overall_waiting_time}
    W = \frac{\lambda_1 P(L'_1)}{\lambda_2 P(L'_2) + \lambda_1 P(L'_1)} W^{(1)} +
    \frac{\lambda_2 P(L'_2)}{\lambda_2 P(L'_2) + \lambda_1 P(L'_1)} W^{(2)}
\end{equation}

\subsection{Mean blocking time}

\subsubsection{Direct approach for mean blocking time}

Unlike the waiting time, the blocking time is only calculated for class 2 individuals.
That is because class 1 individuals cannot be blocked.
Thus, one only needs to consider the pathway of class 2 individuals to get the
mean blocking time of the system.
Blocking occurs at states \((u,v)\) where \(u > 0 \).
Thus, the set of blocking states can be defined as:

\begin{equation*}
    S_b = \{(u,v) \in S \; | \; u > 0\}
\end{equation*}

In order to not consider individuals that will be lost to the system, the set of
accepting states needs to be taken into account. As defined in section
\ref{sec:recursive-waiting-time-ambulance},
the set of accepting states is given by (\ref{eq:accepting_states_class_2}):

\begin{equation*}
    S_A^{(2)}=
    \begin{cases}
        \{(u, v) \in S \; | \; u < M \} & \textbf{if } T \leq N\\
        \{(u, v) \in S \; | \; v < N \} & \textbf{otherwise}
    \end{cases}
\end{equation*}

For the waiting time formula in sections \ref{sec:recursive-waiting-time-others}
and \ref{sec:recursive-waiting-time-ambulance}
the mean sojourn time for each state was considered,
ignoring any arrivals. Here, the same approach is used but ignoring only class 2
arrivals. That is because for the waiting time formula, once an individual enters
the service centre (i.e. starts waiting) any individual arriving after them will
not affect their
pathway. That is not the case for blocking time. When a class 2 individual is
blocked,
any class 1 individual that arrives will cause the blocked individual to remain
blocked for more time. Therefore, class 1 arrivals are considered here:

\begin{equation}\label{eq:time_in_state_blocking_time}
    c(u,v) =
    \begin{cases}
        \frac{1}{\min(v,C) \mu}, & \text{if } v = C\\
        \frac{1}{\min(v,C) \mu + \lambda_1}, & \text{otherwise}
    \end{cases}
\end{equation}

In equation \ref{eq:time_in_state_blocking_time}, both service completions and
class 1 arrivals are considered.
Thus, from a blocked individual's perspective whenever the system moves from one
state \((u,v)\)
to another state it can either:

\begin{itemize}
    \item be because of a service being completed: we will denote the probability
    of this happening by \(p_s(u,v)\).
    \item be because of an arrival of an individual of class 1: denoting such
    probability by \(p_o(u,v)\).
\end{itemize}
The probabilities are given by:

\begin{equation*}
    p_s(u,v) = \frac{\min(v,C)\mu}{\lambda_1 + \min(v,C)\mu}, \qquad
    p_o(u,v) = \frac{\lambda_1}{\lambda_1 + \min(v,C)\mu}
\end{equation*}

Having defined \(c(u,v)\) and \(S_b\) a formula for the blocking time that is
expected to occur at each state can be given by:

\begin{equation}\label{eq:blocking-time-at-each-state}
    b(u,v) =
    \begin{cases}
        0, & \textbf{if } (u,v) \notin S_b \\
        c(u,v) + b(u - 1, v), & \textbf{if } v = N = T\\
        c(u,v) + b(u, v-1), & \textbf{if } v = N \neq T \\
        c(u,v) + p_s(u,v) b(u-1, v) + p_o(u,v) b(u, v+1), & \textbf{if } u > 0
        \textbf{ and } v = T \\
        c(u,v) + p_s(u,v) b(u, v-1) + p_o(u,v) b(u, v+1), & \textbf{otherwise} \\
    \end{cases}
\end{equation}

Unlike equations (\ref{eq:waiting-time-at-each-state-other}) and
(\ref{eq:waiting-time-at-each-state-ambulance}), equation
(\ref{eq:blocking-time-at-each-state}) will not be solved recursively.
A direct approach will be used to solve this equation here.
By enumerating all equations of (\ref{eq:blocking-time-at-each-state}) for all
states \((u,v)\) that belong in \(S_b\)
a system of linear equations arises where the unknown variables are all the \(b(u,v)\)
terms.
For instance, let us consider a Markov model where \(C=2, T=3, N=6, M=2\).
The Markov model is shown in Figure \ref{fig:example-algeb-blocking}
and the equivalent equations are
(\ref{eq:first_eq_of_blocking_example})-(\ref{eq:last_eq_of_blocking_example}).
The equations considered here are only the ones that correspond to the blocking
states.

\begin{multicols*}{2}
    \begin{figure}[H]
        \scalebox{0.50}{\input{MarkovChain/expressions_from_pi/example_model_2242/main.tex}}
        \caption{Example of Markov chain}
        \label{fig:example-algeb-blocking}
    \end{figure}
    \columnbreak
    \begin{align}
        b(1,2) &= c(1,2) + p_o b(1,3) \label{eq:first_eq_of_blocking_example} \\
        b(1,3) &= c(1,3) + p_s b(1,2) + p_o b(1,4) \\
        b(1,4) &= c(1,4) + b(1,3) \\
        b(2,2) &= c(2,2) + p_s b(1,2) + p_o b(2,3) \\
        b(2,3) &= c(2,3) + p_s b(2,2) + p_o b(1,4) \\
        b(2,4) &= c(2,4) + b(2,3)\label{eq:last_eq_of_blocking_example}
    \end{align}
\end{multicols*}

Additionally, the above equations can be transformed into a linear system of the
form \(Zx=y\) where:

\begin{equation}\label{eq:example-algebaric-approach-blocking-time}
    Z=
    \begin{pmatrix}
        -1 & p_o & 0 & 0 & 0 & 0 \\ %(1,2)
        p_s & -1 & p_o & 0 & 0 & 0 \\ %(1,3)
        0 & 1 & -1 & 0 & 0 & 0 \\ %(1,4)
        p_s & 0 & 0 & -1 & p_o & 0\\ %(2,2)
        0 & 0 & 0 & p_s & -1 & p_o \\ %(2,3)
        0 & 0 & 0 & 0 & 1 & -1 \\ %(2,4)
    \end{pmatrix},
    x=
    \begin{pmatrix}
        b(1,2) \\
        b(1,3) \\
        b(1,4) \\
        b(2,2) \\
        b(2,3) \\
        b(2,4) \\
    \end{pmatrix},
    y=
    \begin{pmatrix}
        -c(1,2) \\
        -c(1,3) \\
        -c(1,4) \\
        -c(2,2) \\
        -c(2,3) \\
        -c(2,4) \\
    \end{pmatrix}
\end{equation}

A more generalised form of the equations in
(\ref{eq:example-algebaric-approach-blocking-time})
can thus be given for any value of \(C,T,N,M\) by:

\begin{align}
    b(1,T) =& c(1, T) + p_o b(1, T + 1) \label{eq:first_eq_of_blocking_general}\\
    b(1,T + 1) =& c(1, T + 1) + p_s(1, T) + p_o b(1, T + 1) \\
    b(1,T + 2) =& c(1, T + 2) + p_s(1, T + 1) + p_o b(1, T + 3) \\
    & \vdots \nonumber \\
    b(1, N) =& c(1, N) + b(1, N - 1) \\
    b(2, T) =& c(2, T) + p_s b(1, T) + p_o b(2, T + 1) \\
    b(2, T + 1) =& c(2, T + 1) + p_s b(2, T) + p_o b(2, T + 2) \\
    & \vdots \nonumber \\
    b(M, T) =& c(M, T) + b(M, T-1) \label{eq:last_eq_of_blocking_general}
\end{align}

The equivalent matrix form of the linear system of equations
(\ref{eq:first_eq_of_blocking_general}) - (\ref{eq:last_eq_of_blocking_general})
is given by \(Zx=y\), where:
\begin{equation}\label{eq:general-algebaric-approach-blocking-time}
    \scalebox{0.9}{
        \(
        Z =
        \begin{pmatrix}
            -1 & p_o & 0 & \dots & 0 & 0 & 0 & 0 & 0 & \dots & 0 & 0 \\ %(1,T)
            p_s & -1 & p_o & \dots & 0 & 0 & 0 & 0 & 0 & \dots & 0 & 0 \\ %(1,T+1)
            0 & p_s & -1 & \dots & 0 & 0 & 0 & 0 & 0 & \dots & 0 & 0 \\ %(1,T+2)
            \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \vdots & \vdots &
            \vdots & \ddots & \vdots & \vdots \\
            0 & 0 & 0 & \dots & 1 & -1 & 0 & 0 & 0 & \dots & 0 & 0 \\ %(1,N)
            p_s & 0 & 0 & \dots & 0 & 0 & -1 & p_o & 0 & \dots & 0 & 0 \\ %(2,T)
            0 & 0 & 0 & \dots & 0 & 0 & p_s & -1 & p_o & \dots & 0 & 0 \\ %(2,T+1)
            \vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \vdots & \vdots &
            \vdots & \ddots & \vdots & \vdots \\
            0 & 0 & 0 & \dots & 0 & 0 & 0 & 0 & 0 & \dots & 1 & -1 \\ %(M,T)
        \end{pmatrix},
        x =
        \begin{pmatrix}
            b(1,T) \\
            b(1,T+1) \\
            b(1,T+2) \\
            \vdots \\
            b(1,N) \\
            b(2,T) \\
            b(2,T+1) \\
            \vdots \\
            b(M,T) \\
        \end{pmatrix},
        y=
        \begin{pmatrix}
            -c(1,T) \\
            -c(1,T+1) \\
            -c(1,T+2) \\
            \vdots \\
            -c(1,N) \\
            -c(2,T) \\
            -c(2,T+1) \\
            \vdots \\
            -c(M,T) \\
        \end{pmatrix}
        \)
    }
\end{equation}

Thus, having calculated the mean blocking time for all blocking states \(b(u,v)\),
it only remains to put them together in a formula just like in equations
\ref{eq:recursive-waiting-time-others} and \ref{eq:recursive-waiting-time-ambulance}.
The resultant blocking time formula is given by:

\begin{equation}\label{eq:algebraic-blocking-time}
    B = \frac{\sum_{(u,v) \in S_A} \pi_{(u,v)} \; b(u,v)}{\sum_{(u,v) \in S_A}
    \pi_{(u,v)}}
\end{equation}

\subsection{Mean proportion of arrivals within time target}

Another performance measure that needs to be taken into consideration is the
proportion of individuals whose waiting and service times lie within a specified
time target.
In order to consider such measure though one would need to obtain the
distribution of time in the system for all individuals. The complexity of such
a task lies on the fact that different individuals arrive at different states of
the Markov model. Let us first consider the case when an arrival occurs when the
model is at a specific state.

\subsubsection{Distribution of time at a specific state (with 1 server)}

\begin{figure}[h]
    \centering
    \scalebox{0.75}{\input{MarkovChain/expressions_from_pi/example_model_1242/main.tex}}
    \caption{Example Markov model \(C=1, T=2, N=4, M=2\)}
    \label{fig:distribution-of-time-at-specific-state-1-server}
\end{figure}

Consider the Markov model of figure
\ref{fig:distribution-of-time-at-specific-state-1-server} with one server and a
threshold of two individuals.
Let us now assume that a class 1 individual arrives when the model is at state
\((0,3)\), thus forcing the model to move to state \((0,4)\).
The distribution of the time needed for the specified individual to exit the
system from state \((0,4)\) is given by the sum of exponentially distributed
random variables with the same parameter \(\mu\).
The sum of such random variables form the Erlang distribution which is defined
by the number of random variables that are added and their exponential
parameter.
Note here that these random variables represent the individual's pathway from
the perspective of the individual.
Thus, \(X_i\) represents the random variable of the time that it takes for an
individual to move from the \(i^{\text{th}}\) position of the queue to the
\((i-1)^{\text{th}}\) position (i.e. for someone in front of them to finish
their service) and \(X_0\) is the time it takes that individual to move from
having a service to exiting the system.

\begin{align}
    (0,4) \Rightarrow \quad & X_3 \sim Exp(\mu) \nonumber \\
    (0,3) \Rightarrow \quad & X_2 \sim Exp(\mu) \nonumber \\
    (0,2) \Rightarrow \quad & X_1 \sim Exp(\mu) \nonumber \\
    (0,1) \Rightarrow \quad & X_0 \sim Exp(\mu) \nonumber \\
    S = X_3 + X_2 + & X_1 + X_0 = Erlang(4, \mu)
\end{align}

Thus, the waiting and service time of an individual in the model of figure
\ref{fig:distribution-of-time-at-specific-state-1-server} can be captured by an
erlang distributed random variable.
The general CDF of the erlang distribution \(Erlang(k, \mu)\) is given by:

\begin{equation} \label{eq:cdf-erlang}
    P(S < t) = 1 - \sum_{i=0}^{k-1} \frac{1}{i!} e^{-\mu t} (\mu t)^i
\end{equation}

Unfortunately, the erlang distribution can only be used for the sum of
identically distributed random variables from the exponential distribution.
Therefore, this approach cannot be used when one of the random variables has a
different parameter than the others.
In fact the only case where we can use it is only when the number of servers are
\(C=1\), just like in the explored example, or when an individual arrives and
goes straight to service (i.e. when there is no other individual waiting and
there is an empty server).

\subsubsection{Distribution of time at a specific state (with multiple servers)}

\begin{figure}[h]
    \centering
    \scalebox{0.75}{\input{MarkovChain/expressions_from_pi/example_model_2242/main.tex}}
    \caption{Example Markov model \(C=2, T=2, N=4, M=2\)}
    \label{fig:distribution-of-time-at-specific-state-2-servers}
\end{figure}

Figure \ref{fig:distribution-of-time-at-specific-state-2-servers} represents the
same Markov model as figure
\ref{fig:distribution-of-time-at-specific-state-1-server} with the only
exception that there are 2 servers here.
By applying the same logic, assuming that an individual arrives at state
\((0,4)\), the sum of the following random variables arises.

\begin{align}
    (0,4) \Rightarrow \quad & X_2 \sim Exp(2\mu) \nonumber \\
    (0,3) \Rightarrow \quad & X_1 \sim Exp(2\mu) \\
    (0,2) \Rightarrow \quad & X_0 \sim Exp(\mu) \nonumber
\end{align}

Since these exponentially distributed random variables do not share the same
parameter, an erlang distribution cannot be used.
In fact, the problem can now be viewed either as the sum of exponentially
distributed random variables with different parameters or as the sum of
erlang distributed random variables.
The sum of erlang distributed random variables is said to follow the
hypoexponential distribution.
The hypoexponential distribution is defined with two vectors of size equal
to the number of Erlang random variables that are added together
\cite{Akkouchi2008}, \cite{Smaili2013}.
For this particular example:

\begin{align} \label{eq:multiple-servers-distribution-example}
    \begin{rcases}
        \begin{rcases}
            X_2 \sim Exp(2\mu) \\
            X_1 \sim Exp(2\mu)
        \end{rcases}
        X_1 + X_2 = S_1 \sim Erlang(2, 2\mu) \\
        X_0 \sim Exp(\mu) \Rightarrow \hspace{1cm} X_0 = S_2 \sim Erlang(1, \mu)
    \end{rcases}
    S_1 + S_2 = H \sim Hypo((2,1), (2\mu, \mu))
\end{align}

The random variable \(H\) from equation
\ref{eq:multiple-servers-distribution-example} follows a hypoexponential
distribution with two vector parameters.
The CDF of this distribution can be therefore used to get the probability of the
time in spent in the system being less than a given target.
The CDF of the general hypoexponential distribution \(Hypo(\vec{r},
\vec{\lambda})\), is given by the following expression, where vector \(\vec{r}\)
contains all \(k\)-values of the erlang distributions and \(\vec{\lambda}\)
is a vector of the distinct parameters \cite{Favaro2010}.

\begin{align} \label{eq:general-cdf-hypoexponential}
    & P(H < t) = 1 - \left( \prod_{j=1}^{\mid \vec{r} \mid} \lambda_j^{r_j} \right)
    \sum_{k=1}^{\mid \vec{r} \mid} \sum_{l=1}^{r_k} \frac{\Psi_{k,l}(-\lambda_k)t^{r_k - l}
    e^{-\lambda_k t}}
    {(r_k - l)! (l - 1)!} \nonumber \\
    & \textbf{where} \qquad \Psi_{k,l}(t) = - \frac{\partial^{l - 1}}
    {\partial t ^{l - 1}} \left( \prod_{j = 0, j \neq k}^{\mid \vec{r} \mid}
    (\lambda_j + t)^{-r_j} \right) \nonumber \\
    & \textbf{and} \quad \qquad \lambda_0 = 0, r_0 = 1
\end{align}

The computation of the derivative makes equation \ref{eq:general-cdf-hypoexponential}
computationally expensive.
In \cite{Legros2015} an alternative linear version of that CDF is explored via
matrix analysis, and is given by the following formula:

\begin{equation} \label{eq:linear-general-cdf-hypoexponential}
    \begin{split}
        F(x) = &1 - \sum_{k=1}^{n} \sum_{l=0}^{k-1} (-1)^{k-1} \binom{n}{k}
            \binom{k-1}{l} \sum_{j=1}^{n} \sum_{s=1}^{j-1} e^{-x \lambda_s}
            \prod_{l=1}^{s-1} \left( \frac{\lambda_l}{\lambda_l - \lambda_s} \right)
            ^ {k_s} \\
        & \times \sum_{s < a_1 < \dots < a_{l-1} < j}
            \left( \frac{\lambda_s}{\lambda_s - \lambda_{a_1}} \right) ^ {k_s}
            \prod_{m=s+1}^{a_1-1} \left( \frac{\lambda_m}{\lambda_m -
            \lambda_{a_1}}\right) ^ {k_m}
            \prod_{n=a_1}^{a_2-1} \left( \frac{\lambda_n}{\lambda_n -
            \lambda_{a_2}}\right) ^ {k_n} \\
        & \dots \prod_{r=a_l-1}^{j-1} \left( \frac{\lambda_r}{\lambda_r -
            \lambda_{a_j}}\right) ^ {k_r}
            \sum_{q=0}^{k_s - 1} \frac{((\lambda_s - \lambda_{a_1})x)^q}{q!}, \\
        & \text{for } \geq 0
    \end{split}
\end{equation}

\subsubsection{Specific CDF of hypoexponential distribution}
Equations \ref{eq:general-cdf-hypoexponential} and
\ref{eq:linear-general-cdf-hypoexponential} refers to the general CDF of the
hypoexponential distribution where the size of the vector parameters can be of
any size \cite{Favaro2010}.
In the Markov chain models described in figures
\ref{fig:distribution-of-time-at-specific-state-1-server} and
\ref{fig:distribution-of-time-at-specific-state-2-servers} the parameter vectors
of the hypoexponential distribution are of size two, and in fact, for any
possible version of the investigated Markov chain model the vectors can only be
of size two.
This is true since for any dimensions of this Markov chain model there will
always be at most two distinct exponential parameters; the parameter for
finishing a service (\(\mu\)) and the parameter for moving forward in the queue
(\(C \mu\)).
For the special case of \(C=1\) the hypoexponential distribution will not be
used as this is equivalent to an erlang distribution.
Therefore, by fixing the sizes of \(\vec{r}\) and \(\vec{\lambda}\) to 2, the
following specific expression for the CDF of the hypoexponential distribution
arises, where the derivative is removed:

\begin{align} \label{eq:specific-cdf-hypoexponential}
    & P(H < t) = 1 - \left( \prod_{j=1}^{\mid \vec{r} \mid} \lambda_j^{r_j} \right)
    \sum_{k=1}^{\mid \vec{r} \mid} \sum_{l=1}^{r_k} \frac{\Psi_{k,l}(-\lambda_k)t^{r_k - l}
    e^{-\lambda_k t}}{(r_k - l)! (l - 1)!} \nonumber \\
    & \textbf{where} \qquad \Psi_{k,l}(t) =
    \begin{cases}
        \frac{(-1)^{l} (l-1)!}{\lambda_2} \left[\frac{1}{t^l} - \frac{1}
        {(t + \lambda_2)^l}\right] , & k=1 \\
        - \frac{1}{t (t + \lambda_1)^{r_1}}, & k=2
    \end{cases} \nonumber \\
    & \textbf{and} \quad \qquad \lambda_0 = 0, r_0 = 1
\end{align}

Note here that the only difference between equation
\ref{eq:general-cdf-hypoexponential} and \ref{eq:specific-cdf-hypoexponential}
is the \(\Psi\) function. The next section proves why the following expression
is true:

\begin{equation} \label{eq:hypoexponential-expression-to-proof}
    - \frac{\partial^{l - 1}}{\partial t ^{l - 1}}
    \left( \prod_{j = 0, j \neq k}^{\mid \vec{r} \mid} (\lambda_j + t)^{-r_j} \right) =
    \begin{cases}
        \frac{(-1)^{l} (l-1)!}{\lambda_2} \left[\frac{1}{t^l} - \frac{1}
        {(t + \lambda_2)^l}\right] , & k=1 \\
        - \frac{1}{t (t + \lambda_1)^{r_1}}, & k=2
    \end{cases}
\end{equation}

\subsubsection{Proof of specific hypoexponential distribution
\ref{eq:hypoexponential-expression-to-proof}}

This section aims to show that there exists a simplified expression of equation
\ref{eq:general-cdf-hypoexponential} that is specific to the proposed Markov
model.
Function \(\Psi\) is defined using the parameter \(t\) and the variables \(k\)
and \(l\).
Given the Markov model, the range of values that \(k\) and \(l\) can take can be
bounded.
First of all, from the range of the double summation in equation
\ref{eq:general-cdf-hypoexponential}, it can be seen that
\(k = 1, 2, \dots, \mid \vec{r} \mid\).
Now, \(\mid \vec{r} \mid\) represents the size of the parameter vectors that,
for the Markov model, will always be 2.
That is because, for all the exponentially distributed random variables that are
added together to form the new distribution, there only two distinct parameters,
thus forming two erlang distributions. Therefore:

\begin{equation*}
    k = 1, 2
\end{equation*}

By observing equation \ref{eq:general-cdf-hypoexponential} once more, the range
of values that \(l\) takes are \(l = 1, 2, \dots, r_k\), where \(r_1\) is
subject to the individual's position in the queue and \(r_2 = 1\).
In essence, the hypoexponential distribution will be used with these bounds:

\begin{align}
    k = 1 & \qquad \Rightarrow \qquad l = 1, 2, \dots, r_1 \nonumber \\
    k = 2 & \qquad \Rightarrow \qquad l = 1
\end{align}

Thus the left hand side of equation \ref{eq:hypoexponential-expression-to-proof}
needs only to be defined for these bounds.
The specific hypoexponential distribution investigated here is of the form
\(Hypo((r_1, 1)(\lambda_1, \lambda_2))\).
Note the initial conditions \(\lambda_0=0, r_0=1\) defined in equation
\ref{eq:general-cdf-hypoexponential} also hold here.
Thus the proof is split into two parts, for \(k=1\) and \(k=2\).

\begin{itemize}
    \item \(k = 2, l = 1\)
    \begin{equation*}
        \begin{split}
            LHS &= - \frac{\partial^{1-1}}{\partial t^{1-1}}
            \left( \prod_{j=0, j \neq 2}^{2} (\lambda_j + t)^{-r_j} \right) \\
            &=-\left( (\lambda_0 + t)^{-r_0} \times (\lambda_1 + t)^{-r_1} \right) \\
            &=-\left( t^{-1} \times (\lambda_1 + t)^{-r_1} \right) \\
            &= - \frac{1}{t(t + \lambda_1)^{r_1}} \\
            & \hspace{7cm} \square
        \end{split}
    \end{equation*}
    \item \(k = 1, l = 1, \dots, r_1\)
    \begin{equation*}
        \begin{split}
            LHS &= -\frac{\partial^{l-1}}{\partial t^{l-1}}
            \left( \prod_{j=0, j \neq 1}^{2} (\lambda_j + t)^{-r_j} \right) \\
            &= -\frac{\partial^{l-1}}{\partial t^{l-1}}
            \left( (\lambda_o + t)^{-r_0} \times (\lambda_2 + t)^{-r_2} \right) \\
            &= -\frac{\partial^{l-1}}{\partial t^{l-1}}
            \left( \frac{1}{t(t + \lambda_2)}\right)
        \end{split}
    \end{equation*}
    In essence, it only remains to show that
    \(-\frac{\partial^{l-1}}{\partial t^{l-1}}
    \left( \frac{1}{t(t + \lambda_2)}\right) = \frac{(-1)^{l} (l-1)!}{\lambda_2}
    \left[\frac{1}{t^l} - \frac{1}{(t + \lambda_2)^l}\right]\).

    \textbf{Proof by Induction:}
    \begin{enumerate}
        \item Base case (\(l=1\)):
        \begin{equation*}
            \begin{split}
                LHS &= -\frac{\partial^{1-1}}{\partial t^{1-1}}
                \left( \frac{1}{t(t + \lambda_2)}\right) =
                - \frac{1}{t(t + \lambda_2)} \\
                RHS &= \frac{(-1)^{1} (1-1)!}{\lambda_2}
                \left[\frac{1}{t^1} - \frac{1}{(t + \lambda_2)^1}\right] \\
                &= - \frac{t + \lambda_2 - t}{\lambda_2 t (t + \lambda_2)} \\
                &= - \frac{1}{t (t + \lambda_2)} \\
                LHS &= RHS
            \end{split}
        \end{equation*}
        \item Assume true for \(l = x\):
        \begin{equation*}
            -\frac{\partial^{x-1}}{\partial t^{x-1}}
            \left( \frac{1}{t(t + \lambda_2)}\right) =
            \frac{(-1)^{x} (x-1)!}{\lambda_2}
            \left[\frac{1}{t^x} - \frac{1}{(t + \lambda_2)^x}\right]
        \end{equation*}
        \item Prove true for \(l = x + 1\):
        \(\Bigg( \frac{\partial^x}{\partial t ^ x}
        \left( \frac{-1}{t (t + \lambda_2)} \right) =
        \frac{(-1)^{x + 1} (x)!}{\lambda_2}
        \left[\frac{1}{t^{x+1}}-\frac{1}{(t + \lambda_2)^{x+1}}\right] \Bigg)\)
        \begin{equation*}
            \begin{split}
                LHS &= \frac{\partial}{\partial t}
                \left[ \frac{\partial^{x-1}}{\partial t ^ {x-1}}
                \left( \frac{-1}{t (t + \lambda_2)} \right) \right] \\
                &= \frac{\partial}{\partial t} \left[
                    \frac{(-1)^x (x-1)!}{\lambda_2} \left(
                        \frac{1}{t^x} - \frac{1}{(t + \lambda_2)^x}
                    \right)
                \right] \\
                &= \frac{(-1)^x (x-1)!}{\lambda_2} \left(
                    \frac{(-x)}{t^{x+1}} - \frac{(-x)}{(t + \lambda_2)^x}
                \right) \\
                &= \frac{(-1)^x (x-1)! (-x)}{\lambda_2} \left(
                    \frac{1}{t^{x+1}} - \frac{1}{(t + \lambda_2)^x}
                \right) \\
                &= \frac{(-1)^{x+1} (x)!}{\lambda_2} \left(
                    \frac{1}{t^{x+1}} - \frac{1}{(t + \lambda_2)^x}
                \right) \\
                & = RHS \\
                & \hspace{7cm} \square
            \end{split}
        \end{equation*}
    \end{enumerate}
\end{itemize}

\subsubsection{Proportion within target for class 1 and class 2 individuals}

Given the two CDFs of the Erlang and Hypoexponential distributions a new
function has to be defined to decide which one to use among the two.
Based on the state of the model, there can be three scenarios when an individual
arrives.
\begin{enumerate}
    \item There is a free server and the individual does not have to wait
    \begin{equation*}
        X_{(u,v)} \sim Erlang(1, \mu)
    \end{equation*}
    \item The individual arrives at a queue at the \(n^{th}\) position and the
    model has \(C > 1\) servers
    \begin{equation*}
        X_{(u,v)} \sim Hypo((n, 1), (C \mu, \mu))
    \end{equation*}
    \item The individual arrives at a queue at the \(n^{th}\) position and the
    model has \(C = 1\) servers
    \begin{equation*}
        X_{(u,v)} \sim Erlang(n + 1, \mu)
    \end{equation*}
\end{enumerate}

Note here that for the first case \(Erlang(1, \mu)\) is equivalent to
\(Exp(\mu)\).
Let us define \(X_{(u,v)}^{(1)}\) the distribution of class 1 individuals and
\(X_{(u,v)}^{(2)}\) the distribution of class 2 individuals, when arriving at
state \((u,v)\) of the model.

\begin{equation}
    X_{(u,v)}^{(1)} \sim
    \begin{cases}
        \textbf{Erlang}(v, \mu), & \textbf{if } C = 1 \textbf{ and } v>1 \\
        \textbf{Hypo}\left(\vec{r}=(v - C, 1), \vec{\lambda}=(C \mu, \mu)\right),
            & \textbf{if } C > 1 \textbf{ and } v>C \\
        \textbf{Erlang}(1, \mu), & \textbf{if } v \leq C
    \end{cases}
\end{equation}

\begin{equation}
    X_{(u,v)}^{(2)} \sim
    \begin{cases}
        \textbf{Erlang}(\min(v, T), \mu), & \textbf{if } C = 1
            \textbf{ and } v, T > 1 \\
        \textbf{Hypo}\left(\vec{r}=(\min(v, T) - C, 1), \vec{\lambda}=(C \mu, \mu)\right), &
            \textbf{if } C > 1 \textbf{ and } v, T  > C \\
        \textbf{Erlang}(1, \mu), & \textbf{if } v \leq C \textbf{ or } T \leq C
    \end{cases}
\end{equation}

Equations \ref{eq:cdf-erlang} and \ref{eq:specific-cdf-hypoexponential} can now
be used.
Therefore, the probability that an individual arriving at a specific state is
within a given time target \(t\) is given by the following formulas:

\begin{equation}
    P(X_{(u,v)}^{(1)} < t) =
    \begin{cases}
        1 - \sum_{i=0}^{v-1} \frac{1}{i!} e^{-\mu t} (\mu t)^i,
            & \textbf{if } C = 1 \textbf{ and } v>1 \\
        & \\
        1 - (\mu C)^{v-C} \mu
            \sum_{k=1}^{\mid \vec{r} \mid} \sum_{l=1}^{r_k}
            \frac{\Psi_{k,l}(-\lambda_k)t^{r_k - l}
            e^{-\lambda_k t}}{(r_k - l)! (l - 1)!},
            & \textbf{if } C > 1 \textbf{ and } v > C \\
        \textbf{where } \vec{r}=(v - C, 1) \textbf{ and }
            \vec{\lambda}=(C \mu, \mu) & \\
        & \\
        1 - e^{-\mu t},  & \textbf{if } v \leq C
    \end{cases}
\end{equation}

\begin{equation}
    P(X_{(u,v)}^{(2)} < t) =
    \begin{cases}
        1 - \sum_{i=0}^{\min(v,T)-1} \frac{1}{i!} e^{-\mu t} (\mu t)^i,
            & \textbf{if } C = 1 \textbf{ and } v, T > 1 \\
        & \\
        1 - (C \mu ) ^ {\min(v,T) - C} \mu
            \sum_{k=1}^{\mid \vec{r} \mid} \sum_{l=1}^{r_k}
            \frac{\Psi_{k,l}(-\lambda_k)t^{r_k - l}
            e^{-\lambda_k t}}{(r_k - l)! (l - 1)!},
            & \textbf{if } C > 1 \textbf{ and } v, T  > C \\
        \textbf{where } \vec{r}=(\min(v, T) - C, 1) \textbf{ and }
            \vec{\lambda}=(C \mu, \mu) & \\
        & \\
        1 - e^{-\mu t}, & \textbf{if } v \leq C \textbf{ or } T \leq C
    \end{cases}
\end{equation}

In addition the set of accepting states for class 1 (\(S_A^{(1)}\)) and class 2
(\(S_A^{(2)}\)) individuals defined in  (\ref{eq:accepting_states_class_1}) and
(\ref{eq:accepting_states_class_2}) are also needed here.
Note here that, \(S\) denotes the set of all states of the Markov chain model.

\begin{align*}
    S_A^{(1)} &= \{(u, v) \in S \; | \; v < N \} \\
    S_A^{(2)} &=
    \begin{cases}
        \{(u, v) \in S \; | \; u < M \}, & \textbf{if } T \leq N \\
        \{(u, v) \in S \; | \; v < N \}, & \textbf{otherwise}
    \end{cases}
\end{align*}

Having defined everything, it only remains to use a formula similar to the ones
of equations \ref{eq:recursive-waiting-time-others},
\ref{eq:recursive-waiting-time-ambulance} and
\ref{eq:general-algebaric-approach-blocking-time}.
The following formula uses the state probability vector \(\pi\) to get the
weighted average of the probability below target of all states in the Markov
model.

\begin{equation}
    P(X^{(1)} < t) = \frac{\sum_{(u,v) \in S_A^{(1)}} P(X_{u,v}^{(1)} < t)
    \pi_{u,v} }{\sum_{(u,v) \in S_A^{(1)}} \pi_{u,v}}
\end{equation}

\begin{equation}
    P(X^{(2)} < t) = \frac{\sum_{(u,v) \in S_A^{(2)}} P(X_{u,v}^{(2)} < t)
    \pi_{u,v} }{\sum_{(u,v) \in S_A^{(2)}} \pi_{u,v}}
\end{equation}

\subsubsection{Overall proportion within target}

The overall proportion of individuals for both class 1 and class 2 individuals
is given by the equivalent formula of equations
(\ref{eq:overall_waiting_time_coeff}) and (\ref{eq:overall_waiting_time}).
The following formula uses the probability of lost individuals from both classes
to get the weighted sum of the two already existing probabilities.

\begin{equation*}
    P(L'_1) = \sum_{(u,v) \, \in S_A^{(1)}} \pi(u,v), \hspace{1.5cm}
    P(L'_2) = \sum_{(u,v) \, \in S_A^{(2)}} \pi(u,v)
\end{equation*}

\begin{equation}\label{eq:overall_proportion_within_target}
    P(X < t)= \frac{\lambda_1 P(L'_1)}{\lambda_2 P(L'_2)+\lambda_1 P(L'_1)}
    P(X^{(1)} < t) + \frac{\lambda_2 P(L'_2)}{\lambda_2 P(L'_2) + \lambda_1
    P(L'_1)} P(X^{(2)} < t)
\end{equation}


\documentclass[a4paper, 12pt]{article}

\usepackage{graphicx}

\title{P-values behaving badly}
\author{Brendon J. Brewer}

\begin{document}

\maketitle

Criticising P-values is a favorite pastime among Bayesians. No
``counterexamples'' repository would be complete without it.

\section{{\em Or} implies {\em more}}
I learned this example from Alex Etz ({\tt http://alexanderetz.com/}).
Consider two propositions, $A$ and $B$, which you are trying to reason about.
If you have some judgment about how plausible $A$ is, what would you think
about the proposition $A \vee B$ (read $A$ {\bf or} $B$), which would be true
if either one of $A$ or $B$ is true (or both)?

In probability theory (i.e. Bayesian statistics), the sum rule relates the
probability of $A \vee B$ to the probability of $A$ and the probability of
$B$ like so:
\begin{eqnarray}
P(A \vee B | I) &=& P(A | I) + P(B | I) - P(A, B | I)
\end{eqnarray}

From this, we can show
\begin{eqnarray}
P(A \vee B | I) &=& P(A | I) + P(B | I) - P(A, B | I)\\
                &=& P(A | I) + P(B | I) - P(A | I)P(B | A, I)\\
                &=& P(A | I)\left[1 - P(B | A,I)\right] + P(B | I)\\
                &\geq& P(B | I).
\end{eqnarray}
Therefore the probability of $(A \vee B)$ must be greater than or equal to
the probability of $B$. As a slogan, ``{\em or} implies {\em more}''.

Imagine a drug company has an old drug which is known to cure
75\% of patients of a disease. They are developing a new drug
which they hope will be even more effective.
The effectiveness of the old drug is $\theta_{\rm old} = 0.6$ and the
effectiveness of the new drug is $\theta_{\rm new}$, which is unknown.
Consider three ``coarse-grained''
hypotheses about the value of $\theta_{\rm new}$. The new drug's
effectiveness could be worse than, equal to, or better than that of the old
drug:

\begin{equation}
\begin{array}{lcl}
H_{\rm worse}:  &  &\theta_{\rm new} < 0.6\\
H_0:            &  &\theta_{\rm new} = 0.6\\
H_{\rm better}: &  &\theta_{\rm new} > 0.6
\end{array}
\end{equation}

The middle proposition, $H_0$,
is the ``null hypothesis'' that the new drug and the
old drug are identical in terms of effectiveness. Classical statistical tests
are used to quantify the strength of the evidence against $H_0$. In reality,
it's best to consider the full set of hypotheses about what the value of
$\theta_{\rm new}$ might be, rather than boiling it down to these three
scenarios. This is related to the idea
that ``statistical significance'' and ``practical significance'' are not the
same. For example, $\theta_{\rm new}$ could be 0.60002, which is
technically ``better'' than the old drug, but not by an amount that's likely
to matter.

To measure $\theta_{\rm new}$, the company tests the drug on $N=50$ patients
and counts the number, $x$, who recover. The probability distribution for
$x$ given $\theta_{\rm new}$ (and $N$) is Binomial(50, $\theta_{\rm new}$):

\begin{eqnarray}
p(x | \theta_{\rm new}) &=&
\left(\begin{array}{cc}N \\ x\end{array}\right)
\theta_{\rm new}^x \left(1 - \theta_{\rm new}\right)^{N - x}.
\end{eqnarray}

\begin{figure}[ht!]
\centering
\includegraphics[scale=0.5]{drug_example.pdf}
\caption{\label{fig:drug_example}}
\end{figure}

Suppose $x=37$ was observed.
\begin{eqnarray}
P(x \geq 37 | H_0) &=& 0.028\\
P(x \leq 23 | H_0) &=& 0.031.
\end{eqnarray}

The p-value is supposed to quantify evidence against $H_0$.

Can we interpret it as something like {\em how strongly we should believe in
$H_0$?} Something like a posterior probability?
And do we use the one sided p-value or the two sided?

\begin{eqnarray}
\left(H_0 \vee H_{\rm better}, \textnormal{data}\right) &\Longrightarrow&
\textnormal{p-value} = 0.028\\
\left(H_{\rm worse} \vee H_0 \vee H_{\rm better}, \textnormal{data}\right) &\Longrightarrow&
\textnormal{p-value} = 0.059.
\end{eqnarray}

\end{document}


% !TEX root = index.tex

\section{Geometric Meaning of Curvature}

\subsection{Mean Curvature}
  The Mean Curvature shows up in physics while studying soap films. At a point on a soap film the difference between the pressure on two sides is proportional to the mean curvature of the surface at that point. This is called the \textbf{Young-Laplace equation}.
  \begin{align*}
    \Delta (\mbox{pressure}) \propto H
  \end{align*}
  If the soap film does not bound a volume, for example, if it is bounded by a curve then the pressure on both the sides is the same and hence the mean curvature at every point must be 0. Such surfaces are called \textbf{minimal surfaces}, minimal because these surfaces also have the minimal surface area of all the surfaces bounding the curve. The study of minimal surfaces is a very active area of research in geometry.
  \begin{figure}[H]
    \centering
    \begin{subfigure}[t]{0.495\textwidth}
      \centering
      \includegraphics[width=6cm]{Minimal_Surface_1}
    \end{subfigure}
    \begin{subfigure}[t]{0.495\textwidth}
      \centering
      \includegraphics[width=6cm]{Minimal_Surface_2}
    \end{subfigure}
    \caption{Examples of minimal surfaces. The Mean Curvature at \emph{every} non-boundary point is 0 and hence at every point the surface looks like the perfect potato chip. Images from \href{https://en.wikipedia.org/wiki/Minimal_surface}{Wikipedia}.}
  \end{figure}

\subsection{Gaussian Curvature}
  The Gaussian Curvature is much more subtle and has several interpretations.

  \begin{definition}
    For a surface $S$ the \textbf{geodesic distance} $d_S(p,q)$ between two points $p,q \in S$ is defined to be the shortest length of the curve on the surface $S$ that connects $p$ to $q$.
  \end{definition} For example, on a plane the geodesic distance is simply the Euclidean distance. On a sphere, the geodesic distance between two points is the length of the arc of the great circle connecting them.
  \begin{figure}[H]
    \centering
      \includegraphics[width=3cm]{great_circle}
    \caption{The geodesic distance between two points on a sphere is the length of the arc of the great circle connecting them. Image from \href{https://en.wikipedia.org/wiki/Great-circle_distance}{Wikipedia}.}
  \end{figure}

  \begin{definition}
    The \textbf{geodesic ball} of radius $r$ centered at a point $p \in S$ is the set of points which are at a geodesic distance of at the most $r$ from $p$
    \begin{align*}
      B_r(p) := \{ x \in S : d_S(x,p) \le r\}
    \end{align*}
  \end{definition}
We'll assume the following theorem without proof.
  \begin{thm}
    The Gaussian curvature of $S$ at $p$ equals
    \begin{align*}
      K &= 3 \lim \limits_{r \rightarrow 0} \dfrac{2 \pi r - \mbox{length of } \partial B_S(p,r) }{\pi r^3}
    \end{align*}
  \end{thm}
\begin{cor}
  \label{thm:geodesics}
  The Gaussian curvature can be computed by measuring distances on the surface (without knowing anything about the ambient space).
\end{cor}
This leads directly to the next theorem.

  \subsubsection{Theorema Egregium}
  We can take a sheet of paper and roll into a cylinder without stretching or compressing the sheet.\footnote{Neglect the \emph{thickness} of the paper.} Such a map is called an \textbf{isometry}.

  \begin{definition}
    A smooth map $\phi: S \rightarrow S'$ is called an \textbf{isometry} if
    \begin{align*}
      d_S(p,q) = d_{S'}(\phi(p),\phi(q))
    \end{align*}for any two points $p,q \in S$.
  \end{definition}
  \noindent The map that sends a plane to a cylinder is an example of such an isometry.
  \begin{thm}[Theorema Egregium]
    \label{thm:theorema}
    If there exists an isometry $\phi: S \rightarrow S'$ between two surfaces then the Gaussian curvature of $S$ at $p \in S$ equals the Gaussian curvature of $S'$ at $\phi(p)$.
  \end{thm}
  \begin{proof}
    This is a direct consequence of Corollary \ref{thm:geodesics}. It is possible to measure the Gaussian curvature using only the geodesic distances and geodesic distances are preserved under isometry.
  \end{proof}
  This theorem is interpreted as saying that the Gaussian curvature is \emph{intrinsic} to a surface.

\subsubsection{Gauss Map}
\begin{figure}[H]
  \centering
    \includegraphics[width=8cm]{Gauss_map}
  \caption{The unit normal vector $\vec n$ defines a map from the surface to the unit sphere $\vec n : S \rightarrow S^2$. Image from \href{https://en.wikipedia.org/wiki/Gauss_map}{Wikipedia}.}
\end{figure}
Let $\vec n$ denote a continuously varying unit normal vector field on the surface $S$. There are two possibly choices for $\vec n$, we just pick one. We can think of $\vec n$ as a map, called the \textbf{Gauss map} from $S$ to the unit sphere $S^2$. Then,
\begin{align*}
  K &= \lim \limits_{r \rightarrow 0} \dfrac{\mbox{area of } \vec n (B_r(p)) }{\mbox{area of } B_r(p) }
\end{align*}

\subsubsection{Gauss-Bonnet theorem}
Gaussian curvature has a topological significance as well. If $S$ is a closed surface then
\begin{thm}
  The total Gaussian Curvature
  \begin{align*}
    \int \limits_S K \: dA = 2 \pi \chi(S)
  \end{align*}
  where $\chi(S)$ denotes the Euler characteristic of the surface.
\end{thm}

\subsection{Final Remarks}
The methods that we've described only define the various curvatures for \emph{embedded} surfaces. In general, because the Gaussian curvature can be computed by measuring Geodesic distances it is possible to define the Gaussian curvature (but not the principal and mean curvatures) for manifolds with a metric (also called Riemannian manifold).


% MARKER: check positioning of the figures/tables and put float barriers and/or clearpages if needed

\section{Structural details of the subassembly}
\label{hier:details}

\begin{equation}
\label{eq:EBMjoint}
EBM_{v,j} = \frac{V_{jh}}{\frac{1}{jd}-\frac{l_{b}}{l_{c}l_{b}^{'}}} = \frac{365(699.5)}{\frac{1}{0.628}-\frac{3.4}{3.05*3.17}} = 294.2 kNm (563.8kN \ Pull)
\end{equation}