\section{Checking Property Preservation}
\label{sec:lts-transformation:proppres}

In this section, we show how property preservation of transformations can be checked by generating networks from rule systems and comparing the LTSs of these networks.
Figure~\ref{fig:lts-transformation:check-generation} gives an overview of the approach.

\begin{figure}[hbt]
\centering
\includegraphics[scale=0.6]{lts-transformation/figs/check-generation}
\caption{Checking property preservation by comparing LTSs}
\label{fig:lts-transformation:check-generation}
\end{figure}

First, networks are generated based on the left and right-hand sides of transformation rules.
Then, the network LTSs corresponding to these networks are generated, while applying maximal hiding regarding the property at hand.
Finally, property preservation is checked by comparing the network LTSs generated from left-hand sides of transformation rules with the network LTSs of the corresponding right-hand sides.
If all pairs of LTSs are divergence-sensitive branching bisimilar, the rule system preserves the property at hand.

More formally, a terminating, confluent rule system $\Sigma$ is $\Sf$-preserving for a property~$\Sf \in \dsbrLmu$ iff $\models_{\smodel} \Sf \iff \models_{T_{\Sigma} (\smodel)} \Sf$ for all networks~$\smodel$.
Thus, if $\Sigma$ is $\Sf$-preserving and $\models_{\smodel} \Sf$, we can conclude that~$\models_{T_{\Sigma} (\smodel)} \Sf$ without rechecking property~$\Sf$ for network~$T_{\Sigma} (\smodel)$.
Since $\dsbrLmu$ is compatible with maximal hiding and DSBB, as discussed in Section~\ref{subsec:lts-transformation:lmu}, $\Sigma$ is $\Sf$-preserving if $\maxabstr(\smodel) \dsbbis \maxabstr(T_{\Sigma} (\smodel))$.
In this section, we discuss under which conditions a rule system implies the bisimilarity of $\maxabstr(\smodel)$ and $\maxabstr(T_{\Sigma} (\smodel))$.
The most important condition roughly boils down to checking whether, after some appropriate rewriting, the left and right patterns of the transformation rules are divergence-sensitive branching bisimilar after maximal hiding.
If this is the case, then applying the rules does not result in a network with structurally different behavior.

Without loss of generality, for each network~$\smodel = \networktuple$ and rule system~$\Sigma = \rulesystemtuple$, we assume that each $r \in R$ has exactly one match to some $\Pi[i]$ and that each $\Pi[i]$ is matched on by exactly one $r$.
This is expressed by indexing the~$r \in R$ such that rule~$r_i$ is matched on~$\Pi[i]$.
If~$R$ contains only one rule, we omit its index.
Since~$\Sigma$ is confluent, the results of this section can be lifted to the more general case where rules may have an arbitrary number of matches.
With this assumption, it can also safely be assumed that all the $\actions{i}$ are disjoint.
If this is not the case, some renaming of actions and a corresponding modification of the synchronization rules can resolve this.

\subsection{Extended Transformation Rules}
To show that a rule system~$\Sigma$ preserves a property~$\Sf$ for every network~\smodel, we show that a divergence-sensitive branching bisimulation between the states of~$\maxabstr(\smodel)$ and~$\maxabstr(T_{\Sigma} (\smodel))$ can be constructed based on the divergence-sensitive branching bisimulations between networks constructed from the transformation rules.
To construct these networks from the transformation rules, we extend the transformation rules with self-loops on the glue-states.
The matches of the glue-states of a given transformation rule may be part of transitions that are not present in the patterns of this rule.
The transformation rules are extended to make explicit that such transitions may exist.
Each self-loop is labeled with an action uniquely related to the corresponding state.

\begin{definition}
\label{def:lts-transformation:extendedtransformationrule}
Given an LTS transformation rule $r = \transruletuple{r}$, the corresponding extended transformation rule is defined as~$r_\kappa = \langle \Llts^r_\kappa, \Rlts^r_\kappa \rangle$, where
\begin{itemize}
\item
$\Llts^r_\kappa = \langle \states{\Llts^r}, \actions{\Llts^r} \cup \{ \kappa_s \mid s \in \states{\Llts^r} \cap \states{\Rlts^r} \}, \transitions{\Llts^r} \cup \{ \langle s, \kappa_s, s \rangle \mid s \in \states{\Llts^r} \cap \states{\Rlts^r} \}, \initialstates{\Llts^r} \rangle$;
\item
$\Rlts^r_\kappa = \langle \states{\Rlts^r}, \actions{\Rlts^r} \cup \{ \kappa_s \mid s \in \states{\Llts^r} \cap \states{\Rlts^r} \}, \transitions{\Rlts^r} \cup \{ \langle s, \kappa_s, s \rangle \mid s \in \states{\Llts^r} \cap \states{\Rlts^r} \}, \initialstates{\Rlts^r} \rangle$.
\end{itemize}
\end{definition}

\noindent
We assume that the $\kappa$-actions are not originally in $\actions{\Llts^r}$.
Without these loops, a DSBB check of patterns could consider two deadlock states to be bisimilar, while they are actually different glue-states that are possibly matched on states with different outgoing transitions not present in the patterns.
For example, without the dashed $\kappa$-loops, states~$\it{ii}$ and~$\it{iii}$ in Figure~\ref{fig:lts-transformation:kappa} would be related if~$a,b \in h_{\actions{}}(\Sf)$.
With the $\kappa$-loops, however, they are not related, as indicated by the cross.
Thus, the extra transitions ensure that $\Llts^r_\kappa \dsbbis \Rlts^r_\kappa$ iff there exists a divergence-sensitive branching bisimulation that relates all the glue-states to themselves.
A glue-state $s$ in $\Llts^r_\kappa$ (or $\Rlts^r_\kappa$) with self-loop $s\ \smash{\xrightarrow{\kappa_s}}\ s$ must at least be related to itself in $\Rlts^r_\kappa$ (or $\Llts^r_\kappa$) since it is the only state where a $\kappa_s$-transition is enabled.

\begin{figure}[hbt]
\centering
\includegraphics[scale=0.2]{lts-transformation/figs/kappa-loops}
\caption{An extended transformation rule}
\label{fig:lts-transformation:kappa}
\end{figure}

\subsection{Synchronizing Behavior}
If a rule system consists of multiple transformation rules, then multiple LTS transformations can be applied in a single transformation step.
To check $\Sf$-preservation of such rule systems, we need to take possible synchronization between different rule patterns into~account.

\begin{figure}[hbt]
\centering
\includegraphics[scale=0.2]{lts-transformation/figs/comparison-network}
\caption{A network and the corresponding network LTS}
\label{fig:lts-transformation:comparison-network}
\end{figure}

In Figure~\ref{fig:lts-transformation:comparison-network}, a network involving synchronization and the corresponding network LTS are shown.
The network LTS is obtained after hiding the actions in~$h$, shown in the middle of the figure, which is the hiding set for some property~$\Sf$.
The rule system shown on the left of Figure~\ref{fig:lts-transformation:preserving-rules} can be applied to the network of Figure~\ref{fig:lts-transformation:comparison-network}.
Individually, the rules seem to fundamentally change the behavior of the process LTSs, as shown in the middle of Figure~\ref{fig:lts-transformation:preserving-rules}.
However, since the rule system also adds the new synchronization rules~$\langle\langle c1, e1 \rangle, c1e1\rangle$ and~$\langle\langle c2, \bullet \rangle, c2\rangle$, and the actions~$\it{c1e1}$ and~$\it{c2}$ can be hidden,
the final network LTS, as shown on the right of the figure, is bisimilar to the one before transformation.
To incorporate such possible dependencies between rule patterns, we developed a $\Sf$-preservation check involving networks of rule patterns.

\begin{figure}[hbt]
\centering
\includegraphics[scale=0.2]{lts-transformation/figs/preserving-rules}
\caption{A rule system and an example of its application}
\label{fig:lts-transformation:preserving-rules}
\end{figure}

In general, when considering transformation rules that affect synchronizing actions and thus involve multiple process LTSs, it cannot be determined whether a given rule system~$\Sigma = \rulesystemtuple$ involving such rules is $\Sf$-preserving by just analyzing the $\Llts^\ri$ and $\Rlts^\ri$ of all $\ri \in R$.
However, this can be done if $\Sigma$ has a number of properties regarding synchronizing behavior of a network $\smodel$, which we together call synchronization uniformity.

Before we can give a definition of synchronization uniformity, we need a number of auxiliary definitions.
The set of actions involved in synchronization vector~$\vectornot{t}$ is ${\actions{}}(\vectornot{t}) = \{ a \mid \exists i \in 1..n. \vectornot{t}[i] = a \wedge a \neq \bullet \}$,
and the set of actions involved in synchronization rules in~\synchrules with multiple processes is $\syncactions (\synchrules) = \{a \mid \exists \langle \vectornot{t},a' \rangle \in \synchrules. a \in {\actions{}}(\vectornot{t}) \wedge |{\it Ac}(\vectornot{t})|>1 \}$.
The set of indices of process LTSs that can potentially synchronize with behavior in $\Llts^\ri$ according to a synchronization rule in \synchrules is $\inv(\Llts^\ri, \synchrules) = \bigcup \{ {\it Ac}(\vectornot{t}) \mid \syncruletuple \in \synchrules \wedge \vectornot{t}[i] \in \actions{\Llts^\ri} \}$.
This definition states that $j$ is in $\inv(\Llts^\ri, \synchrules)$ iff there exists a synchronization rule $\syncruletuple$ in $\synchrules$ such that both $i \neq \bullet$ and $j \neq \bullet$, i.e.\ both $i$ and $j$ are active for that rule, and the behavior in $\Pi[i]$ is matched on by transformation rule $\ri = \transruletuple{r_i}$.
The set of actions of process $j$ on which the actions in~$\Llts^\ri$ depend according to the synchronization rules in~\synchrules is $\dep^{\Llts^\ri}(j, \synchrules) = \{ \vectornot{t}[j] \mid \syncruletuple \in \synchrules \wedge \vectornot{t}[i] \in \actions{\Llts^\ri} \wedge \vectornot{t}[j] \neq \bullet \}$.
In other words, the set of all actions $\bigcup_{t \in F} \{\vectornot{t}[j] \} \setminus \{\bullet\}$ constitutes $\dep^{\Llts^\ri}(j, \synchrules)$, where~$F$ represents the set of all synchronization rules applicable on $\Llts^\ri$.
Sets $\inv(\Rlts^\ri, \synchrules)$ and $\dep^{\Rlts^\ri}(j, \synchrules)$ are defined similarly.
Now, synchronization uniformity can be defined.

\begin{definition}
\label{def:lts-transformation:uniformity}
We say that rule system~$\Sigma = \rulesystemtuple$ is synchronization uniform w.r.t.\ network~$\smodel = \networktuple$ iff the following holds.
\begin{enumerate}
\item
$\forall a \in \actions{s} (\synchrules). (\exists \ri \in R. a \in \actions{\Llts^\ri}) \implies \forall s_1\xrightarrow{a}_{i} s_2 . s_1, s_2 \in m_\ri(\states{\Llts^\ri})$;
\item
$\forall \ri \in R, j \in \inv(\Llts^\ri, \synchrules). \dep^{\Llts^\ri}(j, \synchrules) \subseteq \actions{\Llts^\rj}$;
\item
$\forall \langle\vectornot{t},a\rangle \in \hat\synchrules, i \in 1..n. \vectornot{t}[i] = \bullet \vee \vectornot{t}[i] \in \actions{\Rlts^\ri}$.
\end{enumerate}
\end{definition}

The first condition states that if a transformation rule is applicable to a synchronizing transition, then it is applicable to all synchronizing transitions with the same label in~\smodel.
If this is not guaranteed, it becomes very hard to reason about the model after transformation because it is difficult to determine a priori exactly which transitions in different process LTSs will be able to synchronize in the network.
Therefore, predicting the effect of rewriting, for example, $a$-transitions in some places while keeping other $a$-transitions the same is as difficult.
Checking this condition requires inspecting the process LTSs of a network, unless we impose an additional restriction on rule systems.
If we require that all left-hand patterns of rules that modify synchronizing transitions consist of a single transition, the first condition holds, regardless of the structure of the process LTSs of~\smodel.
The second condition states that all actions that can synchronize with $\Llts^\ri$ are also transformed by $\Sigma$.
If this does not hold, it becomes hard to analyze the synchronizing behavior as appearing in transformation patterns.
In such cases, some of the behavior that is relevant for this analysis is not present in any of the patterns, which makes analysis based only on the rule system impossible.
Finally, the third condition states that each new synchronization rule~$\langle\vectornot{t},a\rangle \in \hat\synchrules$ involves actions from the $\Rlts^\ri$ of the corresponding rule~$\ri$ only.
It is crucial to rule out the possibility of transforming merely by introducing synchronization rules, because this also prevents analysis solely based on rule systems.
For example, if we define a new synchronization rule involving existing actions~$a$ and~$b$, and these actions were previously not allowed to synchronize, then we clearly change the model without actually transforming anything.

In the remainder of this chapter, we only consider rule systems that are synchronization uniform regarding a given model.
This may seem a big assumption, but in practice, one tends to transform synchronizing behavior in a uniform way.
Usually, synchronizing actions, say~$a$ and~$b$, represent communication.
If one wants to transform this behavior, it is natural to do this consistently in all places where~$a$ and~$b$ occur, and to transform the behavior of both communicating parties to keep them compatible with each other.

\subsection{Networks of transformation rules}

From a rule system, networks of transformation rules can be constructed.

\begin{definition}
\label{def:lts-transformation:rule-network}
For a model~$\smodel = \networktuple$, rule system~$\Sigma = \rulesystemtuple$, and a rule~$\ri \in R$, the vector~$\xi^\ri$ of transformation rules relevant for the behavior in $\Llts^\ri$ is defined as follows, for all $j \in 1..n$.

\[
\xi^\ri[j] =
\left\{
\begin{array}{ll}
\ast \mapsto \ast & {\rm if} ~ j \not\in \inv(\Llts^\ri, \synchrules) \\
r_{j,\kappa}	  & {\rm if} ~ j \in \inv(\Llts^\ri, \synchrules),
\end{array}
\right.
\]
\end{definition}

\noindent
where $\ast$ is a dummy state.
For a given vector $\xi^\ri$, $\xi_{\Llts}^\ri$ is the vector of left patterns of the extended transformation rules in~$\xi^\ri$, and $\xi_{\Rlts}^\ri$ is the vector of right patterns.
The networks~$\Xi_\Llts^\ri = (\xi_\Llts^\ri, \synchrules)$ and $\Xi_\Rlts^\ri = (\xi_\Rlts^\ri, \synchrules \cup \hat\synchrules)$ allow comparing synchronizing behavior in rule patterns, before and after transformation according to $\Sigma$, in particular involving~$\ri$.

\begin{figure}[hbt]
\centering
\includegraphics[scale=0.2]{lts-transformation/figs/non-preserving-rules}
\caption{A non-preserving transformation and an example of its application}
\label{fig:lts-transformation:non-preserving-trafo}
\end{figure}

On the left of Figure~\ref{fig:lts-transformation:non-preserving-trafo}, another example of a rule system is shown.
In general, this rule system is not $\Sf$-preserving.
Applying this rule system to the network in Figure~\ref{fig:lts-transformation:comparison-network} results in the network shown in the middle of Figure~\ref{fig:lts-transformation:non-preserving-trafo}.
The corresponding network LTS shown on the right of this figure is obtained after hiding the actions in~$h$.
The networks of the left and right patterns of the two transformation rules in this figure are shown in Figure~\ref{fig:lts-transformation:subsets-ltss:middle}.
Actions in~$h$ are hidden.
The dotted lines in this figure illustrate that a divergence-sensitive branching bisimulation exists for these two networks.

%%%\begin{figure}[hbt]
%%%\centering
%%%\includegraphics[scale=0.2]{lts-transformation/figs/subsets-ltss}
%%%\caption{Network LTSs of networks of transformation rules}
%%%\label{fig:lts-transformation:subsets-ltss}
%%%\end{figure}

\begin{figure}[hbt]
  \hfill
  \begin{subfigure}[b]{102pt}
    \centering
    \includegraphics[scale=0.2]{lts-transformation/figs/subsets-ltss-left}
    \caption{}
    \label{fig:lts-transformation:subsets-ltss:left}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{106pt}
    \centering
    \includegraphics[scale=0.2]{lts-transformation/figs/subsets-ltss-middle}
    \caption{}
    \label{fig:lts-transformation:subsets-ltss:middle}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{102pt}
    \centering
    \includegraphics[scale=0.2]{lts-transformation/figs/subsets-ltss-right}
    \caption{}
    \label{fig:lts-transformation:subsets-ltss:right}
  \end{subfigure}
  \hfill
  \caption{Network LTSs of networks of transformation rules}
\end{figure}

Even though a divergence-sensitive branching bisimulation exists between the networks of the left and right patterns of the transformation rules in Figure~\ref{fig:lts-transformation:non-preserving-trafo}, the rule system is not \Sf-preserving.
This clearly shows that it is not sufficient to only take successful synchronization in account.
Instead, we also need to consider situations in which some parties are able to perform a synchronizing action whereas at least one other party involved in the synchronization is not.
For example, the state labeled (1 3) in Figure~\ref{fig:lts-transformation:comparison-network} has a $\tau$-loop that cannot be simulated by the network LTS in Figure~\ref{fig:lts-transformation:non-preserving-trafo}.
This $\tau$-loop is the result of hiding the $b$-loop of the leftmost process of Figure~\ref{fig:lts-transformation:comparison-network}.
This process can perform action~$b$ independently.
After transformation, however, a $\tau$-cycle can only result from interaction between the transformed process LTSs shown in the middle of Figure~\ref{fig:lts-transformation:non-preserving-trafo}.
In situations where both processes are able to interact successfully, an infinite number of $\tau$-actions can be performed, as shown in Figure~\ref{fig:lts-transformation:subsets-ltss:middle}.
However, if the required synchronization between processes is impossible, as is the case in the state labeled (1 3) in Figure~\ref{fig:lts-transformation:non-preserving-trafo}, only one $\tau$-action can be performed.

To be able to consider such scenarios, we define a projection operator on networks of transformation rules.

\begin{definition}
\label{def:lts-transformation:projection}
For each vector of transformation rules~$\xi^\ri$ and~$j \in 1..n$, the projection operator $/I$ ($I \subseteq 1..n$) is defined as follows.
\[
\xi^\ri/I[j] = \left\{
\begin{array}{ll}
\xi^\ri[j]                    & {\rm if} ~ j \in I \\
\ast \mapsto \ast & {\rm otherwise}
\end{array}
\right.
\]
\end{definition}

\noindent
This operator can similarly be applied on the vectors of patterns~$\xi_{\Llts}^\ri$ and~$\xi_{\Rlts}^\ri$, and we say that $\Xi_\Llts^\ri/I = (\xi_\Llts^\ri/I, \synchrules)$ and $\Xi_\Rlts^\ri/I = (\xi_\Rlts^\ri/I, \synchrules \cup \hat\synchrules)$, for a given model~$\smodel = \networktuple$ and rule system~$\Sigma = \rulesystemtuple$ such that $\ri \in R$.

Figure~\ref{fig:lts-transformation:subsets-ltss:left} shows the left and right patterns of the rule network that contains only the topmost transformation rule of Figure~\ref{fig:lts-transformation:non-preserving-trafo}.
The left pattern of this rule network is constructed from the left pattern of this transformation rule, using the synchronization rules and the hiding set of Figure~\ref{fig:lts-transformation:comparison-network}.
The right pattern of this rule network is constructed from the right pattern of this transformation rule, using the synchronization rules and the hiding set of Figure~\ref{fig:lts-transformation:non-preserving-trafo}.
Because these patterns are not divergence-sensitive branching bisimilar, as indicated by the cross, this rule system is not \Sf-preserving.
The remaining patterns are shown in Figure~\ref{fig:lts-transformation:subsets-ltss:right}.

\subsection{Constructing a Bisimulation Relation}
The following theorem formalizes our $\Sf$-preservation check.

\begin{theorem}
\label{theo:lts-transformation:checkpreservation}
Let $\smodel = \networktuple$ be a model and $\Sf \in \dsbrLmu$ a temporal property such that $\models_{\smodel} \Sf$.
Then $\Sigma = \rulesystemtuple$ is $\Sf$-preserving if the following holds, for all $\ri \in R$, $I \subseteq \inv(\Llts^\ri, \synchrules)$.
\begin{equation}
\maxabstr(\Xi_{\Llts}^\ri/I ) \dsbbis \maxabstr(\Xi_{\Rlts}^\ri/I )
\label{eq:lts-transformation:maintheo}
\end{equation}
\end{theorem}

Proving this theorem for a rule system~$\Sigma$ and a property~\Sf entails constructing a relation between~$\maxabstr(\smodel)$ and~$\maxabstr(T_{\Sigma} (\smodel))$ based on the divergence-sensitive branching bisimulations in equation~\ref{eq:lts-transformation:maintheo}, for some model~\smodel, and proving that this relation is a divergence-sensitive branching bisimulation too.
To describe the construction of this relation, we need a number of auxiliary definitions.

First, a state in the network LTS of network~\smodel is a vector $\vectornot{s} = \langle \vectornot{s}[1],\ldots, \vectornot{s}[n] \rangle$.
An arbitrary $\vectornot{s} \in \states{\smodel}$ can have up to $n$ elements that are matched on by some transformation rule.
For an LTS~$\graph$ and a transformation rule~$r = \transruletuple{r}$,
matching $m_r$ is extended to sets of states such that $m_r(\states{\Llts^r}) \subseteq \states{\graph}$ refers to the set of states to which the states in $\states{\Llts^r}$ are matched,
and $\hat m_r(\states{\Llts^r}) = \{ s \in m_r(\states{\Llts^r}) \mid m_r^{-1}(s) \not\in \states{\Rlts^r} \}$ refers to the states in~\states{\graph} that relate to non-glue-states in $\Llts^r$.
By definition, the latter states are those that are removed from~\graph by rule~$r$.
We denote the set of indices of elements in $\vectornot{s}$ matched on by the corresponding rule with $M(\vectornot{s}) = \{ i \mid \vectornot{s}[i] \in m_\ri(\states{\Llts^\ri}) \}$.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%In a similar way, $\hat M(\vectornot{s}) = \{ i \mid \vectornot{s}[i] \in \hat m_\ri(\states{\Llts^\ri}) \}$.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
As indicated above, we assume that if $\vectornot{s}[i]$ is matched on, then it is matched on by rule $r_i$.
Furthermore, for a rule system~$\Sigma = \rulesystemtuple$ and a network~\smodel, a simulation relation exists between the states in~\smodel and those in the rule networks~$\Xi_\Llts^\ri/I$ as well as between the states in~$T_{\Sigma} (\smodel)$ and those in~$\Xi_\Rlts^\ri/I$.
This can be formalized as follows.
For a state vector $\vectornot{s}^*$ in a rule network~$\Xi_\Llts^{\ri}/I$ and a state vector $\vectornot{s}$ in an LTS network~$\smodel$, we say that $\vectornot{s}$ simulates $\vectornot{s}^*$, denoted $\vectornot{s}^* \vdash \vectornot{s}$, iff $\forall i \in 1..n. \vectornot{s}^*[i] \neq \ast \implies \vectornot{s}[i] = m_\ri(\vectornot{s}^*[i])$.
In other words, besides the $\ast$-states, all process states in $\vectornot{s}^*$ are matched on the corresponding states in $\vectornot{s}$.
Now, first of all, if $\vectornot{s}^* \vdash \vectornot{s}$, and $\vectornot{s}^*\xrightarrow{a} \vectornot{s}^*'$, then also $\vectornot{s}\xrightarrow{a} \vectornot{s}'$ and $\vectornot{s}^*' \vdash \vectornot{s}'$.
Second of all, in cases that $\vectornot{s}\xrightarrow{a} \vectornot{s}'$ and some synchronization vector~$\vectornot{t}$ enables transition~$\vectornot{s}\xrightarrow{a} \vectornot{s}'$, with ${\it Ac}(\vectornot{t}) \subseteq I$, ${\it Ac}(\vectornot{t}) \subseteq M(\vectornot{s})$, and ${\it Ac}(\vectornot{t}) \subseteq M(\vectornot{s}')$, then the involved behavior of every active $\Pi[i]$ ($i \in {\it Ac}(\vectornot{t})$) is matched on by $\ri$.
The definition for~$\Xi_\Rlts^{\ri}/I$ and~$T_{\Sigma} (\smodel)$ is similar.
These simulation relations are preserved after maximal hiding.

\begin{figure}[hbt]
\centering
\includegraphics[scale=0.2]{lts-transformation/figs/relations}
\caption{Constructing a divergence-sensitive branching bisimulation}
\label{fig:lts-transformation:relations}
\end{figure}

A relation between two networks representing a model before and after transformation can be constructed by combining the bisimulations between pairs of rule networks with the simulation relations between model networks and rule networks.
In Figure~\ref{fig:lts-transformation:relations}, the dashed lines connect some of the states in the LTSs of the rule networks with the states in the LTSs of the models that simulate them.
The dotted lines in the figure denote the divergence-sensitive branching bisimulations between pairs of rule networks.
By combining these relations, states in the LTS on the left of the figure are related to states in the LTS on the right.
We refer to the relation formed by combining these relations as~$D$.
To increase the readability of the figure, not all states that are related according to~$D$ are connected.
For states~$\vectornot{s}$ of~$\smodel$ and~$\vectornot{p}$ of~$T_{\Sigma} (\smodel)$, we define $\vectornot{s}\ D'\ \vectornot{p}$ iff $\forall i \not \in M(\vectornot{s}) . \vectornot{s}[i] = \vectornot{p}[i]$.
In words, $D'$ relates all state vectors with exactly the same elements apart from those matched on by a transformation rule.
In a sense, $D'$ is a strong bisimulation for the behavior not subjected to transformation.
As mentioned above, not all states that are related according to~$D$ are connected in the figure.
Only those states of the LTS on the left of the figure are connected to states of the LTS on the right that are also related according to~$D'$.
Thus, the dashed and dotted lines together illustrate the relation~$(D \cap D')$ by connecting some of the states that are related according to this relation.
Relation~$D'' = \{ (\vectornot{s}, \vectornot{s}) \mid M(\vectornot{s}) = \emptyset \}$ relates all states that are not subjected to transformation.
The grey lines in Figure~\ref{fig:lts-transformation:relations} connect the states that are related according to~$D''$.
Given these relations, a relation~$C = (D \cap D') \cup D''$ can be constructed, which relates~$\maxabstr(\smodel)$ and~$\maxabstr(T_{\Sigma} (\smodel))$.
By considering the cases of Definition~\ref{def:lts-transformation:dsbbsim}, it can be shown that this relation is a divergence-sensitive branching bisimulation~\cite{EngelenWijsPropPres2012}.


\setlength{\footskip}{8mm}

\chapter{Clustering Human Behaviors with Dynamic Time
Warping and Hidden Markov Models}
\label{ch:clustering}

\textit{In this chapter, we propose and experimentally evaluate a new
method for clustering human behaviors that is suitable for
bootstrapping an anomaly detection module for intelligent video
surveillance systems. The method uses dynamic time warping,
agglomerative hierarchical clustering, and hidden Markov models to
provide an initial partitioning of a set of observation sequences then
automatically identifies where to cut off the hierarchical clustering
dendrogram. We show that the method is extremely effective, providing
100\% accuracy in separating anomalous from typical behaviors on
real-world testbed video surveillance data.}

\section{Introduction}
\label{sec:clustering-intro}

Human behavior understanding is an important component of a wide
variety of desirable intelligent systems.  However, the problem is
very difficult, due to the wide range of activities possible in any
given context and the large amount of variability within any
particular activity. Many researchers have attempted to build systems
able to interpret and understand human behaviors. The most classic
work is from \shortciteA{yamato92hmm}, who model
tennis actions using hidden Markov models (HMMs).
\shortciteA{du06activity} present an approach to recognize
interaction activities using dynamic Bayesian networks (DBNs) that
outperforms conventional HMMs. \shortciteA{gao04dining}
use a single mixture-of-Gaussian HMM, in which the states represent
the stages of dining activities, to monitor the eating behavior of
elderly people in a nursing home.

As video monitoring is becoming more ubiquitous in our lives, research
on advanced video surveillance analysis is increasingly important.  To
help security personnel work reliably and efficiently, we would like
to filter out typical events, and in cases of anomalous events,
automatically raise an alarm or present the event to a human operator
for consideration as a security threat. Most existing work assumes
that the number of ``normal'' behavior patterns need to be known
beforehand. For example, \shortciteA{nair02surveillance} built an
automated video surveillance system using HMMs, each modeling a
common, predefined activity in a scene.

In more recent years, research has started to focus on unsupervised
analysis and clustering of behaviors in a particular scene for a
variety of purposes including anomaly detection, surveillance, and
classification. \shortciteA{zhong04detection} treat video segments as
documents and cluster the documents based on the co-occurrence
information. \shortciteA{li06behavior} cluster human gestures by
constructing an affinity matrix using dynamic time warping (DTW;
Sakoe, 1978)\nocite{sakoe78dtw} then apply the normalized-cut approach
to cluster the gestures. \shortciteA{hautamaki08clustering} apply DTW
and use the pairwise DTW distances as input to a hierarchical
clustering process in which $k$-means is used to fine-tune the output.

Here we use a combination of clustering and HMMs to group
human behaviors in a scene. There is some recent related work using
graphical models such as HMMs to cluster behavior
patterns. \shortciteA{li99pattern} use the Bayesian information
criterion (BIC) for HMM model selection and construct a binary
hierarchical clustering dendrogram to initialize data partitions based
on a sequence-to-model likelihood distance measure. They then compare
each pair of clusters using a partition mutual information (PMI)
criterion \shortcite{bahl86speech} to find the optimal number of
clusters.

\shortciteA{xiang05profiling} model the distribution of
activity data in a scene using a Gaussian mixture model (GMM) and also
employ the BIC to select the optimal number of behavior classes prior
to HMM training.

\shortciteA{swears08clustering} propose hierarchical HMM-based clustering
to find and cluster motion trajectories and velocities in a highway
interchange scene. They build up a set of HMMs incrementally. For each
new trajectory, they first test the likelihood of the trajectory
according to each existing HMM model.  If the new observation is not
fit by any existing model, it is considered deviant and is grouped
with other deviant observations to form a new HMM.

\shortciteA{alon03clustering} propose a method to discover groupings of
similar object motions. They apply a finite mixture of HMMs where the
number of mixture components is assumed to be known. They estimate the
number of clusters using the minimum description length (MDL)
criterion \shortcite{rissanen98mdl}, a penalized likelihood measure.

In this dissertation, we propose a new method for clustering human
behaviors in the context of video surveillance.  After extracting
sequences of features representing individual human behaviors in a
given scene, we use DTW to measure the pairwise similarity between
sequences. Then we construct an agglomerative hierarchical clustering
dendrogram based on the DTW similarity measure. To find the optimal
set of behavior clusters, we start at the root of the tree, train a
HMM on the patterns in that cluster, and determine how well the HMM
models the set of patterns in the cluster.  When we find that a HMM is
an insufficient representation of the patterns in a given cluster, we
throw away that HMM and recursively consider each of the child
clusters according to the pre-calculated DTW-based dendrogram. Our
method is able to automatically find the common human behaviors
occurring in a given scene. In an experiment with a testbed video
surveillance data set, we find that the method separates typical
behaviors and abnormal behaviors into separate sets of clusters with
100\% accuracy.

The most similar related work is that
of \shortciteA{oates01clustering}, who first proposed the idea of
using the DTW with HMMs to cluster time series. They use the DTW
dendrogram cut off at an arbitrary depth as an initial partition of
the training sequences, then they train HMMs on each partition
iteratively until they have a set of HMMs that models all of the
training sequences.  They apply their method to simulated time series
with good results but report obtaining poor clustering results in an
experiment with real robot sensor data.

As we shall see in later chapters, the method improves upon the state
of the art in intelligent video surveillance applications by
bootstrapping human behavior classification and anomaly detection
modules in a given installation. Once a set of initial clusters and
corresponding HMMs representing typical behavior is determined, we can
easily find which cluster a new sequence should fall into by
performing statistical tests on the sequence's likelihood according to
each HMM model.  When the likelihood is low according to all of the
pre-existing clusters, we can consider the sequence to be anomalous
and alert a human operator. When the likelihood is sufficiently high
for one of the pre-existing models, we can simply incrementally update
the sufficient statistics for that model.  This approach would allow
raising alerts for behaviors inconsistent with the
automatically-derived typical behavior profile for the scene while
providing adaptation to gradual changes in typical behavior patterns
over time. We do not focus on these incremental learning and anomaly
detection issues in this chapter. We consider these issues in later
chapters.

In the rest of this chapter, I provide the details of our human
behavior clustering algorithm in
Section \ref{sec:clustering-algorithm}, demonstrate the feasibility of
the algorithm with an experimental evaluation in
Section \ref{sec:clustering-results}, and then conclude and point to
future work in Section \ref{sec:clustering-discussion}.

\section{Human Behavior Pattern Clustering}
\label{sec:clustering-algorithm}

\subsection{Overview}

\begin{figure}[t]
  \centering
  \subfloat[]{\includegraphics[scale=0.4]{figures/blob-extraction-block-diagram}
  \label{fig:overview-blob}}
  \subfloat[]{\includegraphics[scale=0.4]{figures/behavior-clustering-block-diagram}
  \label{fig:overview-clustering}}
  \caption[Block diagrams for the proposed method.]{\small Block
    diagrams for the proposed method.  (a) Blob extraction flow. (b)
    Behavior clustering flow.}
  \label{fig:overview-blob-diagram}
\end{figure}

Figure \ref{fig:overview-blob-diagram} provides an overview of the
architecture of our proposed method.  The blob extraction phase
(Figure \ref{fig:overview-blob}) generates observation sequences from
videos as follows:

\begin{enumerate}
  \item Grab a few initial frames from an the input video to model the
    background scene.
  \item Perform foreground extraction to get a list of blobs.
  \item Find the single largest blob in the scene, remove any pixels
    likely to be shadow pixels, and extract feature vector $\vec{f}_t$
    for the blob at time $t$.
  \item Apply vector quantization using the $k$-means algorithm to
    convert features into symbols.
  \item Aggregate symbol sequences and store for batch cluster
    analysis.
\end{enumerate}

In the behavior clustering phase
(Figure \ref{fig:overview-clustering}), for the set of all discrete
symbol sequences, we perform the following steps:

\begin{enumerate}
  \item Apply DTW to the set of sequences to construct a
    similarity-based distance matrix.
  \item Run agglomerative hierarchical clustering on the distance
    matrix to get a dendrogram.
  \item Perform HMM-based hierarchical clustering using the set of
    sequences and the dendrogram to get a set of HMMs modeling the
    typical behaviors in the scene.
\end{enumerate}

\subsection{Blob Extraction}
\label{sec:clustering-blob-extraction}

Here we use the motion detection and blob extraction methods
previously described in Sections \ref{sec:blob-motion-detection}
and \ref{sec:blob-blob-extraction}, respectively.

In the work reported in this chapter, we use a simple normalized cross
correlation (NCC) shadow elimination method described in
Chapter \ref{ch:shadow} to eliminate shadows cast by moving objects.
We compute the grayscale correlation between the foreground pixels and
a background image constructed as the mean over each mixture of
Gaussian distribution. Any foreground pixels whose NCC with the
background are above some threshold are removed. We choose this method
because it works well in outdoor scenes where background texture
inside shadows is visible.  The sample results from the foreground
extraction and shadow removal procedures are shown in
Figure \ref{fig:shadow-result}. We use the NCC method for shadow
removal through the rest of this dissertation.

\begin{figure}
  \centering
  \subfloat[]{\includegraphics[width=0.28\linewidth]{figures/shadow-result01.png}
  \label{fig:shadow-result-original}}
  \hspace{0.1in}
  \subfloat[]{\includegraphics[width=0.28\linewidth]{figures/shadow-result02.png}}
  \hspace{0.1in}
  \subfloat[]{\includegraphics[width=0.28\linewidth]{figures/shadow-result03.png}}
  \caption[Sample foreground extraction and shadow removal
    results.]{\small Sample foreground extraction and shadow removal
    results.  (a) Original image. (b) Foreground pixels according to
    background model. (c) Foreground pixels after shadow removal.}
  \label{fig:shadow-result}
\end{figure}

We next apply morphological opening and closing operations to remove
noise and connect foreground regions, then we obtain the connected
foreground components (blobs) and filter out any components whose size
is below threshold. In this work, for simplicity, we evaluate our
clustering method with videos containing a single moving blob.

We therefore follow the process previously described in
Section \ref{sec:blob-appearance-based-blob-tracking} for representing
a blob (connected foreground component) by a feature vector,
normalizing each feature independently, and quantizing the feature
vectors into discrete symbols using $k$-means.

%We therefore simply represent a blob (connected foreground component)
%at time $t$ by the feature vector
%\[
%  \vec{f}_t = \begin{bmatrix} x_t & y_t & s_t & r_t & dx_t & dy_t &
%  v_t \end{bmatrix},
%\]
%where $(x_t,y_t)$ is the centroid of the blob, $s_t$ is the size of
%the blob in pixels, $r_t$ is the aspect ratio of the blob's bounding
%box, $(dx_t,dy_t)$ is the unit-normalized motion vector for the blob
%compared to the previous frame, and $v_t$ is the blob's speed compared
%to the previous frame, measured as
%\[
%  v_t = \frac{{\sqrt {(x_t - x_{t - 1} )^2 + (y_t - y_{t - 1} )^2 }
%  }}{{\Delta t}},
%\]
%where $\Delta t$ is the capture time difference between the frames at
%time $t$ and $t-1$. We increase the stability by using an average
%velocity, measured as
%\[
%  v_t  = rv_t  + (1 - r)v_{t - 1},
%\]
%where $r$ is a constant. We use $r=0.5$ in our experiments.

%After extracting the feature vectors $\vec{f}_t$ over a training set,
%we quantize them into discrete symbols using $k$-means. To prevent
%differing numeric scales of the features from affecting the distance
%metric, we normalize each feature independently by $z$-scaling to a
%mean of 0 and standard deviation of 1 over the training set.  For the
%vectors $(x_t,y_t)$ and $(dx_t,dy_t)$, rather than normalize the $x$
%and $y$ components independently, we use a common isotropic scale
%factor for the two dimensions to avoid overemphasizing small
%deviations from typical trajectories in directions without much
%deviation in the training data.

Currently, we empirically tune the free parameters (frame buffer
length, thresholds, and number of $k$-means clusters) to the training
data. We hope to automate the blob extraction parameter selection
process in future work.

\subsection{Behavior Clustering}
\label{sec:clustering-behavior-clustering}

We model the common behaviors in a scene by clustering a set of
observation sequences acquired over some period of time. First, we
apply dynamic time warping (DTW) to estimate the similarity between
every pair of training sequences despite variations in length and
speed, to obtain a similarity matrix. Second, we use the similarity
matrix for hierarchical agglomerative clustering by first combining
the most similar two sequences into a single cluster then repeatedly
merging clusters until just one cluster is left at the root of the
tree or dendrogram.  To determine the similarity of two clusters
during this step, we use the similarity of the most similar pair of
sequences between the two clusters.

The resulting DTW dendrogram provides a convenient representation of
the similarity structure within a set of time sequences, but
hierarchical clustering always comes with the practical issue of
determining the optimal cutoff or number of clusters to use in a
particular application.  We solve this problem using HMMs as described
below.

The flow of the algorithm is summarized in
Figure \ref{fig:flow-diagram}.  We begin at the root of the
hierarchical clustering dendrogram and attempt to model the sequences
in that cluster (all training sequences, for the root) using a
HMM. When there are more than $N$ sequences in parent cluster $c$
whose per-observation log likelihood is less than a threshold $p_c$,
we consider the HMM to be inadequate, throw it away, and then
recursively attempt to model each of $c$'s children in the DTW
dendrogram.  We use $N=10$ in our experiments. The per-observation log
likelihood of a sequence $\vec{O}_i = \{ O_{i, 1}, O_{i, 2} \cdots
O_{i, T_i} \}$ is
\[
  L_{i} = \frac{\log P( \vec{O}_i \mid M_c )}{T_i},
\]
where $M_c$ is the HMM that models the sequences in cluster $c$, $T_i$
is the number of observations in sequence $i$, and $P(\vec{O}_i
\mid M_c)$ is calculated using the forward algorithm \shortcite{rabiner89hmm}.

%(calculated using the forward algorithm; Rabiner,
%1989\nocite{rabiner89hmm})

\begin{figure}[t]
  \begin{center} \includegraphics[width=3.8in]{figures/clustering-flow-diagram} \end{center} \caption[Processing
    flow of the use of HMM clustering method.]{\small Processing flow
    of the use of HMM clustering method.}  \label{fig:flow-diagram}
\end{figure}

To determine the optimal rejection threshold $p_c$ for cluster $c$, we
use an approach similar to that of \shortciteA{oates01clustering}.  We
generate random sequences from the HMM and then calculate the mean
$\mu_c$ and standard deviation $\sigma_c$ of the per-observation log
likelihood over the set of generated sequences.  For the lengths of
the generated sequences, we simply use the average length of the
training patterns in cluster $c$.  After obtaining the statistics of
the per-observation log likelihood, we let $p_c$ be $\mu_c -
z \sigma_c$, where $z$ is an experimentally tuned parameter that gives
us convenient control over the probability of making Type I errors in
classifying a particular sequence as having been generated by a
particular HMM model.

\section{Experimental Results}
\label{sec:clustering-results}

To create a testbed data set, we mounted a CCTV camera to view the
scene in front of an academic building, as seen in
Figure \ref{fig:shadow-result-original}. We recorded videos at a
resolution of $320 \times 240$ and 25 frames per second over one week
during working hours (9:00--17:00). To save disk space, we used a
motion detection technique that automatically segments a raw video
stream into separate videos containing motion. We obtained videos
corresponding to over 500 motion events then manually selected the 298
videos containing only a single motion.

We found that there are at least four common behaviors in this scene:
people walking into the building, walking out of the building, parking
a bicycle, and riding a bicycle out. Figure \ref{fig:example-behavior}
shows examples of each of these behaviors. Other less common
activities include people walking into the scene then walking out or
people walking while telephoning and leaving the scene. For purposes
of evaluating the results of our algorithm, we hand-labeled each of
the videos with the categories Walk-in, Walk-out, Cycle-in, Cycle-out,
or Other.

\begin{figure}[t]
  \centering
  \subfloat[]{\includegraphics[width=0.2\linewidth]{figures/example-behavior01.pdf}}
  \hspace{0.05in}
  \subfloat[]{\includegraphics[width=0.2\linewidth]{figures/example-behavior02.pdf}}
  \hspace{0.05in}
  \subfloat[]{\includegraphics[width=0.2\linewidth]{figures/example-behavior03.pdf}}
  \hspace{0.05in}
  \subfloat[]{\includegraphics[width=0.2\linewidth]{figures/example-behavior04.pdf}}
  \caption[Examples of common human activities in our testbed
    scene.]{\small Examples of common human activities in our testbed
    scene.  (a) Walking in. (b) Walking out.  (c) Cycling in. (d)
    Cycling out.}
  \label{fig:example-behavior}
\end{figure}

We performed three experiments to evaluate our method. In Experiment
I, we applied our proposed method, as previously described, to cluster
the 298 single-motion videos in our testbed data set. In Experiment
II, we used an alternative clustering method that only uses recursive
modeling by HMMs, without DTW. In Experiment III, we used an
alternative method combining HMMs with supervised learning. In every
experiment, we evaluated the clustering results (or classification
results in the case of the supervised system of Experiment III)
according to how well the induced categories separate the anomalous
sequences (hand-labeled with the category ``Other'') from the typical
sequences (Walk-in, Walk-out, Cycle-in, Cycle-out). Our main
hypothesis was that \textit{using DTW as a pre-process prior to
HMM-based clustering should improve the quality of the clusters} in
terms of separating anomalous from typical behaviors. One might also
have hypothesized that supervised learning (Experiment III) would be
better than either of the unsupervised methods (Experiments I and II),
but as we shall see, we obtained a somewhat surprising result to the
contrary.

In all three experiments, we used linear HMMs with four states and
bypass transitions. That is, each HMM had transitions from state 1 to
states 1, 2, and 3, from state 2 to states 2, 3, and 4, from state 3
to states 3 and 4, and from state 4 to itself. We chose this model
structure based on our previous empirical
experience \shortcite{kan08thesis}.

To find the distribution (parameters $\mu_c$ and $\sigma_c$) of the
per-observation log likelihood for a particular HMM, we always
generated 1000 sequences of 120 observations then used a $z$-threshold
of 2.0, corresponding to a Type I error (probability of misclassifying
a sequence generated by the HMM as not generated by the HMM) of
0.0228.  We fixed the parameter $N$ (the number of deviant patterns
allowed in a cluster) to 10.

\subsection{Experiment I (DTW+HMMs)}

\begin{table}[t]
  \caption[Clustering results for Experiment I (DTW+HMMs).]{\small
    Clustering results for Experiment I (DTW+HMMs).}
  \label{tab:dtw-and-hmm-assoc-matrix}
  \begin{center}
    \begin{tabular}{c|c|c|c|c|c}
      \hline
      Cluster \# & Walk-in & Walk-out & Cycle-in & Cycle-out & Other \\
      \hline \hline
      1  & 96 & 0  & 18 & 0 & 0 \\ \hline
      2  & 0  & 54 & 0  & 5 & 0 \\ \hline
      3  & 0  & 3  & 0  & 8 & 0 \\ \hline
      4  & 0  & 2  & 0  & 0 & 0 \\ \hline
      5  & 0  & 1  & 0  & 2 & 0 \\ \hline
      6  & 0  & 0  & 0  & 2 & 0 \\ \hline
      7  & 0  & 0  & 0  & 2 & 0 \\ \hline
      8  & 0  & 0  & 0  & 2 & 0 \\ \hline
      9  & 0  & 0  & 0  & 2 & 0 \\ \hline
      10 & 0  & 0  & 2  & 0 & 0 \\ \hline
      11 & 0  & 0  & 2  & 0 & 0 \\ \hline
      12 & 0  & 0  & 3  & 0 & 0 \\ \hline
      13 & 0  & 0  & 1  & 1 & 0 \\ \hline
      14 & 0  & 0  & 0  & 0 & 4 \\ \hline
      15 & 0  & 0  & 0  & 0 & 4 \\ \hline
      16 & 0  & 0  & 0  & 0 & 2 \\ \hline
      17 & 0  & 0  & 0  & 0 & 2 \\ \hline
      One-seq clusters & 4 & 17 & 34 & 21 & 4 \\ \hline
    \end{tabular}
  \end{center}
\end{table}

The clustering results are shown in
Table \ref{tab:dtw-and-hmm-assoc-matrix}. The method obtained 97
clusters. For the 17 clusters containing more than one sequence, we
show the distribution of the activities represented by each
sequence. For the 80 clusters containing only a single sequence, we
summarize their distribution across the activity categories in the
last row of the table.

It is clear from the results that the separation of anomalous and
typical behaviors is excellent (100\% accuracy), with the caveat that
80 sequences (26.8\% of the data set) fall into single-sequence
clusters that would have to be manually examined by a human operator
if the method was used to bootstrap a real-world surveillance system.

\subsection{Experiment II (HMMs only)}

To determine the extent to which our system benefits from
preprocessing using DTW, in this experiment, we used the concept of
recursive modeling of the data using HMMs without using DTW as a
pre-process.  The method is similar to that
of \shortciteA{swears08clustering}.  We begin by training a single HMM
on all sequences and computing the distribution of the per-observation
log likelihood for that HMM as previously described.  We then assign
every sequence with a per-observation log likelihood above threshold
$p_c$ to a cluster then repeat the process by training a new HMM on
the remaining sequences.  Similarly to the method of Experiment I, we
stop splitting whenever the number of deviant sequences in the cluster
is less than 10.

\begin{table}[t]
  \caption[Clustering results for Experiment II (HMMs only).]{\small
    Clustering results for Experiment II (HMMs only).}
  \label{tab:hmm-assoc-matrix}
  \begin{center}
    \begin{tabular}{c|c|c|c|c|c}
      \hline
      Cluster \# & Walk-in & Walk-out & Cycle-in & Cycle-out & Other \\
      \hline \hline
      1 & 15 & 77 & 49 & 43 & 16 \\ \hline
      2 & 80 & 0 & 11 & 2 & 0 \\ \hline
      3 & 5 & 0 & 0 & 0 & 0 \\ \hline
    \end{tabular}
  \end{center}
\end{table}

The results are shown in Table \ref{tab:hmm-assoc-matrix}. The method
obtains only three clusters, and the clusters are incapable of
separating anomalous behaviors from typical behaviors.  With manual
assignment of all three clusters to the ``typical'' category, we would
achieve a recall of 0, a precision of 100\%, and an accuracy of
94.6\%.  By manually assigning cluster 1 to the anomalous category, we
would achieve a recall of 100\%, a precision of 8\%, and an accuracy
of 38.3\%.  These strikingly poor results confirm our main hypothesis,
and we conclude that recursive HMM modeling without DTW-based
preprocessing is useless for video surveillance.

\subsection{Experiment III (Supervised classification with HMMs)}

As an alternative approach to separating anomalous from typical
behaviors, in this experiment, we trained four HMMs on 80\% of each of
the four typical behaviors observed in our testbed data set.  We
retained 20\% of each typical sequences and the 16 anomalous sequences
as a test set. After HMM training, we manually determined the best
per-observation log likelihood threshold for each HMM by maximizing
the F1 value (a measure combining both precision and recall) for the
separation between the positive and negative test patterns.  The
anomalous pattern detection rate of the the combined classifier was
50\% (8 patterns) with a false alarm rate of 24.6\% (16 out of 65
normal testing patterns).

A priori, one might have hypothesized that supervised sequence
classification should outperform the unsupervised method we have
proposed in this chapter.  To the contrary, we find that the
simple-minded approach of training a single HMM on each typical
behavior is far inferior.  Clearly, there is more variation within the
behavior categories than can be handled precisely by single simple
HMMs.  These supervised results could presumably be improved by
applying our clustering method within each behavior category, thus
obtaining a collection of HMMs for each behavior category.  However,
we have already demonstrated that our method achieves perfect
separation of anomalous and typical behaviors \textit{without any
information about the labels}, so this approach would be unnecessarily
laborious.

\section{Discussion}
\label{sec:clustering-discussion}

In this chapter, we have proposed and evaluated a new method for
clustering human behaviors.  As we shall see, the method can be used
to bootstrap an anomaly detection module for intelligent video
surveillance systems.  The combination of DTW partitioning with linear
HMM training turns out to be quite powerful; manual examination of the
clusters obtained from our method shows a perfect separation between
typical and anomalous behaviors on a real-world testbed video
surveillance data set.

The combination of DTW with the type of linear HMMs we use in this
work is surprisingly effective. It is likely that the patterns DTW
groups together are perfectly suited for modeling by this type of HMM.
We plan to further explore this idea in future work.

There are two key limitations to our current method. The first is that
we limited the data to single motion events. The remaining chapters
will eliminate that constraint. The second is that although the method
achieves 100\% accuracy in separating typical from anomalous events,
it does so at the cost of creating a fairly large number of
single-sequence clusters that would have to be manually identified as
typical or anomalous by a human operator in a real surveillance
setting.

\FloatBarrier


\chapter{Scientific Background}

This chapter can be used as your literature reveiw chapter and to include any
information on scientific theories that are particularly relevant to your
research. Here is an example of and equation.
%
\begin{equation}
    F_{es} = -\frac{1}{2} \int \epsilon(\mathbf{r}) \mathbf{E}^2(\mathbf{r}) dr
    \label{esfnrg}
\end{equation}
%
where $\epsilon(\mathbf{r})$ is the dielectric constant at position
$\mathbf{r}$ and $\mathbf{E}(\mathbf{r})$ is the electric field at the same
position. The equation can be referenced like this \ref{esfnrg}.

The .bib file for this chapter is called ``literature.bib''.
% this is where the bibliography is included. Just add your bibtex citations to
% the literature.bib file and don't modify the code below unless you want to
% use a different citation style instead of "unsrtnat".
\singlespacing
\bibliographystyle{unsrtnat}
\bibliography{body/published-papers-style/chapter2/literature}
\doublespacing


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is just an example/guide for you to refer to when producing your supplementary material for your Frontiers article.                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% Version 2.1 Generated 2015/05/22 %%%
%%% You will need to have the following packages installed: datetime, fmtcount, etoolbox, fcprefix, which are normally inlcuded in WinEdt. %%%
%%% In http://www.ctan.org/ you can find the packages and how to install them, if necessary. %%%

\documentclass{frontiers/frontiers_suppmat} % for all articles
\usepackage[toc,nomain,acronym,shortcuts,translate=false]{glossaries}
\usepackage{url,microtype}
\usepackage[colorlinks=true,linkcolor=black, citecolor=black!80, urlcolor=black!80]{hyperref}
\usepackage{doi}
\usepackage[onehalfspacing]{setspace}

% Leave a blank line between paragraphs in stead of using \\

\def\keyFont{\fontsize{8}{11}\helveticabold }

\def\firstAuthorLast{Esteban {et~al.}} %use et al only if is more than 1 author
\def\Authors{Oscar Esteban*, Emmanuel Caruyer, Alessandro Daducci, Meritxell Bach-Cuadra,%
Mar\'ia-J. Ledesma-Carbayo and Andres Santos}

% The Corresponding Author should be marked with an asterisk
% Provide the exact contact address (this time including street name and city zip code) and email of the corresponding author
\def\corrAuthor{Oscar Esteban}
\def\corrAddress{Biomedical Image Technologies (BIT), ETSI Telecomunicaci\'on, Av. Complutense 30, C203, E28040 Madrid, Spain}
\def\corrEmail{phd@oscaresteban.es}

\input{abbreviations.tex}

\providecommand{\diffantom}{\emph{Diffantom}}
\providecommand{\Diffantom}{\emph{Diffantom}}
\newcommand{\lowb}{\textit{b0}}
\newcommand{\e}[1]{\ensuremath{\;\cdot\,\text{10}^\text{#1}}}
\newcommand{\vmaps}{\ensuremath{\{\mathbf{V}_i \,|\, i \in \{1,2,3\}\}}}
\newcommand{\fmaps}{\ensuremath{\{F_i \,|\, i \in \{1,2,3\}\}}}
\newcommand{\tmaps}{\ensuremath{\{T_j \,|\, j \in \{1,\ldots,5\}\}}}
\newcommand{\diffunits}{\ensuremath{\text{mm}^2\text{s}^{\text{-1}}}}

\begin{document}
\onecolumn
\firstpage{1}

\title[Supplementary Material]{\helvetica{Supplementary Material to: Diffantom}} %Please insert the title of your article here

\author[\firstAuthorLast ]{\Authors} %This field will be automatically populated
\correspondance{} %This field will be automatically populated

\extraAuth{}% If there are more than 1 corresponding author, comment this line and uncomment the next one.
%\extraAuth{Corresponding Author2: email2@uni2.edu}

\maketitle

\section*{Appendix}\label{sec:appendix}
Let $\{T'_j\}$ be the set of original fractions maps obtained with \path{act_anat_prepare_fsl}, a
  tool in \emph{MRTrix} that combines \gls*{fast} and \gls*{first}
  to generate the macrostructural 5TT map.
FA denotes the \gls*{fa} map obtained from the original \gls*{dmri} data: the local fiber orientation maps
  $\{\mathbf{V}_i\}$ with their estimated volume fractions $\{F'_i\}$ calculated with \gls*{bedpostx}.
The final $\{T_j\}$ maps of isotropic fractions are computed as follows:
  \begin{align*}
  T_1 &= (1.0-f_{cgm}) \cdot T'_1 \\
  T_2 &= (1.0-f_{dgm}) \cdot T'_2 \\
  T_3 &= (1.0-f_{wm}) \cdot T'_3 \\
  T_4 &= T'_4 \\
  T_5 &= 0.0
  \end{align*}
where $f_{\{cgm, dgm, wm\}}$ are the fractions of restricted diffusion for each tissue.
\cite{sepehrband_brain_2015} found out that the fiber fraction ranges across the corpus
  callosum from the 70$\pm$8\% in its body to an upper bound of 80$\pm$11\% in the splenium.
Therefore, we choose $f_{wm} =$ 80\% as default fraction of restricted diffusion in the
  \gls*{wm}.
To our knowledge, restricted diffusion fractions have been studied only for \gls*{wm}.
Therefore, we set $f_{cgm} =$ 25\% and $f_{dgm} =$ 50\% as they yield plausible \gls*{fa}
  and \gls*{adc} maps, assessed visually.
The final $\{F_i\}$ maps are computed as follows:
\begin{align*}
F_1 &= f_{wm} \cdot T_2 \cdot \text{FA} + w_{f1} (f_{cgm} \cdot T_1 + f_{dgm} \cdot T_2) \\
F_2 &= f_{wm} \cdot T_2 - (F_1 + F_3) + w_{f2} (f_{cgm} \cdot T_1 + f_{dgm} \cdot T_2) \\
F_3 &= f_{wm} \cdot F'_3 + w_{f3} (f_{cgm} \cdot T_1 + f_{dgm} \cdot T_2)
\end{align*}
where $w_{\{f1, f2, f3\}}$ are the contributions of the \gls*{gm} compartments to each fiber population.
By default: $w_{f1} = $ 48\%, $w_{f2} = $ 37\%, $w_{f3} = $ 15\%.
Finally, the resulting maps are normalized to fulfill $\sum_j T_j + \sum_i F_i = 1.0$.

\newpage
\bibliographystyle{frontiers/frontiersinSCNS_ENG_HUMS}
%\bibliographystyle{frontiersinHLTH&FPHY} % for Health and Physics articles
\bibliography{Remote}

\end{document}

% \documentclass[english]{frontiers/frontiers_suppmat} % for Science, Engineering and Humanities and Social Sciences articles
% \usepackage{xifthen}
% \newboolean{review}
% \setboolean{review}{true}

% \usepackage[mode=buildnew]{standalone}
% \usepackage{tikz}
% \usepackage[framemethod=TikZ]{mdframed}
% %\setcitestyle{square}
% \usepackage{url,microtype}
% \usepackage[right]{lineno}
% \usepackage[toc,nomain,acronym,shortcuts,translate=false]{glossaries}
% \usepackage[colorlinks=true,linkcolor=black, citecolor=black!80, urlcolor=black!80]{hyperref}
% \usepackage{doi}
% \usepackage[onehalfspacing]{setspace}

% \def\keyFont{\fontsize{7}{9}\helveticabold }

% \begin{document}
% \firstpage{1}
% \title[Diffantom]{Diffantom: whole-brain diffusion MRI phantoms derived from real datasets of the \acrlong{hcp}}
% \maketitle

% \end{document}


\documentclass[aspectratio=169]{beamer}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{amsmath,amsfonts,amsthm,bm}
\usepackage{color}
\usepackage{minted}
\usepackage{graphicx} % Allows including images
\usepackage{booktabs} % Allows the use of \toprule, \midrule and \bottomrule in tables
\usepackage{tikz}
\usepackage[version=3]{mhchem}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\setminted{fontsize=\scriptsize}

\hypersetup{
    colorlinks=true,
    linkcolor=red,
    filecolor=magenta,
    urlcolor=red,
}

\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator*{\argmin}{argmin}
\let \vec \mathbf

\mode<presentation> {
    \usetheme{CambridgeUS}
    %\setbeamertemplate{footline} % To remove the footer line in all slides uncomment this line
    \setbeamertemplate{footline}[page number] % To replace the footer line in all slides with a simple slide count uncomment this line
    \setbeamertemplate{navigation symbols}{} % To remove the navigation symbols from the bottom of all slides uncomment this line
}

\title[Unsupervised Learning]{Unsupervised Learning}

\author{Shyue Ping Ong}
\institute[UCSD]{University of California, San Diego\\
\medskip
}
\date{NANO281} % Date, can be changed to a custom date

\begin{document}

\begin{frame}
    \titlepage % Print the title page as the first slide
\end{frame}

\begin{frame}{Overview}
    \tableofcontents
\end{frame}

\section{Preliminaries}

\begin{frame}{Preliminaries}
    \begin{itemize}
        \item Here, we will take a digression from the \textbf{supervised learning} that we have focused on so far, and go into \textbf{unsupervised learning}.
        \item In supervised learning, model development is carried out with a set of input/output examples (training data).
        \item In unsupervised learning, the goal is to infer the properties of a set of data (e.g., its distribution) without training examples.
        \item We will include dimensionality reduction techniques within the umbrella of unsupervised learning.
    \end{itemize}
\end{frame}

\begin{frame}
\frametitle{Supervised vs Unsupervised Learning}
\begin{columns}
\column{0.5\textwidth}
Supervised learning
\begin{itemize}
    \item Learn from example inputs and outputs (labels).
    \item Clear metrics of success (e.g., maximum likelihood, MSE, MAE, etc.)
    \item Computationally efficient.
\end{itemize}
\column{0.5\textwidth}
Unsupervised Learning
\begin{itemize}
    \item Learn only from inputs.
    \item No rigorously-defined metric of success.
    \item Computationally complex.
\end{itemize}

\end{columns}
\end{frame}

\section{Principal Component Analysis}

\begin{frame}{Principal Component Analysis (PCA)}
    \begin{itemize}
        \item Briefly alluded to in lecture on Linear Methods (regressing on derived input directions).
        \item Consider a dataset that has dimension $p$. The principal components provide a sequence of best linear approximations to that data, of all ranks $q \leq p$.
        \item Let the observations be $\vec{x_1}, \vec{x_2}, ..., \vec{x_N}$. The rank $q$ linear model for representing this data is given by:
        \begin{equation*}
            f(\vec{\lambda}) = \vec{\mu} + \vec{V_q}\vec{\lambda}
        \end{equation*}
        \item $\vec{\mu}$ is a location vector, $\vec{V_q}$ is a $p \times q$ matrix with $q$ orthogonal vectors, $\lambda$ is a length $q$ vector of parameters.
        \item We want to minimize the ``reconstruction error'',
        \begin{equation*}
            \min_{\vec{\mu}, \vec{V_q}, \vec{\lambda}} \sum_{i=1}^N ||\vec{x_i} - \vec{\mu} - \vec{V_q}\vec{\lambda_i}||^2
        \end{equation*}
    \end{itemize}
\end{frame}

\begin{frame}{Solution}
    \begin{itemize}
        \item Minimizing wrt to $\vec{\mu}$ and $\vec{\lambda_i}$ gives
        \begin{eqnarray*}
        \hat{\vec{\mu}} & = & \bar{\vec{x}}\\
        \vec{\lambda_i} & = & \vec{V_q}^T(\vec{x_i}-\bar{\vec{x}})\\
    \end{eqnarray*}
        \item Need to solve:
        \begin{equation*}
          \min_{\vec{V_q}} \sum_{i=1}^N ||(\vec{x_i}-\bar{\vec{x}}) - \vec{V_q}\vec{V_q}^T(\vec{x_i}-\bar{\vec{x}})||^2
        \end{equation*}
    \end{itemize}
\end{frame}

\begin{frame}{Solution, contd.}
    \begin{itemize}
        \item Construct singular value decomposition (SVD) of $\vec{X}$, the $N \times p$ matrix of centered $\vec{x_i}$.
        \begin{equation*}
            \vec{X} = \vec{U}\vec{D}\vec{V}^T
        \end{equation*}
        \item $\vec{U}$ is an $N \times p$ orthogonal matrix, $\vec{D}$ is a $p\times p $ diagonal matrix with singular values $d_1 > d_2 > ... > d_p$ and $\vec{V}$ is $p \times p$ orthogonal matrix with right singular vectors $\vec{v_1}, \vec{v_2}, ...\vec{v_p}$ as columns.
        \item $\vec{UD}$ are the principal components.
        \item $\vec{Xv_1}$ has highest variance among all linear combination of features, followed by $\vec{Xv_2}$, $\vec{Xv_3}$, etc.
    \end{itemize}
\end{frame}

\begin{frame}{Linear regression of bulk modulus on first two PCAs of elemental features}
\begin{figure}
    \centering
    \includegraphics[width=0.47\textwidth]{figures/pca-elements.png}
    \includegraphics[width=0.45\textwidth]{figures/pca-regression.png}
\end{figure}
\end{frame}

\begin{frame}[fragile]{Code}
\inputminted{python}{example_sklearn_pca.py}
\end{frame}

\begin{frame}{Example of linear regression on PCA components}
\begin{figure}
    \centering
    \includegraphics[width=0.45\textwidth]{figures/pca-regression.png}
\end{figure}
\end{frame}

\begin{frame}{Extensions to PCA}
    \begin{itemize}
        \item Principal curves: smooth 1D curved approximation to data.
        \item Principal surfaces: curved 2D manifold approximation to data.
    \end{itemize}
\end{frame}

\section{Cluster Analysis}

\begin{frame}{Cluster Analysis}
    \begin{itemize}
        \item Cluster observations into groups so that pairwise differences within cluster tend to be smaller than differences between clusters.
        \begin{description}
        \item[Combinatorial algorithms] Model observed data with no underlying probability model.
        \item[Mixture modeling] Assumes samples are i.i.d. from some population with a probability density function.
        \item[Mode seekers] Estimate modes from PDF.
        \end{description}
    \end{itemize}
\end{frame}

\begin{frame}{K-means}
    \begin{itemize}
        \item One of the most popular iterative descent clustering methods.
        \item Often used with the Euclidean distance as the dissimlarity measure.
        \begin{equation*}
            d(x_i, x_i') = ||x_i - x_i'||^2
        \end{equation*}
        \item Classic k-means measure distance to centroids of clusters.
        \item Other distance metrics are possible: weighted Euclidean, periodic boundary condition distance, etc.
        \item Variants:
        \begin{itemize}
            \item $K$-medoids: Use one of points as cluster center instead of centroid. Removes influence of large outliers that produce large distances.
        \end{itemize}
    \end{itemize}
\end{frame}

\begin{frame}{K-means Algorithm}
    \begin{enumerate}
            \item Initialize a set of $k$ means (centroids), e.g., choosing $k$ observations to be the initial means (Forgy algo) or randomly assigns a cluster to each observation (random partition).
            \item Assign each observation to the cluster with the smallest distance, i.e., partition the observations using the Voronoi diagram generated by means.
            \item Recalculate the new means of the observations in the new clusters.
            \item Algorithm is converged when assignment no longer changes.
        \end{enumerate}
        \begin{figure}
            \includegraphics[width=0.18\textwidth]{figures/k-means-step-1.png}
            \includegraphics[width=0.18\textwidth]{figures/k-means-step-2.png}
            \includegraphics[width=0.18\textwidth]{figures/k-means-step-3.png}
            \includegraphics[width=0.18\textwidth]{figures/k-means-step-4.png}
            \caption{Four steps of K-means algorithm. Source: Wikipedia}
        \end{figure}
\end{frame}

\begin{frame}{Practical considerations}
    \begin{itemize}
        \item K-means is often used for vector quantization, i.e., determining
        \item Determining $K$. Sometimes $K$ is based on goals, e.g., if you have a scientific / practical reason for having $K$ clusters, e.g., only $K$ instruments available or you want to bin the compounds in $K$ chemical classes.
        \item Gap analysis:
        \begin{figure}
            \centering
            \includegraphics[width=0.5\textwidth]{figures/kmeans-gap-analysis.pdf}
        \end{figure}
    \end{itemize}
\end{frame}

\begin{frame}{Hierarchical Clustering}
    \begin{itemize}
        \item Does not require specific of number of clusters.
        \item Require dissimilarity measure.
        \item Produce hierarchical representations in which the clusters at each level of the hierarchy are created by merging clusters at the next lower level.
        \item Two paradigms: agglomerative (bottom-up merging) and divisive (top-down splitting).
        \item Typically shown in a dendrogram.
    \end{itemize}
\end{frame}

\begin{frame}{Recent application: Lithium Superionic Conductors}
\begin{figure}
    \centering
    \includegraphics[width=0.8\textwidth]{figures/hirerachicalclustering-superionicconductors.pdf}
    \caption{a. Dendrogram generated using the agglomerative hierarchical clustering method. The dashed line shows the position where all compounds are partitioned into seven groups, marked as I–VII from left to right and distinguished by different colors. b Mapping the dendrogram to the conductivity reveals the grouping of known solid-state Li-ion conductors in group V and VI. Reproduced from \cite{zhangUnsupervisedDiscoverySolidstate2019}.}
\end{figure}
\end{frame}

\begin{frame}{Density-based Clustering}
    \begin{itemize}
        \item Clusters are defined as areas of higher density.
        \item Most popular variant is Density-based spatial clustering of applications with noise (DBSCAN).\cite{esterDensityBasedAlgorithmDiscovering}
        \item DBSCAN groups together points that are closely packed together and marks points that lie alone in low-density regions as outliers.
        \item Key parameters of the DBSCAN algorithm are:
        \begin{itemize}
            \item eps: Max distance between two samples for one to be considered as in the neighborhood of the other.
            \item min\_samples: No. of samples in a neighborhood for a point to be considered as a core point.
        \end{itemize}
    \end{itemize}
\end{frame}

\begin{frame}[fragile]{K-means and DBSCAN in scikit-learn}
\inputminted{python}{example_sklearn_clustering.py}
\end{frame}

\begin{frame}[allowframebreaks]{Bibliography}
    \bibliographystyle{unsrt}
    \bibliography{refs}
\end{frame}

\begin{frame}
    \Huge{\centerline{The End}}
\end{frame}

\end{document}


\chapter{Sound}

When you set off a firecracker, it makes sound.

Let's break that down a little more: Inside the cardboard wrapper of
the firecracker, there is potassium nitrate ($KNO_3$), sulfer ($S$),
and carbon($C$).  These are all solids. When you trigger the chemical
reactions with a little heat, these atoms rearrange themselves to be
potassium carbonate ($K_2CO_3$), potassium sulfate ($K_2SO_4$), carbon
dioxide ($CO_2$), and nitrogen ($N_2$). Note that the last two are
gasses.

The molecules of a solid are much more tightly packed than the
molecules of a gas. So after the chemical reaction, the molecules
expand to fill a much bigger volume. The air molecules nearby get
pushed away from the firecracker.  They compress the molecules beyond
them, and those compress the molecules beyond them.

This compression wave radiates out as a sphere; its radius growing at
about 343 meters per second (``The speed of sound'').

The energy of the explosion is distributed around the surface of this
sphere. As the radius increases, the energy is spread more and more
thinly around. This is why the firecracker seems louder when you are
closer to it. (If you set off a firecracker in a sewer pipe, the sound
will travel much, much farther.)

This compression wave will bounce off of hard surfaces. If you set off
a firecracker 50 meters from a big wall, you will hear the explosion
twice. We call the second one ``an echo.''

The compression wave will be absorbed by soft surfaces. If you covered
that wall with pillows, there would be almost no echo.

The study of how these compression waves move and bounce is called
\newterm{accoustics}. Before you build a concert hall, you hire an
accoustician to look at your plans and tell you how to make it sound
better.

\section{Pitch and frequency}

The string on a guitar is very similar to the weighted spring
example. The farther the string is displaced, the more force it feels
pushing it back to equilibrium. Thus, it moves back and forth in a
sine wave. (OK, it isn't a pure sine wave, but we will get to that later.)

The string is connected to the center of the boxy part of the guitar,
which is pushed and pulled by the string. That creates compression
waves in the air around it.

If you are in the room with the guitar, those compression waves enter
your ear, push and pull your ear drum, which is attached to bones that
move a fluid that tickles tiny hairs, called \newterm{cilia} in your
inner ear. That is how you hear.

We sometimes see plots of sound waveforms.  The $x$-axis represents
time. The $y$-axis represents the amount the air is compressed at the
microphone that converted the air pressure into an electrical signal.

\includegraphics[width=0.8\linewidth]{soundwave.png}

If the guitar string is made tighter (by the tuning pegs) or shorter
(by the guitarist's fingers on the strings), the string vibrates more
times per second.  We measure the number of waves per second and we
call it the \newterm{frequency} of the tone. The unit for frequency is
\newterm{Hertz}: cycles per second.

Musicans have given the different frequencies names. If the guitarist
plucks the lowest note on his guitar, it will vibrate at 82.4
Hertz. The guitarist will say ``That pitch is low E.'' If the string is made
half as long (by a finger on the 12th fret), the frequency will be
twice as fast (164.8 Hertz), and the guitarist will say ``That is E an
octave up.''

For any note, the note that has twice the frequency is one octave
up. The note that has half the frequency is one octave down.

The octave is a very big jump in pitch, so musicians break it up into
12 smaller steps. If the guitarist shortens the E string by one fret,
the frequency will be $82.4 \times 1.059463 \approx 87.3$ Hertz.

Shortening the string one fret always increases the frequency by a factor of 1.059463. Why?

Because $1.059463^12 = 2$. That is, if you take 12 of these hops, you
end up an octave higher.

This, the smallest hop in western music, is referred to as \newterm{half step}.

\begin{Exercise}[title={Notes and frequecies}, label=note_to_frequency]

The note A near the middle of the piano, is 440Hz. The note E is 7 half steps above A.  What is its frequency?

\end{Exercise}
\begin{Answer}[ref=note_to_frequency]

  A is 440 Hz.  Each half-step is a multiplication by $\sqrt[12]{2} = 1.059463094359295$
  So the frequency of E is $(440)(2^{7/12}) = 659.255113825739859$

\end{Answer}

\section{Chords and harmonics}

Of course, a guitarist seldom plays only one string at a
time. Instead, he uses the frets to pick a pitch for each string and
strums all six strings.

Some combinations of frequencies sound better than others. We have
already talked about the octave: if one string vibrates twice for each
vibration of another, they sound sweet together.

Musicians speak of ``the fifth''.  If one string vibrates three times
and the other vibrates twice in the same amount of time, they sound
sweet together.

If one string vibrates 4 times while the other vibrates 3 times, they
sound sweet together. Musicians call this ``the third.''

Each of these different frequencies tickle different cilia in the
inner ear, so you are able to hear all six notes at the same time when
the guitarist strums his guitar.

When a string vibrates, it doesn't create a single sine wave. Yes, the
string vibrates from end-to-end and this generates a sine wave at what
we call \newterm{the fundmental frequency}.However, there are also
``standing waves'' on the string. One of these standing waves, is
still at the centerpoint of the string, but everything to the left of
the centerpoint is going up when everything to the right is going
down. This creates \newterm{an overtone} that is twice the frequency
of the fundamental.

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]

  \draw[dashed,draw=black,
      domain=-0:6.283,samples=300,variable=\x]
      plot (\x,{0.7 * sin(deg{\x}/2)});
  \draw[thick,draw=black,
      domain=0:6.283,samples=300,variable=\x]
  plot (\x,{0.7 * sin(deg{-1 * \x}/2)});

  \draw[dashed,draw=black,
      domain=-0:6.283,samples=300,variable=\x]
      plot (\x,{0.2 * sin(deg{\x})});
  \draw[thick,draw=black,
      domain=0:6.283,samples=300,variable=\x]
      plot (\x,{0.2 * sin(deg{-1 * \x})});

 \filldraw[black] (0, 0)  circle(3pt);
 \filldraw[black] (6.283, 0)  circle(3pt);

\end{tikzpicture}

The next overtone has two still points -- it divides the string into
three parts.  The outer parts are up while the inner part is
down. It's frequency is three times the fundamental frequency.

\begin{tikzpicture}[
tl/.style = {% tick labels
    fill=white, inner sep=1pt, font=\scriptsize,
            },                        ]

  \draw[dashed,draw=black,
      domain=-0:6.283,samples=300,variable=\x]
      plot (\x,{0.7 * sin(deg{\x}/2)});
  \draw[thick,draw=black,
      domain=0:6.283,samples=300,variable=\x]
  plot (\x,{0.7 * sin(deg{-1 * \x}/2)});

  \draw[dashed,draw=black,
      domain=-0:6.283,samples=300,variable=\x]
      plot (\x,{0.2 * sin(1.5 * deg{\x})});
  \draw[thick,draw=black,
      domain=0:6.283,samples=300,variable=\x]
      plot (\x,{0.2 * sin(1.5 * deg{-1 * \x})});

 \filldraw[black] (0, 0)  circle(3pt);
 \filldraw[black] (6.283, 0)  circle(3pt);

\end{tikzpicture}

And so on: 4 times the fundamental, 5 times the fundamental, etc.

In general, tones with a lot of overtones tend to sound bright. Tones
with just the fundamental sound thin.

Humans can generally hear frequencies from 20Hz to 20,000Hz (or
20kHz).  Young people tend to be able to hear very high sounds better
than older people.

Dogs can generally hear sounds in the 65Hz to 45kHz range.

\section{Making waves in Python}

Let's make a sine wave and add some overtones to it.  Create a file \filename{harmonics.py}

\begin{Verbatim}
import matplotlib.pyplot as plt
import math

# Constants: frequency and amplitude
fundamental_freq = 440.0 # A = 440 Hz
fundamental_amp = 2.0

# Up an octave
first_freq = fundamental_freq * 2.0 # Hz
first_amp = fundamental_amp * 0.5

# Up a fifth more
second_freq = fundamental_freq * 3.0 # Hz
second_amp = fundamental_amp * 0.4

# How much time to show
max_time = 0.0092 # seconds

# Calculate the values 10,000 times per second
time_step = 0.00001 # seconds

# Initialize
time = 0.0
times = []
totals = []
fundamentals = []
firsts = []
seconds = []

while time <= max_time:
    # Store the time
    times.append(time)

    # Compute value each harmonic
    fundamental = fundamental_amp * math.sin(2.0 * math.pi * fundamental_freq * time)
    first = first_amp * math.sin(2.0 * math.pi * first_freq * time)
    second = second_amp * math.sin(2.0 * math.pi * second_freq * time)

    # Sum them up
    total = fundamental + first + second

    # Store the values
    fundamentals.append(fundamental)
    firsts.append(first)
    seconds.append(second)
    totals.append(total)

    # Increment time
    time += time_step

# Plot the data
fig, ax = plt.subplots(2, 1)

# Show each component
ax[0].plot(times, fundamentals)
ax[0].plot(times, firsts)
ax[0].plot(times, seconds)
ax[0].legend()

# Show the totals
ax[1].plot(times, totals)
ax[1].set_xlabel("Time (s)")

plt.show()
\end{Verbatim}

When you run it, you should see a plot of all three sine waves and another plot of their sum:

\includegraphics[width=0.9\linewidth]{harmonicspy.png}

\subsection{Making a sound file}

The graph is pretty to look at, but make a file that we can listen to.

The WAV audio file format is supported on pretty much any device, and
a library for writing WAV files comes with Python.  Lets write some
sine waves and some noise into a WAV file.

Create a file called \filename{soundmaker.py}

\begin{Verbatim}
import wave
import math
import random

# Constants
frame_rate = 16000 # samples per second
duration_per = 0.3 # seconds per sound
frequencies = [220, 440, 880, 392] # Hz
amplitudes = [20, 125]
baseline = 127 # Values will be between 0 and 255, so 127 is the baseline
samples_per = int(frame_rate * duration_per) # number of samples per sound

# Open a file
wave_writer = wave.open('sound.wav', 'wb')

# Not stereo, just one channel
wave_writer.setnchannels(1)

# 1 byte audio means everything is in the range 0 to 255
wave_writer.setsampwidth(1)

# Set the frame rate
wave_writer.setframerate(frame_rate)

# Loop over the amplitudes and frequencies
for amplitude in amplitudes:
    for frequency in frequencies:
        time = 0.0
        # Write a sine wave
        for sample in range(samples_per):
            s = baseline + int(amplitude * math.sin(2.0 * math.pi * frequency * time))
            wave_writer.writeframes(bytes([s]))
            time += 1.0 / frame_rate

        # Write some noise after each sine wave
        for sample in range(samples_per):
            s = baseline + random.randint(0, 15)
            wave_writer.writeframes(bytes([s]))

# Close the file
wave_writer.close()
\end{Verbatim}

When you run it, it should create a sound file with several tones of
different frequencies and volumes. Each tone should be followed by
some noise.


% declare document class and geometry
%\documentclass[12pt]{article} % use larger type; default would be 10pt
%\usepackage[margin=1in]{geometry} % handle page geometry

\input{../plaheader.tex}

\title{Astro 270 -- Astrophysical Dynamics -- The Orbits of Stars}
\author{UCLA, Fall 2014}
%\date{\formatdate{02}{10}{2014}} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed
%\date{\formatdate{07}{10}{2014}}

\begin{document}
\setlength{\unitlength}{1mm}
\maketitle

\section{Orbits in static spherical potentials}
Our fundamental equation in this chapters are the equations of motion which are derived from the Lagrangian
\begin{equation}
L = r^2 \dot{\phi}
\end{equation}
Our equation of motion is:
\begin{equation}
u^2 + \frac{2[\Phi (1/u) - E]}{L^2} = 0
\end{equation}
where u is 1/r

The time that it takes a star to go from apocenter to pericenter back
to apocenter is defined as the \ref{radial period}. Since this is a
spherical symmetric potential, pericenter and apocenter are always the
same for any orbit, though it may precess. The azimuthal period is the
amount of time a particle will take to complete one orbit (traverse
2$\pi$ of $\phi$ if it travelled at the average speed. This will only
be a rational number if the orbit is closed, i.e. the beginning of an
orbit ends at the same position. There are two cases where all bound
orbits are closed. Note that \textit{all} potentials have at least one
orbit that is bound - that of a circular orbit.
\subsection{Examples of Spherical Potentials}
\subsubsection{Spherical Harmonic Oscillator}
In this case, the radial period is $\pi /\Omega$. In order, the
particle only needs to traverse half of one orbit to reach back to the
same distance. This is because all of the orbits are ellipses centered
on the center of the potential. This is in contrast to the Kepler
potential which has radial period of $2\pi / \Omega$, where the
ellipse has its \textit{focus} at the center of the potential

\subsubsection{Kepler Potential}
\subsubsection{Isochrone Potential}
\subsubsection{Hyperbolic Encounters}

\subsection{Constants and integrals of motion}
A \textbf{constant of motion} is a quantity that
\begin{equation}
C(\v{x}(t_1) \v{v}(t_1); t_1) = C(\v x(t_2) \v{v}(t_2); t_2)
\end{equation}
This is different from an integral of motion because you need an
initial condition in order to calculate one of these quantities,
i.e. you need to know the value at some particular time in order to be
able to get it at a different time. Generally any initial conditions
or boundary conditions can be considered constants of
motion. Therefore, any orbit, which has six coordinates in phase space
has six constants of motion. From the current time and the six coordinates and
phase space it is possible to determine the position in phase space.

Integrals of motion are determined solely by the potential, and are
invariant in time. They limit the amount of phase space that a
particle can inhabit. Thus the number of integrals of motion describe
how many coordinates are required to specify the location in phase
space. In other words, having n integrals of motion means that any
particle's orbit will lie on a 6-n dimensional subspace of x. A
non-isolating integral is one that does not limit the number of
dimensions that the orbit can lie on. They're fucking useless.

\section{Orbits in Axisymmetric Potentials}
Effective potential
\begin{equation}
\Phi_{eff} = \Phi(R, z) + \frac{L_z^2}{2R^2}
\end{equation}

\end{document}


% !TEX root = ../thesis.tex

\chapter{Motivation}
% \label{cha:eva_motivation}
The most common method to reach degeneracy in a gas of ultracold atoms is by using evaporative cooling. Evaporative cooling works by selectively removing atoms from the trapped gas that are \enquote{hot} compared to the rest of the sample. The remaining atoms then equilibrate and the new equilibrium temperature is lower. This is commonly also described as cutting off the tail of the Maxwell-Boltzmann distribution.

In many experiments, evaporative cooling is done in a magnetic trap, where an RF signal can be used to selectively pump atoms into an untrapped state and therefore expel them from the trap \cite{PhysRevLett.74.3352}. However, magnetic traps have the disadvantage that magnetic offset fields cannot be used as easily, for example to tune the scattering length using a Feshbach resonance \cite{PhysRevA.71.011602}.

Evaporation can also be done in an optical dipole trap by lowering the laser intensity~\cite{Chaudhuri_2007}. The disadvantage of optical evaporation compared to magnetic evaporation is that lowering the laser intensity is intrinsically coupled to a decrease in confinement strength for the optical trap \cite{PhysRevA.79.061406}. This leads to a reduction in density and hence longer thermalisation times, resulting in less efficient evaporative cooling.\\[\baselineskip]
\noindent
Our research group is interested in Bose gases with uniform density distributions. To achieve this, a box potential is necessary. However, evaporative cooling in a stationary box potential is generally unfavourable as will be shown later. Therefore, evaporation is usually carried out in a different trap first and the atomic cloud is then transferred to a box potential. Transferring the cloud is accompanied with heating and atom loss \cite{PhysRevLett.110.200406}.

Our proposed solution is shown in \cref{fig:evap_box_sketch}. The cloud is trapped in a box potential immediately after a MOT and a first sub-Doppler cooling stage (possibly grey molasses cooling \cite{Rosi2018enhancedGM}). Forced evaporation is then carried out by lowering the trap depth as usual. Simultaneously, the box is compressed to dynamically control the density and therefore the elastic collision rate. This would give us the opportunity to avoid one transfer step, which would help us to reduce the cycle time of the experiment.

In order to probe the feasibility of such a system without having to build an entire experimental setup, a numerical simulation is performed. The following chapters describe the process of programming this simulation and show how this concept compares to traditional evaporative cooling processes.
\vfill
\begin{figure}[htbp]
    \centering
    \input{TexContents/Figures/Evap/BoxTrapEvaporation/BoxEvaporation.tikz}
    \caption[Evaporative cooling in a compressing box potential]{As atoms leave the trap, the box is compressed to control the density. Note: The graphs for the temperature and phase space density are for illustrative purposes only. It is desired that as the temperature decreases, the phase space density increases.}
    \label{fig:evap_box_sketch}
\end{figure}
\vfill