% Macros were moved to macros.tex because I need them in an earlier section.

\section{The Input Language}
\label{sec:input-language}

\iflong
\begin{figure}
	\[
	\begin{array}{rl@{\quad}l}
		\noalign{\textbf{Propositions}}
		\ip,\ipp ::=
		    & \iP \ibar \iselect{\im}{\iP}&\mbox{Predicate names}\\
		  | & \itrue \ | \ \ifalse \ibar \inot{\ip_1} \ibar {\ip_1}{\land}{\ip_2} \ibar
		       {\ip_1}{\lor}{\ip_2}\ | \ {\ip_1}{\Rightarrow}{\ip_2} & \mbox{Predicate logic}\\
                  | & \ipcases{\ie}{\il_1}{\ix_1}{\ip_1}{\il_n}{\ix_n}{\ip_n} & \mbox{Propositional case}\\
		  | & \ilambda{\ix}{\is}{\ip}  \ibar \iapp{\ip}{\ie} & \mbox{Predicates and application}\\
		  | & \iequal{\ie_1}{\ie_2} & \mbox{Term equality}\\
		  | & \iforall{\ix}{\is}{\ip}  \ibar
		      \iexists{\ix}{\is}{\ip} \ |\
		      \iunique{\ix}{\is}{\ip} & \mbox{Term quantifiers}\\[5pt]

		\noalign{\textbf{Sets}}
		\is ::=
		    & \iS  \ibar \iselect{\im}{\iS} &\mbox{Set names}\\
		  | & \isunit \ibar \iprod{\ix}{\is_1}{\is_2}
                  &\mbox{Unit and (dependent) cartesian product}\\
		  | & \isvoid \ibar \isum{\il_1}{\is_1}{\il_2}{\is_2} &\mbox{Void and disjoint union}\\
		  | & \idarrow{\ix}{\is_1}{\is_2} & \mbox{(Dependent) function space} \\
		  | & \ilambda{\ix}{\is_1}{\is_2} \ibar
		      \iapp{\is}{\ie} &\mbox{Dependent set and application}\\
		  | & \iquot{\is}{\ipp} & \mbox{Set quotient by an equivalence relation}\\
		  | & \isubset{\ix}{\is}{\ipp} & \mbox{Subset satisfying a predicate}\\
		  | & \irz{\is}&\mbox{Realizers of a set}\\[5pt]

		\noalign{\textbf{Terms}}
		\ie ::=
		    & \ix \ibar \iselect{\im}{\ix} &\mbox{Term names}\\
		  | & \ilambda{\ix}{\is_1}{\ie} \ibar
		      \iapp{\ie_1}{\ie_2} &\mbox{Function and application}\\
		  | & \ituple{\ie_1}{\ie_2}
		      \ibar \iproj{\ie}{n}&\mbox{Tuple and projection}\\
		  | & \iinj{\il}{\ie}
		      \ibar (\imatch{\ie_0}{\il_1}{\ix_1}{\ie_1}{\il_2}{\ix_2}{\ie_2})&\mbox{Injection and projection from a union}\\
		  | & \ieclass{\ie}{\ipp}
		      \ibar \ileteclass{\ix}{\ipp}{\ie_1}{\ie_2}&\mbox{Equivalence class and picking a representative}\\
		  | & \irz{\ie}
		      \ibar \iletrz{\ix}{\ie_1}{\ie_2}&\mbox{Realized value and picking a realizer}\\
		  | & \icoerce{\ie}{\is} &\mbox{Type coercion (e.g., in and out of a subset)}\\
		  | & \ithe{\ix}{\is}{\ip}&\mbox{Definite description}\\
		  | & \ilet{\ix}{\ie_1}{\ie_2}&\mbox{Local definition}\\[5pt]

		\noalign{\textbf{Models}}
		\im ::=
		    & \iM  \ibar \iselect{\im}{\iM}&\mbox{Model names}\\
		  | & \iapp{\im_1}{\im_2}&\mbox{Application of parameterized model}\\[5pt]

		\noalign{\textbf{Proposition Kinds}}
		\ipt ::=
		    & \iProp \ibar \iStable & \mbox{Classifiers for all propositions/stable propositions}\\
		  | & \iEquiv{\is} &\mbox{Classifier for stable equivalences on $\is$}\\
		  | & \idarrow{\ix}{\is}{\ipt} & \mbox{Classifier for a predicate/relation}\\[5pt]

		\noalign{\textbf{Set Kinds}}
		\ik ::=
		    & \iSet &\mbox{Classifier for a proper set}\\
		   | & \idarrow{\ix}{\is}{\ik} &\mbox{Classifier for a dependent set}\\[5pt]

		\noalign{\textbf{Theory Elements}}
		\ite ::=
		     & \iDefinition{\ix}{\ie}.\ibar\iDefinition{\iS}{\is}.&\mbox{Give a name to a term or set}\\
		   | & \iDefinition{\iP}{\ip}.\ibar\iDefinition{\iTH}{\ith}.&\mbox{Give a name to a predicate or theory}\\
		   | & \iParameter{\ix}{\is}.\ibar\iParameter{\iS}{\ik}.&\mbox{Require an element in the given set or kind}\\
		   | & \iParameter{\iP}{\ipt}.\ibar\iParameter{\iM}{\ith}.&\mbox{Require a predicate or model of the given sort}\\
		   | & \iAxiom{\iP}{\ip}.&\mbox{Axiom that must hold}\\[5pt]

  		\noalign{\textbf{Theories}}
		\ith ::=
		     & \iTH&\mbox{Theory name}\\
%		   | & \iselect{\im}{\iTH}\\
		   	| & \ithy{\ite_1,\ldots,\ite_n} &\mbox{Theory of a model}\\
		 	| & \idarrow{\iM}{\ith_1}{\ith_2} &\mbox{Theory of a uniform family of models}\\
		  	| & \ilambda{\iM}{\ith_1}{\ith_2} \ibar
		      \iapp{\ith}{\iM}&\mbox{Parameterized theory and application}\\
	\end{array}
	\]
	\label{fig:input}
	\caption{Input Syntax (Simplified)}
\end{figure}
\fi % iflong

The input to RZ consists of one or more theories.
A RZ \emph{theory} is a generalized logical signature with associated
axioms, similar to a Coq module signature. Theories describe
\emph{models}, or implementations.
\iflong
A summary of the input language appears in Figure~\ref{fig:input}.
\fi % iflong

The simplest theory $\ith$ is a list of \emph{theory element}\/s
$\ithy{\ite_1 \ldots \ite_n}$. A theory element may specify that a certain
set, set element, proposition or predicate, or model must exist (using
the
\texttt{Parameter} keyword). It may also provide a definition
of a set, term, proposition, predicate, or theory (using the
\texttt{Definition} keyword). Finally, a theory element can be
a named axiom (using the \texttt{Axiom} keyword).

We allow model parameters in theories;
typical examples in mathematics include
the theory of a vector space parameterized by a field of scalars%
\iflong
, or the theory of the real numbers parameterized by a model of the
natural numbers.
\else % \iflong
.
\fi % \iflong

\iflong
Following Sannella, Sokolowski, and
Tarlecki\footnote{``parameterized (program specification) $\neq$ (parameterized program) specification''}~\cite{sannella92:_towar}
RZ supports two forms of parameterization.
\fi % \iflong
A theory of a parameterized implementation
$\idarrow{\iM}{\ith_1}{\ith_2}$ describes a uniform family of models (i.e.,
a single implementation; a functor in OCaml) that maps every
model~$\iM$ satisfying~$\ith_1$ to a model of~$\ith_2$.  In contrast,
a theory $\ilambda{\iM}{\ith_1}{\ith_2}$
maps models to theories; if $\iTH$ is such a theory, then
$\iTH(\im_1)$ and $\iTH(\im_2)$ are theories whose implementations might be completely unrelated.\iflong\footnote{
Further, in some cases $\iTH(\im_1)$ might be implementable while $\iTH(\im_2)$ is not.
}
\fi % \iflong

Propositions and predicates appearing in theories may use full
first-order constructive logic, not just the negative fragment.
\iflong
The grammar for logical inputs is shown in Figure~\ref{fig:input}. Most of
this should be familiar, including the use of lambda abstraction to
define predicates.
\fi

The language of sets is rich, going well beyond the type systems of
typical programming languages. In addition to any base sets postulated
in a theory, one can construct dependent cartesian products and
dependent function spaces. We also supports disjoint unions (with
labeled tags), quotient spaces (a set modulo a stable equivalence
relation), subsets (elements of a set satisfying a predicate). RZ even
permits explicit references to sets of realizers.

The term language includes introduction and elimination constructs
for the set level. For product sets we have
tuples and projections ($\iproj{\ie}{1}$, $\iproj{\ie}{2}$, \ldots),
and for function spaces we have lambda abstractions and application.
One can inject a term into a tagged union, or do case analyses
on the members of a union. We can produce an equivalence class or pick a representative from a equivalence class (as
long as what we do with it does not depend on the choice of
representative). We can produce a set of realizers
or choose a representative from a given set of realizers
(as long as what we do with it does not depend on the choice of
representative). We can inject a term into a subset (if it satisfies
the appropriate predicate), or project an element out of a subset.
Finally, the term language also allows local
definitions of term variables, and definite descriptions (as long as
there is a unique element satisfying the predicate in question).

From the previous paragraph, it is
clear that checking the well-formedness of terms is not decidable. RZ
checks what it can, but does not attempt serious theorem proving.
Uncheckable constraints remain as obligations
in the final output, and should be verified by other means before
the output can be used.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "cie"
%%% End:


\section{Computer Algorithms}
Two computer science concepts are core to the clustering and classification techniques investigated in this thesis.
One is a clustering algorithm that builds clusters by categorizing the datapoints into three different types, clustering some and denoting the unclustered as noise.
Another is a classification technique that searches for nearby datapoints in order to classify an unknown datapoint.

%%%%% DBSCAN %%%%%
\subsection{Density-Based Clustering of \Isols{}}\label{sec:background:dbscan}
Density-based clustering algorithms build clusters based on two parameters: the minimum number of neighbors \minneigh{},  a point must have to be a core point of a cluster, and \eps{}, the radius that those neighbors must be within.
These algorithms define clusters with respect to core points and border points --- points within \eps{} of a core point --- labeling everything else --- the singletons --- as noise.
For this work, we chose to use \dbscan{} as the clustering technique for grouping \isols{} because dense groupings of similar \isols{} fits our intuition of bacterial \isol{} strains.
Closely related ``families'' of \isols{} will appear in the same cluster and we want these clusters to have sufficient purity to aid us in \mst{}.

\dbscan{}\cite{ester1996density} provides the framework for our clustering algorithm.
It uses a \distmetric{}, a minimum neighbors value \minneigh{}, and an \eps{} range to categorize data points as one of three types:
\index{\dbscan{}}
\index{\distmetric{}}
\index{\minneigh{}}
\index{\eps{}}
a core point, a border point, or noise.
A \textit{core point} is a point that has at least \minneigh{} data points within \eps{} of it.
\index{core point}
A \textit{border point} is a point that is within \eps{} of a core point, but that does not have \minneigh{} points within \eps{} of it.
\index{border point}
Every other point is \textit{noise}.
A \textit{cluster} is a group of neighboring core points with their associated border points.
\index{cluster}
According to this definition of a cluster, all clusters must have at least \minneigh{} points in them.
\autoref{fig:density-based-clustering} depicts this process.
\input{figures/density}

Density-based clustering techniques require a distance metric, often times the \euclid{}, between data points in order to cluster.
Performing fast range queries greatly improves the speed of clustering.
If the range query can finish in \bigo{\log{n}} time, then \dbscan{} can run in \bigo{n\log{n}} time.
Organizing the data into a spatial index can optimize these spatial queries.

Spatial indexes structure the data into a search tree, similar to a binary search tree, organizing the points by distance.
When querying for nearby points, the algorithm can traverse this search tree, ignoring certain points along the way.
While this can speed up the range query to a \bigo{\log{n}}, many spatial indexes degenerate into a \bigo{n} operation. In the former case, this makes \dbscan{} run in \bigo{n\log{n}} time.

In \dbscan{}, the \codefn{RangeQuery} function handles range quer\-ies by taking as parameters the data point and a distance and returning all data points within range of the query point.
Mathematically, a \textit{range query} is a function $\mathit{RangeQuery}: D\times \R \rightarrow \{D\}$ that takes a query point $q\in D$ and a real-valued $\eps{}\in\R$ and returns $\{d\in D | \mathit{Dist}(q, d)\} < \eps{}\}$ --- the set of all other points within \eps{} of the query point --- where $\mathit{Dist}$ is some \distmetric{} $\mathit{Dist}: D\times D \rightarrow \R $.
\index{range query}
If the \distmetric{} forms a proper metric space, then data structures like quad trees and octrees can speedup \eps{} range queries for a point.
One can imagine the range query as a hypersphere centered at the query point with a radius of the query range.
In order to make \codefn{RangeQuery} fast, we had to make some optimizations.

%%%%% kNN %%%%%
\subsection{\kNNlong{}}\label{sec:background:knn}
The \kNNlong{} classification algorithm (\kNN{}) is a straightforward algorithm to classify an unclassified object using a library.
Using a \compfunc{}, it compares the unclassified object to ``nearby'' classified objects.
It uses the concept of a \compfunc{} to formulate an idea of ``closeness,'' asserting that the similarity an unknown object has to a class of objects relates to the class of the unknown objects itself.
\autoref{fig:knn}

To outline the process:
Given an unclassified object \UNKNOWN{}, a library of classified objects \LIB{}, and a \compfunc{}, \COMP{}:
\begin{enumerate}
\item Compare \UNKNOWN{} to each object in \LIB{} using \COMP{}
\item Add the classified object and the result to a list of neighbors, $N$
\item Sort $N$ by most similar
\item Consider only the top $k$ entries in $N$, called the \knnlong{} \label{knn:filter}
\item Classify \UNKNOWN{} as the \textit{most plural} classification in the \knnlong{} list
\end{enumerate}
\autoref{alg:knn} describes this process in pseudocode.
\input{algorithms/knn}

\input{figures/knn}

The motivation is that the unclassified object must be ``close'' to some of the classified objects in our database, using an appropriate measure of closeness --- the \textit{\compfunc{}} --- for the data.
\index{\compfunc{}}
By choosing the \textit{most plural} or \textit{dominant} classification --- the classification that shows up the highest number of times ---  in the \knnlong{} we can, with some accuracy, classify our unknown object.
\index{most plural classification}
\index{dominant classification}

\autoref{fig:knn} depicts an example graph of datapoints in a coordinate space.
All but one of the has a class associated with it, any of \achar{}, \bchar{}, or \cchar{}.
The class of one point, denoted by \unknownchar{}, requires determination.
\Compfuncs{} like \euclid{} or \manhattan{} may be the most appropriate way to compare these datapoints.

\autoref{fig:knn} shows how using \kNN{} with \euclid{} and various \k{} can classify this point.
Figures~\ref{fig:knn:4}, \ref{fig:knn:6}, and \ref{fig:knn:9} show the \knnlong{} lists as \k{} changes from 4, to 6, to 9.
At \k{}=4, we see that \kNN{} classifies the unknown as \achar{} because there are 2 \achar{}s, but only one each of \bchar{} and \cchar{}.
For \k{}=6, \bchar{} is the resulting classification, since \bchar{} shows up 3 times --- more than the 2 \achar{} and 1 \cchar{}.
Finally, as we extend \k{} all the way out to 9, \kNN{} classifies it as \cchar{}, since such an extension exposes the \knnlong{} list to all 4 \cchar{}'s, more than any other classification.


\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

% partial derivative as a fraction
\newcommand{\fracpd}[2]{
  \ensuremath{\frac{\partial #1}{\partial #2}}
}

\begin{document}

% --------------------------------------- %
% Section
% --------------------------------------- %
\section{Envelope Method}
  Original problem is

  \begin{align*}
    J_t(k_t, x_t) &= \max_{\chi_{t}} \left[(1 - \beta) c_t^{\rho} + \beta \mu(J_{t+1}(k_{t+1}, x_{t+1}) g_{t+1})^{\rho} \right]^{\frac1\rho} \\
    \text{Subject To }& \\
    c_t &= \left[ \eta k_{t}^{\nu} + (1 - \eta) \right]^{\frac1\nu} + (1 - \delta) k_t - \chi_t \\
    \chi_t &= k_{t+1} g_{t+1} \\
    x_{t+1} &= A x_{t} + B \bar{b}^{\frac{1}{2}} \varepsilon_{1, t+1} \\
    \log(g_t) &= \log(z_t) - \log(z_{t-1}) = \log(\bar{g}) + x_t
  \end{align*}

  We can take first order conditions to get

  \begin{align*}
    (1 - \beta) c_t^{\rho - 1} &= \beta \mu(J_{t+1} g_{t+1})^{\rho - \alpha} E_t \left[ (g_{t+1} J_{t+1})^{\alpha - 1} \fracpd{J_{t+1}}{k_{t+1}} \right]
  \end{align*}

  The envelope condition reveals

  \begin{align*}
    \fracpd{J_t(k_t, x_t)}{k_t} &= J_t^{1 - \rho} (1 - \beta) c_t^{\rho - 1} (\eta y_t^{1 - \nu} k_t^{\nu - 1} + 1 - \delta)
  \end{align*}

  Notice for any pair $(k_t, x_t)$ we can solve the envelope condition to get

  \begin{align*}
    c_t &= \left( \frac{J_{k, t}(k_t, x_t)}{(1 - \beta) J_t^{1 - \rho} (\eta y_t^{1 - \nu} k_t^{\nu - 1} + 1 - \delta)} \right)^{\frac{1}{\rho - 1}} \\
    \rightarrow \chi_t &= y_t + (1 - \delta) k_t - c_t \\
    \chi_t &= y_t + (1 - \delta) k_t - \left( \frac{J_{k, t}(k_t, x_t)}{(1 - \beta) J_t^{1 - \rho} (\eta y_t^{1 - \nu} k_t^{\nu - 1} + 1 - \delta)} \right)^{\frac{1}{\rho - 1}} \\
  \end{align*}

\end{document}


\subsection{Test 2}

\begin{enumerate}[label=\arabic*.]
	\item
		Find the general form of a particular solution to the differential equation
		\begin{equation*}
			y'' + 10y' + 25y = f(t)
		\end{equation*}
		for each of the following cases:
		\begin{enumerate}[label=(\alph*)]
			\item
				$f(t) = \cos{(-5t)} + 7t\sin{(-5t)}$
			\item
				$f(t) = te^{-5t}$.
		\end{enumerate}
	\item
		Consider the matrix $A = \begin{bmatrix}
			1  & 0 & 6 \\
			3  & 1 & 3 \\
			-3 & 3 & -8
		\end{bmatrix}$.
	Compute its characteristic polynomial and find all its eigenvalues.
	Pick one of the eigenvalues and compute a corresponding eigenvector.
	\item
		Choose one of the two parts.
		\begin{enumerate}[label=(\alph*)]
			\item
				Compute the general solution for the equation $y'' - 5y' + 6y = 6e^{4t} - 10e^{t}$.
			\item Use the method of variation of parameters to find a particular solution to $y'' + 9y = \csc{(3t)}$.
		\end{enumerate}
	\item
		A 1kg mass is attached to a spring with stiffness 8N/m. The damping constant is 6Ns/m.
		At time $t=0$, an external force $F(t) = 8\sin{(2t)}$ is applied to the system as the mass is pushed rightward from equilibrium with a velocity of 1m/s.
		\begin{enumerate}[label=(\alph*)]
			\item
				Find the displacement function of the mass.
			\item
				Determine the steady-state solution for the system.
			\item
				Write the steady-state solution in the form $A\cos{(\omega t - \phi)}$ indicating the amplitude, frequency, period, and phase-shift.
		\end{enumerate}
	\item
		Choose one of the following two parts.
		\begin{enumerate}[label=(\alph*)]
			\item
				Consider the following linear system of differential equations with given initial conditions:
				\begin{equation*}
					\begin{cases}
						x_1' = 2x_1 - 3x_2 \\
						x_2' = x_1 - 2x^2
					\end{cases} \text{, } x_1(0)=2 \text{, } x_2(0)=3
				\end{equation*}
				\begin{enumerate}[label=(\roman*)]
					\item
						Write the system in normal form.
					\item
						Show that the vectors $\begin{bmatrix}
							3e^t \\
							e^t
						\end{bmatrix}$ and $\begin{bmatrix}
							e^{-t} \\
							e^{-t}
						\end{bmatrix}$ are solutions to the homogeneous system.
					\item
						Write the general solution for the system and then compute the unique solution satisfying the initial conditions.
				\end{enumerate}
			\item
				The matrix $A = \begin{bmatrix}
					-1 & 2 \\
					-1 & 3
				\end{bmatrix}$ has eigenvalues $\lambda = -2 \pm i$.
				Find the general solution to the system $\vec{x}' = A\vec{x}$.
		\end{enumerate}
	\item
		Consider the system
		$\vec{x}' = \begin{bmatrix}
			2 & -1 \\
			3 & -2
		\end{bmatrix}\vec{x} + \begin{bmatrix}
			2t \\
			3t + 3
		\end{bmatrix}$.
		It is known that the corresponding homogeneous sysstem has general solution
		$\vec{x_h} = C_1e^t\begin{bmatrix}
			1 \\
			1
		\end{bmatrix} + C_2e^{-t}\begin{bmatrix}
			1 \\
			3
		\end{bmatrix}$.
		Find the general solution for the given heterogeneous system.
\end{enumerate}


\subsection{Adjustment}

\index{C}{adjustment!computation}

There are two basic approaches to adjusting for covariates.
Conceptually, the simplest one is to hold the covariates constant at
some level when collecting data or by extracting a subset of data
which holds those covariates constant.  The other approach is to
include the covariates in your models.

For example, suppose you want
to study the differences in the wages of male and females.  The very
simple model \model{\VN{wage}}{\VN{sex}} might give some insight, but
it attributes to \VN{sex} effects that might actually be due to level
of education, age, or the sector of the economy in which the person
works.  Here's the result from the simple model:\datasetCPS
\begin{Schunk}
\begin{Sinput}
> cps = fetchData("cps.csv")
> mod0 = lm( wage ~ sex, data=cps)
> summary(mod0)
\end{Sinput}
\begin{Soutput}
...
            Estimate Std. Error t value Pr(>|t|)
(Intercept)    7.879      0.322   24.50  < 2e-16 ***
sexM           2.116      0.437    4.84  1.7e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 5.03 on 532 degrees of freedom
Multiple R-squared: 0.0422,	Adjusted R-squared: 0.0404
F-statistic: 23.4 on 1 and 532 DF,  p-value: 1.7e-06
\end{Soutput}
\end{Schunk}
The coefficients indicate that a typical male makes \$2.12 more per
hour than a typical female.  (Notice that $R^2 = 0.0422$ is very small:
\VN{sex} explains hardly any of the person-to-person variability in wage.)

By including the variables \VN{age}, \VN{educ}, and \VN{sector} in the
model, you can adjust for these variables:
\begin{Schunk}
\begin{Sinput}
> mod1 = lm( wage ~ age + sex + educ + sector, data=cps)
> summary(mod1)
\end{Sinput}
\begin{Soutput}
...
              Estimate Std. Error t value Pr(>|t|)
(Intercept)    -4.6941     1.5378   -3.05  0.00238 **
age             0.1022     0.0166    6.17  1.4e-09 ***
sexM            1.9417     0.4228    4.59  5.5e-06 ***
educ            0.6156     0.0944    6.52  1.6e-10 ***
sectorconst     1.4355     1.1312    1.27  0.20500
sectormanag     3.2711     0.7668    4.27  2.4e-05 ***
sectormanuf     0.8063     0.7311    1.10  0.27064
sectorother     0.7584     0.7592    1.00  0.31829
sectorprof      2.2478     0.6698    3.36  0.00085 ***
sectorsales    -0.7671     0.8420   -0.91  0.36273
sectorservice  -0.5687     0.6660   -0.85  0.39356
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 4.33 on 523 degrees of freedom
Multiple R-squared: 0.302,	Adjusted R-squared: 0.289
F-statistic: 22.6 on 10 and 523 DF,  p-value: <2e-16
\end{Soutput}
\end{Schunk}
The adjusted difference between the sexes is \$1.94 per hour.  (The
$R^2=0.30$ from this model is considerably larger than for \texttt{mod0},
but still a lot of the person-to-person variation in wages has not
be captured.)

It would be wrong to claim that simply including a covariate in a
model guarantees that an appropriate adjustment has been made.  The
effectiveness of the adjustment depends on whether the model design is
appropriate, for instance whether appropriate interaction terms have
been included.  However, it's certainly the case that if you {\bf don't}
include the covariate in the model, you have {\bf not} adjusted for
it.

The other approach is to subsample the data so that the levels of the
covariates are approximately constant.  For example, here is a subset
that considers workers between the ages of 30 and 35 with between 10
to 12 years of education and working in the sales sector of the
economy:
\begin{Schunk}
\begin{Sinput}
> small = subset(cps, age <=35 & age >= 30 &
                       educ>=10 & educ <=12 &
                       sector=="sales" )
\end{Sinput}
\end{Schunk}
The choice of these particular levels of \VN{age}, \VN{educ}, and
\VN{sector} is arbitrary, but you need to choose some level if you
want to hold the covariates appproximately constant.

The subset of the data can be used to fit a simple model:
\begin{Schunk}
\begin{Sinput}
> mod4 = lm( wage ~ sex, data=small)
> summary(mod4)
\end{Sinput}
\begin{Soutput}
...
            Estimate Std. Error t value Pr(>|t|)
(Intercept)    4.500      0.500     9.0     0.07 .
sexM           4.500      0.866     5.2     0.12
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.707 on 1 degrees of freedom
Multiple R-squared: 0.964,	Adjusted R-squared: 0.929
F-statistic:   27 on 1 and 1 DF,  p-value: 0.121
\end{Soutput}
\end{Schunk}
At first glance, there might seem to be nothing wrong with this
approach and, indeed, for very large data sets it can be effective.
In this case, however, there are only 3 cases that satisfy the various
criteria: two women and one man.
\begin{Schunk}
\begin{Sinput}
> table( small$sex )
\end{Sinput}
\begin{Soutput}
F M
2 1
\end{Soutput}
\end{Schunk}

So, the \$4.50 difference between
the sexes and wages depends entirely on the data from a single male!
(Chapter \ref{chap:confidence} describes how to assess the precision
of model coefficients.  This one works out to be $4.50 \pm 11.00$ ---
not at all precise.)


\chapter{Summarising the Posterior Distribution}\label{chapter:summaries}
The posterior distribution is the full answer to any Bayesian problem. It gives
a complete description of our state of knowledge and our uncertainty about the
value(s) of unknown
parameters. From the posterior distribution, we can calculate any probability
we want. For example, if we had a posterior distribution $p(\theta|x)$ and we
wanted to know the probability that $\theta$ is greater than or equal to 100, we could do:
\begin{eqnarray}
P(\theta \geq 100 | x) &=& \int_{100}^\infty p(\theta | x) \, d\theta
\end{eqnarray}
or
\begin{eqnarray}
P(\theta \geq 100 | x) &=& \sum_{100}^\infty p(\theta | x)
\end{eqnarray}
depending on whether the set of possible $\theta$ values is continuous or
discrete. We could also work out the probability of anything else.
However, the posterior distribution is sometimes too much
information for us to think about easily. Maybe a giant list of $\theta$
values and probabilities isn't easy to digest. Sometimes,
we need to {\it summarise} the posterior distribution to help us
communicate our results with others. A giant Bayes' Box (or a million MCMC
samples of the parameter, we'll see that later), might technically
contain everything we want, but it's not easy to talk about.

For example, say you were trying to estimate
a parameter, and a colleague asked you to state your uncertainty about the
parameter. Well, your posterior distribution might be complicated. It might
have bumps and wiggles in it, or some other kind of structure. If there were
two or more unknown parameters, there might be dependence in the posterior
distribution. In some cases there might even me multiple separate peaks!
Figure~\ref{fig:complicated_posterior} shows an example of what a complicated
posterior distribution might look like. If this was your result, your colleague
might not care about all the little wiggles in this plot. They just want to know
the ``big picture'' of your results.
\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.6]{Figures/complicated_posterior.pdf}
\caption{\it A complicated posterior distribution. When communicating with others,
it is often useful to summarise the posterior distribution with a few numbers. In
this case, something like ``the parameter = $5 \pm 1$'' might be a useful summary.
\label{fig:complicated_posterior}}
\end{center}
\end{figure}

The idea of summarising the posterior distribution is very closely related to
the idea of summarising a data set, which you probably encountered when you
studied descriptive statistics.
\begin{framed}
{\bf In descriptive statistics, you often make summaries of a complex data set
(e.g. the mean and the standard deviation) so that you can communicate about
the data set in a concise way. In Bayesian statistics, you often do a similar
thing, but instead of giving a concise description of the {\it data}, you give a
concise description of the {\it posterior distribution}.}
\end{framed}

\section{Point Estimates}
A ``point estimate'' refers to a single number guess for the value of a parameter.
If you have several parameters, a point estimate would be a single guess for the
value of each parameter (like a single point in a multidimensional space).
If you look at the
posterior distribution plotted in Figure~\ref{fig:complicated_posterior}, you
can see that the true value of the parameter is probably somewhere around 5,
but with some uncertainty. If you were to provide a single number as a guess of
the parameter, you would probably say something close to 5. In classical statistics, a
single number guess is called an ``estimate'', and a rule for generating such
guesses is called an ``estimator''. Estimates are usually written by putting a
hat over the name of the parameter. So, by looking at the plot of the
posterior, you could give an estimate like this:
\begin{eqnarray}
\hat{\theta} = 5.
\end{eqnarray}
But there are better things you could do than just looking at the plot, and you've
probably learnt some of them
in previous statistics courses. Here are three methods you could use to
choose a point estimate using the posterior distribution: the posterior mean
(the expectation value of the parameter), the posterior median
(the value that divides the probability
in half), and the posterior mode (the value where the posterior distribution has its
peak). In our illustrative example, the values of these three point estimates
are:
\begin{eqnarray}
\hat{\theta} &=& 4.988 \textnormal{ (the posterior mean)}\\
\hat{\theta} &=& 4.924 \textnormal{ (the posterior median)}\\
\hat{\theta} &=& 4.996 \textnormal{ (the posterior mode)}
\end{eqnarray}
In this example, there's not much of a difference between these three methods.
But in other situations, they can be quite different (this usually happens if
the posterior distribution is skewed, or has multiple modes; you may notice a
strong analogy between this topic and descriptive statistics). Is there a way
to say which one is the {\it best}? It turns out there is, but that
depends on what you mean by ``best''.

Before we move on to the formal ways of deciding what constitutes a good estimate, I would
like to mention a very common method that is easy to use. If the posterior distribution
looks even vaguely like a normal distribution, it is common to summarise it like
this:
\begin{eqnarray}
\theta = \textnormal{posterior mean }\pm\textnormal{posterior standard deviation}.
\end{eqnarray}
I use this kind of summary frequently in my own research.

\subsection{A Very Brief Introduction to Decision Theory}
Decision theory is a very important topic. In this course we will use a
{\it tiny} amount of it, just enough to solve the problem of ``which point
estimate is best?''. If you think about it, this is a bit of a weird question.
Obviously, the best point estimate is the true value. Of course it is, how could it
be otherwise? Our only problem is that we can't actually implement this suggestion.
We don't know the true value. We only have the posterior distribution
(which is based on all the evidence we have), and we have to do
the best we can with our incomplete information. To think about which decision is best,
the first thing we should think
about is which decisions are {\it possible}. For estimating a single parameter, any
real number is a possible guess.

The key idea in decision theory is the concept of {\it utility}, and the related
concept of {\it loss} (loss is just negative utility). Utility is
a numerical measure of how good it would be if a certain outcome came true.
Conversely, loss is a measure of how bad it would be if a certain outcome
came true.
Utilities are often subjective (not unlike prior probabilities), but in some
applications utility can be more concrete. For example, in betting or investment
decisions the utility can be measured in dollars. The problem with utility is
that we have uncertainty about what is going to happen, or about what is true,
so we can't just choose the decision that gives us the greatest utility. Instead
we will use our posterior probabilities and choose the decision that gives us
the maximum possible {\it expected value} of the utility.

Imagine we were estimating a parameter $\theta$ and we wanted to give a
point estimate $\hat{\theta}$. One idea for what the utility or loss might be is the
{\it quadratic} loss function, which is given by
\begin{eqnarray}
L(\theta, \hat{\theta}) = \left(\hat{\theta} - \theta\right)^2.
\end{eqnarray}
This expression inside the parentheses is the difference between our point estimate
and the true value. This formula says that if our point estimate is off by 2,
that is four times worse than if we were off by 1. If we were off by 10, that is
100 times worse than if we were off by 1, due to the squaring in the quadratic
loss function formula.

It turns out (we will prove this below) that {\it if the loss function is
quadratic, the best estimate you can give is the posterior mean}.
Here is the proof. The expected value of the loss is
\begin{eqnarray}
\mathds{E}\left[L(\theta, \hat{\theta})\right] =
\int p(\theta|x)(\hat{\theta} - \theta)^2 \, d\theta
\end{eqnarray}
Since we are summing (integrating) over all possible true $\theta$ values,
the expected loss is only a function of our estimate $\hat{\theta}$. To minimise a function
of one variable, you differentiate it and then set the derivative to zero.
The derivative is
\begin{eqnarray}
\frac{d}{d\hat{\theta}}\mathds{E}\left[L(\theta, \hat{\theta})\right] &=&
\int p(\theta|x)\frac{d}{d\hat{\theta}}(\hat{\theta} - \theta)^2 \, d\theta \\
&=& \int p(\theta|x)2(\hat{\theta} - \theta) \, d\theta
\end{eqnarray}
Setting this equal to zero and then solving for $\hat{\theta}$ gives the final
result:
\begin{eqnarray}
\hat{\theta} &=& \int \theta p(\theta|x) \, d\theta.
\end{eqnarray}
which is the posterior mean. Some people call the posterior mean the ``Bayes
Estimate'' for this reason. I don't like that term because I don't think
point estimates are really Bayesian.
The actual output of a Bayesian analysis is the posterior distribution.

Note that I didn't verify that $\hat{\theta}$ actually minimises the expected loss
, because setting the derivative to zero would also find a maximum. To make sure
it really does minimise the expected loss, you can calculate the second
derivative and verify that it is
positive. But that's not really needed. It would be pretty bizarre if the
posterior mean was the {\it worst} estimate!

\subsection{Absolute Loss}
Sometimes, the quadratic loss/utility is not a reasonable model
for the consequences of an incorrect estimate. Another plausible form for the loss
function is the {\it absolute} loss. This looks like:
\begin{eqnarray}
L(\hat{\theta}, \theta) &=& |\hat{\theta} - \theta|.
\end{eqnarray}
With this assumption, the ``badness'' of an incorrect estimate is proportional
to how far the estimate is from the true value. If the estimate is twice as
far from the true value, it is twice as bad. We will not prove it (although you
are welcome to derive this yourself), but for this loss function
the best estimate is the posterior median, which is the value of $\hat{\theta}$
for which $P(\theta \leq \hat{\theta}) = P(\theta > \hat{\theta}) = 0.5$.

\subsection{All-or-nothing Loss}
The third kind of loss function we will look at is the ``all-or-nothing'' loss,
also sometimes called {\it 0-1 loss}.
Sometimes, you may need your estimate to be
completely correct, and if it isn't correct, then it is irrelevant how far your
estimate was from the true value. All incorrect estimates are equally bad.
The all-or-nothing loss looks like:
\begin{eqnarray}
L(\hat{\theta}, \theta) = \left\{
\begin{array}{lr}
0 & \hat{\theta} = \theta\\
1, & \textnormal{otherwise.}
\end{array}
\right.
\end{eqnarray}

If you were in this situation you would want to make your chances as high as
possible, which implies you should simply choose the most probable value
of $\theta$ as your point estimate $\hat{\theta}$. That is, the appropriate
estimate is the posterior mode. This intuition is correct. With all-or-nothing loss,
the best estimate is the posterior mode. The three loss functions we consider
in STATS 331 are shown in Figure~\ref{fig:utility}.

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.5]{Figures/utility.pdf}
\caption{\it Three kinds of loss function, which measure how bad it is for our
point estimate $\hat{\theta}$ to be different from the true value of the parameter
$\theta$. Note that the all-or-nothing loss has a small amount of width in this
plot, just so that we can clearly see the spike at
$\hat{\theta} - \theta$ = 0.\label{fig:utility}}
\end{center}
\end{figure}

\subsection{Invariance of Decisions}
You may be wondering about the definitions of our loss functions. For example,
we defined the quadratic loss as $(\hat{\theta} - \theta)^2$, but what if we
defined it as $3(\hat{\theta} - \theta)^2 + 5$ instead? Would our
best decision change? Luckily, the answer is no. The decision (estimate) which
minimises the expected value of a loss function $L$ also minimises the expected
value of a different loss function $aL + b$, where $a$ is any positive number
and $b$ is any other number.
For the mathematicians, the optimal decision is invariant under positive affine
transformations of the utility or loss function. Phew!

\subsection{Computing Point Estimates from a Bayes' Box}
We have just discussed three different point estimates, and under what
circumstances we can consider them to be the best possible estimate we could
make. Now, we will look at how to actually obtain the point estimates.
The posterior mean is straightforward. It's the expectation value of the parameter
using the posterior distribution. In R, the code is:
\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
post_mean = sum(theta*post)
\end{minted}
You should also know how to compute this manually from a Bayes' Box, using a
calculator.

The posterior mode is also fairly straightforward. First, we can find the
highest probability in the Bayes' Box. Then we find the corresponding parameter
value.
\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
highest_probability = max(post)
post_mode = theta[post == highest_probability]
\end{minted}
In the case of a tie, {\tt post\_mode} might be a vector, indicating
that there isn't a single mode.

The posterior median is a little harder. We need to find the $\theta$ value
which has 50\% of the probability to the left and 50\% of the probability to the
right. Note that this isn't precisely defined in some cases, particularly with
discrete distributions. For example, if $\theta$ could be 1, 2, or 3, and the
probabilities of these were 0.3, 0.6, and 0.1, then what is the median? It is
not entirely clear. However, if there are a large number of possibilities then
the definition becomes more clear.

To calculate the posterior median in R, we need to use the cumulative distribution
which is defined as $F(t) = P(\theta \leq t)$. If we then find the value of $t$
where $F(t) = 0.5$, we have found the posterior median. This isn't always
possible but we can always find the value of $t$ which makes $F(t)$ very close to
0.5.
To obtain the cumulative distribution
in R you can use the {\tt cumsum} function, which calculates the cumulative sum
of a vector. The posterior vector contains the probabilities of $\theta$ equalling
certain values. If we want the probability that $\theta$ is less than or
equal to a certain value, we sum all the probabilities up to and including that value. The
cumulative sum function achieves this. Here is the code for calculating the
posterior median:
\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
F = cumsum(post)
dist = abs(F - 0.5) # Distance of the F-values from 0.5
post_median = theta[dist == min(dist)]
\end{minted}
Note that this may also produce more than one result. Like the mode, the
posterior median is not always uniquely defined.

\subsection{Computing Point Estimates from Samples}
When we use a Bayes' Box (or the equivalent R commands which represent the
columns of a Bayes' Box as vectors), we end up with a vector of possible parameter
values and another vector containing the posterior distribution.
When we use MCMC and JAGS, the output is different.
We will only have a vector of parameter
values, without corresponding probabilities. The vector of parameter values is
meant to be a random sample of values drawn from the posterior distribution.
It's like saying ``here are a bunch of guesses for the parameter'', and any
region where there are a lot of guesses is considered to be a region with high
probability.

When we have samples instead of an exhaustive list of parameter values and
probabilities, the methods for computing the summaries are different. For a
parameter called $\theta$, the methods for computing the summaries are given
below.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# Posterior mean using samples
post_mean = mean(theta)
# Posterior mode using samples
# post_mode = ??? (this can't be done easily with samples!)
# If you have a really large number of samples,
# visually finding the peak of
# a histogram can work.
# Posterior median using samples
sorted = sort(theta)
post_median = sorted[0.5*length(theta)]
\end{minted}

\section{Credible Intervals}
Credible intervals are another useful kind of summary. They are used to make
statements like ``There is a 95\% probability the parameter is between
100 and 150''. The basic idea is to
use the posterior distribution to find an interval $[a, b]$ such that
\begin{eqnarray}
P(a \leq \theta \leq b | x) &=& \alpha
\end{eqnarray}
where $\alpha$ is some pre-defined probability. 95\% seems to be the
most popular choice. An example of a 95\% credible interval is given in
Figure~\ref{fig:credible_interval}.

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.5]{Figures/credible_interval.pdf}
\caption{\it A central 95\% credible interval is defined as an interval that
contains 95\% of the posterior probability, while having 2.5\% of the probability
above the upper limit and 2.5\% of the probability below the lower limit. The
credible interval is formed by finding the edges of the grey region. In this
case the credible interval is $[3.310, 7.056]$.
\label{fig:credible_interval}}
\end{center}
\end{figure}
Note that the interval shown in Figure~\ref{fig:credible_interval} is not the
only possible interval that would contain 95\% of the probability. However, to
make the notion of a credible interval precise, we usually use a {\it central}
credible interval. A central credible interval containing an amount of probability
$\alpha$ will leave $(1-\alpha)/2$ of the probability to its left and
the same amount $(1-\alpha)/2$ of the probability to its right.

\subsection{Computing Credible Intervals from a Bayes' Box}
The method for computing credible intervals is closely related to the method
for computing the posterior median. With the median, we found the value of
$\theta$ which has 50\% of the posterior probability to its left and 50\% to its
right. To find the lower end of a 95\% credible interval, we find the $\theta$
value that has 2.5\% of the probability to its left. To find the upper end we
find the value of $\theta$ that has 2.5\% of the posterior probability to its
right, or 97.5\% to the left.

\subsection{Computing Credible Intervals from Samples}
If you have used MCMC and have obtained random samples from the posterior
distribution, you can find a credible interval in a similar way to how you would
find the posterior median. Again, instead of finding the 0.5 quantile of the
posterior distribution you would find the 0.025 quantile and the 0.975 quantile
(if you wanted a central 95\% credible interval).

\section{Confidence Intervals}
In previous stats courses you have probably come across the concept of a
{\it confidence interval}. A confidence interval is a concept in classical
statistics that is somewhat similar to a credible interval in Bayesian statistics.
When people calculate confidence intervals, they usually want to say they are
95\% sure that the parameter is in that interval,
given the data. This is what Bayesian credible intervals do, but {\it it is not
what classical confidence intervals do}!

Luckily, a lot of the time, the classical and the Bayesian methods for making
intervals will actually give the same interval. But this isn't always the case!
In lectures we will study an example (taken from an Ed Jaynes paper from the
70s)
where the Bayesian credible interval and
the classical confidence interval give completely different results. The result
is shown in Figure~\ref{fig:jaynes}. The key thing to note is the classical
confidence interval lies entirely in a region where we are certain
(from the data) that $\theta$ cannot possibly be!

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.5]{Figures/jaynes.pdf}
\caption{\it An example of a Bayesian credible interval and a frequentist confidence
interval applied to a particular problem where they give different answers.
The posterior distribution in blue shows that the parameter $\theta$ is probably
somewhere between 11 and 12, and values above 12 are completely impossible.
However, the entire frequentist confidence interval lies above $\theta=12$.
\label{fig:jaynes}}
\end{center}
\end{figure}


\section{Mathematical Appendix}\label{appx: b}
To simplify notation for \autoref{appx: b}, I denote the continuation value at budget $b$ by:
\begin{equation*}
    \begin{aligned}
        &K_{b}:=\alpha \,\mathbb{E}_{\theta}\left[V_w(\theta', b)\right]
    \end{aligned}
\end{equation*}

\subsection{Proof for \autoref{prop:piecewiseV} and \autoref{cor:optpolicy}}
\begin{proof}
    Fix some $b\in\mathcal{B}_w$ and, starting from \autoref{eq:full bellman}, consider the following:
    \begin{equation*}
        \begin{aligned}
            V_w(\theta,b) \;&=\;\max\left\{\,\overline{\mu} \, u(\theta) +\alpha \,\mathbb{E}_\theta \Big[V_w(\theta', b-1)\Big]\,,\; \alpha\,\mathbb{E}_\theta \Big[ V_w(\theta', b)\Big]\,\right\}\\
            &=\; \max\left\{\,\overline{\mu} \, u(\theta) + K_{b-1} \,,\; K_b \,\right\}\\
            &=\; K_{b-1} + \max\left\{\,\overline{\mu} \, u(\theta) \,,\; K_b - K_{b-1}\,\right\}
        \end{aligned}
    \end{equation*}

    First, note that the difference between any two consecutive continuation values $K_b$ and $K_{b-1}$ must necesarily lie between 0 and $\overline{\mu}u(1)$.
    This is true since the value function denotes the expected lifetime sum of discounted payoffs, and an additional right-swipe can provide an agent with, at most, an additional expected payoff of $\overline{\mu}u(1)$ and, at least, an additional payoff of $0$.
    Furthermore, since $u(\theta)$ is, by assumption, continuous and increasing over $\Theta$ (and we assume $\overline\mu>0$ to prune out degenerate equilibria), then, by the Intermediate Value Theorem, there exists a unique root, $\widetilde\omega_b$, satisfying:
    \begin{equation*}
            \overline\mu u(\widetilde\omega_b) = K_b-K_{b-1}
    \end{equation*}
    Consider now two cases. First, if $\theta\leq\widetilde\omega_b$, then:
    \begin{equation*}
        \begin{aligned}
            V_w(\theta,b) \;&=\; K_{b-1} + \max\left\{\,\overline{\mu} \, u(\theta) \,,\; K_b - K_{b-1}\,\right\}\\
            &=\; K_{b-1} + K_b - K_{b-1}\\
            &=\; K_b.
        \end{aligned}
    \end{equation*}
    Analogously, if $\theta\leq\widetilde\omega_b$, then:
    \begin{equation*}
        V_w(\theta,b) = \overline{\mu} \, u(\theta) + K_{b-1}.
    \end{equation*}

    Thus, by considering the above function over the intervals $[0, \widetilde\omega_b]$ and $[\widetilde\omega_b, 1]$ separately, and substituting back the expressions for $K_b, K_{b-1}$, we conclude that:
    \begin{equation*}
    \begin{split}
        V_w(\theta,b)=\begin{cases}
            \overline\mu u(\theta) +\alpha \,\mathbb{E}_{\theta}\Big[V_w(\theta', b-1)\Big],& \theta \geq \widetilde \omega_b \\[10pt]
            \alpha \,\mathbb{E}_{\theta}\Big[V_w(\theta', b)\Big],& \theta\leq\widetilde \omega_b
        \end{cases}
    \end{split}
    \end{equation*}

    Furthermore, \autoref{cor:optpolicy} follows trivially from the above by considering a cutoff policy over the above intervals such that $V_w(\theta,b)$ is attained.
\begin{comment}
    When a woman with budget $b$ is presented a candidate with attractiveness $\theta \geq \widetilde\omega_b$ and she swipes right, her expected lifetime sum of discounted payoffs is:
    \begin{equation*}
        \begin{split}
            \overline\mu u(\theta) +\alpha \,\mathbb{E}_{\theta}\Big[V_w(\theta', b-1)\Big]\\
            = V_w(\theta,b)
        \end{split}
    \end{equation*}
    Alternatively, when presented a candidate with attractiveness $\theta<\widetilde\omega_b$ and she swipes left:
    \begin{equation*}
        \begin{split}
            \alpha \,\mathbb{E}_{\theta}\Big[V_w(\theta', b)\Big]\\
            = V_w(\theta,b)
        \end{split}
    \end{equation*}
\end{comment}
\end{proof}

\subsection{Proof for \autoref{prop:recurrence relation}}
\begin{proof}
Fix some $b\in\mathcal{B}_w$ and consider the result presented by \autoref{prop:piecewiseV}, which guarantees the existence and uniqueness of some $\widetilde \omega_b$ satisfying:
\begin{align}
    \begin{split}\label{eq:A.1}
        V_w(\theta, b)&=\begin{cases}
            \overline\mu u(\theta) + K_{b-1},& \theta> \widetilde \omega_b \\
            K_b,& \theta\leq\widetilde \omega_b
        \end{cases}
    \end{split}\\
    \begin{split}\label{eq:A.2}
        \overline\mu u(\widetilde\omega_b) &= K_b-K_{b-1}
    \end{split}
\end{align}

Starting out with \autoref{eq:A.2} and expanding out the expectation operator, we can use \eqref{eq:A.1} to substitute in the piecewise definitions of $V_w(\theta,b)$ over the appropriate intervals:
\begin{equation}\label{eq:A.3}
    \begin{split}
        \overline\mu u(\widetilde\omega_b) &= K_b-K_{b-1}\\
                                           &= \alpha \,\int^1_0 V_w(\theta',b)-V_w(\theta',b-1)\,dF_m(\theta')\\
                                           &=\alpha \int^{\widetilde\omega_b}_0 K_b\,dF_m(\theta') \;+\; \alpha \int^1_{\widetilde\omega_b}\,\overline\mu u(\theta') + K_{b-1}\,dF_m(\theta')\\
                                           & \quad -\,\alpha \int^{\widetilde\omega_{b-1}}_0 K_{b-1}\,dF_m(\theta') \;-\; \alpha \int^1_{\widetilde\omega_{b-1}} \overline\mu u(\theta') + K_{b-2}\,dF_m(\theta')
    \end{split}
\end{equation}

Furthermore, \autoref{eq:A.2} implies that:

$$
\overline\mu u(\widetilde\omega_b) +K_{b-1}= K_b
$$

$$
\overline\mu u(\widetilde\omega_{b-1}) +K_{b-2}=K_{b-1}
$$

Then, by substituting these expressions into \eqref{eq:A.3}, we arrive at \eqref{eq:A.4}:
\begin{equation}\label{eq:A.4}
    \begin{split}
        \overline\mu u(\widetilde\omega_b) &=\alpha \int^{\widetilde\omega_b}_0 \overline\mu u(\widetilde\omega_b) +K_{b-1}\,dF_m(\theta') \;+\; \alpha \int^1_{\widetilde\omega_b} \,\overline\mu u(\theta') + K_{b-1}\,dF_m(\theta')\\
                                           & \quad -\,\alpha \int^{\widetilde\omega_{b-1}}_0 K_{b-1}\,dF_m(\theta') \;-\; \alpha \int^1_{\widetilde\omega_{b-1}} \overline\mu u(\theta') + K_{b-1}-\overline\mu u(\widetilde\omega_{b-1})\,dF_m(\theta')
    \end{split}
\end{equation}

With some algebra, this simplifies down to the recurrence relation in \autoref{eq:recurrence relation}:
\begin{equation}
    u(\widetilde\omega_b)=\alpha   u(\widetilde\omega_b)F_m(\widetilde\omega_b) \;+\; \alpha  u(\widetilde\omega_{b-1})\Big[1  - F_m(\widetilde\omega_{b-1})\Big] \;+\; \alpha\int^{\widetilde\omega_{b-1}}_{\widetilde\omega_b} u(\theta') \,dF_m(\theta')
\end{equation}

Furthermore, to obtain the initial condition for the above, note that the right-swiping budget constraint imposes $V_w(\theta,0)=0, \forall b\in \mathcal{B}_w$. Then, \eqref{eq:A.1} and \eqref{eq:A.2} simplify to:
\begin{align}
    \begin{split}\label{eq:A.5}
        V_w(\theta, 1)&=\begin{cases}
            \overline\mu u(\theta),& \theta> \widetilde \omega_1 \\
            K_1,& \theta\leq\widetilde \omega_1
        \end{cases}
    \end{split}\\
    \begin{split}\label{eq:A.6}
        \overline\mu u(\widetilde\omega_1) &= K_1
    \end{split}
\end{align}

Beginning with \autoref{eq:A.6}, we simplify until arriving at \autoref{eq:initial condition}:
\begin{equation*}
    \begin{split}
        \overline\mu u(\widetilde\omega_1) &= \alpha \, \mathbb{E}_\theta\Big[\,V_w(\theta',1)\,\Big]\\
        &= \alpha \,\int^{\widetilde\omega_1}_0\,K_1\,dF_m(\theta') + \alpha \,\int_{\widetilde\omega_1}^1 \overline\mu u(\theta')\,dF_m(\theta')\\
        &= \alpha \,\int^{\widetilde\omega_1}_0 \overline\mu u(\widetilde\omega_1)\,dF_m(\theta') + \alpha \,\int_{\widetilde\omega_1}^1 \overline\mu u(\theta')\,dF_m(\theta')\\
        &= \alpha \overline\mu u(\widetilde\omega_1)F_m(\widetilde\omega_1) + \alpha \,\int_{\widetilde\omega_1}^1 \overline\mu u(\theta')\,dF_m(\theta')\\
        \implies u(\widetilde\omega_1) &= \alpha u(\widetilde\omega_1)F_m(\widetilde\omega_1) + \alpha \,\int_{\widetilde\omega_1}^1 u(\theta')\,dF_m(\theta')
    \end{split}
\end{equation*}

%To conclude the proof, note that the existence and uniqueness of some $\widetilde\omega_b$ that satisfies \ref{eq:A.2} is guaranteed given the assumptions on $u(\theta)$ being continuous and strictly increasing. Since the difference between any two consecutive continuation values must lie strictly between 0 and $\overline{\mu}u(1)$, then, by the Intermediate Value Theorem, there exists one and only one root $\widetilde\omega_b$ satisfying \ref{eq:A.2} and, by extension the above reoccurrence relation.
\end{proof}


% !TeX spellcheck = en_US
\documentclass[]{article}

\usepackage[natbib]{TiPi}

\title{Scaling to integer}
\author{Éric T.}

\newcommand*{\Lag}{\mathscr{L}}
\newcommand*{\IntRange}[1]{[\![#1]\!]}
\newcommand*{\half}{\ensuremath{{}^1\!/\!{}_2}}
\renewcommand*{\proxy}[1]{\widetilde{#1}}

\begin{document}

\maketitle

\begin{abstract}
We discuss the best way to rescale floating-point data to integer values by an
affine transform.
\end{abstract}

\section{Digitization}

Assume that we have data values $\V d = \{d_1,d_2,\ldots,d_m\} \in \Reals^m$
which we want to approximate as integers $\V k = \{k_1,k_2,\ldots,k_m\} \in
\IntRange{k_\Tag{min},k_\Tag{max}}^m\subset\Integers^m$ through the affine transform:
\begin{equation}
  \label{eq:approx-data}
  \V d \approx \proxy{\V d} = \alpha\,\V k + \beta\,\One \, .
\end{equation}
If $\alpha \not= 0$, a possible approximate reciprocal formula to compute $\V
k$ is given by:
\begin{equation}
  \label{eq:digitization}
  \V k = \Round{(\V d - \beta\,\One)/\alpha} \, ,
\end{equation}
where $\Round{\cdot}: \Reals\mapsto\Integers$ is applied elementwise and rounds
its argument(s) to the nearest integer(s).  This function has the following
properties:
\begin{equation}
  \label{eq:rounding}
  u - 1/2 < \Round{u} \le u + 1/2
  \quad\Longleftrightarrow \quad
  \Round{u} - 1/2 \le u < \Round{u} + 1/2 \, ,
\end{equation}
for any $u\in\Reals$.

\section{Criteria for the affine parameters}

A first suggestion is to find the parameters $\alpha$ and $\beta$ such that the
approximation in Eq.~(\ref{eq:approx-data}) yields the least worst error:
\begin{equation}
  \label{eq:criterion}
  (\estim{\alpha},\estim{\beta})
  = \argmin_{\alpha,\beta} \Norm[\big]{\V d - \proxy{\V d}}_{\infty} \, .
\end{equation}
Note that we could have also required to optimize the criterion with respect to
the values of $\V k$ but the problem would have been far more complex.  The
assumed digitization formula~(\ref{eq:digitization}) to compute $\V k$ given
$\V d$, $\alpha$ and $\beta$ is only a possibility but it is simple and
uniformly accurate.

Another point to consider is the stability of the transform when
Eq.~(\ref{eq:approx-data}) and Eq.~(\ref{eq:digitization}) are alternately
applied several times with $\alpha$ and $\beta$ computed each time according to
the actual data range.  To avoid any drift, it may be advisable that the choice
of $\alpha$ and $\beta$ ensures that some determined data value remains exactly
represented.  This suggests to choose $\beta$ to be a multiple of $\alpha$ as
this implies that, for instance, a value of zero for the data will always be
exactly represented.  \oops{Study the successive transforms and check that no
shrink occurs.}

To simplify the reasoning we assume in the sequel that $\alpha \ge 0$ and that
the data bounds:
\begin{align}
   d_\Tag{min} &\bydef \min_{i\in\IntRange{1,m}} d_i \, , \\
   d_\Tag{max} &\bydef \max_{i\in\IntRange{1,m}} d_i \, ,
\end{align}
are both finite.  We also note that if $d_\Tag{max} = d_\Tag{min}$, we can
choose $\estim{\alpha}$, $\estim{\beta}$ and $\V k = \Zero$ such that the worst
error is exactly zero\footnote{ For instance, if $0 \in
\IntRange{k_\Tag{min},k_\Tag{max}}$, taking $\estim{\alpha} = 1$,
$\estim{\beta} = d_\Tag{max} = d_\Tag{min}$ and $\V k = \Zero$ yields
$\Norm[\big]{\V d - \proxy{\V d}}_{\infty} = 0$; otherwise, if $0 \not\in
\IntRange{k_\Tag{min},k_\Tag{max}}$, taking $\estim{\alpha} = 0$,
$\estim{\beta} = d_\Tag{max} = d_\Tag{min}$ and any $\V k \in
\IntRange{k_\Tag{min},k_\Tag{max}}^m$ also yields $\Norm[\big]{\V d - \proxy{\V
d}}_{\infty} = 0$.}.  In what follows, we therefore consider that $d_\Tag{max}
> d_\Tag{min}$.

\section{Choosing the scale}

If $\alpha=0$ then $\proxy{\V d}$ given by Eq.~(\ref{eq:approx-data}) does not
depend on $\V k$ and the worst error is:
\begin{displaymath}
  \Norm[\big]{\V d - \proxy{\V d}}_{\infty} = \max\Brace{
    \Abs{d_\Tag{min} - \beta}, \Abs{d_\Tag{max} - \beta}
  } \qquad\text{(when $\alpha = 0$)} \, .
\end{displaymath}
This error is obviously minimized when $\beta$ is the central value of the data range:
\begin{displaymath}
  \estim{\beta} = (d_\Tag{min} + d_\Tag{max})/2
  \qquad\text{(when $\estim{\alpha} = 0$)} \, .
\end{displaymath}

We now consider the other possibility and assume that $\alpha > 0$.  For $\V k$
given by Eq.~(\ref{eq:digitization}), the pointwise error is then given by:
\begin{displaymath}
  \V d - \proxy{\V d} = \alpha\,\Paren[\big]{\V u - \Round{\V u}} \, ,
\end{displaymath}
with $\V u \bydef (\V d - \beta\,\One)/\alpha$.  Since, from
Eq.~(\ref{eq:rounding}), $-1/2 \le u - \Round{u} < 1/2$, the worst error is
then:
\begin{equation}
  \label{eq:worst-error}
  \Norm[\big]{\V d - \proxy{\V d}}_{\infty} = \Abs{\alpha}/2 = \alpha/2 \, ,
\end{equation}
where the last equality follows from our assumption that $\alpha \ge 0$.  As a
result of the chosen digitization formula~(\ref{eq:digitization}), the worst
error does not depend on $\beta$ but solely on $\alpha$.  According to
Eq.~(\ref{eq:worst-error}), to have the least worst error, we should choose the
smallest $\alpha$ (in magnitude) such that all $\V k$ given by
Eq.~(\ref{eq:digitization}) are in the range
$\IntRange{k_\Tag{min},k_\Tag{max}}$.  This corresponds to the intuition that
the smaller the discretization step $\alpha$, the more accurate the resulting
digitization.

Whatever $\beta$, the smaller the magnitude of $\alpha$ the larger is the
interval spanned by the values of $\V k$ computed according to
Eq.~(\ref{eq:digitization}), thus the smallest possible $\alpha$ is such that
the bounds $k_\Tag{min}$ and $k_\Tag{max}$ are reached when the data span the
range $[d_\Tag{min},d_\Tag{max}]$.  Since the mapping $\V d \mapsto \V k$
implemented by the digitization formula~(\ref{eq:digitization}) is
monotonically increasing (because $\alpha > 0$), the following constraints must
hold:
\begin{align}
   &\left\{
   \begin{array}{l}
   \Round{(d_\Tag{min} - \beta)/\alpha} = k_\Tag{min} \\[1ex]
   \Round{(d_\Tag{max} - \beta)/\alpha} = k_\Tag{max}
   \end{array}\right. \notag  \\
   \Longleftrightarrow \quad
   &\left\{
   \begin{array}{l}
   k_\Tag{min} - 1/2 \le (d_\Tag{min} - \beta)/\alpha < k_\Tag{min} + 1/2 \\[1ex]
   k_\Tag{max} - 1/2 \le (d_\Tag{max} - \beta)/\alpha < k_\Tag{max} + 1/2
   \end{array}\right.
   \label{eq:bounds}
\end{align}
taking the difference between the two last pairs of inequalities yields:
\begin{displaymath}
  k_\Tag{max} - k_\Tag{min} - 1 < (d_\Tag{max} - d_\Tag{min})/\alpha <
  k_\Tag{max} - k_\Tag{min} + 1 \, .
\end{displaymath}
As we assumed that $d_\Tag{max} > d_\Tag{min}$, the above inequalities become:
\begin{displaymath}
  \frac{d_\Tag{max} - d_\Tag{min}}{k_\Tag{max} - k_\Tag{min} + 1}
  < \alpha <
  \frac{d_\Tag{max} - d_\Tag{min}}{k_\Tag{max} - k_\Tag{min} - 1} \, .
\end{displaymath}
Thus:
\begin{equation}
  \label{eq:best-alpha}
  \boxed{
    \alpha = \frac{d_\Tag{max} - d_\Tag{min}}
    {k_\Tag{max} - k_\Tag{min} + \eta}
  } \, ,
\end{equation}
with $\eta \in (-1,1)$.  The smallest possible $\alpha$ corresponds to $\eta =
1$ but this is a strict lower bound.  For now, we keep the freedom to choose
$\eta \in (-1,1)$ and consider how to determine the bias $\beta$.

\section{Choosing the bias}

The inequalities in Eq.~(\ref{eq:bounds}) can be combined to bound the value of
$\beta/\alpha$:
\begin{displaymath}
  \max\Brace{\sigma_0, \sigma_1} - 1/2
  < \beta/\alpha \le
  \min\Brace{\sigma_0, \sigma_1} + 1/2 \, ,
\end{displaymath}
with $\sigma_0 = d_\Tag{min}/\alpha - k_\Tag{min}$ and $\sigma_1 =
d_\Tag{max}/\alpha - k_\Tag{max}$.  The difference $\sigma_1 - \sigma_0$ has a
simple expression for $\alpha$ given by Eq.~(\ref{eq:best-alpha}):
\begin{displaymath}
  \sigma_1 - \sigma_0 =
  (d_\Tag{max} - d_\Tag{min})/\alpha - k_\Tag{max} + k_\Tag{min} = \eta \, ,
\end{displaymath}
and putting all together yields:
\begin{equation}
  \boxed{
    \gamma_0 < \beta/\alpha \le \gamma_1
  } \, ,
  \label{eq:beta-bounds}
\end{equation}
with:
\begin{align}
  \gamma_0 &= \max\Brace{\sigma_0, \sigma_1} - 1/2 \notag \\
  %&= \sigma_0 + \max\Brace{0, \eta} - 1/2 \notag \\
  &= \sigma_0 + (\eta)_{+} - 1/2
   = \sigma_1 - (\eta)_{-} - 1/2 \, , \\
  \gamma_1 &= \min\Brace{\sigma_0, \sigma_1} + 1/2 \notag \\
  %&= \sigma_0 + \min\Brace{0, \eta} + 1/2 \notag \\
  &= \sigma_0 + (\eta)_{-} + 1/2
   = \sigma_1 - (\eta)_{+} + 1/2 \, ,
\end{align}
and where $(\eta)_{+} = \max\Brace{0, \eta}$ and $(\eta)_{-} = \min\Brace{0,
\eta}$.  The range of possible values for $\gamma = \beta/\alpha$ is the
semi-open interval $(\gamma_0,\gamma_1]$.  The width of the interval is:
\begin{displaymath}
  \gamma_1 - \gamma_0 = 1 - (\eta)_{+} + (\eta)_{-} = 1 - \Abs{\eta} \, .
\end{displaymath}
Since $\eta \in (-1,1)$, $\gamma_1 - \gamma_0  \in (0,1]$ so the semi-open
interval $(\gamma_0,\gamma_1]$ is always non-empty.  The center of the interval
is:
\begin{displaymath}
  \gamma_\Tag{cen} = (\gamma_0 + \gamma_1)/2 = \sigma_0 + \eta/2 = \sigma_1 - \eta/2 \, .
\end{displaymath}

If $d_\Tag{max} > d_\Tag{min}$ and $k_\Tag{max} > k_\Tag{min}$, we have the
flexibility to choose $\eta$ in the range $(-1,1)$.   We can either:
\begin{itemize}
\item Take $\eta = 0$, then $\gamma_1 - \gamma_0 = 1$ and there is thus exactly
one integer value in the range  $(\gamma_0,\gamma_1]$ which is
$\Floor{\gamma_1}$.  This let us choose $\gamma=\beta/\alpha$ to be integer so
that a zero in the data is exactly represented in the digitized data.  To have
$\gamma$ integer, we can take $\gamma = \Floor{\gamma_1}$ but noting that, when
$\eta=0$, we have:
\begin{equation}
	\sigma_0 = \sigma_1 = \gamma_\Tag{cen}
	= \frac{d_\Tag{min}\,k_\Tag{max} - d_\Tag{max}\,k_\Tag{min}}
	       {d_\Tag{max} - d_\Tag{min}} \, ,
	       \label{eq:gamma-center}
\end{equation}
which belongs to $(\gamma_0,\gamma_1]$ and thus $\Round{\gamma_\Tag{cen}} =
\Floor{\gamma_1}$.  A possibility is thus to take $\eta=0$, and $\gamma =
\Round{\gamma_\Tag{cen}}$ or $\gamma = \gamma_\Tag{cen}$ depending whether or
not we want to exactly represent specific data values such as zero.

\item Choose $\eta \rightarrow 1^-$ to minimize the worst error which is
slightly better than the previous case but imposes $\gamma = \beta/\alpha
\rightarrow \gamma_1^-$ (or equivalently $\gamma = \beta/\alpha \rightarrow
\gamma_0^+$) which is not guaranteed to be integer.  In practice, the value of
$\eta$ must be such that the denominator in Eq.~(\ref{eq:best-alpha}) is as
close as possible but numerically strictly smaller than $k_\Tag{max} -
k_\Tag{min} + 1$, this leads to take $\eta$ such that:
\begin{align}
  &k_\Tag{max} - k_\Tag{min} + \eta
  = (k_\Tag{max} - k_\Tag{min} + 1)\,(1 - \varepsilon) \notag \\
  \Longrightarrow\quad& \eta = 1 - (k_\Tag{max} - k_\Tag{min} + 1)\,\varepsilon \, ,
  \label{eq:max-eta}
\end{align}
with $\varepsilon$ the smallest value such that $1\pm\varepsilon$ is
numerically different from 1.  The value of $\eta$ in Eq.~(\ref{eq:max-eta}) is
nonnegative if the number of digitization levels $k_\Tag{max} - k_\Tag{min} +
1$ at most $1/\varepsilon$.  For larger number of digitization levels taking
$\eta=0$ is better.  For 64-bit IEEE floating point values, $\varepsilon =
2^{-52}$ and thus using Eq.~(\ref{eq:max-eta}) imposes to digitize to at most
52-bit integers.

\item Taking $\eta < 0$ is worst than the previous cases so must be
disregarded.
\end{itemize}

To summarize, taking $\eta = 0$ is only slightly better than $\eta->1^-$ in
terms of least worst error but is more general (it applies to any number of
digitization levels) and offers more flexibility for choosing the bias $\beta$,
for instance we can have $\beta$ a multiple of $\alpha$.

\section{Stable transform}

As the computations only depend on the four given parameters, $k_\Tag{min}$,
$k_\Tag{max}$, $d_\Tag{min}$ and $d_\Tag{max}$, a simple constraint to preserve
the stability of the resulting transforms can be guaranteed if the data bounds
are exactly preserved, \emph{i.e.}:
\begin{displaymath}
  \left\{
  \begin{array}{l}
    d_\Tag{min} = \alpha\,k_0 + \beta \\[1ex]
    d_\Tag{max} = \alpha\,k_1 + \beta
  \end{array}\right.
  \quad\Longleftrightarrow \quad
  \left\{
  \begin{array}{l}
    \displaystyle
    \alpha = \frac{d_\Tag{max} - d_\Tag{min}}{k_1 - k_0} \\[2ex]
    \displaystyle
    \beta = \frac{d_\Tag{min}\,k_1 - d_\Tag{max}\,k_0}{k_1 - k_0}
  \end{array}
  \right.
\end{displaymath}
for some $(k_0,k_1) \in \IntRange{k_\Tag{min},k_\Tag{max}}^2$ and assuming that
$k_0\not=k_1$.  As discussed above the smaller $\alpha$ the more accurate the
digitization, thus $k_0 = k_\Tag{min}$ and $k_1 = k_\Tag{max}$ is the best
choice if we further impose that $k_0 < k_1$.   In that case the digitization
parameters write:
\begin{equation}
  \left\{
  \begin{array}{l}
    \displaystyle
    \alpha = \frac{d_\Tag{max} - d_\Tag{min}}
                  {k_\Tag{max} - k_\Tag{min}} \\[2ex]
    \displaystyle
    \beta = \frac{d_\Tag{min}\,k_\Tag{max} - d_\Tag{max}\,k_\Tag{min}}
                 {k_\Tag{max} - k_\Tag{min}} \\[2ex]
    \displaystyle
    \gamma = \beta/\alpha
    = \frac{d_\Tag{min}\,k_\Tag{max} - d_\Tag{max}\,k_\Tag{min}}
           {d_\Tag{max} - d_\Tag{min}}
  \end{array}
  \right.
  \label{eq:exact-interpolation}
\end{equation}
This yields the same value for $\alpha$ as in Eq.~(\ref{eq:best-alpha}) with
$\eta = 0$ and $\gamma = \beta/\alpha = \gamma_\Tag{cen}$ with
$\gamma_\Tag{cen}$ given in Eq.~(\ref{eq:gamma-center}).  In other words, the
parameters derived from exact interpolation of the bounds are compatible with
Equations~(\ref{eq:best-alpha}) and (\ref{eq:beta-bounds}) for $\eta = 0$.

\section{Proposed parameters}

To summarize all previous considerations, taking $\eta = 0$ seems the best
choice. Assuming $k_\Tag{max} > k_\Tag{min}$ and $d_\Tag{max} > d_\Tag{min}$, a
good selection of digitization parameters is then:
\begin{equation}
  \boxed{
  \begin{array}{l}
    \displaystyle
    \alpha = \frac{d_\Tag{max} - d_\Tag{min}}
                  {k_\Tag{max} - k_\Tag{min}} \, , \\[2ex]
    \displaystyle
    \gamma_\Tag{cen} = \frac{d_\Tag{min}\,k_\Tag{max} - d_\Tag{max}\,k_\Tag{min}}
                            {d_\Tag{max} - d_\Tag{min}} \, , \\[2ex]
    \displaystyle
    \beta = \alpha\times\begin{cases}
    \Round{\gamma_\Tag{cen}} & \text{to preserve zeros,} \\
    \gamma_\Tag{cen} & \text{to preserve data bounds.} \\
    \end{cases}
  \end{array}
  }
\end{equation}
When $k_\Tag{max} > k_\Tag{min}$ and $d_\Tag{max} = d_\Tag{min}$, taking
$\alpha = 0$ and $\beta = d_\Tag{max} = d_\Tag{min}$ yields an exact
representation.  Finally, when $k_\Tag{max} = k_\Tag{min}$ (which means that
the values are digitized on a single level!), $\alpha = 0$ and $\beta =
(d_\Tag{max} + d_\Tag{min})/2$ yields the least worst error.

\section{Digitization with non-finite values}

In order to cope with non-finite data (NaN or $\pm\infty$), the following
digitization rule can be applied:
\begin{equation}
  \label{eq:complex-digitization}
  k_i = \begin{cases}
     k_\Tag{nan}     & \text{if $d_i$ is not a number;}\\
     k_\Tag{+\infty} & \text{if $d_i > d_\Tag{max}$;}\\
     k_\Tag{-\infty} & \text{if $d_i < d_\Tag{min}$;}\\
     \Round{(d_i - \beta)/\alpha} & \text{otherwise.}
  \end{cases}
\end{equation}
where $k_\Tag{nan}$, $k_\Tag{+\infty}$ and $k_\Tag{-\infty}$ are chosen
integers while $d_\Tag{min}$ and $d_\Tag{max}$ (with $d_\Tag{max} \ge
d_\Tag{min}$) are both finite and specify the range of valid data values.
\oops{Perhaps add cases for $k_\Tag{min}$ and $k_\Tag{max}$ to avoid unwanted
behavior due to rounding errors.}

The integers $k_\Tag{nan}$, $k_\Tag{+\infty}$ and $k_\Tag{-\infty}$ can be
chosen outside the range $\IntRange{k_\Tag{min},k_\Tag{max}}$ (thus reserving a
few values of the integer range for representing theses special cases) but this
is not mandatory.  For instance, if we take $k_\Tag{-\infty} = k_\Tag{min}$ and
$k_\Tag{+\infty} = k_\Tag{max}$, then the above rule implements \emph{data
clipping}:
\begin{equation}
  \label{eq:digitization-with-clipping}
  k_i = \begin{cases}
     k_\Tag{nan} & \text{if $d_i$ is not a number;}\\
     k_\Tag{min} & \text{if $d_i \le d_\Tag{min}$;}\\
     k_\Tag{max} & \text{if $d_i \ge d_\Tag{max}$;}\\
     \Round{(d_i - \beta)/\alpha} & \text{otherwise.}
  \end{cases}
\end{equation}

\bibliographystyle{plainnat}
\bibliography{journals-short,biblio}

\end{document}