\documentstyle{jfp}
%\documentstyle[11pt,fpart,fpbib]{article}
% maths stuff
\newtheorem{theorem}{Theorem}[section]
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{counterexample}[theorem]{Counter Example}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{hypothesis}[theorem]{Hypothesis}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
%\def\proof[#1]{\begin{flushleft} \mbox{\bf Proof of #1} \end{flushleft}
%               \begin{list}{}{} \item[]}
%\def\endproof{\end{list}
%              \begin{flushright} \mbox{$\Box$} \end{flushright}}

%\def\author{David Lester}
%\def\fromemail{dlester@cs.man.ac.uk}
%\def\doctype{Report-0004}
%\def\title{Computable Real Numbers for {\sc Haskell}}

% Filled in on creation of document: \author, \fromemail, \doctype and \title

% RCS amended stuff
%$Log: paper.tex,v $
%Revision 1.1  1996/01/08 20:05:20  partain
%Initial revision
%
%Revision 1.1  94/06/15  11:50:41  dlester
%Initial
%
%$Author: partain $
\def\fpstate{Draft}
\def\fprevision{1.1}
\def\fpdate{94/06/15} \def\fptime{11:50:41}
%\def\acknowledge{}      % delete this if you have no affiliation acks
%\def\fromaddress{Functional Programming Group,\\Department of Computer Science,
%Manchester University,\\Oxford Road,
%Manchester M13 9PL, UK.}

%\rcsheadings{}
\def\indexTT#1{}
\def\indexDTT#1{}

\begin{document}
\title[Computable Reals]{Computable Real Numbers for {\sc Haskell}}
\author[David Lester]{David Lester\\Functional Programming Group,
Department of Computer Science,\\ Manchester University, Oxford Road,
Manchester M13 9PL, UK}

\bibliographystyle{fp}
\maketitle
%\fptitle{}
\begin{abstract}

This work arose from a desire to provide an implementation of
computable real numbers for {\sc Haskell}. The starting point was
Vuillemin's report on continued fraction arithmetic
\shortcite{VUILLEMIN87a}. One advantage that the present
implementation has over Vuillemin's original work is that -- being
implemented in a lazy functional language -- we are able to take
advantage of laziness to ensure that the stream processing takes place
in a computable way.

This paper goes into some of the details that were absent from
Vuillemin's original work. Further, we show how to take advantage of
Gauss' work on continued fractions, to generate the transcendental
functions: \mbox{\tt exp}\indexTT{exp}, \mbox{\tt log}\indexTT{log}, \mbox{\tt
tan}\indexTT{tan}, and \mbox{\tt atan}\indexTT{atan}. This requires us to
process continued fractions whose terms are themselves continued
fractions; we show how to achieve this.

\end{abstract}
%\keywords{Computable Real Numbers, Computable Transcendental
%          Functions, Continued Fractions, {\sc Haskell}}

\section{Introduction}

If we are content with using only rational numbers, accurate computer
arithmetic is easy to implement. Unfortunately, as was discovered
early in the history of mathematics, there are numbers, such as
$\sqrt{2}$, which are {\em irrational}. The discovery of computable
functions by Turing \shortcite{TURING36a} and Church
\shortcite{CHURCH41a} led to work on computable numbers
\cite{RICE54a,BISHOP85a}. This work used a radix representation along
with intervals. This is fine from the point of correctness, but as the
implementation is {\em memoryless}, it could be quite inefficient.  In
their paper \shortcite{Boehm90a}, Boehm and Cartwright make use of a
higher order implementation to ensure that a rational implementation
carries out calculations to sufficient accuracy for the printing of
the answer.

This paper was written in an attempt to understand and interpret the
work of Jean Vuillemin \shortcite{VUILLEMIN87a}, in which he describes a
representation of computable real numbers using continued fractions.
In this paper we will hopefully see the benefits of using a lazy
functional language to implement algorithms that are intrinsically
lazy.  We also explore some of the algorithms in more detail than
\cite{VUILLEMIN87a}, paying particular care to ensure that laziness is
preserved.

The advantage with using continued fractions as a representation of
numbers is that if, for example, we calculate $\pi$ to a high degree
of accuracy at some stage, this accuracy is then available everywhere
else that $\pi$ is used without further calculation being necessary.

Because we will be using the terms frequently, and because the
concepts may not be familiar, the introduction continues by informally
introducing three key concepts used in this paper. They are {\em Real
Numbers}, {\em Computable Real Numbers}, and {\em Continued
Fractions}. Readers already familiar with this material should feel
free to skip the remainder of the introduction.

In Section~\ref{sc:reals} we discuss computable real numbers, and in
Section~\ref{sc:computability} we investigate which
computable/non-computable functions we are required to implement in
{\sc Haskell}. In Section~\ref{sc:comp-cf} computable continued
fractions are discussed.  A gentle introduction to Gosper's algorithm
is provided in Section~\ref{sc:aa}; the main algorithm is given in
Section~\ref{sc:qa}. The continued fraction representation of the
exponential, logarithm, tangent and arctangent functions is given in
Section~\ref{sc:tf}.

\section{What are Real Numbers?}
\label{sc:reals}

The best way to think of the various stages in the development of
number theory is that of a closure operation. If you start with the
integer one, and the operation of $+$ you can generate all of the
positive natural numbers ${\cal N}^{+}$. Augmenting the set of
operations with $-$ it is possible to generate all of the integers,
${\cal Z}$. We may therefore make the statement: the closure of
operations $+$ and $-$ with the number one is ${\cal Z}$, the
integers. Multiplication adds no new numbers, but when we consider the
division operation we must include fractions. These constitute the
rational numbers, which we represent by the symbol ${\cal Q}$. We can
restate this as: the closure of the operations of $+$, $-$, $\times$,
and $/$ on the number one is ${\cal Q}\,\cup\,\{\bot,\,\infty\}$.
(We use $\bot$ to represent the number $0/0$ and $\infty$ to
represent $n/0$, whenever $n\not=0$).

If we think in terms of a pencil and paper implementation for the
moment, then provided that we have a sufficiently large piece of paper
we can perform operations on rational numbers to any desired degree of
accuracy. In the context of the implementation of rational number
arithmetic, this can be paraphrased as: provided that the memory does
not overflow we can perform operations on rational numbers to any
desired degree of accuracy.

But of course -- as the Greek Mathematicians discovered\footnote{The
theory of irrationals in \cite{EUCLID} is attributed to The\ae{}tus.}
-- this is not the whole story. There exist numbers (such as
$\sqrt{2}$) that cannot be represented by any finite fraction. Such
numbers are said to be {\em irrational}. There are a many beautiful
generalisations that arise from this, but for the moment we simply
observe that numbers such as square roots arise from finding the roots
of quadratic polynomials. The rational and irrational numbers that are
generated as roots to polynomial equations are called {\em algebraic
numbers}.

At this stage you may wonder where this process stops; {\it i.e.\/}
how many operations must we use to generate all of the real numbers?
The answer is that the process doesn't stop and no finite set of
operations will generate all of the real numbers. The solution is to
restrict attention to a subset of ${\cal R}$ which I shall call ${\cal
A}$; which has only a limited set of operations.  The set ${\cal A}$
will constitute a useful set of computable real numbers for
engineering applications.

\[\begin{array}{rcl}
1 & \in & {\cal A}\\
{\cal A}+{\cal A}& \subset & {\cal A}\\
-{\cal A}& \subset & {\cal A}\\
{\cal A}/{\cal A}_{\not= 0}& \subset & {\cal A}\\
e^{{\cal A}}& \subset & {\cal A}\\
\log {\cal A}_{\, > 0}& \subset & {\cal A}
\end{array}\]

Notice that by removing the restrictions on the argument for the
logarithm function we would obtain a subset of the complex numbers,
${\cal C}$, which would include trigonometric functions. This should
provide a sufficient range of basic arithmetic operations for most
engineering applications. The next subsection high-lights the major
problem involved in producing an arithmetic package for real numbers.

\subsection{What are Computable Real Numbers?}

Informally, a number is said to be computable if we can write a
program which will print its value to some specified degree of
accuracy. Following Pour-El and Richards \shortcite{POUR-EL89a}, we may
formalise this definition.

\begin{definition}\rm
A real number $x$ is {\em computable} if there exists a computable
sequence $\{r_k\}$ of rationals which converges effectively to $x$.
\end{definition}

\begin{definition}\rm
A sequence $\{r_k\}$ of rational numbers is {\em computable} if there
exist three recursive functions $a$, $b$, and $s$ from ${\cal N}$ to
${\cal N}$ such that $b(k) \not= 0$ for all $k$ and
\[r_k = (-1)^{s(k)}\frac{a(k)}{b(k)} \mbox{ for all } k.\]
\end{definition}

In other words: an infinite {\sc Haskell} sequence, not all of whose
elements are $\bot$.

\begin{definition}\rm
A sequence $\{r_k\}$ of rational numbers {\em converges effectively}
to a real number $x$ if there exists a recursive function $e:{\cal
N}\to{\cal N}$ such that for all $N$:
\[k \geq e(N) \mbox{ implies } |r_k - x| \leq 2^{-N}.\]
\end{definition}

This definition of computable real number is (inefficiently)
implementable directly in {\sc Haskell}. What we require is an
infinite list of \mbox{\tt Rational}\indexTT{Rational} numbers, not all of whic
h are undefined;
and a modulus of convergence function: \mbox{\tt e} from \mbox{\tt Integer}\indexTT{Integer} to
\mbox{\tt Integer}\indexTT{Integer}.

A cardinality argument suffices to show that non-computable numbers
exist: the set of programs is countable, the set of real numbers is
not. It seems reasonable that in dealing with numbers for computers,
we should restrict our attention to just the subset of real numbers
that is computable. In fact for the engineering applications that we
have in mind this set may be further restricted to the closure of the
arithmetic operations on the singleton set: $\{1\}$ -- as outlined
above.

Life is seldom simple. It is an unfortunate fact of life that it is
not even possible to tell whether a computable real number is $0$.
Let us suppose that we are given a monotonically decreasing sequence
of rationals that converges to $0$. One such sequence is $1$, $0.1$,
$0.01$, $0.001$, \ldots; proving the convergence establishes that this
sequence represents the computable real number $0$.  Similarly we
might wish to consider the monotonically increasing sequence: $-1$,
$-0.1$, $-0.01$, $-0.001$, \ldots; again this is another
representation of the real number $0$. But notice that the application
of the \mbox{\tt signum}\indexTT{signum} function to the sequence gives.
\[\mbox{\tt map}\indexTT{map}\,\mbox{\tt signum}\indexTT{signum}\,[-0.1,\, -0.0
1,\, -0.001,\,\ldots] =
[-1,\, -1,\, -1,\,\ldots]\]
But
\[\mbox{\tt signum}\indexTT{signum}\, (\lim\{-0.1,\, -0.01,\, -0.001,\,\ldots\}
) = 0\]

There are more operations that are not computable; some of these are
listed in Theorem~\ref{th:rice}, which is a combination of results
from Turing \shortcite{TURING36a} and Rice \shortcite{RICE54a}. Proofs
of these results may be found in \cite{VUILLEMIN87a}.

\begin{theorem}[{Turing, Rice}]\label{th:rice}

There is no algorithm, which for an arbitrary computable real number
$r\in{\cal R}$, can compute:
\begin{enumerate}
\item whether the number $r$ is zero;
\item whether $r > r'$ for any fixed $r'\in {\cal R}$;
\item whether the number is rational;
\item the first digit of the decimal expansion of $r$;
\item the first term of the regular continued fraction expansion of
$r$ (which is $\lfloor r\rfloor$);
\item the value $f(r)\in {\cal R}$ of any function $f$ which is not
continuous at $r$.
\end{enumerate}
\end{theorem}

This is all very disturbing. However, facing facts is infinitely
preferable to facing defeat, so we must cast around for ways to avoid
the Turing/Rice Theorems.

The first ``fix'' is reasonably obvious: we must give up any idea that
exact comparisons are available. They are replaced by what I shall
refer to as $\varepsilon$-comparisons. Two numbers $r$ and $r'$ are
said to be ${=}_\varepsilon$ if $|r-r'| < \varepsilon$. Similar
properties hold for the other comparison operations. This is not so
very different to the properties of comparison operations available in
floating point packages.

The second ``fix'' is implied by the fourth observation in
Theorem~\ref{th:rice}, where we notice that we can't even find the
first digit of a computable number. To see this consider the sequence
$\{0.9,\,0.99,\,0.999,\,\ldots\}$.  The first digit of each rational
approximation is $0$, but of course the first digit of the number $1$
is $1$. The solution: use a redundant representation of the computable
numbers. This is implicit in the decimal representation of numbers: as
we have seen $0.\dot{9}$ and $1$ are both representations of the same
number.

We now investigate precisely which operations we are required to
implement for {\sc Haskell}.

\section{{\sc Haskell} and Computability}
\label{sc:computability}

One of the more irritating features of {\sc Haskell}'s numeric type classes
is that it makes no distinction between those methods that are
computable and those which are not. It would be useful if we could at
least ensure that each class includes methods that either all
computable or all non-computable. As Table~\ref{tb:comp} shows, this
is not likely to meet with general approval, because some inverses are
non-computable {\it e.g.} \mbox{\tt read}\indexTT{read}/\mbox{\tt show}\indexTT
{show}.

\begin{table}[htbp]
\begin{tabular}{l|p{2in}|p{1.5in}}
\hline
{\tt Class} & {\tt Computable} & {\tt Non-computable} \\
\hline
{\tt Eq} & & \mbox{\tt ==}, \mbox{\tt /=} \\
{\tt Ord} & \mbox{\tt max}\indexTT{max}, \mbox{\tt min}\indexTT{min} & \mbox{\t
t <=}, \mbox{\tt <}, \mbox{\tt >}, \mbox{\tt >=}  \\
{\tt Text} & \mbox{\tt readSigned}\indexTT{readSigned} & \mbox{\tt showSigned}\
indexTT{showSigned} \\
{\tt Num} & \mbox{\tt +}, \mbox{\tt -}, \mbox{\tt *}, \mbox{\tt negate}\indexTT
{negate}, \mbox{\tt abs}\indexTT{abs}, \mbox{\tt fromInteger}\indexTT{fromInteg
er} & \mbox{\tt signum}\indexTT{signum} \\
{\tt Real} & & \mbox{\tt toRational}\indexTT{toRational} \\
{\tt Fractional} & \mbox{\tt /}, \mbox{\tt fromRational}\indexTT{fromRational}
& \\
{\tt Floating} & \mbox{\tt pi}, \mbox{\tt exp}\indexTT{exp}, \mbox{\tt log}\indexTT{log}, \mbox{\tt sqrt}\indexTT{sqrt}, \mbox{\tt **}, \mbox{\tt logBase}\indexTT{logBase}, \mbox{\tt sin}\indexTT{sin}, \mbox{\tt cos}\indexTT{cos}, \mbox{
\tt tan}\indexTT{tan},
                 \mbox{\tt asin}\indexTT{asin}, \mbox{\tt acos}\indexTT{acos},
\mbox{\tt atan}\indexTT{atan}, \mbox{\tt sinh}\indexTT{sinh}, \mbox{\tt cosh}\indexTT{cosh}, \mbox{\tt tanh}\indexTT{tanh}
                 \mbox{\tt asinh}\indexTT{asinh}, \mbox{\tt acosh}\indexTT{acosh}, \mbox{\tt atanh}\indexTT{atanh} & \\
\hline
\end{tabular}
\caption{Computable and non-computable Classes is Standard {\sc Haskell}}
\label{tb:comp}
\end{table}

\subsection{Class \mbox{\tt Ord}\indexTT{Ord}}

It is a shame that the standard prelude defines \mbox{\tt max}\indexTT{max} and
 \mbox{\tt min}\indexTT{min} in
terms of the \mbox{\tt <=} function; from the point of view of computability it
would be more natural for the default methods to be:
\begin{verbatim}
> x <= y  = x == min x y
> x <  y  = x <= y && x /= y
> x >  y  = y < x
> x >= y  = y <= x
\end{verbatim}
This properly places the blame for the non-computability onto the test
for equality and not on the ordering itself.

\subsection{Class \mbox{\tt Text}\indexTT{Text}}

The first, and most obvious, problem is that we must forego the
condition that \mbox{\tt read(show\ x)\ =\ x}. This is a natural consequence of
the fact that we have printed out only a finite approximation to \mbox{\tt x}
using \mbox{\tt show}\indexTT{show}.

There is a problem with \mbox{\tt showSigned}\indexTT{showSigned}. It is define
d as:
\begin{verbatim}
> showSigned f p x = showParen (x < 0 && p > 6) (f x)
\end{verbatim}
\indexDTT{showSigned}%
What happens when \mbox{\tt x} is \mbox{\tt 0}? We first observe that the compa
rison
\mbox{\tt x\ <\ 0} is performed using a rational approximation to \mbox{\tt x},
 which might
not be \mbox{\tt 0}, but some small negative number. The \mbox{\tt f} function,
 however
merely outputs \mbox{\tt "0.000000"}, and we have needlessly added
parenthesis. The cure is to rewrite the function from scratch.

\subsection{Class \mbox{\tt Num}\indexTT{Num}}

The problem with this class revolve aroung the defintion of \mbox{\tt abs}\indexTT{abs} and
\mbox{\tt signum}\indexTT{signum}. One choice is to define \mbox{\tt abs}\indexTT{abs} as:
\begin{verbatim}
> abs x = max x (-x)
\end{verbatim}
\indexDTT{abs}%
which is then computable. The problem with \mbox{\tt signum}\indexTT{signum} is
 (again) that we
must test \mbox{\tt x} for equality with \mbox{\tt 0}. A consequence of this de
finition
is that it is no longer true that: \mbox{\tt abs\ x\ *\ signum\ x\ =\ x}, which
 is
unfortunate.

The alternative which I have chosen to implement preserves the
condition that \mbox{\tt abs\ x\ *\ signum\ x\ =\ x}, by calculating the
approximation to \mbox{\tt signum\ x} first and then using this to decide
whether to negate \mbox{\tt x}.

\subsection{Classes \mbox{\tt Fractional}\indexTT{Fractional} and \mbox{\tt Flo
ating}\indexTT{Floating}}

Provided that the arguments remain within the domain of definition for
each operator, all the operations in these classes are computable.

There are however a number of extra default methods that could be
added for the \mbox{\tt Real}\indexTT{Real} case.
\begin{verbatim}
> pi = 4 * atan 1
\end{verbatim}
The basic idea for \mbox{\tt sin}\indexTT{sin} and \mbox{\tt cos}\indexTT{cos}
is to use the following formul\ae .
\[\sin(x) = \frac{2 \tan(x/2)}{1+\tan^2(x/2)} \mbox{ and }
  \cos(x) = \frac{1-\tan^2(x/2)}{1+\tan^2(x/2)}\]

Unfortunately, we observe that there is a problem at the points
$(2k+1)\pi / 2$ since the \mbox{\tt tan}\indexTT{tan} function switches from $+
\infty$ to
$-\infty$. To cure this we observe the following identities that are
based on the value of the cotangent function:
\[\sin(x) = \frac{2 \cot(x/2)}{1+\cot^2(x/2)} \mbox{ and }
  \cos(x) = \frac{\cot^2(x/2)-1}{\cot^2(x/2)+1}\]

We use these alternative definitions when we expect that $x$ might be
close to $(2k+1)\pi /2$.
\begin{verbatim}
> asin x = atan (x / sqrt (1-x*x))
> acos x = atan (sqrt (1-x*x) / x)
> sinh x = (ex - (1 / ex)) / 2 where ex = exp x
> cosh x = (ex + (1 / ex)) / 2 where ex = exp x
\end{verbatim}
\indexDTT{asin}%
\indexDTT{acos}%
\indexDTT{sinh}%
\indexDTT{cosh}%
It is also marginally more efficient to calculate \mbox{\tt tanh}\indexTT{tanh}
 using the
following identity rather than the one in the standard prelude, which
involves the recomputation of \mbox{\tt exp\ x}.
\begin{verbatim}
> tanh x = (ex - ex') / (ex + ex') where ex  = exp x
>                                        ex' = 1/ex
\end{verbatim}
\indexDTT{tanh}%
\begin{verbatim}
> asinh x = log (x + sqrt (x*x+1))
> acosh x = log (x + sqrt (x*x-1))
> atanh x = log ((1+x)/(1-x)) / 2
\end{verbatim}
\indexDTT{asinh}%
\indexDTT{acosh}%
\indexDTT{atanh}%
>From the foregoing we can see that the transcendental functions
required of a {\sc Haskell} implementation are reduced to: \mbox{\tt exp}\index
TT{exp},
\mbox{\tt log}\indexTT{log}, \mbox{\tt tan}\indexTT{tan} (and \mbox{\tt cot}\indexTT{cot}), and \mbox{\tt atan}\indexTT{atan}.

We now move on to consider continued fractions.

\section{What are Continued Fractions?}

In this section we will see how to represent numbers by continued fractions.
Continued fractions are an attractive representation of real numbers
because: we are able to take advantage of what functional programmers
would call {\em laziness}, and there exist algorithms to perform the
arithmetic operations we require.

\begin{definition}\rm A finite continued fraction representation of
the number $r$ is a sequence of numbers $[x_0,\,x_1,\,\ldots \, x_n]$,
satisfying:
\[r=x_0+\frac{1}{x_1 +\frac{1}{\ddots + \frac{1}{x_n}}}\]
\end{definition}

There is no reason why we should only consider finite continued
fractions, so infinite fractions are also permitted.

For the time being, in this paper, we will make the numbers in the
sequence $x_0\ldots x_n$ be integers, where it is to be understood
that these integers may be arbitrarily large. A finite continued
fraction with integer terms can be used as a compact method to represent
a rational number.  It is possible to calculate the values of the
integers $x_0,\,x_1,\,\ldots \, x_n$ from $r$ in the following way.
Let $r = r_0$; we compute
\[r_0 = x_0 + \frac{1}{r_1},\,
  r_1 = x_1 + \frac{1}{r_2},\,\ldots,\,r_n = x_n \]
where at each step we choose an integer $x_n$ that is {\em close} to
$r_n$. Let us give $x_n$ the value $x_n = f(r_n)$. There are a number
of choices for the function $f$. Obvious candidates are the rounding
operations.
\[\begin{array}{llllll}
\mbox{{\bf floor}} &\lfloor r\rfloor & = & z&\mbox{such that} &
0 \leq r-z < 1;\\
\mbox{{\bf round}} &\lfloor r\rceil & = &z&\mbox{such that} &
-\frac{1}{2} < r-z \leq \frac{1}{2};\\
\mbox{{\bf ceiling}} &\lceil r\rceil & = &z&  \mbox{such that} &
-1 < r-z \leq 0.
\end{array}\]
The choice of \mbox{{\bf floor}} leads to Gosper's {\bf N}-fractions.
As an alternative, we can give the {\bf Z}-fractions for the following
numbers, which are obtained by using the \mbox{\tt round}\indexTT{round} operat
ion to generate
the continued fraction.
\[\begin{array}{lll}
\sqrt{2} & = & [1,\,2,\,\ldots,\,2,\,\ldots]\\
\sqrt{3} & = & [2,\,-4,\,4,\,\ldots,\,-4,\,4,\,\ldots]\\
\phi     & = & [2,\,-3,\,3,\,\ldots,\,-3,\,3,\,\ldots]\\
e & = & [3,\,-4,\,2,\,5,\,-2,\,-7,\,\ldots,\,2,\,4n+5,\,-2,\,-4n-7,\,\ldots]
\end{array}\]
The interested reader can use the definitions given earlier, in
conjunction with a series expansion for each irrational number, to
prove that these continued fractions are correct.

\section{Computable Continued Fractions}
\label{sc:comp-cf}

Although continued fractions are a good representation because they
can be used lazily, there remains a problem with selecting either {\bf
N}-fractions or {\bf Z}-fractions as representations of real numbers:
neither {\bf floor} nor {\bf round} are computable operations. This
means that for some numbers it is not possible for a computer to
construct the continued fraction, even though it exists. What is
needed is a computable analogue to {\bf floor} or {\bf round}; this is
implicitly defined by the function \mbox{\tt cfBound}\indexTT{cfBound}, which i
ndicates the
range in which a continued fraction must lie given its leading term
\mbox{\tt x}.
\begin{verbatim}
> cfBound :: QRational -> Interval
> cfBound x
>  = if qInfinite x then (1%%0,   1%%0)    else
>    if abs x < 2   then (-2%%1,  2%%1)    else
>    if x < 0       then (1%%2-x, x+1%%2)  else
>                        (x-1%%2, -x-1%%2)
\end{verbatim}
\indexDTT{cfBound}%
\indexDTT{cfBound}%
Notice that we have had to extend {\sc Haskell}'s \mbox{\tt Rational}\indexTT{R
ational} numeric
type with infinite values, so that we can effectively deal with open
intervals of the form: $(x,\,\infty )$. It is also worth noting that
-- following Vuillemin's lead -- open intervals of the form $(x,\,y)$
in which $x>y$ are to be interpreted as the interval: $(-\infty
,\,y)\,\cup\,(x,\,\infty )$

The important point to note about {\bf E}-fractions is that although
we have weakened the conditions upon the terms in the continued
fraction so that there is redundancy, the conditions are still strong
enough to ensure effective convergence of the rational approximations.

We now consider ways to implement arithmetic on continued fractions.

\section{The Algebraic Algorithm}
\label{sc:aa}

Having seen various sorts of continued fractions, and how to turn them
into rational approximations, we move on to consider the
implementation of ordinary arithmetic operations on them.  The
simplest is the algebraic algorithm \mbox{\tt algebraicAlgorithm}\indexTT{algeb
raicAlgorithm}, which we
investigate in this section.

The algebraic algorithm satisfies the following condition:
\[\mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,
\left(\begin{array}{cc}n_0&n_1\\d_0&d_1\end{array}\right)\, r =
\frac{n_0r+n_1}{d_0r+d_1}\]

That is: \mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm} is a functio
n that takes a state or
homography (consisting of four integers) and a real number
(represented by a continued fraction) and returns the real number
$(n_0r+n_1)/(d_0r+d_1)$ (again represented as a continued fraction).

Let us briefly look at the domain of definition for this
algorithm. The first constraint is that if the determinant of the
homography be non-zero; if the determinant {\em is} zero then the
result is independent of the value of $r$. This check need only be
applied once, as the state transformations do not affect the
determinant of the homography.

The main use of the \mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm} i
s to perform operations on
real numbers, such as adding a rational to a real or multiplying a
rational by a real. Adding the rational $n/d$ is achieved by using the
following initial configuration:
\[\frac{n}{d}+r = \mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,
\left(\begin{array}{cc}d&n\\0&d\end{array}\right)\, r,\]
and multiplication by $n/d$ is given by:
\[\frac{n}{d}\times r = \mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorith
m}\,
\left(\begin{array}{cc}n&0\\0&d\end{array}\right)\, r.\]
We observe that the following properties arise with the continued
fraction representation of the number $r$ and the result. Firstly if
the continued fraction is finite, then at some stage we will need to
consider the case where $r$ is infinite; we define the algebraic
algorithm in this case in the following manner\footnote{The fraction
$n_0/d_0$ will need to be converted to a continued fraction -- but this is
easy.}:
\[\mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,
\left(\begin{array}{cc}n_0&n_1\\d_0&d_1\end{array}\right)\,\infty =
\frac{n_0}{d_0}.\]

Suppose that we are applying the function
\mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm} to a number of the fo
rm $x_0+\frac{1}{r}$. Then it is
easy to verify the following identity:
\begin{eqnarray*}
\lefteqn{\mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,
\left(\begin{array}{cc}n_0&n_1\\d_0&d_1\end{array}\right)\,(x_0+\frac{1}{r})} \
\
& = & \mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,
\left(\begin{array}{cc}n_0x_0+n_1&n_0\\d_0x_0+d_1&d_0\end{array}\right)\,r.
\end{eqnarray*}

Another identity that holds for the function \mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm} is that for any
$y_0$:

\begin{eqnarray*}
\lefteqn{
\mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,\left(\begin{array}{
cc}n_0&n_1\\d_0&d_1\end{array}\right)\,r}\\
&= & y_0 + \frac{1}{\mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,
\left(\begin{array}{cc}d_0&d_1\\n_0-d_0y_0&n_1-d_1y_0\end{array}\right)\,r }.
\end{eqnarray*}

These two identities define the {\em consumption} and {\em emission}
of terms in the continued fraction representations of the initial real
number $r$ or the resultant real number. The critical question is
therefore: which of these operations should we choose to perform,
given a choice? The answer is straightforward if we wish to be as lazy
as possible: we emit a term provided it is sufficiently close to the
value we require for an {\bf E}-fraction, otherwise we consume a term
from the input.

Let's be a bit more explicit: to evaluate the following state
\[r =\mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,
\left(\begin{array}{cc}n_0&n_1\\d_0&d_1\end{array}\right)\,(x+\frac{1}{x'}),\]
the Algebraic Algorithm goes through the following steps.

\begin{enumerate}
\item After checking that the input continued fraction hasn't terminated,
we use the first term $x$ to determine the bounds for $x+1/x'$, using
the function \mbox{\tt cfBound}\indexTT{cfBound}.
\item From this we can determine the bounds on the value of $r$, by
applying the homography.
\item If the bounds $(i,\,s)$ on $r$ are such that \mbox{\tt cfBound\ o}
contains the interval $(i,\,s)$, then we may output \mbox{\tt o} as the next
term in the continued fraction.  Alternatively, we absorb the term
$x$.
\end{enumerate}

\subsection{Generating Output}

One alternative use of the algebraic algorithm is to generate output;
here we replace the test that determines whether we have sufficient
accuracy to emit a term, by one that determines whether we have
calculated the answer to sufficient accuracy yet. The alternative is
to continue absorbing terms.
\begin{verbatim}
> outputAlgorithm homography@([n0,n1],[d0,d1]) [] = n0 %% d0
> outputAlgorithm homography@([n0,n1],[d0,d1]) xs'@(x:xs)
>  = if i <= s && s-i < accuracy
>    then algebraicEval homography [x%%1]
>    else outputAlgorithm ([n0*x+n1,n0],[d0*x+d1,d0]) xs
>    where (i,s) = algebraicBound homography [x%%1]
\end{verbatim}
\indexDTT{outputAlgorithm}%
\indexDTT{outputAlgorithm}%
The main use to which we will put the Algebraic Algorithm is as a base
case for the more useful Quadratic Algorithm, which we now consider.

\section{Quadratic Algorithm}
\label{sc:qa}

The problem at the moment is that we are limited to rational
operations on any particular real number we care to consider. What we
would really like to do is to add and multiply two real numbers
represented as continued fractions.

This is what Gosper's Quadratic Algorithm does. His original papers
\shortcite{GOSPER80a,GOSPER81a} are unfortunately a little difficult to
obtain but a pr\'{e}cis is available in \cite{PEYTON-JONES84b}. This
paper has as an added bonus, an implementation of continued fractions
in SASL. The Quadratic Algorithm calculates the following function
(which, as we shall see, can be used to implement the basic arithmetic
operations):

\[\mbox{\tt quadraticAlgorithm}\indexTT{quadraticAlgorithm}\,
\left(\begin{array}{cccc}n_0&n_1&n_2&n_3\\d_0&d_1&d_2&d_3\end{array}\right)\, x
\,y =
\frac{(n_0x+n_1)y+(n_2x+n_3)}{(d_0x+d_1)y+(d_2x+d_3)}.\]
Notice that this time the state consists of eight integers and that
the function \mbox{\tt quadraticAlgorithm}\indexTT{quadraticAlgorithm} takes tw
o real numbers (represented as continued
fractions) returning another real number.

We first observe that the following base cases can be given:
\begin{eqnarray*}
\lefteqn{\mbox{\tt quadraticAlgorithm}\indexTT{quadraticAlgorithm}\,
\left(\begin{array}{cccc}n_0&n_1&n_2&n_3\\d_0&d_1&d_2&d_3\end{array}\right)\,\i
nfty\,y}\\
 &= &
\mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,
\left(\begin{array}{cc}n_0&n_2\\d_0&d_2\end{array}\right)\,y,
\end{eqnarray*}
and
\begin{eqnarray*}
\lefteqn{\mbox{\tt quadraticAlgorithm}\indexTT{quadraticAlgorithm}\,
\left(\begin{array}{cccc}n_0&n_1&n_2&n_3\\d_0&d_1&d_2&d_3\end{array}\right)\,x\
,\infty}\\
& =&
\mbox{\tt algebraicAlgorithm}\indexTT{algebraicAlgorithm}\,
\left(\begin{array}{cc}n_0&n_1\\d_0&d_1\end{array}\right)\,x.
\end{eqnarray*}

As we would expect, we can derive similar identities to those deduced
for the algebraic algorithm. Firstly, absorbing a term from each
continued fraction\footnote{To be as lazy as possible, we should
follow Gosper\shortcite{GOSPER81a} and only absorb a term from one of the
continued fractions at a time. The idea is to select the one which
will cause the most rapid convergence.}, we observe that the following
holds:
\begin{eqnarray*}
\lefteqn{\mbox{\tt quadraticAlgorithm}\indexTT{quadraticAlgorithm}\,
\left(\begin{array}{cccc}n_0&n_1&n_2&n_3\\d_0&d_1&d_2&d_3\end{array}\right)
\,(x_0+\frac{1}{x})
\,(y_0+\frac{1}{y})=}\\
\lefteqn{\mbox{\tt quadraticAlgorithm}\indexTT{quadraticAlgorithm}}\\
&&\left(\begin{array}{cccc}
n_0x_0y_0+n_1y_0+n_2x_0+n_3&n_0y_0+n_2&n_0x_0+n_1&n_0\\
d_0x_0y_0+d_1y_0+d_2x_0+d_3&d_0y_0+d_2&d_0x_0+d_1&d_0
\end{array}\right)\,x\,y.\end{eqnarray*}

Emitting a term, $y_0$, remains essentially as described for the Algebraic
Algorithm:
\begin{eqnarray*}
\lefteqn{\mbox{\tt quadraticAlgorithm}\indexTT{quadraticAlgorithm}\,
\left(\begin{array}{cccc}n_0&n_1&n_2&n_3\\d_0&d_1&d_2&d_3\end{array}\right)
\,x\,y =}\\
&&z_0 + \frac{1}{\mbox{\tt quadraticAlgorithm}\indexTT{quadraticAlgorithm}\,\mbox{\tt hom}\indexTT{hom}\,x\,y}\\
\lefteqn{\mbox{where}}\\
\mbox{\tt hom}\indexTT{hom} & = &
 \left(\begin{array}{cccc}d_0&d_1&d_2&d_3\\n_0-d_0z_0&n_1-d_1z_0&n_2-d_2z_0&n_3
-d_3z_0
             \end{array}\right)
\end{eqnarray*}

To use the Quadratic Algorithm to add two numbers $x$ and $y$ we
observe that
\[x+y = \mbox{\tt quadraticAlgorithm}\indexTT{quadraticAlgorithm}\,
\left(\begin{array}{cccc}0&1&1&0\\0&0&0&1\end{array}\right)
\,x\,y\]
Similar initial states may be computed for the other arithmetic
operations. We next consider the problem of transcendental functions.

\section{Transcendental Functions}
\label{sc:tf}

If we are not able to compute logarithms, exponentials, and
trigonometric functions then our real number implementation is
severely limited.  Fortunately these functions can be represented as
continued fractions, as has long been known
\cite{GAUSS1812a,GAUSS1876a}. In this section we show how to use
Gauss' continued fraction representation of $F(\alpha , 1, \gamma ;
x)$ to implement the basic transcendental functions: $\exp$, $\log$,
$\tan$, and $\arctan$.

As a reminder, $F(\alpha , \beta , \gamma ; x)$ is the following series:
\[
[1 + \frac{\alpha \beta}{1! \gamma}x +
\frac{\alpha(\alpha +1) \beta (\beta +1)}{2! \gamma(\gamma +1)}x^2 +
\frac{\alpha(\alpha +1)(\alpha +2) \beta (\beta +1)(\beta +2)}%
{3! \gamma(\gamma +1)(\gamma +2)}x^3 +
\ldots \]

Modifying Gauss' formulation of the continued fraction we get that
$F(\alpha , 1, \gamma ; x)$ is:

\begin{eqnarray*}
 [0,~1,~\frac{1}{\alpha}(\frac{\gamma}{-x}),
~\frac{\alpha}{\gamma-\alpha}(\gamma+1),~
p_0(\frac{\gamma +2}{-x}),~p'_0(\gamma +3)~\ldots~ &&\\
\qquad p_{n}(\frac{\gamma +2n+2}{-x}),~ p'_{n}(\gamma +2n+3),~ \ldots ]&&
\end{eqnarray*}
where
\[p_n = \frac{1}{\alpha}\prod_{i=0}^n \frac{(\gamma - \alpha +i)(i+1)}%
{(\alpha +i+1)(\gamma +i)}\]
and
\[p'_n = \frac{\alpha}{\gamma-\alpha}
\prod_{i=0}^n \frac{(\alpha +i+1)(\gamma +i)}%
{(\gamma - \alpha +i+1)(i+2)}\]

Fortunately, the $p_n$ and $p_n'$ coefficients often simplify.

\subsection{Exponential}

Gauss gives the following representation of $\exp(x)$:
$\lim_{k\to\infty}F(k,1,1;\frac{x}{k})$. If we expand this we have
the following continued fraction:

\[ [0,~1,~1(\frac{-1}{x}),-2,~3(\frac{1}{x}),~\frac{4}{2},~
5(\frac{-1}{x}),~\frac{-6}{3},~\ldots~]\]
We then observe that $\exp(x) = \frac{1}{\exp(-x)}$ getting:
\[ [1,~\frac{1}{x},~-2,~\frac{-3}{x},~2,~\frac{5}{x},~-2,~\ldots ] \]

Setting $x$ to $1$ we have the following continued fraction for $e$:
\begin{verbatim}
> exp1CF :: [QRational]
> exp1CF = 1: f 0
>          where f n = (4*n+1): (-2): (-(4*n+3)): 2: f (n+1)
\end{verbatim}
\indexDTT{exp1CF}%
\indexDTT{exp1CF}%
To use this continued fraction to generate the continued fraction for
$\exp(x)$, where $x\in {\cal Q}$, we first test to see whether
$\mbox{\tt abs}\indexTT{abs}\,\mbox{\tt x}< 1$.  If it is then we multiply each
 odd term of \mbox{\tt exp1CF}\indexTT{exp1CF}
by $1/x$ which results in each term of the continued fraction (apart
from the second one) will have an absolute magnitude greater than $2$.
Alternatively we can multiply the even terms by $x$ and divide the
result by $x$. This is marginally more stable than Vuillemin's
formulation.
\begin{verbatim}
> expQ  :: QRational -> ContinuedFraction
> expQ  x
>  = aaQ hom ts2
>    where (ts1,ts2)   = splitAt 2 cf
>          hom         = absorbQ startH ts1
>          (startH,cf) = if abs x < 1 then
>                         (([1,0],[0,1]), oddQ x exp1CF) else
>                         (([qDenominator x,0],
>                           [0,qNumerator x]),
>                                    evenQ (1/x) exp1CF)
\end{verbatim}
\indexDTT{expQ}%
\indexDTT{expQ}%
The \mbox{\tt aaQ}\indexTT{aaQ} function is a modified version of the \mbox{\tt
 algebraicAlgorithm}\indexTT{algebraicAlgorithm}
that accepts continued fractions with rational terms; it still outputs
integer-termed continued fractions. The \mbox{\tt absorbQ}\indexTT{absorbQ} fun
ction absorbs
terms of the finite continued fraction, to create a starting
homography.

We use an exactly similar process for the case of continued fraction
arguments. This time, however, we are simply concerned with whether
$x$ might be $0$. If it is, then calculating $1/x$ will likely lead to
disaster, and is therefore avoided.
\begin{verbatim}
> expR :: ContinuedFraction -> ContinuedFraction
> expR x = if mightBeZero x
>          then processQS x (splitAt 6 (oddR exp1CF))
>          else quadraticAlgorithm ([0,1,1,0],[0,1,0,0]) x cf2
>          where cf2 = processQS x (splitAt 6
>                                       (oddR (tail exp1CF)))
\end{verbatim}
\indexDTT{expR}%
\indexDTT{expR}%
\subsection{Logarithm}

Vuillemin's continued fraction is too unstable for values of $x-1$
which aren't near $0$. So instead we use Gauss' formula:
\[\log(\frac{1+x}{1-x}) = 2xF(\frac{1}{2},1,\frac{3}{2}; x^2)\]
It works because the transformation eliminates those values of $x$
in the above expansion that would cause problems, {\it e.g.} $|x|
\geq 1$.

The continued fraction for $F(\frac{1}{2},1,\frac{3}{2}; x^2)$ is
closely related to that for $\arctan(x)$, because:
\[\tanh^{-1}(x) = \frac{1}{2} \log(\frac{1+x}{1-x})\]
and
\[\imath \tanh(x) = \tan(\imath x).\]
\begin{verbatim}
> logQ :: QRational -> ContinuedFraction
> logQ x = aaQ startH ts2
>          where (ts1,ts2) = splitAt 2 (oddQ (-t*t) atan1CF)
>                t         = (x-1)/(x+1)
>                tn        = qNumerator t
>                td        = qDenominator t
>                startH    = absorbQ ([-2*td,0],[0,tn]) ts1
\end{verbatim}
\indexDTT{logQ}%
\indexDTT{logQ}%
The nice part is that by requiring the multiplier \mbox{\tt -t*t} to be less
than \mbox{\tt 1}, we place no constraints on \mbox{\tt x} provided that it is
already
in the range $(0,\infty )$. Because there are no range decisions to be
taken, the continued fraction argument function \mbox{\tt logR}\indexTT{logR} i
s analogous to
that of \mbox{\tt logQ}\indexTT{logQ}.

\subsection{Tangent}

For this function we use Lambert's Continued Fraction:
\[\tan(x) = [0,~\frac{1}{x},~\frac{-3}{x},~\frac{5}{x},~\frac{-7}{x},~\ldots]\]

It is much more convenient if we can obtain a fraction with some
constant terms in it so we observe the following transformation is
possible:
\[\tan(x) = \frac{1}{x}[0,~\frac{1}{x^2},~-3,~\frac{5}{x^2},~-7,~\ldots]\]
provided that $x$ is non-zero.
\begin{verbatim}
> tan1CF :: [QRational]
> tan1CF = 0: f 0 where f n = (4*n+1) : (-(4*n+3)) : f (n+1)
\end{verbatim}
\indexDTT{tan1CF}%
\indexDTT{tan1CF}%
\begin{verbatim}
> tanQ  :: QRational -> ContinuedFraction
> tanQ  x = if x /= 0 then aaQ startH ts2
>                     else error "tanQ: 0 argument"
>           where n         = qRound (abs (x/2)) + 1
>                 (ts1,ts2) = splitAt n (bothQ x tan1CF)
>                 startH    = absorbQ ([1,0],[0,1]) ts1
\end{verbatim}
\indexDTT{tanQ}%
\indexDTT{tanQ}%
The case for continued fraction arguments is \mbox{\tt tanR}\indexTT{tanR}, and
 works
similarly; the cotangent function \mbox{\tt cotR}\indexTT{cotR} merely inverts
the top-level
division.

\subsection{Arctangent}

Recall, from the discussion of the continued fraction for $\log$, that
the $\arctan(x)$ is $xF(\frac{1}{2},1,\frac{3}{2};-x^2)$
The terms in this continued fraction are tending towards $\pi$ and $1$.
\begin{verbatim}
> atan1CF :: [QRational]
> atan1CF
>   = 0: 1: 3: 5/4: f (8/9) 0
>     where f :: QRational -> QRational -> [QRational]
>           f p n = (7/2 + 2*n)*p: (9/2 + 2*n)*p': f p'' (n+1)
>                   where p'  = 1/(p*(n+2)*(n+2))
>                         p'' = 1/(p'*(n+5/2)*(n+5/2))
\end{verbatim}
\indexDTT{atan1CF}%
The \mbox{\tt atanQ}\indexTT{atanQ} function is valid for $x \in (-\infty,\,\infty)$.

We note the following identities:
\[\mbox{for $x>0$ } \arctan(x) = \frac{\pi}{2} -\arctan(\frac{1}{x})\]
and
\[\mbox{for $x<0$ } \arctan(x) = -\frac{\pi}{2} -\arctan(\frac{1}{x})\]

\begin{verbatim}
> atanQ :: QRational -> ContinuedFraction
> atanQ x
>  = if x > 1  then
>       quadraticAlgorithm ([0,-1,2,0] ,[0,0,0,1])
>                          atan1 (cf (1/x))
>    else if x < -1 then
>       quadraticAlgorithm ([0,-1,-2,0],[0,0,0,1])
>                          atan1 (cf (1/x))
>    else  cf x
>    where
>    cf x = aaQ startH ts2
>           where
>           (ts1,ts2) = splitAt 4 (oddQ (x*x) atan1CF)
>           startH    = absorbQ ([qDenominator x,0],
>                                [0,qNumerator x]) ts1
\end{verbatim}
\indexDTT{atanQ}%
\indexDTT{atanQ}%
Because we will be using it to help improve the stability of the
$arctan$ function \mbox{\tt atanR}\indexTT{atanR}, we pre-define the constant c
ontinued
fraction $\mbox{\tt atan1}\indexTT{atan1} = \pi/4$.
\begin{verbatim}
> atan1 :: ContinuedFraction
> atan1 = atanQ (1%%1)
\end{verbatim}
\indexDTT{atan1}%
\indexDTT{atan1}%
As in the case of \mbox{\tt expR}\indexTT{expR} -- because we are now distingui
shing between
different domains of definition -- we inspect the leading term of the
argument continued fraction.
\begin{verbatim}
> atanR :: ContinuedFraction -> ContinuedFraction
> atanR []         = algebraicAlgorithm ([2,0],[0,1]) atan1
> atanR xs@(x:xs')
>  = if x > 1 then
>       quadraticAlgorithm ([0,-1,2,0] ,[0,0,0,1])
>                          atan1 ys else
>    if x < -1 then
>       quadraticAlgorithm ([0,-1,-2,0],[0,0,0,1])
>                          atan1 ys else
>       atanRdirect xs
>    where ys = atanRdirect (algebraicAlgorithm ([0,1],[1,0]) xs)
\end{verbatim}
\indexDTT{atanR}%
\indexDTT{atanR}%
\indexDTT{atanR}%
The function \mbox{\tt atanRdirect}\indexTT{atanRdirect} deals with the straigh
tforward case where
the argument is within the domain of definition.

\subsection{Processing Irrational Transcendentals}

To produce continued fractions for the transcendental functions with
continued fraction arguments we use an ``infinite state machine''.
This infinite state is an infinite list of sub-states, each consisting
of a rational, and a homography for the Quadratic Algorithm.
\begin{verbatim}
> type State = (QRational, Homography)
\end{verbatim}
\indexDTT{type}%
To set up the initial continued fraction for the ``infinite state
machine'' from the initial continued fraction we use \mbox{\tt oddR}\indexTT{od
dR}.
If the initial continued fraction is
\[[\frac{a_0}{b_0},~\frac{a_1}{b_1},~\frac{a_2}{b_2},~\frac{a_3}{b_3},~\ldots]\]
then the ``infinite state'' will be:
\[[\left(\frac{a_0}{b_0},
   \left(\begin{array}[c]{cccc}0&a_1&b_1&0\\b_1&0&0&0\end{array}\right),~
\right)
\left(\frac{a_2}{b_2},
   \left(\begin{array}[c]{cccc}0&a_3&b_3&0\\b_3&0&0&0\end{array}\right)
\right),~
\ldots]\]

The processing of the infinite state and the continued fraction is
initiated by \mbox{\tt processQS}\indexTT{processQS}. What it does is to absorb
 leading terms of the state

\begin{verbatim}
> processQS :: ContinuedFraction -> ([State],[State])
>                                -> ContinuedFraction
> processQS x (hs1,hs2) = preProcess x hs1 (qsSetupAlt hs2 x)
\end{verbatim}
\indexDTT{processQS}%
\indexDTT{processQS}%
This in turn makes use of \mbox{\tt preProcess}\indexTT{preProcess} which is us
ed to absorb some of
the leading terms (\mbox{\tt hs1}\indexTT{hs1}) and \mbox{\tt qsSetupAlt}\indexTT{qsSetupAlt} which is used to process
the remainder (\mbox{\tt hs2}\indexTT{hs2}).
\begin{verbatim}
> preProcess :: ContinuedFraction -> -- the argument CF
>               [State]           -> -- leading parts of inf state
>               ContinuedFraction -> -- accumulating CF
>               ContinuedFraction    -- the resulting CF
> preProcess _ []         c = c
> preProcess x ((i,h):hs) c
>  = algebraicAlgorithm ([ni,nd],[nd,0])
>                       (quadraticAlgorithm h x (preProcess x hs c))
>    where ni = qNumerator i
>          nd = qDenominator i
\end{verbatim}
\indexDTT{preProcess}%
\indexDTT{preProcess}%
\indexDTT{preProcess}%
To process the remainder of the infinite state, we use \mbox{\tt qsSetupAlt}\indexTT{qsSetupAlt}.
\begin{verbatim}
> qsSetupAlt :: [State] -> ContinuedFraction
>                       -> ContinuedFraction
> qsSetupAlt hs x = aaQ ([1,0],[0,1]) (qsProcess (hs, x))
\end{verbatim}
\indexDTT{qsSetupAlt}%
\indexDTT{qsSetupAlt}%
\subsection{Quadratic Process}

In essence the state for the algorithm holds:
\begin{enumerate}
\item the infinite list of \mbox{\tt Rational}\indexTT{Rational}/\mbox{\tt Homo
graphy}\indexTT{Homography} pairs; and
\item the infinite continued fraction of the argument.
\end{enumerate}

We advance by absorbing one term from the continued fraction or by
considering one more term of the infinite state.
\begin{verbatim}
> qsProcess :: ([State], ContinuedFraction) -> [QRational]
> qsProcess    (ihs, [])    = asProcess ihs
> qsProcess st@(ihs, (_:_)) = qsNext 1 st
\end{verbatim}
\indexDTT{qsProcess}%
\indexDTT{qsProcess}%
\indexDTT{qsProcess}%
The decision about whether sufficient accuracy has been achieved is
decided by \mbox{\tt qsNext}\indexTT{qsNext}. To do this a call to \mbox{\tt qs
Term}\indexTT{qsTerm} is made. This
returns a list of output values and a modified state (\mbox{\tt ihs'}).  If the
output list is sufficiently long we can output them as part of the
resulting continued fraction. Alternatively we can absorb another term
of the argument continued fraction. In either case we consider one
more term of the infinite state.
\begin{verbatim}
> qsNext :: Int -> ([State], ContinuedFraction) -> [QRational]
> qsNext n (ihs, []) = asProcess (qsEnd ihs)
> qsNext n st@(((i,h):ihs), xs'@(x:xs))
>  = if length outs > 1
>       then init outs ++ qsNext (n+1) (ihs',xs')
>       else qsNext (n+1) (qsIn x ((i,h): ihs), xs)
>    where (outs, (ihs',_)) = qsTerm n st
\end{verbatim}
\indexDTT{qsNext}%
\indexDTT{qsNext}%
\indexDTT{qsNext}%
The \mbox{\tt qsTerm}\indexTT{qsTerm} function considers the initial \mbox{\tt
n} terms of the infinite
state, and the first term of the continued fraction for the argument.
>From this it calculates a modified infinite state, in which, what is
effectively carry propogation has occurred.
\begin{verbatim}
> qsTerm :: Int -> ([State], ContinuedFraction)
>          -> ([QRational],([State], ContinuedFraction))
> qsTerm n (ihs,xs)
>  = (outs, (ihs',xs))
>    where (front,back@((i,_):_)) = splitAt n ihs
>          (outs,ihs') = foldr (qsTerm' (head xs))
>                                       ([i],back) front
\end{verbatim}
\indexDTT{qsTerm}%
\indexDTT{qsTerm}%
To deal with each individual term of the infinite state we use \mbox{\tt qsTerm
'}.
\begin{verbatim}
> qsTerm' :: Integer -> State -> ([QRational], [State]) ->
>                                ([QRational], [State])
> qsTerm' x ih@(i,h) (is,ihs)
>  = (is',((last is',h''):ihs))
>    where h' = qsAbsorb (init is) h
>          i' = last is
>          (is',h'') = qsEmit x i' ([i],h')
\end{verbatim}
This in turn uses \mbox{\tt qsAbsorb}\indexTT{qsAbsorb} and \mbox{\tt qsEmit}\i
ndexTT{qsEmit} to propogate the carry and
emit output terms, respectively.
\begin{verbatim}
> qsAbsorb :: [QRational] -> Homography -> Homography
> qsAbsorb [] h = h
> qsAbsorb (i:is) h@([n0,n1,n2,n3],[d0,d1,d2,d3])
>  = qsAbsorb is ([n0*n+n2*d, n1*n+n3*d, n0*d, n1*d],
>                 [d0*n+d2*d, d1*n+d3*d, d0*d, d1*d])
>    where n = qNumerator i
>          d = qDenominator i
\end{verbatim}
\indexDTT{qsAbsorb}%
\indexDTT{qsAbsorb}%
\indexDTT{qsAbsorb}%
\mbox{\tt qsEmit}\indexTT{qsEmit} is essentially a variant of the Quadratic Alg
orithm, capable
of dealing with the output of more than $1$ term at once.
\begin{verbatim}
> qsEmit :: Integer -> QRational -> ([QRational], Homography)
>                                -> ([QRational], Homography)
> qsEmit x i ish@(is,h@([n0,n1,n2,n3],[d0,d1,d2,d3]))
>  = case quadraticOutput h [x%%1,i] of
>      Just o  -> qsEmit x i (is++[o%%1],
>                             ([d0,d1,d2,d3],
>                              [n0-d0*o, n1-d1*o,
>                                   n2-d2*o, n3-d3*o]))
>      Nothing -> ish
\end{verbatim}
\indexDTT{qsEmit}%
\indexDTT{qsEmit}%
To absorb the leading term of the argument continued fraction into
each Quadratic Homography we use \mbox{\tt qsIn}\indexTT{qsIn}.
\begin{verbatim}
> qsIn :: Integer -> [State] -> [State]
> qsIn = map . input
>        where input x (i,([n0,n1,n2,n3],[d0,d1,d2,d3]))
>                = (i, ([n0*x+n1, n0, n2*x+n3, n2],
>                       [d0*x+d1, d0, d2*x+d3, d2]))
\end{verbatim}
\indexDTT{qsIn}%
\indexDTT{qsIn}%
\subsection{Finite Continued Fractions}

It is possible to generate finite continued fractions, so we need a
way to deal with \mbox{\tt []} in the \mbox{\tt qsProcess}\indexTT{qsProcess}.
This results in a
degeneration to the algebraic process.

\mbox{\tt qsEnd}\indexTT{qsEnd} turns each Quadratic Homography into the equiva
lent Algebraic
Homography.
\begin{verbatim}
> qsEnd :: [State] -> [State]
> qsEnd = map input
>         where input (i,([n0,_,n2,_],[d0,_,d2,_]))
>                 = (i,([n0,n2],[d0,d2]))
\end{verbatim}
\indexDTT{qsEnd}%
\indexDTT{qsEnd}%
The function \mbox{\tt asProcess}\indexTT{asProcess} munges these homographies
to produce an answer.
\begin{verbatim}
> asProcess :: [State] -> [QRational]
> asProcess ihs = fst (head ihs) : asProcess (asNext ihs)
\end{verbatim}
\indexDTT{asProcess}%
\indexDTT{asProcess}%
\begin{verbatim}
> asNext :: [State] -> [State]
> asNext ((i,h@([n0,n1],[d0,d1])):ihs'@((i',_):ihs))
>  = case algebraicOutput h i' of
>      Just o  -> (o%%1, ([d0,d1], [n0-d0*o,n1-d1*o])): ihs'
>      Nothing -> asNext ((i,hIn i' h): asNext ihs')
>    where hIn x ([n0,n1],[d0,d1]) = ([n0*xn+n1*xd,n0*xd],
>                                     [d0*xn+d1*xd,d0*xd])
>                                    where xn = qNumerator x
>                                          xd = qDenominator x
\end{verbatim}
\indexDTT{asNext}%
\indexDTT{asNext}%
And that completes the implementation of the specified subset of
transcendental functions.

\section{Conclusion}

The system only computes the answer to some arithmetic expression to
whatever accuracy you specify. It may run out of memory (in which case
the program will inform you of this fact); it may fail to compute any
answer whatsoever (in which case you will wait for ever to observe
the output of the program). It does have major advantages over
traditional floating point numbers.  Firstly, you may be sure that any
answer it provides is accurate. Secondly, there is no need to re-code
an algorithm when more accuracy is required.

\section{Acknowledgements}

Jean Vuillemin's work \shortcite{VUILLEMIN87a} is clearly the major
source for the implementation work that I have undertaken. I would
also like to thank Geoffrey Burn, Simon Peyton Jones, John Robson,
Peter Sestoft, and Geraint Jones who have all contributed comments
upon preliminary versions of this paper. Of course, all errors and
indiscretions remain my own work.

\begin{thebibliography}{}

\bibitem[\protect\citename{Bishop and Bridges, }1985]{BISHOP85a}
Bishop,~E. and Bridges,~D. 1985.
\newblock {\em Constructive Analysis}.
\newblock Springer Verlag.

\bibitem[\protect\citename{Boehm and Cartwright, }1990]{Boehm90a}
Boehm,~H. and Cartwright,~R. 1990.
\newblock Exact Real Arithmetic: Formulating Real Numbers as Functions
\newblock In {\em Research Topics in Functional Programming}, pages 43--64.
  Addison-Wesley.

\bibitem[\protect\citename{Church, }1941]{CHURCH41a}
Church,~A. 1941.
\newblock {\em The Calculi of Lambda-Conversion}.
\newblock Princeton University Press, Princeton, N.J.

\bibitem[\protect\citename{Euclid}]{EUCLID}
Euclid.
\newblock Elements.

\bibitem[\protect\citename{Gauss, }1812]{GAUSS1812a}
Gauss,~C.F. 1812.
\newblock Disquisitiones generales circa serium infinitam
  $1+\frac{\alpha\beta}{1.\gamma}x + $ etc. pars prior.
\newblock In {\em Werke}, volume~3, pages 123--162. K\"{o}niglichen
  Gesellschaft der Wissenschaften, G\"{o}ttingen.

\bibitem[\protect\citename{Gauss }1876]{GAUSS1876a}
Gauss,~C.F. 1876.
\newblock {\em Werke}, volume~3.
\newblock K\"{o}niglichen Gesellschaft der Wissenschaften, G\"{o}ttingen.

\bibitem[\protect\citename{Gosper }1980]{GOSPER80a}
Gosper,~R.W. 1980.
\newblock Continued fraction arithmetic.
\newblock HAKMEM 101b, MIT.

\bibitem[\protect\citename{Gosper }1981]{GOSPER81a}
Gosper,~R.W. 1981.
\newblock Continued fraction arithmetic.
\newblock Unpublished Draft Paper.

\bibitem[\protect\citename{Peyton Jones }1984]{PEYTON-JONES84b}
{Peyton Jones},~S.L. 1984.
\newblock Arbitrary precision arithmetic using continued fractions.
\newblock Internal Note 1530, Deptartment of Computer Science, University
  College London.

\bibitem[\protect\citename{Pour El and Richards }1989]{POUR-EL89a}
{Pour-El},~M.B. and Richards,~J.I. 1989.
\newblock {\em Computability in Analysis and Physics}.
\newblock Perspectives in Mathematical Logic. Springer-Verlag, Heidelberg.

\bibitem[\protect\citename{Rice }1954]{RICE54a}
Rice,~H.G. 1954.
\newblock Recursive real numbers.
\newblock {\em Proceedings of the American Mathematical Society},
  5(5):784--791.

\bibitem[\protect\citename{Turing }1936]{TURING36a}
Turing,~A.M. 1936.
\newblock On computable numbers, with an application to the
  {E}ntscheidungsproblem.
\newblock {\em Proceedings of the London Mathematical Society}, 42(3):230--265.

\bibitem[\protect\citename{Vuillemin }1987]{VUILLEMIN87a}
Vuillemin,~J. 1987.
\newblock Arithm\'{e}tic r\'{e}elle exacte par les fractions continues.
\newblock Technical Report 760, Instituit National de Recherche en Informatique
  et en Automatique, Domaine de Voluceau, Roquencourt, BP105, 78153 Le Chesnay
  Cedex, France.

\end{thebibliography}
\end{document}


\documentclass{beamer}
\usepackage{lmodern} %Zorgt voor dat juiste lettergrootte is toegestaan
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{bbm}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows}

\usetheme{Frankfurt}
%\usecolortheme{rose}
%\usecolortheme{seahorse}
\usefonttheme[onlysmall]{structurebold}
\usefonttheme{professionalfonts}
\setbeamercovered{transparent} %Zorgt voor grijze tekst als die nog niet op de slide komt.

\theoremstyle{plain}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{property}[theorem]{Property}
\newtheorem{conjecture}[theorem]{Conjecture}

\theoremstyle{definition}
\newtheorem{exercise}[theorem]{Exercise}

\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}

\newcommand{\N}{\mathbb{N}}
\newcommand{\R}{\mathbb{R}}
\renewcommand{\P}{\mathbb{P}}
\newcommand{\E}{\mathbb{E}}
\newcommand{\V}{\mathbb{V}}
\newcommand{\1}{\mathbbm{1}}

\title{Paradoxes in probability theory}
\author{Mathijs Kolkhuis Tanke}
\institute{De Leidsche Flesch}
\date{March 20, 2019}

\begin{document}
\begin{frame}
	\titlepage
\end{frame}

\begin{frame} %Met deze commando begin je nieuwe slide
	\frametitle{Table of contents} %Titel van slide
	\tableofcontents
\end{frame}

\section{Monty Hall's problem}
\subsection*{Introduction}
\begin{frame}
\frametitle{The problem}
\begin{exampleblock}{Monty Hall's game}
\begin{itemize}
\item There are three doors called $a$, $b$ or $c$. One has a car, the others have a goat.
\item Player chooses a door. Without loss of generality choose $a$.
\item Game master opens one of the remaining doors. That door has a goat.
\item Player is asked to switch to the other unopened door.
\item Player wins contents of the door he ultimately chooses.
\end{itemize}
\end{exampleblock}

What is the probability of the other door having a car?
\end{frame}

\begin{frame}
\frametitle{Early `solutions'}
What is the probability of the other door having a car?

\begin{columns}
\column{0.5\linewidth}
\begin{block}{Answer is $\frac{1}{2}$:}
\begin{itemize}
\item There are two doors when the player can switch.
\item One has a car, the other not.
\item The other door has $\frac{1}{2}$ chance of having a car.
\end{itemize}
\end{block}\pause
\column{0.5\linewidth}
\begin{block}{Answer is $\frac{2}{3}$:}
\begin{itemize}
\item Chance of doors $b$ and $c$ having a car is $\frac{2}{3}$.
\item One of them is opened.
\item Thus the other door has chance~$\frac{2}{3}$ of having a car.
\end{itemize}
\end{block}
\end{columns}\pause
\vspace{1em}
In fact, both are wrong. Probability ranges from $\frac{1}{2}$ to $1$.
\end{frame}
\subsection*{Sigma-algebra}
\begin{frame}
\frametitle{Probability spaces}
\begin{definition}[$\sigma$-algebra]
Let $\Omega$ be a set and $\mathcal{P}(\Omega)$ its power set.

The set $\Sigma\subseteq\mathcal{P}(\Omega)$ is a \emph{$\sigma$-algebra} iff
\begin{enumerate}
	\item $\Omega\in\Sigma$,
	\item $\Sigma$ is closed under countable unions,
	\item $\Sigma$ is closed under countable intersections and
	\item $\Sigma$ is closed under complement.
\end{enumerate}
\end{definition}
\pause
\begin{definition}[Probability space]
Let $\Omega$ be a set and $\Sigma$ a $\sigma$-algebra on $\Omega$. Let $\P$ be a probability measure on $\Sigma$.

Then $(\Omega,\Sigma,\P)$ is a probability space.
\end{definition}
\end{frame}

\begin{frame}
\frametitle{Conditional expectation}
We need to extend $\P[A|B]=\frac{\P[A\cap B]}{\P[B]}$.\pause
\begin{definition}[Conditional expectation]
Let $(\Omega,\Sigma,\P)$ be a probability space.

Let $\Sigma'\subseteq\Sigma$ be a sub-$\sigma$-algebra of $\Sigma$.

The \emph{expectation value of $X$ conditioned on $\Sigma'$} is $Y=\E[X|\Sigma']$ s.t.
\begin{itemize}
	\item $Y$ is $\Sigma'$-measurable,
	\item $\E[|Y|]$ is finite and
	\item $\int_A Yd\P=\int_A Xd\P$ for all $A\in\Sigma'$.
\end{itemize}
\end{definition}\pause

\begin{definition}[Conditional probability]
The \emph{probability of $A$ conditioned on $\Sigma'$} is $\P[A|\Sigma']=\E[\1_A|\Sigma']$.
\end{definition}
\end{frame}

\begin{frame}
\frametitle{Back to Monty Hall}
The situation:
\begin{itemize}
	\item $\mathcal{X}=\{a,b,c\}$ is set of doors player can choose.
	\item Take $\Omega=\mathcal{X}$.
	\item $\sigma$-algebra is $\mathcal{P}(\Omega)$.
	\item Condition on smallest $\sigma$-algebra having $\{a,b\}$ and $\{a,c\}$.
\end{itemize}
\pause

However, $\sigma(\{a,b\},\{a,c\})=\mathcal{P}(\Omega)$ is original $\sigma$-algebra.

No information is gained using this conditioning!
\end{frame}

\begin{frame}
\frametitle{Extending the set of possibilities}
The situation:
\begin{itemize}
	\item $\mathcal{X}=\{a,b,c\}$ is set of doors player can choose.
	\item $\mathcal{Y}=\{b,c\}$ is set of doors Monty Hall can open.
	\item Take $\Omega=\mathcal{X}\times\mathcal{Y}$.
	\item $\sigma$-algebra is $\mathcal{P}(\Omega)$.
	\item $X$: r.v.~on $\mathcal{X}$ player choosing a door.
	\item $Y$: r.v.~on $\mathcal{Y}$ which door to open after player's choice.
\end{itemize}
\pause

Then
\begin{align*}
\P[X=a|Y=b]&=\frac{\P[X=a,Y=b]}{\P[Y=b]}\\
&=\frac{\P[X=a,Y=b]}{\sum_{x\in \mathcal{X}}\P[Y=b|X=x]\P[X=x]}.
\end{align*}

$\P[X=x]$ is unknown, thus the conditional probability cannot be calculated. Real answer has many variables.
\end{frame}

\begin{frame}
\frametitle{Example}

\begin{exampleblock}{Probability $\frac{1}{2}$}
If
\begin{itemize}
\item the car is distributed uniformly between the doors,
\item door $a$ is first chosen,
\item door $a$ actually has the car and
\item Monty Hall never opens door $b$ in this case,
\end{itemize}
then probability of having the correct door is $\frac{1}{2}$.
\end{exampleblock}
\end{frame}

\section{Borel-Kolmogorov paradox}
\subsection*{The situation}
\begin{frame}
\frametitle{The Borel-Kolmogorov paradox}

\begin{exampleblock}{The setting of the Borel-Kolmogorov paradox}
\begin{itemize}
	\item Take a sphere and distribute points uniformly.
	\item Draw a great circle through one point.
	\item Mask all distributed points.
\end{itemize}
\end{exampleblock}
What is the distribution of the point being on the great circle?

\pause

Answer: depends on parametrizations.
\end{frame}

\begin{frame}
\frametitle{Bayesian probability}

Let $C$ be the great circle and $E\subset C$ a subset. Then
\[\P[E|C]=\frac{\P[E\cap C]}{\P[C]}=\frac{0}{0}.\]

The conditional probability is undefined!
\end{frame}

\begin{frame}
\frametitle{Widening our view}

Let $S=[0,2\pi]\times[0,\pi]$ be the unit sphere with spherical coordinates.

The uniform probability measure on $A\subseteq S$ is given by
\[\P[A]=\frac{1}{4\pi}\iint_A \sin\psi d\phi d\psi.\]
\end{frame}

\begin{frame}
\frametitle{Look at longitudes}
The $\sigma$-algebra of longitudes is
\[\mathfrak{C}=\left\{[0,2\pi]\times A|A\subseteq[0,\pi]\text{ is measurable}\right\}.\]
The conditional expectation on $\mathfrak{C}$ is
\[\E[X|\mathfrak{C}](\phi,\psi)=\frac{1}{2\pi}\int_0^{2\pi}X(\phi',\psi)d\phi'.\]
\pause

The conditional probability of $B=[\phi_1,\phi_2]\times\{\psi'\}$ on $[0,2\pi]\times\{\psi'\}$ is
\[\bar{\P}[B]=\frac{\phi_2-\phi_1}{2\pi},\]
thus the conditional probability stays uniform!
\end{frame}

\begin{frame}
\frametitle{Look at meridians}
The $\sigma$-algebra of meridians is
\[\mathfrak{M}=\left\{A\times [0,\pi]|A\subseteq[0,2\pi]\text{ is measurable}\right\}.\]
The conditional expectation on $\mathfrak{M}$ is
\[\E[X|\mathfrak{M}](\phi,\psi)=\frac{1}{2}\int_0^{\pi}X(\phi,\psi')\sin\psi'd\psi'.\]
\pause

The conditional probability of $B=\{\phi'\}\times[\psi_1,\psi_2]$ on $\{\phi'\}\times[0,\pi]$ is
\[\bar{\P}[B]=\frac{1}{2}(\cos\psi_1-\cos\psi_2),\]
thus the conditional probability does not stay uniform!
\end{frame}

\subsection*{Solution}
\begin{frame}
\frametitle{Recap}

\begin{itemize}
\item Looking at longitudes the conditional distribution is uniform.
\item Looking at meridians the conditional distribution is cosine.
\end{itemize}

Which one is right?
\end{frame}

\begin{frame}
\begin{theorem}[Gyenis, Hofer-Szabó, Rédei (2017)]
Consider probability spaces $(S,\mathfrak{C},\P)$ and $(S,\mathfrak{M},\P)$, then those spaces are not isomorphic.
\end{theorem}\pause
\begin{proof}
\begin{itemize}
	\item Let $f\colon S\to S$ any measurable isomorphism.
	\item Let $h_f\colon\mathfrak{C}\to\mathfrak{M}$ be a Boolean algebra preserving bijection.
	\item If $C$ is longitude, $h_f(C)$ is a meridian.
	\item All meridians cross in $m_0$, set of north and south poles.
	\item Let $c_0$ such that $h_f(c_0)=m_0$. Note that $c_0$ are longitudes.
	\item Then
	\[\emptyset=h_f(\emptyset)=h_f(C\cap c_0)=h_f(C)\cap h_f(c_0)=h_f(C)\cap m_0=m_0.\]
\end{itemize}
\end{proof}
\end{frame}

\begin{frame}
\frametitle{Discussion}
Our two probability spaces are vastly different! So what now?

\begin{figure}[b]
\begin{center}
\includegraphics[width=0.6\linewidth]{SphericalDistribution_900.png}
\end{center}
\caption{Simulation by Eric Weisstein}
\end{figure}

\begin{itemize}
	\item Simulate the uniform distribution on the sphere by longitudes and meridians.
	\item If points are placed uniformly on longitudes or cosinely on meridians, the uniform distribution on the sphere is recovered!
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Asking the right question}
The question
\begin{alertblock}{}
`what is the conditional distribution on the zero set given I have points there'
\end{alertblock}
is unanswerable.
\vspace{1em}

The question for conditional probability must rather be:
\begin{exampleblock}{}
`if I want to simulate the original distribution using this parametrization, what should my conditional probability be?'
\end{exampleblock}
\end{frame}
\section{Two envelope problem}
\begin{frame}
\frametitle{The game}

\begin{exampleblock}{Two envelope game}
\begin{itemize}
	\item Game master picks a value $x$.
	\item Game master fills one envelope with $x$, other with $2x$.
	\item Game master gives an arbitrary envelope to the player.
	\item Player sees contents of his envelope.
	\item Player gets the option to switch to the other envelope.
	\item Should the player switch?
\end{itemize}
\end{exampleblock}
\pause
Call the player's envelope $A$, then
\[\E[B]=\frac{1}{2}\cdot \frac{x}{2}+\frac{1}{2}\cdot 2x=\frac{5}{4}x.\]
Player must always switch!

\end{frame}

\begin{frame}
\frametitle{Playing the game}

Can I get two volunteers?
\end{frame}

\begin{frame}
\frametitle{Probability space}
Probability space:
\begin{itemize}
	\item $\mathcal{X}=\{(x,2x),(2x,x)\}$,
	\item $\Sigma=\mathcal{P}(\mathcal{X})$,
	\item $\P$ is uniform.
\end{itemize}

Then
\begin{align*}
\E[B]&=\E[B|A=x]\P[A=x]+\E[B|A=2x]\P[A=2x]\\
&=\frac{1}{2}\cdot2x+\frac{1}{2}x=\frac{3}{2}x.
\end{align*}
\pause
Thus $\E[A]=\E[B]=\frac{3}{2}x$, switching does not help!

However, the player does not know $x$...
\end{frame}

\subsection*{Priors}
\begin{frame}
\frametitle{Prior on lowest value}
Let $X$ be the continuous r.v.~for the lowest value $x$ in the envelopes with density $f$.
\begin{itemize}
	\item If $\P[B=2a|A=a]=\P\left[B=\frac{a}{2}\middle|A=a\right]=\frac{1}{2}$, then $X$ is uniform on an unbounded set. This is not possible.
	\item Suppose $\E[X]<\infty$ to avoid the St.~Petersburg paradox.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Switching strategies}
\begin{definition}[Switching strategy]
Let $P\colon(0,\infty)\to[0,1]$ be arbitrary. Then $P$ is a \emph{switching strategy} if the player switches his envelope with probability $P(a)$ after observing $a$ in his envelope.
\end{definition}\pause
\begin{definition}[Gain]
Using switching strategy $P$, the player's \emph{gain} relative to never switching is
\[G=\frac{1}{2}\int_0^\infty xf(x)(P(x)-P(2x))dx.\]
\end{definition}\pause
Never switching has gain $G=0$. If $G>0$, then $P$ is a better strategy than never switching. If $G<0$, then $P$ is a worse strategy than never switching.
\end{frame}
\begin{frame}
\frametitle{Positive gains}
\begin{lemma}
If $P$ is decreasing and strictly decreasing on an interval $I$ with $f(I)>0$, then $G>0$.
\end{lemma}\pause
\begin{example}[Threshold switching]
Let $P(x)=\1_{[0,b]}(x)$, thus always switch if value lower than $b$ is encountered. Then $P$ is decreasing and has non-negative gains.

If $f$ is strictly decreasing, choose $b$ such that $f(2b)=\frac{1}{4}f(b)$. This $P$ is the best switching strategy possible.
\end{example}
\end{frame}
\begin{frame}
\frametitle{Optimize threshold switching}

\begin{theorem}[Egozcue and García (2015)]
Let $X$ be continuous with $\E[X]=\mu$ and $\V[X]=\sigma^2$. Let $m=\mu^2+\sigma^2$. Let $P(x)=\1_{[0,b]}(x)$ be threshold switching.
\begin{enumerate}
	\item The gain has lower bound
	\[G(b)\geq\frac{3+2\sqrt{2}}{b}\left(\frac{3}{2}b\mu-\frac{1}{2}b^2-m\right).\]
	\item If $\mu^2> 8\sigma^2$, then $b^*=\sqrt{2m}$ is optimal with gain
	\[G(b^*)\geq(3+2\sqrt{2})\left(\frac{3}{2}\mu-\sqrt{2m}\right)>0.\]
\end{enumerate}
\end{theorem}
\end{frame}
\begin{frame}
\frametitle{Trade?}

\begin{itemize}
\item Countdown from 3, tell if you want two switch.
\item Switch when both players agree.
\item After possibly switching reveal the contents of your envelopes.
\end{itemize}
\end{frame}
\end{document}


\documentclass[12pt]{article}

\include{preamble}

\newtoggle{spacingmode}
\toggletrue{spacingmode}  %STUDENTS: DELETE or COMMENT this line

\newtoggle{professormode}
\toggletrue{professormode} %STUDENTS: DELETE or COMMENT this line

\newcommand{\spc}[1]{\iftoggle{spacingmode}{\\ \vspace{#1cm}}}

\title{MATH 241 Fall \the\year~Homework \#2}

\author{Professor Adam Kapelner} % STUDENTS: DELETE my name and put your name and section here e.g. \author{John Doe, Section A}. MAKE SURE YOU PUT YOUR SECTION HERE!!!!!!!!

\iftoggle{professormode}{
\date{Due by email 11:59PM October 2, \the\year \\ \vspace{0.5cm} \small (this document last updated \today ~at \currenttime)}
}

\renewcommand{\abstractname}{Instructions and Philosophy}

\begin{document}
\maketitle

\iftoggle{professormode}{
\begin{abstract}
The path to success in this class is to do many problems. Unlike other courses, exclusively doing reading(s) will not help. Coming to lecture is akin to watching workout videos; thinking about and solving problems on your own is the actual ``working out''.  Feel free to \qu{work out} with others; \textbf{I want you to work on this in groups.}

Reading is still \textit{required}. For this homework set, read the section about sample spaces in Chapter 2 and relevant parts of Chapter 1 in Ross. Chapter references are from the 7th edition.

The problems below are color coded: \ingreen{green} problems are considered \textit{easy} and marked \qu{[easy]}; \inorange{yellow} problems are considered \textit{intermediate} and marked \qu{[harder]}, \inred{red} problems are considered \textit{difficult} and marked \qu{[difficult]} and \inpurple{purple} problems are extra credit. The \textit{easy} problems are intended to be ``giveaways'' if you went to class. Do as much as you can of the others; I expect you to at least attempt the \textit{difficult} problems.

This homework is worth 100 points but the point distribution will not be determined until after the due date. See syllabus for the policy on late homework.

Up to [see syllabus] points are given as a bonus if the homework is typed using \LaTeX. Links to instaling \LaTeX~and program for compiling \LaTeX~is found on the syllabus. You are encouraged to use \url{overleaf.com}. If you are handing in homework this way, read the comments in the code; there are two lines to comment out and you should replace my name with yours and write your section. If you are asked to make drawings, you can take a picture of your handwritten drawing and insert them as figures or leave space using the \qu{$\backslash$vspace} command and draw them in after printing or attach them stapled.

The document is available with spaces for you to write your answers. If not using \LaTeX, print this document and write in your answers. I do not accept homeworks not on this printout. Keep this first page printed for your records. Write your name below.

\end{abstract}

\thispagestyle{empty}
\vspace{1cm}
NAME: \line(1,0){240} %~~SECTION (A or B): \line(1,0){35}
\pagebreak
}

~\\ \\

\problem Examine the following words and tell me how many \textit{permutations} there are of the letters. We do not care about keeping track of the individual common letters. For example, in the word $dad$, there are two $d's$ and we want to treat the permutation $d_1 d_2 a$ the \textit{same} as $d_2 d_1 a$.

\begin{enumerate}
\easysubproblem town \spc{1}

\easysubproblem tsktsk (yes, this is a real word!)\spc{1}

\intermediatesubproblem mississippi \spc{2.3}

\hardsubproblem supercalifragilisticexpialidocious \spc{3.0}

\end{enumerate}

\problem Below is a standard chessboard. Rows one and eight have the following pieces: two rooks, two knights, two bishops, a king and a queen. Rows two and seven have 8 pawns. Rows one and two have all black pieces and rows seven and eight have all white pieces.

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=2.5in]{chess.jpg}
\end{figure}
\FloatBarrier
}

\begin{enumerate}
\easysubproblem How many ways are there to place the black queen on a white square? \spc{1}

\intermediatesubproblem How many ways are there to set up the pieces in the back ranks of both white and black \ie arrange the two rooks, two knights, two bishops, king and queen on the first row of 8 squares. Note that this game is called ``Fischer Random Chess'' after the famous grandmaster Bobby Fischer who proposed the idea to make standard chess more exciting.  \spc{2}

\hardsubproblem The game progresses and white takes two black pawns and black takes two white pawns. How many ways are there to arrange the pieces on the board? We don't care about pieces of a type being unique (\ie all white pawns are the same, all black rooks are the same, etc).  \spc{4}

\hardsubproblem Are all arrangements \qu{equally likely} during an actual chess game? Explain why or why not. \spc{3}

\end{enumerate}

\iftoggle{professormode}{
\paragraph{More counting} These counting questions will give you more practice in computing probabilities. Due to computations involving large factorials, we will also review Stirling's Approximation.\\ \\
}

\problem{We have 4 blue marbles, 4 green marbles, 2 orange marbles, and 2 red marbles. For the following questions, if you are using ``choose notation'', please write your choose notation, then write the formula using factorials, then write the actual number after you compute it.}

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=2.1in]{marbles.jpg}
\end{figure}
\FloatBarrier
}

\begin{enumerate}
\easysubproblem{Viewing all the marbles as \textit{unique}, how many ways are there to order the marbles? Note that \qu{order} is another way of saying \qu{permute.}}\spc{2}

\intermediatesubproblem{Viewing all marbles of the same color as \textit{interchangeable}, how many ways is there to order the marbles?}\spc{2}

\extracreditsubproblem{If I pick 4 marbles at random from the collection, how many ways are there to get two-of-a-kind \ie two marbles of one color and two marbles of a different color.}\spc{7}

\end{enumerate}

\problem{Imagine you have a bag of 10 cards where 6 are blue and 4 are red. A \qu{draw} means one card is taken out of the bag at random and the color is revealed. If the problem asks \qu{what is the probability,} this means an explicit computation is required unless otherwise stated.}

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=1.2in]{red_blue.png}
\end{figure}
\FloatBarrier
}

\begin{enumerate}
\easysubproblem{What is the probability of getting a blue card when drawing one card?}\spc{1}

\easysubproblem{What is the probability of drawing 3 red cards in a row \textit{without replacement}?} \spc{1.5}

\intermediatesubproblem{Five cards are drawn. What is the probability of having 3 reds and 2 blues without regards to any order of the cards?} \spc{2}

\hardsubproblem{Five cards are drawn. What is the probability of having 3 reds and 2 blues in that order? Think carefully about the numerator and denominator in this probability computation.} \spc{5}

%\intermediatesubproblem{Five cards are drawn from a new bag with 100 cards where 60 are blue and 40 are red. What is the probability of having 3 reds and 2 blues without regards to any order of the cards?} \spc{3}

%\intermediatesubproblem{Five cards are drawn from a new bag with 1000 cards where 600 are blue and 400 are red. What is the probability of having 3 reds and 2 blues without regards to any order of the cards?} \spc{4}

\end{enumerate}

\problem{Imagine you are putting together musical performances and you are employing musicians at random. There are many available for hire: 23 guitarists, 15 vocalists, 6 drummers, 14 bassists, 8 violinists, 9 violas, 6 cellists.}

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=2in]{musicians.png}
\end{figure}
\FloatBarrier
}

\begin{enumerate}
\easysubproblem{If we hire 4 musicians at random, what is the probability we get a rock band (a vocalist, a guitarist, a bassist and a drummer)?} \spc{3}

\easysubproblem{If we hire 4 musicians at random, what is the probability we get a string quartet (two violinists, 1 cellist and one violist)?} \spc{3}

\easysubproblem{If we hire 4 musicians at random, what is the probability we get a doowop group (four vocalists)?} \spc{7}

\easysubproblem{We now move to a different city and the musicians for hire are different. Here, we have 10 guitarists, 10 vocalists, 10 drummers, 10 bassists. What is the probability we form a rock band when hiring four musicians at random?} \spc{3}

\hardsubproblem{Given the same situation in part (d), what is the probability we get two pairs of musicians (e.g. two guitarists and two bassists or two drummers and two bassists)?} \spc{2.8}

\hardsubproblem{Given the same situation in part (d), what is the probability we get all four musicians be the same type?} \spc{3}

\end{enumerate}

\problem{Combinations are not only useful in probability problems. They come up all over mathematics.}

\begin{enumerate}

\easysubproblem{Below is \qu{Pascal's Triangle} up to $n=4$.

\begin{table}[htp]
\centering
\begin{tabular}{rccccccccc}
$n=0$:&    &    &    &    &  1\\\noalign{\smallskip\smallskip}
$n=1$:&    &    &    &  1 &    &  1\\\noalign{\smallskip\smallskip}
$n=2$:&    &    &  1 &    &  2 &    &  1\\\noalign{\smallskip\smallskip}
$n=3$:&    &  1 &    &  3 &    &  3 &    &  1\\\noalign{\smallskip\smallskip}
$n=4$:&  1 &    &  4 &    &  6 &    &  4 &    &  1\\\noalign{\smallskip\smallskip}
\end{tabular}
\end{table}

Explain why the 6 in the middle of the $n=4$ row is equivalent to $\binom{4}{2}$ by using the fact proved in class.} \spc{2}

\easysubproblem{In the expansion $(a+b)^{100}$, how many terms are of the form $a^2 b^{98}$?} \spc{1}

\extracreditsubproblem{Prove the binomial theorem for arbitrary $n \in \naturals$.} \spc{10}

\end{enumerate}

\iftoggle{professormode}{
\paragraph{Philosophy of Probability} We covered many definitions of probability and discussed some philosophy of probability.\\ \\
}

\problem{Answer the following questions by writing a paragraph or two \textit{in English}.}

\begin{enumerate}
\easysubproblem{Previously we defined probability as $\prob{A} := \frac{|A|}{|\Omega|}$. Describe a situtation where this fails to produce the correct probability that is not the spinner used in lecture.}\spc{4}

\easysubproblem{Which definition of probability does the book use and why do you think the authors chose this definition?}\spc{3}

\easysubproblem{Give an example of an event whose probability cannot be approximated by the limiting frequency.} \spc{1.5}

\extracreditsubproblem{Who picks $\omega \in \Omega$ \ie the outcome from the set of possible outcomes in the universe? This is an issue we ignored. Discuss your thoughts.} \spc{5}

\easysubproblem{What are some problems with the long run frequency definition of probability?} \spc{3}

\intermediatesubproblem{How did Chevalier de Mere in 1654 know that the \\ $\prob{\text{one or more double sixes in 24 rolls of two dice}} < \half$?} \spc{2}

\easysubproblem{What are some problems with the propensity definition of probability?} \spc{2}

\extracreditsubproblem{What idea(s) inspired Karl Popper to invent the propensity definition?} \spc{2}

\easysubproblem{What is the main problem with the subjective definition of probability?} \spc{2}

%\easysubproblem{If all information was known, would there still be randomness? Yes/no is fine.} \spc{1}

\easysubproblem{According to Laplace (and his interpretation of Newton), if all information was known about physical systems including all laws and all initial conditions, would there be randomness? Yes/no and discuss.} \spc{2}

\hardsubproblem{According to Laplace, what is randomness? I've uploaded Laplace's quote in lecture 5 on the course homepage. You can answer this in a few words.} \spc{1}

\hardsubproblem{Knowing what we known in the 21st century, if all information was known about physical systems including all laws and all initial conditions, would there be randomness? If so, what theory has demonstrated evidence for this?} \spc{2}

\hardsubproblem{What is the prevailing historical theory about why probability wasn't formalized using mathematics prior to the 1600's?} \spc{2}

\end{enumerate}

\problem{We will get our feet wet with basic \qu{axioms} and theorems. Assume all capital letters are sets. If the problem asks you to prove a fact, you may only use your knowledge of set theory and the definition of $\prob{\cdot}$ given in the book / lecture. Most of the answers are in the book or in my lecture notes. Try to do them yourself and only use the book if you are having trouble. This section is strongly recommended for math majors but most of it is optional.}

\iftoggle{professormode}{
\begin{figure}[htp]
\centering
\includegraphics[width=4in]{axioms.png}
\end{figure}
\FloatBarrier
}

\begin{enumerate}
\easysubproblem{List (1)  all assumptions prior to and (2) the three conditions that make $\prob{\cdot}$, the set function that returns a probability. These latter three conditions are also known as the \qu{axioms of probability.}} \spc{6}

\extracreditsubproblem{[OPTIONAL] Explain why the three conditions are not technically axioms.} \spc{5}

%\easysubproblem{Prove that if $A_1$ and $A_2$ are disjoint (mutually exclusive), $\prob{A_1 \cup A_2} = \prob{A_1} + \prob{A_2}$.} \spc{1.5}

\easysubproblem{[OPTIONAL] Prove that $\prob{\varnothing} = 0$.} \spc{2.5}

\intermediatesubproblem{[OPTIONAL] Prove that $\prob{A} \leq 1$.} \spc{5}

\hardsubproblem{[OPTIONAL] Assuming the previous theorem that $\prob{A} \leq 1$, prove that $\prob{A} \in \zeroonecl$.} \spc{2}

\hardsubproblem{[OPTIONAL] Prove that if $A \subseteq B$ then $\prob{A} \leq \prob{B}$.} \spc{3.5}

\hardsubproblem{[OPTIONAL] Prove the law of inclusion-exclusion for two arbitrary sets: $\prob{A \cup B} = \prob{A} + \prob{B} - \prob{A, B}$ (in the notes).} \spc{5}

%\intermediatesubproblem{State the general law of inclusion-exclusion for arbitrary $n$ i.e. $\prob{\cup_{i=1}^n A_i} = $ ?} \spc{2}

\extracreditsubproblem{[OPTIONAL] On a separate sheet of paper, prove the general law of inclusion-exclusion i.e. $\prob{\cup_{i=1}^n A_i} = $}

\hardsubproblem{[OPTIONAL] Some authors write $\prob{A} \in \zeroonecl$ insetad of the condition hich we did in class, $\prob{A} \geq 0 ~\forall A$. Why is this addition detail (of being probability being $\leq 1$) unncessary? Hint: see (d).} \spc{3}

\hardsubproblem{[OPTIONAL] Prove that if $A = \braces{\omega_1, \omega_2, \ldots}$ possibly infinite in cardinality, then

\beqn
\prob{A} = \sum_{i=1}^\infty \prob{\braces{\omega_i}}
\eeqn

Hint: look at the proof of why $\prob{A} = \frac{|A|}{|\Omega|}$ in the case of equally likely outcomes.} \spc{4}

\end{enumerate}

\problem{Computations of combinations and permutations is impossible for a computer due to the factorial computations. In this problem, we investigate Stirling's formula. For CS majors, doing these problems is strongly encouraged. But they are optional.}

\begin{enumerate}
\easysubproblem{[OPTIONAL] Use the natural log to derive an expression for $\binom{1500}{300}$ using sums by recalling that $\natlog{n!} = \sum_{i=1}^n \natlog{i}$. Do not compute. Leave in terms of sums and natural logs.} \spc{4}

\easysubproblem{[OPTIONAL] Show that \href{https://en.wikipedia.org/wiki/Stirling%27s_approximation}{Stirling's approximation} is equivalent to:

\beqn
\natlog{n!} \approx \half\natlog{2\pi} + \parens{n + \half}\natlog{n} - n
\eeqn}
\spc{4}

\hardsubproblem{[OPTIONAL] Use the expression in (c) to approximate the probability of getting 300 Heads in 1500 coin flips.} \spc{3}

\end{enumerate}

\end{document}


\documentclass[12pt]{article}
\input{preamble.tex}

\begin{document}

\begin{center}
{\Large Representations and Representation Learning} \\
\vspace{5mm}
{\large Bryan Chiang and Jeanette Bohg}
\end{center}

\section*{Overview}

\subsection*{States and Representations}

Consider the following system consisting of a walking robot. The \textbf{state} is a quantity describing the key aspect of a system. In this example, the state may consist of information about the robot's 3D structure and motion. How do we actually parameterize the state? In this context, a \textbf{representation} refers to the literal data format used to describe the state, often dependent on the task at hand. Numerous representations are possible for a given state; for instance, we could choose to use any selection of joint angles, velocities, positions or the pose of the robot.

\begin{figure}[H]
  \caption{Walker robot from the DeepMind Control Suite.}
  \centering
  \includegraphics[width=4cm]{walker}
\end{figure}

In a \textbf{dynamical} system, the state is not static and evolves over time. Here, we would expect the robot to move. We denote the state at time $t$ as $x_{t}$. \textbf{Markov models} (specifically, Markov chains) are systems that possess the \textbf{Markov property}: future states only depend on the current state, not past states. In other words, state $x_{t+1}$ is conditionally independent of past states $x_{1}, x_{2}, \dots, x_{t-1}$ given current state $x_{t}$, and we can predict the state at the next timestamp given the current state according to specific dynamics of the system, or $f(x_{t}) = x_{t+1}$. For instance, if the state contains the current angular velocity of a joint, we can predict the joint angle at the next time step. $f(\cdot)$ may be a non-linear function, and we often assume that the dynamics are constant over time for simplicity. In the diagram below, the arrows indicating the chain of dependence.

\begin{figure}[H]
  \caption{Diagram of a Markov chain.}
  \centering
\includegraphics[width=12cm]{markov-model}
\end{figure}

In practice, the state at time $t$ is unknown. Our job is to use noisy observations to estimate the the true state: a process called \textbf{state estimation}. Such systems are termed \textbf{hidden Markov models} (or HMMs) since the state is "hidden". We assume that the states $x_{t}$ behave as a Markov process. We also assume that observation $z_{t}$ at time $t$ only depends on the state at that time, $x_{t}$. In other words, $f(x_{t}) = x_{t}$. The observation $z_{t}$ is conditionally independent of all previous states and observations given $x_{t}$. Hidden Markov models are partially-observable, while Markov chains are fully observable.

\begin{figure}[H]
  \caption{Diagram of a hidden Markov model.}
  \centering
\includegraphics[width=12cm]{hmm}
\end{figure}

Let's make this concrete with a vision example. If we're a self-driving car with the following point-of-view, we'd be want to know the state of all the pedestrians to avoid any collisions: 3D locations, bounding boxes (size), possibly velocities of everyone. However, this information is not given to us and therefore must be estimated from our captured observations, which could be a pair of stereo images, RGB frames, or LIDAR data.

\begin{figure}[H]
  \caption{RGB image observation of pedestrians from a car POV.}
  \centering
\includegraphics[width=10cm]{nuscenes}
\end{figure}

In future lectures on optimal estimation, we will account for manually applied actions or control inputs in these Markov processes that affect the state. Specifically, instead of $f(x_{t}) = x_{t+1}$, we will have $f(x_{t}, u_{t}) = x_{t+1}$, where $u_{t}$ is the control input or action taken at time $t$ (such as rotating a joint motor to move the legs). Markov chains generalize to \textbf{Markov decision processes (MDPs)}, and hidden Markov models generalize to \textbf{partially observable Markov decision processes (POMPDPs)}.

\subsection*{Generative and Discriminative Approaches}

How can we estimate the state from observations? Let's consider the task of estimating an object's pose (state $x$) from an input RGB image (observation $z$). A 6D pose refers to both the objects 3D translation and 3D rotation (6 parameters total).

\begin{figure}[H]
  \caption{PoseCNN (2018) network is an example of a discriminative model.}
  \centering
\includegraphics[width=12cm]{pose-cnn}
\end{figure}

We describe two approaches at a high-level; we will delve into specifics later in the course. First, a discriminative model aims to learn the parameters $\theta$ for a functional form of $p_{\theta}(x|z)$ from the observations. For instance, we could train a neural network to directly map the input image to the most likely output pose. Second, in contrast, a generative model aims to learn the parameters for functional forms of $p(z|x)$, $p(x)$ separately to model the joint $p(z, x)$ via Bayes Rule. For instance, we can sample a 3D pose prediction from $p(x)$ and measure $p(z|x)$ by comparing the differences between the image coordinates of the predicted 3D poses's 2D projection on the image plane with the actual observed image coordinates. To learn more about the differences about discriminative and generative models, please refer to the CS229 notes.

\section*{Representation Learning}

\subsection*{Representations in Computer Vision}

We turn our attention to representations in computer vision, which can have multiple meanings depending on the context.

\begin{figure}[H]
  \caption{Input, intermediate, and output representations.}
  \centering
\includegraphics[width=14cm]{cv-reps}
\end{figure}

On one end of the spectrum, when we talk about \textbf{input representations}, we are referring to the raw sensor data format that was used to capture the input. In the above example, our input is the 3D scene within the ocean containing the fish. Depending on sensor choice, the input representation of the scene could be images, depth frames, point clouds, or any other imaging modality. Even within a specific form, there are subtle differences in the input representation. For instance, color vs. grayscale, stereo vs. monocular, RGB vs. HSV (image color spaces). On the other end of the spectrum, \textbf{output representations} succinctly describe the high-level key aspects of the scene that are necessary for decision-making and completing downstream tasks. If we were a biologist and our goal was to count the number of fish we encounter, the relevant output representation may be the "Fish" label on the output side, or even a more specific label indicating the species of the fish. However, if we (the camera) were a drone attempting to navigate the coral reef, we may be more interested in estimating the 6D pose and 3D bounding box of the fish, which would be useful to incorporate into our trajectory planning. In between input and output representations lie \textbf{intermediate representations}, which are compressed, low-dimensional vector representations that summarize the high-dimension sensory data (input). Intermediate representations are derived from the input representation, and subsequently, output representations are derived from the intermediate representations.

Some natural questions at this point are:

\begin{enumerate}[]
  \item Since representations are so key for decision-making, what makes for a good representation?
  \item How do we actually obtain intermediate and output representations?
\end{enumerate}

\subsection*{Representation Criteria}

Bengio proposed several...

\subsection*{Traditional CV and Interpretable Representations}

The traditional ($\approx$ pre-2012) computer vision pipeline is as follows.

\begin{figure}[H]
  \caption{Classical CV pipeline for extracting representations. Intermediate representations can be used to derive higher-level intermediate representations. }
  \centering
\includegraphics[width=14cm]{trad-cv}
\end{figure}

Here, our intermediate representations are combinations of hand-crafted features extracted from the image input representation. The specific choices of features (descriptors) is flexible. For instance, if we believe that specific stripes or designs on a fish's body may be distinguish it from other types of fish or things in the sea, then we could exploit those specific patterns by extracting edges and colors as our intermediate representation. To extract the output representation (here a class label), our intermediate representation is fed into classifier, which incorporates the intermediate representations to make a decision either in a learned manner or based on more manually-defined heuristics. As a simple example, I can define a heuristic that if the histogram of colors in the image contains a high amount of oranges and whites, we may say that the image is likely to contain a clownfish. Much classical literature has focused on developing new feature extractors, often based on image processing and filtering methods. While specific methods are out of this class's scope, we provide several references for further reading at the end of this document. The primary benefit of classical feature extraction methods is that they result in \textbf{interpretable representations}: because we designed these methods by hand, we can easily explain why specific output representations were chosen. This can be especially important for downstream tasks with high stakes, perhaps in legal or medical domains. However, the downside is that coming up with these feature extractors is a tedious process, requiring significant amounts of time and domain expertise that may not be available.

\subsection*{Modern CV and Learned Representations}
Modern computer vision methods ($\approx$ 2012-present) replace manually extracted features \textbf{learned representations}. Through an iterative learning process, we let the layers of parameters of a (convolutional) neural network learn by themselves how to determine intermediate representations that will lead to extracting the most accurate output representations.

\begin{figure}[H]
  \caption{Learned intermediate representations. We do not choose specific feature components to use, hence the blank boxes.}
  \centering
\includegraphics[width=14cm]{learned-cv}
\end{figure}

While learned representations can extract output representations accurately and robustly, the downside is that they lack the interpretability of classical representations. Several methods for interpreting learned representations to understand why they perform so well and how to improve them. Zeiler and Fergus (2014) analyzed the specific image patches that activate filters the most strongly for each layer in a CNN trained on ImageNet. What they discovered was that the learned representations resembled the traditional extracted features (such as edges, textures, body parts). In other words, the pipeline was capable of learning its heuristics that were just as advanced as what a human could come up with.

\begin{figure}[H]
  \caption{Different levels of learned intermediate representations represent those of a traditional pipeline.}
  \centering
\includegraphics[width=14cm]{interpreting}
\end{figure}

Another approach to understanding intermediate representations is projecting them to a lower dimensional space so that we can plot and interpret them. One popular technique for achieving this is tSNE (Van der Maaten and Hinton, 2008), which performs dimensionality reduction on high-dimensional intermediate representations by minimizing the KL-divergence of joint probabilities (based on data similarity) between the low-dimensional embeddings and original high-dimensional representations. We expect that images from the same class will be geographically clustered near each other; we can use tSNE to sanity check that our neural network indeed learns that images with the same label are also similar visually.

\begin{figure}[H]
  \caption{Visualization of tSNE embeddings on the MNIST dataset.}
  \centering
\includegraphics[width=8cm]{tsne}
\end{figure}

\subsection*{Unsupervised and Self-Supervised Learning}

In a traditional supervised learning formulation, we can teach our model (probably a CNN) to learn given a loss function and a dataset of $(x_{i}, y_{i})$, where $x_{i}$ is the input representation (data) and $y_{i}$ is the output representation (label). In practice, data is abundant. However, labels are expensive to acquired and can require specialized knowledge. Can we learn meaningful representations from data \textit{without} labels? Consider the \textbf{autoencoder} architecture shown below. The goal of an autoencoder is to learn to perfectly reconstruct the input image. This is represented by the reconstruction loss, which is defined as the difference between the output representation, $\mathcal{F}(\mathbf{X})$, and the input image, $ \mathbf{X}$. We expect that the output representation should be identical to the input representation. The intermediate representation $ \mathbf{z}$, the compressed image code in the latent space located at the middle of the network. Intuitively, we expect that if the second half of the network $\mathcal{F}$ is able to reconstruct the input from  $ \mathbf{z}$, then $ \mathbf{z}$ is a useful and informative compressed version of the input representation. For this reason, we say that $ \mathbf{z}$ is the output of the \textbf{bottleneck layer}.

\begin{figure}[H]
  \caption{Autoencoder architecture for unsupervised learning. $\mathcal{F}$ is the autoenconder, $ \mathbf{X}$ is the input image, $\mathbf{\hat{X}}$ is the reconstructed input image.}
  \centering
\includegraphics[width=14cm]{ae}
\end{figure}

We say that an autoencoder performs \textbf{unsupervised learning} since it requires no external labels aside from the input image (which may be viewed as the label itself). In a similar vein, \textbf{self-supervised learning} masks out part of the input to use as the output label. For instance, we can keep all pixels below the image diagonal as the input, and all the pixels above the diagonal as the output. Given the lower portion, the task would be to reconstruct the upper portion of the image. The hope is that the network would still learn meaningful representations about the input images to achieve this task. This has been empirically shown to some extent in the following manner. Given an autoencoder trained on a large amount of unlabeled data, we can obtain the compressed intermediate representation $ \mathbf{z}$ with the first half of the network (also called the \textbf{encoder}). Then, with only a small amount of labeled data, we can train a classifier on top of the intermediate representation $ \mathbf{z}$ to accurately predict the output representation since the autoencoder has already done the heavy lifting of learning a meaningful representation for the input.

\end{document}