\mychapter{9}{Lesson 9} %181024

\section{Message Authentication Codes and unforgeability}

After having explored the security concerns and challenges of the \ske{} realm, it is time to turn the attention to symmetric authentication schemes, or \mac{} schemes. Recall that a \mac{} scheme is a couple $(\textit{Tag}, \textit{Verify})$, with the purpose of authenticating the message's source. In this chapter, the tagging function will be denoted as $\textit{Tag}_k$, akin to a \prf.

The desirable property that a \mac{} scheme should hold is to prevent any attacker from generating a valid couple $(m^*, \phi^*)$, even after querying a tagging oracle polynomially many times\footnotemark.
\footnotetext{Do note the similarities (and differences) between unforgeability in this setting and \cpa-security in the secrecy setting}
The act of generating a valid couple from scratch is called \emph{forging}, and the aforementioned property is defined as \emph{unforgeability against chosen-message attacks} (or \ufcma, in short); its game diagram is shown in figure \ref{cryptogame:ufcma}. Do note that $m^*$ is stated to be outside the query set $M$, expressing the ``freshness'' of the forged couple\footnotemark. In formal terms:

\footnotetext{Observe how this setup bears a striking resemblance to the \prf{} game. We're onto something...}

\begin{definition}
    A \textsc{mac} scheme $\Pi$ is \textsc{ufcma}-secure iff:
    \[
        \forall \adversary \in \ppt \implies \Pr[\cryptog{ufcma}(\lambda) = 1] \in \negl(\lambda) \qedhere
    \]
\end{definition}

\begin{cryptogame}
    {ufcma}
    {$\cryptog{ufcma}(\lambda)$}
    {ufcma}

    \cseqchallenger{$k \pickUAR \K$}

    \cseqbeginloop
    \send{}{$m \in M$}{}
    \receive{$\phi = \Tag(k, m)$}{$\phi$}{}
    \cseqendloop

    \cseqdelay

    \send{$m^* \notin M$}{$(m^*, \phi^*)$}{\textsc{Output 1 iff} $\Ver(k, m^*, \phi^*) = 1$}

\end{cryptogame}

Having defined a good notion of security in the \mac{} scheme domain, we turn our attention to a somewhat trivial scheme, and find out that it is indeed \ufcma-secure:

\begin{theorem}
    Let $f_k$ be a \prf, and define the following \mac{} scheme:
    \[
        \Pi = (\Tag, \Ver) := (f_k(m), [f_k(m) = \phi])
    \]
    Then $\Pi$ is \ufcma-secure.
\end{theorem}

\begin{proof}
    The usual proof by hybridization to random functions entails. The original game is identical to the \ufcma{} game, where the tagging function is the \prf, whereas the hybrid game will have it replaced with a truly random function, as shown in figure \ref{cryptogame:prfufcmahyb}.

    \begin{cryptogame}
        {prfufcmahyb}
        {$\hybridg{1}(\lambda)$}
        {}

        \cseqchallenger{$R \pickUAR \mathfrak{R}(\lambda, n, l)$}

        \cseqbeginloop
        \send{}{$m_i \in M$}{}
        \receive{$\phi_i = R(m_i)$}{$\phi_i$}{}
        \cseqendloop

        \cseqdelay

        \send{$m^* \notin M$}{$(m^*, \phi^*)$}{\textsc{Output 1 iff} $\Tag(k, m^*) = \phi^*$}

    \end{cryptogame}

    \begin{lemma}
        $\cryptog{ufcma}(\lambda) \compindist \hybridg{1}(\lambda)$
    \end{lemma}

    \begin{proof}
        By assuming there is a distinguisher $\distinguisher^{\ufcma}$ capable of disproving the lemma, it can be used to distinguish the \prf{} itself, as depicted by the reduction in figure \ref{cryptoredux:prfufcma}
    \end{proof}

    % AP190107: Not so pretty in the last message...
    \begin{cryptoredux}
        {prfufcma}
        {Distinguishing a \prf{} by using $\distinguisher^{\ufcma}$}
        {prf}
        {ufcma}[1.94]

        \cseqchallenger{\shortstack[l]{
            $k \pickUAR \binary^\lambda$ \\
            $R \pickUAR \mathfrak{R}(n, l)$ \\
            $b \pickUAR \binary$
        }}

        \cseqbeginloop
        \return{}{$m \in M$}{}
        \send{}{$m$}{}
        \receive{\shortstack[l]{
            $\phi_0 \pickUAR f_k(r)$ \\
            $\phi_1 \pickUAR R(r)$
        }}{$\phi_b$}{}
        \invoke{}{$\phi_b$}{}
        \cseqendloop

        \cseqdelay

        \return{$m^* \notin M$}{$(m^*, \phi^*)$}{}
        \send{}{$m^*$}{}
        \receive{\shortstack[l]{
            $\varphi_0 \pickUAR f_k(r)$ \\
            $\varphi_1 \pickUAR R(r)$
        }}{$\varphi_b$}{}

        \cseqdelay

        \send{$b' = \begin{cases}
            0 &\textsc{iff } \varphi_b = \phi^* \\
            1 &\textsc{else}
        \end{cases}$
        }{$b'$}{\textsc{Output 1 iff} $b' = b$}

    \end{cryptoredux}

    \begin{lemma}
        $\forall \adversary \in \ppt \implies \Pr[\hybridg{1}(\lambda) = 1] \leq 2^{-l}$
    \end{lemma}

    \begin{proof}
        This is true because the attacker has to predict the output $R(m^*)$ on a fresh input $m^*$ to win the game, which can happen at most with probability $2^{-l}$.
    \end{proof}

    Thus, the conclusion is that $\Pi$ is \ufcma-secure.
\end{proof}

% AP190107: Don't want the next section to creep in before the above figure, so I'm breaking the page. THe best solution would be to put some more text into the previous proof...

\section{Domain extension for \mac{} schemes}

The previous scheme works on fixed-length messages; as in the encryption domain, there are techniques for tagging variable-length messages which are \ufcma-secure. However, before showing them, some other apparently secure modes are described here to give some possible insights on how to tackle the problem.

Assume the message $m = (m_1, \dots, m_t) \in \binary^{n \cdot t}$ for some $t \geq 1$. Given the tagging function $\Tag_k \in \binary^n \to \binary^l$, an attempt to tag the whole message may be to:

\begin{itemize}
    \item \textsc{xor} all the message blocks, and then tag: $\phi = \Tag(k, \bigoplus_{i = 1}^{t} m_i)$. This approach opens to some easy forgeries: given an authenticated message $(m, \phi)$, an adversary can create a valid couple $(m', \phi)$, where $m'$ is the original message with two flipped bits in two distinct blocks at the same offset; the resulting \textsc{xor} would be the same.

    \item define the tag to be a $t$-sequence of tags, one for each message block. Again, there is an easy way to forge authenticated messages: an adversary can just flip the position of two arbitrary distinct message blocks along with their relative tags, resulting in a different, yet authentic message.

    \item attempt a variant of the above approach, by adding the block number to the block itself to avoid the previous forging. Yet again, this is not \ufcma-secure: the adversary may just make two queries on two distinct messages, obtain the two tag sequences, and then forge an authenticated message by choosing at each position $i$ whether to pick the message-tag blocks from the first or second query. Possibilities are endless...

\end{itemize}

\subsection{Universal hash functions}

A devised solution which has been proven to be secure relies on the following definition: a function family $H$ which can be used to \emph{shrink} variable length messages and then composed with a \prf{} $f_k$:
\[
   H \in \binary^\lambda \to (\binary^{n \cdot t} \to \binary^n) : s \mapsto h_s
\]
\[
    \Tag((k, s), m) = f_k(h_s(m))
\]

So what are the properties of the such a function family $H \circ F$? The main problem are \emph{collisions}, since for each $m \in \binary^{n \cdot t}$ it should be hard to find $m' \neq m$ such that $h_s(m) = h_s(m')$. But collisions do exist for functions in $H \circ F$, because they map elements from $\binary^{n \cdot t} $ to $\binary^t$, and since the codomain is smaller than the domain, the functions cannot be injective in any way.

To overcome this problem, we can consider two options:
\begin{itemize}
    \item assume collisions are hard to find, even when $s \in \binary^{\lambda}$ is known; in this case we have a family of \textit{collision-resistant hash functions};
    \item let $s$ be secret, and assume collisions are hard to find because it is hard to know how $h_s$ works.
\end{itemize}

\begin{definition}
    A family of hash functions $h_s$ is deemed \emph{$\varepsilon$-universal} iff:
    \[
        \forall x \neq x' \in \binary^{n \cdot t}, S \sim \unifdist(\binary^\lambda) \implies \Pr[h_S(x) = h_S(x')] \leq \varepsilon \qedhere
    \]
\end{definition}

If $\varepsilon = 2^{-n}$, meaning the collision probability is minimized, then the family is also called \emph{perfectly universal}; in the case where $\varepsilon \in \negl(\lambda)$ instead, it is defined as \emph{almost universal} (\textsc{au}). Care should be taken in telling the difference between universality and pairwise independence, which states:
\[
    (h_S(x), h_S(x')) \equiv U_{2n}
\]

\begin{lemma}
    Any pairwise independent hash function is perfectly universal.
\end{lemma}

\begin{proof} The proof is left as exercise.
    \todo{
    % AP190108: Leaving this part as-is
    (should I use $Col$ for solving this? What is the difference and when should I use $Col$ instead of one-shot-probability?)

    \textbf{ASK FOR SOLVING PROPERLY}

    (Thoughts: when I ask \textit{what's the probability that, chosen 2 distinct x-es, their hashes are the same \textbf{on a certain value}?}, maybe I have to use one-shot, because one-shot refers to the prob. that the two inputs collide on a specific value, even if not specified.

    Instead, if I consider \textit{what's the prob. that , chosen 2 distinct x-es, their hashes are the same?}, maybe I have to calculate all the possible collisions, because I want to know if the 2 inputs can collide in general. )
    }
\end{proof}

\begin{theorem}
    Assuming $f_k$ is a \prf{} with $n$-bit domain and $h_s$ is an \textsc{au} hash function family, then $H \circ F$ is a \prf{} on $(n \cdot t)$-bit domain, for $t \geq 1$.
\end{theorem}

\begin{proof}

    This proof too will proceed by hybridizing the original game up to the ideal random one. Consider the three sequences depicted in figures \ref{cryptogame:prfaureal}, \ref{cryptogame:prfauhyb} and \ref{cryptogame:prfaurand}:

    \begin{cryptogame}
        {prfaureal}
        {$Real_{F, \adversary}(\lambda)$}
        {}

        \cseqchallenger{\shortstack[l]{
            $k \pickUAR \binary^\lambda$ \\
            $s \pickUAR \binary^\lambda$
        }}

        \cseqbeginloop
        \send{}{$x$}{}
        \receive{$y = f_k(h_s(x))$}{$y$}{}
        \cseqendloop

    \end{cryptogame}

    \begin{cryptogame}
        {prfauhyb}
        {$\hybridg{}[R](\lambda)$}
        {}

        \cseqchallenger{\shortstack[l]{
            $\overline{R} \pickUAR \mathfrak{R}(n, l)$ \\
            $s \pickUAR \binary^\lambda$
        }}

        \cseqbeginloop
        \send{}{$x$}{}
        \receive{$y = \overline{R}(h_s(x))$}{$y$}{}
        \cseqendloop

    \end{cryptogame}

    \begin{cryptogame}
        {prfaurand}
        {$Rand_{R', \adversary}(\lambda)$}
        {}

        \cseqchallenger{$R \pickUAR \mathfrak{R}(n, l)$}

        \cseqbeginloop
        \send{}{$x$}{}
        \receive{$y = R(x)$}{$y$}{}
        \cseqendloop

    \end{cryptogame}

    \begin{lemma}
        \[
            Real_{F, \adversary}(\lambda) \compindist \hybridg{}[R](\lambda)
        \]
    \end{lemma}

    \begin{proof}
        The proof is left as exercise.
    \end{proof}

    \begin{lemma}
        \[
            \hybridg{}[R](\lambda) \compindist Rand_{R', \adversary}(\lambda)
        \]
    \end{lemma}

    \begin{proof}

        Again, collisions come into play there, but there are multiple cases that complicate things. Given two queries with arguments $x_1, x_2$ returning the same image $y$, the random game can model two scenarios:

        \begin{itemize}
            \item the arguments are equal, but with negligible probability
            \item the arguments are distinct
        \end{itemize}

        while the hybrid can model three of them:

        \begin{itemize}
            \item the arguments are equal, again with negligible probability
            \item the arguments are distinct, \emph{and so are their hashes}
            \item the arguments are distinct, \emph{but not their hashes}
        \end{itemize}

        We want to show that the collision at hash level is negligible: as long as they don't happen, the random function $\overline{R}$ is run over a sequence of distinct points, and behaves just as the random game's function $R$ does. So let \textsc{bad} be the event:
        \[
            \exists i \neq j \in [q] : h_s(x_i) = h_s(x_j)
        \]
        where $q$ denotes the adversary's query count. It suffices to show that $\Pr[\textsc{Bad}] \in \negl(\lambda)$.

        % AP190108: Copied verbatim from my notes, maybe this is something Venturi wrote. Doesn't mean I fully understand it...
        Since we don't care what happens after a collision, we can alternatively consider a mental experiment where we answer all queries at random, and only at the end sample $s \pickUAR \binary^\lambda$ and check for collisions: this does not change the value of $\Pr(\textsc{Bad})$. Now queries are independent of $s$, and this eases our proof:
        \begin{align*}
            \Pr[\textsc{Bad}] &= \Pr[\exists x_i \neq x_j, h_S(x_i) = h_S(x_j)] &                               \\
            &\leq \sum_{i \neq j} \Pr[h_S(x_i) = h_S(x_j)]                      & \text{$h_s$ is \textsc{au})}  \\
            &\leq \binom{q}{2} \negl(\lambda) \in \negl(\lambda)                &
        \end{align*}

        By ruling out this event, the lemma is proven.
    \end{proof}

     So now we have $Real \compindist \hybridg{} \compindist Rand$
\end{proof}

\begin{corollary}
    Let $\Pi = (\Tag, \Ver)$ be a variable length message \mac{} scheme where, given a \prf{} $f_k$ and an \textsc{au} hash function family $h_s$, the tagging function is defined as $f_k(h_s)$. Then $\Pi$ is \ufcma-secure.
\end{corollary}

\subsection{Hash function families from finite fields}

% AP190110: A "finite field" aka galois field, is a field with finite elements. (...)
%       - Finite fields exist only for carrier cardinality of q^k, where q is prime and k is nonnegative
%       - Both operations are done modulo q

A generic $2^n$-order finite field has very useful properties: adding two of its elements is equal to \textsc{xor}-ing their binary representations, while multiplying them is done modulo $2^n$. It is possible to define a hash function family that makes good use of these properties, and is suitable for a \ufcma-secure \mac{} scheme.

\begin{construction}
    Let $\mathbb{F} = GF(2^n)$ be a \textit{finite field} (or ``Galois field'') of $2^n$ elements, and let $m = (m_1, \dots, m_t) \in \mathbb{F}^t$ and $s = (s_1, \dots, s_t) \in \mathbb{F}^t$. The desired hash function family will have this form:
    \[
        h_{s}(m)= \sum_{i = 1}^{t} s_i m_i = \left<s, m\right> = q_m(s)
    \]
\end{construction}

\begin{lemma}
    The above function family $h_s$ is almost universal.
\end{lemma}

\begin{proof}

    In order for $h_s$ to be almost universal, collisions must happen negligibly. Suppose we have a collision with two distinct messages $m$ and $m'$:
    \[
        \sum_{i = 1}^t m_i s_i = \sum_{i = 1}^t m'_i s_i
    \]

    Let $\delta_i = m_i - m'_i$ and assume, without loss of generality, that $\delta \neq 0$. Then, by using the previous equation, when a collision happens:
    \[
        0 = \sum_{i = 1}^t m_i s_i - \sum_{i = 1}^t m'_i s_i = \sum_{i = 1}^t \delta_i s_i
    \]

    Since the messages are different from each other, there is at least some $i$-th block that contains some of the differences. Assume, without loss of generality, that some of the differences are contained in the first block ($i = 1$); the sum can then be split between the first block itself $\delta_1 s_1$ and the rest:

    \begin{align*}
        \sum_{i = 1}^t \delta_i s_i                     &= 0                                                \\
        \delta_1 s_1 + \sum_{i = 2}^{t} \delta_i s_i    &= 0                                                \\
        \delta_ 1 s_1                                   &= -\sum_{i = 2}^{t} \delta_i s_i                   \\
        s_1                                             &= \frac{-\sum_{i = 2}^{t} \delta_i s_i}{\delta_1}  \\
    \end{align*}

    which means when a collision happens, $s_1$ must be exactly equal to the sum of the other blocks, which is another element of $\mathbb{F}$. But since every seed is chosen at random among $\mathbb{F}$, the probability of picking the element $s_1$ satisfying the above equation is just $\left|\mathbb{F}\right|^{-1} = 2^{-n} \in \negl(\lambda)$. By repeating this reasoning for every difference-block, a sum of negligible probabilities is obtained, which is in turn negligible; therefore the hash function family $h_s$ is almost universal.

\end{proof}

% AP190111: Leaving this section as-is; will come back to it at a later time
\subsubsection{$H$ with Galois fields elements and polynomials}

\begin{exercise}
    Let $\mathbb{F} = GF(2^n)$ be a finite field of $2^n$ elements, and let $m = (m_1, \dots, m_t) \in \mathbb{F}^t$ and $s \pickUAR \mathbb{F}^t$. Construct the following hash function family:
    \[
        h_{s}(m) = \sum_{i=1}^{t}s^{i-1}m_{i}
    \]

    Prove that this construction is \textsc{au}.
\end{exercise}

\begin{solution}
    (possible proof: to be almost universal, looking at the definition, collisions with $m \not= m'$ must be negligible.

    So consider a collision as above: it must be true that
    \begin{align*}
        \sum_{i=1}^{t} m_{i}s^{i-1}                                 &= \sum_{i=1}^{t} m'_{i}s^{i-1} \\
        \sum_{i=1}^{t} m_{i}s^{i-1}-\sum_{i=1}^{t} m'_{i}s^{i-1}    &= 0                            \\
        q_{m-m'}(s)                                                 &= 0                            \\
    \end{align*}
    How can we make a polynomial equal to 0? We have to find the \textbf{roots} of the polynomial, which we know are at most the polynomial's rank. The rank is $t - 1$, and the probability of picking a root from $\mathbb{F}$ as seed of $h_s$ is:
    \[
        \Pr[s=root] = \frac{t - 1}{2^{n}} \in \negl(\lambda)
    \]
\end{solution}


\documentclass{article}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{float}

\title{Assignment for CS 696-04 \\ (Deep Learning)}
\author{Diwas Sharma}
\date{\today}
\begin{document}

\maketitle
\newpage

\section{Design}
The code that is used to train and test CIFAR-10 dataset uses the interface provided by
keras \footnote{https://keras.io/}.

\section{CIFAR 10}
\subsection{Network}
The network that is used to classify the CIFAR 10 dataset is follows:

\begin{enumerate}
    \item{Convolution layer}
        \begin{itemize}
            \item{Input: $N * 3 * 32 * 32$ tensor}
            \item{Filters: 4 filters with size $3*3$}
            \item{Padding: 0 padded with pad width of 1}
            \item{Stride: 1 on each dimension}
            \item{Regularizer: $L_2$ Regularizer}
        \end{itemize}
    \item{Max pooling layer}
        \begin{itemize}
            \item{Input: $N * 4 * 32 * 32$ tensor}
            \item{Pool size: $2*2$}
            \item{Stride: 2 on each dimension}
        \end{itemize}
    \item{Convolution layer}
        \begin{itemize}
            \item{Input: $N * 4 * 16 * 16$ tensor}
            \item{Filters: 8 filters with size $3*3$}
            \item{Padding: 0 padded with pad width of 1}
            \item{Stride: 1 on each dimension}
            \item{Regularizer: $L_2$ Regularizer}
        \end{itemize}
    \item{Max pooling layer}
        \begin{itemize}
            \item{Input: $N * 8 * 16 * 16$ tensor}
            \item{Pool size: $2*2$}
            \item{Stride: 2 on each dimension}
        \end{itemize}
    \item{Convolution layer}
        \begin{itemize}
            \item{Input: $N * 8 * 8 * 8$ tensor}
            \item{Filters: 16 filters with size $3*3$}
            \item{Padding: 0 padded with pad width of 1}
            \item{Stride: 1 on each dimension}
            \item{Regularizer: $L_2$ Regularizer}
        \end{itemize}
    \item{Softmax layer}
        \begin{itemize}
            \item{Input: $512 * N$ tensor}
            \item{Units: 10}
        \end{itemize}
\end{enumerate}

\subsubsection{Source Code}
\begin{lstlisting}[language=python]
# Create the model
model = Sequential()

# Create the regularizer
regularizer = l2(0.01)

model.add(
    Conv2D(
        4, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3),
        kernel_regularizer=regularizer
    )
)
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(
    Conv2D(
        16, kernel_size=(3, 3), activation='relu',
        kernel_regularizer=regularizer
    )
)
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(
    Conv2D(
        32, kernel_size=(3, 3), activation='relu',
        kernel_regularizer=regularizer
    )
)
model.add(Flatten())
model.add(Dense(10, activation='softmax'))

# Compile the model
model.compile(loss='categorical_crossentropy',
              optimizer=Adam(lr=0.0001, decay=1e-6),
metrics=['accuracy'])
\end{lstlisting}

\subsection{Training}
The network was trained using ADAM optimizer with mini batch size of 128. And, the
parameters used for the optimizer were $\eta$ = 0.0001, $\beta_1$ = 0.9 and
$\beta_2$ = 0.999 with learning rate decay of $1e-6$.

The figure \ref{fig:training_loss} shows the plot of loss at the end of each epoch
during the training, similarly the figure \ref{fig:training_accuracy} shows the
accuracy at the end of each epoch during the training phase.

\begin{figure}[!ht]
  \includegraphics[width=\textwidth,height=0.35\textheight,keepaspectratio]{cifar10_loss.png}
  \caption{Training loss}
  \label{fig:training_loss}
\end{figure}

\begin{figure}[!ht]
  \includegraphics[width=\textwidth,height=0.35\textheight,keepaspectratio]{cifar10_accuracy.png}
  \caption{Training accuracy}
  \label{fig:training_accuracy}
\end{figure}

\subsection{Testing}
The accuracy obtained on the test data after training for 300 epochs is 0.6074.

\end{document}


\section{The Challenge}
\label{sec::problem}

Integrating additional computations into an existing system can shift into a difficult assignment because of the following conventional restrictions:

\begin{itemize}
    \item we are not permitted to modify a function's implementation
    \item we are not authorised to alter a function's parameters' types
    \item we are not allowed to transform the means our system composes subcomponents
\end{itemize}

Acknowledging all those limitations, one may question why such practices are necessary when mixing new services. In our examples, we limit ourselves by respecting those rules to separate our new behaviours from our legacy code. Extending our system matures into an exercise of writing new code, rather than changing existant instructions.

Furthermore, we evaluate the general case using a mathematical model which represents our legacy context:

\begin{equation}
\begin{cases}
f_1 :: A_1 \rightarrow A_2 \rightarrow \dots \rightarrow A_n \rightarrow X_1 \\
f_2 :: B_1 \rightarrow B_2 \rightarrow \dots \rightarrow B_n \rightarrow X_2 \\
\vdots \\
f_n :: Z_1 \rightarrow Z_2 \rightarrow \dots \rightarrow Z_n \rightarrow X_n \\
h :: X_1 \rightarrow X_2 \rightarrow \dots \rightarrow X_n \rightarrow Y
\end{cases}
\end{equation}

Our model composes $n$ functions $ f_i, i = \overline{1,n} $ using $ h $, each function represents an independent computation; to simplify our model, we can consider those a set of serial processes.

Our model is composed of $n$ various functions $ f_i, i = \overline{1,n} $ and one transformation $h$ which combines all $f_i$, each mapping represents an independent computation; to simplify, we acknowledge this set of processes to be serial and synchronised.

The granted practices permit us to adjust the manner in which we apply our function $(h (f_1 a_1 \dots a_n) \dots (f_n z_1 \dots z_n))$, without altering the conclusive output. The circumstances enable us to consider two options when blending a new operation:

\begin{itemize}
    \item using dynamic detection and automatic evaluation; with an aspect which encapsulates a new process, and pointcuts which detects the target action's location in our implementation scope
    \item using static detection and automatic evaluation; with a monad to encapsulate our computation, and its bind function to evaluate it
\end{itemize}

Those two proposals tackle unique situations; by using aspects, we enlarge our implementation space from a two-dimensional setting, defined by our operation's position (where we formulate our instructions), and its time (when performing our behaviour) to a multidimensional environment, in which each distinct computation defines a dissimilar dimension of implementation.

\begin{figure}
    \centering
    \input{__diagrams/aop_space.tex}
    \caption{Aspect-Oriented Implementation Space}
    \label{fig::dimensions::aop}
\end{figure}

In different circumstances, monads elevate our computation into a different scope, which distinguishable to an extension is not self-supporting from the previously mentioned framework. A monad represents a pattern which uses high-level functions, mathematical functors, and comes as a solution for effect encapsulation.

\begin{figure}
    \centering
    \input{__diagrams/aop_oop_space.tex}
    \caption{Aspect-Oriented Implementation Space in Object-Oriented Environments}
    \label{fig::dimensions::oop}
\end{figure}

Let us examine a reduced variant of our overall intricacy, to explain each strategy using monads and aspects.

\begin{equation}
\begin{cases}
f:: Int \rightarrow String \rightarrow Boolean \\
g :: String \rightarrow Float \rightarrow Double \\
h :: Boolean \rightarrow Double \rightarrow Int
\end{cases}
\end{equation}

Although all the thoughts exhibited in our paper are self-supporting from a programming language or paradigm, we use Kotlin to produce a \textit{runnable} implementation of our clarifications for several reasons:

\begin{itemize}
    \item Kotlin is a multi-paradigm language with supports functional and object-oriented features
    \item Kotlin holds a static and powerful type system
    \item Kotlin empowers us with extensions on generic types for supplementary operations, which is useful to clarify our monad's implementation
\end{itemize}

Computer Science Research Methodologies/R03 MCSI Paper/sections/challenge.tex


\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage[paperheight=5.75in,paperwidth=8.25in,margin=0.5in]{geometry}

\newcommand\x{\times}
\newcommand\bigzero{\makebox(0,0){\text{\huge0}}}
\newcommand*{\bord}{\multicolumn{1}{c|}{}}

\newcommand{\R}{\mathbb{R}}
\newcommand{\E}{\mathbb{E}}
\newcommand{\sumt}{\sum_{t=0}^\infty}
\newcommand{\I}{\mathcal{I}}
\begin{document}
\thispagestyle{empty}
\section{RBC model}
\begin{gather*}
	\max_{K_{t+1},H_t,C_t} \E_0 \sum_{t=0}^\infty \beta^t\bigg[ \frac{C_{t}^{1-\eta}}{1-\eta} - \frac{H_t^{1+\psi}}{1+\psi}   \bigg]
	\\
	\text{Subject to}
	\\
	C_t + K_{t+1} = Z_t K_t^\alpha H_t^{1-\alpha} + (1-\delta)K_t
	\\
	Z_{t+1} = \rho Z_{t} + \sigma \varepsilon_t \sim \mathcal{N}(0,1)
\end{gather*}

\section{RBC model with non negativity constraint}
\begin{gather*}
	\max_{K_{t+1},H_t,C_t} \E_0 \sum_{t=0}^\infty \beta^t\bigg[ \frac{C_{t}^{1-\eta}}{1-\eta} - \frac{H_t^{1+\psi}}{1+\psi}   \bigg]
	\\
	\text{Subject to}
	\\
	C_t + I_t = Z_t K_t^\alpha H_t^{1-\alpha}
	\\
	I_t = K_{t+1} - (1-\delta)K_t \geq 0 \; \Phi \geq 0
	\\
	Z_{t+1} = \rho Z_{t} + \sigma \varepsilon_t \sim \mathcal{N}(0,1)
\end{gather*}

\end{document}


\section{Fourier's Physics Playground}

    \subsection{Maxwell's Electrodynamics}
    \frame{\sectionpage}

    \begin{frame}{In the beggining, God said:}
        \begin{equation*}
            \left\lbrace
            \begin{aligned}
                &\div{\vb{E}} = \frac{\rho}{\e}\\
                &\div{\vb{B}} = 0\\
                &\curl{\vb{E}} = - \pdv{\vb{B}}{t}\\
                &\curl{\vb{B}} = \m\vb{J}
 + \m\e\pdv{\vb{E}}{t}
            \end{aligned}
            \right.
        \end{equation*}

        \uncover<2>{and there was light!}
    \end{frame}

    \begin{frame}{Too hard, let's try something different}
        \begin{equation*}
            \left\lbrace
            \begin{aligned}
                &\vb{E} = - \grad{V} - \pdv{\vb{A}}{t} \\
                &\vb{B} = \curl{\vb{A}}
            \end{aligned}
            \right.
        \end{equation*}
    \end{frame}

    \begin{frame}{Wave Equations}
        \begin{equation*}
            \left\lbrace
            \begin{aligned}
                &\laplacian{V} - \frac{1}{c^2}\pdv[2]{V}{t} = - \frac{\rho}{\e}\\
                &\laplacian{\vb{A}} - \frac{1}{c^2}\pdv[2]{\vb{A}}{t} = - \m\vb{J}
            \end{aligned}
            \right.
        \end{equation*}
    \end{frame}

    \begin{frame}{All Wave Equations In One}
        \begin{equation*}
            \laplacian{\psi(\vb{r},t)} - \frac{1}{c^2}\pdv[2]{\psi}{t} (\vb{r},t) = - g(\vb{r},t)
        \end{equation*}
    \end{frame}

    \begin{frame}{Fourier's Opinion}
        \begin{equation*}
            \widehat{g}(\vb{r},\omega) = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^{+\infty} g(\vb{r},t) e^{-i\omega t} \dd{t}
        \end{equation*}

        \begin{equation*}
            g(\vb{r},t) = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^{+\infty} \widehat{g}(\vb{r},\omega) e^{i\omega t} \dd{\omega}
        \end{equation*}
    \end{frame}

    \begin{frame}{Fourier's Opinion}
        \begin{equation*}
            \widehat{\psi}(\vb{r},\omega) = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^{+\infty} \psi(\vb{r},t) e^{-i\omega t} \dd{t}
        \end{equation*}

        \begin{equation*}
            \psi(\vb{r},t) = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^{+\infty} \widehat{\psi}(\vb{r},\omega) e^{i\omega t} \dd{\omega}
        \end{equation*}
    \end{frame}

    \begin{frame}{Fourier's Opinion}
        \begin{equation*}
            \laplacian{\psi(\vb{r},t)} - \frac{1}{c^2}\pdv[2]{\psi}{t} (\vb{r},t) = - g(\vb{r},t)
        \end{equation*}

        \begin{equation*}
            \laplacian{\widehat{\psi}(\vb{r},\omega)} + \frac{\omega^2}{c^2} \widehat{\psi}(\vb{r},\omega) = - \widehat{g}(\vb{r},\omega)
        \end{equation*}
    \end{frame}

    \begin{frame}{Green Function}
        \uncover<+->{\begin{equation*}
            L \phi(\vb{r}) = - s(\vb{r})
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            L G(\vb{r} - \vb{r'}) = - \dirac{\vb{r} - \vb{r'}}
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            \phi(\vb{r}) = \int G(\vb{r} - \vb{r'}) s(\vb{r'}) \dd{\tau'}
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            L \phi(\vb{r}) = \int L G(\vb{r} - \vb{r'}) s(\vb{r'}) \dd{\tau'} =  - \int \dirac{\vb{r} - \vb{r'}} s(\vb{r'}) \dd{\tau'} = -s(\vb{r})
        \end{equation*}}
    \end{frame}

    \begin{frame}{One At a Time}
        \uncover<+->{\begin{equation*}
            \laplacian{\widehat{\psi}(\vb{r},\omega)} + \frac{\omega^2}{c^2} \widehat{\psi}(\vb{r},\omega) = - \widehat{g}(\vb{r},\omega)
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            \laplacian{G(\vb{r} - \vb{r'})} + \frac{\omega^2}{c^2} G(\vb{r} - \vb{r'}) = - \dirac{\vb{r} - \vb{r'}}
        \end{equation*}}
    \end{frame}

    \begin{frame}{Solution for $\vb{r} - \vb{r'} \neq \vb{0}$}
        \uncover<+->{\begin{equation*}
            \frac{1}{r}\dv[2]{(rG)}{r} + k^2 G = 0
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            G(r) = \frac{A}{r}e^{\pm i k r}
        \end{equation*}}
    \end{frame}

    \begin{frame}{Recovering $0$ Psychological Trauma}
        \uncover<+->{\begin{equation*}
            \laplacian{G(\vb{r} - \vb{r'})} + \frac{\omega^2}{c^2} G(\vb{r} - \vb{r'}) = - \dirac{\vb{r} - \vb{r'}}
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            A \int \laplacian{\frac{1}{r}} \dd{\tau'} + 4\pi A \frac{\omega^2}{c^2} \int \frac{r^2}{r} \dd{r}  = - \int \dirac{\vb{r} - \vb{r'}} \dd{\tau'}
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            - 4 \pi A  = - 1
        \end{equation*}}
    \end{frame}

    \begin{frame}{Back To Our Problem}
        \uncover<+->{\begin{equation*}
            \widehat{\psi}(\vb{r}, \omega) = \int G(\rc)\widehat{g}(\vb{r'}, \omega) \dd{\tau'}
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            G(\rc) = \frac{1}{4\pi \rc}e^{\pm i k \rc}
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            \widehat{\psi}(\vb{r}, \omega) = \frac{1}{4\pi} \int \frac{\widehat{g}(\vb{r'}, \omega) e^{\pm i k \rc}}{\rc} \dd{\tau'}
        \end{equation*}}
    \end{frame}

    \begin{frame}{Actually Solving Our Problem}
        \uncover<+->{\begin{equation*}
            \psi(\vb{r},t) = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^{+\infty} \widehat{\psi}(\vb{r},\omega) e^{i\omega t} \dd{\omega}
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            \psi(\vb{r},t) = \frac{1}{4 \pi \sqrt{2\pi}} \iint \frac{\widehat{g}(\vb{r'}, \omega) e^{i \omega t \pm i \omega \frac{\rc}{c}}}{\rc} \dd{\omega} \dd{\tau'}
        \end{equation*}}
    \end{frame}

    \begin{frame}{Actually Solving Our Problem}
        \uncover<+->{\begin{equation*}
            \psi(\vb{r},t) = \frac{1}{4 \pi \sqrt{2\pi}} \iint \frac{\widehat{g}(\vb{r'}, \omega) e^{i \omega \prnt{t \pm \frac{\rc}{c}}}}{\rc} \dd{\omega} \dd{\tau'}
        \end{equation*}}

        \uncover<+->{\only<-2>{\begin{equation*}
            \psi(\vb{r},t) = \frac{1}{4 \pi} \int \frac{g(\vb{r'}, t \pm \frac{\rc}{c})}{\rc} \dd{\tau'}
        \end{equation*}}

        \only<3>{\begin{equation*}
            \psi(\vb{r},t) = \frac{1}{4 \pi} \int \frac{g(\vb{r'}, t - \frac{\rc}{c})}{\rc} \dd{\tau'}
        \end{equation*}}}
    \end{frame}

    \begin{frame}{Back at Maxwell's}
        \begin{equation*}
            V(\vb{r},t) = \frac{1}{4 \pi \e} \int \frac{\rho(\vb{r'}, t - \frac{\rc}{c})}{\rc} \dd{\tau'}
        \end{equation*}

        \begin{equation*}
            \vb{A}(\vb{r},t) = \frac{\m}{4 \pi} \int \frac{\vb{J}(\vb{r'}, t - \frac{\rc}{c})}{\rc} \dd{\tau'}
        \end{equation*}
    \end{frame}

    \begin{frame}{One Last Step}
        \begin{equation*}
            \left\lbrace
            \begin{aligned}
                &\vb{E} = - \grad{V} - \pdv{\vb{A}}{t} \\
                &\vb{B} = \curl{\vb{A}}
            \end{aligned}
            \right.
        \end{equation*}
    \end{frame}

    \begin{frame}{Jefimenko Equations}
        \begin{equation*}
            \vb{E}(\vb{r},t) = \frac{1}{4\pi\e}\int \frac{\hrc}{\rc^2}\rtd{\rho} + \frac{\hrc}{c\rc} \rtd{\pdv{\rho}{t}} - \frac{1}{c^2\rc} \rtd{\pdv{\vb{J}}{t}} \dd{\tau'}
        \end{equation*}

        \begin{equation*}
            \vb{B}(\vb{r},t) = \frac{\m}{4\pi}\int \prnt{\frac{1}{\rc^2} \rtd{\vb{J}} + \frac{1}{c\rc} \rtd{\pdv{\vb{J}}{t}}} \cp \hrc \dd{\tau'}
        \end{equation*}
    \end{frame}

    \subsection{Heisenberg's Uncertainty Principle}
    \frame{\sectionpage}
    \begin{frame}{Position and Momentum}
        \uncover<+->{\begin{equation*}
            \psi(x) = \bra{x}\ket{\psi} = \int \bra{x}\ket{k} \bra{k}\ket{\psi} \dd{k} = \frac{1}{\sqrt{2\pi}} \int e^{ikx} \psi(k) \dd{k}
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            \psi(k) = \bra{k}\ket{\psi} = \int \bra{k}\ket{x} \bra{x}\ket{\psi} \dd{x} = \frac{1}{\sqrt{2\pi}} \int e^{-ikx} \psi(x) \dd{x}
        \end{equation*}}
    \end{frame}

    \begin{frame}{Position and Momentum (but weirder)}
        \uncover<+->{\begin{equation*}
            \left\lbrace
            \begin{aligned}
                &X\ket{\psi} = x \psi(x) \\
                &K \ket{\psi} = - i \pdv{\psi}{x} (x)
            \end{aligned}
            \right.
        \end{equation*}}

        \uncover<+->{\begin{equation*}
            \left\lbrace
            \begin{aligned}
                &X\ket{\psi} = - i \pdv{\psi}{k} (k) \\
                &K \ket{\psi} = k \psi(k)
            \end{aligned}
            \right.
        \end{equation*}}
    \end{frame}

    \begin{frame}{Fourier Diplomacy}
        \uncover<+->{\begin{equation*}
            \ket{x} \xleftrightarrow[\mathcal{F}^{-1}]{\mathcal{F}} \ket{k}
        \end{equation*}}
    \end{frame}

    \begin{frame}{Fourier Uncertainty}
        \begin{enumerate}
            \item<+-> $\psi(x):$ what is $x$?
            \item<+-> $\psi(k):$ what is $k$?
        \end{enumerate}
    \end{frame}

    \begin{frame}{Definite Position}
        \begin{equation*}
            \psi(x) = \sqrt{\frac{\pi}{2}}\prnt{\dirac{x-x_0} + \dirac{x+x_0}}
        \end{equation*}

        \centering
        \includegraphics[height = 0.65 \textheight]{images/Pulse3-Fourier.pdf}
    \end{frame}

    \begin{frame}{Undefinite Momentum}
        \begin{equation*}
            \psi(k) = \cos(x_0 k)
        \end{equation*}

        \centering
        \includegraphics[height = 0.65 \textheight]{images/Pulse3.pdf}
    \end{frame}

    \begin{frame}{Uncertainty Relation}
        \begin{block}{\centering$\sigma_x\sigma_p \geq \frac{\hbar}{2}$}
            The uncertainty relation is a consequence of the general fact that anything narrow in one space is wide in the transform space and vice versa. So if you are a 45 kg weakling and are taunted by a 270 kg bully, just ask him to step into momentum space!

            \alert{Ramamurti Shankar}
        \end{block}
    \end{frame}

200+ beamer 模板合集/DeadPhysicistsSocietyPresentationTemplate(DPS 研讨会)/chapters/fourier-playground.tex


\documentclass[10pt]{report}

\usepackage{subcaption} % for subfigures
\usepackage{amsthm} % for QED
%\usepackage{algpseudocode} % for pseudo-code
\usepackage{mathtools} % for delimiter

\usepackage{listings} % for code
\lstset{
	language=R,
	basicstyle=\ttfamily,
	numbers=none,
	stepnumber=1,
	numbersep=8pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	frame=single,
	tabsize=2,
	captionpos=t,
	breaklines=true,
	breakatwhitespace=false
}

\usepackage{float} % for figure [H]
\usepackage{booktabs} % for tabular
\usepackage{caption} % for \caption*
\usepackage[export]{adjustbox} % for valign=t
\usepackage{array} % for column type m
\usepackage{verbatim}
\usepackage{graphicx}
%\graphicspath{ {imgs/} }
\usepackage{fancyhdr}
\usepackage{amssymb}
\usepackage{amsmath}

%%%%%% Pagination
\setlength{\topmargin}{-.3 in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\textheight}{9.in}
\setlength{\textwidth}{6.5in}

%Title page
\newcommand{\hwTitle}{Homework \#1}
\newcommand{\hwCourse}{Applied Statistics/Regression}
\newcommand{\hmwkClassInstructor}{Professor Lulu Kang}

\title{
	\vspace{2in}
	\textmd{\textbf{\hwCourse\\\hwTitle}}\\
	\vspace{0.3in}\large{\textit{\hmwkClassInstructor}}
	\vspace{3in}
}

%\title{Homework 1}
\author{\textbf{Zhihao Ai}}
\date{}

%Header setting.
\pagestyle{fancy}
\fancyhead[L]{Zhihao Ai}
\fancyhead[C]{Math 484}
\fancyhead[R]{Homework 1}
%%%%%%

%Global setting.
%\everymath{\displaystyle}
\setlength\parindent{0pt}

%Custom general commands.
\newcommand{\ds}{\displaystyle}
\newcommand{\ts}{\textstyle}
\newcommand{\f}[1] {f\left(#1\right)}
\newcommand{\eva}[2] {\left. #1 \right|_{#2}}
\newcommand{\dintt}[4] {\int_{#1}^{#2} #3 d#4}

\newcolumntype{N}{ >$ c <$}
\newcolumntype{M}[1]{>{\centering\arraybackslash $}m{#1}<{$}}

\newcommand{\abs}[1] {\left| #1 \right|}

\DeclarePairedDelimiter\autoparen{(}{)}
\newcommand{\pa}[1]{\autoparen*{#1}}
\DeclarePairedDelimiter\autodvert{\Vert}{\Vert}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}
\newcommand{\norm}[1]{\autodvert*{#1}}

\newcommand{\var}{\text{var}}

\begin{document}

\maketitle

\subsection*{Ex 1.27}
\begin{enumerate}
	\item [a.]
	The estimated regression function is $\hat{Y} = -1.19x + 156.35$.
	\lstinputlisting{27a.txt}
	The plot of the function and the data is shown below:
	\begin{figure}[H]
		\centering
		\includegraphics[width=.6\linewidth]{27a.png}
	\end{figure}
	This function appears to give a good fit and the plot supports the anticipation.

	\item [b.]
	\begin{enumerate}
		\item [(1)]
		A point estimate of the difference in the mean muscle mass for women differing in age by one year is $\hat{\beta}_1 = -1.19$.

		\item [(2)]
		A point estimate of the mean muscle mass for women aged $X=60$ years is $-1.19(60) + 156.35 = 84.95$.

		\item [(3)]
		The value of the residual for the eighth case, where $X=41$ and $Y=112$ is $e_8 = Y_8 - \hat{Y}_8 = 112 - [-1.19(41) + 156.35] = 4.44$.

		\item [(4)]
		A point estimate of $\sigma^2$ is $\hat{\sigma}^2 = \frac{RSS}{n-2} = \frac{3874.4}{58} = 66.8$.
		\lstinputlisting{27b4.txt}
	\end{enumerate}
\end{enumerate}

\subsection*{Ex 1.28}
\begin{enumerate}
	\item [a.]
	The estimated regression function is $\hat{Y} = -170.6x + 20517.6$.
	\lstinputlisting{28a.txt}
	The plot of the function and the data is shown below:
	\begin{figure}[H]
		\centering
		\includegraphics[width=.6\linewidth]{28a.png}
	\end{figure}
	This function generally gives not a very good but acceptable fit since the correlation between $X$ and $Y$ here is not that strong.

	\item [b.]
	\begin{enumerate}
		\item [(1)]
		A point estimate of the difference in the mean crime rate for two countries whose high-school graduation rates differ by one percentage point is $\hat{\beta}_1 = -170.6$.

		\item [(2)]
		A point estimate of the mean crime rate last year in countries with high school graduation percentage $X=80$ is $-170.6(80) + 20517.6 = 6869.6$.

		\item [(3)]
		A point estimate of $\epsilon_{10}$, where $X=82$ and $Y=7932$ is $e_{10} = Y_{10} - \hat{Y}_{10} = 7932 - [-170.6(82) + 20517.6] = 1403.6$.

		\item [(4)]
		A point estimate of $\sigma^2$ is $\hat{\sigma}^2 = \frac{RSS}{n-2} = \frac{455273165}{82} = 5552112$.
		\lstinputlisting{28b4.txt}
	\end{enumerate}
\end{enumerate}

\subsection*{Ex 1.39}
\begin{enumerate}
	\item [a.]
	Let $Y_{i,1}$ and $Y_{i,2}$ be the observations on $Y$ whose mean is $\bar{Y}_i, i=1,2,3$. For the six points,
	\begin{align*}
		\hat{\beta}_1^*
		&= \frac{S_{xy}^*}{S_{xx}^*}\\
		&= \frac{\sum_{i=1}^{3} (x_i - \bar{x})(y_{i,1}-\bar{y}) + (x_i - \bar{x})(y_{i,2}-\bar{y}) }{\sum_{i=1}^{3} (x_i - \bar{x}) + (x_i - \bar{x})}\\
		&= \frac{\sum_{i=1}^{3} 2(x_i - \bar{x})(\frac{y_{i,1} + y_{i,2}}{2}-\bar{y}) }{2\sum_{i=1}^{3}(x_i - \bar{x})}\\
		&= \frac{\sum_{i=1}^{3} (x_i - \bar{x})(\bar{Y}_i-\bar{y})}{\sum_{i=1}^{3}(x_i - \bar{x})}\\
		&= \frac{S_{xy}}{S_{xx}}
	\end{align*}
	where $S_{xy}$ and $S_{xx}$ are for three points. So, $\hat{\beta}_1$ is the same for three-point case and six-point case. Since the mean values $\bar{x}$ and $\bar{y}$ are both the same in two cases, $\hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x}$ is also the same. Therefore, the least squares regression lines are identical.

	\item [b.]
	Since $RSS = S_{yy} - \frac{S_{xy}^2}{S_{xx}}$ and $\hat{\sigma}^2 = \frac{RSS}{n-2}$, we can directly calculate the estimate of $\sigma^2$ without fitting a regression line.
\end{enumerate}

\subsection*{Ex 1.42}
\begin{enumerate}
	\item [a.]
	\[
	L(\beta_1) = \prod_{i=1}^{n} f(y_i | \beta_1, \sigma^2) = \pa{\frac{1}{4\sqrt{2\pi}}}^6 \exp\pa{-\frac{1}{32} \sum_{i=1}^{n} (y_i - \beta_1 x_i)^2}
	\]

	\item [b.]
	$L(17) = 9.45133\times 10^{-30}, L(18) = 2.64904\times 10^{-7}, L(19) = 3.04729\times 10^{-37}$. The likelihood function is largest at $\beta_1 = 18$.

	\item [c.]
	$b_1 = \sum_{i=1}^{6} X_i Y_i / \sum_{i=1}^{6} X_i^2 = 17.9285$. The result in part (b) is close to the estimate.

	\item [d.]
	The plot for the likelihood function is shown below.
	\begin{figure}[H]
		\centering
		\includegraphics[width=.5\linewidth]{42d.png}
	\end{figure}
	The point where the likelihood function is maximized corresponds to what's found in part (c).
\end{enumerate}

{\large\bf Show that $\ds \sum_{i=1}^{n} e_i x_i = 0$ and $\ds \sum_{i=1}^{n} e_i = 0$.}
\begin{align*}
	\sum_{i=1}^{n} e_i x_i
	&= \sum_{i=1}^{n} x_i[y_i - (\bar{y} - \hat{\beta}_1 \bar{x}) - \hat{\beta}_1 x_i]\\
	&= \sum_{i=1}^{n} x_i y_i - \bar{y}\sum_{i=1}^{n}x_i + \hat{\beta}_1 \bar{x} \sum_{i=1}^{n} x_i - \hat{\beta}_1 \sum_{i=1}^{n} x_i^2\\
	&= \sum_{i=1}^{n} x_i y_i - n\bar{x}\bar{y} + \frac{\sum_{i=1}^{n} x_i y_i - n\bar{x}\bar{y}}{\sum_{i=1}^{n} x_i^2 - n\bar{x}^2}\pa{n\bar{x}^2 + \sum_{i=1}^{n} x_i^2}\\
	&= \sum_{i=1}^{n} x_i y_i - n\bar{x}\bar{y} - \pa{\sum_{i=1}^{n} x_i y_i - n\bar{x}\bar{y}}\\
	&= 0\\
	\sum_{i=1}^{n} e_i
	&= \sum_{i=1}^{n} [y_i - (\bar{y} - \hat{\beta}_1 \bar{x}) - \hat{\beta}_1 x_i]\\
	&= \sum_{i=1}^{n} y_i - n\bar{y} + \hat{\beta}_1 n\bar{x} - \hat{\beta}_1 \sum_{i=1}^{n} x_i\\
	&= n\bar{y} - n\bar{y} + \hat{\beta}_1 n\bar{x} - \hat{\beta}_1 n\bar{x}\\
	&= 0
\end{align*}

{\large\bf Show that $\hat{\beta}_1 \sim N(\beta_1, \sigma^2/\sum_{i=1}^{n} (x_i - \bar{x})^2)$.}
\[
\hat{\beta}_1 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{S_{xx}} = \frac{\sum_{i=1}^{n} (x_i - \bar{x}y_i)}{S_{xx}} - \frac{\bar{y}\sum_{i=1}^{n} (x_i - \bar{x})}{S_{xx}} = \frac{\sum_{i=1}^{n} (x_i-\bar{x})y_i}{S_{xx}}
\]
Let $k_i = \frac{x_i - \bar{x}}{S_{xx}}, i=1,\dots,n$, then $\hat{\beta}_1 = \sum_{i=1}^{n} k_i y_i$. Since $k_i$ possess the properties:
\begin{align*}
	\sum_{i=1}^{n} k_i &= 0\\
	\sum_{i=1}^{n} k_i x_i &= 1\\
	\sum_{i=1}^{n} k_i^2 &= \frac{1}{S_{xx}}
\end{align*}
we can derive that
\begin{align*}
	E(\hat{\beta}_1) &= E\pa{\sum_{i=1}^{n} k_i Y_i} = \sum_{i=1}^{n} k_i E(Y_i) = \sum_{i=1}^{n} k_i (\beta_0 + \beta_1 x_i) = \beta_0 \sum_{i=1}^{n} k_i + \beta_1 \sum_{i=1}^{n} k_i x_i = \beta_1\\
	\var(\hat{\beta}_1) &= \var\pa{\sum_{i=1}^{n} k_i Y_i} = \sum_{i=1}^{n} k_i^2 \var(Y_i) = \sum_{i=1}^{n} k_i^2\sigma^2 = \frac{\sigma^2}{S_{xx}}
\end{align*}
Since $Y_i$'s are assumed to be normally distributed with mean $\mu_i$ and variance $\sigma_i^2$ , the moment-generating function is given by
\[
m_{Y_i}(t) = \exp\pa{\mu_i t + \frac{\sigma_i^2 t^2}{2}}
\]
Then
\begin{align*}
	m_{\hat{\beta}_1}(t)
	&= \prod_{i=1}^n m_{k_i Y_i}(t)\\ \ds
	&= \prod_{i=1}^n \exp\pa{\mu_i k_i t + \frac{k_i^2 \sigma_i^2 t^2}{2}}\\
	&= \exp\pa{t\sum_{i=1}^{n} k_i \mu_i + \frac{t^2}{2} \sum_{i=1}^{n} k_i^2 \sigma_i^2}
\end{align*}
Therefore, $\hat{\beta}_1$ is also normally distributed. Thus,
\[
\hat{\beta}_1 \sim N\pa{\beta_1, \sigma^2/\sum_{i=1}^{n} (x_i - \bar{x})^2}
\]

\end{document}


\documentclass[pdftex,10pt]{book}
\RequirePackage[hyperindex,colorlinks,plainpages=false]{hyperref}
\hypersetup{pdfauthor={Heng Li},linkcolor=blue,citecolor=blue,urlcolor=blue}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{caption}
\renewcommand{\captionfont}{\fontsize{9pt}{10pt}\selectfont}

\addtolength{\textwidth}{2cm}\addtolength{\hoffset}{-1cm}\addtolength{\textheight}{2cm}\addtolength{\voffset}{-1cm}

\DeclareMathOperator*{\argmax}{argmax}

\makeindex

\title{Mathematical Notes on SAMtools Algorithms}
\author{Heng Li}

\begin{document}

\maketitle

\chapter{Duplicate Rate}

\section{Amplicon duplicates}

Let $N$ be the number of distinct segments (or seeds) before the
amplification and $M$ be the total number of amplicons in the
library. For seed $i$ ($i=1,\ldots,N$), let $k_i$ be the number of
amplicons in the library and $k_i$ is drawn from Poinsson distribution
${\rm Po}(\lambda)$. When $N$ is sufficiently large, we have:
\[
M=\sum_{i=1}^Nk_i=N\sum_{k=0}^{\infty}kp_k=N\lambda
\]
where $p_k=e^{-\lambda}\lambda^k/{k!}$.

At the sequencing step, we sample $m$ amplicons from the library. On the
condition that:
\begin{equation}
  m\ll M
\end{equation}
we can regard this procedure as sampling with replacement. For seed $i$,
let:
\begin{equation*}
  X_i=\left\{\begin{array}{ll}
      1 & \mbox{seed $i$ has been sampled at least once} \\
      0 & \mbox{otherwise}
    \end{array}
  \right.
\end{equation*}
and then:
\begin{equation*}
  {\rm E} X_i= \Pr\{X_i=1\}=1-\Big(1-\frac{k_i}{M}\Big)^m\simeq 1-e^{-k_i m/M}
\end{equation*}
Let:
\[Z=\sum_{i=1}^NX_i\]
be the number of seeds sampled from the
library. The fraction of duplicates $d$ is:
\begin{eqnarray*}
  d&=&1-\frac{{\rm E}(Z)}{m}\\
  &\simeq&1-\frac{N}{m}\sum_{k=0}^{\infty}\big(1-e^{-km/M}\big)p_k\\
  &=&1-\frac{N}{m}+\frac{N e^{-\lambda}}{m}\sum_k \frac{1}{k!}\big(\lambda e^{-m/M}\big)^k\\
  &\simeq& 1-\frac{N}{m}\Big[1-e^{-\lambda}\cdot e^{\lambda(1-m/M)}\Big]
\end{eqnarray*}
i.e.
\begin{equation}
  d \simeq 1 - \frac{N}{m}\Big(1-e^{-m/N}\Big)
\end{equation}
irrelevant of $\lambda$. In addition, when $m/N$ is sufficiently small:
\begin{equation}\label{equ:d2}
  d\approx \frac{m}{2N}
\end{equation}

This deduction assumes that i) $k_i\ll M$ which should almost always
stand; ii) $m\ll M$ which should largely stand because otherwise the
fraction of duplicates will far more than half given $\lambda\sim 1000$
and iii) $k_i$ is drawn from a Poisson distribution.

The basic message is that to reduce PCR duplicates, we should either
increase the original pool of distinct molecules before amplification or
reduce the number of reads sequenced from the library. Reducing PCR
cycles, however, plays little role.

\section{Alignment duplicates}

For simplicity, we assume a read is as short as a single base pair. For
$m$ read pairs, define an indicator function:
\begin{equation*}
  Y_{ij}=\left\{\begin{array}{ll}
      1 & \mbox{if at least one read pair is mapped to $(i,j)$} \\
      0 & \mbox{otherwise}
    \end{array}\right.
\end{equation*}
Let $\{p_k\}$ be the distribution of insert size. Then:
\begin{equation*}
  {\rm E}Y_{ij}=\Pr\{Y_{ij}=1\}=1-\Big[1-\frac{p_{j-i}}{L-(j-i)}\Big]^m\simeq 1-e^{-p_{j-i}\cdot m/[L-(j-i)]}
\end{equation*}
where $L$ is the length of the reference. The fraction of random
coincidence is:
\begin{eqnarray*}
  d'&=&1-\frac{1}{m}\sum_{i=1}^L\sum_{j=i}^L{\rm E}Y_{ij}\\
  &\simeq&1-\frac{1}{m}\sum_{i=1}^L\sum_{j=i}^L\big(1-e^{-p_{j-i}\cdot m/(L-(j-i))}\big)\\
  &=&1-\frac{1}{m}\sum_{k=0}^{L-1}(L-k)\big[1-e^{-p_k m/(L-k)}\big]
\end{eqnarray*}
On the condition that $L$ is sufficient large and:
\begin{equation}
  m\ll L
\end{equation}
\begin{equation}\label{equ:dd}
  d'\simeq\frac{m}{2}\sum_{k=0}^{L-1}\frac{p_k^2}{L-k}
\end{equation}

We can calculate/approximate Equation~\ref{equ:dd} for two types of
distributions. Firstly, if $p_k$ is evenly distributed between
$[k_0,k_0+k_1]$, $d'\simeq\frac{m}{2k_1L}$. Secondly, assume $k$ is
drawn from $N(\mu,\sigma)$ with $\sigma\gg 1$:
\begin{equation*}
  p_k=\frac{1}{\sqrt{2\pi}\sigma}\int_k^{k+1}e^{-\frac{(x-\mu)^2}{2\sigma^2}}\,dx
  \simeq \frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{(k-\mu)^2}{2\sigma^2}}
\end{equation*}
If $p_0\ll 1$, $\mu\ll L$ and $L\gg 1$:
\begin{eqnarray*}
  d'&\simeq&\frac{m}{4\pi\sigma^2}\int_0^1\frac{1}{1-x}\cdot e^{-\frac{(Lx-\mu)^2}{\sigma^2}}\,dx\\
  &\simeq&\frac{m}{4\pi\sigma^2}\int_{-\infty}^{\infty}e^{-\frac{(x-\mu/L)^2}{(\sigma/L)^2}}\,dx\\
  &=&\frac{m}{4\pi\sigma^2}\cdot\frac{\sqrt{2\pi}\cdot \sqrt{2}\sigma}{L}\\
  &=&\frac{m}{2\sqrt{\pi}\sigma L}
\end{eqnarray*}

\chapter{Base Alignment Quality (BAQ)}

Let the reference sequence be $x=r_1\ldots r_L$. We can use a profile
HMM to simulate how a read $y={\tt\char94}c_1\ldots c_l{\tt\char36}$
with quality $z=q_1\ldots q_l$ is generated (or sequenced) from the
reference, where ${\tt\char94}$ stands for the start of the read
sequence and ${\tt\char36}$ for the end.

\begin{figure}[!hb]
\includegraphics[width=\textwidth]{ahmm}
\caption{A profile HMM for generating sequence reads from a reference
  sequence, where $L$ is the length of the reference sequence, $M$
  states stand for alignment matches, $I$ for alignment insertions to
  the reference and $D$ states for deletions.}\label{fig:ahmm}
\end{figure}

The topology of the profile HMM is given in Fig~\ref{fig:ahmm}. Let
$(M,I,D,S)=(0,1,2,3)$. The transition matrix between different types of
states is
$$
{\bf A}=(a_{ij})_{4\times4}=\left(\begin{array}{cccc}
(1-2\alpha)(1-s) & \alpha(1-s) & \alpha(1-s) & s\\
(1-\beta)(1-s) & \beta(1-s) & 0 & s \\
1-\beta & 0 & \beta & 0 \\
(1-\alpha)/L & \alpha/L & 0 & 0 \\
\end{array}\right)
$$
where $\alpha$ is the gap open probability, $\beta$ is the gap extension
probability and $s=1/(2l)$ with $l$ being the average length of a
read. As to emission probabilities, $P(c_i|D_k)=1$,
$P({\tt\char94}|S)=P({\tt\char36}|S)=1$, $P(c_i|I_k)=0.25$ and
$$
P(b_i|M_k)=e_{ki}=\left\{\begin{array}{ll}
1-10^{-q_i/10} & \mbox{if $r_k=b_i$} \\
10^{-q_i/10}/3 & \mbox{otherwise}
\end{array}\right.
$$
The forward-backward algorithm\footnote{We may adopt a banded
  forward-backward approximation to reduce the time complexity. We may
  also normalize $f_{\tilde{k}}(i)$ for each $i$ to avoid floating point
  underflow.} is as follows:
\begin{eqnarray*}
f_S(0)&=&1\\
f_{M_k}(1)&=&e_{k1}\cdot a_{30}\\
f_{I_k}(1)&=&0.25\cdot a_{31}\\
f_{M_k}(i)&=&e_{ki}\cdot\Big[a_{00}f_{M_{k-1}}(i-1)+a_{10}f_{I_{k-1}}(i-1)+a_{20}f_{D_{k-1}}(i-1)\Big]\\
f_{I_k}(i)&=&0.25\cdot\Big[a_{01}f_{M_k}(i-1)+a_{11}f_{I_k}(i-1)\Big]\\
f_{D_k}(i)&=&a_{02}f_{M_{k-1}}(i)+a_{22}f_{D_{k-1}}(i)\\
f_S(l+1)&=&\sum_{k=1}^La_{03}f_{M_k}(l)+a_{13}f_{I_k}(l)
\end{eqnarray*}
\begin{eqnarray*}
b_S(l+1)&=&1\\
b_{M_k}(l)&=&a_{03}\\
b_{I_k}(l)&=&a_{13}\\
b_{M_k}(i)&=&e_{k+1,i+1}a_{00}b_{M_{k+1}}(i+1)+a_{01}b_{I_k}(i+1)/4+a_{02}b_{D_{k+1}}(i)\\
b_{I_k}(i)&=&e_{k+1,i+1}a_{10}b_{M_{k+1}}(i+1)+a_{11}b_{I_k}(i+1)/4\\
b_{D_k}(i)&=&(1-\delta_{i1})\cdot\Big[e_{k+1,i+1}a_{20}b_{M_{k+1}}(i+1)+a_{22}b_{D_{k+1}}(i)\Big]\\
b_S(0)&=&\sum_{k=1}^Le_{k1}a_{30}b_{M_k}(1)+a_{31}b_{I_k}(1)/4
\end{eqnarray*}
and the likelihood of data is $P(y)=f_S(L+1)=b_S(0)$\footnote{Evaluating
  if $f_S(L+1)=b_S(0)$ helps to check the correctness of the formulae
  and the implementation.}. The posterior probability of a read base
$c_i$ being matching state $\tilde{k}$ (M- or I-typed) is
$f_{\tilde{k}}(i)b_{\tilde{k}}(i)/P(y)$.

\chapter{Modeling Sequencing Errors}

\section{The revised MAQ model}
\subsection{General formulae}
Firstly it is easy to prove that for any $0\le\beta_{nk}<1$ ($0\le k\le n$),
$$
\sum_{k=0}^n(1-\beta_{nk})\prod_{l=0}^{k-1}\beta_{nl}=1-\prod_{k=0}^n\beta_{nk}
$$
where we regard that $\prod_{i=0}^{-1}\beta_{ni}=1$. In particular, when
$\exists k\in[0,n]$ satisfies $\beta_{nk}=0$, we have:
\[\sum^n_{k=0}(1-\beta_{nk})\prod_{i=0}^{k-1}\beta_{ni}=1\]
If we further define:
\begin{equation}\label{equ:alpha-nk}
  \alpha_{nk}=(1-\beta_{nk})\prod_{i=0}^{k-1}\beta_{ni}
\end{equation}
on the condition that some $\beta_{nk}=0$, we have:
\[\sum_{k=0}^n\alpha_{nk}=1\]
\begin{equation*}
\beta_{nk}=1-\frac{\alpha_{nk}}{1-\sum_{i=0}^{k-1}\alpha_{ni}}
=\frac{1-\sum_{i=0}^k\alpha_{ni}}{1-\sum_{i=0}^{k-1}\alpha_{ni}}
=\frac{\sum_{i=k+1}^n\alpha_{ni}}{\sum_{i=k}^n\alpha_{ni}}
\end{equation*}
In the context of error modeling, if we define:
\[
\beta_{nk}\triangleq\left\{\begin{array}{ll}
    \Pr\{\mbox{at least $k+1$ errors}|\mbox{at least $k$ errors out of $n$ bases}\} & (k>0) \\
    \Pr\{\mbox{at least $1$ error out of $n$ bases}\} & (k=0)
  \end{array}\right.
\]
we have $\beta_{nn}=0$, and
$$
\gamma_{nk}\triangleq\prod_{l=0}^{k-1}\beta_{nl}=\Pr\{\mbox{at least $k$ errors out of $n$ bases}\}
$$
then
$$
\alpha_{nk}=(1-\beta_{nk})\gamma_{nk}=\Pr\{\mbox{exactly $k$ errors in $n$ bases}\}
$$

\subsection{Modeling sequencing errors}
Given a uniform error rate $\epsilon$ and independent errors, let
$$
\bar{\alpha}_{nk}(\epsilon)=\binom{n}{k}\epsilon^k(1-\epsilon)^{n-k}
$$
and
$$
\bar{\beta}_{nk}(\epsilon)=\frac{\sum_{i=k+1}^n\bar{\alpha}_{ni}(\epsilon)}{\sum_{i=k}^n\bar{\alpha}_{ni}(\epsilon)}
$$
we can calculate that the probability of seeing at least $k$ errors is
$$
\bar{\gamma}_{nk}(\epsilon)=\prod_{l=0}^{k-1}\bar{\beta}_{nk}(\epsilon)
$$
When errors are dependent, the true $\beta_{nk}$ will be larger than
$\bar{\beta}_{nk}$. A possible choice of modeling this is to let
$$
\beta_{nk}=\bar{\beta}_{nk}^{f_k}
$$
where $0<f_k\le1$ models the dependency for $k$-th error. The
probability of seeing at least $k$ errors is thus
$$
\gamma_{nk}(\epsilon)=\prod_{l=0}^{k-1}\bar{\beta}^{f_l}_{nl}(\epsilon)
$$
For non-uniform errors $\epsilon_1\le\epsilon_2\le\cdots\le\epsilon_n$,
we may approximate $\gamma_{nk}(\vec{\epsilon})$ as
$$
\gamma_{nk}(\vec{\epsilon})=\prod_{l=0}^{k-1}\bar{\beta}^{f_l}_{nl}(\epsilon_{l+1})
$$

\subsection{Practical calculation}
We consider diploid samples only. Let $g\in\{0,1,2\}$ be the number of
reference alleles. Suppose there are $k$ reference alleles whose base
error rates are $\epsilon_1\le\cdots\le\epsilon_k$, and there are $n-k$
alternate alleles whose base error rates are
$\epsilon'_1\le\cdots\le\epsilon'_{n-k}$. We calculate
$$
P(D|0)=\gamma_{nk}(\vec\epsilon)=\prod_{l=0}^{k-1}\bar\beta_{nl}^{f_l}(\epsilon_{l+1})
$$
$$
P(D|2)=\gamma_{nk}(\vec\epsilon')=\prod_{l=0}^{n-k-1}\bar\beta_{nl}^{f_l}(\epsilon'_{l+1})
$$
and
$$
P(D|1)=\frac{1}{2^n}\binom{n}{k}
$$
where $f_l=0.97\eta^{\kappa_l-1}+0.03$ with $\kappa_l$ being the rank of base $l$
among the same type of bases on the same strand, ordered by error
rate. For sequencing data, error rates are usually discretized. We may
precompute $\bar\beta_{nk}(\epsilon)$ for sufficiently large
$n$\footnote{SAMtools precomputes a table for $n\le255$. Given higher
  coverage, it randomly samples $255$ reads.} and all possible
discretized $\epsilon$. Calculating the likelihood of the data is
trivial.

\subsection{The original MAQ model}
The original MAQ models the likelihood of data by
$$
\alpha_{nk}(\epsilon)=(1-\bar\beta^{f_k}_{nk})\prod_{i=0}^{k-1}\bar\beta^{f_i}_{ni}
$$
instead of $\gamma_{nk}(\epsilon)$. For non-uniform errors,
$$
\alpha_{nk}(\vec{\epsilon})=c_{nk}(\bar\epsilon)\cdot\prod_{i=0}^{k-1}\epsilon_{i+1}^{f_i}
$$
where
$$
\log{\bar\epsilon}=\frac{\sum_{i=0}^{k-1}f_i\log\epsilon_{i+1}}{\sum_{i=0}^{k-1}f_i}
$$
and
$$
c_{nk}(\bar\epsilon)\triangleq
\Big[1-\bar\beta^{f_k}_{nk}(\bar\epsilon)\Big]
\prod_{i=0}^{k-1}\Bigg[\frac{\bar\beta_{ni}(\bar\epsilon)}{\bar\epsilon}\Bigg]^{f_i}
$$

The major problem with the original MAQ model is that for $\epsilon$
close to 0.5 and large $n$, the chance of seeing no errors may be so
small that it is even smaller than the chance of seeing all errors
(i.e. $\alpha_{n0}<\alpha_{nn}$). In this case, the model prefers seeing
all errors, which is counterintuitive. The revised model uses the
accumulative probability $\gamma_{nk}$ and does not have this problem.
For small $\epsilon$ and $n$, the original and the revised MAQ models
seem to have similar performance.

\chapter{Modeling Multiple Individuals}

\section{Notations}
Suppose there are $N$ sites from $n$ individuals with $i$-th individual
having $m_i$ ploids. Let $M=\sum_im_i$ be the total number of
chromosomes. Let ${\bf D}=(\vec{D}_1,\ldots,\vec{D}_N)^{\rm T}$ be the
data matrix with vector ${\vec D}_a=(D_{a1},\ldots,D_{an})$ representing
the alignment data for each individual at site $a$. Similary, let
\mbox{${\bf G}=(\vec{G}_a,\ldots,\vec{G}_N)^{\rm T}$} and
\mbox{$\vec{G}_a=(G_{a1},\dots,G_{an})$} be the true genotypes, where
$0\le G_{ai}\le m_i$ equals the number of reference alleles~\footnote{If we
  take the ancestral sequence as the reference, the non-reference allele
  will be the derived allele.}. Define
\begin{equation}
X_a=X_a(\vec{G}_a)\triangleq\sum_iG_{ai}
\end{equation}
to be the number of reference alleles at site $a$ and ${\bf
  X}=(X_1,\ldots,X_N)^{\rm T}$. Also define
$\Phi=(\phi_0,\ldots,\phi_M)$ as the allele frequency spectrum (AFS)
with $\sum_k\phi_k=1$.

For convenience, we may drop the position subscript $a$ when it is
unambiguous in the context that we are looking at one locus.  Also
define
\begin{equation}
P(D_i|g_i)\triangleq\Pr\{D_i|G_i=g_i\}
\end{equation}
to be the likelihood of the data for individual $i$ when the underlying
genotype is known. $P(D_i|g_i)$ is calculated in
Section~\ref{sec:pdg}. And define
\begin{equation}
P(g_i|\phi)\triangleq\binom{m_i}{g_i}\phi^{g_i}(1-\phi)^{m_i-g_i}
\end{equation}
to be the probability of a genotype under the Hardy-Weinberg equilibrium
(HWE), when the site allele frequency is $\phi$.

\section{Estimating AFS}\label{sec:afs}
\subsection{The EM procedure}
We aim to find $\Phi$ that maximizes $P({\bf D}|\Phi)$ by EM. Suppose at
the $t$-th iteration the estimate is $\Phi_t$. We have
$$
\log \Pr\{{\bf D},{\bf X}={\bf x}|\Phi\}=\log \Pr\{{\bf D}|{\bf X}={\bf
  x}\}\Pr\{{\bf X}={\bf x}|\Phi\}=C+\sum_a\log \phi_{x_a}
$$
where $C$ is not a function of $\{\phi_k\}$. The EM $Q$ function
is\footnote{We assume site independency in the following.}
\begin{eqnarray*}
  Q(\Phi|\Phi_t)&=&\sum_{\bf x}\Pr\{{\bf X}={\bf x}|{\bf D},\Phi_t\}\log \Pr\{{\bf D},{\bf X}={\bf x}|\Phi\}\\
  &=&C+\sum_{\bf x}\prod_a \Pr\{X_a=x_a|\vec{D}_a,\Phi_t\}\sum_b\log \phi_{x_b}\\
  &=&C+\sum_{a=1}^N\sum_{x_a=0}^M \Pr\{X_a=x_a|\vec{D}_a,\Phi_t\}\log \phi_{x_a}
\end{eqnarray*}
Requiring $\partial_{\phi_k}(Q-\lambda\sum_l{\phi_l})=0$ leads to
$$
\frac{1}{\phi_k}\sum_a\Pr\{X_a=k|\vec{D}_a,\Phi_t\}-\lambda=0
$$
from which $\lambda$ can be calculated as:
$$
\lambda=\sum_k\sum_a \Pr\{X_a=k|\vec{D}_a,\Phi_t\}=N
$$
and thus at the $(t+1)$ iteration:
\begin{equation}\label{equ:em}
  \phi_k^{(t+1)}=\frac{1}{N}\sum_a\Pr\{X_a=k|\vec{D}_a,\Phi_t\}
\end{equation}
where $\Pr\{X_a=k|\vec{D}_a,\Phi_t\}$ is calculated as follows.

\subsection{The distribution of site reference allele count}
Firstly, as we are only looking at a site from now on, we drop subscript
$a$ for convenience. Without proof\footnote{Supposedly, this can be
  proved by polynomial expansion. Wiki gives a simplified version of
  this formula as
  \href{http://en.wikipedia.org/wiki/Vandermonde's_identity\#Generalized_Vandermonde.27s_identity}{generalized
    Vandermonde's identity}.}, we note that
$$
\binom{M}{k}\equiv\sum_{\vec{g}}\delta_{k,s_n(\vec{g})}\prod_i\binom{m_i}{g_i}
$$
where
$$
s_j(\vec{g})\triangleq\sum_{i=1}^jg_i
$$
and $\delta_{kl}=1$ if $k=l$ and 0 otherwise. The probability of
sampling $\vec{g}$ conditional on $\sum_ig_i=k$ is
$\delta_{k,s_n(\vec{g})}\prod_i\binom{m_i}{g_i}\big/\binom{M}{k}$. With this
preparation, we can calculate\footnote{The derivation below does
  \emph{not} assume Hardy-Weinberg equilibrium.}
\begin{eqnarray*}
\Pr\{\vec{D}|X=k\}&=&\sum_{\vec{g}}\Pr\{\vec{D},\vec{G}=\vec{g}|X=k\}\\
&=&\sum_{\vec{g}}\delta_{k,s_n(\vec{g})}\Pr\{\vec{D}|\vec{G}=\vec{g}\}\Pr\{\vec{G}=\vec{g}|X=k\}\\
&=&\sum_{\vec{g}}\delta_{k,s_n(\vec{g})}\prod_iP(D_i|g_i)\cdot\frac{\prod_j\binom{m_j}{g_j}}{\binom{M}{k}}\\
&=&\frac{1}{\binom{M}{k}}\sum_{\vec{g}}\delta_{k,s_n(\vec{g})}\prod_i\binom{m_i}{g_i}P(D_i|g_i)
\end{eqnarray*}
where $P(D_i|g_i)\triangleq\Pr\{D_i|G_i=g_i\}$ is the likelihood of data
when the underlying genotype is known. To calculate
$\Pr\{\vec{D}|X=k\}$, we define
$$
z_{jk}\triangleq\sum_{g_1=0}^{m_1}\cdots\sum_{g_j=0}^{m_j}\prod_{i=1}^j\binom{m_i}{g_i}P(D_i|g_i)
$$
for $0\le k\le\sum_{i=1}^jm_i$ and 0 otherwise. It can be calculated
iteratively with\footnote{To make it explicit, for diploid samples, if
  $A$ is the reference allele:
$$
z_{jk}=z_{j-1,k}P(D_j|\langle aa\rangle)+2z_{j-1,k-1}P(D_j|\langle Aa\rangle)+z_{j-1,k-2}P(D_j|\langle AA\rangle)
$$
}
\begin{equation}\label{equ:zcal}
z_{jk}=\sum_{g_j=0}^{m_j}z_{j-1,k-g_j}\cdot\binom{m_j}{g_j}P(D_j|g_j)
\end{equation}
with $z_{00}=1$. Thus
$$
\Pr\{\vec{D}|X=k\}=\frac{z_{nk}}{\binom{M}{k}}
$$
and
\begin{equation}\label{equ:postk}
\Pr\{X=k|\vec{D},\Phi\}=\frac{\phi_k\Pr\{\vec{D}|X=k\}}{\sum_l\phi_l\Pr\{\vec{D}|X=l\}}
=\frac{\phi_kz_{nk}/\binom{M}{k}}{\sum_l\phi_lz_{nl}/\binom{M}{l}}
\end{equation}

\subsection{Numerical stability}\label{sec:numsta}
Numerical computation of Eq.~(\ref{equ:zcal}) may lead to floating point
underflow for large $n$. To avoid this, let
$y_{jk}=z_{jk}/\binom{M_j}{k}$, where
$M_j=\sum_{i=1}^jm_i$. Eq.~(\ref{equ:zcal}) becomes
$$
y_{jk}=\left(\prod_{l=0}^{m_j-1}\frac{k-l}{M_{j}-l}\right)\cdot\sum_{g_j=0}^{m_j}\left(\prod_{l=g_j}^{m_j-1}\frac{M_{j-1}-k+l+1}{k-l}\right)\cdot y_{j-1,k-g_j}\cdot\binom{m_j}{g_j}P(D_j|g_j)
$$
In case of diploid samples:
\begin{eqnarray*}
y_{jk}&=&\frac{1}{2j(2j-1)}\Big[(2j-k)(2j-k-1)\cdot y_{j-1,k}P(D_j|\langle aa\rangle)+2k(2j-k)\cdot y_{j-1,k-1}P(D_j|\langle Aa\rangle)\\
&&+k(k-1)\cdot y_{j-1,k-2}P(D_j|\langle AA\rangle)\Big]
\end{eqnarray*}
For haploid samples,
\[
y_{jk}=\frac{1}{M_{j}}\Big[(M_{j-1}-k+1)\cdot y_{j-1,k}P(D_j|a)+k\cdot y_{j-1,k-1}P(D_j|A)\Big]
\]
However, this is not good enough. $y_{jk}$ still decreases exponentially
with increasing $j$. To solve this issue, we rescale $y_{jk}$ for each
$j$. Define
$$
\tilde{y}_{jk}=\frac{y_{jk}}{\prod_{i=1}^j t_i}
$$
where $t_j$ is chosen such that $\sum_l\tilde{y}_{jl}=1$. The posterior is
$$
\Pr\{X=k|\vec{D},\Phi\}=\frac{\phi_k\tilde{y}_{nk}}{\sum_l\phi_l\tilde{y}_{nl}}
$$
It should be noted that $P(D_i|g_i)$ can also be rescaled without
affecting the calculation of the posterior. Furthermore, in the
$\{\tilde{y}_{jk}\}$ matrix, most of cells should be close to
zero. Computation of $\tilde{y}_{nk}$ can be carried in a band instead
of in a triangle. For large $n$, this may considerably reduce computing
time.

\subsection{The initial AFS}
The EM procedure garantees that $\Pr\{{\bf D}|\Phi\}$ monotomically
increases with each iteration and converges to a local optima. However,
if we start this iteration from a bad initial AFS, we may need many
iterations; the iteration is also more likely to be trapped by a local
optima. Here we give several AFS on different conditions under the
infinite-site Wright-Fisher model.

Let $\phi'_k$ be the probability of seeing k non-reference alleles out
of $M$ chromosomes. The frequency of reference alleles $\phi_k$ equals
$\phi'_{M-k}$.

If we take the ancestral sequence as the reference, the standard model
gives $\phi'_k=\theta/k$ and $\phi'_0=1-\sum_k\phi'_k$. When we do not
know if the reference allele is ancestral, the same conclusion still
stands. To see this, for $k>0$:
$$
\phi'_k=\frac{M+1-k}{M+1}\left(\frac{\theta}{k}+\frac{\theta}{M+1-k}\right)=\frac{\theta}{k}
$$
and for $k=0$:
$$
\phi'_k=1-\sum_{k=1}^{M+1}\frac{\theta}{k}+\frac{\theta}{M+1}=1-\sum_{k=1}^{M}\frac{\theta}{k}
$$
where the first term corresponds to the case wherein the reference is
ancestral and the second to the case wherein the reference is derived.

Another useful AFS is the derived allele frequency spectrum on the
condition of loci being discovered from two chromosomes. Under the
Wright-Fisher model, it is:
$$
\phi'_k=\frac{2(M+1-k)}{(M+1)(M+2)}
$$

A third AFS is the derived allele frequency spectrum on the condition of
knowing one derived allele from a chromosome. It is a flat distribution
$$
\phi'_k=\frac{1}{M+1}
$$

\subsection{Joint distribution of allele counts for 2 groups of samples}
Suppose at a locus we have two data sets $\vec{D}'$ and $\vec{D}''$,
with $n'$ samples $m'$ haplotypes and $n''$ samples and $m''$
haplotypes, respectively. Let $n=n'+n''$ and $m=m'+m''$ and
$\vec{D}=\vec{D}'\oplus\vec{D}''$ be the joint of the two data sets. In
addition, let $X'=\sum_{i'}G'_{i'}$, $X''=\sum_{i''}G''_{i''}$ and
$X=X'+X''$. We have
$$
\Pr\{\vec{D}'|X'=k'\}=\frac{z'_{n'k'}}{\binom{M'}{k'}}
$$
$$
\Pr\{\vec{D}''|X''=k''\}=\frac{z''_{n''k''}}{\binom{M''}{k''}}
$$
and
$$
\Pr\{\vec{D}',\vec{D}''|X'=k',X''=k''\}=\Pr\{\vec{D}'|X'=k'\}\Pr\{\vec{D}''|X''=k''\}=\frac{z'_{n'k'}z''_{n''k''}}{\binom{M'}{k'}\binom{M''}{k''}}
$$
where
$$
z'_{j'k'}\triangleq\sum_{g'_1=0}^{m'_1}\cdots\sum_{g'_j=0}^{m'_j}\prod_{i'=1}^{j'}\binom{m'_i}{g'_i}P(D'_i|g'_i)
$$
and similar to $z''_{j''k''}$. If we note that
$$
\Pr\{X'=k',X''=k''|\Phi\}=\phi_k\cdot\frac{\binom{M'}{k'}\binom{M''}{k''}}{\binom{M}{k}}
$$
and by definition
$$
z_{nk}=z_{n'+n'',k'+k''}=\sum_{\{k',k''|k'+k''=k\}}z'_{n'k'}z''_{n''k''}
$$
we have
$$
\Pr\{\vec{D}|\Phi\}=\sum_k\Pr\{\vec{D},X=k|\Phi\}=\sum_{k',k''}\Pr\{\vec{D}',\vec{D}'',X'=k',X''=k''|\Phi\}
$$
Thus we can derive the joint distribution:
\begin{equation}
\Pr\{X'=k',X''=k''|\vec{D},\Phi\}=\frac{\phi_{k'+k''}z'_{n'k'}z''_{n''k''}\big/\binom{M}{k'+k''}}{\sum_l\phi_lz_{nl}\big/\binom{M}{l}}
\end{equation}
If we let $y_{jk}=z_{jk}/\binom{M_j}{k}$ as in Section~\ref{sec:numsta},
$$
\Pr\{X'=k',X''=k''|\vec{D},\Phi\}=\frac{\phi_{k'+k''}y'_{n'k'}y''_{n''k''}}{\sum_l\phi_ly_{nl}}\cdot\frac{\binom{M'}{k'}\binom{M''}{k''}}{\binom{M'+M''}{k'+k''}}
$$
This derivation can be extended to arbitrary number of data sets.

To test the association between the two groups of samples, we may use
the following statistics:
\[
P=\sum_{k',k''}\Pr\{X'=k',X''=k''|\vec{D},\Phi\}\cdot P(\chi^2(k',n'-k',k'',n''-k'')|1)
\]
where $\chi^2(n_{11},n_{12},n_{21},n_{22})$ gives the $\chi^2$ statistics of
a 2-by-2 contingency table and $P(\chi^2|1)$ computes the $P$-value of an 1-degree
$\chi^2$ statistics. Theoretically speaking, the equation above is not correct.
As a result, the $P$-value computed this way tends to underestimate the significance
on real data. Nonetheless, the relative value is still useful and this statistics
has a nice feature that when the data has little ambiguity, it approaches
the standard 1-degree $\chi^2$ used in GWAS.

\section{EM estimate of site statistics}

\subsection{Estimating site allele frequency assuming HWE}
Here we aim to find $\phi$ that maximises $\Pr\{\vec{D}|\phi\}$. We have:
$$\log \Pr\{\vec{D},\vec{G}=\vec{g}|\phi\}=\log\prod_iP(D_i|g_i)P(g_i|\phi)=C+\sum_i\log P(g_i|\phi)$$
Given an estimate $\phi_t$ at the $t$-th iteration, the $Q(\phi|\phi_t)$
function of EM is:
\begin{eqnarray*}
  Q(\phi|\phi_t)&=&\sum_{\vec{g}}\Pr\{\vec{G}=\vec{g}|\vec{D},\phi_t\}\log \Pr\{\vec{D},\vec{G}=\vec{g}|\phi\}\\
  &=&C+\sum_{\vec{g_i}}\prod_i\Pr\{G_i=g_i|D_i,\phi_t\}\sum_j\log P(g_j|\phi)\\
  &=&C+\sum_{i=1}^n\sum_{g_i=0}^{m_i}\Pr\{G_i=g_i|D_i,\phi_t\}\log P(g_i|\phi)\\
  &=&C+\sum_i\sum_{g_i}\Pr\{G_i=g_i|D_i,\phi_t\}\log_i \binom{m_i}{g_i}\phi^{g_i}(1-\phi)^{m_i-g_i}\\
  &=&C'+\sum_i\sum_{g_i}\Pr\{G_i=g_i|D_i,\phi_t\}\Big[g_i\log\phi+(m_i-g_i)\log(1-\phi)\Big]\\
\end{eqnarray*}
Requiring $\partial_{\phi}Q\Big|_{\phi=\phi_{t+1}}=0$ gives:
$$\frac{1}{\phi_{t+1}(1-\phi_{t+1})}\sum_i\sum_{g_i}\Pr\{G_i=g_i|D_i,\phi_t\}(g_i-m_i\phi_{t+1})=0$$
Thus
\begin{equation}\label{equ:freq1}
\phi_{t+1}=\frac{1}{\sum_jm_j}\sum_i\sum_{g_i}g_i\Pr\{G_i=g_i|D_i,\phi_t\}
=\frac{1}{M}\sum_i\frac{\sum_{g_i}g_iP(D_i|g_i)P(g_i|\phi_t)}{\sum_{g_i}P(D_i|g_i)P(g_i|\phi_t)}
\end{equation}
which is the EM estimate at the $(t+1)$-th iteration and also the
expected reference allele frequency.

\subsection{Estimating genotype frequency}
In the previous section, we estimate the site allele frequency assuming Hardy-Weinberg equilibrium (HWE).
We can actually relax this assumption when all samples have the sample diploidy.
Let $m=m_1=\cdots=m_n$ and $\vec{\phi}=\{\phi_0,\phi_1,\ldots,\phi_m\}$ be the vector of
genotype frequency satisfying $\sum_g\phi_g=1$. It is easy to compute the $Q$-function:
$$
Q(\vec{\phi}|\vec{\phi}^{(t)})=C+\sum_{i=1}^n\sum_{g=0}^m\Pr\{G_i=g|D_i,\vec{\phi}^{(t)}\}\log\phi_g
$$
Requiring $\partial_{\phi_g}\big[Q(\vec{\phi}|\vec{\phi}^{(t)})-\lambda\sum_{g'}\phi_{g'}\big]\Big|_{\phi_g=\phi_g^{(t+1)}}$=0 gives $\lambda=n$
and thus
$$
\phi_g^{(t+1)}=\frac{1}{n}\sum_{i=1}^n\Pr\{G_i=g|D_i,\vec{\phi}^{(t)}\}=\frac{1}{n}\sum_{i=1}^n\frac{P(D_i|g)\phi_g^{(t)}}{\sum_{g'}P(D_i|g')\phi_{g'}^{(t)}}
$$
Obviously, $\sum_g\phi_g^{(t+1)}=1$ as it should be. This estimate is useful to test HWE.

\subsection{Estimating two-locus LD}
In this section, we only consider diploid samples
(i.e. $m_1=\cdots=m_n=2$). Let ${\bf D}=(\vec{D},\vec{D}')$ be the data
at two loci, respectively; and $H_i$ and $H^{\dag}_i$ be the two
underlying haplotypes for individual $i$ with $H_i\in\{0,1,2,3\}$
representing one of the four possible haplotypes at the 2 loci. We write
${\bf H}=\overrightarrow{(H_i,H^{\dag}_i)}$ as a haplotype configuration of the
samples. Define
$$
\mathcal{G}_{hk}=\mathcal{G}_{kh}=\lfloor h/2\rfloor+\lfloor k/2\rfloor
$$
$$
\mathcal{G}'_{hk}=\mathcal{G}'_{kh}=(h\mod 2)+(k\mod 2)
$$
which calculate the genotype of each locus, respectively.
\begin{eqnarray*}
  Q(\vec{\phi}|\vec{\phi}^{(t)})&=&\sum_{\bf h}P({\bf H}={\bf h}|{\bf D},\vec{\phi}^{(t)})\log\Pr\{{\bf D},{\bf H}={\bf h}|\vec{\phi}\}\\
  &=&C+\sum_{\bf h}\prod_i\Pr\{H_i=h_i,H^{\dag}_i=h^{\dag}_i|{\bf D},\vec{\phi}^{(t)}\}\sum_j\log P(h_j,h^{\dag}_j|\vec{\phi})\\
  &=&C+\sum_i\sum_{h_i}\sum_{h^{\dag}_i}\Pr\{H_i=h_i,H^{\dag}_i=h^{\dag}_i|{\bf D},\vec{\phi}^{(t)}\}\sum_j\log(\phi_{h_i}\phi_{h^{\dag}_i})
\end{eqnarray*}
%P(\mathcal{G}(h_i,h^{\dag}_i)|\vec{D},\vec{\phi})P(\mathcal{G}'(h_i,h^{\dag}_i)|\vec{D}',\vec{\phi})\log(\phi_{h_i}\phi_{h^{\dag}_i})
Solving $\partial_{\phi_k}Q-\lambda=0$ gives
\begin{eqnarray*}
\phi_k&=&\frac{1}{2n}\sum_i\sum_h\left(\Pr\{H_i=h,H^{\dag}_i=k|{\bf D},\vec{\phi}^{(t)}\}+\Pr\{H_i=k,H^{\dag}_i=h|{\bf D},\vec{\phi}^{(t)}\}\right)\\
&=&\frac{\phi^{(t)}_k}{2n}\sum_{i=1}^n\frac{\sum_{h}\phi^{(t)}_h\big[P(D_i|{\cal G}_{hk})P(D'_i|{\cal G}'_{hk})+P(D_i|{\cal G}_{kh})P(D'_i|{\cal G}'_{kh})\big]}{\sum_{k',h}\phi^{(t)}_{k'}\phi^{(t)}_hP(D_i|{\cal G}_{hk'})P(D'_i|{\cal G}'_{hk'})}\\
&=&\frac{\phi^{(t)}_k}{n}\sum_{i=1}^n\frac{\sum_{h}\phi^{(t)}_hP(D_i|{\cal G}_{hk})P(D'_i|{\cal G}'_{hk})}{\sum_{k',h}\phi^{(t)}_{k'}\phi^{(t)}_hP(D_i|{\cal G}_{hk'})P(D'_i|{\cal G}'_{hk'})}
\end{eqnarray*}

\section{An alternative model assuming HWE}
In Section~\ref{sec:afs}, $\phi_k$ in $\Phi=\{\phi_k\}$ is interpreted
as the probability of seeing exactly $k$ alleles from $M$
chromosomes. Under this model, the prior of a genotype configuration is
$$
\Pr\{\vec{G}=\vec{g}|\Phi\}=\phi_{s_n(\vec{g})}\frac{\prod_i\binom{m_i}{g_i}}{\binom{M}{s_n(\vec{g})}}
$$
and the posterior is
$$
\Pr\{\vec{G}=\vec{g}|\vec{D},\Phi\}=\frac{\phi_{s_n(\vec{g})}}{\Pr\{\vec{D}|\Phi\}}\cdot\frac{\prod_i\binom{m_i}{g_i}P(D_i|g_i)}{\binom{M}{s_n(\vec{g})}}
$$
Suppose we want to calculate the expectation of $\sum_if_i(g_i)$, we can
$$
\sum_i\sum_{\vec{g}}f_i(g_i)\Pr\{\vec{G}=\vec{g}|\vec{D},\Phi\}
=\frac{1}{\Pr\{\vec{D}|\Phi\}}\sum_i\sum_k\frac{\phi_k}{\binom{M}{k}}\sum_{\vec{g}}\delta_{k,s_n(\vec{g})}f_i(g_i)\prod_j\binom{m_j}{g_j}P(D_j|g_j)
$$
Due to the presence of $\delta_{k,s_n(\vec{g})}$, we are unable to
reduce the formula to a simpler form. Although we can take a similar
strategy in Section~\ref{sec:afs} to calculate $\sum_k\sum_{\vec{g}}$,
which is $O(n^2)$, another sum $\sum_i$ will bring this calculation to
$O(n^3)$. Even calculating the marginal probability
$\Pr\{G_i=g_i|\vec{D},\Phi\}$ requires this time complexity. All the
difficulty comes from that individuals are correlated conditional on
$\{X=k\}$.

An alternative model is to interpret the AFS as the discretized AFS of
the population rather than for the observed individuals. We define the
population AFS discretized on $M$ chromosomes as
$\Phi'=\{\phi'_k\}$. Under this model,
$$
\Pr\{\vec{G}=\vec{g}|\Phi'\}=\sum_k\phi_k\prod_iP(g_i|k/M)
$$
$$
\Pr\{\vec{G}=\vec{g},\vec{D}|\Phi'\}=\sum_k\phi_k\prod_iP(D_i|g_i)P(g_i|k/M)
$$
$$
\Pr\{\vec{D}|\Phi'\}=\sum_{\vec{g}}\Pr\{\vec{G}=\vec{g},\vec{D}|\Phi'\}
=\sum_{k=0}^M\phi'_k\prod_{i=1}^n\sum_{g_i=0}^{m_i}P(D_i|g_i)P(g_i|k/M)
$$
and
\begin{eqnarray}\label{equ:fexp}
&&\sum_i\sum_{\vec{g}}f_i(g_i)\Pr\{\vec{G}=\vec{g}|\vec{D},\Phi'\}\\\nonumber
&=&\frac{1}{\Pr\{\vec{D}|\Phi'\}}\sum_i\sum_{\vec{g}}f_i(g_i)\sum_k\phi'_k\prod_jP(D_j|g_j)P(g_j|k/M)\\\nonumber
&=&\frac{1}{\Pr\{\vec{D}|\Phi'\}}\sum_k\phi'_k\sum_if_i(g_i)P(D_i|g_i)P(g_i|k/M)\prod_{j\not=i}\sum_{g_j}P(D_j|g_j)P(g_j|k/M)\\\nonumber
&=&\frac{1}{\Pr\{\vec{D}|\Phi'\}}\sum_k\phi'_k\left[\prod_i\sum_{g_i}P(D_i|g_i)P(g_i|k/M)\right]\cdot\left[\sum_i\frac{\sum_{g_i}f_i(g_i)P(D_i|g_i)P(g_i|k/M)}{\sum_{g_i}P(D_i|g_i)P(g_i|k/M)}\right]
\end{eqnarray}
The time complexity of this calculation is $O(n^2)$. Consider that if
$f_i(g_i)=g_i$, $X=\sum_if_i(G_i)$. We can easily calculate
$E(X|\vec{D},\Phi')$ with the formula above.

\subsection{Posterior distribution of the allele count}
Under the alternative model, we can also derive the posterior
distribution of $X$, $\Pr\{X=k|\vec{D},\Phi'\}$ as follows.
$$
\Pr\{X=k|\Phi'\}=\binom{M}{k}\sum_{l=0}^M\phi'_l\left(\frac{l}{M}\right)^k\left(1-\frac{l}{M}\right)^{M-k}
$$
Then
\begin{equation}\label{equ:pdk-alt}
\Pr\{\vec{D},X=k|\Phi'\}=z_{nk}\sum_{l=0}^M\phi'_l\left(\frac{l}{M}\right)^k\left(1-\frac{l}{M}\right)^{M-k}
\end{equation}

In fact, we also have an alternative way to derive this
$\Pr\{\vec{D},X=k|\Phi'\}$. Let $\phi'$ be the true site allele
frequency in the population. Assuming HWE, we have
\begin{eqnarray}\label{equ:pdk2}
\Pr\{X=k,\vec{D}|\phi'\}&=&\sum_{\vec{g}}\delta_{k,s_n(\vec{g})}\Pr\{\vec{D}|\vec{G}=\vec{g}\}\Pr\{\vec{G}=\vec{g}|\phi'\}\\\nonumber
&=&\sum_{\vec{g}}\delta_{k,s_n(\vec{g})}\prod_iP(D|g_i)\binom{m_i}{g_i}\phi'^{g_i}(1-\phi')^{m_i-g_i}\\\nonumber
&=&\phi'^k(1-\phi')^{M-k}\sum_{\vec{g}}\delta_{k,s_n(\vec{g})}\prod_i\binom{m_i}{g_i}P(D_i|g_i)\\\nonumber
&=&\phi'^k(1-\phi')^{M-k}z_{nk}
\end{eqnarray}
Summing over the AFS gives
\begin{equation*}
\Pr\{\vec{D},X=k|\Phi'\}=\sum_l\phi'_l\Pr\{X=k,\vec{D}|\phi'=l/M\}
\end{equation*}
which is exactly Eq.~(\ref{equ:pdk-alt}). It is worth noting that
$E(X|\vec{D},\Phi')$ calculated by Eq.~(\ref{equ:pdk2}) is identical to
the one calculated by Eq.~(\ref{equ:fexp}), which has been numerically
confirmed.

In practical calculation, the alternative model has very similar
performance to the method in Section~\ref{sec:afs} in one
iteration. However, in proving EM, we require $\Pr\{X=k|\Phi\}=\phi_k$,
which does not stand any more in the alternative
interpretation. Iterating Eq.~(\ref{equ:pdk-alt}) may not monotonically
increase the likelihood function. Even if this was also another
different EM procedure, we have not proved it yet. Yi {\it et al.}
(2010) essentially calculates Eq.~(\ref{equ:pdk2}) for $\phi'$ estimated
from data without summing over the AFS. Probably this method also
delivers similar results, but it is not theoretically sound and may not
be iterated, either.

\section{Likelihood of data given genotype}\label{sec:pdg}
Given a site covered by $k$ reads from an $m$-ploid individual, the
sequencing data is:
\[
D=(b_1,\dots,b_k)=(\underbrace{1,\ldots,1}_l,\underbrace{0,\ldots,0}_{k-l})
\]
where 1 stands for a reference allele and 0 otherwise. The $j$-th base
is associated with error rate $\epsilon_j$, which is the larger error
rate between sequencing and alignment errors. We have
\begin{equation}
P(D|0)=\prod_{j=1}^l\epsilon_j\prod_{j=l+1}^k(1-\epsilon_j)=\left(1-\sum_{j=l+1}^k\epsilon_j+o(\epsilon^2)\right)\prod_{j=1}^l\epsilon_j
\end{equation}
\begin{equation}
P(D|m)=\left(1-\sum_{j=1}^l\epsilon_j+o(\epsilon^2)\right)\prod_{j=l+1}^k\epsilon_j
\end{equation}
For $0<g<m$:
\begin{eqnarray}\label{eq:gd}
P(D|g)&=&\sum_{a_1=0}^1\cdots\sum_{a_k=0}^1\Pr\{D|B_1=a_1,\ldots,B_k=a_k\}\Pr\{B_1=a_1,\ldots,B_k=a_k|g\}\\\nonumber
&=&\sum_{\vec{a}}\left(\frac{g}{m}\right)^{\sum_j a_j}\left(1-\frac{g}{m}\right)^{k-\sum_j a_j}\cdot\prod_j p_j(a_j)\\\nonumber
&=&\left(1-\frac{g}{m}\right)^k\prod_j\sum_{a=0}^1 p_j(a)\left(\frac{g}{m-g}\right)^a\\\nonumber
&=&\left(1-\frac{g}{m}\right)^k\prod_{j=1}^l\left(\epsilon_j+\frac{g}{m-g}(1-\epsilon_j)\right)\prod_{j=l+1}^k\left(1-\epsilon_j+\frac{\epsilon_jg}{m-g}\right)\\\nonumber
&=&\left(1-\frac{g}{m}\right)^k\left\{\left(\frac{g}{m-g}\right)^l+\left(1-\frac{g}{m-g}\right)\left(\sum_{j=1}^l\epsilon_j-\sum_{j=l+1}^k\epsilon_j\right)+o(\epsilon^2)\right\}
\end{eqnarray}
where $a=\sum_j a_j$ and
$$
p_j(a)=\left\{\begin{array}{ll}
\epsilon_j & \mbox{$a=1$}\\
1-\epsilon_j & \mbox{$a=0$}\\
\end{array}\right.
$$
In the bracket of Eq.~\ref{eq:gd}, the first term explains the deviation between $l/k$ and
$g/m$ by imperfect sampling, while the second term explains the
deviation by sequencing errors. The second term can be ignored when $k$
is small but may play a major role when $k$ is large. In particular, for
$m=2$, $P(D|1)=2^{-k}$, independent of sequencing errors.

In case of dependent errors, we may replace:
\[\epsilon_1<\epsilon_2<\cdots<\epsilon_l\]
with
\[\epsilon'_j=\epsilon_j^{\alpha^{j-1}}\]
where parameter $\alpha\in[0,1]$ addresses the error dependency.

\section{Multi-sample SNP calling and genotyping}

The probability of the site being polymorphic is
$\Pr\{X=0|\vec{D},\Phi\}$. For individual $i$, we may estimate the
genotype $\hat{g}_i$ as:
$$
\hat{g}_i=\argmax_{g_i} \Pr\{G_i=g_i|D_i,\phi_E\}=\argmax_{g_i}\frac{P(D_i|g_i)P(g_i|\phi_E)}
{\sum_{h_i}P(D_i|h_i)P(h_i|\phi_E)}
$$
where
$$
\phi_E=E(X|\vec{D},\Phi)/M
$$
This estimate of genotypes may not necessarily maximize the posterior
probability $P(\vec{g}|\vec{D})$, but it should be good enough in
practice. However, it should be noted that $\sum_i\hat{g}_i$ is usually
a bad estimator of site allele frequency. The max-likelihood estimator
by Eq. \eqref{equ:freq1} is much better.

\end{document}