\documentclass{article}

\usepackage{amsfonts}
\usepackage{amsmath}

\usepackage{natbib}

\usepackage{subfigure}

\usepackage[ruled, vlined]{algorithm2e}

\usepackage{graphicx}
\graphicspath{{../figures/}}

\usepackage{geometry}
\geometry{
	left=2cm,
	top=1cm,
	right=2cm,
	bottom=2cm
}

\usepackage[colorlinks=false,
pdfpagemode=UseNone,
urlcolor=blue,
bookmarks=true,
backref=page,
hidelinks,
pdftitle={STAT 536D - Final Project},%
pdfauthor={Pramoda Jayasinghe},%
pdfsubject={STAT 536D - Final Project},%
pdfkeywords={latent variables, generative model, missing data}
]{hyperref}

\title{
	{\large \vspace*{-4em}
		STAT 536D - Statistical Theory for the Design and Analysis of Clinical Studies}\\
	Missing indicator in the generative model for MCAR and MAR cases \\
}

\author{Pramoda Jayasinghe}
\date{April 29, 2021}

\begin{document}

	\maketitle

	\section{Introduction}

	Missing values are a common complication in any type of practical statistical analysis. This is especially true in public health studies where the participant can drop out from a study or not respond to questions due to the personal nature of the information. Statistical inference based on incomplete data can lead to erroneous conclusions if the ``missingness" itself carries important information. Literature exists \citep{Little2012} which suggests points to consider in data collection to reduce non-responses leading to missing data. But it is not practical to completely eliminate missing data in public health.

	One common approach to handle missing data is to impute the missing values and treat the problem as a complete-case analysis. Imputation allows the analyst to use pre-existing statistical methods directly without any modification making the analysis simpler \citep{Chen2015}. It is also observed that when the model becomes complex, specifically when the outcome is multivariate as in longitudinal studies, the performance of imputation methods becomes less desirable \citep{Erler2016}.

	Bayesian methods are introduced as an alternative to imputation which can be comparatively easier to implement in more complex cases.

	\section{Bayesian approach to handle missing covariates}

	To introduce a way of handling missing data in the Bayesian context, we will use a case-control study example. Consider a study of the association between herpes simplex virus (HSV) and cervical cancer. Let us define
	%
	\begin{align*}
		Y &= \begin{cases}
			1 & \text{if the patient has invasive cervical cancer (case)},\\
			0 & \text{if the patient is a healthy (control)},
		\end{cases}\\
		X &= \begin{cases}
			1 & \text{HSV is present},\\
			0 & \text{HSV is not present}.
		\end{cases}
	\end{align*}
	%
	Consider the relationship between the $X$ and $Y$ as
	%
	\begin{align*}
		\mathbb{P}(X = 1 \mid Y = y)
		&= \begin{cases}
			\gamma_0 & \text{if } y = 0, \\
			\gamma_1 & \text{if } y = 1,
		\end{cases}
	\end{align*}
	%
	where $\gamma_0$ and $\gamma_1$ are parameters that need to be estimated.

	The lab test to definitively determine the presence of the HSV is very expensive and cannot be measured for all study participants. Alternatively, a lab test (`western blot') exists which is less definitive but is much cheaper. Let's denote the result of this test by $X^{*}$. The sensitivity ($Se$) and the specificity ($Sp$) of the western blot test can be defined as,
	%
	\begin{align*}
		\mathbb{P}(X^{*} = x \mid X = x)
		&= \begin{cases}
			\text{Se} & \text{if } x = 1, \\
			\text{Sp} & \text{if } x = 0.
		\end{cases}
	\end{align*}
	%
	To account for the missing values of $X$, consider an indicator;
	%
	\begin{align*}
		R &= \begin{cases}
			1 & \text{if } X \text{ is observed}, \\
			0 & \text{if } X \text{ is missing}.
		\end{cases}
	\end{align*}
	%
	The posterior distribution for this problem can be defined as
	%
	\begin{align}
		f\left( \gamma_0, \gamma_1, Se, Sp, x^{(mis)} | y, x^{(obs)}, x^{*}, r \right)
		&\propto f(\gamma_0, \gamma_1, Se, Sp) f(x \mid y, \gamma_0, \gamma_1, r) f(x^{*} \mid x, y, Se, Sp) f(r \mid x, x^{*}, y).
		\label{eq:full_gen_model}
	\end{align}

	\section{Missingness mechanisms}

	We can simplify the generative model from \eqref{eq:full_gen_model} further based on the ``missingness mechanism". We can summarize the missing mechanisms as; missing completely at random (MCAR), missing at random (MAR) and non-ignorable missing cases.

	If the data are missing completely at random, the distribution of $R$ will be independent of any other observed or unobserved variables. Therefore the conditional density of $R$ can be written as
	%
	\begin{align*}
		f(r \mid x, x^{*}, y, \lambda)
		&= f(r \mid \lambda),
	\end{align*}
	%
	where $\lambda$ is a parameter. In the case of missing at random, the distribution of $R$ can be described through observed values only. Therefore the density of $R$ can be written as,
	%
	\begin{align*}
		f(r \mid x, x^{*}, y, \boldsymbol{\lambda})
		&= f(r \mid y, x^{*}, \boldsymbol{\lambda}).
	\end{align*}
	%
	Non-ignorable missingness implies that the density of $R$ cannot be described completely by observed values. This can also be seen as cases that do not hold the relationship for MAR.

	In either the MCAR or the MAR, the term $f(r \mid x, x^{*}, y, \lambda)$ in \eqref{eq:full_gen_model} can be absorbed into the proportionality and eventually be ignored. This simplifies the generative model to
	%
	\begin{align}
		f\left( \gamma_0, \gamma_1, Se, Sp, x^{(mis)} | y, x^{(obs)}, x^{*}, r \right)
		&\propto f(\gamma_0, \gamma_1, Se, Sp) f(x \mid y, \gamma_0, \gamma_1) f(x^{*} \mid x, y, Se, Sp).
		\label{eq:reduced_gen_model}
	\end{align}

	From this observation, it is really interesting to find the answer to the following question. \textbf{``Does the posterior answer we get and the numerical efficiency in getting it, change if you include $R$ amongst the observables in the generative model; even for the MCAR or MAR cases"}.

	\section{Examples}

	To answer the question mentioned above, we can consider a simulated dataset for the case-control study of interest. Based on the binary nature of $X$ and $X^{*}$ it is logical to assume Bernoulli distributions for both variables. Let
	%
	\begin{align*}
		X &\sim Bernoulli(p_x); \; p_x = (1-y)\gamma_0 + y\gamma_1,\\
		X^{*} &\sim Bernoulli(p_{x^*}); \; p_{x^*} = (1 - x)(1 - Sp) + xSe.
	\end{align*}
	%
	Uniform distributions are used as priors for the parameters as follows
	%
	\begin{align*}
		\gamma_0 &\sim Unif(0, 1),\\
		\gamma_1 &\sim Unif(0, 1),\\
		Se &\sim Unif(0.5, 1),\\
		Sp &\sim Unif(0.5, 1).
	\end{align*}
	%
	Priors for $Se$ and $Sp$ are bounded so that $X^{*}$ won't be a worse alternative to $X$ than a coin flip.

	These distributions and the data can be then fed into a Markov Chain Monte Carlo (MCMC) mechanism to sample from the posterior and then we can extract summaries from the posterior samples. In this example, we use a Gibbs sampler, namely Just Another Gibbs Sampler (JAGS) (See \url{https://mcmc-jags.sourceforge.io/} for more information on the sampler).

	To answer the question, we can implement 3 cases and use the MCMC algorithm to obtain answers. For the results to be comparable, options of the sampler are fixed among the 3 cases.
	\begin{description}
		\item[``Ignored"] Initially, we can ignore the distribution of $R$ as in \eqref{eq:reduced_gen_model} to obtain the parameter estimates, establishing a baseline to compare other cases against.
		\item[MCAR] Then we can use \eqref{eq:full_gen_model} and introduce a Bernoulli distribution on $R$ as
		%
		\begin{align*}
			R &\sim Bernoulli(p_r); \; p_r = \lambda,
		\end{align*}
		%
		and set a prior on $\lambda \sim Unif(0, 1)$.
		\item[MAR] Changing $p_r = \lambda_1 - \lambda_2 y$ will lead us to a MAR problem where we can set uniform priors on $\lambda_1$ and $\lambda_2$ similar to the MCAR case.
	\end{description}

	\subsection{Statistical results}

	\begin{figure}[h]%
		\centering
		\subfigure[][]
		{\label{fig:ci_ign_mcar}%
			\includegraphics[keepaspectratio, width=0.45\linewidth]{ci_ign_mcar}
		} %\qquad
		\subfigure[][]{
			\label{fig:ci_ign_mar}%
			\includegraphics[keepaspectratio, width=0.45\linewidth]{ci_ign_mar}%
		}
		\caption{$95\%$ credible intervals for the case where $R$ is ignored and \subref{fig:ci_ign_mcar} missing completely at random case and \subref{fig:ci_ign_mar} missing at random case.}
		\label{fig:ci}
	\end{figure}
	%
	If we run the simulation and use the MCMC algorithm to obtain the credible intervals (CI) for the parameters of interest we get the results shown in Figure \ref{fig:ci}. From these plots, it is obvious that adding the distribution of $R$ yields results visually identical to the case where $f(r \mid x, x^{*}, y, \lambda)$ is ignored. This is exactly the result we would expect to see in the MCAR and MAR cases. If the results were not similar between the ignored and other cases, it would be an indication that $R$ depends on the unobserved variables.

	\subsection{Numerical results}

	The potential scale reduction statistic; $\hat{R}$ \citep{Brooks1998} for the 3 cases (Table \ref{tab:rhat}) shows that it does not change much between the 3 cases. The effective sample sizes (ESS) (Table \ref{tab:neff}) shows that MCAR provides slightly lower values in general than the ignored case and the MAR case shows slightly higher ESS than the ignored case.

	\begin{table}[h]
		\centering
		\begin{tabular}{c|c|c|c}
			Parameter & Ignored & MCAR & MAR\\
			\hline
			$\gamma_0$ & 1 & 1.02 & 1.01 \\
			$\gamma_1$ & 1 & 1.01 & 1.01\\
			$Se$ & 1 & 1.00 & 1.01\\
			$Sp$ & 1 & 1.01 & 1.00\\
		\end{tabular}
		\caption{Potential scale reduction statistic; $\hat{R}$ for the parameters in the 3 cases.}
		\label{tab:rhat}
	\end{table}

	\begin{table}[h]
		\centering
		\begin{tabular}{c|c|c|c}
			Parameter & Ignored & MCAR & MAR\\
			\hline
			$\gamma_0$ & 731 & 718 & 811\\
			$\gamma_1$ & 943 & 899 & 1142\\
			$Se$ & 838 & 938 & 998\\
			$Sp$ & 759 & 695 & 741\\
		\end{tabular}
		\caption{Effective sample sizes for the parameters in the 3 cases.}
		\label{tab:neff}
	\end{table}

	The time for the JAGS algorithm to run (Table \ref{eq:runtimes}) shows that ignoring the distribution of $R$ is the fastest and MAR is the slowest. This is understandable as we keep adding parameters to the model which increases the computational burden.

	\begin{table}[h]
		\centering
		\begin{tabular}{c|c|c}
			Ignored & MCAR & MAR\\
			\hline
			2.62 & 2.83 & 3.62\\
			\hline
		\end{tabular}
		\caption{JAGS runtimes (in minutes) for the 3 cases.}
		\label{eq:runtimes}
	\end{table}

	\emph{Note: The program was run on a laptop with an Intel i7-8750H processor, under normal running conditions.}

	\section{Summary}

	The main goal of this report was to examine if there is an advantage, statistically or computationally, of including the indicator for missing values ($R$) in the Bayesian generative model. We observed that adding information about $R$ to the generative model did not significantly change the parameter estimates or credible intervals. It was also hard to see a significant improvement in the statistical performance of the model when R-hat and effective sample size were considered. We also observed that the run time increased with the addition of $R$ to the model. Since there is no increase in the statistical efficiency, it would be preferable to ignore the missing indicator distribution in the missing completely at random and missing at random cases.

	\bibliographystyle{chicago}
	\bibliography{../references/stat_536d_final_project}

	\appendix
	\section{Supplementary Material}

	This analysis was conducted using \texttt{R} and JAGS. The entire GitHub repository is available at \url{https://github.com/sachijay/stat536d_final_project}. Please visit \url{https://github.com/sachijay/stat536d_final_project/tree/main/src} to view the source code for this project (in \texttt{R} scripts).

\end{document}


\selectlanguage{english}
\begin{abstract}
    \noindent For the second project of the Computer Simulations (ELTE Physics MSc) course I propose the concept of a molecular dynamics simulation, using the velocity Verlet integration. I intend to also implement Verlet's pair-list method, along with his Lennard-Jones potential cut-off method. It is also planned, to make the simulation with both periodic and closed boundary conditions. In the second case, if a molecule hits the wall, it will bounce of with a completely flexible collision.
\end{abstract}

\begin{multicols}{2}
\section{Introduction}
The aim of molecular dynamics simulations are to make it possible to effectively study the thermodynamic parameters of fluids. Using our current technology it is not possible yet to simulate the microscopic behaviour of the particles of a macroscopic system. Our goal should be to create such a simulation, where the examined parameters could be measured even using very small number of particles. \par
The method which will be used here was introduced by Loup Verlet in his notorious paper \citep{verlet1967computer}. Along with the numerical corrections proposed by also him, this type of algorithm could be used to simulate a molecular system $10$ times faster, than the Runge-Kutta method.

\section{Motivation}
Since I've run out of time during the making of the previous project, I decided to do a more simple, but still interesting and visually appealing simulation. Last year I've already tried  to solve this problem, but my results of measuring some thermodynamic quantities with the Verlet method were miserable. I hope with my since improved insights and knowledge I could provide much better results and visualization.

\section{Description of the proposed assignment}
In this project I will build a molecular dynamics simulation, using the Velocity-Verlet integration. As a reasonable approximation for the interaction between the particles, I'm going to use the Lennard-Jones potential, which is often used in the approximate description of the interaction between a pair of neutral atoms or molecules. \par
Since this potential is short-ranged, the effect of the distant particles could simply ignored outside an $r_{c}$ cut-off distance, thus creating a so-called "truncated and shifted Lennard-Jones potential". This cut-off distance is usually chosen to be $r_{c} = 2.5 \sigma$ \citep{2011JChPh.134h1102T}, where $\sigma$ is the finite distance at which the Lennard-Jones potential is zero. In conclusion, when calculating the forces acting on a single particle, instead of calculating the effect of every molecule, we calculate the forces from those particles only, which are situated within a sphere of $r_{c}$ radius around our central one. \par
It is also advised to implement Verlet's pair-listing method to speed up the simulation. If we first select all of the particles inside an $r_{c}$ radius around a single particle at every step, the simulation would be still very slow, because it is an $\mathcal{O} \left( N^{2} \right)$ hard problem. To solve this, we define a sphere around every particle with $r_{\text{max}} > r_{c}$ radius, and we call the list of every particle inside the $i$th sphere the pair-list of the $i$th particle. If during the calculation of forces acting on the $i$th particle we only need to loop through its neighbours inside the $i$th pair-list, without the need of selecting nearby ($r < r_{c}$) particles at every step. Since microscopic objects move at a finite speed, it is trivial that if we choose $r_{\text{max}} > r_{c}$, we only need to update this list only after every few step, because the interacting particles need a finite amount of time to move out from it. The standard value of $r_{max} = 3.2 \sigma$ usually. \par
For the simulation I've chosen the following values for these discussed parameters:

\begin{equation}
\text{\texttt{r\_c}} = 2.5
\end{equation}
\begin{equation}
\text{\texttt{r\_max}} = 3.2
\end{equation}
\begin{equation}
\text{\texttt{update\_interval}} = 10
\end{equation}
Where \texttt{update\_interval} is the number of steps, after the pair-list is updated.

\section{Theoretical background}
\subsection{Verlet and velocity Verlet algorithm}
The advisedly used algorithm (velocity Verlet method) is a variation of the somewhat simpler Verlet method. Both of them have their pros and cons. While the Verlet method's accuracy ($\mathcal{O} \left( \tau^{4} \right)$) approaches the Runge-Kutta's ($\mathcal{O} \left( \tau^{5} \right)$), it can't be started from a single initial position, since its stepping rule uses the $n$th and $(n-1)$th positions and velocities to calculate the $(n+1)$th values. The stepping rule is the following:

\begin{equation}
\boldsymbol{r}_{n+1}
=
2 \boldsymbol{r}_{n} - \boldsymbol{r}_{n-1} + \tau^{2} \boldsymbol{a}_{n} + \mathcal{O} \left( \tau^{4} \right)
\end{equation}
\begin{equation}
\boldsymbol{v}_{n+1}
=
\frac{\boldsymbol{r}_{n+1} - \boldsymbol{r}_{n-1}}{2 \tau} +  \mathcal{O} \left( \tau^{2} \right)
\end{equation}
Here $\boldsymbol{r}$ denotes the coordinate vector and $\boldsymbol{v}$ denotes the velocity vector of a particle, $\tau$ is the step size of the simulation. Another disadvantage of the Verlet method, that for the $\boldsymbol{v}$ propagation its accuracy is only $\mathcal{O} \left( \tau^{2} \right)$. \par
In contrast the velocity Verlet method could be started from an arbitrary initial position. Its stepping rule is the following:

\begin{equation}
\boldsymbol{r}_{n+1}
=
\boldsymbol{r}_{n} + \tau \boldsymbol{v}_{n} + \frac{\tau^{2}}{2} \boldsymbol{a}_{n} + \mathcal{O} \left( \tau^{3} \right)
\end{equation}
\begin{equation}
\boldsymbol{v}_{n+1}
=
\boldsymbol{v}_{n} + \frac{\tau}{2} \left( \boldsymbol{a}_{n+1} + \boldsymbol{a}_{n} \right) + \mathcal{O} \left( \tau^{3} \right)
\end{equation}
If we only want to accurately measure the conservation of energy and the coordinates of the molecules, we can combine the two method, where the first two step is evaluated by the velocity Verlet method, and the following are with the Verlet method. Here I will use only the velocity Verlet method for the whole simulation.

\subsection{Lennard-Jones potential}
The interaction between chargeless atoms or molecules could be approximated the already mentioned Lennard-Jones potential:

\begin{equation}
V^{LJ} \left( r \right)
=
4 V_{0} \left[ \left( \frac{\sigma}{r} \right)^{12} - \left( \frac{\sigma}{r} \right)^{6} \right]
\end{equation}
Here $\sigma$ is the already mentioned, finite distance, where the Lennard-Jones potential becomes zero. $V_{0}$ is the absolute value of the potential's value at the minimum ($V_{0} = \left| V_{min} \right|$). The reason to write the equation in this form could be easily understood. The $r^{-12}$ term is comes from the Pauli exclusion principle, which creates a significantly strong repulsion between the particles if they're very close to each other. On larger ranges the Van der Waals force overcomes this repulsion, and exert an attractive force on the molecules, thus adding the $r^{-6}$ term into the equation with a negative sign. At a characteristic distance $r_{m} = 2^{1/6} \sigma$ the potential reaches it's minimum value ($-V_{0}$), where the attraction is the strongest. Further increasing the distance between the particles, this attraction slowly decreases. \par
\begin{center}
	\includegraphics[width=0.5\textwidth]{img_src/Lennard_Jones_Potential.pdf}
	\captionof{figure}{The Lennard-Jones potential} \label{fig:2}
\end{center}
The force, which is used to calculate the accelerations for the stepping rules could be given by calculating the gradient of the potential:

\begin{equation}
\boldsymbol{F} \left( r \right)
=
-\boldsymbol{\nabla} V^{LJ} \left( r \right)
=
\frac{24 V_{0}}{r^{2}} \left[ 2 \left( \frac{\sigma}{r} \right)^{12} - \left( \frac{\sigma}{r} \right)^{6} \right] \boldsymbol{r}
\end{equation}
Using the cut-off method the Lennard-Jones potential becomes zero at $r \leq r_{c}$. Thus the truncated and shifted Lennard-Jones potential is the following:

\begin{equation}
V_{c}^{LJ} \left( r \right)
=
\begin{cases}
4 V_{0} \left[ \left( \frac{\sigma}{r} \right)^{12} - \left( \frac{\sigma}{r} \right)^{6} \right] & \text{if}\ r < r_{c} \\
0 & \text{if}\ r \leq r_{c}
\end{cases}
\end{equation}

\subsection{Correction for velocities}
Choosing the correct starting velocities for the individual particles is is not trivial. I intend to use the \texttt{gasdev()} function found in the \emph{Numerical Recipes}, which solves this problem \citep{press2007numerical}. It uses the Box-Müller algorithm to generate approximately correct velocities with $\sigma=1$ deviation, according to the Maxwell-Boltzman distribution. In 3D it is simply the Gauss distribution:

\begin{equation}
P \left( v \right)
=
\left( \frac{m}{2 \pi k_{B} T} \right)^{3/2}
*
e^{-\dfrac{m \left( v_{x}^{2} + v_{y}^{2} + v_{z}^{2} \right)}{2 k_{B} T}}
\end{equation}
Unfortunately this algorithm is not perfect, the mean of the starting velocities - the velocity of the center of mass - is not $0$ as it should be. To obtain velocities which are truly chosen according to the Gauss distribution, we should apply a correction. \par
First, we subtract the velocity of the center of mass from the current velocities:

\begin{equation}
\boldsymbol{v}_{i} := \boldsymbol{v}_{i} - \boldsymbol{v}_{CM}
\end{equation}
Then, we transform them with a $\lambda$ constant term:

\begin{equation}
\boldsymbol{v}_{i} := \lambda \boldsymbol{v}_{i}
\end{equation}
Where this constant is the following:

\begin{equation}
\lambda
=
\sqrt{\frac{2 \left( N - 1 \right) k_{B} T}{\sum_{i=1}^{N} m \boldsymbol{v}_{i}^{2}}}
\end{equation}
Thus creating correct initial velocities for the simulation.

\section{Measurable values}
To study various thermodynamic variables numerically, it is advised to choose the different parameters in our equations to be equally $1$. This simplification affects the following values (without their dimension):

\begin{equation}
V_{0} = \sigma = m_{i} = 1
\end{equation}
Where $V_{0}$ and $\sigma$ were already discussed, and $m_{i}$ is the mass of the particles in the system. \par
The different thermodynamic variables mostly could be studied through the Hamiltonian of the system:

\begin{equation}
\mathcal{H}
\equiv
E
=
\frac{m}{2} \sum_{i=1}^{N} \boldsymbol{v}_{i}^{2}
+
\sum_{i \neq j} V \left( \left| \boldsymbol{r}_{i} - \boldsymbol{r}_{j} \right| \right)
\end{equation}
Using this quantity, eg. the molar heat capacity at constant $V$, the pressure, and also the compressibility factor could be studied. \par
The molar heat capacity could be expressed from the fundamental equation of the ideal gas, or also from fluctuation-dissipation theorem as follows:

\begin{equation}
C_{V}
=
\left. \frac{\partial E}{\partial T} \right|_{V}
=
\frac{1}{k_{B} T^{2}} \left[ \left< E^{2} \right> - \left< E \right>^{2} \right]
\end{equation}
Which quantity's dimension is $\frac{J}{K * mol}$ and its value is approximately between $10^{1} - 10^{2}$ naturally for simple gases. The terms in the

\begin{equation}
\left[ \left< E^{2} \right> - \left< E \right>^{2} \right]
\end{equation}
variance could be approximated by the $E$ and $E^{2}$ values' average over time. \par
Both the pressure and compressibility factor could be expressed using the Virial theorem. The pressure could be expressed as follows:

\begin{equation}
PV
=
N k_{B} T + \frac{1}{3} \left< \sum_{i < j} \boldsymbol{r}_{i} * \boldsymbol{F}_{ij} \right>
\end{equation}
While the compressibility factor could be given by

\begin{equation}
Z
=
\frac{PV}{N k_{B} T}
\end{equation}
For ideal gas, it is obviously $Z = 1$, where we get to the well-known ideal gas law. For low pressures this quantity should be $Z < 1$ while for high pressure gases $Z > 1$.

\end{multicols}


    \foldertitle{model}{Model Objects and Functions}{model/Contents}

	Model objects are created by loading a \href{modellang/Contents}{model
file}. Once a model object exists, you can use model functions and
standard Matlab functions to write your own m-files to perform the
desired tasks, such calibrate or estimate the model, find its steady
state, solve and simulate it, produce forecasts, analyse its properties,
and so on.

Model methods:

\paragraph{Constructor}\label{constructor}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{model/model}{\texttt{model}} - Create new model object based on
  model file.
\end{itemize}

\paragraph{Getting information about
models}\label{getting-information-about-models}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{model/addparam}{\texttt{addparam}} - Add model parameters to a
  database (struct).
\item
  \href{model/autocaption}{\texttt{autocaption}} - Create captions for
  graphs of model variables or parameters.
\item
  \href{model/autoexogenise}{\texttt{autoexogenise}} - Get or set
  variable/shock pairs for use in autoexogenised simulation plans.
\item
  \href{model/comment}{\texttt{comment}} - Get or set user comments in
  an IRIS object.
\item
  \href{model/eig}{\texttt{eig}} - Eigenvalues of the transition matrix.
\item
  \href{model/findeqtn}{\texttt{findeqtn}} - Find equations by the
  labels.
\item
  \href{model/findname}{\texttt{findname}} - Find names of variables,
  shocks, or parameters by their descriptors.
\item
  \href{model/get}{\texttt{get}} - Query model object properties.
\item
  \href{model/iscompatible}{\texttt{iscompatible}} - True if two models
  can occur together on the LHS and RHS in an assignment.
\item
  \href{model/islinear}{\texttt{islinear}} - True for models declared as
  linear.
\item
  \href{model/islog}{\texttt{islog}} - True for log-linearised
  variables.
\item
  \href{model/ismissing}{\texttt{ismissing}} - True if some initical
  conditions are missing from input database.
\item
  \href{model/isnan}{\texttt{isnan}} - Check for NaNs in model object.
\item
  \href{model/isname}{\texttt{isname}} - True for valid names of
  variables, parameters, or shocks in model object.
\item
  \href{model/issolved}{\texttt{issolved}} - True if a model solution
  exists.
\item
  \href{model/isstationary}{\texttt{isstationary}} - True if model or
  specified combination of variables is stationary.
\item
  \href{model/length}{\texttt{length}} - Number of alternative
  parameterisations.
\item
  \href{model/omega}{\texttt{omega}} - Get or set the covariance matrix
  of shocks.
\item
  \href{model/sspace}{\texttt{sspace}} - State-space matrices describing
  the model solution.
\item
  \href{model/system}{\texttt{system}} - System matrices for unsolved
  model.
\item
  \href{model/userdata}{\texttt{userdata}} - Get or set user data in an
  IRIS object.
\end{itemize}

\paragraph{Referencing model objects}\label{referencing-model-objects}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{model/subsasgn}{\texttt{subsasgn}} - Subscripted assignment for
  model and systemfit objects.
\item
  \href{model/subsref}{\texttt{subsref}} - Subscripted reference for
  model and systemfit objects.
\end{itemize}

\paragraph{Changing model objects}\label{changing-model-objects}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{model/alter}{\texttt{alter}} - Expand or reduce number of
  alternative parameterisations.
\item
  \href{model/assign}{\texttt{assign}} - Assign parameters, steady
  states, std deviations or cross-correlations.
\item
  \href{model/export}{\texttt{export}} - Save carry-around files on the
  disk.
\item
  \href{model/horzcat}{\texttt{horzcat}} - Combine two compatible model
  objects in one object with multiple parameterisations.
\item
  \href{model/refresh}{\texttt{refresh}} - Refresh dynamic links.
\item
  \href{model/reset}{\texttt{reset}} - Reset specific values within
  model object.
\item
  \href{model/stdscale}{\texttt{stdscale}} - Re-scale all std deviations
  by the same factor.
\item
  \href{model/set}{\texttt{set}} - Change modifiable model object
  property.
\item
  \href{model/single}{\texttt{single}} - Convert solution matrices to
  single precision.
\end{itemize}

\paragraph{Steady state}\label{steady-state}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{model/blazer}{\texttt{blazer}} - Reorder steady-state equations
  into block-recursive structure.
\item
  \href{model/chksstate}{\texttt{chksstate}} - Check if equations hold
  for currently assigned steady-state values.
\item
  \href{model/sstate}{\texttt{sstate}} - Compute steady state or
  balance-growth path of the model.
\item
  \href{model/sstatefile}{\texttt{sstatefile}} - Create a steady-state
  file based on the model object's steady-state equations.
\end{itemize}

\paragraph{Solution, simulation and
forecasting}\label{solution-simulation-and-forecasting}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{model/chkmissing}{\texttt{chkmissing}} - Check for missing
  initial values in simulation database.
\item
  \href{model/diffsrf}{\texttt{diffsrf}} - Differentiate shock response
  functions w.r.t. specified parameters.
\item
  \href{model/expand}{\texttt{expand}} - Compute forward expansion of
  model solution for anticipated shocks.
\item
  \href{model/jforecast}{\texttt{jforecast}} - Forecast with judgmental
  adjustments (conditional forecasts).
\item
  \href{model/icrf}{\texttt{icrf}} - Initial-condition response
  functions.
\item
  \href{model/lhsmrhs}{\texttt{lhsmrhs}} - Evaluate the discrepancy
  between the LHS and RHS for each model equation and given data.
\item
  \href{model/resample}{\texttt{resample}} - Resample from the model
  implied distribution.
\item
  \href{model/reporting}{\texttt{reporting}} - Run reporting equations.
\item
  \href{model/shockplot}{\texttt{shockplot}} - Short-cut for running and
  plotting plain shock simulation.
\item
  \href{model/simulate}{\texttt{simulate}} - Simulate model.
\item
  \href{model/solve}{\texttt{solve}} - Calculate first-order accurate
  solution of the model.
\item
  \href{model/srf}{\texttt{srf}} - Shock response functions.
\end{itemize}

\paragraph{Model data}\label{model-data}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{model/data4lhsmrhs}{\texttt{data4lhsmrhs}} - Prepare data array
  for running \texttt{lhsmrhs}.
\item
  \href{model/emptydb}{\texttt{emptydb}} - Create model-specific
  database with empty tseries for all variables and shocks.
\item
  \href{model/rollback}{\texttt{rollback}} - Prepare database for a
  rollback run of Kalman filter.
\item
  \href{model/sstatedb}{\texttt{sstatedb}} - Create model-specific
  steady-state or balanced-growth-path database.
\item
  \href{model/zerodb}{\texttt{zerodb}} - Create model-specific
  zero-deviation database.
\end{itemize}

\paragraph{Stochastic properties}\label{stochastic-properties}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{model/acf}{\texttt{acf}} - Autocovariance and autocorrelation
  functions for model variables.
\item
  \href{model/ifrf}{\texttt{ifrf}} - Frequency response function to
  shocks.
\item
  \href{model/fevd}{\texttt{fevd}} - Forecast error variance
  decomposition for model variables.
\item
  \href{model/ffrf}{\texttt{ffrf}} - Filter frequency response function
  of transition variables to measurement variables.
\item
  \href{model/fmse}{\texttt{fmse}} - Forecast mean square error
  matrices.
\item
  \href{model/vma}{\texttt{vma}} - Vector moving average representation
  of the model.
\item
  \href{model/xsf}{\texttt{xsf}} - Power spectrum and spectral density
  of model variables.
\end{itemize}

\paragraph{Identification, estimation and
filtering}\label{identification-estimation-and-filtering}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{model/bn}{\texttt{bn}} - Beveridge-Nelson trends.
\item
  \href{model/diffloglik}{\texttt{diffloglik}} - Approximate gradient
  and hessian of log-likelihood function.
\item
  \href{model/estimate}{\texttt{estimate}} - Estimate model parameters
  by optimising selected objective function.
\item
  \href{model/evalsystempriors}{\texttt{evalsystempriors}} - Evaluate
  minus log of system prior density.
\item
  \href{model/filter}{\texttt{filter}} - Kalman smoother and estimator
  of out-of-likelihood parameters.
\item
  \href{model/fisher}{\texttt{fisher}} - Approximate Fisher information
  matrix in frequency domain.
\item
  \href{model/lognormal}{\texttt{lognormal}} - Characteristics of
  log-normal distributions returned by filter of forecast.
\item
  \href{model/loglik}{\texttt{loglik}} - Evaluate minus the
  log-likelihood function in time or frequency domain.
\item
  \href{model/neighbourhood}{\texttt{neighbourhood}} - Evaluate the
  local behaviour of the objective function around the estimated
  parameter values.
\item
  \href{model/regress}{\texttt{regress}} - Centred population regression
  for selected model variables.
\item
  \href{model/VAR}{\texttt{VAR}} - Population VAR for selected model
  variables.
\end{itemize}

\paragraph{Getting on-line help on model
functions}\label{getting-on-line-help-on-model-functions}

\begin{verbatim}
help model
help model/function_name
\end{verbatim}


%\label{sec:statistical_framework}

\subsection{Fitting Framework}

The fitting framework is based on XML Analytic Workspace Builder~\cite{xmlAnaWSBuilder} (xmlAnaWSBuilder)
which is widely used in the Higgs Group. XmlAnaWSBuilder creates RooFit~\cite{RooFit} workspaces using one-dimensional
observables and its workflow of the framework is summarised in Fig.~\ref{fig:xmlAnaWSBuilderworkflow}.

\begin{figure}[htb]
 \centering
\includegraphics[width=0.75\textwidth]{figures/06-StatisticalFramework/xmlAnaWSBuilder_workflow}
\caption{The XmlAnaWSBuilder workflow.  \label{fig:xmlAnaWSBuilderworkflow}}
\end{figure}

The data fitting will use the quickFit framework~\cite{quickFit} with modifications to allow for integrating over binned data.
RooFit evaluates it's fit functions at the centre of each bin rather than at the center of gravity. This leads to significant
biases in the fit results ~\cite{gligorov2021avoiding} which are very large with steeply falling data spectra like the dijet mass.
Recent developments in RooFit have created a new class, \texttt{RooBinSamplingPdf}, can now be used to avoid this problem
which is available in a patch to ROOT v6.23~\cite{RooBinSamplingPdf} and built in the v6.24 release.

\subsection{Background Estimation}

In the resonant search the SM background of the \mjj\ spectrum is determined by a functional fit to the data.
Previous searches, from ATLAS and other experiments (such as Refs.~\cite{Bagnaia:1984ip,PhysRevD.79.112002,EXOT-2010-01,CMS-EXO-10-010,EXOT-2010-07,EXOT-2013-11})
have found that a parametric function of the form
\begin{equation}
  f(x) = p_1 (1 - x)^{p_2} x^{p_3 + p_4\ln x + p_5 (\ln x)^2},
%\label{Eq:fitfunction}% uncomment if label used.
\end{equation}
where $x \equiv \mjj /\sqrt{s}$, accurately describes dijet mass distribution predicted by leading and next-to-leading-order
QCD Monte Carlo. In the ATLAS Run~2 analysis with \integLumi\ of data  \cite{EXOT-2019-03,Nishu:2646455}. the four parameter
version of the function was found to properly describe the QCD background ($p_5 = 0$) function sufficiently described the data.
The introduction of  gluon tagging may require more  parameters to properly describe the full invariant mass spectrum.
\todo[inline]{Is the fifth term $(\ln x)^2$ or $x$? }

To ensure the removal of biases from the creation of pseudo data and as a cross check we require an alternative functional
form which is based on that used by UA2 \cite{Alitti:1990kw, Alitti:1993pn} in its observation of the $W$ and $Z$ decaying
to two jets and subsequent search
\begin{equation}
  f(x) = p_1 x^{p_2} \exp\left({p_3 x + p_4  x^2 }\right).
%\label{Eq:fitfunction2}% uncomment if label used.
\end{equation}


\section{Combining the Generate and Copy Distribution}
\label{code:dists}
\begin{lstlisting}[language=Python]
def _calc_final_dist(self, next_outputs, attn_dists, p_gens, oov_ids, oov_sizes, decoder_vocab_size, batch_size):
  """Calculate the final distribution, for the pointer-generator model

  Args:
    vocab_dists: The vocabulary distributions. (batch_size, vsize)
    attn_dists: The attention distributions. (batch_size, attn_len)
    p_gens : The soft switch weight for copy and generate probabilities
    oov_ids : List of ids of the OOV words in the context
    oov_sizes : Size of each OOV set for each example
    decoder_vocab_size : Size of deocde vocab = vsize
    batch_size : Number of examples in the input

  Returns:
    final_dists: The final distributions. List length max_dec_steps of
     (batch_size, extended_vsize) arrays.
  """
  with tf.variable_scope('final_distribution'):
  	# Obtain Weighted Vocab Distribution
    vocab_dists = tf.nn.softmax(next_outputs.rnn_output)
    vocab_dists = tf.multiply(vocab_dists, p_gens)

    # Obtain Weighted Attention Distribution
    def one_minus_fn(x): return 1 - x
    p_gens = tf.map_fn(one_minus_fn, p_gens)
    attn_dists = tf.multiply(p_gens, attn_dists)

    # Concatenate some zeros to each vocabulary dist, to hold the
     probabilities for in-article OOV words
    max_oov_len = tf.reduce_max(oov_sizes, reduction_indices=[0])
    extended_vsize = decoder_vocab_size + max_oov_len
    extra_zeros = tf.zeros((batch_size, max_oov_len))
    vocab_dists_ext = array_ops.concat([vocab_dists, extra_zeros], axis=1)

    # Create Index which maps each word in the context to a particular cell
    batch_nums = tf.range(0, limit=batch_size)
    batch_nums = tf.expand_dims(batch_nums, 1)
    attention_ids = tf.reshape(oov_ids, [batch_size, -1])
    attn_len = tf.shape(attention_ids)[1]  # number of states we attend over
    batch_nums = tf.tile(batch_nums, [1, attn_len])
    indices = tf.stack((batch_nums, attention_ids), axis=2)

    # We project the attention weights to each index cell using scatter_nd
    shape = [batch_size, extended_vsize]
    attn_dists_projected = tf.scatter_nd(indices, attn_dists, shape)

    # Add the vocab distributions and the copy distributions together to get the final distributions
    final_dists = math_ops.add(vocab_dists_ext, attn_dists_projected)
    return BasicDecoderOutput(final_dists, next_outputs.sample_id)

\end{lstlisting}

\section{Multi-Level Attention Mechanism}
\label{code:attn}
\begin{lstlisting}[language=Python]
def _luong_score(query, keys, scale):
  """Implements Luong-style (multiplicative) scoring function at Sentence Level

  Args:
    query: Tensor, shape `[batch_size, num_units]` to compare to keys.
    keys: Processed memory, shape `[batch_size, max_time, num_units]`.
    scale: Whether to apply a scale to the score function.

  Returns:
    A `[batch_size, max_time]` tensor of unnormalized score values.

  Raises:
    ValueError: If `key` and `query` depths do not match.
  """
  # Check key and query depths
  depth = query.get_shape()[-1]
  key_units = word_keys.get_shape()[-1]
  if depth != key_units:
    raise ValueError("Size of query and key don't match")

  query = array_ops.expand_dims(query, 1)

  # Calculate Attention
  score = math_ops.matmul(query, keys, transpose_b=True)
  score = array_ops.squeeze(score, [1])

  return score

def _luong_word_score(query, word_keys, scale, batch_size):
  """Implements Luong-style (multiplicative) scoring function at Word Level

  Args:
    query: Tensor, shape `[batch_size, num_units]` to compare to keys.
    word_keys: Processed word level memory, shape `[batch_size, max_time, num_units]`.
    scale: Whether to apply a scale to the score function.

  Returns:
    A `[batch_size, max_time]` tensor of unnormalized score values.

  Raises:
    ValueError: If `key` and `query` depths do not match.
  """
  # Check key and query depths
  depth = query.get_shape()[-1]
  key_units = word_keys.get_shape()[-1]
  if depth != key_units:
    raise ValueError("Size of query and key don't match")

  query = array_ops.expand_dims(query, 1)

  # Optimisation to transpose over 3D tensor instead of 4D tensor.
  save_shape = word_keys.get_shape().as_list()
  word_keys_small = tf.reshape(word_keys,  [batch_size, -1, save_shape[2]*save_shape[3]])
  word_keys = tf.transpose(word_keys_small, [1, 0, 2])
  word_keys = tf.reshape(word_keys, [-1, batch_size, save_shape[2], save_shape[3]])

  # Calculate Attention
  context_fn = lambda key: array_ops.squeeze(math_ops.matmul(query, key, transpose_b=True), [1])
  scores = tf.map_fn(context_fn, word_keys)
  scores = tf.transpose(scores, [1, 0, 2])

  return scores

\end{lstlisting}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% app1-terms.tex
% Appendix 1: Derivation of SAR notation.
% This section: Charles Twardy
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Derivation of SAR notation}
\label{sec:terms}

If your background is Search \& Rescue, you may be wondering how these
formulas relate to familiar concepts like POA, POS, and POD.  In the
first part of this appendix, we will derive the function to optimize
from the basic concepts taught in SAR courses. The setup is easy, but
the final steps are messy to write.  In the second part, we will
derive the from basic concepts in conditional probability. That
derivation is nicer, but not nearly as clean as the very abstract
treatment given in Section \ref{sec:form}, which is why these are in
the Appendix. First we define our variables.

\subsection{Background}
\label{sec:Background}

The basic equation governing search probability theory\footnote{There
  is no new material in this section. We are merely laying out search
  theory, as can be found in standard SAR references like
  \cite{frost99:_principles, syrotuck75:_introduction,
    cooper00_adjpoa, cooper00_defs}. However, we
do so in a manner convenient for subsequent derivations.} states that the
probability of success $(POS)$ of any search assignment is equal to
the product of two other probabilities: the probability that the
subject is in the area $(POA)$ and the probability of detecting the
subject if they are in that area $(POD)$.
\begin{equation}
  \label{eq:POS}
  POS = POA \times POD
\end{equation}
For now we shall just accept this. It can be derived from a few simple
considerations about search and Bayes' theorem, but we leave that to
the section on conditional probabilities.

Once an area is searched (without
success), the subject is less likely to be there, in proportion to how
well the area was searched. In other words, after a search, the $POA$ of
the region searched decreases. It can also be shown that the usual
formula is correct:
\begin{equation}
  \label{eq:POA_new}
  POA_{new} = POA_{old} - POS
\end{equation}

It is easy to understand $POA$. Each region has a known area (in
square meters, for example), and a probability of area $POA$, the
probability that the subject is in that area.\footnote{Sometimes
  called the probability of containment, $POC$ since it is the
  probability that the area in question contains the subject.} The
$POA$ is usually determined by a Mattson consensus\footnote{Probably
  from Mattson's 1979 presentation to a NATO conference,
  \cite{mattson80:_estab}.}  (a fancy name for ``average'') from case
histories, known facts, and probable scenarios.  Here we will just
assume that it is given and correct. Often we combine these two items
to define the \emph{probability density}:
\begin{equation}
  \label{eq:2}
  Pden = \frac{POA}{area}
\end{equation}
Sometimes $Pden$ is written as greek letter $\rho$, since it looks
like a funny `p'.

It is a little more involved to get the $POD$ of a resource in an
area. Although search managers usually use various crude estimation
techniques (``If there were 100 milk jugs in the area, how many do you
think you would have found?''), search theory provides a better way to
measure $POD$ based on how much effort was applied, among other things..

First, every resource can move with a certain speed $v$ in a certain
region and at that speed (in those conditions, looking for that kind
of object) have a certain
\emph{effective sweep-width}, usually denoted capital $W$.  $W$ is a
measure of how far that resource can expect to see and detect objects.
There are tables of sweep widths for many resources in many
conditions. We shall presume we have them available.\footnote{Alas,
  there are few or none for ground searchers yet. Until as a community
  we can conduct some experiments, Frost has suggested in-field
  estimation using ``critical separation,'' being the distance
  a  searcher estimates he can see effectively enough to almost always
  spot targets if he were looking that way.}

Together $W$ and $v$ measure how quickly the resource can sweep
areas.\footnote{Indeed, sometimes people define \emph{effective
    sweep-rate}, $ESR = W v$. But we don't need an extra variable.}
We let $t$ be our measure of how much effort (resource-hours) has been
put in the region.

To get $POD$ we need to know how well the region has been
\emph{covered}. \emph{Coverage} is a measure of how thoroughly a given
amount of effort was able to search an area of a certain size. Higher
coverage means higher $POD$. For a given amount of a particular
resource's effort, a bigger area means less coverage. Likewise, for a
given amount of effort in a given area, a resource with a bigger $ESR$
will have more coverage. And finally, the longer we leave the resource
in a region (or the more of it we put there), the more area it sweeps,
and the higher the coverage. So, if we let $x$ be a measure of how
much of the resource's effort we put in an area, we may define a
resource's \emph{coverage}:
\begin{equation}
  \label{eq:C}
  C = \frac{Wvt}{\mathrm{area}}
\end{equation}

And $POD$ must be a function of coverage. It must be such that $POD$
starts at zero when we have zero coverage, and $POD$ can
never exceed 1 no matter how much we cover the area. One function
which can do this is a negative exponential function. But we're not
just picking functions out of the air. It turns out that this is the
proper function for what is called \emph{random search}, and that
almost any real ground search is best modeled by the random-search
curve. \cite{stone89:_optimal} Therefore,
\begin{equation}
  \label{eq:3}
  POD = 1 - e^{-C}
\end{equation}
which can also be written
\begin{equation}
  \label{eq:9}
  POD = 1 - \exp(-C)
\end{equation}
(Since later on we will have many things in the exponent, it will be
easier to read if we use the `exp' form.)

\subsection{SAR notation}
\label{sec:SARnotation}

We have $R$ resources available for searching. Together, they give us
the total amount of available effort, $T$. The effort due to any
resource $r$ is $t_r$. It follows that
\begin{equation}
  \label{eq:10}
  T = \sum_{r=1}^N t_r
\end{equation}

We also have $A$ areas to search. Each area $a$ has a certain
probability of area $POA_a$. Usually all probabilities initially sum
to 1, but since they do not really have to be normalized, the only
constraint is that: $(0 < \sum_{a=1}^A POA_a \leq 1)$.

Our problem is to figure out the best way to allocate all available
effort among the many areas. The effort  $t_r$ from any resource
may be split among any number of areas, but $r$ will always be used
completely, so $\sum_{a=1}^A t_{r,a} = t_r$.

Conversely, several resources may combine their efforts in any one
area. But while there may be many areas left unsearched, we will never
fail to use a resource fully.

Coverage is defined in terms of resources and areas. If $W_{r,a}$ is
the sweep width for resource $r$ in area $a$, and the other variables
likewise, then the coverage in area $a$ by resource $r$ is:

\begin{equation}
  \label{eq:C_ra}
  C_{r,a} = \frac{W_{r,a} v_{r,a}}{A_a} t_{r,a}
\end{equation}

However, because the area sizes and the $W_{r,a}$ and $v_{r,a}$ remain fixed,
rather than continuing to write that entire fraction, we will
just define a new variable lowercase $w_{r,a} = \frac{W_{r,a} v_{r,a}}{A_a}$ and write:
\begin{equation}
  \label{eq:C_ra2}
  C_{r,a} = w_{r,a} t_{r,a}
\end{equation}

Once we know the coverage, we can find the $POD$ from equation \ref{eq:9}.
\begin{equation}
  \label{eq:POD_ra}
  POD_{r,a} = 1 - \exp(-C_{r,a}) = 1 - \exp(-w_{r,a} t_{r,a})
\end{equation}

Now in the background section, we wrote the governing equation of
search theory $POS = POA \times POD$, and the associated equation for
updating the $POA$ of a region after an unsuccessful search:
$POA_{new} = POA_{old} - POS$.

Since the second equation is just subtraction, we can apply the
formula repeatedly, and define the $POA$ for any sortie in terms of
the original $POA$ and the sum of all the $POS$ from all previous
searches. If we number our searches with an index, then the POA for
a search with resource $r$ is:
\begin{equation}
  \label{eq:POA2'}
  POA_{r} = POA_0 - \sum_{i=1}^{r-1} POS_i
\end{equation}

This generalizes quite easily to our notation. Imagine If the initial $POA$
for area $a$ is just $POA_a$, then to compute the current $POA$ facing
our next resource $r$ in area $a$, we sum over the
previously-allocated resources (up to resource $r-1$)\footnote{
  The order in which we consider resources for assignment is
  immaterial: all we want in the end is the right global allocation.
  So we might as well pick the resources straight off the list in
  order $1 \ldots r$. Which is how we have done it here.}:
\begin{equation}
  \label{eq:POA_ra}
  POA_{r,a} = POA_a - \sum_{i=1}^{r-1}POS_{i,a}
\end{equation}

Using the basic identity from Equation \ref{eq:POS} ($POS =
POA \times POD$), we can
rewrite Equation \ref{eq:POA_ra} in terms of products. Skipping the algebra:
\begin{eqnarray}
  \label{eq:15}
  \nonumber POA_{r,a} &=& POA_a \left[(1-POD_{1,a})\cdots(1-POD_{r-1,a})\right]\\
                   & & \\
  \nonumber &=& POA_a \left[\exp(w_{1,a}t_{1,a}) \cdots \exp(w_{r-1,a}t_{r-1,a})\right]\\
  \nonumber &=& POA_a \left[\exp\left(\sum_{i=1}^{r-1}w_{i,a}t_{i,a}\right)\right]
\end{eqnarray}
The second step is just substitution, and the final step is is just
replacing a product of exponents with an exponent of sums. At this point we are almost done.

Putting the $POS_{r,a}$ and the $POA_{r,a}$ together:
\begin{equation}
  \label{eq:POS2_ra}
  POS_{r,a} = POA_{r,a} \cdot POD_{r,a}
\end{equation}
which again comes from equation \ref{eq:POS}.

And, in the end, we want to \emph{maximize} the \emph{total}
$POS$. The total $POS$ is just the sum over both $r$ and $a$. Writing
this out and substituting for $POA_{r,a}$ we find:
\begin{eqnarray}
  \label{eq:POS_tot}
  POS &=& \sum_{a=1}^A \sum_{r=1}^R POS_{r,a}\\
  \nonumber &=& \sum_{a=1}^A POS_{1,a} + POS_{2,a} + \cdots + POS_{R,a}\\
  \nonumber &=& \sum_{a=1}^A \left[
    POA_a POD_{1,a} +
    POA_a \exp(-w_{1,a}t_{1,a}) POD_{2,a} + \cdots +
    POA_a \exp\left(-\sum_{i=1}^{R-1}w_{r,a}t_{r,a}\right) POD_{R,a}
    \right]\\
   &=& \sum_{a=1}^A POA_a \left[
     POD_{1,a} +
     \exp(-w_{1,a}t_{1,a}) POD_{2,a}+ \cdots +
     \exp\left(-\sum_{i=1}^{R-1}w_{r,a}t_{r,a}\right) POD_{R,a}
     \right]
\end{eqnarray}

Finally, if we expand the $POD_{r,a}$ terms we find that many of the
exponents cancel, leaving us with a much simpler form.

\fbox{
  \parbox{6in}{
  \begin{eqnarray}
    \label{eq:pos-final}
    \nonumber POS &=& \sum_{a=1}^A POA_a \left[
      (1-\exp(-w_{1,a}t_{1,a})) +
      \exp(-w_{1,a}t_{1,a}) (1-\exp(-w_{2,a}t_{2,a}))+ \cdots +\right. \\
    & & \left. \exp\left(-\sum_{r=1}^{R-1}w_{r,a}t_{r,a}\right)
      \left(1-\exp(-w_{R,a}t_{r,a})\right)
    \right]\\
    &=& \sum_{a=1}^APOA_a \left[
      1 - \exp\left(-\sum_{r=1}^R w_{r,a}t_{r,a}\right)
    \right]
  \end{eqnarray}
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Conditional Probability Notation}
\label{sec:Cond-Prob-Notat}

The initial probability (before we get new evidence) for anything is
called its \emph{prior probability}. For example, we know from the
start that we will use all of our resources. Therefore the prior
probability for any resource is 1:
\begin{equation}
  \label{eq:p(r)}
  \Pr(R) = 1
\end{equation}

Each area has an initial (prior) probability of containing the
subject, which is usually called the $POA$ in search terms. We will
call is $\Pr(A)$.
\begin{equation}
  \label{eq:p(a)}
  \Pr(A) = POA
\end{equation}

The probability of something given that we know something else is
called the \emph{conditional probability}. For example, we might wish
to know the conditional probability of an area $A$, given that we have
chosen a particular resource $R$. This one is easy: the probability of
an area does not depend upon which resource we use
to search it, so it equals its initial probability:
\begin{equation}
  \label{eq:p(a|r)}
  \Pr(A|R) = P(A)
\end{equation}

For a resource searching in the area containing the subject, the
probability of detecting the subject is given by search
theory\footnote{This is the correct formula for the ``random'' or
  ``exponential'' detection profile, which is shown in \cite{} to be
  the most appropriate model for ground search.}
as $1 - e^{-C}$. Keeping with our notation in the previous section, we
use $t_{r,a}$ to denote the amount of effort from resource $r$ used in
area $a$, and $w_{r,a}$ to denote the effectiveness of the resource in
that area. So:
\begin{equation}
  \label{eq:4}
  \Pr(D|R,A) = 1 - e^{-w_{r,a} t_{r,a}} = 1 - \exp(-w_{r,a} t_{r,a})
\end{equation}

So what is the \emph{posterior} probability of an area? It is the
probability that the subject is in the area given that we have
searched but \emph{not detected} ($\no{D})$ the subject. If we have
$N$ resources, then by Bayes'
theorem (without renormalization):
\begin{eqnarray}
  \label{eq:a|-d}
  \Pr(A|\no{D}) &\propto& \Pr(A) \cdot \Pr(\no{D}|A)\\
  \nonumber     &=& \Pr(A) \cdot \left[\Pr(\no{D}|R_1,A) \ldots
                   P(\no{D}|R_R,A)\right] \\
  \nonumber     &=& \Pr(A) \left[\Pr(\no{D}|R_1,A) \ldots
                   P(\no{D}|R_R,A)\right] \\
  \nonumber     &=& \Pr(A) \left[\exp(-w_{1,a} t_{1,a}) \ldots
                   e^{-w_{R,a} t_{R,a}} \right]\\
  \nonumber     &=& \Pr(A) \exp\left(-\sum_{r=1}^R w_{r,a} t_{r,a}\right)
\end{eqnarray}

That gives us the updated probability for one area. And to get the
total probability that we have \emph{not} detected the subject up
until now, we just sum over the areas. By definition, this is just the
sum over areas of the \emph{joint probability} of being in each area
and being not-detected:
\begin{equation}
  \label{eq:6}
  \Pr(\no{D}) = \sum_A \Pr(\no{D},A)
\end{equation}
But the joint probability of $\no{D}$ and $A$ is by definition:
\begin{equation}
  \label{eq:8}
  \Pr(\no{D},A) =  \Pr(A) \Pr(\no{D}|A)
\end{equation}
which is just the right-hand-side of Equation \ref{eq:a|-d}. We
infer that:
\begin{equation}
  \label{eq:-d|a}
  \Pr(\no{D}|A) = \exp\left(-\sum_{r=1}^R w_{r,a} t_{r,a}\right)
\end{equation}

Of course, in the end we want to maximize our chances of \emph{actually
  detecting} the subject, for the \emph{whole set of
  assignments}. Usually called the ``probability of success,'' this is
really just the overall $\Pr(D)$.

\fbox{
  \parbox{5in}{
    \begin{eqnarray}
      \label{eq:p(d)}
      \nonumber \Pr(D) &=& \sum_A \Pr(A) \Pr(D|A)\\
      \nonumber  &=& \sum_A \Pr(A) \left[ 1 - \Pr(\no{D}|A) \right]\\
      & & \\
      \nonumber  &=& \sum_A \Pr(A) \left[ 1 - \exp\left(-\sum_{r=1}^R w_{r,a}
          t_{r,a}\right) \right]
    \end{eqnarray}
    }
  }

Note that this agrees with Equation \ref{eq:pos-final}, the overall
$POS$ derived using conventional notation.

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "master"
%%% End:


\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usetheme{Malmoe}
\usecolortheme{beaver}
%\usepackage{natbib}
%\bibliographystyle{plainnat}
\bibliographystyle{apalike}   % Or any other style you like
%\bibliographystyle{natbib} % kürzt automatisch vornamen ab und so zeug
%\beamertemplatenavigationsymbolsempty
\beamersetuncovermixins{}{}
%\setbeamercovered{invisible}
%\usepackage{float}
\usepackage{amssymb}
\usepackage{wrapfig}
\usepackage{amsmath}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{graphicx}
%\usepackage{wrapfig}
\usepackage{textcomp}
\usepackage{braket}
\usepackage{bbm}
\usepackage{framed}
\usepackage{ifthen}
\usepackage{bbold}
\usepackage{colortbl}
\usepackage{xifthen}
\usepackage{color}
\usepackage{ifthen}
\usepackage[T1]{fontenc}
\usepackage{amsthm}
\usepackage{bm}
\usepackage{amsbsy}
\usepackage{tikz}
\usepackage{xspace}

\usepackage{xcolor}
\usepackage{scalefnt}
\usepackage{caption}
\addto\captionsngerman{
\renewcommand{\figurename}{Figure}%
\renewcommand{\tablename}{Tab.}%
}
\setlength{\parskip}{1.5ex plus0.5ex minus0.5ex}
\setlength{\parindent}{0em}

\sloppy \frenchspacing \raggedbottom

\usetikzlibrary{trees}
\usetikzlibrary{positioning}
\tikzset{main node/.style={circle,fill=black,draw,minimum size=3pt,inner sep=0pt},}
\tikzset{
    invisible/.style={opacity=0,text opacity=0},
    visible on/.style={alt=#1{}{invisible}},
    alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}
    },
    beameralert/.style={alt={<#1>{fill=red!30,rounded corners}{}},anchor=base},
    BeamerAlert/.style={alt={#1{fill=red!30,rounded corners}{}},anchor=base}
}
  \newcommand<>{\tikzMe}[1]{% previously: \def\tikzMe<#1>#2{…
    \tikz[baseline]\node[BeamerAlert={#2},anchor=base] {#1};
}

%\usetikzlibrary{external}
%\tikzexternalize[prefix=external]
\usepackage{verbatim}

\newcommand{\V}{\ensuremath{\mathbf{V}}\xspace}
\newcommand{\F}{\ensuremath{\mathbf{F}}\xspace}

% Prov makro
\DeclareMathOperator*{\Prov}{Prov}
\DeclareMathOperator*{\Var}{Var}

\DeclareMathOperator*{\Proov}{Proof}

\newcommand{\NP}{\ensuremath{\mathcal{NP}}}
\begin{document}
\title{Formula Evaluation}
\author{Abraham Hinteregger}
\institute{Vienna University of Technology}
\date{2016-06-07}
\titlepage
\setcounter{tocdepth}{3}
\AtBeginSection[]{
\begin{frame}
\frametitle{Chapter}
\tableofcontents[currentsection,currentsubsection,hideothersubsections]
\end{frame}
}

\section{Logic as a game}
\setbeamertemplate{footline}[frame number]

\begin{frame}{Motivation}
\begin{itemize}
\item Given is a formula $\varphi$ in a model $M$ with a variable setting $s$
\item In model-theoretic semantics the question was whether this formula is true in this model with the setting ($M,s \models \varphi$) or not ($M,s\nvDash\varphi$)
\item If one person thinks the formula is true and another person doubts that an obvious game arises:
\begin{itemize}
\item The first person (verifier, \V) tries to verify that the formula is true
\item the second person (falsifier, \F) tries to do the opposite
\end{itemize}
\end{itemize}
\end{frame}

\subsection{Predicate logic}
\begin{frame}\frametitle{First order predicate logic (reminder\footnote{in the spirit of CI})}
\begin{itemize}
\item A formula is built from formulas ($A, B,\ldots$) and operators:
\begin{itemize}
\item Constants $\top,\bot$
\item  Unary negation operator $\lnot A$
\item Binary operators $\land, \lor$: $A \circ B$
\item Quantifiers $\exists, \forall$: $\exists xA(x), \forall x B(x)$
\item Predicates, e.g. $A(x), B(x,y)$
\end{itemize}
\item A model is a set of objects, also called the universe/domain
\begin{itemize}
\item Constants are mapped to an object in the domain
\item Functions map one (or more) objects in the domain to another object in the domain
\item Predicates are mapped to a subset of the domain
\end{itemize}
\end{itemize}
\end{frame}

\begin{frame}\frametitle{Evaluation games for predicate logic}
\begin{tabular}{ll}
atoms  $P(x), R(x,y),\top,\bot$ &  \V wins if atom is true, else \F wins\\
disjunction $\varphi \lor \psi$ & \V chooses which disjunct to play\\
conjunction   $\varphi \land \psi$ & \F chooses which conjunct to play\\
negation  $ \lnot \varphi$ & role switch of the two players\\\\\hline\\
existential quantifier $\exists x\varphi(x)$ & \V  picks an object $d$ from the domain\\
universal quantifier $\forall x \varphi(x)$& \F picks an object $d$ from the domain
\end{tabular}
\end{frame}

\subsection{Modal logic}
\begin{frame}{Evaluation games for modal logic}
\begin{itemize}
\item Rules from evaluation games for predicate logic
\item Additional rules that accomodate for the modal operators $\Box$ and $\Diamond$ (or indexed versions $\Box_i, \Diamond_i$):
\begin{tabular}{ll}\\
necessity $\Box P$ & \F chooses a successor of the current world\\
possibility $\Diamond P$ & \V chooses a successor of the current world\\\\
\end{tabular}
Failure to choose a successor means a loss for either player
\item Game state consists not only of setting $s$ and current formula $\varphi$ but also the current world $w$.

\end{itemize}
\end{frame}

%\subsection{Examples}
\begin{frame}\frametitle{Example of evaluation games for predicate logic}
    {
\uncover<2->{(domain consists of two objects, $s$ and $t$)}
\begin{tikzpicture}[grow=down, sloped]
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=2.5cm, sibling distance=6cm]
\tikzstyle{level 2}=[level distance=2.5cm, sibling distance=3cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]
\uncover<2>{

\node[bag] {\F\\ ${\forall x \exists y\ x\neq y}$}
    child[opacity=0] {
	node[bag] {\V\\$\exists y s \neq y$}
		child {
			node[end, label=right:{$lose_V; s\neq s$}] {}
			edge from parent node[above] {$y:=s$}
		}
		child {
			node[end, label=right:{$win_\V; s\neq t$}] {}
			edge from parent node[above] {$y:=t$}
		}
	edge from parent node[above] {x:=s}
    }
    child[opacity=0] {
        node[bag] {\V\\${\exists y t\neq y}$}
        child {
                node[end, label=right:{$win_\V; s\neq t$}] {}
                edge from parent node[above] {$y:=s$}
            }
            child {
                node[end, label=right:{$lose_\V; t\neq t$}] {}
                edge from parent node[above] {$y:=t$}
            }
        edge from parent
            node[above] {$x:=t$}
    };}
\uncover<3>{

\node[bag] {\F\\ ${\forall x \exists y\ x\neq y}$}
    child {
	node[bag] {\V\\$\exists y\ s \neq y$}
		child[opacity=0] {
			node[end, label=right:{$lose_V; s\neq s$}] {}
			edge from parent node[above] {$y:=s$}
		}
		child[opacity=0] {
			node[end, label=right:{$win_\V; s\neq t$}] {}
			edge from parent node[above] {$y:=t$}
		}
	edge from parent node[above] {x:=s}
    }
    child {
        node[bag] {\V\\${\exists y\ t\neq y}$}
        child[opacity=0] {
                node[end, label=right:{$win_\V; s\neq t$}] {}
                edge from parent node[above] {$y:=s$}
            }
            child[opacity=0] {
                node[end, label=right:{$lose_\V; t\neq t$}] {}
                edge from parent node[above] {$y:=t$}
            }
        edge from parent
            node[above] {$x:=t$}
    };}
\uncover<4>{

\node[bag] {\F\\ ${\forall x \exists y\ x\neq y}$}
    child {
	node[bag] {\V\\$\exists y\ s \neq y$}
		child {
			node[bag, label=below:{$lose_\V$}] {$s\neq s$}
			edge from parent node[above] {$y:=s$}
		}
		child {
			node[bag, label=below:{$win_\V$}] {$s\neq t$}
			edge from parent node[above] {$y:=t$}
		}
	edge from parent node[above] {x:=s}
    }
    child {
        node[bag] {\V\\${\exists y\ t\neq y}$}
        child {
                node[bag, label=below:{$win_\V$}] {$t\neq s$}
                edge from parent node[above] {$y:=s$}
            }
            child {
                node[bag, label=below:{$lose_\V$}] {$t\neq t$}
                edge from parent node[above] {$y:=t$}
            }
        edge from parent
            node[above] {$x:=t$}
    };}
\end{tikzpicture}
}
\end{frame}

%\subsection{Examples}
\begin{frame}\frametitle{Example of evaluation game for modal logic}

 \begin{columns}[T]
    \begin{column}{.5\textwidth} {

\begin{itemize}
\item If \V chooses world $3$ \F can't move and loses
\item Else \V  can choose either world 1 or 2 and wins no matter what \F does
\end{itemize}

\begin{tikzpicture}[grow=down, sloped]

 \node[main node,label=left:{1 $p$}] (1) {};
    \node[main node,label=left:{3}] (3) [below of = 1]  {};
    \node[main node,label=right:{2 $p$}] (2) [right = 1.5cm of 1] {};
    \node[main node,label=right:{4}] (4) [below of = 2] {};
     \path[-latex,draw,thick]    (2) edge node {} (1);
     \path[-latex,draw,thick]    (1) edge node {} (3);
     \path[-latex,draw,thick]    (4) edge node {} (2);
     \path[-latex,draw,thick]    (1) edge node {} (4);
     \path[-latex,draw,thick]    (4) edge [out=300,in=240,looseness=30] node {}(4);

\uncover<1>{
%	\draw[green, very thick](1) circle(2pt);
	\path[green,  thick, -latex, draw](1) edge node{} (4);
	\path[green,  thick, -latex, draw](1) edge node{} (3);
};

\uncover<2>{
	\draw[red, very thick](3) circle(2pt);
};

\uncover<3-4>{
%	\draw[green, very thick](4) circle(2pt);
	\path[green,  thick, -latex, draw](4) edge[out=300,in=240,looseness=30] node{} (4);
	\path[green,  thick, -latex, draw](4) edge node{} (2);
};

\uncover<5>{
%	\draw[green, very thick](2) circle(2pt);
	\path[green,  thick, -latex, draw](2) edge node{} (1);
};

%ugly hacks follow
\node[opacity=0](y) [above = 0.5cm of 1]{}; % some spacing above
\node[opacity=0](x) [left = 1.4cm of 1]{}; % for centering
\end{tikzpicture}
}
\end{column}

    \begin{column}{.5\textwidth}
  %  \begin{block}
  {
\begin{tikzpicture}[grow=down, sloped]
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=1.5cm, sibling distance=3cm]
\tikzstyle{level 2}=[level distance=1.5cm, sibling distance=2.5cm]
\tikzstyle{level 3}=[level distance=1.5cm, sibling distance=1.5cm]
% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

\node[beameralert=1,bag] { ${\V\Diamond\Box\Diamond p,1}\quad $}
    child {node[beameralert=2,bag,label=below:{$lose_\F$}] {\F $\Box\Diamond p, 3$}}
    child {node[beameralert=3,bag] { ${\F\ \Box\Diamond p, 4}$}
	child {node[beameralert=4,bag] {{$\V \Diamond p, 4$}}
		child {node[bag, label=below:{$lose_\V$}] {$p, 4$}}
		child {node[bag, label=below:{$win_\V$}] {$p, 2$}}
	}
	child {node[beameralert=5,bag] {$\V \Diamond p, 2$}
		child {node[bag, label=below:{$win_\V$}] {$p, 1 $}}
          }
   };

\end{tikzpicture}
}
    %\end{block}
    \end{column}

\end{columns}
\end{frame}

\begin{frame}
\frametitle{Some observations}
\begin{itemize}[<+->]
\item Both players can choose between two options (assigning either $s$ or $t$ to $x,y$ or deciding a successor for the current world)
\item Both players can win and lose but only \V does influence the outcome of the games (\F cannot even force a loss)
\item \V has a winning strategy (``don't assign the same thing as \F'', ``go to world 1 or 2'')
\item In the second game \V has even two strategies for winning (``pick 3'', ``pick 4 and then pick either 2 or 1'')
\end{itemize}
\end{frame}

%\section{Properties}
\subsection{Determinacy}
\begin{frame}{Determinacy\footnote{Hintikka referred to this as ``determinateness'' \cite{hintikka}}}
%Player A has a winning strategy if he can react to any move the opponent plays with at least one move s.t. A wins the game
\begin{block}{Success Lemma}
$M,s\models \varphi \Longleftrightarrow \text{\V has a winning strategy in $game(\varphi,M,s)$}$\\
$M,s\not\models \varphi \Longleftrightarrow \text{\F has a winning strategy in $game(\varphi,M,s)$}$

Proof by induction on formulas:
\begin{itemize}
%\item For atoms it obviously holds
\item If $v_M(\varphi \lor \psi) = 1 $: w.l.o.g. $v_{M}(\varphi) = 1$. By inductive hypothesis \V has a winning strategy for $game(M,s,\varphi)$ and thus also for $game(M,s,\varphi\lor\psi)$
\item If $v_M(\lnot \varphi) = 1$: $\implies v_M(\varphi) = 0$ and by IH \F has a winning strategy for $game(M,s,\varphi)$. Player switch yields a winning strategy for \V for $game(M,s,\lnot \varphi)$.
\item \ldots
\end{itemize}
\end{block}
\end{frame}

\begin{frame}{Determinacy II}
\begin{itemize}
\item Complexity of formula strictly decreases as game continues until only atomic formula is left
\item At every branching the active player has a winning strategy if there is a winning strategy available for at least one of the branches
\item As this propagates to the root there's a winning strategy for at least (and at most) one of the two players.
\item This theory of truth coincides with Tarski's truth definition
\end{itemize}
\end{frame}

%\begin{frame}{Determinacy III}
%\begin{itemize}
%\item<1-> Truth in model theoretic semantics corresponds 1:1 to existence of winning strategy for either player
%\item<2-> Great\ldots \uncover<3->{ but rather unexciting}
%\item<3-> Is it possible to extend this?
%\begin{itemize}
%\item<4-> One winning strategy versus more of them
%\item<4-> Losing intentionally
%\end{itemize}
%\end{itemize}
%\end{frame}

\section{Game view of logic \& extensions}
\subsection{Formal definition of the game}
\begin{frame}{Inductive definition of the game}
A game ``$game(M,s,\varphi)$'' is defined as a tree where every node is a pair $(s,\psi)$ where $s$ is an $M$-assignment and $\psi$ is a subformula of $\varphi$.\\
\begin{tabular}{c| l}\hline
$\varphi$ & Interpretation of $game(M,s,\varphi)$\\\hline
atomic & one node game where \V wins iff $M,s\models\varphi$\\
$\exists x$ &game where \V picks any available move $s[x:=d]$ and wins
\\\\
$\phi \lor \psi$ & game is disjoint union of two games and it's \V's turn\\
$\phi \land \psi$ & same as above but it's \F's turn\\
$\lnot \phi$ & $game(M,s,\phi)$ with win markings reversed\\\\
$\phi; \psi$ & Game arising by taking $game(M,s,\phi)$ with assignment $t$ \\&at end states and continuing with $game(M,t,\psi)$
\end{tabular}
\end{frame}

\begin{frame}{Extended syntax of formulas}
The changed quantifier and composition rules allow formulas such as:
\begin{itemize}[<+->]
\item $\exists x$: \V chooses new assignment $s[x:=d]$ and wins.
\item $P(x); \exists x$: Test whether $P(s(x))$ holds and then assign a new value to $x$.
\end{itemize}
\end{frame}

\subsection{Additional moves}
\begin{frame}{Additional moves\footnote{Chapter 16, \cite{lig}}}
Until now the model $M$ was fixed and remained unchanged. The game could be extended by allowing moves that manipulate the model in some way:
\begin{itemize}
\item Adding or removing objects from the domain
\item Changing the interpretation
\end{itemize}
\end{frame}

\subsection{More refined semantics}
\begin{frame}{Refined semantics\footnote{Chapter 15, \cite{lig}}}
\begin{itemize}
\item Difference between the existence of one winning strategy and many of them.
\item How many moves does a strategy need to win?
\item Is it possible to lose on purpose?
\end{itemize}
\end{frame}

%\begin{frame}
%\cite{sep-logic-games}
%\cite{sep-logic-dialogical}
%\cite{lig}
%\cite{insightshintikka}
%\end{frame}

\section{Stuff}
\subsection{References}
\begin{frame}[allowframebreaks]{References}
\nocite{*} %load everything from log.bib
\bibliography{log}
\end{frame}

\subsection{Modal $\mu$-calculus}
\begin{frame}\frametitle{Modal $\mu$-calculus\footnote{\cite{venema}}}
Adds two additional operators to propositional (multi-) modal logic:
\begin{itemize}
\item Least fixpoint operator $\mu p: \varphi(p)$
\item Greatest fixpoint operator $\nu p: \varphi(p)$
\end{itemize}
Note that $p$ occurs positively in $\varphi(p)$, meaning that there's an even amount of $\lnot$ in front of every occurence of $p$ in $\varphi$\footnote{The positive syntactic occurence of $p$ implies monotonicity concerning inclusion $\implies$ least and greatest fixpoint exist \cite{knaster}.}
\end{frame}

\begin{frame}{Examples for modal $\mu$-calculus}
\begin{tabular}{l l}
Formula & Interpretation\\\hline
\uncover<+->{$\mu p : (q \lor \Diamond p)$ & \uncover<+->{Set of all worlds $w$ where a world $v$ s.t. $M,v\models q$ \\&is reachable with \alert<7>{finite} path}} \\\\
\uncover<+->{$\nu p : (q \land \Box_a p)$ & \uncover<+->{Set of all worlds $w$ where $M,w\models q$ on \alert<7>{every} $a$-path}}\\\\
\uncover<+->{$\nu p : (\Diamond \top \land \Box p)$ & \uncover<+->{Set of all worlds that have outgoing transitions\\& and don't have a path to a world without\\& outgoing transitions}}\\\\
\end{tabular}
\uncover<8->{$\nu$ means unfolding, $\mu$ means finite unfolding}
\end{frame}

\subsection{Evaluation game for $\mu$-calculus}
\begin{frame}{Evaluation game for $\mu$-calculus}
\begin{itemize}
\item Rules from modal evaluation game
\item If fixed point formula $\mu p : \varphi(p)$ or $\nu p : \varphi(p)$ is reached, the game proceeds with $\varphi(p)$
\item<2-> $p$ is not an atom but a bound variable and instead of testing the atom the original fixed point formula is substituted back in.
\item<3-> If the evaluation loops (node visited multiple times) \V wins if the infinitely many times substituted variable is bound to a $\nu$- formula and \F wins if it's bound to a $\mu$- formula\footnote{if there is more than one such variable the one with highest rank (contains the others as subformulas) counts}
\end{itemize}
\end{frame}

\end{document}

2016-06-13 - Formula Evaluation/FormulaEvaluation.tex


\section{Program gto\char`_genomic\char`_period}
The \texttt{gto\char`_genomic\char`_period} calculates the best order depth of a sequence, using FCMs. It only works "ACGT", while the rest will be discarded.\\
This application has a dependency to represent the results. It requires the Gnuplot to show the execution result.\\
For help type:
\begin{lstlisting}
./gto_genomic_period -h
\end{lstlisting}
In the following subsections, we explain the input and output paramters.

\subsection*{Input parameters}

The \texttt{gto\char`_genomic\char`_period} program needs program needs two streams for the computation, namely the input and output standard. The input stream is a sequence file.\\
The attribution is given according to:
\begin{lstlisting}
Usage: ./gto_genomic_period [options] [[--] args]
   or: ./gto_genomic_period [options]

It calculates the best order depth of a sequence, using FCMs.It only works "ACGT",
while the rest will be discarded.

    -h, --help        show this help message and exit

Basic options
    < input.seq       Input sequence file format (stdin)
    > output          Output is given by log_2(4)*K(x)/|x|) (stdout)

Example: ./gto_genomic_period < input.seq > output
\end{lstlisting}
An example on such an input file is:
\begin{lstlisting}
TCTTTACTCGCGCGTTGGAGAAATACAATAGTGCGGCTCTGTCTCCTTATGAAGTCAACAATTTCGCTGGGACTTGCGGC
TCTTTACTCGCGCGTTGGAGAAATACAATAGTGCGGCTCTGTCTCCTTATGAAGTCAACAATTTCGCTGGGACTTGCGGC
GACTTCATCGTGGTCTCTGTCATTATGCGCTCCAACGCATAACTTTGCGCCAGAAGATAGATAGAATGGTGTAAGAAACT
GTAATATATATAATGAACTTCGGCGAGTCTGTGGAGTTTTTGTTGCATTAGAGAGCCAAGAGGTCGGACGTCCTCACGTA
GCCCGAGACGGGCAGGGCGATGGCGACTGAACGGGCTCCATATCACTTTGAGCTTTTATGCTTTCGACTCCTCCAGGAGC
TGAACAACCTTGTTCCCGGCAAAGCCCACTGCGTCATGGAGCTCACGGTCTACATTCATGACTGACTAACCGTAAACTGC
\end{lstlisting}

\subsection*{Output}
The output of the \texttt{gto\char`_genomic\char`_period} program is a execution report, followed by the plot with this information.\\
Using the input above, an report example for this is the following:
\begin{lstlisting}
Running order: 1 ... Done!
Running order: 2 ... Done!
Running order: 3 ... Done!
Running order: 4 ... Done!
Running order: 5 ... Done!
Running order: 6 ... Done!
Running order: 7 ... Done!
Running order: 8 ... Done!
Running order: 9 ... Done!
Running order: 10 ... Done!
Running order: 11 ... Done!
Running order: 12 ... Done!
Running order: 13 ... Done!
Running order: 14 ... Done!
Running order: 15 ... Done!
Running order: 16 ... Done!
Running order: 17 ... Done!
Running order: 18 ... Done!
Running order: 19 ... Done!
Running order: 20 ... Done!
 1	2.246
 2	2.225
 3	2.237
 4	2.079
 5	1.821
 6	1.733
 7	1.717
 8	1.708
 9	1.717
10	1.712
11	1.717
12	1.721
13	1.725
14	1.729
15	1.733
16	1.738
17	1.742
18	1.746
19	1.75
20	1.754
\end{lstlisting}

In the Figure~\ref{fig:gtoGenomicPeriod} is represented the plot for the execution above.

 \begin{figure}[!h]
  \centering
  \includegraphics[scale=0.7]{./images/gto_genomic_period.png}
  \caption{\texttt{gto\char`_genomic\char`_period} execution plot.}
  \label{fig:gtoGenomicPeriod}
 \end{figure}


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

\chapter{The particle physics Standard Model}

\marginpar{Wednesday\\ 2020-3-11, \\ compiled \\ \today}

For this lecture, a good reference is Peskin, chapter 2 \cite{peskinConceptsElementaryParticle2019}.

We wish to describe what we described yesterday as ``matter'' and ``radiation''.

The problem is similar to the one we have in classical mechanics, an initial value problem: given the positions and velocities of the particles at a certain starting time \(t_0 \) we wish to compute their state at a later time \(t\).

This classical description in which the particles are not wavelike fails at the microscopic level: we want to give a quantum description of such a system of particles.
We will derive it from the classical description using the standard tools of quantization.
We start with a refresher of the classical description.

\section{The classical description of a system of particles}

Our aim is to compute and solve the equations of motion.
The usual approach is to use Hamilton's variational principle: it is the principle of least action, but it is not usually referred to as such: we are actually not \emph{minimizing} the action but finding a \emph{stationary point} for it.
This could also be a maximum or a saddle point.

The action functional \(S\) depends on the coordinates \(q_i(t)\) of the particles at at time \(t\), on the derivatives of these positions \(\dot{q}_{i}(t)\) which represent the velocities of the particles at a time \(t\).
We usually write \(S \qty[q_{i}(t), \dot{q}_{i}(t)]\).

If we fix \(q(t_0 )\) and \(q(t_{f})\), the positions at some initial and final time \(t_f\), we can then trace out a path \(q(t)\) and perturb it by \(\delta q(t)\); we fix \(\delta q(t_0 ) = \delta q(t_f) = 0\).

Under this perturbation of the path \(q \rightarrow q + \delta q\), the action changes to \(S \rightarrow S + \delta S\).
We then ask that \(\delta S =0\).

\(S\) is an action: is dimensions are those of an energy times a time.
In terms of the Lagrangian \(L\), the action is defined as
%
\begin{align}
S \qty[q_i(t), \dot{q}_{i}(t)] = \int_{t_0 }^{t_f} L (q_i (t), \dot{q}_{i}(t)) \dd{t}
\,,
\end{align}
%
which means that the Lagrangian must have the dimensions of an energy.

Moving on from classical mechanics to classical field theory, we will make use of a quantity called the Lagrangian density \(\mathscr{L}\), such that we recover the Lagrangian as:
%
\begin{align}
L = \int \mathscr{L} \qty(\phi (\vec{x}), \partial_{\mu } \phi (\vec{x})) \dd[3]{x}
\,.
\end{align}

From a finite number of particles we move to considering a field \(\phi (\vec{x})\): this means that, in a certain sense, we are considering an ``infinite number of particles'', the values of the field at each point in space.

The dependence of the Lagrangian on the \(q_i\) and \(\dot{q}_{i}\) shifted to a dependence on the spacetime coordinates \(x\) and their 4-derivatives \(\partial_{\mu } x\).
It could depend on many fields simultaneously, we omit this dependence for simplicity.
Now, this Lagrangian density has the dimensions of an energy per unit volume.

Then, the action, computed in a region \(\Omega \) of 4-dimensional spacetime, is
%
\begin{align}
S = \int_{\Omega } \dd[4]{x} \mathscr{L} \qty(\phi (x), \partial_{\mu } (\phi (x)))
\,.
\end{align}

Now that we have established the notation, we can apply the action principle: we consider an infinitesimal variation of the field \(\phi \rightarrow \phi  + \delta \phi \).
We require this variation to vanish not only at the initial and final time, but over all the boundary \(\partial \Omega \):
%
\begin{align}
\eval{\delta \phi }_{\partial \Omega } =0
\,.
\end{align}

Then, it can be shown with an integration by parts that imposing \(\delta S =0\) in the region \(\Omega \) is equivalent to the Euler-Lagrange equations:
%
\begin{align}
\pdv{\mathscr{L}}{\phi (x)} - \pdv{}{x^{\mu }} \qty(\pdv{\mathscr{L}}{\qty(\partial_{\mu} \phi (x))}) = 0
\,.
\end{align}

If we have many fields \(\phi_{r}\), then we have a set of E-L equations for each of them.
This is still classical: for example, classical (relativistic) electrodynamics is formulated in this way.

In order to write the Hamiltonian formulation of the theory we need the momenta, which are
%
\begin{align}
\pi (x) = \pdv{\mathscr{L}}{\dot{\phi} (x)}
\,,
\end{align}
%
where a dot denotes a time derivative.
Using these we define the Hamiltonian density by
%
\begin{align}
\mathscr{H} (x) = \pi (x) \dot{\phi} (x) - \mathscr{L}(\phi, \partial_{\mu }\phi )
\,,
\end{align}
%
and similarly to the Lagrangian we have the full Hamiltonian \(H\)
%
\begin{align}
H = \int \dd[3]{x} \mathscr{H}
\,.
\end{align}

Now, as we quantize the theory the fields (\(\phi \) and \(\pi \)) go from classical fields to Heisenberg-picture \textbf{operators}.
As classical fields, \(\phi \) and \(\pi \) satisfy Poisson bracket relations:
%
\begin{align}
\qty{\phi (\vec{x}, t), \pi (\vec{x}', t)} &= \delta^{(3)} (\vec{x}, \vec{x}')
\qty{\phi (\vec{x}, t), \phi  (\vec{x}', t)} &= 0 =
\qty{\pi (\vec{x}, t), \pi  (\vec{x}', t)}
\,.
\end{align}

The operatorial version, instead, will need to satisfy the classical commutation relations, which are found by substituting the Poisson bracket with a commutator divided by \(i \hbar\) \cite[section 2.4.2]{tissinoTheoreticalPhysicsNotes2020}:
%
\begin{align}
\qty{\phi , \pi } \to \frac{1}{i \hbar} \qty[\hat{\phi}, \hat{\pi}]
\,.
\end{align}

So, we get the following:
%
\begin{subequations}
\begin{align}
\qty[\phi (\vec{x}, t), \pi (\vec{x}', t)] &= i \hbar \delta (\vec{x} - \vec{x}')  \\
\qty[\phi (\vec{x}, t), \phi  (\vec{x}', t)] &=
\qty[\pi (\vec{x}, t), \pi  (\vec{x}', t)] = 0
\,.
\end{align}
\end{subequations}

Note that the commutation relations are computed \emph{at equal time}.

\subsection{A relativistic reminder}

The energy-momentum four-vector is
%
\begin{subequations}
\begin{align}
p^{\mu } = \left[\begin{array}{c}
E \\ \vec{p} c
\end{array}\right]
\,,
\end{align}
\end{subequations}
%
where the greek index \(\mu \) can take values from 0 to 3. A four-vector is an element of the tangent bundle to the spacetime manifold; concretely speaking, under a coordinate transformation which is locally linear and represented by a Lorentz matrix \(\tensor{\Lambda }{^{\mu }_{\nu }}\) a four-vector transforms as \(p^{\mu } \to \tensor{\Lambda }{^{\mu }_{\nu }} p^{\nu } \).

The metric signature used here is the the mostly minus one. So, \(p^{\mu } q_{\mu } = E_{p} E_{q} - \vec{p} \cdot \vec{q}\), since we raise and lower indices using the metric \(\eta_{\mu \nu }\).

The square norm of the 4-momentum is \(p \cdot p  = p^2 = E^2 - \abs{\vec{p}}^2 c^2\). It is Lorentz invariant.

In the rest frame of the observer, \(p^{\mu } = \qty[E_{0}, \vec{0}]\), and this \(E_0\) is just (\(c^2\) times)
 the mass of the particle: this is the \emph{definition} of mass.

When the relation is satisfied we have
%
\begin{subequations}
\begin{align}
p^2 &= E^2 - \abs{p}^2 c^2= \qty(mc^2)^2  \\
E &= c \sqrt{\abs{p}^2 + (mc)^2}
\,.
\end{align}
\end{subequations}

% \todo[inline]{There is an extra \(c\) in the notes.}

When this relation is satisfied we say we are \emph{on shell}: for virtual particles, instead, this might not be satisfied. This is allowed because virtual particles are described in a \emph{quantum} theory: because of the uncertainty principle, we have uncertainty in energy if we consider short times and uncertainty in momentum if we consider small position intervals.
This uncertainty means that we cannot enforce the on-shell condition as an exact equality if we are considering processes on these small scales, such as those which we find in quantum field theory.

We will use natural units: \(\hbar = c = 1\).

This means that we equate energies (\SI{}{eV}) and angular velocities (\SI{}{Hz}); also we equate times (\SI{}{s}) and lengths (\SI{}{m}).

The rest energy of the electron is \(m_e \approx \SI{511}{keV}\).
Let us consider an electron with a momentum \(p\) equal to its mass \(m_e\): then, its uncertainty in position is of the order
%
\begin{align}
\frac{\hbar}{p c} = \frac{1}{m_e} \approx \SI{4e-11}{cm}
\,.
\end{align}

The dimensions of the lagrangian density, in natural units, are those of an energy to the fourth power, or a length to the \(-4\), or a mass to the fourth.

Another useful exercise is to calculate the coupling of the electromagnetic field:
%
\begin{align}
V(r) = \frac{e^2}{4 \pi \epsilon_{0} r} = \frac{e^2}{4 \pi } \frac{1}{r}
\,,
\end{align}
%
since we set \(\epsilon_{0} = \mu_0 = 1\).
We can introduce the electromagnetic \(\alpha \): this is
%
\begin{align}
\alpha = \frac{e^2}{4 \pi } \times \frac{1}{\hbar c}
\,.
\end{align}

This then becomes adimensional: \(\alpha \approx 1 / 137\).
It represents the strength of the electromagnetic interaction: the strength of the coupling of the photon to the electron.
The fact that it is \(\sim \num{e-2}\) is important: it allows us to work in a perturbative way, in powers of \(\alpha \).

What is the coupling of the strong and weak interactions? This will be discussed.

Next time, we will discuss symmetries and symmetry breaking.

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage{algpseudocode}

\title{Edit Distance Calculations}
\author{Aakash Sur, Shawn Sullivan, William Noble, Peter Myler}
\date{August 2021}

\begin{document}

\maketitle

\section{Introduction}
Evaluating the performance of scaffolding algorithms has remained challenging. As sub-problem in the genome assembly field, it borrows heavily from metrics designed for use on de novo assemblies such as N50 and number of gaps. However these metrics can be easily gamed by simply concatenating the assembly together. On the other hand, reference guided approaches to examine how closely a scaffolding mirrors a known reference often rely on visualizations such as dot plots and circle plots. These in turn fail to provide a definitive value on scaffolding performance, hampering efforts to benchmark and evaluate scaffolding algorithms.

Let an assembly \(A\) be a set of scaffolds, which in turn are ordered sets of contigs. Let a contig \(c\) represent an indivisible unit of contiguous sequence. The goal of a scaffolding algorithm \(f\) is to take the set of contigs \(C\) and produce a set of scaffolds \(A\). Since DNA is orientated, each contig in a given scaffold has a order and an orientation associated with it. To evaluate a scaffolding algorithm, we can use contigs associated with a high-quality reference genome \(A_r\) that is known to be scaffolded in the correctly. Then by evaluating \(f(C)\), we can produce \(A_s\) and determine how closely it matches the reference.

Fundamentally, we are interested in the edit distance between \(A_r\) and \(A_s\). This is the number edits we would have to make to \(A_s\) so that is equivalent to \(A_r\). Much work was conducted on this topic in the late 1990s and early 2000s, and one of the more elegant formulations of the edit function was the Double Cut and Join model. In 2005, Yancopoulos defined this operation which greatly simplified calculating edit distances and in 2006, Bergeron provided  another simplification to the DCJ model by utilizing an adjacency graph. Below, we describe the implementation of their algorithm.
Finally, there are three subtasks we will evaluate - grouping, ordering, and orientation. Grouping represents an algorithms ability to correctly group together contigs which belong in the same scaffold. Ordering is the ability to correctly order contigs, and orientation is the ability to correctly orient them given that they are in the correct relative order.

\section{Edit Distance}

Describe the DCJ algorithm and show the length weighted version of it.

\subsection{Grouping}

We can evaluate the grouping accuracy by comparing how closely the set of scaffolds from the assembly match the set of scaffolds from the reference.

Since we are interested in the overlap of elements from scaffolds compared to the total set of elements, we will compute the Jaccard index -

\[J(s_i, s_j) = \frac{|s_i \cap s_j|}{|s_i \cup s_j|} \]

Then for each scaffold \(s_{r_{i}}\) in the reference there exists a scaffold \(s_{a_{j}}\) in the assembly which maximizes the Jaccard index. We compute this max Jaccard index, and then average the results for each scaffold \(s_{r_{i}}\) to find the mean grouping accuracy.

\begin{algorithm}
\caption{Grouping}\label{alg:cap}
\begin{algorithmic}
\For{$s_{r_{i}}$}
    \For{$s_{a_{j}}$}
        \State{compute \(J(s_{r_{i}}, s_{a_{j}})\)}
    \EndFor
    \State{store \(max \:J(s_{r_{i}}, s_{a_{j}})\)}
\EndFor
\State {\textbf{return} mean \(max \:J(s_{r_{i}}, s_{a_{j}})\)}
\end{algorithmic}
\end{algorithm}

\subsection{Ordering}
To evaluate the number of correctly ordered contigs, we need to compute the set of edges and find the intersection between the assembly and the reference. An edge can be described as a set containing two contigs which are contiguous \(\{c_i, c_j\}\). If we compute the full set of edges \(E\) , then the ordering accuracy is simply -

\[Ordering(A_s, A_r) = \frac{|E_s \cap E_r|}{|E_r|}\]

\subsection{Orientation}
In a similar fashion, we can compute the orientation accuracy if we construct our edge set to also retain information about orientation, \(\{c_{i_h}, c_{j_t}\}\).

\end{document}


\subsection{Quaternions}


We compared two different base classification models to analyze their ability to learn from these small handshape datasets; Prototypical Networks \cite{protonet} and DenseNet \cite{densenet}. Prototypical Networks is a model that was designed explicitly to deal with small sample sizes. On the other hand, DenseNet is currently a state of the art model in image classification with convolutional neural networks, and while it has not been explicitly designed for small datasets, it has shown exceptional performance in many different tasks.

In the case of DenseNet, we also experiment using Model-Agnostic Meta-Learning (MAML) \cite{DBLP:journals/corr/FinnAL17} and Transfer Learning \cite{tan2018} training techniques, in addition to the traditional training process.

Transfer Learning is a well known technique to jump-start the training of neural networks for a problem A using datasets from a similar problem B. The weights of a network trained on B are used as initial weights in the training of the network for A. Retraining the network for A is called finetuning, and may retrain only a subset of the weights of the network. However, it still may require large amounts of data for the finetuning phase.

Finally, MAML is a meta-learning technique for few-shot learning, that involves learning subtasks. In this context, each subtask corresponds

In the following subsections we describe in more detail each of these models.

\subsubsection{Wide-DenseNet}

We selected a DenseNet based architecture as it is a state of the art model in many domains and can handle small datasets with low error rate \cite{pmlr-v80-pham18a}.

DenseNet \cite{densenet} works by concatenating the feature-maps of a convolutional block to the feature-maps of all the previous convolutional blocks and using the resulting set of feature-maps as input for the next convolutional block. In this way, each convolutional block receives all the collective knowledge of the previous layers maintaining the global state of the network which can be accessed.

We employed a variation on DenseNet called Wide-DenseNet which follows the strategy used by wide residual networks \cite{He2015DeepRL}. Wide-DenseNet consists on decreasing the depth of the network and increasing the width of the layers. This way the model can be trained faster by optimizing feature reuse and obtain higher accuracy.

Additionally, we use Squeeze and Excitation blocks (SE blocks) \cite{Hu2017SqueezeandExcitationN} to improve the performance of the DenseNet model. Convolutional networks construct informative features by fusion both spatial and chanel-wise information within local receptive fields at each layer. SE blocks focus on  channel-wise information, improving the quality of the representations produced by the network by modeling the interdependency between channels to perform feature recalibration. SE blocks can be included in any model that uses convolutional layers to improve its performance at low computational cost. We use SE blocks between dense and transition blocks, see figure \ref{fig:densenet}.

\begin{figure*}[!ht]
    \centerline{\includegraphics[width=0.85\textwidth]{images/background/densenet.png}}
    \caption{DenseNet using 2 dense blocks and SE blocks.}
    \label{fig:densenet}
\end{figure*}

\subsubsection{Transfer Learning} \label{models:tl}

Gathering new training data for deep neural networks can be an expensive and time consuming task. Transfer learning provides a way to utilize already available data from a source domain and transfer the acquired knowledge from this source domain to a target domain. By doing transfer learning we can obtain much better initializations of the parameters of the model before training in the target domain.

In the past, transfer learning has been used for handshapes, sign language and gesture recognition \cite{farhadi2007}\cite{quiroga2017study}\cite{allard2017} demonstrating the advantages of this technique.

In this work,  we are doing Network-based \cite{tan2018} transfer learning. In this type of transfer learning a part of the network pretrained on the source domain is reused for the training in the target domain. The objective is for the neural network to acquire abstract information from the source domain and transfer this knowledge to the target task.

To obtain a good performance from transfer learning it is commonly needed for the source dataset to be larger than the target dataset. Since the information extracted from the target dataset has a higher value than the information from the source dataset, the data from the target dataset will be more helpful in fitting the target task. In addition to this, it is important for the source domain to be well related to the target domain. If the relation between both domains differ too much it is possible to get a negative transfer which can diminish the performance obtained by using transfer learning to the point of obtaining less performance by applying it \cite{weiss2016}.

\subsubsection{Model-Agnostic Meta-Learning} \label{sec:models:maml}

Like Prototypical Networks, Model-Agnostic Meta-Learning (MAML) \cite{DBLP:journals/corr/FinnAL17} is a technique designed to tackle the problem of few-shot learning. MAML learns how to improve a model so that it can learn a new task in only a few steps by training on many different tasks, commonly phrased as learning to learn. MAML does this by learning over multiple tasks and updating the parameters of the models based on the improvement obtained after training on each task.

More formally, given a set of tasks $T$ each consisting of a loss function $L$ and a set of elements with their corresponding labels. MAML requires a distribution over tasks $p(T)$ that we want to adapt to. Given those distributions, we proceed with the next two steps, task training and meta training. In the task training we sample $K$ tasks. For each task $T_i$, the model is trained on a set of elements extracted from the task distribution using the loss function $L_i$ belonging to $T_i$. With the updated parameters the model is then tested on new data from $T_i$. Once tested on each task the loss obtained from these tests will be added and utilized as loss for the initial model on the meta training obtaining a new initial model with better initial parameters that will grant a bigger improvement for each task on fewer steps.

We made some modifications on the original MAML to work with bigger datasets in a supervised way. Each task $T_i$ is split in 2 subsets, a training subset $Tt$ and a meta training subset $Tm$. The subsets are composed of datasets $D={x,y}$ where $x$ is an image and $y$ the label of that image. Each subset has an equal size $b$ and its labels are mirrored $y\in Tt \iff y\in Tm$. Each dataset with size $n$ has $n/2b$ tasks $T$. We consider our model as a function $f_\theta$ with parameters $\theta$. In each training iteration $\theta$ will change to $\theta'$. Each iterations consists of 2 steps, a training and a meta training step. In the training step we start by storing the value of $\theta$ in $\theta'$, then $\theta$ is updated to fit $Tt_i$. In the meta training step the new $\theta$ is used to calculate the gradients with $Tm_i$ : $\nabla L_i(  f_\theta(Tm_i))$ and these gradients are applied to $\theta'$ finishing the iteration.

\begin{algorithm}[!h]
\caption{Model-Agnostic Meta-Learning for Few-Shot Supervised Learning}
\SetAlgoLined
\KwIn{A set of tasks T }
 Initialize $\theta$\;
 \While{not done}{
  \For{$T_i$ in $T$}{
   Save the parameters $\theta$ in $\theta'$\;
   Train $\theta$ using the training subset: $\theta = \theta -  \alpha \nabla L_i( f_\theta(Tt_i))$ \;
   Get the gradients using the meta training subset: $\nabla L_i(  f_\theta(Tm_i))$ \;
   Apply the gradients to $\theta'$: $\theta' = \theta' - \alpha \nabla L_i( f_\theta(Tm_i))$\;
  }
 }
 Train $f_\theta$ in the objective dataset with less samples
\end{algorithm}

\subsubsection{Prototypical Networks}
\label{models:protonet}

Prototypical Networks \cite{protonet} is a meta-learning model for the problem of few-shot classification, where a classifier must generalize to new classes not seen in the training set, given only a small number of examples of each new class. The ability of an algorithm to perform few-shot learning is typically measured by its performance on n-shot, k-way classification tasks. First a model is given a set of query samples Q belonging to a new, previously unseen class. Then, it receives a support set, S, consisting of n examples, each from k different unseen classes. Finally, the algorithm has to determine the classes of Q, given the samples of S, see figure \ref{fig:protonet:handshape}.

\begin{figure}[!ht]
    \centerline{\includegraphics[width=1.0\columnwidth]{images/background/protonets.png}}
    \caption{Prototypical Networks given a set of query samples and support set}
    \label{fig:protonet:handshape}
\end{figure}

Prototypical Networks apply an inductive bias in the form of class prototypes to achieve impressive few-shot performance. The key assumption is the existence of an embedding in which samples from each class cluster around a single prototypical representation which is simply the mean of the individual samples. This idea streamlines n-shot classification in the case of $n > 1$ as classification is simply selecting the label of the closest class prototype, see figure \ref{fig:protonet}.

Schemes for few shot classification tasks like Prototypical Networks can also be of use for training small datasets.

\begin{figure}[!ht]
    \centerline{\includegraphics[width=1.15\columnwidth]{images/background/prototypical-networks.png}}
    \caption{Prototypical networks in the few-shot and zero-shot scenarios. \textbf{Left}: Few-shot prototypes
    $\mathbf{c}_k$ are computed as the mean of embedded support examples for each class. \textbf{Right}: Zero-shot prototypes $\mathbf{c}_k$ are produced by embedding class meta-data $\mathbf{v}_k$. In either case, embedded query points are classified via a softmax over distances to class prototypes.}
    \label{fig:protonet}
\end{figure}