\documentclass[sigplan]{acmart}\settopmatter{printfolios=true,printccs=false,printacmref=false}
\usepackage{calc}
\sloppy

\settopmatter{printacmref=false} % Removes citation information below abstract
\renewcommand\footnotetextcopyrightpermission[1]{} % removes footnote with conference information in first column
\pagestyle{plain} % removes running headers

\acmConference[PROBPROG'18]{The International Conference on Probabilistic Programming}{October 04--06, 2018}{Boston, MA, USA}
\acmYear{2018}
\acmISBN{} % \acmISBN{978-x-xxxx-xxxx-x/YY/MM}
\acmDOI{} % \acmDOI{10.1145/nnnnnnn.nnnnnnn}
\startPage{1}

\setcopyright{none}
\bibliographystyle{ACM-Reference-Format}
\citestyle{acmnumeric}     %% For numeric citations
%% Citation style
%\citestyle{acmauthoryear}  %% For author/year citations
%\citestyle{acmnumeric}     %% For numeric citations
%\setcitestyle{nosort}      %% With 'acmnumeric', to disable automatic
                            %% sorting of references within a single citation;
                            %% e.g., \cite{Smith99,Carpenter05,Baker12}
                            %% rendered as [14,5,2] rather than [2,5,14].
%\setcitesyle{nocompress}   %% With 'acmnumeric', to disable automatic
                            %% compression of sequential references within a
                            %% single citation;
                            %% e.g., \cite{Baker12,Baker14,Baker16}
                            %% rendered as [2,3,4] rather than [2-4].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Note: Authors migrating a paper from traditional SIGPLAN
%% proceedings format to PACMPL format must update the
%% '\documentclass' and topmatter commands above; see
%% 'acmart-pacmpl-template.tex'.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Some recommended packages.
\usepackage{booktabs}   %% For formal tables:
                        %% http://ctan.org/pkg/booktabs
\usepackage{subcaption} %% For complex figures with subfigures/subcaptions
                        %% http://ctan.org/pkg/subcaption

\setlength{\belowcaptionskip}{-10pt}

\begin{document}

%% Title information
\title[GATK gCNV]{GATK gCNV: accurate germline copy-number variant discovery from sequencing read-depth data}

\author{Mehrtash Babadi}
\email{mehrtash@broadinstitute.org}          %% \email is recommended
\affiliation{
  \department{Data Sciences Platform}              %% \department is recommended
  \institution{Broad Institute}            %% \institution is required
  \streetaddress{415 Main St}
  \city{Cambridge}
  \state{MA}
  \postcode{02142}
  \country{USA}                    %% \country is recommended
}

\author{Samuel K. Lee}
\email{slee@broadinstitute.org}          %% \email is recommended
\affiliation{
  \department{Data Sciences Platform}              %% \department is recommended
  \institution{Broad Institute}            %% \institution is required
  \streetaddress{415 Main St}
  \city{Cambridge}
  \state{MA}
  \postcode{02142}
  \country{USA}                    %% \country is recommended
}

\author{Andrey N. Smirnov}
\email{asmirnov@broadinstitute.org}          %% \email is recommended
\affiliation{
  \department{Data Sciences Platform}              %% \department is recommended
  \institution{Broad Institute}            %% \institution is required
  \streetaddress{415 Main St}
  \city{Cambridge}
  \state{MA}
  \postcode{02142}
  \country{USA}                    %% \country is recommended
}

\begin{abstract}
We introduce GATK gCNV, a novel algorithm and tool for the discovery of rare and common copy-number variants (CNVs) from next-generation sequencing (NGS) read-depth data. In GATK gCNV, sequencing biases are modeled via negative-binomial factor analysis, and copy-number states and genomic regions of low and high CNV activity are modeled using a hierarchical hidden Markov model (HHMM). We use automatic differentiation variational inference (ADVI) and variational message passing to infer continuous and discrete latent variables in a principled framework. We further use a deterministic annealing protocol to deal with the non-convexity of the variational objective function. Inference is implemented using the PyMC3 probabilistic programming language (PPL) and Theano. We demonstrate that GATK gCNV outperforms existing tools for CNV detection.
\end{abstract}

\maketitle

\renewcommand{\thefootnote}{\fnsymbol{footnote}}
\footnotetext{A slightly edited version of this extended abtract was accepted for poster presentation at \textit{PROBPROG 2018, October 04--06, 2018, Cambridge, MA, USA}.  (Last edited \today.)}

\section{Introduction}
Copy-number variation (CNV) is a class of genomic {\em structural variation} where the {\em integer copy-number state} of a large genomic region (typically $>$1000 base pairs, spanning several consecutive exons or genes) is altered with respect to a reference genome.  Inferring these copy-number states is an important problem in computational genomics, both for research applications and clinical practice~\cite{zhang2009copy}. Inferring integer CNV states from NGS read-depth data begins with aligning short reads (sequences of $\sim$100 base pairs from a sample genome) to the reference genome and counting the number of reads that align to each region.  Calling CNV events from these counts is a challenging problem due to strong systematic biases, which can arise from batch effects in sample preparation and sequencing library preparation protocols, variation in sequencing efficiency across genomic regions, and numerous other hidden processes.

Many previous methods for CNV detection from read-depth data attempt to remove systematic biases via PCA denoising \cite{fromer_discovery_2012} or regression~\cite{jiang_codex:_2015, klambauer_cn.mops:_2012}, or try to obviate the issue by pre-clustering samples and genomic regions~\cite{handsaker_large_2015, packer_clamms:_2016}.  CNVs are subsequently detected using hidden Markov models (HMM) or non-parametric change-point detection algorithms~\cite{olshen_circular_2004}. Crucially, these methods suffer from a lack of self-consistency between data normalization and event detection, which results in inadvertent removal of signal in the former and decreased sensitivity in the latter (as discussed in, e.g., ~\cite{jiang_codex:_2015}).

Here, we present GATK gCNV, a principled Bayesian approach for learning global and sample-specific biases of read-depth data from large cohorts while {\em simultaneously} inferring copy-number states. Our model combines a negative-binomial factor analysis module for learning batch effects with a hierarchical HMM (HHMM) for detecting both sample-specific CNV events and global regions of high and low CNV activity.  Self-consistency between bias modeling and CNV calling greatly improves the performance of our algorithm with respect to existing methods.

\section{Summary of Methods}\label{sec:methods}
\noindent{\em\bf Modeling read-depth ---} We seek to model the data $n_{st}$, the integer count of aligned reads for sample $s=1, 2, \ldots, S$ in genomic region $t=1, 2, \ldots, T$.  We model $n_{st}$ with a negative-binomial distribution; taking $\lambda_{st}$ as the Poisson parameter and $\alpha_{st}$ as the Gamma distribution parameter, we may write
\begin{equation}
\begin{split}
n_{st} &\sim \mathsf{NegativeBinomal}(\lambda_{st}, \alpha_{st}),\\
\lambda_{st} &=d_s \, (c_{st} \, \mu_{st}  + \varepsilon_M),\\
\log(\mu_{st}) &= m_t + \sum_{\nu=1}^{D} W_{t\nu} z_{\nu s} + \sum_{\nu=1}^{K} \overline{W}_{t\nu} \overline{z}_{\nu s},\\
\log[\alpha_{st}/(1+ \alpha_{st})] &=\Psi_s + \Psi_t,
\end{split}
\end{equation}
where $d_s \sim \mathsf{LogNormal}(\mu_d, \sigma_d)$ is the mean read-depth per copy, $\varepsilon_M$ is a small alignment error rate, $c_{st} \in \mathbb{N}^0$ is the integer copy-number matrix, and $\mu_{st} > 0$ is the multiplicative bias matrix. We model $\mu_{st}$ in the logarithmic space using a generalized linear model: a global region-specific mean bias $m_t \sim \mathsf{N}(0, \sigma_m)$, a low-rank matrix factorization term with stochastic global bias factors $W_{t\nu} \sim \mathsf{N}(0, a_\nu^{-1})$ and sample-specific loadings $z_{\nu s} \sim \mathsf{N}(0, 1)$, and a contribution from deterministic bias factors $\overline{W}_{t\nu}$ (arising from known biological features of each genomic region) and their respective loadings $\overline{z}_{\nu s} \sim \mathsf{N}(0, \sigma_z)$. Here, $a_\nu$ denotes the automatic relevance determination (ARD) coefficients which are optimized over to select the number of bias factors in a data-driven manner. Finally, we account for the unmodeled overdispersion $\log[\alpha_{st}/(1+ \alpha_{st})]$ as a sum of sample-specific $\Psi_s \sim \mathsf{Exp}(\sigma_S)$ and region-specific $\Psi_t \sim \mathsf{Exp}(\sigma_T)$ components. The graphical model for this module is shown in Fig.~\ref{fig:denoising_component}.\\

\begin{figure}
\includegraphics[width=\columnwidth*3/4]{figures/germline-cnv-caller-model/denoising_component.pdf}
\caption{The read-depth model of GATK gCNV.}
\label{fig:denoising_component}
\end{figure}

\noindent{\em\bf Modeling genomic regions and copy-numbers ---}
\begin{figure}
\includegraphics[width=\columnwidth]{figures/germline-cnv-caller-model/hhmm.pdf}
\caption{(top) Copy-number priors for silent and active region classes. (bottom) The hierarchical HMM of region class states and sample-specific copy-number states.}
\label{fig:hhmm}
\end{figure}
Certain genomic regions are biologically more prone to CNV events. We introduce a per-region two-state categorical random variable $\tau_t \in \{\mathrm{silent}, \mathrm{active}\}$ to model such global regional differences. The correlation between genomically close region classes is modeled with an HMM with exponentially decaying transition probabilities:
\begin{multline}
p(\tau_t \rightarrow \tau_{t+1}) = \exp(-\Delta_{t, t+1}/d_\tau) \, \delta(\tau_t, \tau_{t+1}) \\+ [1 - \exp(-\Delta_{t, t+1}/d_\tau)]\,p(\tau_{t+1}),
\end{multline}
where $p(\tau_{t})$ the prior region class probability, $\Delta_{t, t+1}$ is the genomic distance between the midpoints of region $t$ and $t+1$, and $d_\tau$ is the typical size of regions with similar CNV activity rates.

Sample-specific copy-numbers are modeled similarly, with one Markov chain per sample; however, priors and transition probabilities are conditioned on the region class $\tau_t$. We set a {\em baseline} copy-number state matrix for each sample $\kappa_{st}$ in a preliminary modeling step that estimates chromosome-level copy numbers. That is, $\kappa_{st}$ denotes the copy-number state in the absence of any CNV event. We use a prior copy-number probability that strongly prefers the baseline state $\kappa_{st}$ in regions where $\tau_t = \mathrm{silent}$ and we use a flat prior where $\tau_t = \mathrm{active}$. The copy-number transition matrix is an exponentially decaying process as before, but with a different decay length $d_\mathrm{CNV}$ and per-region priors. The dependency relations of the resulting hierarchical HMM are shown graphically in Fig.~\ref{fig:hhmm}.\\
% The complete model is obtained by attaching the denoising module to emission vertices of the HHMM and is shown in Fig.~\ref{fig:full_model}.

% \begin{figure}
% \includegraphics[width=\columnwidth]{figures/germline-cnv-caller-model/full_model.pdf}
% \caption{The complete GATK gCNV graphical model. The $\mathsf{D}_t$ boxes correspond to the denoising components shown in Fig.~\ref{fig:denoising_component}. The HHMM previously shown in Fig.~\ref{fig:hhmm} appears in a schematic form.}
% \label{fig:full_model}
% \end{figure}

\noindent{\em\bf Hybrid ADVI framework ---} The GATK gCNV model contains both continuous and discrete latent random variables (RVs). In order to leverage PPLs and automatic variational inference, we assume a factorized posterior of the form $p(\mathsf{C}, \mathsf{D} | n_{st}) \approx~q(\mathsf{C})\, q(\mathsf{D})$, where $\mathsf{C} = \{m_t, W_{t\mu}, \ldots\}$ and $\mathsf{D} = \{c_{st}, \tau_t\}$ denote the set of continuous and discrete latent RVs, respectively. Subsequently, the full evidence lower bound (ELBO) admits a natural partitioning $\mathsf{ELBO} = \mathbb{E}_{\mathsf{C} \sim q(\mathsf{C})}[\mathsf{ELBO}|\mathsf{C}] + \mathbb{E}_{\mathsf{D} \sim q(\mathsf{D})}[\mathsf{ELBO}|\mathsf{D}]$. Given $q(\mathsf{D})$ and a variational ansatz for $q(\mathsf{C})$, the ELBO can be increased by performing gradient descent steps on the parameters of $q(\mathsf{C})$. Likewise, given $q(\mathsf{C})$, one can gather sufficient statistics via posterior sampling to apply Bayesian updates on $q(\mathsf{D})$; see below. We refer to this scheme as {\em Hybrid ADVI}.\\

\noindent{\bf Continuous sector: {\em ADVI} ---} We implement inference for the continuous RVs using the PyMC3 PPL~\cite{salvatier_probabilistic_2016}. We assume a fully factorized Gaussian variational posterior for $q(\mathsf{C})$, which is updated using ADVI \cite{Kucukelbir:2017:ADV:3122009.3122023}. Once partial convergence is achieved, we move on to the discrete sector. It can be shown that a sufficient statistic for a Bayesian update of $q(\mathsf{D})$ is the $q(\mathsf{C})$-averaged {\em log emission probability}, $\mathbb{E}_{\mathsf{C} \sim q(\mathsf{C})}[\log p(n_{st} | c_{st}, \mathsf{C})]$, which we obtain via sampling.\\

\noindent{\bf Discrete sector: {\em variational message passing} ---} Performing an exact Bayesian update of $q(\mathsf{D})$ is feasible via message passing; however, this has an exponential complexity in the number of per-sample Markov chains ($S$). Assuming $\mathsf{S} \gg 1$, we expect the posterior $q(\tau_t)$ to become sharp, resulting in an effective decoupling of $\tau_t$ (parent chain) and $c_{st}$ (child chains). Therefore, we assume an ansatz $q(\mathsf{D}) \approx q(\tau_t) \prod_{s=1}^S q_s(c_{st})$, neglecting correlations between $\tau_t$ and $c_{st}$ {\em and} between different child chains, but retaining genomic correlations along each chain. This allows us to update $q(\tau_t)$ and $q_s(c_{st})$ using the standard forward-backward algorithm, although we must use mean-field effective prior and transition probabilities that need to be self-consistently determined.  For brevity, we omit the derivation of the iterative scheme, but note that self-consistency is achieved quickly after 2-3 rounds of forward-backward updates with relaxation.  The complexity of our variational treatment of this HHMM is $\mathcal{O}(S T C^2)$, where $C$ is the number of allowed integer copy-number states; crucially, this is linear in $S$.\\

\noindent{\em\bf Marginalized warm-up and deterministic annealing ---} The complete inference scheme involves interleaving updates of $q(\mathsf{C})$ and $q(\mathsf{D})$. In practice, we found that the non-convexity of ELBO yielded spurious local minima with poor initialization. To alleviate this issue, we utilize two techniques: (1) We initialize $q(\mathsf{C})$ and $q(\mathsf{D})$ by {\em approximately} marginalizing all discrete RVs, and obtain the first estimate of $q(\mathsf{C})$ from the marginalized model. (2) In the spirit of the deterministic annealing expectation-maximization (DA-EM) algorithm \cite{ueda_deterministic_1998}, we apply entropic regularization to both the continuous and the discrete RVs. In brief, we initially encourage high-entropy variational posteriors by replacing the standard ELBO with $\mathsf{ELBO}(\beta) = \mathbb{E}_{\mathbf{z} \sim q(\mathbf{z})}[\log p(\mathbf{x}, \mathbf{z}) - \beta q(\mathbf{z})]$, where $\beta \geq 1$ is the inverse temperature and is slowly annealed during learning. A similar recipe applies to the variational message passing, where all prior, transition, and emission probabilities are scaled by $\beta^{-1}$ in log space and renormalized. Our PyMC3 implementation of DA-ADVI is provided with GATK \cite{mckenna_genome_2010}.

\section{Benchmarking}\label{sec:bench}
\begin{figure}
\includegraphics[width=\columnwidth]{figures/germline-cnv-caller-model/benchmark.pdf}
\caption{The performance of GATK gCNV, XHMM \cite{fromer_discovery_2012}, and CODEX \cite{jiang_codex:_2015} in detecting CNV events from WES data against a matched WGS callset obtained using Genome STRiP \cite{handsaker_large_2015}.  Performance metrics are reported per WES target.  All curves are parameterized by call quality and the dashed red isoclines (right) indicate constant $F_1$ score.}
\label{fig:benchmark}
\end{figure}
Finally, we benchmark GATK gCNV using a cohort of whole-exome sequencing (WES) blood-normal samples. We use a manually validated and FDR-controlled callset obtained from matched whole-genome sequencing (WGS) samples using Genome STRiP~\cite{handsaker_large_2015} as the truth callset. To compare, we also include the CNV calls obtained using two popular CNV calling tools, XHMM \cite{fromer_discovery_2012} and CODEX \cite{jiang_codex:_2015}, in the benchmark. The results are shown in Fig.~\ref{sec:bench}. We find that GATK gCNV yields nearly $20\%$ higher sensitivity and $50\%$ higher specificity over all CNV events compared to the other methods.  Although not presented here, benchmarks that stratify by truth variant frequency also show that GATK gCNV performs favorably on common CNV events. More extensive benchmarking results demonstrate even further improvement, primarily resulting from GATK gCNV hyperparmeter optimization using additional WGS truth callsets, and will be presented elsewhere.

\bibliography{germline-cnv-caller-model}

\end{document}


\section{Work Group}

Work group of 8x8. A work group is a 8 x 8 chunk of rays and we calculate the average, max and variance for primary and secondary rays.
\input{Plots/PrimaryN4L4WorkGroupAnalysis.pgf}
\input{Plots/SecondaryN4L4WorkGroupAnalysis.pgf}


%!TEX root = ../notes.tex
\section{January 28, 2022}
\subsection{Greatest Common Divisors}

\subsection{Euclidean Algorithm}

\subsection{Linear Combinations}


\chapter{Method}
\label{chap:method}

In this chapter, we present the methodology for how accurate models for predicting age and sex were developed, and how they were analysed using saliency mapping techniques in order to gain insight into which functional brain networks in the data are related to age and sex. The chapter can be summarised as follows: firstly, we present how the \acrshort{fmri} data was preprocessed into graphs suitable for graph neural networks. Secondly, the models developed for graph and node prediction are presented, followed by the methods used for analysing and creating saliency maps from the models. See \cref{app:model_training} for more details on the exact model architectures used.

\section{From RS-fMRI scans to graphs}\label{sec:fmri_to_graphs}

\begin{figure}[!htbp]
    \centering
    \includegraphics[width=0.6\textwidth]{chapters/images_methods/fmri_network_ukbiobank.png}
    \caption{A schematic visualisation of a network of functional brain networks. Blue and orange edges represent positive and negative correlation between brain networks, respectively.  The individual functional brain networks are represented as human brains with \acrshort{fmri} activations (orange), along the circumference of the circle. See \cref{tab:Networks} for the names of the functional brain networks. Image adapted from example at UK Biobank Brain Imaging Online Resources \cite{ukbiobank_brain_imaging}.}
    \label{fig:fmri_network}
\end{figure}

The graphs of functional brain networks used in this thesis are derived from \acrshort{fmri}. In short, functional \acrshort{mri} (\acrshort{fmri}) is a technique for measuring the neuronal activity in the human brain \cite{sporns}. In a measurement, the activations of various parts of the subject's brain are measured as time series. The correlations between these time series can then be calculated and formed into a network. Different sub-networks in the obtained \acrshort{fmri} brain network can often be associated with different functionalities of the brain. One example of such a functional brain network is the \acrfull{dmn}, which handles memory processing and mind wandering  \cite{alves_dmn}. The regions that make up the sub-networks do not necessarily have to be physically close together nor directly anatomically connected.

\begin{table}[!htbp]
    \centering
    \caption{A list of which node corresponds to which functional brain network in the data from the UK Biobank. See \cref{fig:fmri_network} for a representation of each functional brain network. Each node will in the remainder of this thesis be referred to by the abbreviation for its functional brain network.}
    \begin{tabular}{||c|c|c||}
        \hline
        Number & Network & Abbreviation  \\ \hline\hline
        1 & \acrlong{cb1} & \acrshort{cb1} \\ \hline
        2 & \acrlong{vl} & \acrshort{vl}  \\ \hline
        3 & \acrlong{ssn} & \acrshort{ssn} \\ \hline
        4 & \acrlong{vm} &  \acrshort{vm}\\ \hline
        5 & \acrlong{dar} & \acrshort{dar} \\ \hline
        6 & \acrlong{dal} &  \acrshort{dal} \\ \hline
        7 & \acrlong{fp} &  \acrshort{fp}\\ \hline
        8 & \acrlong{vv1m} & \acrshort{vv1m} \\ \hline
        9 & \acrlong{dmn} &  \acrshort{dmn} \\ \hline
        10 & \acrlong{pss} & \acrshort{pss} \\ \hline
        11 & \acrlong{pmn} & \acrshort{pmn} \\ \hline
        12 & \acrlong{smm} & \acrshort{smm}  \\ \hline
        13 & \acrlong{va} &  \acrshort{va}\\ \hline
        14 & \acrlong{sn} &  \acrshort{sn}\\ \hline
        15 & \acrlong{cb2} & \acrshort{cb2} \\ \hline
        16 & \acrlong{pl} & \acrshort{pl} \\ \hline
        17 & \acrlong{ts} & \acrshort{ts} \\ \hline
        18 & \acrlong{bg} &  \acrshort{bg} \\ \hline
        19 & \acrlong{vv1l} & \acrshort{vv1l} \\ \hline
        20 & \acrlong{pmc} & \acrshort{pmc} \\ \hline
        21 & \acrlong{tm} & \acrshort{tm} \\ \hline
    \end{tabular}
    \label{tab:Networks}
\end{table}

This thesis utilises \acrshort{fmri} data obtained form the UK Biobank for roughly 35000 subjects, of both sexes, varying between 45 and 80 years of age \cite{ukbiobank}. The \acrshort{fmri} data had been preprocessed and consisted of correlations between 21 different functional brain networks. The correlation between these 21 networks thus constituted a network of networks, and is interpreted as a graph where each node corresponds to one functional network and each edge the correlation between networks. A visualisation of such a brain graph is given in \cref{fig:fmri_network}, in which the various nodes and the functional brain networks they represent form the outer circle, with the coloured lines connecting the nodes being the edges in the graph. The lines are coloured according to the edge weights, with blue and orange lines having positive and negative weights, respectively. For a list of what functional brain network each node corresponds to, and its abbreviation, see \cref{tab:Networks}.

\subsection{Treating negative edge weights}
A brain graph with both positive and negative weights may be problematic. Specifically, when normalising the adjacency matrices according to Equation \eqref{eq:renormalization_trick}, the risk of dividing by zero becomes imminent. There are several ways to handle this problem. One alternative is to only study all positive or all negative connections; another might be to take the absolute value of all connections. We, however, decided to split the negative and positive connections into two separate graphs and thus form a multiplex graph. From a practical point of view this was implemented by creating a block diagonal adjacency matrix with all positive connections in the upper block and all negative connections in the lower block, see \cref{fig:block_diagonal_adjacency_matrix}. The connections in the positive block that were negative in the original adjacency matrix $A$ were replaced with zeros, and vice versa in the negative block.

\begin{figure}[!htbp]
    \centering
    \includegraphics[width=0.9\textwidth]{chapters/images_methods/split.png}
    \caption{An example of how negative values in an adjacency matrix $A$ were handled. The negative values (orange) were extracted into the lower diagonal block of $A_{split}$, and replaced with zeros in the upper, positive block (blue).}
    \label{fig:block_diagonal_adjacency_matrix}
\end{figure}

\section{Models for graph prediction}

Having described the preprocessing of the brain graphs, we now turn to the models used to perform graph prediction for individual subjects. Two models will be presented, a baseline regression model and a \acrshort{gcn} model, for the tasks of predicting subject age and sex.

\subsection{Baseline}
To validate how well the different \acrshort{gcn}-models performed, a baseline model had to be introduced for comparison. In this study, the baseline model consisted of a regression model where the connections between all of the nodes were regarded as separate input features. The adjacency matrix for an individual subject could then be viewed as a high dimensional data point, and the model thus aimed to fit a hyper plane to either separate the classes or predict a continuous variable. For a schematic view of the model, see \cref{fig:Graph_class_baseline}. Note that the output layer has either two softmax-activated neurons in the case of sex classification, or one neuron without activation in the case of age regression.

\begin{figure}[!htbp]
    \centering
    \includegraphics[width=0.7\textwidth]{chapters/images_methods/ffnn_v2.png}
    \caption{The baseline regression model. The adjacency matrix for a brain graph for a single subject forms the input, with a continuous value or class prediction as output in the case of age or sex prediction.}
    \label{fig:Graph_class_baseline}
\end{figure}

From the definition of a graph in \cref{sec:general_graph_theory}, it follows that graphs are completely node order invariant, and several different ways of listing the connections results in the same graph. This might generally impose a problem since a regression model depends on its input being ordered. In this thesis, a regression model was, however, possible as a baseline model, since the brain graphs from the UK Biobank followed a consistent node ordering, and each input feature of the model was thus always the same connection. Furthermore, the regression model was feasible due to the relatively small size of the brain graphs (21 nodes) compared to many other graphs (such as citation networks consisting of thousands of nodes). For larger graphs, models with one learnable parameter per connection would result in huge models. In that case, models that do not scale with the number of connections must be used, such as \acrshort{gcn}-based models.

% \begin{center}
%     \resizebox {0.9\linewidth} {!} {
%         \input{chapters/images_methods/baseline_ffnn.tikz}
%     }
% \end{center}

\subsection{GCN}

The motivation behind using \acrshort{gcn}-based models was that they hopefully would be able to extract information from the topological structure of the graphs, rather than simply studying the individual connections as, for instance, Baseline. A \acrshort{gcn}-based model, referred to simply as GCN, was therefore developed. An illustration of the GCN model can be seen in \cref{fig:gcn_base}. GCN consisted of three consecutive graph convolutional layers, with propagation rule as defined in equation \eqref{eq:propagation_rule}, followed by a fully connected output layer. The graph convolutional layers had a \acrfull{relu} activation function and ten output features. This will be the case for all graph convolutional layers mentioned in the remainder of this thesis, unless otherwise specified. The input to the fully connected output layer consisted of the activations for all three graph convolutional layers, i.e., the activation for all feature maps in all layers, concatenated together. As described in \cref{sec:gcn} the activation of layer $i$ contains information about the $i$:th-order neighbourhood of each node. The inclusion of the activations after each layer in the classifier thus aimed to utilise information of how each node was embedded in a successively larger neighbourhood, which could be beneficial for the predictions.

The inputs to a graph convolutional model generally consists of an adjacency matrix $A$ and a node feature matrix $X$. However, the brain graphs in this thesis did not contain any information beyond connections that could be associated with specific nodes. GCN thus used a \textit{featureless} approach, in which the feature matrix was taken to be the identity matrix, $X=I$ \cite{kipf_vae}.

\begin{figure}[!htbp]
    \centering
    \includegraphics[width=\textwidth]{chapters/images_methods/base_v2.png}
    \caption{The GCN model, which takes a single brain graph as input and outputs a predicted age or sex. The input graph is processed through three graph convolutional layers with ReLU activation, after which the activations for each layer are concatenated together and fed into a fully connected layer.}
    \label{fig:gcn_base}
\end{figure}

% \begin{center}
%     \resizebox {0.9\linewidth} {!} {
%         \input{chapters/images_methods/gcn.tikz}
%     }
% \end{center}

\section{Models for node prediction}

With the models performing graph predictions introduced, two models used to perform node prediction on a population graph will now be presented. First, however, the procedure of forming population graphs will be described, followed by a batching method that enables models to be trained.

\subsection{Forming population graphs}

As discussed in \cref{sec:similarity_measure}, the design of the similarity measure is an important decision and should be done with the given application in mind. For our specific application, it is desirable that subjects that have similar \acrshort{fmri} data are connected with edges that have large weights, with the idea that the models will be able to draw upon this information of similarity to yield a better inference. A general construction that fulfils this requirement is the use of a distance metric inverted by a kernel, as described in Equation \eqref{eq:similarity_measure}. With this construction, and given two subjects and their adjacency matrices $A_1$ and $A_2$, we defined the similarity measure $\sigma\left(A_1, A_2, l\right)$ as
\begin{equation}
    \sigma\left(A_1, A_2, l\right) = \exp{\left(- \frac{||A_1 - A_2||_F^2}{l||A_1||_F ||A_2||_F} \right)}\biggr\rvert_{l=0.5},
    \label{eq:modified_similarity_measure}
\end{equation}
where $||A_1 - A_2 ||_F$ is the matrix Frobenius norm of the difference between $A_1$ and $A_2$. The Frobenius norm is defined as $||A||_F = \left( \sum_i \sum_j |A_{ij}|^2 \right)^{1/2}$. The norm of the difference was weighted with a hyperparameter $l=0.5$ and the norms of $A_1$ and $A_2$, and then fed into a Gaussian kernel. The Gaussian kernel ensured that larger differences between $A_1$ and $A_2$ yielded smaller similarity scores $\sigma\left(A_1, A_2, l\right)$, and also that  $\sigma\left(A_1, A_2, l\right) \in \left[0, 1\right]$. As desired, subjects that had similar fMRI data, and thus a smaller difference between their adjacency matrices, obtained a larger similarity score and vice versa.

% Note that the similarity measure in equation \eqref{eq:modified_similarity_measure} only utilises the \acrshort{fmri} data for each subject. One could imagine a similarity measure that uses other types of data that is related to the tasks of predicting age/sex, such as eventual brain-health related diagnosis \cite{stankeviciute}. The choice of only using \acrshort{fmri} data was motivated by two reasons; partly because using other data sources requires extensive domain knowledge, and partly because we are specifically interested in the predictive power of \acrshort{fmri} data, without introducing other confounding variables.

\subsection{Batches of population graphs}
The adjacency matrix for a population graph quickly becomes very large, since the number of edges in the graph grows quadratically with the number of subjects. For example, a population graph with 30 000 subjects requires approximately 7 GB of memory to store. This is a problem, since predicting and training using such a large matrix is time consuming, and the memory consumption can also be problematic. To resolve this problem, and thus in a feasible way be able to train models on population graphs including all the available data, a batching method was developed.

\begin{figure}[!htbp]
    \centering
    \includegraphics[width=0.9\textwidth]{chapters/images_methods/batching.png}
    \caption{An example of the batching approach for splitting a large population graph $A_{population}$ into two smaller population graphs, each corresponding to a batch $A_{batch}$. In the first epoch, only the connections between the first three (blue) and last three (orange) subjects are included in the batched population graphs. By permuting which subjects are included in each of the smaller population graphs for the second epoch and onwards, all connections not included in the first epoch (gray) will eventually be sampled.}
    \label{fig:batches}
\end{figure}

The batching method was based on dividing the population graph into several smaller population graphs. This was done by splitting the data set into several smaller data sets of 100 subjects each, and constructing one population graph for each smaller data set. In practice, this was done by extracting all connections between the 100 subjects from the larger population graph. For an illustration of how this was done, see \cref{fig:batches}. With this batching approach, dividing a population graph of 30000 subjects into, for instance, 300 smaller graphs with 100 nodes each, would require 24 MB of memory, as compared to 7 GB before batching. This is a difference of roughly two orders of magnitude.

As seen in \cref{fig:batches}, many of the connections between subjects are not utilised if the population graph is divided into several smaller graphs. To solve this, the way the subjects were divided into smaller graphs was changed between epochs, as seen in \cref{fig:batches}. By always changing which people were combined in the graphs, all connections were eventually used after enough epochs. The advantage of always changing the subjects that were included in the graphs was that the models could not overfit against a specific graph structure. Thus, the models were forced to learn very general patterns to make predictions for all subjects in the graphs. However, this generalisation might also be a disadvantage, since overfitting on the graph structure may yield higher validation performance, as long as the graph structure remains fixed.

\subsection{Poptoy}

As a first model for predictions on a population graph, the Poptoy model was introduced. The input to Poptoy consisted of a population graph, which was then propagated through five graph convolutional layers. The architecture of Poptoy was similar to that of GCN, but the output layers differed. The output layer of Poptoy was a fully connected layer, and took the activations of all layers and all features for \underline{a specific} node as input. GCN also had a fully connected output layer, but it took the activations for all layers and all features for \underline{all} nodes as input. Since the fully connected layer for Poptoy outputs a prediction for a single node, it could be reused for all nodes and thus the number of weights was kept low as the number of nodes in the population graph grew. An illustration of the Poptoy model can be seen in \cref{fig:poptoy}.

\begin{figure}[!htbp]
    \centering
    \includegraphics[width=\textwidth]{chapters/images_methods/poptoy_v2.png}
    \caption{The Poptoy model. The input consists of a population graph, which is passed through five graph convolutional layers. The five activations are concatenated together and fed into a fully connected output layer, which outputs a predicted age or sex on a subject-level.}
    \label{fig:poptoy}
\end{figure}

The reason the Poptoy model consisted of five graph convolutional layers instead of three, which GCN had, was that the population graphs became much larger than the individual brain graphs. The individual brain graphs consisted of 21 nodes, which were all more or less connected, and since each graph convolutional layer takes one higher order of neighbours into account, all nodes were to some extent included in the first order neighbourhood. Thus, the need for more graph convolutional layers quickly diminished. For population graphs, the need for considering neighbours farther away in the graph might be much larger, and hence Poptoy utilised more graph convolutional layers than GCN. Five layers were specifically chosen, since adding more layers did not increase performance.

Furthermore, since all subjects in the data set were incorporated in the population graph, the split into validation and training sets had to be handled. To solve this, a set of subjects in the population graph were defined to be the training set, and the rest to be the validation set. The model was then constructed to either do predictions for only the nodes in the training set or the validation set. Then, the training could be performed while only doing predictions on the training set and vice versa when evaluating.

\subsection{Popencoder}
As a means to incorporate more information about each subject in the population graph, a model referred to as Popencoder was designed. Popencoder is identical to Poptoy in the sense that the population graph was propagated through five graph convolutional layers, after which a prediction for each node was made using a fully connected layer. The difference is that in Popencoder, features for each node were introduced. The features were based on the adjacency matrices of each individual subject, but to compress the dimensionality of the feature space, these matrices were encoded into a lower dimensional space. The encoder consisted of two graph convolutional layers, followed by a fully connected layer with either two softmax activated neurons or one linearly activated neuron, in the case of sex or age prediction, respectively. For an illustration of Popencoder, see \cref{fig:popencoder}.

A heuristic explanation of why the features introduced to the population graph would help, is that the encoder can make an initial prediction of the age or sex for each individual subject. Then, by propagating this information through the population graph, the information of similarities to other subjects in the data set might improve on the predictions. In that case, one could argue that the initial prediction might be done as a preprocessing step, for example via a prediction with another model. The encoding is, however, viewed as a trainable part of the model to allow for more abstract and advantageous embeddings to be learnt during model training.

\begin{figure}[!htbp]
    \centering
    \includegraphics[width=0.9\textwidth]{chapters/images_methods/popencoder_v2.png}
    \caption{The Popencoder model, which takes two inputs: a population graph $A$, and the brain graphs for all subjects in the population graph, $X$. The brain graphs are passed through an encoder, consisting of two graph convolutional layers followed by a fully connected layer. The encoded brain graphs and the population graph are then fed into a classifier, which consists of five graph convolutional layers followed by a concatenation and a fully connected layer. The output is a predicted age or sex on a subject-level.}
    \label{fig:popencoder}
\end{figure}

\section{Model explainability through saliency mapping}
To determine what functional networks in the brain are related to sex and age, model analysis, in the form of analysing what the models have learned, is an essential part of the work. Specifically, using saliency mapping techniques to analyse which nodes in the brain graph are important for making predictions will, by extension, give information on what functional networks are important, due to each node representing a functional network.

Two methods for model analysis were used: a naive approach based on node removal, and a more sophisticated method for node masking based on the Zorro algorithm described in \cref{sec:zorro}. Note that these are not methods that open up the black-box of neural networks per say (by e.g. parameter analysis). These methods analyse the networks from an outer perspective, which is beneficial as it poses no assumption on the model architecture, only in- and output data. Thus, the same analysis may be performed for different models, making it beneficial for validation and result comparison.

\subsection{Naive node removal}
A simple method to obtain a saliency map of which nodes are important for predictions, is a node removal method. This particular method is self-composed, and consisted of simply removing a specific node for every subject in the data set, after which a new model was retrained. By comparing the predictive performance of the retrained model with a reference model trained on data with no nodes removed, an indication of the importance of that node could be obtained. This is based on the assumption that a large loss in performance means vital information for the predictions had been removed, which was interpreted to be indicative of the importance of that node. The method was then repeated for all nodes in order to obtain a measure of importance for each node.

\subsection{Zorro}

The Zorro algorithm, described in \cref{sec:zorro}, was developed for saliency mapping of models with graph features as input. It thus needed to be modified to be applied to the models presented in this thesis. The need for modification arose since Baseline and GCN are completely featureless approaches, that only takes an adjacency matrix $A$ as input. Therefore, it was not possible to introduce noise to the feature matrix, as done in the original method. Instead, it had to be done on $A$. The reintroduction of nodes in $A$, in the following referred to as unmasking, could have been done in two ways; either on a connection level where entries in $A$ are unmasked, or on a nodal level where whole rows and columns in $A$ are unmasked. We were primarily interested in which nodes were important, so the latter was used. Unmasking whole nodes instead of connections also yielded computational benefits, since the number of nodes was less than the number of connections. As whole nodes were unmasked, the explanation in the modified Zorro method only contained a set of nodes, $\mathcal{S} = \{V\}$. The masked adjacency matrix was then given by
\begin{equation}
    B_S = A \odot S + Z \odot (1- S), \quad Z \sim \mathcal{N},
\end{equation}
where $S$ is the masking matrix for the explanation $\mathcal{S}$, $1$ is a matrix of ones and $\odot$ represents element-wise multiplication. The noise $Z$ was drawn from a Gaussian distribution with mean and standard deviation given by the entries of $A$ over all subjects in the data set. The model prediction on the masked adjacency matrix $\Phi(B_S)$ was considered correct for sex prediction if $\Phi(A) = \Phi(B_S)$, and for age prediction if
%where $S$ is the masking matrix for the explanation $\mathcal{S}$, and the model predictions are $\Phi(A)$ and $\Phi(B_S)$. For age prediction, $\Phi(B_S)$ was deemed correct if
\begin{equation}
    \left|\Phi(A) - \Phi(B_S)\right| < t,
\end{equation}
for some tolerance $t$, since age is a continuous variable.  The fidelity was calculated in the same way as in the original algorithm, and an explanation $\mathcal{S}$ for an individual subject was still accepted if the fidelity of $\mathcal{S}$ was higher than $\tau$.

Lastly, since the algorithm yielded which nodes were important for the prediction of an individual subject, the procedure was repeated for several subjects to get a sense of which nodes were generally important. To evaluate the importance of each node, an importance score $\mathcal{I}$ was introduced as the number of explanations $\mathcal{S}$ a node was included in, divided by the total number of subjects. An importance score of $\mathcal{I}=1$ indicates that the node is considered important for the prediction of all subjects, and a score of $\mathcal{I}=0$ for none.


%
% API Documentation for Peach - Computational Intelligence for Python
% Module peach.optm.stochastic
%
% Generated by epydoc 3.0beta1
% [Mon Dec 21 08:51:38 2009]
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                          Module Description                           %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    \index{peach \textit{(package)}!peach.optm \textit{(package)}!peach.optm.stochastic \textit{(module)}|(}
\section{Module peach.optm.stochastic}

    \label{peach:optm:stochastic}

General methods of stochastic optimization.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                               Variables                               %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  \subsection{Variables}

\begin{longtable}{|p{.30\textwidth}|p{.62\textwidth}|l}
\cline{1-2}
\cline{1-2} \centering \textbf{Name} & \centering \textbf{Description}& \\
\cline{1-2}
\endhead\cline{1-2}\multicolumn{3}{r}{\small\textit{continued on next page}}\\\endfoot\cline{1-2}
\endlastfoot\raggedright \_\-\_\-d\-o\-c\-\_\-\_\- & \raggedright \textbf{Value:}
{\tt \texttt{...}}&\\
\cline{1-2}
\end{longtable}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                           Class Description                           %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    \index{peach \textit{(package)}!peach.optm \textit{(package)}!peach.optm.stochastic \textit{(module)}!peach.optm.stochastic.CrossEntropy \textit{(class)}|(}
\subsection{Class CrossEntropy}

    \label{peach:optm:stochastic:CrossEntropy}
\begin{tabular}{cccccccc}
% Line for object, linespec=[False, False]
\multicolumn{2}{r}{\settowidth{\BCL}{object}\multirow{2}{\BCL}{object}}
&&
&&
  \\\cline{3-3}
  &&\multicolumn{1}{c|}{}
&&
&&
  \\
% Line for peach.optm.optm.Optimizer, linespec=[False]
\multicolumn{4}{r}{\settowidth{\BCL}{peach.optm.optm.Optimizer}\multirow{2}{\BCL}{peach.optm.optm.Optimizer}}
&&
  \\\cline{5-5}
  &&&&\multicolumn{1}{c|}{}
&&
  \\
&&&&\multicolumn{2}{l}{\textbf{peach.optm.stochastic.CrossEntropy}}
\end{tabular}

Multidimensional search based on cross-entropy technique.

In cross-entropy, a set of N possible solutions is randomly generated at
each interaction. To converge the solutions, the best M solutions are
selected and its statistics are calculated. A new set of solutions are
randomly generated from these statistics.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                Methods                                %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  \subsubsection{Methods}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_init\_\_}(\textit{self}, \textit{f}, \textit{M}=\texttt{30}, \textit{N}=\texttt{60}, \textit{emax}=\texttt{1e-08}, \textit{imax}=\texttt{1000})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

Initializes the optimizer.

To create an optimizer of this type, instantiate the class with the
parameters given below:
    \vspace{1ex}

      \textbf{Parameters}
      \begin{quote}
        \begin{Ventry}{xxxx}

          \item[f]

A multivariable function to be optimized. The function should have
only one parameter, a multidimensional line-vector, and return the
function value, a scalar.
          \item[M]

Size of the solution set used to calculate the statistics to
generate the next set of solutions
          \item[N]

Total size of the solution set.
          \item[emax]

Maximum allowed error. The algorithm stops as soon as the error is
below this level. The error is absolute.
          \item[imax]

Maximum number of iterations, the algorithm stops as soon this
number of iterations are executed, no matter what the error is at
the moment.
        \end{Ventry}

      \end{quote}

    \vspace{1ex}

      Overrides: peach.optm.optm.Optimizer.\_\_init\_\_

    \end{boxedminipage}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{step}(\textit{self})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

One step of the search (\emph{NOT IMPLEMENTED YET})

In this method, the solution set is searched for the M best solutions.
Mean and variance of these solutions is calculated, and these values are
used to randomly generate, from a gaussian distribution, a set of N new
solutions.
    \vspace{1ex}

      Overrides: peach.optm.optm.Optimizer.step

    \end{boxedminipage}

    \label{peach:optm:optm:Optimizer:__call__}
    \index{peach \textit{(package)}!peach.optm \textit{(package)}!peach.optm.optm \textit{(module)}!peach.optm.optm.Optimizer \textit{(class)}!peach.optm.optm.Optimizer.\_\_call\_\_ \textit{(method)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_call\_\_}(\textit{self}, \textit{x})

    \end{boxedminipage}

    \label{object:__delattr__}
    \index{object.\_\_delattr\_\_ \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_delattr\_\_}(\textit{...})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

x.{\_}{\_}delattr{\_}{\_}('name') {\textless}=={\textgreater} del x.name
    \vspace{1ex}

    \end{boxedminipage}

    \label{object:__getattribute__}
    \index{object.\_\_getattribute\_\_ \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_getattribute\_\_}(\textit{...})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

x.{\_}{\_}getattribute{\_}{\_}('name') {\textless}=={\textgreater} x.name
    \vspace{1ex}

    \end{boxedminipage}

    \label{object:__hash__}
    \index{object.\_\_hash\_\_ \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_hash\_\_}(\textit{x})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

hash(x)
    \vspace{1ex}

    \end{boxedminipage}

    \label{object:__new__}
    \index{object.\_\_new\_\_ \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_new\_\_}(\textit{T}, \textit{S}, \textit{...})

      \textbf{Return Value}
      \begin{quote}
\begin{alltt}
a new object with type S, a subtype of T
\end{alltt}

      \end{quote}

    \vspace{1ex}

    \end{boxedminipage}

    \label{object:__reduce__}
    \index{object.\_\_reduce\_\_ \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_reduce\_\_}(\textit{...})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

helper for pickle
    \vspace{1ex}

    \end{boxedminipage}

    \label{object:__reduce_ex__}
    \index{object.\_\_reduce\_ex\_\_ \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_reduce\_ex\_\_}(\textit{...})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

helper for pickle
    \vspace{1ex}

    \end{boxedminipage}

    \label{object:__repr__}
    \index{object.\_\_repr\_\_ \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_repr\_\_}(\textit{x})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

repr(x)
    \vspace{1ex}

    \end{boxedminipage}

    \label{object:__setattr__}
    \index{object.\_\_setattr\_\_ \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_setattr\_\_}(\textit{...})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

x.{\_}{\_}setattr{\_}{\_}('name', value) {\textless}=={\textgreater} x.name = value
    \vspace{1ex}

    \end{boxedminipage}

    \label{object:__str__}
    \index{object.\_\_str\_\_ \textit{(function)}}

    \vspace{0.5ex}

    \begin{boxedminipage}{\textwidth}

    \raggedright \textbf{\_\_str\_\_}(\textit{x})

    \vspace{-1.5ex}

    \rule{\textwidth}{0.5\fboxrule}

str(x)
    \vspace{1ex}

    \end{boxedminipage}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                              Properties                               %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  \subsubsection{Properties}

\begin{longtable}{|p{.30\textwidth}|p{.62\textwidth}|l}
\cline{1-2}
\cline{1-2} \centering \textbf{Name} & \centering \textbf{Description}& \\
\cline{1-2}
\endhead\cline{1-2}\multicolumn{3}{r}{\small\textit{continued on next page}}\\\endfoot\cline{1-2}
\endlastfoot\raggedright \_\-\_\-c\-l\-a\-s\-s\-\_\-\_\- & \raggedright \textbf{Value:}
{\tt {\textless}attribute '\_\_class\_\_' of 'object' objects{\textgreater}}&\\
\cline{1-2}
\end{longtable}

    \index{peach \textit{(package)}!peach.optm \textit{(package)}!peach.optm.stochastic \textit{(module)}!peach.optm.stochastic.CrossEntropy \textit{(class)}|)}
    \index{peach \textit{(package)}!peach.optm \textit{(package)}!peach.optm.stochastic \textit{(module)}|)}


\documentclass[main.tex]{subfiles}
\begin{document}

\section*{Tue Oct 15 2019}

\subsubsection{\(\epsilon \) mechanism}

We now concern ourselves with the energy generation term of equation \eqref{eq:kappa-driving-mechanisms}.

In general the effective energy generation per unit mass is given by
%
\begin{align}
\epsilon _{\text{eff}} = \epsilon _{\text{nuc}} - \epsilon _{\nu }
\,,
\end{align}
%
but we assume that the neutrino loss of energy contribution in the perturbation is negligible, so when we perturb we will have \(\delta \epsilon _{\text{eff}} = \delta \epsilon _{\text{nuc}}\).
We also have the approximate relation
%
\begin{align}
\frac{ \delta T}{T} = (\Gamma_3 -1 ) \frac{ \delta \rho }{\rho }
\,,
\end{align}
%
which implies, since \(\Gamma_3 >1\), that the temperature and density relative perturbations are concordant in sign.
\todo[inline]{Why can we use this relation? Does it have something to do with working ``at first order in \(\dv*{Q}{t}\)''? I'd like to make this more formal but it does not seem to work.
}

Now, the line of reasoning goes like this: the nuclear energy generation perturbation is concordant in sign with the temperature perturbation. So, the term
%
\begin{align}
\qty(\frac{ \delta T}{T} )_{\text{sp}} \delta \epsilon _{\text{eff}} \geq 0
\,,
\end{align}
%
which means that the contribution to \(\kappa \) will be negative (because of the minus sign in equation \eqref{eq:kappa-driving-mechanisms}): so, since the dependence of the radial perturbation looks like \(\zeta \propto \exp(-\kappa t)\), the perturbation is \emph{amplified}: this is a driving layer.

\todo[inline]{The reasoning in slide 6.08 seems way too contorted: plus, it mentions the layers ``absorbing energy'', but we cannot actually talk about that without considering the luminosity gradient perturbation\dots}

Are the fluctuations in \(\epsilon _{\text{nuc}}\) actually enough to power pulsations? \emph{No}.
An illustrative example is given by RR-Lyrae stars. There, we can look at the relative magnitude of the realative temperature variation, \(\delta T / T\), at various layers in the star: it is of the order \num{e-1} or even more at the surface, but quickly drops as we move towards the center; in the Hydrogen burning shell and in the Helium burning core it is of the order \(\num{e-9} \divisionsymbol \num{e-7}\), which implies that the actual temperature fluctuations are of the order \(\delta T \sim \SI{1}{K}\): way too little to amplify pulsations.

\subsubsection{\(\kappa \)-\(\gamma \) mechanism}

Today we look at the \(\kappa \)-\(\gamma \)-mechanism, which is about the term
%
\begin{equation}
\int_M \qty(\frac{\delta T}{T})_{\text{sp}} \pdv{\delta L}{m} \dd{m}
\,,
\end{equation}
%
and we will have driving layers (\(\kappa <0\)) in the regions in which the two terms multiplied in the integrand are discordant.

The mean Rosseland opacity is approximated by a law in the form:
%
\begin{equation}
  \kappa _R \approx \overline{\kappa }_R \rho^{n} T^{-s}
\,,
\end{equation}
%
with \(n \approx 1\), \(s \approx 7/2\) in the case of free-free absorption (that is, inverse \& direct bremsstrahlung) in a non-degenerate, totally ionized gas.
This seems to be a good approximation for \(4 < \log_{10}T < 8\).

This allows us to relate the perturbations in \(\kappa_{R} \) to those in \(T\):
%
\begin{subequations}
\begin{align}
\frac{ \delta \kappa_R}{ \kappa_R} &\approx
n \frac{ \delta \rho }{\rho } - s \frac{ \delta T}{T}  \\
&\approx \qty(n - s (\Gamma_3 -1)) \frac{ \delta \rho }{\rho }
\,,
\end{align}
\end{subequations}
%
and if we substitute in \(\Gamma_3 \approx 5/3\) with the other terms we get
%
\begin{align}
\frac{ \delta \kappa_R}{\kappa _R} \approx - \frac{4}{3} \frac{ \delta \rho }{\rho }
\,,
\end{align}
%
which means that the relative mean opacity perturbation is \emph{discordant} in sign with the pressure one, and thus with the temperature one.

So, up to a positive constant, the integrand looks like
%
\begin{align}
-\frac{ \delta \kappa _R}{\kappa _R} \pdv{ \delta L}{m}
\,,
\end{align}
%
and we want it to be \(<0\).
However, if under the perturbation the layer is \emph{more} opaque than usual (\(\delta \kappa _R > 0\)) then it will let through \emph{less} light than usual, so we will have \(\delta (\pdv*{L}{m}) < 0\).
This can be rendered more formal by considering the luminosity equation \eqref{eq:luminosity-equation}: the dependence of \(L\) on \(\kappa_R \) is inverse.

So, the global contribution to \(\kappa \) will be positive, therefore our layer will be \emph{damping}.

\paragraph{\(\kappa \)-mechanism}

However, layers for which the contribution of this term is negative are present in certain regions: where there is ionization, we can have regions for which the gradient \(\pdv*{\kappa_R}{T}\) reverses: it is usually negative, but it can become locally positive.
This provides an additional channel for energy stocking: it is like a \emph{dam} for energy.
The ionization energy is then released through mechanical work.
This is called the \(\kappa \)-mechanism.

\paragraph{\(\gamma \)-mechanism}

The \(\gamma \)-mechanism, on the other hand, involves a decrease of \(\Gamma_3\) which brings it near to 1: recall,
%
\begin{align}
\Gamma_3 -1 = \eval{\pdv{\log T}{\log \rho }}_{\text{adiabatic}}
\,,
\end{align}
%
so if it is small that means that the change in temperature, i.e.\ internal energy, associated with a compression (\(\delta \rho >0\)) is small: this can happen in the outer layers of the star, which have partial ionization regions. The work of the compression is partially absorbed in order to ionize the atoms in these layers: if \(\Gamma_3 \) is close enough to 1, we can have
%
\begin{align}
n - s (\Gamma_3 -1) >0
\,
\end{align}
%
even with \(s > 0\).

\paragraph{\(\kappa \)-\(\gamma \)-mechanism}

We look at the linearized equations of continuity and radiative transfer for an expression for the gradient of \(\delta L\), neglecting the temperature perturbation gradient term for simplicity: then, starting from equation \eqref{eq:linearized-RT} we have
%
\begin{align}
\frac{ \delta L}{L} =
4 \zeta
- n \frac{ \delta \rho }{\rho }
+ (s+4) \frac{ \delta T}{T}
\,,
\end{align}
%
while from equation \eqref{eq:linearized-cont} we get
%
\begin{align}
\frac{ \delta \rho }{\rho } = - 3 \zeta
\,.
\end{align}

Combining these, and using \(\delta T / T = (\Gamma_3 -1 ) \delta \rho / \rho \) we find:
%
\begin{align}
\frac{ \delta L}{L }= \qty(-\frac{4/3 +n}{\Gamma_3 -1} + s+4) \frac{ \delta T}{T}
\,,
\end{align}
%
and now we make the following consideration: on average, in the outer layers of the stars we have no nuclear energy generation (\(\epsilon _{\text{nuc}} \approx 0\)) and the heat variation is negligible (\(\dv*{Q}{t}\approx 0 \)).\todo{Why?} So, we have \(\pdv*{L}{m} \approx 0\) by the energy conservation equation \eqref{eq:energy-conservation}: using this, we can freely bring \(L\) outside of derivatives with respect to \(m\), in order to write
%
\begin{align}
\pdv{ \delta L}{m} = L \pdv{}{m} \qty[\qty(s+4- \frac{4/3 +n}{\Gamma_3 -1}) \frac{ \delta T}{T}]
\,.
\end{align}

Outside the regions of partial ionization, typical values for the parameters are \(\Gamma_3 \sim 1.6\), \(s \sim 7/2\), \(n \sim 1\). Therefore, we get
%
\begin{align}
\pdv{ \delta L}{m} \approx \num{3.61} \times L \pdv{}{m} \qty(\frac{ \delta T}{T})
\,,
\end{align}
%
\todo[inline]{Here a derivative disappears in the slides: we go from \(\pdv*{}{m}\qty( \delta T / T)\) to \(\delta T / T\).}
which means that
%
\begin{align}
\pdv{ \delta L}{m} \sim 3.6 L \frac{ \delta T}{T}
\,,
\end{align}
%
so they are concordant in sign, so the layer is a damping layer.

In partial ionization regions, instead, \(\kappa _R \propto \rho^{n}T^{-s}\) with negative \(s\): so it is much easier for the terms to be discordant. Keeping \(n =1\) and \(\Gamma_3 = 1.6  \), we need \(s < \num{-.11}\) in order to have a driving layer. However, \(\Gamma_3\) also decreases in these layers, so the threshold for \(s\) is higher: for example, with \(n=1\) and \(\Gamma_3 = 1.4\) we only need \(s < \num{1.83}\).

% \todo[inline]{So, the \(\kappa \) in the \(\kappa\)-\(\gamma \)-mechanism refers to the decrease in \(s\) while the \(\gamma \) refers to the decrease in \(\Gamma_3\), right?}

\paragraph{Opacity bump mechanism}

The \(\kappa \)-\(\gamma \)-mechanism cannot explain the pulsation of hotter stars than RR-Lyr, Cepheids, \(\delta \)-Scuti or SX-Phe, such as \(\beta\)-Ceph, GW Vir and sdBV stars: in these, the stellar material is much more ionized on average: in the regions which, for cooler stars, are partial ionization regions the gas is fully ionized, while the partial ionization regions have moved further out towards the surface, if they have not disappeared entirely.

One could consider the partial second ionization layers: this has been thoroughly considered, and it has been found that they cannot provide a significant enough contribution.

There is an opacity bump at \(5<\log_{10}(T)< 6.5 \), which was found in the eighties by Simon: the old graph for \(\kappa (T)\) had spikes around \(T \sim \num{e4}\) and \(T \sim \num{e4.7}\), and a new spike was found at \(T \sim \num{e5.4}\).
Do note that these are temperatures reached when looking a bit inside the star, not at the surface (although close to it).

\paragraph{Convective blocking}
Convection is slow to adapt, so it cannot move away the heat brought by the pulsations. Then, the heat must be turned into work.
This happens when the period of the pulsations is of the order of the thermal timescale?
\todo[inline]{Not sure about this}

\paragraph{Convective driving}
Convective driving is called the \(\delta \)-mechanism.
It happens when the convective timescale is much shorter than the period of the pulsation, so the convection can mix the gas.
\todo[inline]{And this drives pulsations?}

\paragraph{Stochastic driving}
There also are \emph{stochastically driven} oscillations: they happen in stars which are intrinsically stable, but the pulsations may be fed by convective turbulence.
This can happen in Sun-like Main Sequence stars, or in solar-like red giants, such as \(\xi \)-Hydrae stars.

\paragraph{Strange modes}
There are very luminous \emph{strange modes}, very dim convectively driven modes.

\subsubsection{The classical instability strip}

The pulsation region has boundaries:
for the \(\kappa \)-mechanism:
\begin{itemize}
    \item if the star is too hot, the regions of partial ionization get too close to the surface;
    \item if the star is too cold, they are too far in: in their outer region the pulsation is damped by convective heat transfer.
\end{itemize}

So, there is an optimal region for the partial ionization layers to be.

What is the optimal region? We will only look  at the fundamental mode. We define
%
\begin{equation}
  \phi (m) \equiv \frac{1}{L(\Pi / 2 \pi )}\int_m^M c_V T \dd{m}
\,,
\end{equation}
%
which represents the thermal balance of a single oscillation: \(L(\Pi / 2 \pi )\) is the luminosity radiated in a radian of the pulsation cycle (\(1/ 2 \pi \) cycles),
while \(c_V T = T \qty(\pdv*{Q}{T}) \approx Q\), the heat per unit mass, which when integrated from \(m\) to \(M\), the whole mass of the star, gives us the global heat variation of the layers above \(m\).

We are in the helium ionization region: there are no nuclear reactions, so we set energy generation to zero in equation \eqref{eq:linearized-E2}: we get
%
\begin{align}
\pdv{}{t} \qty(\frac{ \delta T}{T}) =
(\Gamma_3 -1) \pdv{}{t} \qty(\frac{ \delta \rho }{\rho })
- \frac{1}{c_V T} \pdv{ \delta L}{m}
\,.
\end{align}

Now we assume that the temperature, density and luminosity perturbations are oscillating, so they have a certain starting value, and the temporal dependence is absorbed in a factor \(e^{i \sigma t}\). Then, we get
%
\begin{subequations}
\begin{align}
i \sigma \frac{ \delta T}{T} &= (\Gamma_3 -1) i \sigma \frac{ \delta \rho }{\rho } - \frac{1}{c_V T} \pdv{ \delta L}{m} \\
\frac{ \delta T}{T} &= (\Gamma_3 -1) \frac{ \delta \rho }{\rho } + \frac{i}{\sigma  c_V T} \pdv{ \delta L}{m}
\,.
\end{align}
\end{subequations}

Now, consider the following: with our definition of \(\phi \), we can write:
%
\begin{align}
\pdv{}{\phi } = \pdv{m}{\phi } \pdv{}{m}
= - \frac{L(\Pi /2\pi )}{c_V T} \pdv{}{m}
\,;
\end{align}
%
do note that \(\phi \) is adimensional, because of the way we define \(L(\Pi / 2 \pi )\):
%
\begin{align}
L(\Pi / 2\pi ) = L / \sigma
\,,
\end{align}
%
which is an energy. With these facts in mind, plus the fact that approximately \(\pdv*{L}{m} = 0\), we can do the following manipulation:
%
\begin{subequations}
\begin{align}
\frac{i}{\sigma c_V T} \pdv{ \delta L}{m}
&= - \frac{i }{\sigma L(\Pi / 2 \pi ) } \pdv{ \delta L }{\phi }  \\
&= -i \pdv{}{\phi } \qty(\frac{ \delta L}{L})
\,.
\end{align}
\end{subequations}

Now, we can use the chain rule on the derivative in \(\partial \phi \) using any auxiliary variable we like: we choose \(x = r/ R\), the fractionary radius, so we get
%
\begin{equation} \label{eq:temperature-evolution-transition-region}
  \frac{\delta T}{T} = (\Gamma_3 -1 ) \frac{\delta \rho }{\rho } - i \qty(\pdv{\phi }{x} )^{-1} \pdv{}{x} \qty(\frac{\delta L}{L})
\,,
\end{equation}
%
where \(\delta T\) is actually complex, since the perturbations are out of phase.

We can make some considerations on the variations of the terms in the equation:
%
\begin{align}
\phi \approx \frac{1}{L(\Pi / 2 \pi )}
\frac{4}{3} \pi R^3 \qty(1 - x^{3}) \rho c_V T
\propto (1-x^3) \rho c_V T
\,,
\end{align}
%
so
%
\begin{align}
\pdv{\phi }{x} \propto - x^2 \rho c_V T
\,.
\end{align}

The \(x^2\) dependence, however, is not very important: if the internal energy of a layer does not change, we can write the first law of thermodynamics as
%
\begin{align}
\dd{Q} = P \dd{\qty(\frac{1}{\rho })}
\implies
P \sim \dv{Q}{ \qty(\rho^{-1})} \sim Q \times \rho
\,,
\end{align}
%
and \(c_V T \sim Q\), so we have:
%
\begin{align}
\pdv{\phi }{x} \propto - x^2 P
\,,
\end{align}
%
and while \(x\) goes to zero in the center, the pressure increases a lot.

\begin{bluebox}
From what I could gather in 5 minutes, the Sun's pressure profile is very roughly given by
%
\begin{align}
P(x) \sim \SI{2.5e11}{Pa} \exp( - \lambda x)
\,,
\end{align}
%
where \(\lambda \sim 9\). Now, if we plot \(\exp(-9x) x^2\), it becomes small both at the surface and at the center, and has a maximum around \(x \sim \num{.2}\).
So, the reasoning still makes sense, but you have to consider the very center of the star as a special case.
\end{bluebox}

On the surface, the imaginary term is negligible. In the interior, it is relevant.

So, near the surface \(\pdv*{x}{\phi }\) is very large: therefore the term multiplying it, \(\pdv*{}{x} \qty(\delta L / L)\), must become small in order for the temperature perturbation not to diverge. This means that the luminosity perturbation cannot change much: it is ``frozen in''.
Anyway, the imaginary part of the RHS of equation \eqref{eq:temperature-evolution-transition-region} is much larger than the real part.
\todo[inline]{Right?}

On the other hand, near the center the term \(\pdv*{x}{\phi }\) becomes very small, while the spatial derivative of the perturbation does not become very large, so on balance the term remains small: the oscillations are then quasi-adiabatic.

Between these two regimes, we have a \emph{transition region} in which the two terms in equation \eqref{eq:temperature-evolution-transition-region} are similar in terms of order of magnitude: depending on the temperature of the star, this can can occur before or after the ionization region, when going radially outward.

The classical instability strip is the region in the Hertzsprung-Russell diagram in which the partial ionization region roughly coincides with the transition region.

\todo[inline]{Why is the perturbation ``frozen in'' if its spatial derivative is small? we could have \(\delta L / L\)  constant with respect to \(x\) and it would still evolve in time!}

\todo[inline]{Why are the boundaries of the classical instability strip tilted? It seems like the region is defined by some relation like \(\log L = - (\text{large \#}) \log T\), why is this so? Can it be derived by a simple argument?}

\subsection{Star taxonomy}

\subsubsection{RR Lyr\ae}

These are stars with periods between \(\num{.2} \divisionsymbol \SI{1}{d}\), absolute visual magnitude around \num{+.6}, and mean effective temperature around \( \num{6000} \divisionsymbol\SI{7250}{K}\).

It is a stage, which lasts no more than \SI{e8}{yr}, and it is observed only in clusters older than \SI{e10}{yr}.

They are classified by a, b, or c according to the shape of the light curve, its amplitude, its period:

\begin{enumerate}
  \item RRa: sharp rise, large amplitude: the fundamental;
  \item RRb: similar to RRa with smaller amplitude, longer period: the fundamental;
  \item RRc: more symmetric light curve, short periods, low amplitudes: they pulsate in the first overtone.
\end{enumerate}

We have also RRd: bimodal, RRe: second overtone.

These characteristics are seen well in a Bailey diagram, in which we scatter plot the stars with magnitude on the \(y\) axis and period on the \(x\) axis.

% [Argument for the different amplitudes at different wavelengths: to understand]

We can do spectral analysis on a set of several different stars: we find that, when applying a spectral filter to the star and doing Fourier analysis on that light curve, we can identify a relation in the form
%
\begin{align}
M_\nu = k_\nu \log \Pi
\,,
\end{align}
%
where \(M_{\nu }\) is the magnitude measured in that particular spectral band, while \(k_{\nu }\) is a constant depending on the band. \(k_{\nu }\) can be either positive or negative, and it is increasing with \(\nu \).

[Qualitative part of the lecture.]

\end{document}


\section{Timeline}
\label{sec:appA}

\begin{enumerate}
\item \textbf{Add Item 0. Get turnover from Canlas, Cuniff, exercise stuff to know we can fly, can talk to optitrack, and that you have a laptop available with all software planned to be used.}

\item Simulation
\begin{enumerate}
\item Familiarize myself with simulation capabilities.
\begin{enumerate}
\item Test all demos (1 week)
 \item Adapt some variables to see how the simulated results respond (1 week)
\item Upload model of the Crazyflie quadrotor to the simulation to be used for accurate simulation data. (1-3 days)
\end{enumerate}

\item Create a desired path for the Crazyflies to fly.
\begin{enumerate}
\item Choose 3 different desired flight shapes and adapt simulation code to take them in as the desired flight path for the quadrotor. (1 week)
\item Run simulation tests and error calculations. Debug any issues with simulation. (1-2 weeks)
\item Plot all position, velocity, and accelerations vs. time (both linear and rotational on all 3 axis). (1-2 days)
\end{enumerate}

\item Test hummingbird trajectories in simulation.
\begin{enumerate}
\item Upload hummingbird dive trajectories into simulation. (3 days)
 \item Run simulation and plot position, velocity, and acceleration data vs. time for all 3 axes in both the linear and rotational frames. (2 days)
\item Conduct error calculations between the desired trajectory and the simulated trajectory of the quadrotor. (1 week)
\item Determine acceptable region of error, and determine a maximum theoretical performance for the quadrotor as a measure of how fast the quadrotor is able to complete the maneuver as compared to the hummingbird. (1 week)
\end{enumerate}
\end{enumerate}

\item Proof of Concept Demonstration
\begin{enumerate}
\item Purchase new Crazyflie and supporting materials
\begin{enumerate}
\item Create online shopping cart pdf of all materials: Crazyflie, extra blades, extra battery, more OptiTrack markers, etc. (3 days)
\item Create RQ forms and ITPR forms (\textbf{Marcello}\footnote{I've done the first ones as examples for you. You're doing all the others.}) (1 week).
\end{enumerate}

\item Obtain manual control of a Crazyflie with remote control.
\begin{enumerate}
\item Configure Crazyradio and obtain positive connection to the Crazyflie using Crazyflie PC client software. (1-2 days)
\item Connect controller to laptop and configure manual controls to Mode 2 (1 day)
\item Conduct takeoff/landing procedures. (1 day)
\item Fly a rough 3D pattern using manual control. (1 day)
\end{enumerate}

\item Obtain flight data using OptiTrack
\begin{enumerate}
\item Set up OptiTrack system to recognize Crazyflie as a rigid body. (3 days)
\item Run sample code to log flight data. (3 days)
\item Manually fly the Crazyflie and obtain flight data. (3 days)
\item Plot position, velocity, and acceleration data vs. time for all 3 axes in both the linear and rotational frames in \MATLAB. (3 days)
\end{enumerate}

\item Fly the Crazyflie Autonomously
\begin{enumerate}
\item Read about ROS and develop python proficiency (4 months, concurrent)
\item Develop/Utilize currently existing control algorithm architecture to autonomously control the Crazyflie through the input of specified waypoints with associated position, velocity, and (orientation?) information. (1-3 weeks)
\item Execute autonomous control and collect flight data using OptiTrack (1 week)
\item Plot position, velocity, and acceleration data vs. time for all 3 axes in both the linear and rotational frames in \MATLAB. (3 days)
\item Conduct error calculations between the desired trajectory and the actual flight trajectory data collected from the OptiTrack system. (1 week)
\item Present experimental findings (1 month).
\end{enumerate}

\end{enumerate}
\end{enumerate}

Total estimated time to completion: \SI{24}{weeks}.


\section{Language}
\label{sec:language}

\subsection{Modifiers}
A type is either a type variable or consists of an modifier, a type name, and possibly type arguments. There are three types of modifiers.
\begin{itemize}
\item \emph{local} expresses that an object does not escape from its defining method.
\item \emph{sep} expresses that the memory regions reachable from an object is disjoint with other objects.
\item \emph{transient} in \cite{haller2010capabilities} and \emph{references and borrowing} (\KW{mut} and \KW{shr}) in Rust expresses that the passing object's permission will be returned back to the caller once the method finishes executing. In another words, the ownership is \emph{``transfered" with limited lifetime}. (\KW{mut} expresses that the object is mutable. \KW{shr} expreses that the object is read-only.)

Without the \emph{transient} or \KW{mut} or {shr} modifier, the caller loses the permission of the object. (Ownership transfer (Separation logic, Rust))
%\begin{lstlisting}[language=Scala,basicstyle=\footnotesize\ttfamily]
%let s = String::from("hello")
%takes_ownership(s);      // s lost the ownership
%
%let s1 = gives_ownership();  // s1 got the ownership
%
%fn takes_ownership(some_string: String) {
%   println!("{}", some_string);
%}
%
%fn gives_ownership() -> String {
%  let some_string = String :: from("hello");
%  some_string
%}
%\end{lstlisting}

\end{itemize}

\begin{figure}[!htb]
\small
\centering
\begin{grammar}%
\begin{tabular}{r r l}
%$~$\nonterm{md} & \: & \nonterm{m}(\nonterm{x}: \nonterm{T}):\nonterm{T} \{ \nonterm{t} \}
$~$\nonterm{t} & \: & \nonterm{c} \| \nonterm{x} \| \KW{new} $\mu_1\mu_2$\nonterm{T} \| $\lambda$\nonterm{x}:$\mu_1\mu_2$\nonterm{T}.\nonterm{t} \| \nonterm{t}\nonterm{t} \| $\lambda$\nonterm{X}.\nonterm{t} \| \nonterm{t}[\nonterm{T}]
& & \| \{\nonterm{t},\nonterm{t}\} \| \nonterm{t}.$1$ \| \nonterm{t}.$2$
$~$\nonterm{v} & \: & \nonterm{c} \| $\lambda$\nonterm{x}:$\mu_1\mu_2$\nonterm{T}.\nonterm{t} \| $\lambda$\nonterm{X}.\nonterm{t} \| \{\nonterm{v},\nonterm{v}\}
$~$\nonterm{T}$_s$ & \: & \nonterm{B} \| \nonterm{X} \| \nonterm{T}$_s\to$\nonterm{T}$_s$ \| $\forall$\nonterm{X}.\nonterm{T}$_s$ \| \nonterm{T}$_s\times$\nonterm{T}$_s$
$~\Gamma_s$ & \: & $\emptyset$ \| $\Gamma$, \nonterm{x}:\nonterm{T}$_s$ \| $\Gamma$,\nonterm{X}
$~\mu_1$ & \: & \KW{any} \| \KW{sep}
$~\mu_2$ & \: & \KW{any} \| \KW{local} \| \KW{mut} \| \KW{shr}
\end{tabular} %
\end{grammar}
\caption{The syntax of the programming language.}
\label{fig:syntax}
\end{figure}
\subsection{Types}
\begin{figure}[!htb]
\begin{mathpar}
\inferrule[(\emph{O\_Intro})]{ }{mut \MSUBTYPE shr}

\inferrule[(\emph{O\_Ma})]{ }{\mu \MSUBTYPE any}

\inferrule[(\emph{O\_Refl})]{ }{\mu \MSUBTYPE \mu}
\end{mathpar}
\caption{Ordering of modifier}
\label{fig:order-mod}
\end{figure}
\begin{figure}[!htb]
\begin{mathpar}
\inferrule[(\emph{ST\_Refl})]{ }
{ \TypJudg{}{T ~ \SUBTYPE T}}

\inferrule[(\emph{ST\_Fun})]{ \TypJudg{}{T_1 \SUBTYPE T_1'} \\ \TypJudg{}{T_2' \SUBTYPE T_2}}
{\TypJudg{}{T_1' \mapsto T_2' \SUBTYPE T_1 \mapsto T_2}}

\inferrule[(\emph{ST\_Trans})]{  \TypJudg{}{T_1 \SUBTYPE T_2} \\ \TypJudg{}{T_2 \SUBTYPE T_3 }}
{ \TypJudg{}{T_1 ~ \SUBTYPE T_3}} \\

\end{mathpar}
\caption{Static typing rules for subtypes}
\label{fig:subtypes}
\end{figure}

\subsection{Semantics}
Each closure instance will be identified by a unique token that represents the closure occurence from which it was created \cite{kassios2010specification} and method declarations. Tokens and memory addresses are disjoint. Let $\theta$ map from tokens to closure occurences or method declarations.

There is a store $s$ that maps from variables to values, and a heap $h$ that maps from locations to values. A location is a pair of an address and its owner. Addresses are natural numbers.
Heap looks up is denoted by $h(l)$, where $l$ is a location. Heap extension is denoted by $h + (l, v)$, where $\RESTRICT{l}{1} \notin \RESTRICT{\DOM(h)}{1}$.
%Ownership updates is denoted by $h[addr, o]$, where $(addr, \_) \in \DOM(h)$, and $for all (addr', o') \in \DOM(h). (addr' \neq addr \Rightarrow h[addr, o](addr', o') = h(addr', o')) \land addr == addr' \Rightarrow h[addr, o](addr, o) = h(addr', o')$.

\begin{figure}[!ht]
\small
\centering
\begin{grammar}%
\begin{tabular}{r r l}
$~$\nonterm{addr} & \: & $\mathbb{N}$
$~$\nonterm{l} & \: & (\nonterm{addr},\nonterm{o})
$~$\nonterm{s} & \: & $\overline{x \mapsto v}$
$~$\nonterm{h} & \: & $\overline{l \mapsto v}$
$~$\nonterm{o} & \: & $\epsilon$ \| \nonterm{addr} \| \nonterm{id}
$~$\nonterm{v} & \: & \nonterm{l} \| \nonterm{null}$_l$ \| $\lambda$\nonterm{x}.\nonterm{t} \| \{\nonterm{v},\nonterm{v}\}
$~$\nonterm{T} & \: & $\REF_{mut}$\nonterm{T} \| $\REF_{shr}$ \nonterm{T} \| sep \| local
%$~\Gamma_r$ & \: & $\emptyset$ \| $\Gamma$, \nonterm{X} $\mapsto$ \nonterm{T}$_r$ \| $\Gamma$,\nonterm{x} $\mapsto$ v
\end{tabular} %
\end{grammar}
\caption{Definition of the runtime model.}
\label{fig:runtime}
\end{figure}

\figref{fig:semantics} shows the operational semantics. The computation tracks ownership.

\begin{figure}
\begin{mathpar}
\inferrule[(\emph{evar})]{s(x) = v }
{\MEVAL{(s, h)}{x}{(s, h), v}}

\inferrule[(\emph{enew})]{\KW{fresh}(addr) \\ l = (addr, id) \\ v = \DEFAULT(T, addr) \\ h' = h + (l, v)}
{\MEVAL{(s, h)}{\KW{new} ~ T}{(s, h'), l}}

%\inferrule[(\emph{eabs})]{ v = \lambda x . t \\ c' = c + (id, v) }
%{\MEVAL{(s, h)}{\lambda x:T_r.t}{(s, h'), v}}

%\begin{array}{l}
%\inferrule[(\emph{eproj})] { \MEVAL{(s, h)}{t}{l} \\ v = h(l)[i]}
%{\MEVAL{(s, h)}{t.i}{(s, h), v}} \\
%\end{array}

\inferrule[(\emph{eapp})]{ \theta(id_1) = \lambda x. t_3 \\ \MEVAL{(s, h)}{t_2}{(s, h), (addr, o)} \\ h_2 = (h_1[addr, \epsilon][addr_1, o_1]) \\ \NMEVAL{(s, x:(addr, o), h)}{t_3}{(s_1, h_1), (addr_1, o_1)}{id_1}}
{\MEVAL{(s, h)}{t_1 t_2}{(s, h_2), (addr_1, o_1)}}

\end{mathpar}
\caption{Operational Semantics}
\label{fig:semantics}
\end{figure}

\begin{figure}[!htb]
\begin{mathpar}
\inferrule[(\emph{tbnew})]{ }
{ \TypJudg{\Gamma}{\KW{new} ~ T : \KW{own} ~ T}}

\inferrule[(\emph{tpair})]{\TypJudg{\Gamma}{t_1 : \KW{own} ~ T_1 \triangleright \epsilon}  \\ \TypJudg{\Gamma}{t_2: \KW{own} ~ T_2 \triangleright \epsilon}}
{ \TypJudg{\Gamma}{\{t_1, t_2\} : \KW{own} ~ T_1 \times T_2} }

\inferrule[(\emph{tproj})]{\TypJudg{\Gamma}{t : \KW{own} ~ T_1  \times T_2 \triangleright \epsilon;} \\ \Gamma' = \Gamma + (t.1:\KW{own} ~ T_1) + (t.2 : \KW{own} ~ T_2)}
{ \TypJudg{\Gamma'}{t.1 : \KW{own} ~ T_1 } }

\inferrule[(\emph{tbmute})]{ x : \KW{own} ~ T \in \Gamma }
{ \TypJudg{\Gamma}{\KW{mut} \& ~ x : \REF_{mut}}}

\inferrule[(\emph{tbreborrow})]{ x : \REF_{mut} ~ T \in \Gamma }
{ \TypJudg{\Gamma}{\KW{mut} \& ~ x : \REF_{mut} ~ T}}

\inferrule[(\emph{tbshare})]{ x : \KW{own} ~ T \in \Gamma }
{ \TypJudg{\Gamma}{\KW{shr} \& ~ x : \REF_{shr} ~ T}}

\end{mathpar}
\caption{Static typing rules for ownership and borrowing}
\label{fig:subtypes}
\end{figure}

\begin{figure}[!htb]
\begin{mathpar}
\inferrule[(\emph{tsnew})]{ }
{ \TypJudg{\Gamma}{\KW{new} ~ T : \KW{sep}, \KW{own} ~ T}}

\inferrule[(\emph{tspair})]{\TypJudg{\Gamma}{t_1 : \KW{sep}, \KW{own} ~ T_1 \triangleright  \epsilon } \\ \TypJudg{\Gamma}{t_2 : \KW{sep}, \KW{own} ~ T_2 \triangleright  \epsilon} }
{\TypJudg{\Gamma}{ \{t_1, t_2\} : \KW{sep}, \KW{own} ~ T_1 \times T_2}}

\end{mathpar}
\caption{Static typing rules for separation and ownership}
\label{fig:subtypes}
\end{figure}


This chapter describes the lexis, the syntax, and the semantics of TBASIC. In other words, this chapter describes which words are good, how they can be jumbled up, and what the hell they could even mean.

\section{Line Format}

Program lines in BASIC program have the following format:

\forceindent{\monofont nnnnn BASIC-statement <carriage return>} \

Only one BASIC statement may be placed on a line. A program line always begins with a line number and ends with a carriage return.

\section{Lexical Conventions}

\emph{Names} (also called \emph{identifiers}) in TBASIC can be any string of letters, digits, and underscores, not beginning with a digit. Identifiers are used to name variables.

The following \emph{keywords} are reserved and cannot be used as names:

\begin{multicols}{6}
%{\condensedfont%
ABORT\par
ABORTM\par
ABS\par
AND\par % operator
ASC\par
BACKCOL\par
BEEP\par
CBRT\par
CEIL\par
CHR\par
CLR\par
CLS\par
COS\par
DEF\par
DELETE\par
DIM\par
DO\par
END\par
FALSE\par
FLOOR\par
FN\par
FOR\par
GET\par
GO\par
GOSUB\par
GOTO\par
HTAB\par
IF\par
IN\par
INPUT\par
INT\par
INV\par
LEFT\par
LEN\par
LIST\par
LOAD\par
LOG\par
M\_E\par % constant
M\_PI\par % constant
M\_2PI\par % constant
M\_ROOT2\par % constant
MAX\par
MID\par
MIN\par
MINUS\par % operator
NEW\par
NEXT\par
NIL\par
NOT\par % operator
OR\par % operator
PRINT\par
REM\par
RENUM\par
RETURN\par
RIGHT\par
RND\par
ROUND\par
RUN\par
SAVE\par
SCROLL\par
SGN\par
SIN\par
SIZEOF\par % operator
SQRT\par
STEP\par % operator
STR\par
TAB\par
TAN\par
TEXTCOL\par
THEN\par
TO\par % operator
TEMIT\par
TRUE\par
VAL\par
VTAB\par
XOR\par % operator
%}%
\end{multicols}

TBASIC is a case-insensitive language, and so is the reserved word.

The following strings denote other tokens:

\begin{multicols}{6}
%{\condensedfont%
$>$$>$$>$\par
$<$$<$\par
$>$$>$\par
|\par
\&\par
!\par
;\par
$=$$=$\par
$>$\par
$<$\par
$<$$=$\par
$=$$<$\par
$>$$=$\par
$=$$>$\par
!$=$\par
$<$$>$\par
$>$$<$\par
$=$\par
$:$$=$\par
$^\wedge$\par
*\par
/\par
$+$\par
$-$\par
\%\par
(\par
)\par
,\par
%}
\end{multicols}

\section{Constants}

TBASIC predefines some constants using its variable system. You can read from these variables, but any new assignment will have no effects and TBASIC will \emph{not} raise any errors.

\begin{tabularx}{\textwidth}{l l X}
	\textbf{Name} & \textbf{Value} & \textbf{Remarks}
	\\
	\endhead
	M\_PI    & 3.141592653589793 & Mathematical constant $\pi$ \\
	M\_2PI   & 6.283185307179586 & Mathematical constant $2\pi$ \\
	M\_E     & 2.718281828459045 & Mathematical constant $e$ \\
	M\_ROOT2 & 1.414213562373095 & Mathematical constant $\sqrt{2}$ \\
	TRUE    & \emph{true} & Self explanatory \\
	FALSE   & \emph{false} & do. \\
	NIL     & \emph{nil} & do. \\
	\_VERSION & \tbasver & Current TBASIC version \\
\end{tabularx}