\documentclass[modern, letterpaper]{aastex631}

\usepackage{microtype}
\usepackage{amsmath}

\newcommand{\package}[1]{\textsf{#1}}
\newcommand{\project}[1]{\textsl{#1}}
\newcommand{\acronym}[1]{{\small{#1}}}
\newcommand{\Gaia}{\project{Gaia}}
\newcommand{\dd}{\ensuremath{\,\mathrm{d}}}

\shorttitle{Unresolved spectroscopic binaries with \Gaia\ I}
\shortauthors{Foreman-Mackey et al.}

\begin{document}

\title{An open source scientific article}

\title{Unresolved spectroscopic binaries with \Gaia\ I: \\
	Detection \& orbital characterization using EDR3 radial velocity errors}

\correspondingauthor{Daniel~Foreman-Mackey}
\email{dforeman-mackey@flatironinstitute.org}

\author[0000-0002-9328-5652]{Daniel Foreman-Mackey}
\affiliation{Center for Computational Astrophysics, Flatiron Institute, 162 5th Ave, New York, NY 10010, USA}

\author{Quadry~Chance}
\affiliation{Center for Computational Astrophysics, Flatiron Institute, 162 5th Ave, New York, NY 10010, USA}
\affiliation{University of Florida, 211 Bryant Space Science Center, Gainesville, FL 32611, USA}

\author{Andrew~R.~Casey}
\affiliation{School of Physics \& Astronomy, Monash University, Wellington Road, Clayton 3800, Victoria, Australia}

\author{Others TBD}
\noaffiliation{}

\begin{abstract}
	The Early Data Release 3 (EDR3) from the \Gaia\ Mission includes measurements of the radial velocities (RVs) of more than 7 million targets with the goal of providing a detailed view of the phase space of the Milky Way.
	Many of these stars are, in fact, members of unresolved multiple star systems and the effects of these orbits are generally seen as a source of contamination in the RV catalog.
	In this paper, we demonstrate that the published RV error estimates can provide evidence for the existence of an unresolved stellar companion and, under the assumption of a gravitationally-bound binary system, place constraints its orbital parameters.
	This paper makes three key contributions:
	\emph{(1)} We build a flexible data-driven model to estimate \Gaia's intrinsic per-transit RV uncertainty as a function of color and apparent magnitude.
	\emph{(2)} Using these estimates, we develop a procedure for detecting likely unresolved multiple star systems, and apply this procedure to all targets observed at least 3 times in EDR3.
	\emph{(3)} Finally, we derive a physical model for constraining the orbital parameters of a binary system that would be consistent with the observed RV error and number of RV measurements.
	These parameters are generally only weakly constrained, except for the RV semi-amplitude, which can typically be estimated with a precision of $\sim PPP\%$.
	To demonstrate the reliability of this method and quantify its completeness, we apply our method to simulated data and catalogs of previously known spectroscopic binaries.
	We expect that this value-added catalog will be useful for population studies of binary star systems, target selection for precise RV surveys, vetting exoplanet discoveries, and other applications.
\end{abstract}

\keywords{Astrostatistics (1882) --- Binary stars (154) --- Radial velocity (1332)}

\section{Introduction} \label{sec:intro}

By the end of the \Gaia\ Mission, it will discover SOME LARGE NUMBER of exoplanet and multiple star systems based on time resolved astrometry and radial velocities of many targets.
In the meantime, we only have static measurements of the radial velocity, but it turns out that there is still information in the existing public facing catalog to place constraints on the orbital parameters of multiple systems using the \Gaia\ data.
It has been previously demonstrated that the published radial velocity and astrometric ``errors'' can be used a proxy for multiplicity or data quality.
In this paper, however, we demonstrate that it is possible to use the reported radial velocity errors and a probabilistic model to place constraints on the radial velocity amplitude and, in some cases, some other properties of the orbit.
These measurements are useful for many applications, including \emph{(a)} discovering black holes, \emph{(b)} vetting transiting exoplanet discoveries, \emph{(c)} measuring the masses of a large sample of eclipsing binaries, \emph{(d)} quantify the binary fraction across the H--R diagram, and \emph{(e)} informing constraints on exoplanet formation and evolution theory, to name a few.

The fundamental idea of this paper is that even though the \Gaia\ catalog only includes a single RV measurement and its error, we can still make calibrated probabilistic inferences about the RV time series.
In particular, we know the number of RV observations that were used to measure the RV and its uncertainty.
Similarly, we can estimate the expected per-transit RV uncertainty based on the distribution of targets of similar color and magnitude.
Using these auxillary values, we can compute the expected distribution of RV error, and compare this to the measured value.
It has been previously identified that the RV error measurements scale with RV semi-amplitude (IS THERE A CITATION; add a figure), but below we take this one step further and derive an interpretable, physical, probabilistic model for this effect that allows us to directly infer the semi-amplitude, and weak constraints on the other orbital parameters.

In the following sections, we derive and discuss the components of this model, validate and characterize the performance using simulated data, and compare to archival binary surveys.
To start, we build a hierarchical probabilistic model to estimate the per-transit RV measurement uncertainty of the \Gaia\ survey as a function of apparent magnitude and observed color.

\section{The basics}
\label{sect:basics}

For each RV target, the \Gaia\ EDR3 catalog reports the median RV, an estimate of the error on this median, and the number of individual RV measurements (called ``transits'') that were used in this estimate.
In our analysis, we ignore the median RV and instead only consider the RV error and number of transits.
The RV error $\epsilon_n$ for a target $n$ is computed as \citep{Katz19}
\begin{eqnarray}
	{\epsilon_n}^2 = \left(\sqrt{\frac{\pi}{2\,T_n}}\,s_n\right)^2 + 0.11^2
\end{eqnarray}
where
\begin{eqnarray}
	\label{eq:sample-var}
	{s_n}^2 = \frac{1}{T_n-1}\sum_{i=1}^{T_n} \left(v_{n,i} - \bar{v}_n\right)^2
\end{eqnarray}
is the sample variance of the individual RV measurements $v_{n,t}$.
In \autoref{eq:sample-var}, $t=1,\cdots,T_n$ indexes the $T_n$ transits for target $n$ and
\begin{eqnarray}
	\bar{v}_n = \frac{1}{T_n}\sum_{i=1}^{T_n} v_{n,i}
\end{eqnarray}
is the mean RV.
In all that follows, it is much simpler to reason about the sample variance ${s_n}^2$ instead of the error on the median ${\epsilon}_n$, but that isn't a problem because we can compute
\begin{eqnarray}
	{s_n}^2 = \frac{2\,T_n}{\pi}\left({\epsilon_n}^2 - 0.11^2\right)
\end{eqnarray}
for any target, using only values available in the catalog: $\epsilon_n$ and $T_n$.

Now, for a given target $n$, in the absence of any excess radial velocity signal, and if we know it's per-transit radial velocity measurement uncertainty $\sigma_n$, we would expect the quantity
\begin{eqnarray}
	{\xi_n}^2 &=& \frac{(T_n - 1)\,s_n^2}{{\sigma_n}^2}
	\label{eq:chi-sq-samp}
\end{eqnarray}
to be chi-squared distributed with $T_n - 1$ degrees of freedom.
Based on this argument, we can identify targets that are outliers with respect to this expectation.
\autoref{fig:infer_sigma:data} illustrates this idea using a simulated catalog of \Gaia\ RV targets.
Here, we generate a sample of \Gaia\ RV measurements for binary star systems with orbital parameters drawn from a simple population model, and compare the distribution of measured ${s_n}^2$ values to those that we would measure in the absence of binary companions.
In \autoref{fig:infer_sigma:data}, both distributions peak near the (known) measurement uncertainty $\sigma^2$---a fact that we will use in the following section to \emph{infer} $\sigma^2$---but the distribution of binary systems shows a tail to larger values of $s^2$.

We can even go one step further, and derive the expected sampling distribution for ${s_n}^2$ under some model for the excess RV signal.
For example, in our case, we want to infer the orbital properties of any binary systems based on the observed ${s_n}^2$.
In this case, our model for the expected RV time series is (CITE L\&F)
\begin{eqnarray}
	\mu_n(t;\,\theta_n) &=& v_{0,n} + K_n\,\left[\cos(f_n(t) + \omega_n) + e_n\,\cos \omega_n\right] \quad,
	\label{eq:rv-model}
\end{eqnarray}
but the following derivation can be used more generally.
We can evaluate \autoref{eq:rv-model}, at observation times $\{t_{n,i}\}_{i=1}^{T_n}$ and orbital parameters $\theta_n=\{K_n,\,e_n,\,\omega_n,\,\ldots\}$.
Of course, in our case, we don't actually \emph{know} the observation times, so we will need to marginalize those out below, but for now, let's proceed as if we do know them.
For a given set of parameters, it can be shown (see \autoref{app:ncx2}) that the sampling distribution for our scalar
\begin{eqnarray}
	{\xi_n}^2 &=& \frac{(T_n - 1)\,s_n^2}{{\sigma_n}^2}
	\label{eq:chi-sq-samp2}
\end{eqnarray}
is a non-central $\chi^2$ distribution with $T_n$ degrees of freedom, and non-centrality parameter
\begin{eqnarray}
	\lambda &=& \sum_{i=1}^{T_n} \left[\frac{\mu_n(t_i;\,\theta_n) - \bar{\mu}_n}{\sigma_n}\right]^2
	\label{eq:ncx2-nc-param}
\end{eqnarray}
where
\begin{eqnarray}
	\bar{\mu}_n &=& \frac{1}{T_n}\sum_{i=1}^{T_n} \mu_n(t_i;\,\theta_n)
\end{eqnarray}
is the sample mean of the model evaluated at the observation times.
This sampling distribution defines a likelihood function for the parameters $\theta_n$, that we will form the foundation for the probabilistic models built in the following sections.

In all the above, we assumed that the per-observation RV measurement uncertainty $\sigma_n$ was known.
This is not, however, a number that we have access to.
Instead, in the next section we derive a hierarchical model for determining a calibrated data-driven estimate of $\sigma_n$, as a function of color and apparent magnitude, based on the published EDR3 catalog.

\begin{figure}
	\begin{centering}
		\includegraphics[width=0.8\textwidth]{figures/infer_sigma_data.pdf}
		\caption{The distribution of RV sample variance ${s}^2$ for a simulated population of binary stars observed by \Gaia\ (unfilled histogram), compared to the distribution that would have been observed in the absence of companions (shaded histogram).
			The peak of each distribution is determined by the per-measurement RV uncertainty $\sigma^2$, but the catalog of binaries exhibits a heavy tail to large values of $s^2$, which we will use to detect these systems in the real \Gaia\ EDR3 catalog.}
		\label{fig:infer_sigma:data}
	\end{centering}
\end{figure}

\section{Estimating Gaia's per-transit RV measurement uncertainty}\label{sec:noise}

A key element of our analysis is that we must be able to make, for each target $n$ in our sample, a reasonably accurate estimate of the per-transit RV measurement uncertainty, called $\sigma_n$ in the previous section.
The \Gaia\ catalog does not include this value, but as we will demonstrate, it is possible to make an estimate of $\sigma_n$ for each target $n$, using the data available in the catalog.
In practice, we will build a data-driven model to infer $\sigma$ in small bins in observed $G_\mathrm{BP} - G_\mathrm{RP}$ color and apparent $G$-band magnitude $m_\mathrm{G}$, making the simplifying assumption that $\sigma$ is approximately constant at a given color and magnitude.
There are other factors, such as distance and stellar evolutionary state, that will affect the measurement uncertainty for a given target, but we find that including these axes has a negligible effect on the inferred $\sigma$, and the added complexity is not required for the level of precision required here.

To set up this model, we create a fine grid in color and magnitude, and then for each cell of this grid, we infer $\sigma_n$ independently of the other bins.
In each bin, we assume that all the targets are unresolved binaries with unknown RV semi-amplitude, and we fit for the orbital parameters of each target and the shared RV measurement uncertainty, simultaneously.
In principle, we could use the likelihood function defined in the previous section---and our assumption that the value of $\sigma_n$ is constant within a small enough color--magnitude bin---to simultaneously infer $\sigma_n$, $\theta_n$ for a sample of \Gaia\ targets.
This proposal is made somewhat more complicated by the fact that we also don't know the observation times $t_{n,i}$ and need to marginalize over them.
We discuss this in more detail in SOMESECTION, but here we will make the further simplifying assumptions to define a tractable effective model.
We restrict ourselves (for now) to circular orbits and assume that the orbit of each binary is well-sampled.
This means that we can approximate the non-centrality parameter as
\begin{eqnarray}
	\lambda_{\mathrm{eff},n} &=& \frac{T_n\,{K_n}^2}{2\,\pi\,{\sigma_n}^2}\int_{0}^{2\,\pi} \cos^2\phi \dd\phi = \frac{T_n\,{K_n}^2}{2\,{\sigma_n}^2} \quad,
	\label{eq:lambda-eff}
\end{eqnarray}
where $K_n$ is the unknown RV semi-amplitude of the orbit of target $n$.\footnote{Some intution for the factor of $T_n$ in \autoref{eq:lambda-eff} can be developed by recognizing that $\lambda$ is defined in \ref{eq:ncx2-nc-param} as $T_n$ times the sample variance of the model, evaluated at the observed times.}

Given this definition for $\lambda_{n,\mathrm{eff}}$, we can define the probabilistic model
\begin{eqnarray}
	p(\sigma_n,\,\{K_n\}\,|\,\{T_n,\,{s_n}^2\}) \propto
	p(\sigma_n)\,\prod_{n=1}^{N} p(K_n)\,p(T_n,\,{s_n}^2\,|\,\sigma_n,\,K_n)
\end{eqnarray}
where the sampling distribution $p(T_n,\,{s_n}^2\,|\,\sigma_n,\,K_n)$ is a non-central $\chi^2$ distribution with $T_n - 1$ degrees of freedom and non-centrality parameter $\lambda_{\mathrm{eff},n}$ defined by \autoref{eq:lambda-eff}.\footnote{We note that since the sampling distribution is defined for $\xi_n^2$, which is a function of $\sigma_n$, we must (and do!) include the Jacobian in our definition of $p(T_n,\,{s_n}^2\,|\,\sigma_n,\,K_n)$, with respect to the usual definition of the non-central $\chi^2$ probability density.}
For $p(\sigma_n)$ and $p(K_n)$, we assume the following broad, weakly-informative priors
\begin{eqnarray}
	\ln \sigma_n &\sim& \mathcal{N}(0, 10) \nonumber\\
	\ln K_n &\sim& \mathcal{N}(0, 10) \quad.
\end{eqnarray}
Given these definitions, we implement this model using the \package{numpyro} probabilistic programming language and estimate the joint posterior using variational inference.
SOME MORE WORDS ABOUT VI.

We validate this procedure by generating a simulated \Gaia\ catalog for a sample of unresolved binaries (a large fraction of which have undetectable amplitude) with known orbital parameters and RV measurement uncertainty.
This catalog is shown in \autoref{fig:infer_sigma:data}, and \autoref{fig:infer_sigma:results} demonstrates that this procedure provides an estimate of the true RV uncertainty and, to some extent, the injected RV semi-amplitude for each target.
This is not a trivial experiment, because the procedure used to simulate the catalog was not consistent with the simplifying modeling assumptions.
In particular, the simulated values for $\sigma_n$ were generated from a log-normal distribution $\ln\sigma_n \sim \mathcal{N}(\ln\sigma_0,\,0.1)$.
Furthermore, the simulated RV error values ${s_n}^2$ were generated by procedurally sampling observation times and orbital phases, injecting noise, and computing the sample variance.
In most cases, the orbits are far from well-sampled.
This helps to demonstrate that the accuracy of our inference procedure is not significantly affected by our simplifying assumptions.

\begin{figure}
	\begin{centering}
		\includegraphics[width=0.8\textwidth]{figures/infer_sigma.pdf}
		\caption{For the simulated \Gaia\ catalog from \autoref{fig:infer_sigma:data}, this figure shows the recovered RV semi-amplitude for each simulated target, as a function of its true semi-amplitude.
			The dashed horizontal line shows the mean RV measurement uncertainty used to simulate the data, compared to the inferred value shown in blue.}
		\label{fig:infer_sigma:results}
	\end{centering}
\end{figure}

In practice, the values for $\sigma_n$ inferred using the above procedure are somewhat noisy.
To regularize our estimates in each bin with more than 1000 targets, we repeat the inference for 5 different, randomly-selected, 1000-target samples, and average the resulting estimates of $\sigma_n$.
We also perform some light smoothing in color and magnitude to remove some remaining sample noise in bins with few targets.
Our final estimate of the per-transit RV uncertainty $\sigma_n$ as a function of color and magnitude is shown in the color map in \autoref{fig:noise_model}.
The shaded sections of that figure indicate regions where there were fewer than 50 targets per bin.
For targets outside the well sampled regime, our estimate of $\sigma_n$ is extrapolated, and should be considered suspect.
\autoref{fig:noise_model} shows that the \Gaia\ RV spectrograph can reach a precision of better than $300\,\mathrm{m\,s^{-1}}$ for the brightest targets, with more typical values being of order $1\,\mathrm{km\,s^{-1}}$.

\begin{figure}
	\begin{centering}
		\includegraphics[width=0.8\textwidth]{figures/noise_model.pdf}
		\caption{The estimated per-transit \Gaia\ RV measurement uncertainty as a function of color and magnitude.
			The shaded region indicates areas where there were fewer than 50 targets per bin, so this estimate is extrapolated to these regions.}
		\label{fig:noise_model}
	\end{centering}
\end{figure}

\autoref{fig:sigma_cmd} shows the mean RV measurement uncertainty across the color--magnitude diagram, where we have estimated the distance of each source using the inverse parallax.
In this figure, there is some shot noise visible around the edges where the target density drops, but there is not otherwise a huge amount of structure.
DO WE WANT TO SAY ANYTHING ELSE?

\begin{figure}
	\begin{centering}
		\includegraphics[width=0.8\textwidth]{figures/sigma_cmd.pdf}
		\caption{The average per-transit RV uncertainty across the color--magnitude diagram.
			The contours indicate the density of targets in this same space to guide the eye.
			Most of the structure in this diagram can be seen near the edges where the number of samples is small.
			WHAT ABOUT THOSE BLUE BOIS?}
		\label{fig:sigma_cmd}
	\end{centering}
\end{figure}

\section{Binary probabilities}

One side effect of inferring the per-transit radial velocity uncertainty for all sources in \Gaia\ is that we also get (for free!) an estimate of the probability that the measured RV error is consistent with what we would expect if the individual RV measurements were Gaussian distributed with this uncertainty.
This is some sort of ``outlier'' probability, but we can use it as a proxy for binarity in the absence of systematic, or other un-accounted for effects.
We explore the limitations of such an assumption in the following sections, but it is useful for now to discuss the math for the idealized case here.

As discussed in \autoref{sect:basics}, if the RV measurements for a given \Gaia\ target $n$ includes only Gaussian noise with variance ${\sigma_n}^2$, the sampling distribution for the measured, normalized RV variance ${\xi_n}^2 = (T_n - 1)\,{s_n}^2 / {\sigma_n}^2$ is $\chi^2$ with $T_n - 1$ degrees of freedom.
Therefore, we can compute the $p$-value for this null hypothesis as
\begin{eqnarray}
	\mathrm{Pr}(\xi^2 > {\xi_n}^2\,|\,\mathrm{null}) &=& \int_{\xi_n^2}^\infty \chi^2 (\xi^2;\,T_n-1) \dd \xi^2 \quad.
\end{eqnarray}
A detailed discussion of the interpretation of $p$-values is beyond the scope of this paper, but it's useful to recognize that this value quantifies the frequency with which we would expect to mislabel a single star as a binary, given all the assumptions of our model.
% This is a useful number since, it can be used to construct a catalog with a specific expected false positive rate.

We construct a fiducial binary catalog by selecting all targets with a $p$-value below some threshold $P_0$.
If our Gaussian model is correct and our estimates of the per-transit measurement uncertainty is accurate, then we would expect targets without companions to pass threshold with probability $P_0$.
In other words, setting $P_0 = 0.001$, we would expect 1 in 1,000 single targets to pass our cut and be incorrectly labelled as a binary.
This number does not directly map to the false positive rate, since that will also depend on the true binary fraction, however, it gives us a tuning parameter that can trade off precision and recall in our selection.

\autoref{fig:p_value_dist} shows the observed distribution of $p$-values across the full catalog.
Our null hypothesis here is that every target is a single star with only Gaussian noise with known variance as estimated in \autoref{sec:noise} and, if this hypothesis were correct, we would expect the $p$-values to be uniformly distributed between zero and one (as indicated by the orange dashed histogram in \autoref{fig:p_value_dist}).
The fact that the observed distribution is significantly different from this expectation shows that there are a significant number of outliers.

After computing the $p$-value for every target in our catalog, we can use these values to identify targets that are likely multi-star systems or other kinds of outliers.
\autoref{fig:binary_fraction_cmd} shows the fraction of targets with $p < 0.001$ as a function of their location in the color--magnitude diagram.
In the absence of other sources of noise\footnote{We discuss these other noise sources in SOME SECTION.}, this can be interpreted to first order as the binary fraction, but it is important to remember that this is conditioned on the sensitivity of our detection procedure, an effect that we discuss below.
With this interpretation, this figure shows some expected structures such as an equal-mass main sequence and an increased binary fraction at higher mass.

\begin{figure}
	\begin{centering}
		\includegraphics[width=0.8\textwidth]{figures/p_value_dist.pdf}
		\caption{The distribution of $p$-values across the full sample. In the absence of outliers, we would expect to find the distribution shown as a dashed orange line.}
		\label{fig:p_value_dist}
	\end{centering}
\end{figure}

\begin{figure}
	\begin{centering}
		\includegraphics[width=0.8\textwidth]{figures/binary_fraction_cmd.pdf}
		\caption{Like \autoref{fig:sigma_cmd}, but this time the color bar shows the fraction of targets with a $p$-value less than 0.001. This is an estimate of the fraction of \Gaia\ targets that are detectable multiple star systems as a function of color and magnitude. In other words, this can (with the appropriate caveats) be interpreted as the binary fraction across the color--magnitude diagram. Under this interpretation, the equal-mass binary main sequence is clearly visible, and there is an increased binary fraction DESCRIBE THIS.}
		\label{fig:binary_fraction_cmd}
	\end{centering}
\end{figure}

\section{Completeness \& reliability}

We would like to characterize the efficiency with which our method detects astrophysical signals from companions.
In other words we want to estimate the \emph{completeness} of our survey.
To do this, we use simulated injection and recovery tests where we generate simulated data with a known signal and attempt to recover it with our pipeline.
In particular, we will focus on characterizing the survey completeness as a function of the radial velocity measurement uncertainty and semi-amplitude, integrated over all of the other dimensions.
There are other parameters that affect the detection thresholds including, most importantly, the number of RV transits, but these are second order effects and we will marginalize over these effects using realistic distributions.

We use an approximate broken power-law model for the number of radial velocity transits:
\begin{eqnarray}
	\label{eq:nb-transits}
	p(T_n) &\propto& \left\{\begin{array}{ll}
		1                                  & T_n < T_\mathrm{break} \\
		(T_n / T_\mathrm{break})^{-\alpha} & \mathrm{otherwise}
	\end{array} \right.
\end{eqnarray}
where the parameters are set to approximately match the observed distribution.
These parameters are set as follows: the breakpoint is $T_n = 12$ and the power-law index is $\alpha = 6$.
As demonstrated by SOMEFIGURE, this approximate distribution is a reasonably good fit.

Otherwise we sample the other parameters period, RV semi-amplitude, phase, eccentricity, argument of periasteron, and per-transit measurement uncertainty from the simple distributions listed in SOMETABLE.

Then we simulate an RV time series with $T_n$ observations sampled from \autoref{eq:nb-transits} and compute the sample variance.
Then for consistency, we compute the floored and scaled ``error'' $\epsilon$ used to construct the \Gaia\ catalog (SOMEEQUATION) and, like the real catalog, mark any with $\epsilon > 20\,\mathrm{km/s}$ as non-detections.
Using this catalog, we evaluate the $p$-value for each simulated target, and then estimate its RV semi-amplitude.
Figure SOMEFIGURE shows the recovery rate as a function of RV semi-amplitude and per-transit measurement uncertainty.
At small semi-amplitude, the injected signals are not recovered since the variance is too small to distinguish from the measurement noise.
At large semi-amplitude, the signals are not recovered because the catalog is censored at large RV ``error'' ($\epsilon > 20\,\mathrm{km/s}$).
Figure SOMEOTHERFIGURE shows the inferred semi-amplitude for these simulations as a function of the true value.

These measurements don't fully capture all the limitations of this inference, but we will try to address some of those below by comparing to other binary surveys.

% \begin{figure}
% 	\plotone{figures/completeness.pdf}
% 	\caption{The completeness of our binary detection pipeline calculated using a simulated catalog of binary star systems.
% 		The color bar indicates the fraction of simulated binaries that are recovered---where recovery is defined as the system having a $p$-value of less than 0.001.
% 		\emph{(left)} The recovery rate of binary targets as a function of the RV measurement uncertainty and the true RV semi-amplitude of the simulated orbit.
% 		At fixed measurement uncertainty, the recovery rate increases with semi-amplitude because the signal-to-noise of the error measurement increases.
% 		The recovery rate also decreases for large semi-amplitudes because the \Gaia\ processing pipeline discards any measurements where the final RV error is larger than 20~km/s, an effect that is included in our simulated catalog.
% 		\emph{(right)} The same color bar as the right-hand panel, but this time plotted as a function of the number of RV transits.
% 		Perhaps unsurprisingly, the recovery rate is significantly higher for targets with larger numbers of observations.
% 		% It's worth noting that since this figure is integrated over a simulated distribution of transit numbers, the detailed shape depends on the actual distribution.
% 		\label{fig:completeness}}
% \end{figure}

\begin{figure}
	\begin{centering}
		\includegraphics[width=\textwidth]{figures/completeness.pdf}
		\caption{A figure.}
		\label{fig:completeness}
	\end{centering}
\end{figure}

\section{A model for the radial velocity error}

Apparently \Gaia\ reports RV error $\epsilon$ defined as:
\begin{eqnarray}
	\epsilon^2 = \left(\sqrt{\frac{\pi}{2\,N}}\,s\right)^2 + 0.11^2
\end{eqnarray}
where
\begin{eqnarray}
	s^2 = \frac{1}{N-1}\sum_{n=1}^N \left(v_n - \bar{v}\right)^2
\end{eqnarray}
is what we actually want.

We need to compute a probability density for the $s^2$ parameter above conditioned on a set of $N$ noisy observations of an RV orbit (with some given parameters).
I'm going to model the noise as Gaussian with a known, constant variance $\sigma^2$, but it's \emph{possible} (although probably tricky) that we could relax some of that.

Next, the key realization is that if we have a set of $N$ Gaussian random variables $X_n \sim \mathcal{N}\left(\mu_n,\,\sigma^2\right)$ (with known, but different, means $\mu_n$, in our case given by the RV orbit evaluated at $t_n$), then the random variable $Y = \sum X_n^2$ will be distributed as a noncentral chi-squared.
To use this to our advantage, we first argue that the argument in the variance sum $v_n - \bar{v}$ will be a Gaussian random variable
\begin{eqnarray}
	v_n - \bar{v} \sim \mathcal{N}\left(\mu_n - \bar{\mu},\,\sigma^2\right)
\end{eqnarray}
where $\mu_n$ is the RV orbit \emph{model} at time $t_n$.
This isn't totally obvious since the $v_n$ and $\bar{v}$ distributions aren't necessarily independent, but I think it's ok because [insert relevant math here] (this derivation will look similar to the sampling distirbution for the sample variance calculations so I should double check there, but this definitely works in practice).

Then we're where we need to be: $(N - 1)\,s^2$, our quantity of interest, is now the sum of squares of $N$ Gaussian random variables.
Following the Wikipedia page for the noncentral chi-squared distribution\footnote{\url{https://en.wikipedia.org/wiki/Noncentral_chi-squared_distribution}}, we need to rescale our parameters to have unit variance:
\begin{eqnarray}
	\frac{v_n - \bar{v}}{\sigma} \sim \mathcal{N}\left(\frac{v_n - \bar{\mu}}{\sigma},\,1\right)
\end{eqnarray}
where we have used the assumption that $\sigma$ is known and constant.
I think that we could allow non-constant $\sigma$ but that would result in a "generalized chi-squared" distribution which has no closed form\footnote{\url{https://en.wikipedia.org/wiki/Generalized_chi-squared_distribution}}.

Then the parameters of our noncentral chi-squared will be

\begin{eqnarray}
	k = N
\end{eqnarray}

and

\begin{eqnarray}
	\lambda = \sum \frac{N\,(\mu_n - \bar{\mu})^2}{(1 + N)\,\sigma^2}
\end{eqnarray}

And the parameter that is thus distributed is:

\begin{eqnarray}
	\xi^2 = \frac{(N - 1)\,s^2}{\sigma^2}
\end{eqnarray}

That was a bit ugly.
Let's see if this works with a simulation.
We'll simulate a single simple RV curve and then compute the distribution of $s^2$ over many different realizations of the noise.

\section{Bulk radial velocity inference}

Finally we can combine the measurement uncertainty estimates from SOME SECTION with the

\section{Artisanal radial velocities}

\section{Validation}

\section{Discussion}

This is a paper.
See \autoref{fig:noise_model}.

% \begin{figure}
% 	\plotone{figures/recovered.pdf}
% 	\caption{The semi-amplitude estimated from only the RV error and number of transits for a suite of simulated binary orbits, as a function of the true simulated semi-amplitude.
% 		The points are colored by the eccentricity of the orbit showing a weak trend. MORE HERE?
% 		\label{fig:recovered}}
% \end{figure}

% \begin{figure}
% 	\plotone{figures/sb9.pdf}
% 	\caption{This is a cool figure. \label{fig:sb9}}
% \end{figure}

% \begin{figure}
% 	\plotone{figures/apogee-gold.pdf}
% 	\caption{This is a cool figure. \label{fig:apogee-gold}}
% \end{figure}

\begin{acknowledgments}
	The authors would like to thank the Astronomical Data Group at Flatiron for listening to every iteration of this project and for providing great feedback every step of the way.
\end{acknowledgments}

\vspace{5mm}
\facilities{
	\project{APOGEE},
	\project{Gaia}
}
\software{
	\package{AstroPy} \citep{Astropy2013, Astropy2018},
	\package{JAX} \citep{Bradbury2018},
	\package{NumPy} \citep{Harris2020},
	\package{Matplotlib} \citep{Hunter2007},
	\package{SciPy} \citep{Virtanen2020}
}

\appendix

\section{The non-central $\chi^2$ distribution}
\label{app:ncx2}

\bibliography{bib}{}
\bibliographystyle{aasjournal}

\end{document}


\subsection{Order of differential equations}


%!TEX root = ../main.tex
\section{Implementing Copatterns}
\label{sec:implementing-copatterns}
%Description of our language.
%Explains the interesting details of implementing copatterns.
To explore the implementation of copatterns we have made a small toy language called \textit{findus} implemented in Haskell. It is a functional programming language with first-order functions and simple first-order types. It has the following features:

\begin{itemize}
\item Inductive Types
\item Coinductive Types
\item Let bindings
\item Pattern Matching
\item Copatterns
\end{itemize}

Inductive and coinductive types are implemented as iso-recursive types\,\cite[Section~20.2]{Pierce:2002:TPL:509043}, or $\mu$X.T and $\nu$X.T respectively. This means that their implementations are very similar. Inductive types (\texttt{TRecInd} in Figure~\ref{fig:types}) are defined by the name of the type and then a variant type (\texttt{TVari}). The variant type consists of a list which defines the constructors, where the string part is the name of the constructors and the list of types are the types of the constructor parameters. The coinductive type (\texttt{TRecCoind}) is defined by its name and a list of observations that is similar to the variant type definition, except that there is only one type per observation. The coinductive argument to an observation (the object we observe) can be inferred, so only its return type is specified. A possible extension could be to allow observations to take parameters, but we decided against this since the focus of this project is copatterns, and matching on these parameters would just be standard pattern matching. Defining coinductive data types is done with the \texttt{DCodata} definition in Figure~\ref{fig:expr}, which is defined by the name of the type and the coinductive type that defines it.

\begin{figure}
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`$=3\catcode`_=8}]
data Type =
  \vdots
  | TVari [(String, [Type])]          // Sum of Products (Variant Type)
  | TRecInd String Type               // $\mu$X.T
  | TRecCoind String [(String, Type)] // $\nu$X.T
  | TRecTypeVar String                // Recursive type variable
  | TGlobTypeVar String               // Global type variable
\end{Verbatim}
\caption{Part of the \texttt{Type} type in findus}
\label{fig:types}
\end{figure}

Dual to pattern matching we need a way to make observations on our coinductive types. For this we have the \texttt{EObserve} expression in Figure~\ref{fig:expr}. Such an expression is defined by the coinductive type that is being observed and a list of observations. These observations consist of the name of the observation followed by an expression defining that observation. This \texttt{EObserve} expression models copatterns.

\begin{figure}
\begin{alltt}
type Params = [(String, Type)]

data Expr =
  \vdots
  | EVar String                              // Variable
  | EObserve Type [(String, Expr)]           // Observe
  \vdots

data Defi =
  \vdots
  | DData String Type                        // Data
  | DCodata String Type                      // Codata
  | DGlobLet String Type (Maybe Params) Expr // Global Let
  \vdots
\end{alltt}
\caption{Part of the \texttt{Expr} and \text{Defi} type in findus}
\label{fig:expr}
\end{figure}

\begin{figure}
\begin{alltt}
-- Natural numbers

natBody :: Type                         -- nat constructors
natBody = TVari [
            ("Z", []),                  -- Z Constructor
            ("S", [TRecTypeVar "nat"])  -- S Constructor
          ]

natRec :: Type
natRec = TRecInd "nat" natBody          -- nat inductive type

nat :: Defi
nat = DData "nat" natRec                -- nat data definition

-- Nat Stream

natStreamBody :: [(String, Type)]          -- natStream observations
natStreamBody = [
                  ("head", TGlobTypeVar "nat"),     -- Head observation
                  ("tail", TRecTypeVar "natStream") -- Tail observation
                ]

natStreamRec :: Type                    -- natStream coinductive type
natStreamRec = TRecCoind "natStream" natStreamBody

natStream :: Defi                       -- natStream codata definition
natStream = DCodata "natStream" natStreamRec
\end{alltt}
\caption{Natural numbers and a stream of natural numbers}
\label{fig:natandstream}
\end{figure}

Now that we have a way of representing inductive and coinductive types, let us look at some examples of their use. In Figure~\ref{fig:natandstream}, the abstract syntax representation of natural numbers (\texttt{nat}) and a stream of natural numbers (\texttt{natStream}) is shown. The type \texttt{TGlobTypeVar} is used for referencing globally defined types, and \texttt{TRecTypeVar} is a reference to the type currently being defined. From these we can define the stream of zeros in Figure~\ref{fig:astzeros}. We wrap \texttt{zerosExpr} in a \texttt{DGlobLet} which is a top-level let binding existing in global scope. An \texttt{DGlobLet} is defined by its name, its annotated type, its parameters (if any), and its body.

\begin{figure}
\begin{alltt}
zerosLet :: Expr
zerosLet = DGlobLet "zeros" (TGlobTypeVar "natStream") Nothing zerosExpr

zerosExpr :: Expr
zerosExpr = EObserve (TGlobalTypeVar "natStream") [
            ("head", EVar "Z"),
            ("tail", EVar "zeros")
        ]
\end{alltt}
\caption{A stream of zeros.}
\label{fig:astzeros}
\end{figure}

\subsection{Syntax}
We have implemented a simple concrete syntax which requires a brief explain. The language uses \texttt{case} expressions for inductive pattern matching. For copatterns, we use an \texttt{observe as} construct inspired by the case expression. The idea is to describe the different observations of a given coinductive type. Examples of the corresponding concrete syntax to the abstract syntax from above can be seen in Figure \ref{fig:concrete_syntax}.

\begin{figure}
\begin{alltt}
data nat = \{Z | S nat\}

codata natStream = \{head nat | tail natStream\}

let zeros : natStream =
  observe natStream as
    head -> Z ;
    tail -> zeros
\end{alltt}
\caption{Concrete \textit{findus} syntax for \texttt{nat}, \texttt{natStream}, and \texttt{zeros}.}
\label{fig:concrete_syntax}
\end{figure}

\subsection{Type Checking}
\textit{findus} is type checked according to the rules described by Pierce\,\cite{Pierce:2002:TPL:509043}. This means that all types are annotated, and none are inferred. Pierce does, however, not give typing rules for codata and copatterns.

When type checking copatterns, we know from Section~\ref{sec:copatterns} that the type of the outcome of an observation depends on the observation made. This means that the expression defining the outcome of an observation must be the same as the type of the observation. In the example of \texttt{natStream}, the expression defining a \texttt{head} observation must be of type \texttt{TGlobTypeVar "nat"}. As such, type checking copatterns is fairly simple, as we just need to check that the types of the outcome and the definition of an observation match.

\subsection{Evaluation}
Due to the nature of coinductive types, they are not actually evaluated until they are observed. The result of evaluating an observation might be a new coinductive definition that we again cannot evaluate before it is observed. There are situations, however, where coinductive definitions can be rewritten to something else. Consider the example of \texttt{nats} in Figure~\ref{fig:nats_copatterns}, which is defined with copatterns. Evaluating \texttt{nats} in itself does not make much sense as there is nothing to evaluate; evaluation does not occur until we observe \texttt{nats}. When evaluating \texttt{nats}, we simply examine at the definitions and unfold them. Assuming an evaluation function \texttt{eval}, we have the following examples of reductions:

\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`$=3\catcode`_=8}]
eval (head nats) = Z
eval (tail nats) = map S nats
eval (head tail nats) = eval (head (map S nats))
                      = eval (S (head nats))
                      = S Z
eval (tail tail nats) = eval (tail (map S nats))
                      = map S (tail nats)
\end{Verbatim}

We evaluate by unfolding until we either cannot unfold a coinductive definition anymore, or get an expression that is not coinductive. A coinductive definition cannot be unfolded if additional observations are required in order to reduce it. For example, \texttt{nats} requires additional observations, but \texttt{tail nats} is directly defined as \texttt{map S nats}. Unfolding definitions in this manner could lead to an infinite series of unfolds. If we want a guarantee that this will not happen in a given situation, our coinductive definitions must be checked for productivity. Productivity will be discussed further in Section~\ref{sec:productivity}.

\subsection{Termination Checking}
In order to get an in-depth understanding of the size-change principle (as explained in Section~\ref{sec:related_work}), we have implemented a termination checker for \textit{findus} based on the original formulation of the principle\,\citep{LeeJones01SizeChange}. For the size-change principle, two abstract notions are used: \emph{control points} and \emph{data positions}\,\citep{Krauss07certifiedsizechange}. In short, control points are the entities which must be terminating, while data positions are the entities that change size. For our implementation, the control points are \textit{findus} functions, and the data positions are the parameters. Inspired by Hyvernat\,\citep{Hyvernat13}, sizes are modeled with an inductive data type. Since we do not have any primitive data types, values can only become structurally smaller through pattern matching, and structurally larger by constructor application.

Termination is detected by first constructing a call graph of all direct calls from one function to another. For each of these calls, a size-change graph is constructed. A size-change graph is a 3-tuple \texttt{(f, arcs, g)}, where \texttt{f} and \texttt{g} are functions, and \texttt{arcs} is a set of \emph{size-change arcs}. Size-change arcs capture the relationship between a parameter and the argument given at a function call. This relationship is either \emph{descending} or \emph{non-increasing}, depending on the changes in size. If the change in size is increasing, there is no relationship. When the size-change graphs have been constructed, indirect recursive calls are found by detecting cycles in the call graph by simple graph traversal. For each of these cycles, we record the chain of size-change graphs (corresponding to the chain of calls) which lead to the cycle. Such a chain of size-change graphs constitute a \emph{multipath}. To identify whether infinite descent is present in a multipath, it is collapsed to a single size-change graph through a simple composition procedure\,\citep{LeeJones01SizeChange}. If some size-change arc represents a \emph{descending} relationship in such a collapsed size-change graph, then the recursive call leads to termination. If all recursive calls in a program leads to termination, then the entire program is \emph{size-change terminating}.

The size-change termination checker does not cover coinductive definitions. Due to lack of time, the productivity algorithm presented in Section~\ref{sec:productivity} has not been implemented in \textit{findus}.

\subsection{Reflections on Implementation}
From exploring the implementation of codata and copatterns we have found that implementing them is similar to implementing recursive types. In contrast to inductive types, coinductive types do not have to be rolled or unrolled as they are defined by observations, not constructors. We roll to construct inductive types and unroll to inspect how an inductive type was constructed. Neither of these notions make sense in terms of coinductive types. We define coinductive types by giving the observations that can be made on them, and we inspect them by actually observing them. This makes type checking fairly straightforward. We have also discussed evaluation, which is fairly simple with copatterns due to unfolding. With this in mind, the challenge of implementing copatterns does not lie in the implementation itself, but rather in (1) implementing a productivity checker as discussed in Section~\ref{sec:productivity}, and (2) how they can be incorporated into Idris, which will be discussed in Section~\ref{sec:copatterns_in_idris}.


\section{Calculating treatment effects by estimating missing data}


\documentclass[11pt,a4paper]{article}
\usepackage{diagbox}
\usepackage{wrapfig}
\usepackage[utf8]{inputenc}
%\usepackage[swedish]{babel}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{units}
\usepackage{ae}
\usepackage{icomma}
\usepackage{color}
\usepackage{graphics}
\usepackage{bbm}
\usepackage{float}

\usepackage{caption}
\usepackage{subcaption}

\usepackage{hyperref}
\usepackage{epstopdf}
\usepackage{epsfig}
\usepackage{braket}
\usepackage{pdfpages}

\usepackage{tcolorbox}

\usepackage{MnSymbol}

\newcommand{\N}{\ensuremath{\mathbbm{N}}}
\newcommand{\Z}{\ensuremath{\mathbbm{Z}}}
\newcommand{\Q}{\ensuremath{\mathbbm{Q}}}
\newcommand{\R}{\ensuremath{\mathbbm{R}}}
\newcommand{\C}{\ensuremath{\mathbbm{C}}}
\newcommand{\id}{\ensuremath{\,\mathrm{d}}}
\newcommand{\rd}{\ensuremath{\mathrm{d}}}
\newcommand{\Ordo}{\ensuremath{\mathcal{O}}}% Stora Ordo
\renewcommand{\L}{\ensuremath{\mathcal{L}}}% Stora Ordo
\newcommand{\sub}[1]{\ensuremath{_{\text{#1}}}}
\newcommand{\ddx}[1]{\ensuremath{ \frac{\partial}{\partial #1} }}
\newcommand{\ddxx}[2]{\ensuremath{ \frac{\partial^2}{\partial #1 \partial #2} }}
%\newcommand{\sup}[1]{\ensuremath{^{\text{#1}}}}
\renewcommand{\b}[1]{\ensuremath{ {\bf #1 } }}
\renewcommand{\arraystretch}{1.5}

\begin{document}

\begin{center}
\Large \bf Discretization of a 3D delta source in DREAM
\end{center}

A pellet may be treated as a point source which moves through three-dimensional space. In this note, we outline the numerical discretization of such a source within the one-dimensional (in radius) DREAM formulation.

The pellet can be viewed as a source term for an unknown plasma parameter $x$, depending locally on all unknowns $y$ in the system. The pellet is located in the position $\b{x}_s(t)$, and is assumed to deposit material in a location $\b{x}_d = \b{x}_d(\b{x}_s(t),\,y(\b{x}_d))$\footnote{We assume that the spreading is a pure translation which depends only on the plasma at the target location in order to make the source local - in reality there would likely be spreading which would need to be modelled more accurately}, which may be separate from the pellet location itself as the ablated material can drift before spreading out over the flux surface and becoming confined.

Then, the SPI contribution to the evolution of quantity $x$ is
\begin{align}
\left(\frac{\partial x}{\partial t}\right)_\mathrm{SPI} &= F(y) \delta^3( \b{x} - \b{x}_d(t,\,y)),
\end{align}
where $y$ denotes local plasma parameters at the location $\b{x}_d$ where the material is deposited and $F$ is the source amplitude.  The numerical challenge lies in the discretization of the delta function, to which we now turn our attention.

\section*{Flux surface average of delta function}
In DREAM we utilise the toroidal coordinates $(r,\,\varphi,\,\theta)$ where $r$ is a (minor-)radial coordinate labelling the flux surface, $\varphi$ the toroidal angle and $\theta$ a poloidal angle. Refer to \texttt{DREAM/doc/notes/theory.tex} for a more detailed description of the geometry.  The delta function expressed in toroidal coordinates becomes
\begin{align}
\delta^3( \b{x} - \b{x}_d(t,\,y)) = \frac{\delta(r-r_d)\delta(\theta-\theta_d)\delta(\varphi - \varphi_d)}{\mathcal{J}},
\end{align}
where $\mathcal{J}$ is the jacobian of the coordinate system. The flux surface average of this becomes
\begin{align}
\langle\delta^3( \b{x} - \b{x}_d(t,\,y))\rangle = \frac{\delta(r-r_d)}{V'},
\end{align}
where $V(r)$ denotes the total volume enclosed by the flux surface labelled $r$. In DREAM, $V'(r)$ is denoted \texttt{VpVol} and is stored in the \texttt{RadialGrid} object. Here $r_d(\b{x}_s(t), y(r_d))$ denotes the radius (i.e.~flux surface) on which the material is deposited, and $\b{x}_s(t)$ still denotes the pellet location.

\section*{Time averaged delta source}
Since a pellet may pass several radial grid points in one time step, a purely explicit or implicit time step would cause all ablated material to only be deposited in the location of the pellet at the beginning or end of the time step, respectively. In order to spread the material across all radial locations crossed by the pellet during a time step, we may perform a time average where we evaluate the unknown quantities $y_0 = y(t_0)$ at a given time (previous time step for explicit stepping, next time step for implicit), but carry out a time average over the full pellet motion
\begin{align}
\llangle\delta^3( \b{x} - \b{x}_d(t,\,y)) \rrangle &= \frac{1}{\Delta t} \int_t^{t+\Delta t}  \langle \delta^3( \b{x} - \b{x}_d(t,\,y))\rangle \, \rd t \nonumber \\
&= \frac{1}{V'\Delta t} \int_t^{t+\Delta t} \delta(r-r_d(t,y_0)) \, \rd t  \nonumber\\
&= \frac{1}{V'\Delta t} \int_{r_d(t)}^{r_d(t+\Delta t)}\frac{\delta(r-r_d)}{\dot{r}_d} \, \rd r_d \nonumber \\
&= \frac{1}{V'|\dot{r}| \Delta t } \times \begin{cases}
1, & r_{dl} < r < r_{du}  \\
0, & \text{otherwise}
\end{cases} \nonumber \\
r_{dl} &= \text{min}[r_d(t),r_d(t+\Delta t)], \nonumber \\
r_{du} &=  \text{max}[r_d(t),r_d(t+\Delta t)],
\end{align}
where $\dot{r}$ denotes the radial velocity of the pellet deposition location at radial location $r$. This way, we have transformed the 3D delta source to a box shaped source in radius which will behave well even with longer time steps, particularly when combined with implicit time stepping where $t_0 = t_{n+1}$, and $\partial x/\partial t = (x_{n+1}-x_n)/\Delta t$.

An alternative, at the same order of accuracy in $\Delta t$, would be to replace
\begin{align}
|\dot{r}|\Delta t \mapsto r_{du}-r_{dl}
\end{align}
which may be a more attractive option since $r_{dl}$ and $r_{du}$ need to be evaluated regardless.

\section*{Evaluation of radius $r$}
Given the deposition location $\b{x}_d(\b{x}_s,\,y)$, we need to evaluate the radius $r_d(\b{x}_d)$. In DREAM, it would be appropriate to let the \texttt{RadialGridGenerator} class perform this calculation, since this is the class where the magnetic geometry of the system is specified.
In particular, the radial grid generators are implemented in the two classes \texttt{CylindricalRadialGridGenerator} (describing a cylindrical geometry such as used in GO) or \texttt{AnalyticBRadialGridGenerator} which describes a toroidal geometry with shaped flux surfaces parametrised by
\begin{align}
\b{x} &= R\hat{R} + z\hat{z}, \nonumber \\
R &= R_m + \Delta(r) + r\cos[\theta +\delta(r) \sin\theta], \nonumber \\
z &= r \kappa(r) \sin\theta, \nonumber \\
\hat{R} &= \cos\varphi \hat{x} + \sin\varphi \hat{y},
\label{eq:analyticB system}
\end{align}
where $\kappa$ is the elongation, $\Delta$ the Shafranov shift, $\delta$ a triangularity parameter, $R=\sqrt{x^2+y^2}$ the major radius and $R_m$ the radius of the magnetic axis. The \texttt{CylindricalRadialGridGenerator} effectively implements the special case $R_m=\infty$, $\delta=0$, $\kappa=1$, representing an infinite straight cylinder.

A way to accomplish this could be to equip \texttt{RadialGridGenerator} with a function
\begin{verbatim}
real_t RadialGridGenerator::evaluateRadialPosition(real_t x, real_t y, real_t z)
\end{verbatim}
which would be implemented separately in the derived classes (\texttt{Cylindrical}, \texttt{AnalyticB} and other future generators).

In the cylindrical radial grid generator, a suitable definition could be to return $r = \sqrt{x^2+y^2}$ (if we were to assume that the cylinder has $\hat{z}$ as its symmetry axis), whereas in the \texttt{AnalyticB} generator, the equation system (\ref{eq:analyticB system}) above needs to be solved numerically.

In the future we will likely also support a \texttt{NumericalRadialGridGenerator} that loads numerical magnetic field data and interpolates to determine $r$ numerically.

\subsection*{(optional) Evaluation of radial velocity $\dot{r}$}
The radial velocity of the pellet deposition location is given by
\begin{align}
\dot{r} &= \nabla r \cdot \frac{\rd \b{x}_d(\b{x}_s(t), y_0)}{\rd t} \nonumber \\
&= \nabla r \cdot \left( \frac{\partial \b{x}_d}{\partial \b{x}_s} \cdot \dot{\b{x}}_s\right)
%&= \frac{\partial r}{\partial x}\frac{\rd x}{\rd t} + \frac{\partial r}{\partial y}\frac{\rd y}{\rd t} + \frac{\partial r}{\partial z}\frac{\rd z}{\rd t}.
\end{align}
The cartesian components of the pellet velocity $\dot{\b{x}}_s = (\dot{x}_s,\,\dot{y}_s,\,\dot{z}_s)$ will likely be held constant as the pellet is assumed to move along a line at constant speed, although this could be generalised if we wish $(x,y,z)$ to satisfy some equations of motion (say, by adding drag force which depends on plasma parameters or jet propulsion due to anisotropic ablation). The Jacobian matrix $\partial \b{x}_d/\partial \b{x}_s$ connecting deposition location to pellet location will depend on the model imposed for the drift of the ablated material -- in the simplest model, we can assume the material to be deposited locally so that this becomes the identity matrix; otherwise it will probably depend on the plasma parameters $y$.

For the \texttt{AnalyticB} generator, according to \texttt{doc/notes/theory}, we can evaluate $\nabla r$ using
\begin{align}
\nabla r &= \frac{\frac{\partial z}{\partial \theta} \hat{R} - \frac{\partial R}{\partial \theta} \hat{z}}{\frac{\partial R}{\partial r}\frac{\partial z}{\partial \theta} - \frac{\partial R}{\partial \theta}\frac{\partial z}{\partial r}},
\end{align}
where
\begin{align}
\frac{\partial R}{\partial r} &= \Delta' + \cos[\theta+\delta\sin\theta]-r\delta'\sin\theta\sin[\theta+\delta\sin\theta], \nonumber \\
\frac{\partial R}{\partial \theta} &= -r(1+\delta\cos\theta)\sin[\theta+\delta\sin\theta],  \nonumber \\
\frac{\partial z}{\partial r} &=  \kappa\left(1+ \frac{r\kappa'}{\kappa}\right)\sin\theta, \nonumber \\
\frac{\partial z}{\partial \theta} &= r\kappa\cos\theta.
\end{align}
This provides a full description of the geometry of pellet trajectories in shaped magnetic fields in DREAM.

This could be explicitly implemented by, for example, equipping RadialGridGenerator with functions
\begin{verbatim}
real_t RadialGridGenerator::evaluateDrDx(real_t r, real_t theta);
real_t RadialGridGenerator::evaluateDrDy(real_t r, real_t theta);
real_t RadialGridGenerator::evaluateDrDz(real_t r, real_t theta);
\end{verbatim}
which would be implemented in the various derived classes.

\section*{Finite volume discretization of time-flux-averaged source}
The final step of the discretization is to carry out the finite volume discretization of the source function in radius. We discretize the source  with a volume average
\begin{align}
\llangle\delta^3( \b{x} - \b{x}_d(t,\,y)) \rrangle_i &= \frac{1}{r_{i+1/2} - r_{i-1/2}} \frac{1}{V'_i}\int_{r_{i-1/2}}^{r_{i+1/2}}V'\llangle\delta^3( \b{x} - \b{x}_d(t,\,y)) \rrangle  \, \rd r \nonumber \\
&= \frac{1}{r_{i+1/2} - r_{i-1/2}} \frac{1}{V'_i}\int_{\text{max}(r_{i-1/2},r_{dl})}^{\text{min}(r_{i+1/2},r_{du})}\frac{\rd r}{|\dot{r}|(r) \Delta t } \nonumber \\
&\approx \frac{\text{min}(r_{i+1/2},r_{du})-\text{max}(r_{i-1/2},r_{dl})}{(r_{i+1/2} - r_{i-1/2})(r_{du}-r_{dl})V'_i},
\end{align}
where, if the denominator is negative, should be set to 0.
It is likely a good idea to treat the average more carefully in the turning point when $\dot{r}_d$ crosses 0. If we step back and exchange integration order of the cell average and the time average, the exact expression is given by
\begin{align}
\llangle\delta^3( \b{x} - \b{x}_d(t,\,y)) \rrangle_i &= \frac{1}{r_{i+1/2} - r_{i-1/2}} \frac{1}{V'_i\Delta t}\int_t^{t+\Delta t}\rd t \int_{r_{i-1/2}}^{r_{i+1/2}}\delta(r-r_d(t)) \nonumber \\
&=\frac{1}{r_{i+1/2} - r_{i-1/2}} \frac{1}{V'_i\Delta t}\int_t^{t+\Delta t}\rd t \, \Theta(r_{i+1/2} - r_d(t)) \Theta(r_d(t)-r_{i-1/2}).
\end{align}
The remaining integral corresponds to for how long time (in the interval $[t,\,t+\Delta t]$) the inequality $r_{i-1/2} < r_d(t) < r_{i+1/2}$ is satisfied. In general, no closed-form analytic expression exists for this since $r_d(t)$ can be a complicated function, depending on geometry. Far from the turning point of the shard, it may be reasonable to assume that $\dot{r}_d$ is constant during $\Delta t$, in which case
\begin{align}
\int_t^{t+\Delta t}\rd t \, \Theta(r_{i+1/2} - r_d(t)) \Theta(r_d(t)-r_{i-1/2}) \approx \Delta t \frac{\text{min}(r_{i+1/2},r_{du})-\text{max}(r_{i-1/2},r_{dl})}{r_{du}-r_{dl}}
\end{align}
and we reproduce the previous expression, however it is here clearer how to generalize it for the time step during which the point of closest approach occurs (where $r_{du}-r_{dl}$ could in principle vanish and the expression may become singular).

\section*{NGS pellet model}
In the NGS ablation model, we solve the system of equations
\begin{align}
\frac{\partial N}{\partial t}(t) &= - K(y(t,\b{x})) N(t)^{a}, \\
\frac{\partial n_i}{\partial t}(t,\,\b{x}) &= - \frac{\partial N}{\partial t}(t)\delta^3(\b{x} - \b{x}_s(t)) + ...\\
\frac{\partial W}{\partial t}(t,\,\b{x}) &= I_d \frac{\partial N}{\partial t}(t) \delta^3(\b{x}-\b{x}_s(t)) + ...,
\end{align}
where $N =(4/3)\pi r_s^3 \rho$ is the total number of particles in the pellet (/shard), $n_i$ the number density of the ion density that it is composed of (say, neutral hydrogen), $W$ the local energy density of the plasma, $K>0$ and $a=4/9$ parameters of the NGS model which set the ablation rate, $I_d$ the (positive) disocciation energy per particle in the pellet (possibly adding the ionization energy if we take $n_i$ as an ionized species), $\b{x}_s(t)$ the pellet location and $y$ the set of plasma parameters which influence the ablation rate and `...' denote equation terms that are independent of the pellets.

An option to model the motion of the ablated cloud before it becomes confined by the plasma is to add another fluid field $n_\text{abl}(t,\,\b{x})$ of newly ablated material, such that
\begin{align}
\frac{\partial n\sub{abl}}{\partial t}(t,\,\b{x}) &= - \frac{\partial N}{\partial t}(t) \delta^3(\b{x}-\b{x}_s(t)) - \frac{n\sub{abl}}{\tau} + \frac{\partial}{\partial \b{x}}\cdot \Phi, \nonumber \\
\frac{\partial n_i}{\partial t}(t,\,\b{x}) &=  \frac{n\sub{abl}}{\tau} + ... ,
\end{align}
where $\tau$ is the time-scale for the ablated material to become confined (probably characterising some parallel-flow rate?) providing a delay before the ablated material contributes to cooling the plasma and raising collisionality, and $\Phi$ a flux of ablated material, for example of the form of an advection-diffusion term
\begin{align}
\Phi &= \b{A}\sub{abl} n\sub{abl} + \mathsf{D}\sub{abl}\cdot \frac{\partial n\sub{abl}}{\partial \b{x}}
\end{align}
with \b{A} an advective flow velocity and $\mathsf{D}$ a diffusion tensor, which can be used to model the flow of ablated material from the point of ablation to the point where it becomes confined to the flux surfaces.

\paragraph{Solution strategy:} For particle and energy conservation, we wish to discretize the second and third equation as
\begin{align}
\frac{n_i(t_{n+1}) - n_i(t_n)}{\Delta t} = -\frac{N(t_{n+1}) - N(t_n)}{\Delta t}\delta^3(\b{x}-\b{x}_s) + ...,
\end{align}
so that the change in particle number in the pellet directly contributes to the ion density in a number-conserving manner which is independent of resolution -- then, once the pellet has ablated, the correct number of particles will have been added to the plasma. In the first equation, which sets the ablation rate, we are free to discretize any way we see fit. A convenient choice is the implicit scheme
\begin{align}
\frac{N(t_{n+1})^{1-a} - N(t_n)^{1-a}}{\Delta t} + (1-a)K(y(t_{n+1},\,\b{x}) = 0
\label{eq:Ntnp1}
\end{align}
where we integrated the equation exactly under the assumption that $y$ is constant at its value in the next time step, $t_{n+1}$. For extensions of this idealized model, where we may want to add additional non-linear terms $H(y,\,N)$ to the right-hand side of (\ref{eq:Ntnp1}), this form of the equation solved with a Newton method (according to the DREAM recipe) will be suitable, however for this simple case the solution can in principle be explicitly given
\begin{align}
N(t_{n+1}) = \left[ N(t_n)^{1-a} - \Delta t (1-a)K(y(t_{n+1},\,\b{x})  \right]^{1/(1-a)},
\end{align}
which provides the solution without the need of any additional iterations in a Newton solver.

A numerical challenge is posed by the fact that (\ref{eq:Ntnp1}) will yield undefined $N(t_{n+1})$ in the final time step as the pellet is completely ablated (as $N^{1-a}$ goes negative, and the jacobian diverges). A possible resolution is as follows: in each iteration $k$ of the Newton solver, if
\begin{align}
N(t_n)^{1-a} - (1-a)\Delta t K(y(t_{n+1}^{(k)},\b{x}) < 0,
\end{align}
\textcolor{red}{[is this a sufficient constraint to guarantee that the solver will converge?]} replace equation (\ref{eq:Ntnp1}) by $N(t_{n+1}) = 0$ , otherwise use the full expression.

\subsection*{More careful time average of the equation system}
\textcolor{red}{[this section is a bit of a mess, I just jotted it down quickly to have the main pieces in place, but I don't think I really believe in it because of its complexity]}

In case we wish to take such long time steps that multiple radial cells are traversed by a pellet in a single time step, we may wish to perform a more careful time average of the equations that preserves conservation of particle number.

When flux-surface averaging, and carrying out the FVM cell average, the delta function is replaced by
\begin{align}
\delta^3(\b{x}-\b{x}_s) \mapsto \begin{cases}
\frac{1}{V' \Delta r_i}, & r_{i-1/2} < r(\b{x}_s(t)) \leq r_{i+1/2}, \\
0 & \text{otherwise}.
\end{cases}
\end{align}
Then, we can again solve exactly for the ablation of the pellet under the assumption that all plasma quantities are evaluated at their value in the next time step, and also that they are piecewise constant, taking the constant value $K_i = K(y(t_{n+1}, \,r_i)$ within a given cell with radial index $i$:
\begin{align}
\frac{N(t_{k+1})^{1-a} - N(t_k)^{1-a}}{\Delta t} + (1-a) K_{i_k} = 0, \quad k = 0,~1,~...,~k\sub{max}
\end{align}
where $\Delta t_k = t_{k+1} - t_k$ is the time during the time step that the pellet spent in the cell with index $i_k$, where $i_0$ is the cell which contained the pellet $\b{x}_s(t_0 = t_n)$ at the beginning of the time step, $i_{k\sub{max}}$ contains $\b{x}_s(t_{k\sub{max}} = t_{n+1})$, and we solve for all grid cells $k$ that the pellet moves across. Summing over all $k$ up to some particular value, the equation takes the form
\begin{align}
N(t_{k+1}) = \left[ N(t_n)^{1-a} - (1-a)\sum_{m=0}^k\Delta t_m K_{i_m} \right]^{1/(1-a)}.
\end{align}
If at any $k_c\leq k\sub{max}$ the situation occurs where $N(t_{k_c+1})^{1-a}$ becomes negative, then we should set $N(t_{k+1})=0$ for all $k_c \leq k \leq k\sub{max}$.

All this becomes more complicated than the local theory presented before, because (1) we must determine the time $\Delta t_k$ spent by the particle within each cell, which requires the solution of a nonlinear equation system by itself (at least in the case of general toroidal geometry), (2) the change $N(t_{k+1}) - N(t_k)$ of the density within a given cell $i_k$ depends on the values of the plasma parameters in all cells $i_m$ between $i_k$ and $i_0$, so that the jacobian picks up off-diagonal contributions, and (3) this theory does not appear to be (easily) amenable to generalizations with additional non-linear contributions in $N$ (at least not without solving a nonlinear set of equations for all $\{N(t_k)\}$ to obtain the various $N(t_k)$ in terms of $N(t_n)$).

\end{document}


\chapter{Correcting for Telluric Absorption: Methods, Case Studies, and the TelFit Code\footnote{
  This chapter was previously published \citep{Gullikson2014}. Both co-authors were my advisor at some point while I was developing this code. They helped guide the code requirements and provided a science case study for the work. Adam Kraus provided observational data for the case study.
}}
\label{chap:telfit}

\section{Introduction}
All ground-based astronomical spectra suffer from contamination by the Earth's atmosphere, which introduces so-called telluric lines into the spectrum. The wavelength of the telluric lines change very slightly with wind along the line of sight and pressure shifts in water lines, and the relative line strength can vary a great deal with the observatory location, weather and the airmass of the observation. The telluric lines must be removed from the spectrum to retrieve many stellar features redward of about 500 nm, which is a nontrivial task.

Often, telluric lines are removed by observing a rapidly rotating hot star (spectral type A or late B) near the same time and airmass as the target star and using it as a template for the telluric spectrum. This approach has several disadvantages: (1) it can be difficult to find a suitable standard star near the same time and airmass as the observation, so approximate corrections are made to the line strengths using Beer's law \citep{Beer1852}; (2) The water vapor content changes much more rapidly than the other absorbing species in the atmosphere, so scaling the whole empirical telluric spectrum is incorrect (3) the standard star has strong hydrogen and helium lines and weak metal lines that can further contaminate the science spectrum; (4) observing a standard star can take a significant amount of precious telescope time, especially for high signal-to-noise work.

A better solution is often to generate a theoretical telluric absorption spectrum from a line list and the observing conditions. Recently, several groups \citep{Seifahrt2011, Bertaux2013, Husser2013, Cotton2013, Gullikson2013} have used the LBLRTM code\footnote{\url{http://www.rtweb.aer.com/lblrtm_description.html}} \citep[][Line By Line Radiative Transfer Model]{Clough2005} for this purpose. However, the interface for the LBLRTM code can be difficult to learn, and can not directly fit an observed spectrum.

Telluric correction is vital to the work described in this thesis. In the optical, it allows us to use more of the spectrum to search for faint companions. Importantly, the red parts of the optical spectrum that are most affected by telluric contamination are where the primary-to-secondary flux ratio is smallest, and so where much of the companion-detecting power lies. Telluric correction is even more important in near-infrared spectra, where the contamination dominates at almost all wavelengths.

In this chapter we describe Telfit\footnote{The TelFit code documentation and installation directions can be found at \url{http://telfit.readthedocs.org/en/latest/}}, a code that acts as a wrapper to LBLRTM and allows for easy fitting of the telluric spectrum in astronomical data. We compare the results to those obtained from empiricial telluric correction in optical echelle spectra, and find that model-fitting produces similar telluric line residuals and is in fact better for dim targets or high signal-to-noise ratio work. We only demonstrate the use of TelFit in optical spectra in this work, but note that we use it to accurately remove the telluric contamination in near-infrared spectra in Chapters \ref{chap:dsd} and \ref{chap:survey}, and use an earlier version of TelFit for near-infrared spectra in Chapter \ref{chap:pilot}.

We describe the observations and data reduction used in this chapter in Section \ref{paper3_sec:obs}. We then describe the TelFit code and give a brief description of the telluric fitting procedure in Section \ref{paper3_sec:tellcorr}. Finally, we demonstrate the use of the TelFit code to correct several optical telluric bands in Section \ref{paper3_sec:results}.

\section{Observations and Reduction}
\label{paper3_sec:obs}
We observed representative spectra for the early and late-type stars listed in Table \ref{paper3_tab:sample}. The A-stars HIP 20264 and HIP 25608 were observed with the CHIRON spectrograph on the 1.5m telescope at Cerro Tololo Interamerican Observatory (CTIO). This spectrograph has a spectral resolution of $R = 80000$ from $\lambda = 460 - 860$ nm, and uses 3x1 binning along the spatial direction. The data were bias-subtracted, flat-fielded, and extracted using standard IRAF tasks. The reduced spectra were wavelength-calibrated using a ThAr lamp exposure from immediately before the observation of the star. In order to reach high a signal-to-noise ratio and avoid detector saturation, we co-added 7 spectra after reduction and telluric correction (see Section \ref{paper3_sec:tellcorr}).

In addition, several M-type stars were observed with the Magellan Inamori Kyocera Echelle (MIKE) optical echelle spectrograph on the Clay telescope at Magellan Observatory as spectral type standards for a study of young stars \citep{Kraus2014}. We used the $0.7''$ slit, which yields a spectral resolution of $R = 35000$ from $\lambda = 335 - 950$ nm. The pixel scale oversamples the resolution with the $0.7''$ slit, so we used 2x binning in the spatial and spectral directions. The spectra were reduced using the CarPy pipeline \citep{Kelson2003}\footnote{\url{http://code.obs.carnegiescience.edu/mike}}. The reduced spectra were wavelength calibrated using a combination of a ThAr lamp exposure and the 760 nm telluric A band \citep[see][for details]{Kraus2014}.

TelFit can handle rapidly rotating early-type stars without preprocessing the spectra by fitting a Savitzky-Golay smoothing filter \citep{savitzky1964} as a pseudo-continuum for the telluric lines. For more feature-rich late-type stars, we remove an approximation to the stellar spectrum using the procedure below before fitting the telluric lines.

\begin{enumerate}
  \item Make an initial guess telluric spectrum with TelFit. The guess spectrum intentionally has slightly higher molecular mixing ratios than the correct values so that it over-corrects the telluric lines.
  \item Divide the observed spectrum by the guess telluric spectrum. This leaves a spectrum with stellar lines in absorption and telluric residuals appearing like emission lines.
  \item Find the best-fit stellar model to the absorption lines using a grid of PHOENIX model spectra \citep{Hauschildt1999} with effective temperature and log(g) near the expected values for the star.
  \item Divide the original data (before the telluric over-correction in step 1) by the best-fit normalized stellar spectrum. This leaves a spectrum that is mostly telluric lines, but which may have strong stellar line residuals.
  \item Enumerate the wavelength ranges that are strongly affected by stellar line residuals, and tell TelFit to ignore them. We ignored all wavelengths from 817.5 - 820 nm in our M-star spectra, which have strong residuals from the sodium lines.
\end{enumerate}

The fit to the primary star does not need to be perfect. Indeed, if we could perfectly model stellar spectra there would be little reason to observe them! The purpose of the preprocessing described above is to ensure that telluric lines dominate the spectrum before fitting. Any strong residuals from that process can and should be ignored in the telluric fit. While a physically better solution would be to fit the stellar and telluric spectra simultaneously, the method described above will often work well enough  for most purposes and is much faster than a simultaneous fit.

\afterpage{%
\begin{table}
  \centering
  \caption{Observations used in this paper. }
  \begin{tabular}{|ccccccc|}
  \hline
    Star &    R   &  $\mathrm{K_s}$ & SpT  & UT date  & Spectrograph & SNR    \\
         &  (mag) & (mag)      &      & yyyymmdd &              & @820 nm \\
    \hline \hline

    GJ 83.1  &  10.94  &  6.65  &  M4.6  &  20120902  &  MIKE  &  200  \\
    GJ 109 &  9.49  &   5.961   & M3.0 & 20120902 & MIKE        & 219 \\
    GJ 173  &  9.33  &  6.09  &  M1.6  &  20130203  &  MIKE  &  166 \\
    GJ 273  &  8.70  &  4.86  &  M3.6  &  20130204  &  MIKE  &  215\\
    GJ 447  &  9.86  &  5.65  &  M4.0  &  20130203  &  MIKE  &  324 \\
    GJ 581  &  9.46  &  5.84  &  M2.6  &  20130202  &  MIKE  &  336 \\
    GJ 628 &  8.92  &  5.08  &  M3.0  &  20120716  &  MIKE  &  346 \\
    GJ 908  &  8.03  &  5.04  &  M1.0  &  20120717  &  MIKE  &  328 \\
    HD 33793  &  7.90  &  5.05  &  M1.0  & 20130203  &  MIKE  &  315 \\
    HIP 20264  &  5.38  &  5.33 &  A0.0  &  20140301  &  CHIRON  &  167 \\
    HIP 25608  &  5.54  &  5.50  &  A1.0  &  20140301  & CHIRON  &  214 \\
    \hline

  \end{tabular}
  \label{paper3_tab:sample}

\end{table}
}

\section{Telluric Fitting Method}
\label{paper3_sec:tellcorr}

The TelFit code performs a least-squares fit using a constrained Levenberg-Marquardt algorithm \citep{Marquardt1963}. It is extremely flexible; the user can decide which atmospheric parameters to fit, which spectral regions to use in the fit, how the detector point spread function (PSF) is modeled, the order of the continuum fit used in each iteration, and whether the data wavelengths are calibrated to fit the model or vice versa. The algorithm will optimize the fitting variables, as well as the detector resolution and the wavelength solution. LBLRTM requires an atmosphere profile giving the temperature, pressure, and the abundance of several molecules as a function of height. We provide a default atmosphere profile\footnote{The default atmosphere profile was developed for the Michelson Interferometer for Passive Atmospheric Sounding (MIPAS) spacecraft, are is available from \url{http://www-atm.physics.ox.ac.uk/RFM/atm/}} with the TelFit code that is acceptable for mid-latitude observatories, but provide the ability to easily give an alternate atmosphere profile for more accurate results.

While TelFit explicitly fits the temperature, pressure, and abundances at the observatory altitude, it scales the quantities at \emph{all} atmosphere layers. For the pressure and temperature, it finds the difference between the requested pressure (temperature) at the requested altitude, and the pressure (temperature) in the atmosphere profile. If $P_r$ is the requested pressure at altitude $z$, and the atmosphere profile pressure is $P_0 $ at that altitude, then the pressure $P_i $ at each atmosphere layer $z_i$ is scaled as
\begin{equation*}
  P_i \rightarrow P_i - (P_r - P_0) e^{\frac{-(z_i - z)^2}{2\cdot (10 \mathrm{km})^2}}
\end{equation*}
The temperature is scaled in the same way as the pressure above, and is done in this way so that the quantities more than about 10 km above the observatory are effectively unchanged. The mixing ratio of all molecules is scaled in a much simpler way: if $A_r$ is the requested mixing ratio at the telescope altitude, and $A_0$ is the mixing ratio in the atmosphere profile, the mixing ratio at each layer $A_i$ is given by
\begin{equation*}
  A_i \rightarrow A_i  \frac{A_r}{A_0}
\end{equation*}
\emph{Because of the way TelFit scales the temperature, pressure, and molecular mixing ratios in every atmosphere layer, the optimized values should not be taken as a measurement of the actual surface mixing ratios.}

In each iteration for the main variables (temperature, pressure, telescope zenith angle, and molecular abundances), TelFit refines the wavelength solution and detector resolution. The wavelength fitter performs a third-order polynomial fit to adjust the wavelengths of the model such that the telluric lines match the data. Alternatively, the data wavelengths can be adjusted to fit the model. The wavelength solution of the data should be very close, as TelFit will not shift any wavelengths by more than 0.1 nm. By default, TelFit fits the spectrograph PSF as a gaussian, and fits the detector resolution by finding the best gaussian width to convolve the model against. Alternatively, TelFit can do a nonparametric fit to the spectrograph PSF using the singular value decomposition described in detail in \cite{Rucinski1999}

\section{Results}
\label{paper3_sec:results}

We fit the spectra for the rapidly-rotating A0V star HIP 20264 with a two-step approach. In the first step, we fit the observatory temperature and humidity in the echelle orders dominated by the water bands at 590 nm, 650 nm, 700 nm, and 730 nm. For the rest of the fit, the humidity and temperature were fixed at the $\chi^2$-weighted average of the fits for each band. Next, we fit the $O_2$ abundance with the $\gamma$ and B bands near 630 nm and 690 nm, respectively. Because the blue end of the B band is extremely strong and absorbs nearly all the light, we only use the red half in the fit. The $O_2$ abundance was fixed at the $\chi^2$-weighted average of the individual fitted values for the two echelle orders. We then applied the best-fit parameters to every echelle order, allowing the fitter to adjust the model wavelengths and detector resolution separately for each order. For all telluric fits in this paper, we adjusted the temperature, pressure, and water vapor atmosphere profile with sounding data from the Global Data Assimilation System (GDAS) meteorological archive\footnote{The GDAS archive is available starting in December 2004 at \url{http://ready.arl.noaa.gov/READYamet.php}. Instructions for its use are included in the code documentation.}. As stated in Section \ref{paper3_sec:obs}, we fit each frame of HIP 20264 separately, and co-added the telluric-corrected spectra. The fitted value of the relative humidity varied from $18.7\%$ to $22.1\%$, and the airmass of the star increased from 1.15 to 1.52 (a change of $19^{\circ}$ in zenith angle) over the course of the 7 frames, making the individual fits crucial.

We show the telluric correction for the observation of the A0V star HIP 20264 in four water bands and two $\mathrm{O_2}$ bands in Figures \ref{paper3_fig:bstarcorr_water} and \ref{paper3_fig:bstarcorr_oxygen}, respectively. The telluric model shown is the average telluric model of the 7 individual exposures, since we corrected each one separately to better account for the changing water vapor content and telescope zenith angle. We apply a Savitzky-Golay smoothing filter to the corrected data to remove any broad features in the stellar spectrum. The water features are corrected to near the noise level of the spectrum, with the exception of the very strong telluric water band near 730 nm. The poor correction may be due to a slightly incorrect temperature and water-vapor atmospheric profile, which causes the line profile to change appreciably for strong lines. In addition, small errors in the line strength parameters are more noticable for strong telluric lines. The correction in the $\mathrm{O_2}$ B and $\gamma$ bands (Figure \ref{paper3_fig:bstarcorr_oxygen}) is somewhat worse. The same $\mathrm{O_2}$ mixing ratio undercorrects the $\gamma$ band and slightly overcorrects the B band. This systematic error may be from incorrect line strengths in the HITRAN database for the two bands.

We observed the A1V star HIP 25608 30 minutes after HIP 20264 and at similar airmass, and so use it as a telluric standard star to directly compare our method to the empirical telluric correction method. To have comparable S/N ratios as in Figures \ref{paper3_fig:bstarcorr_water} and \ref{paper3_fig:bstarcorr_oxygen}, we co-added all 7 frames of both the the target star (HIP 20264) and the standard star (HIP 25608). We account for the different column density of absorbers in the target and standard star observations using Beer's law \citep{Beer1852} as follows. For each order, we scaled the normalized flux of the standard star ($f_s$) with

\begin{equation}
f_s \rightarrow f_s^{\gamma}
\label{paper3_eqn:correction}
\end{equation}

where $\gamma$ is determined using the normalized flux of the standard star and target star ($f_t$) and is defined as

\begin{equation}
\gamma = \text{median} \left( \frac{\log{f_t}}{\log{f_s}} \right)
\label{paper3_eqn:scale}
\end{equation}

In Equation \ref{paper3_eqn:scale}, we only use the points with $f_t < 0.95$ in the median computation, so that the noise does not dominate in the orders with few telluric lines. These empirical telluric corrections are shown in the top row of Figure \ref{paper3_fig:bstarcorr_empirical}. The corrections are quite poor in this case, mostly because the airmass of the target (standard) star changes from 1.15-1.52 (1.31-1.86) from the first to last frame, and co-adding the spectra amounts to a flux-weighted average over airmass that the correction in Equation \ref{paper3_eqn:correction} cannot capture.

We also test an empirical telluric correction using only the last frame of the target star and the first frame of the standard star, and show the result in the bottom row of Figure \ref{paper3_fig:bstarcorr_empirical}. In this case, the airmass can be treated as approximately constant and Equation \ref{paper3_eqn:correction} does a much better job in accounting for the small airmass difference between the target and standard star spectra, with the exception of the nearly saturated oxygen lines (bottom left panel). The water line correction results in similar line residual amplitudes to the telluric modeling method (see Figure \ref{paper3_fig:bstarcorr_water} for comparison).

We now turn to a more typical-use case for the TelFit code: correcting the telluric lines in late-type stars near a feature of interest. For this, we use a series of M-type stars (see Table \ref{paper3_tab:sample}) with observations near the 819 nm sodium doublet, which is used as a gravity-sensitive age indicator for late-type stars \citep{Slesnick2006}. As the stellar age and therefore surface gravity increases, the line strength increases. Amongst main sequence M-stars, later spectral types have higher surface gravity and so we expect the equivalent width of the sodium lines to increase as we go towards later spectral types.

Figure \ref{paper3_fig:mtellcorr} shows the telluric correction for the M1V star GJ 908. For this and all M-star spectra in this work, we fit the water vapor, temperature, and $O_2$ mixing ratio at the same time for the spectral order covering 819 nm. The bottom panel of Figure \ref{paper3_fig:mtellcorr} shows that each of the spectral lines after telluric correction come from the star itself, and that the telluric contamination is reduced to near the noise level of the spectrum. Importantly, the telluric lines that fall within the sodium doublet line profile no longer affect the profile shape. We can now directly compare the line profile of the sodium doublet lines as a function of spectral type, without the influence of telluric lines. Figure \ref{paper3_fig:lineprofile} shows the evolution of the doublet lines. The central depth stays about the same throughout the sequence, but the later spectral type stars show significantly broader line wings, leading to the known sequence in equivalent width \citep{Slesnick2006}. The robust recovery of the sodium line strengths and profiles demonstrate that TelFit can accurately remove telluric lines, even in feature-rich spectra.

\section{Conclusions}
\label{paper3_sec:conclusion}
We presented the TelFit code, an object-oriented Python code capable of accurately fitting the telluric spectrum in ground-based spectra. We use a high signal-to-noise ratio echelle spectrum of the A0V star HIP 20264 to demonstrate the fit quality in Figures \ref{paper3_fig:bstarcorr_water} and \ref{paper3_fig:bstarcorr_oxygen}. Contrary to expectation, the water lines are typically fit to much higher precision than the $\mathrm{O_2}$ telluric lines. This is likely coming from a systematic error in the HITRAN line strength database, since the same $O_2$ mixing ratio overfits the B band and underfits the $\gamma$ band. We compare our code to an empirical telluric correction of HIP 20264, and find that the model is at least as accurate. In fact, TelFit is significantly more accurate than the empirical method when several frames of both the target and standard star are co-added.

We also demonstrated the use of TelFit for in-depth analysis of spectral features in late-type stars with a series of M-star observations near the 819 nm sodium doublet. The telluric lines were removed to near the noise level of the observations, allowing for analysis of the sodium line profiles and recovery of the known sequence of increasing equivalent width with later spectral types. Regions contaminated by telluric lines are often ignored in optical spectral analysis; accurate correction of telluric features could help open these regions up for further analysis.

This code was mostly developed and tested for correction of optical spectra. However, an early version of this code was used in \cite{Gullikson2013} to correct for telluric methane absorption in B-star spectra, with similar line residuals to those that \cite{Seifahrt2011} found in the same spectral region. We encourage the use of TelFit for correcting telluric absorption in near-infrared as well as optical data.

We would like to thank Andreas Seifahrt for his help in the early stages of code development. This project was funded by a UT Austin Hutchinson fellowship to Kevin Gullikson and start-up funding to Sarah Dodson-Robinson from the University of Texas. This work makes use of Astropy, a community-developed core Python package for Astronomy \citep{Astropy}, as well as SciPy, NumPy \citep{Oliphant2007}, and of course LBLRTM. We would like to thank the developers of all of those packages.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%              Figures                 %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%--------------------------------------------------------------------------------------

\begin{figure}[t]
\begin{center}
\subfloat{\includegraphics[width=0.4\textwidth, height=0.23\textheight]{Figures/paper3_BstarCorr_590nm.pdf}}
\subfloat{\includegraphics[width=0.4\textwidth,height=0.23\textheight]{Figures/paper3_BstarCorr_650nm.pdf}}
\\
\noindent
\subfloat{\includegraphics[width=0.4\textwidth, height=0.23\textheight]{Figures/paper3_BstarCorr_700nm.pdf}}
\subfloat{\includegraphics[width=0.4\textwidth,height=0.23\textheight]{Figures/paper3_BstarCorr_730nm.pdf}}
\caption{Correction of the water bands in optical spectra. All spectra are of the A0V star HIP 20264, and are smoothed with a Savitzky-Golay filter after telluric correction to remove any broad features in the stellar spectrum. The top panel of each figure shows the observed spectrum (black solid line) and the best-fit telluric model (red dashed line), and the bottom panel shows the residuals after division by the telluric model. The telluric water lines are corrected to very near the noise level of the spectrum in the top row, revealing weak interstellar Na D lines (top left). The telluric correction leaves residuals on the order of $5\%$ of the continuum for strong telluric lines (bottom right), possibly due to an incorrect atmosphere profile for water vapor.}

\label{paper3_fig:bstarcorr_water}
\end{center}
\end{figure}

%--------------------------------------------------------------------------------------

\begin{figure}[t]
\begin{center}
\subfloat{\includegraphics[height=0.33\textheight]{Figures/paper3_BstarCorr_630nm.pdf}}
\\
\subfloat{\includegraphics[height=0.33\textheight]{Figures/paper3_BstarCorr_690nm.pdf}}
\caption{Correction of the $\mathrm{O_2}$ bands in optical spectra. All spectra are of the A0V star HIP 20264, and are smoothed with a Savitzky-Golay filter after telluric correction to remove any broad features in the stellar spectrum. The top panel of each figure shows the observed spectrum (black solid line) and the best-fit telluric model (red dashed line), and the bottom panel shows the residuals after division by the telluric model. }

\label{paper3_fig:bstarcorr_oxygen}
\end{center}
\end{figure}

%--------------------------------------------------------------------------------------

\begin{figure}[t]
\begin{center}
\subfloat{\includegraphics[width=0.4\textwidth,height=0.23\textheight]{Figures/paper3_Empirical_690nm.pdf}}
\subfloat{\includegraphics[width=0.4\textwidth,height=0.23\textheight]{Figures/paper3_Empirical_730nm.pdf}}
\\
\subfloat{\includegraphics[width=0.4\textwidth,height=0.23\textheight]{Figures/paper3_Empirical_LowSN_690nm.pdf}}
\subfloat{\includegraphics[width=0.4\textwidth,height=0.23\textheight]{Figures/paper3_Empirical_LowSN_730nm.pdf}}
\caption{Empirical telluric corrections. \emph{Top Row}: Correction at high S/N ratio, where all 7 frames of both the target star (HIP 20264, A0V) and the telluric standard star (HIP 25608, A1V) were co-added before the telluric correction. In this case, both the humidity and the airmass are changing throughout both exposures and the empirical telluric correction is very poor. \emph{Bottom Row}: Correction between the last frame taken of the target star and the first frame of the standard star, a more common mode of empirical correction. In this case, the empirical telluric correction is comparable to the model fitting method presented in this paper. The left column should be compared to the bottom panel in Figure \ref{paper3_fig:bstarcorr_oxygen}, and the right column should be compared to the bottom right subfigure in Figure \ref{paper3_fig:bstarcorr_water}. The poor correction in the oxygen band (lower left panel) is the result of the slight airmass difference between the science and standard stars. The correction in equation \ref{paper3_eqn:correction} is only valid for weak or moderate lines, and does not work as well for the nearly saturated lines shown here. The empirical correction under-corrects the line core while over-correcting the wings.}

\label{paper3_fig:bstarcorr_empirical}
\end{center}
\end{figure}

%--------------------------------------------------------------------------------------

\begin{figure}[t]
  \centering
  \subfloat{\includegraphics[height=0.33\textheight]{Figures/paper3_GJ908_Telluric.pdf}}
  \\
  \subfloat{\includegraphics[height=0.33\textheight]{Figures/paper3_GJ908_Stellar.pdf}}
  \caption{\emph{Top}: The observed spectrum of GJ 908 (black solid line) with the best-fit telluric spectrum (red dashed line) overplotted. There are telluric lines within the sodium doublet line profiles which affect any line shape measurements if they are not corrected. \emph{Bottom}: The telluric-corrected spectrum of GJ 908 (black solid line) with a PHOENIX model spectrum (red dashed line) overplotted to guide the eye. The model spectrum has the following parameters: $\mathrm{T_{eff}} = 3700 K$, $\log g = 4.0$, and $\mathrm{[Fe/H]} = -0.5$, and has been convolved with a gaussian to match the detector resolution. All of the remaining absorption lines in the spectrum come from the star.}

  \label{paper3_fig:mtellcorr}
\end{figure}

%--------------------------------------------------------------------------------------

\begin{figure}[t]
  \centering
  \includegraphics[width=\columnwidth]{Figures/paper3_SodiumLineProfile_BW.pdf}
  \caption{The evolution of the sodium doublet line profile with spectral type for a series of main-sequence M-type stars. The later spectral types show significantly broader line wings.}
  \label{paper3_fig:lineprofile}
\end{figure}