%
% vecmat3.tex - LaTeX documentation source for vecmat3.h version 2.3.1:
%               Fast 3d vector and matrix classes using expression templates
%
% Copyright (c) 2007-2013  Ramses van Zon
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
% THE SOFTWARE.
%

\documentclass[12pt,twoside]{article}

\usepackage{a4wide,fancyhdr,mathptm,charter,color}
%\usepackage[colorlinks]{hyperref}

\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries \hfill\rightmark\hfill}
\fancyhead[RE]{\hfill\bfseries\rightmark \hfill}
\fancyfoot[L]{\textit{vecmat3} v2.3.1}
\fancyfoot[R]{\hfill{\color{white}.}\hfill Ramses van Zon\hfill May 15, 2013}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{2.5pt}
\fancypagestyle{plain}{\fancyhead{}\renewcommand{\headrulewidth}{0pt}}

\newcommand{\Vector}{{Vector}}
\newcommand{\Matrix}{{Matrix}}
\newcommand{\cxx}{C\texttt{++}}
\newcommand{\TT}{{\tt<}T{\tt>}}

\begin{document}

\setlength{\parskip}{1mm}

\title{User Documentation for the  \emph{vecmat3}\\\Vector\ and
  \Matrix\ classes\\(version 2.3.1)}

\author{Ramses van Zon\footnote{vanzonr@gmail.com}}

\date{May 15, 2013}

\maketitle
\begin{abstract}
This document describes how to use the \cxx\ \Vector\ and
\Matrix\ classes defined in the header file vecmat3.h. These classes
offer a very convenient notation for three dimensional vector and
matrix algebra by using overloaded operators.  Furthermore, they are
constructed to be numerically efficient through the internal use of
expression templates, without interfering at the user level.  As a
result, one can convert mathematical matrix-vector expressions
straightforwardly into the corresponding \cxx\ expression without
having to worry about incurring an efficiency penalty.
\end{abstract}

\newpage

\renewcommand{\contentsname}{\vspace{-13mm}}\markright{Contents}
\tableofcontents

\newpage

\section{Introduction}
Vectors and matrices are used frequently in scientific computation (as
well as in modeling, games and movie rendering).  Unfortunately, no
built-in support for matrices and vectors exists in \cxx.  In
principle, expressions involving matrices and vectors, such as
\[
\vec a=\vec b+\mathsf M \cdot \vec c
\]
(with $\vec a$, $\vec b$ and $\vec c$ vectors and $\mathsf M$ a
matrix) can be implemented in \cxx\
such that they strongly resemble their mathematical notations, e.g.
\begin{quote}
\tt
Vector a,b;

Matrix M;

Vector c = a + M*b;
\end{quote}
The technique used to accomplish such notational convenience is
operator overloading, whose straightforward implementation comes with
a high computational cost due to the creation of temporary
objects.

A more efficient implementation is possible by using \emph{expression
templates}.  Efficient matrix-vector implementations are somewhat of a
by-product of \cxx\ templates, and this shows in the awkward and
complicated notation needed for general matrix-vector
manipulations. In addition, the \cxx\ standard is somewhat quirky on
what is and is not allowed when using templates.

These notational issues probably explain why there are far fewer
template-based implementations of matrices and vectors available.
This is especially problematic for small vectors and matrices of fixed
size, for instance three-dimensional ones. These allow additional
efficiency gains over general-size vectors and matrices (because loops
over indices can be replaced by explicit sums in the implementation).
Two known implementations are the \texttt{TinyVector} and \texttt{
TinyMatrix} classes of \texttt{Blitz++} and the ones by the same name of
\texttt{tvmet}. The former is not very developed, i.e., many operations
that one would like to have are not present, and indeed, the latter is
aimed at fixing that. Still, \texttt{tvmet} lacks some functionality that
built-in types do have, for instance, \texttt{TinyVector<3,double> v =
a+b;} is not possible.

This is where \emph{vecmat3} comes in. It defines very efficient
three dimensional vector and matrix manipulations.  The aim is to be
able to use these vectors and matrices as if they were built-in types,
with which the same kind of expressions can be formed as can with
built-in types without worrying about template techniques, but also without
substantial losses compared to hard-coded element-by-element
techniques.

\emph{vecmat3} uses expression templates and operator
overloading. The restrictions of vecmat3 at present are that the
elements of the vectors and matrices have to be of a single type,
which is \texttt{double} by default, and that only three-dimensional
quantities are supported (as the name suggests).

\section*{Change history}
\subsection*{Changes in version 2}

The main difference between the first version of vecmat3 and the
second is that the matrices and vectors no longer need to be all of
one type in a single application.  In addition to the standard Vector
and Matrix classes of type \texttt{DOUBLE} (which defaults to double)
as in version 1, in version 2 one also has three-dimensional
structures of any type \texttt{T} at one's disposal. To be more
precise:
\begin{itemize}
\item \texttt{vecmat3::Vector\TT{}} and \texttt{vecmat3::Matrix\TT{}} are
  three-dimensional vector and matrix classes whose elements are of
  type \texttt{T}.

  For example, one can define a 3x3 matrix of integers as\ \
  \texttt{vecmat3::Matrix<int>~m;}
\item This notation reflects two changes in the code:
\begin{itemize}
\item Almost all of the \texttt{vecmat3} code is now contained in its own
  namespace called \texttt{vecmat3}.
\item The type is a template argument.
\end{itemize}
\item In version 2 the standard Vector and Matrix classes are simply
  typedef'ed as equivalent to \texttt{vecmat3::Vector<DOUBLE>} and
  \texttt{vecmat3::Vector<DOUBLE>}, whereas in version 1 they were the
  only vectors and matrices available.
\item The typedef's of Vector and Matrix will be omitted if the
  compiler flag \texttt{NOVECMAT3DEF} is defined.
\end{itemize}

\subsection*{Changes between version 2 and version 2.3}

(Versions 2.1 and 2.2 were internal development stages.)

\begin{itemize}
\item Most functionality remained the same as in version 2, except that square bracket support has been added. See Section \ref{bracketsnowtoo}.

\item The header file now also enforces inlining the template functions for
the GNU (tested version 4.4.0) and the Intel compilers (tested
versions 11 and 12), even when no optimization is used.

\item A major improvement of version 2.3 is that the library is now
compatible with IBM's xlC compiler, which had trouble with some
template constructions in versions 1 and 2. While I'm on the
subject, one should compile with \texttt{-O4} when using the IBM
compilers with vecmat3 in order to get all inlining done properly (in
the latest version, xlC 11, the options \texttt{-O2 -qinline=level=6} suffice).
\end{itemize}

\subsection*{Changes between version 2.3 and version 2.3.1}

Version 2.3.1 comes with an open-source license (MIT). Here is the
text of the license:
\begin{verbatim}
Copyright (c) 2007-2013 Ramses van Zon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\end{verbatim}

\section{Installation}

To use this header-only library, one merely needs to copy the header
file vecmat3.h to the directory of the source files that include it,
or to a default directory where the compiler will look for header
files (e.g. /usr/local/include).

The \emph{vecmat3} library has been tested with the GNU g++ compiler version
4.4 and up, the Intel C++ compiler version 11 and up and IBM's XL C++
compiler version 10 and up.

\section{Using the \Vector\ and \Matrix\ classes}
\label{use}

\subsection{Classes}

\emph{vecmat3} provides two general template classes within the
namespace \texttt{vecmat3}:

\begin{quote}\tt
  template<typename T> vecmat3::Vector;

  template<typename T> vecmat3::Matrix;
\end{quote}
The template parameter T determines the type of the vector and
matrix elements. Thus, for a vector of integers one uses the type
\texttt{vecmat3::Vector<int>}, while for a matrix of doubles one would
use \texttt{vecmat3::Matrix<double>}.

Since applications often need only one type of vector, a default
vector type a and default matrix type are defined outside the
\texttt{vecmat3} namespace, as follows
\begin{quote}\tt

typedef vecmat3::Vector<DOUBLE> Vector;

typedef vecmat3::Matrix<DOUBLE> Matrix;
\end{quote}
Here, DOUBLE is a predefined macro that should contain the type of the
elements of the default vectors and matrices. Thus, \texttt{Vector v;}
defines a vector with elements of type DOUBLE.

The type DOUBLE can be defined in three ways:
\begin{enumerate}
\item One can write an
\texttt{\#define DOUBLE <something>} before including the
\texttt{vecmat3.h} header, with \texttt{<something>} replaced by the
desired type (e.g.\ \texttt{float} or \texttt{double});
\item One can give a command line argument to the compiler to define
  DOUBLE to be \texttt{<something>} (e.g.\ \texttt{-DDOUBLE=float} for
  g++);
\item One can do nothing, which makes \texttt{DOUBLE} default to
\texttt{double}.
\end{enumerate}

The definition of DOUBLE and the type definition of Vector and Matrix
in the global namespace does pollute the global namespace, and in many
cases is not wanted. These definitions are omitted if \texttt{NOVECMAT3DEF} is defined.

\subsection{Header file}

To use \emph{vecmat3}, the following general procedure should be
followed:

\begin{itemize}
  \item If the elements of the vectors and matrices are to have a
  different type than \texttt{double}, first \texttt{\#define} their type as
  \texttt{DOUBLE}, e.g.
  \begin{quote}
    \tt \#define DOUBLE float
  \end{quote}
  The type of the elements of a vector or matrix will be referred to
  as the ``value type'' in this documentation.
  \item Include the header file vecmat3.h:
  \begin{quote}
    \tt \#include "vecmat3.h"
  \end{quote}

  \item The class \Vector\ and the class \Matrix\ are now
  defined and instances these classes can be declared as follows:
    \begin{quote}\tt
      Vector a;

      Matrix R;
    \end{quote}
    \item One can explicitly use any other value type
      than DOUBLE type, e.g.
    \begin{quote}\tt
      vecmat3::Vector<int> a;

      vecmat3::Matrix<int> R;
    \end{quote}
    If vectors and matrices of a specific value type are used a lot in an
    application, it may be useful to typedef them to a shorter
    notation, e.g.
    \begin{quote}\tt
      typedef vecmat3::Vector<int> intVector;

      typedef vecmat3::Matrix<int> intMatrix;

      intVector b;

      intMatrix S;
    \end{quote}
  \item Alternatively, one can have no default global \Vector\ and \Matrix\ class defined, and use only the namespace vecmat3, e.g.
    \begin{quote}\tt
      \#define NOVECMAT3DEF

      \#include "vecmat3.h"

      vecmat3::Vector<double> a;

      vecmat3::Matrix<double> R;
    \end{quote}

      \item In the above examples, the elements of the vectors and
        matrices are unspecified, and likely contain garbage. In the
        next section, it will be explained how to initialize these
        elements of these
        classes.
    \item Note that currently, operations between matrices and vectors
      of different value types are not supported, even when mathematically
      this would make sense (such as for \texttt{int} and
      \texttt{double}).
      \item However, it is possible to assign any kind of number to an
        element of any value type, as long as a (standard)
        conversion to that type is known to the c++ compiler.  Thus,
        one may, for instance, assign an integer to an element of a
        vector, or one may multiply a vector by 2 (i.e., one may write
        \texttt{2*v} instead of being
        forced to write \texttt{2.0*v} or \texttt{2.0f*v}).
\end{itemize}

\subsection{Initialization methods}

There are four ways to initialize a \Vector\ or \Matrix, which we will
discuss by example.  In describing the initialization methods, the
default Vector and Matrix types will be used; the arbitrary type
versions \texttt{vecmat3::Vector\TT{}} and
\texttt{vecmat3::Matrix\TT{}} have the same functionality.

\subsubsection{Initialization through constructor parameters}
  Example:
    \begin{quote}\tt
         Vector a(1.1, 3.0, -4.3);

         Matrix R(1, 2, 3,

\ \ \ \ \ \ \ \ \ 4, 5, 6,

\ \ \ \ \ \ \ \ \ 7, 8, 9);
    \end{quote}
    defines a \Vector\ \texttt a and \Matrix\ \texttt R with specified
    elements. Note that the first set of three elements given to
    \texttt R comprise the top row of \texttt R, the second set of
    three the middle row and the last set of three the bottom row.

    If fewer than three or nine (for \Vector\ and \Matrix,
    respectively) number are given, the unspecified elements are set
    to zero. Thus, one can define a zero \Vector\ and \Matrix\ simply by
    \begin{quote}\tt
         Vector a(0);

         Matrix R(0);
    \end{quote}

  \subsubsection{Initialization through assignment}
  Example:
    \begin{quote}\tt
         Vector b = a;

         Matrix S = R;
    \end{quote}
    defines a \Vector\ \texttt b with the same elements as \texttt a,
    and a \Matrix\ \texttt S with the elements~as~\texttt R.

    The right hand sides may also be an expression involving \Vector's
    and Matrices. The allowed expressions are explained in
    sections \ref{operations} and \ref{expressions}.
%\newpage
  \subsubsection{Initialization through a comma separated list}
  Example:
    \begin{quote}  \tt
         Vector a;

	 a = 1.1, 3.0, -4.3;

         Matrix R;

	 R = 1, 2, 3,

     \ \ \ \ 4, 5, 6,

     \ \ \ \ 7, 8, 9;
    \end{quote}
    Note: this is the standard construction for \texttt{Blitz++} and
    \texttt{tvmet}, and is achieved though an overloaded comma
    operator. Not everybody likes overloading the comma operator,
    because it may confuse the user (more than the above methods), and
    it is somewhat less efficient than the method in 3.2.1.

    Furthermore, it is  not possible to use this method in
    the declaration, i.e., one cannot write \texttt{Vector
    a=1.1,3.0,-4.3;} since \cxx\ would consider this a
    declaration of 3.0 and -4.3 as being of type \Vector.

    If not enough elements are given in the list, the remaining
    elements are set to zero. Thus, one can write
    \begin{quote}\tt
         b = 1;
    \end{quote}
    to get the vector (1,0,0), and
    \begin{quote}\tt
         R = 2;
    \end{quote}
    to get the matrix $\left(\begin{array}{ccc}2&0&0\\0&0&0\\0&0&0\end{array}\right)$.

  \subsubsection{Initialization through member functions}
  Example:
    \begin{quote}\tt
         Vector a;

	 a.zero();

         Matrix R;

	 R.zero();
    \end{quote}
    also define a \Vector\ and \Matrix, respectively, with zero
    elements.

\noindent
    For a \Matrix, there also is a member function {\tt one()} to turn
    it into an identity matrix:
    \begin{quote}\tt
         Matrix S;

	 S.one();
    \end{quote}
    Furthermore, one can initialize a \Matrix\ also per row or column, e.g.
    \begin{quote}\tt
      R.setRow(0,a);

      R.setRow(1,Vector(0,2,0));

      R.setRow(2,Vector(0,2,-1));

      S.setColumn(0,a);

      S.setColumn(1,Vector(0,2,0));

      S.setColumn(2,Vector(0,2,-1));
    \end{quote}
    Note that rows and columns are numbered from 0 to 2.

 \subsubsection{Arrays}
     Example:
    \begin{quote}\tt
         Vector a[3];

         Matrix S[3];
    \end{quote}
    Defines arrays of three Vectors and Matrices which are
    non-initialized. One can initialize these arrays as follows
    \begin{quote}\tt
         Vector a[3] = \{ Vector(1,2,3), Vector(3,4,5), Vector(5,6,7) \};

         Matrix S[3] = \{ Matrix(0), Matrix(1,2,3,4,5,6,7,8,9), Matrix(2) \} ;
    \end{quote}

\subsection{Accessing the elements}
\label{bracketsnowtoo}

There are three ways to assess the elements of Vectors and Matrices:

\begin{enumerate}

\item Basic elements of Vectors and Matrices are generally accessible using the
parenthesis notation, i.e., the elements of a  \Vector\ \texttt a are
\texttt{a(0)}, \texttt{a(1)} and \texttt{a(2)}, while those of a
\Matrix\ \texttt R are \texttt{R(0,0)}, \texttt{R(0,1)}, \dots
\texttt{R(2,2)}.

\item Bracket notation can also be used,
 i.e., the elements of a  \Vector\ \texttt a are
\texttt{a[0]}, \texttt{a[1]} and \texttt{a[2]}, while those of a
\Matrix\ \texttt R are \texttt{R[0][0]}, \texttt{R[0][1]}, \dots
\texttt{R[2][2]}. Note the double brackets for matrix element access.
This way, Vector and Matrix objects act as if they are of type \texttt{T[3]} and \texttt{T[3][3]}, respectively. Accessing the matrix elements in this way may be moderately slower than the parenthesis method (depending on the compiler).

\item Another way to access the elements is though the class members
themselves, i.e., \texttt x, \texttt y and \texttt z for \Vector, and
\texttt{xx}, \texttt{xy}, \texttt{xz}, \texttt{yx}, \texttt{yy},
\texttt{yz}, \texttt{zx}, \texttt{zy} and \texttt{zz} for Matrix. This is
potentially  more efficient, but cannot be used for expressions, i.e.,
\texttt{(A+B).xx} is not possible, unless one writes \texttt{Matrix(A+B).xx}.
\end{enumerate}
Furthermore, the rows and columns of a Matrix can be used as if they
were vectors as follows
\begin{quote}\tt
  Vector v = R.row(1);

  Vector w = R.column(2);
\end{quote}

\subsection{Operators}
\label{operations}

The available algebraic operators for the Vector and Matrix classes
are summarized in table 1, in which '\Vector\TT' stands for
'\texttt{const Vector\TT\&}' or a \Vector-valued expression, and '\Matrix\TT'
stands for '\texttt{const Matrix\TT\&}' or a Matrix\TT-valued expression.

In addition, \texttt{<<} operators are defined for output of Vectors
and Matrices to \texttt{ostream}s, such that
\begin{quote}\tt
  Vector a(1,2,3);

  std::cout << a << endl;
\end{quote}
would print the numbers 1, 2 and 3 with just a space in between.
\begin{quote}\tt
  Matrix<T> M(1,2,3,4,5,6,7,8,9);

  std::cout << M << endl;
\end{quote}
would print a newline, the numbers 1, 2 and 3, another newline, the
numbers 4, 5 and 6, another newline, the numbers 7, 8 and 9 and
finally another newline.

\begin{table}[t]\small
\begin{center}
\begin{tabular}{|rcl|c|c|rcl|}
\hline
 \multicolumn{3}{|c|}{\bf form} & \bf description & \bf example & \multicolumn{3}{c|}{\bf mathematically}\\\hline\hline
%
&\texttt{\textcolor{red}-}
&\texttt{Vector<T>}
& negative
& \tt c = -a;
& $\quad \vec c$&=&$-\vec a$\\
%
\texttt{Vector<T>}
&\textcolor{red}{\texttt +}
&\texttt{Vector<T>}
& add
& \tt c = a + b;
& $\quad \vec c$&=&$\vec a +\vec b$\\
%
\texttt{Vector<T>}
&\textcolor{red}{\texttt -}
&\texttt{Vector<T>}
& subtract
& \tt c = a - b;
& $\quad \vec c$&=&$\vec a -\vec b$\\
%
\texttt{T}
&\textcolor{red}{\texttt *}
&\texttt{Vector<T>}
& multiply with scalar
& \tt c = d * a;
& $\quad \vec c$&=&$d\,\vec a$\\
%
\texttt{Vector<T>}
&\textcolor{red}{\texttt *}
&\texttt{T}
& multiply with scalar
& \tt c = a * d;
& $\quad \vec c$&=&$\vec a\, d$\\
%
\texttt{Vector<T>}
&\textcolor{red}{\texttt /}
&\texttt{T}
& divide by scalar
& \tt c = a / d;
& $\quad \vec c$&=&$\vec a / d$\\
%
\texttt{Vector<T>}
&\textcolor{red}{\texttt \^{}}
&\texttt{Vector<T>}
& cross/outer product$^\dagger$
& \tt c = a \^{} b;
& $\quad \vec c$&=&$\vec a \times \vec b$\\
%
\texttt{Vector<T>}
&\textcolor{red}{\texttt *}
&\texttt{Vector<T>}
& dot/inner product$^\ddagger$
& \tt d = a * b;
& $\quad d$&=&$\vec a \cdot \vec b$\\
%
\ \textcolor{red}( \texttt{Vector<T>}
&\textcolor{red}{\texttt |}
& \texttt{Vector<T>} \textcolor{red})
& dot/inner product$^\ddagger$
& \tt d = (a|b);
& $\quad d$&=&$\vec a \cdot \vec b$\\
%
\hline
%
&\texttt{\textcolor{red}-}
&\texttt{Matrix<T>}
& negative
& \tt T = -S;
& $\quad \mathsf T$&=&$-\mathsf S$\\
%
\texttt{Matrix<T>}
&\textcolor{red}{\texttt +}
&\texttt{Matrix<T>}
& add
& \tt T = S + R;
& $\quad \mathsf T$&=&$\mathsf S +\mathsf R$\\
%
\texttt{Matrix<T>}
&\textcolor{red}{\texttt -}
&\texttt{Matrix<T>}
& subtract
& \tt T = S - R;
& $\quad \mathsf T$&=&$\mathsf S -\mathsf R$\\
%
\texttt{T}
&\textcolor{red}{\texttt *}
&\texttt{Matrix<T>}
& multiply with scalar
& \tt T = d * S;
& $\quad \mathsf T$&=&$d\,\mathsf S$\\
%
\texttt{Matrix<T>}
&\textcolor{red}{\texttt *}
&\texttt{T}
& multiply with scalar
& \tt T = S * d;
& $\quad \mathsf T$&=&$\mathsf S\, d$\\
%
\texttt{Matrix<T>}
&\textcolor{red}{\texttt /}
&\texttt{T}
& divide by scalar
& \tt T = S / d;
& $\quad \mathsf T$&=&$\mathsf S / d$\\
%
\texttt{Matrix<T>}
&\textcolor{red}{\texttt *}
&\texttt{Matrix<T>}
& matrix-matrix product
& \tt T = S * R;
& $\quad \mathsf T$&=&$\mathsf S\,\mathsf R$\\
%
\texttt{Matrix<T>}
&\textcolor{red}{\texttt *}
&\texttt{Vector<T>}
& matrix-vector product
& \tt c = S * a;
& $\quad \vec c$&=&$\mathsf S \, \vec a$\\
%
\hline
%
\texttt{Vector<T>}
&\textcolor{red}{\texttt{+=}}
&\texttt{Vector<T>}
& add
& \tt c += b;
& $\quad \vec c$&=&$\vec c +\vec b$\\
%
 \texttt{Vector<T>}
&\texttt{\textcolor{red}{-=}}
&\texttt{Vector<T>}
& subtract
& \tt c -= b;
& $\quad \vec c$&=&$\vec c -\vec b$\\
%
\texttt{Vector<T>}
&\texttt{\textcolor{red}{*=}}
&\texttt{T}
& multiply by scalar
& \tt c *= d;
& $\quad \vec c$&=&$d\,\vec c$\\
%
\texttt{Vector<T>}
&\texttt{\textcolor{red}{/=}}
&\texttt{T}
& divide by scalar
& \tt c /= d;
& $\quad \vec c$&=&$\vec c/d$\\
%
\texttt{Matrix<T>}
&\texttt{\textcolor{red}{+=}}
&\texttt{ Matrix<T>}
& add
& \tt T += R;
& $\quad \mathsf T$&=&$\mathsf T +\mathsf R$\\
%
 \texttt{Matrix<T>}
&\texttt{\textcolor{red}{-=}}
&\texttt{ Matrix<T>}
& subtract
& \tt T -= R;
& $\quad \mathsf T$&=&$\mathsf T -\mathsf R$\\
%
\texttt{Matrix<T>}&
\texttt{\textcolor{red}{*=}}
&\texttt{T}
& multiply by scalar
& \tt T *= d;
& $\quad \mathsf T$&=&$d\,\mathsf T$\\
%
 \texttt{Matrix<T>}
&\texttt{\textcolor{red}{/=}}
&\texttt{T}
& divide by scalar
& \tt T /= d;
& $\quad \mathsf T$&=&$\mathsf T/d$\\
\hline
\multicolumn{6}{l}{
$^\dagger$ The \texttt \^{} operator has rather low precedence, so
often one has to write \texttt{(a\^{}b)}.
}\\\multicolumn{6}{l}{
$^\ddagger$ Two operators are provided for the dot product, which do
the exact same thing.
}
\end{tabular}
\end{center}
\caption{Operators available for matrices and
  vectors with elements of type \texttt{T}. }
\end{table}

\subsection{Member functions}

For any \Vector, or \Vector-valued expression, or for any \Matrix, or
\Matrix-valued expression, the following properties are available as
member functions. Note that below, {\tt T} stands for the typename of the template, while {\tt Vector\TT{}} and {\tt Matrix\TT{}} stand for {\tt vecmat3::Vector\TT{}} and {\tt vecmat3::Matrix\TT{}}, respectively.  The examples all use {\tt T=DOUBLE}.

\subsubsection{T nrm2()}

This returns the sum of the squares of the elements, which is its norm
squared. E.g., with {\tt T=DOUBLE},
\begin{quote} \tt
  Vector a(1,2,3.316625);

  Matrix R(1,2,0,

\ \ \ \ \ \ \ \ \ 2,0,2

\ \ \ \ \ \ \ \	\ 1,1,1);

  DOUBLE d1 = a.nrm2(); // will be equal to 16.0000014

  DOUBLE d2 = R.nrm2(); // will be equal to 16
\end{quote}

\subsubsection{T nrm()}

This returns the norm of a \Vector\ or \Matrix, e.g., with {\tt T=DOUBLE},
\begin{quote} \tt
  DOUBLE d3 = a.nrm(); // will be equal to 4.00000017

  DOUBLE d4 = R.nrm(); // will be equal to 4
\end{quote}

\noindent
The following properties are for Matrices only:

\subsubsection{T tr()}

This returns the trace of a \Matrix, i.e., the sum of its diagonal
elements. E.g., with {\tt T=DOUBLE},
\begin{quote} \tt
  DOUBLE d5 = R.tr(); // will be equal to 2
\end{quote}

\subsubsection{T det()}

This returns the determinant of a \Matrix, e.g., with {\tt T=DOUBLE},
\begin{quote} \tt
  DOUBLE d6 = R.det(); // will be equal to -2
\end{quote}

\subsubsection{Vector\TT{} row(int i)}
This returns the ith row of a \Matrix.

\subsubsection{Vector\TT{} column(int j)}

This returns the jth column of a \Matrix.

\pagebreak[3]
\subsection{Non-member functions}

In the definition of the following non-member functions, the specified
return type are effective ones. E.g. a return type of Matrix may
return a Matrix-Expression when this is more efficient. In any case, it
can be treated as a Matrix in virtually all ways. Likewise, if an
argument is of Matrix type, a Matrix expression is also allowed.

\subsubsection{Matrix\TT{} Transpose(const Matrix\TT{} \& M)}

Returns the transpose of the argument, which is a \Matrix, e.g.
\begin{quote}\tt
  Matrix S = Transpose(R);
\end{quote}

\subsubsection{Matrix\TT{} Inverse(const Matrix\TT{} \& M)}

Returns the inverse of the argument, which is a \Matrix, e.g.
\begin{quote}\tt
  Matrix S = Inverse(R);
\end{quote}

\subsubsection{Matrix\TT{} Rodrigues(const Vector\TT{} \& v)}

Returns the \Matrix-valued rotation matrix for a rotation along the
axis given by the direction of the \Vector\ argument, with the angle
equal to the norm of that \Vector, e.g.
\begin{quote}\tt
  Matrix S = Rodrigues(a);
\end{quote}

\subsubsection{Matrix\TT{} Dyadic(const Vector\TT{} \& a, const Vector\TT{} \& b)}

Returns the \Matrix-valued dyadic product of two arguments which are
\Vector s, e.g.
\begin{quote}\tt
  Matrix S = Dyadic(a,b);
\end{quote}

\subsubsection{Vector\TT{} MTVmult(const Matrix\TT{} \& M, const Vector\TT{} \& v)}

This simply returns Transpose(Matrix)*Vector.

\subsubsection{T dist(const Vector\TT{} \& a, const Vector\TT{} \& b)}

Returns the length of the difference vector between a and b. This is a
remnant of earlier versions of the \Vector and \Matrix classes, and
barely if at all more efficient than \texttt{(a-b).nrm()}.

\subsubsection{T dist2(const Vector\TT{} \& a, const Vector\TT{} \& b)}

Returns the square length of the difference vector between a and
b. This is a remnant of earlier versions of the Vector and Matrix
classes, and barely if at all more efficient than \texttt{(a-b).nrm2()}.

\subsubsection{T distwithshift(const Vector\TT{}\&a,const Vector\TT{}\&b,const Vector\TT{}\&s)}

Returns the length of the difference vector between a and b shifted by
s. This is a remnant of earlier versions of the vector and Matrix
classes, and barely if at all more efficient than \texttt{(a+s-b).nrm()}.

\vspace{1cm}\pagebreak[3]
Finally, because the notation \texttt{a*b} and \texttt{a\^{}b} for dot and
cross product may be confusing, the following equivalent alternatives
are defined:
\nopagebreak
\subsubsection{T dotProduct(const Vector\TT{} \& a, const Vector\TT{} \& b)}

Returns the DOUBLE which is the dot, or inner, product of the two
\Vector\ arguments.  It is by definition equal to
\texttt{(\texttt{Vector}|\texttt{Vector})}.
Example, with {\tt T=DOUBLE}:
\begin{quote}\tt
  DOUBLE d = dotProduct(a,b);
\end{quote}

\subsubsection{Vector\TT{} crossProduct(const Vector\TT{} \& a, const Vector\TT{} \& b)}

Returns the \Vector\ which is the cross, or outer, product of the two
\Vector\ arguments.  It is by definition equal to
\texttt{(\texttt{Vector}\^{}\texttt{Vector})}.
Example:
\begin{quote}\tt
  Vector c = crossProduct(a,b);
\end{quote}

\section{Expressions}
\label{expressions}

Using the above elementary operations and functions, complex
expressions can be constructed just as for built-in type such as
\texttt{double}. To be more specific, for all operator expressions in table 1
on page 8, the arguments can be expressions themselves.

For example, one can write,
\begin{quote}\tt
  Vector r[2] = \{ Vector(1,4,5), Vector(2,3,4) \};

  Vector v[2] = \{ Vector(1,0,0), Vector(-1,0,0) \};

  Vector s(7,1,0);

  Matrix A(1,0,0,0,1,0,0,-1,0);

  DOUBLE t =  (r[0]+2*A*(s\^{}r[1]) ) | (v[1]-v[0]);

  // alternatively:

  //DOUBLE t =  dotProduct(r[0]+2*A*crossProduct(s,r[1]), v[1]-v[0]);
\end{quote}

Internally, the expressions are not computed directly via temporaries,
but are computed only upon assignment. As a result, the definitions of
these operators and functions in vecmat3.h is not as simple as
e.g. \texttt{Vector operator+(Vector\&,Vector\&)}.  For that reason,
above we used \texttt{Vector} and \texttt{Matrix} wherever a
\Vector/\Matrix\ or a \Vector/\Matrix\ expression can occur. Never
mind the implementation though, things work as expected.

\newpage
\renewcommand{\refname}{Background references}
\begin{thebibliography}{9}
\bibitem{Lippmann}
T. Veldhuizen, \emph{Expression Templates} C++ Report, Vol. 7 No. 5 June
1995, pp. 26-31. See also
http://ubiety.uwaterloo.ca/\~{}tveldhui/papers/Expression-Templates/exprtmpl.html.\\
Reprinted in:
S. B. Lippmann (ed.) \emph{C++ Gems} (Cambridge
  University Press, 1998).
\bibitem{VandevoordeJosuttis} D.\ Vandevoorde and N.\ M.\ Josuttis,
  \emph{C++ Templates: The Complete Guide} (Addison-Wesley, Boston,
  2002).
\bibitem{blitz} http://www.oonumerics.org/blitz
\bibitem{tvmet} http://tvmet.sourceforge.net
\end{thebibliography}
\addcontentsline{toc}{section}{\refname}

\end{document}


\section{Measurement Processing}


\section{Introduction}
\label{sec:introduction}

Full bayesian inference for most neural network is intractable due to the model's nonlinearity causing the true posterior distribution to be highly complex. This has led to the use of variational methods which attempt to approximate the posterior. With regard to bayesian variational inference; this paper addresses the general issue of prior selection to reduce the sensitivity BNNs have toward such initializations and the high variance that arises in the gradients when employing Monte Carlo approximation techniques.

The authors list their contributions as:
\begin{itemize}
	\item Development of a deterministic procedure for propagating uncertain activations through neural networks with uncertain weights and ReLU or Heaviside activation functions.
	\item Development of an EB method for principled tuning of weight priors during BNN training.
	\item Experimental results showing the accuracy and efficiency of our method and applicability to heteroscedastic and homoscedastic regression on real datasets.
\end{itemize}


\setlength{\footskip}{8mm}

\chapter{Automatic Suspicious Behavior Detection from
a Small Bootstrap Set}
\label{ch:batch}

\textit{In this chapter, we propose and evaluate a new method for
automatic identification of suspicious behavior in video surveillance
data. The approach works by constructing scene-specific statistical
models explaining the behaviors occurring in a small bootstrap data
set. It partitions the bootstrap set into clusters then assigns new
observation sequences to clusters based on statistical tests of HMM
log likelihood scores. Cluster-specific likelihood thresholds are
learned rather than set arbitrarily.  In an evaluation on a real-world
testbed video surveillance data set, the method proves extremely
effective, with a false alarm rate of 7.4\% at a 100\% hit rate. The
method is thus a practical and effective solution to the problem of
inducing scene-specific statistical models useful for bringing
suspicious behavior to the attention of human security personnel.}

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

Video surveillance is ubiquitous in our lives, but the ongoing
proliferation of surveillance cameras makes it increasingly difficult
to monitor all channels continuously. As the amount of surveillance
video increases, monitoring becomes more expensive and less
effective. Security would be enhanced if it were possible to perform
intelligent filtering of typical events and to automatically bring
suspicious events to the attention of human security personnel.

The goal of automated suspicious event detection, however, requires
some level of human behavior understanding. Many researchers have
attempted to build intelligent systems able to interpret and
understand human
behaviors \shortcite{davis98activity,masoud03recognition,cao04motion,sminchisescu05motion,du06activity,li06behavior,wang06gesture}.
Pre-trained hidden Markov models (HMMs) and other dynamic Bayesian
networks such as conditional random fields (CRFs) have been widely
used in this area, ranging from visual activity
recognition \shortcite{yamato92hmm,sminchisescu05motion,xiang05profiling,vail07activity},
gesture recognition \shortcite{wilson00gesture,gao04dining}, and
unusual activity
detection \shortcite{nair02surveillance,lee03modeling,chan04event,wu05behavior,andrade06crowd,snoek06event,zhang05event,arsic07behavior}. However,
the problem is difficult and remains unsolved, due to the wide range
of activities possible in any given context and the large amount of
variability within any particular activity. In the context of video
surveillance, the problem is even more challenging because behavior
considered normal in one scene might be considered unusual in another
scene.

%Most existing work creates separate models for each distinct a priori
%known class of normal behavior. One example is the work
%of \shortciteA{nair02surveillance}, which performs automated video
%surveillance using HMMs, each modeling a common, pre-defined activity
%in a scene. They classify a sequence as anomalous if the log
%likelihood of the sequence is below a pre-defined threshold for all
%trained HMMs. Other
%work \shortcite{arsic07behavior,lee03modeling,wu05behavior} uses
%support vector machines (SVMs) to model and classify behaviors into
%pre-defined classes.

Here we propose a method for automatic suspicious behavior detection
that utilizes a small bootstrap set in which observation sequences are
manually labeled as normal or suspicious (warranting an operator's
attention).  Using the algorithm from Chapter~\ref{ch:clustering}, we
partition the bootstrap set into clusters of similar sequences and
model each cluster with a simple HMM. We then label each behavior
cluster as normal or possibly suspicious based on the labels of the
individual sequences mapped to the cluster.

After bootstrapping is complete, we assign new observation sequences
to behavior clusters using statistical tests on the log likelihood of
the sequence according to the corresponding HMMs.  A sequence is
considered suspicious if the most likely cluster is already labeled as
suspicious or if its log likelihood according to the most likely
cluster's HMM is too low.  The cluster-specific likelihood threshold
is learned rather than set arbitrarily.

In an evaluation on a real-world video surveillance situation, we find
that, based on a bootstrap set of 150 human motion sequences, our
method is extremely effective at identifying suspicious behavior, with
a false positive rate of 7.4\% at a hit rate of 100\%.  The proposed
method dramatically outperforms traditional machine learning
approaches using the same bootstrap set for training.

Our method is thus a practical and effective solution to the problem
of inducing scene-specific statistical models useful for bringing
suspicious behavior to the attention of human security personnel.

In the rest of this chapter, we provide the details of our human
behavior modeling and bootstrapping algorithm in
Section \ref{sec:batch-modeling} and our anomaly detection method in
Section \ref{sec:batch-anomaly-detection}. We demonstrate the feasibility
of the algorithm with an experimental evaluation in
Section \ref{sec:batch-results}, and then discuss and point to future
work in Section \ref{sec:batch-discussion}.

\section{Behavior Model Bootstrapping}
\label{sec:batch-modeling}

Our method for bootstrapping a model of the specific behaviors in a
scene is a batch procedure based on a training video stream acquired
over a short period of time such as one week.  We first perform moving
blob tracking on the training video on the assumption that moving
blobs of sufficient size are people or groups of people.  For each
blob, we extract a sequence of feature vectors describing the blob's
trajectory and appearance over time.  From these data, we
automatically bootstrap a bank of linear HMMs with bypass transitions,
each model specializing in one type of behavior.

\begin{figure}[t]
  \centering
  \includegraphics[width=1\linewidth]{figures/behavior-modeling-block-diagram}
  \caption[Block diagram of our overall system.]{\small Block diagram
    of overall anomaly detection system.}
  \label{fig:behavior-modeling-block-diagram}
\end{figure}

Here we follow the processing steps for motion detection described in
Section \ref{sec:blob-motion-detection} and multiple blob tracking described
in Section \ref{sec:blob-blob-extraction}.

We define an ``event'' as a contiguous change or motion detected over
some period of time. Extremely short events (occasionally occurring
due to noise) are automatically removed before processing.

Next we use the tracking algorithm previously described in
Section \ref{sec:blob-appearance-based-blob-tracking} to track the
obtained blobs, extract a sequence of feature vectors describing each
blob's trajectory and appearance over time, and automatically
bootstrap a bank of linear HMMs with bypass transitions, each model
specializing in one type of behavior.

Note that one difference between the methods of this chapter and the previous
chapter is that this chapter extends the simple single blob tracking method to
the full multiple blob tracking method described in Chapter~\ref{ch:blobanalysis}.

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

After blob tracking, we obtain, from a given video, a set of
observation sequences describing the motion and appearance of every
distinguishable moving object in the scene.  We next partition the
observation sequences into clusters of similar behaviors then model
the sequences within each cluster using a simple linear HMM with
bypass transitions.

In this section, we use the method previously described in
Chapter~\ref{ch:clustering}, which first uses dynamic time warping
(DTW) to obtain a distance matrix for the set of observation sequences
then performs agglomerative hierarchical clustering on the distance
matrix to obtain a dendrogram (a binary tree expressing the similarity
structure within the set of observation sequences).
Once the set of all behaviors has been partitioned into
groups containing similar sequences, as we shall see, each group can
be modeled individually with a simple statistical model, the linear
HMM with bypass transitions. Next, we can use the resulting bank of
trained models for anomaly detection.

%Our method solves the problem by educing an initial partitioning of
%the human behavior observation sequences into groups based on
%similarity.

To determine
where to cut off the dendrogram, we traverse the DTW dendrogram in
depth-first order from the root and attempt to model the observation
sequences within the corresponding subtree using a single linear HMM
with bypass transitions. If, after training, the HMM is unable to
``explain'' (in the sense described below) the sequences associated
with the current subtree, we
discard the HMM then recursively attempt to model each of the current
node's children. Whenever the HMM is able to explain the observation
sequences associated with the current node's subtree, we retain the
HMM and prune the tree.

A HMM is said to explain or model a cluster $c$ of observation
sequences if there are no more than $N_c$ sequences in $c$ whose
per-observation log-likelihood is less than a threshold $p_c$.  We use
$N_c=10$ in our experiments. The per-observation log likelihood of a
sequence is previously defined in
Section~\ref{sec:clustering-behavior-clustering}.

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
sequences in the bootstrap set.  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.

The clustering process results in a set of $K$ different typical
behavior clusters ${\cal C} = \{ C_1, C_2, \ldots, C_K \}$ with a set
of $K$ corresponding HMMs ${\cal M} = \{ M_1, M_2, \ldots, M_K \}$.

\section{Anomaly Detection}
\label{sec:batch-anomaly-detection}

In this section we describe our method for anomalous or suspicious
behavior detection. In the \textit{supervised} approach, one would
construct a training set consisting of anomalous and normal behaviors,
build a model, then use the model to classify new behavior sequences
as anomalous or normal.

The pure supervised approach is obviously not suitable, however, when
examples of anomalous behavior are sparse or nonexistent.  In
practical scenarios, the set of possible anomalous behaviors is
infinite in its variety, making it very difficult to acquire a
representative training set.

In the \textit{unsupervised} approach, on the other hand, we would
simply construct a generative model of the normal behavior patterns,
then use the model to classify new behavior sequences as abnormal when
they are ``too far'' in some sense from typical behavior.  For
example, we could use the algorithm described in
Section \ref{sec:batch-behavior-clustering} to construct a bank of
HMMs explaining the normal sequences in a training set of patterns,
then classify new sequences as abnormal or suspicious whenever the
likelihood of the sequence according to the most likely HMM is below
some specific threshold.

The difficulty with the pure unsupervised approach, however, is that
there is no clear way to calibrate the parameters of the ``too far''
criterion.  In practice, one would have to select a conservative
initial distance threshold then fine-tune the threshold to achieve the
best tradeoff between hit and false positive rates.  An inappropriate
initial cutoff could lead to disastrous misses of suspicious behavior
or inundation with false positives.

Since both approaches have limitations, rather than the pure
supervised method or the pure unsupervised method, we instead propose
a \textit{semi-supervised} method that self-calibrates itself from a
small bootstrap set in which each bootstrap sequence is manually
labeled as normal or suspicious by a human operator.  Our method is
simple.  We acquire labels for the bootstrap patterns from the
operator, then we apply the algorithm of
Section \ref{sec:batch-behavior-clustering} to
\textit{both the positive and negative sequences} in the bootstrap
set.  We identify each cluster as a ``normal'' cluster if \textit{all}
of the sequences falling into it are labeled as normal, or identify it
as an ``abnormal'' cluster if \textit{any} of the sequences falling
into it are labeled as abnormal.

In the best case, if we updated
the log likelihood for each model every time we receive a new
observation token in constant time (this is feasible), the time
complexity of the method would be O($nm$),
where $n$ is the number of observations and $m$ is the number of
models. However, for convenience, we rerun the forward
algorithm \shortcite{rabiner89hmm} on each observation for every
model.

Algorithm \ref{anomaly-detection-algorithm} presents a pseudocode summary of
the runtime anomaly detection method. New sequences are classified as
normal if the most likely HMM for the input sequence is associated
with a cluster of normal sequences \textit{and} the $z$-scaled
per-observation log of the probability of the sequence under that most likely
model is greater than a global empirically determined threshold
$\theta_z$.

\begin{algorithm}[t]
  \caption{Anomaly Detection}
  \label{anomaly-detection-algorithm}
  \begin{algorithmic}
    \REQUIRE $\vec{O}$: behavior sequence
    \REQUIRE ${\cal M}$: set of HMMs
    \STATE ${\cal M}_{ab} \gets \{ M \mid M \in {\cal M} \text{~and $M$ is marked abnormal} \}$
    \STATE $( M_{ml}, L_{ml} ) \gets \textsc{Find-Most-Likely-Model}(\vec{O}, {\cal M})$
    \IF{$M_{ml} \in {\cal M}_{ab}$ or $L_{ml} \leq \theta_z$}
      \STATE $\textsc{Alert-Security-Personnel}(\vec{O})$
    \ENDIF
  \end{algorithmic}
\end{algorithm}

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

We created a testbed data set using a CCTV camera with a view of the
front of a building, as seen in
Figure \ref{fig:batch-example-behavior}(a). Videos were recorded at 25
frames per second with a resolution of $320 \times 240$ during working
hours (9:00--17:00) for two weeks. We used the previously-described
techniques to segment the raw video stream into separate videos
containing motion, track blobs, extract features, discretize the
feature vectors, and create observation sequences. We obtained 423
video segments containing 660 observation sequences. In total, 370
videos contain single sequences, and 53 videos contain multiple
sequences. As previously mentioned, our blob tracking method is not perfect;
some tracks are lost and some tracks are incorrectly associated with each
other. See Figure~\ref{fig:complex-tracking-results} for an example of
a tracking error.

We observed that there are four common activities in this scene:
people walking into the building, people walking out of the building,
people parking bicycles, and people riding bicycles out. Most of the
remaining activities involve people walking past or having
interactions with each other. Most multiple people interactions are
considered normal because each of them includes multiple single activities.
For instance, a person walking in has a conversation with another
person walking out before he/she walks into the building. As a result,
two single activities occur in this situation. Figure~\ref{fig:batch-example-behavior}
shows examples of each of these behaviors. In our experiments, we
considered all other behaviors, such as walking around looking for an
unlocked bicycle, to be suspicious or abnormal.

\begin{figure}[t]
  \centering
  \subfloat[]{\includegraphics[width=0.19\textwidth]{figures/example-behavior01.pdf}\label{fig:example-of-scene}}
  \hspace{0.03in}
  \subfloat[]{\includegraphics[width=0.19\textwidth]{figures/example-behavior02.pdf}}
  \hspace{0.03in}
  \subfloat[]{\includegraphics[width=0.19\textwidth]{figures/example-behavior03.pdf}}
  \hspace{0.03in}
  \subfloat[]{\includegraphics[width=0.19\textwidth]{figures/example-behavior04.pdf}}
  \hspace{0.03in}
  \subfloat[]{\includegraphics[width=0.19\textwidth]{figures/example-behavior05.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.
    (e) Other group activities.}
  \label{fig:batch-example-behavior}
\end{figure}

To evaluate the effectiveness of our method, we divide the experiments
into two parts: model configuration selection (finding an optimal set
of HMMs to model the bootstrap set) and anomaly detection based on the
HMM bootstrap set. In all of the experiments, we used linear HMMs with
bypass transitions. We chose this model structure based on our
previous empirical experience \shortcite{kan08thesis}.

\subsection{Model Configuration Selection}
\label{sec:batch-model-config}

We first manually labeled each of the videos with the categories
``normal'' or ``abnormal.''  For purposes of analyzing the results, we
further subdivided the normal patterns into categories ``walk-in,''
``walk-out,'' ``cycle-in,'' and ``cycle-out,'' but we did not use
these labels for model selection or learning.

Towards model identification, we then performed a series of
experiments with different bootstrap parameter settings and selected
the configuration with the highest accuracy in separating the normal
sequences from the abnormal sequences on the bootstrap sequence set,
as measured by the false positive rate for abnormal sequences.  (Every
bootstrap cluster containing an abnormal sequence is considered
abnormal, so we always obtain 100\% detection on the bootstrap set;
the only discriminating factor is the false positive rate.)  The set
of parameters that we varied were 1) the number of states in each
bootstrap HMM (4--8), 2) the number of sequences used for
bootstrapping (50--200), and the number of symbols used (4--8).

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 150 observations then used a $z$-threshold
of 2.0. We fixed the parameter $N_c$ (the number of deviant patterns
allowed in a cluster) to 10.

A subset of the results of the model configuration selection
experiments is shown in
Figure \ref{fig:batch-some-clustering-results}. Based on the false
positive rate criterion described above, we selected the model
configuration consisting of HMMs with five states and seven tokens
trained on 150 bootstrap sequences.  We arbitrarily chose the model
from one of the 10 trials with this configuration.
Table \ref{tab:batch-bootstrapping-results} shows an example of the
distribution of bootstrap sequences across behavior clusters with the
selected model configuration. In this run, we found that, with 20 behavior
clusters, anomalous and typical sequences are always mapped to
different clusters, although the large number of one-sequence
clusters means there would be a fairly large amount of manual
work required to identify them.

We used this model with the
remaining 510 sequences in the anomaly detection experiments described
next.

\begin{figure}[t]
    \centering
    \subfloat[]{\includegraphics[scale=0.34]{figures/m5-d7-Nc10-fp-rate}}
    \subfloat[]{\includegraphics[scale=0.34]{figures/m5-d7-Nc10-number-of-clusters.pdf}}\\
    \subfloat[]{\includegraphics[scale=0.34]{figures/m7-d7-Nc10-fp-rate}}
    \subfloat[]{\includegraphics[scale=0.34]{figures/m7-d7-Nc10-number-of-clusters.pdf}}\\
    \subfloat[]{\includegraphics[scale=0.34]{figures/m9-d7-Nc10-fp-rate}}
    \subfloat[]{\includegraphics[scale=0.34]{figures/m9-d7-Nc10-number-of-clusters.pdf}}\\

    \caption[Subset of model configuration selection results.]{\small
        Subset of model configuration selection results.  Model
        configuration with (a)--(b) five tokens, (c)--(d) seven tokens
        and (e)--(f) nine tokens. Dashed red, solid green and dash-dot blue
        lines represent models with five, six and seven states, respectively.
        Each point is an average over 10 trials.}
    \label{fig:batch-some-clustering-results}
\end{figure}

\begin{table}[t]
  \caption[Example human behavior pattern bootstrapping
    results.]{\small Example human behavior pattern bootstrapping
    results. We used linear HMMs with five states and seven
    tokens and bypass transitions. The model consists of 20 clusters.
    ``W'' means ``walk''
    and ``C'' means ``cycle.''  For the seven clusters containing more
    than one sequence, shown is the distribution of the patterns in
    the cluster over the activities.  The last row shows the
    distribution of the 13 clusters containing only a single sequence
    over the activity categories.}
  \begin{center}
    \begin{tabular}{c|c|c|c|c|c}
      \hline
      Cluster \# & W-in & W-out & C-in & C-out & Other \\
      \hline \hline
      1 & 44 & 0 & 20 & 0 & 0 \\ \hline
      2 & 0 & 37 & 0 & 19 & 0 \\ \hline
      3 & 0 & 0 & 2 & 0 & 0 \\ \hline
      4 & 0 & 0 & 3 & 0 & 0 \\ \hline
      5 & 0 & 0 & 0 & 4 & 0 \\ \hline
      6 & 0 & 0 & 0 & 0 & 6 \\ \hline
      7 & 0 & 0 & 0 & 0 & 2 \\ \hline
      One-seq clusters & 0 & 2 & 6 & 2 & 3 \\ \hline
    \end{tabular}
  \end{center}
  \label{tab:batch-bootstrapping-results}
\end{table}

\subsection{Anomaly Detection}

Here we describe four experiments to evaluate our anomaly detection
method. In Experiment I, we applied our proposed method, as previously
described, to detect anomalous events. In Experiments II, III and IV,
for comparison, we applied traditional machine learning algorithms to
the same problem. We use $k$-nearest neighbors ($k$-NN), a Mahalanobis
classifier, and support vector machines (SVMs).  For
these methods, we use the 150-sequence bootstrap sequence set from
Section \ref{sec:batch-model-config} as training data and test on the
remaining 510 sequences.  Since the main concern in video surveillance
is to detect every unusual event while minimizing the false positive
rate, in every experiment, we calculate an ROC curve and select the
detection threshold yielding the best false positive rate at a 100\%
hit rate. Our experimental hypothesis was that the proposed method for
modeling scene-specific behavior patterns should obtain better false
positive rates than the traditional methods.

\subsubsection{Experiment I: Proposed method}

The red line in Figure \ref{fig:batch-roc-results} represents the ROC
curve for our method as we vary the likelihood threshold at which a
sequence is considered anomalous.  Note that the ROC does not
intersect the point $(0, 0)$ because any sequence that is most likely
under one of the HMMs modeling anomalous sequences in the bootstrap
set is automatically classified as anomalous regardless of the
threshold.

\begin{figure}[t]
  \centering
  \includegraphics[width=0.8\linewidth]{figures/roc-ours-vs-ml-results}
  \caption[Anomaly detection ROC curves. Solid red, dotted green, and dashed
    blue lines represent ROCs for the proposed method in Experiment I,
    Mahalanobis classifier for anomaly detection in Experiment III,
    and SVM-based anomaly
    detection in Experiment IV, respectively.]{\small Anomaly
    detection ROC curves. Solid red, dotted green, and dashed blue lines
    represent ROCs for the proposed method in Experiment I, Mahalanobis
    classifier for anomaly
    detection in Experiment III, and SVM-based anomaly detection
    in Experiment IV, respectively.}
  \label{fig:batch-roc-results}
\end{figure}

\begin{table}[t]
  \caption[Anomaly detection results for the proposed method, $k$-NN
    anomaly detection method, Mahalanobis classifier for anomaly detection method, and
    SVM-based anomaly detection method in Experiment I, II, III, and
    IV, respectively.]{\small Anomaly detection results for the
    proposed method, $k$-NN anomaly detection method, Mahalanobis classifier for
    anomaly detection method, and SVM-based anomaly detection method
    in Experiment I, II, III, and IV, respectively. For the Mahalanobis classifier for
    anomaly detection method, we include 11 abnormal sequences from
    the bootstrap set in the test set, so the total number of
    positives is 35. TP, FP, TN, and FN are the number of true positives,
    false positives, true negatives, and false negatives, respectively.
    TPR and FPR stand for true positive rate and false positive rate,
    respectively.}
  \begin{center}
    \begin{tabular}{c|c|c|c|c|c|c}
      \hline
      Method & TP & FP & TN & FN & TPR & FPR \\
      \hline\hline
      Ours & 24 & 36 & 450 & 0 & 1 & 0.074 \\ \hline
      $1$-NN & 19 & 1 & 485 & 5 & 0.792 & 0.002 \\ \hline
      $2$-NN & 19 & 2 & 484 & 5 & 0.792 & 0.004 \\ \hline
      $3$-NN & 16 & 0 & 486 & 8 & 0.667 & 0 \\ \hline
      $4$-NN & 16 & 1 & 485 & 8 & 0.667 & 0.002 \\ \hline
      $5$-NN & 14 & 0 & 486 & 10 & 0.583 & 0 \\ \hline
      Mahalanobis classifier & 35 & 421 & 65 & 0 & 1 & 0.87 \\ \hline
      SVM & 24 & 228 & 258 & 0 & 1 & 0.469 \\ \hline
    \end{tabular}
  \end{center}
  \label{tab:batch-detection-results}
\end{table}

The ROC reveals that a threshold of $-3.259$ achieves zero false
negatives at a false alarm rate of 0.074.
Table \ref{tab:batch-detection-results} shows the detailed performance
of this model, and Figure \ref{fig:suspicious-behavior-detected}
shows an example of a sequence classified as abnormal.

%\begin{table}[t]
%  \caption[Anomaly detection results at 100\% hit rate for the
%    proposed method in Experiment I.]{\small Anomaly detection results
%    at 100\% hit rate for the proposed method in Experiment I.}
%  \begin{center}
%    \begin{tabular}{c|c|c|c|c|c}
%      \hline
%      TP & FP & TN & FN & TPR & FPR \\
%      \hline \hline
%      24 & 36 & 450 & 0 & 1 & 0.074 \\ \hline
%    \end{tabular}
%  \end{center}
%  \label{tab:batch-detection-results}
%\end{table}

\begin{figure}[t]
  \centering
  \begin{tabular}{ccccc}
    \includegraphics[scale=0.24]{figures/case-1-suspicious-0187} &
    \includegraphics[scale=0.24]{figures/case-1-suspicious-0208} &
    \includegraphics[scale=0.24]{figures/case-1-suspicious-0225} &
    \includegraphics[scale=0.24]{figures/case-1-suspicious-0249} &
    \includegraphics[scale=0.24]{figures/case-1-suspicious-0267} \\
    \small Frame 187 &
    \small Frame 208 &
    \small Frame 225 &
    \small Frame 249 &
    \small Frame 267 \\
    \includegraphics[scale=0.24]{figures/case-2-suspicious-0188} &
    \includegraphics[scale=0.24]{figures/case-2-suspicious-0334} &
    \includegraphics[scale=0.24]{figures/case-2-suspicious-0344} &
    \includegraphics[scale=0.24]{figures/case-2-suspicious-0353} &
    \includegraphics[scale=0.24]{figures/case-2-suspicious-0377} \\
    \small Frame 188 &
    \small Frame 334 &
    \small Frame 344 &
    \small Frame 353 &
    \small Frame 377 \\
    \includegraphics[scale=0.24]{figures/case-3-suspicious-0173} &
    \includegraphics[scale=0.24]{figures/case-3-suspicious-0197} &
    \includegraphics[scale=0.24]{figures/case-3-suspicious-0233} &
    \includegraphics[scale=0.24]{figures/case-3-suspicious-0246} &
    \includegraphics[scale=0.24]{figures/case-3-suspicious-0268} \\
    \small Frame 173 &
    \small Frame 197 &
    \small Frame 233 &
    \small Frame 246 &
    \small Frame 268
  \end{tabular}
  \caption[Example anomaly detected by the proposed method in
    Experiment I.]{\small Example anomaly detected by the proposed
    method in Experiment I.  The first row represents the sequence
    containing a person walking around looking for an unlocked
    bicycle. The second row represents the sequence containing a
    person walking out of the building then walking back into it.
    The last row represents a person walking in and looking around
    then walking out.}
  \label{fig:suspicious-behavior-detected}
\end{figure}

I have performed some brief experimentation to determine how the
size of bootstrap set affects the results. I found that with more bootstrap
data we tend to discover more compact behavior classes, leading to better
anomaly detection rates in practice.

However, since a larger bootstrap set will generally contain more
variable behavior, our approach consequently creates more models to
handle the more variable set of patterns. The effect is that we
require more interaction with the operator determine whether each
behavior cluster is normal or abnormal.

In this sense, the ``optimal'' bootstrap set size would thus be a
size sufficient to include most typical behaviors (1--2 days) but
no more.

\subsubsection{Experiment II: $k$-NN}

In this experiment, we tested the ability of a more traditional
machine learning algorithm, $k$-nearest neighbors, to detect the
anomalies in our testbed data set using the same division of sequences
into training and testing as in Experiment I.  As the distance
measure, we used the same DTW measure we used for hierarchical
clustering of the bootstrap patterns in our method.  We varied $k$
from 1 to 5.  The results are shown in
Table \ref{tab:batch-detection-results}. While the false positive rates are much
lower than those obtained in our method, the hit rates are
unacceptable.  For $k$-NN to be a practical anomaly detection method,
we would have to adjust it.  For example, we could impose a distance
threshold beyond which a pattern is considered anomalous even if a
majority of the nearest neighbors are normal.

%\begin{table}[t]
%  \caption[$k$-NN anomaly detection results in Experiment II.]{\small
%    $k$-NN anomaly detection results in Experiment II.}
%  \begin{center}
%    \begin{tabular}{c|c|c|c|c|c|c}
%      \hline
%      $k$ & TP & FP & TN & FN & TPR & FPR \\
%      \hline \hline
%      1 & 19 & 1 & 485 & 5 & 0.792 & 0.002 \\ \hline
%      2 & 19 & 2 & 484 & 5 & 0.792 & 0.004 \\ \hline
%      3 & 16 & 0 & 486 & 8 & 0.667 & 0 \\ \hline
%      4 & 16 & 1 & 485 & 8 & 0.667 & 0.002 \\ \hline
%      5 & 14 & 0 & 486 & 10 & 0.583 & 0 \\ \hline
%    \end{tabular}
%  \end{center}
%  \label{tab:kNN-results}
%\end{table}

\subsubsection{Experiment III: Mahalanobis Classifier}

In this experiment, we classified sequences as normal or anomalous
using a Gaussian density estimator derived from principal components
analysis (PCA).  Since PCA requires a fixed-length input vector, we
calculated, for each sequence in the testbed data set, a summary
vector consisting of the means and standard deviations of each
observation vector element over the entire sequence.  With seven
features in the observation vector, we obtained a 14-element vector
summarizing each the sequence.  After feature summarization, we
normalized each component of the summary vector by z-scaling. Then,
since we are performing probability density estimation for the normal
patterns, we applied PCA to the 139 normal sequences in the bootstrap
set.  We chose the number of principal components accounting for 80\%
of the variance in the bootstrap data. Finally, we classified the
remaining 521 test sequences using a Mahalanobis classifier to calculate
the Mahalanobis distance of each sequence's summary vector to the mean of
the normal bootstrap patterns' summary vectors.

The green line in Figure \ref{fig:batch-roc-results} is the ROC curve
obtained by varying the Mahalanobis distance threshold, and
Table \ref{tab:batch-detection-results} shows detailed results for
anomaly detection at a 100\% hit rate.  The high false positive rate
at this threshold and the overall poor performance in the ROC analysis
show that the Mahalanobis classifier is clearly inferior to our proposed
method.

%\begin{table}[t]
%  \caption[PCA-based anomaly detection results in Experiment
%    III.]{\small PCA-based anomaly detection results in Experiment
%    III.}
%  \begin{center}
%    \begin{tabular}{c|c|c|c|c|c}
%      \hline
%      TP & FP & TN & FN & TPR & FPR \\
%      \hline\hline
%      35 & 421 & 65 & 0 & 1 & 0.87 \\ \hline
%    \end{tabular}
%  \end{center}
%  \label{tab:pca-detection-results}
%\end{table}

\subsubsection{Experiment IV: SVMs}

Here we used the same summary vector technique used in Experiment III
but performed supervised classification using support vector
machines. We used the radial basis function kernel implementation in
LIBSVM \shortcite{CC01a} with grid search for the optimal
hyperparameters using five-fold cross validation on the training set
(150 sequences). The blue line in Figure \ref{fig:batch-roc-results}
is the ROC curve obtained by varying the threshold on the signed
distance to the separating hyperplane used for classification as
normal or abnormal.  Table \ref{tab:batch-detection-results} shows the
detailed anomaly detection results for SVMs at a 100\% hit rate.
Although the results are clearly better than those obtained from
$k$-NN or Mahalanobis classifier, they are also clearly inferior
to those obtained in Experiment I.

In the experiments, we use SVM and Mahalanobis classifier to perform
two-class classification (normal vs.\ abnormal). In our data set,
both normal and abnormal patterns contains subclasses, so
the results could be improved if we train the algorithms on those
subclasses separately.

%\begin{table}[t]
%  \caption[SVM anomaly detection results in Experiment IV.]{\small SVM
%    anomaly detection results in Experiment IV.}
%  \begin{center}
%    \begin{tabular}{c|c|c|c|c|c}
%      \hline
%      TP & FP & TN & FN & TPR & FPR \\
%      \hline \hline
%      24 & 228 & 258 & 0 & 1 & 0.469 \\ \hline
%    \end{tabular}
%  \end{center}
%  \label{tab:svm-detection-results}
%\end{table}

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

In this chapter, we have proposed and evaluated a new method for
bootstrapping scene-specific anomalous human behavior detection
systems.  The method requires minimal involvement of a human operator;
the only required action is to label the patterns in a small bootstrap
set as normal or anomalous.  On a testbed data set acquired in a
real-world video surveillance situation, with a bootstrap set of 150
sequences, the method achieves a false positive rate of merely 7.4\%
at a hit rate of 100\%.  The experiments demonstrate that with a
collection of simple HMMs, it is possible to learn a complex set of
varied behaviors occurring in a specific scene.

\FloatBarrier

% --- Maybe use later ---

%In more recent years, research has started to focus on the problem of
%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
%co-occurrence information. \shortciteA{li06behavior} cluster human
%gestures by constructing an affinity matrix using dynamic time warping
%(DTW) \shortcite{sakoe78dtw}, then they apply the normalized-cut
%algorithm to cluster the gestures. \shortciteA{hautamaki08clustering}
%apply DTW and use the resulting pairwise DTW distances as input to a
%hierarchical clustering process in which $k$-means is used to
%fine-tune the output.

%\shortciteA{swears08clustering} propose hierarchical
%HMM-based clustering to find and cluster motion trajectories and
%velocities for surveillance video in a highway interchange
%scene. \shortciteA{xiang05profiling} model the distribution of
%activity data in a scene using a Gaussian mixture model (GMM) and
%employ the Bayesian information criterion (BIC) to select the optimal
%number of behavior classes prior to HMM
%training. \shortciteA{kan10clustering} cluster human behaviors using
%DTW and hierarchical clustering and then perform statistical tests to
%find the optimal number of human behavior classes using HMMs.

%In this paper, we propose an effective method for incremental learning
%of normal human behavior as events occur. We find the optimal set of
%human behaviors using DTW and HMMs in a top-down hierarchical
%fashion. Our 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 created, we perform statistical tests
%on the observation sequence's likelihood according to each HMM
%model. When the likelihood is low according to all of the pre-existing
%clusters, we consider the observation sequence to be anomalous and
%alert a human operator. When the likelihood is sufficiently high for
%one of the pre-existing models, we incrementally update the sufficient
%statistics for that model. We perform support vector machine (SVM)
%classification of the log-likelihood vectors constructed from the set
%of initial HMMs in order to improve detection rates.

%The existing work most similar to ours is the incremental learning
%approach of \cite{xiang08incremental}, who model the activity in a
%scene incrementally after initialization using a small bootstrapping
%dataset; however, both normal and abnormal activities are explicitly
%modeled and classified using a log-likelihood ratio test (LRT). Our
%approach does not require an a priori model of anomalous human
%behavior.  Since anomalous behavior in general scenes is rare and
%diverse, we cannot learn a model for it; we instead detect deviations
%from typical behavior. We consider observation sequences with
%likelihoods below threshold as abnormal. The likelihood thresholds are
%learned rather than set arbitrarily.

% Related works using other methods besides HMMs
% \cite{davis98activity} build an application for human activity
% recognition using the principal component analysis (PCA) to model an
% optical flow field of motions.  \cite{masoud03activity} use the
% work of \cite{ju96motion} to track five human body parts.  Then
% they also apply PCA technique to recognize actions. To classify
% human actions, they compress the training actions, and compute the
% distance between coefficients for a testing action and the
% coefficients of every example action, then choose the minimum
% distance.  \cite{cao04motion} use support vector machines (SVMs) to
% recognize online human motion. Each processed frame of a video is
% classified and labeled separately using SVMs. Then the resulting
% labels are used to determine the motion type of the video through
% majority voting within a sliding window.  \cite{li06behavior}
% propose a method based on space-time image features for automatic
% behavior modeling and recognition. The behavior templates are built
% and used to perform the classification.  \cite{du06activity}
% propose an approach to recognize interaction activities between two
% people based on dynamic Bayesian network (DBN). They add the state
% duration with a reasonable distribution into the model to solve some
% drawbacks from the conventional HMM such as limited performance to
% model long-range process.  Conditional random fields (CRFs) have
% been extended from the natural language processing to a range of
% areas like gesture recognition \cite{wang06gesture} and activity
% recognition \cite{sminchisescu05motion, vail07activity}.
% \cite{sminchisescu05motion} recognize human motion using
% discriminative conditional random field (CRF) and maximum entropy
% Markov models (MEMM).  \cite{vail07activity} apply conditional
% random fields (CRF) for activity recognition in the robot tag
% domain.  \cite{wang06gesture} extend the conditional random fields
% (CRFs) to incorporate with hidden structures for human gesture
% recognition.

% \cite{yamato92hmm} apply discrete HMMs to recognize tennis
% strokes. They train a separate HMM for each action. The correct
% action will yield the highest log-likelihood given the trained
% model.  \cite{wilson00gesture} apply continuous HMMs to recognize
% human gestures in an online fashion. They modify the Baum-Welch
% algorithm to be able to update parameters at runtime.
% \cite{gao04dining} also apply continuous HMMs to recognize dining
% activity in a nursing home.  They define states topology to describe
% stages in dining activities, then identify instances of eating using
% the trained HMM.  \cite{chan06event} present a HMM-based approach
% to detect rare events in aerial video.  Rather than modeling motion
% trajectories directly, they model the spatio-temporal relations
% between objects and uncertainty in observations. Since it is
% difficult to model and differentiate the invariant relationships of
% objects from incidental activities when training data is lacking.
% \cite{snoek06event} propose a method for automatically recognizing
% usual events on stairs and detecting anomalous events such as people
% miss one step or people slip off one step.  They train a single
% continuous HMM on all normal sequences and use a threshold to detect
% anomalies. Additionally, in the experiments, they applied a
% conditional random field (CRF) to compare the detection results.
% \cite{andrade06event} present an automatic method for modeling
% crowd scenes as well as detecting abnormal events. They use the
% combination of the foreground mask and optical flow information to
% only consider the flow vectors inside the foreground objects. After
% that they perform PCA on the optical flow fields of each frame to
% get the activity pattern. They measure the similarity between video
% segments using spectral clustering based on the likelihood given by
% a multi-observation HMM (MOHMM), and then they group the segments
% into $K$ different classes.  They train a MOHHM per class. Then an
% abnormal event is detected if the likelihood given by a bank of
% MOHMM is less than the detection threshold.
% \cite{arsic07behavior}develop a video surveillance application to
% monitor human behaviors in an airplane situation. They use a
% multi-class SVM to model and classify behaviors into the pre-defined
% classes. Because of unknown start and end state or the length of
% motion, their classification is performed by windowing the resulting
% time series of features without any overlap.

% \cite{arsic07behavior} also model and classify behaviors into the
% pre-defined classes using a multi-class support vector machine
% (SVM).

% \cite{xiang05profiling} proposed a HMM-based approach to model
% visual behavior and detect anomalies in a scene without manual
% labeling.

% Works using the extened HMMs \cite{brand97chmm} exploit coupled
% hidden Markov models (CHMMs) to deal with the causal connections
% among multiple temporal processes. In this paper, the author use
% CHMMs to model two-handed actions and to classify gestures into 3
% categories which are single whip, brush knee, and cobra. Their
% experiments presented that CHMMs is superior compared to
% conventional HMMs in terms of training speeds, model likelihoods,
% and robustness to initial conditions. CHMMs use fully-connected
% state space.  \cite{brand00momchmm} propose a multi-observation and
% mixture+counter hidden Markov model (MOMC-HMM).  This kind of HMM is
% used to factorize the observation space. The authors present that
% their framework capable of learning models of office activity and
% outdoor traffic. In this paper, they also demonstrate how this
% framework can be adapted to infer hidden state from extremely
% ambiguous images. For example, it can infer both 3D body pose and
% orientation from image sequences of low resolution silhouettes.
% \cite{xiang05profiling} also employ this kind of HMM in their work
% to learn behavior patterns in a corridor scene, and detect anomalies
% such as a person sneaking into the office area without using an
% entry card.  \cite{vogler01language} propose parallel hidden Markov
% models (PaHMMs). For this kind of HMM, the hidden state space is
% factorized into ``state channels" corresponding to multiple
% independent temporal processes. Consequently, the channels can be
% trained completely independently, and later they can be combined at
% recognition time. Since they apply PaHMMs to recognize American sign
% language (ASL), it is unnecessary to provide examples of all
% possible combinations of phonemes at training time.  Thus, ASL can
% be modeled independently. Their experiments demonstrate that their
% approach outperforms conventional HMMs in dealing with this
% problem. However, the factorizability hypothesis is invalid in most
% cases, particularly when facing the group or interactive activities.
% \cite{gong03activity} propose dynamically multi-linked hidden
% Markov model (DML-HMM). The DML-HMM targets connecting a subset of
% essential hidden state variables across multiple temporal processes
% instead of being fully connected as in the case of a CHMM. The state
% transition matrices are factorized using Schawarz's Bayesian
% information criterion (BIC) \cite{schwarz78bic}. The number of
% unnecessary parameters can be reduced for better network structure
% discovery. Their experiments in this paper presented that DML-HMM
% outperforms a MOMC-HMM, a PaHMM, and a CHMM in modeling group
% activities in a cluttered outdoor scene.

% \cite{zhang05event} propose a method for unusual event detection
% using semi-supervised adapted HMMs. They train an usual event model
% and then used the maximum a posteriori (MAP) to adapt the models as
% well as model unusual event models in a top-down hierarchical
% fashion.  At each iteration, they split the usual event model into
% two models: one represents an usual event model, and the other one
% represents an unusual event model. Then they keep repeating the
% steps until they obtain the desired number of iterations. This
% method does not need explicitly labeled unusual event data.


\documentclass[10pt]{article}
\usepackage[margin=1in]{geometry}

\input{global_macros}
\input{macros}

% Enable (uncolored) cross-reference hyperlinks
% Should always be last package loaded.
% See: https://tex.stackexchange.com/questions/103123/links-do-not-lead-to-right-pages
\usepackage[colorlinks=false]{hyperref}
\begin{document}
  \begin{center}
    {\Large \textbf{Text Classification under Negative Covariate Shift}}

    \vspace{6pt}{\large Project Proposal}

    \vspace{6pt}Zayd Hammoudeh
  \end{center}

  Consider the task of binary (supervised) classification.  Each sample is a tuple of feature vector~${\X \in \domain}$ and label~${\y \in \lbls}$.  Samples are generated from unknown, joint probability distribution~$\joint$.  Given hypothesis class~${\hypoCls=\setbuild{\dec}{\func{\dec}{\domain}{\real}}}$ and loss function~$\func{\loss}{\real \times \lbls}{\real}$, the ideal classifier is:

  \begin{equation}\label{eq:IdealClassifier}
    \dec\opt = \argmin_{\dec \in \hypoCls} \mathbb{E}_{(\X, \y) \sim \joint} \sbrack{\fLoss{\fDec{\X}, \y}}\text{.}
  \end{equation}

  Since $\joint$~is unknown, Eq.~\eqref{eq:IdealClassifier} cannot be determined exactly.  \textit{Empirical risk minimization}, defined in Eq.~\eqref{eq:ERM}, is used in practice for selection of the optimal learner,~${\dec\opt}$. Observe that the true expected loss is replaced with an empirical estimate using i.i.d.\ training set~${\train \sim \joint}$.

  \begin{equation}\label{eq:ERM}
    \dec\opt = \argmin_{\dec \in \hypoCls} \sum_{(\X_i,\y_i) \in \train} \fLoss{\fDec{\X_i}, \y_i}
  \end{equation}

  There are many real-world applications where $\train$~is not an i.i.d.\ sample. For example, given a huge (unlabeled) news corpus, a particular user may only be interested in a single topic,~$\interest$.  There are numerous irrelevant news topics,~${\irrel_1,\ldots,\irrel_n}$ where~$n$ is huge.  This user could label a few relevant articles she manually found as positive~($\pCls$). She could also label any irrelevant articles she encountered negative. Such an ad-hoc labeling mechanism generally induces \textit{covariate shift} (i.e.,~train and test marginal distributions) for the negative class. For instance, the user may fail to label documents from each negative topic~$\irrel_i$. Similarly, the training set's negative topic prior probabilities will not match the corresponding true priors. Constructing a classifier given negative covariate shift is know as \textit{positive, unlabeled, biased negative} (PUbN)~\textit{learning}.

  There are three common approaches to PUbN learning:

  \begin{enumerate}
    \item Supervised (PN -- Positive-Negative) Learning: Ignores the bias in the negative training examples.  Serves as a baseline of comparison and quantifies the effect of any correction.

    \item Positive\-/Unlabeled (PU) Learning: Premised on the idea that using the biased negative training examples can be inherently deleterious.  It constructs a binary classifier strictly using the positive examples and the residual unlabeled corpus.  An example where this approach was used is~\cite{Li:2010}.

    \item Positive, Unlabeled, biased Negative (PUbN) Learning: Constructs a classifier using some \textit{a~priori} knowledge of the negative training set's bias.  Similar to~\cite{Hsieh:2019}, this project assumes knowledge of the negative training sets prior probabilities.
  \end{enumerate}

  \paragraph{Goal} This project will explore negative covariate shift's effect on news topic classification.  The experiments in this section are inspired by Fei \&~Liu~\cite{Fei:2015} and Hsieh\etal~\cite{Hsieh:2019}.

  \paragraph{Experimental Setup} This project will use the 20~newsgroups dataset~\cite{20Newsgroups}.  The dataset statistics are shown in Table~\ref{tab:DatasetComparison}.  We will use the latest (cleaned) version of the dataset published by Jason Rennie where duplicate and empty documents were removed.

  \begin{table}[b]
    \centering
    \caption{Dataset statistics for 20~Newsgroups}\label{tab:DatasetComparison}
    \begin{tabular}{|c|c|c|}
      \hline
      \#~Classes     & 20                     \\\hline
      Train Size     & 13,180                 \\\hline
      Test Size      & 5,648                  \\\hline
      Test Set Prior & Nearly uniform         \\\hline
    \end{tabular}
  \end{table}

  When constructing the experimental datasets, one topic will be selected as the positive class and all remaining~(19) topics will form joint form the negative class.  The experiments will be divided into three categories:

  \begin{itemize}
    \item \textit{In-Training}: All negative subclasses (${\irrel_1,\ldots,\irrel_n}$) are present in both the training and test sets.  The only difference is their train/test marginal distributions.

    \item \textit{Out-of-Training}: Some negative subclasses are only present in the test set. Beside that difference, the train and test marginal distributions are identical.

    \item \textit{Combined}: Synthesis of in-training and out-of-training.  Bias includes missing training set subclasses and a marginal distribution shifts  Most challenging case.
  \end{itemize}

  \noindent
  This procedure will be repeated with different topics as the positive class to ensure as representative of an experiment set as possible.

  \paragraph{Implementation Plan} The PN, PU, and PUbN implementations will share the same standard neural network architecture implemented in PyTorch.  There will be an embedding of some kind (at minimum a word embedding but potentially also character as well), a bidirectional (possibly stacked) RNN, and an output feed forward.

  Where the three learning classes differ is the risk estimator used in gradient descent training.  PN~will use standard logistic loss.  PU~will use the nnPU risk estimator by Kiryo\etal\ in~\cite{Kiryo:2017}.  PUbN will use~\cite{Hsieh:2019}'s risk estimator.

  \bibliographystyle{ieeetr}
  \bibliography{bib/ref.bib}
\end{document}