\section{Background}
This section will provide details on the problem domain and the algorithms that have been made use of. First, we describe the problem domain in terms of the challenge we are attempting to solve. Then, we provide some background on ResNet50V2 \cite{ResNet50V2} - the network employed for feature extraction in our convectional neural network. Finally, we elaborate on convolutional neural networks, as this is the kind of network we built.

\subsection{Makerere Passion Fruit Disease Detection}\label{sec:domain}
The findings presented in this report are relevant to the "Makerere Passion Fruit Disease Detection Challenge" \cite{zindi}. The challenge entails classifying the disease status of a plant, given an image of a passion fruit.

The images were collected from the various areas in Uganda. Capturing images involved guidance from National Crop Resources Research Institute (NaCRRI) passion fruit disease experts. These experts identified the disease manifestation in the fruits of passion fruit plants.

Roughly, the dataset contains 4000 images. All images have been resized to 512x512 pixels. Some images have more than a single fruit. Thus, some images have more than a single bounding box. Each bounding box is tagged to one of three classes: \emph{fruit\_healthy}, \emph{fruit\_brownspot} and \emph{fruit\_woodiness}.

\subsection{ResNet50V2}
Deep residual networks (ResNets) \cite{resnet} provide a learning framework for deep neural networks in particular. Simply, ResNets "reformulate the layers as learning residual functions with reference to the layer inputs, instead of learning un-referenced functions". ResNets were shown to perform better than state-of-the-art methods of the time. Particularly in the context of networks with considerable depth and with challenging image recognition tasks.

Building on the idea of ResNets, ResNet50V2 \cite{ResNet50V2} was introduced. ResNet50V2 adds a new "residual unit". Results show that ResNet50V2 improved on the performance of ResNet50, both in terms of the ease of training and ability to generalize. In the context of this report, ResNet50 is used for feature extraction in both the identification of bounding boxes and classification of fruits.


\section{Metropolis-Hastings algorithm}


    \section{Low-Voltage Induced Random Bit Errors in Quantized DNN Weights}
\label{sec:supp-main}

We provide a more detailed discussion of the considered error model: random bit errors, induced through low-voltage operation of memories commonly used on DNN accelerators \cite{KimDATE2018,KoppulaMICRO2019}. Work such as \cite{ChandramoorthyHPCA2019,KoppulaMICRO2019} model the effect of low-voltage induced bit errors using two parameters: the probability $\pfault$ of bit cells in accelerator memory being faulty at a given low voltage and the probability $\perror$ that a faulty bit cell results in a bit error on access.
Following measurements in works such as \cite{GanapathyHPCA2019,KimDATE2018}, we assume that these errors are \emph{not} transient errors by setting $\perror = 100\%$ such that the overall probability of bit errors is $p := \pfault \cdot \perror = \pfault$. In doing so, we consider the worst-case where faulty bit cells \emph{always} induce bit errors. However, the noise model from the main paper remains valid for any arbitrary but fixed $\perror \neq 100\%$. For the reminder of this document, we assume the probability of bit error $p = \pfault$, with $\perror = 100\%$, as in the main paper. In the following section, we describe the two parameters, $\pfault$ and $\perror$, in more details.

\begin{table}[t]
	\centering
	\caption{\textbf{Quantization-Aware Training Accuracies.} Clean \TE for $m = 8$ bits or lower using our robust fixed-point quantization. We obtain competitive performance for $m = 8$ and $m = 4$ bits. On \CifarH, a Wide ResNet (WRN) clearly outperforms our standard SimpleNet model. Batch normalization (BN), improving \TE slightly on \CifarT, is significantly less robust than group normalization (GN), \cf \tabref{tab:supp-bn}. * For $m \leq 4$, we report results with weight clipping, \Clipping[$0.1$].}
	\label{tab:supp-accuracy}
	\vspace*{-0.25cm}
	\hspace*{-0.25cm}
	\begin{subfigure}[t]{0.19\textwidth}
		\vspace*{0px}
		\small
		\begin{tabular}{|@{\hskip 4px}l@{\hskip 4px}|@{\hskip 4px}c@{\hskip 4px}|}
			\multicolumn{2}{c}{\bfseries \CifarT}\\
			\multicolumn{2}{c}{\bfseries SimpleNet+GN}\\
			\hline
			Quant. $m$ & \TE in \%\\
			\hline
			-- & 4.34\\
			8 & \bfseries 4.32\\
			4* & 5.29\\
			3* & 5.71\\
			\hline
		\end{tabular}
	\end{subfigure}
	\begin{subfigure}[t]{0.28\textwidth}
		\vspace*{0px}
		\small
		\begin{tabular}{|@{\hskip 4px}l@{\hskip 4px}|@{\hskip 4px}c@{\hskip 4px}|@{\hskip 4px}c@{\hskip 4px}|}
			\multicolumn{3}{c}{\bfseries \CifarT}\\
			\multicolumn{3}{c}{\bfseries Arch. Comparison}\\
			\hline
			Model & no Quant. & $m = 8$\\
			\hline
			SimpleNet+GN & 4.34 & 4.32\\
			SimpleBet+BN & 4.04 & 3.83\\
			ResNet-50+GN & 5.88 & 6.81\\
			ResNet-50+BN & \bfseries 3.91 & \bfseries 3.67\\
			\hline
		\end{tabular}
	\end{subfigure}\\[4px]

	\hspace*{-0.5cm}
	\begin{subfigure}[t]{0.19\textwidth}
		\vspace*{0px}
		\small
		\begin{tabular}{| l | c |}
			\multicolumn{2}{c}{\bfseries \MNIST}\\
			\hline
			Quant. $m$ & \TE in \%\\
			\hline
			4 & 0.4\\
			2* & 0.47\\
			\hline
		\end{tabular}
	\end{subfigure}
	\begin{subfigure}[t]{0.2\textwidth}
		\vspace*{0px}
		\small
		\begin{tabular}{| l | c |}
			\multicolumn{2}{c}{\bfseries \CifarH}\\
			\hline
			Quant. $m$, Model & \TE in \%\\
			\hline
			8, SimpleNet & 23.68\\
			8, WRN & 18.53\\
			\hline
		\end{tabular}
	\end{subfigure}
	\vspace*{-0.1cm}
\end{table}

\textbf{Faulty Bit Cells.} Due to variations in the fabrication process, SRAM bit cells become more or less vulnerable to low-voltage operation. For a specific voltage, the resulting bit cell failures can be assumed to be random and independent of each other. We assume a bit to be faulty with probability $\pfault$ increasing exponentially with decreased voltage \cite{GanapathyDAC2017,GanapathyHPCA2019,KimDATE2018,ChandramoorthyHPCA2019}. Furthermore, the faulty bits for $\pfault' \leq \pfault$ can be assumed to be a subset of those for $\pfault$. For a fixed chip, consisting of multiple memory arrays, the pattern (spatial distribution) of faulty cells is fixed for a specific supply voltage. Across chips/memory arrays, however, faulty cells are assumed to be random and independent of each other.

\textbf{Bit Errors in Faulty Bit Cells:} Faulty cells may cause bit errors with probability $\perror$ upon read/write access.
We note that bit errors read from memory affect \emph{all} computations performed on the read weight value.
We assume that a bit error flips the currently stored bit, where flips $0$-to-$1$ and $1$-to-$0$ are assumed equally likely.

\subsection{Profiled Bit Errors}
\label{subsec:supp-errors-profiled}

\begin{figure}[t]
	\centering
	\includegraphics[width=0.4\textwidth]{c10_clipping_linf.pdf}
	\vspace*{-8px}
	\caption{\textbf{Weight Clipping Improves $L_\infty$ Robustness.} On \CifarT, we plot \RTE for \emph{relative} $L_\infty$ perturbations on weights: Random noise with $L_\infty$-norm smaller than or equal to $x\%$ of the weight range is applied. \Clipping clearly improves robustness. Again, the relative magnitude of noise is not affected by weight clipping. Note that $L_\infty$ noise usually affects all weights, while random bit errors affect only a portion of the weights.}
	\label{fig:supp-clipping-inf}
	\vspace*{-0.2cm}
\end{figure}

\figref{fig:supp-errors} splits the bit error distributions of \figref{fig:errors} into a $0$-to-$1$ flip and a $1$-to-$0$ bit flip map. The obtained maps, $p_{\text{1t0}}$ and $p_{\text{0t1}}$, contain per-bit flip probabilities for $1$-to-$0$ and $0$-to-$1$ bit flips at a given low voltage. In this particular profiled chip, \figref{fig:supp-errors} (bottom), $0$-to-$1$ flips are more likely. Similarly, \figref{fig:supp-errors} (right) shows that most  $0$-to-$1$ flips are actually persistent across time at that voltage \ie, not random transient errors.
The following table summarizing the key statistics of the profiled chips: the overall bit error rate $p$, the rate of $1$-to-$0$ and $0$-to-$1$ flips $p_{\text{1t0}}$ and $p_{\text{0t1}}$, and the rate of persistent errors $p_{\text{sa}}$, all in \% at a specific supply voltage:

\begin{center}\small
\begin{tabular}{| l | c | c | c | c |}
	\hline
	Chip & $p$ & $p_{\text{0t1}}$ & $p_{\text{1t0}}$ & $p_{\text{sa}}$ \\
	\hline
	\multirow{2}{*}{1} & 2.744 & 1.27 & 1.47 & 1.223\\
	& 0.866 & 0.38 & 0.49 & 0.393\\
	\hline
	\multirow{3}{*}{2} & 4.707 &  3.443 & 1.091 & 0.627\\
	& 1.01 &  0.82 & 0.19 & 0.105\\
	& 0.136 & 0.115 & 0.021 & 0.01 \\
	\hline
	\multirow{2}{*}{3} & 2.297 & 1.81 & 0.48 & 0.204 \\
	& 0.597 & 0.496 & 0.0995 & 0.206 \\
	\hline
\end{tabular}
\end{center}

\begin{table*}[t]
	\centering
	\small
	\caption{\textbf{Impact of Quantization Scheme on Robustness.} Complementary to \tabref{tab:quantization-robustness}, we report \TE and \RTE for various bit error rates $p$ for the quantization scheme in \eqnref{eq:quantization} with global, per-layer and asymmetric quantization, $m = 8$ bits. Instead of quantizing into signed integer, using unsigned integers works better for asymmetric quantization. Furthermore, proper rounding instead of integer conversion also improves robustness. Note that influence on clean \TE is neglegible, \ie, the DNN can ``learn around'' these difference in quantization-aware training. Especially for $m = 4$ bit, the latter makes a significant difference in terms of robustness.}
	\label{tab:supp-quantization}
	\vspace*{-0.25cm}
	\begin{tabular}{| c | l | c | c | c | c | c | c | c |}
		\hline
		\multicolumn{9}{|c|}{\bfseries \CifarT: quantization robustness}\\
		\hline
		& Model & \multirow{2}{*}{\begin{tabular}{c}\TE\\in \%\end{tabular}} & \multicolumn{6}{c|}{\RTE in \%, $p$ in \% p=0.01}\\
		\cline{4-9}
		& (see text) && $0.01$ & $0.05$ & $0.1$ & $0.5$ & $1$ & $1.5$\\
		\hline
		\hline
		\multirow{5}{*}{\rotatebox{90}{$m = 8$ bit}} & \eqnref{eq:quantization}, global & 4.63 & 10.70 {\color{gray}\scriptsize ${\pm}$1.37} & 86.01 {\color{gray}\scriptsize ${\pm}$3.65} & 90.36 {\color{gray}\scriptsize ${\pm}$0.66} & 90.71 {\color{gray}\scriptsize ${\pm}$0.49} & 90.57 {\color{gray}\scriptsize ${\pm}$0.43} & --\\
		& \eqnref{eq:quantization}, per-layer (= \Normal) & 4.36 & 4.82 {\color{gray}\scriptsize ${\pm}$0.07} & 5.51 {\color{gray}\scriptsize ${\pm}$0.19} & 6.37 {\color{gray}\scriptsize ${\pm}$0.32} & 24.76 {\color{gray}\scriptsize ${\pm}$4.71} & 72.65 {\color{gray}\scriptsize ${\pm}$6.35} & 87.40 {\color{gray}\scriptsize ${\pm}$2.47}\\
		& +asymmetric & 4.36 & 5.76 {\color{gray}\scriptsize ${\pm}$0.09} & 6.47 {\color{gray}\scriptsize ${\pm}$0.22} & 7.85 {\color{gray}\scriptsize ${\pm}$0.46} & 40.78 {\color{gray}\scriptsize ${\pm}$7.56} & 76.72 {\color{gray}\scriptsize ${\pm}$7.01} & 85.83 {\color{gray}\scriptsize ${\pm}$2.58}\\
		& +unsigned & 4.42 & 6.58 {\color{gray}\scriptsize ${\pm}$0.13} & 6.97 {\color{gray}\scriptsize ${\pm}$0.28} & 7.49 {\color{gray}\scriptsize ${\pm}$0.41} & 17.00 {\color{gray}\scriptsize ${\pm}$2.77} & 54.57 {\color{gray}\scriptsize ${\pm}$8.58} & 83.18 {\color{gray}\scriptsize ${\pm}$3.94}\\
		& +rounded (= \Quant) & 4.32 & 4.60 {\color{gray}\scriptsize ${\pm}$0.08} & 5.10 {\color{gray}\scriptsize ${\pm}$0.13} & 5.54 {\color{gray}\scriptsize ${\pm}$0.2} & 11.28 {\color{gray}\scriptsize ${\pm}$1.47} & 32.05 {\color{gray}\scriptsize ${\pm}$6} & 68.65 {\color{gray}\scriptsize ${\pm}$9.23}\\
		\hline
		\hline
		\multirow{2}{*}{\rotatebox{90}{$4$ bit}} & integer conversion & 5.81 & 90.46 {\color{gray}\scriptsize ${\pm}$0.2} & 90.40 {\color{gray}\scriptsize ${\pm}$0.21} & 90.39 {\color{gray}\scriptsize ${\pm}$0.22} & 90.36 {\color{gray}\scriptsize ${\pm}$0.2} & 90.36 {\color{gray}\scriptsize ${\pm}$0.22} & 90.39 {\color{gray}\scriptsize ${\pm}$0.22}\\
		& proper rounding & 5.29 & 5.49 {\color{gray}\scriptsize ${\pm}$0.04} & 5.75 {\color{gray}\scriptsize ${\pm}$0.06} & 5.99 {\color{gray}\scriptsize ${\pm}$0.09} & 7.71 {\color{gray}\scriptsize ${\pm}$0.36} & 10.62 {\color{gray}\scriptsize ${\pm}$1.08} & 15.79 {\color{gray}\scriptsize ${\pm}$2.54}\\
		\hline
	\end{tabular}
	\vspace*{-0.1cm}
\end{table*}

For evaluation, we assume that the DNN weights are mapped linearly onto the memory of these chips. The bit error maps are of size $8192 \times 128$ bits for chips 2 and 3 and $2048 \times 128$ bits for chip 1. Furthermore, to simulate various different mappings, we repeat this procedure with various offsets and compute average \RTE across all mappings. For results, we refer to \appref{subsec:supp-randbet-baselines}.

\subsection{Bounding Generalization to Random Bit Errors}
\label{subsec:supp-bound}

Let $w$ denote the final weights of a trained DNN $f$. We test $f$ using $n$ i.i.d. test examples, \ie, $(x_i,y_i)_{i=1}^n$. We denote by $w'$ the weights where each bit of $w$ is flipped with probability $p$ uniformly at random, corresponding to the error model from \secref{sec:errors}.
The expected \emph{clean} error  of $f$ is given by
\begin{align*}
	\Exp[\Id_{f(x;w)\neq y}] = \Pr(f(x;w)\neq y).
\end{align*}
The expected \emph{robust} error (regarding i.i.d. test examples drawn from the data distribution) with random bit errors in the (quantized) weights is
\begin{align*}
	\Exp[ \Id_{f(x;w')\neq y}] = \Pr( f(x;w')\neq y).
\end{align*}
Here, the weights of the neural network are themselves random variables. Therefore, with $x, y, w$, and $w'$ we denote the random variables corresponding to test example, test label, weights and weights bit random bit errors. With $x_j, y_j, w_i$ and $w'_i$ we denote actual examples. Then, the following proposition derives a simple, probabilistic bound on the deviation of expected robust error from the empirically measured one (\ie, \RTE in our experiments):

\begin{table*}[t]
	\centering
	\small
	\caption{\textbf{Weight Clipping Improves Robustness.} We report \TE and \RTE for various experiments on the robustness of weight clipping with $\wmax$, \ie, \Clipping[$\wmax$]. First, we show that the robustness benefit of \Clipping is independent of quantization-aware training, robustness also improves when applying post-training quantization. Then, we show results for both symmetric and asymmetric quantization. For the latter we demonstrate that label smoothing \cite{SzegedyCVPR2016} reduces the obtained robustness. This supports our hypothesis that weight clipping, driven by minimizing cross-entropy loss during training, improves robustness through redundancy.}
	\label{tab:supp-clipping}
	\vspace*{-0.25cm}
	\begin{tabular}{| c | l | c | c | c | c | c | c | c |}
		\hline
		\multicolumn{9}{|c|}{\bfseries \CifarT ($\mathbf{m = 8}$ bit): clipping robustness for post- and during-training quantization}\\
		\hline
		&Model & \multirow{2}{*}{\begin{tabular}{c}\TE\\in \%\end{tabular}} & \multicolumn{6}{c|}{\RTE in \%, $p$ in \% p=0.01}\\
		\cline{4-9}
		&&& $0.01$ & $0.05$ & $0.1$ & $0.5$ & $1$ & $1.5$\\
		\hline
		\hline
		\multirow{6}{*}{\rotatebox{90}{\begin{tabular}{@{}c@{}}Post-Training\\Asymmetric\end{tabular}}} & \Normal & 4.37 & 4.95 {\color{gray}\scriptsize ${\pm}$0.11} & 5.47 {\color{gray}\scriptsize ${\pm}$0.17} & 6.03 {\color{gray}\scriptsize ${\pm}$0.22} & 15.42 {\color{gray}\scriptsize ${\pm}$3.4} & 51.83 {\color{gray}\scriptsize ${\pm}$9.92} & 81.74 {\color{gray}\scriptsize ${\pm}$5.14}\\
		& \Quant & 4.27 & 4.59 {\color{gray}\scriptsize ${\pm}$0.08} & 5.10 {\color{gray}\scriptsize ${\pm}$0.13} & 5.54 {\color{gray}\scriptsize ${\pm}$0.15} & 10.59 {\color{gray}\scriptsize ${\pm}$1.11} & 30.58 {\color{gray}\scriptsize ${\pm}$6.05} & 63.72 {\color{gray}\scriptsize ${\pm}$6.89}\\
		& \Clipping[$0.25$] & 4.96 & 5.24 {\color{gray}\scriptsize ${\pm}$0.07} & 5.73 {\color{gray}\scriptsize ${\pm}$0.14} & 6.16 {\color{gray}\scriptsize ${\pm}$0.21} & 10.51 {\color{gray}\scriptsize ${\pm}$0.91} & 26.27 {\color{gray}\scriptsize ${\pm}$5.65} & 61.49 {\color{gray}\scriptsize ${\pm}$9.03}\\
		& \Clipping[$0.2$] & 5.24 & 5.48 {\color{gray}\scriptsize ${\pm}$0.05} & 5.87 {\color{gray}\scriptsize ${\pm}$0.09} & 6.23 {\color{gray}\scriptsize ${\pm}$0.13} & 9.47 {\color{gray}\scriptsize ${\pm}$0.7} & 19.78 {\color{gray}\scriptsize ${\pm}$3.58} & 43.64 {\color{gray}\scriptsize ${\pm}$8.2}\\
		& \Clipping[$0.15$] & 5.38 & 5.63 {\color{gray}\scriptsize ${\pm}$0.05} & 6.03 {\color{gray}\scriptsize ${\pm}$0.09} & 6.38 {\color{gray}\scriptsize ${\pm}$0.13} & 8.80 {\color{gray}\scriptsize ${\pm}$0.41} & 15.74 {\color{gray}\scriptsize ${\pm}$2.24} & 36.29 {\color{gray}\scriptsize ${\pm}$7.34}\\
		& \Clipping[$0.1$] & 5.32 & 5.52 {\color{gray}\scriptsize ${\pm}$0.04} & 5.82 {\color{gray}\scriptsize ${\pm}$0.06} & 6.05 {\color{gray}\scriptsize ${\pm}$0.07} & 7.45 {\color{gray}\scriptsize ${\pm}$0.26} & 9.80 {\color{gray}\scriptsize ${\pm}$0.62} & 17.56 {\color{gray}\scriptsize ${\pm}$3.08}\\
		\hline
		\hline
		\multirow{7}{*}{\rotatebox{90}{\begin{tabular}{@{}c@{}}Symmetric\\(during training)\end{tabular}}} & \Normal & 4.36 & 4.82 {\color{gray}\scriptsize ${\pm}$0.07} & 5.51 {\color{gray}\scriptsize ${\pm}$0.19} & 6.37 {\color{gray}\scriptsize ${\pm}$0.32} & 24.76 {\color{gray}\scriptsize ${\pm}$4.71} & 72.65 {\color{gray}\scriptsize ${\pm}$6.35} & 87.40 {\color{gray}\scriptsize ${\pm}$2.47}\\
		& \Quant & 4.39 & 4.77 {\color{gray}\scriptsize ${\pm}$0.08} & 5.43 {\color{gray}\scriptsize ${\pm}$0.21} & 6.10 {\color{gray}\scriptsize ${\pm}$0.32} & 17.11 {\color{gray}\scriptsize ${\pm}$3.07} & 55.35 {\color{gray}\scriptsize ${\pm}$9.4} & 82.84 {\color{gray}\scriptsize ${\pm}$4.52}\\
		& \Clipping[$0.25$] & 4.63 & 4.99 {\color{gray}\scriptsize ${\pm}$0.07} & 5.53 {\color{gray}\scriptsize ${\pm}$0.1} & 6.06 {\color{gray}\scriptsize ${\pm}$0.16} & 13.55 {\color{gray}\scriptsize ${\pm}$1.42} & 41.64 {\color{gray}\scriptsize ${\pm}$7.35} & 73.39 {\color{gray}\scriptsize ${\pm}$7.15}\\
		& \Clipping[$0.2$] & 4.50 & 4.79 {\color{gray}\scriptsize ${\pm}$0.06} & 5.25 {\color{gray}\scriptsize ${\pm}$0.09} & 5.65 {\color{gray}\scriptsize ${\pm}$0.16} & 9.64 {\color{gray}\scriptsize ${\pm}$0.99} & 21.37 {\color{gray}\scriptsize ${\pm}$4.23} & 45.68 {\color{gray}\scriptsize ${\pm}$7.9}\\
		& \Clipping[$0.15$] & 5.18 & 5.42 {\color{gray}\scriptsize ${\pm}$0.05} & 5.76 {\color{gray}\scriptsize ${\pm}$0.08} & 6.07 {\color{gray}\scriptsize ${\pm}$0.09} & 8.36 {\color{gray}\scriptsize ${\pm}$0.43} & 13.80 {\color{gray}\scriptsize ${\pm}$1.45} & 24.70 {\color{gray}\scriptsize ${\pm}$3.77}\\
		& \Clipping[$0.1$] & 4.86 & 5.07 {\color{gray}\scriptsize ${\pm}$0.04} & 5.34 {\color{gray}\scriptsize ${\pm}$0.06} & 5.59 {\color{gray}\scriptsize ${\pm}$0.1} & 7.12 {\color{gray}\scriptsize ${\pm}$0.3} & 9.44 {\color{gray}\scriptsize ${\pm}$0.7} & 13.14 {\color{gray}\scriptsize ${\pm}$1.79}\\
		& \Clipping[$0.05$] & 5.56 & 5.70 {\color{gray}\scriptsize ${\pm}$0.03} & 5.89 {\color{gray}\scriptsize ${\pm}$0.06} & 6.03 {\color{gray}\scriptsize ${\pm}$0.08} & 6.68 {\color{gray}\scriptsize ${\pm}$0.14} & 7.31 {\color{gray}\scriptsize ${\pm}$0.2} & 8.06 {\color{gray}\scriptsize ${\pm}$0.36}\\
		\hline
		\hline
		\multirow{11}{*}{\rotatebox{90}{\begin{tabular}{@{}c@{}}{\color{red}\textbf{A}}symmetric (default) quant.\\(during training)\end{tabular}}} & \Normal & 4.36 & 4.82 {\color{gray}\scriptsize ${\pm}$0.07} & 5.51 {\color{gray}\scriptsize ${\pm}$0.19} & 6.37 {\color{gray}\scriptsize ${\pm}$0.32} & 24.76 {\color{gray}\scriptsize ${\pm}$4.71} & 72.65 {\color{gray}\scriptsize ${\pm}$6.35} & 87.40 {\color{gray}\scriptsize ${\pm}$2.47}\\
		& \Quant & 4.32 & 4.60 {\color{gray}\scriptsize ${\pm}$0.08} & 5.10 {\color{gray}\scriptsize ${\pm}$0.13} & 5.54 {\color{gray}\scriptsize ${\pm}$0.2} & 11.28 {\color{gray}\scriptsize ${\pm}$1.47} & 32.05 {\color{gray}\scriptsize ${\pm}$6} & 68.65 {\color{gray}\scriptsize ${\pm}$9.23}\\
		& \Clipping[$0.25$] & 4.58 & 4.84 {\color{gray}\scriptsize ${\pm}$0.05} & 5.29 {\color{gray}\scriptsize ${\pm}$0.12} & 5.71 {\color{gray}\scriptsize ${\pm}$0.16} & 10.52 {\color{gray}\scriptsize ${\pm}$1.14} & 27.95 {\color{gray}\scriptsize ${\pm}$4.16} & 62.46 {\color{gray}\scriptsize ${\pm}$8.89}\\
		& \Clipping[$0.2$] & 4.63 & 4.91 {\color{gray}\scriptsize ${\pm}$0.05} & 5.28 {\color{gray}\scriptsize ${\pm}$0.08} & 5.62 {\color{gray}\scriptsize ${\pm}$0.11} & 8.27 {\color{gray}\scriptsize ${\pm}$0.35} & 18.00 {\color{gray}\scriptsize ${\pm}$2.84} & 53.74 {\color{gray}\scriptsize ${\pm}$8.89}\\
		& \Clipping[$0.15$] & 4.42 & 4.66 {\color{gray}\scriptsize ${\pm}$0.05} & 5.01 {\color{gray}\scriptsize ${\pm}$0.09} & 5.31 {\color{gray}\scriptsize ${\pm}$0.12} & 7.81 {\color{gray}\scriptsize ${\pm}$0.6} & 13.08 {\color{gray}\scriptsize ${\pm}$2.21} & 23.85 {\color{gray}\scriptsize ${\pm}$5.07}\\
		& \Clipping[$0.1$] & 4.82 & 5.04 {\color{gray}\scriptsize ${\pm}$0.04} & 5.33 {\color{gray}\scriptsize ${\pm}$0.07} & 5.58 {\color{gray}\scriptsize ${\pm}$0.1} & 6.95 {\color{gray}\scriptsize ${\pm}$0.24} & 8.93 {\color{gray}\scriptsize ${\pm}$0.46} & 12.22 {\color{gray}\scriptsize ${\pm}$1.29}\\
		& \Clipping[$0.05$] & 5.44 & 5.59 {\color{gray}\scriptsize ${\pm}$0.04} & 5.76 {\color{gray}\scriptsize ${\pm}$0.07} & 5.90 {\color{gray}\scriptsize ${\pm}$0.07} & 6.53 {\color{gray}\scriptsize ${\pm}$0.13} & 7.18 {\color{gray}\scriptsize ${\pm}$0.16} & 7.92 {\color{gray}\scriptsize ${\pm}$0.25}\\
		\cline{2-9}
		& \Clipping[$0.2$]+LS & 4.48 & 4.77 {\color{gray}\scriptsize ${\pm}$0.05} & 5.19 {\color{gray}\scriptsize ${\pm}$0.1} & 5.55 {\color{gray}\scriptsize ${\pm}$0.12} & 9.46 {\color{gray}\scriptsize ${\pm}$0.82} & 32.49 {\color{gray}\scriptsize ${\pm}$5.07} & 68.60 {\color{gray}\scriptsize ${\pm}$7.33}\\
		& \Clipping[$0.15$]+LS & 4.67 & 4.86 {\color{gray}\scriptsize ${\pm}$0.05} & 5.23 {\color{gray}\scriptsize ${\pm}$0.08} & 5.83 {\color{gray}\scriptsize ${\pm}$0.12} & 7.99 {\color{gray}\scriptsize ${\pm}$0.43} & 29.40 {\color{gray}\scriptsize ${\pm}$6.99} & 68.99 {\color{gray}\scriptsize ${\pm}$8.48}\\
		& \Clipping[$0.1$]+LS & 4.82 & 5.05 {\color{gray}\scriptsize ${\pm}$0.04} & 5.37 {\color{gray}\scriptsize ${\pm}$0.08} & 6.10 {\color{gray}\scriptsize ${\pm}$0.11} & 7.36 {\color{gray}\scriptsize ${\pm}$0.4} & 10.59 {\color{gray}\scriptsize ${\pm}$1.01} & 18.31 {\color{gray}\scriptsize ${\pm}$2.84}\\
		& \Clipping[$0.05$]+LS & 5.30 & 5.43 {\color{gray}\scriptsize ${\pm}$0.03} & 5.63 {\color{gray}\scriptsize ${\pm}$0.06} & 6.43 {\color{gray}\scriptsize ${\pm}$0.07} & 6.51 {\color{gray}\scriptsize ${\pm}$0.15} & 7.30 {\color{gray}\scriptsize ${\pm}$0.23} & 8.06 {\color{gray}\scriptsize ${\pm}$0.38}\\
		\hline
	\end{tabular}
	\vspace*{-0.1cm}
\end{table*}

\begin{proposition}
	\label{prop:bound}
	Let $w'_i$, $i = 1,\ldots,l$ be $l$ examples of weights bit random bit errors (each bit flipped with probability $p$). Then it holds
	\begin{align*}
		\Pr\Big( \frac{1}{nl}&\sum_{j=1}^n \sum_{i=1}^l \Id_{f(x_j;w'_i)\neq y_j} - \Pr(f(x;w')\neq y)\geq \epsilon\Big)\\
		&\quad\leq (n+1) e^{-n\epsilon^2 \frac{l}{(\sqrt{l}+\sqrt{n})^2}}.
	\end{align*}
	As alternative formulation, with probability $1-\delta$ it holds
	\begin{align*}
		\Pr( f(x; w'_i)\neq y) < & \frac{1}{nl}\sum_{j=1}^n \sum_{i=1}^l \Id_{f(x_j; w'_i)\neq y_j}\\ &+ \quad \leq\sqrt{\frac{\log\Big(\frac{n+1}{\delta}\Big)}{n}} \frac{\sqrt{l}+\sqrt{n}}{\sqrt{l}}.
	\end{align*}
\end{proposition}
{\small
\begin{proof}
Let $0<\alpha<1$. Using the Hoeffding inequality and union bound, we have:
\begin{align*}
	&\Pr\Big(\maxop_{j=1,\ldots,n} \frac{1}{l}\sum_{i=1}^l \Id_{f(x_j; w'_i)\neq y_j} - \Exp_{w'}[\Id_{f(x_j; w')\neq y_j}] > \alpha\epsilon\Big) \\
	=& \Pr\Big(\bigcup_{j=1,\ldots,n}\big\{ \frac{1}{l}\sum_{i=1}^l \Id_{f(x_j; w'_i)\neq y_j} - \Exp_{w'}[\Id_{f(x_j; w')\neq y_j}] > \alpha\epsilon\big\}\Big)\\
	&\leq \; n\, e^{-l\alpha^2 \epsilon^2}.
\end{align*}
Then, again by Hoeffding's inequality, it holds:
\begin{align*}
	&\Pr\Big( \frac{1}{n}\sum_{j=1}^n \Exp_{w'}[\Id_{f(x_j; w')\neq y_j}]
	- \Exp_{x,y}[\Exp_{w'}[\Id_{f(x; w')\neq y}]] > (1-\alpha) \epsilon \Big)\\
	&\leq \; e^{-n\epsilon^2 (1-\alpha)^2}.
\end{align*}
Thus, using
\begin{align*}
	a + b>\epsilon \Longrightarrow \{a > \alpha \epsilon\} \cup \{b > (1-\alpha)\epsilon\}
\end{align*}
gives us:
\begin{align*}
	&\Pr\Big( \frac{1}{nl}\sum_{j=1}^n \sum_{i=1}^l \Id_{f(x_j; w'_i)\neq y_j} - \Pr( f(x; w')\neq y)\geq \epsilon\Big)\\
	=& \Pr\Big( \frac{1}{n}\sum_{j=1}^n \big(\frac{1}{l}\sum_{i=1}^l \Id_{f_{w'_i}(x_j)\neq y_j} - \Exp_{w'}[\Id_{f(x_j; w'_i)\neq y_j}]\big)\\
	 +& \frac{1}{n}\sum_{j=1}^n \Exp_{w'}[\Id_{f(x_j; w'_i)\neq y_j}] - \Pr( f(x; w')\neq y)\geq \epsilon\Big)\\
	\leq & \Pr\Big( \frac{1}{n}\sum_{j=1}^n \big(\frac{1}{l}\sum_{i=1}^l \Id_{f(x_j; w'_i)\neq y_j} - \Exp_{w'}[\Id_{f(x_j; w')\neq y_j}]\big)>\alpha \epsilon\Big)\\
	+& \Pr\Big( \frac{1}{n}\sum_{j=1}^n \Exp_{w'}[\Id_{f(x_j; w')\neq y_j}] - \Pr( f(x; w')\neq y)\geq (1-\alpha)\epsilon\Big)\\
	\leq &  n\, e^{-l\alpha^2 \epsilon^2} + e^{-n\epsilon^2 (1-\alpha)^2}
\end{align*}
Having both exponential terms have the same exponent yields $\alpha=\frac{\sqrt{n}}{\sqrt{l}+\sqrt{n}}$ and we get the upper bound of the proposition.
\end{proof}
}

\textbf{Remarks:}
The samples of bit error injected weights $\{w'_i\}_{i=1}^l$ can actually be different for any test example $(x_j, y_j)$, even though this is not the case in our evaluation. Thus, the above bound involves a stronger result: for any test example, the empirical test error with random bit errors (\ie, robust test error \RTE) and the expected one have to be similar with the same margin. Note also that this bound holds for any fixed bit error distribution as the only requirement is that the bit error patterns we draw are i.i.d. but not the bit errors on the pattern.
In \appref{subsec:experiments-stress}, we consider results with $l = 10^6$, \ie, $l \gg n$ with $n = 10^4$ on \CifarT such that $\nicefrac{l}{(\sqrt{l}+\sqrt{n})^2}$ tends towards one. With $\delta=0.99$ the excess term $\sqrt{\frac{\log\Big(\frac{n+1}{\delta}\Big)}{n}} \frac{\sqrt{l}+\sqrt{n}}{\sqrt{l}}$ in the Proposition is equal to $4.1\%$. Thus larger test sets would be required to get stronger guarantees e.g. for $n=10^5$ one would get $1.7\%$.

\section{Quantization and Bit Manipulation in PyTorch}
\label{sec:supp-implementation}

Our fixed-point quantization $Q$ in \eqnref{eq:quantization} quantizes weights $w_i \in [-\qmax, \qmax] \subset \mathbb{R}$ into signed integers $\{-2^{m-1}-1,\ldots,2^{m-1}-1\}$. Here, the quantization range $[-\qmax,\qmax]$ is symmetric around zero. Note that zero is represented exactly. To implement asymmetric quantization, as outlined in \secref{subsec:robustness-quantization}, the same scheme can be used to quantize weights $w_i \in [\qmin, \qmax]$ within any arbitrary, potentially asymmetric, interval. To this end, \eqnref{eq:quantization} with $\qmax = 1$ is used and the weights in $[\qmin,\qmax]$ are mapped linearly to $[-1, 1]$ using the transformation $N$:
\begin{align}
	N(w_i) = \left(\frac{w_i - \qmin}{\qmax - \qmin}\right)\cdot 2 - 1.\label{eq:asymmetric-quantization}
\end{align}
Generally, $\qmin$ and $\qmax$ are chosen to reflect minimum and maximum weight value -- either from all weights (global quantization) or per-layer.
Furthermore, we argue that asymmetric quantization becomes more robust when using \emph{unsigned} integers as representation. In this case, \eqnref{eq:quantization} can be adapted using a simple additive term:
\begin{align}
	\begin{split}
		Q(w_i) &= \left\lceil \frac{w_i}{\Delta}\right\rfloor + (2^{m - 1} - 1)\\
		Q^{-1}(v_i) &= \Delta (v_i - (2^{m - 1} - 1))
	\end{split}\label{eq:unsigned-quantization}
\end{align}
We use asymmetric quantization using $N$ in \eqnref{eq:asymmetric-quantization} with \eqnref{eq:unsigned-quantization} as our \emph{robust} fixed-point quantization.

\begin{table}[t]
	\centering
	\small
	\caption{\textbf{Batch Normalization not Robust.} \RTE with group normalization (GN) or batch normalization (BN). \RTE increases when using BN even though clean \TE improves slightly compared GN. However, using batch statistics at test time (\ie, ``training mode'' in PyTorch) improves \RTE significantly indicating that the statistics accumulated throughout training do not account for random bit errors. We use \textbf{group normalization as default.}}
	\label{tab:supp-bn}
	\vspace*{-0.25cm}
	\hspace*{-0.25cm}
	\begin{tabular}{| c | l | c | c | c |}
		\hline
		\multicolumn{5}{|c|}{\bfseries \CifarT ($\mathbf{m = 8}$ bit): robustness of BN}\\
		\hline
		&& \multirow{2}{*}{\begin{tabular}{@{}c@{}}\TE\\in \%\end{tabular}} & \multicolumn{2}{c|}{\RTE in \%}\\
		\hline
		&& & $p{=}0.1$ & $p{=}0.5$\\
		\hline
		\hline
		\multirow{2}{*}{GN} & \Normal & 4.32 & 5.54 & 11.28\\
		& \Clipping[$0.1$] & 4.82 & 5.58 & 6.95\\
		\hline
		\hline
		\multicolumn{5}{|c|}{\textbf{BN w/ \emph{Accumulated} Statistics}}\\
		\hline
		\multirow{2}{*}{BN} & \Normal & 3.83 & 6.36 & 52.52\\
		& \Clipping[$0.1$] & 4.46 & 5.32 & 8.25\\
		\hline
		\hline
		\multicolumn{5}{|c|}{\textbf{BN w/ Batch Statistics at Test Time}}\\
		\hline
		\multirow{2}{*}{BN} & \Normal & 3.83 & 6.65 & 9.63\\
		& \Clipping[$0.1$] & 4.46 & 6.57 & 7.29\\
		\hline
	\end{tabular}
	\vspace*{-0.1cm}
\end{table}

Following \secref{subsec:robustness-quantization}, we implement ``fake'' fixed-point quantization for quantization-aware training and bit error injection directly in PyTorch \cite{PaszkeNIPSWORK2017}. Here, fake quantization means that computation is performed in floating point, but before doing a forward pass, the DNN is quantized and dequantized, \ie, $w_q = Q^{-1}(Q(w))$ in \algref{alg:training}. Note that we quantize into \emph{unsigned} $8$ bit integers, irrespective of the target precision $m \leq 8$. To later induce random bit errors, the $8 - m$ most significant bits (MSBs) are masked for $m < 8$. Bit manipulation of unsigned $8$ bit integers is then implemented in C/CUDA and interfaced to Python using CuPy \cite{cupy} or CFFI \cite{cffi}. These functions can directly operate on PyTorch tensors, allowing bit manipulation on the CPU as well as the GPU. We will make our code publicly available to faciliate research into DNN robustness against random bit errors.

\section{Weight Clipping with Group/Batch Normalization}
\label{sec:supp-clipping}

While weight clipping, \ie, globally constraining weights to $[-\wmax, \wmax]$ during training, is easy to implement, we make a simple adjustment to group and batch normalization layers: we reparameterize the scale parameter $\alpha$ of batch/group normalization, which usually defaults to $\alpha = 1$ and may cause problems when clipped, \eg, to $[-0.1, 0.1]$. In particular with aggressive weight clipping, $\alpha \leq \wmax < 1$, the normalization layers loose their ability to represent the identity function, considered important for batch normalization in \cite{IoffeICML2015}. Our reparameterization introduces a learnable, auxiliary parameter $\alpha'$ such that $\alpha$ as $\alpha = 1 + \alpha'$ to solve this problem.


\chapter{Specifications}\label{ch:specs}

The application developed implements the \emph{Connect 4} (also called
\emph{Four-In-A-Row}) online game. It allows the users, registered through the
server, to play the game against each other over the network.

The application allows the users, already registered on the server, to log-in
using \emph{public key authentication}. Server and client authenticates each
other verifying that their public keys are valid. The public key of the server
is verified through a certification authority (CA).

After the log-in, a user can see all the other users connected to the server and
challenge one of them. If the challenged user accept the challenge, the game is
started and the two users initiate a \emph{peer-to-peer session} to exchange
messages regarding the game \idest{the moves they make in the game}.

The server knows when two users are playing and when their game ends. In this
way, other users that want to play can not challenge a user that is already in a
game.

The communication between the client and the server and during the
client-to-client peer-to-peer session are secured using the \openssl{} library.
In particular, when a client connects to the server or to another client, a
\emph{key negotiation algorithm}, based on the \emph{Diffie-Hellman key exchange
algorithm}, is executed. Each message exchanged during this negotiation is
authenticated using \emph{public key authentication}. When the two parties have
agreed on the session key, they encrypt each subsequent message using AES
symmetric encryption in \emph{Galois/Counter Mode} (GCM) with a key of 128 bits
(AES-128-GCM).

The application is developed using the C language and offer a command-line
interface (CLI) to the user. The user makes actions in the application by typing
commands when the \emph{prompt} (\code{>}) is shown. During the game, the
application shows the game board in a table where the \code{X} symbol represents
the discs inserted by the user while the \code{O} symbol represents the discs
inserted by the opponent.

For details on how to run and use the application,
see~\appendixref{appendix:howtorun}.


% TODO : explain something
% canal blablabla
% probes : segment & points

see figure \ref{domain}
\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../canal.png}
\caption{Description of the computational domain depicting the probes position.}
\label{domain}
\end{figure}

\section{Configuration}

This validation file was made to show what can be done with the Python post processing code. In this test case, we have
three folders named sim\_1, sim\_2, sim\_3 with sim\_2 the continuation of sim\_1 and sim\_3 the continuation of sim\_2.
The goal is to do statistical post processing of the results coming from the .son and .dt\_ev files. \\

\newpage
\section{Step time}
In this section, we will plot simulation step time. Other quantities can be displayed from .dt\_ev files
as long as the correct keyword is given.

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_1.png}
\caption{Evolution of step time.}
\label{fig1}
\end{figure}

\newpage
\section{Velocity}
Plots the instantaneous probes evolution of velocity field and fluctuations.

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_2.png}
\caption{velocity}
\label{fig2}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_3.png}
\caption{Velocity fluctuation.}
\label{fig3}
\end{figure}

\newpage
\section{Density}
An example with an other field: the instantaneous density evolution.

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_4.png}
\caption{Density instantaneous.}
\label{fig4}
\end{figure}

\newpage
\section{Pressure}
Displayong the pressure evolution, fluctuations and finally spatial mean over the segment.

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_5.png}
\caption{Pressure instantaneous.}
\label{fig5}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_6.png}
\caption{Spatial mean pressure.}
\label{fig6}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_7.png}
\caption{Spatial mean pressure.}
\label{fig7}
\end{figure}

\newpage

\section{Average windows on velocity}
Example of window averaging.

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_8.png}
\caption{Average windows.}
\label{fig8}
\end{figure}

\newpage

\section{Autocorrelation and Spectrum}
We can perform an autocorrelation of the velocity signal, the root of parabola fit gives us the taylor micro scale.
After that, the energy spectrum is computed using welch method.

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_9.png}
\caption{Autocorrelation and parabola fit.}
\label{fig9}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[width=.6\linewidth]{\orig/.tmp/../sim_3/results/fig_10.png}
\caption{Energy spectrum.}
\label{fig10}
\end{figure}


\chapter{Background}\label{chap:background}

Imagine the following task between two players, Anne and Bill. Between Anne and Bill is a lever in the middle position and has five positions in total. Anne thinks the lever should be pulled two positions to her side and Bill thinks the lever should be pulled two positions to his side.
Anne does not know Bill has a goal on his side, and Bill does not know Anne has a goal on her side.
This can be seen in figure \ref{leverexample}.\\
\input{figures/pictures/Hebelbild} \\

With the asynchronous execution order this task could be easily finished if Anne or Bill just pulled the lever two times in their direction. But just as easily this task could go on a really long time if Anne and Bill pulled the lever alternating, once to the right and then to the left, then to the right again and so on. This could go on infinitely. With no set execution order, how can we prevent the task from going on infinitely? To answer this question we first need to introduce the formal framework.

\section{Dynamic Epistemic Logic}
In the following we will define and explain the core concepts of the Dynamic Epistemic Logic (DEL). DEL is a specific mathematical language used as the framework of this thesis.

The definitions are taken from the work of Bolander et al. \cite{bolander2018better} and \cite{bolander2017gentle} and the book ``Dynamic epistemic logic'' from Ditmarsch \cite{Ditmarsch2007}.

Let $\mathcal{A}$ be a finite set of agents, from the example above this would be $\mathcal{A} =\{$Anne, Bill$\}$. Let $\mathcal{P}$ be a finite set of atomic propositions. Atomic propositions, like $p$ or $q$, describe some affairs that can be true or false.
The epistemic language $\mathcal{L}_{\text{KC}}$ is: \\
$$
\varphi ::= \top \ | \ \bot \ | \ p \ | \ \neg \varphi \ | \ \varphi \wedge \varphi \ | \ K_i \varphi \ | \ C\varphi
$$

with $p \in \mathcal{P}$ and $i \in \mathcal{A}$.
$\top$ describes $\varphi$ as being true, $\bot$ as false.
$K_i \varphi$ reads as ``Agent $i$ knows $\varphi$''. $C \varphi$ reads as ``it is common knowledge that $\varphi$ ''.\\
From the example before, the two agents, $a$ (Anne) and $b$ (Bill) have two goal positions, goal $p$ (lever to the left) and $q$ (lever to the right). Anne knows that one goal position is the left, but does not know the other: $K_a p \wedge \neg K_a q$ . Bill knows that one goal position is the right, but does not know the other position: $\neg K_b  p \wedge K_b q$.

Formulas are evaluated in epistemic models %\todo{not defined}
$$
\mathcal{M}=(W, (\sim_i)_{i \in \mathcal{A}}, V)
$$
with the domain $W$ being a nonempty finite set of worlds,
$\sim_i$ being an equivalence relation called the indistinguishably relation for agent $i \in \mathcal{A}$ and $V : \mathcal{P} \rightarrow \mathcal{P}(W)$ assigning a valuation to each atomic proposition.

Using Anne and Bill as an example, Anne only knows $p$ is true. She does not know if $p \land q$ or $p \land \neg q$, so she sees two worlds. Those two worlds are indistinguishable for Anne. Bill only knows $q$ is true, he does not know if $p \land q$ or $\neg p \land q$. One world where just the lever to the left is a goal, a world where another goal is the lever to the right and a world where just the lever to the right is a goal. Let $w_1$ be the world where just the lever to the left is a goal, $w_2$ be the world where the goal is the lever to the left or to the right and $w_3$ be the world where the goal is the lever to the right. Then $w_1 \sim_\text{Anne} w_2$ and $w_2 \sim_\text{Bill} w_3$. A graphic representation of this would be the global state $s = (\mathcal{M}, w_2)$ with the nodes representing the worlds and the edges representing the indistinguishably relation. The circle around a node represent designated worlds. In all graphic representations in this thesis we usually omit reflexive edges and edges that can be implied by transivity for better readability.
\[
s=
\begin{tikzpicture}
  \draw (-2,0) node[world, label=below:{$w_1: p, \neg q$}] (w1) {};
  \draw (0,0) node [desig] {}; % designation
  \draw (0,0) node[world, label=below:{$w_2: p,q$}] (w2) {};
  \draw (2,0) node[world, label=below:{$w_3: \neg p, q$}] (w3) {};
  \draw (w1) -- node[above] {Anne} (w2);
  \draw (w2) -- node[above] {Bill} (w3);
\end{tikzpicture}
\]

For $W_d \subseteq W$, the pair $(\mathcal{M}, W_d)$ is called an epistemic state (or simply a state) and the worlds of $W_d$ are called designated worlds. A state is called global if $W_d=\{w\}$ for some world $w$ (called the actual world). We then often write $(\mathcal{M},w)$ instead of $(\mathcal{M},\{w\} )$. We use $S^{gl}(P,\mathcal{A})$ to denote the set of global states (or simply $S^{gl}$ if $P$ and $\mathcal{A}$ are clear from context). For any state $ s=(\mathcal{M}, W_d) $ we let $Globals(s)= \{ (\mathcal{M},w) | w \in W_d \} $.
A state $(\mathcal{M}, W_d)$ is called a local state for agent $i$ if $W_d$ is closed under $\sim _i$ (that is, if $w \in W_d$ and $w \sim _i v $, then $v \in W_d$).
Given a state $s=(\mathcal{M}, W_d)$ the associated local state of agent $i$, denoted $s^i$, is $(\mathcal{M},\{v|v\sim _i w \text{ and } w \in W_d\})$. Going from $s$ to $s^i$ amounts to a \textit{perspective shift} to the local perspective of agent $i$.

In the example above, Anne has the local state $s^{\text{Anne}}=(\mathcal{M},\{w_1,w_2\})$ and Bill has the local state $s^{\text{Bill}}=(\mathcal{M},\{w_2,w_3\})$.

Let $(\mathcal{M}, W_d)$ be a state on $P,\mathcal{A}$ with $\mathcal{M}=(W, (\sim_i)_{i \in \mathcal{A}}, V)$. For $i \in \mathcal{A}$, $p \in P$ and $\varphi, \psi \in \mathcal{L}_{\text{KC}}(P,\mathcal{A})$, we define truth as follows:
\begin{align*}
  &(\mathcal{M}, W_d) \models \varphi
    & &\text{ iff } \qquad
    (\mathcal{M},w)\models \varphi \text{ for all } w \in W_d \\
  &(\mathcal{M}, w) \models p
    & &\text{ iff } \qquad
    w \in V(p) \\
  &(\mathcal{M}, w) \models \neg \varphi
    & &\text{ iff } \qquad
    (\mathcal{M},w) \not\models \varphi \\
  &(\mathcal{M}, w) \models \varphi \wedge \psi
    & &\text{ iff } \qquad
    (\mathcal{M},w) \models \varphi \text{ and } (\mathcal{M},w) \models \psi \\
  &(\mathcal{M}, w) \models K_i \varphi
    & &\text{ iff } \qquad
    (\mathcal{M},v) \models \varphi \text{ for all } v \sim_i w \\
  &(\mathcal{M}, w) \models C \varphi
    & &\text{ iff } \qquad
    (\mathcal{M},v) \models \varphi \text{ for all } v \sim^* w \\
  &(\mathcal{M}, w) \models \top
    & &\text{        } \qquad
    \text{always} \\
  &(\mathcal{M}, w) \models \bot
    & &\text{        } \qquad
    \text{never}
\end{align*}
where $\sim^*$ is the transitive closure of $\bigcup_{i \in \mathcal{A}}\sim_i$.

From the example above, in $(\mathcal{M},w_2)$, $w_2$ is the designated world. In this designated world, Anne still does not know if $q$ is true or not. $(\mathcal{M},w_2) \models \neg K_aq \wedge \neg K_a \neg q$.
Performing a perspective sift on $s=(\mathcal{M},w_2)$ for Anne is $(\mathcal{M},w_2)^{\text{Anne}} =(\mathcal{M},\{w_1,w_2\})$. This perspective shift gives us $(\mathcal{M},\{w_1,w_2\})\not\models q$ and $(\mathcal{M},\{w_1,w_2\})\not\models \neg q$. This way, we can verify the knowledge from the global world for an agent by performing a perspective shift for that agent.

\subsection{Epistemic Actions and Product Updates}

An event model is a 4-tuple $\mathcal{E} = \langle E, (\sim_i)_{i\in \mathcal{A}}, pre, \textit{eff}  \rangle$ where the domain $E$ is a non-empty finite set of events; $\sim_i \subseteq E \times E$ is an equivalence relation called the indistinguishably relation for agent $i$;
$pre:E \rightarrow \mathcal{L}_{KC}$ assigns a precondition to each event;
and $\textit{eff}:E \rightarrow \mathcal{L}_{KC}$ assigns a post condition, or effect to each event.
For all $e\in E$, $\textit{eff}(e)$ is a conjunction of literals, that means atomic propositions and their negations, including $\top$ and $\bot$.\\
For $E_d \subseteq E$, the pair $(\mathcal{E}, E_d)$ is called an epistemic action, or simply action and the events in $E_d$ are called a local action for agent $i$ when $E_d$ is closed under $\sim_i$. \\
Each event of an action represents a different possible outcome.
By using multiple events $e, e' \in E$ that are indistinguishable ($e \sim e' )$, it is possible to model only partially observable actions.\\
If the event model has $E=\{e\}$, we will write $\mathcal{E}=\langle pre(e), \textit{eff}(e)\rangle$.

The product update is used to specify the next state resulting from performing an action in a state.
Let a state $s = (\mathcal{M},W_d)$ and an action $a=(\mathcal{E},E_d)$ be given with $\mathcal{M}=\langle W,(\sim_i)_{i \in \mathcal{A}}, V\rangle $ and $\mathcal{E}=\langle E, (\sim_i)_{i \in \mathcal{A}},pre, \textit{eff} \rangle$
then the product update of $s$ with $a$ is defined as $s \otimes a = ((W',(\sim_i')_{i \in \mathcal{A}}, W_d'))$ where :

 \begin{itemize}
   \item each world is paired up with all applicable events \\
   $W'=\{(w,e)\in W \times E ~|~ \mathcal{M}, w \models pre(e)\};$
   \item new worlds are indistinguishable if the old worlds and the events are indistinguishable \\
   $\sim_i'=\{((w,e),(w',e')) \in W'\times W' ~|~ w \sim_i w' \text{ and } e \sim_i e'\};$
   \item propositions become true if the proposition occurred as a positive literal in the effect of the event or the proposition was satisfied in the world before and did not appear negative in the effect \\
   $V'(p) = \{ (w,e) \in W' ~|~ \textit{eff}(e) \models p \text{ or } (\mathcal{M},w \models p \text{ and } \textit{eff}(e)\not \models \neg p)\};$
   \item worlds are designated if both predecessor world and event are designated. \\
   $W_d' = \{ (w,e) \in W' ~|~ w \in W_d \text{ and } e \in E_d\}$.
 \end{itemize}

$a=(\mathcal{E}, E_d)$ is applicable in $s=(\mathcal{M},W_d)$ if for all $w \in W_d$ there is an event $e \in E_d$ such that $(\mathcal{M},w) \models pre(e)$.

Let us imagine a planning task between Lea and Max. Lea puts two cards face down in front of Max. The goal is that Max picks up the queen. The problem here is that Max does not know where the queen is. \\
Lea has put the queen to the right, so $qR$.

\[
s=
\begin{tikzpicture}
  \draw (-2,0) node[world, label=below:{$w_1: \neg qR$}] (w1) {};
  \draw (0,0) node [desig] {}; % designation
  \draw (0,0) node[world, label=below:{$w_2: qR$}] (w2) {};
  \draw (w1) -- node[above] {Max} (w2);
\end{tikzpicture}
\]

There are two types of actions that could be performed to solve this problem:
\begin{enumerate}
  \item Announcement actions \\
    Lea could tell Max where she put the queen. $\omega(a_{\text{announcement}})=\text{Lea}$

    \[
    \begin{tikzpicture}
      \draw (-2,0) node[world, label=below:{$w_1: \neg qR$}] (w1) {};
      \draw (0,0) node [desig] {}; % designation
      \draw (0,0) node[world, label=below:{$w_2: qR$}] (w2) {};
      \draw (w1) -- node[above] {Max} (w2);
    \end{tikzpicture}
    %
    ~\otimes~
    %
    \begin{tikzpicture}
      \draw (0,0) node [desig] {}; % designation
      \draw (0,0) node[world, label=below:{$e_1: \langle qR, \top \rangle $}] (w1) {};
    \end{tikzpicture}
    %
    ~=~
    %
    \begin{tikzpicture}
      \draw (0,0) node [desig] {};
      \draw (0,0) node[world, label=below:{$(w_2, e_1): qR$}] (w1e1) {};
    \end{tikzpicture}
    \]

  \item Sensing actions \\
    Max could look at the left card, then he knows where the queen is.
    $\omega(a_{\text{sense}})=\text{Max}$

    \[
    \begin{tikzpicture}
      \draw (-2,0) node[world, label=below:{$w_1: \neg qR$}] (w1) {};
      \draw (0,0) node [desig] {}; % designation
      \draw (0,0) node[world, label=below:{$w_2: qR$}] (w2) {};
      \draw (w1) -- node[above] {Max} (w2);
    \end{tikzpicture}
    %
    ~\otimes~
    %
    \begin{tikzpicture}
      \draw (-2,0) node[world, label=below:{$e_1: \langle qR, \top \rangle $}] (e1) {};
      \draw (0,0) node [desig] {}; % designation
      \draw (0,0) node[world, label=below:{$e_2: \langle \neg qR, \top \rangle $}] (e2) {};
    \end{tikzpicture}
    %
    ~=~
    %
    \begin{tikzpicture}
      \draw (-2,0) node[world, label=below:{$w_1: \neg qR$}] (w1) {};
      \draw (0,0) node [desig] {}; % designation
      \draw (0,0) node[world, label=below:{$w_2: qR$}] (w2) {};
    \end{tikzpicture}
    \]

\end{enumerate}

\subsection{Planning tasks}

A planning task $\Pi = \langle s_0, A, \omega, \gamma \rangle$ consists of a global state $s_0$ called the \textit{initial state}; a finite set of actions A; an owner function $\omega: A \rightarrow \mathcal{A}$; and a \textit{goal formula} $\gamma \in \mathcal{L}_{KC}$.

Consider a version of the lever problem from before. For simplicity, in this example there is only one player and the lever can only be pulled once. The planning task $\langle s_0, \{ a_1 \} , \omega, p \rangle$ consists of the initial state $s_0 = $
\begin{tikzpicture}
  \draw (0,0) node [desig] {}; % designation
  \draw (0,0) node[world, label=below:{$\neg p$}] (w1) {};
\end{tikzpicture}
with the lever being in the upright position. The action $a_1$ =
\begin{tikzpicture}
  \draw (0,0) node [desig] {}; % designation
  \draw (0,0) node[world, label=below:{$e_1: \langle \top, p \rangle $}] (w1) {};
\end{tikzpicture}
has the owner $\omega(a_1) = 1$ (player 1). Everything is fully observable for the agent. The intuitive  solution should prescribe the action $a_1$ to agent 1, pulling the lever to the right.

\[
\begin{tikzpicture}
  \draw (0,0) node [desig] {}; % designation
  \draw (0,0) node[world, label=below:{$w_1: \neg p$}] (w1) {};
\end{tikzpicture}
%
~\otimes~
%
\begin{tikzpicture}
  \draw (0,0) node [desig] {}; % designation
  \draw (0,0) node[world, label=below:{$e_1: \langle \top, p \rangle $}] (w1) {};
\end{tikzpicture}
%
~=~
%
\begin{tikzpicture}
  \draw (0,0) node [desig] {};
  \draw (0,0) node[world, label=below:{$(w_1, e_1): p$}] (w1e1) {};
\end{tikzpicture}
\]

A policy $\pi$ for $\Pi = \langle s_0, A, \omega, \gamma \rangle$ is a partial mapping $\pi: S^{gl} \hookrightarrow \mathcal{P}(A)$ such that:
\begin{enumerate}
  \item Applicability\\
    We require actions to be applicable in all states they are assigned to: \\
    for all $a \in S^{gl}, a \in \pi(s): a$ is applicable in $s$.
  \item Uniformity \\
    If the policy $\pi$ prescribes some action $a$ to agent $i$ in state $s$ and agent $i$ cannot distinguish $s$ from some other state $t$, then $\pi$ has to prescribe the same action $a$ for $i$ in $t$ as well: \\
    for all $s,t \in S^{gl} $ such that $ s^{\omega(a)} = t^{\omega(a)}, a \in \pi(s): a \in \pi(t)$

    A planning task with the agents Lea and Max,
    initial state Lea puts two cards face down on the table, one of them is a queen, the other a king. Max does not know if the queen is on the left or the right side. The goal is that Max turns over the queen. Max has two actions, he can either turn over the left card or the right card. The policy in which Max turns over the left card is a valid policy because even though he does not know if that card is the queen, the action is prescribed in both cases.

  \item Determinism \\
    We require $\pi$ to be unambiguous for all agents in the sense that in each state $s$ where an agent $i$ is supposed to act according to $\pi$, $\pi$ will always prescribe the same action for agent $i$.
\end{enumerate}

The properties uniformity and applicability together imply knowledge of preconditions, the property that in each state, an agent who is supposed to perform a particular action must also know that the action is applicable in that state.

We also must allow policies to sometimes prescribe multiple actions of different owners to the same state. This is because the set of indistinguishable states can differ between the agents. To characterize the different outcomes of agents acting according to a common policy, we define the notion of policy executions.

An execution of a policy $\pi$ from a global state $s_0$ is a maximal (finite or infinite) sequence of alternating global states and actions $(s_0, a_1, s_1, a_2, s_2,...)$, such that for all $ m \leq 0$
\begin{enumerate}
  \item $a_{m+1} \in \pi(s_m)$ and
  \item $s_{m+1} \in Globals(s_m \otimes a_{m+1})$
\end{enumerate}
An execution is called successful for a planning task $\Pi = \langle s_0, A, \omega, \gamma \rangle$, if it is a finite execution $(s_0, a_1, s_1,...,a_n, s_n)$ such that $s_n \models \gamma$.

%\extend{Example: Anna lets Bill pull the lever to the right. Globally this makes sense, since that is Bills goal. But individually, this makes no sense at all because Anne doesent know Bills goal.}

In the lever example, a policy could be that, starting in the position with the lever being in the middle, Bill pulls the lever to the right and then to the right again. This is a policy that satisfies all properties and where the last state satisfies the goal formula. The execution of the policy is finite and successful. \\
For Bill, this is a reasonable policy. It satisfies the goal formula he knows to be true, with the lever being at the far right position. But for Anne, it is not a reasonable policy because she can not see that the execution of this policy will satisfy the goal formula.

%\todo{Geht das hier etwas schöner?}
We now want to restrict our focus to policies that are guaranteed to achieve the goal after a finite number of steps. More formally, all of their executions must be successful. As in nondeterministic planning, such policies are called strong (Cimatti et al. \cite{cimattietal}).
For a planning task $\Pi = \langle s_0, A, \omega, \gamma \rangle$, a policy $\pi$ is called strong if $s_0 \in \text{Dom}(\pi) \cup \{s \in S^{gl} ~|~ s \models \gamma\}$ and for each $s \in \text{Dom}(\pi)$, any execution of $\pi$ from $s$ is successful for $\Pi$. A planning task is called solvable if a strong policy for $\Pi$ exists.
For $ i \in \mathcal{A} $ , we call a policy $i$-strong if it is strong and  $Globals(s_0^i ) \subseteq \text{Dom}(\pi) \cup\{ s \in S^{gl} ~|~ s \models \gamma \}$.

When a policy is i-strong it means that the policy is strong and defined on all the global states that agent $i$ cannot distinguish inbetween. It follows directly from the definition that any execution of an $i$-strong policy from any of those initially indistinguishable states will be successful. So if agent $i$ comes up with an $i$-strong policy, agent $i$ knows the policy to be successful.

The policy from above, with Bill pulling the lever to the right twice is Bill-strong but not Anne-strong.

Sometimes the agents cannot coordinate their plans but rather have to come up with plans individually. These plans can differ a lot, the agents often have different knowledge about the states, the actions and therefore the action outcomes. For this reason we will define a policy profile for a planning task $\Pi$ to be a family $(\pi_i)_{i \in \mathcal{A}}$ where each $\pi_i$ is a policy for $\Pi$. We assume actions to be instantaneous and executed asynchronously. This leads to the following generalization:

An execution of a policy profile $(\pi_i)_{i \in \mathcal{A}}$ is a maximal (finite or infinite) sequence of alternating global states and actions $(s_0, a_1, s_1,...)$, such that for all $m \leq 0$,
\begin{enumerate}
  \item $a_{m+1} \in \pi_i(s_m)$ where $i=\omega(a_{m+1})$ \\
    Note here the source of nondeterminism as a result from the possibility of multiple policies prescribing actions for their respective agents.
  \item $s_{m+1} \in Globals(s_m \otimes a_{m+1}) $ \\
    Here the source of nondeterminism is from the possibility of nondeterministic action outcomes.
\end{enumerate}

If all agents have one strong policy in common which all of them follow, then at execution time, the goal is guaranteed to be eventually reached. If, however, each agent acts on its individual strong policy, then the incompatibility of the individual policies may prevent the agents from reaching the goal, even though each individual policy is strong.

The cost of a a policy can be defined as its worst-case execution length, the number of actions in its longest possible execution. An optimal policy is one with minimal costs. Due to partial observability and different knowledge, the agents might assign different costs to the same policy and therefore the costs are subjective. \\
Let $\pi$ be a strong policy for a planning task $\Pi$. The perspective-sensitive cost (or simply cost) of $\pi$ from a state $s\in Dom(\pi)$, denoted $\kappa_{\pi}(s)$ is defined as:
$$\kappa_{\pi}(s) =
\begin{cases}
0 \qquad & \text{if there exists no } a \in \pi(s)\\
1+ \max_{a\in \pi(s), s'\in Globals(s^{\omega(a)} \otimes a)} \kappa_{\pi}(s') \qquad & \text{else.}
\end{cases}$$


\documentclass{easychair}

\usepackage{doc}
\usepackage{scalerel}
\usepackage{amsfonts}

\newcommand{\easychair}{\textsf{easychair}}
\newcommand{\miktex}{MiK{\TeX}}
\newcommand{\texniccenter}{{\TeX}nicCenter}
\newcommand{\makefile}{\texttt{Makefile}}
\newcommand{\latexeditor}{LEd}
\newcommand{\emptycon}{\scaleobj{.75}\bullet}
\newcommand{\ext}{\triangleright}
\newcommand{\arri}{\Rightarrow}

\newcommand{\ms}[1]{\mathsf{#1}}
\newcommand{\ToS}{\mathsf{ToS}}
\newcommand{\U}{\mathsf{U}}
\newcommand{\Code}{\mathsf{Code}}
\newcommand{\Ty}{\mathsf{Ty}}
\newcommand{\V}{\mathsf{V}}
\newcommand{\C}{\mathsf{C}}
\newcommand{\Bool}{\ms{Bool}}

\title{Staged Compilation and Generativity%
  \thanks{The author was supported by the European Union, co-financed
    by the European Social Fund (EFOP-3.6.3-VEKOP-16-2017-00002).}}

\author{
Andr\'as Kov\'acs
}

% Institutes for affiliations are also joined by \and,
\institute{
  E\"otv\"os Lor\'and University,
  Budapest, Hungary \\
  \email{kovacsandras@inf.elte.hu}
}

\authorrunning{Kov\'acs}
\titlerunning{Staged Compilation and Generativity}
\pagenumbering{gobble}
\begin{document}

\maketitle

The purpose of staged compilation is to write code-generating programs in a safe
and ergonomic way. Although it is always possible to write metaprograms by
simply manipulating strings or deeply embedded syntax trees, this is often
error-prone and tedious. Staging is a way to have more guarantees about the
safety and well-typing of metaprograms, and also a way to integrate object-level
and meta-level syntaxes more organically.

Two-level type theory (2LTT) \cite{twolevel} was originally developed for the
purpose of doing synthetic homotopy theory, by adding a metaprogramming layer on
top of homotopy type theory \cite{hottbook}. However, it turns out that 2LTT is
also a great framework for metaprogramming and staging in general; it is
applicable to a wide range of object theories.

There is a natural semantics to 2LTT which justifies the metaprogramming view:
this is the \emph{presheaf model} of 2LTT. Here, meta-level types are presheaves
over the underlying category of the object theory. Hence, every meta-level
construction must be stable under the object-level morphisms.

\paragraph{Generativity}Generativity means that we can only generate code, but not look inside
object-level syntax and make decisions based on that. Generativity simplifies
staging, and it is often enforced in practical implementations
\cite{kiselyov14metaocaml}. However, non-generative staging provides additional
power and flexibility. A simple example for a non-generative feature is
conversion checking. Assume that given an object-level type $A$, $\Code\,A$ is
the type of meta-level programs which compute $A$-expressions. Conversion
checking may be postulated as $\ms{conversion}_A : (t\,u : \Code\,A) \to (t = u)
+ (t \neq u)$. We aim to investigate generativity in the presheaf model. We
observe that it depend on the choice of morphisms in the object theory.

If base morphisms are \emph{substitutions}, then $\ms{conversion}$ does not hold
in the model, because definitional inequality is not stable under substitution. For
example, inequal variables may become equal after substitution.

If base morphisms are \emph{weakenings}, then $\ms{conversion}$
holds. However, in this case the object theory can only support features which
can be specified with weakenings. For instance, this allows simple types at at
the object level, without any $\beta$-rules, but it does not allow polymorphism
or dependent types. This is still sufficient for many staging applications.

\paragraph{Yoneda lemma} It is worth to note that we get generativity statements
in the presheaf model from the Yoneda lemma. For example, the natural
transformation $\ms{y}\,\Bool_0 \Rightarrow \Bool_1$ must be a constant map in the
model. How is it possible then to have non-generativity? The answer is that
the Yoneda lemma can only meaningfully apply to \emph{types} (as opposed to
contexts) in the presheaf model, if context extension is preserved by Yoneda
embedding. If base morphisms are weakenings, the base category does not have all
binary products, and context extension is not preserved by $\ms{y}$. Hence, the
singleton context $\emptycon,\ms{y}\,\Bool_0$ is not representable, and the
Yoneda lemma does not say anything about dependency on $\ms{y}\,\Bool_0$.

\bibliographystyle{plain}
\bibliography{references}

\end{document}


\subsection{Model construction}
\label{S:MODELCONSTRUCTION}

%Once the model has been configured, the next step is to build the model.
Model construction builds the full model matrices $A$, $C$, $Q$, $R$ by assembling the sub-matrices associated with each block component and each time-series.
The corresponding values for the model parameters are also considered during model construction.
%Because the model matrices are often time-dependent, OpenBDLM creates a function for each matrix, that depends on the time $t$, and model parameters $\bm\theta$.

\subsubsection{Model construction functions}

The model construction workflow is presented in Figure~\ref{FIG:ModelConstructionWorkflow}. The function in OpenBDLM used for model construction is:

\begin{description}[style=unboxed]
\item[Builds the model ] \leavevmode
  \begin{lstlisting}[ basicstyle = \mlttfamily \small, breaklines=true]
  [model, misc]=buildModel(data, model, misc)
 \end{lstlisting}
\end{description}

\begin{figure}[!h]
  \centering
  \captionsetup{justification=centering}
\scalebox{0.7}{
\begin{tikzpicture}

\node[parabisque](inputModelConstruct){\begin{tabular}{c} \lstinline[basicstyle = \mlttfamily \small ]!model.components.block! \\ \lstinline[basicstyle = \mlttfamily \small ]!model.components.ic! \\  \lstinline[basicstyle = \mlttfamily \small ]!model.components.const!  \\ \lstinline[basicstyle = \mlttfamily \small ]!data!  \end{tabular}};
\node[esbisque](ModelConfiguration)[below of = inputModelConstruct, yshift = -1.5cm]{\phantom{} ModelConfiguration.m \phantom{}};
\node[esbisque](defineModel)[below of = ModelConfiguration, yshift = -1cm]{\phantom{} defineModel.m \phantom{}};
\node[esbisque](buildModel)[below of = defineModel , yshift=-1cm, xshift = 0cm]{\phantom{} buildModel.m \phantom{}};

\node[parabisque](outputModelConstruct)[below of = defineModel, yshift = -3.5cm]{\begin{tabular}{c}  \lstinline[basicstyle = \mlttfamily \small ]!model.A,C,Q,R,Z!  \\ \lstinline[basicstyle = \mlttfamily \small ]!model.param_properties! \\ \lstinline[basicstyle = \mlttfamily \small ]!model.initX! \\  \lstinline[basicstyle = \mlttfamily \small ]!model.initV!  \\ \lstinline[basicstyle = \mlttfamily \small ]!model.initS!  \end{tabular}};

\path[->, thick] (inputModelConstruct)edge(ModelConfiguration);
\path[->, thick] (ModelConfiguration)edge(defineModel);
\path[->, thick] (defineModel)edge (buildModel);
\path[->, thick] (buildModel)edge (outputModelConstruct);

\end{tikzpicture} }
\caption{Model construction workflow} \label{FIG:ModelConstructionWorkflow}
\end{figure}


\chapter{Digital Media}

\section{Text}

We've already looked at one method of sending digital text: Morse code. A piece of text is broken up in to a sequence of individual blocks, and somehow the content of that block is specified by a code. A page is broken into a sequence of lines. And each line is broken in to a sequence of characters.\\

\begin{center}\imagegraphic[0.75]{text.png}\end{center}

For a digital text, the available characters are determined first. Basically, a number, or code, is assigned for each separate character we wish to be able to print. Capital letters are separate characters than lower case. A space is a character. More characters are needed for punctuation and other symbols like \$, \&, and any other symbol we wish to be able to represent.\\

We don't want all the text to be on the same line, and so there needs to be a way to tell where the text appears in the vertical direction. Just as there is a character to represent a space, which basically means moving over a certain amount without printing anything, there is a character to represent the action of going to a new line of text without printing anything. This is called the new-line character. Even though we can't 'see' the newline character on a page of text, just like the space character, we can its result. A new-line is usually what happens when you hit 'enter' on your keyboard when typing.\\

Instead of sending dots and dashes, the codes are binary numbers, since our hardware is built to be able to store and send any sequence of bits. Now what numbers, specifically, represent each character? For example, what might be the number for the letter 'A'? Well, it really depends. In some sense the numbers chosen are arbitrary. However, there are certain standards that have been established.\\

If you wish to represent a single character by 8 bits (an 8 bit number), then one of the first standards was the ASCII standard. In ASCII, the letter 'A' is number 65 (in decimal). The letter 'a' is 97. A space is 32, and a new line is 10. Since there are 8 bits to work with, there are 256 possible characters.\\

The ASCII standard is somewhat language specific. Many languages require additional characters. The Unicode standard is a way to add more possible characters needed for other languages and symbols to be written, and itself varies in how many bits are required to represent individual characters. UTF-8 is a variable length 8-bit version which is replacing ASCII. The first 128 characters of UTF-8 are exactly the same as ASCII. When the above message "Have a good day!" is encoded in UTF-8, the result is the following hexadecimal numeric values of the characters.\\

\begin{center}\imagegraphic[0.75]{text_code.png}\end{center}

The message is composed of a total of 16 characters, each requiring 8 bits, or 1 byte, to store. To send this message is then 16 bytes of information. Converted to a string of bits, the above message would look like the following, since each character has to be 8 bits:\\

\begin{verbatim}
01000100011000010111011001100101001000000110000100100000
01100111011011110110111101100100001000000110010001100001
0111100100100001
\end{verbatim}

The numeric value of a Unicode character must be found in a lookup table. As a shorthand, a format U+\#\#\#\# (where \# are hexadecimal digits) is used to say "this is a Unicode character with a value of \#\#\#\#". So, the letter 'H' is character U+0044. A space is U+0020, and so on. Remember, this is hexadecimal, not decimal: \(20_{hex} = 32_{dec}\).\\

Notice there are 4 reserved digits (2 bytes). Just doing the math, and knowing these are hexadecimal digits, that gives \(16^4 = 65,536\) possibilities here. However, this is just one sub-set of the total Unicode characters. There are currently a total 17 sets, which specifies a total of \(17 \times 65,536 = 1,114,112\) different characters.\\

\section{Text Compression}

Text is an excellent subject for compression, because it is very redundant. The average information content per character of text is usually only about 3 bits (sometimes even lower), versus the 8 bits we might store it as. The reason it doesn't contain quite as much information as we think is because of patterns. A pattern means each character is less surprising, and we might be able to guess what it is. For example, if I just removed some characters from a word by replacing them with asterisks, could you still guess what the word is? What w***s d* y** t***k I *m typ*** i* t*** se****c*?\\

Humans are very good at seeing patterns by instinct, but it's hard to spell out exactly how we do it. What we need is a method, or algorithm, to find a pattern and use it to remove redundant information, but still be able to replace it later to reconstruct the original.\\

One method is called the Lempel-Ziv algorithm. This algorithm looks for a pattern where a specific sequence of characters appears in more than one place in a piece of text. Let's look at an example that contains a lot of redundancy:\\

\begin{verbatim}
How much wood would a woodchuck chuck if a woodchuck could chuck
 wood?
\end{verbatim}

I'm going to scan through the text, and look at pairs of characters. For each pair, I'm going to scan backwards to see if that pair occurred before. If it did, I'm going to replace the pair with a number (in brackets) that says how many characters back it occurred. The idea is if I can replace 2 characters, which would be 16 bits total, with a single number, which I would store as 8 bits, then every replacement would reduce the total by 8 bits.\\

\begin{verbatim}
How much wood[5]oul[6]a[8][13]d[20][23]k [6][6][6]if[21][21][21]
[21][21][21][21][39][39][6][12][12][22][22]d?
\end{verbatim}

The original sentence had 70 characters, or a total of 560 bits. After the replacements it has 24 letters and 23 numbers, for a total of 376 bits. That gives a compression of about 67\%. But it's only useful if it can be reversed.\\

Start scanning from the beginning of the result. When you get to a number in brackets, go backwards that many characters. Get the two characters that start there, and replace the number in brackets with those two characters. For example, the first number is [5]. So I go back 5 characters. The two characters there are " w" (a space, then 'w'), so I replace the [5] with " w", giving the following:

\begin{verbatim}
How much wood woul[6]a[8][13]d[20][23]k [6][6][6]if[21][21][21]
[21][21][21][21][39][39][6][12][12][22][22]d?
\end{verbatim}

The next number is [6]. 6 characters back from that is "d " ('d', then a space).

\begin{verbatim}
How much wood would a[8][13]d[20][23]k [6][6][6]if[21][21][21]
[21][21][21][21][39][39][6][12][12][22][22]d?
\end{verbatim}

Try uncompressing the following message on your own, using the same rules. How many characters are in the original message, and what is the compression factor?

\begin{verbatim}
Can you [8][8]a[6][6][6]s[3][9][9]ner[7][7][4][4][17][6][6]?
\end{verbatim}

The algorithm can be made better by looking at more than 2 characters at a time. Imagine if we can replace entire words with a single number. The compression of text can typically be quite good because of how redundant it really is. However, how much it \textit{can} be compressed is highly dependent on the text itself. How good an algorithm is also usually depends on how much effort is expended to find more obscure patterns. Thus, better compression tends to take longer to perform.

\section{Raster Graphics}

\epigraph{Life isn't always \#000000 and \#FFFFFF. It's a million areas of \#888888.}{\textit{unknown}}

The first step to digitize something is to decide how to break it into a discrete number of elements in a systematic way, where each element can take on one of multiple possible values. Then to assign a code to each possible value an element can have. The codes are what are stored or transmitted. To re-construct the original thing requires knowing the meaning of the codes, and effectively running the deconstruction process in reverse. There are two predominant general methods of storing a digital image: raster, and vector.\\

A raster image is probably the most familiar to you. That is the kind of image displayed on a TV or computer monitor. The idea is to divide a 2-dimensional area into a set of rows and columns which defines a grid of small areas, called pixels. If you look close enough at the screen you can probably make out the individual pixels, each with its own color. The more pixels used, the smoother the image can appear. At some point our eyes blend the individual pixels and colors together into a single image.\\

The following are example raster images of the letter 'J', where each pixel is either black or white. Remember that this is different than sending digital text letter 'J', which only required 1 byte of information. A raster image has to specify the value of every pixel against all the values it \textit{could} be. Thus, a raster image can describe not just this shape, or font, of J, but any other shape that can fit in the same area. The three versions use more pixels (more rows and columns), increasing resolution and making a smoother looking image.\\

\begin{center}\imagegraphic[0.25]{J1.png}\imagegraphic[0.25]{J2.png}\imagegraphic[0.32]{J3.png}\end{center}

First lets look at how the pixel values might be stored. So that I can write out all of the values, I'm going to stick with a 5x5 grid, which would be 25 pixels. And to start out, I will leave all of the squares filled with white. This grid lines are just for guidance to show where the pixels are, and aren't part of the actual picture.\\

\begin{center}\imagegraphic[0.25]{5x5.png}\end{center}

I'm going to write the bit values on a single line, but group them by rows. I'm using a single bit per pixel, and the bit 1 to mean white.\\

\begin{verbatim}
11111 11111 11111 11111 11111
\end{verbatim}

Now I've filled in one of the pixels with black. This will change the value of a single bit. Which bit? Let me write out all the bits again with the change. It's in the 4th row, 2nd column, and so I go to the 4th group of bits, and go to the 2nd bit in that group. I change it from a 1 to a 0, meaning that it's black instead of white.\\

\begin{center}\imagegraphic[0.25]{5x5_1.png}\end{center}

\begin{verbatim}
11111 11111 11111 10111 11111
\end{verbatim}

Now I'm going to try to draw a face. I don't have very many pixels to work with, so you'll have to use your imagination. I'll also give the pixel's bit values.

\begin{center}\imagegraphic[0.25]{5x5_face.png}\end{center}

\begin{verbatim}
11111 10101 11111 10001 11111
\end{verbatim}

So, just to recount what happened, the first 5 bits map to the first row of the image. All first 5 bits are 1s, so all the first row is white. The next group of bits is 10101, which maps to the second row. The first bit on the second group is 1, so the first pixel on the second row is white. The next bit is 0, so the next pixel is black. Then white, then black, then white again which finished the second row. And so on for the 3rd, 4th, and 5th rows.\\

The process will work in reverse as well. If we're given the bits (along with how wide each row is), we can reconstruct the image. Now take the following bits and figure out what the image is by drawing a 5x5 grid and shading in the pixels that have a 0.

\begin{verbatim}
11111 10101 11111 01110 10001
\end{verbatim}

In these examples I only used 1 bit per pixel, which meant it could only be one of two colors (black, or white in this case). But if I use several bits per pixel, there are many more possibilities for each pixel to have different colors. If I used 3 bits for each pixel, that would give \(2^3 = 8\) possibilities. I might use a color code like the following, where each 3-bit binary number mapped to an increasing shade of gray:

\begin{center}\imagegraphic[0.5]{gray.png}\end{center}

Suppose I came up with the following 5x5 image using the 8 shades. When I write the bits out for the image, each pixel is now 3 bits, which means each row takes up 15 bits (\(3\times5\)), and 75 bits total.

\begin{center}\imagegraphic[0.25]{5x5_gradient.png}\end{center}

I'm adding spaces in the bits just to make the groupings more obvious. Just keep in mind that when they are stored or transmitted, the bits are a continuous stream.

\begin{verbatim}
111 000 000 000 000
111 000 001 010 000
111 000 000 011 000
111 110 101 100 000
000 000 000 000 000
\end{verbatim}

With more bits per pixel, comes more possibilities. Since a computer is built to work with bytes, and a byte is 8 bits, a pixel is usually no less than 8 bits. That means for a black and white image there are 256 shades to work with, which is usually enough for a human to not be able to perceive the jumps between shades.\\

\begin{center}\imagegraphic[0.5]{gray32.png}\end{center}

Of course we have gone beyond black and white. So how are full color images stored? This goes back to the basics in art class. You might remember that given a few primary colors, any other color can be created if you mix them in just the right way. And there are two types of color mixing: additive, and subtractive.\\

Computer graphics uses additive color mixing because of how the displays are constructed. You can see something on the computer monitor, or TV, because it is emitting light. Essentially each pixel is really three different pixels. If you could look closely, you would see a red pixel, and green pixel, and a blue pixel packed right next to each other. If the red pixel is made brighter, it can only add more red light. The brightest light is when all three are turned on, making white light. These are the primary colors for additive mixing, and is called the RGB color scheme (or red, green, blue).\\

\begin{center}\imagegraphic[0.75]{RGB_pixels.png}\end{center}
\begin{center}\imagegraphic[0.25]{RGB_pixels2.png}\imagegraphic[0.25]{Dell_axim_LCD_under_microscope.png}

http://commons.wikimedia.org/wiki/File:Dell\_axim\_LCD\_under\_microscope.jpg
\end{center}

Just as with a gray scale, each color is represented by an 8-bit number, and so each color, for each pixel, has 256 possibilities: from black (off), all the way up to fully on for that color.\\

\begin{center}red\\ \imagegraphic[0.5]{red32.png}\end{center}
\begin{center}green\\ \imagegraphic[0.5]{green32.png}\end{center}
\begin{center}blue\\ \imagegraphic[0.5]{blue32.png}\end{center}

There is no gray pixel. Shades of gray are made by adding equal parts of each color. However, since they have to be equal, there are still only 256 possible shades of gray, since there are only 256 shades of each color.\\

If the colors are added in non-equal parts, many other colors appear. There are a total of \(256\times256\times256 = 16,777,216 \) possible different colors. In many web-based applications, the RGB values are specified by the literal bit values. This is where hexadecimal numbers come in handy so that we don't have to write out all the bits. Since each color takes 8 bits, that is the same as 2 hexadecimal digits. There are three colors, and so we only need 6 hexadecimal digits to specify a color. The first 2 digits are for red, the next 2 digits are for green, and then blue.\\

\begin{center}\imagegraphic[0.50]{additive.png}\end{center}

For example, to say red would be \#FF0000, since we want red but not green or blue. Remember that in hex, FF is the same as 11111111 in binary. The \# sign comes from historical reasons to mean "rgb color specified by hexadecimal number".\\

\#00FF00 would be green, and \#0000FF would be blue. To make yellow you combine red and green (but not blue), and so yellow would be \#FFFF00. Since magenta is made by combining blue and red, it would be \#FF00FF. Which color would be \#00FFFF? Well, the first 2 digits are zero so you know there's no red. The rest of the digits are F, which means it has full green and blue. Blue and green make the color cyan.\\

There is often a 4th "color" (or channel) added to the mix, called alpha. What this cryptic name basically represents is how opaque the color is. If something is not completely opaque, then you can see stuff behind it like a pane of colored glass. If it is omitted then it is assumed to be completely opaque. If it is included then it appears as a 4th set of hex numbers, where 00 would mean completely transparent, and FF means completely opaque. For example, \#FF0000FF would be red, the same as \#FF0000. But \#FF000077 would mean red that is half-way transparent (since 7 is about halfway to 15, or F in hex).\\

All four channels together would take up \(8\times 4 = 32 \) bits, and so this is called a 32 bit color. If the alpha is ignored, that's 24 bits per pixel. Most graphics you see are using either 24 bit or 32 bit colors.  So, if you have an image that is 1280 by 720, a total of \(1280\times720\times24 = 22,118,400\) bits, or 2,764,800 bytes, are required to store the entire image.\\

\section{Vector Graphics}

Another method of storing an image is called vector graphics. The idea of vector graphics is not to divide the image area in to pixels, storing the color of each pixel, but to divide it in to the primitive shapes that exist in the image such as lines, curves, circles, and rectangles. A vector graphics 'image' is stored independent of the resolution that it will be viewed.\\

When a vector graphic is displayed on a monitor that is made of pixels, the image is rasterized. It is converted to a raster image made up of pixels. The color of each resulting pixel is determined by what is "under" that pixel, and the color of the thing that is under it.\\

One useful application of vector graphics is in Fonts and type-setting. Instead of storing a raster image of each letter, it's better to simply store the outline of the letter as a series of curves. Then, when the letter is displayed or printed it is rasterized based on the resolution and size that is desired. Letters and words can be made larger or smaller without worrying about 'jaggies' appearing due to the limited resolution of an original image of the letter.\\

The most prominent use of vector graphics you are probably familiar with is computer games. A game is not made of a series of static raster images like a movie. What you see is determined in real-time by what you are doing. The information about position, orientation, and general state about a scene is stored as vector data. Then it is rasterized by either your cpu or the graphics card on your computer to produce the image on your screen. This works for both 2D and 3D graphics.\\

\section{Audio}

\epigraph{Art is how we decorate space; music is how we decorate time.}{unknown}

Audio was first digitized in the development of the telephone network. Before they worked digitally, telephones operated on an analog system. The sound of the voice of a person was translated into a voltage by a microphone, and those voltages were carried directly by wires over large distances to a another telephone. The problem was that over large distances the signals became distorted. This is different than just becoming weaker, since amplifiers could be used to boost the amplitude, or loudness, of the signal.\\

Distortion is where the shape of the wave itself changes, and so the sound coming out of one telephone would be different than what went into the other one, even though it had been amplified to be just as loud. Distortion is caused by several factors, which are not important at this point. What is important is how this problem was solved.\\

Audio signals are digitized in a similar fashion as raster images. A sound wave is divided up in time, rather than area. The pressure, or loudness, of the wave is recorded at intervals of time just like the brightness of an image is recorded. A number, or code, is assigned to that chunk of time to say how loud it was at that moment. This method is called pulse-code modulation (PCM).

If the chunks of time are small enough, and number of possible 'loudness' levels is high enough, then the sound can be reproduced by causing a speaker to make sound with the correct loudness at exactly the right times.\\

\begin{center}\imagegraphic[0.75]{sound_1.png}\end{center}
\begin{center}\imagegraphic[0.75]{sound_2.png}\end{center}

So, how did this revolutionize telephones? Since the sound is now a series of numbers, and numbers can be represented by bits, the original audio is converted into a series of 1s and 0s. Each sample is converted into many pulses of 1s and 0s to represent the number that corresponds to how loud the sound is at that moment, instead of sending the sound directly. Suppose each sample is recorded in 8 bits, then every 8 bits gets converted back into a single pulse.\\

Those 1s and 0s are transmitted over the telephone line as voltages just as the analog audio used to be. However, since the machinery knows exactly what a 1 looks like, and what a 0 looks like, as they come over the line it can reconstruct the stream of bits even if the signal is slightly distorted. It couldn't do that with the normal audio because it doesn't know what the original was supposed to sound like. But since it knows what a 1 'sounds' like, and what a 0 'sounds' like, it can make a really good guess as to what they're supposed to be and make them perfect again. Keep in mind that this waveform is not the original signal, but representing the 1s and 0s of the codes.\\

\begin{center}\imagegraphic[0.75]{regenerative.png}\end{center}

Every so often down the telephone line a piece of equipement would take the signal and regenerate it back into the correct form. These are called regenerative repeaters, and their only function is to keep the digital signal together as it's being transmitted even when there is distortion or noise.\\

Now that the bits are ensured to arrive at the destination with the correct values, the original audio is reconstructed performing the PCM in reverse.

\begin{center}\imagegraphic[0.75]{sound_1.png}\end{center}

The two values that describe the quality of digital audio is bit depth and sample rate. Bit depth is how many bits are used per sample, and determine how many 'loudness' levels are possible to represent per sample (like how many color levels in an image). The more bits, the higher the quality, and is either 16 or 24 bits for high quality audio.\\

The sample rate is exactly what it sounds like; the number of samples per unit of time. This is analogous to the resolution of an image. Basically, how many divisions 1 second of sound gets divided into. The sample rate determines the maximum frequency that can be captured. Basically, the sample rate has to be twice as big as the biggest frequency you want to record. The human hearing range is from 20Hz up to 20,000Hz. To capture any sounds we can hear, then the sample rate needs to be above 40kHz (40,000 samples, or divisions, per second). High quality digital audio sample rate is usually set at 44.1kHz. The extra 4.1kHz are used to enable filtering, to smooth out the sound jaggies, so that the resulting maximum frequency is still 20kHz.\\

However, the telephone system used a sample rate of only around 8kHz, which meant it could only pick up frequncies up to around 4kHz. This is why sounds through the telephone don't sound quite right; the higher frequencies are all cut out.

\section{MIDI}

The audio equivalent of vector graphics is midi data, Musical Instrument Digital Interface. The idea is not to store the literal sound waves, but instead store when certain notes are supposed to be played and a reference to how the sounds should be generated (like a musical instrument).\\

MIDI files are written in a similar way that sheet music is written. Each note has to specify when it should be played, the tone at which it's played, and for how long.

\section{General Compression}

Most images that you encounter are already compressed in some way. Like with text, images tend to be redundant in their information, and so they don't take as many bits to store as it would seem. In a strange inversion of intuition, a purely random image actually contains more information than a recognizable image.

\begin{center}\imagegraphic[0.750]{noise.png}\end{center}

The above image is an example of "noise". Every pixel in the image was set to a random RGB value. The image is \(400 \times 400 = 16,000\) pixels, but there are 16,777,216 possible 24bit colors, and so the probability of a color being repeated even once is very small. There is no repetition or pattern that can be used to compress the image, even in principle, and is so called incompressible.\\

The reason a random image contains more information is because there are many more possible "noise" images than "recognizable" images. And so if we want to store one particular "noise" image, we have to specify what makes it different than all the other "noise" images.\\

The reason we, as humans, don't think a noise image contains any information is because all noise images look the same to us: we can't really tell the difference. I'm sure you would be hard pressed to tell the difference between the following image and the previous one with just a glance, even though every single pixel is different between the two.\\

\begin{center}\imagegraphic[0.75]{noise2.png}\end{center}

Ok, so now you believe me that we need a pattern to be able to compress something. There are many different compression algorithms. Some work the same way as described for text compression, except it just looks at sequences of bytes without interpreting them as letters. Compression can be used on any type of digital media because they are all just made up of a series of bits. Any types of patterns in the bits can be used to compress the data, it is just a matter of figuring out how (the algorithm).\\

However, some image compression algorithms use what is called lossy compression. That is, the original image cannot be reconstructed exactly. This \textit{can} be acceptable because we humans usually can't tell the difference between two images that have only subtle differences. So a compression algorithm can get away with throwing away some information if it 'looks' the same to a human, even if it's not exactly the same image anymore.\\

As an example, the first image below is compressed with a lossless algorithm, the second one with a lossy algorithm. The second image has lost some details, but has a file size 20x smaller than the first image. If you look closely you may notice differences in colors and shapes of some features. But if the two images were not next to each other you might not notice it.

\begin{center}\imagegraphic[0.75]{picture_bw.png}\end{center}
\begin{center}\imagegraphic[0.75]{picture_bw.jpg}\end{center}

\section{Chapter Puzzles}


\documentclass{article}

% these packages let you do math
\usepackage{amsmath}
\usepackage{amssymb}

% we need these packages for fancy R tables
\usepackage{booktabs}
\usepackage{float}
\usepackage{colortbl}
\usepackage{xcolor}

% these packages play with the spacing/margins of the document. Uncomment the commands on lines 16 and 17 to see what they do.
\usepackage{a4wide}
\usepackage{setspace}
\usepackage{geometry}
\usepackage{parskip}
%\doublespacing
%\geometry{margin=1.5in}

% this package helps us with including images. Setting the graphics path makes it easier to refer to things in the \includegraphics command.
\usepackage{graphicx}
\graphicspath{ {../figures/} }

% make some hyperlinks using the \href command
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=black,
    urlcolor=blue
}

% set the author, title, and date of the document. \maketitle adds it to the document.
\author{Patrick Massey}
\title{Hidden Curriculum Assignment}
\date{02/18/2022}

\begin{document}
\maketitle

\section*{Data}
I use a subset of data from the 1997 National Longitudinal Survey of Youth, which contains longitudinal data on individuals from 1997 to 2019. In particular I use observations from 2002 that focus on the incarceration status of an individual for that year. When analyzing the data I remove any individuals for which there is no incarceration information available, or if they began the year already incarcerated. After cleaning the data I am left with 8,621 observations. I then create an dummy indicator value for if the individual is incarcerated at all during the year. This indicator variable \textit{incarcerated} will be the variable of interest for the model.
\newpage
\section*{Empirical Analysis}
In this analysis I seek to estimate the probability of an individual becoming incarcerated based on race and sex. Looking at Figure \ref{fig:graph} below, we see that Black Males have the highest incarceration rate of almost 6\%. Males in general have a higher incarceration rate than Females. One anomaly is for the Mixed Race (Non-Hispanic) group. This should be seen as an small sample size issue because out of the 8,621 observations Mixed Race (Non-Hispanic) make up 81 observations.
\begin{figure}[H]
    \begin{center}
        \includegraphics[width=.85\textwidth]{incarceration_rate_by_racegender}
    \end{center}
    \caption{Incarceration Rate in 2002 by Race and Gender}
    \label{fig:graph}
\end{figure}
I summarize the graph in Table \ref{tab:tab:summarystats} below which shows the incarceration rate broken down by sex and race.
\input{../tables/incarceration_rates_by_racegender.tex}
\newpage
The model we are seeking to estimate is shown below:
\begin{equation*}
Y_i = \beta X_i
\end{equation*}
Where $Y_i$ is a binary value for an individuals incarceration status and $X_i$ is a vector containing sex and race characteristics. I summarize the regression results in Table \ref{tab:regression} below.

\input{../tables/regress_incarceration_by_racegender.tex}

All variables are statistically significant at the 99\% level except for Mixed Race (Non-Hispanic) category which I believe goes back to the small $n$ issue.

Analyzing the regression results we can see that there appears to be racial discrimination towards Black individuals as every other racial group shows a constant that is less than or equal to 0. This indicates that simply being Black shows higher risk of incarceration. We also see that Black Males show higher risk for incarceration than Black Females which matches the trends in the data.
\end{document}


\section{Unit Test Description}
A series of unit tests are performed to check the validity of this module's operation.

\subsection{Test 1}
The first test uses an input vector of \input{AutoTex/usFalseFalseFalse.tex} Nm.  The RW spin inertia is set to $J_{s}$ = 0.1 kg m$^{s}$, while the maximum RW motor torque is set to $u_{\text{max}}$ = 0.2 Nm.  In this test case no RW availability or wheel speed messages are set.  The simulation is first run for 1.5 seconds with a 0.5 second control update period.  Next, the module is reset and run for another 1.5 sections.  With only the open-loop voltage conversion active and the RW motor torque
The resulting actual values and differences with hand-computed values are shown in Table~\ref{tbl:testFalseFalseFalse}.  The reset of the module should have not impact on the voltage conversion, which is the case.

\subsection{Test 2}
This test repeats the values of test 1, except that the RW motor torque input vector is set to \input{AutoTex/usTrueFalseFalse.tex} Nm.  This should saturate the first and last RW voltage output, which is seen in Table~\ref{tbl:testTrueFalseFalse}.

\subsection{Test 3}
This test repeats the values of test 1, except that a RW availability message is created.  Here all RWs have a status of {\tt AVAILABLE} except for the 3rd RW which is {\tt UNAVAILALBE}.    The 3rd RW voltages should thus all be 0.0 in this case, which is seen in Table~\ref{tbl:testFalseTrueFalse}.

\subsection{Test 4}

This test repeats the values of test 1, except that a RW wheel speed message is created.  The feedback gain is set to $K = 1.5$.  For the first 1.0 seconds the RW wheel speeds are set to \input{AutoTeX/Omega1.tex} rad/sec.  At 1.0 seconds the RW wheel speeds are set to  \input{AutoTeX/Omega2.tex} rad/sec.  Then the module is reset at 1.5 seconds and the simulation continued for another 1.5 second for a 3 second total simulation time.   The speed message remains the same after the reset.   Table~\ref{tbl:testFalseFalseTrue} shows the results of the actual values, and the differences with the hand-computed values.

\section{Test Parameters}
Test parameters are interspersed with the test description above. For all tests, $V_{\mathrm{min}} = 1$ and $V_{\mathrm{max}} = 11$ which correspond to a maximum torque of 0.2 [Nm].

\section{Test Results}
Results for each test are shown in the tables below:
\input{AutoTeX/testFalseFalseFalse.tex}
\input{AutoTeX/testTrueFalseFalse.tex}
\input{AutoTeX/testFalseTrueFalse.tex}
\input{AutoTeX/testFalseFalseTrue.tex}

All of the tests passed:
\begin{table}[H]
	\caption{Test results}
	\label{tab:results}
	\centering \fontsize{10}{10}\selectfont
	\begin{tabular}{c | c  } % Column formatting,
		\hline
		\textbf{Test} 						  		&\textbf{Pass/Fail} \\ \hline
	   1	   			& \input{AutoTex/passFailOne} \\ \hline
		2	   			& \input{AutoTex/passFailTwo} \\ \hline
		3	   			& \input{AutoTex/passFailThree} \\ \hline
		4	   			& \input{AutoTex/passFailFour} \\ \hline
	\end{tabular}
\end{table}

\pagebreak %needed to keep images/paragraphs in the right place. Cannot \usepackage{float} here because it is not used in the AutoTex implementation.