\documentclass[a4paper,10pt]{article}
\usepackage{fullpage}
\usepackage{hyperref}
\usepackage[sort&compress,numbers]{natbib}
\usepackage{doi}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{epsfig}
\usepackage{amssymb,amsmath,stmaryrd,tabularx}
\usepackage{wrapfig}
\usepackage{bm}
\usepackage[center]{subfigure}
\usepackage{enumerate}
%Not actually used. You can put multiple includegraphics commands right after each other without spaces without problems. This is only useful for separated captions.
%Prevent indentation of first line in paragraphs.
%\setlength{\parindent}{0pt}

\setlength{\bibsep}{1pt} % or use whatever dimension you want
\renewcommand{\bibfont}{\small}

\usepackage{titlesec}

\titleformat*{\section}{\large\bfseries}
\titleformat*{\subsection}{\large\bfseries}
\titleformat*{\subsubsection}{\large\bfseries}

\renewcommand{\v}[1]{\mathbf{#1}}
\newcommand{\intf}{\textrm{if}}

% Physics header, modified from dfdc.net %
\let\vaccent=\v % rename builtin command \v{} to \vaccent{}
\renewcommand{\v}[1]{\ensuremath{\mathbf{#1}}} % for vectors

\newcommand{\ti}[1]{\ensuremath{\tilde{#1}}} % for dimensionless equations
\renewcommand{\times}{\cdot} % times symbol
\renewcommand{\vec}[1]{\v{#1}} % also for vectors
\newcommand{\gv}[1]{\ensuremath{\mbox{\boldmath$ #1 $}}}
% for vectors of Greek letters
\newcommand{\uv}[1]{\ensuremath{\mathbf{\hat{#1}}}} % for unit vector
\newcommand{\abs}[1]{\left| #1 \right|} % for absolute value
\newcommand{\avg}[1]{\left< #1 \right>} % for average
\let\underdot=\d % rename builtin command \d{} to \underdot{}
\newcommand{\diff}{\mathrm{d}}
\renewcommand{\d}[2]{\frac{\diff #1}{\diff #2}} % for derivatives
\newcommand{\dd}[2]{\frac{\diff^2 #1}{\diff #2^2}} % for double derivatives
%\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\pdinl}[2]{\partial #1 / \partial #2 }
% for partial derivatives
\newcommand{\pdd}[2]{\frac{\partial^2 #1}{\partial #2^2}}
% for double partial derivatives
\newcommand{\pdc}[3]{\left( \frac{\partial #1}{\partial #2}
	\right)_{#3}} % for thermodynamic partial derivatives
\newcommand{\pdcinl}[3]{\left(\partial #1 / \partial #2 \right)_{#3}}
\newcommand{\ket}[1]{\left| #1 \right>} % for Dirac bras
\newcommand{\bra}[1]{\left< #1 \right|} % for Dirac kets
\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right|
	\left. #2 \vphantom{#1} \right>} % for Dirac brackets
\newcommand{\matrixel}[3]{\left< #1 \vphantom{#2#3} \right|
	#2 \left| #3 \vphantom{#1#2} \right>} % for Dirac matrix elements
\newcommand{\grad}[1]{\gv{\nabla} #1} % for gradient
\let\divsymb=\div % rename builtin command \div to \divsymb
\renewcommand{\div}[1]{\gv{\nabla} \cdot #1} % for divergence
\newcommand{\curl}[1]{\gv{\nabla} \times #1} % for curl
\newcommand{\mean}[1]{\left< #1 \right>}
\newcommand{\laplacian}[1]{\grad^2 #1}
\newcommand{\emphy}[1]{\textsc{#1}}
\newcommand{\pdt}[1]{\partial_t #1}
\newcommand{\sym}[1]{\mathrm{sym} #1 }
\newcommand{\inpr}[2]{\left< #1, #2 \right>}

\newcommand{\wall}{\mathrm{wall}}
\newcommand{\inlet}{\mathrm{in}}
\newcommand{\outlet}{\mathrm{out}}

\begin{document}
\section{Equations}
The equations of the system:
\begin{gather}
\begin{split}
\rho(\phi) \pdt \v v + \left( \left(\rho(\phi) \v v - \rho'(\phi) M(\phi) \grad g_\phi  \right) \cdot \grad \right) \v v - \div \left[  \mu(\phi)(\grad \v v + \grad \v v ^T) \right] + \grad p \\
= - \rho_e \grad V - \tilde \sigma \xi\div \left[ \grad \phi \otimes \grad \phi \right] - \frac 1 2 \epsilon'(\phi) | \grad V |^2 \grad \phi,
\end{split}\\
\div \v v = 0,
\end{gather}
\begin{gather}
\pdt \phi + \v v \cdot \grad \phi - \div(M(\phi) \grad g_\phi ) = 0,
\end{gather}
\begin{gather}
\pdt c_i + \v v \cdot \grad c_i - \div \left( D_{i} (\phi) \grad c_i + \frac{ q_e  z_i}{k_{\rm B} T} D_i(\phi) c_i \grad V +\frac{1}{k_{\rm B} T} D_i(\phi)c_i\beta_i'(\phi) \grad \phi \right) = 0,
\end{gather}
\begin{gather}
\div ( \epsilon (\phi) \grad V ) = - \rho_e.
\end{gather}
\begin{align}
g_\phi &= \tilde\sigma\xi^{-1}W'(\phi) - \tilde\sigma\xi\laplacian\phi + \sum_i \beta_i'(\phi) c_i - \frac{1}{2} \epsilon'(\phi) | \grad V |^2 ,
\end{align}
\begin{equation}
\tilde p = p - \tilde \sigma \left( \frac{\xi}{2} |\grad \phi|^2 + \xi^{-1} W(\phi) \right) - \sum_i ( k_{\mathrm{B}} T c_i(\log(c_i) -1) + \beta_i(\phi) c_i ) .
\end{equation}

\section{Dimensionless Equations}
\begin{align}
c_i &\propto c_0, ~~~~ V \propto V_T = \frac{k_B T}{z_i q_e}, ~~~~\v{v} \propto U_0 = \frac{\epsilon_D V_T^2}{\mu l}, ~~~~p \propto \frac{\mu U_0}{l},\\
x &\propto l , ~~~~ t \propto \frac{l^2}{D_i}, ~~~~ \rho \propto \rho_0.
\end{align}
Navier-Stokes equation:
\begin{gather}
\begin{split}
\frac{\rho_0 U_0 D}{ l^2}\ti{\rho}(\phi) \ti{\pdt{}} \ti{\v{v}}\\
+ \frac{U_0}{l}\left( \left( \rho_0
 	U_0 \ti\rho(\phi) \ti{\v{v}} - \frac{\rho_0 M_0 g_0}{l}\ti\rho'(\phi) \ti M(\phi) \ti{\grad{}} \ti g_\phi  \right) \cdot \ti{\grad{}} \right) \ti{\v{v}}\\
- \frac{\mu_0 U_0}{l^2}\ti{\div{}} \left[\ti \mu(\phi)(\ti{\grad{}} \ti{\v{v}} + \ti{\grad{}} \ti{\v{v}} ^T) \right] + \frac{\mu_0 U_0}{l^2}\ti{\grad{}} p \\
= - \frac{q_e z_t c_0 V_T}{l} \ti \rho_e \ti{\grad{}} \ti{V} - \frac{\sigma_0 \xi_0 }{l^3}\hat{\ti{\sigma}} \ti \xi\ti{\div{}} \left[ \ti{\grad{}} \phi \otimes \ti{\grad{}} \phi \right] - \frac 1 2 \frac{\epsilon_D V_T^2}{l^3} \ti \epsilon'(\phi) | \ti{\grad{}} \ti V |^2 \ti{\grad{}} \phi,
\end{split}\\
\ti{\div{}} \ti{\v{v}} = 0,
\end{gather}
or
\begin{gather}
\begin{split}
\mathrm{Sc} \ti{\rho}(\phi) \ti{\pdt{}} \ti{\v{v}}\\
+ \left( \left( \mathrm{Re} \ti\rho(\phi) \ti{\v{v}} - \frac{\rho_0 M_0 g_0}{\mu_0}\ti\rho'(\phi) \ti M(\phi) \ti{\grad{}} \ti g_\phi  \right) \cdot \ti{\grad{}} \right) \ti{\v{v}}\\
- \ti{\div{}} \left[\ti \mu(\phi)(\ti{\grad{}} \ti{\v{v}} + \ti{\grad{}} \ti{\v{v}} ^T) \right] + \ti{\grad{}} p \\
= - \frac{-1}{2\tilde{\lambda}_D^2} \ti \rho_e \ti{\grad{}} \ti{V} - \frac{\xi_0 }{l \mathrm{Ca}  }\hat{\ti{\sigma}} \ti \xi\ti{\div{}} \left[ \ti{\grad{}} \phi \otimes \ti{\grad{}} \phi \right] - \frac 1 2 \ti \epsilon'(\phi) | \ti{\grad{}} \ti V |^2 \ti{\grad{}} \phi,
\end{split}\\
\ti{\div{}} \ti{\v{v}} = 0, \mathrm{Sc} = \frac{\rho_0 D}{\mu},\mathrm{Re} = \frac{\rho_0
	U_0 l}{\mu} ,\tilde{\lambda}_D = \frac{1}{l}\sqrt{\frac{\epsilon_D k_B T}{2 c_0 e^2 z^2}}, \mathrm{Ca} = \frac{\mu_0 U_0}{\sigma_0}
\end{gather}
The pressure is defined as
\begin{equation}
\ti{p} = \ti{\mathcal{P}} -   \frac{\sigma_0 \xi_0}{l \mu U_0} \ti{\sigma}\ti{\xi}|\ti{\grad{}} \phi|^2 - \frac{\sigma_0 l}{\xi_0 \mu U_0}\ti{\sigma}\ti{\xi}^{-1} W(\phi)  - \sum_i ( \frac{k_{\mathrm{B}} T c_0 l}{\mu U_0} \ti{c}_i(\log(c_i) -1) + \frac{\beta_0 c_0 l}{\mu U_0}\ti{\beta}_i(\phi) \ti{c}_i ) .
\end{equation}
The phase field
\begin{gather}
\frac{D}{l^2}\ti{\pdt{}} \phi + \frac{U_0}{l}\ti{\v v} \cdot \ti{\grad{}}\ti{\phi} - \frac{M_0 g_0 }{l^2}\ti{\div{}} (\ti{M}(\phi) \ti{\grad{}} \ti{g}_\phi ) = 0,
\end{gather}
or
\begin{gather}
\ti{\pdt{}} \phi + \mathrm{Pe}\ti{\v v} \cdot \ti{\grad{}}\ti{\phi} - \frac{\mathrm{Pe} }{\mathfrak{Pe}} g_0\ti{\div{}} (\ti{M}(\phi) \ti{\grad{}} \ti{g}_\phi ) = 0,
\end{gather}
The "Chemical" Potential
\begin{align}
\tilde{g}_\phi &= \frac{\sigma_0}{\xi_0 g_0 }\hat{\ti{\sigma}}\ti{\xi}^{-1}W'(\phi) - \frac{\sigma_0 \xi_0}{g_0 l^2}\hat{\ti{\sigma}}\ti{\xi}\ti{\laplacian{}}\phi + \frac{\beta_0 c_0}{g_0}\sum_i \ti{\beta}_i'(\phi) \ti{c}_i - \frac{\epsilon_D V_T^2}{2 g_0 l^2} \ti{\epsilon}'(\phi) | \ti{\grad{}} \ti{V} |^2 ,
\end{align}
Nernst-Planck equation
\begin{gather}
\frac{D c_0}{l^2}\ti{\pdt{}} \ti{c}_i + \frac{U_0 c_0}{l}\ti{\v{v}} \cdot \ti{\grad{}} \ti{c}_i - \ti{\div{}} \left( \frac{D c_0}{l^2}\ti{D}_{i} (\phi) \ti{\grad{}} \ti{c}_i + \frac{ q_e  z_i D c_0 V_T}{k_{\rm B} T l^2}\ti{D}_i(\phi)\ti{c}_i \ti{\grad{}} \ti{V} +\frac{D c_0 \beta_0}{k_{\rm B} T l^2}\ti{D}_i(\phi)\ti{c}_i\ti{\beta}_i'(\phi) \ti{\grad{}} \phi \right) = 0,
\end{gather}
or
\begin{gather}
\ti{\pdt{}} \ti{c}_i + \mathrm{Pe}\ti{\v{v}} \cdot \ti{\grad{}} \ti{c}_i - \ti{\div{}} \left( \ti{D}_{i} (\phi) \ti{\grad{}} \ti{c}_i + \mathrm{sign}(z_i)\ti{D}_i(\phi)\ti{c}_i \ti{\grad{}} \ti{V} +\frac{ \beta_0}{k_{\rm B} T }\ti{D}_i(\phi)\ti{c}_i\ti{\beta}_i'(\phi) \ti{\grad{}} \phi \right) = 0,\\
\mathrm{Pe} = \frac{U_0 l}{D }
\end{gather}
Gauss' law:
\begin{gather}
\ti{\div{}} ( \ti{\epsilon} (\phi) \ti{\grad{}} \ti{V} ) = - \frac{\sum_i \mathrm{sign}(z_i) c_i}{\ti{\lambda}_D^2}.
\end{gather}
\newpage
\section{All The Equations}
\begin{align}
c_i &\propto c_0, ~~~~ V \propto V_T = \frac{k_B T}{z_i q_e}, ~~~~\v{v} \propto U_0 = \frac{\epsilon_D V_T^2}{\mu l}, ~~~~p \propto \frac{\mu U_0}{l},\\
x &\propto l , ~~~~ t \propto \frac{l^2}{D_i}, ~~~~ \rho \propto \rho_0, ~~~~ \mathrm{Sc} = \frac{\rho_0 D}{\mu}, ~~~~\mathrm{Re} = \frac{\rho_0 U_0 l}{\mu} \\
\tilde{\lambda}_D &= \frac{1}{l}\sqrt{\frac{\epsilon_D k_B T}{2 c_0 q_e^2 z^2}}, ~~~~ \mathrm{Ca} = \frac{\mu_0 U_0}{\sigma_0}, ~~~~\mathfrak{Pe} = \frac{U_0 l}{M_0}, ~~~~ \mathrm{Pe} = \frac{U_0 l}{D }\\
\xi &\propto \xi_0 = \alpha l, ~~~~ \beta \propto \beta_0 = k_{\mathrm{B}} T
\end{align}
\begin{gather}
\begin{split}
\mathrm{Sc} \ti{\rho}(\phi) \ti{\pdt{}} \ti{\v{v}}
+ \left( \left( \mathrm{Re} \ti\rho(\phi) \ti{\v{v}} - \frac{\mathrm{Re} }{\mathfrak{Pe}}\ti\rho'(\phi) \ti M(\phi) \ti{\grad{}}  g_\phi  \right) \cdot \ti{\grad{}} \right) \ti{\v{v}}\\
- \ti{\div{}} \left[\ti \mu(\phi)(\ti{\grad{}} \ti{\v{v}} + \ti{\grad{}} \ti{\v{v}} ^T) \right] + \ti{\grad{}} \ti{p} \\
= - \frac{-1}{2\tilde{\lambda}_D^2} \ti \rho_e \ti{\grad{}} \ti{V} - \frac{ \xi_0 }{l\mathrm{Ca}}\hat{\ti{\sigma}} \ti \xi\ti{\div{}} \left[ \ti{\grad{}} \phi \otimes \ti{\grad{}} \phi \right] - \frac 1 2 \ti \epsilon'(\phi) | \ti{\grad{}} \ti V |^2 \ti{\grad{}} \phi,
\end{split}\\
\ti{\div{}} \ti{\v{v}} = 0,
\end{gather}
\begin{equation}
\ti{p} = \ti{\mathcal{P}} -   \frac{\alpha}{ \mathrm{Ca}} \ti{\sigma}\ti{\xi}|\ti{\grad{}} \phi|^2 - \frac{ 1}{\alpha \mathrm{Ca}}\ti{\sigma}\ti{\xi}^{-1} W(\phi)  - \sum_i \left( \frac{ 1}{2\ti{\lambda}_D^2} \ti{c}_i(\log(c_i) -1) + \frac{1}{2\ti{\lambda}_D^2}\ti{\beta}_i(\phi) \ti{c}_i \right) .
\end{equation}
\begin{gather}
\ti{\pdt{}} \phi + \mathrm{Pe}\ti{\v v} \cdot \ti{\grad{}}\ti{\phi} - \frac{\mathrm{Pe} }{\mathfrak{Pe}} g\ti{\div{}} \left(\ti{M}(\phi) \ti{\grad{}} g_\phi \right) = 0,,
\end{gather}
\begin{align}
g_\phi &= \frac{l}{\sigma_0}\left(\frac{\sigma_0}{\alpha l  }\hat{\ti{\sigma}}\ti{\xi}^{-1}W'(\phi) - \frac{\sigma_0 \alpha}{ l}\hat{\ti{\sigma}}\ti{\xi}\ti{\laplacian{}}\phi + k_{\mathrm{B}}T c_0\sum_i \ti{\beta}_i'(\phi) \ti{c}_i - k_{\mathrm{B}}T c_0  \ti{\lambda}_D^2\ti{\epsilon}'(\phi) | \ti{\grad{}} \ti{V} |^2\right) ,
\end{align}
\begin{gather}
\ti{\pdt{}} \ti{c}_i + \mathrm{Pe}\ti{\v{v}} \cdot \ti{\grad{}} \ti{c}_i - \ti{\div{}} \left( \ti{D}_{i} (\phi) \ti{\grad{}} \ti{c}_i + \mathrm{sign}(z_i)\ti{D}_i(\phi)\ti{c}_i \ti{\grad{}} \ti{V} +\ti{D}_i(\phi)\ti{c}_i\ti{\beta}_i'(\phi) \ti{\grad{}} \phi \right) = 0,
\end{gather}
\begin{gather}
\ti{\div{}} ( \ti{\epsilon} (\phi) \ti{\grad{}} \ti{V} ) = - \frac{\sum_i \mathrm{sign}(z_i) c_i}{\ti{\lambda}_D^2}.
\end{gather}
\newpage
\section{Equations}
Stokes equation:
\begin{align}
\rho(\psi) \partial_t \v{u}  &=  \nabla P +\eta(\psi) \nabla^2 \mathbf{u} -  \rho_e  \nabla \phi + \mathbf{f}_{if}(\psi),  \\
\nabla \cdot\mathbf{u} &= 0.
\end{align}
$f_{if}$ is the interface force and the rest should be self explained for now. \newline
Poissons equation:
\begin{align}
\nabla\cdot\left(\epsilon_r(\psi)\nabla\phi\right) &= \frac{\rho_e}{\epsilon_0 },
\end{align}
Nernst-Planck equation:
\begin{align}
\partial_t c_i  &=  \nabla  \cdot  \left( - \mathbf{u} c_i  + D_i(\psi) \nabla c_i  +   \frac{D_i(\psi) z_i q_e}{k_B T} c_i \nabla( \phi \right), \\
\rho_e &= \sum_i q_e z_i c_i.
\end{align}
The Phase field equation??
\begin{align}
\partial_t \psi + \mathbf{u}\cdot\nabla\psi &= M\nabla^2\left(\frac{3 \sigma }{W} \psi \left(\psi^2 -1\right) - \frac{3 \sigma W}{8}\nabla^2 \psi\right).
\end{align}
\section{Dimensionless Equations}
The Normalization constants:
\begin{align}
c_i &\propto c_0, ~~~~ \phi \propto V_T = \frac{k_B T}{z_i q_e}, ~~~~\mathbf{u} \propto U_0 = \frac{\epsilon_D V_T^2}{\eta L}, ~~~~p \propto \frac{\eta U_0}{l},\\
x &\propto l, ~~~~ t \propto \frac{l^2}{2 D}, ~~~~ \rho \propto \rho_e.
\end{align}
The factor off two in the time $t$ might be changed. \newline
Stokes dimensionless:
\begin{align}
\frac{2}{Sc}\tilde{\rho}(\psi) \partial_{\tilde{t}} \tilde{\mathbf{u}}  &=  \tilde{\nabla} \tilde{P} + \eta_0(\psi)\tilde{\nabla}^2 \tilde{\mathbf{u}} - \frac{1}{2\tilde{\lambda}^2_D} \tilde{\rho}_e  \tilde{\nabla} \tilde{\phi} + \mathbf{f}_{if_0} \tilde{\mathbf{f}}_{if}(\psi), \\
\tilde{\nabla }\cdot\tilde{\mathbf{u}} &= 0, \\
Sc &= \frac{\eta}{\rho_0,D}, \\
\lambda_D &= \sqrt{\frac{k_B T \epsilon_r \epsilon_D}{2 q_e^2 c_0 z^2}}.
\end{align}
Poissons dimensionless:
\begin{align}
\tilde{\nabla}^2\tilde{\phi}&= \frac{1}{2\tilde{\lambda}_D^2 }\frac{\tilde{\rho}_e}{\tilde{\epsilon}(\psi)}.
\end{align}
Nernst-Planck dimensionless
\begin{align}
\tilde{\rho}_e &= \sum_i \tilde{c}_i, \\
\tilde{\partial}_t\tilde{c}_i &= \tilde{\nabla}\cdot\left(\frac{Pe}{2}\tilde{\mathbf{u}}\tilde{c_i}+\frac{1}{2}\tilde{\nabla}\tilde{c_i}+\frac{1}{2}\tilde{c}_i\tilde{\nabla}\tilde{\phi}\right).
\end{align}
The Phase field dimensionless??
\section{Weak Forms}
The Stokes equation:
\begin{align}
\int_\Omega \frac{2\tilde{\rho}}{Sc\Delta\tilde{t}} \mathbf{v}\cdot\left(\tilde{\mathbf{u}}-\tilde{\mathbf{u}}_0\right) - \mathbf{v}\cdot \nabla \tilde{p} + \tilde{\eta}(\psi)\mathbf{v}\cdot\tilde{\nabla}^2\tilde{u}_{cn} &- \frac{1}{2\tilde{\lambda}_D}\tilde{\rho}_e\mathbf{v}\cdot\tilde{\nabla}\tilde{\phi}\\\nonumber
 &+\frac{l U_0 f_{it_0}}{\eta}\mathbf{v}\cdot\tilde{f}+q\nabla\cdot\tilde{\mathbf{u}}_{cn} dx
\end{align}
\begin{align}
\Rightarrow \int_\Omega \frac{2\tilde{\rho}}{Sc\Delta\tilde{t}} \mathbf{v}\cdot\left(\tilde{\mathbf{u}}-\tilde{\mathbf{u}}_0\right) &- \mathbf{v}\cdot \nabla \tilde{p} -\tilde{\nabla}\left(\tilde{\eta}(\psi)\mathbf{v}\right)\cdot\tilde{\nabla}\tilde{u}_{cn}  -\frac{1}{2\tilde{\lambda}_D}\tilde{\rho}_e\mathbf{v}\cdot\tilde{\nabla}\tilde{\phi}\\\nonumber
&+\frac{l U_0 f_{it_0}}{\eta}\mathbf{v}\cdot\tilde{f}+q\nabla\cdot\tilde{\mathbf{u}}_{cn} dx  +\int_{\partial\Omega}\left(\mathbf{v}\tilde{\eta}(\psi)\tilde{\nabla}\tilde{\mathbf{u}}_{cn}\right)\cdot\mathbf{n}ds
\end{align}

\end{document}


%% SECTION HEADER /////////////////////////////////////////////////////////////////////////////////////
\section{Conclusions}
\label{sec:conclusionsSEM}
%% SECTION CONTENT ////////////////////////////////////////////////////////////////////////////////////
This chapter describes the \ac{sem} implementation for use in modelling \acp{hsc} with full core geometries.
This model requires the development of structural matrices for \ac{2d} and \ac{3d} elements and a transformation of the nodal displacements between the local and global systems.
An original approach was developed to connect elements with non-matching grids using an interface based on Lagrange multipliers.
For this purpose, the shape fusions of common points in the space of connected elements were determined to calculate the traction forces necessary to provide joint displacements.
In addition, a parallel central difference method implementation was optimized for even more efficient computation on the graphics card.


\section{Experiments}
This section details both quantitative and qualitative evaluation of the system described in the previous sections. Due to the lack of animal keypoint datasets in the public domain, it is necessary to construct a new Benchmark Animal Dataset of Joint Annotations (BADJA). BADJA is a dataset comprising several video sequences with hand-clicked 2D joint labels and segmentation masks. Qualitative results are provided on BADJA and also on images used in the evaluation of 3D Menagerie (3DM). However, it should be noted this comparison is not entirely fair, since 3DM requires hand-clicked keypoints as input and system introduced in this chapter is optimized for video input.

\subsection{BADJA Dataset}
The BADJA dataset contains 9 videos, with manually collected keypoint and silhouette annotations. 7 video sequences were obtained from the DAVIS video segmentation dataset~\cite{Perazzi2016} and came with existing silhouettes, and the remainder were sourced from online stock footage. Segementation masks for these were generated using Adobe's UltraKey tool~\cite{adobe_ultrakey}. For all sequences, a set of 20 joints were labelled as illustrated in \figref{badja_examples}. The first 16 joints are located on legs, neck and tail, and defined by the rigged 3D SMAL skeleton. The final joints (nose tip, chin and ears) do not directly relate to 3D SMAL joints, but are instead defined by particular SMAL model vertices. A similar trick is used in SMALify~\cite{bogo16keep} to indicate the position of the human nose which has no corresponding SMPL joint. The collection of joints were chosen on the basis of being of informative to the skeleton and being simple for a non-expert human annotator to localize. To make manual annotation feasible and to ensure a diverse set of data, annotations are provided for every fifth video frame and were collected using the excellent LabelMe tool~\lazycite{LabelMe}{LabelMe}.

The video sequences were selected to comprise a range of different quadrupeds undergoing various movement typical of their species. Although the dataset is perhaps insufficient in size to train deep neural networks, the variety in animal shape and pose renders it suitable for evaluating quadruped joint prediction methods.

\subsection{Joint prediction}
\label{sec:exp-network}
%bjb_insert
For the joint predictor $\rho$ the stacked hourglass network~\cite{newell2016stacked} modified for multi-modal output is trained on synthetic animal silhouette data. Following state-of-the-art performance on related human 2D pose estimation datasets (\cite{andriluka14cvpr,lin2014microsoft}), the network consists of 8 stacks, 256 features and 1 block. Synthetic silhouette images of size $256\times 256$ are provided as input, which are obtained by randomly sampling shape and pose parameters from the SMAL model. The corresponding training targets are ground truth heatmaps produced by smoothing the 2D projected joint locations with a Gaussian kernel. As a benefit of working with synthetic data, training samples can be generated on the fly, which effectively results in a training set of infinite size. A small adaptation was required to prevent the network degenerating to an unfavourable solution on silhouette input: foreground masks were applied to both ground truth silhouette and predicted heatmaps to prevent the network degenerating to an all-zero heatmap, which produces a reasonably good loss and prevents the network training successfully. The network was trained using the RMSProp optimizer for 40k iterations with a batch size of 18 and learning rate of $2.5\times 10^{-4}$. The learning rate was decayed by 5\% every 10k iterations. Training until convergence took 24 hours on a Nvidia Titan X GPU.

Joint accuracy is evaluated with the Probability of Correct Keypoint (PCK) metric defined by Yang and Ramanan~\cite{yang2013articulated}. The PCK is the percentage of predicted keypoints which are within a threshold distance $d$ from the ground truth keypoint location. The threshold distance is given by $d=\alpha\sqrt{|S|}$ where $|S|$ is the area of the silhouette and $\alpha$ is a constant factor which we set to $\alpha=0.2$ for these experiments.

\figref{exp-network} shows a selection of maximum likelihood joint predictions on real world images. Note that despite being trained only on synthetic data, the network generalizes extremely well to animals in the wild. The performance extends even to species which were not present in the SMAL model, such as the impala and rhino. The network is also robust to challenging poses (\ref{fig:exp-network}b), occlusions (\ref{fig:exp-network}c) and distraction objects such as the human rider in (\ref{fig:exp-network}d). It is however susceptible to situations where the silhouette image is ambiguous, for example if the animal is facing directly towards or away from the camera. Figure~\ref{fig:blooper} contains examples of failure modes.

\input{Chapter4/FigTex/badja.tex}

\subsection{Optimal joint assignment}
Following non-maximum suppression of the joint heatmaps obtained in Section~\ref{sec:exp-network}, OJA is applied to select an optimal set of joints which initialize the final 3D model fitting stage. It can be seen that the OJA step is able to address many of the failure cases introduced by the joint prediction network, for example by eliminating physically implausible joint configurations (\figref{comparison}, row 1) or by resolving the ambiguity between the left and right legs (\figref{comparison}, row 2).  Table~\ref{tab:animal} summarizes the performance of both the raw network predictions and results of the two OJA methods. Over most of the sequences in the BADJA dataset it can be seen that the use of coverage terms (employed by the OJA-GA model) improves skeleton accuracy. In particular, the bear, camel and rs\_dog sequences show substantial improvements. The method does however struggle on the horsejump\_high sequence, in which part of the silhouette is occluded by the human rider which adversely affects the silhouette coverage term. Across all sequences the selected OJA-GA method improves joint prediction accuracy by 7\% compared to the raw network output.

\begin{figure}[t]
\begin{floatrow}
\ffigbox{
\def\bb{\rule{2in}{0pt}\rule{0pt}{1in}}
\def\comparisonheight{20mm}
\begin{tabular}{ccc}
    \includegraphics[trim={7cm 6cm 7cm 6cm},clip,height=\comparisonheight]{ga_vs_qp/0046_skel_sil_raw_2.png} &

    \includegraphics[trim={7cm 6cm 7cm 6cm},clip,height=\comparisonheight]{ga_vs_qp/0046_skel_sil_cleaned_qp_3.png} &
    \includegraphics[trim={7cm 6cm 7cm 6cm},clip,height=\comparisonheight]{ga_vs_qp/0046_skel_sil_cleaned_ga_2.png} \\

    \includegraphics[trim={6cm 6cm 6cm 6cm},clip,height=\comparisonheight]{ga_vs_qp/0069_skel_sil_raw.png}
        &
    \includegraphics[trim={6cm 6cm 6cm 6cm},clip,height=\comparisonheight]{ga_vs_qp/0069_skel_sil_cleaned_qp.png}
        &
    {}\includegraphics[trim={6cm 6cm 6cm 6cm},clip,height=\comparisonheight]{ga_vs_qp/0069_skel_sil_cleaned_ga.png} \\
    (a) & (b) & (c)
\end{tabular}
}{
\caption{Example skeletons from raw predictions (a), processed with OJA-QP (b), and OJA-GA (c).}
\label{fig:comparison}
}
~
\capbtabbox{%
\small
\begin{tabular}{lccc}
\toprule
                & Raw       & QP        & GA    \\
\midrule
bear           & 83.1      & 83.7      & \textbf{88.9}   \\
camel          & 73.3      & 74.1      & \textbf{87.1}   \\
cat            & 58.5      & \textbf{60.1}      & 58.4    \\
cows           & 89.2      & 88.4      & \textbf{94.7}  \\
dog            & \textbf{66.9}  & 66.6   & \textbf{66.9}  \\
horsejump-high & 26.5      & \textbf{27.7}      & 24.4   \\
horsejump-low  & 26.9      & 27.0      & \textbf{31.9}   \\
tiger          & 76.5      & 88.8      & \textbf{92.3}   \\
rs\_dog        & 64.2      & 63.4      & \textbf{81.2}   \\
\midrule
Average        & 62.8      & 64.4      & \textbf{69.5}   \\
\bottomrule
\end{tabular}
}{\caption{Accuracy of OJA on BADJA test sequences.}
\label{tab:animal}
}
\end{floatrow}
\end{figure}

\input{Chapter4/Tables/synth-experiment.tex}

\input{Chapter4/FigTex/comp-to-smal.tex}

\subsection{Model fitting}
The predicted joint positions and silhouette are input to the 3D model fitting optimization phase, which proceeds in four stages. The first stage solves for the model's global rotation and translation parameters, which positions the camera. Following SMPLify~\cite{bogo16keep}, this camera stage is solved for torso points only, which remain largely fixed through shape and pose variation. The remaining stages solve for all shape, pose and translation parameters the emphasis of the priors gradually decreased. The silhouette term is introduced in the penultimate stage, as including this too early can lead to the optimizer finding unsatisfactory local minima.

The final outputs of our optimization pipeline are shown in \figref{example_results}. In each of the cases illustrated the optimizer is able to successfully find a set of pose and shape parameters which, when rendered, closely resembles the input image. The final row of \figref{example_results} demonstrates the generalizability of the proposed method: the algorithm is able to find a reasonable pose despite no camel figurines being included in the original SMAL model.

\subsubsection*{Comparison to other work.} Our approach is compared visually to that given by Zuffi {\em et al.}~\cite{zuffi2017menagerie}. Recall that their results require hand-clicked keypoints rather than fitting to points predicted automatically by the hourglass network, which was trained on synthetic animal images. Further, their work is optimized for single frame fitting and is tested on animals in simple poses, whereas the focus of this work is on the more challenging task of tracking animals in video. \figref{compar_smal} shows the application of our model to a number of single frame examples from the SMAL result data~\cite{zuffi2017menagerie}.

\subsubsection*{Quantitative experiments.}
There is no existing ground truth dataset for comparing reconstructed 3D animal meshes, but an estimate of quantitative error is obtained by testing on synthetic sequences for a range of quadruped species. These are generated by randomly deforming the model and varying the camera position to animate animal motion, see Figure~\ref{fig:synth}. Table~\ref{tab:synthetic} shows results on these sequences.
%bjb_insert

\input{Chapter4/FigTex/synth-experiment.tex}

\subsection{Automatic silhouette prediction}
While not the main focus of our work, the system presented in this chapter is able to perform the full 3D reconstruction process from an input image with no user intervention. This is achieved by incorporating the DeepLabv3+ network~\cite{deeplabv3plus} as a front-end segmentation engine which automatically generates animal silhouettes. This network was trained on the PASCAL VOC 2012 dataset, which includes a variety of animal quadruped classes. An example result generated using the fully automatic pipeline is shown in \figref{overview}.

\input{Chapter4/FigTex/qual-results.tex}
\input{Chapter4/FigTex/blooper.tex}


\chapter{Introduction}

\section{Motivation}

Scheduling arises in countless decision processes and its abstract nature results in a wide range of practical applications, such as in healthcare \cite{sanr,operations}. Scheduling problems are accurately modelled in mathematics, hence scheduling is often interpreted as a class of mathematical optimisation problems. With many mature and developed optimisation solvers \cite{clp}, solvers can find solutions to large scheduling problems quickly, which would have been impractical using manual optimisation techniques. However, solver scalability and efficiency often lead to complex algorithms. Such complexity combined with mathematical formulations of scheduling, result in users interpreting solvers and solutions as black-boxes. Therefore, decision making is often not transparent.
\linespace
Transparency of decision making is important. Users require a means to understand why a schedule, whether a solution of a solver or not, is reasonable given a context. For example, hospital managers may seek to understand the efficiency of their staff and resources. Schedules may need to be robust to unpredictable changes in staff and resources, such as a nurse being unavailable to attend patients due to personal reasons. In other settings, schedules may need to minimise the number of staff to maximise profits while fulfilling all staff and patients requirements.
\linespace
Explanations are vital in communication for understanding. With many possible schedules and many variations of scheduling problems \cite{sta}, it is impractical to manually-craft explanations for every schedule of every variation. This motivates a tool to generate clear explanations.

\section{Objectives}

The first main objective is to devise and implement a tool that explains an solver's scheduling solutions by human-understandable means, and allows the user to interact with the solver in a human-friendly manner. By using the explanation methodology outlined using argumentation \cite{aes}, the user can easier understand scheduling in a practical environments such as nurse rostering and dialysis scheduling. Hence, analysis of such methodology important to understand the applicability of argumentation in practical settings.
\linespace
The second main objective is to extend the theoretical or practical capabilities of argumentation for scheduling. Extensions may include interval scheduling, shop scheduling and job-dependent scheduling.

\section{Challenges}

A tool satisfying such objectives are subjected solving the challenges:
\begin{enumerate}
	\item\textbf{Trust:} Users of the tool need to be confident that the explanations generated are true. This requires correctness of algorithms proposed and implemented.
	\item\textbf{Accessibility:} Explanations generated from this tool are required to be accessible to people without domain-specific knowledge of optimisation or argumentation. The tool should be accessible to computer novices.
	\item\textbf{Applicability:} Explanations should relate to makespan schedules. The tool should generate clear explanations promptly to users.
	\item\textbf{Knowledge transfer:} Explanations are constructed using knowledge structures, commonly represented using natural languages or diagrams. A challenge would be to effectively explore the usefulness of using natural languages compared to diagrams.
	\item\textbf{Background:} Explainable planning is relatively new research area compared to optimisation \cite{pe}. Challenges arises from finding related literate on the title.
\end{enumerate}

\section{Contributions}
\label{introcontributions}

The main contributions of this report are listed below:
\begin{itemize}
	\item A new tool \emph{\toolname} that implements the concepts behind using argumentation for optimisation for makespan scheduling.
	\item Algorithms and their optimisations, alongside with some proofs.
	\item Theoretical applications of argumentation with two theorems.
	\item A discussion on applicability of argumentation.
\end{itemize}


    \filetitle{plotpred}{Plot Kalman filter predictions}{tseries/plotpred}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
[H1,H2,H3] = plotpred(X,Y,...)
[H1,H2,H3] = plotpred(Ax,X,Y,...)
[H1,H2,H3] = plotpred(Ax,Range,X,Y,...)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{X} {[} tseries {]} - Input data with time series observations.
\item
  \texttt{Y} {[} tseries {]} - Input data with predictions calculated in
  a Kalman filter run with an \texttt{'ahead='} option.
\item
  \texttt{Ax} {[} numeric {]} - Handle to axes object in which the data
  will be plotted.
\item
  \texttt{Range} {[} numeric \textbar{} Inf {]} - Date range on which
  the input data will be plotted.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\item
  \texttt{H1} {[} numeric {]} - Handles to a line object showing the
  time series observations (the first column, \texttt{X}, in the input
  data).
\item
  \texttt{H2} {[} numeric {]} - Handles to line objects showing the
  Kalman filter predictions (the second and further columns, \texttt{Y},
  in the input data).
\item
  \texttt{H3} {[} numeric {]} - Handles to one-point line objects
  displaying a marker at the start of each line.
\end{itemize}

\paragraph{Options}\label{options}

\begin{itemize}
\item
  \texttt{'connect='} {[} \emph{\texttt{true}} \textbar{} \texttt{false}
  {]} - Connect the prediction lines, \texttt{Y}, with the corresponding
  observation in \texttt{X}.
\item
  \texttt{'firstMarker='} {[} \emph{\texttt{'none'}} \textbar{} char {]}
  - Type of marker displayed at the start of each prediction line.
\item
  \texttt{'showNaNLines='} {[} \emph{\texttt{true}} \textbar{}
  \texttt{false} {]} - Show or remove lines with whose starting points
  are NaN (missing observations).
\end{itemize}

See help on \href{tseries/plot}{\texttt{plot}} and on the built-in
function \texttt{plot} for options available.

\paragraph{Description}\label{description}

\paragraph{Example}\label{example}


\nonstopmode
\RequirePackage[dvipsnames]{xcolor}

\documentclass[a4paper]{easychair}
\usepackage{amssymb}
%\usepackage{dsmath}
%\usepackage{bbm}
\usepackage{bbold}
\usepackage[utf8x]{inputenc}

\usepackage{natbib}
\usepackage{hyperref}
\urlstyle{sf} % less wide than tt
% \usepackage{agda} % colors

\definecolor{DeepJungleGreen}{rgb}{0.0, 0.29, 0.29}
\definecolor{DarkSpringGreen}{rgb}{0.09, 0.45, 0.27}
\definecolor{ForestGreenTraditional}{rgb}{0.0, 0.27, 0.13}
\definecolor{GreenPigment}{rgb}{0.0, 0.65, 0.31}
\definecolor{CalPolyPomonaGreen}{rgb}{0.12, 0.3, 0.17}
\definecolor{CSSGreen}{rgb}{0.0, 0.5, 0.0}
\definecolor{DarkCerulean}{rgb}{0.03, 0.27, 0.49}
\definecolor{DarkRaspberry}{rgb}{0.53, 0.15, 0.34}

\colorlet{DarkBlue}{MidnightBlue!80!black}
\definecolor{HanBlue}{rgb}{0.27, 0.42, 0.81}
\definecolor{FrenchBlue}{rgb}{0.0, 0.45, 0.73}
\definecolor{EgyptianBlue}{rgb}{0.06, 0.2, 0.65}

\hypersetup{colorlinks=true,
  linkcolor=Red,
  citecolor=DarkRaspberry,%DarkPurple,%DarkCerulean,%CSSGreen,%CalPolyPomonaGreen,%GreenPigment,%ForestGreenTraditional,%DarkSpringGreen,%DeepJungleGreen,%ForestGreen,
  urlcolor=MidnightBlue}

\makeatletter
\renewcommand\bibsection{%
  \section*{\large{\refname}}%
}%
\makeatother

% less space between entries in bibliography
\let\OLDthebibliography\thebibliography
\renewcommand\thebibliography[1]{
  \OLDthebibliography{#1}
  \setlength{\parskip}{0pt}
  \setlength{\itemsep}{0.2ex plus 0.3ex}
}

%% Macros

\input{macros}

\renewcommand{\patom}{\ensuremath{o}}
\renewcommand{\tVnf}{\mathsf{Nf}}

\begin{document}

\title{Normalization by Evaluation for Call-by-Push-Value}
\titlerunning{Normalization by Evaluation for Call-by-Push-Value}
\authorrunning{A. Abel and C. Sattler}
\author{Andreas Abel\thanks{Supported by VR grant 621-2014-4864 and EU
  Cost Action CA15123.}~
  \and Christian Sattler}
\institute{
  Department of Computer Science and Engineering,
  Gothenburg University
}

% Keywords:
% Modal typing, linear typing, strictness typing, positivity checking, guarded recursion

\maketitle

%\begin{abstract}
%\end{abstract}

\noindent

Normalization by evaluation (NbE) \citep{bergerSchwichtenberg:lics91}
is the interpretation of
an (open) term of type $A$ as value in a suitable model $\den A$,
followed by \emph{reification} of the value to a normal form of type
$A$.  Functions $f$ in $\den{A \To B}$ are reified as
$\lambda$-abstractions whose bodies are obtained
by \emph{reflecting} a fresh variable of type $A$ as value $a$ in
$\den{A}$ and reifying the application $f\,a$ at type $B$.
A suitable model that supports fresh variable generation
are \emph{presheaves} over the category of
typing contexts $\Gamma$ and order-preserving embeddings
$\Gamma \subseteq \Gamma'$, where a base type $o$ is interpreted as
the presheaf $\tNe\;o$ of neutral normal forms of type $o$, and
function types by the presheaf exponential aka Kripke function space
\citep{catarina:csl93,altenkirchHofmannStreicher:ctcs95}.

NbE for sum types requires a refinement of the model, since reflection of
a variable of type $A + B$ as a value in $\den{A + B}$ requires case
distinction in the model.  One such refinement are \emph{sheaves}
\citep{altenkirchDybjerHofmannScott:lics01}; another is the use of a
monad $\C$
\citep{%filinski:semaccounttdpe,
filinski:tlca01,barral:PhD}
in the category of presheaves
for the interpretation of sum types:
$\den{A + B} = \C(\den A + \den B)$.
The smallest such \emph{``cover'' monad} $\C$ are binary trees where leaves are
the monadic unit aka $\treturn$, and the nodes case distinctions
over neutrals $\tNe\,(A_1 + A_2)$ of sum type.  When leaves are normal
forms, the whole tree represents a normal form, thus,
$\trunNf : \C(\tNf\,A) \to \tNf\,A$ is trivial.
This \emph{running of the monad}
on normal forms represents the algorithmic part of the sheaf condition
on $\tNf\,A$ and extends as $\trun : \C \den A \to \den A$ to all
semantic types.

The given interpretation of sum types
$\den{A + B} = \C(\den A + \den B)$ corresponds to the call-by-name
(CBN) lambda calculus with lazy constructors.  NbE can also be
performed in call-by-value (CBV) style, then the monad is placed in
the codomain of function types: $\den{A \To B} = \den A \To \C \den B$
\citep{danvy:popl96}.  A systematic semantic analysis of CBN and CBV
lambda-calculi has been pioneered by \citet{moggi:infcomp91} through
translation into his computational lambda calculus;
\citet{filinski:tlca01} studied NbE for the latter calculus using the
continuation monad.  Moggi's work was continued and refined
by \citet{levy:hosc06} who subsumed CBV and CBN under his monadic
call-by-push-value (CBPV) calculus.  In this work, we study NbE for CBPV.

CBPV was designed to study lambda-calculus with effects.  It separates
types into \emph{value} types $P$ and \emph{computation} types $N$, which we, in
analogy to polarized lambda-calculus
\citep{zeilberger:PhD} %,brockNannestadSchuermann:lpar10,espiritoSanto:entcs17}
refer to as \emph{positive} and \emph{negative} types.
Variables stand for values, thus, have positive types.
The monad that models the effects is placed at the transition from values
to computations $\Comp P$, and computations can be embedded into
values by \emph{thunking} ($\Thunk N$).
\[
\begin{array}{lllrl@{\qquad}l}
  \PTy & \ni & P & ::= & \patom
    % \mid \tyone \mid P_1 \times P_2
    % \mid \tyzero
    \mid P_1 + P_2
    \mid \Thunk N
  & \mbox{positive type / value type}
\\
  \NTy & \ni & N & ::= & % \natom
    % \mid \top \mid N_1 \ntimes N_2
    % \mid
    P \To N
    \mid \Comp P
  & \mbox{negative type / computation type}
\\
\end{array}
\]
We restrict to a fragment of \emph{pure} CBPV
with a single positive connective, sum
types $P_1 + P_2$, and a single negative connective, call-by-value
function types $P \To N$.  While we have no proper effects, the
evaluation of open terms requires the effect of case distinction over
neutrals, modeled by a cover monad $\C$.  In the following, we give
inductive definitions of the presheaves of
normal ($\tNf$) and neutral normal forms
($\tNe$) of our fragment of CBPV and a concrete, strong cover monad $\Cov$.
\begin{small}
\begin{gather*}
  \nru{\tvar}{\Var \patom \Gamma}{\PValNf {\patom} \Gamma}
\quad
  \nru{\tthunk}{\Nf N \Gamma}{\PValNfP{\Thunk N}\Gamma}
\quad
  \nru{\tinj_i}{\PValNf{P_i}\Gamma}{\PValNfP{P_1 + P_2}\Gamma}
%\\[2ex]
\quad
  \nru{\tret}
      {\CoverP {\tVNf\,P}  \Gamma}
      {\NfP {\Comp P} \Gamma}
\quad
  \nru{\tabs}
      {\Nf N {(\cext \Gamma P)}}
      {\NfP {P \To N} \Gamma}
\end{gather*}
\end{small}
\begin{small}
\begin{gather*}
  \nru{\tforce}{\VarP {\Thunk N} \Gamma}{\Ne N \Gamma}
\qquad
  \nru{\tapp}
      {\NeP {P \To N} \Gamma \qquad \PValNf P \Gamma}
      {\Ne N \Gamma}
\qquad
  \nru{\tbind}
      {\NeP {\Comp P} \Gamma \qquad \Cover \J (\cext \Gamma P)}
      {\Cover \J \Gamma}
\\[2ex]
  \nru{\treturn}{\J\,\Gamma}{\Cover \J \Gamma}
\qquad
  \nru{\tcase}
      {\VarP {P_1 + P_2} \Gamma
       \qquad \Cover \J (\cext \Gamma {P_1})
       \qquad \Cover \J (\cext \Gamma {P_2})
      }
      {\Cover \J \Gamma}
\end{gather*}
\end{small}
\noindent
($\J$ stands for an arbitrary presheaf in $\Cov\,\J$.)
Normal forms start from a variable of base type and continue with
introductions,
except that the services of the monad can be used at the
transition $\tret$ from positive to negative types ($\Comp P$).
Neutrals are eliminations of variables of type $\Thunk N$ into a
positive type $\Comp P$, and can then be bound
to a variable of type $P$ to be used in a computation (see $\tbind$).
Variables of
sum type $P_1 + P_2$ can be utilized in computations through a
$\tcase$ split.

\emph{Terms} $\tTm$ of CBPV are obtained by blurring the
distinction between $\tNe$ and $\tNf$,
generalizing $\tbind$ and $\tcase$ from $\Cov\,\J$ to
computations $\tTm\,N$,
and relaxing $\tvar$ to variables
of arbitrary type $P$ and $\tforce$ to arbitrary terms of type
$\Thunk N$.  Terms are evaluated in the following presheaf model,
which interprets $\tThunk$ as the identity and $\tComp$ as $\Cov$.
\[
\begin{array}[t]{lcl}
  % \den{\tyone} & = & \hatone \\
  % \den{P_1 \times P_2} & = & \den{P_1} \hattimes \den{P_2} \\
  % \den{\tyzero} & = & \hatzero \\
  \den{P_1 + P_2} & = & \den{P_1} \hatplus \den{P_2} \\
  \den{\Thunk N} & = & \den N \\
  \den{\patom} & = & \tVar\;\patom \\
\end{array}
\qquad
\begin{array}[t]{lcl}
  % \den{\top} & = & \hatone \\
  % \den{N_1 \ntimes N_2} & = & \den{N_1} \hattimes \den{N_2} \\
  % \\
  \den{P \To N} & = & \den P \hatto \den N \\
  \den{\Comp P} & = & \Cov \den P \\
  % \den{\natom}  & = & \C (\tNe\, \natom) \\
\end{array}
\]
The evaluation of $\tbind$ terms in $\Tm N$ relies on
$\trun : \Cov \den N \to \den N$, which makes any computation type
monadic.  Reflection $\uparrow$ and reification $\downarrow$ are
defined mutually by induction on the type.  They take the
usual form, only that reflection of positive variables is monadic, to
allow the complete splitting of sums via $\tcase$.
It is invoked by reification of functions $\down {P \To N}$ via $\trunNf$.
\[
\begin{array}[t]{lcl}
  \up P  & : & \tVar\, P \to \Cov\,\den P \\
  \up N & : & \tNe\,N \to \den N \\
\end{array}
\qquad
\begin{array}[t]{lcl}
  \down P & : & \den P \to \tVNf\,P \\
  \down N & : & \den N \to \tNf\,N \\
\end{array}
\]
The details of our construction, plus extension to product types and
polarized lambda calculus, can be found in the full version at
\url{https://arxiv.org/abs/1902.06097}.
A partial Agda formalization is available at
\url{https://github.com/andreasabel/ipl}.

% \paragraph*{Acknowledgments.}
% %Thanks to the anonymous referees, who helped improving the quality of
% %this abstract through their feedback.
% This work was supported by
% Vetenskapsr\aa{}det under Grant
%   No.~621-2014-4864 \emph{Termination Certificates
%   for Dependently-Typed Programs and Proofs via Refinement Types}
% and by the EU Cost Action CA15123 \emph{Types for programming and verification}.

\begin{footnotesize}

\bibliographystyle{abbrvnat}
%\bibliographystyle{abbrvurl}
%\bibliographystyle{plainurl} % no author-year cit.
\bibliography{auto-types19}

\end{footnotesize}
\end{document}


\section{Results \& Performance}
\label{results}

To demonstrate the three variant WORG methods, an unconstrained
once-through fuel cycle is modeled with the Cyclus simulator
\cite{DBLP:journals/corr/HuffGCFMOSSW15}. In such a scenario, uranium
mining, enrichment, fuel fabrication, and storage all have effectively
infinite capacities. The only meaningful constraints on the system are
how many light-water reactors (LWR) are
built.\footnote{\url{https://github.com/scopatz/fc-deploy-opt}
contains the relevant optimizer implementation and Cyclus input files.
The entry point for the implementation discussed in this paper is the
`cases.ipynb' notebook.}

When Cyclus is executed here, it is representative of the inner loop
evaluation of a deployment schedule guess, seen as the \texttt{run\_sim()}
call in Listing \ref{worg-pseudo}. When implementing WORG in fuel cycle
simulator \emph{in situ}, these Cyclus executions would be replaced by
a look ahead sub-simulation. For expediency in development of the WORG
algorithm, this work simply simulated the simulator.

The base simulation begins with 100 reactors in 2016 that each produce
1 GWe, have an 18 month batch length with a one month reload time.
The initial fleet of LWRs retires evenly over the 40 years from 2016 to
2056. All new reactors have 60 year life times.  The simulation itself
follows 20 years from 2016 to 2035. This is on the higher end of
\emph{in situ} time horizons expected, which presumably
will be in the neighborhood of 1, 5, 10, or 20 years.

The study here compares how WORG performs for 0\% (steady state), 1\%,
and 2\% growth curves from an initial 90 GWe target. These are examined
using the three estimation methods variants described in the previous section.
Calling $\rho$ the growth rate as a
fraction, the demand curve is thus,
\begin{equation}
\label{f-rate}
f(t) = 90 (1 + \rho)^t
\end{equation}
Moreover, the upper bound for the number of deployable facilities at
each time is set to be the ceiling of ten times the total growth.
That is, assuming ten facilities at most could be deployed in the first
year, increase the upper bound along with the growth rate.  This yields
the following expression for $N$.
\begin{equation}
\label{n-rate}
N(t) = \left\lceil 10 (1 + \rho)^t\right\rceil
\end{equation}
The lower bound for the number of deployed reactors is taken to be the
zero vector, $M = \mathbf{0}$.  A maximum of twenty simulations are allowed,
or $S = 20$.
This is because an \emph{in situ} method cannot afford many optimization
iterations. The random seed for all optimizations was 424242.

Note that because of the integral nature of facility deployment,
exactly matching a continuous demand curve is not possible in general.
Slight over- and under-prediction are expected for most points in time.
Furthermore,
it is unlikely that the initial facilities will match the demand curve
themselves. If the initial facilities do no meet the demand on their own,
then the optimized deployment schedule is capable making up the difference.
However if the initial facilities produce more than the demand curve,
the the optimizer is only capable of deploying zero facilities at these
times. The WORG method does not help make radical adjustments to
accommodate
problems with the initial conditions, such as when 50 GWe are demanded
but 100 GWe are already being produced.

First examine Figures \ref{deploy-0} - \ref{deploy-2} which
show the optimized deployment schedule for the three estimation methods.
The figures represent the 0\%, 1\% and 2\% growth curves respectively.
Figures \ref{deploy-0} \& \ref{deploy-1} show that the \stochastic method
has the highest number of facilities deployed at any single time. In the
2\% case, the \allflag method predicts the largest number of facilities
deployed.

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{deploy-0.eps}
\caption{Optimized deployment schedule $\Theta$ for a 0\% growth (steady
state) demand curve. The number of deployed facilities shown are
for the \stochastic (black), \innerprod (green), and \allflag (purple)
estimation methods.}
\label{deploy-0}
\end{figure}

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{deploy-1.eps}
\caption{Optimized deployment schedule $\Theta$ for a 1\% growth
demand curve. The number of deployed facilities shown are
for the \stochastic (black), \innerprod (green), and \allflag (purple)
estimation methods.}
\label{deploy-1}
\end{figure}

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{deploy-2.eps}
\caption{Optimized deployment schedule $\Theta$ for a 2\% growth
demand curve. The number of deployed facilities shown are
for the \stochastic (black), \innerprod (green), and \allflag (purple)
estimation methods.}
\label{deploy-2}
\end{figure}

\clearpage

More important than the deployment schedules themselves, however, are the
production curves that they elicit.
Figures \ref{demand-product-stochastic} -
\ref{demand-product-all} display the
power production for the best-guess deployment schedule $G_1$ (solid lines),
production for the second best schedule $G_2$ (dotted lines),
and demand curves (dashed lines) for 0\%, 1\%, and 2\% growth.
The figures show each estimation mechanism separately.

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{demand-product-stochastic.eps}
\caption{Power production to demand comparison for 20 year deployment
schedule optimization using only the \stochastic estimation method.
0\%, 1\%, and 2\% growth rates starting at 90 GWe are shown. Solid lines
represent the best guess deployment schedule.  Dotted lines are represent
the second best guess deployment schedule. Dashed lines represent the
demand curve that is targeted.
}
\label{demand-product-stochastic}
\end{figure}

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{demand-product-inner-product.eps}
\caption{Power production to demand comparison for 20 year deployment
schedule optimization using only the \innerprod estimation method.
0\%, 1\%, and 2\% growth rates starting at 90 GWe are shown. Solid lines
represent the best guess deployment schedule.  Dotted lines are represent
the second best guess deployment schedule. Dashed lines represent the
demand curve that is targeted.
}
\label{demand-product-inner-product}
\end{figure}

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{demand-product-all.eps}
\caption{Power production to demand comparison for 20 year deployment
schedule optimization using the \allflag estimation method that selects the
best of both \stochastic and \innerprod estimations.
0\%, 1\%, and 2\% growth rates starting at 90 GWe are shown. Solid lines
represent the best guess deployment schedule.  Dotted lines are represent
the second best guess deployment schedule. Dashed lines represent the
demand curve that is targeted.}
\label{demand-product-all}
\end{figure}

As seen in Figure \ref{demand-product-stochastic}, the \stochastic only
estimations follow the trend line of the growth curve.  However, only for
a few regions such as for 2\% growth between 2025 - 2030, does the
production very closely match the the demand.  The second best guess for the
deployment schedule shows a relatively large degree of eccentricity. This
indicates that $G_2$ at the end of the optimization is there to show the
Gaussian process model regions in the $\Theta$ option space that do not work.

Alternatively the inner product search estimation can be seen in Figure
\ref{demand-product-inner-product}. This method does a reasonable job of
predicting a steady state scenario at later times. However, the 1\% and 2\%
curves are largely under-predicted.  For the 2\% case, this is so severe
as to be considered wholly wrong. This situation arises because the
\innerprod method can enter into deterministic traps where the same cycle
of deployment schedules is predicted forever.  If such a trap is fallen
into when the
production curve is far from an optimum, the inner product method does not
yield a sufficiently close guess of the deployment schedule.

However, combining the stochastic and inner product estimation methods limits
the weakness of each method. Figure \ref{demand-product-all} shows the
production and demand information for the \allflag estimation flag.
By inspection,
this method produces production curves that match the demand much more
closely. This is especially true for later times. Over-prediction
discrepancies for early times come from the fact the initially deployed
facilities (100 LWRs with 18 month cycles) does not precisely match an
initial 90 GWe target. The problem was specified this way in order to show
that reasonable deployments are still selected even in slightly
unreasonable situations.

With respect to the differences between the production and demand seen in
Figure \ref{demand-product-all}, if the decommissioning schedule is known
it is always possible to pick a deployment schedule that directly
compensates for the decommissions. However, the decommissioning schedule
cannot always be guaranteed. This is particularly true in agent-based
simulators where unforeseen decommissions may be triggered on any time step
or license extensions may be granted (and decommissioning avoided) at
run time.
WORG, in its general formulation here, does not take into account any
expected decommissioning information. This helps the methods generality
but may weaken its solution or increase the number of iterations
required.

Still, the main purpose of the WORG algorithm is to converge as quickly as
possible to a reasonable best-guess $\Theta$. The limiting factor is the
number of
predictive simulations which must be run. WORG will always execute
at least three full simulations: the lower bound, the upper bound, and one
iteration of the optimization loop. A reasonable limit on the total number
of simulations $S$ is 20.  For an \emph{in situ} calculator, it is unlikely
to want to compute more than this number of sub-simulations to predict
the deployment schedule for the next 1, 5, 10, or 20 years. However, it
may be possible to limit $S$ to significantly below 20 as well.

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{converge-0per.eps}
\caption{Convergence of 0\% growth rate solution for the distance between
the demand and production curves $d(f, g)$ as a function of the number of
simulations. The three estimation methods are shown. Additionally, the
$S$, $I$, and $A$ marker represent whether the \stochastic, \innerprod,
or \allflag method was selected as the best fit. For $2 < s$, the \allflag
will select either $S$ or $I$.
}
\label{converge-0per}
\end{figure}

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{converge-1per.eps}
\caption{Convergence of 1\% growth rate solution for the distance between
the demand and production curves $d(f, g)$ as a function of the number of
simulations. The three estimation methods are shown. Additionally, the
$S$, $I$, and $A$ marker represent whether the \stochastic, \innerprod,
or \allflag method was selected as the best fit. For $2 < s$, the \allflag
will select either $S$ or $I$.
}
\label{converge-1per}
\end{figure}

\begin{figure}[htb]
\centering
\includegraphics[width=0.9\textwidth]{converge-2per.eps}
\caption{Convergence of 2\% growth rate solution for the distance between
the demand and production curves $d(f, g)$ as a function of the number of
simulations. The three estimation methods are shown. Additionally, the
$S$, $I$, and $A$ marker represent whether the \stochastic, \innerprod,
or \allflag method was selected as the best fit. For $2 < s$, the \allflag
will select either $S$ or $I$.
}
\label{converge-2per}
\end{figure}

Figures \ref{converge-0per} - \ref{converge-2per} demonstrate a convergence
study for the three
different growth rates as a function of the number of simulations $s$.
These three figures demonstrate important properties of the WORG method.
The first is that the \allflag estimation method here consistently has
the lowest minimum dynamic time warping distance, and thus the best guess
for the deployment schedule $\Theta$.  Furthermore, the \allflag curves
in these figures show that the best estimation method consistently switches
between the inner product search and stochastic search. These imply
that together the \stochastic and \innerprod methods converge more quickly
that the sum of their parts.  This is particularly visible in the 0\% growth
rate case seen in Figure \ref{converge-0per}.

Additionally, These convergence plots show that the majority of the
$\Theta$ selection gains are made by $s=10$.  Simulations past ten may
show differential improvement for the \allflag method.  However, they
do not tend to generate any measurable improvement for \stochastic or
\innerprod selection mechanisms. Furthermore, most of the gains for the
\allflag method are realized by simulation five or six. Thus a reduction
by a factor of two to four in the number of simulations is available.
This equates directly to a like reduction in computational cost.

Moreover,
Figures \ref{converge-0per} - \ref{converge-2per} also show the tendency of
the \innerprod method to become deterministically stuck when used on its own.
In all three cases the \innerprod method resolves to a constant by
simulation five or ten.  In the 1\% case, this solution happens to be quite close
to the solution predicted by the \allflag method.  In the 0\% case, this
constant is meaningfully distinct, but is still more akin to the \stochastic
solution than the \allflag solution.  In the 2\% case, the converged \innerprod
result has little to do with the \allflag prediction.  Thus it is not
recommended to use \innerprod on its own. While it may succeed, it is too
risky because it may cease improving at the wrong place.

The \stochastic estimation method is also prone to similar cyclic behavior.
However, in the \innerprod method,
is the second best guess $G_2$ is also in a constant prediction cycle along
with $G_1$.  With the stochastic search, the second best
guess has more freedom to roam the option space, creating potentially
different Gaussian process models with each iteration $s$.  Eventually, given
enough guesses and enough iterations, the stochastic model will break
out of a local optimum to perhaps find a better global optimum elsewhere.
For the \emph{in situ} use case, though, eventual solution is not fast
enough.  The inner product search spans regions that the stochastic weighting
labeled as unlikely.  This assist is what enables the \allflag method
to converge faster than the stochastic method on its own.  That said,
if the \emph{in situ} use case is not relevant, the stochastic method on
its own could be used without any algorithmic qualms.


\documentclass[a4paper]{article}

\title{au.id.cxd.math}
\date{03/10/2014}
\author{Chris Davey}

\usepackage[pdftex]{graphicx}

\usepackage{times}

\usepackage{mathptmx}

\usepackage{amsmath}

\usepackage{enumitem}

\usepackage{graphicx}

\pagestyle{headings}
\begin{document}

\section{Overview}

This library implements a number of functions for mathematical operations, it has dependencies upon the scala-nlp-breeze matrix library, and its purpose is to explore the implementation and useage of a variety of tools and techniques starting with some statistical tools.
This document is structured with the main headings corresponding to the subject areas implemented in the library, and minor headings corresponding to the major package names in the project. Each subsection below the minor headings is also decomposed into individual subject areas, and the lowest level heading represents a class.

\input{./au.id.cxd.math.count.tex}

\section{Probability}

\subsection{package au.id.cxd.math.probability}

This package provides a series of modules that support operations for inference via probability, and for estimation of distributions.

\subsubsection{Inequalities}

The class TchebysheffInequality implements a simple estimation of a pdf using the inequality rule:\\
$$
P(\mu - k\sigma < Y < \mu + k\sigma) \ge 1 - \frac{1}{k^2}
$$
Which can be restated as:
$$
P(lower < Y < upper) \ge 1 - \frac{1}{k^2}
$$
The value of $k$ is derived from either upper and lower bounds since:
$$
k = \frac{upper - \mu} {\sigma}
$$
After determining the value of $k$ the probability can be estimated by substituting
$$
p = 1 - \frac{1}{k^2}
$$

\input{./au.id.cxd.math.probability.discrete.tex}

\subsubsection{Functions}

\subsubsection*{Package au.id.cxd.math.function}

The package au.id.cxd.math.function contains a set of supporting functions.

\subsubsection*{The Gamma Function}

The gamma function (class name GammaFn) provides the integral for the following:

$$
  \Gamma(z) = \int_0^\infty  t^{z-1} e^{-t} dt
$$

It is equivalent to:

$$\Gamma(\alpha) = (\alpha - 1)!$$

In order to support real numbers, it can be approximated using:
 $$
   \Gamma(z+1) = \sqrt{2\pi}\left(z + \gamma + \frac{1}{2} \right)^{z + \frac{1}{2}} e^{-\left(z + \gamma + \frac{1}{2} \right)}\left[ c_0 + \sum_{i=1}^N \frac{C_i}{z+i} \right]
 $$
Note that the approximation is minimized when $\gamma = 5$ and $N = 6$

The implementation of the gamma function is taken from
Grant Palmer "Technical Java : Developing Scientific and Engineering Applications", Prentice Hall 2007
It is also described in  "Numerical Recipes The Art of Scientific Computing" pp213..215. Both define $c_0$ and $C_{1..6}$ as constants.

\subsubsection*{The Beta Function}

The beta function (class name BetaFn) supports the beta distribution and is built upon the gamma function in the following manner:

$$\beta(\alpha, \beta) = \frac{ (\alpha - 1)! (\beta - 1)! }{ (\alpha + \beta - 1)! }$$

and is implemented as:

$$
\beta(\alpha, \beta) = \frac{\Gamma(\alpha)\Gamma(\beta)}{\Gamma(\alpha + \beta)}
$$

\subsubsection{Continuous Distributions}

\subsubsection*{Package au.id.cxd.math.probability.continuous}

The package au.id.cxd.math.probability.continuous contains a set of classes that can be used to model continuous distributions and their simple properties.

\subsubsection*{Uniform Distribution}

The uniform distribution (class name Uniform) provides a uniform output for $y$ between a $min$ and $max$ and is implemented as follows.
\[
f(y; min, max) =
	\begin{cases}
		\frac{1}{max - min} & \text{if } min \le y \le max \\
		0 & \text{otherwise}

	\end{cases}
\]
The distribution has the following simple properties:\\
Mean: $\mu = \frac{max + min}{2}$\\
Variance: $\sigma^2 = \frac{(max - min)^2}{12}$

\subsubsection*{Gamma Distribution}
The Gamma distribution (class name Gamma) is suitable for modelling distributions that are left skewed and is useful for things such as time measurements (similar to the Poisson distribution for discrete events). The distribution is defined as:
\[
f(y; \alpha, \beta) =
	\begin{cases}
		\frac{ y^{\alpha - 1}e^{-y/\beta} }{ \beta^{\alpha} \Gamma(\alpha) } & \text{for } 0 \le y \le \infty \\
		0 & \text{elsewhere}
	\end{cases}
\]
The simple properties of the distribution are:\\
Mean: $\mu = \alpha\beta$\\
Variance: $\sigma^2 = \alpha\beta^2$\\
The Gamma distribution also has an important relationship with the chi-square distribution where $\nu$ is the degrees of freedom, if the gamma distribution has the parameters $\alpha = \nu/2$ and $\beta = 2$ then it is also a $\chi^2$ distribution with $\nu$ degrees of freedom. \\
\subsubsection*{Exponential Distribution}
The Exponential distribution (class name Exponential) is an instance of the Gamma distribution where the $\alpha$ parameter equals 1, leaving the parameter $\beta$ to be provided. The distribution can be used to model processes with "lifetimes" or decay. The distribution is defined as:
\[
f(y; \beta) =
	\begin{cases}
		\frac{1}{\beta} e^{-y/\beta}, & 0 \le y \le \infty\\
		0 & elsewhere
	\end{cases}
\]
The exponential distribution has the following simple properties:\\
Mean: $\mu = \beta$\\
Variance: $\sigma^2 = \beta^2$

\subsubsection*{Beta Distribution}
The Beta distribution (class name Beta) is often used to model proportions over the range of 0 to 1. It has two parameters, $\alpha > 0$ and $\beta > 0$, the domain of the dependent variable is $0 \le y \le 1$, however it is possible to scale variables to fit within this range for use with the beta distribution. The probability density function is modelled as:
\[
f(y; \alpha, \beta) =
	\begin{cases}
		\frac{ y^{\alpha - 1} (1- y)^{\beta - 1} }{ \beta(\alpha, \beta) } & 0 \le y \le 1\\
		0 & \text{elsewhere}
	\end{cases}
\]
Note the $\beta(\alpha, \beta)$ is the Beta function described in "The Beta Function" earlier in this document. The distribution has the following simple properties:\\
Mean: $\mu = \frac{\alpha}{\alpha + \beta}$\\
Variance: $\sigma^2 = \frac{\alpha\beta}{(\alpha + \beta)^2(\alpha + \beta + 1)}$

\subsubsection*{Normal Distribution}

The normal distribution is commonly used for modelling natural processes and due to the central limit theorem is useful for inferences with a large enough sample and population. It accepts two parameters $\mu$ for the mean and $\sigma^2$ for the variance. The normal distribution is defined as follows:
$$
f(y; \mu, \sigma^2) =
		\frac{1}{\sigma \sqrt{2\pi} } e^{-\frac{1}{2\sigma^2}(y - \mu)^2} \ \  -\infty \le y \le \infty
$$
The simple properties of the distribution are as follows:\\
Mean: $E[Y] = \mu$\\
Variance: $Var[Y] = \sigma^2$

\subsubsection*{Chi-square Distribution}

The chi-square $\chi^2$ distribution with degree of freedom $df$ is implemented as a Gamma distribution in the class "ChiSquare" with the distribution having the parameters $\alpha = df/2$ and $\beta = 2$:

\[
\chi_{df}^2 = f(y; \alpha = df/2, \beta = 2) =
	\begin{cases}
		\frac{ y^{df/2 - 1}e^{-y/2} }{ 2^{\alpha} \Gamma(df/2) } & \text{for } 0 \le y \le \infty \\
		0 & \text{elsewhere}
	\end{cases}
\]

\subsubsection*{F Distribution}

The F distribution is the ration of two chi-square distributions divided by their respective degrees of freedom.
$$
f(x; d_1; d_2) = \frac{\chi_{d_1}^2/d_1}{\chi_{d_2}^2/d_2}
$$
It is defined as:
$$
f(x; d_1, d_2) = \frac{ \Gamma\left( \frac{ d_1 + d_2 }{2} \right) d_1^{d_1/2} d_2^{d_2/2} } {\Gamma(d_1/2)\Gamma(d_2/2) } \frac{x^{d_1/2 - 1}}{(d_1 + d_2x)^{(d_1 + d_2)/2}}
$$
$$
= \frac{ d_1^{d_1/2} d_2^{d_2/2}x^{d_1/2 - 1} } { (d_2 + d_1x)^{(d_1+d_2)/2} B(d_1/2, d_2/2)}
$$
$$
= \frac{ \sqrt{d_2^{d_2}}  \sqrt{ d_1^{d_1}  } \sqrt{ x^{d_1-2} }  }{\sqrt{ (d_2+d_1x)^{d_1 + d_2} } \beta(d_1/2, d_2/2) }
$$
where $\beta$ is the beta function (from the gamma function) and $x > 0$\\
The mean of the distribution is defined where denominator $df > 2$.
$$
\mu = \frac{ d_2 }{d_2 - 2}
$$
And the variance of the distribution is defined where the denominator $df > 4$
$$
\sigma^2 = \frac{ 2 d_2^2 (d_1 + d_2 - 2) }{ d_1 (d_2 - 2)^2 (d_2 - 4) }
$$
The f distribution is most often used in testing hypothesis about an unknown variance.

\subsubsection{Inference}

\subsubsection*{Analysis of Variance (Anova)}

The ratio of Mean Square of Treatment ($MST$) and Mean Square of Error ($MSE$) is used in the test of the hypothesis that the means of $k$ normally distributed populations are equal $H_0: \mu_1 = \mu_2 = ... = \mu_k$ assuming that variances are also equal, $\sigma_1^2 = \sigma_2^2 = ... = \sigma_k^2$. The alternate hypothesis states that at least one of the population means are not equal to the others. The test statistic is calculated as
$$F = \frac{MST}{MSE}$$
and the rejection region is given for the critical value of the F distribution at $k-1$ and $n - k$ degrees of freedom.
$$
F > F_{\alpha,(k-1),(n-k)}
$$
The procedure of calculating the test statistic involves the calculation of the Total Sum of Square Treatment and the Sum of Square Error.\\
Firstly the correction for the mean is calculated ($CM$).
$$
CM = \frac{1}{n} \left( \sum_{i=1}^k \sum_{j=1}^{n_i} Y_{ij} \right)^2 = n\bar{Y}^2
$$
The total sum of squares is calculated.
$$
Total SS = \sum_{i=1}^k \sum_{j=1}^{n_i} (Y_{ij} - \bar{Y})^2 = \sum_{i=1}^k \sum_{j=1}^{n_i}  Y_{ij}^2 - CM
$$
The Sum of square treatment is calculated as:
$$
SST = \sum_{i=1}^k n_i (\bar{Y}_{i \bullet } - \bar{Y})^2 = \sum_{i=1}^k \frac{Y_{i .}^2}{n_i} - CM
$$
where $Y_{i \bullet } = \sum_{j=1}^{n_i} Y_{ij}$ and $\bar{Y}_{i \bullet } = \left( \frac{1}{n_i} \right) \sum_{j=1}^{n_i} Y_{ij} = \left( \frac{1}{n_i} \right) Y_{i \bullet }$.\\
The Sum of square error is calculated from the Sum of Square treatment:
$$
SSE = TotalSS - SST
$$
Note that the $SSE$ is also equal to the pooled sum of squares for all samples:
$$
SSE = \sum_{i=1}^k (n_i - 1)S_i^2
$$
where
$$
S_i^2 = \frac{1}{n_i - 1} \sum_{j=1}^{n_i} (Y_{ij} - \bar{Y}_{i\bullet}) ^ 2
$$
The mean squared error is the pooled variance defined as:
$$
S^2 = MSE = \frac{SSE}{n - k}
$$
and the mean sum of square treatment is the sum of square treatment divided by the degree of freedom $k-1$.
$$
MST = \frac{SST}{k-1}
$$
The implementing class "Anova" performs the calculation for a multivariate set of k samples by breaking the calculations into their components as described above. Note that the operation is performed on a matrix, hence for samples of unequal sizes, a sparse matrix is used where empty values for samples of smaller sizes are padded with $0s$, the total number of rows is the maximum $n_i$ of the $k$ samples.

\subsubsection{Regression}

\end{document}


\documentclass[11pt]{article}
\usepackage{amsmath,textcomp,amssymb,geometry,graphicx,enumerate}
\usepackage{algorithm} % Boxes/formatting around algorithms
\usepackage[noend]{algpseudocode} % Algorithms
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=blue,
}

\def\Name{Neeraj Pandey}  % Your name
%\def\SID{PUT SOMETHING HERE}  % Your student ID number
%\def\Login{PUT SOMETHING HERE} % Your login (your class account, cs170-xy)
\def\Homework{3} % Number of Homework
\def\Session{Monsoon 2018}

\title{CS103 -- Monsoon 2018 --- Homework \Homework}
\author{\Name
%SID \SID,
%\texttt{\Login}
}
\markboth{CS103\Session\  Homework \Homework\ \Name}{CS103--\Session\ Homework \Homework\ \Name, %\texttt{\Login}
}
\pagestyle{myheadings}
\date{}

\newenvironment{qparts}{\begin{enumerate}[{(}a{)}]}{\end{enumerate}}
\def\endproofmark{$\Box$}
\newenvironment{proof}{\par{\bf Proof}:}{\endproofmark\smallskip}

\textheight=9in
\textwidth=6.5in
\topmargin=-.75in
\oddsidemargin=0.25in
\evensidemargin=0.25in

\begin{document}
\maketitle

Collaborators: None

\section*{Question 1: Consider an unpipelined or single-stage processor design like the one discussed in class. At the start of a cycle, a new instruction enters the processor and is processed completely within a single cycle. It takes 1500 ps to navigate all the circuits. Therefore, for this design to work, the cycle time has to be at least 1500 picoseconds.}
\begin{qparts}
\item
What is the clock speed of this processor?
\newline
\textbf{Solution (a): } There are 1500ps in 1 cycle
\[= 1500 * 10\text{ sec}\]
\[= \frac{10^{12}}{1500}\]
\[= \frac{10^{10}}{15}\]
\[= \frac{2}{3} * 10^{9}\]
\[\implies 0.666 * 10^{9}\text{ Hz}\]

\item What is the CPI of this processor, assuming that every load/store instruction finds its instruction/data in the instruction or data cache (memory)?
\newline
\textbf{Solution (b): } Average CPI is 1 because each instruction takes only 1 instruction to run.

\item What is the throughput of this processor (in billion instructions per second)?
\newline
\textbf{Solution (c):} 1 instruction takes 1500ps \\
\[1500 * 10^{-12} * 10 ^{9}\]
\[= 1500 * 10^{-3}\]
\[= 1.5\text{ sec = 1 Billion instructions}\]
\[\implies 1\text{sec} = \frac{10^{9}}{1.5}\]
\[= 0.0666 \text{ billion instructions per second}\]
\end{qparts}

\section*{Question 2: The processor in Q1 above is converted into a 5-stage pipeline, similar to the one discussed in class. It takes 300 ps to navigate the circuits in each stage. Assume that latches do not introduce a noticeable overhead.}
\begin{qparts}
\item What is the clock speed of this processor?
\newline
\textbf{Solution (a): } 1 cycle = 300ps\\
\[= 300 * 10^{-12}\]
\[\implies 1\text{sec} = \frac{10^{12}}{300}\]
\[= 3.3 * 10^{9} \text{ Hz}\]

\item What is the CPI of this processor, assuming that every load/store instruction finds its instruction/data in the instruction or data cache, and there are no stalls from data/control/structural hazards?
\newline
\textbf{Solution (b): } The CPI is 1 as one instruction is finished in 1 cycle.

\item What is the throughput of this processor (in billion instructions per second)?
\newline
\textbf{Solution (c): } 1 instruction completes 1 cycle of the processor. Therefore, throughput of the processor: \\
\[3.3 \text{ Billion/second}\]
\end{qparts}

\section*{Question 3: Show how the following four instructions move through each stage of the five stage pipeline, similar to the example discussed in class. Assume that this pipeline does not support any bypassing. Make sure the decode stage does not advance an instruction through the pipeline unless all data dependencies are correctly resolved. Assume that register reads and writes take half a cycle each, with the same convention as discussed in class.
}
\newline
\textbf{Solution 3: }
\begin{table}
\centering
\begin{tabular}{lllll}
IM & Reg R & ALU & DM & Reg W  \\
I1 &       &     &    &        \\
I2 & I1    &     &    &        \\
I3 & I2    & I1  &    &        \\
I3 & I3    & I2  & I1 &        \\
I4 & I3    &     & I2 & I1     \\
   & I3    &     &    & I2     \\
   & I4    & I3  &    &        \\
   &       & I4  &    &        \\
   &       &     & I3 &        \\
   &       &     & I4 & I3     \\
   &       &     & I4 &
\end{tabular}
\end{table}

\section*{Question 4: Identify all data dependencies between these instructions. Assume that your processor has a 5 stage pipeline that does not support bypassing. Also assume that both memory reads and writes take one full cycle. Show the flow of these instructions through the 5 stage pipeline. How many cycles will it take to execute all these instructions? Show your work.}
\textbf{Solution: 4}

\begin{table}
\centering
\begin{tabular}{lllll}
IM & Reg R & ALU & DM & Reg W  \\
I1 &       &     &    &        \\
I2 & I1    &     &    &        \\
I3 & I2    & I1  &    &        \\
I4 & I3    & I2  & I1 &        \\
I4 & I3    &     &    & I1     \\
I4 & I3    &     &    & I2     \\
I4 & I3    &     &    &        \\
   & I4    & I3  &    &        \\
   &       & I4  & I3 &        \\
   &       &     & I4 & I3     \\
   &       &     & I4 &
\end{tabular}
\end{table}

\section*{Question 5: In a 5 stage pipeline, assume the presence of a control instruction, bne, which has the following format : bne $t0,$t1, 1024. The pipeline has been designed such that you are able to find out that $t0 and $t1 are either equal or not only at the end of the ALU stage. Additionally, you are able to calculate the target of the branch (PC+1024) only at the end of the Data Memory stage. In this case, how many instructions will need to be squashed because of these assumptions?  Give reasons for your claim. }

\textbf{Solution: } We are able to know that the $\$t0$ and $\$t1$ are not equal and at the fourth cycle, the counter changes for the program and the fifth cycle starts at the branch (PC + 1024). So, 4 instructions need to be squashed.

\section *{Question 6: }
\begin{qparts}
\item What is the cycle time of both processors?
\newline
\textbf{Solution (a): } P1 cycle time = 450 + 20 ps $\implies$ 470ps \\ and P2 cycle time = 620 + 20 ps $\implies$ 640ps

\item What is the latency of executing a single instruction on P1 and P2?
\newline
\textbf{Solution (b): } For one second : Latency of P1 = 5 * 470 ps $\implies$ 2350ps \\ and Latency of P2 = 5 * 640 $\implies$ 3200ps

\item What is the maximum possible throughput for each processor, assuming ideal conditions?
\newline
\textbf{Solution (c): } Throughput for P1 = $\frac{1}{470 \text{ ps}}$ = $\frac{10^{12}}{470}$ $\implies$ 2.1 * 10^9 instructions \\ and Throughput for P2 = $\frac{1}{640 \text{ ps}}$ = $\frac{10^{12}}{640}$ $\implies$ 1.5 * 10^9 instructions

\item Which one is faster? Explain briefly.
\newline
\textbf{Solution (d): } The cycle time of P1 is less than that of P2, which allows the 5th pipleline in P1 to finish first. Therefore, P1 is faster than P2.
\end{qparts}

\
\end{document}