\newcommand{\z}{\mathpzc{z}}
\newcommand{\falign}{0}
\newcommand{\talign}{7.5}

\chapter{Resampling phased SWARM for VLBI}
\label{chap:aphids}

%%\section*{abstract}

To enable VLBI observations with heterogeneous arrays, it is sometimes necessary to pre-process
the data before correlating.  In this chapter we describe a software solution to change the
sampling rate of the data taken at the SMA for the EHT's March 2015 observing campaign.  We
focus on the sampling rate conversion problem and investigate several possible algorithms.  The project
has applications for future VLBI observations at the SMA as well as other sites with similar
challenges.

\section{Introduction}

Very Long Baseline Interferometry (VLBI) uses similar techniques as the connected element arrays such as the
SMA and ALMA
featured in the previous chapters.  By using detectors located thousands of miles away, perhaps on different
continents or even in space, this technique can achieve milliarcsecond resolution at radio wavelengths.  Each
station must have its own clock and, after the observation campaign, the recorded signals is correlated off-site.
Examples of VLBI applications include geodesy, astrometry, and imaging \citep{felli89,sovers98}.  Connected
elements arrays may also participate in these experiments as a single, phased array site \citep{thompson01}.

Technical developments in the field continue to open new scientific opportunities.  One such exciting example
is the development of mm and sub-mm wavelength VLBI by collaborations such as the Event Horizon
Telescope (EHT).  The EHT observes nearby super massive black holes on scales of their Schwarzschild radii and
their observations provide a unique lab to study general relativity and accretion physics.  One of the
challenges that this experiment faces is the inhomogeneity of its array which is composed of many independently
operated observatories.  While some of these telescopes use the same back-end hardware such as the
state-of-the-art ROACH2 VLBI Digital Back-end \citep[R2DBE;][]{vertatschitsch16}, a few sites
cannot use these systems and produce very
different data products. In particular, the sampling rate of the phased ALMA, SMA, and CARMA arrays are
incompatible with the standard VLBI rate.  Before the EHT's software correlator
\citep[DIFX;][]{deller07} can find fringes using these data, they must be resampled to the R2DBE rate of
4096\,MHz.

This chapter summarizes the Adaptive Phased-array and Heterogeneous
Interpolator, and Downsampler for SWARM (APHIDS), a resampling software designed for
phased SMA data products.  We concentrate our discussion to early development and design in the project with
a particular emphasis on the resampling problem.  APHIDS also has very general applications for
post-processing of VLBI data since it features fast I/O, a modular threaded pipeline
and Graphics Processor Unit (GPU) accelerated data processing.  The code base is open-source and available
online at \url{https://github.com/sma-wideband/sdbe}.

The purpose of APHIDS is to take data recorded at the SMA and process it for correlation using
DIFX.  The SMA is a key site for the EHT that provides important East-West baselines and a near zero baseline
with its neighbor the JCMT (see Figure \ref{fig:eht_baselines}).  The phased sum is calculated by the SMA correlator, the SMA Wideband Astronomical
ROACH2 Machine (SWARM\footnote{\url{https://www.cfa.harvard.edu/twiki5/view/SMAwideband}}),
which is currently being deployed in stages as the array's bandwidth capabilities are
upgraded.  For EHT observations taken in March 2015, SWARM recorded at a sampling rate of 2496\,MHz in
two separate phased sums.

In \S\ref{sec:src_basics} we discuss the sampling rate conversion problem and several common solutions.
The current APHIDS resampling algorithm is described in \S\ref{sec:aphids} along with several important
design considerations.  We include measurements of performance as well as a successful
demonstration of the software's operation.  We end by summarizing the goals for near and long-term project
development.

 %% upsampling cartoon
\begin{figure}[t]
\begin{tikzpicture}[
declare function={
    funcx(\x) = -0.5*sin(2*pi*0.2*\x r) + 0.2*cos(2*pi*0.32*\x r) +
                0.3*sin((2*pi*0.9*\x - 0.3) r) + 0.6*sin(2*pi*0.5*\x r);},
]
% frequency domain

%% |X|
\draw [<->, rounded corners, very thick](\falign,1.5) -- (\falign,0) --(\falign+6.5,0) node[below] {$f$};
\node[left] at (\falign,0.7) {$|X|$};
\node[below] at (\falign+1,0) {$f_0/2$};
\foreach \i in {1,...,6}
  \draw[thick] (\i, 0) -- (\i,0.1);
  \draw[very thick] (\falign,1) to [out=0, in=170]
                    (0.5+\falign,1.03) to [out=350, in=105]
                    (\falign+1,0);
  \draw[very thick,dashed] (\falign+1,0) to [out=75, in=190]
                           (\falign+1.5,1.03) to [out=10, in=180]
                           (\falign+2, 1) to [out=0,in=170]
                            (\falign+2.5, 1.03) to [out=350, in=105]
                            (\falign+3,0);
  \draw[very thick,dashed] (\falign+3,0) to [out=75, in=190]
                            (\falign+3.5,1.03) to [out=10, in=180]
                            (\falign+4, 1) to [out=0,in=170]
                            (\falign+4.5, 1.03) to [out=350, in=105]
                            (\falign+5,0);
  \draw[very thick,dashed] (\falign+6-0,1) to [out=170, in=0]
                            (\falign+6-0.5,1.03) to [out=190, in=75]
                            (\falign+6-1,0);

  %% W
  \draw [<->, rounded corners, very thick](\falign,-0.5) -- (\falign,-2) -- (\falign+6.5,-2);
  \node[left] at (\falign,0.7-2) {$|X_L|$};
  \node[below] at (\falign+3,-2) {$Lf_0/2$};
  \foreach \i in {1,...,6}
    \draw[thick] (\i, -2) -- (\i,0.1-2);
  \draw[very thick] (\falign,1-2) to [out=0, in=170]
                    (0.5,1.03-2) to [out=350, in=105]
                    (\falign+1,-2);
  \draw[very thick,] (\falign+1,-2) to [out=75, in=190]
                     (\falign+1.5,1.03-2) to [out=10, in=180]
                     (\falign+2, 1-2) to [out=0,in=170]
                     (\falign+2.5, 1.03-2) to [out=350, in=105]
                     (\falign+3,-2);
  \draw[very thick,dashed] (\falign+3,-2) to [out=75, in=190]
                            (\falign+3.5,1.03-2) to [out=10, in=180]
                            (\falign+4, 1-2) to [out=0,in=170]
                            (\falign+4.5, 1.03-2) to [out=350, in=105]
                            (\falign+5,-2);
  \draw[very thick,dashed] (\falign+6-0,1-2) to [out=170, in=0]
                            (\falign+6-0.5,1.03-2) to [out=190, in=75]
                            (\falign+6-1,-2);

%% H
  \draw [<->, rounded corners, very thick](\falign,-2.5) -- (\falign,-4) -- (\falign+6.5,-4);
  \foreach \i in {1,...,6}
    \draw[thick] (\i, -4) -- (\i,0.1-4);
  \node[left] at (\falign,0.7-4) {$|H|$};
  \node[below] at (\falign+1,-4) {$f_0/2$};
  \node[below] at (\falign+6,-4) {$Lf_0$};
  \draw[very thick] (\falign+0,1-4) -- (\falign+0.8,1-4) -- (\falign+1,0-4);
  \draw[very thick,dashed] (\falign+5,0-4) -- (\falign+5+0.2,1-4) -- (\falign+6,1-4);

  %% Y = H \ast X_L
  \draw [<->, rounded corners, very thick](\falign,-4.5) -- (\falign,-6) -- (\falign+6.5,-6) node[below] {$f$};
  \foreach \i in {1,...,6}
    \draw[thick] (\i, -6) -- (\i,0.1-6);
  \node[left,align=right] at (\falign,0.7-6) {$|Y|$};
  \node[below] at (\falign+3,-6) {$f_1/2$};
  \draw[very thick] (\falign,1-6) to [out=0, in=170]
                    (0.5,1.03-6) to [out=350, in=105]
                    (\falign+1,-6);
  \draw[very thick,dashed] (\falign+6-0,1-6) to [out=170, in=0]
                            (\falign+6-0.5,1.03-6) to [out=190, in=75]
                            (\falign+6-1,-6);

  %%%%%%%%%%%%%%%
  % time domain
  %% x
  \draw [<->, rounded corners, very thick](\talign,1.5) -- (\talign,0) -- (\talign+6.5,0) node[below] {$i$};
  \node[left] at (\talign,0.7) {$x$};
  \foreach \i in {0,...,7}
    \draw[thick] (3*\i/4+\talign,0) -- (3*\i/4+\talign,{funcx(\i/2.)});
  \foreach \i in {0,...,7}
    \draw[fill] (3*\i/4+\talign,{funcx(\i/2.)}) circle[radius=0.05];
  \draw[very thick, dashed, domain=0:23,samples=50] plot(\x/4+\talign, {funcx(\x/6.)});

  %% x_L
  \draw [<->, rounded corners, very thick](\talign,-0.5) -- (\talign,-2) -- (\talign+6.5,-2) node[below] {$i$};
  \node[left] at (\talign,0.7-2) {$x_L$};
  \foreach \i in {0,...,7}{
    \draw[thick] (3*\i/4+\talign,-2) -- (3*\i/4+\talign,{funcx(\i/2.)-2});
    \draw[fill] (3*\i/4+\talign,{funcx(\i/2.)-2}) circle[radius=0.05];}
  \foreach \i in {0,...,7}
    \foreach \j in {1,2}
    \draw[fill] (3*\i/4+\talign+\j/4,0-2) circle[radius=0.05];
  \draw[very thick, dashed, domain=0:23,samples=50] plot(\x/4+\talign, {funcx(\x/6.)-2});

 %% y
  \draw [<->, rounded corners, very thick](\talign,-4.5) -- (\talign,-6) -- (\talign+6.5,-6) node[below] {$j$};
  \node[left] at (\talign,0.7-6) {$y$};
  \foreach \i in {0,...,22}{
    \draw[thick] (\i/4+\talign,-6) -- (\i/4+\talign,{funcx(\i/6.)-6});
    \draw[fill] (\i/4+\talign,{funcx(\i/6.)-6}) circle[radius=0.05];}
  \draw[very thick, dashed, domain=0:23,samples=50] plot(\x/4+\talign, {funcx(\x/6.)-6});

\end{tikzpicture}
\caption{A sketch of upsampling when $L = 3$. The left column shows how the spectrum changes with each operation
and the right column mirrors the effect in the time domain.}
\label{fig:upsampling}
\end{figure}

\section{Sample Rate Conversion} \label{sec:src_basics}

Sample rate conversion, or resampling, is the process of taking a digital signal ($x[i]$) sampled at some rate
($f_0$) and calculating new samples ($y[i]$) at a different rate ($f_1$).  To analyze this problem we use the
ratio between the two rates: $f_1/f_0 = L/M$, where $L$ and $M$ are relatively prime integers.  A sample rate
conversion can be helpfully thought of as the combination of upsampling and downsampling
\citep{oppenheim10,lyons11}.
Upsampling, (also commonly called interpolation or expansion), increases the sampling rate
by a factor of $L$ with the insertion of $L-1$ zeroes between the original $x[i]$.  A low-pass,
anti-imaging filter smooths the signal and suppresses the high frequency spectral images greater than the original
$f_0$ that have been introduced by the zero inserts.  Figure \ref{fig:upsampling} sketches this process in the
time and frequency domain for when $L$ equals 3.

 %% cartoon explaining down-sampling
 % http://tex.stackexchange.com/questions/124878/declare-function-for-tikzpicture
\begin{figure}[t!]
\begin{tikzpicture}[
declare function={
    funcx(\x) = -0.1*sin(2*pi*2.8*\x r) + 0.2*sin(2*pi*1.3*\x r) +
                0.3*sin((2*pi*0.9*\x - 0.3) r) + 0.6*sin(2*pi*0.5*\x r);
    funchx(\x) = 0.3*sin((2*pi*0.9*\x - 0.3) r) + 0.6*sin(2*pi*0.5*\x r);},
]
  % frequency domain

  %% |X(f)|
  \draw [<->, rounded corners, very thick](0,1.5) -- (0,0) --(6.5,0) node[below] {$f$};
  \node[left] at (0,0.7) {$|X|$};
  \foreach \i in {1,...,6}
    \draw[thick] (\i, 0) -- (\i,0.1);
  \node[below] at (3,0) {$f_0/2$};
  \draw[very thick] (0,1) to [out=10, in=170] (0.5,1.03)
                        to [out=350, in=190] (2,0.95)
                        to [out=10, in=170] (2.5,1)
                        to [out=-10, in=110] (3,0);
  \draw[very thick,dashed] (6-0,1) to
                            [out=170, in=10] (6-0.5,1.03) to
                            [out=190, in=350] (6-2,0.95) to
                            [out=170, in=10] (6-2.5,1) to [out=190, in=70] (6-3,0);

  %% |H(f)|
  \draw [<->, rounded corners, very thick](0,-0.5) -- (0,-2) -- (6.5,-2);
  \node[left] at (0,0.7-2) {$|H|$};
  \foreach \i in {1,...,6}
    \draw[thick] (\i, -2) -- (\i,0.1-2);
  \node[below] at (1,0-2) {$f_0/2M$}; \node[below] at (6,0-2) {$f_0$};
  \draw[very thick] (0,1-2) -- (0.8,1-2) -- (1,0-2);
  \draw[very thick,dashed] (5,0-2) -- (5.2,1-2) -- (6,1-2);

  %% |W(f)|
  \draw [<->, rounded corners, very thick](0,-2.5) -- (0,-4) -- (6.5,-4);
  \node[left] at (0,0.7-4) {$|HX|$};
  \foreach \i in {1,...,6}
    \draw[thick] (\i, -4) -- (\i,0.1-4);
  \node[below] at (1,0-4) {$f_0/2M$}; \node[below] at (6,0-4) {$f_0$};
  \draw[very thick] (0,1-4) to [out=10, in=170] (0.5,1.03-4) to [out=350, in=105] (1,0-4);
  \draw[very thick,dashed] (6-0,1-4) to [out=170, in=10] (6-0.5,1.03-4) to [out=190, in=75] (6-1,0-4);

  %% Y(f)
  \draw [<->, rounded corners, very thick](0,-4.5) -- (0,-6) -- (6.5,-6) node[below] {$f$};
  \node[left] at (0,0.7-6) {$|Y|$};
  \foreach \i in {1,...,6}
    \draw[thick] (\i, -6) -- (\i,0.1-6);
  \node[below] at (1,0-6) {$f_1/2$};
  \draw[very thick] (0,1-6) to [out=10, in=170] (0.5,1.03-6) to [out=350, in=105] (1,0-6);
  \draw[very thick,dashed] (1,0-6) to [out=75, in=190]  (1.5,1.03-6) to [out=10, in=170]
                            (2, 1-6) to [out=10,in=170] (2.5, 1.03-6) to [out=350, in=105] (3,-6);
  \draw[very thick,dashed] (3,0-6) to [out=75, in=190]  (3.5,1.03-6) to [out=10, in=170]
                            (4, 1-6) to [out=10,in=170] (4.5, 1.03-6) to [out=350, in=105] (5,-6);
  \draw[very thick,dashed] (6-0,1-6) to [out=170, in=10] (6-0.5,1.03-6) to [out=190, in=75] (6-1,0-6);

  %%%%%%%%%%%%%%%
  % time domain
  %% x
  \draw [<->, rounded corners, very thick](7.5,1.5) -- (7.5,0) -- (13.5,0) node[below] {$i$};
  \node[left] at (7.5,0.7) {$x$};
  \foreach \i in {1,...,22}{
    \draw[thick] (\i/4+7.5,0) -- (\i/4+7.5,{funcx(\i/6.)});
    \draw[fill] (\i/4+\talign,{funcx(\i/6.)}) circle[radius=0.05];}
  \draw[very thick, dashed, domain=0:22,samples=100] plot(\x/4+7.5, {funcx(\x/6.)});

  %% h \ast x
  \draw [<->, rounded corners, very thick](7.5,1.5-4) -- (7.5,0-4) -- (13.5,0-4) node[below] {$i$};
  \node[left] at (7.5,0.7-4) {$h\ast x$};
  \foreach \i in {1,...,22}{
    \draw[thick] (\i/4+7.5,-4) -- (\i/4+7.5,{funchx(\i/6.)-4});
    \draw[fill] (\i/4+\talign,{funchx(\i/6.)-4}) circle[radius=0.05];}
  \draw[very thick, dashed, domain=0:22,samples=50] plot(\x/4+7.5, {funchx(\x/6.)-4});

  %% y
  \draw [<->, rounded corners, very thick](7.5,1.5-6) -- (7.5,0-6) -- (13.5,0-6) node[below] {$j$};
  \node[left] at (7.5,0.7-6) {$y$};
  \foreach \i in {1,...,22}
    \draw[gray] (\i/4+7.5,-6) -- (\i/4+7.5,{funchx(\i/6.)-6});
  \foreach \i in {0,...,7}{
    \draw[thick] (3*\i/4+7.5,-6) -- (3*\i/4+7.5,{funchx(3*\i/6.)-6});
    \draw[fill] (3*\i/4+\talign,{funchx(3*\i/6.)-6}) circle[radius=0.05];}
  \draw[very thick, dashed, domain=0:22,samples=50] plot(\x/4+7.5, {funchx(\x/6.)-6});

\end{tikzpicture}
%\caption{Series of steps for down sampling shown in the frequency domain when $M=3$.}
\caption{A sketch of downsampling when $M = 3$. The left column shows how the spectrum changes with each operation
and the right column mirrors the effect in the time domain.}
\label{fig:downsampling}
\end{figure}

Downsampling, or decimation, by an integer factor of $M$ requires that the signal be first low-pass filtered to
avoid aliasing at frequencies greater than the desired rate $f_0/M$.  Next, the sampling rate is reduced to
$f_0/M$ by selecting every $M$th sample from the filtered signal.  Figure \ref{fig:downsampling} shows this
process in the time and frequency domain for when $M$ equals 3.

Sample rate conversion by a factor $L/M$ can be conceptualized as a three step process where the original signal
is expanded by $L$, filtered, and then decimated by $M$.  As illustrated by Figure \ref{fig:resample_basic},
the serial anti-imaging and
anti-aliasing filters are combined into a single filter with cutoff frequency of
$1/\mathrm{max}(L,M) \times Lf_0/2$.  Under this scheme, interpolation must precede decimation otherwise desired
frequency components greater than $f_0/M$ cannot be preserved.

Computationally, this basic sample rate conversion scheme is highly inefficient.  The filtering is applied at
the highest possible sample rate, $Lf_0$, on a time-series that is $(L-1)/L$ zeros by fraction. Furthermore, the
decimator discards an $(M-1)/M$ fraction of the samples.  As a result, this algorithm requires a lot of memory
and spends precious clock time with wasted math.  There is a vast literature of techniques and algorithms that
improve performance including multistage resampling, folded filter structures, and polyphase representations
\citep{oppenheim10,lyons11,vaidyanathan93}.

\begin{figure}[t]
\centering
\begin{tikzpicture}
   \node[dspnodeopen,dsp/label=left]  (c0) {$x[i]$};
   \node[dspsquare,right=of c0]                     (c1) {\upsamplertext{L}};
   \node[dspfilter,right=of c1]                     (c2) {$H(\z)$};
   \node[dspsquare,right=of c2]                    (c3) {\downsamplertext{M}};
   \node[dspnodeopen,right=of c3,dsp/label=right]  (c4) {$y[j]$};
   \foreach \i [evaluate = \i as \j using int(\i+1)] in {0,1,...,3}
       \draw[dspconn] (c\i) -- (c\j);
\end{tikzpicture}
\caption{Basic multirate resampling signal flow graph.}
\label{fig:resample_basic}
\end{figure}

There have been recent development of polyphase filter representations for GPUs
\citep[i.e.][]{vanderveldt12,adamek14,kim14a}.  These filter structures can operate at the low sample rate of
$f_0/M$
by introducing delays to switch the expander and decimatator \citep{crochiere81}.  However, they require
some filter design and careful book-keeping or buffering to keep track of sample indices \citep{wang01}.
The implementation in \citet{kim14a} assigned each thread to a filter coefficient, parallelizing the
inner product operation.  However, \citet{kim14a} found that their GPU kernel was dominated by the
indexing operations.  \citet{adamek14} (building on the work by \citet{vanderveldt12}) presented an
implementation of a polyphase filter bank that could run at data rates in excess of 6.5\,GB/s
(their estimate for the single channel output of the SKA's Low Frequency Aperture Array).

%  https://github.com/wesarmour/astro-accelerate
\begin{figure}[t!]
\epsscale{1.0}
\plotone{aphids/windows.eps}
\caption{Frequency response for FIR filters equivalent to linear and nearest interpolation when $L/M = 64/39$.
The orange region shows the first Nyquist zone of the target $L/Mf_0$ sample rate.  Spectral components at all
other frequencies are aliased into this region.}
\label{fig:windows}
\end{figure}

For testing and development we also considered two elementary options for the resampling: linear and nearest
neighbor interpolation.  Linear interpolation is:
\begin{equation}
y[i] = x[j] + (x[j+1] - x[j]) (if_0/f_1 - j)
\end{equation}
where $j = \mathrm{floor}(if_0/f_1)$.  This is equivalent to applying a ``tent'' FIR filter with $2L$ taps on the
upsampled $Lf_0$ time series before decimation \citep{oppenheim10}.  Similarly, a $2L$ tap box-car filter is
equivalent to nearest-neighbor interpolation:
\begin{equation}
y[i] = x[\mathrm{round}(if_0/f_1)].
\end{equation}

Neither linear or nearest-neighbor interpolation consider the frequency content of the original signal and, as a
result, perform poorly for signals with any significant power near the Nyquist rate \citep{fraser89}.  To further
emphasize this point, Figure
\ref{fig:windows} shows the frequency response for both methods when
the resampling factor $L/M$ is $64/39$.  Note that both methods have large side lobes and so the
resampled signal will introduce a large slope in the passband.  However, GPUs can compute
these low-order interpolations directly on the card using texture memory.  \citet{kim14b} utilize this feature
by using the cuFFT library to upsample the signal in the Fourier domain by some initial factor, $U$, and then
interpolating in texture memory.  It would be straight forward to
expand upon the framework introduced by \citet{kim14b} to handle cases where $M$ is greater than $L$.
This scheme should help reduce loss from aliasing, albeit
at the cost of two FFTs and much larger memory requirements.

\subsection{Resampling in the Fourier Domain} \label{sec:fourier_resampling}
In contrast to the previous methods, one can also implement a rational $L/M$ sample rate conversion entirely in
the Fourier domain \citep{gold69}.  After accumulating $kM$ samples at a clock rate of $f_0$
(where $k=1,2,3\ldots$), the DFT
returns spectral components spaced at $f_0/kM$.  If $f_1 > f_0$, the resampled spectrum is generated by inserting
$p$ zeros to match the new $f_1$ while maintaining the correct frequency components:
\begin{equation}
\frac{f_1}{kM+p} = \frac{f_0}{kM}.
\end{equation}
Solving for $p = k(L - M)$.  If $f_0 < f_1$, the spectrum is instead trimmed by $p$ samples.  The time series
sampled at $f_1$ can then be constructed from the inverse DFT.  This method is equivalent to sinc
interpolation using an ideal low-pass filter and is a perfect interpolator
if the original, continuous signal has a discrete spectral density distribution that is band-limited below the
Nyquist limit.  In practice, errors may be introduced from aliased spectral leakage.

Trimming or padding the spectrum is equivalent to convolving a normalized sinc function with the original series
wrapped periodically.  Consequently, errors will be introduced into the beginning and end of the
resampled signal.  How far and significantly this error propagates away from the time series' edges depends
on the width of the sinc function which is set by the resampling factors $L$ and $M$:
\begin{equation} \label{eq:sinc}
B\,\mathrm{sinc}(B t) \overset{\mathcal{F}}{\Longleftrightarrow} \begin{cases} 1 \quad |f| < B/2 \\ 0 \quad |f| > B/2 \end{cases}
\end{equation}
where $B = \mathrm{min}(f_0,L/Mf_0)$.
The number of samples for which this makes a large difference is small and can be roughly treated as less than a
1\% effect when $N > 100\,\mathrm{max}(L/M,1)$.  One option to mitigate this error to stitch together
overlapping, resampled segments \citep{bi11} which requires more computation and memory.  A second strategy is to
multiply the original signal by some tapered window function \citep{fraser89}.

For post-processing phased SWARM, DFT resampling is a good fit because one can access large chunks of
the time series without accumulators in hardware and it has well-behaved errors
that can be easily modeled.  However, the speed of the FFT depends on the resampling factors ($L/M = 32/39$ for
the March 2015 EHT observations) and zero-padding in the case that the $L\gg M$ may require large amounts of
memory.  In practice, we also found that the memory space of the GPU could be a limiting factor not only
on how much data could be simultaneously resampled, but also storing FFT plans (which depends on
values of $L$ and $M$).

%% Loss table : /home/krosenfe/resampling/losses.py
% losses for FIR filtering, interpolation schemes, and FFT
\begin{table}
\begin{center}
\centering
\caption{Correlation coefficient loss from resampling \label{tab:loss}}
\begin{threeparttable}
\begin{tabular}{l|ccc}
\toprule
Method & 64/39 & 32/39 & 128/125 \\
\midrule
Nearest-Neighbor            & 13\% & 17\% & 13\% \\
Linear                      &  5\% &  7\% &  5\% \\
Hamming window ($16L$ taps) &  1\% &  2\% &  2\% \\
FFT ($N=M$)                 &  1\% &  1\% & 0.2\% \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{center}
\end{table}

\tikzstyle{line} = [draw,-latex']
\tikzstyle{cpu} = [rectangle, draw, fill=blue!20,
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{gpu} = [rectangle, draw, fill=red!20,
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{resamp} = [rectangle, draw, minimum height=4.5em, align=center, minimum width=15em]

\begin{figure}[t!]
\linespread{1.}\selectfont{}
\begin{center}
\begin{tikzpicture} [node distance = 5cm, auto]
\node [cpu] (mark6_in) {Mark6 Reader};
\node[gpu,below of=mark6_in](reader){Reader};
\node[gpu,right of=reader](resampler){Resampler};
\node[cpu,right of=resampler](VDIF_pkt){VDIF packet \\ \& header};
\node[cpu,below of=VDIF_pkt](mark6_out){Mark6 Writer};

\path[line] (mark6_in) -- node[align=center] {BENG @ 2496 GHz \\ quantized to 2-bits}(reader);
\path[line] (reader) -- node[align=center] {spectra of \\ 16k complex64} (resampler);
\path[line] (resampler) --  node[align=center] {time series \\ @ 2048 GHz}(VDIF_pkt);
\path[line] (VDIF_pkt) -- node[align=center, left]{VDIF @ 4096 GHz \\ quantized to 2-bits}(mark6_out);
\end{tikzpicture}
\caption{This signal flow chart shows an overview of APHIDS.  The red boxes represent operations on the GPU
while blue boxes are for the CPU.  Once the B-engine frames have been unpacked, each of the two phased sum
streams are processed independently and in a serial fashion.}
\label{fig:aphids_flow_chart}
\end{center}
\end{figure}

\section{APHIDS} \label{sec:aphids}

We are developing a software solution to the resampling problem called APHIDS.  This
open-source project is written in C/CUDA using
the High Availability Shared Pipeline
(HASHPIPE)\footnote{\url{https://github.com/david-macmahon/hashpipe}}.  The software is designed to use
modularized, lightweight threads to handle reading data packets from hard drives, resampling on a GPU cluster,
and then writing back to separate disks.  These three steps are each loosely mapped to threads we call the
vdif\_in\_net thread, vdif\_inout\_gpu\_thread, and vdif\_out\_net thread.
The ultimate goal of is to process data streamed directly at the telescope.  The flowchart in Figure
\ref{fig:aphids_flow_chart} shows how data progresses
through the pipeline and this section will describe each block covering the major hardware components, software
design, and data formats.  We will conclude with a discussion of future development and applications of APHIDS.

\subsection{Hardware and Data description}

APHIDS features three main hardware components: an I/O data system, CPU, and GPU cluster.  For I/O, the
EHT observations taken by SWARM in March 2015 have been stored on four disk-packs that each hold eight,
6\,TB hard drives (totaling 192\,TB of disk space).  The Mark6 VLBI data system has four modules
that can hold one disk-pack and we use
two separate Mark6 units to handle the recording and playback of data.  APHIDS itself is run on a separate CPU
that controls the GPU cluster.

The GPU cluster has four GTX 980 GPUs, each with 4GB of local memory.  Data from the CPU (or host) is copied over
a PCI-E bus into the local GPU (or device) memory.  Once the data is transferred, the host directs the device
to perform functions (or kernels) on the data which must be again moved from the local device memory onto
on-chip memory for the individual streaming multiprocessors that make up the GPU.  After computation is done,
the host transfers the data off of the device memory.

The observations were recorded using a scatter/gather file system which disperses the
individual scan data over the four modules in a Mark6.  APHIDS uses fast, general purpose software called
sgcomm (Scatter/Gather Communication) to read in
the scattered data into CPU shared memory over a 10 Gigabit Ethernet.  Data from phased SWARM was
streamed through a specialized version of the R2DBE called the SDBE (Swarm Digital Back End) and packaged into
a customized B-engine format.  Each B-engine frame is composed of 2048, 1056B VDIF frames
\footnote{\url{http://www.vlbi.org/vdif/}} with 128 snapshots of a
16384 sample SWARM spectra.  However, the current SDBE bitcode packs
neither the snapshots contiguously in time nor the spectrum samples contiguously in frequency.  Furthermore,
each B-frames overlaps the preceding one in memory.  Our resampling algorithm handles $19968$\,SWARM samples
simultaneously, so in order to reduce the amount of book-keeping the inout thread buffers 40 B-engine frames to
produce $39 \times 128$ complete SWARM snapshots.  The inout thread does preliminary checks to ensure that the
B-engine frames are complete before assigning each buffer to a GPU.  The data is written as a time series using
following the VDIF specification.

As mentioned previously, the data is split into two phased sums that together cover the desired 4096\,MHz of
observing bandwidth.  Since DIFX can handle the chunks independently we do not stitch the spectra together.
However, the observation was designed to include guard bands which must be removed by APHIDS
(see Figure \ref{fig:swarm_amp_spec} and \S\ref{sec:resamp_block}).

\begin{figure}[t!]
\linespread{1.}\selectfont{}
\centering
\begin{tikzpicture}[node distance = 2.2cm, auto]
\node[align=center](init){};
\node[resamp,below of=init](irfft){(A) batched real IFFT \\ dimension of $32768$};
\node[resamp,below of=irfft](rfft){(B) batched real FFT \\ dimension of $39 \times 512$};
\node[resamp,below of=rfft](trim){Trim band and shift frequency};
\node[resamp,below of=trim](rifft){(C) batched real IFFT \\ dimension of $32 \times 512$};
\node[align=center,below of=rifft](final){};

\path[line](init) -- node[align=left]{ complex spectra \\ @ 2496\,MHz} (irfft);
\path[line](irfft) -- (rfft);
\path[line](rfft) -- (trim);
\path[line](trim) -- (rifft);
\path[line](rifft) -- node[align=left]{ time series \\ @ 2048\,MHz}(final);
\end{tikzpicture}
\caption{The resampling block trims the phased SWARM guard bands and shifts the passband to DC as part of the
resampling operation.}
\label{fig:resampling_block}
\end{figure}

\subsection{Resampling Block}\label{sec:resamp_block}

APHIDS resamples the data in the Fourier domain (see \S \ref{sec:fourier_resampling}).  There were several
contributing factors that led us to make
this choice rather than apply filters in the time domain.  First, the GPU cluster can handle relatively large
chunks of data at a time so losses from the DFT are very manageable.  Second, regardless of
what resampling algorithm is used the SWARM spectra must
be transformed into the time domain, the guard bands trimmed and the signal modulated.  These operations are done
naturally in the Fourier domain.  However, the cost of the resampling operation will be largely dominated by
these operations.  Lastly, we can utilize the optimized cuFFT library
\footnote{\url{https://developer.nvidia.com/cuFFT}} to ensure good performance, reduce development time and
maintain a manageably sized codebase.

The APHIDS resampling block is composed of three 1-dimensional FFTs (A, B, and C; see Figure
\ref{fig:resampling_block}).  Following the cuFFT API reference to improve performance, we utilize batched
FFTs (launching more than one FFT at a time using the cuFFT API), out-of-place transforms,
and operate on contiguous chunks of memory.  We also use single precision transforms to
reduce the memory transfer bandwidth and ensure that
the resampling block can be run on a single device.  The latter point is important since it allows the inout
thread to parallelize the resampling operation across the cluster and provide a roughly x4 speedup.

The first FFT (A) takes the complex valued, 16384 sample SWARM spectrum into the time domain, returning
32768 real samples.  Since SWARM omits the Nyquist sample which is outside the SWARM guard band cutoff frequency,
we insert a zero as the 16385th sample.  Since this transform is radix-2, the performance should benefit from
the Cooley-Tukey algorithm.  However, on our cluster the cuFFT library exhibits linear improvement in Gflops
with transform size up to N of 16384 (see Figure \ref{fig:C2R_performance}).  This behavior is consistent with
the CUDA 7.0 performance
report\footnote{\url{http://on-demand.gputechconf.com/gtc/2015/webinar/gtc-express-cuda7-performance-overview.pdf}}
and we find that this transform, just outside of the optimal regime, operates at 6.7 Gsps.

\begin{figure}[t!]
\epsscale{0.95}
\plotone{aphids/C2R_performance.eps}
\caption{This figure shows the performance of radix-2, single precision, batched, complex-to-real, 1D transforms
measured on our GPU cluster (solid lines). The dashed lines show the same measures for non radix-2, candidate
resampling FFT sizes ($N_B$).  The benchmark conventions follow FFTW  (http://www.fftw.org/speed/method.html).}
\label{fig:C2R_performance}
\end{figure}

Next, we Fourier transform chunks of $N_B = 19968$ consecutive time series samples (B).  The resulting
spectrum has a frequency resolution of $2496$\,MHz$/19958=125$\,kHz.  The following step is to apply a third
complex-to-real transform (C) on a subset of the spectrum, using only frequencies from $150$\,MHz to $1174$\,MHz.
The final time series has size $N_C= 2\times(1174-150)/0.125=16384$ per batch element and our desired
sampling rate of
$16384 \times 0.125$\,MHz$ = 2048$\,MHz.  This simple operation simultaneously resamples the signal, modulates
the signal, and trims the guard bands.  Furthermore, it is easily implemented using cuFFT by using the same
batch size as the second real-to-complex transform, increasing the input pointer index by $150 / 0.125 = 1200$
(equivalent to masking out the first 1200 channels) and then using a input stride of $9980$ for the
$N=16384$ complex-to-real transform.

The choice of FFT size was set to maximize performance.  In order for the target sampling rate to be achieved
$N_B$ must be a multiple of 39.  Furthermore, so that the 150\,MHz guard band can be removed, we must ensure that
150 is a multiple of $2496/N_B$:
\begin{equation}
\frac{150\times39}{2496} = \frac{75}{32} = \frac{i}{j}, \quad i,j = 1, 2,3, \ldots
\end{equation}
To ensure this, $j$ should be a multiple of 32 so that
\begin{equation}
N_B = 39j = 1248i, \quad i=1,2,3,\ldots
\end{equation}
Lastly, the size of the C transform is then determined:
\begin{equation}
N_C = \frac{32}{39} N_B.
\end{equation}
The dashed line in Figure \ref{fig:C2R_performance} shows the performance for candidate $N_B$s.  Unlike for
the radix-2 cases, the configuration that has the best Gflop performance does not also provide the best Gsps
rate.  This is likely because the Gflop calculation is not an actual flop count, but a scaling from the
original Cooley-Tukey algorithm \citep{cooley65}.  The Gsps metric is perhaps more useful in this case as it
shows that choices of $N_B \leq 19968$ will have good performance.  However, the figure also demonstrates
that this non radix-2 FFT is about $2\times$ slower than the radix-2 case.

\subsection{Timing Performance}

In this section we report on timing measurements of the APHIDS resampling kernels.  We consider the cost of
loading the data onto the device separately and do not include it in the scores of the individual kernels.
Furthermore, we do not include the cost of building the FFT plan since this operation is done only once.
We have designed the resampling block to operate on a single GPU so there is no time spent moving data
between devices and we can parallelize across each card.  The
kernels (except for the depacketer) must be called
twice, once on each phased sum.  Our current implementation uses synchronous calls (one kernel function is
called at a time).  We tested an asynchronous setup, but saw no speedup.  This indicates that the majority of
the GPU resources are being used.  Table \ref{tab:kernel_times} lists the time it takes for the kernel to be
executed a single time and also the speed of the kernel assuming it is operating on a buffered 0.32\,Gb data
chunk (see \S\ref{sec:resamp_block}).

%% Loss table : /home/krosenfe/resampling/losses.py
% losses for FIR filtering, interpolation schemes, and FFT
\begin{table}
\begin{center}
\caption{Timing measurements of APHIDS kernels \label{tab:kernel_times}}
\begin{threeparttable}
\begin{tabular}{l|cc}
\toprule
Kernel & time [ms] & rate [Gbps] \\
\midrule
Reorder                & 10.5 & 31.5 \\
FFT A (N=32768)        & 25.4 & 12.9 \\
FFT B (N=19968)        & 38.0 & 8.6  \\
FFT C (N=16384)        & 13.8 & 23.7 \\
Quantize (2-bit)       & 12.0 & 27.3 \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{center}
\end{table}

\subsection{FFT Accuracy}
One concern with using multiple FFTs to change the sampling rate is the error introduced by bit growth.  The
floating point error grows as $\mathcal{O}(\sqrt{\log{N}})$ \citep{schatzman96} on average for the Cooley-Tukey
algorithm, but errors can be exacerbated by inaccurate twiddle factors or our choice of FFT algorithm (see
benchFFT\footnote{\url{http://www.fftw.org/accuracy/}}).  We have computed the accuracy of the cuFFT library
using single precision for
radix-2 transforms as well as the $N_b = 19968$ case in Figure \ref{fig:fft_accuracy}.  These values
were computed using benchFFT and we compare against fftw3\footnote{\url{http://www.fftw.org/}}
results also computed on our GPU cluster.  For random input, the cuFFT library follows the expected
$\mathcal{O}(\sqrt{\log{N}})$ behavior, but with larger scatter and slightly worse performance than fftw3.

\begin{figure}[t]
\plotone{aphids/fft_accuracy.eps}
\caption{Accuracy of the FFT algorithm as measured by the $L_2$ error from benchFFT} \label{fig:fft_accuracy}
%/home/krosenfe/testing/cufft_accuracy/make_figure.py
\end{figure}

Round off error and bit growth is also a general concern for scientific computing on GPUs.
Increasing the precision to double can significantly slow down performance and then
require reoptimization or even the redesign of kernels.  Generally, well implemented FFTs can
be very stable options.  The results of ours test suggest that
bit growth will not be a large source of loss for the resampling scheme presented in \S\ref{sec:resamp_block}.

\subsection{Quantization}
Another source of loss and distortion to consider is the requantization back to 2-bits that is done
before writing back to disk on the Mark6 (see Figure \ref{fig:aphids_flow_chart}).  In theory, APHIDS could
record higher bit data (or even the full 32 bits for floating point), but this adds complications for
correlation
in DIFX and would incur costs for both disk space and output data rate.  While the former cost is monetary, the
latter could be an important limiting factor for real-time operation of APHIDS at the back end of the SDBE.
The signal-to-noise loss expected from 2-bit quantization is a function of the quantization
thresholds, but for Gaussian noise and Nyquist sampling is at best 12\% \citep{cooper70,thompson01}.

\subsection{Inverting the Polyphase Filterbank}
Under normal operation, SWARM uses a polyphase filter bank (PFB) to generate its spectra.  The PFB is an
efficient method of producing spectra where each channel has a frequency response much better than the
sinc pattern of the DFT.  This is achieved by using a window function and a polyphase filter structure to reduce
the effects of leakage and scalloping \citep{lyons11}.  However, for VLBI observations we must transform the
spectrum into the original time series to resample the data.  Inverting the PFB is a non-trivial operation so
the operators use a version of the SWARM bit-code that replaces the PFB with an FFT.  However, for the first
two nights of the 2015 EHT campaign this version was not used and so we must consider this problem.

One option is to simply apply the generic APHIDS pipeline on the PFB spectra which uses an DFT to invert the
SWARM spectra into time series.  The resulting pseudo time-series is a series of weighted
averages of the original time series.  The weights are determined by the window function used in the
PFB and the number of elements in the sum by the number of taps, $N_T$.  For example, if the window function is
a box car, then the weights are equal and the correlation coefficient for a white noise time stream goes like:
\begin{equation}
\rho = \frac{1}{\sqrt{N_T}}.
\end{equation}
SWARM uses four taps and a Hanning filter.  We simulated the resulting correlation loss and saw a 34\% loss,
exceeding the largest loss we expect in the pipeline from the 2-bit quantization (12\%).  Using the PFB inversion
technique developed by Richard Shaw\footnote{\url{https://github.com/jrs65/pfb-inverse}} we see excellent
recovery with a loss of only 0.5\% although the inversion routine takes about 10 times longer than numpy's
iFFT and already uses the highly optimized LAPACK library.  CUDA libraries offer many of the required functions
to implement this solution on the GPU and we have ported Shaw's solution to CUDA using a combination of
CUBLAS and CUSolver.  The current implementation runs about 5 times slower than an inverse FFT batched over
the same size data chunk.

\begin{figure}[t!]
\epsscale{1.0}
\includegraphics{aphids/kr_autos.eps}
\caption{This figure shows the spectral density of phased SWARM along with the single reference dish
(in purple).  The APHIDS output is also plotted (dotted).}
\label{fig:swarm_amp_spec}
\end{figure}

\subsection{Current Status and Future Development}

APHIDS is still under active development, but rapidly nearing full-scale production.  Figure \ref{fig:swarm_amp_spec}
shows the original SDBE and resampled auto spectra from data taken in March 2015.  Using data resampled by APHIDS, Mike Titus,
the correlator engineer at MIT Haystack, has found a cosmic fringe on 3C297 using the short
baseline between the JCMT and the SMA (see Figure \ref{fig:aphids_fringe}).  He reported an increase in the fringe amplitude over initial tests
using linear interpolation.  APHIDS currently runs about 4.5 times slower than real time, sufficient for
processing the March 2015 EHT campaign data.  The resampling block with PFB inversion should be available
shortly.

APHIDS promises to be an important tool for the EHT.  It crucially enables the correlation in
DIFX of non-standard sampling rates and also provides a platform for any general pre-processing of
VLBI data.  It features fast data I/O from Mark6 recorders and GPU accelerated processing.
We have demonstrated the correctness and speed of this design and its immediate applicability to
the 2015 EHT data.  However,
there remain a number of challenges for future development of the software.

For future VLBI campaigns at the SMA, it would be ideal if APHIDS could run in real-time
at the backend of the SDBE.  This would reduce the media requirements and also the
hassle of post-processing the data off-site.  However, the performance will need to improve as the
SWARM data rates increase and if the front-end of the pipeline is optimized to reduce the
amount of SNR loss from quantization.  Additionally, some analysis on the cooling requirements
of the cards should be conducted to ensure that the GTX980s used in our GPU cluster can safely run
at the altitude of the SMA (4100\,meters).  Regardless of where it operates, APHIDS will need
maintenance to remain compatible with SWARM as it and the SDBE are updated.  The current
git repository and its wiki is a good solution for keeping track of versioning and performance status.

%\includepdf[pages={1}]{aphids/089-0810_JS_fix.pdf}
\begin{figure}
\includegraphics[width=\linewidth]{aphids/089-0810_JS_fix.pdf}
\caption{Fourfit plots of a sky fringe found using APHIDS resampled SWARM data and JCMT.}
\label{fig:aphids_fringe}
\end{figure}

\section{The APHIDS Team}
Andre Young, Lindy Blackburn, Laura Vertatschitsch, Rurik Primiani, and Katherine Rosenfeld are part of
the core APHIDS team located at the Smithsonian Astrophysical Observatory.  Jonathan Weintroub and Sheperd
Doeleman were responsible for project oversight and invaluable support was provided by Mike Titus and Geoff Crew at MIT
Haystack.  The EHT is supported by the NSF and the Gordon and Betty Moore Foundation.


\section{Introduction}

The \textit{proceedings} are the records of a conference.\footnote{This
  is a footnote}  ACM seeks
to give these conference by-products a uniform, high-quality
appearance.  To do this, ACM has some rigid requirements for the
format of the proceedings documents: there is a specified format
(balanced double columns), a specified set of fonts (Arial or
Helvetica and Times Roman) in certain specified sizes, a specified
live area, centered on the page, specified size of margins, specified
column width and gutter size.

\section{The Body of The Paper}
Typically, the body of a paper is organized into a hierarchical
structure, with numbered or unnumbered headings for sections,
subsections, sub-subsections, and even smaller sections.  The command
\texttt{{\char'134}section} that precedes this paragraph is part of
such a hierarchy.\footnote{This is a footnote.} \LaTeX\ handles the
numbering and placement of these headings for you, when you use the
appropriate heading commands around the titles of the headings.  If
you want a sub-subsection or smaller part to be unnumbered in your
output, simply append an asterisk to the command name.  Examples of
both numbered and unnumbered headings will appear throughout the
balance of this sample document.

Because the entire article is contained in the \textbf{document}
environment, you can indicate the start of a new paragraph with a
blank line in your input file; that is why this sentence forms a
separate paragraph.

\subsection{Type Changes and {\itshape Special} Characters}

We have already seen several typeface changes in this sample.  You can
indicate italicized words or phrases in your text with the command
\texttt{{\char'134}textit}; emboldening with the command
\texttt{{\char'134}textbf} and typewriter-style (for instance, for
computer code) with \texttt{{\char'134}texttt}.  But remember, you do
not have to indicate typestyle changes when such changes are part of
the \textit{structural} elements of your article; for instance, the
heading of this subsection will be in a sans serif\footnote{Another
  footnote here.  Let's make this a rather long one to see how it
  looks.} typeface, but that is handled by the document class file.
Take care with the use of\footnote{Another footnote.}  the
curly braces in typeface changes; they mark the beginning and end of
the text that is to be in the different typeface.

You can use whatever symbols, accented characters, or non-English
characters you need anywhere in your document; you can find a complete
list of what is available in the \textit{\LaTeX\ User's Guide}
\cite{Lamport:LaTeX}.

\subsection{Math Equations}
You may want to display math equations in three distinct styles:
inline, numbered or non-numbered display.  Each of
the three are discussed in the next sections.

\subsubsection{Inline (In-text) Equations}
A formula that appears in the running text is called an
inline or in-text formula.  It is produced by the
\textbf{math} environment, which can be
invoked with the usual \texttt{{\char'134}begin\,\ldots{\char'134}end}
construction or with the short form \texttt{\$\,\ldots\$}. You
can use any of the symbols and structures,
from $\alpha$ to $\omega$, available in
\LaTeX~\cite{Lamport:LaTeX}; this section will simply show a
few examples of in-text equations in context. Notice how
this equation:
\begin{math}
  \lim_{n\rightarrow \infty}x=0
\end{math},
set here in in-line math style, looks slightly different when
set in display style.  (See next section).

\subsubsection{Display Equations}
A numbered display equation---one set off by vertical space from the
text and centered horizontally---is produced by the \textbf{equation}
environment. An unnumbered display equation is produced by the
\textbf{displaymath} environment.

Again, in either environment, you can use any of the symbols
and structures available in \LaTeX\@; this section will just
give a couple of examples of display equations in context.
First, consider the equation, shown as an inline equation above:
\begin{equation}
  \lim_{n\rightarrow \infty}x=0
\end{equation}
Notice how it is formatted somewhat differently in
the \textbf{displaymath}
environment.  Now, we'll enter an unnumbered equation:
\begin{displaymath}
  \sum_{i=0}^{\infty} x + 1
\end{displaymath}
and follow it with another numbered equation:
\begin{equation}
  \sum_{i=0}^{\infty}x_i=\int_{0}^{\pi+2} f
\end{equation}
just to demonstrate \LaTeX's able handling of numbering.

\subsection{Citations}
Citations to articles~\cite{bowman:reasoning,
clark:pct, braams:babel, herlihy:methodology},
conference proceedings~\cite{clark:pct} or maybe
books \cite{Lamport:LaTeX, salas:calculus} listed
in the Bibliography section of your
article will occur throughout the text of your article.
You should use BibTeX to automatically produce this bibliography;
you simply need to insert one of several citation commands with
a key of the item cited in the proper location in
the \texttt{.tex} file~\cite{Lamport:LaTeX}.
The key is a short reference you invent to uniquely
identify each work; in this sample document, the key is
the first author's surname and a
word from the title.  This identifying key is included
with each item in the \texttt{.bib} file for your article.

The details of the construction of the \texttt{.bib} file
are beyond the scope of this sample document, but more
information can be found in the \textit{Author's Guide},
and exhaustive details in the \textit{\LaTeX\ User's
Guide} by Lamport~\shortcite{Lamport:LaTeX}.

This article shows only the plainest form
of the citation command, using \texttt{{\char'134}cite}.

Some examples.  A paginated journal article \cite{Abril07}, an enumerated
journal article \cite{Cohen07}, a reference to an entire issue \cite{JCohen96},
a monograph (whole book) \cite{Kosiur01}, a monograph/whole book in a series (see 2a in spec. document)
\cite{Harel79}, a divisible-book such as an anthology or compilation \cite{Editor00}
followed by the same example, however we only output the series if the volume number is given
\cite{Editor00a} (so Editor00a's series should NOT be present since it has no vol. no.),
a chapter in a divisible book \cite{Spector90}, a chapter in a divisible book
in a series \cite{Douglass98}, a multi-volume work as book \cite{Knuth97},
an article in a proceedings (of a conference, symposium, workshop for example)
(paginated proceedings article) \cite{Andler79}, a proceedings article
with all possible elements \cite{Smith10}, an example of an enumerated
proceedings article \cite{VanGundy07},
an informally published work \cite{Harel78}, a doctoral dissertation \cite{Clarkson85},
a master's thesis: \cite{anisi03}, an online document / world wide web
resource \cite{Thornburg01, Ablamowicz07, Poker06}, a video game (Case 1) \cite{Obama08} and (Case 2) \cite{Novak03}
and \cite{Lee05} and (Case 3) a patent \cite{JoeScientist001},
work accepted for publication \cite{rous08}, 'YYYYb'-test for prolific author
\cite{SaeediMEJ10} and \cite{SaeediJETC10}. Other cites might contain
'duplicate' DOI and URLs (some SIAM articles) \cite{Kirschmer:2010:AEI:1958016.1958018}.
Boris / Barbara Beeton: multi-volume works as books
\cite{MR781536} and \cite{MR781537}.

A couple of citations with DOIs: \cite{2004:ITE:1009386.1010128,
  Kirschmer:2010:AEI:1958016.1958018}.

Online citations: \cite{TUGInstmem, Thornburg01, CTANacmart}.

\subsection{Tables}
Because tables cannot be split across pages, the best
placement for them is typically the top of the page
nearest their initial cite.  To
ensure this proper ``floating'' placement of tables, use the
environment \textbf{table} to enclose the table's contents and
the table caption.  The contents of the table itself must go
in the \textbf{tabular} environment, to
be aligned properly in rows and columns, with the desired
horizontal and vertical rules.  Again, detailed instructions
on \textbf{tabular} material
are found in the \textit{\LaTeX\ User's Guide}.

Immediately following this sentence is the point at which
Table~\ref{tab:freq} is included in the input file; compare the
placement of the table here with the table in the printed
output of this document.

\begin{table}
  \caption{Frequency of Special Characters}
  \label{tab:freq}
  \begin{tabular}{ccl}
    \toprule
    Non-English or Math&Frequency&Comments\\
    \midrule
    \O & 1 in 1,000& For Swedish names\\
    $\pi$ & 1 in 5& Common in math\\
    \$ & 4 in 5 & Used in business\\
    $\Psi^2_1$ & 1 in 40,000& Unexplained usage\\
  \bottomrule
\end{tabular}
\end{table}

To set a wider table, which takes up the whole width of the page's
live area, use the environment \textbf{table*} to enclose the table's
contents and the table caption.  As with a single-column table, this
wide table will ``float'' to a location deemed more desirable.
Immediately following this sentence is the point at which
Table~\ref{tab:commands} is included in the input file; again, it is
instructive to compare the placement of the table here with the table
in the printed output of this document.

\begin{table*}
  \caption{Some Typical Commands}
  \label{tab:commands}
  \begin{tabular}{ccl}
    \toprule
    Command &A Number & Comments\\
    \midrule
    \texttt{{\char'134}author} & 100& Author \\
    \texttt{{\char'134}table}& 300 & For tables\\
    \texttt{{\char'134}table*}& 400& For wider tables\\
    \bottomrule
  \end{tabular}
\end{table*}
% end the environment with {table*}, NOTE not {table}!

It is strongly recommended to use the package booktabs~\cite{Fear05}
and follow its main principles of typography with respect to tables:
\begin{enumerate}
\item Never, ever use vertical rules.
\item Never use double rules.
\end{enumerate}
It is also a good idea not to overuse horizontal rules.

\subsection{Figures}

Like tables, figures cannot be split across pages; the best placement
for them is typically the top or the bottom of the page nearest their
initial cite.  To ensure this proper ``floating'' placement of
figures, use the environment \textbf{figure} to enclose the figure and
its caption.

This sample document contains examples of \texttt{.eps} files to be
displayable with \LaTeX.  If you work with pdf\LaTeX, use files in the
\texttt{.pdf} format.  Note that most modern \TeX\ systems will convert
\texttt{.eps} to \texttt{.pdf} for you on the fly.  More details on
each of these are found in the \textit{Author's Guide}.

\begin{figure}
\includegraphics{fly}
\caption{A sample black and white graphic.}
\end{figure}

\begin{figure}
\includegraphics[height=1in, width=1in]{fly}
\caption{A sample black and white graphic
that has been resized with the \texttt{includegraphics} command.}
\end{figure}

As was the case with tables, you may want a figure that spans two
columns.  To do this, and still to ensure proper ``floating''
placement of tables, use the environment \textbf{figure*} to enclose
the figure and its caption.  And don't forget to end the environment
with \textbf{figure*}, not \textbf{figure}!

\begin{figure*}
\includegraphics{flies}
\caption{A sample black and white graphic
that needs to span two columns of text.}
\end{figure*}

\begin{figure}
\includegraphics[height=1in, width=1in]{rosette}
\caption{A sample black and white graphic that has
been resized with the \texttt{includegraphics} command.}
\end{figure}

\subsection{Theorem-like Constructs}

Other common constructs that may occur in your article are the forms
for logical constructs like theorems, axioms, corollaries and proofs.
ACM uses two types of these constructs:  theorem-like and
definition-like.

Here is a theorem:
\begin{theorem}
  Let $f$ be continuous on $[a,b]$.  If $G$ is
  an antiderivative for $f$ on $[a,b]$, then
  \begin{displaymath}
    \int^b_af(t)\,dt = G(b) - G(a).
  \end{displaymath}
\end{theorem}

Here is a definition:
\begin{definition}
  If $z$ is irrational, then by $e^z$ we mean the
  unique number that has
  logarithm $z$:
  \begin{displaymath}
    \log e^z = z.
  \end{displaymath}
\end{definition}

The pre-defined theorem-like constructs are \textbf{theorem},
\textbf{conjecture}, \textbf{proposition}, \textbf{lemma} and
\textbf{corollary}.  The pre-defined de\-fi\-ni\-ti\-on-like constructs are
\textbf{example} and \textbf{definition}.  You can add your own
constructs using the \textsl{amsthm} interface~\cite{Amsthm15}.  The
styles used in the \verb|\theoremstyle| command are \textbf{acmplain}
and \textbf{acmdefinition}.

Another construct is \textbf{proof}, for example,

\begin{proof}
  Suppose on the contrary there exists a real number $L$ such that
  \begin{displaymath}
    \lim_{x\rightarrow\infty} \frac{f(x)}{g(x)} = L.
  \end{displaymath}
  Then
  \begin{displaymath}
    l=\lim_{x\rightarrow c} f(x)
    = \lim_{x\rightarrow c}
    \left[ g{x} \cdot \frac{f(x)}{g(x)} \right ]
    = \lim_{x\rightarrow c} g(x) \cdot \lim_{x\rightarrow c}
    \frac{f(x)}{g(x)} = 0\cdot L = 0,
  \end{displaymath}
  which contradicts our assumption that $l\neq 0$.
\end{proof}

\section{Conclusions}
This paragraph will end the body of this sample document.
Remember that you might still have Acknowledgments or
Appendices; brief samples of these
follow.  There is still the Bibliography to deal with; and
we will make a disclaimer about that here: with the exception
of the reference to the \LaTeX\ book, the citations in
this paper are to articles which have nothing to
do with the present subject and are used as
examples only.
%\end{document}  % This is where a 'short' article might terminate

\appendix
%Appendix A
\section{Headings in Appendices}
The rules about hierarchical headings discussed above for
the body of the article are different in the appendices.
In the \textbf{appendix} environment, the command
\textbf{section} is used to
indicate the start of each Appendix, with alphabetic order
designation (i.e., the first is A, the second B, etc.) and
a title (if you include one).  So, if you need
hierarchical structure
\textit{within} an Appendix, start with \textbf{subsection} as the
highest level. Here is an outline of the body of this
document in Appendix-appropriate form:
\subsection{Introduction}
\subsection{The Body of the Paper}
\subsubsection{Type Changes and  Special Characters}
\subsubsection{Math Equations}
\paragraph{Inline (In-text) Equations}
\paragraph{Display Equations}
\subsubsection{Citations}
\subsubsection{Tables}
\subsubsection{Figures}
\subsubsection{Theorem-like Constructs}
\subsubsection*{A Caveat for the \TeX\ Expert}
\subsection{Conclusions}
\subsection{References}
Generated by bibtex from your \texttt{.bib} file.  Run latex,
then bibtex, then latex twice (to resolve references)
to create the \texttt{.bbl} file.  Insert that \texttt{.bbl}
file into the \texttt{.tex} source file and comment out
the command \texttt{{\char'134}thebibliography}.
% This next section command marks the start of
% Appendix B, and does not continue the present hierarchy
\section{More Help for the Hardy}

Of course, reading the source code is always useful.  The file
\path{acmart.pdf} contains both the user guide and the commented
code.

\begin{acks}
  The authors would like to thank Dr. Yuhua Li for providing the
  MATLAB code of the \textit{BEPS} method.

  The authors would also like to thank the anonymous referees for
  their valuable comments and helpful suggestions. The work is
  supported by the \grantsponsor{GS501100001809}{National Natural
    Science Foundation of
    China}{http://dx.doi.org/10.13039/501100001809} under Grant
  No.:~\grantnum{GS501100001809}{61273304}
  and~\grantnum[http://www.nnsf.cn/youngscientists]{GS501100001809}{Young
    Scientists' Support Program}.

\end{acks}


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

\include{preamble}

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

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

\date{Wednesday, April 28, \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 70 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

%$\cprobsub{1}{X}{\theta} = \binomial{n}{\theta}$ with $n$ fixed, $\probsub{2}{\theta} = \betanot{\alpha}{\beta}$, $\cprobsub{3}{X}{\theta} = \poisson{\theta}$,  $\probsub{4}{\theta} = \gammanot{\alpha}{\beta}$, $\cprobsub{3}{X}{\theta} = \poisson{\theta}$

\problem\timedsection{7} Let $\cprob{X}{\theta} \sim \poisson{\theta}$, $\prob{\theta} =\invgammanot{\alpha}{\beta}$ and $\alpha, \beta > 0$.

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

\begin{enumerate}[(a)]
\item $\prob{\theta}$ is the conjugate prior for the parametric model given by $\cprob{X}{\theta}$
\item $\prob{\theta}$ has the same support as the parameter space of $\cprob{X}{\theta}$
\item $\cprob{\theta}{X}$ is a mixture / compound distribution
\item $\prob{X}$ is a mixture / compound distribution
\item The kernel of $\cprob{\theta}{X}$ is $\theta^{x - \alpha - 1} e^{-(\theta + \beta / \theta)}$
\item The kernel of $\cprob{\theta}{X}$ is $\theta^x / x!$
\item The posterior is gamma-distributed
\item The normalization constant of $\cprob{\theta}{X}$ is given by $\beta^\alpha / (x! \Gammaf{\alpha})$
\item The posterior predictive distribution can be computed via $\int_0^\infty \cprob{X}{\theta} \prob{\theta} d\theta$
\item The posterior predictive distribution rv will have higher variance than the Poisson rv
\item The posterior predictive distribution rv will have one parameter: $\theta$
\item The posterior predictive distribution rv will have two parameters: $\alpha, \beta$
\item The posterior predictive distribution rv will have support $(0, \infty)$
\end{enumerate}
\eenum\instr\pagebreak

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

\problem\timedsection{8} Let $\cprob{X}{\theta} \sim \poisson{\theta}$ and then consider the reparameterization of $\theta$ called $\phi := t(\theta) = \natlog{\theta}$ and $\theta = t^{-1}(\phi) = e^\phi$ so that $\cprob{X}{\phi} = e^{x \phi \displaystyle-e^\phi} / x!$

%\beqn
%\cprob{X}{\phi} = e^{-x \phi} e^{\displaystyle-e^\phi} / x!
%\eeqn

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

\begin{enumerate}[(a)]
\item To compute the Jeffrey's prior, the only information necessary is the likelihood function of $\mathcal{F}$
\item $\probsub{J}{\theta} = \gammanot{1/2}{0}$
\item $\ell(\phi; x) = x \phi - e^\phi - \natlog{x!}$
\item $\ell'(\phi; x) = x  - e^\phi$
%\item $\ell''(\phi; x) = - e^\phi$
\item $\probsub{J}{\phi} \propto e^{\phi}$
\item $\probsub{J}{\phi} \propto e^{\phi / 2}$
\item If you parameterize by $\theta$ and use $\probsub{J}{\theta}$ to derive the posterior, you will get the same point estimates $\thetahatmmse, \thetahatmmae, \thetahatmap$ if you parameterize by $\phi$ and use $\probsub{J}{\phi}$ to derive the posterior.
\item $f_1(\phi) := \probsub{J}{\phi}$ can be derived from $f_2(\theta) := \probsub{J}{\theta}$ using the change of variables equation, $f_1(\phi) = f_2(t^{-1}(\theta)) \abss{\frac{d}{d\phi}[t^{-1}(\theta)]}$
\end{enumerate}
\eenum\instr\pagebreak

%\problem\timedsection{7} Let $\cprob{X}{\theta} \sim \poisson{\theta}$ and let $\phi := t(\theta) = 1 / \theta$ so that $\cprob{X}{\phi} = \phi^{-x} e^\phi / x!$.
%
%\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{10}
%
%\begin{enumerate}[(a)]
%\item $\probsub{J}{\theta} = \gammanot{1/2}{0}$
%\item $\ell(\phi; x) = -x \natlog{\phi} + \phi - \natlog{x!}$
%\item $\ell'(\phi; x) = 1 - x \phi^{-1}$
%%\item $\ell''(\phi; x) = x \phi^{-2}$
%%\item $I(\phi; x) = -\phi^{-3}$
%\item $\probsub{J}{\phi} = \gammanot{-2}{0}$
%\end{enumerate}
%\eenum\instr\pagebreak

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

\problem\timedsection{13} We are looking to understand the annual risk of heart attacks in men with high cholesterol as defined by the mean number of annual heart attacks. In 2020, among $n = 20$ men with high cholesterol, one man had two heart attacks, four men had one heart attack and the other men had no heart attacks. An iid Poisson parametric model seems to be a reasonable choice for these annual heart attack events. We have absolutely no prior information so we are most comfortable employing a Haldane-type ignorance prior. All quantities below are rounded to the nearest two decimals.

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

\begin{enumerate}[(a)]
\item $\thetahatmle = 0.30$
\item $\prob{\theta} = \gammanot{1}{0}$
\item $\thetahatmmse = 0.35$
\item Given quantities provided, you need a computing device to compute $\thetahatmmae$
\item $\thetahatmmse = 0.30$
\item $\thetahatmap = 0.35$
\item $\thetahatmap = 0.30$
\item Under this Haldane prior we chose, $\thetahatmmse$ will shrink towards $x_0 = 0$
\item A 95\% credible region for $\theta$ can be constructed by computing [qgamma(0.025, 6, 20), qgamma(0.975, 6, 20)]
\item To test the theory that high cholesterol induces higher heart attack risk than the average risk of 0.07, you can calculate pgamma(0.07, 6, 20) and compare it to your significance level (which is usually set to be 5\%)
\item Using this data and prior, you can compute to the nearest two decimals the probability that a randomly selected man with high cholesterol having zero heart attacks in a year would be zero.
\item Using this data and prior, it is possible to compute to the nearest two decimals the probability that a randomly selected man with high cholesterol having one heart attack in a year would be $\theta e^\theta$.
\item Using this data and prior, it is possible to compute to the nearest two decimals the probability that a randomly selected man with high cholesterol having one heart attack in a year would be $(20 / 21)^7$.
\item Using this data and prior, it is possible to compute to the nearest two decimals the probability that a randomly selected man with high cholesterol having one heart attack in a year would be $6 \times 20^6 / 21^7$.
\end{enumerate}
\eenum\instr\pagebreak

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

\problem\timedsection{14} We are looking to understand IQ levels in adults that were exposed to a toxin in youth. We sample $n = 7$ adults exposed to the toxin and measure IQ's of 79,  91, 103,  87,  86,  91 and  75 and thus $\xbar = 87.43$. The normal iid model is a reasonable model as the population IQ metric has been demonstrated to be normally distributed. It is also known that the population variance of IQ scores is $\sigsq = 15^2$ since the scale is designed pegged to that value (so it seems reasonable to assume the value of $\sigsq$ is known in our setting). You have no strong prior belief on the IQ levels in these adults after being exposed to a toxin so you can employ any of the three principled objective priors we discussed found in the class lectures. All quantities are rounded to the nearest two decimals.

%x = c(79,  91, 103,  87,  86,  91, 75)
%mean(x)

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

\begin{enumerate}[(a)]
\item $\prob{\theta} = \normnot{0}{0} \propto 1$
\item The kernel of the posterior can be expressed as $e^{a\theta - b\theta^2}$ where $a,b$ are functions of $\xoneton$, $\sigsq$ and fundamental constants (but not functions of $\theta$)
\item $\thetahatmmse = 87.43$
\item $\thetahatmap = \xbar$
\item Given quantities provided, you need a computing device to compute $\thetahatmmae$
\item A 98\% credible region can be constructed by computing [qnorm(0.01, $\xbar, \sqrt{15}$), qnorm(0.99, $\xbar, \sqrt{15}$)]
\item A 98\% credible region can be constructed by computing [qnorm(0.01, $\xbar, \sqrt{15} / 7$), qnorm(0.99, $\xbar, \sqrt{15} / 7$)]
\item The mean IQ in the overall population is pegged to be 100. To test the theory that this toxin exposure in youth results in a below-mean IQ, you can calculate pnorm(100, $\theta_p$, $\sigsq_p$) where $\theta_p$ is the posterior mean and $\sigsq_p$ is the posterior variance and compare it to your significance level (which is usually set to be 5\%)
\item Under this prior we chose, $\thetahatmmse$ will shrink towards $x_0 = 100$, the true population mean.
\item This prior we chose is equivalent to seeing $n_0 = 7$ pseudoobservations
\item Using this data and prior, you can compute the probability that a randomly selected man (exposed to the toxin in youth) will have an adult IQ less than 100 is pnorm(100, 87.43, 4.14)
\item A 95\% posterior predictive interval can be computed as [qnorm(0.025, $\theta_p$, $\sqrt{\sigsq + \sigsq_p}$), qnorm(0.975, $\theta_p$, $\sqrt{\sigsq + \sigsq_p}$)]
\item A 95\% posterior predictive interval can be computed as \\ ~[qt.scaled(0.025, 7, $\theta_p$, $\sqrt{\sigsq + \sigsq_p}$), qt.scaled(0.975, 7, $\theta_p$, $\sqrt{\sigsq + \sigsq_p}$)]
\end{enumerate}
\eenum\instr\pagebreak

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

\problem\timedsection{14} We are looking to understand annual financial returns of a mutual fund for the NYC teachers' retirement system. The fund is guaranteed to produce a mean return of 7.5\% over 40 years since inception but is allowed to vary its return year-year based on natural variation in the world economy. You unfortunately have only $n=3$  years of data to use: you see returns 18\%, -3\% and 4\%. Although it is probably not an appropriate parametric model for annual return, assume yearly returns are iid normal with known $\theta = 7.5\%$. For now, you have no strong prior belief on variance in these returns so you should employ the standard uninformative prior in this situation, the Jeffrey's prior. All quantities below are rounded to the nearest two digits.

%x = c(79,  91, 103,  87,  86,  91, 75)
%mean(x)

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

\begin{enumerate}[(a)]
\item $\prob{\theta} \propto 1$
\item $\sigsqhatmle = 77.58$
\item $\sigsqhatmap = 77.58$
\item Given quantities provided, you need a computing device to compute $\sigsqhatmmae$
\item $\cprob{\sigsq}{X, \theta} = \invgammanot{1.50}{116.38}$
\item $\cprob{\sigsq}{X, \theta} = \invgammanot{1.50}{114.33}$
\item $\cprob{\sigsq}{X, \theta} = \invgammanot{3.00}{232.75}$
\item $\cprob{\sigsq}{X, \theta} = \invgammanot{0}{0}$
\item $\cprob{\sigsq}{X, \theta} = k(\sigsq\,|\,X,\theta)$
\item A 98\% credible region can be constructed by computing [qinvgamma(0.01, $n/2, ns^2 / 2$), qinvgamma(0.99, $n/2, ns^2 / 2$)] where $s^2 = \oneover{n-1}\sum_{i=1}^n (x_i - \xbar)^2$
%\item The mean IQ in the overall population is pegged to be 100. To test the theory that this toxin exposure in youth results in a below-mean IQ, you can calculate pnorm(100, $\theta_p$, $\sigsq_p$) where $\theta_p$ is the posterior mean and $\sigsq_p$ is the posterior variance and compare it to the significance level which is usually set to be 5\%
%\item Under this prior we chose, $\thetahatmmse$ will shrink towards $x_0 = 100$, the true population mean.
%\item This prior we chose is equivalent to seeing $n_0 = 7$ pseudoobservations
\item Using this data and prior, you can compute the probability that the variance of the returns is less than $5\%^2$ is pinvgamma($5\%^2, n/2, n\sigsqhatmle/2$)
\item The probability that the annual return next year will be more than 10\% can be exactly computed via \\ 1 - pt.scaled($3, 10\%, 7.5\%, \sqrt{\sigsqhatmle}$)
\item The probability that the annual return next year will be more than 10\% can be approximated accurately via computing 1 - pnorm($10\%, 7.5\%, \sqrt{\sigsqhatmle}$)
\end{enumerate}
\eenum\instr\pagebreak

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

\problem\timedsection{14} \ingray{We are looking to understand annual financial returns of a mutual fund for the NYC teachers' retirement system. The fund is guaranteed to produce a mean return of 7.5\% over 40 years since inception but is allowed to vary its return year-year based on natural variation in the world economy. You unfortunately have only $n=3$  years of data to use: you see returns 18\%, -3\% and 4\%. Although it is probably not an appropriate parametric model for annual return, assume yearly returns are iid normal with known $\theta = 7.5\%$.} Since we only have $n=3$, inference for $\sigsq$ will be very inaccurate as $\var{\sigsq~|~X, \theta}$ from Problem 5 is too high to be useful. We wish to \qu{borrow estimation strength} by examining other past financial products that are similar to this NYC teachers' retirement system mutual fund. Using the data from other financial products, we fit the following distribution to their lifetime variances: $\cprob{\sigsq}{\theta} = \invgammanot{5.93}{18.17}$ and use this as our prior to perform our Bayesian statistical inference. \ingray{All quantities below are rounded to the nearest two digits.}

%x = c(79,  91, 103,  87,  86,  91, 75)
%mean(x)

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

\begin{enumerate}[(a)]
\item $\prob{\sigsq} \propto 1$
\item This prior is informative
\item Using this prior, the inference would constitute an example of \qu{Empirical Bayes} in action
\item This prior's strength is approximately equivalent to observing 12 annual returns of the NYC teachers' retirement system mutual fund that we are investigating
\item To generate this prior, we looked at 12 similar past financial products
\item Under this prior, you expect $\sigsq$ to be 3.69
\item If you employ $\sigsqhatmmse$ for point estimation, your point estimate will be weighted more towards the prior's expectation than the maximum likelihood estimate from the data
\item $\sigsqhatmle = 20.92$
\item $\sigsqhatmmse = 20.92$
\item We estimate that the variance of returns for the NYC teachers' retirement system mutual fund is now smaller than previously when we employed the Jeffrey's prior
\item You need a computing device to compute $\sigsqhatmmae$
\item $\cprob{X_*}{X, \theta} = T_{n+n_0}(\theta, \sigsqhatmmse)$
%\item A 98\% credible region can be constructed by computing [qinvgamma(0.01, $n/2, ns^2 / 2$), qinvgamma(0.99, $n/2, ns^2 / 2$)] where $s^2 = \oneover{n-1}\sum_{i=1}^n (x_i - \xbar)^2$
%\item Using this data and prior, you can compute the probability that the variance of the returns is less than $5\%^2$ is pinvgamma($5\%^2, n/2, n\sigsqhatmle/2$)
\item The probability that the annual return next year will be more than 10\% can be exactly computed via \\ 1 - pt.scaled($3, 10\%, 7.5\%, \sqrt{20.92}$)
%\item The probability that the annual return next year will be more than 10\% can be approximated accurately via computing 1 - pnorm($10\%, 7.5\%, \sqrt{\sigsqhatmle}$)
\end{enumerate}
\eenum\instr\pagebreak

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

\end{document}

x = c(79,  91, 103,  87,  86,  91, 75)
mean(x)
n = length(x)
sigsq = 15

sqrt(sigsq + sigsq / n)

x =c(18,-3, 4)
n = length(x)
theta = 7.5
mean(x)
sigsqhatmle = sum((x - theta)^2) / n
sigsqhatmle
sigsqhatmle * n / 2
ssq = sum((x - mean(x))^2) / n
ssq * n / 2

alpha = 5.93
beta = 18.17
beta / (alpha - 1)
n0 = alpha * 2
sigsq0 = beta * 2 / n0
n0
sigsq0

(sigsqhatmle * n + sigsq0 * n0) / (n + n0 - 2)
(n0 - 2) / (n+n0-2)

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


\section{Introduction}
\labsec{intro}

Ever since the sequence of the human genome was made available some 20
years ago, \sidecite[-1\baselineskip]{Lander2001a} one of the most
compelling goals in both science and medicine has been that of finding
the genetic variants associated to diseases.

% The first attempts relied on statistical association tests, like the
% $\chi^2$ test, to identify the genetic variants that are more likely to
% occur in ill rather than healthy individuals. \sidecite{Visscher2012a}
% Despite its successes, the power of this approach is hampered by the
% fact that there are so many genetic variants (on average, the DNA
% sequences of two non-related people differ of about one letter every one
% thousand \sidecite{Durbin2010}), most of which, the so-called rare
% variants, appear only in a tiny fraction of individuals.

In 2015 it was proposed a new method,
\sidecite[-1.1cm]{Gamazon2015a} dubbed Transcriptome-Wide
Association Study (TWAS), which consists of using the genetic variants
to predict gene expression, and then finding associations between the
predicted expression and a disease. The first advantage is that it is
possible to avoid a direct measurement of gene expression, which can be
expensive or even impossible for certain tissues. But another, more
subtle advantage is that predicted values are less noisy than the real
ones, because they do not include the environmental component of
expression (\refeq{expr}). In view of this method, predicting gene
expression is a relevant problem.

A gene is a region of DNA, and its expression can be defined as the
amount of RNA molecules that originate from that region
(\vreffig{response}). This amount is different in different individuals,
and our goal is to predict these differences.

\begin{marginfigure}[-3.8cm]
  \includegraphics{response}
  \caption{Each gene is \enquote{transcribed} into many RNA molecules,
which then are \enquote{translated} into proteins.}
  \labfig{response}
\end{marginfigure}

As regards the DNA itself, in this context it can be regarded as a
3-billion letter long string, different for each individual. Most of the
positions in the sequence are the same, but there are some specific
positions, called polymorphic loci, which harbour different letters in
different people (\reffig{predictorsA}). There are many kinds of
differences, but, as similar works do,
\sidecite[-8.6cm]{Nagpal2019} here we will focus only on
single-letter differences such that in the population only two letters
are present, \ie we will only consider those positions that can be in
two states.

\begin{marginfigure}[-1.5cm]
\setlength{\tabcolsep}{2pt}
\scalebox{0.7}{%
\begin{subfigure}{\textwidth}
  \caption{}
  \labfig{predictorsA}
  \small
  \texttt{%
  \begin{tabular}{l c c c c c c c c c c c c c c c c c c}
    \textsf{Alice\hspace{.2cm}} & A & T & G & \cellcolor{yellow}G & T & G & \cellcolor{cyan}C   & T & G & T & C & T & C & C & T & G & \cellcolor{red}T  & C \\
    \textsf{Bob\hspace{.2cm}}   & A & T & G & \cellcolor{yellow}G  & T & G & \cellcolor{cyan}C  & T & G & T & C & T & C & C & T & G & \cellcolor{cyan}C & C \\
    \textsf{Craig\hspace{.2cm}} & A & T & G & \cellcolor{green}A   & T & G & \cellcolor{green}A & T & G & T & C & T & C & C & T & G & \cellcolor{cyan}C & C \\
    \textsf{Dave\hspace{.2cm}}  & A & T & G & \cellcolor{green}A   & T & G & \cellcolor{cyan}C  & T & G & T & C & T & C & C & T & G & \cellcolor{cyan}C & C \\
    \textsf{Eve\hspace{.2cm}}   & A & T & G & \cellcolor{yellow}G  & T & G & \cellcolor{green}A & T & G & T & C & T & C & C & T & G & \cellcolor{red}T  & C \\
    \textsf{Frank\hspace{.2cm}} & A & T & G & \cellcolor{yellow}G  & T & G & \cellcolor{green}A & T & G & T & C & T & C & C & T & G & \cellcolor{cyan}C & C \\
  \end{tabular}}
\end{subfigure}
}

\scalebox{0.7}{%
\begin{subfigure}{\textwidth}
  \caption{}
  \labfig{predictorsB}
  \small
  \texttt{%
  \begin{tabular}{l c c c c c c c c c c c c c c c c c c}
    \textsf{Alice\hspace{.2cm}} & \phantom{A} & \phantom{A} & \phantom{A} & 0 & \phantom{A} & \phantom{A} & 0 & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & 1 & \phantom{A} \\
    \textsf{Bob\hspace{.2cm}}   & \phantom{A} & \phantom{A} & \phantom{A} & 0 & \phantom{A} & \phantom{A} & 0 & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & 0 & \phantom{A} \\
    \textsf{Craig\hspace{.2cm}} & \phantom{A} & \phantom{A} & \phantom{A} & 1 & \phantom{A} & \phantom{A} & 1 & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & 0 & \phantom{A} \\
    \textsf{Dave\hspace{.2cm}}  & \phantom{A} & \phantom{A} & \phantom{A} & 1 & \phantom{A} & \phantom{A} & 0 & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & 0 & \phantom{A} \\
    \textsf{Eve\hspace{.2cm}}   & \phantom{A} & \phantom{A} & \phantom{A} & 0 & \phantom{A} & \phantom{A} & 1 & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & 1 & \phantom{A} \\
    \textsf{Frank\hspace{.2cm}} & \phantom{A} & \phantom{A} & \phantom{A} & 0 & \phantom{A} & \phantom{A} & 1 & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & \phantom{A} & 0 & \phantom{A} \\
  \end{tabular}}
\end{subfigure}
}
\caption{(a) Examples of DNA sequences; polymorphic loci are highlighted
with different colours. (b) Predictors matrix built from the sequences
in (a).}
\labfig{predictors}
\end{marginfigure}

DNA lends itself to a representation where the positions that are the
same in all the individuals are ignored, and the two possible letters of
these polymorphic loci are encoded as zeroes or ones
(\reffig{predictorsB}). Indeed, all the published works on this topic
use a matrix of this kind, constructed on a window of one million
letters around each gene. Moreover, each gene is treated independently
of all the others, and one model is run for each gene. The expression of
gene $A$ in individual $i$ can be modeled as in \vrefeq{expr}, where
$X_{ij}$ is the genotype (0 or 1) of individual $i$ at locus $j$, and
$\beta_j$ is the increase in expression for individuals that have a 1 at
locus $j$ with respect to the other individuals. Importantly, this is an
additive model and no interactions are considered.

%\marginnote[-1.3cm]{%
\begin{equation}
  \labeq{expr}
  Y_i^{(A)} = \overbrace{\underbrace{\sum_{j=0}^p \beta_j^{(A)} X_{ij}^{(A)}}_{\text{Genetic effect ($\hat{Y}^{(A)}$)}} + \underbrace{\epsilon_i^{(A)}}_{\text{Environmental effect}}}^{\text{Theoretical real expression}}
\end{equation}
%}