%        File: TDDD41-lectures.tex
%     Created: Wed Jan 27 10:00 AM 2016 C
% Last Change: Wed Jan 27 10:00 AM 2016 C
%
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{amsthm}
\usepackage{amsmath}
\newtheorem{definition}{Definition}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\title{TDDD41: Datamining - Clustering and Association Analysis\\Lecture Notes}
\author{Pontus Persson}
\date{VT-2016}
\begin{document}
\maketitle
\tableofcontents
\section{Partition Methods}
Partitions.
No overlap, not empty, all should be in.
k-mean - problem with outliers. Mitigate with metoids (PAM). CLARA, CLARANS?
\section{Hierarchical Methods}
\marginpar{\emph{Lecture 3\\2016-01-27}}
Build clusters with sub-clusters, by merging or splitting clusters.
\begin{description}
	\item[AGNES] agglomatric
	\item[DIANA] division
\end{description}

Complete-link clustering uses the longest distance of the objects inside a
cluster. (the maximum path).\\
Single-link uses the shortest distance instead.\\
Build the tree graph of clustering and introduce a threshold to see how many
clusters it gets.
\subsection{AGNES}
\begin{itemize}
	\item Implemented in static analysis
	\item Single-link
\end{itemize}
\subsection{DIANA}
\begin{itemize}
	\item Reverse order
\end{itemize}
Disadvantages - DO NOT SCALE! $O(n^2)$. Can not undo.

\subsection{Birch}
Balanced Iterative Reducing and Clustering using Hierarchies.
\begin{enumerate}
	\item Scan DB to build an initial in-memory CF tree (a multi-level
		compression of the data that tries to preserve the inherent
		clustering structure of the data)
	\item Use an arbitrary clustering algorithm to cluster the leaf nodes of
		the CF-tree
\end{enumerate}
Clustering feature: $CF=(N,LS,SS)$ $LS=\sum X_i$, $SS=\sum X_i^2$ X is
coordinates.\\
EX: CF=(5,(16,30),(54,190)).
\begin{description}
	\item[Centroid] $\frac{LS}{N}$
	\item[Add two clusters together] $CF_1+CF_2$
\end{description}
A CF-tree is a height balanced tree (all leaves at the same level). All nodes
include CF, a nonleaf node in a tree stores the sums of the CFs of their
children. Leaves stores the actual data.
\begin{description}
	\item[Branch factor] Maximum number of children
	\item[threshold] max diameter of sub-clusters stored at the leaves
\end{description}
When creating the CF-tree, add element to same node if it fits.
Otherwise ``split'', take the two object with the furthest distance, put one of
them in the first cluster and the other one in the second (newly created)
cluster. Add the third element in the cluster where the other object is the
closest.
As soon as the leaves are created, create a parent with the sum of its
children's
CFs.
\\Next element: start at top of tree, check centroid of children and choose
the closest one.
\begin{lstlisting}
	If T requirement satisfied
	then if data in cluster is not too large
		Add data point
	else
		split
\end{lstlisting}
After creating the tree we can use for example PAM. Scales linearly. Handles
only numeric data and is sensitive to the order of the data.

\subsection{ROCK}
Robust, Clustering using Links. Works on categorical data.\\
\begin{definition}
	$p_1$ and $p_2$ are neighbours\\
	iff $sim(p_1,p_2)\geq t $ and $sim\in[0,1]$
\end{definition}
\begin{definition}
	Link($p_1,p_2)$ is the number of common neighbours.
\end{definition}
\begin{definition}
	$Link(C_i,C_j)=$ the number of cross links between clusters $C_i,
	C_j$.\\
	$\sum Link(p_i,p_j)$
\end{definition}
\begin{definition}
	Goodness $G(C_i,C_j)$ when to merge $= Link(C_i,C_j)$ divided by the
	expected number of cross links.
\end{definition}
\begin{itemize}
\item	Drag random sample
\item Hierarchical clustering with links using goodness
\item Label data in disk, add other data to cluster with most neighbours.
\end{itemize}
\marginpar{\emph{Lecture 4\\2016-02-02}}
Expected number of cross-links between $C_i$ and $C_j$.	= Expected number of
links in $C_i\cup C_j - $ Expected number of links in $C_i -$ Expected number of
links in
$C_j$.\\
Expected number of links in $C_i =$ number of data points in $C_i *$
contribution of one data point to the ``links'' in $C_i$\\
Assume $m_i$ data points in $C_i$. Assume each point in $C_i$ has $m_i^{f(t)}$
neighbours. T is similarity threshold. $f(t)\leq 1$. For market data
$f(t)=\frac{1-t}{1+t}$.\\
Data point contributes to link when it is a common neighbour for two data
points.
\begin{itemize}
	\item It is a common neighbour for:
		\begin{itemize}
			\item Its own neighbours
		\end{itemize}
	\item How many times is it a common neighbour?
		\begin{itemize}
			\item For each pair of its neighbours. $=m_i^{2f(t)}$
		\end{itemize}
\end{itemize}
\begin{definition}
	Expected number of links in $C_i = m_i^{1+2f(t)}$
\end{definition}
\begin{definition}
	Expected number of links in $C_i\cup C_j = (m_i+m_j)^{1-2f(t)}$
\end{definition}
\subsection{CHAMELEON: Hierarchical Clustering Using Dynamic Modeling}
\begin{enumerate}
	\item Use a graph partitioning algorithm: cluster objects into a large
		number of relatively small sub-clusters
		\begin{itemize}
			\item Based on k-nearest neighbour graph (connect two if
				one is among k nearest neighbours)
			\item Similarity on edges
			\item Edge weight is density of region
		\end{itemize}
	\item Use an agglomerative hierarchical clustering algorithm:
		\begin{itemize}
			\item Edge cut: cut the lowest edges to create separated
				graphs
			\item Cut either until minimum number of edges in a
				cluster. Maximum $1-2\%$ of data points in a
				cluster
			\item Merge if
				\begin{itemize}
					\item Closeness of clusters: avg
						similarity between points in the
						clusters that are connected to
						both clusters.
					\item Interconnectivity of clusters:
						normalized sum of the weights of
						the edges that connect nodes int
						the clusters.
						$\frac{2*EC_{C_i,C_j}}{EC_{C_i}-EC_{C_j}}$.
						$EC_{C_i}=$ When dividing
						$C_i$ in two approx. equal
						parts.
					\item Merge if both measurements are
						above user=defined thresholds
				\end{itemize}
		\end{itemize}
\end{enumerate}
\section{Density-Based Clustering}
\begin{itemize}
	\item Eps: Max radius of the neighbourhood
	\item MinPts: Min. number of points in an Eps-neighbourhood of that
		point.
\end{itemize}
\begin{definition}
	A point is density reachable from a point q if there is a chain of
	points such that each are density reachable.
\end{definition}

\begin{definition}
	A pont p is density-connected to a point q if there is a point o that is
	density-reachable to both p and q.
\end{definition}
\begin{lemma}
	p density-reachable to q $\Rightarrow$ p density-connected to q
\end{lemma}
\begin{lemma}
	If p DDR for q $\Rightarrow$ p DR for q
\end{lemma}
\subsection{DBSCAN}
\begin{itemize}
	\item Put all density-connected points in a cluster.
	\item Core points
	\item Border points, connected to a core point, but not enough
		neighbours to be a core point
	\item Outliers, not connected to a core point and not enough to be a
		core point
\end{itemize}
Algorithm:
\begin{enumerate}
	\item Select point p
	\item Retrieve all density-reachable points
	\item If p is core point, form cluster
	\item If p is a border point, no point are density-reachable from p and
		DBSCAN visits next point from the database
	\item Continue untill all of the points have been processed
\end{enumerate}
\subsection{OPTICS}
Order the data in such a way that you can look at different epsilons at the same
time (preprocessing).\\
\begin{definition}
	Core distance is the smallest Eps needed between p and an object in its
	eps-neighbourhood such that p would be a core object.
\end{definition}
\begin{definition}
	Reachability distance of p and o\\
	max(core-distance(o),d(o,p)) if o is a core object
\end{definition}
Algorithm:
\begin{enumerate}
	\item Select non-processed object o
	\item Find neighbours (eps-neighbourhood)
	\item Compute core distance for o
	\item Mark o as processed
	\item If o is not a core, restart at (1)
	\item Put neighbours of o in Seedlist and order
		\begin{itemize}
			\item If neighbour n is not yet in Seedlist then add (n,
				reachability from o) else if reachability from o
				< current reachability, then update reachability
				+ order Seedlist wrt reachability
		\end{itemize}
	\item Take new objec from SeedList with smallest reachability and go to
		(2)
\end{enumerate}
You can then plot the new order (x) with eps (y) to see how many clusters and
outliers you get for different eps.
\marginpar{\textit{Lecture 6\\2016-02-16}}
\section{Association rules}
Look for patterns on form $antecedent \rightarrow consequent$. ``If something
happens something else with happen with a certain probability''. Can be used for
revenue maximization. Market basket analysis.
\begin{definition}
		Frequent itemset. A set of items bought together frequently
\end{definition}
Association rules can be produced from the frequent itemsets.
\begin{definition}
		Support $= p(X,Y) =$ probability that a transaction contains $X\cup Y$
\end{definition}
\begin{definition}
		Confidence $= p(Y|X) = \frac{p(X,Y)}{p(X)}$
\end{definition}
High support, many cases. High confidence, rule is likely to be ``true''.
\textbf{Goal:} Find all rules $X\rightarrow Y$ with at least minimum support and
confidence.\\
\textbf{Solution:}
\begin{itemize}
		\item Find all itemsets with minimum support (Apriori, FP grow
				algorithms)
		\item Generate all the rules with minimum confidence from the frequent
				itemsets
\end{itemize}

\begin{theorem}
		Any subset of a frequent itemset is frequent. Or, any superset of an
		infrequent itemset set is infrequent
\end{theorem}
\subsection{Apriori algorithm}
\begin{enumerate}
		\item Scan db  once to get frequent 1-itemsets
		\item Generate candidates to frequent (k+1)-itemsets from frequent
				k-itemsets
		\item Test the candidates against database
		\item Terminate when no frequent or candidate itemsets can be generated
\end{enumerate}
\textbf{Create new candidates from candidates with the same prefix (all elements but the last
one).}\\
\begin{enumerate}
		\item Self joining
		\item Pruning (Prune candidates whose subsets are not frequent)
\end{enumerate}

\begin{lemma}
		All frequent (k+1)-itemsets are in $C_{k+1}$
\end{lemma}
\begin{proof}
		$k=0, L_1\subseteq C_1$ Trivial\\
		Hypothesis: $L_s\subseteq C_s \forall s\leq k$\\
		Show that it wont be removed in self-join and pruning.
\end{proof}
\subsubsection{Produce rules}
Generate all rules on the form $a\rightarrow l-a$ with minimum confidence from a
large itemset $l$.\\
If a subset $a\subseteq l$ does not generate a rule,  then neither does any
subset of $a$.\\
Start with rules with a large antecedent and try to make new rules with smaller
antecedents of subsets of ``good rules''.\\
Problems with Apriori:
\begin{itemize}
		\item Too many candidates to generate
		\item Each candidate implies expensive operations
\end{itemize}
\marginpar{\emph{Lecture 7\\2016-02-17}}
\subsection{Frequent pattern growth algorithm}
\begin{enumerate}
		\item Scan db once, record items as the frequent 1-itemsets.
		\item Sort itemset in frequency descending order. (f-list)
		\item Create tree
				\begin{enumerate}
						\item First transaction is put as left children
						\item Put next transaction in tree and create minimum
								new branches. Mark nodes with number of
								transactions it represents
				\end{enumerate}
		\item Also create header table linking all items to their place is the
				tree. If there are multiple instances of an item in the tree,
				the nodes should keep track of each other.
		\item For each frequent item in the header table
				\begin{itemize}
						\item Traverse the tree by following the corresponding
								link
						\item Record all prefix paths leading to the item. This
								is the item's conditional pattern base
				\end{itemize}
		\item For each conditional pattern base, Start the process again
				recursively
\end{enumerate}
\begin{definition}
		$lift(X,Y) =
		\frac{support(X,Y)}{support(X)support(Y)}=\frac{p(Y|X)}{p(Y)}$
		\begin{itemize}
				\item $lift >1$ positive correlation
				\item $list <1 $ negative correlation
				\item $lift = 1$ independent
		\end{itemize}
\end{definition}
\marginpar{\textit{Lecture 8\\2016-02-23}}
\subsection{Constrained frequent itemset mining}
Only find rules that apply to a specified constraint. \\
A Constraint C(.) is:
\begin{description}
		\item[Monotone] If $C(A)$ then $C(B) ,\forall A\subseteq B$
		\item[Antomonotone]
				\begin{itemize}
						\item
						\item If $C(A)$ then $C(B) ,\forall B\subseteq A$
						\item If not $C(B)$ then not $C(A)$
				\end{itemize}
\end{description}
Example:
\begin{align*}
		sum(S.price) &\geq v \mbox{ Is monotone}\\
		sum(S.price &\leq v \mbox{ Is anti monotone}
\end{align*}
Constraints that are not monotone or anti monotone can sometimes be converted.
\subsubsection{Apriori algorithm with constraints}
You could remove frequent itemsets not matching the constraints with a simple
brute force approach.
\begin{itemize}
		\item If anti monotone, cross off items early to not do any extra work.
				(Prune search space)
		\item If monotone, does not prune, but avoids constrains checking.
				Superset of constraint OK is always OK.
\end{itemize}
\subsubsection{FP grow algorithm and constraints}
With anti monotone constraints:
\begin{itemize}
		\item Remove items that do not satisfy the constraint
		\item If the conditioning itemset $\alpha$ does not satisfy the
				constraint, then do not generate $\alpha$ nor its conditional
				database.
		\item Let $\beta$ denote the frequent items in the conditional database
				database of $\alpha$. If $\alpha \cup \beta$ satisfies the
				constraint, then do not check the constraint in the conditional
				database of $\alpha$.
\end{itemize}
With monotone constraints:
\begin{itemize}
		\item If the conditioning itemset $\alpha$ satisfies the constraint,
				then do not check the constraint in its conditional database.
\end{itemize}
\subsection{Constraints}
\begin{itemize}
		\item Average is neither monotone nor anti monotone
		\item Convertible monotone
				\begin{itemize}
						\item If there exists an item order R such that:
								\begin{itemize}
										\item If $C(A) \Rightarrow C(B), \forall
												A,B$ respecting R such that A is
												a suffix of B.
								\end{itemize}
						\item E.g. $avg(S.Price) \geq v$ with respect to
								decreasing price order
				\end{itemize}
		\item Convertible anti monotone
				\begin{itemize}
						\item It there exists an item order R such that:
								\begin{itemize}
										\item If $C(A) \Rightarrow C(B), \forall
												A,B$ respecting R such that B is
												a suffix of A
										\item Or if $\lnot C(B) \Rightarrow
												\lnot C(A), \forall A,B$
												respecting R such that B is a
												suffix of A
								\end{itemize}
						\item E.g. $avg(S.Price)\geq v$ with respect to
								increasing price order
				\end{itemize}
\end{itemize}
\begin{definition}
		If a constraint is \textbf{both} convertible monotone and convertible
		anti monotone, it is \textbf{strongly convertible}.
\end{definition}
\marginpar{\textit{Lecture 9\\2016-02-24}}

\end{document}


\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{tikz-cd}
\usepackage{mathrsfs}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage{enumitem}
\usepackage{yfonts}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhfoffset[L]{1cm} % left extra length
\fancyhfoffset[R]{1cm} % right extra length
\rhead{Nov 16, 2021}
\lhead{\bfseries Rui Qiu (rq47)}
\rfoot{}

\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}

\newtheorem{defn}[thm]{Definition}
\newtheorem{eg}[thm]{Example}
\newtheorem{ex}[thm]{Exercise}
\newtheorem{conj}[thm]{Conjecture}
\newtheorem{cor}[thm]{Corollary}
\newtheorem{claim}[thm]{Claim}
\newtheorem{rmk}[thm]{Remark}

\newcommand{\ie}{\emph{i.e.} }
\newcommand{\cf}{\emph{cf.} }
\newcommand{\into}{\hookrightarrow}
\newcommand{\dirac}{\slashed{\partial}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\LieT}{\mathfrak{t}}
\newcommand{\T}{\mathbb{T}}

\begin{document}

\title{ANLY-511 Midterm \\ Part (I) - Theoretical}

\author{Rui Qiu (rq47)}

\date{November 16, 2021}

\maketitle

\section{Problem 1: Joint distribution and conditional expectations}

\subsection{a. Find the joint probability density function.}

\[
	f(x,y)= \frac{\partial^2}{\partial x\partial y}F(x, y)=\begin{cases}
		\frac{\partial^2}{\partial x\partial y}C\left(x^3y+x^2y^3\right)dxdy = C(3x^2+6xy^2)&,\ (0\leq y\leq 3, 0\leq x \leq 2)\\
			0&,\ \text{otherwise.}
	\end{cases}
\]

\subsection{b. Find the value of $C$.}

Suppose the upperbound and the lowerbound of $x$ and $y$ are $x_1$, $x_2$, $y_1$, $y_2$ respectively. Then,

\[
\begin{split}
	P(0\leq x\leq 2, 0\leq y\leq 3) &= F(x_2, y_2) - F(x_1, y_2) - F(x_2, y_1) + F(x_1, y_1) \\
	& = C\times 4 \times 3 \times (2 + 3^2) \\
	& = 132C\\
	& = 1
\end{split}
\]

Therefore,

$$C = \frac{1}{132}$$

\subsection{c. Find the marginal density of $X$.}

We can now plug in the value of $C$ to the result of (a).

\[
f(x,y) = \begin{cases}
	\frac{1}{44}\left(x^2 + 2xy^2\right),&\ 0\leq y\leq 3, 0\leq x \leq 2\\
	0,&\ \text{otherwise.}
\end{cases}
\]

So the marginal density of $X$ is:

\[
\begin{split}
	f_X(x) &=\int^3_0\frac{1}{44}\left(x^2+2xy^2\right)dy\\
	&=\frac{1}{44}\left(x^2y + \frac{2}{3}xy^3\right)\bigg\rvert^3_{y=0}\\
	&= \frac{1}{44}\left(3x^2+18x\right)
\end{split}
\]

\subsection{d. Find the conditional expectation $E(Y|X)$.}

The conditional expectation can be directly computed as:

\[
\begin{split}
	E(Y|X) &=\int^3_0 y\cdot f_{Y|X}(y|x) dy\\
	&=\frac{1}{f_X(x)}\int^3_0 y\cdot f(x,y)dy \\
	&=\frac{44}{3x^2+18x}\int^3_0\frac{1}{44}y\cdot\left(x^2+2xy^2\right)dy\\
	&=\frac{1}{3(x^2+6x)}\int^3_0\left(x^y+2xy^3\right)dy\\
	&=\frac{1}{3(x^2+6x)}\left(\frac12x^2y^2+\frac24xy^4\right)\bigg\rvert^3_{y=0}\\
	&=\frac{1}{3(x^2+6x)}\left(\frac12 x^2\times 9 +\frac12 x\times  81\right)\\
	&=\frac{1}{3(x^2+6x)}\frac92 \left(x^2+9x\right)\\
	&=\frac{3(x^2+9x)}{2(x^2+6x)}
\end{split}
\]

There is an extra limitation for marginal density $f_X(x)$, since it serves as the denominator in the formula, it must not be 0.

\[\frac{1}{44}\left(3x^2+18x\right)\not=0\]

This implies $x\not=0$, so we can further simplify the formula above:

\[E(Y|X) = \frac{3(x+9)}{2(x+6)},\ 0<x\leq 2.\]

\subsection{e. Find the probability $Pr(X\leq Y)$.}

Imagine a two-dimensional x-y plane, the PDF is actually the value on z-axis, and the CDF is the space that is bounded by a range of $x$, a range of $y$ and their corresponding $f(x, y)$.

$P(X\leq Y)$ can be divided into two parts:

\begin{itemize}
	\item $P(0\leq X \leq Y, 0\leq Y\leq 2)$.
	\item $P(0\leq X\leq 2, 2\leq Y\leq 3)$. We have to understand that $X$ has its own range $[0, 2]$, once $Y$ exceeds $2$, $X$ can be integrated over $[0, 2]$ only, it cannot surpass $2$.
\end{itemize}

The calculation can be broken down as:

\[
\begin{split}
	P(0\leq X\leq Y, 0\leq Y\leq 2) &=\int^2_0\int^y_0\frac{1}{44}\left(x^2+2xy^2\right)dxdy\\
	&=\frac{1}{44}\int^2_0\left(\frac13 x^3 + x^2y^2\right)\bigg\rvert^y_{x=0}dy \\
	&=\frac{1}{44}\int^2_0\left(\frac13 y^3 + y^4\right)dy\\
	&=\frac{1}{44}\left(\frac1{12}y^4+\frac15 y^5\right)\bigg\rvert^2_{y=0}\\
	&=\frac{1}{44}\left(\frac{1}{12}\times 16 + \frac{1}{5}\times 32\right)\\
	&=\frac{1}{44}\times\frac{116}{15}\\
	&=\frac{29}{165}\\
	P(0\leq X\leq2, 2\leq Y\leq 3)&=\int^3_2\int^2_0\frac{1}{44}\left(x^2+2xy^2\right)dxdy\\
	&=\frac{1}{44}\int^3_2\left(\frac{1}{3}x^3+x^2y^2\right)\bigg\rvert^2_{x=0}dy\\
	&=\frac{1}{44}\int^3_2\left(\frac{8}{3}+4y^2\right)dy\\
	&=\frac{1}{44}\left(\frac{8}{3}y +\frac{4}{3}y^3\right)\bigg\rvert^3_{y=2}\\
	&=\frac{1}{44}\left[\left(\frac{8}{3}\times 3 + \frac{4}{3} \times 27\right)-\left(\frac{8}{3}\times 2 + \frac{4}{3}\times 8\right)\right]\\
	&=\frac{1}{44}\left[44 - 16\right]\\
	&=\frac{7}{11}
\end{split}
\]

Therefore, we add them up:

\[P(X\leq Y) =\frac{29}{165} + \frac{7}{11} = \frac{134}{165} \approx 0.8121\]

\section{Problem 2: MLE}

\subsection{a. Find the likelihood function.}

Let $\mathbf{x} = (X_1, X_2, \dots, X_n)$ be the sample vector.

\[
\begin{split}
	L(\theta|\mathbf{x}) &= \prod^n_{i=1}f(x_i|\theta) \\
	&=\frac{2x_1}{\theta}\exp\left(-\frac{x_1^2}{\theta}\right)\cdot
	\frac{2x_2}{\theta}\exp\left(-\frac{x_2^2}{\theta}\right)\cdots
	\frac{2x_n}{\theta}\exp\left(-\frac{x_n^2}{\theta}\right)\\
	&=\frac{2^n}{\theta^n}\left(\prod^n_i x_i\right)\exp\left(-\frac{\sum^n_{i=1}x_i^2}{\theta}\right)
\end{split}
\]

\subsection{b. Find the log-likelihood.}

Directly take the log on $L(\theta|\mathbf{x})$, then we have:

\[
\begin{split}
\log L(\theta|\mathbf{x}) &=n\log\left(\frac{2}{\theta}\right) +\sum^n_{i=1}\log x_i - \frac{\sum^n_{i=1}x_i^2}{\theta}\\
&=n\log2-n\log\theta +\sum^n_{i=1}\log x_i - \frac{\sum^n_{i=1}x_i^2}{\theta}
\end{split}
\]

\subsection{c. Find the MLE of $\theta$.}

Take the derivative of log-likelihood:

\[
\begin{split}
	\frac{\partial}{\partial\theta}\log L(\theta|\mathbf{x})&= -\frac{n}{\theta}+\frac{\sum^n_{i=1}x_i^2}{\theta^2} = 0\\
	\frac{\sum^n_{i=1} x_i^2}{\theta^2} &= \frac{n}{\theta}\\
\end{split}
\]

Since $\theta>0$, we can simplify and calculate the equation above:

\[
\begin{split}
	\sum^n_{i=1}x_i^2 &= n\theta \\
	\hat{\theta}_{MLE}&=\frac{\sum^n_{i=1}x_i^2}{n}
\end{split}
\]

\subsection{d. Find the method of moments estimate (MOM) of $\theta$.}

For MOM, we have the following as our first moment:

\[
\begin{split}
	E(X) &=\int^\infty_0 x\frac{2x}{\theta}\exp\left(-\frac{x^2}{\theta}\right)dx\\
	&=\frac{\sqrt{\pi}\sqrt{\theta}}{2}=\overline{X}\\
	\sqrt{\pi\theta}&=2\overline{X}\\
	\pi\theta &=4(\overline{X})^2
\end{split}
\]

Therefore,

\[
\hat{\theta}_{MM} = \frac{4(\overline{X})^2}{\pi} = \frac{4}{\pi}\left(\frac{\sum^n_{i=1}x_i}{n}\right)^2
\]

\subsection{e. Is the MOM estimator unbiased (part d)?}

Basically, this is to show if $E\left[\hat{\theta}_{MM}\right]$ is equal to $\theta$.

First, we rewrite the expectation as:

\[E\left[\hat{\theta}_{MM}\right] = \frac{4}{\pi} E\left[\left(\frac{1}{n}\sum^n_{i=1}x_i\right)^2\right] = \frac{4}{\pi n^2}\sum^n_{i=1}\sum^n_{j=1}E\left(X_iX_j\right)\ \ \ \ \ (\star)
\]

Since for $i\not=j$, $X_j$ and $X_j$ are independent samples, this implies the linearity of their expectations, we have:

\[E[X_iX_j]=E[X_i]E[X_j]=\frac{\pi}{4}\theta\ \ \ \ \ (\star\star)\]

But for the case when $i=j$,

\[
\begin{split}
E[X_iX_j] &= E[X_i^2]\\
&= \int^\infty_0 x^2\cdot\frac{2x}{\theta}\exp\left(-\frac{x^2}{\theta}\right)dx\\
&= \frac{2}{\theta}\left(\frac{\theta^2}{2}-0\right)\\
&=\theta\ \ \ \ \ (\star\star\star)
\end{split}
\]

We plug in both $(\star\star)$ and $(\star\star\star)$ to $(\star)$. Note that there are $n(n-1)$ cases of $(\star\star)$, and $n$ cases of $(\star\star\star)$.

\[
E[\hat{\theta}_{MM}]=\frac{4}{\pi n^2}\left(n(n-1)\frac{\pi}{4}\theta + n\theta\right)=\left(1+\frac{4-\pi}{\pi n}\right)\theta\not=\theta
\]

Clearly, the expected value of $\hat{\theta}_{MM}$ is not equal to $\theta$, so it is biased.

\section{MCQ (problem 3 - 10)}

\begin{itemize}
	\item \textbf{Problem 3}: ABCD
	\item \textbf{Problem 4}: C
	\item \textbf{Problem 5}: A
	\item \textbf{Problem 6}: AC
	\item \textbf{Problem 7}: BC
	\item \textbf{Problem 8}: CD
	\item \textbf{Problem 9}: AC
	\item \textbf{Problem 10}: AD. (Note: The choices are A, B, D, E. By ``AD" I choose the first and the third options.)
\end{itemize}

\end{document}


\chapter{Quantities and Units in Mechanics}
\section{SI Units}


\subsubsection{Fundamental features}
\label{sec:methods_features_fundamental}

The dataset of bitcoin prices and volume comes with open, close, highest and
lowest daily prices, and volume expressed in transacted coins and their market
value in USD. Figures \ref{fig:btc_prices_all} and \ref{fig:btc_prices_split}
show the bitcoin daily prices. Figure \ref{fig:btc_volume} shows the volume
series.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/btc_prices_all.png}
    \caption{Overlapped open, close, high and low bitcoin daily prices in USD.}
    \label{fig:btc_prices_all}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/btc_prices_split.png}
    \caption{Split of open, close, high and low bitcoin daily prices in USD.}
    \label{fig:btc_prices_split}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/btc_volume.png}
    \caption{The graph on the left shows the bitcoin daily volume. The graph on the right shows the market volume valuation in USD.}
    \label{fig:btc_volume}
\end{figure}

For volume series, the fractional differentiation was not enough so a log transformation
was added to stabilize the mean. Results can be seen in figure \ref{fig:btc_volume_log}.

\begin{figure}[!htb]
    \centering
    \includegraphics[width=\textwidth]{methods/images/btc_volume_log.png}
    \caption{Same features as in \ref{fig:btc_volume} but taking the logarithm.}
    \label{fig:btc_volume_log}
\end{figure}

Price series require fractional differentiation as explained in
\ref{sec:methods_features}. We could obtain the best $d^*$ for each price time
series applying the method previously described. Ten samples in the range
$[0, 1]$ are taken to look for the best $d^*$ which shields the results in table
\ref{table:frac_diff_prices}.

\begin{table}[!htb]
  \begin{center}
    \begin{tabular}{ | c | c | }
      \hline
      Feature & $d^*$ \\
      \hline
      Close   & 0.4   \\
      \hline
      Open    & 0.4   \\
      \hline
      High    & 0.4   \\
      \hline
      Low     & 0.2   \\
      \hline
    \end{tabular}
    \caption{Fractional differentiation order for price series.}
    \label{table:frac_diff_prices}
  \end{center}
\end{table}

Just for illustration purposes, see figure \ref{fig:close_ffd} that shows the
Close price series and the same series with an overlap of the fractionally
differentiated counterpart. Note the $y$ axis on the right which tracks the
scale of the fractionally differentiated Close price series. It can be seen
perfectly the effect of differentiating the series.

\begin{figure}[!htb]
    \centering
    \includegraphics[width=\textwidth]{methods/images/btc_close_ffd.png}
    \caption{Close price and fractionally differentiated Close price with $d^* = 0.4$ (see table \ref{table:frac_diff_prices}).}
    \label{fig:close_ffd}
\end{figure}

On the other hand, other features were created out of the Close price series.
Those features features are:

\begin{itemize}
  \item RSI: Relative Strength Indicator. It is a momentum index that measures
        the strength or weakness of a stock price. Some time windows are used in
        favor of capturing high speed and low speed trends. See figure
        \ref{fig:rsi}.
  \item Autocorrelation: with different lags and time window lengths, the price
        series autocorrelation looks for repetitive patterns in the signal.
        Peaks in the autocorrelation signal indicate an occurrence of
        repetition. See figure \ref{fig:autocorrelation}
  \item Logarithm of the returns: just what the index expresses. It uses a
        logarithm transform to control the variance of the series. See figure
        \ref{fig:log_returns} and the histogram of values.
  \item Volatility: computed as the standard deviation of the moving average of
        the log returns. That yields the trend in return variation. Multiple
        time windows are used to capture different speeds. See figure
        \ref{fig:volatility}.
\end{itemize}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/rsi.png}
    \caption{RSI indexes for Close prices.}
    \label{fig:rsi}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/autocorrelation.png}
    \caption{Different autocorrelation signals with different lags and time
    windows.}
    \label{fig:autocorrelation}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/log_returns.png}
    \caption{Logarithm of returns on the left, the histogram of the logarithm of returns on the right.}
    \label{fig:log_returns}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/volatility.png}
    \caption{Volatility index.}
    \label{fig:volatility}
\end{figure}


\chapter{Preliminaries}\label{c:preliminaries}
In this section, we present the basic principles of cryptography and also some fundamental cryptographic protocols that are essential for the subsequent sections.

\input{encryption}
\input{symmetric-encryption}
\input{pk-encryption}
\input{homomorphic-encryption}
\input{smpc}
\input{secret-sharing}


\section{Introduction}

\begin{frame}{Lecture 1 -- Outline}
  In this lecture, we introduce the concept of {\bf mathematical proofs}:\bigskip

  \begin{itemize}
    \item {\bf Section 1:} What are proofs, and why we need them;
    \item {\bf Section 2:} Some proof methods;
    \item {\bf Section 3:} Logical formulas and satisfiability;
  \end{itemize}

  \bigskip
  \begin{block}{}
  This lecture covers the textbook's chapters 1, 2 and 3.
  \end{block}
\end{frame}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{dwindow}
\section*{\hspace*{-1.6cm} dwindow}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Derive a window.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
dh = dwindow(h)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty dwindow} derives the window {\ty h}.\\
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Example}
\begin{verbatim}
         h=window(200,'hanning');
         subplot(211); plot(h);
         subplot(212); plot(dwindow(h));
\end{verbatim}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
window.
\end{verbatim}
\end{minipage}


\pagebreak
\chapter{Snippets}
\label{appndx:snippets}

\noindent Snippet on \href{https://gitlab.com/ttis-simulations/ttis-software/-/snippets/2043270}{GitLab}.
\begin{lstlisting}[language=C,caption={Sphere \gls{CAD} model generation code in \texttt{geo} format},captionpos=b, label=lst:sphere_code]
SetFactory("OpenCASCADE");

head_radius = 87;

// Ratio values from Grossman et al. 2017
brain_ratio = 0.83;
csf_ratio = 0.023;
skull_ratio = 0.085;
scalp_ratio = 0.05;

Sphere(1) = {0, 0, 0, head_radius};
Sphere(2) = {0, 0, 0, head_radius*(brain_ratio + csf_ratio + skull_ratio + scalp_ratio)};
Sphere(3) = {0, 0, 0, head_radius*(brain_ratio + csf_ratio + skull_ratio)};
Sphere(4) = {0, 0, 0, head_radius*(brain_ratio + csf_ratio)};
Sphere(5) = {0, 0, 0, head_radius*(brain_ratio)};

Physical Volume("Outer Boundary", 1) = {1};
Physical Volume("Skin", 2) = {2};
Physical Volume("Skull", 3) = {3};
Physical Volume("CSF", 4) = {4};
Physical Volume("Brain", 5) = {5};

Physical Surface("Outer Boundary", 1) = {1};
Physical Surface("Skin", 2) = {2};
Physical Surface("Skull", 3) = {3};
Physical Surface("CSF", 4) = {4};
Physical Surface("Brain", 5) = {5};

Mesh 2;
RefineMesh;
RefineMesh;
RefineMesh;

Mesh 3;
RefineMesh;
\end{lstlisting}

\pagebreak
\noindent Snippet on \href{https://gitlab.com/ttis-simulations/ttis-software/-/snippets/2067442}{GitLab}.
\begin{lstlisting}[language=Python,caption={Meshing of the spherical layered model},captionpos=b,label=lst:sphere_model_meshsing]
import os
import sys
import pymesh

sys.path.append(os.path.realpath('library path containing the necessary scripts'))

import Meshing.MeshOperations as MeshOps

base_path = 'desired base path'

# Change the angles to your desired configuration
elec_attributes = {
	'electrodes': {
		'B_VCC': {
			'theta': 258.5217,
			'phi': 0
		},
		'B_GND': {
			'theta': 326.2893,
			'phi': 0
		},
		'D_VCC': {
			'theta': 101.4783,
			'phi': 0
		},
		'D_GND': {
			'theta': 33.7107,
			'phi': 0
		},
	},
	'cylinder_radius': 4,
	'cylinder_width': 3,
	'skin_radius': 0,
	'elements': 200,
}

skin_stl = pymesh.load_mesh(os.path.join(base_path, 'spheres_skin.stl'))

meshing = MeshOps.MeshOperations(skin_stl, elec_attributes)
meshing.load_surface_meshes(base_path, ['spheres_skin.stl', 'spheres_skull.stl', 'spheres_csf.stl', 'spheres_brain.stl'])

meshing.sphere_model_meshing(os.path.join(base_path, 'meshed_model_sphere.poly'))
\end{lstlisting}

\pagebreak
\noindent Snippet on \href{https://gitlab.com/ttis-simulations/ttis-software/-/snippets/2067447}{GitLab}.
\begin{lstlisting}[language=Python,caption={\gls{PHM} model meshing},captionpos=b,label=lst:phm_model_meshsing]
import importlib
import os
import gc
import sys
import yaml

with open(os.path.realpath('settings file path')) as stream:
    settings = yaml.safe_load(stream)

sys.path.append(os.path.realpath(settings['SfePy']['lib_path']))

import FEM.Solver as slv

models = ['103414', '105014', '105115', '110411', '111716', '113619', '117122', '163129', '196750']

for model in models:
    settings['SfePy']['real_brain']['mesh_file'] = 'model path prefix' + model + 'model suffix'

    solve = slv.Solver(settings, 'SfePy', '10-20')
    solve.load_mesh('real_brain')
    solve.define_field_variable('potential_base', 'voltage')
    solve.define_field_variable('potential_df', 'voltage')

    solve.define_essential_boundary('Base_VCC', 26, 'potential_base', 150.0)
    solve.define_essential_boundary('Base_GND', 16, 'potential_base', -150.0)
    solve.define_essential_boundary('DF_VCC', 22, 'potential_df', 150.0)
    solve.define_essential_boundary('DF_GND', 12, 'potential_df', -150.0)

    solve.solver_setup(600, 1e-10, 1e-8, verbose=True)
    state = solve.run_solver(save_results=True, output_dir='output directory path', output_file_name='model prefix' + model + 'model suffix')
    del state
    solve.clear_all()
    gc.collect()
\end{lstlisting}

\pagebreak
\noindent Snippet on \href{https://gitlab.com/ttis-simulations/ttis-software/-/snippets/2068520}{GitLab}.
\begin{lstlisting}[language=Python,caption={Electrode position sweep study},captionpos=b,label=lst:electrode_position_sweep]
import importlib
import os
import gc
import sys
import yaml

with open(os.path.realpath('settings file path')) as stream:
    settings = yaml.safe_load(stream)

sys.path.append(os.path.realpath(settings['SfePy']['lib_path']))

import FEM.Solver as slv

settings['SfePy']['real_brain']['mesh_file'] = 'model path prefix' + model + 'model suffix'

# Change the combinations as you wish and/or add others
electrodes = [[26, 16, 22, 12], [25, 15, 23, 13]]

for electrode in electrodes:
    solve = slv.Solver(settings, 'SfePy', '10-20')
    solve.load_mesh('real_brain')
    solve.define_field_variable('potential_base', 'voltage')
    solve.define_field_variable('potential_df', 'voltage')

    solve.define_essential_boundary('Base_VCC', electrode[0], 'potential_base', 150.0)
    solve.define_essential_boundary('Base_GND', electrode[1], 'potential_base', -150.0)
    solve.define_essential_boundary('DF_VCC', electrode[2], 'potential_df', 150.0)
    solve.define_essential_boundary('DF_GND', electrode[3], 'potential_df', -150.0)

    solve.solver_setup(600, 1e-10, 1e-8, verbose=True)
    state = solve.run_solver(save_results=True, output_dir='output directory path', output_file_name='fem_model-name_' + "{}-{}-{}-{}".format(*electrode))
    del state
    solve.clear_all()
    gc.collect()
\end{lstlisting}

\noindent Snippet on \href{https://gitlab.com/ttis-simulations/ttis-software/-/snippets/2072723}{GitLab}.
\begin{lstlisting}[language=Python,caption={Region volume calculation},captionpos=b,label=lst:region_volume_calc]
import meshio
import numpy as np

loaded_mesh = meshio.read('path to the solved VTK file')

# https://en.wikipedia.org/wiki/Tetrahedron#Volume
voxel_volume = (1./6)*np.fromiter((np.absolute(np.linalg.det([loaded_mesh.points[cell[0]] - loaded_mesh.points[cell[3]], loaded_mesh.points[cell[1]] - loaded_mesh.points[cell[3]], loaded_mesh.points[cell[2]] - loaded_mesh.points[cell[3]]])) for cell in loaded_mesh.cells[0][1]), dtype=np.float32)

print("****************************")
print("****** Region volumes ******")
print("****************************")
print("Skin: {} cm^3".format(np.round(np.sum(voxel_volume[loaded_mesh.cell_data['mat_id'][0] == 1])/1000, 2)))
print("Skull: {} cm^3".format(np.round(np.sum(voxel_volume[loaded_mesh.cell_data['mat_id'][0] == 2])/1000, 2)))
print("CSF: {} cm^3".format(np.round(np.sum(voxel_volume[loaded_mesh.cell_data['mat_id'][0] == 3])/1000, 2)))
print("Grey Matter: {} cm^3".format(np.round(np.sum(voxel_volume[loaded_mesh.cell_data['mat_id'][0] == 4])/1000, 2)))
print("White Matter: {} cm^3".format(np.round(np.sum(voxel_volume[loaded_mesh.cell_data['mat_id'][0] == 5])/1000, 2)))
print("Cerebellum: {} cm^3".format(np.round(np.sum(voxel_volume[loaded_mesh.cell_data['mat_id'][0] == 6])/1000, 2)))
print("Ventricles: {} cm^3".format(np.round(np.sum(voxel_volume[loaded_mesh.cell_data['mat_id'][0] == 7])/1000, 2)))
\end{lstlisting}


% marking changes

%\newcommand{\mitras}[1]{\textcolor{blue}{#1}}
\newcommand{\mitras}[1]{{#1}}
% for nomencl to produce page links
%\renewcommand*{\pagedeclaration}[1]{\unskip, \hyperpage{#1}}
%\renewcommand{\nomname}{List of Symbols and Functions}

\newcommand{\authcomment}[1]{\textbf{[[#1]]}}
\newcommand{\sayan}[1]{\textbf{[[#1]]}}

%FIXED SETS

%\newcommand{\Time}{{\sf T}}                     %time domain
\newcommand{\nnt}{{\sf T}^{\geq 0}}             %nonnegative time points
\newcommand{\post}{{\sf T}^{>0}}                %positive time points
\newcommand{\Variables}{{\sf V}}                %variables

\newcommand{\num}[1]{\relax\ifmmode \mathbb #1\else $\mathbb #1$\fi}
\newcommand{\nnnum}[1]{\relax\ifmmode
  {\mathbb #1}_{\geq 0} \else ${\mathbb #1}_{\geq 0}$
  \fi}
\newcommand{\npnum}[1]{\relax\ifmmode
  {\mathbb #1}_{\leq 0} \else ${\mathbb #1}_{\leq 0}$
  \fi}
\newcommand{\pnum}[1]{\relax\ifmmode
  {\mathbb #1}_{> 0} \else ${\mathbb #1}_{> 0}$
  \fi}
\newcommand{\nnum}[1]{\relax\ifmmode
  {\mathbb #1}_{< 0} \else ${\mathbb #1}_{< 0}$
  \fi}
\newcommand{\plnum}[1]{\relax\ifmmode
  {\mathbb #1}_{+} \else ${\mathbb #1}_{+}$
  \fi}
\newcommand{\nenum}[1]{\relax\ifmmode
  {\mathbb #1}_{-} \else ${\mathbb #1}_{-}$
  \fi}

\newcommand{\reals}{{\num R}}                    %reals
\newcommand{\nnreals}{{\nnnum R}}                    %nonnegative reals
\newcommand{\realsinfty}{{\num R} \cup \{\infty, -\infty\}}                    %nonnegative reals
\newcommand{\plreals}{{\plnum R}}                    %positive reals
\newcommand{\naturals}{{\num N}}                      %natural numbers
\newcommand{\integers}{{\num Z}}                      %integers
\newcommand{\rationals}{{\num Q}}                      %rationals
\newcommand{\nnrationals}{{\nnnum Q}}                   % nonnegative rationals
\newcommand{\Time}{{\num T}}
\newcommand{\bools}{{\num B}}

% EXECUTIONS TRACES and FRAGS
\newcommand{\extb}[1]{\relax\ifmmode {\sf ExtBeh}_{#1} \else ${\sf ExtBeh}_{#1}$\fi}
\newcommand{\tdists}[1]{\relax\ifmmode {\sf Tdists}_{#1} \else ${\sf Tdists}_{#1}$\fi}

\newcommand{\exec}[1]{\relax\ifmmode {\sf Execs}_{#1} \else ${\sf Exec}_{#1}$\fi}
\newcommand{\execf}[1]{\relax\ifmmode {\sf Execs}^*_{#1} \else ${\sf Exec}^*_{#1}$\fi}
\newcommand{\execi}[1]{\relax\ifmmode {\sf Execs}^\omega_{#1} \else ${\sf Exec}^\omega_{#1}$\fi}

\newcommand{\ctrace}[1]{\relax\ifmmode {\sf Ctraces}_{#1} \else ${\sf Ctraces}_{#1}$\fi}

\newcommand{\trace}[1]{\relax\ifmmode {\sf Traces}_{#1} \else ${\sf Traces}_{#1}$\fi}
\newcommand{\tracef}[1]{\relax\ifmmode {\sf Traces}^*_{#1} \else ${\sf Traces}^*_{#1}$\fi}
\newcommand{\tracei}[1]{\relax\ifmmode {\sf Traces}^\omega_{#1} \else ${\sf Traces}^\omega_{#1}$\fi}

\newcommand{\frag}[1]{\relax\ifmmode {\sf Frags}_{#1} \else ${\sf Frags}_{#1}$\fi}
\newcommand{\fragf}[1]{\relax\ifmmode {\sf Frags}^*_{#1} \else ${\sf Frags}^*_{#1}$\fi}
\newcommand{\fragi}[1]{\relax\ifmmode {\sf Frags}^\omega_{#1} \else ${\sf Frags}^\omega_{#1}$\fi}

\newcommand{\reach}[1]{\relax\ifmmode {\sf Reach}_{#1} \else ${\sf Reach}_{#1}$\fi}

\newcommand{\execs}{{\exec{}}}
\newcommand{\traces}{{\trace{}}}
\newcommand{\fragss}{{\frag{}}}

\newcommand{\fexecs}{{\execf{}}}
\newcommand{\ftraces}{{\tracef{}}}
\newcommand{\ffragss}{{\fragf{}}}

\newcommand{\iexecs}{{\execi{}}}
\newcommand{\itraces}{{\tracei{}}}
\newcommand{\ifragss}{{\fragi{}}}

\newcommand{\fstate}{{\sf fstate}}
\newcommand{\lstate}{{\sf lstate}}
\newcommand{\ltime}{{\sf ltime}}

\newenvironment{noqedproof}{\pf}{}
\newcommand{\pf}{\par\noindent{\bf Proof:}~}

 \newtheorem{theorem}{Theorem}
 \newtheorem{lemma}[theorem]{Lemma}
 \newtheorem{corollary}[theorem]{Corollary}
 \newtheorem{claim}{Claim}
 \newtheorem{assumption}{Assumption}
%\theoremstyle{definition}
 \newtheorem{definition}{Definition}
 \renewcommand{\qed}{\hfill{\rule{2mm}{2mm}}\medskip}
 %\newenvironment{proof}{\pf}{\qed}
 \newtheorem{proposition}[theorem]{Proposition}
 \newtheorem{inv}[theorem]{Invariant}
\newtheorem{remark}[theorem]{Remark}

 \newenvironment{example}
 {\refstepcounter{theorem} \vspace{2ex}\par\noindent
 \textbf{Example}~\textbf{\thetheorem~}}{\qed}

%\theoremstyle{remark}
%\newcounter{example}
% \newtheorem{example}{Example}[chapter]
% {\refstepcounter{theorem} \vspace{2ex}\par\noindent
% \textbf{Example}~\textbf{\theexampletheorem~}}{\qed}

 \def\examplenonum#1#2{
	\vspace{.15in}
	\noindent
	{\bf Example #1}{\em ~(continued)}{\bf .}
	{#2}{\qed}
	}
%
% \renewcommand{\theequation}{\thesection.\arabic{equation}}
% \renewcommand{\thefigure}{\thesection.\arabic{figure}}
% \renewcommand{\thetable}{\thesection.\arabic{table}}
% \newcommand{\Section}[1]{\section{#1}%
%    \setcounter{equation}{0}\setcounter{figure}{0}\setcounter{table}{0}%
%    \setcounter{example}{0}}
%
% \newenvironment{anotation}[1][Nancy]{\begin{quote}\small[[[#1]]]}{\normalsize\end{quote}}
% \newcommand{\ba}{\begin{anotation}}
% \newcommand{\ea}{\end{anotation}}
%
% \newcommand{\bcb}{\chgbarbegin}
% \newcommand{\ecb}{\chgbarend}
% \chgbarwidth 1pt
%
% \newcommand{\dec}{\ensuremath{{:}}}
% \newcommand{\eqdef}{\mathbin{::=}}
% \newcommand{\I}{{\ensuremath{\cap}}}

% OPERATIONS ON SETS, RELATIONS AND FUNCTIONS
\newcommand{\pow}[1]{{\bf P}(#1)} % powerset
\newcommand{\inverse}[1]{#1^{-1}}
\newcommand{\range}[1]{\ms{range{(#1)}}}
\newcommand{\domain}[1]{{\it dom}(#1)}
\newcommand{\type}[1]{\ms{type{(#1)}}}
\newcommand{\dtype}[1]{\ms{dtype{(#1)}}} % dynamic type
\newcommand{\restr}{\mathrel{\lceil}}
\newcommand{\proj}{\matrel{\lceil}}
\newcommand{\restrrange}{\mathrel{\downarrow}}
\newcommand{\point}[1]{\wp(#1)}     		%point trajectory

% HYBRID AUTOMATA
\def\A{{\cal A}} % HA
\def\B{{\cal B}} % HA
\def\C{{\cal C}} % HA
\def\D{{\cal D}} % set of discrete steps
\def\E{{\cal E}} % HA
\def\F{{\cal F}} % HA
\def\G{{\cal G}} % pieces of SHIOA
\def\H{{\cal H}} % HA
\def\I{{\cal I}} % environment sequence
\def\K{{\cal K}} % environment sequence
\def\M{{\cal M}} % Mode switching transitions
\def\O{{\cal O}} % outcome function
\def\P{{\cal P}} % set of modes
\def\Q{{\cal Q}} % set of modes
\def\R{{\cal R}} % relation
\def\S{{\cal S}} % set of trajectories
\def\T{{\cal T}} % set of trajectories
\def\V{{\cal V}} % Lyapunov function
\def\U{{\cal U}} % set of trajectories
\def\X{{\cal X}} % Lyapunov function
\def\Y{{\cal Y}} % set of trajectories

% more special characters

\newcommand{\col}[1]{\relax\ifmmode \mathscr #1\else $\mathscr #1$\fi}

\def\statemodels{\col{S}}

% Names of actions, automata etc
\definecolor{HIOAcolor}{rgb}{0.776,0.22,0.07}
\newcommand{\HIOA}{\textcolor{HIOAcolor}{\tt HIOA\hspace{3pt}}}
\newcommand{\PVS}{\textcolor{HIOAcolor}{\tt PVS\hspace{3pt}}}
\newcommand{\PVSnogap}{\textcolor{HIOAcolor}{\tt PVS\hspace{1pt}}}
\newcommand{\HIOAbiggap}{\textcolor{HIOAcolor}{\tt HIOA\hspace{6pt}}}
\newcommand{\HIOAnogap}{\textcolor{HIOAcolor}{\tt HIOA}}
\newcommand{\anyrelation}{\lessgtr}

% Transformation for ADT
\newcommand{\SC}[2]{\relax\ifmmode {\tt Scount}(#1,#2) \else ${\tt Scount}(#1,#2)$\fi}
\newcommand{\SCM}[2]{\relax\ifmmode {\tt Smin}(#1,#2) \else ${\tt Smin}(#1,#2)$\fi}
\newcommand{\Aut}[1]{\relax\ifmmode {\tt Aut}(#1) \else ${\tt Aut}(#1)$\fi}

\newcommand{\auto}[1]{{\operatorname{\mathsf{#1}}}}
\newcommand{\act}[1]{{\operatorname{\mathsf{#1}}}}
\newcommand{\smodel}[1]{{\operatorname{\mathsf{#1}}}}
\newcommand{\pvstheory}[1]{{\operatorname{\mathit{#1}}}}
%\newcommand{\auto}[1]{\relax\ifmmode \sf #1\else $\sf #1$\fi}
%\newcommand{\act}[1]{\relax\ifmmode \sf #1\else $\sf #1$\fi}

\newcommand{\Automaton}{{\bf automaton}}
\newcommand{\Asserts}{{\bf asserts}}
\newcommand{\Assumes}{{\bf assumes}}
\newcommand{\Backward}{{\bf backward}}
\newcommand{\By}{{\bf by}}
\newcommand{\Case}{{\bf case}}
\newcommand{\Choose}{{\bf  choose}}
\newcommand{\Components}{{\bf components}}
\newcommand{\Const}{{\bf const}}
\newcommand{\Converts}{{\bf converts}}
\newcommand{\Do}{{\bf do}}
\newcommand{\Eff}{{\bf eff}}
\newcommand{\Else}{{\bf else}}
\newcommand{\Elseif}{{\bf elseif}}
\newcommand{\Enumeration}{{\bf enumeration}}
\newcommand{\Ensuring}{{\bf ensuring}}
\newcommand{\Exempting}{{\bf exempting}}
\newcommand{\Fi}{{\bf fi}}
\newcommand{\For}{{\bf for}}
\newcommand{\Forward}{{\bf forward}}
\newcommand{\Freely}{{\bf freely}}
\newcommand{\From}{{\bf from}}
\newcommand{\Generated}{{\bf generated}}
\newcommand{\Local}{{\bf local}}
\newcommand{\Hidden}{{\bf hidden}}
\newcommand{\If}{{\bf if}}
\newcommand{\In}{{\bf in}}
\newcommand{\Implies}{{\bf implies}}
\newcommand{\Includes}{{\bf includes}}
\newcommand{\Introduces}{{\bf introduces}}
\newcommand{\Input}{{\bf input}}
\newcommand{\Kind}{{\bf kind}}
\newcommand{\Initially}{{\bf initially}}
\newcommand{\Internal}{{\bf internal}}
\newcommand{\Invariant}{{\bf invariant}}
\newcommand{\Od}{{\bf od}}
\newcommand{\Of}{{\bf of}}
\newcommand{\Output}{{\bf output}}
\newcommand{\Partitioned}{{\bf partitioned}}
\newcommand{\Pre}{{\bf pre}}
\newcommand{\Signature}{{\bf signature}}
\newcommand{\Simulation}{{\bf simulation}}
\newcommand{\Sort}{{\bf sort}}
\newcommand{\States}{{\bf states}}
\newcommand{\Tasks}{{\bf tasks}}
\newcommand{\Then}{{\bf then}}
\newcommand{\To}{{\bf to}}
\newcommand{\Trait}{{\bf trait}}
\newcommand{\Traits}{{\bf traits}}
\newcommand{\Transitions}{{\bf transitions}}
\newcommand{\Tuple}{{\bf tuple}}
\newcommand{\Type}{{\bf type}}
\newcommand{\Union}{{\bf union}}
\newcommand{\Uses}{{\bf uses}}
\newcommand{\Where}{{\bf where}}
\newcommand{\While}{{\bf while}}
\newcommand{\With}{{\bf with}}

% Spacing
\newcommand{\FFF}{\vspace{0.1in}}
\newcommand{\BBB}{\hspace{-0.1in}}

\newcommand{\deq}{\mathrel{\stackrel{\scriptscriptstyle\Delta}{=}}}

% systematic labels

\newcommand{\seclabel}[1]{\label{sec:#1}}
\newcommand{\secref}[1]{Section~\ref{sec:#1}}
\newcommand{\secreftwo}[2]{Sections~\ref{sec:#1}~and~\ref{sec:#2}}
\newcommand{\figlabel}[1]{\label{fig:#1}}
\newcommand{\figref}[1]{Figure~\ref{fig:#1}}
\newcommand{\figrefs}[2]{Figures~\ref{fig:#1} and~\ref{fig:#2}}
\newcommand{\applabel}[1]{\label{app:#1}}
\newcommand{\appref}[1]{Appendix~\ref{app:#1}}
\newcommand{\lnlabel}[1]{\label{line:#1}}
\newcommand{\lnrngref}[2]{lines~\ref{line:#1}--\ref{line:#2}\xspace}
\newcommand{\lnref}[1]{line~\ref{line:#1}\xspace}
\newcommand{\thmref}[1]{Theorem~\ref{thm:#1}\xspace}

% defs FROM ptioa PAPER

\newcommand{\remove}[1]{}
\newcommand{\salg}[1]{\relax\ifmmode {\mathcal F}_{#1}\else ${\mathcal F}_{#1}$\fi}
\newcommand{\msp}[1]{\relax\ifmmode (#1, \salg{#1}) \else $(#1, \salg{#1})$\fi}
\newcommand{\msprod}[2]{\relax\ifmmode ( #1 \times #2, \salg{#1} \otimes \salg{#2}) \else $(#1 \times #2, \salg{#1} \otimes \salg{#2})$\fi}
\newcommand{\dist}[1]{\relax\ifmmode {\mathcal P}\msp{#1}
  \else ${\mathcal P}\msp{#1}$\fi}
\newcommand{\subdist}[1]{\relax\ifmmode {\mathcal S}{\mathcal P}\msp{#1}
  \else ${\mathcal S}{\mathcal P}\msp{#1}$\fi}
\newcommand{\disc}[1]{\relax\ifmmode {\sf Disc}(#1)
  \else ${\sf Disc}(#1)$\fi}

\newcommand{\Trajeq}{\relax\ifmmode {\mathcal R}_\T \else ${\mathcal R}_\T$\fi}
\newcommand{\Acteq}{\relax\ifmmode {\mathcal R}_A \else ${\mathcal R}_A$\fi}
\newcommand{\noop}{\relax\ifmmode \lambda \else $\lambda$\fi}
\newcommand{\close}[1]{\relax\ifmmode \overline{#1} \else $\overline{#1}$\fi}

\newcommand{\corrtasks}{\mathop{\mathsf {c}}}
\newcommand{\full}{\mathop{\mathsf {full}}}
%\newcommand{\fstate}{\mathop{\mathsf {fstate}}}
%\newcommand{\lstate}{\mathop{\mathsf {lstate}}}
\newcommand{\tdist}{\mathop{\mathsf {tdist}}}
\newcommand{\extbeh}{\mathop{\mathsf {extbeh}}}
%\newcommand{\apply}{\mathop{\mathsf {apply}}}
\newcommand{\apply}[2]{\mathop{\mathsf {apply}({#1},{#2})}}
%\newcommand{\applytwo}{\mathop{\mathsf {apply2}}}
\newcommand{\support}{\mathop{\mathsf {supp}}}
\newcommand{\relation}{\mathrel{R}}
\newcommand{\cone}{C}
%\newcommand{\tracef}{\mathord{\mathsf {trace}}}
\newcommand{\flatten}{\mathord{\mathsf {flatten}}}
\newcommand{\discrete}{\mathord{\mathsf {Disc}}}
\newcommand{\lift}[1]{\mathrel{{\mathcal L}(#1)}}
\newcommand{\expansion}[1]{\mathrel{{\mathcal E}(#1)}}
%End of commands added by Roberto on May, 12

%>> Ling, December 2005

\newcommand{\subdisc}{\operatorname{\mathsf {SubDisc}}}
\newcommand{\tran}{\operatorname{\mathsf {tran}}}
%\newcommand{\act}{\operatorname{\mathsf {act}}}

\renewcommand{\execs}{{\operatorname{\mathsf {Execs}}}}
\newcommand{\frags}{{\operatorname{\mathsf {Frags}}}}

\newcommand{\tracefnc}{{\operatorname{\mathsf {trace}}}}

\newcommand{\finite}{{\operatorname{\mathsf {finite}}}}
\newcommand{\hide}{{\operatorname{\mathsf {hide}}}}

\newcommand{\early}{{\operatorname{\mathsf {Early}}}}
\newcommand{\late}{{\operatorname{\mathsf {Late}}}}
\newcommand{\toss}{{\operatorname{\mathsf {Toss}}}}

\newcommand{\define}{:=}

\newcommand{\pc}{{\operatorname{\mathsf {counter}}}}
\newcommand{\chosen}{{\operatorname{\mathsf {chosen}}}}

\newcommand{\rand}{{\operatorname{\mathsf {random}}}}
\newcommand{\unif}{{\operatorname{\mathsf {unif}}}}

\newcommand{\ie}{i.e.,\xspace}
\newcommand{\Ie}{I.e.,\xspace}

\newcommand{\eg}{e.g.,\xspace}
\newcommand{\Eg}{E.g.,\xspace}

% IOA related stuff

\newcommand{\mybox}[3]{
  \framebox[#1][l]
  {
    \parbox{#2}
    {
      #3
    }
  }
}

\newcommand{\two}[4]{
  \parbox{.98\columnwidth}{\vspace{1pt} \vfill
    \parbox[t]{#1\columnwidth}{#3}%
    \hspace{.5cm}
    \parbox[t]{#2\columnwidth}{#4}%
  }}

\newcommand{\twosep}[4]{
  \parbox{\columnwidth}{\vspace{1pt} \vfill
    \parbox[t]{#1\columnwidth}{#3}%
   	\vrule width 0.2pt
    \parbox[t]{#2\columnwidth}{#4}%
  }}

\newcommand{\eqntwo}[4]{
  \parbox{\columnwidth}{\vspace{1pt} \vfill
    \parbox[t]{#1\columnwidth}{$ #3 $}
    \parbox[t]{#2\columnwidth}{$ #4 $}
  }}

\newcommand{\three}[6]{\vspace{1pt} \vfill
        \parbox{\columnwidth}{%
        \parbox[t]{#1\columnwidth}{#4}%
        \parbox[t]{#2\columnwidth}{#5}%
        \parbox[t]{#3\columnwidth}{#6}%
      }}

\newcommand{\tup}[1]
           {
             \relax\ifmmode
             \langle #1 \rangle
             \else $\langle$ #1 $\rangle$ \fi
           }

\newcommand{\lit}[1]{ \relax\ifmmode
                \mathord{\mathcode`\-="702D\sf #1\mathcode`\-="2200}
                \else {\it #1} \fi }

\newcommand{\figuresize}{\scriptsize}

\newcommand{\equationsize}{\footnotesize}

%\newcommand{\act}[1]{%
%  \relax\ifmmode
%                \mathord{\mathcode`\-="702D\sf #1\mathcode`\-="2200}%
%                \else
%                {\sf #1\/}%
%                \fi }

\lstdefinelanguage{ioa}{
  basicstyle=\figuresize,
  keywordstyle=\bf \figuresize,
  identifierstyle=\it \figuresize,
  emphstyle=\tt \figuresize,
  mathescape=true,
  tabsize=20,
%  tabsize=4,
  sensitive=false,
  columns=fullflexible,
  keepspaces=false,
  flexiblecolumns=true,
%  basewidth=0.5em,
  basewidth=0.05em,
  moredelim=[il][\rm]{//},
  moredelim=[is][\sf \figuresize]{!}{!},
  moredelim=[is][\bf \figuresize]{*}{*},
  keywords={automaton,and,
  	 choose,const,continue, components,
  	 discrete, do,
  	 eff, external,else, elseif, evolve, end,
  	 fi,for, forward, from,
  	 hidden,
  	 in,input,internal,if,invariant, initially, imports,
     let,
     or, output, operators, od, of,
     pre,
     return,
     such,satisfies, stop, signature, simulation,
     trajectories,trajdef, transitions, that,then, type, types, to, tasks,
     variables, vocabulary,
     when,where, with,while},
  emph={set, seq, tuple, map, array, enumeration},
   literate=
        {(}{{$($}}1
        {)}{{$)$}}1
        % LaTeX math symbols
        {\\in}{{$\in\ $}}1
        {\\preceq}{{$\preceq\ $}}1
        {\\subset}{{$\subset\ $}}1
        {\\subseteq}{{$\subseteq\ $}}1
        {\\supset}{{$\supset\ $}}1
        {\\supseteq}{{$\supseteq\ $}}1
        {\\forall}{{$\forall$}}1
        {\\le}{{$\le\ $}}1
        {\\ge}{{$\ge\ $}}1
        {\\gets}{{$\gets\ $}}1
        {\\cup}{{$\cup\ $}}1
        {\\cap}{{$\cap\ $}}1
        {\\langle}{{$\langle$}}1
        {\\rangle}{{$\rangle$}}1
        {\\exists}{{$\exists\ $}}1
        {\\bot}{{$\bot$}}1
        {\\rip}{{$\rip$}}1
        {\\emptyset}{{$\emptyset$}}1
        {\\notin}{{$\notin\ $}}1
        {\\not\\exists}{{$\not\exists\ $}}1
        {\\ne}{{$\ne\ $}}1
        {\\to}{{$\to\ $}}1
        {\\implies}{{$\implies\ $}}1
        % LSL symbols (one-character)
        {<}{{$<\ $}}1
        {>}{{$>\ $}}1
        {=}{{$=\ $}}1
        {~}{{$\neg\ $}}1
        {|}{{$\mid$}}1
        {'}{{$^\prime$}}1
        % LSL symbols (two characters)
        {\\A}{{$\forall\ $}}1
        {\\E}{{$\exists\ $}}1
        {\\/}{{$\vee\,$}}1
        {\\vee}{{$\vee\,$}}1
        {/\\}{{$\wedge\,$}}1
        {\\wedge}{{$\wedge\,$}}1
        {=>}{{$\Rightarrow\ $}}1
        {->}{{$\rightarrow\ $}}1
        {<=}{{$\Leftarrow\ $}}1
        {<-}{{$\leftarrow\ $}}1
%        {<=}{{$\leq$}}1
%        {>=}{{$\geq$}}1
        {~=}{{$\neq\ $}}1
        {\\U}{{$\cup\ $}}1
        {\\I}{{$\cap\ $}}1
        {|-}{{$\vdash\ $}}1
        {-|}{{$\dashv\ $}}1
        {<<}{{$\ll\ $}}2
        {>>}{{$\gg\ $}}2
        {||}{{$\|$}}1
%%       {\[\]}{{\[\,\]}}2 {\{\}}{{\{\,\}}}2
%%        {[}{{$\langle$}}1
%%        {]}{{$\rangle$}}1
        {[}{{$[$}}1
        {]}{{$\,]$}}1
        {[[}{{$\langle$}}1
        {]]]}{{$]\rangle$}}1
        {]]}{{$\rangle$}}1
        {<=>}{{$\Leftrightarrow\ $}}2
        {<->}{{$\leftrightarrow\ $}}2
        {(+)}{{$\oplus\ $}}1
        {(-)}{{$\ominus\ $}}1
        {_i}{{$_{i}$}}1
        {_j}{{$_{j}$}}1
        {_{i,j}}{{$_{i,j}$}}3
        {_{j,i}}{{$_{j,i}$}}3
        {_0}{{$_0$}}1
        {_1}{{$_1$}}1
        {_2}{{$_2$}}1
        {_n}{{$_n$}}1
        {_p}{{$_p$}}1
        {_k}{{$_n$}}1
        {-}{{$\ms{-}$}}1
        {@}{{}}0
        {\\delta}{{$\delta$}}1
        {\\R}{{$\R$}}1
        {\\Rplus}{{$\Rplus$}}1
        {\\N}{{$\N$}}1
        {\\times}{{$\times\ $}}1
        {\\tau}{{$\tau$}}1
        {\\alpha}{{$\alpha$}}1
        {\\beta}{{$\beta$}}1
        {\\gamma}{{$\gamma$}}1
        {\\ell}{{$\ell\ $}}1
        {--}{{$-\ $}}1
        {\\TT}{{\hspace{1.5em}}}3
      }

\lstdefinelanguage{ioaNums}[]{ioa}
{
  numbers=left,
  numberstyle=\tiny,
  stepnumber=2,
  numbersep=4pt
%  firstnumber=1
}

\lstdefinelanguage{ioaNumsRight}[]{ioa}
{
  numbers=right,
  numberstyle=\tiny,
  stepnumber=2,
  numbersep=4pt
%  firstnumber=1
}

\newcommand{\ioa}{\lstinline[language=IOA]}

\lstnewenvironment{IOA}%
  {\lstset{language=IOA}}
  {}

\lstnewenvironment{IOANums}%
  {
  \if@firstcolumn
    \lstset{language=IOA, numbers=left, firstnumber=auto}
  \else
    \lstset{language=IOA, numbers=right, firstnumber=auto}
  \fi
  }
  {}

\lstnewenvironment{IOANumsRight}%
  {
    \lstset{language=IOA, numbers=right, firstnumber=auto}
  }
  {}

%\lstnewenvironment{IOA}%
%  {\lstset{language=ioaLang}
%   \csname lst@SetFirstLabel\endcsname}
%  {\csname lst@SaveFirstLabel\endcsname\vspace{-4pt}\noindent}

\newcommand{\figioa}[5]{
  \begin{figure}[#1]
      \hrule \F
      {\figuresize \bf #2}
      \lstinputlisting[language=ioaLang]{#5}
      \F \hrule \F
      \caption{#3}
      \label{fig: #4}
  \end{figure}
}

\newcommand{\linefigioa}[9]{

}

\newcommand{\twofigioa}[8]{
  \begin{figure}[#1]
    \hrule \F
    {\figuresize \bf #2} \\
    \two{#5}{#6}
    {
      \lstinputlisting[language=ioaLang]{#7}
    }
    {
      \lstinputlisting[language=ioaLang]{#8}
    }
    \F \hrule \F
    \caption{#3}
    \label{fig: #4}
  \end{figure}
}

\lstdefinelanguage{ioaLang}{%
  basicstyle=\ttfamily\small,
  keywordstyle=\rmfamily\bfseries\small,
  identifierstyle=\small,
%  commentline=\%,
  keywords={assumes,automaton,axioms,backward,bounds,by,case,choose,components,const,d,det,discrete,do,eff,else,elseif,ensuring,enumeration,evolve,fi,fire,follow,for,forward,from,hidden,if,in,%
    input,initially,internal,invariant,let, local,od,of,output,pre,schedule,signature,so,%
    simulation,states,variables, tasks, stop,tasks,that,then,to,trajdef,trajectory,trajectories,transitions,tuple,type,union,urgent,uses,when,where,while,yield},
  literate=
        % LaTeX math symbols
        {\\in}{{$\in$}}1
        {\\preceq}{{$\preceq$}}1
        {\\subset}{{$\subset$}}1
        {\\subseteq}{{$\subseteq$}}1
        {\\supset}{{$\supset$}}1
        {\\supseteq}{{$\supseteq$}}1
        {\\rho}{{$\rho$}}1
        {\\infty}{{$\infty$}}1
        % LSL symbols (one-character)
        {<}{{$<$}}1
        {>}{{$>$}}1
        {=}{{$=$}}1
        {~}{{$\neg$}}1
        {|}{{$\mid$}}1
        {'}{{$^\prime$}}1
        % LSL symbols (two characters)
        {\\A}{{$\forall$}}1 {\\E}{{$\exists$}}1
        {\\/}{{$\vee$}}1 {/\\}{{$\wedge$}}1
        {=>}{{$\Rightarrow$}}1
        {->}{{$\rightarrow$}}1
        {<=}{{$\leq$}}1 {>=}{{$\geq$}}1 {~=}{{$\neq$}}1
        {\\U}{{$\cup$}}1 {\\I}{{$\cap$}}1
        {|-}{{$\vdash$}}1 {-|}{{$\dashv$}}1
        {<<}{{$\ll$}}2 {>>}{{$\gg$}}2
        {||}{{$\|$}}1
%       {\[\]}{{\[\,\]}}2 {\{\}}{{\{\,\}}}2
        % LSL symbols (three or more characters)
        {<=>}{{$\Leftrightarrow$}}2
        {<->}{{$\leftrightarrow$}}2
        {(+)}{{$\oplus$}}1
        {(-)}{{$\ominus$}}1
}

\lstdefinelanguage{bigIOALang}{%
  basicstyle=\ttfamily,
  keywordstyle=\rmfamily\bfseries,
  identifierstyle=,
%  commentline=\%,
  keywords={assumes,automaton,axioms,backward,by,case,choose,components,const,%
    d,det,discrete,do,eff,else,elseif,ensuring,enumeration,evolve,fi,for,forward,from,hidden,if,in%
    input,initially,internal,invariant,local,od,of,output,pre,schedule,signature,so,%
    tasks, simulation,states,stop,tasks,that,then,to,trajdef,trajectories,transitions,tuple,type,union,urgent,uses,when,where,yield},
  literate=
        % LaTeX math symbols
        {\\in}{{$\in$}}1
        {\\preceq}{{$\preceq$}}1
        {\\subset}{{$\subset$}}1
        {\\subseteq}{{$\subseteq$}}1
        {\\supset}{{$\supset$}}1
        {\\supseteq}{{$\supseteq$}}1
        % LSL symbols (one-character)
        {<}{{$<$}}1
        {>}{{$>$}}1
        {=}{{$=$}}1
        {~}{{$\neg$}}1
        {|}{{$\mid$}}1
        {'}{{$^\prime$}}1
        % LSL symbols (two characters)
        {\\A}{{$\forall$}}1 {\\E}{{$\exists$}}1
        {\\/}{{$\vee$}}1 {/\\}{{$\wedge$}}1
        {=>}{{$\Rightarrow$}}1
        {->}{{$\rightarrow$}}1
        {<=}{{$\leq$}}1 {>=}{{$\geq$}}1 {~=}{{$\neq$}}1
        {\\U}{{$\cup$}}1 {\\I}{{$\cap$}}1
        {|-}{{$\vdash$}}1 {-|}{{$\dashv$}}1
        {<<}{{$\ll$}}2 {>>}{{$\gg$}}2
        {||}{{$\|$}}1
%       {\[\]}{{\[\,\]}}2 {\{\}}{{\{\,\}}}2
        % LSL symbols (three or more characters)
        {<=>}{{$\Leftrightarrow$}}2
        {<->}{{$\leftrightarrow$}}2
        {(+)}{{$\oplus$}}1
        {(-)}{{$\ominus$}}1
}

\lstnewenvironment{BigIOA}%
  {\lstset{language=bigIOALang,basicstyle=\ttfamily}
   \csname lst@SetFirstLabel\endcsname}
  {\csname lst@SaveFirstLabel\endcsname\vspace{-4pt}\noindent}

\lstnewenvironment{SmallIOA}%
  {\lstset{language=ioaLang,basicstyle=\ttfamily\scriptsize}
   \csname lst@SetFirstLabel\endcsname}
  %{\csname lst@SaveFirstLabel\endcsname\vspace{-4pt}\noindent}
  {\csname lst@SaveFirstLabel\endcsname\noindent}

\newcommand{\true}{\relax\ifmmode \mathit true \else \em true \/\fi}
\newcommand{\false}{\relax\ifmmode \mathit false \else \em false \/\fi}

\newcommand{\Real}{{\operatorname{\texttt{Real}}}}
\newcommand{\Bool}{{\operatorname{\texttt{Bool}}}}
\newcommand{\Char}{{\operatorname{\texttt{Char}}}}
\newcommand{\ioaInt}{{\operatorname{\texttt{Int}}}}
\newcommand{\ioaNat}{{\operatorname{\texttt{Nat}}}}
\newcommand{\ioaAugR}{{\operatorname{\texttt{AugmentedReal}}}}
\newcommand{\ioaString}{{\operatorname{\texttt{String}}}}
\newcommand{\Discrete}{{\operatorname{\texttt{Discrete}}}}
%\newcommand{\Bool}{{\operatorname{\texttt{Bool}}}}

%\newcommand{\lnot}{\neg}
%\newcommand{\land}{\wedge}
%\newcommand{\lor}{\vee}
\newcommand{\limplies}{\Rightarrow}
\newcommand{\liff}{\Leftrightarrow}

\newlength{\bracklen}
\newcommand{\sem}[1]{\settowidth{\bracklen}{[}
     [\hspace{-0.5\bracklen}[#1]\hspace{-0.5\bracklen}]}

\newcommand{\defaultArraystretch}{1.4}
\renewcommand{\arraystretch}{\defaultArraystretch}

\newcommand{\gS}{\mathcal{S}}
\newcommand{\gV}{\mathcal{V}}
\newcommand{\freevars}{\mathcal{FV}}

\newcommand{\gVspec}{\mathcal{V}_\mathit{spec}}
\newcommand{\gVa}{\mathcal{V}_\mathit{A}}
\newcommand{\gVsig}{\mathcal{V}_\mathit{sigs}}
\newcommand{\gVso}{\mathcal{V}_\mathit{sorts}}
\newcommand{\gVop}{\mathcal{V}_\mathit{ops}}
\newcommand{\sort}{\mathit{sort}}
\newcommand{\sig}{\mathit{sig}}
\newcommand{\id}{\mathit{id}}
\newcommand{\sigsep}{\lsl`->`}

%\newcommand{\T}{\mathit{true}}
%\newcommand{\F}{\mathit{false}}

\newcommand{\super}[2]{\ensuremath{\mathit{#1}^\mathit{#2}}}
\newcommand{\tri}[3]{\ensuremath{\mathit{#1}^\mathit{#2}_\mathit{#3}}}
\newcommand{\Assumptions}{\ensuremath{\mathit{Assumptions}}}
\newcommand{\actPred}[3][\pi]{\tri{P}{#2,#1}{#3}}
\newcommand{\actualTypes}[1]{\super{actualTypes}{#1}}
\newcommand{\actuals}[1]{\super{actuals}{#1}}
\newcommand{\autActVars}[2][\pi]{\vars{#2}{},\vars{#2,#1}{}}
\newcommand{\bracket}[2]{\mathit{#1}[\mathit{#2}]}
\newcommand{\compVars}[1]{\super{compVars}{#1}}
\newcommand{\context}{\mathit{context}}
\newcommand{\ensuring}[2]{\tri{ensuring}{#1}{#2}}
\newcommand{\initPred}[1]{\tri{P}{#1}{init}}
\newcommand{\initVals}[1]{\super{initVals}{#1}}
\newcommand{\initially}[2]{\tri{initially}{#1}{#2}}
\newcommand{\invPred}[2]{\tri{Inv}{#1}{#2}}
\newcommand{\knownVars}[1]{\super{knownVars}{#1}}
\newcommand{\localPostVars}[2]{\tri{localPostVars}{#1}{#2}}
\newcommand{\localVars}[2]{\tri{localVars}{#1}{#2}}
\newcommand{\locals}[1]{\bracket{Locals}{#1}}
\newcommand{\nam}[1]{\rho^{\mathit{#1}}}
\newcommand{\otherActPred}[3][\pi]{\otherTri{P}{#2,#1}{#3}}
\newcommand{\otherParams}[2]{\otherTri{params}{#1}{#2}}
\newcommand{\otherSub}[2]{\otherTri{\sigma}{#1}{#2}}
\newcommand{\otherTri}[3]{\tri{\smash{#1'}}{#2}{#3}}
\newcommand{\otherVars}[2]{\otherTri{vars}{#1}{#2}}
\newcommand{\params}[2]{\tri{params}{#1}{#2}}
\newcommand{\postVars}[1]{\super{postVars}{#1}}
\newcommand{\pre}[2]{\tri{Pre}{#1}{#2}}
\newcommand{\prog}[2]{\tri{Prog}{#1}{#2}}
\newcommand{\prov}[2]{\tri{Prov}{#1}{#2}}
\newcommand{\stateSorts}[1]{\super{stateSorts}{#1}}
\newcommand{\stateVars}[1]{\super{stateVars}{#1}}
\newcommand{\states}[1]{\bracket{States}{#1}}
\newcommand{\sub}[2]{\tri{\sigma}{#1}{#2}}
\newcommand{\sugActPred}[3][\pi]{\tri{P}{#2,#1}{#3,desug}}
\newcommand{\sugLocalVars}[2]{\ifthenelse{\equal{}{#2}}%
                             {\tri{localVars}{#1}{desug}}%
                             {\tri{localVars}{#1}{#2,desug}}}
\newcommand{\sugVars}[2]{\ifthenelse{\equal{}{#2}}%
                        {\tri{vars}{#1}{desug}}%
                        {\tri{vars}{#1}{#2,desug}}}
\newcommand{\cVars}[1]{\super{cVars}{#1}}

\newcommand{\vmap}{\dot{\varrho}}
\newcommand{\map}[2]{\tri{\vmap}{#1}{#2}}

\newcommand{\types}[1]{\super{types}{#1}}
\newcommand{\vars}[2]{\tri{vars}{#1}{#2}}

\newcommand{\subActPred}[3][\pi]{\sub{#2,#1}{#3}(\tri{P}{#2,#1}{#3,desug})}
\newcommand{\subLocalVars}[2]{\sub{#1}{#2}(\tri{localVars}{#1}{#2,desug})}

\newcommand{\dA}{\hat{A}}
\newcommand{\renameAction}[1]{\ensuremath{\rho_{#1}(\vars{\dA{#1},\pi}{})}}
\newcommand{\renameComponent}[1]{\ensuremath{\rho_{#1}\dA_{#1}}}

\newenvironment{Syntax}{\[\begin{subSyntax}}{\end{subSyntax}\]\vspace{-.3in}}
\newenvironment{subSyntax}{\begin{array}{l}}{\end{array}}
\newcommand{\w}[1]{\mbox{\hspace*{#1em}}}

%TIOA macros
% Script math symbol name
\newcommand{\ms}[1]{\ifmmode%
\mathord{\mathcode`-="702D\it #1\mathcode`\-="2200}\else%
$\mathord{\mathcode`-="702D\it #1\mathcode`\-="2200}$\fi}

%\newcommand{\domain}[1]{{\it dom}(#1)} % domain

% KEYWORDS
\newcommand{\kw}[1]{{\bf #1}}
\newcommand{\tcon}[1]{{\tt #1}}
\newcommand{\syn}[1]{{\tt #1}}
\newcommand{\pvskw}[1]{{\sc #1}}
\newcommand{\pvsid}[1]{{\operatorname{\mathit{#1}}}}

% TIMED AUTOMATA
\def\A{{\cal A}} % TA
\def\B{{\cal B}} % TA
\def\D{{\cal D}} % set of discrete steps
\def\T{{\cal T}} % set of trajectories

% VALUATIONS
\newcommand{\vu}{{\bf u}}
\newcommand{\vv}{{\bf v}}
\newcommand{\vw}{{\bf w}}
\newcommand{\vx}{{\bf x}}
\newcommand{\vy}{{\bf y}}
\newcommand{\vz}{{\bf z}}
\newcommand{\va}{{\bf a}}
\newcommand{\vb}{{\bf b}}
\newcommand{\vq}{{\bf q}}
\newcommand{\vs}{{\bf s}}
\newcommand{\vm}{{\bf m}}

% Transitions and trajectory operations
\newcommand{\arrow}[1]{\mathrel{\stackrel{#1}{\rightarrow}}}
\newcommand{\sarrow}[2]{\mathrel{\stackrel{#1}{\rightarrow_{#2}}}}
\newcommand{\concat}{\mathbin{^{\frown}}} % concatenation
\newcommand{\paste}{\mathrel{\diamond}}
\newcommand{\Arrow}[1]{\mathrel{\stackrel{#1}{\Longrightarrow}}}

% AUTOMATA
%\def\A{{\cal A}}
%\def\B{{\cal B}}
%\def\T{{\cal T}} % set of trajectories

\def\CC{{\mathscr C}} % concatenation closure

% PVS STUFF

\lstdefinelanguage{pvs}{
  basicstyle=\tt \figuresize,
  keywordstyle=\sc \figuresize,
  identifierstyle=\it \figuresize,
  emphstyle=\tt \figuresize,
  mathescape=true,
  tabsize=20,
%  tabsize=4,
  sensitive=false,
  columns=fullflexible,
  keepspaces=false,
  flexiblecolumns=true,
%  basewidth=0.5em,
  basewidth=0.05em,
  moredelim=[il][\rm]{//},
  moredelim=[is][\sf \figuresize]{!}{!},
  moredelim=[is][\bf \figuresize]{*}{*},
  keywords={and,
  	 begin,
  	 cases, const,
  	 do,
  	 external, else, exists, end, endcases, endif,
  	 fi,for, forall, from,
  	 hidden,
  	 in, if, importing,
     let, lambda, lemma,
     measure,
     not,
     or, of,
     return, recursive,
     stop,
     theory, that,then, type, types, type+, to, theorem,
     var,
     with,while},
  emph={nat, setof, sequence, eq, tuple, map, array, enumeration, bool, real, exp, nnreal, posreal},
   literate=
        {(}{{$($}}1
        {)}{{$)$}}1
        % LaTeX math symbols
        {\\in}{{$\in\ $}}1
        {\\mapsto}{{$\rightarrow\ $}}1
        {\\preceq}{{$\preceq\ $}}1
        {\\subset}{{$\subset\ $}}1
        {\\subseteq}{{$\subseteq\ $}}1
        {\\supset}{{$\supset\ $}}1
        {\\supseteq}{{$\supseteq\ $}}1
        {\\forall}{{$\forall$}}1
        {\\le}{{$\le\ $}}1
        {\\ge}{{$\ge\ $}}1
        {\\gets}{{$\gets\ $}}1
        {\\cup}{{$\cup\ $}}1
        {\\cap}{{$\cap\ $}}1
        {\\langle}{{$\langle$}}1
        {\\rangle}{{$\rangle$}}1
        {\\exists}{{$\exists\ $}}1
        {\\bot}{{$\bot$}}1
        {\\rip}{{$\rip$}}1
        {\\emptyset}{{$\emptyset$}}1
        {\\notin}{{$\notin\ $}}1
        {\\not\\exists}{{$\not\exists\ $}}1
        {\\ne}{{$\ne\ $}}1
        {\\to}{{$\to\ $}}1
        {\\implies}{{$\implies\ $}}1
        % LSL symbols (one-character)
        {<}{{$<\ $}}1
        {>}{{$>\ $}}1
        {=}{{$=\ $}}1
        {~}{{$\neg\ $}}1
        {|}{{$\mid$}}1
        {'}{{$^\prime$}}1
        % LSL symbols (two characters)
        {\\A}{{$\forall\ $}}1
        {\\E}{{$\exists\ $}}1
        {\\/}{{$\vee\,$}}1
        {\\vee}{{$\vee\,$}}1
        {/\\}{{$\wedge\,$}}1
        {\\wedge}{{$\wedge\,$}}1
        {->}{{$\rightarrow\ $}}1
        {=>}{{$\Rightarrow\ $}}1
        {->}{{$\rightarrow\ $}}1
        {<=}{{$\Leftarrow\ $}}1
        {<-}{{$\leftarrow\ $}}1
%        {<=}{{$\leq$}}1
%        {>=}{{$\geq$}}1
        {~=}{{$\neq\ $}}1
        {\\U}{{$\cup\ $}}1
        {\\I}{{$\cap\ $}}1
        {|-}{{$\vdash\ $}}1
        {-|}{{$\dashv\ $}}1
        {<<}{{$\ll\ $}}2
        {>>}{{$\gg\ $}}2
        {||}{{$\|$}}1
%%       {\[\]}{{\[\,\]}}2 {\{\}}{{\{\,\}}}2
%%        {[}{{$\langle$}}1
%%        {]}{{$\rangle$}}1
        {[}{{$[$}}1
        {]}{{$\,]$}}1
        {[[}{{$\langle$}}1
        {]]]}{{$]\rangle$}}1
        {]]}{{$\rangle$}}1
        {<=>}{{$\Leftrightarrow\ $}}2
        {<->}{{$\leftrightarrow\ $}}2
        {(+)}{{$\oplus\ $}}1
        {(-)}{{$\ominus\ $}}1
        {_i}{{$_{i}$}}1
        {_j}{{$_{j}$}}1
        {_{i,j}}{{$_{i,j}$}}3
        {_{j,i}}{{$_{j,i}$}}3
        {_0}{{$_0$}}1
        {_1}{{$_1$}}1
        {_2}{{$_2$}}1
        {_n}{{$_n$}}1
        {_p}{{$_p$}}1
        {_k}{{$_n$}}1
        {-}{{$\ms{-}$}}1
        {@}{{}}0
        {\\delta}{{$\delta$}}1
        {\\R}{{$\R$}}1
        {\\Rplus}{{$\Rplus$}}1
        {\\N}{{$\N$}}1
        {\\times}{{$\times\ $}}1
        {\\tau}{{$\tau$}}1
        {\\alpha}{{$\alpha$}}1
        {\\beta}{{$\beta$}}1
        {\\gamma}{{$\gamma$}}1
        {\\ell}{{$\ell\ $}}1
        {--}{{$-\ $}}1
        {\\TT}{{\hspace{1.5em}}}3
      }

\lstdefinelanguage{BigPVS}{
  basicstyle=\tt,
  keywordstyle=\sc,
  identifierstyle=\it,
  emphstyle=\tt ,
  mathescape=true,
  tabsize=20,
%  tabsize=4,
  sensitive=false,
  columns=fullflexible,
  keepspaces=false,
  flexiblecolumns=true,
%  basewidth=0.5em,
  basewidth=0.05em,
  moredelim=[il][\rm]{//},
  moredelim=[is][\sf \figuresize]{!}{!},
  moredelim=[is][\bf \figuresize]{*}{*},
  keywords={and,
  	 begin,
  	 cases, const,
  	 do, datatype,
  	 external, else, exists, end, endif, endcases,
  	 fi,for, forall, from,
  	 hidden,
  	 in, if, importing,
     let, lambda, lemma,
     measure,
     not,
     or, of,
     return, recursive,
     stop,
     theory, that,then, type, types, type+, to, theorem,
     var,
     with,while},
  emph={nat, setof, sequence, eq, tuple, map, array, first, rest, add, enumeration, bool, real, posreal, nnreal},
   literate=
        {(}{{$($}}1
        {)}{{$)$}}1
        % LaTeX math symbols
        {\\in}{{$\in\ $}}1
        {\\mapsto}{{$\rightarrow\ $}}1
        {\\preceq}{{$\preceq\ $}}1
        {\\subset}{{$\subset\ $}}1
        {\\subseteq}{{$\subseteq\ $}}1
        {\\supset}{{$\supset\ $}}1
        {\\supseteq}{{$\supseteq\ $}}1
        {\\forall}{{$\forall$}}1
        {\\le}{{$\le\ $}}1
        {\\ge}{{$\ge\ $}}1
        {\\gets}{{$\gets\ $}}1
        {\\cup}{{$\cup\ $}}1
        {\\cap}{{$\cap\ $}}1
        {\\langle}{{$\langle$}}1
        {\\rangle}{{$\rangle$}}1
        {\\exists}{{$\exists\ $}}1
        {\\bot}{{$\bot$}}1
        {\\rip}{{$\rip$}}1
        {\\emptyset}{{$\emptyset$}}1
        {\\notin}{{$\notin\ $}}1
        {\\not\\exists}{{$\not\exists\ $}}1
        {\\ne}{{$\ne\ $}}1
        {\\to}{{$\to\ $}}1
        {\\implies}{{$\implies\ $}}1
        % LSL symbols (one-character)
        {<}{{$<\ $}}1
        {>}{{$>\ $}}1
        {=}{{$=\ $}}1
        {~}{{$\neg\ $}}1
        {|}{{$\mid$}}1
        {'}{{$^\prime$}}1
        % LSL symbols (two characters)
        {\\A}{{$\forall\ $}}1
        {\\E}{{$\exists\ $}}1
        {\\/}{{$\vee\,$}}1
        {\\vee}{{$\vee\,$}}1
        {/\\}{{$\wedge\,$}}1
        {\\wedge}{{$\wedge\,$}}1
        {->}{{$\rightarrow\ $}}1
        {=>}{{$\Rightarrow\ $}}1
        {->}{{$\rightarrow\ $}}1
        {<=}{{$\Leftarrow\ $}}1
        {<-}{{$\leftarrow\ $}}1
%        {<=}{{$\leq$}}1
%        {>=}{{$\geq$}}1
        {~=}{{$\neq\ $}}1
        {\\U}{{$\cup\ $}}1
        {\\I}{{$\cap\ $}}1
        {|-}{{$\vdash\ $}}1
        {-|}{{$\dashv\ $}}1
        {<<}{{$\ll\ $}}2
        {>>}{{$\gg\ $}}2
        {||}{{$\|$}}1
%%       {\[\]}{{\[\,\]}}2 {\{\}}{{\{\,\}}}2
%%        {[}{{$\langle$}}1
%%        {]}{{$\rangle$}}1
        {[}{{$[$}}1
        {]}{{$\,]$}}1
        {[[}{{$\langle$}}1
        {]]]}{{$]\rangle$}}1
        {]]}{{$\rangle$}}1
        {<=>}{{$\Leftrightarrow\ $}}2
        {<->}{{$\leftrightarrow\ $}}2
        {(+)}{{$\oplus\ $}}1
        {(-)}{{$\ominus\ $}}1
        {_i}{{$_{i}$}}1
        {_j}{{$_{j}$}}1
        {_{i,j}}{{$_{i,j}$}}3
        {_{j,i}}{{$_{j,i}$}}3
        {_0}{{$_0$}}1
        {_1}{{$_1$}}1
        {_2}{{$_2$}}1
        {_n}{{$_n$}}1
        {_p}{{$_p$}}1
        {_k}{{$_n$}}1
        {-}{{$\ms{-}$}}1
        {@}{{}}0
        {\\delta}{{$\delta$}}1
        {\\R}{{$\R$}}1
        {\\Rplus}{{$\Rplus$}}1
        {\\N}{{$\N$}}1
        {\\times}{{$\times\ $}}1
        {\\tau}{{$\tau$}}1
        {\\alpha}{{$\alpha$}}1
        {\\beta}{{$\beta$}}1
        {\\gamma}{{$\gamma$}}1
        {\\ell}{{$\ell\ $}}1
        {--}{{$-\ $}}1
        {\\TT}{{\hspace{1.5em}}}3
      }

\lstdefinelanguage{pvsNums}[]{pvs}
{
  numbers=left,
  numberstyle=\tiny,
  stepnumber=2,
  numbersep=4pt
%  firstnumber=1
}

\lstdefinelanguage{pvsNumsRight}[]{pvs}
{
  numbers=right,
  numberstyle=\tiny,
  stepnumber=2,
  numbersep=4pt
%  firstnumber=1
}

\newcommand{\pvs}{\lstinline[language=PVS]}

\lstnewenvironment{BigPVS}%
  {\lstset{language=BigPVS}}
  {}

\lstnewenvironment{PVSNums}%
  {
  \if@firstcolumn
    \lstset{language=pvs, numbers=left, firstnumber=auto}
  \else
    \lstset{language=pvs, numbers=right, firstnumber=auto}
  \fi
  }
  {}

\lstnewenvironment{PVSNumsRight}%
  {
    \lstset{language=pvs, numbers=right, firstnumber=auto}
  }
  {}

\newcommand{\figpvs}[5]{
  \begin{figure}[#1]
      \hrule \F
      {\figuresize \bf #2}
      \lstinputlisting[language=pvs]{#5}
      \F \hrule \F
      \caption{#3}
      \label{fig: #4}
  \end{figure}
}

\newcommand{\linefigpvs}[9]{

}

\newcommand{\twofigpvs}[8]{
  \begin{figure}[#1]
    \hrule \F
    {\figuresize \bf #2} \\
    \two{#5}{#6}
    {
      \lstinputlisting[language=pvsLang]{#7}
    }
    {
      \lstinputlisting[language=pvsLang]{#8}
    }
    \F \hrule \F
    \caption{#3}
    \label{fig: #4}
  \end{figure}
}

\lstdefinelanguage{pvsproof}{
  basicstyle=\tt \figuresize,
  mathescape=true,
  tabsize=4,
  sensitive=false,
  columns=fullflexible,
  keepspaces=false,
  flexiblecolumns=true,
  basewidth=0.05em,
}


\documentclass[12pt]{article}
\usepackage{amssymb,latexsym,amsmath,bm}

\newif\ifpdf
\ifx\pdfoutput\undefined
\pdffalse % we are not running PDFLaTeX
\else
\pdfoutput=1 % we are running PDFLaTeX
\pdftrue
\fi
\ifpdf
\usepackage[pdftex]{graphicx}
\else
\usepackage{graphicx}
\fi
\ifpdf
\DeclareGraphicsExtensions{.pdf, .jpg, .tif}
\else
\DeclareGraphicsExtensions{.eps, .jpg}
\fi

\textwidth = 6.5 in
\textheight = 9 in
\oddsidemargin = 0.0 in
\evensidemargin = 0.0 in
\topmargin = 0.0 in
\headheight = 0.0 in
\headsep = 0.0 in
\parskip = 0.2 in
\parindent = 0.0 in

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\khat}{\hat{\mathbf k}}
\newcommand{\uv}{\mathbf u}
\newcommand{\up}{\mathbf u'}
\newcommand{\w}{\mathbf w}
\newcommand{\grad}{\nabla}
\newcommand{\curlp}{\gradp \times}
\newcommand{\curl}{\grad \times}
\newcommand{\gradp}{\nabla'}
\newcommand{\bk}{\bm{\kappa}}
\newcommand{\kp}{\kappa}

\DeclareMathOperator{\Span}{span}

\title{Transfer function in the Boussinesq equations}
\author{Susan Kurien}
\begin{document}
\section{Preliminaries}
We would like to compute the energy transfer as a function of
wavenumber in the Boussinesq equations which are given by:
\begin{eqnarray}
\frac{\partial}{\partial t}{u}_j + u_i \partial_i u_j + f\epsilon_{j3k}u_k +
\frac{1}{\rho_0}\partial_j p + \delta_{j3}\frac{\tilde\rho}{\rho_0}g\delta_{j3} &=&
\nu \partial_i^2 u_j\label{momentum}\\ \frac{\partial}{\partial t}\tilde\rho+u_i \partial_i \tilde\rho  -
bu_3 &=& \gamma \partial_i^2 \tilde \rho \label{density}\\ \partial_i u_i &=& 0,
\label{bous}
\end{eqnarray}
in a cartesian coordinate system with unit vectors ($\hat{\bm{x}_1},
\hat{\bm{x}_2}, \hat{\bm{x}_3}$); where $\bm{u}$ is the velocity, $u_3$ is its (vertical) component along $\hat{\bm{x}_3}$, $p$ is the pressure,
$f=2\Omega$ is the Coriolis parameter, $\Omega$ is the constant
background rotation rate, gravitational accelaration $g$ acts in the
vertical direction $\hat{\bm{x}_3}$, $\nu = \mu/\rho_0$ is the
viscosity and $\gamma$ is the mass diffusivity coefficient.  The total
density is $\rho(\bm{x}) = \rho_0 - bx_3 + \tilde\rho(\bm{x})$, where
$\rho_0$ is the constant background, $b$ is also constant and larger
than zero for stable stratification, and $\tilde \rho$ is the density
fluctuation such that $\tilde \rho \ll bw \ll \rho_0$.

Let us derive the energy spectral distribution dynamics first directly
from the fourier representation of the momentum-density equations and
then from the physical space correlation function equations.

\section{Fourier transform of the momentum-density equations}

Let us denote the fourier transform operator by
$\cal{F}_{\bk}$ (using Pope's notation). The fourier transform of a function is denoted by the tilde above the corresponding variable. Term by term the fourier transform of Eq. \ref{momentum} is:
\begin{eqnarray}
{\cal F}_{\bk}\{\partial_t u_j\} &=& \frac{d}{dt} \tilde u_j(\bm{\kappa},t) =
\frac{d}{dt}\sum_{\bm{k}} e^{i\bm{\kappa\cdot x}} u_j(\bm{x},t)\\
{\cal F}_{\bk}\{u_i \partial_i u_j\} &=& \tilde G_j(\bm{\kappa})\\
{\cal F}_{\bk}\{f\epsilon_{j3k}~u_k \} &=& f\epsilon_{j3k}\tilde u_k(\bm{\kappa})\\
{\cal F}_{\bk}\{\frac{1}{\rho_0}\partial_j p\} &=& \frac{-i}{\rho_0} \kappa_j \tilde p \\
{\cal F}_{\bk}\{\frac{\tilde\rho}{\rho_0}g \delta_{j3}\}&=& \frac{g \delta_{j3}}{\rho_0}\tilde{\tilde \rho}\\
{\cal F}_{\bk}\{\nu \partial_i^2 u_j\}& =& -\nu\kappa^2 \tilde u_j
\end{eqnarray}
Hence the fourier representation of the momentum equation is:
\begin{eqnarray}
\frac{d}{dt} \tilde u_j(\bm{\kappa},t) + \tilde G_j(\bm{\kappa}) + f\epsilon_{j3k}\tilde u_k(\bm{\kappa}) - \frac{i}{\rho_0} \kappa_j \tilde p +  \delta_{j3}\frac{g\delta_{j3}}{\rho_0}\tilde{\tilde \rho} =  -\nu\kappa^2 \tilde u_j
\label{momentum-f}
\end{eqnarray}
Similarly, the density equation and the incompressibility may be written as:
\begin{eqnarray}
\frac{d}{dt} \tilde {\tilde \rho} + \tilde J(\bm{\kappa}) - b \tilde u_3 = \gamma \kappa^2 \tilde {\tilde \rho}\label{density-f}\\
\kappa_i \cdot \tilde u_i \label{incomp-f} = 0
\end{eqnarray}
where ${\cal F}_{\bm{\kappa}}\{u_i \partial_i \tilde\rho\} = \tilde J(\bm{\kappa})$, the $\tilde \tilde$ on the $\rho$ denotes fourier transform of the density fluctuations.

Note that $\tilde G_j(\bm{\kappa})$ and $\tilde J(\bm{\kappa})$ are the fourier
transforms of the non-linear terms and may be simplified to diadic
interactions (see eg. Pope chapter 6 for $\tilde G_j$) but we won't do that here since we
can directly compute the fourier transforms of the entire term.

Next, lets write down the projection operator for the pressure.
Left-multiply Eq. (\ref{momentum-f}) by $\kp_j$ and using Eq. (\ref{incomp-f}):
\begin{eqnarray}
\kp_j\tilde G_j(\bk) &-& f \tilde \omega_3 - \frac{i}{\rho_0} \kp^2 \tilde p +
\frac{g\delta_{j3}}{\rho_0}\kp_3\tilde{\tilde \rho} = 0\nonumber\\
\mbox{that~is,}~~\tilde p &=& -i\frac{\rho_0}{\kp^2}
\Big(\kp_j\tilde G_j(\bk) - f \tilde \omega_3
+  \frac{g\delta_{j3}}{\rho_0}\kp_3\tilde{\tilde \rho}\Big)
\label{k-momentum-f}
\end{eqnarray}
where $\tilde\omega_3=\epsilon_{3jk}\kp_j \tilde u_k(\bk)$ is the
vertical component of the fourier transform of the vorticity. Note the contribution to the pressure from rotation and stratification. Substitute (\ref{k-momentum-f}) back into (\ref{momentum-f}) (keep track of dummy indices!!)..
\begin{eqnarray}
\frac{d}{dt} \tilde u_j(\bm{\kappa},t)  + (\delta_{ij}-\frac{\kappa_j \kp_i}{\kp^2}) \tilde G_i + (\delta_{j3} - \frac{\kp_j \kp_3}{\kp^2})\frac{g\delta_{j3}}{\rho_0}\tilde{\tilde \rho} =  -\nu\kappa^2 \tilde u_j
\label{momentum-f-elimp}
\end{eqnarray}
THUS: when pressure is eliminated using incompressibility, the rotation terms disappear from the momentum equation.

\subsection{Energy spectrum evolution}
The mean energy in wavenumber $\bk$ is given by:
\begin{equation}
E(\bk) = \langle \tilde{{u}_j}(\bk) \tilde{{u}_j}^*(\bk) \rangle = \langle \tilde{{u}_j}(\bk) \tilde{{u}_j}(-\bk) \rangle= \langle |\tilde{\bm{u}}(\bk)|^2 \rangle
\end{equation}
where $\langle \cdot  \rangle$ denotes the average.
So, left-multiply Eq. (\ref{momentum-f-elimp}) by
$\tilde{u_j}^*(\bk)$ and add it to the equation for $\tilde{u_j}^*$
left-multiplied by $\tilde{u_j}$ (the $\bk$ argument is implicit everywhere and the $\delta_{j3}$ for the direction of $g$ will be dropped from now on):
\begin{equation}
\frac{d}{dt} \langle\tilde u_j \tilde{{u}_j}^*\rangle +
\langle \tilde{u_j}^* \tilde G_j + \tilde{u_j} \tilde G_j^*\rangle+ \langle \tilde{u_3}^*\frac{g}{\rho_0}\tilde{\tilde \rho}  + \tilde u_3 \frac{g}{\rho_0}
\tilde{\tilde \rho}^*\rangle  = -\nu \kp^2 \langle\tilde u_j \tilde u_j^*\rangle
\label{ke}
\end{equation}
The potential energy in wavenumber $\bk$ is obtained by multiplying
(\ref{density-f}) by $\frac{g }{b \rho_0} \tilde{\tilde\rho}^*$ and adding to the equation for
$\frac{g}{b \rho_0}\tilde{\tilde\rho}^*$ multiplied by $\tilde{\tilde\rho}$:
\begin{eqnarray}
\frac{d}{dt}( \frac{g }{b \rho_0} \tilde{\tilde \rho} \tilde{\tilde \rho}^*)  +
\frac{g }{\rho_0} \tilde{\tilde \rho}^* \tilde J(\bm{\kappa}) + \frac{g}{\rho_0} \tilde{\tilde \rho} \tilde J^*(\bm{\kappa})
- \frac{g}{\rho_0} \tilde u_3 \tilde{\tilde \rho}^* - \frac{g }{\rho_0} \tilde u_3^* \tilde{\tilde \rho} = \gamma \frac{g }{b\rho_0} \kappa^2 (\tilde u_3^* \tilde {\tilde \rho} + \tilde u_3 \tilde {\tilde \rho}^*)
\label{pe}
\end{eqnarray}

The total energy evolution is given by the sum of Eqs.~\ref{ke} and \ref{pe}. The transfer terms are:
\begin{itemize}
\item Kinetic-to-kinetic term: $T_K(\bk)=\tilde{u_j}^* \tilde G_j + \tilde{u_j} \tilde G_j^*$

\item Potential-to-potential term: $T_P(\bk)=\frac{g }{\rho_0} (\tilde{\tilde \rho}^* \tilde J +  \tilde{\tilde \rho} \tilde J^*)$

\item Exchange term: $T_E(\bk)=\frac{g}{\rho_0}(\tilde{u_3}^*\tilde{\tilde \rho}  + \tilde u_3 \tilde{\tilde \rho}^*)$
\end{itemize}again, $\tilde G_j(\bk) = {\cal F}_{\bk}\{u_i \partial_i u_j\}$, $\tilde J(\bk) = {\cal F}_{\bm{\kappa}}\{u_i \partial_i \tilde\rho\}$.
This should be enough for our calculations.

\section{The connection to physical space (two-point correlation function) representation}

watch this space.

\end{document}


\chapter{Overview of sharp wave-ripple detectors}

Given a signal $\z_t$ and an associated task (such as ``detect the sharp
wave-ripples in this hippocampal multi-electrode LFP signal''), there are a
sheer infinite number of techniques available to tackle the problem.

This chapter gives an overview of these techniques, and of how they differ or
are equivalent. For a select number of techniques, we also present their
performance in sharp-wave ripple detection. Two techniques are analysed in
detail in the next two chapters (\namecref{sec:RNN} and \namecref{sec:GEVD}).

% Different names are used for the same technique, and the same name is used for different techniques. % e.g. filtering, see Barber p. 527 footnote

% \section{Signals and maps}
\section{Signals}

Let us first define the relevant signals.

$\z_t \in \reals^C$ is the input sample at discrete time step
$t$,\footnotemark{} with $C$ the number of electrodes (or `channels')
simultaneously recorded from.

The output is a one-dimensional variable $\in \reals$, signifying how
strongly a sharp wave-ripple event is currently present in the input signal
$\z_t$. For data-driven algorithms, we make a distinction between $x_t \in
\reals$, which is the `ideal' output of the detection algorithm (this is
implicitly or explicitly the training signal); and $\xhat_t$, which is the
actual output of the algorithm for the input $\z_t$. For non-data-driven
algorithms (such as classic temporal filtering), this distinction does not
matter.

The scientist running the SWR detection experiment imposes a threshold on
$\xhat_t$, to continously make binary decisions on whether an SWR event is
detected or not.

Some detection algorithms additionally maintain an $M$-dimensional internal
state vector, or 'memory', which we shall denote by $\h_t \in \reals^M$.

\footnotetext{We consider only discrete-time systems, as \swr{} detection is
mostly performed in the digital domain. (Though see \cite{Ego-Stengel2009}
for an all-analog closed-loop SWR detection system).}

% \section{Modelling versus machine learning}
\section{A note about terminology}
% \section{Modelling, science and engineering}

We will use the terms `model' and `event detection algorithm'
interchangeably, to denote the calculations that transform the $\z_t$ signal
into an acceptable $x_t$ signal.

The term ``model'' suggests that we want to build a causal or mechanistic
mathematical model of the underlying data-generating system (the
hippocampus), similarly to how mathematical equations are used to describe
reality in physics, or to the generalised and multilevel linear models
ubiquitous in the social sciences. Or, more pertinent to neural science, how
nonlinear differential equations describe membrane current and action
potential dynamics \cite{Hodgkin1952}, and how generalised linear models,
convolutional neural networks, and recurrent neural networks describe and map
to different components of the early visual processing \cite{Mante2005},
higher order visual processing \cite{Nayebi2018}, and auditory processing
\cite{Kell2018} systems.
%
% We *must* also cite Mante & Susillo's 2013 RNN somewhere :)

In the case of the hippocampal local electric field potential, this would
thus correspond to building a model of the hippocampus that generates the
observed time series $\z_t$. This is emphathically \emph{not} what we want to
do. Our primary goals are to detect sharp wave-ripples in the $\z_t$ signal,
with as low a latency and as high a precision and sensitivity as possible.

We can frame this task as an inverse modelling problem however. Let us
imagine a very simplified model of the hippocampus, where it can exist in
either of two states: SWR-generating or not. A forward model would then
consist of calculations that generate realistic LFP time series given the
state, or given the `inputs' to the hippocampus system. The inverse problem
is to infer the state of the hippocampus (which is a `latent variable', to
borrow a term from probabilistic modelling) from the observed LFP time series
$\z_t$.

% In general, signal processing techniques may be divided in two techniques:
% Only care about I/O vs. care about internal state

A final note: we will often use `the scientist' to mean the eventual operator
and user of the sharp wave-ripple detection algorithm.

\section{Typology of online event detectors}

We can categorise event detectors along multiple axes.

\subsection{Data-driven or not?}

A first distinction can be made between data-driven and non-data-driven
algorithms. Temporal linear time-invariant (LTI) filters belong to the
non-data-driven category. The scientist chooses the frequency band(s) to
suppress, and a few additional parameters that depend on the particular
filter design method, such as the amount of attenuation and the `transition
width' between pass- and stop-bands. The scientists learns good values for
these parameters from experience over multiple experiments, either done by
themselves, or by others, who transfer their knowledge through the literature
or through lab training lineages and personal peer-to-peer communication.

Most other algorithms belong to the data-driven class however. These are the
(in)famous \emph{machine-learning} algorithms. Here, the algorithm parameters
are not set by scientists, but are a function of the data (with `the data'
being a dataset of the same type as the data on which the online algorithm
will eventually be applied -- i.e. a dataset $\z_t$, and optionally a
corresponding output time series $x_t$).

Within this class of data-driven algorithms, a further distinction can be made between

Examples include

linear filters whose parameters are a function of (labelled) data

calculated through a decomposition of a

The distinction between these two categories is less clear than it might seem
at first glance. . Similarly, prior knowledge about the problem at hand is
often incorporated into the design of a particular data-driven algorithm
implementation.

The Bayesian framework is the proper method to unite both data-driven and
non-data-driven algorithms into one continuum. We will not expand on this in
this thesis.

A first class only considers a fixed number $p$ of past input samples, $\z_t,
\z_{t-1} \tdots \z_{t-p}$, to calculate the output $\xhat_t$ at time $t$.
This includes FIR filters, and various 'memoryless' learning models such as
generalised linear models, multi-layer perceptrons (AKA feedforward neural
networks), convolutional neural networks, and support vector machines.

On the other hand, some event detectors

\begin{enumerate}
\item FIR filter.
\end{enumerate}