\documentclass{homework}
\course{Math 5522H}
\author{Jim Fowler}
\input{preamble}
\DeclareMathOperator{\Res}{Res}

\usepackage[symbol]{footmisc}
\renewcommand{\thefootnote}{\fnsymbol{footnote}}

\begin{document}
\maketitle

\begin{inspiration}
  Wenn ich nur erst die S\"atze habe! Die Beweise werde ich schon
  finden.\footnote{``If only I first had the theorems, then I'd find
    the proofs somehow.''  Riemann knew all too well the challenge of
    PODASIPs.} \byline{Bernhard Riemann}
\end{inspiration}

\section{Terminology}

\begin{problem}
  When $\Real s > 0$, what is the Gamma function $\Gamma(s)$?
\end{problem}

\begin{problem}
  When $\Real s > 1$, what is the Riemann zeta function $\zeta(s)$?
\end{problem}

\begin{problem}
  What is the von Mangoldt function $\Lambda(n)$?
\end{problem}

\section{Numericals}

\begin{problem}
  Compute $\Res(\Gamma,s)$ for $s \in \{ 0, -1, -2, \ldots \}$.
\end{problem}

\begin{problem}Evaluate $\Res(f,s)$ for the
  function $f(s) = \Gamma(s) \Gamma(1-s)$.
\end{problem}

\begin{problem} % be careful to get the sign right!
  Use \ref{euler-reflection-formula} to compute $\Gamma(1/2)$.
\end{problem}

\section{Exploration}

\begin{problem}
  In previous courses, you have seen that
  $\Gamma(s+1) = s \cdot \Gamma(s)$.  Use this fact to explain how to
  define a meromorphic function on $\C$ agreeing with $\Gamma(s)$ for
  $\Real s > 0$.  This is \textbf{analytic continuation} which succeeds
  here by using a \textbf{functional equation}.
\end{problem}

\begin{problem}\label{start-of-reflection-formula}For $\lambda \in (0,1)$, evaluate
  \[
    \int_{0}^\infty \frac{u^{\lambda - 1}}{1 + u} \, du.
  \]
  \textit{Hint:} make the substitution $u = e^x$ and invoke \ref{integral-for-euler-reflection}.
\end{problem}

\begin{problem}\label{integrate-gamma-one-minus-s}For $s \in (0,1)$ and positive $t \in \R$, show that
  \[
    \Gamma(1 - s) = t \int_0^\infty (xt)^{-s} e^{-xt}\, dx.
  \]
\end{problem}

\begin{problem}\label{euler-reflection-formula}Combine \ref{start-of-reflection-formula} and \ref{integrate-gamma-one-minus-s} to conclude that
  \[
    \Gamma(1-s) \, \Gamma(s) = \frac{\pi}{\sin \left( \pi s \right)}.
  \]
  This is \textbf{Euler's reflection formula}.
\end{problem}

\begin{problem}
  Show that the series
  \[\displaystyle\sum_{n=1}^\infty \displaystyle\frac {1}{n^{s}}\]
  converges to a holomorphic function when $\Real s > 1$.
\end{problem}

\begin{problem}\label{euler-product-formula}Recall that you defined
  \(\displaystyle\prod_{n=1}^\infty \left( 1 + a_n \right)\) in
  \ref{terminology-infinite-product}.  Show that when $\Real s > 1$, we
  have
  \[
    \frac{1}{\zeta(s)} = \displaystyle\prod_{n=1}^\infty \left( 1 - {p_n}^s \right),
  \]
  where $p_n$ is the $n$th prime number.
\end{problem}

\begin{problem}
  Use \ref{euler-product-formula} and \ref{nonzero-infinite-product}
  to show that if $\Real s > 1$ then $\zeta(s) \neq 0$.
\end{problem}

\begin{problem}
  Use \ref{euler-product-formula} to show that
  \[
    \Real \left( \frac{ \zeta'(a+bi) }{ \zeta(a+bi) } \right) = - \sum_{n=1}^\infty \frac{\Lambda(n) \, \cos \left( b \log n \right)}{n^{a}}.
  \]
\end{problem}

\begin{problem}
  Use the trigonometric fact $3 + 4 \cos \theta + \cos (2\theta) \geq 0$ to show that
  \[
    \Real \left( 3 \frac{ \zeta'(a) }{ \zeta(a) } + 4 \frac{ \zeta'(a+bi) }{ \zeta(a+bi) } +  \frac{ \zeta'(a+2bi) }{ \zeta(a+2bi) } \right) \leq 0.
  \]
  Knowing $\zeta$ has a simple pole at $s = 1$ and assuming
  $\zeta(1+bi) = 0$ for $b \in \R$, define
  \[
    f(a) = \zeta(a)^3 \cdot \zeta(a+bi)^4 \cdot \zeta(a + 2bi)
  \]
  and study $f$ near $a = 1$ to uncover a contradiction.  You will
  have shown that $\zeta(s) \neq 0$ when $\Real s = 1$.
\end{problem}

\section{Prove or Disprove and Salvage if Possible}

\begin{problem}\label{nonzero-infinite-product}For a sequence $a_n \neq 0$ of complex numbers, suppose
  $\displaystyle\sum_{n=1}^\infty |a_n|$ converges.  Then
  $\displaystyle\prod_{n=1}^\infty \left( 1 + a_n \right)$ converges
  to a nonzero quantity.
\end{problem}

\begin{problem} % missing pole at s = 1
  Define the \textbf{Dirichlet eta function} via
  \[
    \eta(s) = := \sum_{n=1}^\infty \frac{(-1)^n}{n^s}.
  \]
  This series converges to a holomorphic function when $\Real s > 0$.
\end{problem}

\begin{problem}
  The Riemann zeta and Dirichlet eta functions are related via
  \[
    \left( 1 - 2^{1-s} \right) \zeta(s) = \eta(s).
  \]
\end{problem}

\end{document}


\chapter{STATISTICAL TESTS}

This chapter describes the different statistical tests available
in TestU01 and how they can be applied.
These tests are organized in different modules,
sometimes according to their similarity and
sometimes according to the author of the book/article from which they
were taken.
Each test looks, in its own way, for empirical evidence against
the null hypothesis $\cH_0$ defined in the introduction.
It computes a test statistic $Y$ whose distribution under $\cH_0$
is known (or for which a good approximation is available).

%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph*{Single-level tests.} \

A {\em first-order\/} (or {\em single-level\/}) test
observes the value of $Y$, say $y$,\index{single-level tests}
and rejects $\cH_0$ if the {\em $p$-value\/}
(or {\em significance level\/})
 $$ p = P[Y \ge y \mid \cH_0] $$
is much too close to either 0 or 1.
If the distribution of $Y$ is [approximately] continuous,
$p$ is [approximately]
a $U(0,1)$ random variable under $\cH_0$.
Sometimes, this $p$ can be viewed as a {\em measure of uniformity},
in the sense that it will be close to 1 if the generator produces
its values with excessive uniformity, and close to 0 in the opposite
situation (see, e.g., the module {\tt smultin}).

In the case where $Y$ has a
 {\em discrete distribution\/}\index{discrete distribution}
under $\cH_0$, we distinguish the {\em right $p$-value\/}
$p_R =  P[Y \ge y \mid \cH_0]$ and the {\em left $p$-value\/}
$p_L =  P[Y \le y \mid \cH_0]$.  We then define the $p$-value as
\begin{eqnarray*}
   p & = & \left\{
 \begin{array}{l@{\qquad}l}
      p_R, & \mbox{if } p_R <  p_L \\[6pt]
  1 - p_L, & \mbox{if } p_R \ge p_L \mbox{ and } p_L < 0.5 \\[6pt]
      0.5  &         \mbox{otherwise.}
 \end{array}\right.
\end{eqnarray*}
Why such a definition?
Consider for example a Poisson random variable $Y$ with mean 1
under $\cH_0$.  If $Y$ takes the value 0, the right $p$-value is
$p_R =  P[Y \ge 0 \mid \cH_0] = 1$.  In the uniform case, this would
obviously lead to rejecting $\cH_0$ on the basis that the
$p$-value is too close to 1.
However, $P[Y = 0 \mid \cH_0] = 1/e \approx 0.368$, so it does not
really make sense to reject $\cH_0$ in this case.
In fact, the left $p$-value here is $p_L = 0.368$, and the $p$-value
computed with the above definition is $p = 1 - p_L \approx 0.632$.
Note that if $p_L$ is very small, with this definition, $p$ becomes
close to 1.
If the left $p$-value was defined as
$p_L = 1 - p_R = P[Y < y \mid \cH_0]$, this would also lead to problems;
in the example, one would have $p_L = 0$.

%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph*{Two-level tests.} \

In a {\em second-order\/} (or {\em two-level\/}) test, one generates
$N$ ``independent'' copies of $Y$, say $Y_1,\dots,Y_N$, by replicating
the first-order test $N$ times.\index{two-levels tests}
Let $F$ be the theoretical distribution function of $Y$ under $\cH_0$.
In the case where $F$ is {\em continuous}, the transformed observations
$U_1 = F(Y_1),\dots, U_N = F(Y_N)$ should behave as i.i.d.\ uniform
random variables.
One way of performing the two-level test is to compare the empirical
distribution of these $U_j$'s to the uniform\index{goodness-of-fit tests}
distribution, via a {\em goodness-of-fit\/} (GOF) test such as
those of Kolmogorov-Smirnov, Anderson-Darling, Cr\'amer-von Mises, etc.
These GOF test statistics are defined in module {\tt gofs} and their
$p$-values are computed by the functions of module {\tt gofw}
(these two modules are in library ProbDist).
For example, if $d_N^+$ is the sample value taken by the Kolmogorov-Smirnov
statistic $D_N^+$ (defined in module {\tt gofs}), the corresponding
$p$-value at the second level is $\delta^+ = P[D_N^+ > d_N^+ | \cH_0]$.
Under $\cH_0$, $\delta^+$ has the $U(0,1)$ distribution.

In TestU01, several of these GOF tests can actually be applied
simultaneously, and all their $p$-values are reported in the results.
Those that are too close to 0 or 1 are marked by special indicators
in the printouts.  %% The symbol {\tt eps} in the printout stands for a
%%  value smaller than $10^{-15}$.
The GOF tests that are applied are those that belong to the set
{\tt gofw\_ActiveTests}.
This kind of flexibility is sometimes convenient for comparing the
power of these GOF tests to detect the weaknesses of specific classes
of generators.

This type of two-level testing procedure has been widely applied for
testing RNGs \cite{sFIS96a,rKNU98a,rLEC92a,rLEE97a,rMAR85a}.
The arguments supporting it are that
(i) it sometimes permits one to apply the test with a larger total
sample size to increase its power
(for example, if the memory size of the computer
limits the sample size of a single-level test), and
(ii) it tests the RNG sequence at the local level,
not only at the global level (i.e., there could be very bad behavior
over short subsequences, which cancels out when averaging
over larger subsequences).
As an example of this, consider the extreme case of a generator
whose output values are $i/2^{31}$, for $i=1,2,\dots,2^{31}-1$,
in this order.
A simple test of uniformity over the entire sequence would give
a perfect fit, whereas the same test applied repeatedly over
(disjoint) shorter sub-sequences would easily detect the anomaly.

Another way of performing the test at the second level is to simply add
the $N$ observations of the first level
and reject $\cH_0$ if the sum is too large or too small.
For the great majority of the tests in this library, the distribution
of $Y$ is either chi-square, normal, or Poisson.
In these three cases, the sum $\tilde Y = Y_1 + \cdots + Y_N$ has the
same type of distribution.  That is, if $Y$ is chi-square with $k$ degrees
of freedom [resp., normal with mean $\mu$ and variance $\sigma^2$,
Poisson with mean $\lambda$], $\tilde Y$ is chi-square with $Nk$ degrees
of freedom [resp., normal with mean $N\mu$ and variance $N^2\sigma^2$,
Poisson with mean $N\lambda$].
TestU01 usually
\hrichard {Not yet fully implemented everywhere, but will be done very soon.}
reports the results of the test based on $\tilde Y$ in
these situations, in addition to the second-order GOF tests specified
by {\tt gofs\_ActiveTests} (for the Poisson case, where the
second-order GOF tests are not valid unless $\lambda$ is large enough
for the Poisson distribution to be well approximated by a normal,
only the results of the tests base on $\tilde Y$ are reported).

Our empirical investigations indicate that for a fixed total
sample size $Nn$, when testing RNGs, a test with $N=1$ is often
more efficient than the corresponding test with $N > 1$.
\hrichard{Il y a des tests pour lesquels $N > 1$ est plus sensible
  que $N=1$: au moins
  {\tt sstring\_HammingWeight, sstring\_AutoCor,
  sstring\_LongestHeadRun, sknuth\_MaxOft}}.
This means that for typical RNGs, the type of structure found in
one (reasonably long) subsequence is usually found in (practically)
all subsequences of the same length.
In other words, when a RNG started from a
given seed fails spectacularly a certain test, it usually fails
that test for {\em most\/} admissible seeds, though
there are some exceptions.
In the case where $N > 1$, the test based on $\tilde Y$ is usually
more powerful than the second-order GOF tests comparing the empirical
distribution of $F(Y_1),\dots,F(Y_N)$ to the uniform,
according to our experience.

%%%%%%%%%%%%%%%%%%%%
\paragraph*{Rejecting $\cH_0$.} \

In statistical studies where a limited amount of data is available,
people sometimes fix the significance level $\alpha$ in advance
to arbitrary values such as 0.05 or 0.01,
and reject $H_0$ if and only if the $p$-value is below $\alpha$.
However, statisticians often recommend to just report the $p$-value,
because this provides more information than reporting a ``reject'' or
``do not reject'' verdict based on a fixed $\alpha$.

When a $p$-value is extremely close to 0 or to 1 (for example,
if it is less than $10^{-10}$), one can obviously conclude that
the generator {\em fails\/} the test.
If the $p$-value is suspicious but failure is not clear enough,
($p=0.0005$, for example), then the test can be replicated independently
until either failure becomes obvious or suspicion disappears
(i.e., one finds that the suspect $p$-value was obtained only by chance).
This approach is possible because there is no limit (other than CPU time)
on the amount of data that can be produced by a RNG to increase the
sample size and the power of the test.

%%%%%%%%%%%%%%%%%%%%%%
\paragraph*{Common parameters and tools.} \

Three parameters, called $N$, $n$, and $r$, are common to all the
functions that apply a test in the {\tt s} modules.
The parameter $N$ gives the number of independent replications of the
base test, i.e. the number of distinct subsequences on which it is applied,
and $n$ is the sample size for each replication.
The parameter $r$ gives the number of bits that are discarded
from each generated random number.
That is, each real-valued random number is multiplied by $2^r$ modulo 1,
to drop its $r$ most significant bits.
These three parameters are not re-explained in each test description.
It is implicit that the first $r$ bits of each uniform are always
discarded, that the test explained in the function description
is always replicated $N$ times, and that a two-level test is applied
whenever $N > 1$.
% In many cases, it is best to choose $N=1$ and $n$ as large as possible.

For the tests based on bit strings, another parameter that usually
appears is $s$.  It represents the number
of bits of each uniform that are effectively used by the test.
That is, when $s$ appears, the test drops the $r$ most significant bits
and takes the $s$ bits that follow.
In this case, it is important to make sure that $r+s$ does not exceed
the number of bits of precision provided by the RNG.
For example, if the RNG's output is always a multiple of $1/2^{31}$,
$r+s$ should not exceed 31.
% There are also other situations (e.g., the tests in {\tt snpair})
% where the test would detect the discretization error when the sample
% size is large and the RNG does not return enough bits.

\ifdetailed  %%%
Modules {\tt swrite} and  {\tt sres}
provide basic tools used inside the other
{\tt s} modules, which implement the tests.
Typical users do not need to use them directly.
In the majority of cases, it suffices to create the generator
to be tested and pass it as a parameter to
the function that applies the desired test.
The test results are then printed automatically
to the standard output, with a level of detail that can be modified
by the user (see module {\tt swrite}).
\fi  %%%%%

%%%%%%%%%%%%%%%%%%%%
\paragraph*{Reports.} \

By default, each test prints a report, on standard output,
giving the name of the test, the name of the tested generator,
the test parameters, the values of the statistics,
the significance level of those statistics
and the CPU time used by the test.
This report may also contain information specific to a given test.

It is possible to print more or less detailed statistical reports
by setting one or more of the {\tt lebool} flags defined in module {\tt swrite}.
One may wish to see, for example, the value of the test
statistic $Y$ for each of the $N$ replications,
% (usually the values in the statistical collectors),
the values of the counters, the groupings of the classes, their
expected and observed numbers for the chi-square test, etc.
For some of the tests, printing the counters would generate huge reports
and is not practically useful. For other tests (for example those
based on a chi-square test), seeing the counters and the classes may be
enlightening as to why a given generator fails a test.
It is even possible to have no output at all from any of the {\tt s}
modules of TestU01 by setting all the {\tt lebool} flags in module {\tt swrite}
 to {\tt FALSE}.

The test functions automatically print the state of the generator
at the beginning of an experiment and at the end of each test.
If more than one test are called in a program, the initial state
of the generator at the beginning of a test will be the final state
of the generator at the end of the preceding test. This permits one to
keep track of which segment of the stream of random numbers has been
used by each test.

A more flexible way of examining detailed information
about what has happened in the tests, to have a closer look at
specific details or perhaps for post-processing the results of the tests,
is via the {\tt ...\_Res} structures.
These data structures are specific to each type of test and
are described explicitly in the detailed version of this guide
(see also module {\tt sres}).
Each function implementing a test has a parameter {\tt \ldots\_Res *}
pointing to a structure that keeps the results.

Perhaps in the majority of situations, the automatic printout made
by the testing function suffices and there is no need to
examine the {\tt ...\_Res} structure(s) after the test(s).
In this case, it suffices to pass
a {\tt NULL} pointer for the {\tt ...\_Res *} parameter.
The structure will then be created internally and destroyed automatically
after the results are printed.
% , inside the function that applies the test.

\ifdetailed %%%%

To examine or process the results of a test after its completion,
one must create a
{\tt \ldots\_Res} structure by calling the appropriate {\tt Create...}
function, and pass it as a parameter to the test. Failing to call the
appropriate {\tt Create...} function before using a {\tt \ldots\_Res}
structure will result in a memory fault because each such
function creates inner sub-structures used in the tests.
The same structure can be used for many successive tests of the same type.
When the structure is no longer needed, it should be destroyed by
calling the appropriate {\tt Delete} function.
These structures are explained in the {\em detailed\/}
version of this user's guide.

\fi  %%%%

%%%%%%%%%%%%%%%%%%%%
\paragraph*{Scatter plots.} \

There is a  module {\tt scatter} that permits one to plot points produced
by a  generator in the $t$-dimensional hypercube $[0,1)^t$.
A rectangular box is defined in this hypercube, and the points lying
in this box are projected on a selected two-dimensional subspace
and placed on a 2-dimensional scatter plot.
The plot is put in a file ready to be processed by
\LaTeX\ or {\it Gnuplot}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\paragraph*{An example: The birthday spacings tests applied to an LCG.} \

Figure~\ref{fig:progstat} shows how to apply a test to a generator.
The call to {\tt ulcg\_CreateLCG} creates and initializes the generator
{\tt gen} to the LCG with modulus $m$ = 2147483647,
multiplier $a$ = 397204094, additive constant $c=0$, and initial
state $x_0 = 12345$.  This LCG is used in the SAS statistical
 software \cite{iSAS90a}.
Then the birthday spacings test is applied twice to this generator,
with $N=1$, $r=0$, in $t=2$ dimensions.
The sample sizes are $n=10^3$ and $n=10^4$, and the number $d$ of
divisions along each coordinate is chosen so that the expected number
of collisions $\lambda = n^3/(4d^t)$ is 2.5 in the first case and 0.25
in the second case (the values of $d$ are $10^4$ and $10^6$, respectively).
Under $\cH_0$, the number of collisions is approximately a Poisson
random variable with mean $\lambda$.

\setbox0=\vbox {\hsize = 6.0in
\smallc
\verbatiminput{../examples/birth1.c}
}

\begin{figure}[htb] \centering \myboxit{\box0}
\caption{Applying two birthday spacings tests to a LCG.}
   \label{fig:progstat}
\end{figure}

The results are in Figure~\ref{fig:resstat}.
These results are printed to the standard output, which may be redirected
to a file if desired.
%% Here, the symbol {\tt eps} means a $p$-value smaller than $10^{-15}$.
At sample size $n=10^3$, there are 6 collisions and the $p$-value is 0.04,
which is not extreme enough to reject $\cH_0$.
At sample size $n=10^4$, there are 44 collisions and the $p$-value is
close to $10^{-81}$ (i.e., if $Y$ is Poisson with mean 0.25,
$P[Y\ge 44] < 10^{-81}$).
The generator fails miserably in this case, with a sample size as
small as ten thousands.  This test took approximately 0.02 second to run.

\ifdetailed  %%%

One may also want to examine or post-process the results of the tests in
some way. In this case, one would create a {\tt res} structure as shown
in the code segment of Figure~\ref{fig:progstat2}, and delete the
structure when it is no longer needed. By setting the flag
 {\tt swrite\_Basic} to {\tt FALSE}, no output will be automatically
printed by the testing function.

\setbox0=\vbox {\hsize = 6.0in
\smallc
\verbatiminput{../examples/birth2.c}
}

\begin{figure}[htb] \centering \myboxit{\box0}
\caption{Two birthday spacings tests and post-processing the results.}
  \label{fig:progstat2}
\end{figure}

\fi  %%%

\setbox1=\vbox {\hsize = 6.0in
\smallc
\verbatiminput{../examples/birth1.res}
}

\begin{figure}[ht] \centering \myboxit{\box1}
\caption{Results of the two birthday spacings tests.
 \label{fig:resstat} }
\end{figure}


\chapter{Notation Convention, Structured Rotations, and Kronecker Products}
\label{chap:K_and_R}

We begin with some notation conventions before reviewing
standard orthogonal matrices and
looking at the Kronecker product and how it affects fast matrix-vector
multiplication. One standard reference for matrix-related topics is~\cite{gvl4}.
These topics will then be used to look at
matrix factorizations of Chebyshev-Vandermonde (C\=/V) matrices
in Chapters~\ref{chap:CV_mat_1D_I} and \ref{chap:CV_mat_HD_I}.


\section{Complex numbers with round-off error}

{\it Remark} FIXME:7.1.
The theoretical method for proving Theorem FIXME(0.2)
has been implemented via the computer program {\it verify}, which is available, together with the relevant data sets, at the {\it Annals} web site.  To make this computer-aided proof rigorous, we needed to deal with round-off error in calculations.

One approach to round-off error would be to use interval arithmetic packages to carry out all calculations with floating-point machine numbers, or to generate our own version of these packages.
However, it appears that this approach
would be much too slow given the size of our collection of sub-boxes and conditions/killerwords.

To solve this problem of speed, we implement round-off error at a higher level of programing.  That is, we incorporate round-off error directly
 into AffApproxes,  which makes our error calculations more accurate, thereby avoiding much subdivision of
sub-boxes. This necessitates that we incorporate round-off error directly into complex numbers as well.
In this section we show how to do standard operations on complex numbers while keeping track of round-off error.
In the next section we work with  AffApproxes.

\demo{Definition {\rm FIXME(7.2)}}
There are two types of complex numbers to consider:

\begin{itemize}
\item[1)]  An {\it XComplex} $x = (x.{\rm re}, x.{\rm im})$ corresponds to a complex number that is represented exactly; it
simply consists of a real part and an imaginary part.

\item[2)]  An {\it AComplex} $x = (x.{\rm re}, x.{\rm im}; x.e)$ corresponds to an ``interval'' that contains the complex number in
question.  Thus, it consists of an XComplex and a floating-point number representing the error.  In particular, the AComplex
$x$ represents the set $S(x)$ of complex numbers
$\{w\ :\ |w - (x.{\rm re} + i (x.{\rm im}))| \le x.e\}$.  Note that $S(x)$ is also defined
for an XComplex if we conceptualize an XComplex as an AComplex with
$x.e = 0.$
\end{itemize}

\enddemo

{\it Remark} 7.3.
In general,  our operations  act on XComplexes and produce AComplexes, or they act on AComplexes and produce
 AComplexes.  In one case, the unary minus, an XComplex goes to an XComplex.
In the calculations that follow the effect on the error is the whole point.

\demo{Conventions and standards {\rm FIXME:7.4}}
We begin, by writing down our basic rules, which follow easily from the IEEE-754 standard for machine arithmetic (see [IEEE]).
(Actually,  the ``hypot" function $h(a,b)$, which computes by elaborate chicanery $\sqrt{a^2 + b^2},$ is not part of the IEEE-754 standard, but  satisfies the appropriate standard according to the documentation provided (see [K1]).)  The operations here are on
double-precision floating-point real numbers (``doubles") and we denote a true operation by the usual symbol and the associated machine operation by the same symbol in a circle, with two exceptions: a machine square root $\sqrt a$ is denoted $\root o \of a$ and the machine version of the hypot function is denoted $h_\circ$.  Perhaps a third exception is our occasional notation of true multiplication by the absence of a symbol.

There is a finite set of numbers (sometimes called ``machine numbers") which are representable on the computer.  With
technicalities ignored,    a nonzero floating-point number is represented by a fixed number of bits of which
the first determines the sign of the number, the next $m$ represent the exponent, and the remaining $n$ represent the
mantissa of the number.  Because our nonzero numbers start with a 1, that means the $n$ mantissa bits actually represent
the next
$n$ binary digits after the 1.  That is, the mantissa is actually $1.b_1b_2b_3...b_{n}.$   The IEEE-754 standard calls for
64-bit doubles with $m = 11$ and $n = 52.$  We define $EPS$ to be $2^{-n},$ in which case $EPS/2$ is $2^{-(n + 1)}.$

The IEEE-754 standard states that the result of an operation
 is always the closest representable number to the true solution (as long as we are in the bounds of representable
numbers).  For example, for machine numbers $a$ and~$b$, we have $a \oplus b = m(a+b)$ where $m$ is the function which
takes the machine value of its argument (when it lies in the range of representable numbers).  Thus, properties of the type
$$|(a + b) - (a \oplus b)| \leq (EPS/2) |a + b|$$
follow immediately from the IEEE-754 standard, as long as we do not {\it underflow} or {\it overflow} outside of the range of representable numbers.
Specifically, underflow occurs when the result of an operation is smaller in absolute value than $2^{-1022}$,
 and overflow occurs when the result of an operation is larger in absolute value than roughly $2^{1024}$
 (see [IEEE, \S 7]).

We further note that the formula
$$|(a + b) - (a \oplus b)| \leq (EPS/2) |a \oplus b|$$
follows because the true answer has ``exponent" which is less than or equal to the exponent of the machine answer.  We reiterate, that in both cases, $a$ and $b$ are assumed to be machine numbers.

Of course, a machine operation such as $\oplus$ must act on doubles, while a ``true" operation such as $+$ can act on reals (which includes doubles).
In this section, long strings of inequalities will be used to prove the various propositions, and care was taken to ensure that machine operations act on machine numbers.  In particular, the various variables appearing in the propositions are assumed to be doubles. The IEEE-754 standard provides for conversions from decimal to binary (within the appropriate range, conversion is to the nearest representable number) and from binary to decimal.  However, these are rarely used in this paper, although a trivial class of exceptions is provided by the decimal numbers in the conditions of
Section 5.

When calculations underflow or overflow outside of the range of representable numbers, we require that the computer inform us if either exception has occurred.

As in Section 6, we now break with the usual numbering convention.  Note that the above comments provide a proof of the
following properties.
\vglue12pt

 {\bf Basic Properties FIXME:7.0 (assuming no underflow and no overflow)}.
\vglue6pt

In the formulas that follow, $a$,$b$, and $A$ are machine numbers and
$1 + k \times EPS = 1 \oplus (k \otimes EPS)$ when $k$ is an integer which is not huge in absolute value (that is,
smaller than roughly $2^{50}$).  Thus, within the appropriate range,  $1 + k \times EPS$ is a machine number.  Similarly,
$2^k
\times A = 2^k \otimes A$ when $k$ is an integer and  $2^k \otimes A$ neither underflows nor overflows.
\begin{eqnarray*}
|(a + b) - (a \oplus b)| &\leq& (EPS/2) |a + b|,\\
|(a + b) - (a \oplus b)|&\leq& (EPS/2) |a \oplus b|,\\
|(a - b) - (a \ominus b)| &\leq& (EPS/2) |a - b|,\\
|(a - b) - (a \ominus b)|&\leq& (EPS/2) |a \ominus b|,\\
|(a \times b) - (a \otimes b)| &\leq& (EPS/2) |a \times b|,\\
|(a \times b) - (a \otimes b)| &\leq&(EPS/2) |a \otimes b|,\\
|(a / b) - (a \oslash b)| &\leq& (EPS/2) |a / b|,\\
|(a / b) - (a \oslash b)| &\leq& (EPS/2) |a \oslash b|,\\
|\sqrt a - \root o \of a| &\leq& (EPS/2) |\sqrt a|,\\
|\sqrt a - \root o \of a|  &\leq&  (EPS/2) |\root o \of a|,\\
|h(a,b) - h_\circ(a,b)| &\leq& (EPS) |h(a,b)|,\\
|h(a,b) - h_\circ(a,b)| &\leq&   (EPS) |h_\circ(a,b)|.
\end{eqnarray*}

From these formulas, we immediately compute the following.
\begin{eqnarray*}
(1 - EPS/2) |a + b| &\leq& |a \oplus b|
\leq (1 + EPS/2) |a+b|, \\
  (1 - EPS/2) |a \oplus b|& \leq& |a + b|
\leq (1 + EPS/2) |a \oplus b|,\\
\noalign{\vskip4pt}
\noalign{\hfil .\hfil }  \noalign{\vskip4pt}
\noalign{\hfil .\hfil } \noalign{\vskip4pt}
\noalign{\hfil .\hfil } \noalign{\vskip4pt}
(1 - EPS/2) |\sqrt a| &\leq&  |\root o \of a|
\leq (1 + EPS/2) |\sqrt a|, \\ (1 - EPS/2) |\root o \of a|&\leq&  |\sqrt a|
\leq (1 + EPS/2) |\root o \of a|, \\  (1 - EPS) |h(a,b)| &\leq&  |h_\circ (a,b)|
\leq (1 + EPS) |h(a,b)|, \\  (1 - EPS) |h_\circ (a,b)| &\leq& |h(a,b)|
\leq (1 + EPS) |h_\circ (a,b)|.
\end{eqnarray*}
Of course, we can also get the following type of formula, which is sometimes convenient,
 for example, in the proof of Lemma FIXME(7.2):
$$\left({1 \over {1 + {EPS \over 2}}}\right) |a \oplus b| \leq |a + b|
\leq \left({1 \over {1 - {EPS \over 2}}}\right) |a \oplus b|.$$

Before stating our propositions, we prove two lemmas.

\nonumproclaim{Lemma FIXME(7.0) {\rm (assuming no underflow and no overflow)}}
For machine numbers $a$ and $b${\rm ,}
$$(1 - EPS) \otimes |a \oplus b| \le |a + b| \le (1 + EPS) \otimes |a \oplus b|.$$
 Analogous formulas hold for $\ -,\ *,\ /,\ \sqrt{}$.
\endproclaim

\demo{Proof}
Assume $a+b > 0$.
If $(1+EPS) \otimes (a \oplus b) < (a+b)$ then the machine number
$(1+EPS) \otimes (a \oplus b)$ is a better approximation to $a+b$ than $a \oplus b$, because $(a \oplus b)
< (1+EPS) \otimes (a \oplus b)$.  This contradicts the IEEE standard.  The case $a+b < 0$ can be handled similarly, and the
case $a+b = 0$ is trivial, similarly for the left-hand inequality.
\enddemo

\nonumproclaim{Lemma FIXME:7.1} $$(1 + EPS/2)^a A \le (1 + k EPS) \otimes A$$
 where $A$ is a nonnegative machine number{\rm ,} and $a$ is a {\rm (}\/not huge\/{\rm )} integer{\rm ,} such that
for $a$ even{\rm ,} $k = {a \over 2} + 1$ and  for $a$ odd, $k = {a+1 \over 2} + 1$.
\endproclaim

\demo{Proof}
$$(1 + EPS/2)^a A\le (1 - EPS/2)(1 + k EPS) A \le (1 + k EPS) \otimes A.$$
The first inequality holds if $a$ and $k$ are as in the lemma, and the second inequality is a consequence of one of the
formulas preceding Lemma FIXME:7.0 ($A \ge 0$). \enddemo

We now begin our construction of complex arithmetic. We will give proofs for most of the operations; the others should be straightforward to derive,
or can be found in the {\it Annals} web site.
\vglue6pt
{\it Remarks} FIXME:7.5.
i) We remind the reader that all machine operations are on machine numbers, and that the various variables appearing in the propositions are assumed to be doubles.
\vglue4pt
ii) The propositions that follow include in their statements the definitions of the various operations (see Remark FIXME:6.6iii).

\nonumproclaim{Proposition FIXME:7.1 {\rm $(-X)$}}
If $x$ is an {\rm XComplex,} then
$$-x \equiv (-x.{\rm re},-x.{\rm im}).$$
\endproclaim

\nonumproclaim{Proposition FIXME:7.2 {\rm $(X + D)$}}
If $x$ is an {\rm XComplex} and $d$ is a double{\rm ,} then
$S(x + d) \supseteq S(x) + S(d)${\rm ,} where
 $$x + d \equiv (x.{\rm re} \oplus d, x.{\rm im};
(EPS/2)\otimes |x.{\rm re} \oplus d|).$$
\endproclaim

\demo{Proof}
The  error is bounded by
\vglue12pt
\hfill ${\displaystyle |(x.{\rm re} + d) - (x.{\rm re} \oplus d)|
\le (EPS/2) | x.{\rm re} \oplus d|
  =  (EPS/2) \otimes | x.{\rm re} \oplus d|.}$ \enddemo

\nonumproclaim{Proposition FIXME:7.3 {\rm $(X - D)$}}
If $x$ is an {\rm XComplex} and $d$ is a double{\rm ,} then
$S(x - d) \supseteq S(x) - S(d)${\rm ,} where
 $$x - d \equiv (x.{\rm re} \ominus d, x.{\rm im};
(EPS/2)\otimes |x.{\rm re} \ominus d|).$$ \endproclaim

\nonumproclaim{Proposition FIXME:7.4 {\rm $(X + X)$}}
If $x$ and $y$ are {\rm XComplexes,} then
$S(x + y) \supseteq S(x) + S(y)${\rm ,} where
 \begin{eqnarray*}
x + y &\equiv& (x.{\rm re} \oplus y.{\rm re}, x.{\rm im} \oplus y.{\rm im};
(EPS/2)\\
&&  \otimes\ ((1 + EPS)  \otimes (|x.{\rm re} \oplus y.{\rm re}| \oplus |x.{\rm im} \oplus y.{\rm im}|))).
\end{eqnarray*}
\endproclaim

\demo{Proof}
The error is bounded by
\begin{eqnarray*}
&&|(x.{\rm re} + y.{\rm re}) - (x.{\rm re} \oplus y.{\rm re})| +
  |(x.{\rm im} + y.{\rm im}) - (x.{\rm im} \oplus y.{\rm im})|\\[4pt]
&&\qquad \le (EPS/2)(| x.{\rm re} \oplus y.{\rm re}| +
   |x.{\rm im} \oplus y.{\rm im}|)\\[4pt]
&&\qquad \le (EPS/2)((1 + EPS) \otimes (| x.{\rm re} \oplus y.{\rm re}| \oplus
   |x.{\rm im} \oplus y.{\rm im}|))\\[4pt]
&&\qquad = (EPS/2) \otimes ((1 + EPS) \otimes (| x.{\rm re} \oplus y.{\rm re}| \oplus
   |x.{\rm im} \oplus y.{\rm im}|)).
\end{eqnarray*}
To go from line 2 to line 3 we used Lemma FIXME:7.0. \enddemo

\nonumproclaim{Proposition FIXME:7.5 $(X - X)$}
If $x$ and $y$ are {\rm XComplexes,} then
$S(x - y) \supseteq S(x) - S(y)${\rm ,} where
\begin{eqnarray*}
x - y &\equiv& (x.{\rm re} \ominus y.{\rm re}, x.{\rm im} \ominus y.{\rm im};\\[4pt]
&&\quad
(EPS/2)\otimes ((1 + EPS) \otimes (|x.{\rm re} \ominus y.{\rm re}| \oplus |x.{\rm im} \ominus y.{\rm im}|))).
\end{eqnarray*} \endproclaim

\nonumproclaim{Proposition FIXME:7.6 $(A + A)$}
If $x$ and $y$ are {\rm AComplexes,} then
$S(x + y) \supseteq S(x) + S(y)${\rm ,} where
\begin{eqnarray*}
 x+y &\equiv& ({\rm re},{\rm im};e) \hbox{ with }\\[4pt]
{\rm re}& =& x.{\rm re} \oplus y.{\rm re}\\[4pt]
{\rm im}& =& x.{\rm im} \oplus y.{\rm im}\\[4pt]
e& =& (1 + 2 EPS) \otimes ( ((EPS/2) \otimes (|{\rm re}| \oplus |{\rm im}|))
 \oplus (x.e \oplus y.e)). \end{eqnarray*}
\endproclaim

\demo{Proof}
The error is bounded by the sum of the contributions from the real part, the imaginary part, and the two individual errors:
\begin{eqnarray*}
 & &\hskip-20pt |(x.{\rm re} \oplus y.{\rm re}) -(x.{\rm re} + y.{\rm re})| + |(x.{\rm im} \oplus y.{\rm im}) -
(x.{\rm im} + y.{\rm im})| + (x.e + y.e).\\[4pt]
&&\hskip-12pt \le (EPS/2) |x.{\rm re} \oplus y.{\rm re}| + (EPS/2) |x.{\rm im} \oplus y.{\rm im}| +
(1 + EPS/2) (x.e \oplus y.e)\\[4pt]
&&\hskip-12pt \le  (1 + EPS/2) (EPS/2)(|x.{\rm re} \oplus y.{\rm re}| \oplus
 |x.{\rm im} \oplus y.{\rm im}| ) \\[4pt]
&&\hskip-12pt \qquad + (1 + EPS/2) (x.e \oplus y.e)\\[4pt]
&&\hskip-12pt = (1 + EPS/2) ((EPS/2)(|x.{\rm re} \oplus y.{\rm re}| \oplus
 |x.{\rm im} \oplus y.{\rm im}| ) + (x.e \oplus y.e))\\[4pt]
&&\hskip-12pt \le (1 + EPS/2)^2 (((EPS/2) (|x.{\rm re} \oplus y.{\rm re}| \oplus
 |x.{\rm im} \oplus y.{\rm im}| )) \oplus  (x.e \oplus y.e))\\[4pt]
&&\hskip-12pt \le   (1 + 2 EPS) \otimes (((EPS/2)\otimes (|x.{\rm re} \oplus
y.{\rm re}| \oplus |x.{\rm im} \oplus y.{\rm im}| )) \oplus (x.e \oplus y.e)).\\[4pt]
\noalign{\vskip-24pt}
\end{eqnarray*}
\enddemo

The precedence for machine operations is the same as that for true operations, so some  parentheses are unnecessary
and will often be omitted in what
 follows.

\nonumproclaim{Proposition FIXME:7.7 $(A - A)$}
If $x$ and $y$ are {\rm AComplexes,} then
$S(x - y) \supseteq S(x) - S(y)${\rm ,} where
\begin{eqnarray*}
x-y &\equiv& ({\rm re},{\rm im};e) \hbox{ with }\\
{\rm re}& =& x.{\rm re} \ominus y.{\rm re}\\
{\rm im}& =& x.{\rm im} \ominus y.{\rm im}\\
e &=& (1 + 2 EPS) \otimes ( ((EPS/2) \otimes (|{\rm re}| \oplus |{\rm im}|))
 \oplus (x.e \oplus y.e)).
\end{eqnarray*}
\endproclaim

\nonumproclaim{Proposition FIXME:7.8 $(X  \times  D)$}
If $x$ is an {\rm XComplex} and $d$ is a double{\rm ,} then
$S(x \times d) \supseteq S(x) \times S(d)${\rm ,} where
\begin{eqnarray*}
x \times d &\equiv& ({\rm re},{\rm im};e) \hbox{ with}\\[6pt] {\rm re} &= &x.{\rm re} \otimes d\\[6pt] {\rm im} &= &x.{\rm im} \otimes d\\[6pt] e &=& (EPS/2) \otimes ((1 + EPS) \otimes (|{\rm re}| \oplus |{\rm im}|) ). \end{eqnarray*}
\endproclaim

\nonumproclaim{Proposition FIXME:7.9 $(X / D)$}
If $x$ is an {\rm XComplex} and $d$ is a double{\rm ,} then
$S(x / d) \supseteq S(x) / S(d)${\rm ,} where
\begin{eqnarray*}
x / d& \equiv& ({\rm re},{\rm im};e) \hbox{ with }\\[6pt] {\rm re}& =& x.{\rm re} \oslash d\\[6pt] {\rm im} &= &x.{\rm im} \oslash d\\[6pt] e& = &(EPS/2) \otimes ((1 + EPS) \otimes (|{\rm re}| \oplus |{\rm im}|) ).
\end{eqnarray*}

\nonumproclaim{Proposition FIXME:7.10 $(X  \times  X)$}
If $x$ and $y$ are {\rm XComplexes,} then
$S(x \times y) \supseteq S(x) \times S(y)${\rm ,} where
\begin{eqnarray*}
x \times y &\equiv& ({\rm re},{\rm im};e)\hbox{ with}\\[6pt] {\rm re}& =&{\rm  re}1 \ominus {\rm re2}, \hbox{ with ${\rm re1} = x.{\rm re} \otimes y.{\rm re}$ and ${\rm re}2 =
x.{\rm im} \otimes y.{\rm im}$} \\[6pt] {\rm im}& =& {\rm im}1 \oplus {\rm im}2, \hbox{ with ${\rm im}1 = x.{\rm re} \otimes y.{\rm im}$ and ${\rm im}2 = x.{\rm im} \otimes
y.{\rm re}$}\\[6pt] e& =& EPS \otimes ((1 + 2EPS) \otimes  ((|{\rm re}1| \oplus |{\rm re}2|) \oplus  (|{\rm im}1| \oplus |{\rm im}2|))).
\end{eqnarray*}
\endproclaim

\demo{Proof}
The error is bounded by the sum of the contributions from the real part and the imaginary part:
\begin{eqnarray*}
&&|(x.{\rm re} \times  y.{\rm re} - x.{\rm im} \times y.{\rm im}) - ((x.{\rm re} \otimes y.{\rm re}) \ominus
(x.{\rm im}
\otimes y.{\rm im}))| \\[6pt]
&&\quad + |(x.{\rm re} \times  y.{\rm im} + x.{\rm im} \times y.{\rm re}) - ((x.{\rm re} \otimes y.{\rm re}) \oplus (x.{\rm im}
\otimes y.{\rm im}))|.
\end{eqnarray*}
   We want to bound this by a machine formula.  Let us begin by bounding
$$|(x.{\rm re} \times  y.{\rm re} - x.{\rm im} \times y.{\rm im}) - ((x.{\rm re} \otimes y.{\rm re}) \ominus (x.{\rm im}
 \otimes y.{\rm im}))| $$
by a machine formula:\eject

\centerline{
${\displaystyle |(x.{\rm re} \times  y.{\rm re} - x.{\rm im} \times y.{\rm im}) - ((x.{\rm re} \otimes y.{\rm re}) \ominus
(x.{\rm im}
\otimes y.{\rm im}))| }$}
\begin{eqnarray*}
\noalign{\vskip-16pt}
&&\le |((x.{\rm re} \times  y.{\rm re}) - (x.{\rm im} \times y.{\rm im})) - ((x.{\rm re} \otimes y.{\rm re}) -
(x.{\rm im}
\otimes y.{\rm im}))|\\
&&\quad + |((x.{\rm re} \otimes y.{\rm re}) - (x.{\rm im} \otimes y.{\rm im}))-((x.{\rm re} \otimes y.{\rm re})
\ominus (x.{\rm im} \otimes y.{\rm im}))| \\
&&\le |(x.{\rm re} \times  y.{\rm re}) - (x.{\rm re} \otimes y.{\rm re})| + |(x.{\rm
im} \times y.{\rm im})- (x.{\rm im} \otimes y.{\rm im})| \\
&&\quad + (EPS/2)|(x.{\rm re} \otimes y.{\rm re}) - (x.{\rm im} \otimes
y.{\rm im})|\\
&&\le (EPS/2)|(x.{\rm re} \otimes y.{\rm re})| + (EPS/2)|(x.{\rm im} \otimes y.{\rm im})| \\
&&\quad + (EPS/2)(|x.{\rm re} \otimes
y.{\rm re}| + |x.{\rm im} \otimes y.{\rm im}|)\\
&&= (EPS/2)(2) (|x.{\rm re} \otimes y.{\rm re}| + |x.{\rm im} \otimes y.{\rm
im}|)\\
&&\le EPS (1 + EPS/2) (|x.{\rm re} \otimes y.{\rm re}| \oplus |(x.{\rm im} \otimes y.{\rm im}|) .
\end{eqnarray*}

Almost the exact same calculation produces the analogous formula for the imaginary contribution, and we now combine the
two to get a bound on the total error.
\begin{eqnarray*}
&&\le EPS (1 + EPS/2) (|x.{\rm re} \otimes y.{\rm re}| \oplus |x.{\rm im} \otimes y.{\rm im}|) \\
&&\quad +
EPS (1 + EPS/2) (|x.{\rm re} \otimes y.{\rm im}| \oplus |x.{\rm im} \otimes y.{\rm re}| )\\
&&\le EPS \otimes ((1 +
2EPS)\otimes((|x.{\rm re} \otimes y.{\rm re}| \oplus |x.{\rm im} \otimes y.{\rm im}| ) \\
&&\quad \oplus
(|x.{\rm re} \otimes y.{\rm im}| \oplus |x.{\rm im} \otimes y.{\rm re}| ))).
\\
\noalign{\vskip-36pt}
\end{eqnarray*}
\enddemo

\nonumproclaim{Proposition FIXME:7.11 $(D / X)$}
If $x$ is a double and $y$ is an {\rm XComplex,} then
$S(x / y) \supseteq S(x) / S(y)${\rm ,} where
\begin{eqnarray*}
x  / y & \equiv& ({\rm re},{\rm im};e)\hbox{ with }\\
re& =& (x  \otimes y.{\rm re})\oslash nrm \hbox{ where $nrm = y.{\rm re} \otimes y.{\rm re} \oplus y.{\rm im} \otimes
y.{\rm im}$}
\\
{\rm im} &=& -(x  \otimes y.{\rm im}) \oslash nrm\\
e& =& (2EPS) \otimes ((1 +  2EPS)\otimes(|{\rm re}| \oplus |{\rm im}|)).
\end{eqnarray*}
\endproclaim

{\it Proof}.
The true version of $x/y$ is equal to
$$(x \times y.{\rm re} + i (-x \times y.{\rm im}))/ ((y.{\rm re})^2 + (y.{\rm im})^2)$$ and we need to compare this with the
machine version to find the error.  Further, this error is less than or equal to the sum of the real error and the imaginary
error. Thus, we start with the real calculation (as in the statement of the proposition, we use $nrm$ to represent the
machine version of $(y.{\rm re})^2 + (y.{\rm im})^2$).
\begin{eqnarray*}
&&\hskip-36pt \left|{x \times y.{\rm re}  \over (y.{\rm re})^2 + (y.{\rm im})^2} - ((x \otimes y.{\rm re})\oslash
nrm)\right|\\
&
\le &\left|(x
\otimes y.{\rm re}) \oslash nrm - {x \otimes y.{\rm re} \over nrm}\right|\\
&& +\ \left|{x \otimes y.{\rm re} \over nrm} - {x
\times y.{\rm re}
\over nrm}\right| + \left|{x \times y.{\rm re} \over nrm} -
{x \times y.{\rm re}  \over (y.{\rm re})^2 + (y.{\rm im})^2}\right|.
\end{eqnarray*}

Before continuing, let us compare ${1 \over nrm}$ and ${1 \over (y.{\rm re})^2 + (y.{\rm im})^2}$ by developing a formula for
comparing ${1 \over a^2 + b^2}$ and its associated ${1 \over nrm}$:
\nonumproclaim{Lemma FIXME:7.2} $$\left|{1 \over nrm} - {1 \over a^2 + b^2}\right| \le   (EPS + (EPS/2)^2) {1 \over nrm}$$
{\it where} $nrm = a\otimes a \oplus b \otimes b.$
\endproclaim

\demo{Proof}
We compute that
$$\left({1 \over 1 + EPS/2}\right)^2 \times nrm
\le a^2 + b^2
\le \left({1 \over 1 - EPS/2}\right)^2 \times nrm;$$ hence
$${1 \over nrm} (1 - EPS/2)^2
\le {1 \over a^2 + b^2}
\le {1 \over nrm} (1 + EPS/2)^2.$$  It then follows that
\begin{eqnarray*}
\left|{1 \over nrm} - {1 \over a^2 + b^2}\right|& \le &
    {1 \over nrm} (1 + EPS/2)^2 - {1 \over nrm}\\
&=& {1 \over nrm} ((1 + EPS/2)^2 - 1) =
  (EPS + (EPS/2)^2){1 \over nrm} .\\
\noalign{\vskip-24pt}
\end{eqnarray*}
\enddemo

\phantom{someone}
Getting back to our main calculation (with $nrm = y.{\rm re} \otimes y.{\rm re} \oplus y.{\rm im} \otimes y.{\rm im}$), we
have
\begin{eqnarray*}
&&\hskip-16pt\left|(x \otimes y.{\rm re}) \oslash nrm - {x \otimes y.{\rm re} \over nrm}\right|\\
&&\quad +
\left|{x \otimes y.{\rm re} \over nrm} - {x \times y.{\rm re} \over nrm}\right| +
\left|{x \times y.{\rm re} \over nrm} -
{x \times y.{\rm re}  \over (y.{\rm re})^2 + (y.{\rm im})^2}\right|\\
&&  \le(EPS/2){|x \otimes y.{\rm re}| \over nrm} +
        (EPS/2){|x \otimes   y.{\rm re}| \over nrm} +
(EPS + (EPS/2)^2) {|x \times   y.{\rm re}| \over nrm}\\
&&  =(EPS/2)\left({1 \over nrm}\right)(2 |x \otimes y.{\rm re}| +
(2 + EPS/2) \times |x \times y.{\rm re}| )\\
&&  \le (EPS/2)\left({1 \over nrm}\right)(2 |x \otimes y.{\rm re}| +
(2 + EPS/2)(1 + EPS/2) \times |x \otimes y.{\rm re}| )\\
&&\quad = (EPS/2)\left({1 \over nrm}\right)(|x \otimes y.{\rm re}|)
(2 + (2 + EPS/2)(1+EPS/2))\\
&&  \le (EPS/2)(4 + 3EPS/2 + (EPS/2)^2)(|x \otimes y.{\rm re}|)\left({1 \over nrm}\right)\\
&& \le (EPS/2)(4 + 3EPS/2 +
(EPS/2)^2)(1+EPS/2)(|x \otimes y.{\rm re}| \oslash nrm)\\
&&  \le (2EPS)(1 + 3EPS/8 + (EPS/4)^2)(1+EPS/2)(|(x \otimes y.{\rm
re} \oslash nrm)|).
\end{eqnarray*}
 We also get the analogous formula for the imaginary contribution for the error, so our total error is
bounded by
\begin{eqnarray*}
&&\hskip-24pt (2EPS)(1 + 3EPS/8 + (EPS/4)^2)(1+EPS/2)((|(x \otimes y.{\rm re})\oslash nrm|)\\
&&\quad+(|(x \otimes y.{\rm
im})\oslash nrm|))\\
&& \le (2EPS)(1 + 3EPS/8 + (EPS/4)^2)(1+EPS/2)^2\\
&&\quad\cdot\ ((|(x \otimes y.{\rm re})\oslash nrm|)  \oplus (|(x \otimes
y.{\rm im})\oslash nrm|))\\
&&  \le (2EPS)(1 - EPS/2)(1+ 2EPS)\\
&&\quad\cdot\ ((|(x \otimes y.{\rm re})\oslash nrm|) \oplus (|(x \otimes y.{\rm
im})\oslash nrm|))\\
&&  \le (2EPS) \otimes ((1+ 2EPS) \otimes ((|(x \otimes y.{\rm re})\oslash nrm|)\\
&&\quad  \oplus (|(x \otimes y.{\rm
im})\oslash nrm|))).
\end{eqnarray*}

Here we used the fact that
\vglue6pt
\hfill $(1 + 3EPS/8 + (EPS/4)^2)(1+EPS/2)^2 \le (1 - EPS/2)(1+ 2EPS).$ \hfill\qed
\vglue9pt

This should give the flavor of division proofs.  As such, we will skip the proofs of $X/X$ and $A/A$ and simply refer to the
 {\it Annals} web site.
\nonumproclaim{Proposition FIXME:7.12 $(X / X)$}
If $x$ and $y$ are {\rm XComplexes,} then
$S(x / y) \supseteq S(x) / S(y)${\rm ,} where
\begin{eqnarray*}
x  / y  &\equiv& ({\rm re},{\rm im};e)\hbox{ with}\\
{\rm re}& =& (x.{\rm re} \otimes y.{\rm re} \oplus x.{\rm im} \otimes y.{\rm im}) \oslash nrm\\
&&\hbox{ where $nrm = y.{\rm
re}
\otimes y.{\rm re} \oplus y.{\rm im} \otimes y.{\rm im}$}\\
{\rm im}& = &(x.{\rm im} \otimes y.{\rm re} \ominus x.{\rm re} \otimes y.{\rm im}) \oslash nrm\\
e& =& (5EPS/2) \otimes ((1
+  3EPS) \otimes A) \hbox{  where}\\
 A& =&
((|x.{\rm re} \otimes y.{\rm re}| \oplus |x.{\rm im} \otimes y.{\rm im}|)
 \oplus
 (|x.{\rm im} \otimes y.{\rm re}| \oplus |x.{\rm re} \otimes y.{\rm im}|))
\oslash nrm
.\end{eqnarray*}
\endproclaim

\nonumproclaim{Proposition FIXME:7.13 $(A / A)$}
If $x$ and $y$ are {\rm AComplexes} with
$y.e < 100 EPS \otimes |y|${\rm ,} or{\rm ,} more accurately{\rm , }
$$(y.e)^2< ((10000 EPS) \otimes EPS)\otimes nrm$$ then
$S(x / y) \supseteq S(x) / S(y)${\rm ,} where
\begin{eqnarray*}
x  / y & \equiv& ({\rm re},{\rm im};e)\hbox{ with}\\
{\rm re}& =& (x.{\rm re} \otimes y.{\rm re} \oplus x.{\rm im} \otimes y.{\rm im}) \oslash nrm\\
&&\hbox{ where $nrm = y.{\rm
re}
\otimes y.{\rm re} \oplus y.{\rm im} \otimes y.{\rm im}$}\\
{\rm im}& =& (x.{\rm im} \otimes y.{\rm re} \ominus x.{\rm re} \otimes y.{\rm im}) \oslash nrm\\
e& =& (1 + 4EPS)\otimes
(
((5 EPS/2) \otimes A \oplus (1+103EPS) \otimes B)
\oslash nrm)\hbox{ where}\\
 A& =& (|x.{\rm re} \otimes y.{\rm re}| \oplus |x.{\rm im}\otimes y.{\rm im}|)
 \oplus
(|x.{\rm im} \otimes y.{\rm re}| \oplus | x.{\rm re} \otimes y.{\rm im}|)\\
B& =& x.e\otimes(|y.{\rm re}|\oplus|y.{\rm im}|)
\oplus
   (|x.{\rm re}|\oplus|x.{\rm im}|)\otimes y.e.
\end{eqnarray*}
\endproclaim

In our last proposition we will construct the square-root function.  As a warm-up, ignoring round-off error, our
construction is as follows.  If $x = x.{\rm re} + i x.{\rm im}$ then $\sqrt{x} = s + {\rm i d}$ where $s=\sqrt{(|x.{\rm
re}| + h(x.{\rm re},x.{\rm im}))/2}$ and $d = x.{\rm im}/(2s)$ when $x.{\rm re} > 0.0$, and $\sqrt{x} = d + i s$ otherwise.
Thus, we take our (no-round-off) square roots to be in the first and fourth quadrants.

\nonumproclaim{Proposition FIXME:7.14 ($\sqrt{X}$)}
If $x$ is an {\rm XComplex,} then $S(\sqrt{x}) \supseteq \sqrt{S(x)}$
where we
let $s_o = \root o \of {(|x.{\rm re}| \oplus h_o (x.{\rm re},x.{\rm im})) \otimes 0.5}$
 and $d_o = (x.{\rm im} \oslash s) \otimes 0.5${\rm ,} and define
\begin{eqnarray*}
 \sqrt{x} & \equiv &({\rm re},{\rm im};e) \hbox{ where }\\
 {\rm re} &=& s_o \hbox{ if $x.{\rm re} > 0.0$ and $re = d_o$ otherwise},\\
{\rm im}& =& d_o \hbox{ if $x.{\rm re} > 0.0$ and $im = s_o$ otherwise},
\\
e& =& EPS \otimes ((1 + 4 EPS) \otimes (1.25 \otimes s_o \oplus 1.75 \otimes |d_o|)).
\end{eqnarray*}
\endproclaim

\demo{Proof}
This will be a little nasty.  Let us begin by analyzing $e_s$, which is the difference between the true calculation of $s$ and
the machine calculation of~$s$, that is $e_s = |s - s_o|.$   First, we bound $s.$
\begin{eqnarray*}
s &=& \sqrt{(|x.{\rm re}| + h(x.{\rm re},x.{\rm im})) * 0.5}\\
&\le& (1+EPS)^{1/2} \sqrt{(|x.{\rm re}| + h_o(x.{\rm re},x.{\rm im}))
* 0.5}\\
&\le& (1+EPS)^{1/2} (1+EPS/2)^{1/2}
\sqrt{(|x.{\rm re}| \oplus h_o(x.{\rm re},x.{\rm im})) * 0.5}\\
&\le& (1+EPS)^{1/2} (1+EPS/2)^{1/2}\\
&&\cdot\ (1+EPS/2)
\root o \of {(|x.{\rm re}| \oplus h_o(x.{\rm re},x.{\rm im})) * 0.5}\\
&=& (1+EPS)^{1/2} (1+EPS/2)^{3/2} s_o.
\end{eqnarray*}
By a power series expansion, we see that
\begin{eqnarray*}
(1+EPS)^{1/2} (1+EPS/2)^{3/2}&=&\left(1 + {1 \over 2} EPS - {1 \over 8} EPS^2 + \cdots\right)\\
&& +\
     \left(1 + {3 \over 2} EPS/2 + {3 \over 8} (EPS/2)^2 +\cdots\right)\\
&=&\left(1 + {5 \over 4} EPS + {11 \over 32} EPS^2 +\cdots\right),
\end{eqnarray*}
so that,
$$s \le \left(1 + {5 \over 4} EPS + {11 \over 32} EPS^2 + \cdots\right) s_o.$$
Similarly,
$$s \ge \left(1 - {5 \over 4} EPS\right) s_o.$$

Thus, we can bound the $s$ error,
\begin{eqnarray*}
e_s = |s - s_o|&\le&\left(\left(1 + {5 \over 4} EPS + {11 \over 32} EPS^2 +\cdots\right) - 1\right) s_o\\
&=& \left({5 \over 4} EPS +
{11 \over 32} EPS^2 +\cdots\right) s_o.
\end{eqnarray*}

Next, we analyze $e_d$, which is the absolute value of the difference between the true calculation of $d$ and the machine calculation of $d$.  That is, $e_d = |d - d_o|.$
\begin{eqnarray*}
e_d &=& |x.{\rm im}/(2s) - x.{\rm im} \oslash (2 s_o)|\\
&\le& |x.{\rm im} \oslash (2 s_o) - x.{\rm im} / (2 s_o)| +
|x.{\rm im}/(2s_o) - x.{\rm im}/(2s) |\\
&\le& (EPS/2) |x.{\rm im} / (2 s_o)| + \left|{x.{\rm im} \over 2} {s - s_o \over s s_o} \right|\\
&\le& (EPS/2) |x .{\rm im}/ (2
s_o)| +\left|{x.{\rm im} \over 2} {1 \over s s_o} ((5/4)EPS + (11/32)EPS^2+\cdots) s_o\right|\\
&\le& (EPS/2) |x.{\rm im} / (2
s_o)| \\
&&+ \left|{x.{\rm im} \over 2} {1 \over s_o (1 - (5/4)EPS)}((5/4)EPS + (11/32)EPS^2+\cdots)\right|\\
&= &(EPS/2) |x .{\rm
im}/ (2 s_o)| \left(1 + {(5/2) + (11/16)EPS+\cdots) \over (1 - (5/4)EPS)}\right)\\
&= &(EPS/2) {(7/2) + (-9/16) EPS +\cdots\over
(1 - (5/4)EPS)} |x .{\rm im}/ (2 s_o)|\\
&\le& (EPS/2) (1+EPS/2)
 {7/2 \over (1 - (5/4)EPS)}
|x .{\rm im} \oslash (2 s_o)|\\
&= &(EPS/2) (1+EPS/2)
 {7/2 \over (1 - (5/4)EPS)}
|d_o|.
\end{eqnarray*}

Finally, we can bound the overall error $e = e_s + e_d$.
\begin{eqnarray*}
e_s + e_d&\le& \left({5 \over 4} EPS + {11 \over 32} EPS^2 +\cdots\right) s_o\\
&& +\
 (EPS/2) (1+EPS/2)
 {7/2 \over (1 - (5/4)EPS)}|d_o|\\
&\le& \left( EPS + {11 \over 40} EPS^2 +\cdots\right) \left({5 \over 4} s_o\right) \\
&&+\
 EPS (1+EPS/2)
 {1 \over (1 - (5/4)EPS)}\left|{7 \over 4} d_o\right|\\
&\le& EPS (1+EPS/2)
 {1 \over (1 - (5/4)EPS)} \left({5 \over 4} s_o\right) \\
&&+
\  EPS (1+EPS/2)
 {1 \over (1 - (5/4)EPS)}\left|{7 \over 4} d_o\right|\\
&\le& EPS (1+EPS/2)
 {1 \over (1 - (5/4)EPS)}
\left({5 \over 4} s_o +\left|{7 \over 4} d_o\right|\right)\\
&\le& EPS (1+EPS/2)^3
 {1 \over (1 - (5/4)EPS)}
\left({5 \over 4} \otimes s_o \oplus \left|{7 \over 4} \otimes d_o\right|\right)\\
&\le& EPS (1 - (EPS/2))(1 + 4 EPS)
\left({5 \over 4} \otimes s_o \oplus \left|{7 \over 4} \otimes d_o\right|\right)\\
&\le& EPS \otimes \left((1 + 4 EPS) \otimes
\left({5 \over 4} \otimes s_o \oplus \left|{7 \over 4} \otimes d_o\right|\right)\right).\\
\noalign{\vskip-36pt}
\end{eqnarray*}
\enddemo

Now, we develop two formulas for the absolute value of an XComplex.

\demo{Formula {\rm FIXME:7.0 (${\rm absUB}(X)$)}}
If $x$ is an XComplex, then
there is an upper bound on the absolute value of $x$ as follows:
\begin{eqnarray*}
|x| &=& h(x.{\rm re},x.{\rm im}) \le (1 + EPS) h_{\circ} (x.{\rm re},x.{\rm im})\\
&\le& (1 - EPS/2) (1 + 2EPS)h_{\circ} (x.{\rm re},x.{\rm im}) \\
&\le& (1 + 2EPS) \otimes h_{\circ} (x.{\rm re},x.{\rm im}).
\end{eqnarray*}
Thus, we define
$${\rm absUB}(x) = (1 + 2EPS) \otimes h_{\circ} (x.{\rm re},x.{\rm im}).$$
\enddemo

{\it Formula} FIXME:7.1 (${\rm absLB}(X)$).
If $x$ is an XComplex, then
we get a lower bound on the absolute value of $x$ as follows.
\begin{eqnarray*}
|x|& =& h(x.{\rm re},x.{\rm im}) \ge (1 - EPS) h_{\circ} (x.{\rm re},x.{\rm im})\\
&\ge& (1 + EPS/2) (1 - 2EPS) h_{\circ} (x.{\rm
re},x.{\rm im})\\
&\ge& (1 - 2EPS) \otimes h_{\circ} (x.{\rm re},x.{\rm im}).
\end{eqnarray*}
Thus, we define
$${\rm absLB}(x) = (1 - 2EPS) \otimes h_{\circ} (x.{\rm re},x.{\rm im}).$$
\vglue12pt

Finally, in several places in the {\it verify} program we perform a standard operation on a pair of doubles and must take into account round-off error.  This is easy if we use Lemma FIXME:7.0.

For example, in {\it inequalityHolds} we want to show that $$wh \times wh  > {\rm absUB}{\rm (along),}$$ where $wh =
{\rm absLB}{\rm (whirle)}.$
By Lemma FIXME:7.0, we know that $$(1 - EPS) \otimes (wh \otimes wh) \le wh \times wh$$ and we simply test that
$$(1 - EPS) \otimes  (wh \otimes wh) \ge {\rm absUB}{\rm (along)}.$$

A slightly more complicated version of this occurs in the computer calculation of ${\rm pos}[i]$ and $size[i],$ that is, the center and size of a sub-box.  Prior to multiplication by ${\rm scale}[i] = 2 ^{(5 - i)/6},$  the calculations of ${\rm pos}$ and $size$ are exact.  However, multiplication by $scale$ introduces round-off error.  For the center of the sub-box we will have the computer use ${\rm pos}[i] \otimes {\rm scale}[i]$ with the realization that this is not necessarily ${\rm pos}[i] \times {\rm scale}[i]$.   Thus, we have to choose appropriate sizes to ensure that the machine sub-box contains the true sub-box.

Notationally, this is annoying, because we typically use a computer command like ${\rm pos}[i] = {\rm pos}[i] \otimes {\rm scale}[i],$ while in an exposition, we need to avoid that.  We will denote the true center of the sub-box by $p[i]$ and the machine center of the sub-box by $p_0[i],$ and the true and machine sizes will be denoted $s[i]$ and $s_0[i].$  We will let $pos[i]$ and $size[i]$ be the position and size (true and machine are the same) before multiplication by ${\rm scale}[i].$

Let $p[i] = pos[i] \times {\rm scale}[i],\ p_0[i] = pos[i] \otimes {\rm scale}[i],$ and
$s[i]= size[i] \times {\rm scale}[i].$  We must select $s_0[i]$ so that
$p_0[i] + s_0[i] \ge p[i] + s[i].$  (Here, taking $+$ on the left-hand side is correct, because the need for machine calculation there is incorporated at other points in the programs.)  So, we must find $s_0[i]$ such that $s_0[i] \ge (p[i] - p_0[i]) + s[i].$
\begin{eqnarray*}
(p[i] - p_0[i]) + s[i]. &\le& (EPS/2) |p_0[i]| + {\rm size}[i] \times {\rm scale}[i]\\[4pt]
&\le& (EPS/2) |p_0[i]| + (1 + EPS/2) ({\rm size}[i] \otimes
{\rm scale}[i])\\[4pt]
&\le& (1 + EPS/2) ((EPS/2) |p_0[i]| +  ({\rm size}[i] \otimes {\rm scale}[i]))\\[4pt]
&\le& (1 + EPS/2)^2 ((EPS/2) |p_0[i]| \oplus  ({\rm size}[i]
\otimes {\rm scale}[i]))\\[4pt]
&\le& (1 + 2EPS) \otimes ((EPS/2) |p_0[i]| \oplus  ({\rm size}[i] \otimes {\rm scale}[i])).
\end{eqnarray*}
Thus we take
$$s_0 [i]= (1 + 2EPS) \otimes ((EPS/2) |p_0[i]| \oplus  ({\rm size}[i] \otimes {\rm scale}[i])).$$
This also works to give $p_0[i] - s_0[i] \le p[i] - s[i].$\hfill\qed


\begin{enumerate}[(a)]
	\item This is not a degree sequence of a graph, as there would be an odd
		number of vertices with odd degrees.
	\item Suppose this was the degree sequence of a graph. Then, by Havel-Hakimi
		theorem, so is $3,2,1,0,0,0$, and $1,0,0,0,0,-1$. But the last is clearly
		not a degree sequence, so the initial sequence is not either.
	\item This is the degree sequence of the graph \\
		\begin{tikzpicture}
			\draw
			(0:1) node {} -- (60:2) node {} -- (0:3) node {}
			(180:1) node {} -- (120:2) node {} -- (180:3) node {}
			(120:2) -- (60:2)
			(240:2) node {} -- (300:2) node {}
			;
		\end{tikzpicture}
	\item Iterations of Havel-Hakimi algorithm go as follows:
\subparagraph{0} 7,4,3,3,2,2,2,1,1,1
\subparagraph{1}   3,2,2,1,1,1,0,1,1
\subparagraph{2}     1,1,0,1,1,0,1,1 which is, of course (with rearrangement), the degree sequence of the graph of $3K_2$ with 2 isolated vertices added.
A graph with the original degree sequence is then \\
\begin{tikzpicture}
	\draw
	(0:2) node {} -- (0:0) node {} -- (45:2) node {} -- (90:2) node {} -- (0:0)
	(0:0) -- (135:2) node {} -- (180:2) node {} -- (270:2) node {} -- (0:0)
	(0:0) -- (225:1) node {} -- (180:2) -- (0:0)
	(225:1) -- (270:2)
	(300:2) node {} -- (330:2) node {}
	;
\end{tikzpicture}
\end{enumerate}


\chapter{May}

\section{Build sparse matrix with Eigen efficiently} \index{Build sparse matrix with Eigen efficiently}

When I build a sparse matrix with Eigen, I come across a problem that it is quite slow. The problem is that
when you \textit{insert} elements one by one, each time Eigen needs to reallocate space for the matrix and copy
the data, which is reason that it can be slow.

The solution is to first record the row, column index as well as value of non-zero entries with \textit{triplets},
and then build the sparse matrix with the function \textit{setFromTriplets}.

\chapter{June}
\section{Transform matrix and projective matrix} \index{Transform matrix and projective matrix}
To transform a vertex from world space to camera space, we need to construct the rigid transform matrix. Given a right
hand system, we need \textbf{eye} $\in \mathbb{R}^3$ position, \textbf{lookAt} $\in \mathbb{R}^3$ position as well as \textbf{up} $\in \mathbb{R}^3$ direction to determine the matrix.
The matrix is in the following format:
 \myequ{
V & =       \begin{pmatrix}
           s.x & u.x & -f.x & 0.0 \\
           s.y & u.y & -f.y & 0.0 \\
           s.z & u.z & -f.z & 0.0 \\
           - (s \cdot eye) & -(u \cdot eye) & -(f \cdot eye) & 1.0
       \end{pmatrix} \\
  where\  & f = normalize(lookAt - eye) \\
  & s = normalize(f \times up) \\
  & u = s \times f
}

\begin{remark}
    Note this matrix is constructed for right-hand coordinate system.
\end{remark}

In addition, to transform a point from camera space to image space, we need to construct the perspective projection matrix.
In this case, we need field of view in $x$ and $y$ axis, namely \textbf{fovX} and \textbf{fovY}, \textbf{nearClip} and \textbf{farClip}. The matrix is build as:

 \myequ{
     P & =       \begin{pmatrix}
        \frac{1}{\text{tan}(\frac{fovX}{2})} & 0 & 0 & 0 \\
        0 & \frac{1}{\text{tan}(\frac{fovY}{2})} & 0 & 0 \\
        0 & 0 & -\frac{far + near}{far - near}
        &  -\frac{2 * far * near}{far - near} \\
        0 & 0 & -1 & 0
 \end{pmatrix}
}


\section{Materials and Methods}
\label{sec:method}
Consider a panmictic diploid population with fixed size of $N$
individuals.  Let $\bm{ \nu} =\{\nu_t\}_{t\in\Tc}$ be frequencies of
the derived allele at generations $t\in\Tc$ for a given variant, where
at generations $\Tc= \{\tau_i: 0\le \tau_0<\tau_1\ldots< \tau_T\}$
samples of $n$ individuals are chosen for pooled sequencing. The
experiment is replicated $R$ times. We denote allele frequencies of
the $R$ replicates by the set $\{\bm{\nu}\}_R$.  To identify the genes
and variants that are responding to selection pressure, we use the
following procedure:
\begin{enumerate}
\item {\bf Estimating population size.} The procedure starts by
  estimating the effective population size, $\hN$, under the
  assumption that much of the genome is evolving neutrally.
\item {\bf Estimating selection parameters.} For each polymorphic
  site, selection and dominance parameters $s,h$ are estimated so
  as to maximize the likelihood of the time series data, given $\hN$.
\item {\bf Computing likelihood statistics.} For each variant, a
  log-odds ratio of the likelihood of selection model ($s>0$) to the
  likelihood of neutral evolution/drift model is computed. Likelihood
  ratios in a genomic region are combined to compute the \comale\
  statistic for the region.
\item {\bf Hypothesis testing.} An empirical null distribution of the
  \comale\ statistic is calculated using genome-wide drift
  simulations, and used to compute $p$-values and thresholds for a
  specified FDR. We perform single locus hypothesis testing within
  selected regions to identify significant variants and report genes
  that intersect with the selected variants.
\end{enumerate}
These steps are described in detail below.
\subsection{Estimating Population Size}
Methods for estimating population sizes from temporal neutral
evolution data have been
developed~\cite{williamson1999using,anderson2000monte,
  bollback2008estimation, Terhorst2015Multi,jonas2016estimating}.
Here, we aim to extend these models to explicitly model the sampling
noise that arise in pool-seq data. Specifically, we model the
variation in sequence coverage over different locations, and the noise
due to sequencing only a subset of the individuals in the population.
In addition, many existing
methods~\cite{bollback2008estimation,feder2014Identifying,topa2015gaussian,Terhorst2015Multi}
are designed for large populations, and model frequency as a
continuous quantity. We observed that using Brownian motion to model
frequency drift may be inadequate for small populations, low starting
frequencies and sparse sampling (in time), factors that are common in
experimental evolution (see Results, \ref{fig:power}A-C, and
\ref{fig:markov}). To this end, we model the Wright-Fisher Markov
process for generating pool-seq data~(\ref{proc:arya}) via a
{discrete} HMM (~\ref{fig:1}-B). We start by computing a likelihood
function for the population size given neutral pool-seq data.

\paragraph{Likelihood for Neutral Model.}
We model the allele frequency counts $2N\nu_t$ as being sampled from a
Binomial distribution. Specifically,
\begin{eqnarray*}
  \nu_0 &\sim& \pi,\\
  2N\nu_t|\nu_{t-1} &\sim& \bino(2N,\nu_{t-1})
\end{eqnarray*}
where $\pi$ is the global distribution of allele frequencies in the
base population.
Note that  $\pi$
depends on the
demographic history of the founder lines and can be estimated from site
frequency spectrum(see~\ref{fig:sfs}) of the initial population. For notational
convenience, henceforth we omit the dependence of likelihoods to the
parameter $\pi$.

To estimate frequency after $\tau$ transitions, it is enough to
specify the $2N\times2N$ transition matrix $P^{(\tau)}$, where
$P^{(\tau)}[i,j]$ denotes probability of change in allele frequency
from ${i}/{2N}$ to ${j}/{2N}$ in $\tau$ generations:
\beq
  P^{(1)}[i,j] &= \pr\left(\nu_{t+1}=\frac{j}{2N} \left| \right.
  \nu_{t}=\frac{i}{2N}\right)\\
  &={2N \choose j} s  \nu_{t}^j
  (1-\nu_{t})^{2N-j}, \label{eq:P1}
\eeq
\beq
  P^{(\tau)} =   P^{(\tau-1)}P^{(1)} \label{eq:Pt}
\eeq
Furthermore, in an E\&R experiment, $n\le N$ individuals are randomly
selected for sequencing. The {sampled allele frequencies},
$\{y_{t}\}_{t\in\Tc}$, are also Binomially distributed \beq 2ny_{t}
\sim \text{Binomial}(2n,\nu_t) \eeq We introduce the $2N\times2n$
sampling matrix $Y$, where $Y[i,j]$ stores the probability that the
sample allele frequency is ${j}/{2n}$ given that the true allele
frequency is ${i}/{2N}$.

We denote the pool-seq data for that variant as $\{x_t = \langle
c_t,d_t \rangle\}_{t\in\Tc}$ where $d_t, c_t$ represent the coverage,
and the read count of the derived allele, respectively. Let
$\{\lambda_t\}_{t\in\Tc}$ be the sequencing coverage at different
generations. Then, the observed data are sampled according to
\begin{equation} d_t \sim \poiss(\lambda_t), \hspace{1in} c_t \sim
\bino(d_t,y_t)
\end{equation}
The emission probability for a observed tuple $x_t=\langle d_t,
c_t\rangle $ is
\begin{equation} {\bf e}_{i}(x_t) = {d_t \choose c_t}
\left(\frac{i}{2n} \right)^{c_t}\left (1- \frac{i}{2n}
\right)^{d_t-c_t} .
\end{equation}
For $1\le t\le T, 1\le j\le 2N$, let $\alpha_{t,j}$ denote the
probability of emitting $x_1,x_2,\ldots,x_t$ and reaching state $j$ at
$\tau_t$. Then, $\alpha_{t}$ can be computed using the
forward-procedure~\cite{durbin1998biological}:
\begin{align}
	\alpha_t^T&=\alpha_{t-1}^T P^{(\delta_t)}\text{diag}(Y\bfe(x_t))
	\label{eq:hmm}
\end{align}
where $\delta_t=\tau_t-\tau_{t-1}$. The joint likelihood of the
observed data from $R$ independent observations is given by
\beq
\Lc(N|\{\bm{x}\}_R,
n)&=\prod_{r=1}^R\Lc(N|\bm{x}^{(r)},n)=\pr(\{\bm{x}\}_R|N,n)\\& =
	\prod_{r=1}^R \sum_i\alpha_{T,i}^{(r)}
	\label{eq:hmmlik}
\eeq
where $\bm{x}=\{x_t\}_{t\in \Tc}$. The graphical model and the generative
process for which data is being generated is depicted in~\ref{fig:1}-B
and~\ref{proc:arya}, respectively.

Finally, the last step is to compute an estimate $\hN$ that maximizes
the likelihood of all $M$ variants in whole genome. Let
$\bm{x}_i^{(r)}$ denote the time-series data of the $i$-th variant in
replicate $r$. Then,
\begin{equation}
 \hN =
\underset{N}{\arg \max} \prod_{i=1}^M  \prod_{r=1}^R\Lc(N|\bm{x}_i^{(r)})
\label{eq:mlen}
\end{equation}

\subsection{Estimating Selection Parameters}

\paragraph{Likelihood for Selection Model.}
Assume that the site is evolving under selection constraints $s\in
\Rbb$, $h\in \Rbb_+$, where $s$ and $h$ denote selection strength and
dominance parameters ,
respectively. By definition, the relative fitness values of genotypes
0$|$0, 0$|$1 and 1$|$1 are given by $w_{00}=1$, $w_{01}=1+hs$ and
$w_{11}=1+s$.  Then, $\nusp$, the frequency at time
$\tau_{t}+1$ (one generation ahead), can be estimated using:
\beq
\hat{\nu}_{t^+} =
\mathbb{E}[\nusp|s,h,\nu_t]&=\frac{w_{11}\nu_t^2 +
  w_{01}\nu_t(1-\nu_t)}{w_{11}\nu^2_t + 2w_{01}\nu_t(1-\nu_t) +
  w_{00}(1-\nu_t)^2}\\
&=\nu_t+\frac{s(h+(1-2h)\nu_t)\nu_t(1-\nu_t)}{1+s\nu_t(2h+(1-2h)\nu_t))}.
  \label{eq:transition}
\eeq
The machinery for computing likelihood of the selection parameters is
identical to that of population size, except for transition matrices. Hence, here
we only describe the definition transition matrix $Q_{s,h}$ of the selection
model.
Let $Q^{(\tau)}_{s,h}[i,j]$ denote the
probability of transition from ${i}/{2N}$ to ${j}/{2N}$ in
$\tau$ generations, then (See~\cite{Ewens2012Mathematical}, Pg.~24,
Eqn.~$1.58$-$1.59$):
\beq
  Q^{(1)}_{s,h}[i,j] &= \pr\left(\nusp=\frac{j}{2N} \left\lvert
      \nu_{t}=\frac{i}{2N};s,h,N \right .\right)\\
  &    ={2N \choose j}
  \hat{\nu}_{t^+}^{j} (1-\hat{\nu}_{t^+})^{2N-j}\label{eq:Q1}
  \eeq
  \beq
  Q^{(\tau)}_{s,h} &= Q^{(\tau-1)}_{s,h}Q^{(1)}_{s,h}\label{eq:Qt}
\eeq
The maximum likelihood estimates are given by
\beq
\hs,\hh = \underset{s,h}{\arg \max} \prod_{r=1}^R \Lc(s,h|\bm{x}^{(r)},\hN)
\label{eq:mlesh}
\eeq

Using grid search, we first estimate $N$ (Eq.~\ref{eq:mlen}), and
subsequently, we estimate parameters $s,h$
(Eq.~\ref{eq:mlesh},~\ref{fig:slikes}). By
broadcasting and vectorizing the grid search operations across all
variants, the genome scan on millions of polymorphisms can be done in
significantly smaller time than iterating a numerical optimization
routine for each variant(see Results and \ref{fig:runTime}).
\subsection{Empirical Likelihood Ratio Statistics}
The  likelihood
ratio statistic for testing directional selection, to be computed for each variant,
is given by
\beq
	H &= -2 \log
	\left(\frac{\Lc(\bar{s},0.5|\{\bm{x}\}_R,\hN)}{\Lc(0,0.5|\{\bm{x}\}_R,\hN)}\right),\\
	\label{eq:ELRS}
\eeq
where $\bar{s} = \underset{s}{\arg \max} \prod_{r=1}^R
 \Lc(s,0.5|\bm{x}^{(r)},\hN)$. Similarly we can define a test statistic for testing
 if selection is dominant by
\beq
 D &= -2 \log
 \left(\frac{\Lc(\hs,\hh|\{\bm{x}\}_R,\hN)}{\Lc(\bar{s},0.5|\{\bm{x}\}_R,\hN)}\right).
 \eeq

 While extending the single-locus WF model to a multiple linked-loci
 can improve the power of the model~\cite{Terhorst2015Multi}, it is
 computationally and statistically expensive to compute exact
 likelihood. In addition, computing linked-loci joint likelihood requires
 haplotype resolved data, which pool-seq
 does not provide. Here, similar to Nielsen~\emph{et
   al}~\cite{nielsen2005genomic}, we calculate \emph{composite likelihood
 ratio} score for a genomic region.
\begin{equation}
\Hc = \frac{1}{|L|}\sum_{\ell \in L} H_\ell.
\label{eq:pihmm}
\end{equation}
where $L$ is a collection of segregating sites and $H_\ell$ is the
likelihood ratio score based for each variant $\ell$ in $L$.  The
optimal value of the hyper-parameter $L$ depends upon a number of
factors, including initial frequency of the favored allele,
recombination rates, linkage of the favored allele to neighboring
variants, population size, coverage, and time since the onset of
selection (duration of the experiment). In~\ref{sec:winSize}, we
provide a heuristic to compute a reasonable value of $L$, based on
experimental data.

We work with a normalized value of $\Hc$, given by
\begin{equation} \Hc_i^*=\frac{\Hc_i-\mu_\Cc}{\sigma_\Cc},
\hspace{0.5in} \forall i \in \Cc,
\end{equation}
where $\mu_\Cc$ and $\sigma_\Cc$ are the mean and standard deviation
of $\Hc$ values in a large region $\Cc$. We found different
chromosomes to have different distribution of $\Hc_i$ values, and
therefore decided to use single chromosomes as $\Cc$.
\subsection{Hypothesis Testing}
\paragraph{Single-Locus tests.}
Under neutrality, Log-likelihood ratios can be approximated by $\Xc^2$
distribution~\cite{williams2001weighing}, and $p$-values can be
computed directly. However, Feder \emph{et
  al.}~\cite{feder2014Identifying} showed that when the number of
independent samples (replicates) is small, $\Xc^2$ is a crude
approximation to the true null distribution and results in more false
positive.  Following their suggestion, we first compute the empirical
null distribution using simulations with the estimated population size
(See~\ref{proc:arya}). The empirical null distribution of statistic
$H$ is used to compute $p$-values as the fraction of null values that
exceed the test score.  Finally, we use Storey and Tibshirani's
method~\cite{storey2003statistical} to control for False Discovery
Rate in multiple testing.

\paragraph{Composite likelihood tests.}

Similar to single-locus tests, we compute the null distribution of the
$\Hc^*$ statistic using whole-genome simulations with the estimated
population size, and subsequently compute FDR. The simulations for
generating the null distribution of $\Hc^*$ are described next.

\subsection{Simulations}\label{sec:sims}
We use the same simulation procedure for two purposes. First, we use
them to test the power of \comale\ against other methods in small
genomic windows. Second, we use the simulations to generate the
distribution of null values for the statistic to compute empirical
$p$-values. We mainly chose parameters that are relevant to \dmel
experimental evolution~\cite{kofler2013guide}. See also \ref{fig:1}-A
for illustration.
\begin{enumerate}
\item {\bf Creating initial founder line haplotypes.} Using
  \texttt{msms}~\cite{ewing2010msms}, we created neutral populations
  for $F$ founding haplotypes with command \texttt{\$./msms <F> 1 -t
    <2$\mu WN_o$> -r <$2rWN_o$> <W>}, where $F=200$ is number of
    founder
  lines, $N_o=10^6$ is effective founder population size,
  $r=2\times10^{-8}$ is recombination rate, $\mu=2\times 10^{-9}$ is
  mutation rate. The window size $W$ is used to compute $\theta=2\mu
  N_oW$ and $\rho=2N_orW$. We chose $W=50$Kbp for simulating
  individual windows for performance evaluations, and $W=20$Mbp for
  simulating \dmel chromosomes for $p$-value computations.

\item{\bf Creating initial diploid population.} An initial set of
  $F=200$ haplotypes was created from step I, and duplicated to create
  $F$ homozygous diploid individuals to simulate generation of inbred
  lines. $N$ diploid individuals were generated by sampling with
  replacement from the $F$ individuals.

\item{\bf Forward Simulation.} We used forward simulations for
  evolving populations under selection. We also consider selection
  regimes which the favored allele is chosen from standing variation
  (not \emph{de novo} mutations). Given initial diploid population,
  position of the site under selection, selection strength $s$, number
  of replicates $R=3$, recombination rate $r=2\times10^{-8}$ and
  sampling times $\Tc=\{0,10,20,30,40,50\}$,
  \texttt{simuPop}~\cite{peng2005simupop} was used to perform forward
  simulation and compute allele frequencies for all of the $R$
  replicates.  For hard sweep (respectively, soft sweep) simulations
  we randomly chose a site with initial frequency of $\nu_0=0.005$
  (respectively, $\nu_0=0.1$) to be the favored allele. For generating
  the null distribution with drift for $p$-value computations, we used
  this procedure with $s=0$.

\item{\bf Sequencing Simulation.} Given allele frequency trajectories
  we sampled depth of each site in each replicate identically and
  independently from Poisson($\lambda$), where $\lambda \in
  \{30,100,300\}$ is the coverage for the experiment. Once depth $d$
  is drawn for the site with frequency $\nu$, the number of reads $c$
  carrying the derived allele are sampled according to
  Binomial$(d,\nu)$. For experiments with finite depth the tuple
  $\langle c,d\rangle$ is the input data for each site.
\end{enumerate}


    \documentclass{article}
    \usepackage{amsfonts}
    \usepackage{amsmath,multicol,eso-pic}
    \begin{document}
    \title{Algebra 101 exam 1}
 \date{\vspace{-5ex}}
 \maketitle

    \section{Linear equations}
    Solve the following equations for the specified variable.
    \begin{multicols}{2}
    \begin{enumerate}
    \item Solve for $e$ : $J + e n = N e -11$
\item Solve for $V$ : $- 3 V + d = 6 V + z$
\item Solve for $f$ : $R f + R = 17 f -9$
\item Solve for $S$ : $- 14 S + 20 = - 17 S + k$
\item Solve for $h$ : $5 h + n = 25 h + r$
\item Solve for $C$ : $C M + v = - 24 C + K$
\item Solve for $r$ : $10 r + 17 = - 11 r + 2$
\item Solve for $U$ : $3 U -24 = - 12 U + 4$
\item Solve for $W$ : $W Y -18 = 10 W -24$
\item Solve for $v$ : $J + 6 v = - 2 v + 14$
\item Solve for $J$ : $J Q -20 = J p + k$
\item Solve for $p$ : $R p + 13 = p z -17$
\item Solve for $B$ : $B c + x = 2 B + 12$
\item Solve for $a$ : $R + 10 a = N a + h$
\item Solve for $g$ : $K - 8 g = d + g t$
\item Solve for $A$ : $- 2 A + 8 = - 7 A + 19$
\item Solve for $d$ : $8 d + 16 = L + 22 d$
\item Solve for $w$ : $- 10 w + 7 = 10 w + 6$
\item Solve for $m$ : $- 12 m + 19 = V m -25$
\item Solve for $Z$ : $V - 20 Z = Z d + g$
    \end{enumerate}
    \end{multicols}

    \section{Quadratic equations}
    Solve the following quadratic equations.
    \begin{multicols}{2}
    \begin{enumerate}
    \item $17 x^{2} = - 14 x^{2} + 12$
\item $20 x^{2} - 10 x = 5 x^{2} + 5 x$
\item $3 x^{2} + 22 x = 19 x -2$
\item $2 x^{2} - 26 x -19 = 0$
\item $- 17 x^{2} + 13 x = 0$
\item $18 x^{2} + 3 x = - 18 x^{2} + 24 x -12$
\item $2 x^{2} + 15 = 8 x$
\item $- 24 x^{2} = - 17 x + 16$
\item $6 x^{2} - 7 x + 15 = - 12 x^{2}$
\item $- 16 x^{2} = 0$
\item $- 12 x^{2} + 15 x + 14 = - 21 x$
\item $- 15 x^{2} - 9 x = 0$
\item $- 8 x^{2} - 25 x -6 = - 23 x$
\item $7 x^{2} - 24 x -11 = - 7 x$
\item $- 26 x^{2} + 7 x = 20$
\item $- 2 x^{2} + 4 x -20 = 4 x^{2} + 3$
\item $- 26 x^{2} - 24 x = -7$
\item $7 x^{2} + 20 x + 10 = - 23 x^{2}$
\item $14 x^{2} + 20 x + 10 = -18$
\item $24 x^{2} + 23 x = 9$
    \end{enumerate}
    \end{multicols}

    \end{document}