\documentclass[nofootinbib,amssymb,amsmath]{revtex4}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{color}
\usepackage{bm}

%Put an averaged random variable between brackets
\newcommand{\ave}[1]{\left\langle #1 \right\rangle}

\newcommand{\vzero}{{\bf 0}}
\newcommand{\vI}{{\bf I}}
\newcommand{\vb}{{\bf b}}
\newcommand{\vd}{{\bf d}}
\newcommand{\vf}{{\bf f}}
\newcommand{\vc}{{\bf c}}
\newcommand{\vv}{{\bf v}}
\newcommand{\vz}{{\bf z}}
\newcommand{\vn}{{\bf n}}
\newcommand{\vm}{{\bf m}}
\newcommand{\vG}{{\bf G}}
\newcommand{\vQ}{{\bf Q}}
\newcommand{\vM}{{\bf M}}
\newcommand{\vW}{{\bf W}}
\newcommand{\vX}{{\bf X}}
\newcommand{\vPsi}{{\bf \Psi}}
\newcommand{\vSigma}{{\bf \Sigma}}
\newcommand{\vlambda}{{\bf \lambda}}
\newcommand{\vpi}{{\bf \pi}}
\newcommand{\valpha}{{\bm{\alpha}}}
\newcommand{\vbeta}{{\bm{\beta}}}
\newcommand{\vomega}{{\bm{\omega}}}
\newcommand{\vLambda}{{\bf \Lambda}}
\newcommand{\vA}{{\bf A}}

\newcommand{\code}[1]{\texttt{#1}}
\newcommand*{\Comb}[2]{{}^{#1}C_{#2}}

\newtheorem{lemma}{Lemma}
\newtheorem{corollary}{Corollary}

\def\SL#1{{\color [rgb]{0,0,0.8} [SL: #1]}}
\def\DB#1{{\color [rgb]{0,0.8,0} [DB: #1]}}

\newcommand{\HOM}{$\mathsf{Hom}$}
\newcommand{\HET}{$\mathsf{Het}$}
\newcommand{\REF}{$\mathsf{Ref}$}
\newcommand{\epss}{\varepsilon}

\begin{document}

\title{Mathematical Notes on Mutect}
\author{David Benjamin}
\email{davidben@broadinstitute.org}
\affiliation{Broad Institute, 75 Ames Street, Cambridge, MA 02142}
\author{Takuto Sato}
\email{tsato@broadinstitute.org}
\affiliation{Broad Institute, 75 Ames Street, Cambridge, MA 02142}

\date{\today}

\maketitle

\section{Somatic Likelihoods Model}\label{introduction}

We have a set of potential somatic alleles and read-allele likelihoods $\ell_{ra} \equiv P({\rm read~}r|{\rm allele~}a)$.  We don't know which alleles are real somatic alleles and so we must compute, for each subset $\mathbb{A}$ of alleles, the likelihood that the reads come from $\mathbb{A}$.  A simple model for this likelihood is as follows: each read $r$ is associated with a latent indicator vector $\vz_r$ with one-hot encoding $z_{ra} = 1$ iff read $r$ came from allele $a \in \mathbb{A}$.  The conditional probability of the reads $\mathbb{R}$ given their allele assignments is
\begin{equation}
P( \mathbb{R} | \vz, \mathbb{A}) = \prod_{r \in \mathbb{R}} \prod_a \ell_{ra}^{z_{ra}}.
\end{equation}
The alleles are not equally likely because there is a latent vector $\vf$ of allele fractions -- $f_a$ is the allele fraction of allele $a$.  Since the components of $\vf$ sum to one it is a categorical distribution and can be given a Dirichlet prior,
\begin{equation}
P(\vf) = {\rm Dir}(\vf | \valpha).
\end{equation}
Then $f_a$ is the prior probability that a read comes from allele $a$ and thus the conditional probability of the indicators $\vz$ given the allele fractions $\vf$ is
\begin{equation}
P(\vz | \vf) = \prod_r \prod_a f_a^{z_{ra}}.
\end{equation}
The full-model likelihood is therefore
\begin{equation}
\mathbb{L}(\mathbb{A}) = P(\mathbb{R}, \vz, \vf | \mathbb{A}) = {\rm Dir}(\vf | \valpha) \prod_a  \prod_r \left( f_a \ell_{ra}\right)^{z_{ra}}.
\label{full_likelihood}
\end{equation}
And the marginalized likelihood of $\mathbb{A}$, that is, the model evidence for allele subset $\mathbb{A}$, is
\begin{equation}
P(\mathbb{R} | \mathbb{A}) = \sum_\vz \int d \vf \, {\rm Dir}(\vf | \valpha) \prod_a  \prod_r \left( f_a \ell_{ra}\right)^{z_{ra}},
\label{evidence}
\end{equation}
where the integral is over the probability simplex $\sum_a f_a = 1$.

The integral over $\vf$ is the normalization constant of a Dirichlet distribution and as such we can simply look up its formula.  However, the sum over all values of $\vz$ for all reads has exponentially many terms.  We will get around this difficulty by handling $\vz$ with a mean-field approximation in which we factorize the likelihood as $\mathbb{L} \approx q(\vz) q(\vf)$.  This approximation is exact in two limits: first, if there are many reads, each allele is associated with many reads and therefore the Law of Large Numbers causes $\vf$ and $\vz$ to become uncorrelated.  Second, if the allele assignments of reads are obvious $\vz_r$ is effectively not a random variable at all (there is no uncertainty as to which of component is non-zero) and also becomes uncorrelated with $\vf$.

In the variational Bayesian mean-field formalism the value of $\vf$ that $\vz$ ``sees'' is the expectation of $\log \mathbb{L}$ with respect to $q(\vf)$ and vice versa.  That is,
\begin{equation}
q(\vf) \propto {\rm Dir}(\vf | \valpha) \prod_a  \prod_r f_a^{\bar{z}_{ra}} \propto {\rm Dir}(\vf | \valpha + \sum_r \bar{\vz}_r),
\label{qf}
\end{equation}
where $\bar{z}_{ra} \equiv E_q \left[ z_{ra} \right]$, and
\begin{equation}
q(\vz_r) = \prod_a \left( \tilde{f}_a \ell_{ra}\right)^{z_{ra}}, \tilde{f}_a = \exp E[\ln f_a]
\end{equation}
Because $q(\vz)$ is categorical and $q(\vf)$ is Dirichlet\footnote{Note that we didn't \textit{impose} this in any way.  It simply falls out of the mean field equations.} the necessary mean fields are easily obtained and we have
\begin{equation}
\bar{z}_{ra} = \frac{\tilde{f}_a \ell_{ra}}{\sum_{a^\prime} \tilde{f}_{a^\prime} \ell_{ra^\prime}}
\label{z_mean_field}
\end{equation}
and
\begin{equation}
\ln \tilde{f}_a = \psi(\alpha_a + \sum_r \bar{z}_{ra}) - \psi(\sum_{a^\prime} \alpha_{a^\prime} + N)
\label{f_mean_field}
\end{equation}
where $\psi$ is the digamma function and $N$ is the number of reads.  To obtain $q(\vz)$ and $q(\vf)$ we iterate Equations \ref{z_mean_field} and \ref{f_mean_field} until convergence.  A very reasonable initialization is to set $\bar{z}_{ra} = 1$ if $a$ is the most likely allele for read $r$, 0 otherwise.  Having obtained the mean field of $\vz$, we would like to plug it into Eq \ref{evidence}.  We can't do this directly, of course, because Eq \ref{evidence} says nothing about our mean field factorization.  Rather, we need the variational approximation (Bishop's Eq 10.3) to the model evidence, which is
\begin{align}
\ln P(\mathbb{R} | \mathbb{A}) \approx& \sum_{\vz} \int d \vf q(\vz) q(\vf) \left[ \ln P(\mathbb{R}, \vz, \vf | \mathbb{A}) - \ln q(\vz) - \ln q(\vf) \right] \\
=& E_q \left[ \ln P(\mathbb{R}, \vz, \vf | \mathbb{A}) \right] - E_q \left[ \ln q(\vz) \right] - E_q \left[ \ln q(\vf) \right]. \label{lagrangian}
\end{align}
Before we proceed, let's introduce some notation.  First, from Eq \ref{qf} the posterior $q(\vf)$ is
\begin{equation}
q(\vf) = {\rm Dir}(\vf | \vbeta), \quad \vbeta = \valpha + \sum_r \bar{\vz}_r.
\end{equation}
Second, let's define the log normalization constant of a Dirichlet distribution as $g$ so that
\begin{equation}
\ln {\rm Dir}(\vf | \vomega) = g(\vomega) + \sum_a (\omega_a - 1) \ln f_a, \quad g(\vomega) = \ln \Gamma(\sum_a \omega_a) - \sum_a \ln \Gamma(\omega_a).
\end{equation}
Finally, define the Dirichlet mean log (aka ``that digamma stuff") as $h$:
\begin{equation}
E_{\rm Dir(\vf | \vomega)} \left[ \ln f_a \right] = \psi(\omega_a) - \psi(\sum_{a^\prime} \omega_{a^\prime}) \equiv h_a(\vomega).
\end{equation}

The log of Eq \ref{full_likelihood} is
\begin{equation}
\ln P(\mathbb{R}, \vz, \vf | \mathbb{A}) = g(\valpha) + \sum_a (\alpha_a - 1) \ln f_a + \sum_{ra} z_{ra} (\ln f_a + \ln \ell_{ra}).
\end{equation}
and thus the first term in Eq \ref{lagrangian} is
\begin{align}
E_q \left[ \ln P(\mathbb{R}, \vz, \vf | \mathbb{A}) \right] =& g(\valpha) + \sum_a (\alpha_a - 1) h_a(\vbeta) + \sum_{ra}\bar{z}_{ra} \left( h_a(\vbeta) + \ln \ell_{ra} \right) \\
=& g(\valpha) + \sum_a (\beta_a - 1) h_a(\vbeta) + \sum_{ra}\bar{z}_{ra} \ln \ell_{ra}, \label{first_term}
\end{align}
where we used the relationship $\vbeta = \valpha + \sum_r \bar{\vz}_r$.

The second term in Eq \ref{lagrangian} is
\begin{align}
- E_q \left[ \ln q(\vz) \right] = - \sum_{ra} \bar{z}_{ra} \ln \bar{z}_{ra} \label{second_term}.
\end{align}

The third term in Eq \ref{lagrangian} is
\begin{align}
- E_q \left[ \ln q(\vf) \right] = -g(\vbeta) - \sum_a (\beta_a - 1) E_q [\ln f_a] = -g(\vbeta) - \sum_a (\beta_a - 1) h_a(\vbeta) \label{third_term}.
\end{align}

Adding Eqs \ref{first_term}, \ref{second_term}, and \ref{third_term} and noting the cancellation between parts of Eqs \ref{first_term} and \ref{third_term} we obtain
\begin{equation}
\ln P(\mathbb{R} | \mathbb{A}) \approx g(\valpha) - g(\vbeta) +  \sum_{ra} \bar{z}_{ra} \left( \ln \ell_{ra} - \ln \bar{z}_{ra} \right).
\end{equation}

We now have the model evidence for allele subset $\mathbb{A}$.  This lets us choose which alleles are true somatic variants.  It also lets us make calls on somatic loss of heterozygosity events.  Furthermore, instead of reporting max-likelihood allele fractions as before, we may emit the parameters of the Dirichlet posterior $q(\vf)$, which encode both the maximum likelihood allele fractions and their uncertainty.

\section{Strand Artifact Model}

\begin{figure}
\centering
\includegraphics[width=0.3\textwidth]{strand_artifact_pgm.png}
\caption{\label{fig:strand artifact}The probabilistic graphical model for the strand artifact model}
\end{figure}

The strand artifact filter detects sequencing artifacts in which the evidence for the alt allele consists entirely of forward strand reads alone or reverse strand reads alone. We must detect this while taking into account the fact that at some loci, such as near the end of an exome target, \emph{all} reads are biased towards one direction, and therefore a bias towards a particular strand among alt reads is no cause for alarm.

Let $z \in \{ z_+, z_-, z_o \}$ be a latent random variable with 1-hot encoding that represents the artifact state of a suspected variant. $z_+ = 1$ when the candidate variant is a forward strand artifact, $z_- = 1$ when it's a reverse artifact, and $z_o = 1$ when there's no artifact. At each locus, the conditional distribution over the number of forward alt reads $x^+$ is a binomial random variable

\begin{equation}
P(x^+ | f, \epsilon, z) = \text{Bin} (x^+ | n^+, f + \epsilon(1-f))^{z_+} \text{Bin} (x^+ | n^+, f)^{1 - z_+}
\end{equation}

where $n^+$ is the number of \emph{total} forward reads, $f$ is the allele fraction, and $\epsilon$ is the artifactual error rate, which is the probability that we mistakenly read a ref allele as alt due to strand artifact. We put a flat prior on $f$ and a beta prior on $\epsilon$. The conditional distribution of $x^-$ is defined analogously.

In order to decide whether to filter a variant, we compute the posterior probabilities of $z$ given the observed alt read counts $X = \{ x^+, x^- \}$.  We obtain the likelihood $P(X | z)$ by marginalizing the latent variables $f$ and $\epsilon$ out of the joint distribution $P(X, f, \epsilon | z)$, and we do this for each of the three possible values of $z$ separately. First consider the case $z_+ = 1$, which we denote by the shorthand $z_+$ below.

\begin{align}
P(X | z_+ )  &= \iint  P(X, f, \epsilon | z_+) \,df\,d\epsilon \nonumber \\
		  &= \iint  P(f) P(\epsilon) P(x^+ | z_+, f, \epsilon) P(x^- | z_+, f, \epsilon) \,df\,d\epsilon \nonumber \\
		  &= \iint \mathrm{Beta}(\epsilon|\alpha, \beta) \mathrm{Bin}(x^+ | n^+, f + \epsilon(1-f)) \mathrm{Bin}(x^- | n^-, f) \,df\,d\epsilon
\end{align}

where $\alpha$ and $\beta$ are fixed hyperparameters to the beta prior on $\epsilon$. By Bayes Rule the posterior probability of strand bias for forward reads is therefore

\begin{align}
P(z_+ | X) & \propto P(z_+) P(X | z_+) = \pi_+ P(X | z_+)
\end{align}

where $\pi_+ = P(z_+)$ is a fixed prior probability of strand artifact. The posterior probability of reverse strand artifact $P(z_- | X)$ may be derived analogously.

The derivation of $P(z_o|X)$ is similar except that the integral becomes much simpler because $P(X|z_o)$ does not depend on the artifactual error rate $\epsilon$.

\begin{align}
P(X | z_o)  &= \iint P(\epsilon) P(x^+ | z_o, f, \epsilon) P(x^- | z_o, f, \epsilon) \,df\,d\epsilon \nonumber \\
	 	 &= \int  P(x^+ | z_o, f) P(x^- | z_o, f) \,df \int  P(\epsilon) d\epsilon \nonumber \\
		 &= \int  \mathrm{Bin}(x^+ | n^+, f) \mathrm{Bin}(x^- | n^-, f) \,df
\end{align}

And the posterior probability is

\begin{equation}
P(z_o | X) \propto P(z_o) P(X | z_o) =  \pi_o P(X | z_o)
\end{equation}

where $\pi_o = P(z_o = 1)$ is the fixed prior probability of no artifact. We normalize the posteriors and filter the variant if the posterior probability of $z_+ = 1$ or $z_- = 1$ exceeds the threshold.

\section{Germline Filter}\label{germline-filter}
Suppose we have detected an allele such that its (somatic) likelihood in the tumor is $\ell_t$ and its (diploid) likelihood in the normal is $\ell_n$\footnote{This is the total likelihood for het and hom alt in the normal.}.  By convention, both of these are relative to a likelihood of $1$ for the allele \textit{not} to be found.  If we have no matched normal, $\ell_n = 1$.  Suppose we also have the population allele frequency $f$ of this allele.  Then the prior probabilities for the normal to be heterozygous and homozygous alt for the allele are $2f(1-f)$ and $f^2$ and the prior probability for the normal genotype not to contain the allele is $(1-f)^2$.  Finally, suppose that the prior for this allele to arise as a somatic variant is $\pi$.

We can determine the posterior probability that the variant exists in the normal genotype by calculating the unnormalized probabilities of four possibilities:
\begin{enumerate}
\item The variant exists in the tumor and the normal as a germline het.  This has unnormalized probability $2f(1-f) \ell_n \ell_t (1 - \pi)$.
\item The variant exists in the tumor and the normal as a germline hom alt.  This has unnormalized probability $f^2 \ell_n \ell_t (1 - \pi)$.
\item The variant exists in the tumor but not the normal.  This has unnormalized probability $(1-f)^2 \ell_t \pi$.
\end{enumerate}

We exclude possibilities in which the variant does not exist in the tumor sample because we really want the conditional probability that the variant is germline given that it would otherwise be called.

Normalizing, we obtain the following posterior probability that an allele is a germline variant:
\begin{equation}
P({\rm germline}) = \frac{(1) + (2)}{(1) + (2) + (3)} = \frac{\left(2f(1-f) + f^2 \right) \ell_n \ell_t (1 - \pi)}{\left(2f(1-f) + f^2 \right) \ell_n \ell_t  (1 - \pi) + \ell_t (1-f)^2  \pi}.
\end{equation}

The above equation, in which the factors of $\ell_t$ could cancel if we wished, is not quite right.  The tumor likelihood $\ell_t$ is the probability of the tumor data given that the allele exists in the tumor \textit{as a somatic variant}.  If the allele is in the tumor as a germline het we must modify $\ell_t$ to account for the fact that the allele fraction is determined by the ploidy -- it must be either $f_g$ or $1- f_g$with equal probability, where $f_g$ is the minor allele fraction of germline hets.  It would be awkward to recalculate the tumor likelihood with the allele frequency constrained to these two values\footnote{The model could easily accommodate this change, but the likelihoods are long gone from memory once the germline computation occurs.}, but we can estimate a correction factor as follows:  assuming that the posterior on the allele fraction in the somatic likelihoods model is fairly tight, the likelihood of $a$ alt reads out of $n$ total reads is $\binom na (1-f_t)^{n-a}f^a$, where $f_t$ is the tumor alt allele fraction.  That is, our sophisticated model that marginalizes over $f_t$ reduces to something more naive.  If the variant is a germline event, the likelihood becomes $\frac{1}{2} \binom na  \left[(1-f_g)^{n-a}f_g^a + f_g^{n-a}(1-f_g)^a \right]$.  Thus, in case (1) we have $\ell_t \rightarrow \chi \ell_t$, where
\begin{equation}
\chi = \frac{1}{2} \frac{(1-f_g)^{n-a}f_g^a + f_g^{n-a}(1-f_g)^a}{(1-f_t)^{n-a}f_t^a}.
\end{equation}
For germline hom alts, both the tumor and normal allele fractions will be similarly large, so to decent approximation we don't have to modify $\ell_t$.  Of course, this only applies if the allele fraction is large.  Rather than try to model the count of ref reads within a germline hom alt site, we simply set a threshold of allele fraction 0.9, so that in case (2) $\ell_t \rightarrow {\rm I}[f_t > 0.9] \ell_t$.
and the corrected germline probability is
\begin{equation}
P({\rm germline}) = \frac{(1) + (2)}{(1) + (2) + (3)} = \frac{\left( 2f(1-f) \chi + {\rm I}[f_t > 0.9] f^2 \right) \ell_n (1 - \pi)}{\left( 2f(1-f) \chi + {\rm I}[f_t > 0.9] f^2 \right) \ell_n  (1 - \pi) + (1-f)^2  \pi}.
\end{equation}
To filter, we set a threshold on this posterior probability.

So far we have assumed that the population allele frequency $f$ is known, which is the case if it is found in our germline resource, such as gnomAD.  If $f$ is not known we must make a reasonable guess as follows.  Suppose the prior distribution on $f$ is ${\rm Beta}(\alpha, \beta)$.  The mean $\alpha/(\alpha +\beta)$ of this prior is the average human heterozygosity $\theta \approx 10^{-3}$, so we have $\beta \approx \alpha / \theta$.  We need one more constraint to determine $\alpha$ and $\beta$, and since we are concerned with imputing $f$ when $f$ is small we use a condition based on rare variants.  Specifically, the number of variant alleles $n$ at some site in a germline resource with $N/2$ samples, hence $N$ chromosomes, is given by $f \sim {\rm Beta}(\alpha, \beta), n \sim {\rm Binom}(N,f)$.  That is, $n \sim {\rm BetaBinom}(\alpha, \beta, N)$.  The probability of a site being non-variant in every sample is then $P(n = 0) = {\rm BetaBinom}(0 | \alpha, \beta, N)$, which we equate to the empirical proportion of non-variant sites in our resource, about $7/8$ for exonic sites in gnomAD.  Solving, we obtain approximately $\alpha = 0.01, \beta = 10$ for gnomAD.  Now, given that some allele found by Mutect is not in the resource, the posterior on $f$ is ${\rm Beta}(\alpha, \beta + N)$, the mean of which is, since $\beta << N$, about $\alpha / N$.  By default, Mutect uses this value.

\section{Contamination Filter}\label{contamination-filter}
Suppose our tumor bam has contamination fraction $\alpha$ and that at some site we have $a$ alt reads out of $d$ total reads.  Suppose further that the alt allele has population allele frequency $f$.  We will compute a simple estimate of the posterior probability that these alt reads came from a contaminating sample and not from a true somatic variant.  Let $\pi$ be the prior probability of somatic variation as above.  Our crude model for the alt count distribution of somatic variation is a uniform distribution.  That is, we assume that any value of $a$ from $0$ to $d$ is equally likely.  Then the likelihood of the data given a true somatic variant is
\begin{equation}
P(a | {\rm somatic})  = \frac{1}{d + 1}.
\end{equation}

We consider two models of contamination.  If there are multiple contaminants we approximate each contaminant read as independent.  Then the probability of any given read being an alt contaminant read is $\alpha f$, so we have
\begin{equation}
P(a | {\rm many~contaminant}) = {\rm Binom}(a | d, \alpha f).
\end{equation}
If there is a single contaminating sample it is heterozygous with probability $2f(1-f)$ and homozygous for the alt with probability $f^2$, in which cases fractions $\alpha/2$ and $\alpha$ of all reads to be alt contaminants.  The contaminant is homozygous for the ref with probability $(1-f)^2$, which yields no alt reads. Thus
\begin{equation}
P(a | {\rm one~contaminant}) = 2f(1-f) {\rm Binom}(a | d, \alpha /2) + f^2 {\rm Binom}(a | d, \alpha) + (1-f)^2 {\rm I}[a = 0].
\end{equation}
We take the likelihood $P(a | {\rm contamination})$ to be the maximum of these, which admittedly is not quite rigorous.  Usually one will be overwhelmingly larger than the other, however, so it's a decent approximation.  Our posterior probability of contamination is then
\begin{equation}
P({\rm contamination} | a) = \frac{  P(a, {\rm contamination}) } {P(a, {\rm contamination}) + P(a, {\rm somatic}) } = \frac{  (1-\pi)P(a | {\rm contamination}) } {(1-\pi)P(a | {\rm contamination}) + \pi P(a | {\rm somatic}) }
\end{equation}
We filter by setting a threshold on this posterior probability.

\section{Finding Active Regions}
Mutect triages sites based on their pileup at a single base locus.  If there is sufficient evidence of variation Mutect proceeds with local reassembly and realignment.  As in the downstream parts of Mutect we seek a likelihood ratio between the existence and non-existence of an alt allele.  Instead of obtaining read likelihoods via Pair-HMM, we assign each base a likelihood.  For substitutions we can simply use the base quality.  For indels we assign a heuristic effective quality that increases with length.  Supposing we have an effective quality for each element in the read pileup we can now estimate the likelihoods of no variation and of a true alt allele with allele fraction $f$.  Let $\mathcal{R}$ and $\mathcal{A}$ denote the sets of ref and alt reads.  The likelihood of no variation is the likelihood that every alt read was in error.  Letting $\epsilon_i$ be the error probability of pileup element $i$ we have:

\begin{equation}
L({\rm no~variation}) = \prod_{i \in \mathcal{R}} (1 - \epsilon_i) \prod_{j \in \mathcal{A}} \epsilon_j \approx \prod_{j \in \mathcal{A}} \epsilon_j,
\end{equation}
where the approximation amounts to ignoring the possibility that ref reads are actually alt, or, equivalently, giving each ref read infinite quality.  This is not necessary but it speeds the computation because, as we will see, we will only need to keep alt base qualities in memory.
\begin{equation}
L(f) = \prod_{i \in \mathcal{R}} \left[ (1 -f)(1 - \epsilon_i) + f \epsilon_i \right] \prod_{j \in \mathcal{A}} \left[f(1 - \epsilon_j) + (1 - f) \epsilon_j \right]
\approx (1-f)^{N_{\rm ref}} \prod_{j \in \mathcal{A}} \left[f(1 - \epsilon_j) + (1 - f) \epsilon_j \right],
\end{equation}
where we again assign infinite base quality to ref reads and let $N_{\rm ref} = | \mathcal{R}|$.

This is equivalent to the following model in which we give the $n$th alt read a latent indicator $z_j$ which equals 1 when the read is an error:
\begin{align}
P({\rm reads}, f, \vz) = (1-f)^{N_{\rm ref}} \prod_{n=1}^{N_{\rm alt}} \left[ (1-f)\epsilon_n \right]^{z_n} \left[ f (1 - \epsilon_n) \right]^{1 - z_n}
\end{align}
We will approximate the model evidence $L(f) = \sum_\vz \int \, df P({\rm reads}, f, \vz)$ via a mean field variational Bayes approximation in which we factorize the full data likelihood as $P({\rm reads}, f, \vz) \approx q(f) q(\vz) = q(f) \prod_n q(z_n)$\footnote{The latter step is an induced factorization -- once $f$ and $\vz$ are decoupled, then the different $z_n$ become independent as well.}.  For simplicity and speed, we will not iteratively compute $q(f)$.  Rather, we use the fact that $z_n$ is almost always 0 to see, by inspection, that
\begin{equation}
q(f) \approx {\rm Beta}(f | \alpha, \beta), \quad \alpha = N_{\rm alt} + 1, \beta = N_{\rm ref} + 1.
\end{equation}
Here the ``$+1$"s come from the pseudocounts, one ref and one alt, of a flat prior of $f$.  Then, following the usual recipe of averaging the log likelihood with respect to $f$ and re-exponentiating, we find that $z_n$ ``sees" the following distribution:
\begin{align}
q(z_n) &\propto \left[ \epsilon_n \exp \overline{\ln (1 - f)} \right]^{z_n} \left[ (1 - \epsilon_n) \exp \overline{\ln f} \right]^{1 - z_n} \\
&= \left[ \epsilon_n \rho \right]^{z_n} \left[ (1 - \epsilon_n) \tau \right]^{1 - z_n},
\end{align}
where we have defined the standard Beta distribution moments (with respect to $q(f)$) $\ln \rho \equiv \overline{\ln (1 - f)} = \psi(\beta) - \psi(\alpha + \beta)$ and $\ln \tau \equiv \overline{\ln f} = \psi(\alpha) - \psi(\alpha + \beta)$. By inspection, we see that
\begin{equation}
q(z_n) = {\rm Bernoulli}(z_n | \gamma_n), \quad \gamma_n = \frac{ \rho \epsilon_n}{\rho \epsilon_n + \tau (1 - \epsilon_n)}.
\end{equation}
Then, Equation 10.3 of Bishop gives us the variational lower bound on $L(f)$:
\begin{align}
L(f) &\approx E_q \left[ \ln P({\rm reads}, f, \vz) \right] + {\rm entropy}[q(f)] + \sum_n {\rm entropy}[q(z_n)] \\
&= H(\alpha, \beta) +  N_{\rm ref} \ln \rho + \sum_n \left[ \gamma_n \ln \left( \rho \epsilon_n \right) + (1 - \gamma_n) \ln \left( \tau (1 - \epsilon_n) \right) + H(\gamma_n) \right],
\end{align}
where $H(\alpha, \beta)$ and $H(\gamma)$ are Beta and Bernoulli entropies.  We summarize these steps in the following algorithm:

\begin{algorithm}
\begin{algorithmic}[1]
\State Record the base qualities, hence the error probabilities $\epsilon_n$ of each alt read.
\State $\alpha = N_{\rm alt} + 1$, $\beta = N_{\rm ref} + 1$
\State $\rho = \exp \left( \psi(\beta) - \psi(\alpha + \beta) \right) $, $\tau = \exp \left( \psi(\alpha) - \psi(\alpha + \beta) \right)$.
\State $\gamma_n =  \rho \epsilon_n / \left[ \rho \epsilon_n + \tau (1 - \epsilon_n) \right]$
\State $L(f) \approx H(\alpha, \beta) + N_{\rm ref} \ln \rho + \sum_n \left[ \gamma_n \ln \left( \rho \epsilon_n \right) + (1 - \gamma_n)\ln \left( \tau (1 - \epsilon_n) \right) + H(\gamma_n) \right]$
\end{algorithmic}
%\caption{Pair HMM algorithm}
%\label{pairHMM}
\end{algorithm}
To get the log odds we subtract the log likelihood, $\sum_n \ln \epsilon_n$, from $L(f)$.

\section{Calculating Contamination}
Below, we present the GATK's fast, simple, and accurate method for calculating the contamination of a sample.  This methods does not require a matched normal, makes no assumptions about the number of contaminating samples, and remains accurate even when the sample has a lot of copy number variation.

The inputs to our tool are a bam file and a vcf of common variants -- for example ExAC, gnomAD, or 1000 Genomes -- with their allele frequencies.  The basic idea, which comes from ContEst\footnote{ContEst: estimating cross-contamination of human samples in next-generation sequencing data, \textit{Bioinformatics} \textbf{27}, 2601 (2011)} by Kristian Cibulskis and others in the Broad Institute Cancer Genome Analysis group, is simply to count ref reads at hom alt sites and subtract the number of ref reads expected from sequencing error to obtain the number of ref reads contaminating these hom alt sites.  Finally, we use the allele frequencies to account for the fact that some contaminating reads have the alt allele.  The only subtlety is in distinguishing hom alt sites from loss of heterozygosity events, which we describe below.

Suppose we have a set $\mathbb{H}$ of SNPs at which our sample is homozygous for the alternate allele.  Let $N_{\rm ref}$ be the total number of ref reads at these sites.  We can decompose $N_{\rm ref}$ as follows:
\begin{align}
N_{\rm ref} = N_{\rm ref}^{\rm error} + N_{\rm ref}^{\rm contamination}, \label{decomposition}
\end{align}
where $N_{\rm ref}^{\rm error}$  and $N_{\rm ref}^{\rm contamination}$ are as the number of ref reads due to error and contamination, respectively.  We can obtain $N_{\rm ref}$ by counting reads, and we estimate $N_{\rm ref}^{\rm error}$ as follows.  Suppose, WLOG, that the ref allele is A and the alt is C.  Then, assuming that all substitution errors are equally likely, $N_{\rm ref}^{\rm error}$ is approximately half the number of Gs and Ts.  This is, of course, not a perfect assumption for any one site, but on average over all the sites in $\mathbb{H}$ it is very good.

Next we take the expectation of both sides of Equation \ref{decomposition} to obtain
\begin{align}
\ave{N_{\rm ref} - N_{\rm ref}^{\rm error}} =& \ave{\sum_{s \in \mathbb{H}} {\rm number~of~contaminant~ref~reads~at~}s} \\
=& \sum_{s \in \mathbb{H}} \ave{{\rm number~of~contaminant~ref~reads~at~}s} \\
=& \sum_{s \in \mathbb{H}} \ave{{\rm number~of~contaminant~reads~at~}s \times {\rm ref~fraction~of~contaminant~reads~at~}s} \\
=& \sum_{s \in \mathbb{H}} \ave{{\rm number~of~contaminant~reads~at~}s} \times \ave{{\rm ref~fraction~of~contaminant~reads~at~}s}
\end{align}
where we have used the linearity of the expectation and the independence of the total number of contaminant reads with the fraction of contaminant reads that are ref.  The expectation of the total number of contaminant reads is the depth $d_s$ at site $s$ times the contamination, which we denote by $\chi$.  The expected fraction of contaminant reads that are ref is one minus the alt allele frequency $f_s$.  Crucially, this fact is independent of how many contaminating samples there are.  Thus we have
\begin{align}
\ave{N_{\rm ref} - N_{\rm ref}^{\rm error}} = \chi \sum_{s \in \mathbb{H}} d_s (1 - f_s)
\end{align}
and obtain the estimate
\begin{align}
\hat{\chi} \approx \frac{N_{\rm ref} - N_{\rm ref}^{\rm error}}{\sum_{s \in \mathbb{H}} d_s (1 - f_s)} \label{contamination_estimate}
\end{align}

Let us now roughly estimate the error bars on this result.  The main source of randomness is the stochasticity in the number of contaminating ref reads.  Although the nature of this randomness depends on the number of contaminants, the most variable case, hence an upper bound, is that of a single haploid contaminant, since at each site the only possibilities are the extremes of all contaminant reads being ref or all being alt.  In this case, the contribution to the numerator of Eq. \ref{contamination_estimate} from site $s$ is the random variable $X_sZ_s$, where $X_s \sim {\rm Binom(d_s, \chi)}$ is the number of contaminant reads at $s$ and $Z_s$ is a binary indicator for whether the contaminant reads are ref, with $P(Z_s=1) = 1 - f_s$.  $X$ and $Z$ are independent, so we can work out the variance of $XZ$ as:
\begin{align}
{\rm var}(XZ) =& E[X^2Z^2] - E[XZ]^2 \\
=& (1 - f_s) E[X^2] - (1-f_s)^2 E[X]^2 \\
=& (1 - f_s) \left( {\rm var}(X) + E[X]^2 \right) - (1 - f_s)^2E[X]^2 \\
=& (1 - f_s) d_s \chi(1 - \chi) + f_s(1 - f_s) d_s^2 \chi^2
\end{align}
And therefore the standard error on $\hat{\chi}$ comes out to the square root of the sum of these per-site variances, divided by the denominator of Eq. \ref{contamination_estimate}, that is,
\begin{equation}
{\rm std}(\hat \chi) = \frac{\sqrt{  \sum_s \left[ (1 - f_s) d_s \hat{\chi}(1 - \hat{\chi}) + f_s(1 - f_s) d_s^2 \hat{\chi}^2  \right] }}{\sum_s d_s (1 - f_s) }
\end{equation}

It remains to describe how we determine which sites are hom alt.  The fundamental challenge here is that in cancer samples loss of heterozygosity may cause het sites to look like hom alt sites.  Our strategy is to partition the genome into allelic copy-number segments, then infer the minor allele fraction of those segments.  We segment the genome just as in GATK CNV, using a kernel segmenter with a Gaussian kernel computed on the alt fraction.  A nonlinear kernel is important because each segment is multimodal, with peaks for hom ref, alt minor het, alt major het, and hom alt.

We then perform maximum likelihood estimation MLE on a model with learned parameters $\mu$, the local minor allele fraction for each segment, $\chi$, the contamination, and a constant base error rate parameter $\epsilon$ determined by counting reads that are neither the ref nor primary alt base at biallelic SNPs as described above.  The model likelihood is
\begin{equation}
P(\{ a\}| \{f\}, \chi, \{d\}) =    \prod_{{\rm segments~}n}\prod_{{\rm sites~}s} \sum_{{\rm genotype~}g} P(g|f_s) {\rm Binom}(a_s | d_s, (1 - \chi) \phi(g, \mu_n, \epsilon) + \chi f_s)
\end{equation}
where $a_s$ and $d_s$ are the alt and total read counts at site $s$, allelic CNV genotypes $g$ run over hom ref, alt minor, alt major, and hom alt with priors $P({\rm hom~ref}) = (1-f_s)^2$, $P({\rm alt~minor}) = P({\rm alt~major}) = f_s(1-f_s)$, and $P({\rm hom alt} = f^2$.  $\phi(g, \mu, \epsilon)$ is the alt allele fraction of the uncontaminated sample: $\phi({\rm hom~ref}) = \epsilon$, $\phi({\rm alt~minor}) = \mu$, $\phi({\rm alt~major}) = 1 - \mu$, $\phi({\rm hom~alt}) = 1 - \epsilon$.  The binomial is the weighted average of the uncontaminated sample and sample reads drawn independently from allele frequency $f$.  This is inconsistent with a single diploid contaminant sample, or indeed with any finite number of contaminants, which is why we do not the the MLE estimate in the final output of the tool.  The model also assumes that the uncontaminated and contaminating samples have the same overall depth distribution at each site, which is inconsistent with any differences in copy-number.  We perform the MLE by brute force, alternately maximizing with respect to $\chi$ with $\mu$ fixed and vice versa.  In order to make the solution more robust, we exclude segments with low $\mu$ from the maximization over $\chi$ by taking the highest possible threshold (up to 0.5, of course) for $\mu$ that retains at least $1/4$ of all sites.

Once we have learned the parameters of this model, we can easily infer the posterior probabilities of hom alt genotypes.  We take every site with a posterior probability greater than 0.5.  In order to make the result more reliable against CNVs, we again impose a threshold on segment minor allele fraction and apply the above formula only to hom alt sites in these segments.  This time, however, we choose the highest possible threshold such that the estimated relative error is less than 0.2.

Finally, we note that the same calculation can be reversed by using alt reads in hom ref sites as the signal and replacing $f$ by $1-f$ everywhere above.  We use the estimate from hom refs as a backup when the hom alt estimate has too great an error, as can occur in the case of targeted panels with few sites.  We do not use this as our primary estimate because it is much more affected by uncertainty in the population allele frequencies and is thus susceptible to systematic bias.

\section{Proposed tumor in normal estimation tool}

Note: the following notes are just a proposal for which no GATK tool yet exists.  A popular tool is DeTiN\footnote{DeTiN: overcoming tumor-in-normal contamination, \textit{Nature Methods} \textbf{15}, 531 (2018)} by Amaro Taylor-Weiner and others at the Broad Institute Cancer Genome Analysis group.

Similar to the spirit of CalculateContamination, the fraction of tumor reads in the normal bam is a single number with a large amount of evidence and is probably well-estimated by simple descriptive statistics rather than a full-fledged probabilistic model.  It shouldn't be much more complicated than finding somatic variants and comparing their signal in the normal sample to that in the tumor.

We propose the following steps to obtain our input of confident somatic SNVs:
\begin{itemize}
\item Run Mutect in tumor-only mode to obtain a preliminary list of somatic SNVs.  For the sake of speed, we could implement a pileup-based mode in which we skip reassembly and equate read likelihoods with base qualities.  This would allow us to obtain variant annotations using the existing architecture of Mutect and therefore to filter calls with no new code.  It would probably make sense at this stage to filter more stringently than usual based on population allele frequencies in gnomAD.
\item Run FilterMutectCalls.  With default settings this eliminates the great majority of sequencing artifacts.  To eliminate even more we could increase the log odds threshold slightly, essentially requiring a slightly larger alt allele count.  Normally we don't do this because it sacrifices some sensitivity, but for our purposes here a 10 or 20 percent loss of sensitivity is perfectly acceptable as long as we are left with enough SNVs for our estimate.
\item Remove all SNVs that have enough read counts in the normal that we conclude they are germline variants, as opposed to tumor in normal contamination.  We could also accomplish this by using tumor-normal mode in the first step, but we would need to modify our active region determination which currently would mark a region as inactive based only on a small number of tumor-in-normal alt reads.  We would also have to turn off the normal artifact filter.
\end{itemize}

The above steps are all very reliable, so at this point we can assume we have a collection of confident biallelic somatic SNVs that are hom ref in the germline.  Similar to CalculateContamination, we can now estimate the number of alt reads in the normal at these sites:
\begin{align}
{\rm alt~in~normal} \approx \sum_{\rm sites} ({\rm depth~in~normal}) \times ({\rm alt~fraction~in~tumor}) \times ({\rm tumor~in~normal~fraction})
\end{align}
Hence we estimate
\begin{align}
{\rm tumor~in~normal~fraction} \approx
\frac{\rm total~number~of~alt~reads~in~normal~at~somatic~SNV~sites}
{\sum_{\rm somatic~SNV~sites} ({\rm depth~in~normal}) \times ({\rm alt~fraction~in~tumor})}
\end{align}

\section{Mutect filters}
\code{Mutect2} emits candidate variants with a set of annotations.  After that \code{FilterMutectCalls} produces filtered calls by subjecting these variants to a series of hard filters that reject sites if some annotation is out of an allowable range.  Here are the command line arguments that control these filters, along with the annotations they relate to.

\begin{itemize}
\item  \code{tumor-lod} is the minimum likelihood of an allele as determined by the somatic likelihoods model required to pass.
\item \code{max-events-in-region} is the maximum allowable number of called variants co-occurring in a single assembly region.  If the number of called variants exceeds this they will all be filtered.
\item \code{unique-alt-read-count} is the minimum number of unique (start position, fragment length) pairs required to make a call.  This count is a proxy for the number of unique molecules (as opposed to PCR duplicates) supporting an allele.  Normally PCR duplicates are marked and filtered by the GATK engine, but in UMI-aware calling this may not be the case, hence the need for this filter.
\item \code{max-alt-allele-count} is the maximum allowable number of alt alleles at a site.  By default only biallelic variants pass the filter.
\item \code{max-germline-posterior} is the maximum posterior probability, as determined by the above germline probability model, that a variant is a germline event.
\item \code{normal-artifact-lod} is the maximum acceptable likelihood of an allele in the normal \textit{by the somatic likelihoods model}.  This is different from the normal likelihood that goes into the germline model, which makes a diploid assumption.  Here we compute the normal likelihood as if it were a tumor in order to detect artifacts.
\item \code{max-strand-artifact-probability} is the posterior probability of a strand artifact, as determined by the model described above, required to apply the strand artifact filter.  This is necessary but not sufficient -- we also require the estimated max a posteriori allele fraction to be less than \code{min-strand-artifact-allele-fraction}.  The second condition prevents filtering real variants that also have significant strand bias, i.e. a true variant that \textit{also} has some artifactual reads.
\item \code{min-median-base-quality} is the minimum median base quality of bases supporting a SNV.
\item \code{min-median-mapping-quality} is the minimum median mapping quality of reads supporting an allele.
\item \code{max-median-fragment-length-difference} is the maximum difference between the median fragment lengths reads supporting alt and reference alleles.  Note that fragment length is based on where paired reads are mapped, not the actual physical fragment length.
\item \code{min-median-read-position} is the minimum median length of bases supporting an allele from the closest end of the read.  Indels positions are measured by the end farthest from the end of the read.
\item If \code{FilterMutectCalls} is passed a \code{contamination-table} from \code{CalculateContamination} it will filter alleles with allele fraction less than the whole-bam contamination in the table\footnote{This should be made more sophisticated by integrating the possibility of contamination into the germline model.}.
\end{itemize}

Additionally, there are two unadjustable filters. the panel of normals filter removes all alleles at a site belonging to the panel of normals, which is a vcf of blacklisted artifact sites.  It can be disabled by not passing a panel of normals to \code{Mutect2}.  There is also an STR contraction filter which removes variants that are the deletion of a single repeat unit of an STR when this repeat unit contains more than one base.  This filter can be disabled with the argument \code{-XA TandemRepeat} which turns off the \code{TandemRepeat} annotation.

Here for convenience is a table of \code{Mutect2} filters with their corresponding annotations specified by the \code{-A} argument\footnote{Most of these are default annotations and do not need to be invoked explicitly.}, vcf keys for these annotations, and command line arguments controlling filtering thresholds.

\begin{table}[h!]
\centering
 \begin{tabular}{|| c c c c ||}
 \hline
 Filter & Annotation & Key & Argument \\ [0.5ex]
 \hline\hline
 \code{t\_lod} & - &\code{TLOD} & \code{tumor\_lod} \\
 \code{clustered\_events} & - & \code{ECNT} & \code{max-events-in-region} \\
 \code{duplicate\_evidence} & \code{UniqueAltReadCount} & \code{UNIQ\_ALT\_READ\_COUNT} & \code{unique-alt-read-count} \\
 \code{multiallelic} & - & - & \code{max-alt-alleles-count} \\
 \code{germline\_risk} & - & \code{P\_GERMLINE} & \code{max-germline-posterior} \\
 \code{artifact\_in\_normal} & - & \code{N\_ART\_LOD} & \code{normal-artifact-lod} \\
 \code{strand\_artifact} & \code{StrandArtifact} & \code{SA\_POST\_PROB}, \code{SA\_MAX\_AF} & \code{max-strand-artifact-probability} \\
 \code{base\_quality} & \code{BaseQuality} & \code{MBQ} & \code{min-median-base-quality} \\
 \code{mapping\_quality} & \code{MappingQuality} & \code{MMQ} & \code{min-median-mapping-quality} \\
 \code{fragment\_length} & \code{FragmentLength} & \code{MFRL} & \code{max-median-fragment-length-difference} \\
 \code{read\_position} & \code{ReadPosition} & \code{MPOS} &\code{min-median-read-position} \\
 \code{panel\_of\_normals} & - & \code{IN\_PON} & \code{panel-of-normals} \\
 \code{contamination} & - & - & \code{contamination-table} \\
 \code{str\_contraction} & \code{TandemRepeat} & \code{RU}, \code{RPA} & - \\  [1ex]
 \hline
 \end{tabular}
\end{table}

\section{Read Orientation Artifact Filter}

The read orientation artifact, also known as the orientation bias artifact, arises due to a chemical change in the nucleotide during library prep that results in, for example, G base-paring with A. This kind of artifact has a clear signature (e.g. C to A SNP that occurs predominantly for the middle C in the DNA sequence CCG), and it's single-stranded in nature. Downstream, this artifact manifests as low allele fraction SNPs whose evidence for the alt allele consists almost entirely F1R2 reads or F2R1 reads. A read pair is F1R2 (forward 1st, reverse 2nd) if the sequence of bases in Read 1 maps to the forward strand of the reference (F1), and the sequence of Read 2 to the reverse strand of the reference (R2). F2R1 is defined similarly.

Without loss of generality, suppose that the reference context at locus $i$ is ACT. Let $\vz_i$ denote the genotype at locus $i$ with the one-hot encoding $z_{ik} = 1$ iff the genotype of locus $i$ is $k$, where the possible genotypes are
\begin{equation*}
z_i \in \{ \text{F1R2}_A, \text{F1R2}_G, \text{F1R2}_T, \text{F2R1}_A, \text{F2R1}_G, \text{F2R1}_T,  \text{Hom Ref}, \text{Germline Het}, \text{Somatic Het}, \text{Hom Var} \}
\end{equation*}
$z_i = \mathrm{F1R2_A}$ denotes that at locus $i$ we have an artifact in which the evidence for alt allele A consists entirely of reads in the F1R2 orientation. The remaining artifact states are defined analogously. Let $\vpi$ denote the prior probabilities of the $\vz_i$ under the reference context ACT. Then we have
\begin{equation}
P(\vz_i) = \prod_k \pi_{k}^{z_{ik}}
\end{equation}

The number of alt reads at a locus depends on the genotype $z_i$. Let $n_i$ and $m_i$ denote the total depth and alt depth at locus $i$, respectively. The conditional distribution of $m_i$ is
\begin{equation}
P(m_i | z_{ik} = 1) = \mathrm{BetaBinomial}(m_i | n_i, \alpha_k, \beta_k)
\end{equation}
where $\alpha_k$ and $\beta_k$ are fixed hyperparameters for genotype $z_k$. When the site's genotype indicates in $m_i$ alt reads we expect a heavily skewed distribution of F1R2 reads. This is captured in the conditional distribution of F1R2 alt reads. Let $c_i$ denote the number of F1R2 reads among the $m_i$ alt reads at locus $i$. Then we have
\begin{equation}
P(c_i | m_i, z_{ik} = 1) = \mathrm{BetaBinomial}( c_i | m_i, \alpha'_k, \beta'_k)
\end{equation}
We learn the prior artifact probabilities $\vpi$ based on the observed values of $n_i$, $m_i$, $c_i$ for each of $N$ loci using the EM algorithm. In the E-step, we compute the posterior probabilities of $\vz_i$ for $i = 1 ... N$. The joint probabilities of $\vz$ factorizes over $i$, thus the posteriors over $\vz$ are independent across loci.
\begin{equation}
P(z_{ik} = 1 | m_i, c_i) \propto P(z_{ik} = 1, m_i, c_i) = \pi_{k} \mathrm{BetaBinomial}(m_i | n_i, \alpha_k, \beta_k)  \mathrm{BetaBinomial}( c_i | m_i, \alpha'_k, \beta'_k)
\end{equation}
In the M-step we maximize the expectation of the log complete-data likelihood with respect to $\vpi$. The log complete data likelihood is given as
\begin{equation}
\ln P(\vz, \vm, \vc) = \sum_i \sum_k z_{ik} \big( \ln \pi_{k} + \ln \mathrm{BetaBinomial} ( m_i | n_i, \alpha_k, \beta_k)  + \ln \mathrm{BetaBinomial}( c_i | m_i, \alpha'_k, \beta'_k) \big)
\end{equation}
Maximizing the log likelihood under the constraint $\sum_k \pi_k = 1$ gives us
\begin{equation}
\pi_k = \frac{N_k}{N}
\end{equation}
where $N_k = \sum_i P(z_{ik} | m_i, c_i)$ is the effective count of loci with genotype $k$. We alternate E-step and M-step until convergence. We then use the learned prior genotype probabilities to compute the posterior artifact probabilities of variants in a vcf. The filtering threshold is set such that the false discovery rate doesn't exceed a specified value, as described below.

\section{Filtering False Discovery Rate}

Let $p_1 \leq p_2 \leq ... \leq p_n$ denote ordered posterior probabilities that given variants are sequencing artifacts. Suppose we filter variants for which $p_i > p_j$ for some index $j$; that is, we keep the first $j$ variants as \code{PASS} and filter the rest. Then the expected number of false positives is
\begin{equation}
\mathbb{E} [ \mathrm{FP}(j)] = \sum_{i = 1}^{j} p_i
\end{equation}
And the expected false positive rate is
\begin{equation} \label{eq:fpr}
\mathbb{E} [ \mathrm{FPR}(j)]= \frac{1}{j} {\sum_{i = 1}^{j} p_i}
\end{equation}
We choose the threshold so as to maximize the number of variants to let through while keeping the false positive rate below $\delta \in [0, 1]$. In other words, we solve for
\begin{equation}
j^* = \mathrm{argmax}_j \mathbb{E} [ \mathrm{FPR}(j)]= \frac{1}{j} \sum_{i = 1}^{j} p_i
\end{equation}
under the constraint $ \mathbb{E} [ \mathrm{FPR}(j)] < \delta$. We can prove by induction that $\mathbb{E} [ \mathrm{FPR}(j)]$ is monotonically increasing in $j$. Thus we test $\mathbb{E} [ \mathrm{FPR}(j)]$ for $j = 1, \cdots, k$, where $k$ is the smallest integer such that $\mathbb{E} [ \rm{FPR}(k)] > \delta$, and set $j^* = k-1$.

\end{document}


\section{Programming Markov Chain with Pig}

\begin{lstlisting}[language=pig,caption=Markov Chain in Pig Latin,label=pigmarkovstructure,columns=fullflexible]
wikipedia = LOAD 'wikipedia' USING PigStorage('\t') AS (row:chararray);
tuples = FOREACH wikipedia GENERATE FLATTEN(splitsuc.SPLITSUC(*));
grouped = GROUP tuples by (word1, word2, word3, word4) PARALLEL 16;
grouped_counted = FOREACH grouped GENERATE group, COUNT(tuples);
STORE grouped_counted INTO 'wikipedia.sql' USING splitsuc.STORESQL();
\end{lstlisting}

For the implementation two UDFs had to be programmed.
\begin{itemize}
\item The first one is {\tt SPLITSUC()}, which is called within a {\tt FOREACH} loop, processing each row of the input. It is splitting the rows into 4-word tuples with a tuple containing every word with its three predecessors.
\item The other one is {\tt STORESQL()}, that is transforming the 4-tuples into SQL Multi-INSERT statements ready to be imported into a database. The number of output files is determined by the number of reduce jobs.
\end{itemize}

These UDFs each are about 80 lines of code and consumed most of the development time needed for implementing the algorithm. Writing the Pig Latin code was a fairly easy task, since the necessary steps were straight-forward: \\
{\bf 1.} Split into 4-tuples\\{\bf 2.} Group by 4-tuples\\{\bf 3}. Count the occurrences of each 4-tuple


\section{Implementation}
We have implemented the pipeline using python. Information about installation and usage can be found in \texttt{README.MD}.

Reporting of statistics other than the resulting probabilities only happens with our pipeline when MiniC2D is selected as model counter (flag --model\_counter minic2d).

\section{Pipeline with previous tasks}
\subsection{Cancer Bayesian network}
\begin{itemize}
    \item Probabilities:
    \begin{lstlisting}
cancer           : 0.01163
dyspnoea         : 0.3040705
pollution("high"): 0.1
pollution("low") : 0.9
smoker           : 0.3
xray("negative") : 0.7918589999999999
xray("positive") : 0.208141
    \end{lstlisting}
    \item Total runtime: 0.594s
    \item Runtime of the separate parts:
    \begin{lstlisting}
Time to parse ground problog:  0.011s
Time to convert ground problog to first order logic:  0.000s
Time to convert first order logic to CNF:  0.002s
Time for all model counting:  0.58s
    \end{lstlisting}
    \item Number of variables and lines in CNF: 17, 77
    \item Depth of vtree: 11
    \item Number of edges and nodes in the circuit: 81, 94
\end{itemize}
\subsection{Monty Hall}
\begin{itemize}
    \item Probabilities:
    \begin{lstlisting}
prize(1)      : 0.3333333333333333
prize(2)      : 0.3333333333333333
prize(3)      : 0.3333333333333333
select_door(1): 1.0
win_keep      : 0.3333333333333333
win_switch    : 0.6666666666666666
    \end{lstlisting}
    \item Total runtime: 0.469s
    \item Runtime of the separate parts:
    \begin{lstlisting}
Time to parse ground problog:  0.011s
Time to convert ground problog to first order logic:  0.0s
Time to convert first order logic to CNF:  0.001s
Time for all model counting:  0.458s
    \end{lstlisting}
    \item Number of variables and lines in CNF: 10, 26
    \item Depth of vtree: 8
    \item Number of edges and nodes in the circuit: 52, 44
\end{itemize}

\section{Pipeline on Bayesian learning example}
\begin{itemize}
    \item Probabilities:
    \begin{lstlisting}
weight(c1, 0.1): 0.0
weight(c1, 0.3): 2.167977370376305e-06
weight(c1, 0.5): 0.004149743263716653
weight(c1, 0.7): 0.13174850211546205
weight(c1, 0.9): 0.8640995866434509
weight(c2, 0.1): 3.227661043218289e-06
weight(c2, 0.3): 0.02410999702877393
weight(c2, 0.5): 0.6672475437022279
weight(c2, 0.7): 0.3062862583084689
weight(c2, 0.9): 0.0023529732994859343
    \end{lstlisting}

    \item Total runtime: 3.633s
    \item Runtime of the separate parts:
    \begin{lstlisting}
Time to parse ground problog:  0.338s
Time to convert ground problog to first order logic:  0.024s
Time to convert first order logic to CNF:  0.088s
Time for all model counting:  3.183s
    \end{lstlisting}

    \item Number of variables and lines in CNF: 192, 1062
    \item Depth of vtree: 95
    \item Number of edges and nodes in the circuit: 646, 525
\end{itemize}

\section{Pipeline on alarm Bayesian network}
We had to drop many nodes in order for the pipeline to be able to process the ALARM Bayesian network. This is due to how we implemented the conversion from First Order Logic to CNF. We have come across methods with better performance, but were out of time to try other implementations. Only 21 of the 37 nodes are left in the network.

We used the pipeline to convert files/networks/alarm.net to CNF with the following command.
\begin{lstlisting}
python3 scripts/inference.py --bayesian_network files/networks/alarm.net
\end{lstlisting}
We kept dropping leaf nodes until a CNF was created within reasonable time (a couple seconds). Then, we used the ground problog file in the output of the pipeline and stored it in files/problog/alarm.pl. Finally, to this file, we added queries for all the nodes in the network.

The probabilities shown below are only those of the nodes lower in the network. To see all of the probabilities, run:
\begin{lstlisting}
python3 scripts/inference.py --problog files/problog/alarm.pl
\end{lstlisting}
\begin{itemize}
    \item Probabilities:
    \begin{lstlisting}
cVP("HIGH")           : 0.1541757517134
cVP("LOW")            : 0.1129475165156
cVP("NORMAL")         : 0.7160774170523999
lVEDVOLUME("HIGH")    : 0.2095
lVEDVOLUME("LOW")     : 0.0886
lVEDVOLUME("NORMAL")  : 0.7019
pAP("HIGH")           : 0.0575
pAP("LOW")            : 0.0496
pAP("NORMAL")         : 0.8929
pCWP("HIGH")          : 0.2064165610039
pCWP("LOW")           : 0.1129475165156
pCWP("NORMAL")        : 0.6755051316624
sHUNT("HIGH")         : 0.103095
sHUNT("NORMAL")       : 0.8969050000000001
sTROKEVOLUME("HIGH")  : 0.0404
sTROKEVOLUME("LOW")   : 0.1808
sTROKEVOLUME("NORMAL"): 0.7787999999999999
tPR("HIGH")           : 0.2971
tPR("LOW")            : 0.3068
tPR("NORMAL")         : 0.3961
vENTMACH("HIGH")      : 0.056
vENTMACH("LOW")       : 0.056
vENTMACH("NORMAL")    : 0.838
vENTMACH("ZERO")      : 0.05
    \end{lstlisting}
    \item Total runtime: 6.413s
    \item Runtime of the separate parts:
    \begin{lstlisting}
Time to parse ground problog:  0.037s
Time to convert ground problog to first order logic:  0.003s
Time to convert first order logic to CNF:  0.148s
Time for all model counting:  6.225s
    \end{lstlisting}
    \item Number of variables and lines in CNF: 112, 1118
    \item Depth of vtree: 27
    \item Number of edges and nodes in the circuit: 974, 711
\end{itemize}

\section{Task 2.4}


\section{Putting Our Homemade Climate Model Through Its Paces}
Big stream this stream as we got some working code! Always great when stuff works. This stream, we tackled the radiation problem, added axial tilt to the planet, fixed vertical motion (but not
advection), added stratospheric heating and some other code clean up stuff. This means that the big rework is getting closer and closer. How exciting!
%TESTSSSS WE GOT WORKING CODE! HELLO WORLD!

\subsection{Fixing Up the Code}
First thing to mention is that vertical advection is still broken. Why? Because the gradient in the $z$ direction is broken. This is due to finite differencing on an exponential function. The way
we calculate the differenc from one layer to the other is by differencing them (subtracting) which is always finite. Therefore we always get some inaccuracies. Usually that is fine, but with an
exponential function the differences, you guessed it, become exponentially wrong. As such, the function would eventually be so far off that the model would blow up. So we need to fix it. To
prevent a blow up, we have disabled the call to the gradient $z$ funciton in \autoref{alg:divergence layer}. This ensures that the horizontal bits still work, but the vertical stuff does not.
As always, we will try to fix this in a future stream.

We also fixed up the radiation scheme, as shown in \autoref{alg:optical depth}. Basically we had the definition of $U[k + 1] = \text{something something} U[k + 1]$. This means that the definition
was relying on itself, which is obviously impossible and wrong. So we changed it to it's current form and it is fixed, hooray!

Vertical motion has also been fixed, as shown in \autoref{alg:velocity}. Due to some error in the representation of the vertical motion it did not work. So we changed from that representation to
another. Now the vertical velocity is proportional to the rate of change of the pressure which does work like it should.

\subsection{Tilting the Planet}
In order to model a planet that has seasons, like Earth, we need to tilt the planet. This has as effect that the sun is not always directly above the equator but is above a certain band around
the equator as the year moves on. This means that some hemispheres receive more/less sun based on what part of the year it is. Which corresponds to the various seasons we have on Earth. But in
order to do that, we have to change the \texttt{solar} function. The new version as shown in \autoref{alg:solar tilt} will replace \autoref{alg:solar}. Here $\alpha$ is the tilt in degrees.

\begin{algorithm}
    \SetKwInput{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{insolation $ins$, latitude $lat$, longitude $lon$, time $t$, time in a day $d$}
    \Output{Amount of energy $S$ that hits the planet surface at the given latitude, longitude and time combination.}
    $sun\_lon \leftarrow -t \text{ mod } d$ \;
    $sun\_lon \leftarrow sun\_lon \cdot \frac{360}{d}$ \;
    $sun\_lat \leftarrow \alpha\cos(\frac{2t\pi}{year})$ \;
    $S \leftarrow insolation\cos(\frac{\pi(lat - sun\_lat)}{180})$ \;

    \uIf{$S < 0$}{
        \Return{$0$} \;
    } \uElse {
        $lon\_diff \leftarrow lon - sun\_lon$ \;
        $S \leftarrow S\cos(\frac{lon\_diff\pi}{180})$ \;

        \uIf{$S < 0$}{
            \uIf{$lat + sun\_lat > 90$ or $lat + sun\_lat < -90$}{
                \Return{$insolation\cos(\frac{\pi(lat + sun\_lat)}{180})\cos(\frac{lon\_diff\pi}{180})$} \;
            } \uElse {
                \Return{$0$} \;
            }
        } \uElse {
            \Return{$S$} \;
        }
    }
    \caption{Calculating the energy from the sun (or similar star) that reaches a part of the planet surface at a given latitude and time}
    \label{alg:solar tilt}
\end{algorithm}

What the code in \autoref{alg:solar tilt} does boils down to calculating the latitude and longitude of the sun and checking whether the planet receives any energy. If not return $0$ immediately.
If so we check if the difference between the sun's longitude and the planet's longitude and calculate how much energy would hit the planet given that the sun is not straight above the equator.
We do this by multiplying the energy it would receive from the sun if it were above the equator $S$ by the cosine of the difference in longitudes, which represents the tilt. Then we check again
if the planet is receiving energy, if not we check if it happens around the poles. We do this because due to the tilt it can be the case that at certain points in the year the pole is in constant
sunlight, i.e. the sun does not go down. This creates a sort of overshoot which needs to be accounted for. If it does this then we add the latitudes of the sun and the planet together and use
that to calculate the energy that would hit that spot. If it is not the case that we are around the poles and we do not receive energy, then we return $0$. If it happens to be that we do receive
energy (so no negative values) then we return $S$.

\subsection{Adding In Some Ozone (Or Something Else That Approximates It)}
Adding in ozone in the stratosphere is hella complicated, so we leave that as an exercise to the reader as in true academic fashion. Just joking, if you want you can work on implementing ozone
however we opt not to because it is quite complicated. Instead we approximate it, which is decent enough for our purpose. We need to do it in \autoref{alg:optical depth} as we need to adjust the
$Q$. We add in a check to see if we are currently calculating the radiation in the stratosphere. If so we add some radiation extra to replicate the effect of ozone. As can be seen in
\autoref{alg:ozone}, where we only focus on the $Q$ part of \autoref{alg:optical depth}, we add in some extra radiation based on how high the current layer calculation is, which scales with the
height.

\begin{algorithm}
    \For{$level \in [0, nlevels]$}{
        $Q[level] \leftarrow - \frac{S_z(U - D, 0, 0, level)}{10^3 \cdot densityProfile[level]}$ \;
        \uIf{$heights[level] > 20 \cdot 10^3$}{
            $Q[level] \leftarrow Q[level] + \texttt{solar}(5, lat, lon, t) \frac{24 \cdot 60 \cdot 60(\frac{heights[level] - 20 \cdot 10^3}{10^3})^2}{30^2}$ \;
        }
    }
    \caption{Replicating the effect of ozone}
    \label{alg:ozone}
\end{algorithm}

It is at this point that we reached the state that CLAuDE is in a testable state. This means that we have the model working in such a way that we can do some simple experiments like altering how
long a day is, what would happen if the sun would send out more energy (which usually means that it is bigger) or what would happen if you tidally lock a planet (stop it rotating completely).


%\documentclass[12pt]{article}
\documentclass[12pt,landscape]{article}

\include{preamble}

\newcommand{\instr}{\small Your answer will consist of a string (e.g. \texttt{aebgd}) where the order of the letters does not matter nor does upper / lowercase. \normalsize}

\title{Math 369 / 650 Fall \the\year{} \\ Midterm Examination One}
\author{Professor Adam Kapelner}

\date{Wednesday, September 30, \the\year{}}

\begin{document}
\maketitle

%\noindent Full Name \line(1,0){410}

\thispagestyle{empty}

\section*{Code of Academic Integrity}

\footnotesize
Since the college is an academic community, its fundamental purpose is the pursuit of knowledge. Essential to the success of this educational mission is a commitment to the principles of academic integrity. Every member of the college community is responsible for upholding the highest standards of honesty at all times. Students, as members of the community, are also responsible for adhering to the principles and spirit of the following Code of Academic Integrity.

Activities that have the effect or intention of interfering with education, pursuit of knowledge, or fair evaluation of a student's performance are prohibited. Examples of such activities include but are not limited to the following definitions:

\paragraph{Cheating} Using or attempting to use unauthorized assistance, material, or study aids in examinations or other academic work or preventing, or attempting to prevent, another from using authorized assistance, material, or study aids. Example: using an unauthorized cheat sheet in a quiz or exam, altering a graded exam and resubmitting it for a better grade, etc.
\\

\noindent By taking this exam, you acknowledge and agree to uphold this Code of Academic Integrity. \\

%\begin{center}
%\line(1,0){250} ~~~ \line(1,0){100}\\
%~~~~~~~~~~~~~~~~~~~~~signature~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ date
%\end{center}

\normalsize

\section*{Instructions}

This exam is 75 minutes (variable time per question) and closed-book. You are allowed \textbf{one} page (front and back) of a \qu{cheat sheet}, blank scrap paper and a graphing calculator. Please read the questions carefully. No food is allowed, only drinks. %If the question reads \qu{compute,} this means the solution will be a number otherwise you can leave the answer in \textit{any} widely accepted mathematical notation which could be resolved to an exact or approximate number with the use of a computer. I advise you to skip problems marked \qu{[Extra Credit]} until you have finished the other questions on the exam, then loop back and plug in all the holes. I also advise you to use pencil. The exam is 100 points total plus extra credit. Partial credit will be granted for incomplete answers on most of the questions. \fbox{Box} in your final answers. Good luck!

\pagebreak

\problem\timedsection{8} The following are questions about testing and power.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{9}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}
%\item Type I errors are only possible if $H_0$ is true
%\item Type I errors are only possible if $H_a$ is true
\item Type II errors are only possible if $H_0$ is true
\item Type II errors are only possible if $H_a$ is true
\item If you set a higher $\alpha$, the probability of making a Type II error increases (if $H_a$ is true)
\item If you set a higher $\alpha$, the probability of making a Type II error decreases (if $H_a$ is true)
\item As $n$ increases, the probability of making a Type I error increases (if $H_0$ is true)
\item As $n$ increases, the probability of making a Type II error increases (if $H_a$ is true)
%\item If the standard deviation of the sampling distribution increases, the power increases
%\item If the standard deviation of the sampling distribution increases, the power decreases
\item A lower $\alpha$ setting makes the p-value come out larger.
\item A lower $\alpha$ setting makes null hypothesis rejections more \qu{statistically significant}.

\item Imagine you are doing a two-tailed two-sample (or two-proportion) test. Let \qu{effect size} denote $\theta_1 - \theta_2$. If you are trying to prove a large effect size, the power is higher than if you trying to prove a small effect size.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{5} The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the company known to be the best national lab rat supplier.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{7}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}
\item The 30 rats is likely a representative sample of all rats if the sampling was done by the supplier via simple random sampling.
\item The 30 rats is likely a representative sample of all brown rats if the sampling was done by the supplier via simple random sampling.
\item The 30 rats is likely a representative sample of all Sprague-Dawley rats if the sampling was done by the supplier via simple random sampling.
%\item The 30 rats is definitely representative of all Sprague-Dawley rats even if the sampling was done by the supplier \emph{without} simple random sampling.
\item The 30 rats could be representative of all Sprague-Dawley rats even if the sampling was done by the supplier \emph{without} simple random sampling.
\item The population size is $N=30$.
\item The population size is infinite.
\item There is no definite population, but you assume a population and consider this population to be infinite if you invoke the population sampling assumption.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{5} Same as before. \ingray{The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the rat supplier.} Animal studies are frequently done on rats since rats are considered a model for humans. Consider a nutritional study that tests the effect of magnesium supplementation on cardiovascular disease. In this study, the $n=30$ rats are given 10mg of magnesium daily. Since the lifespan of rats is on average 2 years, this multi-year study waits until all the rats die until they do the data collection. The data is whether or not each rat had heart problems during their life (i.e. each rat either \emph{did have} a heart problem or \emph{did not have} a heart problem).

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{10}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}
\item The data collected is commonly denoted $x_1, x_2, \ldots, x_{30}$.
\item The data collected is commonly denoted $X_1, X_2, \ldots, X_{30}$.
\item The DGP is most likely $\iid \normnot{\theta}{\sigsq}$ and $\theta$ measures how long the rats live.
\item The DGP is most likely $\iid \normnot{\theta}{\sigsq}$ and $\theta$ measures the probability of heart problems.
\item The DGP is most likely $\iid \bernoulli{\theta}$ and $\theta$ is mean life length measured in years.
\item The DGP is most likely $\iid \bernoulli{\theta}$ and $\theta$ is the probability of at least one lifetime heart problem.
\item The DGP is most likely hypergeometric and thus the rat measurements are dependent.
\item The DGP is most likely $\iid$ with mean 10mg.
\item The DGP is most likely $\iid$ with mean 2yr.
\item The researcher's intent is most likely to use the data to make inference about population or DGP parameter(s).
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{4} Same as before. \ingray{The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the rat supplier. Animal studies are frequently done on rats since rats are considered a model for humans. Consider a nutritional study that tests the effect of magnesium supplementation on cardiovascular disease. In this study, the $n=30$ rats are given 10mg of magnesium daily. Since the lifespan of rats is on average 2 years, this multi-year study waits until all the rats die until they do the data collection. The data is whether or not each rat had heart problems during their life (i.e. each rat either \emph{did have} a heart problem or \emph{did not have} a heart problem).} Thus the DGP is $\iid \bernoulli{\theta}$ and $\theta$ is the probability of at least one heart problem and we denote the data $x_1, x_2, \ldots, x_{30}$.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{6}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}
\item $\theta$ is a parameter of the DGP.
\item $\theta$ is a realization from a rv.
\item $\theta$ is a point estimate.
\item The value of $\theta$ is known before the study begins and this study will only confirm it.
\item The value of $\theta$ is unknown before the study begins.
\item The value of $\theta$ is unknown before the study begins but will be known after the study is over as that is the purpose of this study.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{6} Same as before. \ingray{The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the rat supplier. Animal studies are frequently done on rats since rats are considered a model for humans. Consider a nutritional study that tests the effect of magnesium supplementation on cardiovascular disease. In this study, the $n=30$ rats are given 10mg of magnesium daily. Since the lifespan of rats is on average 2 years, this multi-year study waits until all the rats die until they do the data collection. The data is whether or not each rat had heart problems during their life (i.e. each rat either \emph{did have} a heart problem or \emph{did not have} a heart problem).} Thus the DGP is $\iid \bernoulli{\theta}$ and $\theta$ is the probability of at least one heart problem and we denote the data $x_1, x_2, \ldots, x_{30}$.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{9}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}
\item We can use the data to compute a point estimate $\thetahathat$ which is the best numeric guess of the value of $\theta$.
\item The point estimate of $\theta$ is a realization from the rv denoted $X$.
\item The point estimate of $\theta$ is a realization from the rv denoted $\thetahat$.
\item The point estimate of $\theta$ is a realization from the sampling distribution.
\item To compute the point estimate of $\theta$, you need to presuppose an $H_a$.
\item A reasonable point estimate of $\theta$ is the proportion of $x_i$'s that are equal to one.
\item A reasonable point estimate of $\theta$ is the proportion of $x_i$'s that are equal to zero.
\item A reasonable point estimate of $\theta$ is $\xbar$.
\item A reasonable point estimate of $\theta$ is $\hat{\sigma}^2$.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{11} Same as before. \ingray{The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the rat supplier. Animal studies are frequently done on rats since rats are considered a model for humans. Consider a nutritional study that tests the effect of magnesium supplementation on cardiovascular disease. In this study, the $n=30$ rats are given 10mg of magnesium daily. Since the lifespan of rats is on average 2 years, this multi-year study waits until all the rats die until they do the data collection. The data is whether or not each rat had heart problems during their life (i.e. each rat either \emph{did have} a heart problem or \emph{did not have} a heart problem). Thus the DGP is $\iid \bernoulli{\theta}$ and $\theta$ is the probability of at least one heart problem and we denote the data $x_1, x_2, \ldots, x_{30}$.} The point estimate we will use for $\theta$ is $\xbar$ and the estimator is $\Xbar$.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{12}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}
\item The estimator is biased.
\item The estimator is asymptotically unbiased.
\item The estimator is unbiased.
\item The estimator has an MSE of zero for some values of $\theta \in (0, 1)$.
\item The largest MSE of the estimator is $1/(4n)$.
\item Consider $\ell(\thetahathat, \theta) = 0$ if $\thetahathat = \theta$ and 1 otherwise. This is a legal loss function.
\item The loss function in (f) is a reasonable loss function that you can use to compare other estimators to $\Xbar$.
\item The risk under the loss function in (f) is equal to the variance.
\item Consider $\ell(\thetahathat, \theta) = |\thetahathat - \theta|$. This is a legal loss function.
\item The loss function in (i) is a reasonable loss function that you can use to compare other estimators to $\Xbar$.
\item The risk under the loss function in (i) is equal to the variance.
\item The estimator $\thetahat = \half\parens{\max{x_1, x_2, \ldots, x_{30}} + \min{x_1, x_2, \ldots, x_{30}}}$ will have similar MSE to $\Xbar$.
%\item The sampling distribution is normally distributed.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{4} Same as before. \ingray{\footnotesize The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the rat supplier. Animal studies are frequently done on rats since rats are considered a model for humans. Consider a nutritional study that tests the effect of magnesium supplementation on cardiovascular disease. In this study, the $n=30$ rats are given 10mg of magnesium daily. Since the lifespan of rats is on average 2 years, this multi-year study waits until all the rats die until they do the data collection. The data is whether or not each rat had heart problems during their life (i.e. each rat either \emph{did have} a heart problem or \emph{did not have} a heart problem). \normalsize Thus the DGP is $\iid \bernoulli{\theta}$ and $\theta$ is the probability of at least one heart problem and we denote the data $x_1, x_2, \ldots, x_{30}$. The point estimate we will use for $\theta$ is $\xbar$ and the estimator is $\Xbar$.} We wish to prove that the incidence of heart problems in the rats given magnesium \emph{is less than} 48\% (the national average for heart problems in the American adult population).

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{11}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}

\item $H_a: \theta < 0.48$
\item $H_a: \theta > 0.48$
\item $H_a: \theta \neq 0.48$
\item $H_0: \theta \leq 0.48$
\item $H_0: \theta \geq 0.48$
\item $H_0: \theta = 0.48$
\item $\alpha = 5\%$ is the scientific community's standard.
\item $\alpha = 2.5\%$ in the left tail is the scientific community's standard.
\item A target power of $1$ is desirable and achievable.
\item A target power of $1 - \alpha$ is desirable and achievable.
\item A target power of $\alpha$ is desirable and achievable.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{7} Same as before. \ingray{\footnotesize The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the rat supplier. Animal studies are frequently done on rats since rats are considered a model for humans. Consider a nutritional study that tests the effect of magnesium supplementation on cardiovascular disease. In this study, the $n=30$ rats are given 10mg of magnesium daily. Since the lifespan of rats is on average 2 years, this multi-year study waits until all the rats die until they do the data collection. The data is whether or not each rat had heart problems during their life (i.e. each rat either \emph{did have} a heart problem or \emph{did not have} a heart problem).  Thus the DGP is $\iid \bernoulli{\theta}$ and $\theta$ is the probability of at least one heart problem and we denote the data $x_1, x_2, \ldots, x_{30}$. The point estimate we will use for $\theta$ is $\xbar$ and the estimator is $\Xbar$.\normalsize We wish to prove that the incidence of heart problems in the rats given magnesium \emph{is less than} 48\%, the national average for heart problems in the American adult population.} Thus $H_a: \theta < 0.48$ and $H_0: \theta \geq 0.48$. Upon the study's completion, the researchers compute $\thetahathat = 9/30 = 0.3$.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{7}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}

\item The binomial test is an exact test of the hypothesis of interest. \\

Let $B \sim \binomial{30}{48\%}$ with PMF $p_B(x)$ and CDF $F_B(x)$. The following is an abridged table of the PMF and CDF. The values are rounded to the nearest two digits but should be treated as exact.

%xs = 7:22
%pacman::p_load(xtable)
%xtable(
%rbind(
%  xs,
%  round(dbinom(xs, 30, .48),2),
%  round(pbinom(xs, 30, .48),2)
%))
\begin{table}[ht]
\centering
\begin{tabular}{c|rrrrrrrrrrrrrrrr}
$x$ & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 \\ \hline
$p_B(x)$ & 0 & 0.01 & 0.02 & 0.04 & 0.07 & 0.10 & 0.13 & 0.14 & 0.14 & 0.12 & 0.09 & 0.06 & 0.04 & 0.02 & 0.01 & 0 \\
$F_B(x)$ & 0 & 0.01 & 0.04 & 0.08 & 0.14 & 0.24 & 0.37 & 0.52 & 0.66 & 0.78 & 0.87 & 0.93 & 0.97 & 0.99 & 1 & 1 \\
   \hline
\end{tabular}
\end{table}

\item The scientific standard of $\alpha = 5\%$ is attainable in the binomial test.
\item A retainment region of $\braces{0, 1, \ldots, 9}$ is the the region that most closely provides the scientific standard of $\alpha = 5\%$.
\item A rejection region of $\braces{0, 1, \ldots, 9}$ is the the region that most closely provides the scientific standard of $\alpha = 5\%$.
\item At $\alpha = 0.04$, the test rejects the null hypothesis.
\item At $\alpha = 0.04$, the test retains the null hypothesis.
\item Fisher's p value is 1\%.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{5} Same as before. \ingray{\footnotesize The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the rat supplier. Animal studies are frequently done on rats since rats are considered a model for humans. Consider a nutritional study that tests the effect of magnesium supplementation on cardiovascular disease. In this study, the $n=30$ rats are given 10mg of magnesium daily. Since the lifespan of rats is on average 2 years, this multi-year study waits until all the rats die until they do the data collection. The data is whether or not each rat had heart problems during their life (i.e. each rat either \emph{did have} a heart problem or \emph{did not have} a heart problem).  Thus the DGP is $\iid \bernoulli{\theta}$ and $\theta$ is the probability of at least one heart problem and we denote the data $x_1, x_2, \ldots, x_{30}$. The point estimate we will use for $\theta$ is $\xbar$ and the estimator is $\Xbar$.~\normalsize We wish to prove that the incidence of heart problems in the rats given magnesium \emph{is less than} 48\%, the national average for heart problems in the American adult population.} Thus $H_a: \theta < 0.48$ and $H_0: \theta \geq 0.48$. Upon the study's completion, the researchers compute $\thetahathat = 9/30 = 0.3$. We wish to test by using the one-proportion z test at $\alpha = 1\%$. Note that $\Phi(-2.33) = 1\%$.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{8}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}
%\item The one-proportion z test is an approximate test.
\item  $\thetahat~|~H_0 \approxdist \normnot{0.48}{0.48 (1 - 0.48)}$
\item  $\thetahat~|~H_0 \approxdist \normnot{0.48}{0.48 (1 - 0.48) / 30}$
\item  $30(\thetahat~|~H_0 - 0.48) / (0.48 (1 - 0.48)) \approxdist \stdnormnot$
\item  $\sqrt{30}(\thetahat~|~H_0 - 0.48) / \sqrt{0.48 (1 - 0.48)} \approxdist \stdnormnot$
\item The retainment region is $\thetahathat \geq .27$  (to the nearest two digits)
\item The retainment region is $z \geq -2.33$ on the standardized scale
\item The test rejects the null hypothesis.
\item The test retains the null hypothesis.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{5} Same as before. \ingray{The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the rat supplier. Animal studies are frequently done on rats since rats are considered a model for humans. Consider a nutritional study that tests the effect of magnesium supplementation on cardiovascular disease. In this study, the $n=30$ rats are given 10mg of magnesium daily. Since the lifespan of rats is on average 2 years, this multi-year study waits until all the rats die until they do the data collection.} The researchers also were interested in mean life expectancy of the magnesium-supplemented rats. Regardless of what $\theta$ was before, we now denote mean life expectancy as $\theta$.  The lifespans in years of each rat were 1.09, 2.48, 3.08, 2.57, 1.04, 0.87, 4.18, 2.23, 3.22, 1.33, 2.49, 1.69, 3.18, 1.39, 2.52, 4.8, 2.44, 1.47, 2.64, 3.96, 3.08, 2.71, 2.8, 3.4, 3.86, 2.28, 3.65, 3.28, 1.54, 1.94. Here are two statistics: $\xbar = 2.57$ and $s = 1.00$. We wish to test if these rats lived longer than the average life expectancy of 2 years.
\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{9}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}\item $H_a: \theta < 0.48$
\item $H_a: \theta > 2$
\item $H_a: \theta \neq 2$
%\item $H_0: \theta \leq 2$
%\item $H_0: \theta \geq 2$
%\item $H_0: \theta = 2$
\item We can use the one sample z test to run this test without any assumptions.
\item We can use the one sample z test to run this test by assuming an $\iid \normnot{\theta}{1^2}$ DGP.
\item We can use the one sample z test to run this test by assuming an $ \iid \normnot{\theta}{\sigsq}$ DGP if $\sigsq$ were given to you.
\item We can use the one sample z test to run this test by assuming an $ \iid \normnot{\theta}{\sigsq}$ DGP where $\sigsq$ is an unknown constant.
\item We can use the one sample t test to run this test by assuming an $ \iid \normnot{\theta}{1^2}$ DGP.
\item We can use the one sample t test to run this test by assuming an $ \iid \normnot{\theta}{\sigsq}$ DGP if $\sigsq$ were given to you.
\item We can use the one sample t test to run this test by assuming an $\iid \normnot{\theta}{\sigsq}$ DGP where $\sigsq$ is an unknown constant.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{5} Same as before. \ingray{The rats used in most laboratories are called Sprague-Dawley rats (the \qu{lab rat}), a special breed of brown rat because they are calmer and easier to handle. 30 Sprague-Dawley rats are purchased from the rat supplier. Animal studies are frequently done on rats since rats are considered a model for humans. Consider a nutritional study that tests the effect of magnesium supplementation on cardiovascular disease. In this study, the $n=30$ rats are given 10mg of magnesium daily. Since the lifespan of rats is on average 2 years, this multi-year study waits until all the rats die until they do the data collection. The researchers also were interested in mean life expectancy of the magnesium-supplemented rats. Regardless of what $\theta$ was before, we now denote mean life expectancy as $\theta$. } The lifespans in years of each rat were 1.09, 2.48, 3.08, 2.57, 1.04, 0.87, 4.18, 2.23, 3.22, 1.33, 2.49, 1.69, 3.18, 1.39, 2.52, 4.8, 2.44, 1.47, 2.64, 3.96, 3.08, 2.71, 2.8, 3.4, 3.86, 2.28, 3.65, 3.28, 1.54, 1.94. Here are two statistics: $\xbar = 2.57$ and $s = 1.00$. We wish to test if these rats lived longer than the average life expectancy of 2 years. Hence $H_a: \theta > 2$ and $H_0: \theta \leq 2$. We will use the one-sample t-test and use $\alpha = 1\%$. Note that $F_{T_{29}}(-2.46) = 1\%$.
\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{6}

\begin{enumerate}[(a)]
%\setcounter{enumi}{3}\item $H_a: \theta < 0.48$
\item The rejection region is $\thetahathat > 4.46$ (to the nearest two digits)
\item The rejection region is $\thetahathat > 5.03$ (to the nearest two digits)
\item The rejection region is $\thetahathat > 2.45$ (to the nearest two digits)
\item The rejection region is $\thetahathat > 3.02$ (to the nearest two digits)
%\item You can conclude from this test that Sprague-Dawley rats given daily supplements of 10mg of magnesium live longer than the average rat.
\item You can conclude from this test that the power in this test is very high (i.e. near 1).
\item The one-sample t-test is an approximate test.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

%xbar1 = 2.57
%s1 = 1
%n1 = 30
%n2 = 6
%
%set.seed(1984)
%x = rnorm(n2, 2.7, 1)
%paste0(round(x, 2), collapse = ", ")
%
%xbar2 = mean(x)
%s2 = sd(x)
%
%
%
%ssqpooled = ((n1-1) * s1^2 + (n2-1) * s2^2) / (n1+n2-2)
%naive_pooled_ssq = s1^2 / n1 + s2^2 / n2
%
%round(ssqpooled, 2)
%round(sqrt(ssqpooled), 2)
%n_factor = sqrt(1/n1+1/n2)
%round(sqrt(ssqpooled) * n_factor, 2) #*************************************
%round(sqrt(ssqpooled) * n_factor^2, 2)
%round(naive_pooled_ssq, 2)
%round(sqrt(naive_pooled_ssq), 2)
%round(sqrt(naive_pooled_ssq) * n_factor, 2)
%
%#now if they used just sigma = 1
%round(sqrt(1) * n_factor, 2)
%
%#satterthwaite df
%sdf = naive_pooled_ssq^2 / (s1^4 / (n1^2 * (n1 - 1)) + s2^4 / (n2^2 * (n2 - 1)))
%sdf
%n1+n2-2
%qt(.01, n1+n2-2)
%qt(.01, sdf)

\problem\timedsection{10} Same as before. [But no space to put the old text] Researchers repeated this study with a higher dose of magnesium on 6 rats. The lifespan of these \qu{higher} dose rats where 3.11, 2.38, 3.34, 0.85, 3.65, 3.89. Here are their statistics: $\xbar = 2.87$ and $s = 1.11$. The statistics for the previous sample of rats (who received the 10mg dose which we now called the \qu{lower} dose) was $n=30$, $\xbar = 2.57$ and $s = 1.00$. We want to test if there's any difference in rat lifespan between the two doses. We will assume the same DGP (iid normal) for the higher dose group (but with a different mean than the lower dose group). We will also assume the same variance for both groups. Note that $F_{T_{34}}(-2.44) = 1\%$ and $F_{T_{6.70}}(-3.04) = 1\%$.
\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{11}

\begin{enumerate}[(a)]
\item The standard error of the sampling distribution of $\thetahat_{\text{higher}} - \thetahat_{\text{lower}}$ is 1.04 (to the nearest two digits)
\item The standard error of the sampling distribution of $\thetahat_{\text{higher}} - \thetahat_{\text{lower}}$ is 1.02 (to the nearest two digits)
\item The standard error of the sampling distribution of $\thetahat_{\text{higher}} - \thetahat_{\text{lower}}$ is 0.46 (to the nearest two digits)
\item The standard error of the sampling distribution of $\thetahat_{\text{higher}} - \thetahat_{\text{lower}}$ is 0.20 (to the nearest two digits)
\item The standard error of the sampling distribution of $\thetahat_{\text{higher}} - \thetahat_{\text{lower}}$ is 0.24 (to the nearest two digits)
\item The standard error of the sampling distribution of $\thetahat_{\text{higher}} - \thetahat_{\text{lower}}$ is 0.49 (to the nearest two digits)
\item The standard error of the sampling distribution of $\thetahat_{\text{higher}} - \thetahat_{\text{lower}}$ is 0.22 (to the nearest two digits)

\item You were given sufficient information to compute an exact rejection region
\item You were given sufficient information to compute a retainment region at $\alpha = 1\%$
\item You were given sufficient information to compute a retainment region at $\alpha = 2\%$
\item You were given sufficient information to compute a retainment region at $\alpha = 5\%$
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}


\section{Discounted utility}


\chapter{Source code}
\label{cha:sourcecode}

\section{Math utilities}
\label{sec:mathsource}

\lstinputlisting[caption=Math utilities in JavaScript,label=listing:mathjs,language=JavaScript]{math.js}
\lstinputlisting[caption=Math utilities in C++,label=listing:mathjs,language=C++]{math.cpp}

\section{Particle system}
\label{sec:particlesource}

\lstinputlisting[caption=Particle object in JavaScript,label=listing:particlejs,language=JavaScript]{particles/particle.js}
\lstinputlisting[caption=Particle object in C++,label=listing:particlejs,language=C++]{particles/particle.cpp}
\lstinputlisting[caption=Particle emitter object in JavaScript,label=listing:particleemitterjs,language=JavaScript]{particles/particleEmitter.js}
\lstinputlisting[caption=Particle emitter object in C++,label=listing:particleemittercpp,language=C++]{particles/particleEmitter.cpp}
\lstinputlisting[caption=Initial particle system object in JavaScript,label=listing:particlesystem1js,language=JavaScript]{particles/particleSystem.js}
\lstinputlisting[caption=Initial particle system in C++,label=listing:particlesystem1cpp,language=C++]{particles/particleSystem.cpp}
\lstinputlisting[caption=Optimised particle system object in JavaScript,label=listing:particlesystem2js,language=JavaScript]{particles/particleSystem2.js}
\lstinputlisting[caption=Optimised particle system in C++,label=listing:particlesystem2cpp,language=C++]{particles/particleSystem2.cpp}

\section{Spheres collision detection}
\label{sec:spheressource}

\lstinputlisting[caption=Sphere object in JavaScript,label=listing:spherejs,language=JavaScript]{spheres/sphere.js}
\lstinputlisting[caption=Sphere object in C++,label=listing:spherejs,language=C++]{spheres/sphere.cpp}

\lstinputlisting[caption=Sphere system object in JavaScript,label=listing:spheresystemjs,language=JavaScript]{spheres/sphereSystem.js}
\lstinputlisting[caption=Sphere system in C++,label=listing:spheresystemcpp,language=C++]{spheres/sphereSystem.cpp}

\lstinputlisting[caption=Octree in JavaScript,label=listing:octreejs,language=JavaScript]{spheres/octree.js}
\lstinputlisting[caption=Octree in C++,label=listing:octreecpp,language=C++]{spheres/octree.cpp}

\lstinputlisting[caption=Octree-based sphere system object in JavaScript,label=listing:spheresystem2js,language=JavaScript]{spheres/sphereSystem2.js}
\lstinputlisting[caption=Octree-based sphere system in C++,label=listing:spheresystem2cpp,language=C++]{spheres/sphereSystem2.cpp}