\section{Overview}
\label{sec:overview}

%first introduce the question
In \autoref{sec:running-ex}, we describe a recursion-free CHC system,
$\mcchc$ (\autoref{fig:chc}), that models the safety of the program
\cc{dblAbs} (\autoref{fig:multicall-code}).
%
In \autoref{sec:solve-ex}, we show that $\mcchc$ is a CDD system and
how \sys can solve it by encoding it into binary interpolants.
%
In \autoref{sec:not-in}, we illustrate that $\mcchc$ is not in
directly solvable classes introduced in previous work.
%

% introduce running example program
\subsection{Verifying \cc{dblAbs}: an example hierarchical program}
\label{sec:running-ex}

% include code of running example and some figure:
\begin{figure}[t]
  \centering
  \begin{floatrow}[2]
    \ffigbox[.3\textwidth] {%
      \caption{\cc{dblAbs}: an example hierarchical program.} %
      \label{fig:multicall-code} }
    {
      \lstinputlisting[
        basicstyle=\small,
        % xleftmargin=\parindent,
        xleftmargin=2em,framexleftmargin=1.5em,
        numbers=left,
        escapeinside={(*@}{@*)},
        morekeywords={def, return, if, else, assert}]
        {code/dblAbs.java}
    }
    %
    \ffigbox[.66\textwidth]{%
      \caption{A CHC system that models the safety condition of $\cc{dblAbs}$,
    named $\mcchc$.}}{%
      \label{fig:chc}
      \begin{align}
        % semantic constraint of dbl:
        \cc{dbl}(\cc{x}, \cc{d}) &\gets \cc{d} = 2 * \cc{x} \\
        % semantic constraint of line 3:
        \cc{L}_4(\cc{n}, \cc{abs}) &\gets  \cc{abs} = 0 \\
        % semantic constraint of line 5 (then branch)
        \cc{L}_6(\cc{n}, \cc{abs}) &\gets \cc{L}_4(\cc{n}, \cc{abs}) \land \cc{n} \ge 0 \\
        % semantic constraint of line 6 (else branch)
        \cc{L}_8(\cc{n}, \cc{abs}) &\gets \cc{L}_4(\cc{n}, \cc{abs}) \land \cc{n} < 0 \\
        % assign branch
        \cc{L}_9(\cc{n}, \cc{abs'}) &\gets \cc{L}_6(\cc{n},\cc{abs}) \land \cc{abs'} = \cc{n} \\
        \cc{L}_9(\cc{n}, \cc{abs'}) &\gets \cc{L}_8(\cc{n},\cc{abs}) \land \cc{abs'} = -\cc{n} \\
        % semantic constraint of main procedure:
        \cc{main}(\cc{n},\cc{res}) &\gets \cc{L}_9(\cc{n}, \cc{abs'}) \land
        \cc{dbl}(\cc{x}, \cc{d})
        \land \cc{abs'} = \cc{x} \land \cc{res} = \cc{d} \\
        %
        False &\gets \cc{main}(\cc{n}, \cc{res}) \land \cc{res} < 0
      \end{align}
    }
  \end{floatrow}
\end{figure}
% walk through code of running example:
%
\cc{dblAbs} is a procedure that doubles the absolute value of
its input and stores the result in \cc{res}.
% state the safety problem:
The program also asserts that \cc{res} is greater than or equal to $0$
before exiting.
% talk about solving Horn Clauses:
Verifying this assertion reduces to solving a recursion-free
CHC system over a set of uninterpreted predicates that represent
the control locations in \cc{dblAbs}.
%
In particular, one such system, $\mcchc$, is shown in
\autoref{fig:chc}.
%
%
While $\mcchc$ has been presented as the result
of a translation from \cc{dblAbs}, \sys is
purely a solver for CHC systems: it does not require access to the
concrete representation of a program, or for a given CHC system to be
the result of translation from a program at all.

% solving the running example
\subsection{$\mcchc$ as a Clause-Dependence Disjoint System}
\label{sec:solve-ex}
% state it is CDD
The recursion-free CHC system $\mcchc$ is a
\emph{Clause-Dependence Disjoint} (CDD) system.
% state the defn of CDD:
A CHC system can be classified as CDD when each clause satisfies the
following rules:
%
\textbf{(1)} no two predicates in the body of the same clause share
any transitive dependencies on other predicates and
\textbf{(2)} no clause has more than one occurrence of a given
predicate in the body.
% give a example
As an example, clause $(7)$ is dependence disjoint.
%
Two predicates $\cc{L}_9$ and $\cc{dbl}$ are in its body.
%
The transitive dependency of $\cc{L}_9$ is the set
$\{\cc{L}_4,\cc{L}_6,\cc{L}_8\}$ while the transitive dependency of
\cc{dbl} is the empty set.
%
Therefore, their transitive dependencies are disjoint:
$\{\cc{L}_4,\cc{L}_6,\cc{L}_8\} \cap \varnothing = \varnothing$.
%
All other clauses in $\mcchc$ have at most one uninterpreted predicate
in the body, so they are trivially disjoint dependent.
%
Therefore $\mcchc$ is a CDD system.
%
%
The formal definition of CDD and its key properties are given in
\autoref{sec:CDD-defn}.
The formal definition of transitive dependency is given in
\autoref{sec:chcs}.
%

% restate defn of CDD systems:
\sys solves CDD systems directly by issuing a binary interpolation
query for each uninterpreted predicate in topological order.
%
Each interpretation of a predicate $P$ can be computed by
interpolating
\textbf{(1)} the \emph{pre}-formula, constructed from clauses where
$P$ is the head, and
\textbf{(2)} the \emph{post}-formula, constructed from
all clauses where the head transitively depends on $P$.
%

For example, consider $\cc{L}_9$. By the time \sys attempts to
synthesize an interpretation for $\cc{L}_9$ it will have solutions for
$\cc{L}_4$, $\cc{L}_6$, $\cc{L}_8$.
%
Possible interpretations of these predicates are shown in
\autoref{fig:ex-graph}.
%
The pre-formula is constructed from the bodies of clauses where
$\cc{L}_9$ is the head.
%
Each relational predicate, $P$,
is replaced by a corresponding boolean indicator variable, $\cc{b}_P$.
%
Each boolean indicator variable implies the solution for its
predicate,
encoded as the disjunction of the negation of the boolean
indicator variable and the solution.
%
In particular, the pre-formula for $\cc{L}_9$ is constructed from
clauses (5) and (6):
\begin{gather}
  ((\cc{b}_{\cc{L}_6} \land \cc{abs'} = \cc{n})
  \lor
  (\cc{b}_{\cc{L}_8} \land \cc{abs'} = -\cc{n}))
  \land
  (\neg \cc{b}_{\cc{L}_6} \lor \cc{n} \ge 0)
  \land
  (\neg \cc{b}_{\cc{L}_8} \lor \cc{n} < 0)
\end{gather}
%
The post-formula is constructed from clauses that transitively depend
on $\cc{L}_9$. Again, we replace relational predicates by
corresponding boolean indicators. However, we omit the boolean indicator for
$\cc{L}_9$. The post-formula is composed from clauses (1), (7), and
(8):
\begin{gather}
  (\neg \cc{b}_{\cc{dbl}} \lor \cc{d} = 2*\cc{x})
  \land
  (\neg \cc{b}_{\cc{main}} \lor
    (\cc{b}_{\cc{dbl}}
    \land \cc{abs'}= \cc{x}
    \land \cc{res}=\cc{d} ))
  \land
  (\cc{b}_{\cc{main}} \land \cc{res} < 0)
\end{gather}
%
Interpolating the pre and post formulas yields an interpretation
of $\cc{L}_9$: $\cc{abs'} \geq 0$.
%
The procedure for solving a CDD system is described in formal detail
in \autoref{sec:solve-cdd}.

\subsection{$\mcchc$ is not in other recursion-free classes}
\label{sec:not-in}
%
In this section, we show that $\mcchc$ is not in other
known classes of recursion-free CHC systems.
%
Specifically, we will
discuss body-disjoint systems and linear systems.
%

% tree solver
Body-disjoint (or derivation tree)
systems~\cite{mcmillan14,bjorner13,heizmann10,rummer13a,rummer13b} are
a class of recursion-free CHC system where each uninterpreted
predicate appears in the body of at most one clause and appears in
such a clause exactly once.
%
Such systems cannot model a program with multiple control paths that
share a common prefix, typically modeled as a CHC system with an
uninterpreted predicate that occurs in the body of multiple clauses.
%
$\mcchc$ is not a body-disjoint system because $\cc{L}_4$ appears in the
body of both clause (3) and clause (4).
%
In order to handle $\mcchc$, a solver that uses
body-disjoint systems would have to duplicate
$\cc{L}_4$.
%
Worse, if $\cc{L}_4$ had dependencies, then each dependency would also
need to be duplicated.

%linear systems:
Previous work has also introduced the class of linear
systems~\cite{albarghouthi12a}, where the body of each clause has at
most one uninterpreted predicate.
%
However, such systems cannot directly model the control flow of a
program that contains procedure calls.
%
$\mcchc$ is not a linear system because the body of clause (7) has two
predicates, \cc{L9} and \cc{dbl}.
%
CHC solvers that use linear systems effectively inline the
constraints for relational predicates that occur in non-linear
clauses~\cite{albarghouthi12b}.
%
In the case of $\mcchc$, inlining the constraints of $\cc{dbl}$ is
efficient, but in general such approaches can generate systems that
are exponentially larger than the input. For example, if the procedure
$\cc{dbl}$ were called more than once in $\cc{dblAbs}$ then multiple
copies of the body of the procedure would be inlined. And if the body
of this procedure were large, the inlining could become
prohibitively expensive.

%% talk about expansion:
\begin{figure}[t]
  \centering
  \begin{floatrow}[2]
    \ffigbox[.45\textwidth] {%
      {\caption{$\mcchc$ as a directed hypergraph. Each relational
          predicate is depicted as a graph node while each clause is
          represented by a hyperedge. Each hyperedge is labeled by the
          constraint in the corresponding CHC\@. Each node has a valid
          corresponding interpretation, written in braces.
      }\label{fig:ex-graph}}}
      {\input{fig/example}}
    \ffigbox[.45\textwidth]
      {\caption{The hierarchy of classes of recursion-free CHC
          systems. Body Disjoint and Linear systems are subsumed by
          CDD systems. Solving Directly Solvable CHC systems is in
          co-NP while solving general, recursion-free systems is
          co-NEXPTIME Complete.
      }\label{fig:hierarchy}}
      {\input{fig/hierarchy}}
  \end{floatrow}
\end{figure}

The hierarchy of discussed classes of recursion-free systems is
depicted in \autoref{fig:hierarchy}.
%
As shown, the class of CDD systems is a superset of both the class
of body-disjoint systems and the class of linear systems. So any
recursion-free system that is efficiently expressible in body-disjoint
or linear form is also efficiently expressible in CDD form.
%
In addition, some systems which are expensive to express in
body-disjoint or linear form are efficiently expressible in CDD form.
%
\sys~takes advantage of this fact when solving input systems.
Given an arbitrary recursion-free CHC system $S$, \sys~reduces $S$ to
a CDD system $S'$ and solves $S'$ directly.
%
In general, $S'$ may have size exponential in the size of
$S$.
%
However, \sys~generates CDD systems via heuristics analogous to
those used to generate compact verification conditions of hierarchical
programs~\cite{flanagan01,lal-qadeer15}.
%
In practice these heuristics often yield CDD systems which are
small with respect to the input system.
%
A general procedure for constructing a CDD expansion of a given CHC
system is given in \autoref{app:cons-cdd}.


    \filetitle{autocaption}{Create captions for graphs of model variables or parameters}{model/autocaption}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
C = autocaption(M,X,Template,...)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{M} {[} model {]} - Model object.
\item
  \texttt{X} {[} cellstr \textbar{} struct \textbar{} poster {]} - A
  cell array of model names, a struct with model names, or a
  \href{poster/Contents}{\texttt{poster}} object.
\item
  \texttt{Template} {[} char {]} - Prescription for how to create the
  caption; see Description for details.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{C} {[} cellstr {]} - Cell array of captions, with one for each
  model name (variable, shock, parameter) found in \texttt{X}, in order
  of their appearance in \texttt{X}.
\end{itemize}

\paragraph{Options}\label{options}

\begin{itemize}
\item
  \texttt{'corr='} {[} char \textbar{}
  \emph{\texttt{'Corr \$shock1\$ X \$shock2\$'}} {]} - Template to
  create \texttt{\$descript\$} and \texttt{\$alias\$} for correlation
  coefficients based on \texttt{\$descript\$} and \texttt{\$alias\$} of
  the underlying shocks.
\item
  \texttt{'std='} {[} char \textbar{} \emph{\texttt{'Std \$shock\$'}}
  {]} - Template to create \texttt{\$descript\$} and \texttt{\$alias\$}
  for std deviation based on \texttt{\$descript\$} and
  \texttt{\$alias\$} of the underlying shock.
\end{itemize}

\paragraph{Description}\label{description}

The function \texttt{autocaption} can be used to supply user-created
captions to title graphs in \texttt{grfun/plotpp},
\texttt{grfun/plotneigh}, \texttt{model/shockplot},
\texttt{dbase/dbplot}, and \texttt{qreport/qplot}, through their option
\texttt{'caption='}.

The \texttt{Template} can contain the following substitution strings:

\begin{itemize}
\item
  \texttt{\$name\$} -- will be replaced with the name of the respective
  variable, shock, or parameter;
\item
  \texttt{\$descript\$} -- will be replaced with the description of the
  respective variable, shock, or parameter;
\item
  \texttt{\$alias\$} -- will be replaced with the alias of the
  respective variable, shock, or parameter.
\end{itemize}

The options \texttt{'corr='} and \texttt{'std='} will be used to create
\texttt{\$descript\$} and `$alias$ for std deviations and
cross-correlations of shocks (which cannot be created in the model
code). The options are expected to use the following substitution
strings:

\begin{itemize}
\item
  \texttt{'\$shock\$'} -- will be replaced with the description or alias
  of the underlying shock in a std deviation;
\item
  \texttt{'\$shock1\$'} -- will be replaced with the description or
  alias of the first underlying shock in a cross correlation;
\item
  \texttt{'\$shock2\$'} -- will be replaced with the description or
  alias of the second underlying shock in a cross correlation.
\end{itemize}

\paragraph{Example}\label{example}


% in RST now, don't edit here

\subsection{PVSlice\_AT}

\subsubsection{Description}

PVSlice\_AT creates a position-velocity slice through a spectral
window cube, which
should be a representative version showing all the spectral lines.

\subsubsection{Use Case}

A position-velocity slice is a great way to visualize all the emission
in a spectral window cube, next to CubeSum.

\subsubsection{Input BDPs:}

\begin{description}

\item[SpwCube] Spectral Window Cube to take the slice through.  We normally
mean this to be a Position-Position-Velocity (or Frequency) cube.

\item[CubeSum]  Optional. One of CubeSum or PeakPointPlot can be used to
estimate the best slit.
It will use a moment of inertia analysis to decipher the best line
in RA-DEC for the slice.

\item[PeakPointPlot]  Optional.

\end{description}

\subsubsection{Input keywords}

\begin{description}

\item[center]
Center and Position angle of the line in RA/DEC.  E.g.   129,129,30
Optional length?  Else full slice through cube is taken.\\
*** NEED TO DESCRIBE HOW POSITION ANGLE IS DEFINED.  EAST OF NORTH? ***

\item[line]
Begin and End points of the line in RA/DEC.  E.g.   30,30,140,140\\
*** WHAT ARE THE UNITS? COULD THIS BE DONE WITH A REGION KEYWORD? ***

\item[major]
Place the slit along the major axis?  Optionally the minor axis slit can
be taken. For rotation flow the major axis makes more  sense, for outflows
the minor axis makes more sense.  Default: True.

\item[width]
Width of the slit. By default sampling through the cube is
done, which equals a zero thickness, but a finite number of
pixels can be choosen as well, in which case the signal within
that width is averaged.
Default: 0.0

\item[minval]
Minimum intensity value below which no data from CubeSum or PeakPointPlot are
used to determine the best slit.

\item[gamma]
The factor by which intensities are weighed (intensity**gamma) to compute
the moments of inertia from which the slit line is computed.
Default: 1

\end{description}

\subsubsection{Output BDPs}

\begin{description}

\item[PVSlice\_BDP]

\end{description}

\subsubsection{Procedure}

Either a specific line is given manually, or it can be derived from a reference map (or table).
By this it computes a (intensity weighted) moment of inertia, which then defines a major and minor
axis. Spatial sampling on output is the same as on input (this means if there ever
is a map with unequal sampling in RA and DEC, there is an issue, WSRT data?).

\subsubsection{CASA tasks used}

\begin{description}

\item[impv]

\end{description}

\clearpage


\subsection{Modelling heterogeneous preferences with mixed logit}

Using demographic data


%
% Chapter 7
%

\chapter{Signal Extraction and Systematic Uncertainties}
\label{sig_ext}
\epigraph{Statistics is the grammar of science.}{\textit{Karl Pearson}}
\vskip 0.5in
\section{Introduction}
The analysis is, in its essence, a sophisticated counting experiment. The presence of a signal is indicated by an excess of events over the predicted background, in the distribution of a signal variable. For our analyses the signal variables are collinear mass or BDT output, as described in chap~\ref{chap:event_sim} and~\ref{evt_sel}. Given that there are several uncertainties, both experimental and theoretical and also due to the innate randomness in the process, it is possible that an excess is observed when there is no signal. So, when an excess is observed, a p-value which represents the probability that the excess is due to statistical fluctuations is computed. A very low p-value is taken to indicate that the excess corresponds to an observed signal and not merely a statistical fluctuation. Conversely, if no excess is observed (upper exclusion) limits are set on the product of branching fraction and production cross-section. A 95\% CL (confidence level) is taken as a requirement for ruling out a signal at or above a certain value known i.e. upper exclusion limit. The first part of this chapter describes the statistical methods used, that very closely follow the procedure used for LHC Higgs boson search described in ~\cite{note2011}.

Several sources of systematic uncertainties need to be considered when making the above measurement. The sources of these uncertainties can be theoretical, experimental or purely statistical in nature. Further, they can affect only the overall scale of the distributions (used to make the measurement), or affect there shape i.e. change the scale differently in each bin of the distribution. All the uncertainties used in the analyses and their sources are described in the second part of this chapter.

\section{Statistical methods for signal extraction}
\label{stat_meth}
In the following section, the expected signal event yields are denoted by $s$, and backgrounds by $b$. The parameter $\mu$ that appears below is the signal strength modifier, which changes the signal production cross-sections of all the production mechanisms by exactly the same scale $\mu$.

\subsection{Likelihood function}
The Poisson distribution is an appropriate model for n, the number of times an event occurs in an interval if the following assumptions are true~\cite{poisson_wiki}.
\begin{itemize}
\item The occurrence of one event does not affect the probability that a second event will occur. That is, events occur independently.
\item The rate at which events occur is constant. The rate cannot be higher in some intervals and lower in other intervals. This rate is the average number of events in the interval, $\lambda$.
\item Two events cannot occur at exactly the same instant; instead, at each very small sub-interval exactly one event either occurs or does not occur.
\end{itemize}
The poisson probability of distribution is then given by:
\begin{equation}
  P(n_{events})=\frac{e^{-\lambda}\lambda^{n}}{n!}
\end{equation}
For a counting experiments such as ours, the above conditions approximately hold. The expected number of events is $\mu\cdot s + b$. The likelihood function $\mathcal{L}(data|\mu)$ is then given by:
\begin{equation}
  \mathcal{L}(data|\mu)=\prod_{i=1}^{bins}\frac{(\mu\cdot s_i + b_i)^{n_i}}{n_{i}!}e^{-\mu\cdot s_i - b_i}
\end{equation}
where $n_i$ is the number of events observed in the bin i of the distribution, and $s_i$ and $b_i$ are expected number of signal and background events in that bin respectively.

\subsection{Treatment of systematic uncertainties}
\label{sys_treat}
All systematic uncertainties are handled by introducing them as nuisance parameters. Nuisance parameters are parameters that influence the model but are not of interest in our measurement, e.g., if we are interested in knowing only the mean of a population that is expected to be distributed as a gaussian, the standard deviation becomes a nuisance parameter for the model that we fit. In our experiment, the nuisance parameters are embedded into the likelihood function. In order for the likelihood function to have a clean factorized form~\cite{note2011}, all sources of uncertainties considered are taken to be 100\%-correlated or uncorrelated. If an uncertainty is partially correlated, it is either separated into 100\%-correlated or uncorrelated components, or considered 100\%-correlated or uncorrelated, depending on whichever is a more conservative estimate. The full suite of nuisance parameters is represented as $\theta$. These effect the expected signal and background yields which are now represented as $s(\theta)$ and $b(\theta)$. Each component of $\theta$ is associated with a default value $\tilde{\theta}$, reflecting our degree of belief on the real value of $\theta$. The pdf (probability distribution function) $\rho(\theta|\tilde{\theta})$ can then be interpreted as a posterior distribution from measurements of $\tilde{\theta}$. Using Bayes' theorem:
\begin{equation}
  \rho(\theta|\tilde{\theta})=\rho(\tilde{\theta}|\theta)\cdot\pi_\theta(\theta),
\end{equation}
where the priors $\pi_\theta(\theta)$ are taken as flat distributions representing no prior knowledge of $\theta$. This reformulation allows us to use the pdf of $\tilde{\theta}$ instead, i.e. $\rho(\tilde{\theta}|\theta)$  to directly constrain the likelihood of the measurement. The likelihood function after the introduction of systematic uncertainties now becomes:
\begin{equation}
  \mathcal{L}(data|\mu,\theta)=Poisson(data|\mu\cdot s(\theta) + b(\theta))\cdot\rho(\tilde{\theta}|\theta)
\end{equation}

Systematic uncertainties that effect only the overall scale of the distributions, correspond to a multiplicative factor in the signal and/or background yields, and are described by log-normal pdfs. Log-normal pdfs are characterized by the width $\kappa$, and are well-suited for positively valued observables. The log-normal distribution looks like:
\begin{equation}
\rho(\theta|\tilde{\theta})=\frac{1}{\sqrt{2\pi}\text{ ln}(\kappa)}\text{exp }(\frac{\text{ln}(\theta/\tilde{\theta})^2}{2(\text{ln }\kappa)^2}) \frac{1}{\theta}
\end{equation}

Systematic uncertainties that effect the scale of the distribution differently in each bin have the effect of altering its shape along with its scale. Such uncertainties are called shape uncertainties ~\cite{shape_syst1}, and are modeled using a linear extrapolation method ~\cite{shape_syst2}. In practice, two alternate distributions obtained by varying the nuisance by $\pm 1$ standard deviation are used, and a parameter is added to the likelihood that smoothly interpolates between these shapes.

\subsection{Calculation of exclusion limits}
\label{exc_cal}
The CL$_\text{s}$ method~\cite{cls1,cls2,cls3} is used to set upper exclusion limits when no excess of data over background is observed. The test statistic used generally for hypothesis testing in searches at the LHC, uses profiling of nuisances as described above, and is based on the likelihood ratio~\cite{prof_likelihood}, which by the Neyman-Pearson lemma is known as the most powerful discriminator. This is denoted by $\tilde{q_\mu}$, and is given by:
\begin{equation}
\label{eq:proflik}
  \tilde{q_\mu}=-2\text{ ln}\frac{\mathcal{L}(\text{data}|\mu,\hat{\theta_\mu})}{\mathcal{L}(\text{data}|\hat{\mu},\hat{\theta})},\text{   with  } 0\leq \mu \leq \hat{\mu}
\end{equation}
where $\hat{\theta_\mu}$ refers to the conditional maximum likelihood estimators of $\theta$, i.e. the set of nuisances parameters that maximize the likelihood for a given signal strength $\mu$, while $\hat\mu$ and $\hat\theta$ refer to the global maximum likelihood estimators for $\mu$ and $\theta$. The lower constraint on $\hat{\mu}$ i.e., $\hat{\mu}\geq 0$ ensures that the signal rate cannot be negative, while the upper constraint that $\hat{\mu}$, which is the global maximum value, cannot be less than the value of $\mu$ under consideration is imposed to guarantee that upward fluctuations of data such that $\hat{\mu}\geq \mu$ are not considered as evidence against the signal hypothesis, i.e., a signal of strength $\mu$.

Now, using equation~\ref{eq:proflik}, the observed value of the test statistic, $\tilde{q_\mu}^{obs}$, is calculated for the signal strength $\mu$. Also, maximum likelihood estimators for the nuisance parameters, for the background-only($\mu=0$) and signal-plus-background(current $\mu>0$ under consideration) hypotheses are calculated. They are denoted by $\hat{\theta_{0}}^{obs}$ and $\hat{\theta_\mu}^{obs}$ respectively, and are used to generate toy Monte Carlo pseudo-datasets. These pseudo datasets are used to construct  pdfs, using equation~\ref{eq:proflik}, of test statistics $f(\tilde{q_\mu}|0,\hat{\theta_{0}}^{obs})$ and $f(\tilde{q_\mu}|\mu,\hat{\theta_\mu}^{obs})$ by treating them as they were real data. Example of these distributions are shown in Fig.~\ref{fig:test_stat_dist}.
\begin{figure*}[!htpb]\centering
 \captionsetup{width=.87\textwidth,justification=centering}
 \includegraphics[width=0.85\textwidth]{plots_and_figures/chapter7/test_statistic_distri.png}
 \caption{Test statistic distributions for ensembles of pseudo-data generated for signal-plus-background (red) and background-only (blue) hypotheses~\cite{note2011}.}
 \label{fig:test_stat_dist}
\end{figure*}

Having constructed the above pdfs, it is now possible to calculate the probabilities of the observations under both hypotheses. The first quantity that we calculate is:
\begin{equation}                                                                                \label{eq:pmu}                                                       p_\mu=P(\tilde{q_\mu}\geq \tilde{q_\mu}^{obs}|\text{signal-plus-background})=\int_{\tilde{q_\mu}^{obs}}^{\inf}f(\tilde{q_\mu}|\mu,\hat{\theta_\mu}^{obs})d\tilde{q_\mu}
\end{equation}

The above quantity corresponds to CL$_\text{s+b}$ and measures the incompatibility of data with signal-plus-background hypothesis. This quantity alone is not adequate for hypothesis testing in situations when the signal is so small that both hypotheses are compatible with the observation and a downward fluctuation of the background can lead to an inference of signal.

The second quantity we calculate is:
\begin{equation}
  \label{eq:pb}
  1-p_b=P(\tilde{q_\mu}\geq \tilde{q_\mu}^{obs}|\text{background-only})=\int_{\tilde{q_\mu}^{obs}}^{\inf}f(\tilde{q_\mu}|0,\hat{\theta_0}^{obs})d\tilde{q_\mu}
\end{equation}
This quantity corresponds to CL$_\text{b}$ and measures the incompatibility of data with the background. The incompatibility of the data with background-only hypothesis alone doesn't tell us that it is indeed compatible with the signal, and so is not considered a good test of the signal hypothesis.

The ratio of the two quantities referred to as CL$_\text{s}$~\cite{cls1,cls2,cls3} helps deal with both situations above well, and is given by:
\begin{equation}
  \label{eq:cls}                                                                                \text{CL}_\text{s}=\frac{p_\mu}{1-p_b}
\end{equation}

The 95\% CL is then arrived at by iterating over $\mu$ until we have CL$_\text{s}=0.05$. And the amount of signal or above, given by that $\mu$, denoted as $\mu^{95\%CL}$, is said to be excluded at 95\% CL.

\subsection{Median expected limits}

Upper exclusion limits calculated using toy datasets of background-only expectation, are called expected limits. A large set of background-only pseudo-data is generated, and CL$_\text{s}$ and $\mu^{95\%CL}$ is calculated for each of them. The median expected limit is calculated by integrating over this distribution until the 50\% quantile is reached. The $\pm 1\sigma$ and $\pm 2\sigma$ bands are calculated similarly by integrating the distribution to the appropriate quantiles are reached. The calculation of median expected limits does not involve using the observed data and hence can be calculated when the analyses is blinded to prevent experimenter's bias (as mentioned in Section~\ref{evt_sel_intro}). This can be use to maximize the sensitivity of the search, as described in Sections~\ref{h125_cb_sel} and ~\ref{H125_cb_sel}. A more stringent (lower) median limit corresponds to a more sensitive search.

\subsection{Quantifying an excess of events}
\label{theo_uncert}
In case an excess of data over background is observed, it is necessary to make sure beyond a reasonable doubt that the excess is not merely a fluctuation. This is quantified using the background-only p-value, which is the probability for the background to fluctuate and give an excess of events as large or larger than that observed. The same test statistic as equation~\ref{eq:proflik} is used with the signal strength set to 0 to correspond to the background-only hypothesis:

\begin{equation}
\label{eq:proflik_b}
  \tilde{q_0}=-2\text{ ln}\frac{\mathcal{L}(\text{data}|0,\hat{\theta_0})}{\mathcal{L}(\text{data}|\hat{\mu},\hat{\theta})},\text{   with  } 0\leq\hat{\mu}
\end{equation}
The constraint on $\hat\mu$ being greater than 0 is required so that a deficit of events in observed data is not interpreted in the same manner as we would an excess. In other words a departure from the background hypothesis in the form of deficit of events is not considered in favor of the signal hypothesis. Following the same procedure as calculation of observed limits (as described in~\ref{exc_cal}) and generating pseudo-data, the distribution $f(\tilde{q_0}|0,\hat{\theta_{0}}^{obs})$ is constructed. The p-value is then given by:

\begin{equation}
  \label{eq:p0}
  p_0=P(\tilde{q_0}\geq \tilde{q_0}^{obs})=\int_{\tilde{q_0}^{obs}}^{\inf}f(\tilde{q_0}|0,\hat{\theta_0}^{obs})d\tilde{q_0}               \end{equation}

The p-value can be converted to significance $\mathcal{Z}_0$, which is an equivalent way of quantifying an excess and is related to the p-value by the following:

\begin{equation}
  \label{eq:sig}                                                                                p_0=\int_{\mathcal{Z}_0}^{\inf}\frac{1}{\sqrt{2\pi}}exp(-x^2/2)dx
\end{equation}

Broadly, the significance corresponds to how far into the tail of the distribution (i.e., away from the most probable value), assuming background hypothesis, the test statistic value corresponding to the observed data lies. The farther it is, the less likely it is to have been a fluctuation. The conventional standard in high energy physics to be able to claim observation of a process is a significance of $5\sigma$, which corresponds to a p-value of $2.8\times 10^{-7}$.

\section{Systematic uncertainties}
\label{exp_uncert}
It is important to consider all relevant sources of uncertainties when performing sophisticated counting experiments such as these. Uncertainties that are introduced as a result of imprecise/inaccurate knowledge of the system or gaps in prior knowledge that is used in the measurement are called systematic uncertainties. They are a different class of uncertainties than those arising purely out randomness in statistical measurements, called statistical uncertainties. The sources of systematic uncertainties range from purely theoretical in nature to purely experimental. They can be categorized in the two following ways:
\subsection{Normalization uncertainties}
The value of these uncertainties are independent of the signal/discriminant variable. To be more precise, these uncertainties are independent of the value of $\mcol$ or BDT response. Hence, they effect each bin of those distributions in exactly the same manner and thus change only the overall scale of the distribution without altering its shape.

The muons in the analysis are required to pass certain identification, isolation and triggering criteria (see chapter~\ref{evt_sel}). The efficiencies for muon to pass these criteria are measured via tag-and-probe methods~\cite{muon_recon2018} using $Z\rightarrow\Pgm\Pgm$ events, and the scale factors are used to match the efficiency in MC to that in data (see section~\ref{anal_trigger}). The efficiencies determined by this process, like any other quantity, are associated with systematic  uncertainties. For the muons used in the analyses described here, a combined normalization uncertainty of 2\% is associated with muon trigger, identification and isolation. Similar to muons, the efficiencies for electrons used in the analyses have also been measured via tag-and-probe methods~\cite{e_recon} using $Z\rightarrow\Pe\Pe$ events. The uncertainties in efficiencies  of electron identification and isolation criterion are also included as a normalization uncertainty of 2\% in the fit. Both the above uncertainties are applied to processes which are derived from MC simulation. As mentioned earlier, a b tagging veto is applied in the analysis in order suppress backgrounds involving top quarks. The efficiency of b tagging procedure is different in MC simulation than data. A scaling procedure is applied to match these efficiencies, and the uncertainties associated with these factors are found to not effect the shape of the $\mcol$ or BDT distributions. They are thus included in the fit as normalization uncertainties and range across categories from 2-4.5\% and 2-2.5\% for $\hmue$ and $\Hmue$ analysis respectively.

Several backgrounds in the analyses are estimated using MC simulations (see chapter ~\ref{bg_val}). These include \ztt, \ttb, \wjets, $\PW\PW$, $\PW\PZ$ and $\PZ\PZ$, $Z\to\ell\ell$ $(\ell = \Pe, \Pgm)+\text{jets}$, single-top quark production, $\PW\gamma^{(*)}+\text{jets}$. The production cross-sections for these backgrounds determine the number of events each background would contribute. These cross-sections are measured experimentally and the uncertainty in those measurements are included in the fit. Given that a change in cross-section changes the overall number of events produced, it has no effect on the shape of distributions. Hence these uncertainties are included as normalization uncertainties. These uncertainties in general  arise from: uncertainties on the parton distribution functions and strong coupling constant (called PDF+$\alpha_s$); variations in renormalization and factorization scales. In the $\Hmue$ analysis a separate uncertainty is applied for PDF+$\alpha_s$ and renormalization/factorization scales for each of the backgrounds. In $\hmue$ analysis, a combined uncertainty for each background is applied to cover both sources. All the above uncertainties are considered 100\% correlated among categories. For each background, a 5\% uncertainty, uncorrelated among all categories, is also applied to conservatively cover differences across categories. The QCD multijet background is estimated using a data-driven procedure. An uncertainty of 30\% associated with this procedure (corresponding to the uncertainty in the extrapolation factor from the same-sign to opposite-sign region) is included in the fit. All uncertainties are summarized in table ~\ref{table:syst}~\cite{HIG-17-001,HIG-18-017}. All uncertainties in the table are treated as correlated between the categories, except those with more values separated by the $\oplus$~symbol. In the case of two values, the first value is the correlated uncertainty and the second value is the uncorrelated uncertainty for each individual category. In the case of three values, the  first and second values correspond to the uncertainties arising from factorization and renormalization scales and PDF variations and are correlated between categories, while the third value is the uncorrelated uncertainty for each individual category. Two values separated by the ``--'' sign represent the range of the uncertainties from the different sources and/or in the different jet categories.

\begin{table}[htpb]
  \begin{center}
    \caption{The systematic uncertainties for both analyses
      (see text for explanation of the $\oplus$~symbol)}
\begin{tabular}{l*{2}{c}} \hline
Systematic  uncertainty            & $\hmue$& $\Hmue$  \\ \hline
Muon  trigger/ID/isolation         &          2\%           &        2\%         \\
Electron trigger/ID/isolation      &          2\%           &        2\%           \\
b tagging veto                     &      2.0--4.5\%        &      2.0--2.5\%         \\
[\cmsTabSkip]
QCD multijet background            &          30\%          &        30\%                \\
$\ztt+\textrm{jets}$ background    &    10\%$\oplus$5\%     & 0.1\%$\oplus$2\%$\oplus$5\%         \\
$\ttb$ background                  &    10\%$\oplus$5\%     &   10\%$\oplus$5\%              \\
$W+\textrm{jets}$ background       &    10\%$\oplus$5\%     & 0.8\%$\oplus$3.8\%$\oplus$5\%        \\
$\PW\PW,\PZ\PZ,\PW\PZ$ background  &    5\%$\oplus$5\%      &  3.5\%$\oplus$5\%$\oplus$5\%       \\
$\PW\gamma^{(*)}$ background       &    10\%$\oplus$5\%     &  10\%$\oplus$5\%       \\
Single top quark background        &    5\%$\oplus$5\%      & 3\%$\oplus$5\%$\oplus$5\%    \\
$Z\to\Pgm\Pgm/\Pe\Pe$ background   &    10\%$\oplus$5\%     & 0.1\%$\oplus$2\%$\oplus$5\%     \\
[\cmsTabSkip]
Jet energy scale                   &        3--20\%         &        3--20\%                         \\
$\Pgm$ energy scale                &        0.2\%           &        0.2\%                              \\
 $\Pe$ energy scale                &        0.1--0.5\%      &        0.1--0.5\%                          \\
Unclustered energy scale           &        $\pm 1 \sigma$  &     $\pm 1 \sigma$                            \\
pileup                             &        $\pm 1 \sigma$  &     $\pm 1 \sigma$                            \\
[\cmsTabSkip]
Integrated luminosity              &         2.5\%          &       2.5\%                                        \\ \hline
\end{tabular}
\label{table:syst}
\end{center}
\end{table}

Just like MC backgrounds described above, the signal in both $\hmue$ and $\Hmue$ analysis comes from MC simulation. The signal process and the SM Higgs background are associated with uncertainties in the Higgs boson production cross sections. These come from variations in factorization/renormalization scales, as well as PDF+$\alpha_s$, and result in changes only in normalization. These uncertainties are summarized for SM Higgs boson and heavier Higgs of different masses in table~\ref{table:syst}. They are taken from Handbook of LHC Higgs cross-sections found in Ref.~\cite{YR4}. The PDF and $\alpha_s$ uncertainties are computed following the recommendation of the PDF4LHC working group. The remaining Gaussian uncertainty accounts for additional intrinsic sources of theory uncertainty described in detail in the reference~\cite{HIG-18-017}.

\begin{table}[!htbp]
  \begin{center}
  \caption{ Theoretical uncertainties applied to the Higgs boson production cross sections for the different masses}
  \begin{tabular} {cccc}
    \hline
    $m_{\PH}$  (GeV) &Production mode&Theory, Gaussian (\%) & PDF$+\alpha_s$  (\%)\\\hline
125 & GGF&$\pm$3.9
&$\pm$3.2\\
125 & VBF&$\pm$0.4
&$\pm$2.1\\
200 & GGF &$\pm$1.8
&$\pm$3.0\\
300&GGF&$\pm$1.8
&$\pm$3.0\\
450&GGF
&$\pm$2.0
&$\pm$3.1\\
600&GGF
&$\pm$2.1
&$\pm$3.5\\
750&GGF
&$\pm$2.1
&$\pm$4.0\\
900&GGF
&$\pm$2.2
&$\pm$4.6\\\hline
  \end{tabular}
  \label{tabe:syst_signal}
\end{center}
\end{table}

The estimation of a particular background, that is derived from simulation, needs to correspond to the number of events (of that background, having a particular cross-section) that would be produced in the amount of proton-proton collision data that we are using for this search. In other words, the background estimations need to be normalized to (brought to the same scale as) the integrated luminosity of the data collected. This integrated luminosity (defined in chapter~\ref{chap:exper_setup}) is a measured quantity, and like all measured quantities, has an uncertainty associated with it. This amounts to 2.5\% and, like other normalization uncertainties, only effects only the overall scale of distributions.

\subsection{Shape uncertainties}
In this section, we describe the systematic uncertainties which not only alter the scale but also the shape of the distributions. We start with the description of uncertainties associated with jet energy corrections. As described in Section~\ref{jet_recon}, the reconstruction of complex objects such as jets need to corrected using several correction factors. These factors have uncertainties associated with them and these are included in the fit as shape uncertainties. There are several different sources of these uncertainties and the effect of each is propagated to the fit by including alternate distributions for each process where each source of uncertainty has been moved by one standard deviation on either side. The effect of changing these sources is propagated through the jets in the analysis, and also other affected quantities such as the \ptmiss. A total of 27 sources are considered, and these include effects of pileup, composition of jets, $\eta$ dependence etc. They vary in the range from 3-20\% and are considered uncorrelated.

Just like jets, there are shape uncertainties associated with the energy scale of electrons and muons. The effect of electron energy scale uncertainty is treated in a similar manner by propagating the effect of varying the scale to process distributions which are then included in the fit. The uncertainties are a result of the sum in quadrature of the following components: electron selection efficiency, pseudorapidity dependence, and shower-shape related categorization. The resolution systematics result to be negligible and are thus not considered in the fit. The value of this uncertainty ranges from 0.1-0.5\%. The muon energy scale uncertainty amounts to 0.2\% and is treated in the same manner as above.

Jets with $\pt<15$ GeV or PF candidates which do not get clustered inside any jets are called unclustered energy. Their scale that effects the \ptmiss in particular, and is associated with a shape uncertainty that is treated the same way as others~\cite{Sirunyan:2019kia}. Four sources of unclustered energy scale uncertainty are considered, and estimated independently for these four particle categories: charged particles, photons, neutral hadrons, and HF (very forward, high $|\eta|$) particles which are not contained in jets. The effect of these sources on the unclustered energy is propagated in the same manner as described above, and they are considered uncorrelated.

A set of weights is applied in order make the distribution of pileup in MC simulation match that of pp collision data. There is an uncertainty associated with this process. This is included  by varying the weights by changing by 5\%, in each direction, the total inelastic cross section used in the estimation of the pileup events in data~\cite{Sirunyan:2018nqx}. These new set of weights are then applied event-by-event, producing alternate distributions that are included in the fit as shape uncertainties.

A shape uncertainty is also considered to deal with the fact that purely statistical fluctuations can change the shape of the distribution. These uncertainties are called bin-by-bin uncertainties as they account for statistical uncertainty in every bin of the distribution. Alternate distributions are created by varying the contents of each bin up and down, and these distributions are included in the fit as shape uncertainties~\cite{BARLOW}. Given that considering all bins of all processes will result in a very large number of such nuisances being considered, shape uncertainties for only those bins are considered in which there is more than 10\% variation in the up and down shift. All shape uncertainties are summarized in Table~\ref{table:syst}.

% % uncomment the following lines,
% if using chapter-wise bibliography
%
% \bibliographystyle{ndnatbib}
% \bibliography{example}


\section{Bridges in HECRAS and \anuga{}}
This test compares a prismatic channel flow with a bridge in HECRAS and ANUGA.
A 10m wide, 1000m long channel (slope of 1/200, bankfull depth of 1m,
rectangular cross-section) flows through a floodplain (10m wide on either side
of the channel) (Figure~\ref{schematic}). 500m downstream there is a bridge
with a 1.3m high rectangular opening over the channel, and a deck elevation of
-1m. In HECRAS the bridge is modelled using the enegy method, wheras in ANUGA
the boyd-box-culvert routines are used for the bridge opening, and the shallow
water equations are used for bridge overflow. Both models have a uniform
Manning's n of 0.03.

\begin{figure}
\begin{center}
\includegraphics[width=1.0\textwidth]{hecras_bridge_test/RASGeometry_Bridge.png}
\end{center}
\caption{Screenshot showing the HECRAS model geometry schematization}
\label{schematic}
\end{figure}

A discharge timeseries is imposed upstream for both models, with the discharge
increasing from 1m$^3$/s initially to 70m$^3$/s at the end of the simulation.
Details of the model setup can be seen in the code / input files in this
directory.

\subsection{Results}

Figure~\ref{Reach} show stage timeseries at various stations downstream in each model.
The ANUGA and HECRAS results are qualitatively similar. Just upstream of the
bridge (stations 525+), ANUGA shows a backwater effect earlier than HECRAS,
while the final steady-state stage is somewhat lower than in HECRAS. This is
attributable to the different bridge models used (energy equation for HECRAS vs
culvert+shallow-water for ANUGA). HECRAS includes several other bridge models
(based on the momentum equation or other methods), and these would also give
different results.

\begin{figure}
\begin{center}
\includegraphics[width=0.9\textwidth]{CENTRAL_CHANNEL.png}
\end{center}
\caption{Stage at various points downstream in the channel}
\label{Reach}
\end{figure}

Another minor point is that for the channelised flow, HECRAS computes the
friction slope in the channel using the hydraulic radius, which include the
'bank-drag' effect, whereas ANUGA does not. Hence, even away from the bridge
steady-state results can differ slightly.


% BEGIN LICENSE BLOCK
% Version: CMPL 1.1
%
% The contents of this file are subject to the Cisco-style Mozilla Public
% License Version 1.1 (the "License"); you may not use this file except
% in compliance with the License.  You may obtain a copy of the License
% at www.eclipse-clp.org/license.
%
% Software distributed under the License is distributed on an "AS IS"
% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
% the License for the specific language governing rights and limitations
% under the License.
%
% The Original Code is  The ECLiPSe Constraint Logic Programming System.
% The Initial Developer of the Original Code is  Cisco Systems, Inc.
% Portions created by the Initial Developer are
% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
%
% Contributor(s):
%
% END LICENSE BLOCK

\chapter{Program annotation}
When visualising CLP program behaviour, not all the variables of the
program are of interest.  {\eclipse} supports the concept of a set of
\viewable{} variables whose state over the course of a program run are
of interest to the user.  The library
\bipref{lib(viewable)}{../bips/lib/viewable/index.html} contains the
annotation predicates that allow a programmer to define (and expand)
these \viewable{} sets.

\section{Viewables}
\label{sec:viewables}
By collecting together related program
variables into a logical, multidimensional array-like structure called
a \viewable{}, the user can view the changing state of these variables
in a number of ways using the provided visualisation clients (these
will be covered in depth later (section \ref{sec:visu-clients})).

As an example of how to annotate an {\eclipse} program, consider the
following classic cryptographic example, \texttt{SEND+MORE=MONEY}

\begin{code}
sendmore(Digits) :-
    Digits = [S,E,N,D,M,O,R,Y],
    Digits :: [0..9],
    Carries = [C1,C2,C3,C4],
    Carries :: [0..1],
    alldifferent(Digits),
    S #\verb+\+= 0,
    M #\verb+\+= 0,
    C1         #= M,
    C2 + S + M #= O + 10*C1,
    C3 + E + O #= N + 10*C2,
    C4 + N + R #= E + 10*C3,
         D + E #= Y + 10*C4,
    labeling(Carries),
    labeling(Digits).
\end{code}

It is hopefully clear from the code that this formulation of the
classic puzzle uses four variables \texttt{[C1,C2,C3,C4]} to indicate
the \emph{carry} digits.  If we suppose that the user is only
interested in the behaviour of the program with respect to the primary
problem variables, which in this case corresponds to the variables
\texttt{[S,E,N,D,M,O,R,Y]}, then we can annotate the program code by
declaring a \viewable{} which contains these variables.

\begin{code}
sendmore(Digits) :-
    Digits = [S,E,N,D,M,O,R,Y],
    Digits :: [0..9],
    viewable_create(digits, Digits),
    ...
    ...
    labeling(Carries),
    labeling(Digits).
\end{code}

As can be seen, \viewable{}s are declared using the
\viewablecreatetwo{} predicate, the first parameter of which is an
atom which will be used to uniquely identify the \viewable{} later,
and the second argument is a (possibly nested) list of variables.

Declaring \viewable{}s has little performance overhead when running
code normally (that is to say, without any visualisation clients), and
so it is safe to leave the visualisation annotations in the code even
when not visualising.

\subsection{2D and beyond}
In the previous example, the created \viewable{} was a simple one
dimensional structure, it is possible however to create
multi-dimensional structures if the problem variables are so related.
For example one could choose to group the variables in a way that
mirrors the problem structure, for example a 2D array representing the
equation

\begin{center}
\begin{tabular}{c c c c c}
  & S & E & N & D \\
+ & M & O & R & E \\
\hline
M & O & N & E & Y
\end{tabular}
\end{center}

would be the array
\begin{displaymath}
\left(\begin{array}{c c c c c}
0 & S & E & N & D \\
0 & M & O & R & E \\
M & O & N & E & Y
\end{array}\right)
\end{displaymath}

and would be declared in the program as nested lists

\begin{quote}\begin{verbatim}
viewable_create(equation,[[0, S, E, N, D],[0, M, O, R, E],[M, O, N, E, Y]]
\end{verbatim}\end{quote}

or it could be declared in the program using {\eclipse} array syntax
\begin{quote}\begin{verbatim}
viewable_create(equation,[]([](0, S, E, N, D),
                            [](0, M, O, R, E),
                            [](M, O, N, E, Y)))
\end{verbatim}\end{quote}

Three points should be noted here,
\begin{enumerate}
\item \viewablecreatetwo{} accepts both nested lists and arrays.
\item Variables may occur more than once in \viewable{}.
\item Constants may occur in \viewable{}s.
\end{enumerate}

\subsection{Growth}

So far we have introduced only static (or \emph{fixed} dimension)
\viewable{}s, but it is conceivable that during the course of program
runs new variables may be introduced which the user has an interest in
looking at.  In order to accommodate this, \viewable{}s may be
declared as having \emph{flexible} dimensions.

To declare a \viewable{} with flexible dimensions, the three argument
form of the \viewablecreatethree{} predicate is used.  The third
argument specifies the type of the \viewable{} and at present the type
must be of the form \texttt{array(FixityList, ElementType)} where

\begin{description}
\item[\texttt{FixityList}] is a list with an atom \texttt{fixed} or
\texttt{flexible} specifying the fixity for each dimension. The fixity
denotes whether the dimension's size is fixed or may vary during the
time when the viewable is existent.
\item[\texttt{ElementType}] is a term which specifies the type of the
constituent viewable elements. Currently there are two supported
element types:

  \begin{description}
  \item[\texttt{any}] which includes any ECLiPSe term.
  \item[\texttt{numeric_bounds}] which includes any ground number,
  integer \bipref{fd}{../bips/lib/fd/index.html} variables,
  \bipref{ic}{../bips/lib/ic/index.html} variables and
  \bipref{range}{../bips/lib/range/index.html} variables (including
  \bipref{eplex}{../bips/lib/eplex/index.html} and
  \bipref{ria}{../bips/lib/ria/index.html} variables).
  \end{description}

\end{description}

Let us expand our example by assuming that, during the program run our
user is only interested in the \emph{digit} variables but once
labelling has finished they wish to also see the \emph{carry}
variables.  Clearly the user is free to simply print out the
\emph{carry} variables after completing the labelling, but within the
visualisation framework they may also expand the viewable by adding
the \emph{carry} digits to it.  The code to do this is

\begin{code}
sendmore(Digits) :-
    Digits = [S,E,N,D,M,O,R,Y],
    Digits :: [0..9],
    viewable_create(equation,
                    []([](0, S, E, N, D),
                       [](0, M, O, R, E),
                       [](M, O, N, E, Y)),
                    array([flexible,fixed], any)),
    ...
    ...
    labeling(Carries),
    labeling(Digits),
    viewable_expand(equation, 1, [C1, C2, C3, C4, 0]).
\end{code}

Once declared as flexible, dimensions may be expanded by the
\viewableexpandthree{} predicate.  The predicate specifies which
dimension to expand and which values should be added.  Had the
\viewable{} been 3 dimensional, then the values to be added would need
to be 2 dimensional.  In general for an N dimensional \viewable{},
when expanding a flexible dimension, the values to be added must be
N-1 dimensional arrays or nested lists.

As with \viewablecreatetwo{} and \viewablecreatethree{},
\viewableexpandthree{} silently succeeds with little overhead at
runtime, so it too may be left in code even when not visualising.

\subsection{Types}

As mentioned briefly in the previous section, \viewable{}s have a type
definition which determines what sort of values may be stored in them.
This type information allows visualisation clients to render the
values in a fitting manner.

Explicitly stating that the variables in a viewable are
\texttt{numeric_bounds} where known will increase the number
of ways in which the
\viewable{} elements may be viewed.

\subsection{Named dimensions}

Each position in a \viewable{}'s dimension has an associated name.  By
default, these names are simply the increasing natural numbers
starting from ``1''.  So, for example, in the previous code samples
the variable \texttt{R} would be at location \texttt{["2","4"]}.

By using the most expressive form, the \viewablecreatefour{} predicate
allows the user to assign their own symbolic names to dimension
locations.

In our ongoing example, we could name the first dimension positions
\texttt{["send", "more", "money"]} and the second dimension positions
\texttt{["ten thousands", "thousands", "hundreds", "tens", "units"]}.

A version of \viewableexpandfour{} exists also which allows the user to
assign a name to the new position of an expanded dimension.

Our completed example now looks like this

\begin{code}
:-lib(viewable).

sendmore(Digits) :-
    Digits = [S,E,N,D,M,O,R,Y],
    Digits :: [0..9],
    viewable_create(equation,
                    []([](0, S, E, N, D),
                       [](0, M, O, R, E),
                       [](M, O, N, E, Y)),
                    array([flexible,fixed], numeric_bounds),
                    [["send", "more", "money"],
                     ["ten thousands", "thousands",
                      "hundreds", "tens", "units"]]),
    Carries = [C1,C2,C3,C4],
    Carries :: [0..1],
    alldifferent(Digits),
    S #\verb+\+= 0,
    M #\verb+\+= 0,
    C1         #= M,
    C2 + S + M #= O + 10*C1,
    C3 + E + O #= N + 10*C2,
    C4 + N + R #= E + 10*C3,
         D + E #= Y + 10*C4,
    labeling(Carries),
    labeling(Digits),
    viewable_expand(equation, 1, [C1, C2, C3, C4, 0], "carries").
\end{code}

\subsection{Structured data}

In an effort to increase the ease with which program behaviour can be
viewed and to provide tighter integration between {\eclipse} modules,
data held in graph structures can also be annotated.

The following code demonstrates how a simple graph structure from the
\bipref{lib(graph_algorithms)}{../bips/lib/graph_algorithms/index.html}
library can be used to define a \viewable{}.

\begin{code}
:-lib(graph_algorithms).
:-lib(viewable).
:-lib(ic).

test:-
    make_graph(7,
               [e(1,2,F12), e(2,3,F23), e(2,4,F24), e(3,5,F35),
                e(4,5,F45), e(4,6,F46), e(5,6,F56), e(6,3,F63),
                e(6,7,F67)],
               Graph),
    Flows = [F23,F24,F35,F45,F46,F56,F63],
    Flows :: 0..5,
    (for(Node, 2, 6), param(Graph) do
        graph_get_incoming_edges(Graph, Node, InEdges),
        graph_get_adjacent_edges(Graph, Node, OutEdges),
        (foreach(e(_From, _To, Flow), InEdges),
         foreach(Flow, InFlow) do true),
        (foreach(e(_From, _To, Flow), OutEdges),
         foreach(Flow, OutFlow) do true),
        sum(InFlow) #= sum(OutFlow)
    ),
    F12 #= 9,
    viewable_create(flow_viewable, Graph, graph(fixed),
                    [node_property([0->[name(nodes), label]]),
                     edge_property([0->[name(edges), label]])
                    ]),
    labeling(Flows).
\end{code}

This simple network flow problem uses the graph structure to hold the
problem variables and also to define the network topology.  Note the
single \viewablecreatefour{} statement immediately before the
labeling step.

As with the regular list/array based viewable create calls, the first
argument specifies the viewable name and the structure containing the
variables of interest (in this case the graph) comes second.  The
third argument defines the type as being a graph whose structure is
fixed (as all graph_algorithms graphs are).  Currently only fixed
graphs are supported.  The final (optional) argument defines a mapping
between the node/edge structures within the graph and properties
useful for visualisation.  The table below outlines the currently
supported properties.

\begin{tabular}{|l|p{0.5\textwidth}|c|c|}
\hline
markup & meaning & applicability & required \\
\hline
\hline
name(String) & A unique name to refer to this property & both & yes \\
\hline
label & This property should be used as the node/edge text label & both & yes \\
\hline
\end{tabular}
For more control over the display of graphs structures, consider using
the \bipref{lib(graphviz)}{../bips/lib/graphviz/index.html} library.

\subsection{Solver variables}
The program annotations shown so far will work with most solvers in
{\eclipse} but not all.  Generally speaking if the solver operates by
monotonically reducing the domain of its variables then no further
annotations are required.  There are solvers however which do not
manipulate variables in this way.  For instance the
\bipref{lib(eplex)}{../bips/lib/eplex/index.html} library uses
{\eclipse} program variables as handles to the values calculated by an
external solver.  When solutions are found by the external solver, the
{\eclipse} variables are not (always) instantiated but rather must be
queried to obtain their values.

In order to facilitate the visualisation of such variables, the same
\viewable creation annotations can be used, but the name of the solver
must be given explicitly.  As an example consider the following
\bipref{lib(eplex)}{../bips/lib/eplex/index.html} model of a simple
transportation problem involving 3 factories \texttt{1,2,3} and 4
clients \texttt{A,B,C,D} taken from the {\eclipse} examples web page.

\begin{code}
%----------------------------------------------------------------------
% Example for basic use of ECLiPSe/CPLEX interface
%
% Distribution problem taken from EuroDecision chapter in D4.1
%----------------------------------------------------------------------

:- lib(eplex_xpress).
:- eplex_instance(foo).

%----------------------------------------------------------------------
% Explicit version (clients A-D, plants 1-3)
%----------------------------------------------------------------------

main(Cost, Vars) :-
        Vars = [A1, B1, C1, D1, A2, B2, C2, D2, A3, B3, C3, D3],
        foo:(Vars :: 0.0..10000.0),              % variables

        foo:(A1 + A2 + A3 $= 200),               % demand constraints
        foo:(B1 + B2 + B3 $= 400),
        foo:(C1 + C2 + C3 $= 300),
        foo:(D1 + D2 + D3 $= 100),

        foo:(A1 + B1 + C1 + D1 $=< 500),         % capacity constraints
        foo:(A2 + B2 + C2 + D2 $=< 300),
        foo:(A3 + B3 + C3 + D3 $=< 400),

        foo:eplex_solver_setup(
                       min(                      % solve
                           10*A1 + 7*A2 + 11*A3 +
                           8*B1 + 5*B2 + 10*B3 +
                           5*C1 + 5*C2 +  8*C3 +
                           9*D1 + 3*D2 +  7*D3)),

        foo:eplex_solve(Cost).
\end{code}

Adding the following line immediately before the call to
\texttt{eplex_solve/1} indicates that the solution values computed by
the eplex instance \texttt{foo} are of interest.  Note the
\emph{element type} field of the third argument says that the values
of interest may be changed by the solver \texttt{foo}.  Further note
that you will need to load the \viewable library inorder to access
these annotations.

\begin{code}
viewable_create(vars, Vars
                array([fixed], changeable(foo, any))),
\end{code}

This \emph{changeable} element type can appear in any form of the
annotations, so as another example, the following annotation gives
more structure to the variables.

\begin{code}
viewable_create(vars, []([](A1, A2, A3),
                         [](B1, B2, B3),
                         [](C1, C2, C3),
                         [](D1, D2, D3)),
                array([fixed,fixed], changeable(foo, any))),
\end{code}

As a final example, adding these two lines will make the structure of
the problem even more explicit.

\begin{code}
make_graph_symbolic([]('A','B','C','D',1,2,3),
                    [edge(1,'A',A1),edge(2,'A',A2),edge(3,'A',A3),
                     edge(1,'B',B1),edge(2,'B',B2),edge(3,'B',B3),
                     edge(1,'C',C1),edge(2,'C',C2),edge(3,'C',C3),
                     edge(1,'D',D1),edge(2,'D',D2),edge(3,'D',D3)],G),
viewable_create(network, G, graph(fixed,changeable(foo,graph_data))),
\end{code}

\quickref{Overview of program annotation}{
\begin{description}
\item[viewable_create\biprefnoidx{/2}{../bips/lib/viewable/viewable_create-2.html}\biprefnoidx{/3}{../bips/lib/viewable/viewable_create-3.html}\biprefnoidx{/4}{../bips/lib/viewable/viewable_create-4.html}]
  used to group problem variables for visualisation purposes.  Groupings
  referred to as \viewable{}s.
\item[viewable_expand\biprefnoidx{/3}{../bips/lib/viewable/viewable_expand-3.html}\biprefnoidx{/4}{../bips/lib/viewable/viewable_expand-4.html}] \viewable{}s can be of a fixed size, or can expand and shrink.
\item[types] elements of a \viewable{} may be defined as being numeric values or may be any \eclipse term.  The type of a \viewable{} will determine how it can be visualised.
\item[structure] interesting variables contained within graph structures can be directly annotated using the \texttt{graph(static)} viewable type.
\end{description}
}


\chapter{Probabilistic methods of surface water detection}
\label{ch4}

\begin{abstract}
	A method has been developed to estimate surface water mask for noisy TOA reflectance images, where water bodies can only be partially visible under thin clouds, haze, fog or snow/ice cover. The presence of these effects increases the need for high accuracy and more frequent observations of surface water bodies, making it essential for rapidly varying surface water bodies such as small reservoirs, wetlands, and floodplains. Monitoring these waterbodies at high frequency is essential to understanding changes in the availability of water for food, conditions of fisheries, ecosystems, and the occurrence of floods.

	Here, a Bayesian Network is used as a framework to introduce the method and, finally, to infer the actual surface water area from partially-visible surface water masks. The method makes use of a high-resolution probability density function, which is estimated using a set of cloud-free satellite images from measured by multiple freely available satellite missions from NASA and ESA. Variability of TOA reflectance values for different land use and atmospheric conditions to estimate cloud and snow detection parameters in an automated manner is explored. The method is validated using in-situ water level measurements.

	%\url{http://bit.ly/agu16\_reservoirs}

	\begin{center}
		\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0}]
		\node[draw=none,shade,blur shadow={shadow blur steps=5}] {
			\includegraphics[width=3in]{01.3-prob/figures/prob-title}
		};
		\end{tikzpicture}
	\end{center}

	\textbf{Keywords:} $M_2$, probabilistic graphical models, filling, Bayesian Networks, KDE, inference.

\end{abstract}

%% Start the actual chapter on a new page.
\newpage

\section{Introduction}
\dropcap{I}{n} the previous chapters, we saw various methods to detect surface water at high accuracy. However, when a waterbody is partially invisible, for example, due to clouds or missing pixels, these methods are insufficient to estimate the actual surface water. In this case, more advanced methods are required to fill missing pixels. In this chapter, this problem is addressed by making use of probabilistic graphical models, in particular, by formulating the task of inferring the most probable water mask in the form of a simple Bayesian Network.

The general idea is to combine methods discussed in the previous chapter with the probabilistic estimate of surface water mask using a priori knowledge about surface water. This will be done by generating a joint probability density function using surface water estimates based on images where an accurate surface water mask can be generated.

This turns our task of detecting surface water from satellite data into a generative class of models, where the most probable surface water mask is generated using density functions, in contrast to the discriminative way of processing of satellite data.

Additionally, a \gls{PGM} notation is used to discuss the steps required to estimate (or infer) the actual surface water mask from available raw satellite data.

\section{Probabilistic graphical models}

\glspl{PGM} (\cite{koller2009probabilistic}) is a rich framework for encoding probability distributions over complex domains, in the form of univariate or multivariate (joint) distributions. Random variables and their conditional dependencies are then expressed in the form of a graph, where child nodes are usually represented by \glspl{CPD}, either defined analytically or in a tabular form, and root nodes are represented by marginal distributions. Two commonly used types of \glspl{PGM} are \glspl{BN} and \glspl{MRF}. However, many other types of \glspl{PGM} exists today, such as \glspl{CRF}, factor graphs, restricted Boltzmann machines and many others.

\begin{wrapfigure}{r}{0.4\textwidth}
	\begin{center}
		\includegraphics[width=0.3\textwidth]{01.3-prob/figures/bn_example}
	\end{center}
	\caption{A simple Bayesian network.}
	\label{fig:bn_example}
\end{wrapfigure}

Here, a \gls{BN} notation is used, which is usually expressed in the form of a \gls{DAG}. Consider the example of a simple network as shown in \ref{fig:bn_example}. Here, $X_{1...3}$ nodes represent random variables and the shaded variable $X_2$ indicates a variable that is measured.

The root nodes of the graph of a BN are defined as marginal distributions, which are in general expressed as single-variable \glspl{PDF}, and all child nodes can then be represented as \glspl{CPD}, which can be expressed as multivariate probability distribution functions.

This DAG can then be used to conduct statistical inference, for example, to infer the unknown variables $X_3$ and $X_1$ by making assumptions about their distributions, while at the same time, satisfying the available (measured) distribution of $X_2$. The joint distribution of this BN can then be expressed (factorized) as:

\begin{equation}
P{(X_1, X_2, X_3)} = P{(X_1)} P{(X_2 \mid X_1)} P{(X_3 \mid X_1, X_2)}
\end{equation}

or in a general form:

\begin{equation}
P{(X_i, \ldots, X_n)} = \prod_{i=1}^{n} P{(X_i \mid S_i)}
\end{equation}

where $S_i$ represents a set of parents of every node $X_i$.

After defining the model, many types of questions can be answered, by means of direct inference methods, such as conditioning parent variables and computing resulting distributions of dependent variables, marginalization --- deriving marginal distribution by integrating over other variables,  or by applying methods of learning to simulate the graph as a whole and to infer hidden variables.

Many methods exist to perform inference in \glspl{BN}, such as \gls{MCMC}, or \gls{EM}. The main goal of these methods is to estimate unknown distributions from known variables and hypothetical relations between them. Many examples of \glspl{PGM} as well as methods used to conduct inference in \glspl{BN} are given in \cite{koller2009probabilistic}.

\section{Plate notation}

One of the useful notations used for \glspl{BN} is a \textit{plate notation}, which allows grouping of multiple random variables of a similar type. In this way, more complex models can be expressed by the network, as shown in Figure \ref{fig:plate-example}.

\begin{figure}[H]
	\centering
	\includegraphics[width=1\textwidth]{01.3-prob/figures/plate_example}
	\caption{An example of the use of plate notation (right) for Naive Bayes classifier graphical model (left).}
	\label{fig:plate-example}
\end{figure}

The above graph, in fact, represents a model of a Naive Bayes classifier, where the random variable $w$ represents a distribution of some feature belonging to some class, given a set of independent variables $X_i$ with known conditional distributions $P{(X_i|w)}$. During training, the model variables $X_i$ (conditional distributions) are defined. During inference, the most probable class is selected, for example, by estimating the maximum a posteriori probability.

\section{Discriminative vs. Generative methods}

In machine learning, a generative model is defined as a model where some variable values are generated from a joint probability density function. Generative models differ from discriminative models in that they require the use of joint probability density function to be constructed, while discriminative models provide a way to model only the target variables conditioned on observed variables \citet{ng2002discriminative}. Some examples of generative models include Naive Bayes classifier, Gaussian Mixture Model, and the Hidden Markov Model while examples of discriminative models include Logistic Regression, Support Vector Machines, and Conditional Random Fields.

The method presented in this chapter can be classified as a generative one, as the joint probability density function is estimated for pixels belonging to water $P(x, y)$. However, it will be estimated in a discriminative way, by combining surface water masks detected from cloud-free images, as was discussed in the previous chapter.

\section{Probabilistic nature of the satellite observations}

Here, the multispectral satellite observations are expressed in the form of a Bayesian network, to allow better reasoning about the methods used to detect surface water changes. Eventhough, standard algorithms used to reconstruct marginal distributions were not applied, the framework is a very rich way to express and explain probabilistic surface water inference methods introduced later in this chapter.

In multispectral remote sensing using satellites, we can think of observed radiance or reflectance values as random variables. In fact, the observed values represent a joint distribution in a multidimensional space (time, space, wavelength). Even though most of the sensors provide 2D surface radiance values, the actual values are reflected from the 3D surface of the Earth. The actual values measured at-the-sensor represent a signal, backscattered from the aerosol particles, and from the ground (frequently represented by non-Lambertian surfaces). The actual path of the light is extremely complex and may be impossible to model in a deterministic way. Interpreting this signal using probabilistic models makes more sense.

For passive multispectral satellite sensors, the most important factors disturbing the resulting radiance values are clouds and topographic effects. While many algorithms exist to detect clouds, modeling complex interaction of cloud and hill shadows, thin clouds, fog or haze is extremely difficult. Additionally, physical parameters of the sensors, such as spatial and radiometric resolution, result in observations of mixed signals, reflected from multiple surfaces, such as different types of soil, water, and vegetation. Furthermore, surface water pixels may be fully or partially covered by snow and ice.

In Figure \ref{fig:pgm} the most important variables influencing the observed signal are shown. Note, that the observed variables are shown as shaded circles and that wavelength, time and space ($x \in \mathbb{R}^2$) are defined using plate notation.

\begin{figure}
	\centering
	\includegraphics[width=1.0\textwidth]{01.3-prob/figures/probabilistic_model}
	\caption{\gls{BN} of a passive multispectral satellite sensor. Filled circles represent observed variables, with a known distributions. Empty circles indicate latent variables that need to be reconstructed. The variable $L$ defines observed radiances, where values in the distribution are based on the solar parameters, cloud cover, topography, as well as on the actual land use classes occurring at a given location. Plate notation is used to indicate dependency on time, space and wavelength.}
	\label{fig:pgm}
\end{figure}

A more general task of remote sensing can be defined as a classification of the hidden, or latent, variable $Y_{class}$ for every land-use type observed by the satellite. Clouds are introduced as a separate variable $C$, to stress, that it represents a noise and is not mixed with the actual land use type hidden under the clouds or distorted by the cloud shadows.

To indicate a complex light interaction between clouds and Earth's surface topography, a separate random variable $Z$ is added. It is a random variable, because it is never measured exactly, and may be very noisy for most existing global digital elevation datasets, such as 30m \gls{SRTM} or 3m \gls{NED}. These errors can be very large (sometimes ~50m) and may result in an incorrect interpretation of the observed pixels when used, for example, to model illumination effects, as shown in Chapter \ref{ch2}.

The Sun parameters, $S_\phi$ and $S_\theta$ represent the Sun's azimuth and elevation at the time, when the image was measured, and are usually very accurate, perhaps making it more appropriate to introduce them as parameters instead of random variables.

The Sun's light interacts with clouds and topography, which results in a distortion of the signal, which is expressed by the variables $C_s$, $Z_s$, which may indicate the amount of light scattered and absorbed due to clouds and shadows.

The arrows between these variables represent conditional dependencies. For example, the conditional probability representing the amount of light influenced by cloud shadows can be defined as: $P(C_s|Z,S_\phi,S_\theta,C)$. Here, the cloud shadow variable depends on the topography because the actual illumination usually changes depending on the cloud height, which in turn depends on the actual elevation at that location.

To reconstruct (infer) unknown variables, one can try to simulate the joint probability for this PGM factored as:

\begin{equation}
\begin{split}
\begin{aligned}
P{(L_{\lambda_1}, ... L_{\lambda_N}, C, C_s, Z, Z_s, S_\phi, S_\theta, Y_{class_1} ... Y_{class_M})} &= P(Y_{class_1}) ... P(Y_{class_M}) \\
& P{(C)} P{(S)} P({C_s)} P{(S_\theta)} P{(S_\phi)}  P{(Z_s)} P{(Z)} \\
& P{(C_s \mid C,S_\theta,S_\phi,Z)}  P{(Z_s|Z,S_\theta, S_\phi)}
\end{aligned}
\end{split}
\label{eq:bn-joint}
\end{equation}

with $N$ bands $\lambda$ and $M$ classes $class$ defined as:

\begin{equation}
\begin{split}
\begin{aligned}
\lambda &= \{blue, green, red, nir, swir1, swir2, temp\} \\
class &= \{water, snow, sand, vegetation, urban, ...\}
\end{aligned}
\end{split}
\end{equation}

Note that in addition to the class and wavelength dimensions, most of the variables also depend on space and time, which is indicated as indices of the corresponding plates.

While variables $Y_{class}$ can be simulated as discrete variables, with a corresponding probability mass function representing the probability of a pixel belonging to a given class, we can also think of it as a continuous variable, defined for every class separately and indicating probability of a pixel fraction (varying from 0 to 1) belonging to a given land-use class. Many of the variables in the above equation are known or partially known as well as their respective errors, however, it may be difficult to simulate the whole joint distribution using standard methods (MCMC), mainly because of the amount of data to be processed.

For the water detection task, we are interested in the reconstruction of $P(C_{water})$ distribution, or more generally - as a multivariate distribution $P(C_{water}, x, y)$, marginalized in time and representing the probability of water in every pixel of a given area for some specific period. It is important to note that this can be done only under the assumption that morphology of the waterbody does not change during the selected period. For more complex configurations, this density function should be assumed as a time-dependent function, for example, to represent seasonal surface water variability.

One of the methods to infer this water distribution, as well as other latent variables in the above graphical model, is to estimate them in a discriminative way, directly from the observed reflectance values measured by satellite sensors. With sufficient measurements and under the assumption that morphology of the waterbody does not change significantly, we should be able to condition our joint distribution \ref{eq:bn-joint} into subsets where clouds are present and absent. Also, instead of integrating this joint distribution, marginalizing it to estimate $P(C_{water}, x, y)$, it can be directly computed by making use of the methods described in Chapter \ref{ch3} and estimating the frequency of pixels to be covered by water or land.

An alternative could be to fully simulate the above graphical model by introducing some analytical models, like FMask or topographic correction formulas from Chapter \ref{ch2} to simulate the \glspl{CPD} representing cloud and hill shadows. After the reconstruction, standard methods used for \glspl{BN} can be applied to infer all other unknown land-use class variables $P(C_{class})$.

%\url{http://sci-hub.cc/10.1111/j.1467-9876.2010.00739.x}
%\url{https://courses.engr.illinois.edu/cs543/sp2011/lectures/Lecture\%2012\%20-\%20MRFs\%20and\%20Graph\%20Cut\%20Segmentation\%20-\%20Vision_Spring2011.pdf}

\subsection{Sampling spectral reflectance values from multi-temporal Landsat images for water and land pixels}

In many cases, distributions of the marginal variables introduced in Figure \ref{fig:pgm} are unknown and may represent very dynamic processes occurring at the same time. To demonstrate this complexity, N=100 locations are sampled for the area outlined in Figure \ref{fig:study-area-PC}, and then, using only an area covered by surface water, in the middle of the reservoir. Because surface water of the reservoir changes in time, but also because image registration may be imperfect, it is hard to ensure that all sampled values correspond to water and clouds only. However, it should be sufficient to demonstrate the variability of the processes over a period of 30 years.

\begin{figure}[H]
	\includegraphics[width=0.8\textwidth,center]{01.3-prob/figures/map-ProsserCreekReservoir-sampling-area}
	\centering
	\captionsetup{justification=centering}
	\caption{Sampling area used for scatter plots, Prosser Creek Reservoir, CA, USA}
	\label{fig:study-area-PC}
\end{figure}

\begin{figure}[H]
	\includegraphics[width=1.0\textwidth,left]{01.3-prob/figures/sampled_water_clouds_snow}
	\includegraphics[width=1.0\textwidth,center]{01.3-prob/figures/sampled_scalebar}

	\caption{TOA reflectance values sampled over the study area but covering mainly water pixels. Scatter plot points are colored as a false-color combination: [swir1, nir, green]. The scalebar indicates probability after applying the \gls{KDE} method.}
	\label{fig:prob-sampled-water-cloud-snow}
\end{figure}

\begin{figure}[H]
	\includegraphics[width=1.0\textwidth,left]{01.3-prob/figures/sampled_water_soil_clouds_snow}
	\includegraphics[width=1.0\textwidth,center]{01.3-prob/figures/sampled_scalebar}

	\caption{TOA reflectance values sampled over study area (water and soil pixels) from all Landsat images available for that area (N=930).}

	\label{fig:prob-sampled-water-soil-cloud-snow}
\end{figure}

\begin{figure}[H]
	\includegraphics[width=1.0\textwidth,left]{01.3-prob/figures/sampled_water_soil}
	\includegraphics[width=1.0\textwidth,center]{01.3-prob/figures/sampled_scalebar}

	\caption{TOA reflectance values after conditioning on cloud and snow cover.}

	\label{fig:prob-sampled-water-soil}
\end{figure}

The resulting scatter matrix plots, colored as a false-color image using swir1, nir, and green bands, are shown in figures \ref{fig:prob-sampled-water-cloud-snow} and \ref{fig:prob-sampled-water-soil-cloud-snow}, for water-only pixels and water plus soil pixels. Additionally, the density of the pixels for every scatter plot in the lower triangle is analyzed. The reason for this exercise is to try to see if different classes can be determined from these distributions or at least to identify the most frequently occurring values in the case of water, soil, clouds or snow. It is interesting to see that for this area, the distance between the most frequently occurring values, which can be used to separate water and land, correspond to nir and green bands. However, also the temperature band shows a lot of variability, where temperature values are formed by the presence of snow, soil, and water in pixels (three peaks in the histogram of the temperature band) as shown in the directed graph above. The Figure \ref{fig:prob-sampled-water-cloud-snow} represents a joint distribution, conditioned on some of the variables, in this case, on $P(C_{water}=1)$. As can also be seen, reflectance values for clouds and snow vary significantly in contrast to lower reflectance values in the case of water or soil.

By sampling values in the center of the reservoir and the area covering water and the nearby land, some of the parameters can be automatically reconstructed from these distributions to be used in combination with classical remote sensing methods for cloud detection, such as ACCA or FMask. Adjusting the actual thresholds used in these methods with more objective parameters, it should be possible to automatically identify fully cloud-free images, and then use them to simulate the marginal distribution representing water occurrence.

Here, this approach is used to adjust threshold values used to estimate clouds and snow scores, to identify the most cloud-free images over the reservoir area (in fact, conditioning the joint distribution for $L$ on a $C=0$). These cloud-free images are used to reconstruct the marginal distribution $P(C_{water}, x, y))$ for a given time interval, assuming that the water dynamics under the clouds looks similar to when there are clouds.

\section{Probabilistic filling of missing pixels in surface water detection}

One of the ways to solve the problem of filling missing pixels, or to remove the less probable false-positive water pixels is to make use of the a priori information about surface water dynamics. By adjusting cloud (or snow/ice) detection algorithms with local statistics, we can identify images where the number of bad or missing pixels is small, resulting in very accurate water masks. The figures \ref{fig:prob-water-isolines} and \ref{fig:prob-water-density} show edges for several of these water masks and a 2D probability density image estimated from these water masks.

\begin{figure}[H]
	\centering
	\includegraphics[width=1\textwidth]{01.3-prob/figures/contour_lines}
	\caption{Surface water mask detected from cloud-free images (multiple sensors). The images were resampled using bicubic interpolation before the actual mask was detected. This has resulted in a better final water occurrence.}
	\label{fig:prob-water-isolines}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=0.6\textwidth]{01.3-prob/figures/water_occurrence}
	\caption{Water occurrence (prior) estimated from cloud-free images measured by multiple satellites (ASTER, Sentinel-2, and Landsat)}
	\label{fig:prob-water-density}
\end{figure}

\begin{figure}[H]
	\centering
	\includegraphics[width=1\textwidth]{01.3-prob/figures/method-diagram}
	\caption{A high-level workflow of the surface water detection method}
	\label{fig:prob-workflow}
\end{figure}

The method to fill surface area for missing pixels is based on the use of this probability density image to infer water mask, employing Bayes' rule. The actual algorithm involves a few more steps, where the reconstructed water \gls{PDF} is also used during edge detection (less probable water edges are removed). The resulting algorithm, outlined in Figure \ref{fig:prob-workflow} allows for the automated estimation of reservoir surface area using both cloud-free and partially cloud-free images.

In fact, the inference step of the algorithm can be schematically visualized as in \ref{fig:prob-diagram}, where on the left side, the original graph is shown, with all irrelevant variables removed.

\begin{figure}[H]
	\centering
	\includegraphics[width=0.4\textwidth]{01.3-prob/figures/probabilistic_model_single_scene}
	\caption{An example of a BN for a single scene, assuming marginal distribution on the left estimated during the previous step.}
	\label{fig:prob-diagram}
\end{figure}

During the first step, where the water probability density function was estimated, the Landsat 7 and Sentinel-1 images were excluded, because of SLC-OFF errors and because the \gls{SAR} water detection algorithm may be less reliable, and because of effects like Bragg scattering, resulting in false-negative pixels.

In many cases, slightly better results are achieved for noise-free pixels when compared to probabilistic estimate (our initial prior estimate is never perfect). An additional step was added to choose between a generative surface water estimate and the one performed using discriminative methods. The choice between different water masks detected using generative (density function) and discriminative (dynamic thresholding) methods is made based on the quality of the pixels, which depends on cloud and snow scores. For very hilly topographies, it may be necessary to make use of $Z_s$ as well, to avoid misclassification of hill shadows as water pixels.

\section{Application to the reservoir surface water area reconstruction}

\begin{figure}
	\centering
	\includegraphics[width=1.0\textwidth]{01.2-water-detection/figures/PC_NDWI_3_Otsu_prior}
	\caption{Canny/Otsu, probabilistic update}
	\label{fig:r1_canny_otsu_nb}
\end{figure}

\begin{figure}
	\centering
	\includegraphics[width=1.0\textwidth]{01.2-water-detection/figures/PC_NDWI_2_Otsu_HAND_prior}
	\caption{Canny/Otsu, HAND < 15m, probabilistic update}
	\label{fig:r1_canny_otsu_hand_nb}
\end{figure}

\begin{figure}
	\centering
	\includegraphics[width=1.0\textwidth]{01.2-water-detection/figures/PC_NDWI_4_Otsu_HAND_prior_fill}
	\caption{Canny/Otsu, probabilistic update, HAND < 15m, fill}
	\label{fig:r1_canny_otsu_hand_nb_fill}
\end{figure}

\begin{figure}
	\centering
	\includegraphics[width=1.0\textwidth]{01.2-water-detection/figures/PC_NDWI_4_Otsu_HAND_topo_prior_fill}
	\caption{topographic, Canny/Otsu, probabilistic update, HAND < 15m, fill}
	\label{fig:r1_canny_otsu_topo_hand_nb_fill}
\end{figure}

A hybrid, stepwise method, combining image processing algorithms and remote sensing methods would be more appropriate for the accurate estimation of the water mask.

\newpage

\section{Measuring performance of surface water detection with filling applied}

To evaluate the overall multi-step algorithm performance, where a combination of the dynamic thresholding and the generative estimation of surface water mask were involved, the final surface water area values were compared with the surface water area estimates computed using the statistical model introduced in Chapter \ref{ch3}.

Additionally, surface water masks not passing the quality criteria were excluded. The quality score was defined as a combination of multiple parameters, such as the fraction of false-positive, false-negative pixels, and the number of cloud and snow pixels.

The final surface water area time series, together with the original surface area, estimated from only optical satellite images and NDWI=0, are shown in Figure \ref{fig:prob-results}.

To further explore the quality of the estimates, a residuals analysis is performed for the final results, comparing them to the estimates based on the model constructed for the reservoir. We can see from the figures, that the errors are mostly normally distributed, $RMSE=0.059km^2$, indicating good performance of the algorithm.

In the case of cloud presence, the actual threshold variability of the spectral indices makes them less applicable, resulting in much larger variation of the surface water mask estimates. Use of the step-wise approach introduced in the last two chapters provides a way to automatically detect surface water with a very high degree of accuracy.

\begin{figure}[H]
	\centering
	\includegraphics[width=1.0\textwidth]{01.3-prob/figures/results}
	\caption{Comparison of estimated surface water area $A$ vs. simulated surface water $A*$, based on the linear regression constructed previously. Scatter plots (a, b) show results of the surface water detection algorithm, without (a) and with (c) probabilistic filling. Time series charts (b, d) correspond to estimated (dots) and measured surface water area values.}
	\label{fig:prob-results}
\end{figure}

\section{Analysis of residuals and outliers}

\begin{figure}[H]
	\centering
	\includegraphics[width=1.0\textwidth]{01.3-prob/figures/residuals}
	\caption{Analysis of residuals (surface area estimated from satellite images vs. surface area estimated from the linear model). The top chart (a) compares time series estimated from water level measurements and satellite photos (dots), as well as outliers ($\epsilon > \sigma$). Chart (b) shows residuals values, assuming surface water area A* as the ground truth. Scatter plots (c, d) show a comparison of measured water level and estimated surface water area (c) vs. water levels and A/A* plot (d). The chart (e) indicates histogram of residual values (<3\%).}
	\label{fig:prob-results}
\end{figure}

%\section{Comparison with the JRC surface water mask dataset}
%\textbf{compare monthly surface water changes estimated from JRC dataset %(scatter plots, time series and residual analysis)}

\section{Difficult surface water detection examples}

To demonstrate the method performance, figures \ref{fig:prob-guess1}, \ref{fig:prob-guess2}, \ref{fig:prob-guess3} demonstrate a few examples where the surface water mask estimate is particularly difficult. Using the method presented here, an almost perfect water mask can be reconstructed, when compared to the observed water level values.

The first example in Figure \ref{fig:prob-guess1} illustrates the algorithm performance for the case in which the reservoir is almost fully covered by clouds, and only a small area in the upper part of the reservoir can be seen. Because these pixels are located in a very flat area, where water surface area changes correlate with changes in water levels, accurate estimates of the actual surface area could be made by utilizing the density function constructed above. This case may result in larger errors in some reservoirs, where large morphological changes may take place in the river. In this case, our prior distribution, used to provide posterior estimates, is less informative. In this case, it would be more appropriate to utilize the temporal variability of the surface area. This is relatively easy to achieve by constructing the water occurrence distribution as three-dimensional, instead of two-dimensional - $P(C_{water}) = f(x,y,t)$, where the probability of water at a given location may change in time as well.

\afterpage{%
	\begin{figure}
		\centering
		\includegraphics[width=0.8\textwidth]{01.3-prob/figures/guess1_clipped.png}
		\caption{Surface water reconstructed from a small amount of visible water at the edge of the reservoir. Visible water pixels are located in a dynamic part of the reservoir.}
		\label{fig:prob-guess1}
	\end{figure}

	\begin{figure}
		\centering
		\includegraphics[width=0.8\textwidth]{01.3-prob/figures/guess2_clipped.png}
		\caption{Surface water reconstructed from a Sentinel-2 image with a very dark cloud shadow present. False-positive surface water detected during first pass (\textcolor{red}{red}) is corrected after the use of Bayesian update step.}
		\label{fig:prob-guess2}
	\end{figure}

	\begin{figure}
		\centering
		\includegraphics[width=0.8\textwidth]{01.3-prob/figures/guess3_clipped.png}
		\caption{Surface water mask reconstructed from Sentinel-1 \gls{SAR} image. A very limited number of pixels are visible in a steep part of the reservoir.}
		\label{fig:prob-guess3}
	\end{figure}
	\clearpage
}

Figure \ref{fig:prob-guess2} shows a case where the original surface water mask estimate is incorrect with regards to the very dark cloud shadow pixels. The image was acquired at a relatively low sun elevation, resulting in lower reflectance values, with a very evident cloud shadow. In fact, looking at the values \gls{NDWI} of the pixels, corresponding to water and cloud shadow only, it was impossible to detect the surface water mask reliably. In this situation, as well as in the case of SLC-OFF images, the surface water mask can be accurately estimated. This claim holds true because a strong correspondence between water occurrence and detected surface water edges is expected to occur.

A slightly more difficult case can be seen in Figure \ref{fig:prob-guess3}, where a small part of the Sentinel-1 \gls{SAR} backscatter intensity image is used to estimate surface water. The challenge here lies in the fact that this part of the reservoir is relatively steep, resulting in a much higher chance of an incorrect surface water area estimate. Another challenge here is related to the presence of a very dark edge present in the Sentinel-1 image, which was excluded by eliminating low entropy areas along the image edges.

Density-based methods may be extremely useful when the waterbody is partially covered by clouds, cloud shadows, or when only part of a waterbody is visible, due to limited swath or due to sensor artifacts (SLC-OFF for Landsat 7).

\section{Conclusions and Discussion}

In Chapter \ref{ch3}, it was demonstrated that automated surface water detection from multiple multispectral missions is feasible, however, the surface water detection from noisy satellite images remained difficult. Where such noisy images are frequently occurring (e.g. in predominantly clouded regions) or where high frequency observations are required, the applicability of the method presented in Chapter \ref{ch3} is limited and more advanced methods are required.

In this chapter, it has been shown that even when a small part of a surface water body is observed and the remainder obscured, the rest of the surface water body can be inferred by analyzing historical imagery in which accurate estimates of the surface water body were established. The accuracy of the final surface water mask depends on how informative the observed surface water area is, which directly depends on how many changes (inter- or intra-annual) occur to the surface water at a given location.

The historical imagery is introduced in the water classification algorithm through Bayesian inference. However, this also assumes independence of the observations from each other, resulting in overestimation or underestimation of surface water area in several cases, such as very noisy images with multiple types of noise present: snow, clouds, shadows or very noisy backscatter data. To overcome these problems, many other methods can also be applied such as Hidden Markov Models (HMM), where transition probabilities can be estimated based on multiple historical observations.

Essentially, the method presented in this chapter allows enhancing the spatial resolution of the final estimated water mask. This is done by combining surface water masks from multiple sensors, with the spatial resolution varying between 10m (Sentinel-2) up to 30m (for Landsat). The final density function was estimated at a resolution of 5m combined with the bicubic resampling applied to the images to interpolate values. Eventhough the actual spectral signature gets damaged at the sub-pixel level, it was possible to generate very accurate surface water mask estimates due to the use of local dynamic thresholding methods introduced in Chapter \ref{ch3}.

The framework presented in this chapter can also be used in combination with more general image segmentation methods instead of local thresholding. Alternatives may include image segmentation algorithms such as local k-means (SLIC, \citet{achanta2012slic}), combined with methods such as Markov Random Fields (MRF) or Conditional Random Fields (CRF) to infer the actual land use types based on the spatial neighborhood and temporal variability of reflectance values.