% !TeX root = ../main.tex

\begin{survey}
\label{cha:survey}

\title{
  Bound Analysis : A Brief Overview
}

\maketitle

Automatic methods for computing bounds on the resource consumption of programs are an active research field. Bound analysis, or complexity analysis, tells the potential worst-case scenario complexity of a program, and is thus useful for predicting performance. This literature review briefly documents some of the recent works on automated bound analysis.

\tableofcontents

\section{Introduction}

Bound analysis is a branch of program analysis which attempts to determine the \textit{resource consumption} of a given program. The result is a symbolic formula over the input parameters of the program. Basically, it's the problem of deciding how many steps a Turing machine can take at most, given any input.

This is obviously an undecidable problem, since if it's decidable, we can use the decision procedure to decide the famous halting problem:

\begin{theorem}[Turing 1936\cite{turing_computable_1937}, Halting Problem]
  Given a Turing machine M and input word w, it is undecidable if M will halt on w.
\end{theorem}

Bound analysis nowadays is usually based upon \textit{termination analysis}{\cite{cook_proving_2011}}, which is the problem of deciding if a program always terminates on any input. Termination analysis is even not semi-decidable. In fact, it is neither RE nor co-RE.

The undecidability of this problem implies that, any sound method to bound analysis must be incomplete. That is, there is always an input on which the method cannot give a result. Current techniques often presume a special subset of program features that make the program class decidable (for termination), one of which is simple linear loop program. Even so, proof of a program to be terminating is not always directly appliable to bound analysis.

There are many automated bound analysis methods in the literature. We mainly focus on two methodologies in the following sections.

\section{Preliminary}

We briefly introduce some concepts here.

\begin{definition}[Transition System]
  Formally, a {\textbf{transition system}} is a pair $(S, R, I)$ where $S$ is a set of states and R is a relation of state transitions (i.e., a subset of $S \times S$). A transition from state p to state q, i.e. $(p, q) \in R$, is written as $R(p,q)$. $I$ is the set of initial states.
\end{definition}

Transition system can be seen as the most low-level model we use. On the other hand, a program has higher abstraction via CFG:

\begin{definition}[Program]
  A program is a CFG $(L, \tau, l_0)$, where L is the set of program locations, $\tau \subseteq L \times R \times L$ is the transition relation, where $R \subseteq S \times S$, and $l_0$ is a distinguished initial location.

  A program state is $(l, s)$ where l is a location and s is a memory state.

  A computation is a sequence $(l_0, s_0), (l_1, s_1), \ldots$ where for each i > 0, there is $(l_i, \rho, l_{i + 1}) \in \tau$ and $\rho (s_i, s_{i + 1})$.

  A program is said to be terminating if there is no infinite computation.
\end{definition}

If all the variables of a program are natural number, we can get a special case : the lossy VASS.

\begin{definition}[Lossy Vector Addition System with States]
  A VASS is a program with a fixed set of variables $\{ x_1, \ldots, x_n \}$. The edge is denoted as $x' \leqslant x + d$ with $d \in \mathbb{N}^n$.
\end{definition}

Note that variables in VASS are natural numbers, so there is a lower bound 0 for them.

\begin{definition}[Loop]
  Let G = (V, E) be a directed graph with a unique entrypoint such that all nodes are reachable from the entry point. A node a dominates a node b, if every path from entry to b includes a. An edge l1 $\rightarrow$ l2 is a back edge, if l2 dominates l1 . G is reducible, if G becomes acyclic after removing all backedges. A node is a loop header, if it is the target of a back edge. The (natural) loop of a loop header h in a reducible graph is the maximal set of nodes L such that for all x $\in$ L (1) h dominates x and (2) there is a back edge from some node n to h such that there is a path from x to node n that does not contain h.

  A loop-path $\pi$ is a simple cyclic path, which starts and ends at some loop header l, and visits only locations inside the natural loop of l.
\end{definition}

\begin{definition}[Inductive Invariants]
  An invariant map Q maps program locations l to state formulas $I_l$.

  For a program C, Q is inductive if for any $(l, l', \rho) \in \tau_C$, the following holds:

  \[ \forall s, s', I_l (s) \wedge \rho (s, s') \rightarrow I_{l'} (s') \]

\end{definition}

\section{Control Flow Abstraction}

The tool \textit{Loopus} {\cite{sinn_simple_2014, sinn_complexity_2017, sinn_difference_2015}} utilizes an automated method of bound analysis. The algorithm is composed of 4 steps:

\begin{enumerate}
  \item Abstracting a program into a VASS

  \item Control Flow Abstraction, which 'flatten' the VASS and merge the loops

  \item Ranking function generation, which proves the VASS to be terminating

  \item Bound Analysis, which computes a bound for every transition.
\end{enumerate}

The first step is neglected here. The second step, \textit{control flow abstraction}, basically analysizes the program and find all the loop paths. It then transform the original VASS into a singleton state transition system with many transtion on it. The algorithm is as follows:

\begin{figure}
    \centering
    \includegraphics[width=0.8\linewidth]{survey1.pdf}
    \caption{control flow abstraction}
\end{figure}

Now we get a transition system with a single state where each transition stands for a loop path. In {\cite{sinn_simple_2014}}, the transition system is then analysized and a ranking function is generated. The ranking function is lexicographic, and each component of the ranking function is a variable among $x_1, \ldots, x_n$. In general, a ranking function could be any valid expression, thus it's possible that a single-variable ranking function does not exist. However inexpressive, this generation algorithm is relatively complete.

The last step of the analysis gives the actual bound. Suppose the transtion $\rho$ has the ranking function component x, then $\rho$ can be executed $\tmop{Init} (x)$ times if no other transitions increase x. So we have to take
other transitions into account. The algorithm is as follows:

\begin{figure}
    \centering
    \includegraphics[width=0.8\linewidth]{survey2.pdf}
    \caption{algorithm to compute the bound}
\end{figure}

Now that a bound for each transition is obtained, we simply add them to get
the final bound.

\section{Compute WCCC using Ehrhart polynomials}

The tool \textit{Rank}{\cite{alias_multi-dimensional_2010}} gives a novel way of computing \textit{worst-case computational complexity}(WCCC). The method also generate (lexicographic) ranking function first. However, different to the method mentioned above, the ranking function can be any affine expression. Besides, since no control flow abstraction are used, the CFG is general. Thus every location in the graph is assigned a ranking function, in other words, the ranking function is inductive.

Consider any trace $(l_0, x_0), \ldots, (l_p, x_p)$, by definition of ranking function, we have $r (l_i, x_i) < r (l_{i + 1}, x_{i + 1})$, thus, every states in the trace must be distinct.

Hence the length of a trace is bounded by:

\[ \tmop{WCCC} \leqslant \# \bigcup_k r (k, P_k) \leqslant \sum_k \#r (k, P_k)
\]

The goal here is to compute $r (k, P_k)$ for each location k, where $P_k$ is the inductive invariant at location k. Basically, we find the number of points in the intersection of $\mathbb{Z}^n$ and the image of a polyhedron.

The authors use Smith normal form and Ehrhart polynomials to compute this ordinal. Suppose $r (k, x) = R x + r$, we compute $R = U S V$, where U and V are unimodular and $S = \left[ \begin{array}{cc}
  D & 0\\
  0 & 0
\end{array} \right]$ where D is diagonal positive matrix of the same rank as R. Then $\#V$ is a slight overapproximation of $\#r (k, P_k)$. The number of integer vector in V is obtained using Ehrhart polynomials.

\bibliographystyle{unsrtnat}
\bibliography{ref/library}

\end{survey}


\section{Reduction Method and Challenges} \label{gua:sec:paramsynt}

We show how to use existing cutoff results of Emerson and Kahlon~\cite{Emerson00} to reduce the PMCP to a standard model checking problem,
and parameterized template synthesis to template synthesis.
We note the limitations of the existing results that are crucial in the context of synthesis.

\subsection*{Reduction by Cutoffs} \label{page:gua:def:cutoff}

\parbf{Cutoffs}
A \emph{cutoff} for a system type $(\templateI,\templateII)$ and a specification $\spec$ is a number $c \in \bbN$ such that:
\[
\forall n \ge c: \left( \cutoffsys \models \spec ~~\Iff~~ \largesys \models \spec \right).
\]
Similarly,
a \emph{cutoff for deadlock detection} for a system type $(\templateI,\templateII)$ is a number $c \in \bbN$ such that:
\[
\forall n \ge c: \left( \cutoffsys \textit{ has a deadlock} ~~\Iff~~ \largesys \textit{ has a deadlock}\right).
\]
Here, ``has a deadlock'' means ``there is a locally or globally deadlocked run''.

For the systems and specifications presented in this work, cutoffs can be computed from
the size of the process template $B$ and the number $k$ of copies of $B$
mentioned in the specification, and are given as expressions like
$\card{B}+k+1$.

\begin{remark}\label{re:EK_cutoffs}
Our definition of a cutoff is different from that of Emerson and Kahlon~\cite{Emerson00}, and instead similar to, e.g., Emerson and Namjoshi~\cite{Emerso03}. The reason is that we want the following property to hold for any $(A,B)$ and $\Phi$:
\begin{quote}
if $n_0$ is the smallest number such that ~$\forall n \geq n_0:\ \largesys \models \Phi$,
then any $c<n_0$ is not a cutoff, any $c\geq n_0$ is a cutoff.
\end{quote}
We call $n_0$ the \emph{tight} cutoff.
The definition of Emerson and Kahlon~\cite[page 2]{Emerson00} requires that
$\forall{n\leq c}. \largesys \models \Phi$
if and only if
$\forall{n \geq 1}: \largesys \models \Phi$, and thus allows stating $c<n_0$ as a cutoff if $\Phi$ does not hold for all $n$.
\end{remark}

\parbf{Parameterized synthesis}
We encourage the reader to revisit Chapter~\ref{defs:bounded_synthesis} on page~\pageref{page:defs:bounded_synthesis}
to recall how bounded synthesis works in the case of non-distributed systems.
Now we adapt the procedure to guarded parameterized systems.
In parameterized model checking
a cutoff allows us to check whether any ``big'' system satisfies the specification
by checking it in the cutoff system.
A similar reduction applies to the parameterized synthesis problem~\cite{JB14}.
For guarded protocols, we obtain the following
\emph{semi-decision procedure for parameterized synthesis}\ak{is it decidable?}:
\begin{enumerate}
  \item[0.] set initial bound $(\bound_A,\bound_B)$ on the size of the process templates;
  \item[1.] determine the cutoff for $(\bound_A,\bound_B)$ and $\spec$;
  \item[2.] solve the bounded template synthesis problem for cutoff, size bound, and $\spec$;
  \item[3.] if successful, return $(A,B)$, else increase $(\bound_A,\bound_B)$ and goto (1).
\end{enumerate}
This procedure was implemented inside our parameterized synthesis tool PARTY~\cite{party}
by Simon Au{\ss}erlechner as a part of his Master Thesis~\cite{SimonThesis}.

\subsection*{Existing Cutoff Results}
Emerson and Kahlon~\cite{Emerson00} have shown:

\begin{theorem}[Disjunctive Cutoff Theorem] \label{thm:disj-cutoff-pairs}
    For closed disjunctive systems $A{\parallel}B^n$,
    $\card{B}+2$ is a cutoff {$^{(\dagger)}$} for formulas of the
    form $\A h(A,B^{(1)})$ and $\E h(A,B^{(1)})$, and for global
    deadlock detection.
\end{theorem}

\begin{theorem}[Conjunctive Cutoff Theorem] \label{thm:conj-cutoff}
    For closed conjunctive systems ${A{\parallel}B^n}$,
    $2\card{B}$ is a cutoff {$^{(\dagger)}$} for formulas of the
    form $\A h(A)$ and $\E h(A)$, and for global deadlock detection.
    For formulas of the form $\A h(B^{(1)})$ and $\E h(B^{(1)})$,
    $2\card{B}+1$ is a cutoff.
\end{theorem}
\noindent
In the above theorems,
$h(A)$ (resp.\ $h(B^{(1)})$) means that the formula talks about the $A$-process only (resp.\ $B_1$).

\begin{remark} ${(\dagger)}$
Note that Emerson and Kahlon \cite{Emerson00} proved these results for
a different definition of a cutoff (see Remark \ref{re:EK_cutoffs}).
Their results also hold for our definition, except possibly for
global deadlocks.  For the latter case to hold with the new cutoff definition, one
also needs to prove the direction ``global deadlock in the cutoff system implies global
deadlock in a large system'' (later called Monotonicity Lemma).
In Sections~\ref{gua:sec:proofs-disj-deadlock-unfair} and \ref{gua:sec:proofs-disj-deadlock-fair},
Sections~\ref{gua:sec:proofs-conj-deadlock-unfair} and \ref{gua:sec:proofs-conj-deadlock-fair},
we prove these lemmas for the case of general deadlock (global \emph{or} local).
\end{remark}

\subsection*{Challenge: Open Systems}
For any open system $S$ there exists a closed system $S'$ such that $
S$ and $S'$ cannot be distinguished by $\LTL$ specifications
(e.g., see Manna and Pnueli~\cite{Manna92}). Thus, one approach to PMC for open
systems is to use a translation between open and closed systems, and then use the
existing cutoff results for closed systems.

%While such an approach works in theory, it is not feasible when cutoffs
%depend on the size of process templates: in this case the conversion not only results in a
%blowup of the local state space of each process, but also in the number of
%processes that we need to consider.
While such an approach works in theory, it might not be feasible in practice:
since cutoffs depend on the size of the process templates,
and the translation blows up the process template,
it also blows up the cutoffs.
Thus, cutoffs that directly support open systems are important.

\subsection*{Challenge: Liveness and Deadlocks under Fairness}
We are interested in cutoff results that support liveness properties.
Consider a specification $\Phi=h(A,B^{(k)})$.
In general, we would like to consider only runs where all processes move infinitely often,
i.e.,
use the unconditional fairness assumption $\forall{p}. \GF \sched_p$ and thus have $\A_{uncond}\Phi$.
However, this would mean that we accept all systems that always go into a local deadlock,
since then the assumption is violated (i.e., there will be no unconditionally-fair runs).
This is especially undesirable in synthesis, because the synthesizer often tries to violate the assumptions to satisfy the specification.
To avoid this,
we require the absence of local deadlocks.
But local deadlocks may appear due to unfair scheduling.
Therefore we require the absence of local deadlocks under the strong fairness assumption,
i.e., we require satisfaction of the formula
$\A_{strong} \spec_{\neg dead}=\big(\forall{p}. (\GF \enabled_p \impl \GF \sched_p)\big) \impl \forall{p}. \GF\enabled_p$.
This formula can be roughly read as ``the absence of local deadlocks under fair scheduling''.
Since absence of global deadlocks and absence of local deadlocks under strong fairness imply unconditional fairness,
we can safely use $\A_{uncond}\Phi$.

%In these systems, processes may be disabled depending on their input and the global state. \change{Thus, strong fairness $\forall{p}. (\GF \enabled_p \impl \GF \sched_p)$ is an insufficient assumption, since the environment can easily violate liveness properties by choosing inputs and scheduling such that some process is only enabled finitely often.}
%{Thus, strong fairness $\forall{p}. (\GF \enabled_p \impl \GF \sched_p)$ is an
%insufficient assumption, since the environment can easily violate a process's liveness
%property by choosing inputs and scheduling such that the process never moves
%after some moment.}
%%
%Moreover, using the unconditional fairness assumption $\forall{p}. \GF \sched_p$
%\remove{for the complete specification} is also undesirable, since then we would accept all systems that always go into a local deadlock. This is especially undesirable in synthesis.
%To exclude this case, we require absence of local deadlocks under the strong fairness assumption.

In summary, for a parameterized specification $\spec$, we consider satisfaction of
\[
\begin{array}{lllll}
\textit{``all runs are infinite''} &~~\land~~& \A_{strong} \spec_{\neg dead} & ~~\land~~ & \A_{uncond} \spec.
\end{array}
\]
%
This is equivalent to $\textit{``all runs are infinite''} \land \A_{strong} (\spec_{\neg dead} \,\land\, \spec)$, but by considering the form above we can separate the tasks of deadlock detection and of model checking $\LTLmX$-properties, and obtain modular cutoffs.
(The phrase ``all runs are infinite'' is another way of saying ``all runs have no global deadlocks''.)

%%%In the following, we present cutoffs for problems of the forms
%%%(i) $\A_{uncond} \spec$ and
%%%(ii) $\E_{strong} \spec_{dead} \lor \textit{``some run is finite''}$
%%%(and the variants of (i) with $\E$ path quantifier).
%%%%, as well as for the detection of global deadlocks.
%%%% AK: the previous version reads like we provide cutoffs for three problems -- we have only two


%++++++++++++++++++++++++++++++++++++++++
% Don't modify this section unless you know what you're doing!
\documentclass[letterpaper,11pt]{article}
\usepackage{natbib}
\bibliographystyle{unsrtnat}
\usepackage{tabularx} % extra features for tabular environment
\usepackage{amsmath}  % improve math presentation
\usepackage{graphicx} % takes care of graphic including machinery
\usepackage{listings}
\usepackage{braket}
\usepackage[margin=1in,letterpaper]{geometry} % decreases margins
%\usepackage{cite} % takes care of citations
\usepackage[final]{hyperref} % adds hyper links inside the generated pdf file
\hypersetup{
	colorlinks=true,       % false: boxed links; true: colored links
	linkcolor=blue,        % color of internal links
	citecolor=blue,        % color of links to bibliography
	filecolor=magenta,     % color of file links
	urlcolor=blue
}
%++++++++++++++++++++++++++++++++++++++++

\usepackage[
backend=biber,
style=numeric,
sorting=none
]{biblatex}

\addbibresource{bibliography.bib}

\begin{document}

\title{Quantum Machine Learning Project}
\maketitle

For this project, you will perform quantum machine learning on the Scikit learn breast cancer data set. The data can be obtained the following way
\begin{lstlisting}[language=Python]
from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()
x = data.data #features
y = data.target #targets

\end{lstlisting}
x is the feature matrix and y are the targets.

\section*{a) Encoding the Data Into a Quantum State}
For this task you will consider a simple way of encoding a randomly generated data set sample into a quantum state:

\begin{lstlisting}[language=Python]
import qiskit as qk
import numpy as np
np.random.seed(42)

p = 2 #number of features
data_register = qk.QuantumRegister(p)
classical_register = qk.ClassicalRegister(1)

circuit = qk.QuantumCircuit(data_register, classical_register)

sample = np.random.uniform(size=p)
target = np.random.uniform(size=1)

for feature_idx in range(p):
    circuit.ry(2*np.pi*sample[feature_idx],data_register[feature_idx])

print(circuit)
\end{lstlisting}
The above code shows how a randomly generated data sample of $p=2$ features are encoded into a quantum state on two qubits utilizing Qiskit. Each feature is encoded into a respective qubit utilizing a $R_y(\theta)$ gate. The features are scaled with $2\pi$ to represent rotation angles (the $R_y(\theta)$ gate performs a rotation). The classical register will be used later for storing the measured value of the circuit.  print(circuit) can be utilized at any point to see what the circuit looks like.

\bigskip

Your task is to get familiar with the functionality utilized in the above example and implement your own function to encode $p$ of the first features in the breast cancer data set to a quantum state.

\section*{b) Processing the Encoded Data with Parameterized Gates}
After the quantum state has been encoded with the information of a data set sample, one needs extend the circuit with operations that process the state in a way that allows us to infer the target data. This can be done by introducing quantum gates that are dependant on learnable parameters $\boldsymbol{\theta}$. We will do this in a similar fashion as for the encoding of the features:

\begin{lstlisting}[language=Python]
n_params = 4
theta = 2*np.pi*np.random.uniform(size=n_params)

circuit.rx(theta[0],data_register[0])
circuit.ry(theta[1],data_register[1])
circuit.cx(data_register[0],data_register[1])
circuit.ry(theta[2],data_register[0])
circuit.rx(theta[3],data_register[1])

print(circuit)
\end{lstlisting}
The above parameterization of the quantum state is what we will refer to as the 'ansatz'. Your task is again to familiarize yourself with the functionality utilized in the above example and implement your own ansatz to be utilized together with the $p$ first features of the breast cancer data set. The number of learnable parameters 'theta' should be arbitrary.

\section*{c) Measuring the Quantum State and Making Inference}
The next step is to generate a prediction from our quantum machine learning model. This is done by performing a measurement on the quantum state:
\begin{lstlisting}[language=Python]
circuit.measure(data_register[-1],classical_register[0])
shots=1000

job = qk.execute(circuit,
                backend=qk.Aer.get_backend('qasm_simulator'),
                shots=shots,
                seed_simulator=42
                )
results = job.result()
results = results.get_counts(circuit)

prediction = 0
for key,value in results.items():
    if key == '1':
        prediction += value
prediction/=shots
print('Prediction:',prediction,'Target:',target[0])
\end{lstlisting}
\begin{verbatim}
    Prediction: 0.285 Target: 0.7319939418114051
\end{verbatim}
In the above example, we are first applying a measurement operation on the final qubit in the circuit, and we are interpreting our prediction as the probability that this qubit is in the $\ket{1}$ state. Make sure all the steps in the example are understood.

Implement your own function that generates a prediction by measuring one of the qubits.

\section*{d) Putting it all together}
Now it is time to put together all of the above steps. Ideally, you should make a class or a function that given a feature matrix of $n$ samples and an arbitrary number of model parameters, returns a vector of $n$ outputs. For example:
\begin{lstlisting}[language=Python]
n = 100 #number of samples
p = 10 #number of features
theta = np.random.uniform(size=20) #array of model parameters
X = np.random.uniform(size=(n,p)) #design matrix
y_pred = model(X,theta) #prediction, shape (n)
\end{lstlisting}

\bigskip

We will now deal with how to train the model:

\section*{e) Parameter Shift-Rule and Calculating the Analytical Gradient}
Since the model with random initial parameters is no good for inference, we need to optimize the parameters in order to yield good results, as is the usual with machine learning.

Since we are dealing with classification, we will use cross-entropy as the loss function

\begin{equation*}
    L = -\sum_{i=1}^{n}{y_i \ln{f(x_i;\boldsymbol{\theta})}},
\end{equation*}
where $y_i$ are the target labels, and $f(\boldsymbol{x}_i;\boldsymbol{\theta})$ is the output of our model for a given sample $\boldsymbol{x}_i$ and parameterization $\boldsymbol{\theta})$. We calculate the gradiant by taking the derivative of the loss with respect to the parameters

\begin{equation*}
    \frac{\partial}{\partial \boldsymbol{\theta}_k}L = \sum_{i=1}^{n}{\frac{f_i - y_i}{f_i(1 - f_i)}} \frac{\partial}{\partial \boldsymbol{\theta}_k}f_i,
\end{equation*}
where $f_i = f(x_i;\boldsymbol{\theta})$ for clarity. The only term we do not know how to calculate is $\frac{\partial}{\partial \boldsymbol{\theta}_k}f(x_i;\boldsymbol{\theta})$, but it turns out there is a simple trick to do this, the so-called parameter shift-rule \cite{ParameterShift}. To calculate the derivative of the model output, we need to evaluate the model twice with the respective parameter shifted by a value $\frac{\pi}{2}$ up and down. The two resulting outputs are then put together to yield the derivative

\begin{equation*}
    \frac{\partial f(x_i; \theta_1, \theta_2, \dots, \theta_k)}{\partial \theta_j}  = \frac{f(x_i; \theta_1, \theta_2, \dots, \theta_j + \pi /2, \dots, \theta_k) -f(x_i; \theta_1, \theta_2, \dots, \theta_j - \pi /2, \dots, \theta_k)}{2}
\end{equation*}

Train your model by utilizing the Parameter Shift-Rule and some gradient descent algorithm. Compare your results with for example logistic regression.

\section*{f) Adding Variations on the Data Encoding and Ansatz}
Change the gates utilized for the encoding of the data samples and also make changes to the parameterized ansatz. Train these new models on the breast cancer data set. How do they compare?

\newpage

\printbibliography

\end{document}


\chapter{Symbolic notation}
\label{app.notation}

\section{Alternative nomenclature}

\paragraph{Truth-functional logic.} TFL goes by other names. Sometimes it is called \emph{sentential logic}, because this branch of logic deals fundamentally with sentences. Sometimes it is called \emph{propositional logic} because it might also be thought to deal fundamentally with propositions. We have used with \emph{truth-functional logic} to emphasize that it deals only with assignments of truth and falsity to sentences and that its connectives are all truth-functional.

\paragraph{Formulas.} In \S\ref{s:TFLSentences}, we defined \emph{sentences} of TFL. These are also sometimes called `formulas' (or `well-formed formulas') since in TFL there is no distinction between a formula and a sentence.

\paragraph{Valuations.} Some texts call valuations \emph{truth-assignments} or \emph{truth-value assignments}.

\section{Alternative symbols}
In the history of formal logic, different symbols have been used at different times and by different authors. Often, authors were forced to use notation that their printers could typeset. This appendix presents some common symbols, so that you can recognize them if you encounter them in an article or in another book.

\paragraph{Negation.} Two commonly used symbols are the \emph{hoe}, `$\neg$', and the \emph{swung dash} or \emph{tilda}, `${\sim}$.' In some more advanced formal systems it is necessary to distinguish between two kinds of negation; the distinction is sometimes represented by using both `$\neg$' and `${\sim}$'. Older texts sometimes indicate negation by a line over the formula being negated, e.g., $\overline{A \eand B}$.

\paragraph{Disjunction.} The symbol `$\vee$' is typically used to symbolize inclusive disjunction. One etymology is from the Latin word `vel', meaning `or'.%In some systems, disjunction is written as addition.

\begin{table*}\centering\sffamily\footnotesize
\ra{1.25}
\begin{tabular}{@{}l l@{}}\toprule
\textth{Symbols of formal logic} & \\\midrule
negation & $\neg$, ${\sim}$\\
conjunction & $\wedge$, $\&$, {\scriptsize\textbullet}\\
disjunction & $\vee$\\
conditional & $\rightarrow$, $\supset$\\
biconditional & $\leftrightarrow$, $\equiv$\\
\bottomrule
\end{tabular}
\caption{}\label{symbols-all}
\end{table*}

\paragraph{Conjunction.}
Conjunction is often symbolized with the \emph{ampersand}, `{\&}'. The ampersand is a decorative form of the Latin word `et', which means `and'.  (Its etymology still lingers in certain fonts, particularly in italic fonts; thus an italic ampersand might appear as `\emph{\&}'.) This symbol is commonly used in natural English writing (e.g.  `Smith \& Sons'), and so even though it is a natural choice, many logicians use a different symbol to avoid confusion between the object and metalanguage---as a symbol in a formal system, the ampersand is not the English word `\&'. The most common choice now is `$\wedge$', which is a counterpart to the symbol used for disjunction. Sometimes a single dot, `{\scriptsize\textbullet}', is used. In some older texts, there is no symbol for conjunction at all; `$A$ and $B$' is simply written `$AB$'.

\paragraph{Conditional.} There are two common symbols for the conditional (which can also be called the \textit{material conditional}): the \emph{arrow}, `$\rightarrow$', and the \emph{hook}, `$\supset$'.

\paragraph{Biconditional.} The \emph{double-headed arrow}, `$\leftrightarrow$', is used in systems that use the arrow to represent the biconditional. Systems that use the hook for the conditional typically use the \emph{triple bar}, `$\equiv$', for the biconditional.

%
%
%
%\section*{Polish notation}
%
%This section briefly discusses sentential logic in Polish notation, a system of notation introduced in the late 1920s by the Polish logician Jan {\L}ukasiewicz.
%
%Lower case letters are used as sentence letters. The capital letter $N$ is used for negation. $A$ is used for disjunction, $K$ for conjunction, $C$ for the conditional, $E$ for the biconditional. (`A' is for alternation, another name for logical disjunction. `E' is for equivalence.)
%%\marginpar{
%%\begin{tabular}{cc}
%%notation & Polish\\
%%of TFL & notation\\
%%\enot & $N$\\
%%\eand & $K$\\
%%\eor & $A$\\
%%\eif & $C$\\
%%\eiff & $E$
%%\end{tabular}
%%}
%
%In Polish notation, a binary connective is written \emph{before} the two sentences that it connects. For example, the sentence $A\eand B$ of TFL would be written $Kab$ in Polish notation.
%
%The sentences $\enot A\eif B$ and $\enot (A\eif B)$ are very different; the main logical operator of the first is the conditional, but the main connective of the second is negation. In TFL, we show this by putting parentheses around the conditional in the second sentence. In Polish notation, parentheses are never required. The left-most connective is always the main connective. The first sentence would simply be written $CNab$ and the second $NCab$.
%
%This feature of Polish notation means that it is possible to evaluate sentences simply by working through the symbols from right to left. If you were constructing a truth table for $NKab$, for example, you would first consider the truth-values assigned to $b$ and $a$, then consider their conjunction, and then negate the result. The general rule for what to evaluate next in TFL is not nearly so simple. In TFL, the truth table for $\enot(A\eand B)$ requires looking at $A$ and $B$, then looking in the middle of the sentence at the conjunction, and then at the beginning of the sentence at the negation. Because the order of operations can be specified more mechanically in Polish notation, variants of Polish notation are used as the internal structure for many computer programming languages.
%


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

\usepackage{graphicx,caption}
\graphicspath{ {./images/} }
\usepackage{float}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[unicode]{hyperref}
\usepackage{amsmath}
\usepackage[shortlabels]{enumitem}

\title{Homework 3 - Theory}
\author{Dainese Fabio, 857661}
\date{March 22, 2020}

\begin{document}

\maketitle

\section{Exercise 1}
    \begin{figure}[H]
        \centering
        \includegraphics[width=0.5\textwidth]{1.png}
        \caption{Graph \(G=(V,E)\)}
        \label{fig:figure-1}
    \end{figure}

    \begin{enumerate}[a)]
        \item The \textit{clique number} of \(G\) is \(w(G) = 3\). This also means that the \textit{chromatic number} (\(c(G)\)) has a lower bound of:
        \begin{align*}
            c(G) &\geq w(G) \\
            c(G) &\geq 3
        \end{align*}

        \item The \textit{max degree} of \(G\) is \(\triangle(G) = max\{d(V) | v \in V\} = 4\). This also means that the \textit{chromatic number} (\(c(G)\)) has a upper bound of:
        \begin{align*}
            c(G) &\leq \triangle(G)+1 \\
            c(G) &\leq 5
        \end{align*}
    \end{enumerate}

    \begin{figure}[H]
        \centering
        \includegraphics[width=0.5\textwidth]{2.png}
        \caption{Graph \(G=(V,E)\)}
        \label{fig:figure-2}
    \end{figure}

    \begin{enumerate}[c)]
        \item The coloring illustrated in the 'Figure \ref{fig:figure-2}' is not a proper 3-colouring of \(G\), since the vertices \(v_{2}\) and \(v_{4}\) are adjacent and with the same color.

        \item The partitions of the vertex set induced by the provided colouring are:
        \begin{align*}
            V_{black} &= \{v_{2},v_{4},v_{5},v_{9},v_{11},v_{15},\} \\
            V_{red} &= \{v_{1},v_{3},v_{7},v_{8},v_{13},v_{14},\} \\
            V_{blue} &= \{v_{6},v_{10},v_{12},v_{16},\} \\
        \end{align*}
    \end{enumerate}

\section{Exercise 2}
    \begin{figure}[H]
        \centering
        \includegraphics[width=\textwidth]{3.png}
        \caption{Multi-layer network}
        \label{fig:figure-3}
    \end{figure}
    The multi-layer network pictured in the 'Figure \ref{fig:figure-3}' has its vertex and edge sets equals to:

    \begin{align*}
        V_{M}=&\{v_{1}=\{1,A\},v_{2}=\{2,A\},v_{3}=\{3,A\},v_{4}=\{5,A\},v_{5}=\{1,B\},v_{6}=\{2,B\},\\
        &v_{7}=\{3,B\},v_{8}=\{4,B\},v_{9}=\{5,B\},v_{10}=\{1,C\},v_{11}=\{2,C\},v_{12}=\{4,C\},\\
        &v_{13}=\{5,C\},v_{14}=\{2,D\},v_{15}=\{3,D\},v_{16}=\{4,D\},v_{17}=\{5,D\}\}
    \end{align*}
    \begin{align*}
        E_{M}=&\{\{v_{1},v_{2}\}, \{v_{2},v_{3}\}, \{v_{3},v_{4}\}, \{v_{1},v_{5}\}, \{v_{7},v_{8}\}, \{v_{9},v_{10}\}, \{v_{10},v_{11}\}, \{v_{12},v_{13}\}, \\ &\{v_{11},v_{14}\}, \{v_{12},v_{16}\}, \{v_{13},v_{17}\}, \{v_{14},v_{15}\}, \{v_{15},v_{16}\}\}
    \end{align*}

    \noindent With that said, it also has the following characteristics:

    \begin{enumerate}[a)]
        \item Intra-layer edge sets:
        \begin{align*}
            E_{A,A}&=\{\{v_{1},v_{2}\}, \{v_{2},v_{3}\}, \{v_{3},v_{4}\}\} \\
            E_{A,B}&=\{\{v_{7},v_{8}\}\} \\
            E_{A,C}&=\{\{v_{10},v_{11}\},\{v_{12},v_{13}\}\} \\
            E_{A,D}&=\{\{v_{14},v_{15}\},\{v_{15},v_{16}\}\} \\ \\
            E_{A}&= E_{A,A} \cup E_{A,B} \cup E_{A,C} \cup E_{A,D} \\
            &= \{\{v_{1},v_{2}\}, \{v_{2},v_{3}\}, \{v_{3},v_{4}\},\{v_{7},v_{8}\},\\ &\quad\quad\{v_{10},v_{11}\},\{v_{12},v_{13}\},\{v_{14},v_{15}\},\{v_{15},v_{16}\}\}
        \end{align*}

        \par\noindent Inter-layer edge sets:
        \begin{align*}
            E_{C,A,B}&=\{\{v_{1},v_{5}\}\} \\
            E_{C,B,C}&=\{\{v_{9},v_{10}\}\} \\
            E_{C,C,D}&=\{\{v_{11},v_{14}\}, \{v_{12},v_{16}\},\{v_{13},v_{17}\}\} \\ \\
            E_{C}&=E_{C,A,B} \cup E_{C,B,C} \cup E_{C,C,D} = E_{M} \setminus E_{A} \\
            &= \{\{v_{1},v_{5}\},\{v_{9},v_{10}\},\{v_{11},v_{14}\}, \{v_{12},v_{16}\}, \{v_{13},v_{17}\}\}
        \end{align*}

        \par\noindent Coupling edge set:
        \begin{align*}
            E_{\widetilde{C},A,B}&=\{\{v_{1},v_{5}\}\} \\
            E_{\widetilde{C},C,D}&=\{\{v_{11},v_{14}\}, \{v_{12},v_{16}\}, \{v_{13},v_{17}\}\} \\ \\
            E_{\widetilde{C}}&=E_{\widetilde{C},A,B} \cup E_{\widetilde{C},C,D} \\ &=\{\{v_{1},v_{5}\},\{v_{11},v_{14}\}, \{v_{12},v_{16}\}, \{v_{13},v_{17}\}\}
        \end{align*}

        \item The provided network is not \textit{fully interconnected} since not all the layers contain all the nodes.

        \item The tensor representation of the network is:
        \begin{align*}
            A_{::,A,A} &=
            \begin{bmatrix}
            0 & 1 & 0 & 0 & 0 \\
            1 & 0 & 1 & 0 & 0 \\
            0 & 1 & 0 & 0 & 1 \\
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 1 & 0 & 0 \\
            \end{bmatrix}\\
            A_{::,B,B} &=
            \begin{bmatrix}
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 1 & 0 \\
            0 & 0 & 1 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            \end{bmatrix}\\
            A_{::,C,C} &=
            \begin{bmatrix}
            0 & 1 & 0 & 0 & 0 \\
            1 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 1 \\
            0 & 0 & 0 & 1 & 0 \\
            \end{bmatrix}\\
            A_{::,D,D} &=
            \begin{bmatrix}
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 1 & 0 & 0 \\
            0 & 1 & 0 & 1 & 0 \\
            0 & 0 & 1 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            \end{bmatrix}\\
            A_{::,A,B} = A_{::,B,A} &=
            \begin{bmatrix}
            1 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            \end{bmatrix}\\
            A_{::,B,C} = A_{::,C,B} &=
            \begin{bmatrix}
            0 & 0 & 0 & 0 & 1 \\
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            1 & 0 & 0 & 0 & 0 \\
            \end{bmatrix}\\
            A_{::,C,D} = A_{::,D,C} &=
            \begin{bmatrix}
            0 & 0 & 0 & 0 & 0 \\
            0 & 1 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 1 & 0 \\
            0 & 0 & 0 & 0 & 1 \\
            \end{bmatrix}
        \end{align*}

        \noindent Just to clarify, the rest of the tensors combinations has not been reported for brevity, since they all are equals to the null matrix.

    \end{enumerate}

\section{Exercise 3}
    \begin{figure}[H]
        \centering
        \includegraphics[width=0.6\textwidth]{4.png}
        \caption{Multi-layer graph}
        \label{fig:figure-4}
    \end{figure}
    \begin{enumerate}[a)]
    \item The multi-layer graph pictured in the 'Figure \ref{fig:figure-4}' has the following tensor representation:
    \begin{align*}
            A_{::,A,A} &=
            \begin{bmatrix}
            0 & 1 & 0 & 0 \\
            1 & 0 & 1 & 0 \\
            0 & 1 & 0 & 1 \\
            0 & 0 & 1 & 0 \\
            \end{bmatrix}\\
            A_{::,B,B} &=
            \begin{bmatrix}
            0 & 1 & 0 & 0 \\
            1 & 0 & 1 & 0 \\
            0 & 1 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            \end{bmatrix}\\
            A_{::,C,C} &=
            \begin{bmatrix}
            0 & 0 & 0 & 0 \\
            0 & 0 & 1 & 0 \\
            0 & 1 & 0 & 1 \\
            0 & 0 & 1 & 0 \\
            \end{bmatrix}\\
            A_{::,D,D} &=
            \begin{bmatrix}
            0 & 1 & 0 & 0 \\
            1 & 0 & 0 & 0 \\
            0 & 0 & 0 & 1 \\
            0 & 0 & 1 & 0 \\
            \end{bmatrix}\\
            A_{::,A,B} = A_{::,B,A} &=
            \begin{bmatrix}
            1 & 0 & 0 & 0 \\
            0 & 0 & 1 & 0 \\
            0 & 1 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            \end{bmatrix}\\
            A_{::,B,C} = A_{::,B,C}&=
            \begin{bmatrix}
            0 & 0 & 0 & 0 \\
            0 & 0 & 1 & 0 \\
            0 & 1 & 0 & 0 \\
            0 & 0 & 0 & 1 \\
            \end{bmatrix}\\
            A_{::,C,D} = A_{::,D,C} &=
            \begin{bmatrix}
            1 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            \end{bmatrix}\\
    \end{align*}

    \noindent As before, the rest of the tensors combinations has not been reported for brevity, since they all are equals to the null matrix.

    \item The degree of each node are:
    \begin{align*}
        d_{1} &= (1,1,0,1) \\
        d_{2} &= (2,2,1,1) \\
        d_{3} &= (2,1,2,1) \\
        d_{4} &= (1,0,1,1) \\
    \end{align*}

    \par\noindent Meanwhile the overlapping degree are:
    \begin{align*}
        o_{1} &= 3 \\
        o_{2} &= 6 \\
        o_{3} &= 6 \\
        o_{4} &= 3 \\
    \end{align*}

    \par\noindent Finally by applying the uniform-vector-like eigenvector centrality we obtain:

    \begin{align*}
    \Tilde{A} = A_{::,A,A}^{T} + A_{::,B,B}^{T} + A_{::,C,C}^{T} + A_{::,D,D}^{T} =
        \begin{bmatrix}
            0 & 3 & 0 & 0 \\
            3 & 0 & 3 & 0 \\
            0 & 3 & 0 & 3 \\
            0 & 0 & 3 & 0 \\
        \end{bmatrix}\\
    \end{align*}

    \noindent In which every column, and also every row since it's a symmetric matrix, identifies the eigenvector of the relative node (e.g. first row = first column = eigenvector of \(v_{1}\)).

    \end{enumerate}

\end{document}


% Created 2019-09-05 jue 11:37
\documentclass[letterpaper,fleqn]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage{khpreamble}
\usepackage{tabularx}
\usepackage{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\geometry{top=20mm, bottom=20mm, left=24mm, right=18mm}
\author{Kjartan Halvorsen}
\date{}
\title{Pole-placement exercise}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs 25.3.50.2 (Org mode 8.2.10)}}
\begin{document}

\maketitle

\section*{Plot the poles}
\label{sec-1}
\begin{center}
\textbf{s-plane} \hspace*{0.4\linewidth} \textbf{z-plane}\\
\includegraphics[height=0.34\textheight]{../../figures/sgrid-crop} \hspace*{3mm}
\includegraphics[height=0.34\textheight]{../../figures/zgrid-crop}\\
\end{center}

Plot the poles of the following closed-loop discrete-time systems (as crosses in the z-plane). Plot also the corresponding continous-time poles (in the s-plane) using the sampling period \(h=0.2\). Rank the systems from 1 to 5 according to how desirable the performance of each system is.
\begin{description}
\item[{a}] \( G_c(z) = \frac{0.026z + 0.024}{z(z-0.95)}\)
\item[{b}] \( G_c(z) = \frac{0.13z + 0.12}{z^2 - z + 0.25} \)
\item[{c}] \(G_c(z) = \frac{0.54z + 0.52}{(z-0.5)^2 + 0.81}\)
\item[{d}] \(G_c(z) = \frac{0.025z + 0.025}{(z-0.8)^2 - 0.09}\)
\item[{e}] \(G_c(z) = \frac{0.068z + 0.062}{(z-0.8)^2 + 0.09}\)
\end{description}

\newpage

\section*{Pole placement and step response}
\label{sec-2}
Pair each of the discrete-time systems in the previous exercise with the correct step response below.
\begin{center}
\includegraphics[width=\linewidth]{../../figures/closed-loop-step-responsen}
\end{center}
% Emacs 25.3.50.2 (Org mode 8.2.10)
\end{document}


\documentclass[11pt]{article}
\usepackage[
top    = 2.50cm,% presumably you don't want it to be 0pt as well?
bottom = 2.50cm,
left   = 2cm,
right  = 2cm,
marginparsep = 0pt,
marginparwidth=0pt,
]{geometry}

\usepackage{amssymb}
\usepackage{fancyhdr}
\usepackage[most]{tcolorbox}
\usepackage{siunitx}
\usetikzlibrary{calc,patterns,angles,quotes}
\usepackage{amsmath}
\usepackage{tikz, tikz-3dplot}
\usepackage{caption}
\usepackage{pgfplots}
\usepackage{multicol}
\pagestyle{fancy}
\fancyhead[l]{Waves - Abridged edition}
\fancyhead[r]{Giorgio Grigolo {\textcopyright}}
\newcommand\textcenter[1]{
	\begin{center}
		#1
	\end{center}
}
\begin{document}
	\section{Simmple Harmonic Motion: }
	\subsection{Definition: }
\begin{center}
		The type of oscillatory motion in which the acceleration of the oscillating body is proportinal  to the body's displacement from the equilibrium position and always acts towards the equilibrium position.
\end{center}
	\subsection{Graph: }
	\begin{center}
		{\begin{tikzpicture}[scale=1]
				\begin{axis}[
					x=1cm,y=1cm,
					axis lines=middle,
					ymajorgrids=true,
					xmajorgrids=true,
					xmin=0,
					xmax=12,
					ymin=-3,
					x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
					y label style={at={(axis description cs:-0.1,0.5)},rotate=90,anchor=south},
					xlabel=$t$ time,
					ylabel=$s$ displacement,
					ymax=3,
					xtick={0,1,...,12},
					ytick={-3,-2,...,3},]
					\addplot[color=red, domain=0:12,samples=1000]{2*sin(90*(x+1))};
				\end{axis}
			\end{tikzpicture}
			\captionof*{figure}{}}
	\end{center}
	\subsection{Diagram: }
	\begin{center}
		\begin{tikzpicture}[scale=1.6]
			% save length of g-vector and theta to macros
			\pgfmathsetmacro{\Gvec}{1.5}
			\pgfmathsetmacro{\myAngle}{45}
			% calculate lengths of vector components
			\pgfmathsetmacro{\Gcos}{\Gvec*cos(\myAngle)}
			\pgfmathsetmacro{\Gsin}{\Gvec*sin(\myAngle)}

			\coordinate (centro) at (0,0);
			\draw[dashed,gray,-] (centro) -- ++ (0,-3.5) node (mary) [black,below]{$ $};
			\draw[thick] (centro) -- ++(270+\myAngle:3) coordinate (bob);
			\pic [draw, ->, "$\theta$", angle eccentricity=1.5] {angle = mary--centro--bob};
			\draw [blue,-stealth] (bob) -- ($(bob)!\Gcos cm!(centro)$);
			\draw [-stealth] (bob) -- ($(bob)!-\Gcos cm!(centro)$)
			coordinate (gcos)
			node[midway,above right] {$mg\cos\theta$};
			\draw [-stealth] (bob) -- ($(bob)!\Gsin cm!90:(centro)$)
			coordinate (gsin)
			node[midway,above left] {$mg\sin\theta$};
			\draw [-stealth] (bob) -- ++(0,-\Gvec)
			coordinate (g)
			node[near end,left] {$mg$};
			\pic [draw, ->, "$\theta$", angle eccentricity=1.5] {angle = g--bob--gcos};
			\filldraw [fill=black!40,draw=black] (bob) circle[radius=0.1];
		\end{tikzpicture}
	\end{center}
	\subsection{Equation: }
	\begin{equation}
		a = -\frac{4\pi^2}{T^2} \times x\tag{\si{\meter\per\second\squared}}
	\end{equation}

\begin{center}
	Where $T$ is the \textbf{periodic time} (\si{\second}).
\end{center}

	\section{Phase difference: }
\subsection{\textcolor{blue}{$\frac{T}{4}$}, \textcolor{green}{$\frac{T}{2}$} phase difference: }
	\begin{center}
		\begin{tikzpicture}[scale=1.2]
				\begin{axis}[
					x=1cm,y=1cm,
					axis lines=middle,
					ymajorgrids=true,
					xmajorgrids=true,
					xmin=0,
					xmax=12,
					ymin=-3,
					x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
					y label style={at={(axis description cs:-0.1,0.5)},rotate=90,anchor=south},
					xlabel=$t$ time,
					ylabel=$s$ displacement,
					ymax=3,
					xtick={0,1,...,12},
					ytick={-3,-2,...,3},]
					\addplot[color=red, domain=0:12,samples=1000]{2*sin(80*(x+1))};
					\addplot[color=blue, domain=0:12,samples=1000]{2*sin(80*(x))};
					\addplot[color=green, domain=0:12,samples=1000]{2*sin(80*(x-1))};
				\end{axis}
			\end{tikzpicture}
	\end{center}

\subsection{Full representation of SHM: }
\begin{center}
\begin{tikzpicture}[scale=1.2]
		\begin{axis}[
			x=1cm,y=1cm,
			axis lines=middle,
			ymajorgrids=true,
			xmajorgrids=true,
			xmin=0,
			xmax=12,
			ymin=-3,
			x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
			y label style={at={(axis description cs:-0.1,0.5)},rotate=90,anchor=south},
			xlabel=$t$ time,
			ymax=3,
			xtick={0,1,...,12},
			ytick={-3,-2,...,3},]
			\addplot[color=red, domain=0:12,samples=1000]{2*sin(45*(x))};
			\addplot[color=blue, domain=0:12,samples=1000]{2*sin(45*(-x))};
			\addplot[color=green, domain=0:12,samples=1000]{2*sin(45*(x+2};
		\end{axis}
	\end{tikzpicture}
	\begin{tikzpicture}[
		blacknode/.style={shape=circle, draw=black, line width=2},
		bluenode/.style={shape=circle, draw=blue, line width=2},
		greennode/.style={shape=circle, draw=green, line width=2},
		rednode/.style={shape=circle, draw=red, line width=2}
		]
		\matrix [draw,below left] at (0,0) {
			\node [greennode,label=right:Velocity] {}; \\
			\node [bluenode,label=right:Acceleration] {}; \\
			\node [rednode,label=right:Displacement] {}; \\
		};
	\end{tikzpicture}
\end{center}

\section{$n$\textsuperscript{th} harmonic}

\begin{equation}
	f_n = \frac{n }{2l}\sqrt{\frac{T}{\mu}}
\end{equation}

\begin{center}
	Where $l$ is the \textbf{length} (\si\meter) of the given string, $T$ the \textbf{tension} (\si\newton) present through it and $\mu$ its \textbf{linear mass density} (\si{\kilogram\per\meter}).
\end{center}
\section{Young's Double Slit Experiment}

\begin{center}
	 \begin{tikzpicture}[scale=1.5,every node/.append style={transform shape}]
	 	\foreach \x in {-0.5,-0.25,0} {
	 		\draw (\x,-1) -- (\x,1);
	 	}
	 	\foreach \x in {-0.375,-0.125,-0.125} {
	 		\draw[dashed] (\x,-1) -- (\x,1);
	 	}
	 	\draw[fill=black!10] (0.5,-2,-1) -- (0.5,-2,1) -- (0.5,2,1) -- (0.5,2,-1) -- (0.5,-2,-1);
	 	\fill (0.5,0,0) circle (0.05);
	 	\foreach \r in {0.25,0.5,...,1.75} {
	 		\draw (0.5,0) ++(-60:\r) arc (-60:60:\r);
	 	}
	 	\foreach \r in {0.125,0.375,...,1.875} {
	 		\draw[dashed] (0.5,0) ++(-60:\r) arc (-60:60:\r);
	 	}
	 	\draw[fill=black!10] (2,-2,-1) -- (2,-2,1) -- (2,2,1) -- (2,2,-1) -- (2,-2,-1);
	 	\fill (2,0.5) circle (0.05) (2,-0.5) circle (0.05);
	 	\foreach \r in {0.25,0.5,...,2} {
	 		\draw (2,0.5) ++(-60:\r) arc (-60:60:\r);
	 		\draw (2,-0.5) ++(-60:\r) arc (-60:60:\r);
	 	}
	 	\foreach \r in {0.125,0.375,...,2.125} {
	 		\draw[dashed] (2,0.5) ++(-60:\r) arc (-60:60:\r);
	 		\draw[dashed] (2,-0.5) ++(-60:\r) arc (-60:60:\r);
	 	}
	 	\draw (2,0.5,0.625) -- (2,0.5,0.5);
	 	\draw (2,-0.5,0.625) -- (2,-0.5,0.5);
	 	\draw (2,0.5,0.5) -- (2,-0.5,0.5);
	 	\draw[densely dotted] (2,0.5,0.5) -- (2,0.5,0);
	 	\draw[densely dotted] (2,-0.5,0.5) -- (2,-0.5,0);
	 	\draw[fill=black!10] (4,-2,-1) -- (4,-2,1) -- (4,2,1) -- (4,2,-1) -- (4,-2,-1);
	 	%       LABELLING
	 	\begin{scope}[canvas is yz plane at x=2,rotate=-90]
	 		\node[above] at (0,0.5) {A};
	 		\node[below] at (0,-0.5) {B};
	 		\node[left] at (-0.5,0) {d};
	 	\end{scope}
	 	\begin{scope}[canvas is yz plane at x=0.5,rotate=-90]
	 		\node[below left=-0.1cm] at (0,0) {S${}_0$};
	 	\end{scope}
	 	\begin{scope}[xshift=4cm,yshift=2cm,rotate=-90,canvas is xy plane at z=0]
	 		\fill[white] (0,0) rectangle (4,1);
	 		\begin{axis}[
	 			width=5.575cm,
	 			xmin=-0.62,
	 			xmax=0.62,
	 			ymin=0,
	 			ticks=none
	 			]
	 			\addplot [samples=500,black,smooth
	 			]
	 			{(cos(deg(5*pi*sin(deg(x)))))^(2)*((sin(deg(4*pi*sin(deg(x)))))/(4*pi*sin(deg(x))))^(2)};
	 			\draw[thin, densely dashed,blue] (axis cs:0.1593,0.1327) -- (axis cs:0.1593,0);
	 			\draw[thin, densely dashed,blue] (axis cs:-0.1593,0.1327) -- (axis cs:-0.1593,0);
	 			\draw[thin, densely dashed,blue] (axis cs:0.3941,0.03938) -- (axis cs:0.3941,0);
	 			\draw[thin, densely dashed,blue] (axis cs:-0.3941,0.03938) -- (axis cs:-0.3941,0);
	 		\end{axis}
	 	\end{scope}
	 	\draw[thin,densely dashed,blue] (2,0) -- (6.9,0);
	 	\begin{scope}
	 		\clip (2,-2) rectangle (4,2);
	 		\draw[thin,densely dashed,blue] (2,0) --    +(15:4);
	 		\draw[thin,densely dashed,blue] (2,0) --    +(-15:4);
	 		\draw[thin,densely dashed,blue] (2,0) --    +(32:4);
	 		\draw[thin,densely dashed,blue] (2,0) --    +(-32:4);
	 		\draw[thin,densely dashed,red] (2,0) --     +(8:4);
	 		\draw[thin,densely dashed,red] (2,0) --     +(-8:4);
	 		\draw[thin,densely dashed,red] (2,0) --     +(24:4);
	 		\draw[thin,densely dashed,red] (2,0) --     +(-24:4);
	 	\end{scope}
	 \end{tikzpicture}\\
The observable pattern achieved in this experiment can be seen above. It consists of alternate dark and bright bands. The centreal band, the one equidistant from both slits is always bright ($AO - BO = 0\lambda$)
\begin{multicols}{2}
	destructive interference $\implies$ dark\\
	constructive interference $\implies$ bright
\end{multicols}
\end{center}
\subsection{Path difference}
\begin{multicols}{2}
	\begin{center}
		\textbf{\underline{Bright band}}\\
		~\\
		$n\lambda,\, n \in \mathbb{N}$
	\end{center}
	\begin{center}
	\textbf{\underline{Dark band}}\\
	~\\
	$n\frac12\lambda,\, n \in \mathbb{N}$
\end{center}
\end{multicols}
\subsection{Bright slit interval distance: }
\begin{equation}
	y=\frac{D\lambda}{d}\tag{\si{\meter}}
\end{equation}
\begin{center}
	Where $D$ is the \textbf{distance} (\si{\meter}) between the slits and the screen, $d$ is the \textbf{seperation} ({\si{\meter}}) between the two slits and $\lambda$ the \textbf{wavelength} (\si{\meter}) of the wave at the source.
\end{center}
\end{document}


    \foldertitle{modellang}{Model File Language}{modellang/Contents}

	Model file language is used to write model files. The model files are
plain text files (saved under any filename with any extension) that
describes the model: its equations, variables, parameters, etc. The
model file, on the other hand, does not describe what to do with the
model. To run the tasks you want to perform with the model, you need
first to load the model file into Matlab using the
\href{model/model}{\texttt{model}} function. This function creates a
model object. Then you write your own m-files using Matlab and IRIS
functions to perform the desired tasks with the model object.

Why do all the keywords (except pseudofunctions) start with an
exclamation point? Why do the comments have the same style as in Matlab?
Why do substitutions and steady-state references use the dollar sign?
Because this way, you can get the model files syntax-highlighted in the
Matlab editor. Syntax highlighting improves enormously the readability
of the files, and helps understand the model more quickly. See
\href{setup/Contents}{the setup instructions} for more details.

\paragraph{Variables, parameters, substitutions and
functions}\label{variables-parameters-substitutions-and-functions}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{modellang/transitionvariables}{\texttt{!transition\_variables}}
  - List of transition variables.
\item
  \href{modellang/transitionshocks}{\texttt{!transition\_shocks}} - List
  of transition shocks.
\item
  \href{modellang/measurementvariables}{\texttt{!measurement\_variables}}
  - List of measurement variables.
\item
  \href{modellang/measurementshocks}{\texttt{!measurement\_shocks}} -
  List of measurement shocks.
\item
  \href{modellang/exogenousvariables}{\texttt{!exogenous\_variables}} -
  List of exogenous variables.
\item
  \href{modellang/parameters}{\texttt{!parameters}} - List of
  parameters.
\item
  \href{modellang/autoexogenise}{\texttt{!autoexogenise}} - Definition
  of variable/shock pairs for use in autoexogenised simulation plans.
\end{itemize}

\paragraph{Equations}\label{equations}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{modellang/transitionequations}{\texttt{!transition\_equations}}
  - Block of transition equations.
\item
  \href{modellang/measurementequations}{\texttt{!measurement\_equations}}
  - Block of measurement equations.
\item
  \href{modellang/dtrends}{\texttt{!dtrends}} - Block of deterministic
  trend equations.
\item
  \href{modellang/links}{\texttt{!links}} - Define dynamic links.
\end{itemize}

\paragraph{Linearised and log-linearised
variables}\label{linearised-and-log-linearised-variables}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{modellang/logvariables}{\texttt{!log\_variables}} - List of
  log-linearised variables.
\item
  \href{modellang/allbut}{\texttt{!all\_but}} - Inverse list of
  log-linearised variables.
\item
  \href{modellang/regexpression}{\texttt{\textless{}...\textgreater{}}}
  - Regular expression in log-variable list.
\end{itemize}

\paragraph{Special operators}\label{special-operators}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{modellang/sstateversion}{\texttt{!!}} - Steady-state version of
  an equation.
\item
  \href{modellang/ttrend}{\texttt{!ttrend}} - Linear time trend in
  deterministic trend equations.
\item
  \href{modellang/laglead}{\texttt{\{...\}}} - Lag or lead.
\item
  \href{modellang/sstateref}{\texttt{\&}} - Reference to the
  steady-state level of a variable.
\item
  \href{modellang/exactnonlin}{\texttt{=\#}} - Mark an equation for
  exact non-linear simulation.
\item
  \href{modellang/alias}{\texttt{'...!!...'}} - Beginning of aliasing
  inside descriptions and labels.
\end{itemize}

\paragraph{Pseudofunctions}\label{pseudofunctions}

Pseudofunctions do not start with an exclamation point.

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{modellang/min}{\texttt{min}} - Define loss function for optimal
  policy.
\item
  \href{modellang/diff}{\texttt{diff}} - First difference
  pseudofunction.
\item
  \href{modellang/dot}{\texttt{dot}} - Gross rate of growth
  pseudofunction.
\item
  \href{modellang/difflog}{\texttt{difflog}} - First log-difference
  pseudofunction.
\item
  \href{modellang/movavg}{\texttt{movavg}} - Moving average
  pseudofunction.
\item
  \href{modellang/movprod}{\texttt{movprod}} - Moving product
  pseudofunction.
\item
  \href{modellang/movsum}{\texttt{movsum}} - Moving sum pseudofunction.
\end{itemize}

\paragraph{Preparser control commands}\label{preparser-control-commands}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \href{modellang/substitutions}{\texttt{!substitutions}} - Define text
  substitutions.
\item
  \href{modellang/pseudosubs}{\texttt{\${[}...{]}\$}} -
  Pseudosubstitutions.
\item
  \href{modellang/import}{\texttt{!import}} - Include the content of
  another model file.
\item
  \href{modellang/export}{\texttt{!export}} - Create a carry-around file
  to be saved on the disk.
\item
  \href{modellang/if}{\texttt{!if...!elseif...!else...!end}} - Choose
  block of code based on logical condition.
\item
  \href{modellang/switch}{\texttt{!switch...!case...!end}} - Switch
  among several cases based on expression.
\item
  \href{modellang/for}{\texttt{!for...!do...!end}} - For loop for
  automated creation of model code.
\item
  \href{modellang/linecomments}{\texttt{\%}} - Line comments.
\item
  \href{modellang/blockcomments}{\texttt{\%\{...\%\}}} - Block comments.
\end{itemize}

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

When getting help on model file language, type the names of the keywords
and commands without the exclamation point:

\begin{verbatim}
help modellang
help modellang/keyword
help modellang/command
help modellang/pseudofunction
\end{verbatim}

\paragraph{Matlab functions and user functions in model
files}\label{matlab-functions-and-user-functions-in-model-files}

You can use any of the built-in functions (Matlab functions, functions
within the Toolboxes you have on your computer, and so on). In addition,
you can also use your own functions (written as an m-file) as long as
the m-file is on the Matlab search path or in the current directory.

In your own m-file functions, you can also (optionally) supply the first
derivatives that will be used to compute Taylor expansions when the
model is being solved, and the second derivatives that will be used when
the function occurs in a loss function.

When asked for the derivatives, the function is called with two extra
input arguments on top of that function's regular input arguments. The
first extra input argument is a text string \texttt{'diff'} (indicating
the call to the function is supposed to return a derivative). The second
extra input argument is a number or a vector of two numbers; it
determines with respect to which input argument or arguments the first
derivative or the second derivative is requested.

For instance, your function takes three input arguments,
\texttt{myfunc(x,y,z)}. To be able to supply derivates avoiding thus
numerical differentiation, the function must be written so that the
following three calls

\begin{verbatim}
myfunc(x,y,z,'diff',1)
myfunc(x,y,z,'diff',2)
myfunc(x,y,z,'diff',3)
\end{verbatim}

return the first derivative wrt to the first, second, and third input
argument, respectively, while

\begin{verbatim}
myfunc(x,y,z,'diff',[1,2])
\end{verbatim}

returns the second derivative wrt to the first and second input
arguments. Note that second derivatives are only needed for functions
that occur in an equation defining optimal policy objective,
\href{modellang/min}{min}.

If any of these calls fail, the respective derivative will be simply
evaluated numerically.

\paragraph{Basic rules IRIS model
files}\label{basic-rules-iris-model-files}

\begin{itemize}
\item
  There can be four types of equations in IRIS models: transition
  equations which are simply the endogenous dynamic equations,
  measurement equations which link the model to observables,
  deterministic trend equations which can be added at the top of
  measurement equations, and dynamic links which can be used to link
  some parameters or steady-state values to each other.
\item
  There can be two types of variables and two types of shocks in IRIS
  models: transition variables and shocks, and measurement variables and
  shocks.
\item
  Each model must have at least one transition (aka endogenous) variable
  and one transition equation.
\item
  Each variable, shock, or parameter must be declared in the appropriate
  declaration section.
\item
  The declaration sections and equations sections can be written in any
  order.
\item
  You can have as many declaration sections or equations sections of the
  same kind as you wish in one model file; they all get combined
  together at the time the model is being loaded.
\item
  Transition variables can occur with lags and leads in transition
  equations. Transition variables cannot, though, have leads in
  measurement equations.
\item
  Measurement variables and the shocks cannot have any lags or leads.
\item
  Transition shocks cannot occur in measurement equations, and the
  measurement shocks cannot occur in transition equations.
\item
  Exogenous variables can only occur in dtrends (deterministic trend
  equations), and must be always supplied in the input database to
  commands like \texttt{model/simulate}, \texttt{model/jforecast},
  \texttt{model/filter}, \texttt{model/estimate}, etc. They are not
  returned in the output databases.
\item
  You can choose between linearisation and log-linearisation for each
  individual transition and measurement variable. Shocks are always
  linearised. Exogenous variables must be always introduced so that
  their effect on the respective measurement variable is linear.
\end{itemize}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\include{common_symbols_and_format}
\renewcommand{\cfttoctitlefont}{\Large\bfseries}
\begin{document}

\logo

\rulename{Differential Moving Average Momentum}
\tblofcontents
\ruledescription{This trading rules takes the slope, or derivative or momentum of two different price moving averages and subtracts one slope from the other to determine position size. The parameters accepted are the momentum length, the look back length of the short moving average and the look back length of the long moving average.}

\ruleparameters
{Short price average length}{20}{Number of days in the short price
average.}{$\averagelengthshort^{\price}$}
{Long price average length}{100}{Number of additional days in the longer price average (added to the number in the short price average).}{$\averagelengthlong^{\price}$}
{Moving average momentum length}{5}{Number of days in the moving
average slope calculation.}{$M^{\price}$}
\stoptable

\section{Equation}

\begin{equation}
\bigcontribution(\averagelengthshort, \price) = \frac{1}{\averagelengthshort} \sum_{\dummyiterator=0}^{\averagelengthshort} \price_{n}
\end{equation}

\begin{equation}
\bigcontribution(\averagelengthlong, \price) = \frac{1}{\averagelengthlong} \sum_{\dummyiterator=0}^{\averagelengthlong} \price_{n}
\end{equation}

\begin{equation}
\bigcontribution(\bigcontribution(\averagelengthshort, \price), M^{\price}, \currenttime) = \frac
{(\bigcontribution(\averagelengthshort, \price)(t) -
(\bigcontribution(\averagelengthshort, \price)(\currenttime - M^{\price}))}
{M^{\price}}
\end{equation}

\begin{equation}
\bigcontribution(\bigcontribution(\averagelengthlong, \price), M^{\price}, \currenttime) = \frac
{(\bigcontribution(\averagelengthlong, \price)(t) -
(\bigcontribution(\averagelengthlong, \price)(\currenttime - M^{\price}))}
{M^{\price}}
\end{equation}

\begin{equation}
\position(\currenttime) =
\bigcontribution(\bigcontribution(\averagelengthshort, \price), M^{\price}, \currenttime) - \bigcontribution(\bigcontribution(\averagelengthlong, \price), M^{\price}, \currenttime)
\end{equation}

\hspace{200mm}

\noindent where $\position_\currenttime$ is the portfolio allocation at time $\currenttime$ and $\price = \price(\currenttime)$ is the value of the price series.

\hspace{200mm}
\hspace{200mm}

\keyterms

\furtherlinks

\end{document}


\documentclass{article}

\usepackage{mathtools}
\usepackage[margin=0.5in]{geometry}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{float}
\usepackage{multirow}

\setlength{\parindent}{0pt}
\graphicspath{ {./experiment/data/} }

\title{Edge Detection Evaluation of HEp-2 Cells in Fluorescent Images}
\author{Ossama Edbali - ID: 1466610}

\begin{document}

	\maketitle

	\section{Aim}

	This study aims to analyse the results and efficacy of several edge detection algorithms
	on HEp-2 cells.

	The edge detection/segmentation algorithms used are: Otsu, Sobel, Roberts, Canny,
	Canny using anisotropic diffusion,
	Laplacian of Gaussian, Difference of Gaussians and dilate-erode method.

	The evaluation of each method is carried by ROC curves, sensitivity-specificity variation graphs and correspondence analysis.

	\textbf{All the Matlab code and full data can be found here: \url{http://uobcs.github.io/hep2-edge-detection/}.}

	\section{Method}

	First of all the experiments where conducted using three main scripts (one for each
	image):
	\verb|x9343AM_task|, \verb|x10905JL_task| and \verb|x43590AM_task|.

	Each script loads the relative image and true edge image. Then produces a greyscale image using the \texttt{rgb2gray} Matlab function.
	Finally, it performs all the edge detection algorithms described above (delegating
		to the class \texttt{EdgeDetection.m}).
	A pre-analysis (qualitative) of the input has been carried out using the
	background approximation image as a surface to see where illumination varies (using \verb|show_background| function).
	It can be seen that the various input images are the outcome of different fluorescence patterns on	the HEp-2 cells.

	For all edge detectors there is a method inside the \texttt{EdgeDetection} class
	as well as methods for generating the ROC space and specificity and sensitivity
	in function of some parameters (e.g. threshold, sigma, number of iterations).

	For Roberts and Sobel a function \verb|detect_edges(img, filterX, filterY)| was developed which convolves \verb|img| with the two filters and uses
	\verb|magnitude| to produce the final result (before thresholding).

	In regards to LoG, Gaussian smoothing was performed using \verb|gaussian_smoothing(image, sigma)|. Then convolve the smoothed image with the laplacian
	operator and finally find the zero crossings using \verb|edge(res, 'zerocross')|.

	\subsection*{Otsu's method}
	One of the algorithms used in this study is Otsu's algorithm.
	The aim is to find the threshold value where the sum of foreground and background spreads is at its minimum. First of all I calculate the weighted mean and variance
	for both the background and foreground ($\mu_b$, $\sigma_b^2$ and
	$\mu_f$, $\sigma_f^2$). Then I compute the \textbf{within-class variance} (two variances multiplied by their associated weights): $\sigma_W^2 = W_b \sigma_b^2+W_f \sigma_f^2$. I do the same calculation for candidate thresholds and the one that has the lowest within-class variance will be chosen. This algorithm is implemented in Matlab's
	\verb|graythresh|. This will produce a binary image which edges' can be detected
	easily by any of the edge detectors (Sobel in the experiment).

	\subsection*{Dilate-erode method}

	The dilate-erode method is a 4-step algorithm that uses a combination of  morphological operators before using Sobel edge detector. The reason why this method
	has been used is that when Sobel (or Roberts) is performed on the greyscale image, there are some lines of high contrast surrounding
	the actual edge (like noise). The gaps between these small lines and the actual edge
	can be filled using a \textbf{dilate morphological operator} (or non-linear neighbourhood
	operator) with a structuring element (from the \verb|strel| function). Then
	\verb|imfill| has been used to fill the holes inside the cell - this step can
	be skipped but it will result in a lower performance.
	The next step is to "reduce" the cell to get it back to its state before
	dilation using
	the \textbf{erosion} operator. Finally the Sobel filter has been used to detect
	edges.
	There are various ways to create the  morphological operators. Here is the one
	used in this study (erode example using a 5x5 mask): \verb|erode = @(x) min(x(:)); out = nlfilter(in,[5 5],erode)|.

	\subsection*{Canny with anisotropic diffusion (CAD)}

	From the study it has been noted that Canny performed with sensitivity lower than
	0.4 in all input images. To enhance the images but still preserving true edges,
	\textbf{anisotropic diffusion} has been used.
	Anisotropic diffusion is a non-linear diffusion filtering for avoiding the
	blurring problems of Gaussian smoothing (and others). It is an iterative
	process to perform edge-preserving smoothing on the cells.
	The algorithm is described mathematically as follows:

	\begin{center}
	$\frac{\partial}{\partial t}I(x, t) = \nabla \bullet (c(x, t) \nabla I(x, t))$
	\end{center}

	Here $t$ is the iteration step, $c(x, t)$ is a decreasing diffusion function
	of the image gradient magnitude.

	The \verb|anisodiff2D(im, num_iter, delta_t, kappa, option)| function in the codebase
	has been implemented to accomplish anisotropic diffusion. Here \verb|im| is the
	input image, \verb|num_iter| is the number of iterations, \verb|delta_t| is
	the integration constant, \verb|kappa| is the diffusion constant and
	\verb|option| defines which diffusion function to use.
	\newline

	I adopted three evaluation methods in order to assess the accuracy/efficacy of each edge
	detector technique:

	\subsection*{ROC curve}

	The ROC curve has been used to detect which parameter to choose according
	to a \textbf{diagnosis line} (in the experiment
	(0, 1) to (1, 0) has been used). The implementation can be found under
	the \verb|compute_roc(ID, IT)| function, where ID is the image
	with detected edges and \verb|IT| is the image with true edges.
	Basically it takes the two binary images and performs logical operations
	for all possible cases (e.g. true positive, false positive etc\ldots) and
	calculates the sum of "on" pixels in the resulting images. Then it combines
	the results to find sensitivity and specificity:

	\begin{center}
	\verb|sensitivity = TP / (TP + FN)| and \verb|specificity = TN / (TN + FP);|
	\end{center}

	\subsection*{Sensitivity/Specificity variation}

	This evaluation method simply uses the sensitivity and specificity parameters in function
	of a variable (threshold, sigma, kappa or number of iterations). From this we
	can see how different algorithms respond to the variation of some parameters.
	This is implemented by the \verb|roc_params_comparison| static method in the
	\verb|EdgeDetection| class.

	\subsection*{Correspondence analysis (intra-method analysis)}

	From $N$ edge detection results (using the same edge detector) test for
	correspondence as follows: a pixel location identified as an edge by
	all $N$ detector configurations (i.e. changing parameter)
	will have the highest correspondence ($N$), and a
	location identified as an edge by only one detector setup will have the lowest.

	This is computed by the \verb|correspondence_analysis| function in the codebase which
	adds all the input image matrices and, using Matlab's \verb|bar|, plots a bar diagram
	with frequencies for each correspondence level (using \verb|histc|).

	\section{Results}

	A very interesting result is the comparison between Canny and Canny with anisotropic diffusion (using 9343 AM as a reference).

\begin{figure}[htp]

\centering
\includegraphics[width=.3\textwidth]{x9343AM/comparison.png}\hfill
\includegraphics[width=.3\textwidth]{x9343AM/canny/result.png}\hfill
\includegraphics[width=.3\textwidth]{x9343AM/cad/result_15_iterations.png}

\caption{(1) ROC space comparison, (2) Canny, (3) Canny with anisotropic diffusion }
\label{fig:figure3}

\end{figure}

	It can be noted that Canny does not perform very well (sensitivity under 0.4).
	However if we apply anisotropic diffusion,
	sensitivity drastically increases (over 0.8). This happens because the input image
	is very dark at the borders and Canny looses information in the smoothing
	step as well as in the non-maximum suppression step. Supporting this statement
	are the results of applying Canny to 43590 AM (which is very dark compared to
	the other two images and therefore a high smoothing).

	A summary of the whole data using the optimal parameters (\textbf{for the FULL evaluation graphs, tables and image results please visit this site: \url{http://uobcs.github.io/hep2-edge-detection/}}):

\begin{table}[H]
\centering
\caption{Overall results (1: 9343 AM, 2: 10905JL, 3: 43590AM)}
\label{my-label}
\begin{tabular}{|c|c|l|l|}
\hline
\multicolumn{1}{|l|}{Edge detector} & \multicolumn{1}{l|}{Image} & Sensitivity & Specificity \\ \hline
\multirow{3}{*}{Otsu}               & 1                          & 0.9225      & 0.9974      \\ \cline{2-4}
                                    & 2                          & 0.9279      & 0.9957      \\ \cline{2-4}
                                    & 3                          & 0.8813      & 0.9958      \\ \hline
\multirow{3}{*}{Sobel}              & 1                          & 0.8498      & 0.8638      \\ \cline{2-4}
                                    & 2                          & 0.8953      & 0.8852      \\ \cline{2-4}
                                    & 3                          & 0.8195      & 0.8237      \\ \hline
\multirow{3}{*}{Roberts}            & 1                          & 0.8477      & 0.8400      \\ \cline{2-4}
                                    & 2                          & 0.8646      & 0.8779      \\ \cline{2-4}
                                    & 3                          & 0.7540      & 0.8215      \\ \hline
\multirow{3}{*}{Canny}              & 1                          & 0.3404      & 0.9408      \\ \cline{2-4}
                                    & 2                          & 0.3057      & 0.9863      \\ \cline{2-4}
                                    & 3                          & 0.2396      & 0.9808      \\ \hline
\multirow{3}{*}{CAD}                & 1                          & 0.7910      & 0.86651     \\ \cline{2-4}
                                    & 2                          & 0.8139      & 0.9204      \\ \cline{2-4}
                                    & 3                          & 0.6734      & 0.8921      \\ \hline
\multirow{3}{*}{Dilate-Erode}       & 1                          & 0.8299      & 0.8058      \\ \cline{2-4}
                                    & 2                          & 0.8003      & 0.8119      \\ \cline{2-4}
                                    & 3                          & 0.7446      & 0.7863      \\ \hline
\multirow{3}{*}{LoG}                & 1                          & 0.2250      & 0.9419      \\ \cline{2-4}
                                    & 2                          & 0.2135      & 0.9324      \\ \cline{2-4}
                                    & 3                          & 0.1996      & 0.9207      \\ \hline
\multirow{3}{*}{DoG}                & 1                          & 0.2137      & 0.9857      \\ \cline{2-4}
                                    & 2                          & 0.2446      & 0.9941      \\ \cline{2-4}
                                    & 3                          & 0.17301     & 0.99094     \\ \hline
\end{tabular}
\end{table}

\begin{table}[H]
\centering
\caption{Otsu evaluation}
\label{otsu}
\begin{tabular}{@{}llll@{}}
\toprule
Image    & Sensitivity & Specificity & Threshold \\ \midrule
9343 AM  & 0.9225      & 0.9974      & 0.0902    \\
10905 JL & 0.9279      & 0.9957      & 0.1333    \\
43590 AM & 0.8813      & 0.9958      & 0.0510    \\ \bottomrule
\end{tabular}
\end{table}

Overall Otsu has proven to be the best choice in all the images. Although
computationally expensive, Otsu's method takes into account foreground and
background and gives satisfactory results only when the numbers of pixels in each class are close to each other (which is the case here).

Sobel and Roberts have performed almost equally in all images however Sobel has
higher specificity because it is less sensitive to isolated high intensity point variations. Thus it gives correct results when the edge do not exist in a given point.

In regards to the dilate-erode method
we can notice that the sensitivity decreases very fast compared to Sobel (or Roberts). This is because during the erode process some edges might be removed and this propagates to the Sobel edge detection and thresholding.

	\section{Conclusion}

	In this study various alternatives to the traditional edge detectors have been
	tried because of their specific application to cell edge detection. For example
	Canny is very sensitive to the fluorescence around the cells. LoG and DoG have
	low sensitivity (for optimal parameters) because they highlight regions
	of rapid intensity changes but the input images have gradual
	change from the cell to the background (fluorescence effect). Some extensions to
	the edge detectors (such as dilate-erode, adjust and anisotropic diffusion)
	have proven to increase the efficacy of the edge detection.

\end{document}


\documentclass{article}

\usepackage{tikz}
\usepackage{amsthm,amsmath,amssymb}
\usepackage{csquotes}
\usetikzlibrary{automata}
\usetikzlibrary{graphs}

\theoremstyle{definition}
\newtheorem{defn}{Definition}

\begin{document}

\section{Formal Verification of Flexibility in Swarm Robotics}

Microscopic v.s. macroscopic
approach to model checking
swarm robotics.

"The first approach consists of building a model by first creating a corresponding
finite state machine for each robot behavior and then by taking the composition of
all those finite state machines. The second approach consists of building a single
finite state machine containing a state for each different sub-behavior of the robots.
Each state is associated with a counter that keep track of the number of robot
currently in that state."

\section{Transition System Representing a Battle}

\begin{tikzpicture}
    \matrix[row sep=2cm, column sep=1cm]{
        \node (E) {$E$} ; &
        \node (K) {$K$} ; &
        \node (G) {$G$} ; \\
    } ;

    \graph[use existing nodes] {
         E -> [edge label={atk},loop above]
         E ->[edge label={atk}]
         K ->[edge label={atk},loop above]
         K ->[edge label={done}] G ;
    } ;
\end{tikzpicture}

At the beginning of each battle, some number
of robots are assigned to fit enemy $ i $,
which is represented in state E. In state E,
the action "attack" can be taken. This action
will probabilistically result in the defeat
of the enemy, or require additional attacks.

The probability of neutralizing an enemy
is given in the next section.

If the attack succeeds, then the battle transitions
to the "complete" phase (state K), upon which
all robots get redistributed to the other
battles that are being fought (concurrently)!
Furthermore, we must set the base level of the
freshly defeated enemy as 0.

Because we create a product transition system
in which the action "attack" is a handshake action,
we include a dummy "attack" action in state K
that goes to itself, in order to allow other
battles to continue.

Upon the completion of all battles, there is a
"done" action that can be taken (also a handshake action)
that allows all battles to reach
the "goal" state simultaneously, thus
concluding the engagement.

Future extensions: Attacks could probabilistically result
in casualties.

\section{The Role of Probability}

As discussed, the event of defeating some enemy
occurs probabilistically. We have heterogeneous
enemies, which we model by assigning them
"levels". Conceptually, enemies with higher
levels are more difficult to defeat than other
enemies (i.e. the probability of defeating an
enemy in an attack is lower).

However, conceptually it also makes sense that
a higher number of robots attacking the same
enemy would make it more likely that an enemy
is defeated in a round of combat.

Hence, we define the probability of \textbf{not defeating}
an enemy in a round of combat using the following
function of the level of the enemy
(the cumulative distribution function of the
exponential distribution).

$$ f(l_i) = 1 - e^{-\lambda_i l_i} $$

This is motivated by the idea of dynamically adjusting the
probability of not defeating an enemy as there
are more robots fighting it.

$ \mathcal{l}_i $ is the base level of an enemy.
Then, we say that $ \lambda_i = \frac{1}{cn_i + 1} $ is the adjustment
factor, where $ n_i $ is the number of robots
currently fighting enemy $ i $, and $ c $ is a
hyperparameter that describes robot neutralization efficiency.
1 must be added to avoid division by zero problems.

\section{Optimization}

Roughly stated, the ultimate goal is to find the optimal
portioning of robots to minimize the number of rounds of
combat in order to successively neutralize all targets.

\begin{displayquote}

    We have N robots.

    Let $ \mathcal{M} = \{m_1, m_2, \ldots, m_k \} $ be the set of all enemies.

    Further, let L be the bound in $ \mathcal{L} = \{ 1, \ldots, L \} $.

    Our goal is to find a function $ L^\mathcal{M} \to [0,1]^\mathcal{M} $
    such that the expected number of turns to neutralize all enemies
    is minimized.

\end{displayquote}

This function takes ordered tuples of enemies levels (the remaining battles),
and produces a probability distribution
over them that is used to inform the proportions by which the newly unallocated
robots are distributed among the remaining battles.

Because arbitrary functions cannot be described in PRISM, we have assumed
that the distribution of robots over the remaining battles will simply be the weighted distribution
by current adjusted level (what we previously called $ \lambda_i l_i $).

However, we would like to be able to tune the function in order
to optimize how robots are (re)distributed among battles.
For our initial experiment, we choose the function $ g(x) = x^a $,
where $ a $ is a parameter that will be tuned using
simulated annealing (by verifying desired properties repeatedly
using different values of $ a $). This function can be interchanged
with any other function in order to achieve the desired scaling amount
for redistribution.

Thus, we can now define our
function $ L^\mathcal{M} \to [0,1]^\mathcal{M} $ as follows:

$$ f(\langle l_1, \ldots, l_k \rangle) = \frac{g(\langle \lambda_1 l_1, \ldots, \lambda_k l_k \rangle)}{\sum_j^{g(\langle \lambda_j l_j \ldots \lambda_k l_k \rangle)}} i \in \{1, \ldots, M\} $$

We assume that the functions g and f operate element-wise on tuples.

We assume that all of our robots are homogeneous.
However, our enemies are assumed to be heterogeneous
(as defined using our level system).

\end{document}