\subsection{Definition}
As the name implies, pseudo-random number generation (PRNG) does not produce a truly random, indeterminate value. The exact nature of this pseudo-randomness varies between languages and implementations, but essentially, PRNG is accomplished via an algorithm that uses mathematical formulae or precalculated tables to produce numerical values that appear random.\autocite{20210517:haahr}

However, a common characteristic of all methods of PRNG is that they are deterministic, meaning that they require an initial seed in the form of another numerical value, and that, if the seed is known and replicated exactly, the PRNG will produce the exact same results multiple times. These seeds can take the form of literal values or variable representation of other, unrelated values, such as system time.

Another property of PRNG is that the values it generates are periodic, meaning that, due to the finite amount of pseudo-random values a given implementation can generate, the output will eventually begin to repeat.

\subsection{Examples}
Most common programming languages have built-in PRNG implementations. The method \texttt{rand(}), defined in the C Standard General Utilities Library, is perhaps the most basic PRNG function in the C family, using a seed, provided by the developer as an argument to an earlier call to the method \texttt{srand()}, to return an integer value between zero and the language-defined constant \texttt{RAND\_MAX}.\autocite{20210517:cpp-rand} C++11 expands upon these PRNG capabilities in its \texttt{<random>} header, which introduces numerous PRNG implementations, from uniform distributors to algorithmic implementations of various mathematical distributions.\autocite{20210517:cpp-random}

Likewise, Java implements a \textit{random} series of methods, found in the \texttt{Random} class of the \texttt{java\-.\-util\-.\-Random} object, which function similarly to the C \texttt{rand()} function but can be specified to accept as a seeds and output values of other numeric data types.\autocite{20210517:java}

\subsection{Issues}
Because of the deterministic and periodic factors, the use of PRNG in a security context is typically discouraged, as the requirement of a defined seed and the finite amount of possible outcomes means that, even if an attacker is not aware of the initial condition, a supposedly random value can be realistically guessed by the use of brute force alone. Still, PRNG has its place in such contexts, as it is generally considered significantly more efficient and practical to implement than true random number generation (TRNG), as TRNG extracts randomness from captured aspects of physical phenomena, such as radioactive decay or atmospheric noise, which most commercially available computer hardware is simply not equipped to do.

An example of a use of PRNG in an identification and authentication scenario is the creation of a temporary session ID for a user. The viability of this implementation is entirely dependent on the source of the seed value used in the PRNG algorithm. One hypothetical source is a given user’s authentication ID, but this value is the same each time the user logs in. Another option is that of the system time in Unix Time format, which, on one hand, is guaranteed to change each second. On the other hand, an attacker could replicate a Unix Time string corresponding with a date and time at which any given user is likely to be authenticating (say, a weekday at around 9:00\textsc{AM}), and, with enough luck, could potentially spoof the session ID of an active, verified user simply by correctly guessing what time they logged on, a tactic that only increases in viability the more users a system supports.

From a software assurance perspective, in the event of scenario similar in scope to the previous example, reported issues will usually adhere to a vulnerability description enumerated as CWE-337 or CWE-338,\autocites{20210517:cwe-337}{20210517:cwe-338} the former with respect to the use of the seeded value and the latter with respect to the PRNG function itself. Occasionally, both will occur concurrently for the same instance.

\subsection{Mitigations}
In general, the larger and more arbitrary the seed, the less likely the likely the PRNG implementation can be exploited by an attacker. Or, at least, it would result in exploitation attempts taking longer to accomplish, thus increasing the chance of the attack being noticed before the attacker succeeds. Regardless, the very presence of a PRNG implementation is overwhelmingly likely to be documented as an issue by a scan tool, but the security of such of an implementation can still be ensured through a multilayered approach tailored to fit the specific context in which the pseudo-random value is being used.

In an authentication context, imposing a limit to the number of times a user can unsuccessfully supply valid credentials and/or placing a restriction on subsequent attempts can mitigate attacks based around exploiting PRNG to replicate a valid set of credentials or the privileges attached to a valid set of credentials.

In a cryptographic context, the use of algorithms whose PRNG has been sufficiently tested and independently verified is the best way to mitigate potential issues. A list of random number generators approved for use in cryptographic modules can be found in Annex C of the FIPS 140-2 publication from the Information Technology Laboratory of the National Institute of Standards and Technology.\autocite{20210517:annexc}


\section{Variational updates derived from gradient descent optimisation}

In this section we will show how the variational updates that were found via calculus of variations in section XX are equivalent to the updates derived from gradient descent optimisation.
%This is part of the derivation of the variational updates for stochastic variational inference in section XX. \\

The original derivation can be found in \cite{hoffmann}. Here we present simplified version for completeness. For consistency and with the notation presented in \cite{hoffmann}, we will generalise all probability distributions to the exponential family form. See section X for details and nomenclature.\\

As a starting point, we classify the variables of the model into four different types:
\begin{itemize}
	\itemsep-1.5em
	\item observations: $N$ different vectors $\bfy_{n}$ which contain the observed variables. \\
	\item local variables: $N$ different vectors $\bfz_{n}$ which contain all $K$ hidden variables associated with each sample $n$. \\
	\item global variables: a vector $\bbeta$ which contains all $B$ hidden variables not indexed by $n$. \\
	\item parameters: a vector $\balpha$ which contains all fixed parameters for the global variables.
\end{itemize}
The distinction between local and global variables lies on the conditional dependencies. Given the global variables $\beta$, the $n$th local variable $z_n$ is conditionally independent from any other observation $y_{j}$ or local variable $z_{j}$ (where $j \neq n$):
\[
p(\bfy_n,\bfz_n| \bfy_j,\bfz_j,\bbeta,\balpha) = p(\bfy_n,\bfz_n|\bbeta,\balpha)
\]
%As an example, in the MOFA model the factors belong to the local variables whereas the weights belong to the global variables.

As in Equation XXX, the observations are assumed to be independent, which leads to a factorised likelihood:
\[
p(\bfY,\bfZ,\beta,\alpha) = p(\beta|\alpha) \prod_{n=1}^{N} p(\bfy_n,\bfz_n|\beta)
\]

Additionally, to obtain closed-form variational updates, we need to assume that the complete conditionals of the hidden variables are members of the exponential family:
\baln
p(\beta_b|\bfY,\bfZ,\alpha) = h(\beta_b) \exp\{ \eta_g(\bfY,\bfZ,\balpha)^T t(\beta_b) - a_g(\eta_g(\bfY,\bfZ,\alpha)) \} \\
p(z_{nk}|y_{nj},z_{nj},\bbeta) = h(\beta) \exp\{ \eta_l(y_{nj}, z_{nj},\bbeta)^T t(z_{nk}) - a_g(\eta_l(y_{nj},z_{nj},\bbeta)) \} \\
\ealn
where $\lambda_b$ are the parameters governing the global variable $\beta_b$. Similarly, $\phi_{nk}$ are the parameters governing the local variable $z_{nk}$. In some cases, this assumpion results naturally from the choice of conjugated likelihood and prior distributions (section XXX). Yet, even in the case of non-conjugated distributions, some approaches introduce local approximations to the likelihood to achieve conjugacy. See section XXX.\\

To set the inference framework, variational distributions are introduced for both the local variables and the global variables. As in XX, following the mean-field assumption, the variational distributions factorise:
\[
q(\bfZ,\bbeta) = \prod_{b=1}^{B} q(\beta_b,\lambda_b) \prod_{n=1}^{N}\prod_{k=1}^{K} p(z_{nk}|\phi_{nk})
\]

So far no assumptions were made regarding the nature of the probability distributions. To derive the gradient descent coordinate update, we need to assume exponential family distributions for the variational distributions:
\baln
q(\beta_b|\lambda_b) &= h(\beta_b) \exp\{ \lambda_b^T t(\beta_b) - a_g(\lambda_b) \} \\
q(z_{nk}|\phi_{nk}) &= h(z_{nk}) \exp \{ \phi_{nk}^T t(z_{nk}) - a_l(z_{nk}) \}
\ealn

From the assumptions above, the ELBO factorises as:
\baln
\Lagr &= \E_q[\log p(\bfY,\bfZ,\bbeta)] - \E_q[\log q(\bfZ,\bbeta)]  \\
&= \E_q[\log p(\bfY,\bfZ,\bbeta)] - \sum_{b=1}^{B}\E_{q(\beta_b)}[\log q(\beta_b)] - \sum_{n=1}^{N}\sum_{k=1}^{K}  \E_{q(z_{nk})}[\log q(z_{nk})] \\
\ealn

\subsubsection{Computing the gradients}
Equation X  contains the objective function. First we derive the updates for the global parameters. As a function of $\lambda$, the ELBO becomes:
\baln
	\Lagr(\lambda) &= \E_{q(Z,\beta)}[\log p(\beta|\bfY,\bfZ)] - \E_{q(\beta)}[\log q(\beta)] + \const \\
	&= \E_{q(Z,\beta)}[\eta_g(\bfY,\bfZ,\alpha)^T t(\beta)] - \E_{q(\beta)}[\lambda^T t(\beta) - a_g(\lambda) ] + \const \\
	&= \E_{q(Z)}[\eta_g(\bfY,\bfZ,\alpha)^T] \nabla a_g(\lambda) - \lambda^T \nabla a_g(\lambda) - a_g(\lambda) + \const
\ealn
where we have used the exponential family identity $\E_{q(\beta)}[t(\beta)] = \nabla a_g(\lambda)$. Taking the gradient with respect to $\lambda$ leads to the solution:
\[
	\lambda = \E_{q(Z)}[\eta_g(\bfY,\bfZ,\alpha)]
\]

Turning to the local parameters, as a function of $\phi_{nk}$ the ELBO becomes:
THIS IS WRONG, CHECK EXPECTATIONS
\baln
\Lagr(\phi_{nk}) &= \E_{q(\beta,z_{nk})}[\log p(z_{nk}|\bfy_{n},\bfz_{nj}, \beta)] - \E_{q(z_{nk})}[\log q(z_{nk})] + \const \\
&= \E_{q(\beta,z_{nk})}[\eta_l(\bfy_n,\bfz_{nj},\beta)^T t(\bfz_{nj})] - \E_{q(z_{nk})}[\phi_{nk}^T t(z_{nk}) - a_l(\phi_{nk}) ] + \const \\
%&= \E_{q(\beta,z_{nk})}[\eta_l(\bfy_n,\bfz_{nj},\beta)^T t(\bfz_{nj})] - \E_{q(z_{nk})}[\phi_{nk}^T t(z_{nk}) - a_l(\phi_{nk}) ] + \const
\ealn
where $j$ denotes all except the $k$-th local variable associated with sample $n$.

Taking the gradient with respect to $\phi_{nk}$ leads to the following solution:
\[
\phi_{nk} = \E_{q(\beta,\bfz_{n,j})}[\eta_l(\bfy_{n},\bfz_{n,j},\beta)]
\]
Equation XX and XX define the variational updates for the gradient ascent algorithm.

\subsubsection{Equivalency with calculus of variations}
TO-DO


\documentclass[12pt]{amsart}
\usepackage{geometry} % see geometry.pdf on how to lay out the page. There's lots.
\usepackage{bsymb}
\usepackage{unitb}
\usepackage{calculational}
\usepackage{ulem}
\usepackage{hyperref}
\normalem
\geometry{a4paper} % or letter or a5paper or ... etc
% \geometry{landscape} % rotated page geometry

% See the ``Article customise'' template for some common customisations

\title{}
\author{}
\date{} % delete this line to display the current date

%%% BEGIN DOCUMENT
\setcounter{tocdepth}{4}
\begin{document}

\maketitle
\tableofcontents

\newcommand{\Train}{\text{TRAIN}}
\newcommand{\Blk}{\text{BLK}}

\section{Initial model}
\begin{machine}{m0}
\input{train-station-set/machine_m0}
\begin{align*}
	\false \tag{default} \label{default}
\end{align*}

\newset{\Train}

\with{sets}

\begin{align*}
\variable{	in : \set [ \Train ]}
\end{align*}

\begin{align*}
\dummy{	t : \Train}
\end{align*}

\newevent{m0:enter}{enter}
\newevent{m0:leave}{leave}

\begin{align*}
\indices{m0:leave}{	t : \Train}
\end{align*}
\begin{align*}
\indices{m0:enter}{	t : \Train}
\end{align*}

% \removecoarse{m0:leave}{default} % \weakento{m0:leave}{default}{lv:c0}

\begin{align*}
\cschedule{m0:leave}{lv:c0}
	{	t &\in in } \\
\evassignment{m0:leave}{lv:a0}
	{	in' &= in \setminus \{ t \} } \\
\evassignment{m0:enter}{a1}
	{	in' &= in \bunion \{ t \} }
\end{align*}

\begin{align*}
&\progress{m0:prog0}
	{	t \in in }{ \neg t \in in }
\refine{m0:prog0}{discharge}{m0:tr0}{}
&\transientB{m0:leave}{m0:tr0}{ \index{t}{t' = t} }
	{	t \in in }
\end{align*}

\end{machine}

\section{First refinement}
\begin{machine}{m1}

\refines{m0}

\newset{\Blk}

% \with{sets}
\with{functions}

\begin{align*}
\variable{	loc : \Train \pfun \Blk}
\end{align*}

\begin{align*}
\\ \initialization{in1}
	{ in = \emptyset }
\end{align*}

\begin{align*}
\invariant{inv0}
	{	\dom.loc = in }
\end{align*}

\begin{align*}
\constant{	ent,ext : \Blk} ; \quad
\constant{	plf : \set [ \Blk ]}
\end{align*}

\subsection{New requirements}
\begin{align*}
\safety{m1:saf0}
	{ \neg t \in in& }{ t \in in \land loc.t = ent }
\\ \safety{m1:saf1}
	{ t \in in \land loc.t = ent& }{ t \in in \land loc.t \in plf }
\\ \safety{m1:saf2}
	{ t \in in \land loc.t \in plf& }{ t \in in \land loc.t = ext }
\\ \safety{m1:saf3}
	{ t \in in \land loc.t = ext& }{ \neg t \in in }
\end{align*}

\subsection{Proofs}

\subsubsection{Invariant \ref{inv0}}

\begin{align*}
\evassignment{m0:leave}{lv:a2}
	{ loc' &= \{ t \} \domsub loc }
\\ \evassignment{m0:enter}{a3}
	{ loc' &= loc \1| t \fun ent }
\\ \initialization{in0}
	{ loc \, &= \emptyfun }
\end{align*}

\subsubsection{Safety \ref{m1:saf1}, \ref{m1:saf2}}

This takes care of \eqref{m1:saf2}:

\begin{align*}
\evguard{m0:leave}{lv:grd1}
	{ loc.t = ext }
\\ \evguard{m0:enter}{ent:grd1}
	{ \neg t \in in }
\end{align*}

This takes care of \eqref{m1:saf1}
\newcommand{\compl}{-}
\begin{align*}
\evguard{m0:leave}{lv:grd0}
	{ t \in in }
\\ \assumption{asm0}
	{ \neg ext \in plf \1\land \neg ext = ent } \\
%
\assumption{asm7}{ (\{ ext \} \bunion plf = \compl \{ent\}) \land (\{ ent \} \bunion plf = \compl \{ext\}) \land \{ ext, ent \} = \compl plf }
\end{align*}
\subsubsection{Side conditions}
In order to take care of the schedulability of \ref{m0:leave}, we need to strengthen the coarse schedule by adding \ref{c1}. The side conditions for schedule replacement requires us to prove \ref{m1:prog0} and \ref{m1:saf3} in order to prove refinement.
\replace{m0:leave}{lv:c1}{m1:prog0}{m1:saf3}

\begin{align*}
\cschedule{m0:leave}{lv:c1}
	{ loc.t = ext }
\\ \progress{m1:prog0}
	{ t \in in }{ t \in in \land loc.t = ext }
\end{align*}

The first step in implementing \eqref{m1:prog0} is to break it down in a few cases. The property says ``a train inside the station eventually reaches the exit''. Now, we're going to break the predicate ``the train is inside the station'' into ``the train is at the entrance'', ``the train is at a platform'' and ``the train is at the exit'', as described by the following assumption:
\hide{
	\begin{align*}
	\dummy{		b : \Blk}
	\end{align*}
}
\begin{align*}
\assumption{asm1}
{	\qforall{b}{}{ b \in \Blk \2\equiv b \in plf \1\lor b = ent \1\lor b = ext }	}
\end{align*}

Which allows us to appy \emph{disjunction} to \eqref{m1:prog0}.

\begin{align*}
& { t \in in }\3\mapsto{ t \in in \land loc.t = ext }
\refine{m1:prog0}{disjunction}{m1:prog1,m1:prog2,m1:prog3}{}
& \progress{m1:prog2}
	{ t \in in \land loc.t = ext }{ t \in in \land loc.t = ext }
\\ & \progress{m1:prog1}
	{ t \in in \land loc.t = ent }{ t \in in \land loc.t = ext }
\\ & \progress{m1:prog3}
	{ t \in in \land loc.t \in plf }{ t \in in \land loc.t = ext }
\end{align*}
%
\hide{ \refine{m1:prog2}{implication}{}{} } %
%
\eqref{m1:prog2} is true by implication. This leaves us \eqref{m1:prog1} and \eqref{m1:prog3}. \eqref{m1:prog1} says that a train at the entrance reaches the exit. However, \eqref{m1:saf1} says that a train can only leave the entrance through a platform:

\begin{align*}
	& { t \in in \land loc.t = ent }\3\mapsto{ t \in in \land loc.t = ext } \tag{\ref{m1:prog1}}
\refine{m1:prog1}{transitivity}{m1:prog4,m1:prog3}{}
& \progress{m1:prog4}
	{ t \in in \land loc.t = ent }{ t \in in \land loc.t \in plf }
\\ & { t \in in \land loc.t \in plf }\3\mapsto{ t \in in \land loc.t = ext } \tag{\ref{m1:prog3}}
\end{align*}

We introduce new events to satisfy \eqref{m1:prog3} and \eqref{m1:prog4}
\newevent{m1:movein}{move\_in}
\newevent{m1:moveout}{move\_out}
\hide{
	\refine{m1:prog3}{discharge}{m1:tr0,m1:saf2}{}
	\refine{m1:prog4}{discharge}{m1:tr1,m1:saf1}{}
}
\begin{align*}
& \transientB{m1:movein}{m1:tr1}{ \index{t}{t' = t} }
	{ t \in in \1\land loc.t = ent }
\\ & \transientB{m1:moveout}{m1:tr0}{ \index{t}{t' = t} }
	{ t \in in \1\land loc.t \in plf }
\\ & \evguard{m1:movein}{mi:grd7}
	{ b \in plf }
\end{align*}
\subsubsection{New events}

\begin{align*}
\indices{m1:moveout}{	t : \Train}
\end{align*}

We adjust \ref{m1:moveout} to satisfy \ref{m1:tr0}.

% \removecoarse{m1:moveout}{default} % \weakento{m1:moveout}{default}{c1}{}

\begin{align*}
\evassignment{m1:moveout}{a2}{ loc' = loc \1 | t \fun ext }
\\ \assumption{asm2}
	{ \qexists{b}{}{b \in plf} }
\\ \cschedule{m1:moveout}{c1}{ t \in in \land loc.t \in plf }
\\ \evguard{m1:moveout}{mo:g1}{ t \in in }
\\ \evguard{m1:moveout}{mo:g2}{ loc.t \in plf }
\end{align*}

We adjust \ref{m1:movein} to satisfy \ref{m1:tr1}.

% \removecoarse{m1:movein}{default} % \weakento{m1:movein}{default}{mi:c1,mi:c2}{}

\begin{align*}
\indices{m1:movein}{	t : \Train} ; \quad
\param{m1:movein}{ b : \Blk }
\end{align*}

\begin{align*}
\evassignment{m1:movein}{mi:a2}
	{ loc' &= loc \1| t \fun b }
\\ \assumption{asm3}
	{ \neg ent &\in plf }
\\ \cschedule{m1:movein}{mi:c1}{ t &\in in }
\\ \cschedule{m1:movein}{mi:c2}{ loc.t &= ent }
\\ \evguard{m1:movein}{mi:g1}{ t &\in in }
\\ \evguard{m1:movein}{mi:grd0}
	{ loc.t &= ent } % m1:saf3
\end{align*}
%
\end{machine}

\section{Second refinement}

\begin{machine}{m2}

\refines{m1}

\begin{align*}
\dummy{	t_0,t_1 : \Train}
\end{align*}
\newcommand{\injective}{\text{injective}}
\subsection{New Requirement}
\begin{align*}
\invariant{m2:inv0}
	{	\injective.loc } % \qforall{t_0,t_1}{t_0 \in in \land t_1 \in in \land loc.t_0 = loc.t_1}{t_0 = t_1}
\end{align*}
%
\subsection{Design}
%
\replace{m1:movein}{mi:c0}{m2:prog0}{m2:saf0}
\begin{align*}
\progress{m2:prog0}
	{\true}
	{\neg plf \subseteq \ran.loc  }
% \\ \safetyB{m2:saf0}{m1:movein}{ \neg ~ plf \subseteq \ran.loc }{\false} % \2{\textbf{except}} \text{\ref{m1:movein}}
\end{align*}

%The above property, \eqref{m2:saf0}, should not discharge automatically.

\begin{align*}
\evguard{m1:movein}{mi:g0}
	{	 \neg b \in \ran.loc  	}
\\ \cschedule{m1:movein}{mi:c0}
	{	\neg ~ plf \subseteq \ran.loc 	}
%\\ \evguard{m1:movein}{mi:g3}
%	{ \qforall{t}{t \in in}{ \neg loc.t = b} }
\\ \evguard{m0:enter}{et:g1}
	{	\neg ent \in \ran.loc }
\\ \evguard{m1:moveout}{mo:g3}
	{	\neg ext \in \ran.loc 	}
\end{align*}
%
\begin{proof}{\ref{m0:leave}/INV/\ref{m2:inv0}}
\[ \assert{goal0}{\qforall{t_0,t_1}{t_0 \in in' \land t_1 \in in' \land loc'.t_0 = loc'.t_1}{t_0 = t_1}	} \]
\begin{subproof}{goal0} \begin{free:var}{t_0}{t_0}
\begin{free:var}{t_1}{t_1}
	\begin{align}
	\assume{hyp0}{\neg t_0 = t_1}
	\hide{
		\\ \assume{hyp1}{ t_0 \in in' }
		\\ \assume{hyp2}{ t_1 \in in' }}
	\\ \assert{hyp5}{ t_0 \in in }
	\\ \assert{hyp6}{ t_1 \in in }
	\\ \assert{hyp3}{ \neg t_0 = t } % \label{hyp7}
	\\ \assert{hyp4}{ \neg t_1 = t } % \label{hyp8}
	\\ \goal{\neg (loc'.t_0 = loc'.t_1)} \notag
	\end{align}
	\hide{
		\assert{hyp7}{ t_0 \in \dom.loc \setminus \{ t \} }
		\assert{hyp8}{ t_1 \in \dom.loc \setminus \{ t \} }

	\begin{calculation}
		loc'.t_0 = loc'.t_1
	\hint{=}{ \ref{lv:a2} \ref{m2:inv0} }
		(\{ t \} \domsub loc).t_0 = (\{ t \} \domsub loc).t_1
	\hint{=}{ \hide{ \eqref{hyp7} and \eqref{hyp8} }
			\igeqref{hyp3} and \igeqref{hyp4} }
		loc.t_0 = loc.t_1
	\hint{=}{ \eqref{hyp5}, \eqref{hyp6},
			\eqref{hyp0} with \ref{m2:inv0}
			}
		\false
	\end{calculation} }
	\hide
	{	\begin{subproof}{hyp3} \easy \end{subproof}
		\begin{subproof}{hyp4} \easy \end{subproof}
		\begin{subproof}{hyp5} \easy \end{subproof}
		\begin{subproof}{hyp6} \easy \end{subproof}
		}
	\hide{
		\begin{subproof}{hyp7}
		\begin{calculation}
			t_0 \in \dom.loc \setminus \{ t \}
		\hint{=}{ \ref{inv0} }
			t_0 \in in \setminus \{ t \}
		\hint{=}{ \ref{lv:a0} }
			t_0 \in in'
		\hint{=}{ \eqref{hyp1} }
			\true
		\end{calculation}
		\end{subproof}
		\begin{subproof}{hyp8}
		\begin{calculation}
			t_1 \in \dom.loc \setminus \{ t \}
		\hint{=}{ \ref{inv0} }
			t_1 \in in \setminus \{ t \}
		\hint{=}{ \ref{lv:a0} }
			t_1 \in in'
		\hint{=}{ \ref{hyp2} }
			\true
		\end{calculation}
		\end{subproof}
		}
\end{free:var}
\end{free:var} \end{subproof} \easy
\end{proof}

\replacefine{m1:moveout}{m2:prog1}

\begin{align*}
\fschedule{m1:moveout}{mo:f0}{ \neg ext \in \ran.loc }
\\ \progress{m2:prog1}{\true}{ \neg ext \in \ran.loc }
\end{align*}

\begin{align*}
	& \true \2\mapsto \neg ext \in \ran. loc
\refine{m2:prog1}{trading}{m2:prog2}{}
	& \progress{m2:prog2}
		{ext \in \ran. loc }
		{ \neg ext \in \ran. loc }
\refine{m2:prog2}{disjunction}{m2:prog3}{}
	& \progress{m2:prog3}
		{ t \in in \land loc.t = ext }
		{\neg ext \in \ran. loc  }
%\\	& \progress{m2:prog3b}{ t \in in \land loc.t = ext }{ t \in in \land \neg loc.t = ext  }
\end{align*}
%
\hide{
	\refine{m2:prog3}{discharge}{m2:tr0,m2:saf1}{}
	}
%
We implement \eqref{m2:prog3} with the following:
%
\begin{align*}
	\transientB{m0:leave}{m2:tr0}{ \index{t}{t' = t} }
		{ t \in in \1\land loc.t = ext }
\\	\safety{m2:saf1}
		{  t \in in \land loc.t = ext }
		{ \neg ext \in \ran. loc }
\end{align*}

%\begin{proof}{m1:movein/SCH/m2/0/REF/delay/prog/rhs}
%
%\end{proof}

\begin{align*}
	& {\true} \1\mapsto { \neg ~ plf \subseteq \ran.loc }
%	\tag{\ref{m2:prog0}}
\refine{m2:prog0}{trading}{m2:prog4}{}
%	& {\qforall{b}{b \in plf}{ \qexists{t}{t \in in}{ loc.t = b}} \!\!} \3\mapsto
%		{\!\! \qexists{b}{b \in plf}{ \qforall{t}{t \in in}{ \neg loc.t = b}} }
%	\notag \\
	& \progress{m2:prog4}{plf \subseteq \ran.loc \!\!}
		{\!\! \neg ~ plf \subseteq \ran.loc }
\refine{m2:prog4}{monotonicity}{m2:prog5}{}
%	& {\qexists{b,t}{b \in plf \land t \in in}{ loc.t = b}} \3\mapsto
%		{\qexists{b}{b \in plf}{ \qforall{t}{t \in in}{ \neg loc.t = b}} } \notag \\
	& \progress{m2:prog5}{\qexists{b}{b \in plf}{ b \in \ran.loc }}
		{ \qexists{b}{}{ b \in plf \1\land \neg b \in \ran.loc } }
\refine{m2:prog5}{disjunction}{m2:prog6}{}
	& \progress{m2:prog6}{ \qexists{t}{t \in in}{ b \in plf \1\land  loc.t = b } }
		{ b \in plf \land \neg b \in \ran.loc }
\refine{m2:prog6}{disjunction}{m2:prog7}{}
	& \progress{m2:prog7}{ t \in in \land b \in plf \1\land  loc.t = b }
		{ b \in plf \land \neg b \in \ran.loc }
\end{align*}

\hide{
	\refine{m2:prog7}{discharge}{m2:tr1,m2:saf2}{}
	}

\begin{align*}
	\transientB{m1:moveout}{m2:tr1}{ \lt{m2:prog1} \index{t}{t' = t} }
		{ b \in plf \land t \in in \land loc.t = b }
\\ 	\safety{m2:saf2}{ t \in in \land b \in plf \1\land  loc.t = b }
		{ b \in plf \1\land \qforall{t}{ t \in in }{ \neg loc.t = b } }
\end{align*}

\input{train-station-set/m2_m1-moveout.tex}

In order to prove \ref{m1:moveout}, we need a need progress property, to make sure that the fine schedule becomes true infinitely often. We can use the same that we used to introduce the fine schedule \eqref{m2:prog1}.

\subsection{Summary of the events}

\begin{block}

\item[{}] Here are the events
\item[{}]
\input{train-station-set/m2_m0-enter}
\item[{}]
\input{train-station-set/m2_m1-movein}
\item[{}]
\input{train-station-set/m2_m1-moveout}
\item[{}]
\input{train-station-set/m2_m0-leave}
\end{block}
\input{train-station-set/machine_m2}
\end{machine}

\section{Third refinement}

\begin{machine}{m3}

\refines{m2}

\[	\variable{isgn: \Bool} ; ~
	\variable{osgn: \set [\Blk] }
\]

% \with{sets}
% \with{sets}
% \with{functions}

\begin{align*}
	\dummy{ b_0, b_1, p, p_0, p_1, p_2 : \Blk }
\\	\invariant{m3:inv0}{ osgn \subseteq plf }
\\	\invariant{m3:inv1}{ \qforall{p_0,p_1}{ p_0 \in osgn \land p_1 \in osgn }{ p_0 = p_1 } }
\end{align*}
\begin{align*}
	\evassignment{m0:enter}{m3:ent:act0}{isgn' &= isgn}
\\	\evassignment{m0:enter}{m3:ent:act1}{osgn' &= osgn}
\\	\evassignment{m1:movein}{m3:mi:act0}{isgn' &= isgn}
\\	\evassignment{m1:movein}{m3:mi:act1}{osgn' &= osgn}
\\	\evassignment{m0:leave}{m3:ext:act0}{isgn' &= isgn}
\\	\evassignment{m0:leave}{m3:ext:act1}{osgn' &= osgn}
\\	\evassignment{m1:moveout}{m3:mo:act0}{isgn' &= isgn}
\\	\evassignment{m1:moveout}{m3:mo:act1}{osgn'  &\2 = osgn
	\setminus \{ loc.t \} }
\\	\initialization{m3:init0}{ osgn &= \emptyset }
\\	\initialization{m3:init1}{ isgn &= \false }
\end{align*}

\begin{align*}
	\evguard{m1:moveout}{m3:mo:grd0}{ loc.t \in osgn } \\
	\cschedule{m1:moveout}{m3:mo:sch0}{ loc.t \in osgn }
\end{align*}

\replace{m1:moveout}{m3:mo:sch0}{m3:prog0}{m3:saf0}
	% it works because the PO is trivial p |-> true
\replacefine{m1:moveout}{m3:prog0} \removefine{m1:moveout}{mo:f0}
\removeguard{m1:moveout}{mo:g3}

\begin{align*}
	\progress{m3:prog0}{ t \in in
		\1\land loc.t \in plf }{ t \in in \land loc.t \in osgn }
\\	\safety{m3:saf0}{ t \in in \land loc.t \in osgn }{
		 \, ext \in \ran.loc  }
\end{align*}

\begin{align*}
	\invariant{m3:inv2}{ ext \in \ran.loc \implies osgn = \emptyset } % \qforall{t}{}{t \in in \land loc.t \in osgn \1\implies \neg \, ext \in \ran.loc } }
\\	\invariant{m3:inv3}{ osgn \subseteq \ran.loc } %{ \qforall{p}{}{ p \in osgn \1\implies p \in \ran.loc } }
\end{align*}

\input{train-station-set/m3_m1-moveout}

%\begin{align*}
\newevent{m3:ctr:plf}{ctr\_plf}
%\end{align*}

\begin{align*}
	&{  t \in in
		\1\land loc.t \in plf }\2\mapsto{ t \in in \land loc.t \in osgn }
\refine{m3:prog0}{discharge}{m3:tr0,m3:saf1}{}
	&\transientB{m3:ctr:plf}{m3:tr0}
		{ \index{p}{p' = loc.t} }
		{  t \in in
			\1\land loc.t \in plf \setminus osgn }
\\ 	& \safety{m3:saf1}{ t \in in
		\1\land loc.t \in plf }
	{	t \in in \land loc.t \in osgn }
\end{align*}

We should use intersection here:
\begin{align*}
\indices{m3:ctr:plf}{ p : \Blk }
\\ \cschedule{m3:ctr:plf}{m3:cp:c0}{ p \in plf \land p \in \ran.loc \land \neg p \in osgn }
\\ \evassignment{m3:ctr:plf}{m3:cp:act0}{ osgn' = osgn \bunion \{ p \} }
\end{align*}
% EN should not pass this way
% \removecoarse{m3:ctr:plf}{default}  %\weakento{m3:ctr:plf}{}{m3:cp:c0}
\input{train-station-set/m3_m1-moveout}
\input{train-station-set/m3_m3-ctr-plf}
\input{train-station-set/machine_m3}
% \invariant{m3:inv4}{ ext \in \ran.loc \2\implies osgn = \emptyset }
\end{machine}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Information Content}
\begin{frame}
  \frametitle{XAFS Analysis: Information Content in XAFS }

  The Number of Parameters we can measure from our data is limited:

  \vmm

  \begin{center}  {\Red{
        $\displaystyle  N \approx { { 2 \Delta k \Delta R} \over{ \pi}}  $
      }}
  \end{center}

  \vmm

  where $\Red{ \Delta k}$ and $\Red{ \Delta R}$ are the $k$- and
  $R$-ranges of the data.

  Typical:  $k = [2.0, 12.0] \rm\,\AA^{-1}$ and $R =
  [1.0, 3.0]\rm\,\AA$, gives  $\sim 12$ Parameters.

  \pause \vmm \hrule \vmm

  Fit statistics, and Error Bars  need to reflect this limit.

  \vmm
  \pause

  Need to {\Red{constrain}} Parameters $R$, $N$,
  $\sigma^2$ for different paths  and different data sets (different
  edge elements, temperatures, etc)

  \vmm

  \begin{postitbox}{90mm}
    Use as much outside information about the system as possible!
  \end{postitbox}

   \vmm \pause
   It's also possible to add {\Red{restraints}} to describe external
   knowledge of the system (crystallography, Bond Valence, etc).

\end{frame}

\subsection{Building Models}
\begin{frame}
 \frametitle{XAFS Analysis: Building Models }

  The basic difficulties in  EXAFS Analysis are

  \begin{enumerate}

  \item The scattering factors \feffc{f(k)}, \feffc{\delta(k)} are
    non-trivial (we use {\feff}).

 \item The basis functions (Paths) are not very well resolved, and their
    number grows exponentially with $R$.

  \item There's not much information in a real measurement:
    \[ N_{\rm idp} \approx \frac{2\Delta k\Delta R}{\pi} \]

  \end{enumerate}

    \hrule \vmm \pause

    We address these with methods to:

    \begin{enumerate}
    \item {reduce} the number of Paths to consider (Fourier analysis).

    \item parameterize {\emph{ab initio}} calculations of
      \feffc{f(k)}, \feffc{\delta(k)} (use {\feff})

    \item reduce the number of  variables in the fit, while keeping a
      meaningful analysis.

    \end{enumerate}

    \hrule \vmm

    We parameterize the EXAFS with a physical model and then
    put {\RedEmph{Constraints}} on the  %%% and {\RedEmph{Restraints}} on the
    parameters in a least-squares fit.

    \vmm
\end{frame}

\subsection{Constraints}
\begin{frame}[fragile]
  \frametitle{Constraints in {\larch} /  {\artemis} }

  All Path Parameters written as expressions of  Variables refined in Fit.
  \vmm

  \setbeamertemplate{blocks}[rounded][shadow=true]

  \begin{tabular}{ll}
    \begin{minipage}{45mm}
      \begin{block}
        {Parameter = Variable}
{\tiny{\begin{alltt}

{\Red{guess e0    = 1.0 }}
path(1,  e0 = e0)
path(2,  e0 = e0)
\end{alltt}}}
 \end{block}
   \vspace{1.75mm}
   \onslide+<2->
   \begin{block}
{mixed coordination shell}

{\tiny{\begin{alltt}
 set S02   = 0.80

 {\Red{guess x   = 0.5}}

 path(1,  Amp= S02 * x )
 path(2,  Amp= S02 * (1-x))

\end{alltt}}}
   \end{block}
   \end{minipage}
    & \hspace{0.2mm}
    \begin{minipage}{65mm}
      \onslide+<3->   \begin{block}
 { Fit Einstein Temperature }

{\tiny{\begin{alltt}

 set   factor  = 24.254337   {\Blue{#= (hbar*c)^2/(2 k_boltz)}}
{\Blue{# mass and reduced mass in amu}}
 set   mass1 = 63.54,  mass2 = 63.54
 set   r_mass =  1/ (1/mass1 +  1/mass2)

{\Blue{# the Einstein Temp will be adjusted in the fit!}}
 {\Red{guess thetaE = 200}}
{\Blue{# use for data set 1, T=77}}
 set   temp1 = 77
 {\Red{def ss2_path1 = factor*coth(thetaE/(2*temp1))/r_mass )}}
 path(101,  sigma2 = ss2_path1   )

{\Blue{# use for data set 2, T=300}}
 set   temp2 = 300
 {\Red{def ss2_path2 = factor*coth(thetaE/(2*temp2))/r_mass )}}
 path(201,  sigma2 = ss2_path2   )
\end{alltt}}}
        \end{block}
    \end{minipage}\\
  \end{tabular}

  \vmm \vmm

  \onslide+<4-> Other Examples:

\begin{itemize}
\item force one $R$ for the same bond for data taken from different  edges.
\item model complex distortions (height of a sorbed atom above a surface).
\end{itemize}

\end{frame}

\subsection{Fit statistics}
\begin{frame}\frametitle{Fitting with {\ifeffit} / {\artemis} }

  {\ifeffit} optimizes the Fitting Parameters with a least-squares fit to the
  Data

  \begin{postitbox}{90mm}
    Find the variables that make the Model best match the Data
  \end{postitbox}

  \pause \vmm

  $\chi^2$ (don't confuse with EXAFS $\chi$!!) describe the fit:

  \[
  \chi^2  =   \sum_i^{N_{\rm fit}} \frac{[\chi_i^{\rm data} - \chi_i^{\rm
      model}({x})]^2}{\epsilon^2}
  \]

  ${N_{\rm fit}} = $ number of data points, ${x} = $ set of variables,
  $\epsilon =$ noise level in the data. \pause

  We should consider only $ N_{\rm idp} $ data points:

  \begin{postitbox}{88mm}
    \[   \chi^2  =  \frac{ N_{\rm idp}}{\epsilon^2 N_{\rm fit}}
    \sum_i^{N_{\rm fit}} [\chi_i^{\rm data} - \chi_i^{\rm model}({x})]^2 \]
  \end{postitbox}

  \pause   Fitting is typically done in $R$-space to ignore higher shells.

\end{frame}

\subsection{More Fit Statistics}
\begin{frame}
\frametitle{Goodness of Fit and Error Bars}

  Goodness-of-Fit statistics:
  \begin{itemize}[<+->]
  \item{\Red{chi-square}}:     $  \chi^2  =  \frac{ N_{\rm idp}}{\epsilon^2 N_{\rm fit}}
    \sum_i^{N_{\rm fit}} [\chi_i^{\rm data} - \chi_i^{\rm model}({x})]^2   $

  \item{\Red{reduced chi-square}}:  scale $\chi^2$ by the  ``degrees of freedom''

    $ \chi^2_\nu =  \chi^2 / (N_{\rm idp}-N_{\rm varys}) $

    A Good Fit should have $\chi^2_\nu \sim 1$. This {\RedEmph{never}}
    happens!

    $ \chi^2_\nu \sim 10 $ or higher, typically.

  \item{\Red{R-factor}}:  Fractional misfit.
    \[
    {\cal{R}} =
    {\sum_i^{N_{\rm fit}}[\chi_i^{\rm data} - \chi_i^{\rm model}({x})]^2 }
    /
    { \displaystyle{\sum_i^{N_{\rm fit}} [{\chi_i^{\rm data}}]^2}}
    \]

  \end{itemize}

\hrule \vmm \onslide+<4->

  Error bars for Fitting Parameters are calculated, and
  increase $\chi^2$ by $\chi^2_\nu$.

  \vmm
  Correlations between parameters are also calculated.

\end{frame}


\documentclass[12pt, letterpaper]{report}
\usepackage[margin=1in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{float}
\usepackage{subfig}
\graphicspath{ {./img/} }
\setlength\parindent{0pt}
\renewcommand\thesection{\Roman{section}.}
\renewcommand{\thesubsection}{\alph{subsection}.}

\title{CS1675 - Assignment 5}
\author{Zachary M. Mattis}

\begin{document}

\maketitle

\section{Problem 1 - Logistic Regression Model}

% A
\subsection{Normalization}

\begin{verbatim}
    data_normalize.m
\end{verbatim}

% B
\subsection{Batch-Mode Gradient}

\begin{verbatim}
    Log_regression.m
\end{verbatim}

% C
\subsection{Training Gradient}

\begin{verbatim}
    main1.m
\end{verbatim}

% D
\subsection{Misclassification, Confusion, Sensitivity, Specificity}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l| }
		\hline
		\textbf{Dataset} & \textbf{Misclassification Error} \\
		\hline
		Training & 0.2988 \\
		\hline
		Testing & 0.2722 \\
		\hline
	\end{tabular}
	\caption{Misclassification Error}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |r|l|l| }
		\hline
		\textbf{Predict / Target} & \textbf{1} & \textbf{1} \\
		\hline
		\textbf{1} & 118 & 42 \\
		\hline
		\textbf{0} & 82 & 297 \\
		\hline
	\end{tabular}
	\caption{Training Confusion Matrix}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |r|l|l| }
		\hline
		\textbf{Predict / Target} & \textbf{1} & \textbf{1} \\
		\hline
		\textbf{1} & 46 & 27 \\
		\hline
		\textbf{0} & 22 & 134 \\
		\hline
	\end{tabular}
	\caption{Test Confusion Matrix}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l| }
		\hline
		Sensitivity & 0.6765 \\
		\hline
		Specificity & 0.8323 \\
		\hline
	\end{tabular}
	\caption{Test Sensitivity / Specificity}
\end{table}

\subsection{Efficiency}

For a $\frac{2}{\sqrt{k}}$ schedule, the gradient converged after 30,000 epochs. For a $\frac{2}{k}$ schedule, the gradient converged after 200 epochs. Differing starting weights of $\pm100$ also converged in the same amount of time, resulting in a test misclassification error of 0.2722 and training error of 0.2988.

\section{Problem 2.1 - Naive Bayes Model}

% A
\subsection{Data Analysis}

\begin{figure}[H]
	\centering
	\includegraphics[width=\columnwidth]{p2a0.png}
	\caption{Class 0}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=\columnwidth]{p2a1.png}
	\caption{Class 1}
\end{figure}

% B
\subsection{Data Distribution}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l| }
		\hline
		\textbf{Attribute} & \textbf{Distribution} \\
		\hline
		1 & Exponential \\
		\hline
		2 & Normal \\
		\hline
		3 & Normal \\
		\hline
		4 & Normal \\
		\hline
		5 & Exponential\\
		\hline
		6 & Normal \\
		\hline
		7 & Exponential \\
		\hline
		8 & Exponential \\
		\hline
	\end{tabular}
	\caption{Attribute Distribution}
\end{table}

\section{Problem 2.2 - Naive Bayes Learning}

% A
\subsection{Naive Bayes Training Data}

\begin{verbatim}
    main2_2.m
\end{verbatim}

% B
\subsection{Naive Bayes Parameters}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l|l|l|l|l|l| }
		\hline
		\textbf{Attribute} & \textbf{$\mu$} & \textbf{$\sigma$} & \textbf{CI $\mu$ lower} & \textbf{CI $\mu$ upper} & \textbf{CI $\sigma$ lower} & \textbf{CI $\sigma$ upper} \\
		\hline
		2 & 109.98 & 26.1412 & 107.6831 & 112.2769 & 24.6152 & 27.8705 \\
		\hline
		3 & 68.184 & 18.0621 & 66.5969 & 69.7711 & 17.0086 & 19.258 \\
		\hline
		4 & 19.664 & 14.8899 & 18.3557 & 20.9723 & 14.020 & 15.8749 \\
		\hline
		6 & 30.3042 & 7.6899 & 29.6285 & 30.9799 & 7.2409 & 8.1986 \\
		\hline
	\end{tabular}
	\caption{Class 0 Normal}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l|l|l|l|l|l| }
		\hline
		\textbf{Attribute} & \textbf{$\mu$} & \textbf{$\sigma$} & \textbf{CI $\mu$ lower} & \textbf{CI $\mu$ upper} & \textbf{CI $\sigma$ lower} & \textbf{CI $\sigma$ upper} \\
		\hline
		2 & 141.2575 & 31.9396 & 137.4161 & 145.0988 & 29.4451 & 34.8995 \\
		\hline
		3 & 70.8246 & 21.4918 & 68.2398 & 73.4094 & 19.8133 & 23.4835 \\
		\hline
		4 & 22.1642 & 17.6797 & 20.0379 & 24.2905 & 16.2989 & 19.3181 \\
		\hline
		6 & 35.1425 & 7.2630 & 34.269 & 36.0160 & 6.6957 & 7.9360 \\
		\hline
	\end{tabular}
	\caption{Class 1 Normal}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l|l|l|l }
		\hline
		\textbf{Attribute} & \textbf{$\mu$} & \textbf{CI $\mu$ lower} & \textbf{CI $\mu$ upper} \\
		\hline
		1 & 3.2980 & 3.0270 & 3.6073 \\
		\hline
		5 & 68.792 & 63.139 & 75.2436 \\
		\hline
		7 & 0.4297 & 0.3944 & 0.4700 \\
		\hline
		8 & 31.190 & 28.627 & 34.1151 \\
		\hline
	\end{tabular}
	\caption{Class 0 Exponential}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l|l|l|l }
		\hline
		\textbf{Attribute} & \textbf{$\mu$} & \textbf{CI $\mu$ lower} & \textbf{CI $\mu$ upper} \\
		\hline
		1 & 4.8657 & 4.3319 & 5.5051 \\
		\hline
		5 & 100.3358 & 89.3289 & 113.5215 \\
		\hline
		7 & 0.5505 & 0.4901 & 0.6228 \\
		\hline
		8 & 37.0672 & 33.0009 & 41.9384 \\
		\hline
	\end{tabular}
	\caption{Class 1 Exponential}
\end{table}

\section{Problem 2.3 - Naive Bayes Classification}

% A
\subsection{NB Prediction}

\begin{verbatim}
    main2_3.m
\end{verbatim}

% B
\subsection{Misclassification, Confusion, Sensitivity, Specificity}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l| }
		\hline
		\textbf{Dataset} & \textbf{Misclassification Error} \\
		\hline
		Training & 0.5356 \\
		\hline
		Testing & 0.6241 \\
		\hline
	\end{tabular}
	\caption{Misclassification Error}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |r|l|l| }
		\hline
		\textbf{Predict / Target} & \textbf{1} & \textbf{1} \\
		\hline
		\textbf{1} & 168 & 156 \\
		\hline
		\textbf{0} & 32 & 183 \\
		\hline
	\end{tabular}
	\caption{Training Confusion Matrix}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |r|l|l| }
		\hline
		\textbf{Predict / Target} & \textbf{1} & \textbf{1} \\
		\hline
		\textbf{1} & 59 & 79 \\
		\hline
		\textbf{0} & 9 & 82 \\
		\hline
	\end{tabular}
	\caption{Test Confusion Matrix}
\end{table}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l| }
		\hline
		Sensitivity & 0.8676 \\
		\hline
		Specificity & 0.5093 \\
		\hline
	\end{tabular}
	\caption{Test Sensitivity / Specificity}
\end{table}

% C
\subsection{Logistic Regression vs. Naive Bayes}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l|l| }
		\hline
		\textbf{Model} & \textbf{Training} & \textbf{Testing} \\
		\hline
		Logistic Regression & 0.2988 & 0.2722 \\
		\hline
		Naive Bayes & 0.5356 & 0.6241 \\
		\hline
	\end{tabular}
	\caption{Misclassification Error}
\end{table}

\section{Problem 3 - ROC Analysis}

% A
\subsection{\textit{perfcurve()}}

Receiver operating characteristic (ROC) curve or other performance curve for classifier output.

% B
\subsection{ROC}

\begin{verbatim}
    roc_analysis.m
\end{verbatim}

% C
\subsection{AUC}

\begin{figure}[H]
	\captionsetup[subfigure]{labelformat=empty}
	\centering
	\subfloat[Figure 3]{{\includegraphics[width=18em]{p3lr.png} }}
	\qquad
	\subfloat[Figure 4]{{\includegraphics[width=18em]{p3nb.png} }}
	\label{fig:example}
\end{figure}

\begin{table}[H]
	\centering
	\begin{tabular}{ |l|l| }
		\hline
		\textbf{Model} & \textbf{AUC} \\
		\hline
		Logistic Regression & 0.8518 \\
		\hline
		Naive Bayes & 0.4450 \\
		\hline
	\end{tabular}
	\caption{AUC}
\end{table}

Based on the collected data for the ROC curves and the AUC statistics, logistic regression outperforms the Naive Bayes implementation, with a higher true positive rate and AUC.

\end{document}


\input{preamble}

\begin{document}
    \section{Subjects}
    \begin{itemize}
        \item VC Dimension
        \item Bias Variance
        \item Regularization
        \item Validation
    \end{itemize}
    \section{Notes}

    \subsection{Preceding discussion}
    \textit{Chapter 2 in Learning from data.}\\
    What we want to minimize, is the out-of-sample error:
    \begin{equation*}
    E_{out}(g) = \Ex_{x,y}\left(e\left(g(x), y\right)\right)
    \end{equation*}
    We can't minimize this, however what we can minimize is the in-sample-error:
    \begin{equation*}
    E_{in}(g)=\frac{1}{\|D\|} \sum_{(x,y) \in D} e\left(g(x), y\right)
    \end{equation*}
    So we are hoping, or aiming for, that the generalization error
    $E_{in}-E_{out}$ should be small, so when we minimize $E_{in}$ it benefits
    $E_{out}$.\\
    Recall that the Hoeffding inequality provides a way to bound the
    generalization error:
    \begin{equation*}
    \Pr[|E_{in}(g) - E_{out}(g)| > \epsilon] \leq 2Me^{-2\epsilon^2N}
    \end{equation*}
    We can rephrase this, by introducing a tolerance level $\delta$ and assert
    with probability at least $1 - \delta$ that:
    \begin{equation*}
    E_{out}(g) \leq E_{in}(g) + \sqrt{\frac{1}{2N}\ln\frac{2M}{\delta}}
    \end{equation*}
    We notice here, that the bound depends on $M$, which is the size of the
    hypothesis set. Unfortunately, most problems has infinite hypotheses, and
    thus the bound will become meaningless as it goes towards infinity. So we
    want to replace $M$ by something that stays meaningful as $M$ goes to
    infinity.

    To this end we introduce the growth function, which will formalize the
    effective number of hypotheses. Furthermore, a \textit{dichotomy} is an
    $N$-tuple, generated by a hypothesis, which splits the data into two
    groups.

    The set of dichotomies generated by the hypothesis set $\Hy$ on the points
    $x_1,\dots,x_n$ is defined by:
    \begin{equation}
    \Hy(x_1,\dots,x_N)= \{(h(x_1),\dots,h(x_N)) \,|\, h \in \Hy)\}
    \end{equation}
    One can think of the dichotomies as being the hypothesis set as seen
    through the eyes of just $N$ points. We can then define the growth function
    as:
    \begin{equation}
    m_\Hy(N)=\max\limits_{x_1, \dots, x_n \in X} |\Hy(x_1,\dots, x_N)|
    \end{equation}
    I.e. the maximum number of dichotomies that can be generated by $\Hy$ on
    any $N$ points. If we just look at binary classification, then the upper
    limit on the amount of dichotomies for a data-set of size $N$ is:
    \begin{equation}
    m_\Hy(N) \leq 2^N
    \end{equation}
    If $\Hy$ is capable of generating all possible dichotomies on the data-set,
    then $m_\Hy(n) = 2^N$ and we say that $\Hy$ shatter $x_1,\dots,x_n$. If
    there is no such data-set of size $k$ that can be shattered by $\Hy$ then
    we say that $k$ is a breakpoint for $\Hy$.

    If there is such a breakpoint $k$, then we know that $m_\Hy(k) < 2^N$. We
    define $B(N,k)$ as the maximum number of dichotomies on $N$ points, such
    that no subset of size $k$ can be shattered by these dichotomies. We can
    then see that:

    \begin{equation*}
    m\Hy(N) \leq B(N,k)\, \text{ if $k$ is a break point for }\Hy
    \end{equation*}

    Sauer's lemma then states that:
    \begin{equation*}
    B(N,k) \leq \sum_{i=0}^{k-1}\begin{pmatrix}
    N\\
    i
    \end{pmatrix}
    \end{equation*}

    Which means that:
    \begin{equation*}
        m_\Hy(N) \leq \sum_{i=0}^{k-1} \begin{pmatrix}
        N\\i
        \end{pmatrix}
    \end{equation*}

    We then see that, if $\Hy$ has a breakpoint then $m_\Hy(N)$ has a
    polynomial bound. This is important because when $m_\Hy(N)$ has a
    polynomial bound, the generalization error will go to zero as $N
    \rightarrow \infty$

    \subsection{VC Dimension}
    The Vapnik-Chervonenkis dimension of a hypothesis set $\Hy$, denoted by
    $d_{vc}(\Hy)$ or simply $d_{vc}$ is the largest value of $N$ for which
    $m_\Hy(N)=2^N$. If $m_\Hy(N)=2^N$ for all $N$, then $d_{vc}(\Hy)=\infty$.

    It follows then, that if $d_{vc}$ is the VC dimension for $\Hy$, then $k =
    d_{vc} + 1$ is a breakpoint and there are no smaller breakpoints. We can
    therefore rewrite the previous sum in terms of the VC dimension:

    \begin{equation*}
        m_\Hy(N) \leq \sum_{i=0}^{d_{vc}} \begin{pmatrix}
        N\\i
        \end{pmatrix}
    \end{equation*}

    We then arrive at the VC Generalization Bound:
    \begin{equation}
        E_{out}(g) \leq E_{in}(g) + \sqrt{\frac{8}{N} \ln
        \frac{4m_\Hy(2N)}{\delta}}
    \end{equation}
    with probability $\geq 1-\delta$

    VC-Dimension captures expressiveness/capacity of hypothesis spaces and
    relate them to generalization. Leads to out-of-sample error equals
    in-sample-error + model complexity:
    \begin{equation*}
    E_{out}(h) \leq E_{in}(h) + \Omega(N,\Hy, \delta)
    \end{equation*}
    \begin{equation*}
    \Omega(N, \Hy, \delta) = \sqrt{\frac{8}{N}\ln\frac{4m_\Hy(2N)}{\delta}}
    \end{equation*}
    \todo{Sampling	Complexity}

    \subsection{Bias-Variance decomposition}
    \textit{Page 62}\\
    The out-of-sample error for hypothesis $g^{(D)}$ learned on data $D$ is:
    \begin{equation*}
        E_{out}(g^{(D)}) = \Ex_x\left[(g^{(D)}(x)-f(x))^2\right]
    \end{equation*}
    Where $\Ex_x$ denotes the expected value with respect to $x$ (based on the
    probability distribution on the input space $X$)

    We can generalize this, to remove the dependence on a specific data-set by
    taking the expectation with respect to all data-sets:
    \begin{align*}
        \Ex_D\left[E_{out}(g^{(D)})\right] &=
        \Ex_D\left[\Ex_x\left[(g^{(D)}(x)-f(x))^2\right]\right]\\
            &= \Ex_x\left[\Ex_D\left[(g^{(D)}(x)-f(x))^2\right]\right]\\
            &= \Ex_x\left[\Ex_D\left[g^{(D)}(x)^2\right] -
            2\Ex_D\left[g^{(D)}(x)\right]f(x)+f(x)^2)\right]
    \end{align*}

    The term $\Ex_D\left[g^{(D)}(x)\right]$ gives an 'average function', which
    we denote by $\bar{g}(x)$ it can be seen as an average function as a result
    of many data-sets $D_1,\dots,D_K$ where:
    \begin{equation}
        \bar{g}(x)\approx \frac{1}{K} \sum_{k=1}^{K}g_k(x)
    \end{equation}

    We can now rewrite the expected out-of-sample error in terms of $\bar{g}$:
    \begin{align*}
        &\Ex_D\left[E_{out}(g^{(D)})\right]\\
        &=\Ex_x\left[\Ex_D\left[g^{(D)}(x)^2\right] - 2\bar{g}(x)f(x)+
        f(x)^2\right]\\
        &=\Ex_x\left[\Ex_D\left[g^{(D)}(x)^2\right]-\bar{g}(x)^2 + \bar{g}(x)^2
        - 2\bar{g}(x)f(x)+f(x)^2\right]\\
        &=\Ex_x\left[\Ex_D\left[(g^{(D)}(x) - \bar{g}(x))^2\right] +
        (\bar{g}(x)-f(x))^2\right]
    \end{align*}

    The term to the right $(\bar{g}(x)-f(x))^2$ measure how much the average
    function we would learn using the $D$ different data-sets deviates from the
    target function, we call this term the bias:
    \begin{equation*}
        \text{bias}(x)=(\bar{g}(x)-f(x))^2
    \end{equation*}
    The other term, $\Ex_D\left[(g^{(D)}(x)-\bar{g}(x))^2\right]$ is what we
    call the variance, which measures the variation in the final hypothesis:
    \begin{equation}
        \text{var}(x)=\Ex_D\left[(g^{(D)}(x)-\bar{g}(x))^2\right]
    \end{equation}
    We thus arrive at the bias-variance decomposition of out-of-sample error:
    \begin{align*}
        \Ex_D\left[E_{out}(g^{(D)})\right] &= \Ex_x\left[\text{bias}(x) +
        \text{var}(x)\right]\\
            &= \text{bias} + \text{var}\\
        \text{bias} &= \Ex_x\left[\text{bias(x)}\right]\\
        \text{var} &= \Ex_x\left[\text{var(x)}\right]
    \end{align*}
    \textit{Bias:} How well can we actually fit - on average\\
    \textit{Variance:} How much will data samples lead me astray - on average

    We cannot compute actual bias and variance in practice, since they depend
    on the target function and input probability distribution. So it is a
    conceptual tool, which is helpful when it comes to developing a model.

    There are two typical goals: we want to reduce the variance without
    significantly increasing bias et vice versa. These goals are achieved
    through heuristics, regularization being one them.

    \textbf{Intuition}
    The bias term is big if the model has too little capacity to fit the data.

    The variance term is big if the model has so much capacity, that it is good
    at fitting noise (sampling errors) in any training set.

    \subsection{Regularization}
    In learning, we want to achieve two things:
    \begin{enumerate}
        \item Ensure that out-of-sample error is close to in-sample-error \\
        $\Pr\left[|E_{in} - E_{out}| > \epsilon\right] \leq
        2Me^{-2\epsilon^2N}$ or:\\
        $E_{out}(h)\leq E_{in}(h)+\Omega(N,\Hy,\delta)$
        \item Minimize in-sample-error $E_{in}$
    \end{enumerate}
    So far we have looked at how to prevent underfitting, i.e. how do we fit
    our data as well as possible. Regularization is about preventing
    over-fitting, i.e. if we fit our data really well, then our in-sample-error
    might be really low $E_{in}=0$, but it's no longer close to the
    out-of-sample-error $E_{out}>>E_{in}$

    This happens because we fit the noise rather than the signal, if we look at
    the target complexity, e.g. for some polynomial, then if we try to fit it
    with a hypothesis of $h_{10}$, then as the target complexity increases
    there will be more and more data that our hypothesis cannot capture, so
    even though it is not stochastic noise, it wil look like noise to our
    hypothesis. This is reflected in the fact that:
    \begin{itemize}
        \item Data increases $\rightarrow$ Overfitting Decreases
        \item Noise increases $\rightarrow$ Overfitting Increases
        \item Target Complexity Increases $\rightarrow$ Overfitting Increases
    \end{itemize}
    So increasing the target complexity seems to introduce noise similar to
    increasing the noise. In order to avoid this, we want to fit it in a
    ``simpler'' way, so we avoid the precise fitting we get from our current
    techniques.

    In linear regression, this is what we currently minimize:
    \begin{equation*}
        E_{in} = \frac{1}{|D|} \sum_{x,y \in D}(w^Tx-y)^2
    \end{equation*}
    If we instead, for different values of $\alpha$, minimize:
    \begin{equation*}
    E_{in}+\Omega(h) = \frac{1}{|D|} \sum_{x,y \in D}(w^Tx-y)^2 +
    \alpha\|w\|^2_2
    \end{equation*}

    Which gives us the constrained optimization:
    \begin{align*}
        \text{Minimize: }&\frac{1}{|D|}\sum_{x,y \in D} (w^Tx - y)^2\\
        \text{Subject to: }&\lambda\|w\|^2_2\leq C
    \end{align*}
    Then we get the constrained hypothesis set $\Hy_{\text{Constrained}}$ where:
    \begin{equation*}
        \Hy_\text{Constrained} \subseteq \Hy \implies
        d_{vc}(\Hy_\text{Constrained}) \leq d_{vc}(\Hy)
    \end{equation*}
    So we get a simpler hypothesis set (even though $d_{vc}$ might be the
    same), which also means the variance should go down (as the regularization
    favors $h$'s that look alike, and rules out the ones that are specialized
    to a single data-set) although the bias should go up.\todo{why?}

    Then for e.g. linear regression we get that:
    \begin{equation*}
        \nabla_w=\frac{1}{n}(2X^TXw-2X^Ty) + \frac{\lambda w}{2n}
    \end{equation*}
    Which gives us that:
    \begin{equation*}
        w = (X^TX+\lambda I)^{-1}X^Ty
    \end{equation*}

    For gradient descent, we get that when we minimize $E_{in} + \lambda
    \|w\|^2_2$, the gradient descent step that previously looked like:
    \begin{equation*}
        w_{t+1} = w_t - \alpha \nabla_w f(w_t)
    \end{equation*}
    will now become:
    \begin{align*}
        w_{t+1} &= w_t - \alpha \nabla_w E_{in}(w_t) - 2 \alpha \lambda w_t\\
            &= w_t(1-2 \alpha \lambda) - \alpha \nabla_w E_{in}(w_t)
    \end{align*}
    Thus, every round the $(1-2\alpha \lambda)$ factor will make $w$ decay
    towards the zero vector.

    \subsection{Validation}
    Regularization introduces the need for validation in order to be able to
    train on the data again and again with different values of $\lambda$. The
    need for validation becomes apparent without regularization in more
    advanced models (like Neural Networks) but regularization is the first
    technique we encounter.

    Validation simply means to take out $K$ points from the data-set and use
    them to validate the result of the training on the remaining $N-K$ points.

    Validation has the issue that as you increase $K$, the $E_{val}$ estimate
    tightens but it also increases, as you has less data to train on so
    $E_{out}$ increases with smaller $N-K$. Analytically:
    \begin{equation*}
        E_{val} = E_{out} \pm \bigO{\frac{\sigma}{\sqrt{K}}}
    \end{equation*}
    As a rule of thumb: $K=\frac{N}{5}$ (20\% of data). If we validate and try
    out different models with different $E_{val}(m_i)$, then we can pick the
    best and retrain on all the data, hoping that the model we chose is still
    the best.

    Usually if we have few hyperparameters, (like $\lambda$) then we can just
    perform grid search (exhaustively search through a range of parameters). If
    we have many hyperparameters, we should just do ``random sampling'' and
    repeatedly search on promising areas.

    We do, however risk that we now overfit on the validation parameters. So
    what we really want is a training set, a validation set \textit{and a test
    set}! We will usually split the data-set by $50/25/25\%$ or $60/20/20\%$
    depending on the application and the data available.

    For small $K$ we get that $E_{out}(h_{all}) \approx E_{out}(h_{train})$ and
    for large $K$ we get $E_{out}(h_{train}) \approx E_{val}(h_{train})$, but
    we would like to have both of these.

    It turns out that setting $K=1$ and computing:
    \begin{align*}
        D_n &= D - \{(x_n, y_n)\}\\
        e_n &= E_{val}(h_{D_n}) = e(h_{D_n}, (x_n), y_n)\\
        E_{cv} &= \frac{1}{N} \sum_{i=1}^{N} e_n
    \end{align*}
    This is hard to analyze, since errors are correlated but it has been used
    successfully in practice. However, doing this cross-validation is slow, as
    we have to train $N$ times on $N-1$ points. Therefore we use $K$-Fold Cross
    Validation:
    \begin{itemize}
        \item Split data in $K$ parts of size $\frac{N}{K}$
        \item Run $K$ rounds of validation with different data-sets of size
        $N-K$ and take the mean error.
    \end{itemize}
    Usually, we will run for $K=10$ or $K=5$, as we are impatient beings with
    deadlines.
\end{document}


\vsssub
\subsubsection{~$S_{ice}$: Empirical/parametric damping by sea ice} \label{sec:ICE4}
\vsssub

\opthead{IC4}{\ws/NRL}{C. Collins and E. Rogers}

\noindent
The fourth option ({\code IC4}) for damping of waves by sea ice was introduced by \cite{rep:CR17}. It gives methods to implement one of several simple, empirical/parametric forms for the dissipation of wave energy by sea ice.  The motivation for {\code IC4} is to provide a simple, flexible, and efficient source term which reproduces, albeit in a highly parameterized way, some basic physics of wave-ice interaction. The method is set by the integer value (presently 1 to 7) for {\code IC4METHOD} namelist parameter: 1) an exponential fit to the field data of \cite{art:WAD88}, 2) the polynomial fit in \cite{art:MBK14}, 3) a quadratic fit to the calculations of \cite{art:KM08} given in \cite{art:HT15}, 4) Eq. 1 of \cite{art:Ko14}, 5) a simple step function with up to 4 steps (may be nonstationary and non-uniform), and 6) a simple step function with up to 10 steps (must be stationary and uniform), and 7) a formula from \cite{art:Dob15} which uses ice thickness. All but the fourth method of {\code IC4} feature frequency-dependent attenuation. With the fourth method, attenuation varies with waveheight but is uniform in frequency space.

In the following discussion we use {\code IC4M1} to denote {\code IC4} method 1, and so forth. {\code IC4} appears in the {\file switch} and namelist {\code IC4METHOD=1} (for example) appears in the file {\file ww3\_grid.inp}. Whereas in {\code IC1}, ${C_{ice,1}}$ is the user-determined attenuation, for {\code IC4M1}, {\code IC4M2}, and {\code IC4M4} ${C_{ice,n}}$ are constants of the equations. For {\code IC4M3}, ${C_{ice,1}}$ is ice thickness. For {\code IC4M5}, ${C_{ice,n}}$ controls the step function. Note that ${C_{ice,n}}$ may be provided by the user as non-stationary and non-uniform using methods analogous to methods used to input water levels.

{\code IC4M1}: an exponential equation was chosen to fit the data contained in table 2 of \cite{art:WAD88} which results in preferential attenuation of high frequency waves. This parameterizes the well-known low-pass filtering effect of ice. The equation has the following form:
\begin{equation}\label{eq:ice1}
  {\alpha} = \exp\left[\frac{-{2\pi}C_{ice, 1}}{{\sigma}} - C_{ice, 2}\right]
\end{equation}

\noindent Here, $\alpha$ is the exponential decay rate for energy, which is twice that for amplitude: $\alpha = 2k_i$. The values determined from the data are ${C_{ice,1...2}}=[0.18, 7.3]$, but these may be modified by the user. This method is described and applied in \cite{rep:CR17}.

{\code IC4M2}: In this method, the dissipation is represented using a user-specified polynomial. It is a powerful method, since many shapes can be represented, e.g. by fitting to observation-based dissipation rates. The method is described and applied in \cite{rep:CR17}. The equation is the following:
\begin{equation}\label{eq:ice2}
  {\alpha} = C_{ice,1} + C_{ice,2}\left[{\frac{\sigma}{2\pi}}\right] + C_{ice,3}\left[{\frac{\sigma}{2\pi}}\right]^2 + C_{ice,4}\left[{\frac{\sigma}{2\pi}}\right]^3 + C_{ice,5}\left[{\frac{\sigma}{2\pi}}\right]^4
\end{equation}

\noindent
If a user wishes to follow \cite{art:MBK14}, the suggested values for the coefficients are ${C_{ice,1...5}}=[0, 0, 2.12\times 10^{-3}, 0, 4.59\times 10^{-2}]$. Additional suggested polynomials can be found in \cite{rep:RMK18}.

With appropriate coefficients, this polynomial method can be used to reproduce the so-called “roll-over effect” where the attenuation is non-monotonic in frequency space. However, some recent studies do not indicate this effect, e.g. \cite{art:RTS16} and \cite{art:LK17}, and it may just be a spurious artifact in prior observational studies.

{\code IC4M3}: \cite{art:HT15} fit a quadratic equation to the attenuation coefficient calculated by \cite{art:KM08} as a function of frequency, $T$, and ice thickness, $h$. Attenuation increases for thicker ice and higher frequencies (lower periods). The number of  coefficients of the quadratic equation were prohibitively large to be user-determined, so the equation is hardwired in and the tunable parameter, ${C_{ice,1}}$, is ice thickness $h$. This method is described and applied in \cite{rep:CR17}. For reference, the equation is the following:
\begin{equation}\label{eq:ice3}
  {\ln{\alpha(T,h)}} = -0.3203 + 2.058h - 0.9375T - 0.4269h^2 + 0.1566hT + 0.0006T^2
\end{equation}

\noindent
There are two warnings to make about {\code IC4M3}. First, the equation itself was an extrapolation of the original range of $h$ used to calculate the attenuation coefficients in \cite{art:KM08} which was between 0.5 and 3 m, see \cite{art:HT15}. Second, in \cite{art:KM08}, wave attenuation predicted is based on scattering (a conservative process), whereas in {\code IC4M3}, the wave attenuation is treated as dissipation (non-conservative). This is ad hoc and not recommended for general use. Most especially, users should think twice before using {\code IC4M3} in combination with scattering routines {\code IS1} or {\code IS2}, since this is essentially double-counting scattering.

{\code IC4M4}: \cite{art:Ko14} found that attenuation was a function of significant wave height. Attenuation increased linearly with ${H_s}$ until ${H_s} = 3$ m at which point attenuation is capped, thus:
\begin{equation}
\left \{
\begin{array}{llrcl}
{\frac{\partial H_s}{\partial dx}} = {C_{ice,1}}\times {H_s}   & & \text{for} \> {H_s} \leq 3 \text{ m}  \\
{\frac{\partial H_s}{\partial dx}} = {C_{ice,2}}               & & \text{for} \> {H_s} > 3 \text{ m}     \\
\end{array} \right .
\end{equation}
where {$k_i=\frac{\partial H_s}{\partial dx}/H_s$}.

The values given in \cite{art:Ko14} are ${C_{ice,1...2}}=[5.35\times 10^{-6}, 16.05\times 10^{-6}]$. See regression test {\file ww3\_tic1.1/input\_IC4/M4} for examples. This method is described and applied in \cite{rep:CR17}.

{\code IC4M5}: This is a simple step function with up to 4 steps. It is controlled by the optionally nonstationary and non-uniform parameters ${C_{ice,1...7}}$. Parameters ${C_{ice,1...4}}$ control the step levels, which are in terms of dissipation rate, ${k_i}$. Parameters ${C_{ice,5...7}}$ control the step boundaries (given in Hz). See regression test {\file ww3\_tic1.1/input\_IC4/M5} for examples. This method is described in \cite{rep:CR17}.

{\code IC4M6}: This is a simple step function with up to 10 steps. It is controlled by the stationary and uniform namelist parameters {\code IC4KI} and {\code IC4FC}. Array {\code IC4KI} controls the step levels, which are in terms of dissipation rate, ${k_i}$, in radians per meter. Array {\code IC4FC} controls the step boundaries (given in Hz). See regression test {\file ww3\_tic1.1/input\_IC4/M6} for examples.

{\code IC4M7}: This is a formula for dissipation from \cite{art:Dob15}, developed for a mixture of pancake and frazil ice, using data collected in the Weddell Sea (Antarctica). The formula depends on wave frequency and ice thickness:
\begin{equation}\label{eq:ice7}
  {\alpha=0.2T^{-2.13}h} \:\:\: .
\end{equation}
This method is described in \cite{rep:RPLA18}.


\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{verbatim}
\usepackage{url}
\begin{document}

\title{APPM 4510 HW1}
\author{Zane Jakobs}
\date{}
\maketitle
Note: I'm using your notation for pdfs since we aren't talking about Lie algebras, and it's pretty nice.\\
\subsection*{1}
$$ [X,Y] = [X] [Y | X] = \dfrac{1}{c} e^{-\frac{x^2}{2\sigma^2} - \frac{(y-x)^2}{2\gamma^2}}$$

\subsection*{2} Given that $X = 2.5$, it must be the case that $Y = 2$ exactly, so
$$[Y] = \delta(y - 2),$$
where $\delta$ is the Dirac delta.
\subsection*{3(a)}
\begin{proof}
Taking the expectation of this process at time $j$ gives
\[ \mathbb{E}[v_{j}] = \lambda \mathbb{E}[v_{j-1}] + \bar{\epsilon}\tag{1}.\]
In order for the desired limit to exist (which it must, since $|\lambda | < 1$ so the process is covariance-stationary), it must be the case that as $j\to\infty,$ the absolute value of the series of innovations, $|\Delta V|$, must converge to $0$. Therefore, as $j\to\infty,$ $\mathbb{E}][v_{j+1}] \to \mathbb{E}[v_{j}],$ and hence, in this limit, (1) becomes
\[
\lim\limits_{j\to\infty}\left(\mathbb{E}[v_{j}] = \lambda \mathbb{E}[v_{j}] + \bar{\epsilon} \right)\tag{2}.
\]
This can be solved to give the desired limit,
\[
\lim\limits_{j\to\infty}\mathbb{E}[v_{j}] = \dfrac{\bar{\epsilon}}{1-\lambda}.\tag*{\qedhere}
\]
\end{proof}
\subsection*{3(b)}
\begin{proof}
Using the same justification as above to let successive terms have equal moments in the large-time limit, we set up the system
\[
\lim\limits_{j\to\infty} \mathrm{Var}[v_{j}] = \lambda^2\mathrm{Var}[v_{j}] + \sigma_{\epsilon}^2 \tag{3}.
\]
Then, again like with the mean, we solve to get
\[
\lim\limits_{j\to\infty} \mathrm{Var}[v_{j}] = \dfrac{\sigma_{\epsilon}^2}{1-\lambda^2}.\tag*{\qedhere}
\]
\end{proof}

\subsection*{3(c)} \begin{proof}The lag-1 autocovariance in the infinite-time limit is
\[ \lim\limits_{j\to\infty}\mathrm{Cov}[v_{j+1},v_j] =\lim\limits_{j\to\infty}\left( \mathbb{E}[v_{j+1}v_j] -\mathbb{E}[v_j]\mathbb{E}[v_{j+1}]\right). \tag{4}
\] Since $v_{j+1} = \lambda v_j + \epsilon_j$,

\[
\mathbb{E}[v_{j+1}v_j] = \mathbb{E}[\lambda v_j^2 + \epsilon_j v_j] = \lambda\mathbb{E}[v_j^2] + \mathbb{E}[\epsilon_j]\mathbb{E}[v_j] = \lambda (\mathrm{Var}(v_j) + \mathbb{E}[v_j]^2) + \bar{\epsilon}\mathbb{E}[v_j],\tag{5} \]
where the second equality follows from the independence of the $v_j$ and $\epsilon_j$. We then have
\[
\begin{aligned}
\lim\limits_{j\to\infty}\mathrm{Cov}[v_{j+1},v_j] &= \lambda\left(\dfrac{\sigma_{\epsilon}^2}{1-\lambda^2} + \dfrac{\bar{\epsilon}^2}{(1-\lambda)^2}\right) + \dfrac{\bar{\epsilon}^2}{1-\lambda} -   \dfrac{\bar{\epsilon}^2}{(1-\lambda)^2}\\
&= \dfrac{(\lambda - 1)\bar{\epsilon}^2}{(1-\lambda)^2} + \dfrac{\bar{\epsilon}^2}{1-\lambda} + \dfrac{\lambda\sigma_{\epsilon}^2}{1-\lambda^2}\\
&= \dfrac{\lambda\sigma_{\epsilon}^2}{1-\lambda^2}.
\end{aligned}
\] \qedhere
\end{proof}
\subsection*{3(d)}\begin{proof} Since we justified in 3(a) and 3(b) that the variance of $v_{j+1}$ approaches that of $v_j$ as $j\to\infty$, the autocorrelation in that limit is
\[
\begin{aligned}
\lim\limits_{j\to\infty}\mathrm{Corr}[v_{j+1},v_j]  &= \lim\limits_{j\to\infty}\dfrac{\mathrm{Cov}[v_{j+1},v_j]}{\left(\frac{\sigma_{\epsilon}^2}{1-\lambda^2}\right)^2}\\
 &= \dfrac{\lambda}{\frac{\sigma_{\epsilon}^2}{1-\lambda^2}}\\
 &= \dfrac{\lambda(1-\lambda^2)}{\sigma_{\epsilon}^2}.
 \end{aligned}
\]
\end{proof}

\subsection*{4} $\mathrm{Rk}(\mathbf{A}) = k-1$.\ \begin{proof} By the definition of a basis, the $\mathbf{v}_i$ form a basis for $\mathbb{R}^k$, thus the rank of $\mathbf{V}$ is $\mathrm{Rk}(\mathbf{V}) = k$. Subtracting $\frac{1}{k}\mathbf{1}\mathbf{1}^T$ from the $k\times k$ identity gives a matrix, each of whose rows has one element that is 1 greater than the element in the row above/below it, allowing $k-1$ of those rows to be put into a linear combination that is equal to the $k$-th row, thus the matrix $\mathbf{I} - \frac{1}{k}\mathbf{1}\mathbf{1}^T$ must have rank $k-1$. Since a matrix product is one representation for composition of linear transformations, we note that $\mathbf{A}$ can be viewed as the composition of a rank $k-1$ transformation and a rank $k$ transformation, and thus cannot have a rank greater than $k-1$ (in simpler language, a transformation that is composed of other transformations is "no more invertible" than its "least invertible" individual part). It is also true that $\mathbf{A}$ can be constructed by "gluing"  the $k\times k$ submatrix of $\mathbf{V}$ that spans $\mathbb{R}^k$ multiplied against  $\mathbf{I} - \frac{1}{k}\mathbf{1}\mathbf{1}^T$, with the result of the product of $\mathbf{I} - \frac{1}{k}\mathbf{1}\mathbf{1}^T$ and the remaining $(n-k)\times k$ submatrix of $\mathbf{V}$, and applying an (invertible) permutation matrix. The first product is the product of a full-rank matrix (of rank $k$) and a rank-deficient (rank $k-1$) matrix, and thus has rank $k-1$. The second product, when "glued" with the first, cannot add to the rank of $\mathbf{A}$, since these $n-k$ rows were originally linear combinations of the other $k$ and they were composed with the exact same transformation as those other $k$, so those original linear combinations are preserved (that is, if row $k+3$ of $\mathbf{V}$ were equal to the sum of rows $k-5$ and $k-143$ of $\mathbf{V}$, the same will be true of $\mathbf{A}$). Thus, $\mathrm{Rk}(\mathbf{A}) = k-1$ exactly.
\end{proof}

\subsection*{5}
Newton's method entails the repeated computation of $\mathbf{J}$, the Jacobian of $J(\mathbf{x})$ (Note: since $J$ is a scalar function, its Jacobian would probably be more appropriately called its gradient, and is a vector, not a matrix), the Hessian $\mathbf{\mathcal{H}}$ (NOT the same as $\mathbf{H(x)}$, solution of the equation
\[
 \mathbf{\mathcal{H}} \cdot (\mathbf{\delta x}) = -\mathbf{J} \tag{6}
 \]
for $\mathbf{\delta x}$, and updating $\mathbf{x}\to\mathbf{x} + \gamma\mathbf{\delta x}$ for some step size $\gamma\in (0,1] $ (the choice of a $\gamma$ could be replaced with a line search for the optimal step size). We can compute $\mathbf{J}$ as
\[
\begin{aligned}
\mathbf{J} &= \dfrac{\partial J}{\partial\mathbf{x}^T}\\
&= 2\mathbf{x}^T\mathbf{C}^{-1} +  \epsilon^2 \dfrac{\partial}{\partial\mathbf{x}^T}\left(\mathbf{y}^T\mathbf{y} - \mathbf{y}^T\mathbf{H(x)} - \mathbf{H(x)}^T\mathbf{y} + \mathbf{H(x)}^T\mathbf{H(x)}\right)\\
&= 2\mathbf{x}^T\mathbf{C}^{-1} - \epsilon^2\left(\mathbf{y}^T\dfrac{\partial\mathbf{H(x)}}{\partial\mathbf{x}^T} + \dfrac{\partial\mathbf{H(x)}^T}{\partial\mathbf{x}^T}\mathbf{y} - \dfrac{\partial\mathbf{H(x)}^T}{\partial\mathbf{x}^T}\mathbf{H(x)} - \mathbf{H(x)}^T\dfrac{\partial\mathbf{H(x)}}{\partial\mathbf{x}^T}\right).
\end{aligned}
\]

\subsection*{6(a)}
\begin{proof}
 By definition, we have
 \[
 E = \dfrac{1}{2}\sum\limits_j x_j^2\implies \dfrac{dE}{dt} =\sum\limits_j x_j \dot{x}_j .\tag{7}
 \]
Expanding the above sum from the Lorenz-'96 model, we have
\[
\begin{aligned}
\dfrac{dE}{dt} &= \sum\limits_j x_j (x_{j+1}x_{j-1} - x_{j-2}x_{j-1} - x_j + F)\\
&= F\sum\limits_j x_j + \sum\limits_j x_j(x_{j+1}x_{j-1} - x_{j-2}x_{j-1} - x_j)\\
&= F\sum\limits_j x_j + \sum\limits_j x_jx_{j+1}x_{j-1} - x_{j-2}x_{j-1}x_j - x_j^2
\end{aligned}
\]
Now, since index addition and subtraction is performed over a space where we have identified $J$ and $0$, and add modulo $J$, we want to consider the terms $x_jx_{j+1}x_{j-1}$ and  $- x_{j-2}x_{j-1}x_j$. Since each of these terms is a product of the three $x_j$ for consecutive values of $j$, their sum over all $j\in \{1,2,\ldots , J\}$ is equal, and hence they cancel out. This leaves us with
\[
\begin{aligned}
\dfrac{dE}{dt} &= F\sum\limits_j x_j - \sum\limits_j x_j^2\\
&= -2E + F\sum\limits_j x_j,
\end{aligned}
\]
which is the desired result.
\end{proof}
\subsection*{6(b)} Code is attached to the back of this assignment (integration routine is \url{boost::numeric::odeint::runge_kutta54_cash_karp}). The final timestep has the following values for the $x_j$, in order from $j=1$ to $J$, to two digist of precision:\\
\[ \mathbf{x} = (-1.5, 1.2, -0.54, 0.15, 1.1, 5.5, 1.1\cdot 10^1, -2.3, -0.37, 4.2) \]
\end{document}


%%
%%  appendixSVM.tex - Obstacle Detection and Planning for Autonomous Vehicles based on Computer Vision Techniques
%%
%%  Copyright 2014 Néstor Morales <nestor@isaatc.ull.es>
%%
%%  This work is licensed under a Creative Commons Attribution 4.0 International License.
%%

\graphicspath{{./images/chapter06/bmps/}{./images/chapter06/vects/}{./images/chapter06/}}

\chapter{Support Vector Machines}\label{ch:appendixSVM}

\acfp{SVM} are a set of linear classifiers usually employed both for classification and regression, which are based on methods of supervised learning. This technique is based on the Vapnik Chervonenkis (VC) dimension from the statistical learning theory and Structural Risk Minimization.
The original algorithm was developed by Vladimir Vapnik in 1963 as a linear classifier, but the most currently used version (soft margin) was proposed together by Vapnik and Corinna Cortes in 1995 \citep{cortes1995support}.

\section{Margin Maximization}\label{ch:appendixSVM_01}

Let $(x_1,y_1),\dots,(x_N,y_N)$, with $\begin{array}{l c r}
  x_k \in \mathbb{R}^m & y_k \in {-1,1} & k=1,\dots,N
\end{array}$ be the set of samples separated by the hyperplane $w^T \cdot x + b = 0$, where $x$ is a $N$-dimensional point and $w$ is a vector of weights. This hyperplane divides data, in the way that $w^T \cdot x_k + b > 0$ for the $x_k$ in a class, and $w^T \cdot x_k + b < 0$ for the $x_k$ in the other class. If data is separable, it is quite likely to have more than one way to do that. From all possible solutions, the \ac{SVM} selects that with the biggest distance from the hyperplane to the nearest data points. The described method takes advantage of this fact.
If training points are labeled in the way that $y_k \in \{ -1, +1\}$, where $+1$ is a positive sample, and $-1$ a negative sample, the following expression is obtained:

\begin{equation}\label{eq:appendixSVM_hyperplane_scaled}
  \begin{array}{l r}
    y_k (w^T \cdot x_k + b) \geq 1 & \forall k \in \{1, \dots, N \}
  \end{array}
\end{equation}

Based on this, both classes can be separated by two hyperplanes, $H_1: w^T \cdot x_k + b = 1$ and $H_2: w^T \cdot x_k + b = -1$, in the way that there are not data points inside the gap existing between $H_1$ and $H_2$. The distance between them is $2 / \|w\|$, where $\|w\|$ is the Euclidean norm of $w$. Therefore, the way in which the parameters that maximize this distance are obtained is by minimizing the following objective function:

\begin{equation}\label{eq:appendixSVM_objective_function}
 L(w) = {{\|w\|^2} \over 2}
\end{equation}

, such that $\begin{array}{l r}
y_k (w^T \cdot x_k + b) \geq 1 & \forall k \in \{1, \dots, N \}
\end{array} $.

To solve this, the dual problem must be obtained. After a few calculations, we obtain the following formulation:

\begin{equation}\label{eq:appendixSVM_dual_formulation}
 L_D (\alpha) = \sum_{k=1}^N \alpha_k - {1 \over 2} \sum_{k = 1, l=1}^N \alpha_k \alpha_l y_k y_l x_k^T x_l
\end{equation}

$L_D$ must be maximized. The training vectors $x_k$ with positive Lagrange multipliers $\alpha_k$ in any of the two hyperplanes are called Support Vectors (SV). SVs define the hyperplane parameters. Therefore, using the SVs it is possible to define a discriminative function like the next one:

\begin{equation}\label{eq:appendixSVM_discriminative_function}
 y = sign(w^T \cdot x + b) = sign \left(\sum_{k \in S} \alpha_k y_k x_k^T x + b \right)
\end{equation}

, where $S$ is the set containing all SVs.

\section{Soft Margin}\label{ch:appendixSVM_02}

In a real problem, it is very unlikely that a line divides data in an exact way. In fact, most of the times it is not desired to divide them accurately, even having the possibility of defining a non-linear decision boundary.
In these cases, the objective function \ref{eq:appendixSVM_objective_function} can be substituted by:

\begin{equation}\label{eq:appendixSVM_soft_objective_function}
 L(w) = {{\|w\|^2} \over 2} + C \sum_{k=1}^N \xi_k
\end{equation}

, where $\xi_k$ is the error allowed in the classification.

The solution of this problem can also be obtained through the dual formulation, as we did for the linear case. The non-zero $\alpha_k$ corresponds to the next two cases:
\begin{itemize}
 \item For SVs on either of two hyperplanes, $0 < \alpha_k < C$.
 \item For SVs inside the hyperplanes, $\alpha_k = C $.
\end{itemize}

\section{Non-linear \ac{SVM}}\label{ch:appendixSVM_03}

\acp{SVM} can also be used to determine non-linear separable surfaces. To do that, it is possible to use kernel based techniques. Using the appropriate kernel function we can find the separating hyperplane by using higher dimensions. This kernel function allows changing the problem of finding a non-linear separating surface in the original space by computing a separating hyperplane inside the high-dimensional space, allowing reducing the calculation costs and, at the same time, obtaining an optimal non-linear separating surface inside the original space, as described at \cite{burges1998tutorial}.

Let $\pi$ be the mapping of the data from the original space to higher dimensions. This mapping is given by several functions $K$:

\begin{equation}\label{eq:appendixSVM_kernel_func}
 K(x_1, x_2) = \pi(x_1)^T \cdot \pi(x_2)
\end{equation}

Using this kernel function, the objective function in \ref{eq:appendixSVM_dual_formulation} becomes:

\begin{equation}\label{eq:appendixSVM_soft_formulation}
 L_d(\alpha) = \sum_{k=1}^N  \alpha_k - {1 \over 2} \sum_{k = 1, l=1}^N \alpha_k \alpha_l y_k y_l K(x_k, x_l)
\end{equation}

And the discriminative function:

\begin{equation}\label{eq:appendixSVM_soft_discriminative_function}
 y = sign \left( \sum_{k \in S} \alpha_k y_k K(x_k, x) + b \right)
\end{equation}

Function $K$ can take several forms, being the most common those listed next:
\begin{itemize}
 \item Linear: $K(x_k, x_l) = x_k^T \cdot x_l$.
 \item Polynomial: $K(x_k, x_l) = (\gamma \cdot x_k^T \cdot x_l + r)^d$.
 \item \acf{RBF}: $K(x_k, x_l) = \exp(-\gamma \cdot \| x_k - x_l\|)$.
 \item Sigmoid: $K(x_k, x_l) = \tanh(\gamma \cdot x_k^T \cdot x_l + r)$
\end{itemize}

In the method described in chapter \ref{ch:chapter06}, we use the Gaussian \acf{RBF}, as it is able to produce continuous soft decision boundaries, making it the most suitable for the application.


\chapter{Localization}
Localization of an integral happens when the integral is exactly equal to its saddle-point approximation.
% The localization loci are then the saddle-points.
A trivial example is therefore the Gaussian integration formula, which the saddle-point method is based on:
\begin{equation}\label{GaussianIntegral}
 \int_{\mathbb{R}^n} d^n x \, e^{-\frac{1}{2} x^T A x} = \sqrt{\dfrac{(2\pi)^n}{\text{det}A}},
\end{equation}
where $A$ is a positive-definite $n\times n$ matrix.

In the case of path integrals in quantum field theories, which are of infinite dimension,
localization, if applicable, reduces them to finite dimensional integrals.
% This is achieved using functional generalizations of the Gaussian formula \eqref{GaussianIntegral},
% and the resulting integral is over the moduli space of the theory vacua.
This is an exact approach, in contrast to perturbative methods such as Feynman diagrams,
valid only in the weakly-coupled regime.
Unfortunately, very few path integrals are solvable,
but there are very specific classes of theories where localization does apply.
These are supersymmetric theories and topological quantum field theories,
from which we distinguish two types of localization:
supersymmetric localization and equivariant localization.
% As a matter of language, and
% this effect is often said to be \emph{semiclassically-exact}, or \emph{1-loop exact}.

Many results of this thesis are based on the supersymmetric localization.
We will review its basic idea and illustrate it explicitly in an example with an ordinary integral.
We refer the reader to \cite{Szabo:1996md} and \cite{Pestun:2016zxk} for reviews of the topic.

\section{Supersymmetric localization}

In the path integral formulation of quantum theories,
physical observables are expectation values of operators.
Let us consider the path integral (in the Euclidean signature and set $\hbar = 1$):
\begin{equation}
 \braket{O} = \int D\phi O \, e^{-S[\phi]},
\end{equation}
where $O$ is an operator, built out of the fields of the theory, represented by $\phi$.

Assume we can deform the action $S$ in such a way that it does not affect the physical quantity,
namely
\begin{equation}
 \braket{O}_t = \int D\phi e^{-S[\phi]-t Q V[\phi]},
\end{equation}
where
% $QV\geq 0$, and
$Q$ is a fermionic symmetry of the theory, and require
\begin{equation}
 \dfrac{d \braket{O}_t}{d t} = 0.
%  \overset{!}{=} 0.
\end{equation}
Integrating by parts and assuming vanishing boundary terms,
we obtain the following necessary conditions:
\begin{equation}\label{loc:susyCondition}
 Q^2 = 0, \quad Q S = 0, \quad Q O = 0,
\end{equation}
and that the measure $D\phi$ is also invariant (the symmetry must not be anomalous).

If this deformation were possible, then the deformation parameter $t$ could be of any value.
The most useful case is to take it to be infinitely large,
in order to use the saddle-point approximation.
Since by construction the integral is independent of $t$, the saddle-point approximation must be exact,
hence the path integral localizes to a set of loci.
These are determined by\footnote{
If the bosonic part is positive-definite.
The fermionic contribution of the localization action is subleading.}
\begin{equation}\label{loc:saddlePoint}
 (QV)_\text{Bosonic} = 0
\end{equation}
whose solutions---let us denote them by $\phi_*$---belong to the moduli space of vacua of the theory.
This means that some fields acquire a non-zero vacuum expectation value.
Accounting the fluctuations around the vacuum configuration,
the localized integral can be formally written as:
\begin{equation}
 \braket{O}=\int_\text{Moduli} D\phi_* O(\phi_*) e^{-S(\phi_*)} Z_\text{1-loop},
\end{equation}
where the integral over $\phi_*$ is analogous to the sum over all the saddle-points,
and $Z_\text{1-loop}$  is the functional generalization of the Gaussian integral \eqref{GaussianIntegral} for the fluctuations around the saddle-points.
It is thus a functional determinant, which is divergent in general.
In order to obtain a finite result, the theory is defined in a compact manifold, like a sphere,
such that the spectrum of the operator is discrete, and supersymmetry guarantees the cancellation of divergences between bosons and fermions.
Despite conceptual simplicity, the main challenge of this technique is to find the localization action $QV$, since no general recipe exists.

\section{A toy example}

Consider an ordinary integral
\begin{equation}
 I = \int_m^M dx \,p g'(x)\,e^{p g(x)},
\end{equation}
with $p$ being a constant.
It can be solved exactly as a total derivative:
\begin{equation}
 I = \int_m^M  dx \, \dfrac{d}{d x}\,e^{p g(x)}
   = e^{p g(M)}-e^{p g(m)}.
\end{equation}

If we were to solve it using the saddle-point approximation for $p \rightarrow \infty$,
then the saddle-points must be the endpoints $x_* = \{m, M\}$.
Let us consider this case and expand around the saddle-points:
\begin{equation}
 g(x_*+\xi) = g(x_*)+\frac{1}{2} g''(x_*) \xi^2 +\mathcal{O}(\xi^3).
\end{equation}
Each saddle-point contributes to the integral as following:
\begin{eqnarray}
 I_m &=& e^{p g(m)} \int_0^\infty  d\xi \, p g''(m) \xi\, e^{p \frac{1}{2} g''(m) \xi^2 }
      =- e^{p g(m)} \\
 I_M &=& e^{p g(M)} \int_{-\infty}^0 d\xi \, p g''(M) \xi\, e^{p\frac{1}{2} g''(M) \xi^2 }
      = e^{p g(M)}
\end{eqnarray}
valid for $\Re(p g'') < 0$.
The sum $I_m + I_M$ indeed gives the exact result.
% In conclusion, the integral $I$ is a trivial example of localization.

Now, let us see how we can use supersymmetric localization to solve it.
The integral $I$ can be rewritten in the supersymmetric form:
\begin{equation}
 I = \int_m^M \, dx \int da \int db \, e^{p(g(x)- a b g'(x))},
\end{equation}
where $a$ and $b$ are Grassmann numbers (our fermions),
which means they satisfy $a b = -b a$ and $a^2=b^2=0$, hence
\begin{equation} \label{GrassmannIntegral}
 f(x) = \int da \int db \, e^{-a\, f(x) \,b }.
\end{equation}

The action $S=p(g(x)-a b g'(x))$ is invariant under the supersymmetry transformation:
\begin{equation}
 \delta_\epsilon x = -\epsilon a,
 \quad
 \delta_\epsilon a = 0,
 \quad
 \delta_\epsilon b = \epsilon,
\end{equation}
where $\epsilon$ is a Grassmann number.
The supersymmetric operator, defined by $\epsilon Q = \delta_\epsilon$,
is explicitly
\begin{equation}
 Q = -a \frac{\partial }{\partial x}+\frac{\partial }{\partial b}.
\end{equation}
We can check that, indeed, \eqref{loc:susyCondition} are fulfilled.

Let us deform the integral with a $Q$-exact term
\begin{equation}
 I(t)=\int_m^M \, dx \int da \int db \, e^{p(g(x)-a b g'(x))+ t Q V},
\end{equation}
where
\begin{equation}
 V(x, a, b) = f(x) b
\end{equation}
that leads to a non-trivial localization action
\begin{equation}
 QV = f(x)-a b f'(x).
\end{equation}
This is actually the most general supersymmetric action we can write for this case.
Thus, $S$ is not just $Q$-closed (i.e. $QS=0$), but also $Q$-exact (i.e. $S=QV_s$).

We require the deformed integral to be independent of the deformation parameter $t$:
\begin{equation}
 I'(t)=0.
\end{equation}
An explicit and straightforward computation shows
\begin{eqnarray}
 I'(t) &=& \int_m^M \, dx \int da \int db \, e^{S + t Q V} Q V \\
       &=& \int_m^M \, dx \int da \int db \, Q (e^{S + t Q V} V)\\
       &=& \left. e^{p g(x)+ t f(x)} f(x) \right|^M_m
\end{eqnarray}
where we used the supersymmetry condition and nilpotency.
In this case, we must additionally require vanishing boundary conditions
\begin{equation}
 f(m)=f(M)=0.
\end{equation}

Now we can take large $t$ to solve the integral using the saddle-point method.
The boundary points here must be either global maxima or global minima of $f(x)$,
for $t>0$ or $t<0$, in order for the saddle-point integral to be convergent.
In other words, for $t$ negative (positive),
the bosonic part of the localization action is positive (negative) definite,
and it vanishes at the saddle-points:
\begin{equation}
 (QV)_\text{Bosonic}=0.
\end{equation}

The saddle-point approximation is exact, in the same fashion as for the large $p$ case we studied before.
This is to be expected,
since the deformed integral is still a total derivative when we integrate out the fermions:
\begin{equation}
 I(t) = \int_m^M  dx \, \dfrac{d}{d x}\,e^{p g(x) + t f(x)}
      = e^{p g(M)+ t f(M)}-e^{p g(m)+ t f(m)}.
%       = e^{p g(M)}-e^{p g(m)}.
\end{equation}

When $g(x)=\cos x$ and the integration region is extended to a sphere
parametrized by the polar angle $ x\in [0,\pi]$ and the azimuthal angle $\varphi \in [0, 2\pi]$,
this becomes a particular example of the Duistermaat-Heckman integration formula,
which is the precursor of the localization of path integrals.

% Witten used this example to illustrate the equivariant localization in the appendix of \cite{Witten:1992xu}.

% \begin{equation}
%   \int_0^{2\pi} d\phi \, \int_0^{\pi} \, d\theta \sin\theta \, e^{t \cos\theta}
% = \dfrac{2\pi}{t}\left(e^{t} - e^{-t}\right).
% \end{equation}
% The localization loci are the north and south pole, namely $\theta_*=\{0, \pi\}$.

% The theorem was later proved to be a special case of a more general localization property of equivariant cohomology
% and Berline and Vergne derived a localization formula for general compact Riemannian manifolds.
% The first infinite-dimensional generalization of the theorem,
% in the setting of a supersymmetric path integral,
% was worked out by Atiyah and Witten.