\section{Equational Reasoning}
\label{sec:equationalreasoning}

Verification is the process of checking if software does what its specification demands. To verify a program, a specification is required. In the case of functions that implement an instance of the type classes of section \ref{sec:typeclasses}, the specification is defined in form of the type class laws.
This section will compare different verification techniques and describe the method equational reasoning by example using type class laws as specification. In section \ref{sec:example} equational reasoning will be applied to proof the property of listing \ref{lst:monoidinstance1} in section \ref{sec:typeclasses}.

There are several ways to check the behavior of a program.
We will describe the difference with a simple example. Given the following property:

\begin{equation}
  \label{eq:reverse_prop}
\text{reverse} (\text{reverse } (xs)) = xs
\end{equation}
Equation \ref{eq:reverse_prop} shows that if we apply \verb|reverse| twice on the same list \verb|xs| we get back the original list \verb|xs|. \verb|reverse| is the inverse of \verb|reverse| (other functions, like \verb|id|, have this property too). Verification techniques allow us to check if equation \ref{eq:reverse_prop} holds. We describe three techniques. The first two, testing and property-based testing, are very common and the third one is the topic of this article.

\begin{description}
\item[Testing] Run the program with a selected input and check if it behaves as expected. In order to check the behavior, a function evaluates both sides of equation \ref{eq:reverse_prop} and compares the values. The following listing shows an example test.

\begin{lstlisting}[caption={Function definition for testing},label={lst:testing}]
input = [1,2,3]
test_reverse :: Bool
test_reverse = reverse (reverse input) == input
\end{lstlisting}

The selected input is \verb|[1,2,3]|. \verb|test_reverse| evaluates to a Boolean expression to indicate if the property holds for the given input. It's necessary to run the program to evaluate \verb|test_reverse|.
An advantage of this method is, that the programmer doesn't have to define general properties. The specification is expressed with a concrete input value and a concrete output value. It's easier to think about a concrete input and the corresponding output value than to find general properties that a function must obey.
\item[Property-based testing] The input for the test program is generated randomly. The tests are executed by a tool (e.g. QuickCheck).
\item[Proof] A Proof can show that a property holds in all circumstances. To prove a property we use the technique equational reasoning. This technique requires knowledge of the function definition.
\end{description}

Figure \ref{fig:property_validation} compares the input coverage of the described methods. Testing checks if the program behaves correctly with one chosen point of the input space. Property-based testing checks the behavior at hundreds of randomly-generated points. Proving a property covers all possible cases of the possible input. It is the most reliable verification.

\begin{figure}
  \centering
     \includegraphics[width=0.9\textwidth]{testing}
  \caption{Comparison of testing, property-based-testing and proof}
  \label{fig:property_validation}
\end{figure}

\subsection{Reasoning about algebraic properties}

Equational reasoning is a method originally used in algebra. It's the process of proving a given property by substituting equal expressions.
For example, it's possible to show that the following property holds:
\begin{equation}
  \label{eq:sum}
  (x+a)(x+b) = x^2 + (a+b)x+ab
\end{equation}
To show that the equality holds, we have to transform the expression on the left-hand side  $(x+a)(x+b)$  to an equal expression on the right-hand side with the help of the basic algebraic properties of numbers (\gls{distributive_law}, \gls{commutative_law}) until we get $x^2 + (a+b)x+ab$  \cite{hutton}.

In the first step (equation \ref{eq:firstalgebra} we use distributivity to expand the term on the left-hand side.
\begin{equation}
   (x+a)(x+b) = x^2 + ax + xb + ab
\end{equation}
In the second step we use commutativity to substitute $xb$ with $bx$.
\begin{equation}
x^2 + ax + xb + ab = x^2 + ax + bx + ab \text{     (use commutativity)}
\end{equation}
In the last step we use distributivity to factorize $x$ and we get $x^2 + (a+b)x+ab$.
\begin{equation}
x^2 + ax + bx + ab = x^2 + (a + b)x + ab \text{     (use distributivity)}
\end{equation}
All we did, was substituting expression according the algebraic properties

\subsection{Reasoning about Haskell programs}

A function definition in Haskell means that we can substitute the left-hand side with the right-hand side and vice versa. This is possible because Haskell is a purely functional language. Hence, we can use the same approach to prove that a property of a program written in Haskell holds, as we used to reason about mathematical expressions.
For example, it's possible to show that the length of a list with one element is actually 1. This general property can be formed as an Haskell expression.
\begin{verbatim}
length [x] = 1
\end{verbatim}
The property holds no matter what \verb|x| is. To show that, we use the \gls{function-definition} of \verb|length| as a general description of the behavior. Listing \ref{lst:lengthdefinition} shows the definition of \verb|length| \cite{hutton}.
\begin{lstlisting}[caption={Function definition of {\ttfamily length}},label={lst:lengthdefinition}]
length [] = 0
length (x:xs) = 1 + length xs
\end{lstlisting}

In order to conclude that \verb|length [x] == 1| is always true, we substitute \verb|length [x]| until we get \verb|1|. Listing \ref{lst:lengthproof} shows the step by step substitution. It's a stepwise transformation of the expression \verb|length [x]| to \verb|1|.
\begin{lstlisting}[caption={step by step substitution of {\ttfamily length}},label={lst:lengthproof}]
length [x]         -- [x] is the same as x:[]
= length (x:[])    -- apply definition
= 1 + length []    -- apply definition
= 1 + 0            --  1 + 0 = 1
1
\end{lstlisting}
Function definitions are general descriptions and we can use them to deduce other general properties by substituting equal expressions.

\subsection{Proof by structural induction}
\label{sec:induction}

If we apply simple substitution to a recursive function, we run into problems.
Consider the \gls{function-definition} of \verb|length| in listing \ref{lst:lengthdefinition}.
If we substitute \verb|length x| with the definition \verb|1 + length xs|, we end up substituting \verb|length x| forever. A way to verify recursive programs is to use proof by structural induction.
Proof by structural induction can be used for list or algebraic data types with a recursive constructor (e.g. Tree) \cite{Thompson}.

 The principle of induction states, that it is sufficient to prove a property $p$ for the base case and that $p$ is preserved by the inductive case \cite{Doets}. In order to prove $p$, two steps are required:
 \begin{description}
 \item[Base case] Prove $p(0)$ is true.
 \item[Induction step] Prove $p(n+1)$ if $p(n)$ (induction hypothesis) is true.
 \end{description}

Proof by induction is similar to writing a recursive function. Recursive functions use a base case (e.g. \verb|[]|, 0).
If we use structural induction we proof the base case. We show that the property holds for a concrete input value (e.g. \verb|[]|, 0).

In a recursive function definition we define \verb|f (x:xs)| and use \verb|f x| in the right-hand side. In the proof we show that $p(n+1)$ with the assumption that $p(n)$ holds.

We explain proof by structural induction with another example from \cite{Thompson}. We verify that the overall length of two concatenated lists $xs$ and $ys$, is the same as the sum of the length of $xs$ and the length of $ys$.  The ++-operator concatenates two lists.
\begin{equation}
  \label{eq:lengthprop}
  \text{length (xs ++ ys)} = \text{length(xs) + length(ys)}
\end{equation}
In order to verify property \ref{eq:lengthprop}, we need the function definitions for \verb|length| and \verb|(++)|.
Listings \ref{lst:lengthdefinition} and \ref{lst:concatdefintion} show he function definitions of \verb|length| and \verb|(++)|.

\begin{lstlisting}[caption={Haskell function definition of the concatenation operator},label={lst:concatdefintion}]
[] ++ xs = xs
(x:xs) ++ ys = x:(xs++ys)
\end{lstlisting}

\begin{description}
\item[Base case]
We have to show that property \ref{eq:lengthprop} holds for the base case. The base case, in this example, are the arguments \verb|[]| and an arbitrary list for \verb|ys|. It is not necessary to replace \verb|ys| because the definition of \verb|++| uses recursion over \verb|xs|. \verb|ys| will always be the same list.
In order to check if property \ref{eq:lengthprop} holds for the base case, we replace \verb|xs| with  \verb|[]|, leading to the following Haskell expression.

\begin{verbatim}
length ([] ++ ys) = length [] + length ys
\end{verbatim}

We will evaluate the expression stepwise on the left-hand side and the right-hand side separately.
The left-hand side evaluates to

\begin{verbatim}
length ([] ++ ys) -- apply ++
= length ys
\end{verbatim}

The right-hand side evaluates to

\begin{verbatim}
length [] + length ys     -- apply length []
= 0 + length ys
= length ys
\end{verbatim}

When we evaluate each side of the equation \ref{eq:lengthprop} with the value \verb|[]| for \verb|xs|, the result is \verb|length ys| on both sides. Hence, property \ref{eq:lengthprop} holds for the base case.

\item[Induction step]
We have to show that if equation \ref{eq:lengthprop} holds for any list \verb|xs| then it also holds for \verb|x:xs| (\verb|x:xs| is the list \verb|xs| with a  element \verb|x| attached to its head). Therefore we have to show that the two expressions on both sides of the \verb|=| sign of the  expression in listing \ref{lst:inductionstep} are equal with the assumption that the property of equation \ref{eq:lengthprop} holds (induction hypothesis).
\begin{lstlisting}[caption={Equality expression for induction step},label={lst:inductionstep}]
length ((x:xs) ++ ys) = length (x:xs) + (length ys)
\end{lstlisting}
Again, we evaluate the the left-hand side of equation in listing \ref{lst:inductionstep} step by step.
\begin{verbatim}
length ((x:xs) ++ ys)        -- apply definition of ++
= length (x:(xs ++ ys))      -- apply definition of length
= 1 + length (xs ++ ys)      -- use induction hypothesis
= 1 + length xs + length ys
\end{verbatim}
If we evaluate the right-hand side of the equation in listing \ref{lst:inductionstep} we get:
\begin{verbatim}
length (x:xs) + length ys    -- apply definition of length
= 1 + length xs + length ys
\end{verbatim}

The last listing shows, that the equality in listing \ref{lst:inductionstep} follows from the induction hypothesis in equation \ref{eq:lengthprop}. This completes the induction step and therefore the proof itself.
\end{description}

The previous example used \glspl{function-definition} of \verb|length| and \verb|(++)|. In order to apply equational reasoning we have to know the function definitions of involved functions or we can rely on already proven properties.
For example all types of the standard library, that are an instance of a type class, satisfy the type class laws (see \ref{sec:typeclasses}) \cite{yorgey}. Some libraries exhibit properties in their documentation (e.g. pipes library \cite{gonzales13}).


\chapter{Equivalence of Guesses}

\section{Overview}

Just as a human would be easy to identify equivalent guesses, we use that in computer as well.

[Isomorphism of guesses]

 * Precise definition of state equivalence should be on the
 * resulting partition. However, if computing that equivalence
 * is too expensive, we could instead compute the equivalence
 * of guesses.

\section{Prerequisites}

A \emph{constraint} is an associated pair of guess and response. A set of constraints is denoted $\vC$. When applied to a set of potential secrets, a constraint set selects a subset of the secrets that are consistent with those constraints. We call this subset  \emph{generated} by the constraint set. In particular, an empty constraint set generates all codewords.

A constraint set is called \emph{contradictory} if its generated secret set is empty, i.e.\ if no secret could satisfy all the constraints.

A constraint in a constraint set is called \emph{redundant} if the secrets generated from this constraint set is the same with or without that constraint. For example, the same constraint appearing twice is clearly redundant. Also, consider the example:
\[
\begin{array}{c}
\cw{1234} : \fb{0}{0} \\
\cw{3456} : \fb{0}{0} \\
\cw{1256} : \fb{0}{0}
\end{array}
\]
Any one of the constraints is redundant.

A constraint set without redundant constraints is called \emph{irreducible}. In the following we are only interested in irreducible constraint sets.

Two constraint sets are \emph{equal} in the traditional set-equal sense, i.e.\ if they contain the same constraints, regardless of order. Note that this is \emph{not} the same as set-equal on the generated secrets. For example, the constraint sets
\[
\vC_1 = (1234:0A0B, 3456:0A0B)
\]
and
\[
\vC_2 = (1234:0A0B, 1256:0A0B)
\]
are not equal by definition, although they generate the same secrets.

Let $\vS$ denote the set of all codewords under a given set of rules. A few properties of constraint sets are shown below.

1. Not all subsets of $\vS$ can be generated from constraints. For example, in Bulls and cows, after the first guess, the number of remaining secrets will not be more than 1440; this means any proper subset with more than 1440 elements cannot be generated from constraints.

Although there are as many as $2^5040$ subsets, only a fraction can be generated by constraints. If we consider equivalence, the number is even smaller and probably tractable.

2. If two constraint sets are equal, then the subsets generated from them are equal; if two subsets are different, and their generating constraints cannot be equal.

3. If two constraint sets are different, their generated secrets may or may not be equal. See the example above.
%For a non-trivial example, consider the following simulated guessing process by different strategies:
%
%	 Secret: 7450
%	 # simple    minmax    entropy   maxparts
%	-------------------------------------------
%	 1 0123:0A1B 0123:0A1B 0123:0A1B 0123:0A1B
%	 2 1456:2A0B 1456:2A0B 1456:2A0B 1456:2A0B
%	 3 1478:1A1B 2435:1A1B 7418:2A0B 2475:1A2B
%	 4 1759:1A1B 0276:0A2B 0752:1A2B 0482:1A1B
%	 5 1896:0A0B 7450:4A0B 7450:4A0B 7450:4A0B
%	 6 2457:2A1B
%	 7 7450:4A0B
%Each column represents a filter that generates the single-element subset
%<code>{7450}</code>. [Note: however, they are reductible!]

\section{Color equivalence}

As a start-up, let's work with a simple but useful way to detect guess equivalence.

\subsection{Definitions}

\begin{definition}
(Color mask) A \emph{color mask}, denoted $\tau$, is a subset of the available colors. Since the number of colors in a game is small (6 for Mastermind and 10 for Bulls and cows), it is convenient to implement this subset as a bit-mask with present colors set to one. That's why we call it a mask here.
\end{definition}

Given a codeword $g$, define $\tau(g)$ to be the colors present in $g$. For example, $\tau(\cw{1442}) = \{1, 2, 4\}$.

Given a codeword set $\mathcal{S}$, define $\tau(\mathcal{S})$ to be the colors present in any of the codewords in $\mathcal{S}$. That is, $\tau(\mathcal{S}) = \bigcup_{g \in \mathcal{S}} \tau(g)$. For example, $\tau(\{\cw{1442},\cw{2315} \}) = \{ 1, 2, 3, 4, 5\}$.

of a codeword is the set of colors present in the codeword. The color mask of a set of codewords is the set of colors present in any of the codewords.

A few of the important color masks are as follows.

$\tau_{\text{all}}$ is the set of all colors.

$\tau_{\text{free}}$ is the set of colors that have never been guessed. $\tau_{\text{used}}$ is the set of colors that have been guessed. We have $\tau_{\text{free}} \cup \tau_{\text{used}} = \tau_\text{all}$.

$\tau_{\text{excl}}$ is the set of colors that are excluded from the potential secrets.

\subsection{Algorithm}

\newcommand{\cmall}{\tau_\text{all}}
\newcommand{\cmfree}{\tau_\text{free}}
\newcommand{\cmexcl}{\tau_\text{excl}}

Denote by $\mathcal{F}_k$ a color equivalence filter. It is characterized by a pair of color masks: the free/fresh/unguessed/uncalled colors and the excluded colors. That is,
\[
\mathcal{F} = (\tau_\text{free}, \tau_\text{excl}) .
\]

Three operations are:

\emph{Initialize}. At the beginning of the game, there are no constraints. A color equivalence filter is initialized by
\[
\cmfree = \cmall, \cmexcl = \phi .
\]

\emph{Filter}. For each candidate $g \in \mathcal{G}$, check whether $g$ is the lexical minimum of its equivalence class. We check this by replacing all excluded colors with the same symbol (e.g. *), and replace the unguessed colors using suitable mapping.

\emph{Restrict}. Given a constraint $(g,r)$ and the set of remaining possibilities $\mathcal{S}$, we construct a new filter as $\mathcal{F}' = (\cmfree', \cmexcl')$, where
\[
\cmfree' = \cmfree \setminus \tau(g), \cmexcl' = \cmall \setminus \tau(\mathcal{S})  .
\]

\section{Constraint equivalence}

Here the definition of \emph{constraint equivalence} is rather restrictive. Ideally, constraint equivalence should be the same as partition equivalence, but this is not the case here.

Introduced by \cite{neuwirth81,koyama93}.

[Constraint/filter equivalence]

It is easy to see that solving a game only depends on what colors have appeared before, and the relative peg position of the guesses. The particular colors or pegs guessed are not important. From this knowledge, we define the following.

\subsection{Definitions}

\begin{definition}
(Peg permutation) A \emph{peg permutation}, denoted $\pi_p$, permutes the pegs in a codeword. For example, the peg permutation
\[
\begin{pmatrix}
1 & 2 & 3 & 4 \\
3 & 1 & 2 & 4
\end{pmatrix}
\]
moves peg 1 to the 3rd place, peg 2 to the 1st place, peg 3 to the 2nd place, and peg 4 in its original place. The reordered sequence of the pegs is given by $\pi_p^{-1}$, which in this example is equal to
\[
\begin{pmatrix}
1 & 2 & 3 & 4 \\
2 & 3 & 1 & 4
\end{pmatrix} .
\]
\end{definition}

\begin{definition}
(Color permutation) A \emph{color permutation}, denoted $\pi_c$, permutes the colors in a codeword. For example, the color permutation
\[
\begin{pmatrix}
1 & 2 & 3 & 4 & 5 & 6 \\
5 & 2 & 1 & 6 & 3 & 4
\end{pmatrix} ,
\]
replaces color 1 by color 5, color 2 as is, color 3 by color 1, etc.
\end{definition}

\begin{definition}
(Partial color permutation) A \emph{partial color permutation} permutes a subset of colors. The set of colors not involved in the partial color permutation are called \emph{free colors} and denoted by $\tau$.
% , and leave the rest colors unchanged
%with the additional attribute that only a subset of the mappings are subject to future changes. These mappings are called \emph{free} mappings.
%, denoted $\Pi_c$, is an incomplete permutation of the colors where the mapping among some colors are fixed, but the mapping among the rest colors are not specified.
For example, the partial color permutation
\[
\begin{pmatrix}
%1^* & 2^* & 3^* & 4^* & 5^* & 6^*  \\
%1^* & 2^* & 3^* & 4^* & 5^* & 6^*
%1 & 2 & 3 & 4 & 5 & 6 \\
%\textbf{1} & 2 & 3 & 4 & 5 & 6 \\
%* & * & * & * & * & *
\underline{1} & \underline{2} & 3 & \underline{4} & 5 & \underline{6} \\
\underline{1} & \underline{2} & 5 & \underline{4} & 3 & \underline{6}
\end{pmatrix} .
\]
permutes (and \emph{restricts}) colors \cw{3} and \cw{5}, and leaves the free colors (marked with an underline) unchanged.
\end{definition}

A class of (fully-restricted) color permutations can be generated from a partial color permutation $\pi_c$ and its associated set of free colors $\tau$ by composing the partial permutation with every possible permutation of the free colors. We denote such class of color permutations by $\Pi_c = \pi_c \circ S_\tau$.

%A partial permutation in this context is actually a \emph{permutation group} [see appendix] formed by composing the fixed mappings with all permutations of the free colors. We can conveniently denote it as $\Pi_c = \pi_c \circ \tau$, where $\pi_c$ is the color mapping already fixed, and $\tau$ is the set of unmapped colors.

\begin{definition}
(Codeword permutation) A \emph{codeword permutation}, denoted $\pi$, is the combination of a peg permutation $\pi_p$ and a (partial) color permutation $\pi_c$. We write it as $\pi = \pi_p \circ \pi_c$. Note that the actual order of applying the peg permutation or the color permutation first does not matter.
\end{definition}

Given codeword $g$, we denote the permuted codeword under $\pi$ as $\pi(g)$ or $g^\pi$.
%Note that since a codeword usually does not contain all available colors, the mapping of unused colors does not matter when applying a color permutation to a given codeword.
To compute the image $h = (h_i)$ of a given codeword $g = (g_i)$ under a given permutation $\pi = \pi_p \circ \pi_c$, use the formula
\[
h_i = \pi_c\left(g_{\pi_p^{-1}(i)}\right) .
\]

%[Properties.] Some usual properties of permutations apply to codeword permutation as well.
%
%1) A codeword permutation is invertible, i.e.\ for any $\pi = (\pi_p, \pi_c)$, its inverse exists and is equal to $\pi^{-1} = (\pi_p^{-1}, \pi_c^{-1})$.

\begin{definition}
(Codeword equivalence) Two codewords $g$ and $h$ are \emph{equivalent} if and only if there exists a codeword permutation $\pi$ such that $g^\pi = h$.
\end{definition}

For example, \cw{1314} and \cw{5336} are equivalent because we can use the following (non-unique) permutation of pegs and colors to map \cw{1314} to \cw{5336}.
\[
\pi_p =
\begin{pmatrix}
1 & 2 & 3 & 4 \\
2 & 1 & 3 & 4
\end{pmatrix}
, \pi_c =
\begin{pmatrix}
1 & 2 & 3 & 4 & 5 & 6 \\
3 & 2 & 5 & 6 & 1 & 4
\end{pmatrix} .
\]

The equivalence relation defined above partitions a set of codewords into equivalence classes, where each equivalence class can be represented by an arbitrary element in that class, called a \emph{representative}.
%All elements in the same equivalence class are equivalent to this representative.
%; i.e.\ for all $g$, there exists $\pi$ such that $g = g_0^\pi$.

\begin{definition}
(Constraint equivalence) [cite a few references] Let $\mathcal{C}_1$ and $\mathcal{C}_2$ be two ordered sets of constraints of the same size $k$. Let the guesses in $\mathcal{C}_1$ be $(g_1,g_2,\cdots,g_k)$ and the guesses in $\mathcal{C}_2$ be $(h_1,h_2,\cdots,h_k)$. Then constraint sets $\mathcal{C}_1$ and $\mathcal{C}_2$ are \emph{equivalent} if there exists a codeword permutation $\pi$ such that
$g_i^\pi = h_i$ for all $1 \le i \le k$.
\end{definition}

The intuition about equivalent constraints are rather simple: if we rearrange the peg positions and relabel the colors, then the two sets of constraints become exactly the same. Hence for the purpose of devising a strategy, we only need to work with one of them and the other follows automatically.

Note, however, that there is a strong restriction in the above definition: the responses in the constraints are ignored. This has two implications. On the one hand, constraints such as
\[
\mathcal{C}_1 = \{ (1234:0A0B), (3456:2A0B) \}
\]
and
\[
\mathcal{C}_2 = \{ (1234:1A0B), (3456:0A2B) \}
\]
are considered equivalent, although they lead to drastically different potential secrets. On the other hand, constraints such as
\[
\mathcal{C}_1 = \{ (1234:0A0B), (1234:0A0B) \}
\]
and
\[
\mathcal{C}_2 = \{ (1234:0A0B), (4321:0A0B) \}
\]
are obviously not equivalent by definition, but from the first feedback \fb{0}{0} we could already exclude the colors \cw{1} -- \cw{4} from the potential secret, so any permutation of \cw{1234} should ideally be considered equivalent.

Despite the less-than-ideal properties of such definition, this equivalence relation is still commonly used \cite{neuwirth81,koyama93,francis10} because of its clarity, relative simplicity, and effectiveness for the first few guesses. A more powerful (but also more complex) equivalence definition will be introduced in the next section.

\subsection{Illustration}

To actually apply a constraint equivalence filter in practice, we implement an \emph{incremental filter}, as illustrated below.\footnote{The incremental filter described in this section is effectively the same technique used by \cite{francis10} to detect equivalence in the Bulls and cows game.}
An alternative, more generic method, which relies on \emph{graph automorphism}, will be introduced in the next section.

Given a set $\mathcal{C}$ of constraints and a set $\mathcal{G}$ of candidate codewords to filter for the next guess, we want to find all canonical codewords $g \in \mathcal{G}$ to be used as the next guess. There are a couple of ways to do this,\footnote{For example, one could \emph{generate} a list of canonical guesses from scratch, or \emph{filter} a supplied list of codewords for canonical guesses. Here we employ the second approach because of its simplicity, flexibility, and good performance in an implementation.}
and we employ a simple algorithm: traverse the set and keep the codewords that are lexically minimal in its equivalence class. That is, for each $g \in \mathcal{G}$, we find the lexically minimum codeword $g_0$ that is equivalent to $g$, and keep $g$ if and only if $g = g_0$.
%We call $g_0$ the \emph{canonical representative} of $g$.

Note that in order for the algorithm to work correctly, the lexically minimal codeword of each equivalence class must be present in the candidate set $\mathcal{G}$.\footnote{
This requirement is satisfied in most practical application. The algorithm could be slightly modified to remove this requirement: instead of removing a non-canonical element, it could employ a subset union algorithm to label the equivalence class of each element. After all candidates are processed, it can scan the labels to find the canonical elements.
}

To find the lexically minimum codeword equivalent to $g$, we could permute $g$ in all possible ways and check if any of the permuted image is smaller than $g$. For a Mastermind game, there are 4 pegs and 6 colors. The total number of permutations is therefore $4! \times 6! = 24 \times 720 = 17280$. This number is much larger in the Bulls and Cows game with 4 pegs and 10 colors, which is equal to $4! \times 10! = 87,091,200$.

Therefore we need to iterate the permutations in a more efficient way. We first conveniently classify the codeword permutations by their peg permutation, i.e.\ those with the same peg permutation are put into the same class. Then, as it will turn out shortly, the permutations in each class can be written as
\[
\pi_p \circ \pi_c \circ S_\tau ,
\]
where $\pi_p$ is the peg permutation that represents the class, $\pi_c$ is a partial color permutation restricted by the supplied constraints under this peg permutation, and $S_\tau$ is the group of all permutations of the set of free colors.

%We first find the canonical representative given each possible peg permutation of $g$. We then take the minimum of these minimum as the global minimum. [TBC]

Let's illustrate the algorithm with 4 pegs and 6 colors. We first list all $4! = 24$ permutations of the pegs, $\pi_p^1$ to $\pi_p^{24}$, where $\pi_p^1$ is the identity permutation.

Next, for each peg permutation $\pi_p^i$, we associate with it a class of \emph{eligible} color permutations, $\Pi_c^i = \pi_c^i \circ S_\tau$, where $\pi_c^i$ is the partial color permutation restricted by the supplied constraints under this peg permutation, and $\tau$ is the set of free colors.
%$\Pi_c$ hence denotes the set of all eligibal color permutations as the product of $\pi_c^i$ and all permutations of the colors in $\tau$.

At the beginning of the game, there are no constraints; all colors are free, and every partial color permutation is unrestricted. We can conveniently write $\pi_c \circ S_\tau$ as
%We can conveniently write an identity permutation in the set of eligible color permutations as
\[
%\Pi_c^i =
\begin{pmatrix}
%1^* & 2^* & 3^* & 4^* & 5^* & 6^*  \\
%1^* & 2^* & 3^* & 4^* & 5^* & 6^*
%1 & 2 & 3 & 4 & 5 & 6 \\
%\textbf{1} & 2 & 3 & 4 & 5 & 6 \\
%* & * & * & * & * & *
\underline{1} & \underline{2} & \underline{3} & \underline{4} & \underline{5} & \underline{6} \\
\underline{1} & \underline{2} & \underline{3} & \underline{4} & \underline{5} & \underline{6}
\end{pmatrix} ,
\]
where the underlined colors are controlled by $S_\tau$ and hence can be mapped freely.

To filter a set of codewords $\mathcal{G}$ to obtain canonical guesses, we check each codeword $g \in \mathcal{G}$ in order.\footnote{The particular order of traversal is not important, as long as the lexical minimum belongs to the set.}
Take for example $g = \cw{1223}$. We iterate through each peg permutation $\pi_p^i$ to permute $g$. Apply for example the following peg permutation,
\[
\pi_p =
\begin{pmatrix}
1 & 2 & 3 & 4 \\
2 & 1 & 3 & 4
\end{pmatrix} ,
%\]
%whose associated partial color permutation is initially
%\[
%\Pi_c =
%\begin{pmatrix}
%1^* & 2^* & 3^* & 4^* & 5^* & 6^*  \\
%1^* & 2^* & 3^* & 4^* & 5^* & 6^*
%\end{pmatrix} .
\]
followed by the (eligible) identity color permutation.
The permuted codeword is $g' = \cw{2123}$. Since $g' \succ g$, we cannot yet conclude that $g$ is not lexically minimum in its equivalence class. We proceed as follows.

We are free to map the free colors in $g'$ to any free color. Given our objective is to find the lexical minimum, we start from the leftmost peg of $g'$, which contains the color \cw{2}. This is a free color, and it should be mapped to the smallest available free color, \cw{1}, to achieve lexical minimum. This also means we have to map the color \cw{1} to something else temporarily because we cannot have two colors map to the same value. For convenience we map it to \cw{2}. Thus we apply the following color permutation to $g' = \cw{2123}$,
\[
\pi_c =
\begin{pmatrix}
\underline{1} & 2 & \underline{3} & \underline{4} & \underline{5} & \underline{6} \\
\underline{2} & 1 & \underline{3} & \underline{4} & \underline{5} & \underline{6}
\end{pmatrix} ,
\]
which yields $\pi_c(g') = \cw{1213}$.

Note that \cw{1213} is already lexically smaller than \cw{1223}, so we can conclude that \cw{1223} is not the lexical minimum of its equivalence class and can thus remove it from the candidates. However, for illustration purpose here we show a few more steps.

Now proceed to the color on the second peg of $g'$, \cw{1}. Again, \cw{1} is not mapped in the color permutation, so we are free to choose its image. And for the same reason of achieving minimal lexicographical value, we map it to the smallest available free color, which in this case is \cw{2}. The resulting color permutation is
\[
\pi_c =
\begin{pmatrix}
1 & 2 & \underline{3} & \underline{4} & \underline{5} & \underline{6} \\
2 & 1 & \underline{3} & \underline{4} & \underline{5} & \underline{6}
\end{pmatrix} ,
\]
and the permuted codeword is $\pi_c(g') = \cw{1213}$. Repeat this until all pegs in $g'$ are processed, and we find the lexical minimum to be \cw{1213}, which is smaller than $g$. We then remove $g$ from the candidate set.

Repeat the above steps for all peg permutations. If $g$ is lexically minimum in all these cases, then it is the lexical minimum of its equivalence class, and we can add it to the filtered set.

Above is an examine where all colors are free. Now suppose we have already have some constraints, so some colors are fixed. Without loss of generality, suppose that the constraint consists of just one guess $g_1 = \cw{3445}$. To find all canonical candidates for the second guess, we first need to \emph{restrict} the partial color permutations associated with each peg permutation.

Take for example
\[
\pi_p =
\begin{pmatrix}
1 & 2 & 3 & 4 \\
1 & 3 & 2 & 4
\end{pmatrix} ,
\Pi_c =
\begin{pmatrix}
\underline{1} & \underline{2} & \underline{3} & \underline{4} & \underline{5} & \underline{6} \\
\underline{1} & \underline{2} & \underline{3} & \underline{4} & \underline{5} & \underline{6}
\end{pmatrix} .
\]
The permuted first guess is $\pi(g_1) = \cw{3445}$. In order for the permuted constraint to be equivalent, the permuted guess must be equal to $g_1$. This means we must map \cw{3445} to \cw{3445} in the color permutation. Thus we must restrict the associated color permutation as
\[
\Pi_c' =
\begin{pmatrix}
\underline{1} & \underline{2} & 3 & 4 & 5 & \underline{6} \\
\underline{1} & \underline{2} & 3 & 4 & 5  & \underline{6}
\end{pmatrix} .
\]

After restricting the partial permutation, we can apply the previous steps again.
[The following example need to be changed to continue from the previous example] Suppose we come again to \cw{1123}. If we apply peg permutation $\pi_p^3$, the permuted representative becomes \cw{1213}. Now to find out what color permutations are eligible, note that the color 3 is already fixed in the partial permutation; only colors 1 and 2 are free to be mapped. We could therefore map them freely among the unrestricted colors, i.e.\ $\{1, 2, 6\}$, and mark all resulting codewords as equivalent to \cw{1123}.

Note that given a set of constraints, not all peg permutations can yield an eligible color permutation. For example, consider the peg permutation
\[
\pi_p =
\begin{pmatrix}
1 & 2 & 3 & 4 \\
1 & 2 & 4 & 3
\end{pmatrix} .
\]
When applied to $g_1 = \cw{3445}$, we get $\pi_p(g_1) = \cw{3454}$. However, there is no way whatsoever to map \cw{3445} to \cw{3454}, because \cw{4} would have to be mapped to both \cw{4} and \cw{5}, which is impossible. In this case, we conclude that this peg permutation is \emph{ineligible} and remove it from further consideration.

\subsection{Algorithm}

%Given a set of $k$ constraints,
%%$\mathcal{C}_k = \{ (g_i,r_i) \given 1 \le i \le k \}$,
%$\mathcal{C}_k = \{ (g_1,r_1), \cdots, (g_k, r_k) \}$,
%a \emph{constraint equivalence filter}, denoted $\mathcal{F}_k$, produces a short-list of canonical codewords for the next guess.

A constraint equivalence filter is characterized by the set of eligible codeword permutations which map existing constraints to themselves. These permutations
can be classified into $m$ classes according to their peg permutation. The permutations in the $i$th class can be written as the product of the peg permutation $\pi_p^i$, the associated partial color permutation $\pi_c^i$, and the symmetric group of the free colors $S_\tau$ (which is the same across all $i$).

Thus, we can write a constraint equivalence filter as
\[
\mathcal{F}_k = \bigcup_{i=1}^m \pi_p^i \circ \pi_c^i \circ S_\tau ,
\]
where $m$ is the number of eligible peg permutations.

Three operations are defined for a constraint equivalence filter:

\emph{Initialize}. At the beginning of the game, there are no constraints. Hence the initial filter, $\mathcal{F}_0$, is set to
\[
m = \text{number of pegs}!, \pi_c^i = (), \tau = \{1, 2, \ldots, c \}.
\]

\emph{Filter}. To filter a set $\mathcal{G}$ of candidate codewords for canonical guesses, we check each codeword $g \in \mathcal{G}$ in order.\footnote{The particular order of traversal is not important, as long as the lexical minimum belongs to the set.}
Given $g$, we check each eligible peg permutation in $\mathcal{F}$. For peg permutation $i$, we first permute $g$ to get $g' = \pi_p \circ \pi_c (g)$. If $g' \prec g$, then $g$ is not minimum. Otherwise, let $g' = (c_1, c_2, c_3, c_4)$. For each $j = 1$ to $4$, if $j \in \tau$, then map $j$ to the smallest color in $\tau$, etc.

%Formally, let $\mathcal{F} = \left\{(\pi_p^i, \pi_c^i \circ \tau) \given 1 \le i \le m \right\}$ be an incremental equivalence filter containing $m$ (partial) codeword permutations. Every partial color permutation has the property that some of the colors are restricted, while the rest are not. Let the set of free colors be $\tau_i$. As will show shortly, $\tau_i$ is the same across all permutations in a filter, so we can denote them by $\tau$. Also, for each $\Pi_c^i$, we pick a representitive $\pi_c^i \in \Pi_c^i$. Hence the filter can be written as
%\[
%\mathcal{F} = \left\{(\pi_p^i, \pi_c^i; \tau) \given 1 \le i \le m \right\} .
%\]

\emph{Restrict}. Given a filter $\mathcal{F}_k$ that satisfies all existing constraints $\mathcal{C}_k$. When a constraint $(g, r)$ is added, we construct a new filter $\mathcal{F}_{k+1} \subseteq \mathcal{F}_k$ by selecting the permutations in $\mathcal{F}_k$ that maps $g$ to itself. This is effectively restricting the partial color permutation associated with each peg permutation.
%so that
%$\pi_c^i ( \pi_p^i (g) ) = \pi_p^i (g)$ for all $\pi_c^i \in \Pi_c^i$.
%$\pi(g_k) = g_k$ for all $\pi \in \mathcal{F}_{k+1}$.

[TBC] It can be seen from the above requirement that any color that is mapped from in a partial color partition is also mapped to, and any color that is not mapped in one direction is also not mapped in the other direction. In fact, the unmapped colors are those \emph{unused} in any of the prior constraints. This means this equivalence filter fully considers the ``unguessed color equivalence'' described in the previous section.

As we proceed in the game, we are supplied with more constraints. The partial color permutation associated with each peg permutation gets more restrictive with each
added constraint, and more peg permutations becomes ineligible and gets removed. Finally we will be left with only the identity codeword permutation, where every codeword will become a representative of its own equivalence class.
%Hence we call it "incremental equivalence detection".

\subsection{Results}

Applying the constraint equivalence filter described in this section, we find 5 canonical guesses for the first round of a standard Mastermind game (p4c6r), listed in Table \ref{tab:canonical-mastermind}. Listed alongside is the number of canonical guesses in the second round given each initial guess.
\begin{table}[h]
\begin{center}
\begin{tabular}{c c}
\hline
\hline
$g_1$ & $\#\{g_2\}$ \\
\hline
\cw{0000} & 12 \\
\cw{0001} & 53 \\
\cw{0011} & 39 \\
\cw{0012} & 130 \\
\cw{0123} & 57 \\
\hline
\hline
\end{tabular}
\caption{Canonical 1st and 2nd guesses in Mastermind}
\label{tab:canonical-mastermind}
\end{center}
\end{table}

Applying the same filter to the Bulls and cows game (p4c10n) yields only one canonical initial guess, \cw{0123}, and 20 canonical guesses for the second round. They are listed in Table \ref{tab:canonical-bulls} along with the number of canonical guesses for the third round.\footnote{The table on page 7 of \cite{francis10} contains the same information; however their number for \cw{0456} is 373 and their number for \cw{4567} is 218.}
\begin{table}[h]
\begin{center}
\begin{tabular}{c c | c c | c c | c c}
\hline
\hline
$g_2$ & $\#\{g_3\}$ & $g_2$ & $\#\{g_3\}$ & $g_2$ & $\#\{g_3\}$ & $g_2$ & $\#\{g_3\}$ \\
\hline
\cw{0123} & 20  & \cw{0214} & 270  & \cw{1032} & 39  & \cw{1234} & 501  \\
\cw{0124} & 107 & \cw{0231} & 75   & \cw{1034} & 270 & \cw{1245} & 1045 \\
\cw{0132} & 67  & \cw{0234} & 501  & \cw{1045} & 295 & \cw{1435} & 541  \\
\cw{0134} & 270 & \cw{0245} & 1045 & \cw{1204} & 175 & \cw{1456} & 1012 \\
\cw{0145} & 295 & \cw{0456} & 363  & \cw{1230} & 59  & \cw{4567} & 180  \\
\hline
\hline
\end{tabular}
\caption{Canonical 2nd and 3rd guesses in Bulls and cows}
\label{tab:canonical-bulls}
\end{center}
\end{table}

[consistent with all prior guesses]

\section{Partition equivalence}

State/Partition Equivalence

This is the finest-grained definition of equivalence, but is complex in implementation.

\begin{definition}
(Partition) Let $(b_1, b_2, \cdots, b_r)$ be the ordered set of distinct feedbacks in a game. Then the \emph{partition} of a codeword set $\mathcal{S}$ by a codeword $g$ is a partition of $\mathcal{S}$ into ordered cells $(V_1, V_2, \cdots, V_r)$ such that the codewords in $V_i$ compare to $g$ yields $b_i$.
\end{definition}

\begin{definition}
(Partition equivalence.) Given two partitions $P_1 = (V_1, V_2, \cdots, V_r)$ and $P_2 = (V'_1, V'_2, \cdots, V'_r)$ of a codeword set $\mathcal{S}$. $P_1$ and $P_2$ are \emph{equivalent} if there exists codeword permutation $\pi$ such that $V_i^\pi = V'_i$ for all $1 \le i \le r$. That is, $\pi$ maps the cells in $P_1$ to the same cells in $P_2$. [color preserving permutation]
\end{definition}

\begin{definition}
(Guess equivalence.) Given a set of potential secrets $\mathcal{S}$, two guesses $g_1$ and $g_2$ are \emph{equivalent} if the partitions given by $g_1$ and $g_2$ on $\mathcal{S}$ are equivalent.
\end{definition}

[Can we show that partition equivalence is the \emph{finest} equivalence relation that preserves the \emph{structure} of a strategy tree?] Of course we need to define the motivation and what is a ``structure'' first.

\section{Spacial Equivalence}

The codewords can be thought of as points in a space. The comparison feedback between two codewords can be thought as the distance between the points. For a formal treatment on distance, see \cite{stuckman06}.

Can we define guess equivalence based on the guess's relative position to all the remaining secrets or the whole secret space?

For example, for Bulls and cows, at the beginning of the game all guesses are equivalent as the first guess. This is because the entire codeword set is completely symmetric.

However, for the Mastermind game, there are a few distinct initial guesses. It looks like right that these guesses have different relative spacial position relative to the codeword space.

Consider again when there is one secret left. Obviously [?] two guesses are equivalent if they yield the same feedback against this potential secret (if no repetition of digit is allowed). How about for Mastermind which allows repetition?

Also need to consider when two secrets are left, etc.

[And is such equivalence correct? Is it equivalent to partition equivalence? Is it weaker than partition equivalence? How to implement?]

\section{Chaining Multiple Filters}

[Note that constraint equivalence is strictly weaker(stronger?) than partition equivalence. For example, consider the first constraint 1234:0A0B. Then for the next guess, 1156 or 1256 are obviously the same (they both contain excluded colors 1, 2). However, 1156 and 1256 are clearly not equivalent under constraint equivalence. This is because constraint equivalence doesn't use the info of the feedback.

One way to deal with this is to use both. If two guesses are equivalent under any measure, then they are equivalent. This approach is discussed in this section.

Alternatively, we could combine the two. When there are a lot of possibilities, we compute constraint equivalence. When there are a few possibilities, we compute partition equivalence. This approach is discussed in ?? future sections.]

The equivalence filters described above vary in complexity and performance. More complex filters tend to produce fewer canonical guesses, but at the cost of higher computational overhead. If the computational overhead is too high, the benefit of filtering could be offset.

It is therefore useful in practice to chain multiple equivalence filters together by feeding the output of one filter as input into the next filter. This has two benefits: i) it potentially reduces the number of canonical guesses by testing different equivalence relations, and ii) it speeds up the computation of the latter filter by supplying potentially fewer candidates as input.\footnote{This relies on the assumption that the computational complexity of a filter grows with the number of candidates, which is satisfied for the filters described in the previous sections.}
Below we discuss some issues in implementing such a filter chain.

\begin{definition}
(Composite equivalence) Let $R_1$ and $R_2$ be two equivalence relations defined on the codeword set $\vS_0$. Then $R$ is a \emph{composite equivalence relation} of $R_1$ and $R_2$ if for any $g_1, g_2 \in \vS_0$, $g_1 R g_2$ if and only if $g_1 R_1 g_2$ or $g_1 R_2 g_2$. We denote it as $R = R_1 \vee R_2$. The $\vee$ operator is commonly known as the \emph{join} of the partitions induced by the two equivalence relations.\footnote{See \url{http://en.wikipedia.org/wiki/Lattice\_(order)}.}
\end{definition}

\begin{definition}
(Equivalence refinement) For two equivalence relations $R_1$ and $R_2$, $R_1$ is called \emph{finer} than $R_2$ (and $R_2$ \emph{coarser} than $R_1$) if any pair of elements that are equivalent under $R_1$ are also equivalent under $R_2$. It is easy to see that if $R_1$ is finer than $R_2$, then $R_1 \vee R_2 = R_2$.
\end{definition}

\begin{definition}
(Compatible relations) Let $R_1$ and $R_2$ be two equivalence relations defined on the set $S$. Then $R_1$ and $R_2$ are called \emph{compatible} if for any $g \in \vS$, $[g]_{R_1} \subseteq [g]_{R_2}$ or $[g]_{R_2} \subseteq [g]_{R_1}$. Intuitively, this means $R_1$ is a refinement or $R_2$ for part of the set, and $R_2$ is a refinement of $R_1$ for the rest part of the set. In particular, any equivalence relation is compatible with its refinement.
\end{definition}

\begin{definition}
(Discrete equivalence) A \emph{discrete} equivalence relation is an equivalence relation where every element is equivalent only to itself. A discrete equivalence relation is finer than any other equivalence relation defined on the same set.
\end{definition}

\begin{definition}
(Unit equivalence) A \emph{unit} equivalence relation is an equivalence relation where all elements are equivalent. A unit equivalence relation is coarser than any other equivalence relation defined on the same set.
\end{definition}

\begin{definition}
(Trivial equivalence) Discrete and unit equivalence relations are collectively called \emph{trivial} equivalence relations.
\end{definition}

\begin{definition}
(Simple filter) Let $R$ be an equivalence relation defined on a set $S$. A \emph{simple filter} of $\vS$ with respect to $R$ is a function that takes as input a subset $\vG \subseteq \vS$ and produces as output a set containing a representative for each class of equivalent elements in $\vG$.\footnote{Such output is known as the set of \emph{class representatives} of $\vG$; see \url{http://mathworld.wolfram.com/ClassRepresentative.html}. }
\end{definition}

One way to implement a composite equivalence filter is using \emph{disjoint sets}.\footnote{See \url{http://en.wikipedia.org/wiki/Disjoint-set\_data\_structure}.} However, this approach does not reduce the computational complexity of the second filter, and is not useful when one equivalence is finer than the other. In addition, the data structure is more complex to implement.

Therefore, we prefer to implement a composite filter by \emph{chaining} the individual filters. That is, given candidate set $\vG$, we first apply the first filter to get $\vG_1 = \vF_{R_1}(\vG)$, and then apply the second filter to get $\vG_2 = \vF_{R_2}(\vG_1)$. If we can ensure $\vG_2 = \vF_{R_1 \vee R_2}(\vG)$, then this method is a simple and efficient way to compute the composite.

However, arbitrary equivalence filters for arbitrary equivalence relations \emph{cannot} be chained in the above manner to produce the correct result. Let's look at a counter-example. Take a set of three elements, $X = \{1, 2, 3\}$, and take the following equivalence relations:
\begin{align}
R_1 &= \{ \{1, 2\}, \{3\}  \} , \notag \\
R_2 &= \{ \{1\}, \{2,3\}  \} . \notag
\end{align}
It follows that the composite equivalence relation is $R_1 \vee R_2 = \{ \{ 1, 2, 3 \} \}$.

Now suppose we use a ``natural'' filter $\vF$ that keeps elements in the input sequence that are not equivalent to any of the prior elements. Then we have $\vF_{R_1}(X) = \{ 1, 3 \}$. If we in turn feed this output to the next filter, we get $\vF_{R_2}(\{1,3\}) = \{1,3 \}$. However, this result is not desired because $\vF_{R_1 \vee R_2}(X) = \{1\}$.

This counter-example can be generalized to show that for arbitrary choices of equivalence relations and simple filters, we are not guaranteed to get the desired result by simply chaining individual filters. This is formalized by the following theorem.

\begin{theorem}
Let $R_1$ be a non-trivial equivalence relation defined on a set $\vS$, and let $\vF_{R_1}$ be a simple filter. Then there exists equivalence relation $R_2$ and set $\vG \subseteq \vS$ such that for any simple filter $\vF_{R_1 \vee R_2}$ and $\vF_{R_2}$, $\vF_{R_1 \vee R_2}(\vG) \ne \vF_{R_2}(\vF_{R_1}(\vG))$.
\end{theorem}

\begin{proof}
Since $R_1$ is non-trivial, there must exist a set of three distinct elements $\vG = \{ g_1, g_2, g_3 \} \subseteq \vS$ such that $R_1$ partitions $\vG$ as
\[
\vG / R_1 = \{ \{ g_1, g_2 \}, \{ g_3 \} \} .
\]
It follows that $\vF_{R_1} (\vG) = \{ g, g_3 \}$ where $g$ is one of $g_1$ and $g_2$.
Now define equivalence relation $R_2$ on $\vS$ as
\[
\vS / R_2 = \{ \{g\}, \vS \setminus \{g\} \} .
\]
It is easy to see that $R_1 \vee R_2$ is the unit equivalence $\{ \vS \}$, so $\vF_{R_1 \vee R_2}(\vG)$ contains only one element. However, $\vF_{R_2}(\vF_{R_1}(\vG)) = \vF_{R_2}(\{ g, g_3 \}) = \{ g, g_3 \}$.
\end{proof}

The above theorem implies that for a non-trivial equivalence relation $R_1$, it is impossible to implement a ``universal'' simple filter that can be chained to an arbitrary equivalence relation $R_2$. However, for specific choices of $R_2$, in particular those compatible with $R_1$, this actually is possible. This is shown by the following theorem.

\begin{theorem}
Let $R_1$ and $R_2$ be two compatible equivalence relations defined on a set $\vS$, and let $\vF_{R_1}$, $\vF_{R_2}$, and $\vF_{R_1 \vee R_2}$ be simple filters. Then $\vF_{R_1 \vee R_2}(\vG) = \vF_{R_2}(\vF_{R_1}(\vG))$ for all $\vG \subseteq \vS$.
\end{theorem}

This is quite obvious, so we are not going to elaborate the proof here.

\begin{theorem}
Let $R_1$ and $R_2$ be two non-compatible equivalence relations defined on a set $\vS$, and let $\vF_R$ denote a simple filter for $R$. Then for any $\vF_{R_2}$, there exists $\vF_{R_1}$ and $\vG \subseteq \vS$ such that $\vF_{R_2}(\vF_{R_1}(\vG)) \ne \vG / (R_1 \vee R_2)$.
\end{theorem}

\begin{proof}
Since $R_1$ and $R_2$ are not compatible, there must exist a set of three distinct elements $\vG = \{ g_1, g_2, g_3 \} \subseteq \vS$ such that $R_1$ and $R_2$ partitions $\vG$ as
\[
\begin{array}{ r l }
R_1: & \{ g_1, g_2 \}, \{ g_3 \} , \\
R_2: & \{ g_1 \}, \{ g_2, g_3 \} .
\end{array}
\]
It is easy to see that $g_1, g_2, g_3$ are all equivalent under $R_1 \vee R_2$, so $\vG / (R_1 \vee R_2)$ contains only one element.
Now define $\vF_{R_1} (\vG) = \{ g_1, g_3 \}$. It then follows that
\[
\vF_{R_2}(\vF_{R_1}(\vG)) = \vF_{R_2}(\{ g_1, g_3 \}) = \{ g_1, g_3 \} \ne \vG / (R_1 \vee R_2) .
\]
\end{proof}

%The above theorem shows that natural filters cannot be chained for arbitrary equivalence relations and arbitrary input. However, for specific pairs of equivalence relations, they may still be chained. In particular, if one equivalence is finer than the other, then it is easy to see that they can be chained.\footnote{This is why we require $R_1$ to be non-trivial in the above theorem, since a discrete relation is finer than any relation, and any relation is finer than a unit relation.}

The counter-example and the theorem above means in order to chain filters on arbitrary relations, we must impose additional restriction on how a filter returns representative elements. For practical usefulness, we define a class of \emph{canonical} filters which satisfy this.

\begin{definition}
(Canonical filter) A \emph{canonical filter} is an equivalence filter that only returns representatives that are minimum in their respective equivalence classes. Formally, let $\prec$ be a total order on the set $\vS_0$ of all codewords.\footnote{A convenient example of such total order is the lexicographical order.}
Let $R$ be an equivalence relation that partitions $\vS_0$ into $r$ cells, where the minimum element (with respect to $\prec$) of the $i$th cell is denoted by $m_i$. Then a \emph{canonical filter}, $\vF$ is a function $\vS_0 \rightarrow \vS_0$ defined by
\begin{equation}
\vF(\vG) = \vG \cap \left\{ m_i \given 1 \le i \le r \right\} \text{ for any } \vG \subseteq \vS_0 . \label{eq:canonical-filter-1}
\end{equation}
In particular, if some $m_i$ is not in $\vG$, then its equivalence class is excluded from the output.
\end{definition}

Next we show that we can safely chain canonical filters together to produce the desired output efficiently.

\begin{theorem}
Let $\mathcal{F}_1$ and $\mathcal{F}_2$ be the canonical filters for equivalence relations $R_1$ and $R_2$. Let $\vF$ be the canonical filter for equivalence relation $R = R_1 \vee R_2$. Then $\vF(\vG) = \vF_2(\vF_1(\vG))$ for any $\vG \subseteq \vS_0$.
\end{theorem}

\begin{proof}
Before going into details, we illustrate the idea with an example of 7 codewords. A $\times$ sign indicates a representative of one of the canonical filters, and a $\otimes$ sign indicates a representative of both filters.
\[
\begin{array}{r c c c c c c c }
      & g_1 & g_2 & g_3 & g_4 & g_5 & g_6 & g_7 \\
\vF_1: & \otimes &        & \times & \otimes & & \times & \\
\vF_2: & \otimes & \times &        & \otimes & &        & \times
\end{array}
\]

Formally, let $\vu = (u_1, \cdots, u_r)$ and $\vv = (v_1, \cdots, v_s)$ be the ordered set of minimum representatives of $\vS_0 / R_1$ and $\vS_0 / R_2$ respectively. According to equation \eqref{eq:canonical-filter-1}, we have
\[
\vF_1(\vG) = \vG \cap \vu, \vF_2(\vG) = \vG \cap \vv.
\]
Let $\vw = \vu \cap \vv = (w_1, \cdots, w_t)$. We now show that $\vw$ is the set of minimum representatives of $\vS_0 / (R_1 \vee R_2)$.

First, it is obvious that any $w_{k_1}, w_{k_2} \in \vw$ are not equivalent under $R_1$ or $R_2$. Otherwise, suppose $w_{k_1} \sim w_{k_2}$ under $R_1$, then the corresponding elements $u_{i_1}, u_{i_2} \in \vu$ are equivalent under $R_1$, which is contradictory to the definition of $\vu$.

Next, we show that any given $w \in \vS_0$ is equivalent to some $w_k \le w$ under $R = R_1 \vee R_2$. Given $w$, there exists $u_i \le w$ and $v_j \le w$ which are equivalent to $w$ under $R_1$ and $R_2$ respectively. By definition of $R$, $w$ is equivalent to $u_i$ and $v_j$ under $R$ as well. If $u_i = v_j$, then $w \in \vw$ and the statement is true. Otherwise, suppose without loss of generality that $u_i < v_j$. Since $w \sim u_i$ under $R$, we just need to show that $u_i$ is equivalent to some $w_k \le u_i$. Repeat this process until some $u_i = v_j$, or we come to $u_1 = v_1 = \min \vS_0$, where the statement trivially holds.

The above two paragraphs show that $\vw$ is the minimum representative of $R$. [Still need to show the $R$ defined this way satisfies $R = R_1 \vee R_2$?] It then follows that for any $\vG \subseteq \vS_0$,
\[
\vF(\vG) = \vG \cap \vw = \vG \cap (\vu \cap \vv ) = (\vG \cap \vu) \cap \vv
= \vF_2(\vF_1(\vG)) .
\]

\end{proof}

%However, if each filter in implemented in a way such that it keeps a specific element as the representative, such as the lexical minimum of its equivalence class of each particular equivalence, then chaining them together may incorrectly drop out canonical guesses. This section discuss the conditions under which chaining them together will still produce correct results.


\subsection{MNIST dataset}

We tested RSOM on the standard MNIST dataset \citep{Lecun:1998} that contains $60,000$ training images and $10,000$ testing images. The dimension of each image is 28$\times$28 pixels and they are encoded using grayscale levels as the result of the normalization of the original black and white NIST database. The standard performance on most algorithms on the MNIST dataset is below 1\% error rate (with or without preprocessing) while for the regular SOM it is around 90\% recognition rate depending on the initial size, learning rate, and neighborhood function. Our goal here is not to find the best set of hyper-parameters but rather to explore if SOM and RSOM are comparable for a given set of hyper-parameters. Consequently, we considered a size of 32$\times$32 neurons and used the entire training set (60,000 examples) for learning and we measured performance on the entire testing set. We did not use any preprocessing stage on the image and we fed directly each image of the training set with the associated label to the model. Labels (from 0 to 9) have been transformed to a binary vector of size 10 using one-hot encoding (e.g. label 3 has been transformed to 0000001000). These binary labels can then be learned using the same procedure as for the actual sample. To decode the label associated to a code word, we simply consider the argmax of these binary vectors. Figure \ref{fig:MNIST:results} shows the final self-organisation of the RSOM where the class for each cell has been colorized using random colors. We can observe a number of large clusters of cells representing the same class (0, 1, 2, 3, 6) while the other classes (4,5,7,8,9) are split in two or three clusters. Interestingly enough, the codewords at the borders between two clusters are very similar. In term of recognition, this specific RSOM has an error rate just below 10\% ($0.903,\, \pm 0.296$) which is quite equivalent to the regular SOM error rate ($0.906,\, \pm 0.292$). The perfomances of the RSOM and SOM are actually not significantly different, suggesting that the regular grid hypothesis can be weaken.

In a similar way we measured the similarity of the neural spaces generated by both the regular SOM and the RSOM using the persistent diagram and barcodes. The only significant difference from previous analysis was the projection of the input and neural spaces to a lower-dimension space via UMAP \citep{Mcinnes:2018}. Projections of high-dimensional spaces to lower-dimension ones have been used before in the analysis of latent spaces of autoencoders \citep{Detorakis:2019}. Here, we use the UMAP since it's an efficient and robust method for applying a dimensionality reduction on input and neural spaces. More precisely, we project the MNIST digits as well as the code words (dimension $784$) to a space of dimension $7$. Once we get the projections, we proceed to the topological analysis using the persistent diagram and barcodes as we already have described in previous paragraphs.
Figure~\ref{fig:MNIST:analysis} shows the results regarding the persistent barcodes and diagrams.
The persistent barcodes in figures~\ref{fig:MNIST:analysis}A, B, and C indicate that RSOM captures more
persistent features (panel C, orange and green lines reflect the $H1$- and $H2$-homological features, respectively)
than the regular SOM (panel B). The persistence diagrams of input, SOM and RSOM are shown in
figures~\ref{fig:MNIST:analysis} D, E, and F, respectively.
These figures indicate that the RSOM has more persistent features (orange and green dots away from the diagonal
line) than the regular SOM, consistently with the two previous experiments ($2$D uniform distribution with holes
and $3$D uniform distribution). The Bottleneck distance between the persistence diagrams of input space and
those of SOM and RSOM for the $H0$ are SOM: $1.0$ and RSOM: $1.12$, for $H1$ SOM: $0.19$ and RSOM: $0.22$, and
finally for the $H2$ are SOM: $0.05$ and RSOM:$0.05$. Again we observe that the regular SOM has a persistent
diagram that is closer to the one of the input space than that of RSOM, however the RSOM seems to approache
slightly better the input space topology since it has more pairs (birth, death) away from the diagonal (black
line) in figures~\ref{fig:MNIST:analysis}D, E, and F. Moreover, the persistent barcode of RSOM (figure~\ref{fig:MNIST:analysis}C indicates that has more persistent features for the radius $\alpha$
between $0$ and $1.512$ than the regular SOM.

\begin{figure}
  \includegraphics[width=\columnwidth]{experiment-MNIST.pdf}
  \vspace{2mm}
  \centering
  \includegraphics[width=.975\columnwidth]{figures/colormap.pdf}
  %
  \caption{%
  %
  {\bfseries \sffamily MNIST dataset (results)}
  %
  Randomized SOM made of $1024$ neurons with a $3$-nearest neighbors induced topology. Model has been trained for $25,000$ epochs on the MNIST dataset. \textbf{A} Map topology in neural space. \textbf{B} Map topology in data space. \textbf{C to H} Normalized distance map for six samples. Normalization has been performed for each sample in order to enhance contrast but this prevents comparison between maps.
  %
  }
  \label{fig:MNIST:results}
\end{figure}

\begin{figure}
  \centering
  \includegraphics[width=\textwidth]{experiment-MNIST-analysis.pdf}
  \caption{{\bfseries \sffamily MNIST dataset (analysis)}
  Persistent Barcodes of \textbf{A} input space, \textbf{B} SOM, and \textbf{RSOM}.
  The blue, orange, and green line segments represent the $H0$-, $H1$-, and $H2$-homology,
  respectively. This means
  that blue color represents connected segments within the space, orange color reflects the holes
  within the space and green the voids. The longer the line segment the more important the
  corresponding topological feature. \textbf{D} illustrates the persistent diagram for the input space.
  \textbf{E} and \textbf{F} depict the persistent diagrams for SOM and RSOM, respectively. Again blue dots
  indicate $H0$-homology features, orange dots represent $H1$-homolocical features, and green the
  $H2$-homological features.}
  \label{fig:MNIST:analysis}
\end{figure}


%%% \footnote{Anecdotally, with proof-to-code ratios of 6:1
%%% instead of 50:1 or 100:1 with classical proof assistants~\citep{Ironfleet}},

%%% In present of lambda expressions in
%%% the specifications, our translation
%%% can (optionally) insert extensionality
%%% axioms that express $\alpha$- and
%%% $\beta$-equivalence in the logic,
%%% but do not preserve undecidability
%%% and thus unpredictability of type checking.

Structure
* Introduction
* Overview
* Refinement Reflection
* Algorithmic Checking
* Proof Combinator Library
* Evaluation
* Related Work
--------------------------------------------------------------------------------
Dependently typed languages like demonstrate the
immense potential of writing proofs \emph{of}
programs, \emph{by} programs.
%
They enable developers to \emph{specify} correctness
properties and to \emph{verify} those properties, by
writing both the propositions and the proofs as
ordinary terms in the language, thereby uniting
programming and proving into complementary parts
of a whole.
%
Unfortunately, so far, these benefits require one
to work in one of these specially designed languages.
%
Wouldn't it be great to program proofs and write
proofs of programs in \emph{your} favorite language
and libraries, and have them be built and executed
using \emph{your} favorite compilers and run-times?

We used \libname as an assistant
to state and prove theorems
of Haskell functions
on Integers and inductive data types (Lists and Maybe).
%
In this section, we present by examples how \libname
can be used to prove properties on linear arithmetic
(\eg @fib@ is increasing), higher order properties on
integers and inductive data types (\eg generalization
of increasing property and map fusion), and interpretation
of $\lambda$-terms (\eg associativity of monadic bind).

\spara{Proving Strategies.}
%
The proofs reply on four main proving strategies.
We use color notations to make the proofs more readable.
%
\begin{itemize}
\item\textbf{Theorem application.}
We prove
@f op! g ?  thm e1 .. en@
when the theorem @thm x1 ... xn@
is of the form $(\ttf\ \op ! \ \ttg) \subst{\overline{x}}{\overline{e}}$.
\item \textbf{Definition folding.}
Folding the definition of @f e1 ... en == g@ once
proves that
@rf e1 ... e2 ==! g@
(in which case we color @f@ with red).
%
\item \textbf{Definition unfolding.}
Unfolding the definition of @f e1 ... en == g@ once
proves that
@g ==! gf e1 ... e2@
(in which case we color @f@ with green).
\item \textbf{SMT knowledge}
We rely on SMT interpretations to prove
linear arithmetic properties (\eg @1 < 2@)
or $\eta$-reduction (\eg @e = (\x -> e) x@).
\end{itemize}

\subsection{Arithmetic Properties}
%
We start by proving arithmetic properties
of Haskell functions.  @fib_incr@ states
and proves that the @fib@ function
from \S~\ref{sec:intro} is increasing.
%
\begin{code}
  fib_incr :: n:Nat -> {fib n <= fib (n+1)}
  fib_incr n
    | n == 0
    =   rfib 0
    <!  gfib 1
    *** QED
    | n == 1
    =   fib 1
    <=! fib 1 + fib 0
    <=! gfib 2
    *** QED
    | otherwise
    =   rfib n
    ==! fib (n-1) + fib (n-2)
    <=! fib n + fib (n-2) ? fib_incr (n-1)
    <=! fib n + fib (n-1) ? fib_incr (n-2)
    <=! gfib (n+1)
    *** QED
\end{code} %$
Proofs are total and terminating Haskell functions.
@fib_incr@ proves that @fib n@ is increasing using three cases.
1. If @n==0@,
  then by unfolding @fib@ we get @fib 0 == 0@.
  SMT proves that $0 < 1$, which in turn is folded to @1 == fib 1@.
2. If @n==1@, since @fib@ always returns @Nat@, SMT proves that @fib 1 <= fib 1 + fib 0@
 which is folded to @fib 2@.
3. Otherwise,
  @fib n@ is unfolded to @fib n = fib (n-1) + fib (n-2)@.
  We recursively apply the theorem to the (provably) smaller arguments @n-1@ and @n-2@,
  and fold the result to get @fib n + fib (n-1) = fib (n+1)@.

\subsection{Higher Order Theorems}\label{subsec:higherorder}
Next, we state and prove a generalization of increasing functions.
Namely, that for every function @f@,
if there is a proof that @f@ is increasing, that is,
an expressions with type @z:Nat -> {f z <= f (z+1)}@,
then, for every @x:Nat@ and @y@ greater than @x@,
we prove that @f x <= f y@.
%
\begin{code}
  type Greater N = {v:Int | N < v}

  gen_incr :: f:(Nat -> Int)
           -> (z:Nat -> {f z <= f (z+1)})
           -> x:Nat
           -> y:Greater x
           -> {f x <= f y}
           / [y]
  gen_incr f thm x y
    | x + 1 == y
    =   f x
    <=! f (x+1) ? thm x
    <=! f y
    *** QED
    | x + 1 < y
    =   f x
    <=! f (y-1) ? gen_incr f thm x (y-1)
    <=! f y     ? thm (y-1)
    *** QED
\end{code}
%
We prove the theorem by induction on @y@,
which is stated by the termination metric~\citep{Vazou15} @[y]@.
%
If @x+1 == y@, then we apply the @thm@ argument.
Otherwise, @x+1<y@ and the theorem holds by induction on @y@.

We use the above theorem to directly prove
that @fib@ in increasing with the generalized notion:
%
\begin{code}
  fib_incr_gen :: n:Nat
               -> m:Greater n
               -> {fib n <= fib m}
  fib_incr_gen = gen_incr_eq fib fib_incr
\end{code}

\RJ{FIXME, moved from examples}
\subsection{Example: Reflecting Lists} \label{subsec:list}

%
In the rest of this section, we use \libname to prove
properties about a user defined inductive List data type
\begin{code}
  data L [length] a = N | C a (L a)
\end{code}
%
The annotation @[length]@ on the List definition
states that \liquidHaskell will use the @length@ of lists
as a default termination metric on functions inductive on lists.
%
Where @length@ is defined as expected to map lists to natural numbers
\begin{code}
  measure length
  length :: L a -> Nat
  length N        = 0
  length (C _ xs) = 1 + length xs
\end{code}
%
The @measure@ definition (as defined in~\citep{Vazou14})
translates the @length@ function to refinements in the list's
data constructors.
%
Specifically, the above definition strengthens list's data constructors
as
\begin{code}
 N :: {v:L a | length v == 0 }
 C :: x:a -> xs:L a
   -> {v:L a | length v == 1 + length xs }
\end{code}

Moreover, the \liquidHaskell flag @exact-data-constructors@,
automatically generates the
logical record selectors and checkers for lists.
%
Specifically, the following measures will get generated:
\begin{code}
  is_N N          = True
  is_N (C _ _)    = False

  is_C (C _ _)    = True
  is_C N          = False

  sel_C_1 (C x _) = x
  sel_C_2 (C _ x) = x
\end{code}

\subsection{Append Associativity}\label{subsec:append}
Next we recursively define and axiomatize list append
\begin{code}
  axiomatize (++)
  (++) :: L a -> L a -> L a
  N        ++ ys = ys
  (C x xs) ++ ys = C x (xs ++ ys)
\end{code}
%
The above axiomatization, will automatically produce a type for append
that exactly captures its implementation.
%
\begin{code}
  (++) :: xs:L a -> ys:L a ->
    {v:L a |
       v == if (is_N xs) then ys
            else C (sel_C_1 xs)
                   (sel_C_2 xs ++ ys) }
\end{code}
%
The formal translation of arbitrary terminating functions
is formalized in \S~\ref{sec:algorithmic}.


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{tfrpage}
\section*{\hspace*{-1.6cm} tfrpage}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Page time-frequency distribution.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[tfr,t,f] = tfrpage(x)
[tfr,t,f] = tfrpage(x,t)
[tfr,t,f] = tfrpage(x,t,N)
[tfr,t,f] = tfrpage(x,t,N,trace)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty tfrpage} computes the Page distribution of a discrete-time
        signal {\ty x}, or the cross Page representation between two
        signals. The expression of the Page distribution is
\begin{eqnarray*}
P_x(t,\nu) &=& \dfrac{d[|\int_{-\infty}^t x(u)\ e^{-j2\pi \nu u}\
du|^2]}{dt}\\
 &=& 2\ \Re{\left\{x(t)\ \left(\int_{-\infty}^t\ x(u)\ e^{-j2\pi \nu
u}du\right)^* \ e^{-j2\pi \nu t}\right\}}.
\end{eqnarray*}

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8cm} c}
Name & Description & Default value\\
\hline
        {\ty x}     & signal if auto-Page, or {\ty [x1,x2]} if cross-Page
			({\ty Nx=length(x)})\\
        {\ty t}     & time instant(s) & {\ty (1:Nx)}\\
        {\ty N}     & number of frequency bins  & {\ty Nx}\\
        {\ty trace} & if nonzero, the progression of the algorithm is shown
                                          & {\ty 0}\\
     \hline {\ty tfr}   & time-frequency representation\\
        {\ty f}     & vector of normalized frequencies\\

\hline
\end{tabular*}
\vspace*{.2cm}

When called without output arguments, {\ty tfrpage} runs {\ty tfrqview}.
\end{minipage}
\vspace*{1cm}

{\bf \large \sf Example}
\begin{verbatim}
         sig=fmlin(128,0.1,0.4);
         tfrpage(sig);
\end{verbatim}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
all the {\ty tfr*} functions.
\end{minipage}
\vspace*{.2cm}

{\bf \large \sf References}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
[1] C. Page ``Instantaneous Power Spectra'' J. Appl. Phys., Vol. 23,
pp. 103-106, 1952.  \\

[2] O. Grace ``Instantaneous Power Spectra'' J. Acoust. Soc. Am., Vol. 69,
pp. 191-198, 1981.
\end{minipage}


%&LaTeX

\section{Hello, Digital!}

In this lab, you will investigate how capturing an analog signal for
computer use --- sampling and quantization --- modifies the signal,
seeing how the choices you make in the parameters for sampling and
quantization affect the quality of the digitized, computer signal.

\subsection{Sampling}

The first step in digitization is \emph{sample and hold}, in which the
continuous analog signal is converted to a discrete-time analog signal
(an analog signal that only changes its value at particular points in
time). You will use the \texttt{samplehold} method to do this:
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily\small]
% samplehold Perform a sample and hold function on an AnalogSignal
% Usage:
%  x = obj.samplehold(h)
% where  obj = AnalogSignal
%        h = hold time in sec (sampling interval)
%        x = resultant sampled AnalogSignal
\end{lstlisting}

\paragraph{Step 1.1} Create an analog sine waveform ranging from -5 to
5V with a frequency of 200Hz and a duration of 2 seconds. Produce a
plot with X-axis limits set to make the waveform visible (i.e., don't
just make a 2s plot that tries (and fails) to show 400 cycles of the
sinusoid.

\paragraph{Step 1.2} Use the \texttt{samplehold} method to produce
sampled versions of this signal at 300Hz, 500Hz, 1000Hz, and
2000Hz. Use the Matlab \texttt{subplot} command, and the
\texttt{discreteplot} function provided with this class's Matlab code,
to plot the original and all four sampled signals together. One of the
things that you will note from these plots is that the X axes of these
plots do not have units of continuous time; their units are \emph{not}
seconds. Instead, the X axis values are sample count, starting with
sample 1. You will want these plots to show their signals over the
same time duration, and so you'll need to choose that duration,
then figure out, for each sampled signal, how many samples correspond
to that duration, and then set the X axis limits for each figure so
that the plots show equal durations.

Clearly, the results are not the same, and none look identical to the
original sine wave. What are the two essential pieces of information
about a sine wave that need to be preserved when sampling it? Does it
appear that all sampled versions are equally useful in achieving this?
Why or why not (in other words, your answer to this question should
not be just ``yes'' or ``no'')?

\paragraph{Step 1.3} Let's look at aliasing in a little more detail
and with a lot more numerical precision. You'll recall from the text
that, once we sample a signal, we have limited the range of
frequencies that we can represent in our discrete signal to the range
$0 \leq \hat{\omega} \leq \pi$, corresponding to a range of apparent
frequencies in the physical world of $0 \leq \omega' \leq \omega_s/2$
(or $0 \leq f' \leq f_s/2$). Any frequency in the original signal
above $f_s/2$ will be \emph{aliased} into the range of possible
apparent frequencies. To keep things simple, we'll stick with a
sinusoid; this time, make it 10Hz with an amplitude of -1 to +1 and a
duration of 1s. This will make it easy to count cycles when
plotted. Set up a figure that can hold three plots and plot this
analog signal in the top plot.

\paragraph{Step 1.4} Sample this signal at 25Hz and use the
\texttt{discreteplot} function to plot the sampled signal in the
middle. Sample it at 15Hz and similarly plot that sampled signal at
the bottom.

\paragraph{Step 1.5} Before examining the plots in detail, answer the
following questions: For each of the two sampling frequencies, what is
the range of apparent frequencies that can be represented? For each,
will a sinusoid with $f = 10$Hz be aliased? If so, what will be the
digital frequency and the apparent frequency of a 10Hz sinusoid?

\paragraph{Step 1.6} Examine the plots and count the number of
up-and-down cycles in each. Don't worry that each cycle doesn't look
the same, or that every other cycle seems different; just count
each. You should see 10 cycles in the top graph; how many do you see
in the middle and bottom? How does this compare to the theory you
discussed in the previous step? If there is any discrepancy, explain it.

\subsection{Analog to Digital Conversion}

The last step of digitization is called ``analog to digital
conversion,'' or \emph{quantization}. In this step, the sampled analog
signal is converted to a discrete signal, with values represented by
$b$ bit integers. We will use the \texttt{quant} function provided
with this class's Matlab code to perform this conversion:
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily\small]
% QUANT Quantize a sampled (discrete) signal using a prescribed
%       number of bits per point.
% Usage:
%  y = quant(x,nb,out)
% where y = digital signal quantized to 2^(nb) bits resolution
%       x = vertical points of sampled signal
%       nb = number of bits to use per point
%      out = 'raw' means output binary values: 0,...,2^(nb-1)
%      otherwise, set ouput value range = input value range
\end{lstlisting}

\paragraph{Step 2.1} Write a Matlab function that compares two signals
by computing the \emph{signal to noise ratio} (SNR) that results from
changing one into the other (by quantization). Your function should do
this by first computing \emph{root mean squared} (RMS) error between
the two. In this case, you will be comparing a sampled signal with a
quantized version of it.  To do this, you should subtract the
quantized signal from the sampled signal to produce a vector of
differences (errors), then square each difference value using the
MATLAB \verb|.^| operator (to get squared errors), then take the mean
of these squared values using the MATLAB \verb|mean| function (mean
squared error --- a scalar value), and finally take the square root of
that scalar result (root mean squared error). We can compute the RMS
signal in a similar way, by squaring the signal values, getting the
mean of those squared values, and then taking the square root. The
final result should be a single value --- the signal-to-noise ratio
(SNR) in \emph{decibels}. What is in the numerator and what is in the
denominator of this ratio? Note that this is different than what we
did in the textbook, because we are now doing the computation for a
\emph{specific} signal, not just figuring SNR for a possible
\emph{range} of signal values.

  \paragraph{Step 2.2} Use your code to compute the SNR for a
  quantized sinusoid. Generate an analog signal with with a range of 0
  to 5, frequency of 10Hz, and duration 2sec. Sample it at 25Hz. Use
  2, 4, 8, 12, and 16 bits quantization, and plot SNR on the Y-axis
  versus number of quantization bits on the X-axis.

  \paragraph{Step 2.3} Repeat Step 2.2 using a square waveform with
  the same parameters.

  \paragraph{Step 2.4} Repeat Step 2.2 using a triangle waveform with
  the same parameters.

  \paragraph{Step 2.5} As you double the number of bits used in
  quantization, how does the SNR change? How does this compare to what
  your learned from the textbook? Refer to specific features of your
  plots from Steps~2.2--2.4 to justify your answer.


%---------------------------Shape-----------------------------
\section{Shape\label{s:tri-shape}}

Let $C$ be the condition number as defined in \S\ref{s:tri-condition}.
Then the shape metric is simply
\[
  q = \frac{1}{C}
\]

\trimetrictable{relative size squared}%
{$1$}%                                                Dimension
{$[0.25,1]$}%                                         Acceptable range
{$[0,1]$}%                                            Normal range
{$[0,1]$}%                                            Full range
{$1$}%                                                Unit equilateral triangle value
{\cite{knu:03}}%                                      Reference(s)
{v\_tri\_shape}%                            Verdict function name


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\geometry{a4paper}
\usepackage{amsmath}
\usepackage{physics}
\usepackage{color,soul}
\DeclareMathOperator{\di}{d\!}
\newcommand*\Eval[3]{\left.#1\right\rvert_{#2}^{#3}}
\newcommand{\inlist}[1]{\texttt{#1}}

\author{J L Kaplan}

\begin{document}

\section*{SICP Exercise 1.19}

\subsection*{Derivation of $p'$ and $q'$}
First transformation:
\begin{equation}
  a_{n+1} \leftarrow q \left( a_n + b_n \right) + p a_n, \qquad b_{n+1} \leftarrow p b_n + q a_n,
\end{equation}
second transformation:
\begin{equation}
  \label{eq:t2}
  a_{n+2} \leftarrow q \left( a_{n+1} + b_{n+1} \right) + p a_{n+1}, \qquad b_{n+2} \leftarrow p b_{n+1} + q a_{n+1},
\end{equation}
then substituting the values from the first transformation into the $b_{n+2}$ transformation in equation~\ref{eq:t2}, we can see that:
\begin{equation}
  b_{n+2} \leftarrow b_n \left( p^2 + q^2 \right) + a_n \left( 2pq + q^2 \right),
\end{equation}
and comparing with transform 1 it can be seen that
\begin{equation}
  p' = p^2 + q^2,
\end{equation}
and
\begin{equation}
  q' = 2pq + q^2.
\end{equation}

\end{document}


\documentclass{memoir}
\usepackage{linalg}

% \begin{figure}[ht]
%     \centering
%     \incfig{riemmans-theorem}
%     \caption{Riemmans theorem}
%     \label{fig:riemmans-theorem}
% \end{figure}

\begin{document}
\section{Matrices}
\label{cha:matrices}
\subsection{Representing a Linear Map by Matrices}
\begin{defn}[Matrix]
	Let $m$ and $n$ denote positive integers. An $m$-by-$n$ \textbf{matrix} $A$ is a rectangular array of elements of $F$ with $m$ rows and $n$ columns:
\begin{align*}
	A = \begin{bmatrix} A_{1,1} & \dots & A_{1,n} \\
		\vdots & \ddots & \vdots \\
		A_{m,1} & \dots & A_{m,n}
	\end{bmatrix}
\end{align*}
where $j$ refers to the row number, and $k$ refers to the column number.
\end{defn}
\begin{defn}[Matrix of a linear map]

Suppose $T \in \mathcal{L}(V,W)$, $v_1,\ldots,v_n$ a basis of $V$, and $w_1,\ldots,w_m$ a basis of $W$. Then, the \textbf{matrix of $T$} with respect to these bases is the $m$-by-$n$ matrix $\mathcal{M}(T)$ whose entries $A_{j,k}$ are defined by
\begin{align*}
	Tv_k = A_{1,k}w_1 + \ldots + A_{m,k}w_m .
\end{align*}
If the bases are not clear from context, we use the notation $\mathcal{M}(T,(v_1,\ldots,v_n),(w_1,\ldots,w_m))$ is used.

\end{defn}
\color{gray}
\begin{exmp}[Examples of matrices of linear maps]
	Consider $T:\R^2\to \R^3$ defined by $(x,y) \mapsto (x+3y,2x+5y,7x+9y)$. By applying this to the standard basis for $\R^2$ and $\R^3$, we can determine the matrix:
	\begin{align*}
		T(v_1) = T(1,0) = (1,2,7) \\
		T(v_2) = T(0,1) = (3,5,9) \\
		\text{ so then } \mathcal{M}(T) = \begin{bmatrix}
			1 & 3\\
			2 & 5\\
			7 & 9
		\end{bmatrix}
	\end{align*}

	Another important example is the differentiation map $D: P_3(\R) \to P_2(\R)$ defined by $p \mapsto p'$.\\

	Choose bases $P_3(\R) = \left\{1,x,x^2,x^3 \right\}$ and $P_2(\R) = \left\{1,x,x^2 \right\} $. Then
	\begin{align*}
		D(v_1) = D(1) = 0 = 0\cdot w_1 + 0\cdot w_2 + 0\cdot w_3 \\
		D(v_2) = D(x) = 1 = 1\cdot w_1 + 0 \cdot w_2 + 0 \cdot w_3 \\
		D(v_3) = D(x^2) = 2x = 0\cdot w_1 + 2 \cdot w_2 + 0 \cdot w_3 \\
		D(v_4) = D(x^3) = 3x^2 = 0 \cdot w_1 + 0\cdot w_2 + 3\cdot w_3 \\
		\text{ So therefore } \mathcal{M}(D) = \begin{bmatrix}
			0 & 0 & 0\\
			1 & 0 & 0\\
			0 & 2 & 0 \\
			0 & 0 & 3
		\end{bmatrix}
	\end{align*}
\end{exmp}
\color{black}

\section{Addition and Scalar Multiplication of Matrices of Linear Maps}
\label{sec:addition_and_scalar_multiplication_of_matrices_of_linear_maps}
\begin{cor}[Linearity of Matrices]
	Suppose $S,T \in \mathcal{L}(V,W)$. Then $\mathcal{M}(S+T) = \mathcal{M}(S) + \mathcal{M}(T)$.\\

	Suppose $\lambda \in F$ and $T \in \mathcal{L}(V,W)$. Then $\mathcal{M}(\lambda T) = \lambda \mathcal{M}(T)$.
\end{cor}
Notation: The set of all $m$-by-$n$ matrices with entries in $F$ is denoted by $F^{m,n}$.
\begin{cor}
	If $m,n$ are positive integers, then $F^{m,n}$ is a vector space with dimension $mn$.
\end{cor}
\end{document}


\chapter{Random Variables}

\section{Important/Useful Theorems}

\subsection{Chebyshev's Inequality}

\begin{equation}
	P \{ |\xi| > \epsilon \} \leq \frac{1}{\epsilon^2} \textbf{E}\xi^2
\end{equation}

\section{Answers to Problems}

\subsection{}
%problem 4.1
Thinking of this problem as 4 buckets each with 2 possibilities paves a clear way to the solution of the problem.  There are $2^4 = 16$ possibilities.  There is only one way for them to all be green ($\xi = 4$), and again only one way for 3 greens followed by one red ($\xi = 3$).  Once you have get to $\xi = 2$, then the last light can be either green or red giving two possibilities then at $\xi = 1$ you have two lights that can be either red or green giving 4 possibilities.  Clearly, then, there are 8 for the case of $\xi = 0$.

\begin{equation}
	P(\xi) = \left\{ \begin{array}{rl}
	\frac{1}{2}, \xi=0  \\
	\frac{1}{4}, \xi=1  \\
	\frac{1}{8}, \xi=2  \\
	\frac{1}{16}, \xi=3  \\
	\frac{1}{16}, \xi=4
       \end{array} \right.
\label{answer4.1}
\end{equation}
\textbf{Answer not verified}

\subsection{}
%problem 4.2
To summarize what we want here:
\begin{eqnarray}
	\xi_1 \neq \xi_2 \\
	\Phi_{\xi_1}(x) = \Phi_{\xi_2}(x) \\
	\int_{-\infty}^x p_{\xi_1}(x')dx' = \int_{-\infty}^x p_{\xi_2}(x')dx'
\end{eqnarray}
Since the question does not rule out the possibility that the probability distributions are the same, we can just say that $\xi_1$ is the outcome of a coin-flip experiment and that $\xi_2$ is the outcome of the first spin measurement of an EPR experiment.
\textbf{Answer not verified}

\subsection{}
%problem 4.3
If
\begin{equation}
	p(x)dx = \frac{dx}{b-a}
\end{equation}
then
\begin{equation}
	\Phi(x) = \int_{-a}^x p(x')dx' = \int_{a}^x \frac{dx'}{b-a}
\end{equation}
\begin{equation}
	\Phi(x) = \frac{x - a}{b-a}
\label{answer4.3}
\end{equation}
\textbf{Answer not verified}

\subsection{}
%problem 4.4
The distribution is clearly not normalized so the first step will be to normalise it.

\begin{equation}
	\int^{\infty}_{-\infty} \frac{a}{x^2+1}dx = 1 = a \pi
\end{equation}

\begin{equation}
	a = \frac{1}{\pi}
\label{answer4.4a}
\end{equation}

Just by definition:
\begin{equation}
	\Phi(x) = \int^{x}_{\infty} \frac{1}{\pi(x'^2+1)}dx' = \left \frac{\arctan x'}{\pi} \right] ^x_{-\infty} = \frac{\arctan x}{\pi}+\frac{1}{2}
\label{answer4.4b}
\end{equation}
and last but not least:
\begin{equation}
	P( \{ -1 \leq x \leq 1 \} = \int^{1}_{-1} \frac{1}{\pi(x'^2+1)}dx' = \frac{1}{2}
\label{answer4.4c}
\end{equation}

\textbf{Answers verified}

\subsection{}
%problem 4.5
Once again we start by normalizing
\begin{equation}
	1= \int^{\infty}_{0} a x^2 e^{-k x} dx = -\left \frac{e^{-k x} \left(2+2 k x+k^2 x^2\right)}{k^3} \right]^{\infty}_{0}= \frac{2 a}{k^3}
	\end{equation}
\begin{equation}
	a = \frac{k^3}{2}
\label{answer4.5a}
\end{equation}
\begin{equation}
	\Phi(x) = \int^{x}_{0} \frac{k^3}{2}x'^2 e^{-k x'}dx' = 1 - \frac{e^{-k x} \left(2+2 k x+k^2 x^2\right)}{2}
\label{answer4.5b}
\end{equation}
\begin{equation}
	P( \{ 0 \leq x \leq \frac{1}{k} \} = \int_{0}^{\frac{1}{k}} \frac{k^3}{2}x^2 e^{-k x}dx = \frac{2e - 5}{2 e}
\label{answer4.4c}
\end{equation}
\textbf{Answer not verified}

\subsection{}
%problem 4.6
\begin{equation}
	\Phi(\infty) = 1 \Rightarrow 1 = a + \frac{b \pi}{2}
\end{equation}
\begin{equation}
	\Phi(-\infty) = 0 \Rightarrow 0 = a - \frac{b \pi}{2}
\end{equation}
Which you can solve to get
\begin{eqnarray}
	b = \frac{1}{\pi} \\
	a = \frac{1}{2}
\end{eqnarray}
Since $\Phi$ is the integral of $p$, we can take the derivative of it and then make sure it's normalized.
\begin{equation}
	p(x) = \frac{d \Phi}{dx} = \frac{1}{2 \pi \left(1+\frac{x^2}{4}\right)}
\end{equation}
And the normalization is indeed still correct!

\textbf{Answer not verified}

\subsection{}
%problem 4.6
The area of the table is simply $\pi R^2$ and the are of each of the smaller circles are $\pi r^2$.  The ratio of the sum of the area of the two circles to the total table area will be the chance that one of the circles gets hit: $p = \frac{2r^2}{R^2}$.
\textbf{Answer verified}

\subsection{}
%problem 4.7
Just like in example 2 in the book, this problem will go by very well if we draw a picture indicating the given criteria:

\begin{figure}[hbtp]
\includegraphics[width=\textwidth]{overlap.png}
\caption{The Desired Region}
\end{figure}
Where does this come from?
\begin{eqnarray}
	x_1 + x_2 \leq 1 \\
	x_2 \leq 1 - x_1
\end{eqnarray}
which is the straight line.
\begin{eqnarray}
	x_1x_2 \leq \frac{2}{9} \\
	x_2 \leq \frac{2}{9 x_1}
\end{eqnarray}
A little bit of algebra shows that these two lines intersect at $\frac{1}{3}$ and $\frac{2}{3}$ so the area underneath the straight line but not above the curved line is
\begin{eqnarray}
	A =\int_0^{\frac{1}{3}}(1-x)dx + \int_{\frac{1}{3}}^{\frac{2}{3}}\frac{2}{9x}dx + \int_{\frac{2}{3}}^{1}(1-x)dx \\
	= \frac{5}{18} + \int_{\frac{1}{3}}^{\frac{2}{3}}\frac{2}{9x}dx + \frac{1}{18} \\
	= \frac{1}{3} + \int_{\frac{1}{3}}^{\frac{2}{3}}\frac{2}{9x}dx \\
	= \frac{1}{3	} + \frac{2 \ln 2}{9} \approx 0.487366
\end{eqnarray}
And the answer is properly normalized since the initial probability distributions were unity (the box length is only 1).
\textbf{Answer  verified}

\subsection{}
%problem 4.9
As we showed in example number 4, $p_{\eta}$ is the convolution of $p_{\xi_1}$ and $p_{\xi_2}$.
\begin{eqnarray}
	p_{\eta}(y) = \int_{-\infty}^{\infty} p_{\xi_1}(y-x)p_{\xi_2}(x)dx
\end{eqnarray}
I think the integration will look more logical if we stick in Heaviside step-functions.
\begin{eqnarray}
	p_{\eta}(y) = \frac{1}{6} \int_{-\infty}^{\infty}e^{-\frac{y-x}{3}}e^{-\frac{x}{3}}H(y-x)H(x)dx
\end{eqnarray}
Clearly $x$ has to be greater than zero and $y$ must be greater than x, leading to the following limits of integration.
\begin{eqnarray}
	p_{\eta}(y) = \frac{1}{6} \int_{0}^{y}e^{-\frac{y-x}{3}}e^{-\frac{x}{3}}dx \\
	= e^{-\frac{y}{3}}\left(1- e^{-\frac{y}{6}}  \right)
\end{eqnarray}
Only when y is greater than zero!
\textbf{Answer verified}

\subsection{}
%problem 4.10
Due to the magic of addition, finding the probability distribution of $\xi_1 + \xi_2 + \xi_3$ is no different than the probability distribution of $(\xi_1 + \xi_2) + \xi_3$ but we already know what the probability distribtution of the parenthesized quantity is:
\begin{equation}
	p_{\xi_1 + \xi_2}(y) = \int_{-\infty}^{\infty} p_{\xi_1}(y-x)p_{\xi_2}(x)dx
\end{equation}
Therefore the total combination is
\begin{equation}
	p_{\xi_1 + \xi_2+ \xi_3}(z) = \int_{-\infty}^{\infty} p_{\xi_1 + \xi_2}(z-y)p_{\xi_3}(y)dx
\end{equation}
\begin{equation}
	p_{\xi_1 + \xi_2+ \xi_3}(z) = \int_{-\infty}^{\infty} \left[ \int_{-\infty}^{\infty} p_{\xi_1}(z-y-x)p_{\xi_2}(x)dx \right]p_{\xi_3}(y)dy
\end{equation}
\begin{equation}
	p_{\xi_1 + \xi_2+ \xi_3}(z) = \int_{-\infty}^{\infty}  \int_{-\infty}^{\infty} p_{\xi_1}(z-y-x)p_{\xi_2}(x)p_{\xi_3}(y)dx dy
\end{equation}
which is just a triple convolution.

\textbf{Answer not verified}

\subsection{}
%problem 4.11
\begin{equation}
	p_{\xi}(n) = \frac{1}{3^n}
\end{equation}
therefore
\begin{equation}
	\textbf{E}\xi = \sum_{n=1}^{\infty}\frac{n}{3^n} =  \frac{3}{4}
\label{answer4.11}
\end{equation}
\textbf{Answer not verified}

\subsection{}
%problem 4.12
Since finding a ball in one urn versus the other urn has nothing to do with each other, the events are clearly independent so we can multiply probabilities.  The probability of finding a white ball at any given urn is
\begin{equation}
	p_w = \frac{w}{w+b}
\end{equation}
If you find a white ball on the $n^{th}$ try then that means you found $n-1$ black balls before you got to the white ball.
\begin{equation}
	p_w(n) = \frac{b^{n-1} w}{(w+b)^n}
\end{equation}
\begin{equation}
	\textbf{E}n = \sum_{i=1}^{\infty}n p_w(n) = \sum_{i=1}^{\infty}n \frac{b^{n-1} w}{(w+b)^n} = \frac{b+w}{w}
\end{equation}
Which is the total number of balls drawn, subtract one to get the average number of black balls drawn:
\begin{equation}
	m=\frac{b}{w}
\end{equation}
Now for the variance. to start with we need the average of the square of the random variable.
\begin{equation}
	\textbf{E}n^2 = \sum_{i=1}^{\infty}n^2 p_w(n) = \sum_{i=1}^{\infty}n^2 \frac{b^{n-1} w}{(w+b)^n} = \frac{(b+w) (2 b+w)}{w^2}
\end{equation}
\begin{equation}
	\textbf{D}n = \textbf{E}n^2 - (\textbf{E}n)^2 =  \frac{(b+w) (2 b+w)}{w^2} - \left( \frac{b+w}{w} \right)^2 = \frac{b^2+wb}{w^2}
\end{equation}
A note that we don't need to subtract anything for the variance since shifting a distribution over does not affect its variance: just its average.
\textbf{Answer verified}

\subsection{}
%problem 4.13
\begin{equation}
	\textbf{E}\xi = \int_{-\infty}^{\infty} x\frac{1}{2}e^{-|x|} = 0
\end{equation}
because the function is even about $x=0$.
\begin{equation}
	\textbf{E}\xi^2 = \int_{-\infty}^{\infty} x^2\frac{1}{2}e^{-|x|} = 2
\end{equation}
Therefore:
\begin{equation}
	\textbf{D}\xi = \textbf{E}\xi^2 - (\textbf{E}\xi)^2 = 2
\end{equation}
\textbf{Answer verified}

\subsection{}
%problem 4.14
\begin{equation}
	\textbf{E}x = \int_{a-b}^{a+b} \frac{xdx}{2b} = a
\end{equation}
\begin{equation}
	\textbf{E}x^2 = \int_{a-b}^{a+b} \frac{x^2dx}{2b} = a^2+\frac{b^2}{3}
\end{equation}
Therefore:
\begin{equation}
	\textbf{D}x = \frac{b^2}{3}
\end{equation}
\textbf{Answer verified}

\subsection{}
%problem 4.15
If the distribution function is
\begin{equation}
	\Phi_{\xi}(x) = a + b \arcsin x, |x| \leq 1
\end{equation}
then it must fulfill the proper boundary conditions as specified by both the problem and the definition of a distribution function.
\begin{equation}
	\Phi_{\xi}(-1)= 0 = a - b \frac{\pi}{2}
\end{equation}
\begin{equation}
	\Phi_{\xi}(1) = 1 = a + b \frac{\pi}{2}
\end{equation}
Some easy algebra gets you
\begin{eqnarray}
	\Phi_{\xi}(x) = \frac{1}{2} + \frac{1}{\pi} \arcsin x
\end{eqnarray}
Therefore:
\begin{equation}
	p_{\xi}(x) = \frac{1}{\pi\sqrt{1-x^2}}
\end{equation}
\begin{eqnarray}
	\textbf{E}x = \int_{-1}^{1} \frac{x dx}{\pi\sqrt{1-x^2}} = 0 \\
	\textbf{D}x = \int_{-1}^{1} \frac{x^2 dx}{\pi\sqrt{1-x^2}} = \frac{1}{2}
\end{eqnarray}
\textbf{Answer verified}

\subsection{}
%problem 4.16
Each side of the die has the same probability, $\frac{1}{6}$
\begin{equation}
	\textbf{E}x = \sum_{i=1}^6 \frac{i}{6} = \frac{7}{2}
\end{equation}
\begin{equation}
	\textbf{E}x^2 = \sum_{i=1}^6 \frac{i^2}{6} = \frac{91}{6}
\end{equation}
Therefore:
\begin{equation}
	\textbf{D}x = \frac{91}{6} - \left( \frac{7}{2} \right)^2 = \frac{91}{6} - \frac{49}{4} = \frac{35}{12}
\end{equation}
\textbf{Answer verified}

\subsection{}
%problem 4.17
This problem may seem difficult until you realize that being $\pm \frac{5}{2}$ away from the mean means you're at either 1 or 6, meaning that there's a unity probability of being that far from the mean.  Chebyshev's inequality, however, would have us believe that
\begin{equation}
	P \{ |x + \textbf{E}x| > \frac{5}{2}  \} \leq \frac{4}{25} \frac{91}{6} = \frac{182}{75} = 2.42667
\end{equation}
which is not only far off from the actual answer, it's also unphysical to have probabilities greater than 1!
\textbf{Answer not verified}

\subsection{}
%problem 4.18
We want to consider the probability distribution of $\xi$ by way of $\eta$
\begin{equation}
	\eta = e^{\frac{a\xi}{2}}
\end{equation}
We know from Chebyshev's identity:

\begin{equation}
	P\{ \eta > \epsilon(\eta) \} \leq \frac{\textbf{E}\eta^2}{\epsilon(\eta)^2}
\end{equation}
Let $\epsilon$ be the error in $\xi$ we're looking for.
\begin{equation}
	P\{ \xi > \epsilon \} \leq \frac{\textbf{E}(e^{\frac{a\xi}{2}})^2}{(e^{\frac{a\epsilon}{2}})^2}
\end{equation}
\begin{equation}
	P\{ \xi > \epsilon \} \leq \frac{\textbf{E}e^{a\xi}}{e^{a\epsilon}}
\end{equation}
\textbf{Answer not verified}

\subsection{}
%problem 4.19
First some initial calculations:
\begin{eqnarray}
	\textbf{E}\xi = \frac{1}{4} \left( -2+-1+1+2  \right) = 0 \\
	\textbf{E}\xi^2 = \frac{1}{4} \left( (-2)^2+(-1)^2+1^2+2^2  \right) = \frac{10}{4} = \frac{5}{2} = \textbf{E}\eta \\
	\textbf{E}\xi^4 = \frac{1}{4} \left( (-2)^4+(-1)^4+1^4+2^4  \right) = \frac{34}{4} = \frac{17}{2} = \textbf{E}\eta^2
\end{eqnarray}
Now, we know that
\begin{eqnarray}
	r = \frac{\textbf{E} \left[ (\xi - \textbf{E}\xi)(\eta - \textbf{E}\eta)  \right]}{(\textbf{E}\xi^2 - (\textbf{E}\xi)^2)(\textbf{E}\eta^2 - (\textbf{E}\eta)^2)}
\end{eqnarray}
The denominator $D$ is the easiest:
\begin{equation}
	D = \left( \frac{5}{2} - 0 \right)\left( \frac{17}{2} - \frac{5^2}{2^2} \right) = \frac{45}{8}
\end{equation}
Now for the numerator:
\begin{eqnarray}
	\textbf{E} \left[ (\xi - \textbf{E}\xi)(\eta - \textbf{E}\eta)  \right] \\
	= \frac{1}{16} \left[  \sum_{\xi, \eta} (\xi - \textbf{E}\xi)(\eta - \textbf{E}\eta)      \right]
\end{eqnarray}
If we look at the set we'll be summing over, we have ${\xi - \textbf{E}\xi} = -2, -1, 1, 2$ and ${\eta - \textbf{E}\eta} = \frac{3}{2}, -\frac{3}{2}, -\frac{3}{2}, \frac{3}{2}$ and clearly, since we have to sum over all possible products, since both distributions are symmetric about 0, the sum will go to zero.
\begin{equation}
	r=0
\end{equation}
\textbf{Answer not verified}

\subsection{}
%problem 4.20
First some initial calculations:
\begin{eqnarray}
	\textbf{E}x_1 = \int_0^{\frac{\pi}{2}} x_1 \sin x_1 \sin x_2 dx_1dx_2 = 1 \\
	\textbf{E}x_2 = 1 \\
	\textbf{E}x_1^2 = \int_0^{\frac{\pi}{2}} x_1^2 \sin x_1 \sin x_2 dx_1dx_2 = (\pi - 2) \\
	\textbf{E}x_2^2 = (\pi - 2) \\
\end{eqnarray}
\begin{eqnarray}
	\textbf{E} \left[ (x_1 - \textbf{E}x_1)(x_2 - \textbf{E}x_2)  \right] \\
	= \int_0^{\frac{\pi}{2}} \int_0^{\frac{\pi}{2}} \left[(x_1 - 1)(x_2 - 1)\right]\sin x_1 \sin x_2 dx_1dx_2 = 0 \\
	\sigma_1 \sigma_2 =  \sqrt{(\pi - 2)- 1}\sqrt{(\pi - 2) - 1} = (\pi - 3) \\
	r= \frac{0}{(\pi - 3)} = 0
\end{eqnarray}
\textbf{Answer not verified}

\subsection{}
%problem 4.21
First some initial calculations:
\begin{eqnarray}
	\textbf{E}x_1 = \frac{1}{2}\int_0^{\frac{\pi}{2}} x_1 \sin (x_1 + x_2) dx_1dx_2 = \frac{\pi}{4} \\
	\textbf{E}x_2 = \frac{\pi}{4} \\
	\textbf{E}x_1^2 = \frac{1}{2}\int_0^{\frac{\pi}{2}} x_1^2 \sin (x_1 + x_2) dx_1dx_2 = -2+\frac{\pi}{2} +\frac{\pi ^2}{8} \\
	\textbf{E}x_2^2 = -2+\frac{\pi}{2} +\frac{\pi ^2}{8} \\
\end{eqnarray}
\begin{eqnarray}
	\textbf{E} \left[ (x_1 - \textbf{E}x_1)(x_2 - \textbf{E}x_2)  \right] \\
	= \frac{1}{2}\int_0^{\frac{\pi}{2}} \int_0^{\frac{\pi}{2}} \sin (x_1 + x_2) \left[(x_1 - \frac{\pi}{2})(x_2 - \frac{\pi}{2})\right]dx_1dx_2  = -\frac{1}{16} (\pi -4)^2 \\
	\sigma_1\sigma_2 = -2+\pi +\frac{\pi ^2}{2} +\frac{\pi^2}{8} - \frac{\pi^2}{16} = -2+\pi +\frac{\pi ^2}{2} +\frac{\pi^2}{16} \\
	r = \frac{-\frac{1}{16} (\pi -4)^2}{-2+\pi +\frac{\pi ^2}{2} +\frac{\pi^2}{16}}
\end{eqnarray}
\textbf{Answer verified}

\subsection{}
%problem 4.22
Ill-defined problem, don't feel like translating: \textbf{SKIPPED}

\subsection{}
%problem 4.23
Based off 4.22: \textbf{SKIPPED}

\subsection{}
%problem 4.21
\begin{eqnarray}
	E\xi = \int_{-\infty}^{\infty} \frac{x}{\pi(1+x^2)}dx = \left \frac{\log \left(x^2+1\right)}{2 \pi } \right]_{-\infty}^{\infty} \rightarrow \infty \\
	E\xi^2 = \int_{-\infty}^{\infty} \frac{x^2}{\pi(1+x^2)}dx = \left \frac{x}{\pi }-\frac{\tan ^{-1}(x)}{\pi } \right]_{-\infty}^{\infty} \rightarrow \infty
\end{eqnarray}
Neither integral actually converges so we cannot define averages or dispersions for this distribution.
\textbf{Answer verified}
%%answer template
%\subsection{}
%%problem n.n
%
%
%\begin{equation}
%
%\label{answern.n}
%\end{equation}
%\textbf{Answer [not] verified}