\chapter{3D source reconstruction: Imaging approach \label{Chap:eeg:imaging}}

This chapter describes an Imaging approach to 3D source reconstruction.

\section{Introduction\label{sec:imaginv_intro}}
This chapter focuses on the imaging (or distributed) method for implementing EEG/MEG source reconstruction in SPM. This approach results in a spatial projection of sensor data into (3D) brain space and considers brain activity as comprising a very large number of dipolar sources spread over the cortical sheet, with fixed locations and orientations. This renders the observation model linear, the unknown variables being the source amplitudes or power.

Given epoched and preprocessed data (see chapter \ref{Chap:eeg:preprocessing}), the evoked and/or induced activity for each dipolar source can be estimated, for a single time-sample or a wider peristimulus time window.

The obtained reconstructed activity is in 3D voxel space and can be further analyzed using mass-univariate analysis in SPM.

Contrary to PET/fMRI data reconstruction, EEG/MEG source reconstruction is a non trivial operation. Often compared to estimating a body shape from its shadow, inferring brain activity from scalp data is mathematically ill-posed and requires prior information such as anatomical, functional or mathematical constraints to isolate a unique and most probable solution~\cite{Baillet01}.

Distributed linear models have been around for several decades now~\cite{Dale93} and the proposed pipeline in SPM for an imaging solution is classical and very similar to common approaches in the field. However, at least two aspects are quite original and should be emphasized here:

\begin{itemize}
\item Based on an empirical Bayesian formalism, the inversion is meant to be generic in the sense it can incorporate and estimate the relevance of multiple constraints of varied nature; data-driven relevance estimation being made possible through Bayesian model comparison~\cite{peb1,cp_empirical_eeg,jm_multiple,karl_induced}.
\item The subject's specific anatomy is incorporated in the generative model of the data, in a fashion that eschews individual cortical surface extraction. The individual cortical mesh is obtained automatically from a canonical mesh in MNI space, providing a simple and efficient way of reporting results in stereotactic coordinates.
\end{itemize}

The EEG/MEG imaging pipeline is divided into four consecutive steps which characterize any inverse procedure with an additional step of summarizing the results. In this chapter, we go through each of the steps that need completing when proceeding with a full inverse analysis:

\begin{enumerate}
    \item Source space modeling,
    \item Data co-registration,
    \item Forward computation,
    \item Inverse reconstruction.
    \item Summarizing the results of inverse reconstruction as an image.
\end{enumerate}

Whereas the first three steps are part of the whole generative model, the inverse reconstruction step consists in Bayesian inversion, and is the only step involving actual EEG/MEG data.\\

\section{Getting started}

Everything which is described hereafter is accessible from the SPM user-interface by choosing the ``EEG'' application, \texttt{3D Source Reconstruction} button. When you press this button a new window will appear with a GUI that will guide you through the necessary steps to obtain an imaging reconstruction of your data. At each step, the buttons that are not yet relevant for this step will be disabled. When you open the window the only two buttons you can press are \texttt{Load} which enables you to load a pre-processed SPM MEEG dataset and the \texttt{Group inversion} button that will be described below. You can load a dataset which is either epoched with single trials for different conditions, averaged with one event related potential (ERP) per condition, or grand-averaged. An important pre-condition for loading a dataset is that it should contain sensors and fiducials. This will be checked when you load a file and loading will fail in case of a problem. You should make sure that for each modality present in the dataset as indicated by channel types (either EEG or MEG) there is a sensor description. If, for instance, you have an MEG dataset with some EEG channels that you don't actually want to use for source reconstruction, change their type to ``\textit{LFP}'' or ``\textit{Other}'' before trying to load the dataset (the difference is that \textit{LFP} channels will stil be filtered and available for artefact detection whereas \textit{Other} channels won't). MEG datasets converted by SPM from their raw formats will always contain sensor and fiducial descriptions. In the case of EEG for some supported channel setups (such as extended 10-20 or BioSemi) SPM will provide default channel locations and fiducials that you can use for your reconstruction. Sensor and fiducial descriptions can be modified using the \texttt{Prepare} interface and in this interface you can also verify that these descriptions are sensible by performing a coregistration (see chapter \ref{Chap:eeg:preprocessing} and also below for more details about coregistration).

When you successfully load a dataset you are asked to give a name to the present analysis cell. In SPM it is possible to perform multiple reconstructions of the same dataset with different parameters. The results of these reconstructions will be stored with the dataset if you press the \texttt{Save} button. They can be loaded and reviewed again using the 3D GUI and also with the SPM EEG \textsc{Review} tool. From the command line you can access source reconstruction results via the \texttt{D.inv} field of the \texttt{meeg} object. This field (if present) is a cell array of structures and does not require methods to access and modify it. Each cell contains the results of a different reconstruction. In the GUI you can navigate between these cells using the buttons in the second row. You can also create, delete and clear cells. The label you input at the beginning will be attached to the cell for you to identify it.

\section{Source space modeling}

After entering the label you will see the \texttt{Template} and \texttt{MRI} button enabled. The \texttt{MRI} button will create individual head meshes describing the boundaries of different head compartments based on the subject's structural scan. SPM will ask for the subject's structural image. It might take some time to prepare the model as the image  needs to be segmented. The individual meshes are generated by applying the inverse of the deformation field needed to normalize the individual structural image to MNI template to canonical meshes derived from this template. This method is more robust than deriving the meshes from the structural image directly and can work even when the quality of the individual structural images is low.

Presently we recommend the \texttt{Template} button for EEG and a head model based on an individual structural scan for MEG. In the absence of individual structural scan combining the template head model with the individual headshape also results in a quite precise head model. The \texttt{Template} button uses SPM's template head model based on the MNI brain. The corresponding structural image can be found under \texttt{canonical$\backslash$single\_subj\_T1.nii} in the SPM directory. When you use the template, different things will happen depending on whether your data is EEG or MEG. For EEG, your electrode positions will be transformed to match the template head. So even if your subject's head is quite different from the template, you should be able to get good results. For MEG, the template head will be transformed to match the fiducials and headshape that come with the MEG data. In this case having a headshape measurement can be quite helpful in providing SPM with more data to scale the head correctly. From the user's perspective the two options will look quite similar.

No matter whether the \texttt{MRI} or \texttt{Template} button was used the cortical mesh, which describes the locations of possible sources of EEG and MEG signal, is obtained from a template mesh. In the case of EEG the mesh is used as is, and in the case of MEG it is transformed with the head model. Three cortical mesh sizes are available ''coarse'', ''normal'' and ''fine'' (5124, 8196 and 20484 vertices respectively).  It is advised to work with the ''normal'' mesh. Choose ''coarse'' if your computer has difficulties handling the ''normal'' option. ''Fine'' will only work on 64-bit systems and is probably an overkill.

\section{Coregistration}

In order for SPM to provide a meaningful interpretation of the results of source reconstruction, it should link the coordinate system in which sensor positions are originally represented to the coordinate system of a structural MRI image (MNI coordinates). In general, to link between two coordinate systems you will need a set of at least 3 points whose coordinates are known in both systems. This is a kind of \textit{Rosetta stone} that can be used to convert a position of any point from one system to the other. These points are called ``fiducials'' and the process of providing SPM with all the necessary information to create the \textit{Rosetta stone} for your data is called ``coregistration''.

There are two possible ways of coregistrating the EEG/MEG data into the structural MRI space.

\begin{enumerate}
    \item A Landmark based coregistration (using fiducials only).\\
    The rigid transformation matrices (Rotation and Translation) are computed such that they match each fiducial in the EEG/MEG space into the corresponding one in sMRI space. The same transformation is then applied to the sensor positions.
    \item Surface matching (between some headshape in MEG/EEG space and some sMRI derived scalp tesselation).\\
    For EEG, the sensor locations can be used instead of the headshape. For MEG, the headshape is first coregistrated into sMRI space; the inverse transformation is then applied to the head model and the mesh.\\
Surface matching is performed using an Iterative Closest Point algorithm (ICP). The ICP algorithm~\cite{Besl_McKay} is an iterative alignment algorithm that works in three phases:
\begin{itemize}
    \item Establish correspondence between pairs of features in the two structures that are to be aligned based on proximity;
    \item Estimate the rigid transformation that best maps the first member of the pair onto the second;
    \item Apply that transformation to all features in the first structure. These three steps are then reapplied until convergence is concluded. Although simple, the algorithm works quite effectively when given a good initial estimate.
\end{itemize}
\end{enumerate}

In practice what you will need to do after pressing  the \texttt{Coregister} button is to specify the points in the sMRI image that correspond to your M/EEG fiducials. If you have more fiducials (which may happen for EEG as in principle any electrode can be used as a fiducial), you will be ask at the first step to select the fiducials you want to use. You can select more than 3, but not less. Then for each M/EEG fiducial you selected you will be asked to specify the corresponding position in the sMRI image in one of 3 ways.

\begin{itemize}
\item \texttt{select} - locations of some points such as the commonly used nasion and preauricular points and also CTF recommended fiducials for MEG (as used at the FIL) are hard-coded in SPM. If your fiducial corresponds to one of these points you can select this option and then select the correct point from a list.
\item \texttt{type} - here you can enter the MRI coordinates in mm for your fiducial ($1 \times 3$ vector). If your fiducial is not on SPM's hard-coded list, it is advised to carefully find the right point on either the template image or on your subject's own image normalized to the template. You can do it by just opening the image using SPM's Display/images functionality. You can then record the MNI coordinates and use them in all coregistrations you need to do using the ``type'' option.
\item \texttt{click} - here you will be presented with a structural image where you can click on the right point. This option is good for ``quick and dirty'' coregistration or to try out different options.
\end{itemize}

You will also have the option to skip the current fiducial, but remember you can only do it if you eventually specify more than 3 fiducials in total. Otherwise the coregistration will fail.

After you specify the fiducials you will be asked whether to use the headshape points if they are available. For EEG it is advised to always answer ``yes''. For MEG if you use a head model based on the subject's sMRI and have precise information about the 3 fiducials (for instance by doing a scan with fiducials marked by vitamin E capsules) using the headshape might actually do more harm than good. In other cases it will probably help, as in EEG.

The results of coregistration will be presented in SPM's graphics window. It is important to examine the results carefully before proceeding. In the top plot you will see the scalp, the inner skull and the cortical mesh with the sensors and the fiducials. For EEG make sure that the sensors are on the scalp surface. For MEG check that the head positon in relation to the sensors makes sense and the head does not for instance stick outside the sensor array. In the bottom plot the sensor labels will be shown in topographical array. Check that the top labels correspond to anterior sensors, bottom to posterior, left to left and right to right and also that the labels are where you would expect them to be topographically.

\section{Forward computation (\textit{forward})}
This refers to computing for each of the dipoles on the cortical mesh the effect it would have on the sensors. The result is a $N \times M$ matrix where N is the number of sensors and M is the number of mesh vertices (that you chose from several options at a previous step). This matrix can be quite big and it is, therefore, not stored in the header, but in a separate \texttt{*.mat} file which has \texttt{SPMgainmatrix} in its name and is written in the same directory as the dataset. Each column in this matrix is a so called ``lead field'' corresponding to one mesh vertex.

The lead fields are computed using the ``forwinv'' toolbox\footnote{forwinv: \url{http://fieldtrip.fcdonders.nl/development/forwinv}} developed by Robert Oostenveld, which SPM shares with FieldTrip. This computation is based on Maxwell's equations and makes assumptions about the physical properties of the head. There are different ways to specify these assumptions which are known as ``forward models''.

The ``forwinv'' toolbox can support different kinds of forward models. When you press \texttt{Forward Model} button (which should be enabled after successful coregistration), you will have a choice of several head models depending on the modality of your dataset. We presently recommend useing a single shell model for MEG and ``EEG BEM'' for EEG. You can also try other options and compare them using model evidence (see below). The first time you use the EEG BEM option with a new structural image (and also the first time you use the \texttt{Template} option) a lengthy computation will take place that prepares the BEM model based on the head meshes. The BEM will then be saved in a quite large \texttt{*.mat} file with ending \texttt{\_EEG\_BEM.mat} in the same directory with the structural image (''canonical'' subdirectory of SPM for the template). When the head model is ready, it will be displayed in the graphics window with the cortical mesh and sensor locations you should verify for the final time that everything fits well together.

The actual lead field matrix will be computed at the beginning of the next step and saved. This is a time-consuming step and it takes longer for high-resolution meshes. The lead field file will be used for all subsequent inversions if you do not change the coregistration and the forward model.

\section{Inverse reconstruction}
To get started press the \texttt{Invert} button. The first choice you will see is between \texttt{Imaging}, \texttt{VB-ECD} and \texttt{DCM}. For reconstruction based on an empirical Bayesian approach to localize either the evoked response, the evoked power or the induced power, as measured by EEG or MEG press the \texttt{Imaging} button. The other options are explained in greater detail elsewhere.

If you have trials belonging to more than one condition in your dataset then the next choice you will have is whether to invert all the conditions together or to choose a subset. It is recommended to invert the conditions together if you are planning to later do a statistical comparison between them. If you have only one condition, or after choosing the conditions, you will get a choice between ``Standard'' and ``Custom'' inversion. If you choose ``Standard'' inversion, SPM will start the computation with default settings. These correspond to the multiple sparse priors (MSP) algorithm \cite{karl_msp} which is then applied to the whole input data segment.

If you want to fine-tune the parameters of the inversion, choose the ``Custom'' option. You will then have the possibility to choose between several types of inversion differing by their hyperprior models (IID - equivalent to classical minimum norm, COH - smoothness prior similar to methods such as LORETA) or the MSP method .

You can then choose the time window that will be available for inversion. Based on our experience, it is recommended to limit the time window to the activity of interest in cases when the amplitude of this activity is low compared to activity at other times. The reason is that if the irrelevant high-amplitude activity is included, the source reconstruction scheme will focus on reducing the error for reconstructing this activity and might ignore the activity of interest. In other cases, when the peak of interest is the strongest peak or is comparable to other peaks in its amplitude, it might be better not to limit the time window to let the algorithm model all the brain sources generating the response and then to focus on the sources of interest using the appropriate contrast (see below). There is also an option to apply a hanning taper to the channel time series in order to downweight the possible baseline noise at the beginning and end of the trial. There is also an option to pre-filter the data. Finally, you can restrict solutions to particular brain areas by loading a \texttt{*.mat} file with a $K \times 3$ matrix containing MNI coordinates of the areas of interest. This option may initially seem strange, as it may seem to overly bias the source reconstructions returned. However, in the Bayesian inversion framework you can compare different inversions of the same data using Bayesian model comparison. By limiting the solutions to particular brain areas you greatly simplify your model and if that simplification really captures the sources generating the response, then the restricted model will have much higher model evidence than the unrestricted one. If, however, the sources you suggested cannot account for the data, the restriction will result in a worse model fit and depending on how much worse it is, the unrestricted model might be better in the comparison. So using this option with subsequent model comparison is a way, for instance, to integrate prior knowledge from the literature or from fMRI/PET/DTI into your inversion. It also allows for comparison of alternative prior models.

Note that for model comparison to be valid all the settings that affect the input data, like the time window, conditions used and filtering should be identical.

SPM imaging source reconstruction also supports multi-modal datasets. These are datasets that have both EEG and MEG data from a simultaneous recording. Datasets from the ''Neuromag'' MEG system which has two kinds of MEG sensors are also treated as multimodal. If your dataset is multimodal a dialogue box will appear asking to select the modalities for source reconstruction from a list. If you select more than one modality, multiomodal fusion will be performed. This option based on the paper by Henson et al. \cite{rnah_fusion} uses a heuristic to rescale the data from different modalities so that they can be used together.

Once the inversion is completed you will see the time course of the region with maximal activity in the top plot of the graphics window. The bottom plot will show the maximal intensity projection (MIP) at the time of the maximal activation. You will also see the log-evidence value that can be used for model comparison, as explained above. Note that not all the output of the inversion is displayed. The full output consists of time courses for all the sources and conditions for the entire time window. You can view more of the results using the controls in the bottom right corner of the 3D GUI. These allow focusing on a particular time, brain area and condition. One can also display a movie of the evolution of neuronal activity.

\section{Summarizing the results of inverse reconstruction as an image}
SPM offers the possibility of writing the results as 3D NIfTI images, so that you can then proceed with GLM-based statistical analysis using Random Field theory. This is similar to the 2nd level analysis in fMRI for making inferences about region and trial-specific effects (at the between subject level).

This entails summarizing the trial- and subject-specific responses with a single 3-D image in source space. Critically this involves prompting for a time-frequency contrast window to create each contrast image. This is a flexible and generic way of specifying the data feature you want to make an inference about (e.g., gamma activity around 300 ms or average response between 80 and 120 ms). This kind of contrast is specified by pressing the \texttt{Window} button. You will then be asked about the time window of interest (in ms, peri-stimulus time). It is possible to specify one or more time segments (separated by a semicolon). To specify a single time point repeat the same value twice. The next question is about the frequency band. If you just want to average the source time course leave that at the default, zero. In this case the window will be weighted by a Gaussian. In the case of a single time point this will be a Gaussian with 8 ms full width half maximum (FWHM). If you specify a particular frequency or a frequency band, then a series of Morlet wavelet projectors will be generated summarizing the energy in the time window and band of interest.

There is a difference between specifying a frequency band of interest as zero, as opposed to specifying a wide band that covers the whole frequency range of your data. In the former case the time course of each dipole will be averaged, weighted by a gaussian. Therefore, if within your time window this time course changes polarity, the activity can average out and in an ideal case even a strong response can produce a value of zero. In the latter case the power is integrated over the whole spectrum ignoring phase, and this would be equivalent to computing the sum of squared amplitudes in the time domain.

Finally, if the data file is epoched rather than averaged, you will have a choice between ``evoked'', ``induced'' and ``trials''. If you have multiple trials for certain conditions, the projectors generated at the previous step can either be applied to each trial and the results averaged (induced) or applied to the averaged trials (evoked). Thus it is possible to perform localization of induced activity that has no phase-locking to the stimulus. It is also possible to focus on frequency content of the ERP using the ``evoked'' option. Clearly the results will not be the same. The projectors you specified (bottom plot) and the resulting MIP (top plot) will be displayed when the operation is completed. ``trials'' option makes it possible to export an image per trial which might be useful for doing within-subject statistics. The images are exported as 4D-NIfTI with one file per condition including all the trials for that condition.

The \texttt{Image} button is used to write out the contrast results. It is possible to export them as either values on a mesh (GIfTI) or volumetric 3D images (NIfTI). Both formats are supported by SPM statistical machinery. When generating an image per trial the images are exported as 4D-NIfTI with one file per condition including all the trials for that condition. The values of the exported images are normalized to reduce between-subject variance. Therefore, for best results it is recommended to export images for all the time windows and conditions that will be included in the same statistical analysis in one step. Note that the images exported from the source reconstruction are a little peculiar because of smoothing from a 2D cortical sheet into 3D volume. SPM statistical machinery has been optimized to deal with these peculiarities and get sensible results. If you try to analyze the images with older versions of SPM or with a different software package you might get different (less focal) results.

\section{Rendering interface}
By pressing the \texttt{Render} button you can open a new GUI window which will show you a rendering of the inversion results on the brain surface. You can rotate the brain, focus on different time points, run a movie and compare the predicted and observed scalp topographies and time series. A useful option is ``virtual electrode'' which allows you to extract the time course from any point on the mesh and the MIP at the time of maximal activation at this point. Just press the button and click anywhere in the brain.\\
An additional tool for reviewing the results is available in the SPM M/EEG \textsc{Review} function.

\section{Group inversion}
A problem encountered with MSP inversion is that sometimes it is ``too good'', producing solutions that were so focal in each subject that the spatial overlap between the activated areas across subjects was not sufficient to yield a significant result in a between-subjects contrast. This could be improved by smoothing, but smoothing compromises the spatial resolution and thus subverts the main advantage of using an inversion method that can produce focal solutions.

To circumvent this problem we proposed a modification of the MSP method \cite{vl_group} that effectively restricts the activated sources to be the same in all subjects with only the degree of activation allowed to vary. We showed that this modification makes it possible to obtain significance levels close to those of non-focal methods such as minimum norm while preserving accurate spatial localization.

The group inversion can yield much better results than individual inversions because it introduces an additional constraint for the ill-posed inverse problem, namely that the responses in all subjects should be explained by the same set of sources. Thus it should be your method of choice when analyzing an entire study with subsequent GLM analysis of the images.

Group inversion works very similarly to what was described above. You can start it by pressing the ``Group inversion'' button right after opening the 3D GUI. You will be asked to specify a list of M/EEG data sets to invert together. Then the routine will ask you to perform coregistration for each of the files and specify all the inversion parameters in advance. It is also possible to specify the contrast parameters in advance. Then the inversion will proceed by computing the inverse solution for all the files and will write out the output images. The results for each subject will also be saved in the header of the corresponding input file. It is possible to load this file into the 3D GUI after the inversion and explore the results as described above.

\section{Batching source reconstruction}
There is a possibility to run imaging source reconstruction using the SPM batch tool. It can be accessed by pressing the ``Batch'' button in the main SPM window and then going to ``M/EEG source reconstruction'' in the ``SPM'' under ``M/EEG''. There are separate tools there for building head models, computing the inverse solution and computing contrasts and generating images. This makes it possible for instance to generate images for several different contrasts from the same inversion. All the three tools support multiple datasets as inputs. In the case of the inversion tool group inversion will be done for multiple datasets.

\section{Appendix: Data structure}
The \matlab\ object describing a given EEG/MEG dataset in SPM is denoted as \textit{D}.
Within that structure, each new inverse analysis will be described by a new cell of sub-structure
field \textit{D.inv} and will be made of the following fields:

\begin{itemize}
    \item \texttt{method}: character string indicating the method, either ``ECD'' or ``Imaging'' in present case;
    \item \texttt{mesh}: sub-structure with relevant variables and filenames for source space and head modeling;
    \item \texttt{datareg}: sub-structure with relevant variables and filenames for EEG/MEG data registration into MRI space;
    \item \texttt{forward}: sub-structure with relevant variables and filenames for forward computation;
    \item \texttt{inverse}: sub-structure with relevant variable, filenames as well as results files;
    \item \texttt{comment}: character string provided by the user to characterize the present analysis;
    \item \texttt{date}: date of the last modification made to this analysis.
    \item \texttt{gainmat}: name of the gain matrix file.
\end{itemize}


\documentclass[12pt, preprint]{aastex}
\usepackage{bm, graphicx, subfigure, amsmath, morefloats}
\bibliographystyle{apj}

% naming macros
\newcommand{\tc}{\textsl{The~Cannon}}
\newcommand{\apogee}{\textsl{APOGEE}}

% math and symbol macros
\newcommand{\set}[1]{\bm{#1}}
\newcommand{\starlabel}{\ell}
\newcommand{\starlabelvec}{\set{\starlabel}}
\newcommand{\mean}[1]{\overline{#1}}
\newcommand{\given}{\,|\,}
\newcommand{\teff}{\mbox{$\rm T_{eff}$}}
\newcommand{\kms}{\mbox{$\rm kms^{-1}$}}
\newcommand{\feh}{\mbox{$\rm [Fe/H]$}}
\newcommand{\xfe}{\mbox{$\rm [X/Fe]$}}
\newcommand{\alphafe}{\mbox{$\rm [\alpha/Fe]$}}
\newcommand{\mh}{\mbox{$\rm [M/H]$}}
\newcommand{\logg}{\mbox{$\rm \log g$}}
\newcommand{\noise}{\sigma_{n\lambda}}
\newcommand{\scatter}{s_{\lambda}}
\newcommand{\pix}{\mathrm{pix}}
\newcommand{\rfn}{\mathrm{ref}}

\begin{document}

\title{\tc\ in the Gaussian Process Framework: \\ Data-driven spectral
model determination}
\author{A.Y.Q.~Ho\altaffilmark{1},
D.~Foreman-Mackey\altaffilmark{2},
M.~Ness\altaffilmark{1},
David~W.~Hogg\altaffilmark{1,2,3},
H.-W.~Rix\altaffilmark{1}
}
\altaffiltext{1}{Max-Planck-Institut f\"ur Astronomie, K\"onigstuhl 17, D-69117 Heidelberg, Germany}
\altaffiltext{2}{Center for Cosmology and Particle Physics, Department of Phyics,
New York University, 4 Washington Pl., room 424, New York, NY, 10003, USA}
\altaffiltext{3}{Center for Data Science, New York University, 726 Broadway, 7th Floor, New York, NY 10003, USA}

\email{annaho@mpia.de}

\begin{abstract}

\tc\ is a data-driven method for determining stellar labels (parameters and
abundances) from stellar spectra in the context of vast spectroscopic surveys.
For a ``training set'' of stars with known labels, \tc\ fits for a spectral
model that describes how the flux in each pixel of the spectrum depends on the
labels of the star. The model is then applied to the rest of the stars in the
survey to solve for their labels.
In the first iteration of \tc\ \ref{ness2015} the functional form of the
spectral model was determined through empirical experimentation and held fixed
across the spectrum: only the coefficients varied between pixels.
This constituted a severe limitation: the functional form of the model was too
flexible for some pixels
not flexible enough for others. In this paper, we cast \tc\ into the framework
of a Gaussian Process. In this framework, instead of optimizing for
coefficients of a fixed spectral model at each pixel, we optimize for the
functional form of the spectral model at each pixel.

\end{abstract}

\keywords{
methods: data analysis
---
methods: statistical
---
stars:abundances
---
stars: fundamental parameters
---
surveys
---
techniques: spectroscopic}

\section{Introduction}

\tc\ is a data-driven method for determining stellar labels (parameters and
abundances) from stellar spectra in the context of vast spectroscopic surveys.
For a ``training set'' of stars with known labels, \tc\ fits for a spectral
model that describes how the flux in each pixel of the spectrum depends on the
labels of the star. The model is then applied to the rest of the stars in the
survey to solve for their labels.
In the first iteration of \tc\ \ref{ness2015} the functional form of the
spectral model was determined through empirical experimentation and held fixed
across the spectrum: only the coefficients varied between pixels.

More specifically, we took the
spectral model to be characterized by a coefficient vector $\theta_\lambda$
that allowed us to predict the flux at every pixel $f_{n \lambda}$ for a
given label vector $\textbf{l}_n$:

\begin{equation}
  f_{n\lambda} = g(\starlabelvec_n | \set{\theta}_\lambda) + \mbox{noise}
\end{equation}

\begin{equation}
  f_{n\lambda} = \set{\theta}_\lambda^T \cdot \starlabelvec_n + \mbox{noise}
\end{equation}

In this case, we optimized for coefficients of a model that was quadratic in the
labels, such that the label vector was,

\begin{equation}
  \starlabelvec_n \equiv
  [1, \teff, \logg, \feh, \teff^2, \teff\cdot\logg, \teff\cdot\feh, \logg^2, \logg\cdot\feh, \feh^2]
\end{equation}

A major limitation of this existing framework is that the model is inflexible:
although a set of coefficients is uniquely determined for each pixel,
the functional form of the model itself is held fixed across pixels.
This, however, is not physically motivated. Indeed, the
polynomial family is probably not the best family of functions to be
exploring, since they extrapolate badly (edge effects) and require explicit,
qualitative choices about order and cross-terms. In that paper, we mentioned
the hope to eventually move to a non-parametric form for the functions,
such that model complexity would be controlled by continuous parameters
and at each pixel the functional form would only be complex as warranted
by the training data.

To give a more specific example, different pixels should have
different functional dependencies on labels: for example, one might expect a
continuum pixel to be linear, while a pixel in a magnesium line should show
polynomial (?) dependence on temperature. In particular, absorption features,
particularly strong lines, are known to vary non-linearly as a function of
stellar labels. And, indeed, in the paper, systematic discrepencies were found.

Extending \emph{The Cannon} to a Gaussian process framework is one way of
allowing the training data to determine the functional form and complexity
of the model at each pixel. We now provide a brief introduction to Gaussian
Processes: an excellent, more detailed review can be found here and here.

A Gaussian Process is a probability distribution over functions. More
precisely, it is a collection of random variables, any finite number of which
have a joint Gaussian distribution.
It is an example of a stochastic process: the generalization
of a probability distribution (which describes a finite-dimensional random
variable) to function space. Using some set of finite training data, our goal
is to solve for a function that can make a predictions for all possible
input values: this is regression, input-output mapping (learning) for
continuous inputs. This is a problem of induction: moving from points you
know to points you don't know. It basically entails doing inference in
function space.

In order to fit for the function, you need to begin by making some assumptions
about the characteristics of the
underlying function. This entails assigning a prior probability to every
possible function, essentially specifying a probability distribution over
the functions themselves, by specifying a mean and covariance function.
This is a prior distribution over the functions.
It turns out that you only need to deal with
some of the points in a function governed by a Gaussian distribution in order
to get the answer that you would if you had access to all of the points.
This computational tractability is a big bonus of Gaussian processes.
And at the end you get
a mean prediction and a posterior distribution.

The basic structure of \tc\ remains unchanged: we still have a training step
in which we use a set of training objects to fit for a model, and then a test
step in which we apply that model to solve for labels of the test objects.
Below we walk through what it
means to cast each step into the Gaussian Process framework.

\section{\emph{The Cannon} Training Step in the Gaussian Process Framework}

For each pixel, we optimize for a set of five hyperparameters that fully
describe the covariance matrix that characterizes the Gaussian process model.

We begin by selecting the functional form of the covariance between any
two outputs (in this case, between the flux value at a given pixel for any
two objects, across
label space). We choose a squared exponential covariance function.

For a set of $N_{ref}$ reference objects $n_1, n_2, \dots, n_{N_{ref}}$,
each has a continuum-normalized
flux measurement $f_{n \lambda}$ at wavelength $\lambda$ and a set of
$K$ training labels $l_{nk}$
Say we have a set of $N_{ref}$ training objects, each with $k$ training labels
$l = l_{nk_1}, l_{nk_2}, \cdots, l_{nk_K}$.

At any given pixel, the covariance between any pair of flux values for two
stars $n_i$ and $n_j$ with label sets $l_{n_i k}$ and $l_{n_j k}$,
$ f_{n_i \lambda}$ and $f_{n_j \lambda}$ across label space is:

\begin{equation}
  Cov(f_{n_i \lambda}, f_{n_j \lambda}) =
  a_\lambda^2 \exp(-\frac{1}{2}
  \sum_{k=1}^K \frac{(l_{n_i k}-l_{n_j k})^2}{\tau_{\lambda k}^2}) +
  \delta_{ij} s_\lambda^2
\end{equation}

More compactly, the covariance matrix $\Sigma$ can be written as follows:

\begin{equation}
  \Sigma_{ij} =
  a_\lambda^2 \exp(-\frac{1}{2}
  \sum_{k=1}^K \frac{(l_{i k}-l_{j k})^2}{\tau_{\lambda k}^2}) +
  \delta_{ij} s_\lambda^2
\end{equation}

Note a few things. First, the covariance between outputs is a function of the
input labels. Second, this covariance approaches unity as the points become
closer together in label space and becomes smaller as the distance between
the points increases. Thus the variable $\tau$ can informally be thought of
as a length scale over which flux values become more ``different`` or contain
less information about each other.

In this equation, the pixel-dependent values $a, \tau_k, $ and $s$ constitute
a set of hyperparameters $\theta$ that determine a Gaussian process model. Thus,
each pixel has its own Gaussian process model characterized by hyperparameters,
the number of which depends on the number of labels. For three labels, then,
the set of hyperparameters at a given pixel would look like this:

\begin{equation}
  \theta_\lambda = a_\lambda,
  \tau_{\lambda 0}, \tau_{\lambda 1}, \tau_{\lambda 2}, s_\lambda
\end{equation}

In a nutshell, we optimize by maximizing the log marginal likelihood for a
single pixel across all objects. The mathematics of how to do so is laid out
below\ldots

The marginal likelihood $p(g|X)$ is the integral of the likelihood times the
prior. It refers to the marginalization over the function values g.

\begin{equation}
  p(f|X) = \int p(f|g,X) p(g|X)dg
  \label{}
\end{equation}

For the special case of a Gaussian process model, we have that

\begin{equation}
  \ln{p}(\textbf{f} | \theta, X) =
  -\frac{1}{2} \textbf{f}^{T}\,\Sigma^{-1}\,\textbf{f}-\frac{1}{2}\ln{|\Sigma|}
\end{equation}

In practice, inverting the covariance matrix is computationally challenging.
It is faster and numerically more stable to avoid directly inverting the
matrix: here, we use Cholesky decomposition and the associated routines
in the scipy linear algebra package.

In Cholesky decomposition, we decompose the covariance matrix as follows:

\begin{equation}
  \Sigma = L^T L
  \label{}
\end{equation}

where L is called the Cholesky factor. We also introduce $\alpha$,

\begin{equation}
  \Sigma \, \alpha = \textbf{f}
  \label{}
\end{equation}

such that the expression for the log likelihood can be written as follows:

\begin{equation}
  \ln{p}(\textbf{f} | \theta, X) =
  -\frac{1}{2} \textbf{f}^T \alpha - \sum_i \ln{L_{ii}}
\end{equation}

We use the scipy optimize routine to find, for each pixel, the set of
hyperparameters (in this case, five) that maximize the log likelihood.
In other words, we're looking for the hyperparameters that make it most likely
that this set of flux value and training label values would be measured.

Once we have those hyperparameters, the training step is over: we now have
a Gaussian Process model at each pixel of the spectrum.

\section{\emph{The Cannon} Test Step in the Gaussian Process Framework}

While the training step took place across pixel space, optimizing for
hyperparameters across objects at each pixel independently, the test step
will take place across object space, optimizing for labels across pixels at
each object independently.

Now, we have a set of $(N_{ref}, K)$ hyperparameters where $N_{ref}$ is
the number of training objects and $K$ is the number of training labels
describing each object.

In the end, we get a mean function and a variance function.

We have a vector of covariances between the test point and the training
points. Denote this $\Sigma(X,X_*)$. Then the mean function is

\begin{equation}
  \bar{g}_* = \Sigma(X_*, X)\,\Sigma(X, X)^{-1}\,f
  \label{}
\end{equation}

which, in the $\alpha$ and $L$ framework we adopt for computational
tractability, is

\begin{equation}
  \bar{g}_* = \Sigma(X, X_*)\,\alpha
  \label{}
\end{equation}

and the covariance array corresponding to the Gaussian process model is

\begin{equation}
  \sigma_* = \Sigma(X_*, X_*)-\Sigma(X_*,X)\,\Sigma(X,X)^{-1}\,K(X,X_*)
  \label{}
\end{equation}

For some reason, that first term is actually just
$\Sigma(X_*, X_*) = a^2 + \sigma$ where $\sigma$ here is the uncertainties
in the fluxes in the data.

\begin{equation}
  \sigma_* = \sigma_f + a^2 - \Sigma(X_*,X) (L^T)^{-1}L^{-1} K_*[0]
  \label{}
\end{equation}

Now that we have both the mean and variance function,
we can write a test log likelihood function that optimizes over the full pixel
and 3D label space in order to determine, for a particular object, which
combination of labels is most likely given the measured flux at that pixel.

\begin{equation}
  \ln{p(f|l)} = -\frac{1}{2} \frac{(f-\bar{g}_*)^2}{\sigma_*^2} - \ln{\sigma_*^2}
  \label{}
\end{equation}

We perform the optimization for each object individually, across all pixels,
using the scipy.optimize minimize routine.

\section{Results: Comparison with Quadratic Model}

\section{Discussion}

One challenge is the computational time. Much slower than simply choosing
a quadratic model. The training steps scales
with the cube of the number of training objects. In this case, with 544
training objects, it took 20
hours on a 4-core processor, with the operations running in parallel (all
pixels are independent.)

Labels for the test objects can also be
evaluated in parallel, and the test step took X hours on a 4-core processor.
The matrix inversion is the time-consuming step, but as you can see from this
equation, this step only depends on the training set: thus, we can evalute
this for each pixel only once, before performing the optimization for each
pixel.

\section{Acknowledgements}

AYQH was partially supported by a Fulbright grant through the German-American
Fulbright Commission.

The research has received funding from the European Research Council under the
European Union's Seventh Framework Programme (FP 7) ERC Grant Agreement n.
[321035].

\begin{thebibliography}{24}

  \bibitem[{ {Ness}{ et~al.}(2015){Ness}, {Hogg}, {Rix}, {Ho}, \&
    {Zasowski}}]{ness2015}
    {Ness}, M., {Hogg}, D.W., {Rix}, H.-W., {Ho}, A.Y.Q., \& {Zasowski}, G. 2015

  \bibitem[{ {Rasmussen}{et~al.}(2006){Rasmussen}, \& {Williams}}]{rasmussen2006}
    {Rasmussen}, C.E., {Williams}, C.K.I. 2006

\end{thebibliography}

\end{document}