\section{Verification}

In this section, two-dimensional DEM models are used to demonstrate the effectiveness of the up-scaling methodology. While the results presented leave a positive impression, future verification using three-dimensional models is desirable. The framework is validated using three tests: 1) The homogenized stress and strain behaviour obtained from the DEM microscale response are compared to that of the macroscale response. This test verifies the effectiveness of the parameter estimation module and the ability of the chosen macroscale constitutive model to capture the salient features of NFR behaviour. 2) The homogenization and parameter estimation algorithms are rerun using the same data, but with different REV sizes to investigate the REV size effect has on the resultant parameter set. 3) Slope stability analyses carried out by both Direct Numerical Simulations (DNS) with a DEM model and with an up-scaled macroscale model are compared. This last test verifies the whole up-scaling methodology.

The up-scaling is conducted by running a series of four quasi-static DEM 'triaxial' compression tests under different confining stresses. These are not true triaxial tests as simulations are in 2D, but illustrate the method regardless. Algorithms are rerun using different REV sizes to determine an appropriate REV size. In a macroscopically homogenous domain, as the REV size increases, the parameter values will converge to a single value, when the REV is too small, local heterogeneities induce a variance into the optimal parameter set.


\section{Strain stratification} \label{strain}
The model is stratified by ``strain'' or Variant of Concern (VoC),
adopting the summer approach to strain stratification through the \code{StrainStratification} object.
This general approach is described in full in the summer model documentation for to represent explicitly multiple competing strains,
each with a separate force of infection calculation (see \code{ModelRunner} object code where force of infection calculations are made and
\code{Stratification} object code where strain stratification can be requested).
This framework allows for independent calculations of the force of infection for each of the modelled strains,
with the compartmental infectiousness multipliers applied identically for all of the competing strains.
Additional strain features (such as differential infectiousness and duration of latency and active disease)
can then be implemented with the standard features of stratification,
such as adjustments to flow rates.

\subsection{Seeding of new VoC}
Seeding of each new strain into the model is achieved through the addition of an importation flow
that introduces a small number of new infectious persons with the new VoC into the model.
To achieve this, a new entry flow is added to the model with destination being the early infectious compartment.
The rate of this flow is a step function that steps from zero to the requested daily rate of seeding for the requested number of days,
before stepping back down to zero for the remainder of the period simulated.


\documentclass[a4paper]{article}

\usepackage{preamble}

\title{Hawkes EM}
\date{\vspace{-5ex}} % avoid date

\begin{document}
\maketitle

The following presents the n-dimensional version of the 1-dimensional algorithm
presented in \citep{lewis2011nonparametric}.

\section{Original paper} % (fold)
\label{sec:original_paper}

\subsection{Notations}
\begin{itemize}
\item $n$ : Number of events in the process
\item $T$ : duration of the time serie
\item $\lambda$ : Hawkes intensity
\item $\mu$ : Hawkes baseline
\item $g$ : Hawkes kernel
\item $i$ or $j$ : some events
\item $t_i$ : the time of event $i$
\item $\delta t$ : the discretization step of the kernel
\item $m$ : an index used for discretzing the kernels $g_d(m\Delta t)$
\item $M$ : number of discretizations of the kernel
\item $M \delta t$ : the size of the support of the kernel
\item $I_m$ : time interval : $[(m-1) \delta t, m \delta t)$
\item $A_m$ : set of all events $(i, j)$ such that $(t_i - t_j) \in I_m$
\item $k$ : algorithm iteration number
\end{itemize}

\subsection{Context} % (fold)
\label{sub:context}

One dimensional Hawkes process with the following intensity
\begin{equation}
\label{eq:one_dimensional_intensity}
	\lambda(t) = \mu + \sum_{t_i < t} g(t - t_i)
\end{equation}
Hence, $\mu$ is the baseline and $g$ is the kernel.
To perform non parametric estimation of $g$ we will suppose the following form for $g$:
\[
	g(t) = \sum_{m=1}^{M} g_m 1_{(t \in I_m)}
\]
Where $\forall m \in \{1 \dots M\}, g_m \in \R$ and $I_m = [(m-1) \delta t, m \delta t)$, where $\delta t$ is
the kernel discretization parameter.

% subsection context (end)

\subsection{Algorithm} % (fold)
\label{sub:algorithm}

\subsubsection{Expectation} % (fold)
\label{ssub:expectation}

At iteration $k$,
the triggering weight (the probabilty that event $i$ was caused by event $j$) is estimated by
\begin{equation}
	\label{eq:expectation_triggering}
	p_{ij}^k = \frac{g^k(t_i - t_j)}{\mu^k + \sum_{l=1}^{i-1} g^k(t_i - t_l)}
\end{equation}
for $t_i < t_j$. The background weight (the probability that $i$ is a background event) is estimated as
\begin{equation}
	\label{eq:expectation_background}
	p_{ii}^k = \frac{\mu^k}{\mu^k + \sum_{l=1}^{i-1} g^k(t_i - t_l)}
\end{equation}

% subsubsection expectation (end)

\subsubsection{Maximization} % (fold)
\label{ssub:maximization}

Knowing that
\begin{align*}
	\int_0^T \lambda(t) dt &= \int_0^T \sum_{t_j < t} g(t - t_j) dt = \int_0^T \sum_{t_j} g(t - t_j) 1_{t_j < t} dt \\
						   &= \sum_{t_j} \int_{t_j}^T g(t - t_j) dt
						    = \sum_{t_j} \int_{t_j}^{\min (T, t_j + M \delta t)} g(t - t_j) dt \\
						   &\approx \sum_{t_j} \sum_{m=1}^M g_m \delta t = n \sum_{m=1}^M g_m \delta t \\
\end{align*}
where $T$ is the duration of the time serie. Approximation is valid as $M \delta t \ll T$.
With intensity specified in~\ref{eq:one_dimensional_intensity}, expectation of complete data log-likelihood is:
\begin{align}
\label{eq:complete_data_loglik_expectation}
	\E[ L(\mu, g)] &= \sum_{i=1}^n p_{ii} \log \mu - \mu T
	             + \sum_{i=2}^n \left[ \sum_{j=1}^{i-1} \left[ p_{ij} \log g(t_i - t_j) \right]
	                                   - \int_{t_j}^T g(t - t_j) dt \right] \\
              &= \sum_{i=1}^n p_{ii} \log \mu - \mu T
                 + \sum_{m=1}^M \sum_{i, j \in A_m} p_{ij} \log g_m
                 - n \sum_{m=1}^M g_m \delta t \\
\end{align}
where $A_m$ is the set of pairs of events such that $(t_i - t_j) \in I_m$.
The background rate is computed as:
\begin{equation}
	\label{eq:maximization_baseline}
	\frac{\partial \E[ L(\mu, g)]}{\partial \mu} = 0
	\quad \Leftrightarrow \quad
	\mu^{k+1} = \frac{1}{T} \sum_{j=1}^{n} p_{jj}^k
\end{equation}
And the updated rates are computed as ($\frac{\partial \E[ L(\mu, g)]}{\partial g_m}$):
\begin{equation}
	\label{eq:maximization_rates}
	\frac{\partial \E[ L(\mu, g)]}{\partial g_m} = 0
	\quad \Leftrightarrow \quad
	g_m^{k+1} = \frac{1}{n \delta t } \sum_{i,j \in A_m} p_{ij}^k
\end{equation}

% subsubsection maximization (end)

% subsection algorithm (end)

% section original_paper (end)

\section{Multi dimensional Hawkes process} % (fold)
\label{sec:multi_dimensional_hawkes_process}

\subsection{Context} % (fold)
\label{sub:multi_context}

Now our hawkes process has $D$ nodes. We replace intensity of equation~\ref{eq:one_dimensional_intensity} by
\begin{equation}
\label{eq:multi_dimensional_intensity}
	\lambda(t) = \sum_{u=1}^{D} \left( \mu^u + \sum_{v=1}^{D} \sum_{t_j^v < t} g^{uv}(t - t_j^v) \right)
\end{equation}
we will still suppose the following form for each $g^{uv}$:
\[
	g^{uv}(t) = \sum_{i=1}^{M} g_m^{uv} 1_{(t \in I_m)}
\]

% subsection context (end)

\subsection{Algorithm} % (fold)
\label{sub:multi_algorithm}

For more clarity we will omit iteration $k$ index in the following equations.

\subsubsection{Expectation} % (fold)
\label{ssub:multi_expectation}

Equation~\ref{eq:expectation_triggering} (the probabilty that event $i$ from node $u$ was caused by event $j$ from node $v$) becomes:
\begin{equation}
	\label{eq:multi_expectation_triggering}
	p_{ij}^{uv} = \frac{g^{uv}(t_i - t_j)}{\mu^u + \sum_{w = 1}^{D} \sum_{t_l^w < t_i} g^{uw}(t_i - t_l^w)}
\end{equation}
where $\{t_l^w < t_i\}$ are events of node $w$ occuring before $t_i$. Equation~\ref{eq:expectation_background} becomes:
\begin{equation}
	\label{eq:multi_expectation_background}
	p_{ii}^{u} = \frac{\mu^u}{\mu^u + \sum_{w = 1}^{D} \sum_{t_l^w < t_i} g^{uw}(t_i - t_l^w)}
\end{equation}
% subsubsection expectation (end)

\subsubsection{Maximization} % (fold)
\label{ssub:multi_maximization}

Complete loglikelihood equation~\ref{eq:complete_data_loglik_expectation} data becomes for each node $u$:
\begin{align*}
\E[ L_u(\mu, g)] &= \sum_{i=1}^n p_{ii}^u \log \mu^u - \mu^u T  \\
	             &\quad +  \sum_{t_i^u}^n \sum_{v=1}^D \sum_{t_j^v < t_i^u} p_{ij}^{uv} \log g^{uv}(t_i^u - t_j^v)
	                                               - \sum_{v=1}^D \sum_{t_j^v} \int_{t_j^v}^T g^{uv}(t - t_j^v) dt \\
                 &= \sum_{i=1}^n p_{ii}^u \log \mu^u - \mu^u T
                    + \sum_{v=1}^D \sum_{m=1}^M \left[ \sum_{i, j \in A_m^{uv}} p^{uv}_{ij} \log g_m^{uv}
                     - n_v g_m^{uv} \delta t \right]
\end{align*}
Where $A_m^{uv}$ is the set of pairs of events such that $(t_i^u - t_j^v) \in I_m$.
We have
\[\E[ L(\mu, g)] = \sum_{u=1}^D \E[ L_u(\mu, g)] \]
Equation~\ref{eq:maximization_baseline} becomes:
\begin{equation}
	\label{eq:multi_maximization_baseline}
	\frac{\partial \E[ L(\mu, g)]}{\partial \mu^u} = 0
	\quad \Leftrightarrow \quad
	\mu^u = \frac{1}{T} \sum_{i=1}^{n} p_{ii}^u
\end{equation}
and equation~\ref{eq:maximization_rates} becomes:
\begin{equation}
	\label{eq:multi_maximization_rates}
	\frac{\partial \E[ L(\mu, g)]}{\partial g_m^{uv}} = 0
	\quad \Leftrightarrow \quad
	g_m^{uv} = \frac{1}{n_v \delta t } \sum_{i, j \in A_m^{uv}} p_{ij}^{uv}
\end{equation}

% subsubsection maximization (end)

% section multi_dimensional_hawkes_process (end)

\newpage

\section{Efficient implementation} % (fold)
\label{sec:efficient_implementation}

On each node $u$ we can compute directly $p_{ij}^{uv}$ and the following $g^{uv}_m$ with this procedure:
\begin{algorithmic}[1]
\REQUIRE $g^u$ $D \times M$ array used to compute $g^{uv}_m$
\FOR {$i=n_u \ldots 1$}
	\STATE {$\text{norm}_u \gets 0$}
	\STATE {$\text{next}\_\hat{g}^u \gets (0, \dots, 0)$}
	\FOR {$v=1 \ldots D$}
		\STATE {$j \gets$ the biggest $j$ such that $t_j \le t_i$} \label{op:find_tj}
		\WHILE {$j \geq 0$}
			\IF {$t_i - t_j \geq M \delta t$}
				\STATE {break}
				\COMMENT{Events are too far away, out of kernel support}
			\ENDIF
			\IF {$u = v$ \AND $t_i = t_j$}
				\STATE {$\text{norm}_u \mathrel{{+}{=}} \mu^u$}
			\ELSE
				\STATE {$m = \lfloor \frac{t_i - t_j}{\delta t} \rfloor $}
				\STATE {$\text{next}\_\hat{g}_m^{uv} \mathrel{{+}{=}} g_m^{uv} $}
				\COMMENT {$g_m^{uv}$ is equal to $g^{uv}(t_i - t_j)$}
				\STATE {$\text{norm}_u \mathrel{{+}{=}} g_m^{uv}$}
			\ENDIF
			\STATE {$j \mathrel{{-}{=}} 1$}
		\ENDWHILE
	\ENDFOR \\
	\COMMENT {At this point $\text{norm}_u = \mu^u + \sum_{w=1}^D \sum_{t_l^w < t_i} g^{uw} (t_i - t_l^w)$}
	\STATE {$\text{next}\_\mu^u \mathrel{{+}{=}} \frac{\mu^u}{\text{norm}_u} $}
	\COMMENT {This equals $p^u_{ii}$}
	\FOR {$v=1 \ldots D$}
		\STATE {$\text{next}\_g^{uv}_m \mathrel{{+}{=}} \frac{\text{next}\_\hat{g}_m^{uv}}{\text{norm}_u n_v \delta t}$}
		\COMMENT {This equals $p^{uv}_{ij} / n_v \delta t$}
	\ENDFOR
\ENDFOR
\end{algorithmic}
%
This algorithm can be launched in parallel on all node $u$ and will result in one step of expectation-maximization.
We loop in reverse order such that line~\ref{op:find_tj} can be executed very quickly by storing last indices of each
node $v$ in memory.

% section efficient_implementation (end)

\bibliographystyle{plainnat}
\bibliography{ref}

\end{document}


\chapter{Abstract}
GenOpt is an optimization program for the minimization
of a cost function that is evaluated by an external simulation program.
It has been developed for optimization problems where the cost function
is computationally expensive and its derivatives are not available or
may not even exist.
GenOpt can be coupled to any simulation program
that reads its input from text files
and writes its output to text files.
The independent variables can be continuous variables
(possibly with lower and upper bounds), discrete variables, or both,
continuous and discrete variables.
Constraints on dependent variables can be implemented using penalty
or barrier functions.
GenOpt uses parallel computing to evaluate the simulations.

GenOpt has a library with local and global multi-dimensional and
one-dimensional optimization algorithms,
and algorithms for doing parametric runs.
An algorithm interface allows adding
new minimization algorithms without
knowing the details of the program structure.

GenOpt is written in Java so that it is platform independent.
The platform independence and the general interface make GenOpt applicable
to a wide range of optimization problems.

GenOpt has not been designed for linear programming problems,
quadratic programming problems, and problems
where the gradient of the cost function is available. For such problems,
as well as for other problems,
special tailored software exists that is more efficient.

% ---------------------------------------------------------------
\chapter{Notation}
\begin{enumerate}
\item
We use the notation $a \triangleq b$ to denote that $a$ is equal to $b$ by definition.
We use the notation $a \leftarrow b$ to denote that $a$ is assigned the value
of $b$.
\item
$\Re^n$ denotes the Euclidean space of $n$-tuplets of real numbers.
Vectors $x \in \Re^n$ are always column vectors,
and their elements are denoted by superscripts.
The inner product in $\Re^n$ is denoted by $\langle \cdot, \cdot \rangle$ and
for $x,y \in \Re^n$
defined by $\langle x, y \rangle \triangleq \sum_{i=1}^n x^i \, y^i$.
The norm in $\Re^n$ is denoted by $\|\cdot\|$ and
for $x \in \Re^n$ defined by $\|x\|\triangleq \langle x, x \rangle^{1/2}$.
\item
We denote by $\mathbb Z$ the set of integers, by $\mathbb Q$
the set of rational numbers, and by $\mathbb N \triangleq \{0, \, 1,
\, \ldots \}$ the set of natural numbers. The set
$\mathbb N_+$ is defined as $\mathbb N_+ \triangleq \{1, \, 2, \,
\ldots \}$.
Similarly, vectors in $\Re^n$ with strictly positive elements are denoted by
$\Re_+^n \triangleq \{ x
\in \Re^n \ | \  x^i > 0, \ i \in \{ 1, \ldots , n \} \, \}$
and the set $\mathbb Q_+$
is defined as $\mathbb Q_+ \triangleq \{ q \in \mathbb Q \ | \ q > 0 \}$.
\item
Let $\mathbb W$ be a set containing a sequence
$\{ w_i \}_{i=0}^k$.
Then, we denote by $\underline w_k$ the sequence
$\{ w_i \}_{i=0}^k$ and by $\underline{\mathbf W}_k$ the set
of all $k+1$ element sequences in $\mathbb W$.
\item
If $\mathbf A$ and $\mathbf B$ are sets,
we denote by $\mathbf A \cup \mathbf B$ the union of $\mathbf A$ and $\mathbf B$ and
by $\mathbf A \cap \mathbf B$ the intersection of $\mathbf A$ and $\mathbf B$.
\item
If $\mathbf S$ is a set,
we denote by $\overline {\mathbf S}$ the closure of $\mathbf S$ and
by $2^{\mathbf S}$ the set of all
nonempty subsets of $\mathbf S$.
\item
If $\widehat D \in \mathbb Q^{n \times q}$ is a matrix,
we will use the notation $\widehat d \in \widehat D$
to denote the fact that $\widehat d \in \mathbb Q^n$ is a
column vector of the matrix $\widehat D$. Similarly, by
$D \subset \widehat D$ we mean that $D \in
\mathbb Q^{n \times p}$ ($1 \le p \le q$) is a matrix containing only
columns of $\widehat D$. Further, $\operatorname{card}(D)$ denotes the number
of columns of $D$.
\item
$f(\cdot)$ denotes a function where $(\cdot)$ stands for the undesignated variables. $f(x)$ denotes the value of $f(\cdot)$ at the point $x$. $f\colon A \rightarrow B$ indicates that the domain of $f(\cdot)$ is in the space $A$ and its range in the space $B$.
\item
We say that a function $f \ \colon \ \Re^n \to \Re$ is once continuously differentiable
if $f(\cdot)$ is defined on $\Re^n$,
and if $f(\cdot)$ has continuous derivatives on $\Re^n$.
\item
For $x^* \in \Re^n$ and $f \colon \Re^n \to \Re$ continuously differentiable,
we say that $x^*$ is stationary if $\nabla f(x^*) = 0$.
\item
We denote by $\{ e_i \}_{i=1}^n$ the unit vectors in $\mathbb R^n$.
\item
We denote by $\rho \sim U(0,1)$ that $\rho \in \Re$
is a uniformly distributed random number, with $0 \le \rho \le 1$.
\end{enumerate}
% ---------------------------------------------------------------

\chapter{Introduction}
The use of system simulation for analyzing complex engineering problems is increasing.
Such problems typically involve many independent variables\footnote{The independent
variables are the variables that are varied by the optimization algorithm
from one iteration to the next.
They are also called design parameters or free parameters.},
and can only be optimized by
means of numerical optimization.
Many designers use parametric studies to achieve better performance
of such systems,
even though such studies typically yield only partial improvement while requiring
high labor time.
In such parametric studies, one usually fixes all but one variable
and tries to optimize a cost function\footnote{The cost function is the function being optimized.
The cost function measures a quantity that should be minimized, such as
a building's annual operation cost,
a system's energy consumption, or
a norm between simulated and measured values in a data fitting process.
The cost function is also called objective function.}
with respect to the non-fixed variable.
The procedure is repeated iteratively by varying another variable.
However, every time a variable is varied, all other variables typically become non-optimal and hence need also to be adjusted.
It is clear that such a manual procedure is very time-consuming and often impractical for more than two or three independent variables.\\

GenOpt, a generic optimization program, has been developed
to find with less labor time the independent variables that
yield better performance of such systems.
GenOpt does optimization of a user-supplied cost function,
using a user-selected optimization algorithm.\\

In the most general form, the optimization problems addressed by GenOpt
can be stated as follows:
Let $\mathbf X$ be a user-specified constraint set, and
let $f \colon \mathbf X \to \Re$ be a user-defined cost function
that is bounded from below.
The constraint set $\mathbf X$ consists of all possible design options,
and the cost function $f(\cdot)$ measures the system performance.
GenOpt tries to find a solution to the problem\footnote{
If $f(\cdot)$ is discontinuous, it may only have an infimum
(i.e., a greatest lower bound) but no minimum even if the constraint set
$\mathbf X$ is compact.
Thus, to be correct,~\eqref{eq:optProMosGen}
should be replaced by $\inf_{x \in \mathbf X} f(x)$.
For simplicity, we will not make this distinction.}
\begin{equation}
  \min_{x \in \mathbf X} f(x).
\label{eq:optProMosGen}
\end{equation}
This problem is usually ``solved'' by iterative methods,
which construct infinite sequences, of progressively better approximations
to a ``solution'', i.e., a point that satisfies an optimality condition.
If $\mathbf X \subset \Re^n$, with some $n \in \Na$,
and $\mathbf X$ or $f(\cdot)$ is not convex,
we do not have a test for global optimality,
and the most one can obtain is a point that satisfies a local optimality condition.
Furthermore, for $\mathbf X \subset \Re^n$, tests for optimality are based on
differentiability assumptions of the cost function.
Consequently,
optimization algorithms can fail, possibly far from a solution,
if $f(\cdot)$ is not differentiable in the continuous independent variables.
Some optimization algorithms are more likely to fail at discontinuities than others.
GenOpt has algorithms that are not very sensitive to (small) discontinuities
in the cost function, such as Generalized Pattern Search algorithms,
which can also be used in conjunction with heuristic global optimization algorithms.\\

Since one of GenOpt's main application fields is building energy use or
operation cost optimization,
GenOpt has been designed such that it addresses the special properties
of optimization problems in this area.
In particular, GenOpt is designed for optimization problems with the following properties:
\begin{enumerate}
\item
The cost function may have to be defined on
approximate numerical solutions of
differential algebraic equations,
which may fail to be continuous
(see Section~\ref{sec:proAppCosFun}).
\item
The number of independent variables is small.\footnote{By small,
we mean on the order of $10$, but the maximum number of independent variables
is not restricted in GenOpt.}
\item
Evaluating the cost function requires much more computation time
than determining the values for the next iterate.
\item
Analytical properties of the cost function
(such as formula for the gradient) are not available.
\end{enumerate}

\noindent GenOpt has the following properties:
\begin{enumerate}
\item
GenOpt can be coupled to any simulation program that calculates
the cost function
without having to modify or recompile either program,
provided that the simulation program reads its input from text files
and writes its output to text files.
\item
The user can select an optimization algorithm from an algorithm library,
or implement a custom algorithm without having
to recompile and understand the whole optimization environment.
\item
GenOpt does not require an expression for the gradient of the cost function.
\end{enumerate}

With GenOpt, it is easy to couple a new simulation program,
specify the optimization variables and minimize the cost function.
Therefore, in designing complex systems, as well as in system analysis,
a generic optimization program like GenOpt offers valuable assistance.
Note, however, that optimization is not easy:
The efficiency and success of an optimization is strongly affected by the properties and
the formulation of the cost function, and by the selection of an appropriate optimization algorithm.\\

This manual is structured as follows:
In Section~\ref{sec:optProGenOpt}, we classify optimization problems
and discuss which of GenOpt's algorithms can be used for each of these problems.
Next, we explain the algorithms that are implemented in GenOpt:
In Section~\ref{sec:algImp}, we discuss the algorithms
for multi-dimensional optimization;
in Section~\ref{sec:algOneDimOpt} the algorithms for
one-dimensional optimization;
and
in Section~\ref{sec:algParRun} the algorithms for parametric runs.
In Section~\ref{cha:conGen}, we discuss how constraints on independent variables are
implemented, and how constraints on dependent variables can be implemented.
In Section~\ref{sec:proIns}, we explain
the structure of the GenOpt software,
the interface for the simulation program and
the interface for the optimization algorithms.
How to install and start GenOpt is described in Section~\ref{sec:InsAndRunGen}.
Section~\ref{sec:SetUpOptPro} shows how to set up the configuration and input files,
and how to use GenOpt's pre- and post-processing capabilities.
% ---------------------------------------------------------------


%!TEX root = paper.tex
%% \section{\DIs}

\def\Inv{\mathtt{Inv}}
\def\Dom{\mathtt{Dom}}
\def\coDom{\mathtt{coDom}}
\def\DDom{\mathbf{Dom}}
\def\CC{\mathcal{C}}
\def\Real{{\mathbb{R}}}
%\def\dist{{\mathtt{qmem}}}

\def\Bool{{\mathtt{Bool}}}
\def\true{{\mathtt{True}}}
\def\false{{\mathtt{False}}}
\def\vecw{{\vec{w}}}
\def\vecv{{\vec{v}}}
\def\lb{{\mathtt{lb}}}
\def\ub{{\mathtt{ub}}}
\def\atomic{{atomic}}
\def\Atomic{{Atomic}}
%\def\M{{\infty}}
%\def\Conj{{\varphi}}
%\def\Disj{{\Phi}}
\newcommand{\semq}[1]{{[\![{#1}]\!]}}
%\newcommand\avg[1]{{\overline{#1}}}
\newcommand\avg[1]{{\mathtt{\mu}}(#1)}
\newcommand\sign[1]{{\mathtt{sign}({#1})}}
\newcommand\delF[2]{{\Delta{#1}({#2})}}
\newcommand\stddev[1]{{\mathtt{\sigma}}(#1)}
\def\monthy{M}

In this section,
%we define \dis that allow us to capture complex arithmetic dependencies involving numerical attributes of a dataset.
we first provide the general definition of \dis. Then we propose a language for
representing them. Finally, we define quantitative semantics over \dis, which
allows us to quantify their violation.

% We present one possible \emph{language} for representing \dis,
% and attach a \emph{quantitative semantics} to it.
% The quantitative semantics, intuitively, computes the degree of
% \invariant violation on data.
%Finally, we provide a principle for synthesizing effective \dis.
% Finally, we discuss how we prioritize
% \invariants during quantitative semantics computation based on their
%effectiveness.

\smallskip
% \noindent \textbf{tuple, attribute, and Dataset.}

\looseness-1
\paragraph{Basic notation.}
%
We use $\mathcal{R} (A_1, A_2, \dots, A_m)$ to denote a relation schema where
$A_i$ denotes the $i^{th}$ attribute of $\mathcal{R}$. We use $\Dom_i$ to
denote the domain of attribute $A_i$. Then the set ${\DDom}^m=\Dom_1\times
\cdots\times \Dom_m$ specifies the domain of all possible tuples. We use $t
\in\DDom^m$ to denote a tuple in the schema $\mathcal{R}$. A dataset
$D\subseteq {\DDom}^m$ is a specific instance of the schema $\mathcal{R}$. For
ease of notation, we assume some order of tuples in $D$ and we use $t_i \in D$
to refer to the $i^{th}$ tuple and $t_i.A_j \in \Dom_j$ to denote the value of
the $j^{th}$ attribute of $t_i$.

\ignore{
\paragraph{Tuple, Dataset, and Attribute.} Let $\Dom_1, \Dom_2, \ldots, \Dom_n$
denote some $n$ domains for values. We fix the set $\Dom_1\times \cdots\times
\Dom_n$ (written as ${\DDom}^m$) as the domain of all {\em{tuples}}. Each
\emph{tuple} $d\in\DDom^m$ is an $n$-tuple, where each component of the tuple
is an {\em{attribute}}.\footnote{\scriptsize{In this work, we use the term
``attribute'' to denote a single attribute.}} We use $A_1,\ldots,x_n$ to denote
the names of these $n$ attributes. We use $d_{x_j}\in \Dom_j$ to denote the
value of the $j^{th}$ attribute of $d$. A {\em{dataset}} $D\subseteq {\DDom}^m$
is a sequence of tuples, where the $i^{th}$ tuple is denoted using $d^{(i)}$.
\endignore
}

%We differentiate between two types of attributes in a dataset, namely
%categorical and numerical. An attribute with domain $\Dom_i$ is categorical if
%$\Dom_i$ is a finite discrete set with small (e.g., up to 50) cardinality. The
%domain of a numerical attribute is the set of numbers.

% \sloppy
% \begin{definition}[\Di]\label{def:di}
%      %
%      A \di for a dataset $D \,{\subseteq}\, {\DDom}^m$ is another
%      dataset $\Inv \,{\subseteq}\, {\DDom}^m$ s.t. $D - {D'} \,{\subseteq}\,
%      \Inv$, and $\frac{|D'|}{|D|} \le \delta$.
%      %
% \end{definition}

\ignore{
\smallskip
\looseness-1 We start with a \emph{strict} definition of \dis, and then explain how it generalizes to account for noise in the data.
%
\begin{definition}[\Di (strict)]\label{def:di}
     %
     A \di for a dataset $D \,{\subseteq}\, {\DDom}^m$ is a formula $\Phi:
     \DDom^m \mapsto \{\true,\false\}$ such that $\forall t\in D,
     \Phi(t) = \true$.
     %
\end{definition}
%

\looseness-1
A \di ($\Phi$) characterizes a set of allowable or conforming tuples and is
expressed through a \emph{conformance language} (Section~\ref{sec:cl}). We write
$\Phi(t)$ and $\neg\Phi(t)$ to denote that $t$ satisfies and violates the
\invariant $\Phi$, respectively. In practice, because of noise, some tuples in
$D$ may violate a \invariant. To account for noise, we relax the definition of
\di as follows.
\endignore}
% %
% \looseness-1 We start with a formal definition of \dis, which is a formula
% expressed in a \emph{conformance language} (Section~\ref{sec:cl}),
% that is satisfied by almost all tuples in $D$.

\smallskip
% \noindent \textbf{tuple, attribute, and Dataset.}

\looseness-1 \paragraph{\Di.} A \di $\Phi$ characterizes a set of allowable or
conforming tuples and is expressed through a \emph{conformance language}
(Section~\ref{sec:cl}). We write $\Phi(t)$ and $\neg\Phi(t)$ to denote that $t$
satisfies and violates $\Phi$, respectively.

\begin{definition}[\Di]\label{def:di2}
     %
     A \di for a dataset $D \,{\subseteq}\, {\DDom}^m$ is a formula
     $\Phi: {\DDom}^m \mapsto \{\true,\false\}$ such that $|\{t \in D \mid
     \neg\Phi(t)\}| \ll |D|$.
	 %
\end{definition}
%
The set $\{t \in D \mid \neg\Phi(t)\}$ denotes atypical tuples in $D$ that do
not satisfy the \di $\Phi$. In our work, we do not need to know the set of
atypical tuples, nor do we need to purge the atypical tuples from the dataset.
Our techniques derive \invariants in ways that ensure there are very few
atypical tuples (Section~\ref{sec:synth-data-inv}).

\subsection{Conformance Language}\label{sec:cl}
%
\looseness-1 \paragraph{\View.} A central concept in our conformance language
is \linebreak \emph{\view}. Intuitively, a \view is a derived attribute that
specifies a ``lens'' through which we look at the tuples. More formally, a
\view is a function $F: \DDom^m \mapsto\Real$ that maps a tuple $t\in\DDom^m$
to a real number $F(t) \in \Real$. In our language for \dis, we only consider
\views that correspond to linear combinations of the numerical attributes of a
dataset. Specifically, to define a \view, we need a set of numerical
coefficients for all attributes of the dataset and the \view is defined as a
sum over the attributes, weighted by their corresponding coefficients. We extend
a projection $F$ to a dataset $D$ by defining $F(D)$ to be the sequence of
reals obtained by applying $F$ on each tuple in $D$ individually.

\smallskip

{
%\setlength{\abovedisplayskip}{0pt}
%\setlength{\belowdisplayskip}{0pt}
\paragraph{Grammar.} Our language for \dis consists of formulas
$\Phi$ generated by the following grammar:
%
{
\begin{eqnarray*}
    \phi & := & \lb \leq F(\vec{A}) \leq \ub \; \mid\; \wedge(\phi,\ldots,\phi)
    \\[-0.3em]
    \psi_A & := & \vee(\;(A = c_1) \maxand \phi,\; (A = c_2) \maxand \phi, \;\ldots)
    \\[-0.3em]
    \Psi & := & \psi_A \; \mid \; \wedge(\psi_{A_1}, \psi_{A_2}, \ldots)
	\\[-0.3em]
    \Phi & := & \phi  \;\mid\; \Psi
\end{eqnarray*}
}
%
% The language consists of
% (1)~equality constraints $A_j = c_j$ where $A_j$ is an attribute and $c_j \in \Dom_j$ is a concrete value;
% (2)~bounded \view constraints $\lb \leq F(\vec{A}) \leq \ub$ where
%     $F$ is a \view on $\DDom^m$, $\vec{A}$ is the tuple of formal
%     parameters $(A_1, A_2, \ldots, A_m)$, and $\lb, \ub \in \Real$ are reals; and
% (3)~their conjunctions ($\wedge$) and disjunctions ($\vee$).
%   %
  %
\looseness-1
\noindent
The language consists of (1)~bounded constraints $\lb \leq F(\vec{A})\leq \ub$
where $F$ is a \view on $\DDom^m$, $\vec{A}$ is the tuple of formal parameters
$(A_1, A_2, \ldots, A_m)$, and $\lb, \ub \in \Real$ are reals; (2)~equality
constraints $A = c$ where $A$ is an attribute and $c$ is a constant in
$A$'s domain; and (3)~operators ($\maxand$, $\wedge$, and $\vee$,) that connect
the constraints.
}
%where the first two are intuitively conjunctive and  and $\wedge$ are two conjunction operators and $\vee$ is a disjunctions ($\vee$).
  % conjunction $\wedge$ and disjunction $\vee$ operators are variable arity operators.
Intuitively, $\maxand$ is a switch operator that specifies which \invariant $\phi$
applies based on the value of the attribute $A$, $\wedge$ denotes conjunction,
and $\vee$ denotes disjunction.
% $\vee$ and $\wedge$ are the usual Boolean operators. We allow an
% equality $x = c$ to be part of a \di only when $x$ is categorical.
Formulas generated by $\phi$ and $\Psi$ are called {\em{simple \invariants}}
and {\em{compound \invariants}}, respectively. Note that a formula generated by
$\psi_A$ only allows equality constraints on a single attribute, namely $A$,
among all the disjuncts.
% Then $\Psi$ generates a
% \emph{compound \invariant} which is a conjunction over a set of disjunctive
% \invariants (on different attributes). Finally, $\Phi$ gives either a
% conjunctive \invariant or a compound \invariant.

\begin{example}\label{ex:constraints}
    Consider the dataset $D$ consisting of the first four tuples  $\{t_1, t_2, t_3, t_4\}$ of Fig.~\ref{fig:flights}.
    A simple \invariant for $D$ is:
	{
	$$
    \phi_1: -5\leq \mathtt{AT} - \mathtt{DT} - \mathtt{DUR} \leq 5.
	$$
	}
	%
    Here, the projection ${F(\vec{A})} = \mathtt{AT} - \mathtt{DT} - \mathtt{DUR}$, with attribute coefficients
    $\langle 1, - 1, -1 \rangle$, $\lb = {-}5$, and $\ub = 5$. A compound
    \invariant is:
	%
	{
	\begin{align*}
     \psi_2:   \mathtt{\monthy} &= \text{``May''}  \maxand -2 \leq \mathcolorbox{lightgray}{\mathtt{AT} - \mathtt{DT} - \mathtt{DUR}} \leq 0 \\[-0.4em]
     \vee \;\; \mathtt{\monthy} &= \text{``June''} \maxand \phantom{-}0  \leq \mathcolorbox{lightgray}{\mathtt{AT} - \mathtt{DT} - \mathtt{DUR}} \leq 5\\[-0.4em]
     \vee \;\; \mathtt{\monthy} &= \text{``July''} \, \maxand {-}5 \leq \mathcolorbox{lightgray}{\mathtt{AT} - \mathtt{DT} - \mathtt{DUR}} \leq 0
	\end{align*}
	}
	 %
     For ease of exposition, we assume that all times are converted to minutes
     (e.g., \texttt{06:10} $= 6 \times 60 + 10 = 370$) and $\monthy$ denotes
     the departure month, extracted from $Departure$ $Date$.
	 %
\end{example}

Note that arithmetic expressions that specify linear combination of numerical
attributes (highlighted above) are disallowed in denial
constraints~\cite{DBLP:journals/pvldb/ChuIP13} which only allow raw attributes
and constants (more details are in \appOrTechRep).

\subsection{Quantitative Semantics}\label{quant-sem}

\looseness-1 \Dis have a natural Boolean semantics: a tuple either satisfies a
\invariant or it does not. However, Boolean semantics is of limited use in
practice, because it does not quantify the degree of \invariant violation. We
interpret \dis using a quantitative semantics, which quantifies violations, and
% to handle noisy data.
% This is important because data can be noisy and
%Quantitative semantics has the additional benefit that it
reacts to noise more gracefully than Boolean semantics.

%Given a formula $\Phi$,
% and a tuple $d$,
The quantitative semantics $\semq{\Phi}(t)$ is a measure of the violation of
$\Phi$ on a tuple $t$---with a value of $0$ indicating no violation and a value
greater than 0 indicating some violation. In Boolean semantics, if $\Phi(t)$ is
$\true$, then $\semq{\Phi}(t)$ will be $0$; and if $\Phi(t)$ is $\false$, then
$\semq{\Phi}(t)$ will be $1$. Formally, $\semq{\Phi}$ is a mapping from
$\DDom^m$ to $[0,1]$.

\smallskip

\noindent\emph{Quantitative semantics of simple \invariants.}
%
\revisethree{We build upon $\epsilon$-insen\-sitive loss~\cite{vapnik1997support}
to define the quantitative semantics of simple \invariants, where the bounds
$\lb$ and $\ub$ define the $\epsilon$-insensitive
zone:}\footnote{\revisethree{For a target value $y$, predicted value $\hat{y}$,
and a parameter $\epsilon$, the $\epsilon$-insensitive loss is $0$ if $|y -
\hat{y}| < \epsilon$ and $|y - \hat{y}| - \epsilon$ otherwise.
% $\epsilon$-insensitive loss is defined by $
% %
%         \begin{cases}
%         0, & \text{if } |y - \hat{y}| < \epsilon\\
%         |y - \hat{y}| - \epsilon, & \text{otherwise}
%         \end{cases}
%   $
}}\label{fn1}
%
\begin{align*}
    \semq{\lb\leq F(\vec{A})\leq \ub}(t) &:= \eta(\alpha \cdot \max(0, F(t)-\ub, \lb - F(t)))
    \\
    \semq{\wedge(\phi_1,\ldots,\phi_K)}(t) &:= \textstyle\sum_{k}^{K} \gamma_k \cdot \semq{\phi_k}(t)
\end{align*}
%
Below, we describe the parameters of the quantitative semantics, and provide further details on them in \appOrTechRep.
% The quantitative semantics uses the following parameters:\footnote{More details on system parameters are in \appOrTechRep.}
% \begin{itemize}
%   \item a \emph{scaling factor} $\alpha\in\Real^+$,
%   \item a \emph{normalization function} $\eta(.): \Real\mapsto [0,1]$, and
%   \item \emph{importance factors} $\gamma_k \in \Real^+$ s.t. $\sum \gamma_k = 1$.
% \end{itemize}
% %and $\gamma_i$'s---which are non-negative real numbers whose sum is $1$---denote \emph{importance factors}.
% We discuss these parameters next.

\smallskip
% \sloppy \noindent \textbf{Scaling factor $\alpha$.}
\paragraph{Scaling factor $\alpha\in\Real^+$.}\\ \Views are unconstrained
functions and different \views can map the same tuple to vastly different
values. We use a scaling factor $\alpha$ to standardize the values computed by
a \view $F$, and to bring the values of different \views to the same comparable
scale. The scaling factor is automatically computed as the inverse of the
standard deviation, $\alpha = \tfrac{1}{\stddev{F(D)}}$.
We set $\alpha$ to a large positive number when $\stddev{F(D)}$ = $0$.

% A typical choice---inspired from the classical z-score normalization---is the inverse of standard deviation
% of $F(D)$, i.e., $1/\stddev{F(D)}$.\footnote{\scriptsize{When $\stddev{F(D)}$ = $0$, we use a large positive number $\M$ as the scaling factor.}}

\smallskip
% \sloppy \noindent \textbf{Normalization function $\eta$.}
\paragraph{Normalization function $\eta(.): \Real\mapsto [0,1]$.}\\
The normalization function
maps values in the range $[0,\infty)$ to the range $[0,1)$.
While any monotone mapping from $\Real^{\geq 0}$ to $[0,1)$ can be used, we
pick $\eta(z) = 1 - e^{-z}$.

\smallskip
% \noindent\textbf{Importance factors $\gamma_i$.}
\looseness-1
\paragraph{Importance factors $\gamma_k\in \Real^+$, $\textstyle\sum_{k}^{K}\gamma_k {=} 1$.}\\
The weights $\gamma_k$ control the contribution of each bounded-\view \invariant in a
conjunctive formula. This allows for prioritizing \invariants that are more
significant than others.
% within the context of a particular application.
In our work, we derive the importance factor of a \invariant automatically,
based on its \view's standard deviation over $D$.

\subsubsection*{Quantitative semantics of compound \invariants}
Compound \invariants are first simplified into simple \invariants, and they get
their meaning from the simplified form.
We define a function
$\norm(\psi, t)$ that takes a compound \invariant $\psi$ and a tuple $t$ and returns a simple
\invariant. It is defined recursively as follows:
\begin{align*}
    &\norm(\vee(\;(A = c_1) \maxand\phi_1, (A = c_2) \maxand \phi_2,\ldots), t) :=
      \phi_k  \; \mbox{if $t.A = c_k$}
    \\
    &\norm(\wedge(\psi_{A_1}, \psi_{A_2},\ldots), t)  := \wedge(\norm(\psi_{A_1}, t), \norm(\psi_{A_2},t),\ldots)
\end{align*}
%
\begin{sloppypar} If the condition in the definition above does not hold for any $c_k$, then
$\norm(\psi,t)$ is undefined and $\norm(\wedge(\dots,\psi,\dots),t)$ is
also undefined. If $\norm(\psi,t)$ is undefined, then $\semq{\psi}(t) :=
1$. When $\norm(\psi,t)$ is defined, the quantitative semantics of $\psi$ is
given by:
$$
\semq{\psi}(t) := \semq{\norm(\psi,t)}(t)
$$
\end{sloppypar}

Since compound \invariants simplify to simple \invariants, we mostly focus on
simple \invariants. Even there, we pay special attention to bounded-projection
\invariants ($\phi$) of the form \linebreak $\lb \le F(\vec{A}) \le \ub$, which lie at the core of simple \invariants.

\begin{example}\label{ex:semantics}
	%
    Consider the \invariant $\phi_1$ from Example~\ref{ex:constraints}.
	%
    % denoting $-5\leq F(\vec{A}) \leq 5$, where $F(\vec{A}) = AT - DT -
%     DUR$, over the database $D$ consisting of the first four tuples,
%     $\{t_1, t_2, t_3, t_4\}$, shown in Fig.~\ref{fig:flights}.
	%
	For $t \in D$,
    $\semq{\phi_1}(t)= 0$ since $\phi_1$ is satisfied by all tuples in $D$. The
    standard deviation of the projection $F$ over $D$, $\sigma(F(D)) {=}
    \sigma(\{0,-5, 5, -2\}) {=} 3.6$. Now consider the last tuple $t_5 \not\in
    D$. $F(t_5) = (370 - 1350)-458 = -1438$, which is way below the lower bound $-5$ of $\phi_1$.
	Now we compute how much $t_5$ violates $\phi_1$:
	$\semq{\phi_1}(t_5) = \semq{-5 \leq F(\vec{A}) \leq 5}(t_5)
		 			   = \eta(\alpha \cdot \max(0, -1438 - 5, -5 + 1438)) = 1 - e^{-\frac{1433}{3.6}} \approx 1$.
%		 			   &= 1 - e^{-\frac{1433}{\sigma(F(D))}} = 1 - e^{-\frac{1433}{3.6}} \approx 1
%	\end{align*}
	%
	Intuitively, this implies that $t_5$ strongly violates $\phi_1$.
	%
\end{example}