% Sample LaTeX file for creating a paper in the Morgan Kaufmannn two
% column, 8 1/2 by 11 inch proceedings format.

\documentclass[]{article}
\usepackage{proceed2e}
\usepackage[
backend=bibtex,
style=alphabetic,
citestyle=authoryear
]{biblatex}
\usepackage{aliascnt}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{caption}
\usepackage{mathtools}
\usepackage[capitalise, noabbrev]{cleveref}
\usepackage{float}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{bbm}
\usepackage{amsthm}

% Set the typeface to Times Roman
\usepackage{times}

%\binoppenalty=\maxdimen % to prevent breaking equations
%\relpenalty=\maxdimen % to prevent breaking equations
% Define the equation float env
\newaliascnt{eqfloat}{equation}
\newfloat{eqfloat}{h}{eqflts}
\floatname{eqfloat}{Equation}

\newtheorem{proposition}{Proposition}

\title{Differentiable Particle Filter}

\author{} % LEAVE BLANK FOR ORIGINAL SUBMISSION.
          % UAI  reviewing is double-blind.

% The author names and affiliations should appear only in the accepted paper.
%
%\author{ {\bf Harry Q.~Bovik\thanks{Footnote for author to give an
%alternate address.}} \\
%Computer Science Dept. \\
%Cranberry University\\
%Pittsburgh, PA 15213 \\
%\And
%{\bf Coauthor}  \\
%Affiliation          \\
%Address \\
%\And
%{\bf Coauthor}   \\
%Affiliation \\
%Address    \\
%(if needed)\\
%}

\addbibresource{DPFbibli.bib}

\begin{document}

\maketitle

\begin{abstract}
	Sequential Monte Carlo (SMC or Particle Filters) methods are a set of powerful techniques for continuous state space models. Additionally to providing a belief for the state of an observed system, they also provide an estimate of its likelihood which is non-differentiable with respect to the model parameters. This is due to the fact that standard resampling schemes consist in a non-smooth re-indexing of the Monte Carlo sample. In this article we propose two new resampling schemes that rely on regularised optimal transport techniques and are respectively differentiable but biased, and non-differentiable but optimal in a certain metric. Furthermore we assess the behaviour of the methods in a linear setup by comparing with the Kalman Filter and discuss the influence of the hyper-parameters.
\end{abstract}

\section{INTRODUCTION}
	Particle Filters \parencite[see][]{particlefilter} offer an efficient way of performing posterior inference in otherwise intractable non-linear state space models and provide an unbiased estimate of the likelihood of the state space model parameters given observed data. Formally particle filters are interested in estimating state space hidden Markov models described by an unobserved state $X_t \in \mathbb{R}^{d_X}$ following $X_t|(X_{t-1}=x) \sim f_t(\cdot|x), t > 0$ and $X_0 \sim \mu(\cdot) \in \mathcal{M}(\mathbb{R}^{d_X})$ and an observed process $Y_t|(X_t=x) \sim g_t(\cdot|x) \in \mathcal{M}(\mathbb{R}^{d_Y})$. They do this by keeping track of $X_t$ in the form of a weighted sample $(w^i_t, X_t^i)$ through a method called Sequential Importance Sampling, however, this technique applied by itself leads to weight degeneracy that needs to be mitigated \parencite[see][]{doucet2009tutorial}, a well accepted way to fight this is through the use of a resampling scheme that replaces low weight particles with high weights ones \parencite[see][]{hol2006resampling}.

	In this paper we focus on regularised optimal transport as a resampling technique in two different ways: in \cref{sec:differentiable} we use the planning matrix as a direct map from a weighted sample $(w_i, X_i) \sim \mathbf{X}$ to an equally weighted sample $(\mathbbm{1}_N, \mathbf{Z}_i^{\epsilon}) \sim \mathbf{X}^{\epsilon}$, where $\mathbbm{1}_N$ is the vector of size $N$ filled with $\frac 1 N$ and show $\mathbf{X}^{\epsilon} \xrightarrow[\epsilon \to 0]{\mathcal{L}} \mathbf{X}$, because the mapping we use is differentiable w.r.t $(w_i, X_i)$, it will provide a biased but differentiable resampling scheme, then in \cref{sec:optimal} we provide a novel algorithm that learns the optimal equally-weighted sample $(\mathbbm{1}_N, \mathbf{Z}_i^{\epsilon})$ in the $\epsilon$-Sinkhorn divergence sense.

	Our main contributions are two fold: we introduce the planning matrix of the regularised optimal transport as a differentiable resampling scheme and we introduce a novel resampling algorithm that guarantees optimality of the resampled particles.

	The rest of the paper is organised as follows: in \cref{sec:background} we give a brief recapitulation of the Particle Filter \parencite[][]{particlefilter}, the biased Sinkhorn distances \parencite[][]{cuturi2013sinkhorn} and the Sinkhorn divergence \parencite[][]{feydy:interpolating}, in \cref{sec:differentiable} we introduce the differentiable particle filter, discuss its behaviour and provide examples, in \cref{sec:optimal} we introduce an optimal resampling scheme and provide examples for it, and finally we conclude with future possible extensions and improvements.

\section{BACKGROUND}
\label{sec:background}
	\subsection{Particle Filters}
	\label{subsec:PF}

		Particle filters have emerged as a standard technique for non linear data assimilation and approximates the filtering (posterior) distribution $X_t|(Y_t=y, X_{t-1}))$ using a weighted set of $N$ samples $(w_i, X_i)$ which are updated following a predict-update approach. Any given quantity of interest $\mathbb{E}[\phi(X_t)]|(Y_t=y, X_{t-1}))]$ can then estimated as a weighted average $\sum_i w_i \phi(X_i)$ with variance given by a central limit theorem \parencite[see][]{chopin2004central}.
		The "predict step" consists in proposing particles $X_t^i \sim p(\cdot|\mathbf{X}_{t-1}=X_{t-1}^i)$ whose weights will then be updated as per the bayes formula:

		\begin{align}
			w_t^i &= p(X_t^i|\mathbf{Y}_t=\mathbf{y},X_{t-1}^i) \\
			&\propto p(Y_t|X_{t}^i) \cdot p(X_t^i|X_{t-1}^i) \label{eq:likelihood}\\
			&= p(Y_t|X_{t}^i) \cdot w_{t-1}^i
		\end{align}

		These will then be normalised to sum to 1. Additionally the marginal likelihood $P(\mathbf{Y}_{u=1..t}|\mathbf{X}_{u=1..t}, \mathbf{\theta})$ up to time time $T$ where $\mathbf{\theta}$ are model parameters can be estimated through \cref{eq:likelihood} prior to normalisation.

		However, as time passes, the weights suffer a well-known degeneracy problem, that is all weights will converge to 0 apart from 1, hence not providing a good distributional estimate for the posterior. This has traditionally been mitigated by ancestry resampling: instead of proposing $X_t^i \sim p(\cdot|\mathbf{X}_{t-1}=X_{t-1}^i)$, we propose $X_t^i \sim p(\cdot|\mathbf{X}_{t-1}=X_{t-1}^{a_i})$ where $a_i \in {1,..,N}$ is any index sampling such that $\mathbb{E}[\sum_i \mathbb{I}(a_i=j)|\mathbf{w}] = N w_j, \, \forall j \in {1,...,N}$ \parencite[see][]{doucet2009tutorial}. In practice this is only done when the efficient sample size (ESS) $\frac{1}{\sum_i w_i^2}$ is lower than a certain threshold (usually 50\% of the sample size $N$).

		This is summarised in \cref{algo:bootstrap} and \cref{algo:resampling}. Because of the reparametrization trick \parencite[see][]{kingma2013auto}, only \cref{algo:resampling} makes the particle filter non-differentiable with respect to its inputs.

		\begin{algorithm}
			\caption{Bootstrap Particle Filter}
			\label{algo:bootstrap}
			\begin{algorithmic}
				\STATE{\bfseries Input:} $X_i$, $w_i$, $y$, $N$, $X$, $L$ \COMMENT{Inputs at time $t > 0$},
				\IF{$\text{ESS} < N \cdot \text{threshold}$}
					\STATE{Resample}
				\ENDIF
				\FOR{$i=1$ {\bfseries to} $N$}
					\STATE{Propose:} Sample $X_i ~ p(X_{t+1}|X_t=X_i)$
					\STATE{Update:} Compute $w_i *= p(Y_t=y|X_t=X_i)$
				\ENDFOR
				\STATE{Compute log-likelihood update:} $L+=\log{\sum_i w_i}$

				\FOR{$i=1$ {\bfseries to} $N$}
					\STATE $w_i = \frac{w_i}{\sum_i w_i}$
				\ENDFOR
			\end{algorithmic}
		\end{algorithm}

		\begin{algorithm}
			\caption{Generic resampling}
			\label{algo:resampling}
			\begin{algorithmic}
				\STATE{\bfseries Input:} $X_i$, $w_i$, $N$,
				\FOR{$i=1$ {\bfseries to} $N$}
					\STATE{Sample:} $a_i$ \COMMENT{satisfying hypotheses, for example $a_i \sim \text{Multinomial}(\mathbf{w})$}
					\STATE{Set:} $w_i = \frac{1}{N}$, $X_i = X_{a_i}$
				\ENDFOR
				\OUTPUT $\mathbf{w}$, $\mathbf{X}$
			\end{algorithmic}
		\end{algorithm}

	In this article, similarly to \parencite{reich2012nonparametric,graham2019scalable} we consider an ensemble approach to particle filtering: instead of the resampling scheme $(w_i, X_i) \mapsto (\frac 1 N, X_{a_i})$ we provide two new mappings: we present a biased ensemble reweighting (\cref{sec:differentiable}) that comes from the planning matrix solution of a regularised Optimal Transport problem $(w_i, X_i) \mapsto (\frac 1 N, (M_{\epsilon}^{\mathbf{w}, \mathbf{X}} X)_i)$, and we present a learnt optimal recentering of the particles learnt to minimize the Sinkhorn Divergence \parencite[see][]{genevay2017learning,feydy:interpolating} $(w_i, X_i) \mapsto (\frac 1 N, Z^{\epsilon}_i)$.

	\subsection{REGULARISED OPTIMAL TRANSPORT}
	\label{subsec:OT}
		\subsubsection{Optimal Transport}
			Optimal transport is interested in computing a distance between measures. Formally, given two empirical probability measures $\alpha, \beta \in \mathcal{M}^+_1(\mathcal{X})$, and given a symmetric positive cost function $C: \mathcal{X} \times \mathcal{X} \to \mathbb{R}$, it is interested in computing both the minimum and the minimising argument of the functional $\pi \mapsto \int_{\mathcal{X}^2} C(x,y) d \pi$ for $\pi$ belonging to the simplex
			$$S_{\alpha, \beta} = \left\{ \pi \in \mathcal{M}^+_1(\mathcal{X} \times \mathcal{X} ) | \int \pi(\cdot, dy) = \alpha, \int \pi(dx, \cdot) = \beta \right\}$$

		\subsubsection{Sinkhorn distances}

			If the supports of $\alpha$ and $\beta$ are of size $N$, this is known to scale in $O(n^3)$. \cite{cuturi2013sinkhorn} shows that a regularised version of the algorithm can be considered instead:
			$$\textbf{OT}_{\epsilon} := \min_{\pi \in S_{\alpha, \beta}} \int_{\mathcal{X}^2} C(x,y) d \pi + \epsilon KL(\pi||\alpha \otimes \beta)$$

			Thanks to Fenchel-Rockafellar theorem, this can be rewritten \parencite[see][]{feydy:interpolating,peyr2018computational} using dual functions $f, g \in \mathcal{C}(\mathcal{X})$:
			\begin{align}
				\textbf{OT}_{\epsilon} 	=&\max_{f, g \in \mathcal{C}(\mathcal{X})} \langle \alpha, f \rangle + \langle \beta, g \rangle \\
										&- \epsilon \langle \alpha \otimes \beta, \exp \left( \frac 1 \epsilon \left( f \oplus g - C \right)  \right) - 1 \rangle \nonumber\\
				\text{with: } \pi_{\epsilon} 			= &\exp \left( \frac 1 \epsilon \left( f \oplus g - C \right)  \right) \cdot \alpha \otimes \beta \label{eq:plan}
			\end{align}

			This has been key for the recent development of computational optimal transport \parencite{peyr2018computational} as it translates a problem over a matrix to a problem over two related vectors. Moreover the optimality condition on $f$ and $g$ can be written in a fixed-point theorem form: if $\alpha = (w^X_i, X_i)_{1 \leq i \leq N}, \beta = (w^Y_j, Y_j)_{1 \leq j \leq M}$ \parencite{feydy:interpolating} which one only has to iterate successively to until convergence.

			\begin{align}
				\forall i, j &\nonumber\\
				f_i &= -\epsilon \text{LSE}_k(\log w^Y_k + \frac 1 \epsilon g_k - \frac 1 \epsilon C(X_i, Y_k)) \label{eq:fixed_1}\\
				g_j &= -\epsilon \text{LSE}_k(\log w^X_k + \frac 1 \epsilon f_k - \frac 1 \epsilon C(X_k, Y_j)) \label{eq:fixed_2}
			\end{align}

		\subsubsection{Sinkhorn divergences}
			\label{subsubsec:Div}
			Crucially $\textbf{OT}_{\epsilon}(\cdot, \alpha)$ doesn't reach its minimum in $\alpha$, which means that the solution of $\min_{\beta} \textbf{OT}_{\epsilon}(\beta, \alpha)$ may actually lay far from $\alpha$. This has been outlined first in \cite{genevay2017learning}, and a solution is to consider instead the so-called "Sinkhorn Divergence"
			\begin{align}
				\mathcal{W}_\epsilon(w_X, X, w_y, Y) :=
					& \; \text{OT}_{\epsilon}(w_X, X, w_y, Y) \label{eq:non_sym}\\
					& - 0.5 \text{OT}_{\epsilon}(w_X, X, w_X, X) \label{eq:sym_1}\\
					& - 0.5 \text{OT}_{\epsilon}(w_Y, Y, w_Y, Y)\label{eq:sym_2}
			\end{align}

			An important point is that the symmetric Optimal transport problems \cref{eq:sym_1,eq:sym_2} can be solved faster than the non-symmetric one, hence the computational burden is controlled by \cref{eq:non_sym}

\section{DIFFERENTIABLE RESAMPLING}
\label{sec:differentiable}
	Given a weighted empirical distribution $(w_i, X_i)_{1 \leq i \leq N} \in \mathcal{M}^+_1(\mathbb{R}^d$ we are interested in finding a "good enough" unweighted sample in the sense that for any $\phi := \mathbb{R}^d \to \mathbb{R}$, $\sum_{1 \leq i \leq N} w_i \phi(X_i) \approx \frac{1}{N}\sum_{1 \leq i \leq N} \phi(X_i)$ with small enough variance \parencite[][]{resampling_comp}. While most approaches consider a bootstrapping of the particle based on the weights: $(w_i, X_i) \mapsto (\frac 1 N, X_{a_i})$ this provides a non-differentiable mapping that prevents exact propagation of the gradient through the resampling step. Because of this and to prevent high variance in the gradient estimation, recent works by \cite{maddison2017filtering,naesseth2017variational,le2017auto} that link SMC and Variational Inference simply ignore the additional impact of resampling and tweak the AutoDiff scheme to propagate the gradient of particles that were not discarded at the resampling step only. As discussed in their papers this provides a biased estimator of the likelihood.

	\subsection{OPTIMAL TRANSPORT MAP RESAMPLING}
	\label{subsec:otResampling}
		To the best of our knowledge the first paper to have introduced Optimal Transport map as an ensemble technique for resampling is \cite{reich2012nonparametric}, and the method has since been applied in \cite{graham2019scalable} and \cite{jacob2016coupling} to provide a local mapping from prior to posterior and to couple same-seed particle filters trajectories in order to compute sensitivities with respect to its hyper-parameters.

		The paradigm introduced in \cite{reich2012nonparametric} can be phrased as follows.
		Let $(w, X)_i$ be a weighted sample before resampling and $C \in \mathbb{R}^d \times \mathbb{R}^d$ be a symmetric positive cost matrix, and lets consider the following Optimal Transport problem:
		\begin{align}
			\mathbf{U} = \text{argmin}_{M \in S_{\mathbf{w}, \mathbf{1}_N}} \sum_{i,j} C_{i,j}, M_{i,j} \label{eq:ot_res}
		\end{align}

		As discussed in	\cite{reich2012nonparametric}, this matrix constitutes a Markov Chain $\mathbf{P} := N \mathbf{U}$ and the mapping $\tilde{X} = \mathbf{P} \mathbf{X}$ provides a consistent estimate of $(w_i, X_i)_i$.

		However the function $(w_i, X_i)_i \to \mathbf{P}\mathbf{X}$ is difficult to compute \parencite[see][]{cuturi2013sinkhorn}.

	\subsection{DIFFERENTIABLE RESAMPLING}
	\label{subsec:regOTResamp}
		Instead of considering the non-regularised problem in \cref{eq:ot_res}, we instead use the mapping resulting from the regularised version of the Optimal Transport problem \cref{eq:plan} \parencite{cuturi2013sinkhorn,feydy:interpolating} $(\mathbf{w}, \mathbf{X}) \to \mathbf{P}_\epsilon \mathbf{X}$.

		\begin{proposition}
			\label{prop:differentiability}
			The mapping $$(\mathbf{w}, \mathbf{X}) \to \mathbf{P}_\epsilon = \exp \left( \frac 1 \epsilon \left( \mathbf{f}^T + \mathbf{g} - \mathbf{C}(\mathbf{X},\mathbf{X}) \right)  \right) \cdot \mathbf{w}^T$$ where $\mathbf{f}$ and $\mathbf{g}$ are given by \cref{eq:fixed_1,eq:fixed_2} is differentiable, with:
			\begin{align}
				\forall i, j &\nonumber\\
				\frac{\partial f_i}{\partial \cdot} &= \frac{\partial}{\partial \cdot}-\epsilon \text{LSE}_k(\log w^Y_k + \frac 1 \epsilon g_k - \frac 1 \epsilon C(X_i, Y_k)) \label{eq:fixed_1_deriv}\\
				\frac{\partial g_j}{\partial \cdot} &= \frac{\partial}{\partial \cdot}-\epsilon \text{LSE}_k(\log w^X_k + \frac 1 \epsilon f_k - \frac 1 \epsilon C(X_k, Y_j)) \label{eq:fixed_2_deriv}
			\end{align}
		\end{proposition}

		\begin{proof}
			The system of equations \cref{eq:fixed_1,eq:fixed_2} defines an system of implicit functions to which we can apply the implicit function theorem. In this case the application of it is trivial as the relationship is linear.
		\end{proof}

		In practice this means that the gradients can be propagated by automatic differentiation at the last step of the Sinkhorn iterates only provided that the algorithm has converged.

		When using automatic differentiation, this can be summarised as:
		\begin{algorithm}
			\caption{Biased resampling}
			\label{algo:biasedResampling}
			\begin{algorithmic}
				\STATE{\bfseries Input:} $X_i$, $w_i$, $N$, $n_{\text{steps}}$
				\STATE Stop registering gradients
				\STATE{\bfseries Initialise:} $\mathbf{f}$, $\mathbf{g}$
				\STATE
					\FOR{$i=1$ {\bfseries to} $n_{\text{steps}}-1$}
						\STATE evaluate \cref{eq:fixed_1} and \cref{eq:fixed_2} simultaneously
					\ENDFOR
				\STATE Register gradients
				\STATE Set gradients of $\mathbf{f}$, $\mathbf{g}$ to $0$
				\STATE Evaluate \cref{eq:fixed_1}, \cref{eq:fixed_2}
				\OUTPUT $\frac 1 N \mathbbm{1}_N$, $\mathbf{P} \mathbf{X}$ with $P$ given by \cref{eq:plan}
			\end{algorithmic}
		\end{algorithm}

		\subsubsection{Illustration}
			To illustrate the behaviour of the resampling scheme we consider a bimodal 2D distribution of 500 constructed as follows: 500 points $X_i \in \mathbb{R}^2$ are drawn uniformly within a circle of radius 1, then half the sample is randomly set to have weights proportional to $\mathcal{N}\left(\left(\begin{matrix}-0.5 \\ 0.5\end{matrix}\right), \left(\begin{matrix}0.3 & 0.\\ 0. & 0.3 \end{matrix}\right)\right)$ and the other half $\mathcal{N}\left(\begin{matrix}0.5 \\ -0.5\end{matrix}, \begin{matrix}0.1 & 0.\\ 0. & 0.1 \end{matrix}\right)$, see \cref{fig:BiasedTransport}. This corresponds tp a multimodal distribution $X |\, ||X||^2_2 \leq 1$ where $X\sim \mathcal{N}\left(\left(\begin{matrix}-0.5 \\ 0.5\end{matrix}\right), \left(\begin{matrix}0.3 & 0.\\ 0. & 0.3 \end{matrix}\right)\right) + \mathcal{N}\left(\left(\begin{matrix}0.5 \\ -0.5\end{matrix}\right), \left(\begin{matrix}0.1 & 0.\\ 0. & 0.1 \end{matrix}\right)\right)$

			\begin{figure}
				\centering
				\captionsetup{justification=centering}
				\includegraphics[width=\linewidth]{BiasedTransport}
				\caption{Biased transport comparison}
				\label{fig:BiasedTransport}
			\end{figure}

			\cref{fig:BiasedTransport} illustrates a well-known problem of the regularised Sinkhorn algorithm: as the regularisation increases, the resulting transporting plan will converge to the one that minimizes $KL(\pi||\alpha\otimes\beta)$, in this case $\mathbf{w_X} \otimes \mathbf{\mathbbm{1}_N}$, hence the resulting resampling of $\mathbf{X}$: $\mathbf{X_\epsilon} = \mathbf{P_\epsilon} \mathbf{X}$ collapses to the weighted mean of the sample $\mathbf{X}$.

		\subsubsection{Application To A State Space Model}
			We now consider the following noisy resonator:

			\begin{align}
				x_\text{true}(t) = \sin(t) + \frac 1 2 \mathcal{N}(0, 1), t = 0., 0.1, ..., 20
			\end{align}

			That we model by the following 2D state space model

			\begin{align}
				\label{eq:ssm}
				x_0(t + dt) &= x_0(t) + x_1(t) dt + N(0, \sigma_0^2)\\
				x_1(t + dt) &= x_1(t) - x_0(t) dt + N(0, \sigma_1^2)\\
				y(t) &= x_0(t) + N(0, \sigma_y^2)
			\end{align}

			\begin{figure}
				\centering
				\captionsetup{justification=centering}
				\includegraphics[width=\linewidth]{KF_OptimalTransportPF_comp}
				\caption{Filtering applied to \cref{eq:ssm}
				Top: Kalman Filter, Second and Third: Biased Transport with $\epsilon=0.25,1$, Bottom: Systematic Resampling }
				\label{fig:kf_illustration}
			\end{figure}

			\cref{fig:kf_illustration} compares \cref{algo:biasedResampling} for different regularisations with \cref{algo:resampling} with systematic resampling \parencite{resampling_comp}, all filters have 100 particles. The shaded zone corresponds to $\pm 2 \text{standard deviations}$

			The collapsing phenomenon in the sample due to the regularisation as highlighted in \cref{fig:BiasedTransport} is visible in \cref{fig:kf_illustration}: each resampling step results in a decrease in variance of the sample.

		\subsubsection{Likelihood evaluation}
			Together with the resampling in \cref{algo:resampling} the formula coming from \cref{algo:bootstrap} provides an unbiased estimate of the likelihood for the state space model associated and also comes with a central limit theorem \cite{chopin2004central}. However, the resulting function $\hat{\mathcal{L}}(\mathbf{y}|\mathbf{\theta})$ is not continuous and as a consequence not differentiable with respect to $\mathbf{\theta}$. On the other hand using \cref{algo:biasedResampling} provides a theoretically everywhere differentiable scheme for "recentering" the particles, albeit to the cost of biasedness in the resulting estimate.

			\begin{figure*}
				\centering
				\captionsetup{justification=centering}
				\includegraphics[width=\textwidth]{likelihood}
				\caption{Likelihood estimate w.r.t. the observation log-error, 400 points}
				\label{fig:likelihood}
			\end{figure*}

			\begin{figure*}
				\centering
				\captionsetup{justification=centering}
				\includegraphics[width=\textwidth]{likelihoodGradient}
				\caption{Gradient of the likelihood estimate w.r.t. the observation log-error}
				\label{fig:likelihoodGradient}
			\end{figure*}

\section{OPTIMAL RESAMPLING}
\label{sec:optimal}

	While the scheme in \cref{sec:differentiable} has the advantage of providing a gradient for the resampling schemes, it suffers from the inconvenient of providing a collapsed estimate of the state post resampling, which in turns results in a biased estimate of the likelihood. Instead of learning a biased linear mapping, we can instead learn the best unweighted point cloud minimising a distance from the weighted degenerate sample. To this end we consider the Sinkhorn divergence (see \cref{subsubsec:Div})

	\subsection{LEARNT POINTS CLOUD}
		As in \cite{genevay2017learning} we consider the optimisation problem given by the Wasserstein divergence $\mathcal{W}_\epsilon = 2 OT_{\epsilon}(\alpha,\beta) - OT_{\epsilon}(\alpha,\alpha) - OT_{\epsilon}(\beta,\beta)$, where in our case $\alpha$ is the weighted degenerate sample $(w_i, X_i)_i$ and $\beta$ is the target unweighted sample $(\frac 1 N, Z_i)_i$: this constitutes a gradient descent on $Z$ with respect to the loss given by the Wasserstein divergence.

		The resampling algorithm is therefore modified as in \cref{algo:optimalResampling}

		\begin{algorithm}
			\caption{Optimal resampling}
			\label{algo:optimalResampling}
			\begin{algorithmic}
				\STATE{\bfseries Input:} $X_i$, $w_i$, $N$, $n_{\text{steps}}$, tolerance, $\lambda$ \COMMENT{Learning rate}
				\STATE Stop registering gradients
				\STATE{\bfseries Initialise:} $\mathbf{f}$, $\mathbf{g}$
				\STATE $Z \leftarrow X$
				\FOR{$i=1$ {\bfseries to} $n_{\text{steps}}$}
					\IF{$\mathcal{W}_\epsilon(w,X,\frac 1 N, Z)<\text{tolerance}$}
						\STATE Break
					\ENDIF
					\STATE $Z \leftarrow Z - \lambda \nabla_Z \mathcal{W}_\epsilon$
				\ENDFOR
				\OUTPUT $\frac 1 N \mathbbm{1}_N$, $Z$
			\end{algorithmic}
		\end{algorithm}

		\begin{figure*}
			\centering
			\captionsetup{justification=centering}
			\includegraphics[width=\textwidth]{LearntReweighting}
			\caption{Gradient Flow for learning the unweighted sample\\
				Top left: original sample, right and bottom: evolution of the gradient descent}
			\label{fig:LearntReweighting}
		\end{figure*}

		The behaviour of \cref{algo:optimalResampling} is shown in \cref{fig:LearntReweighting}

\section{CONCLUSION AND FUTURE WORKS}
\label{sec:conclusion}

\subsubsection*{References}

\printbibliography[heading=none]

\end{document}


\section{FE cantonmathguy Seclected Problems}

\begin{enumerate}

    \item Determine all functions $f: \mathbb{Q} \to \mathbb{Q}$ satisfying $f(x + y) = f(x) + f(y)$ for all $x, y \in \mathbb{Q}$.

    \item Let $a_1, a_2, \ldots$ be a sequence of integers with infinitely many positive and negative terms. Suppose that for every positive integer $n$ the numbers $a_1, a_2, \ldots, a_n$ leave $n$ different remainders upon division by $n$. Prove that every integer occurs exactly once in the sequence $a_1, a_2, \ldots$.

    \item Determine all functions $f: \mathbb{R} \to \mathbb{R}$ satisfying \[f(x)f(y) = f(x + y) + xy\]for all real $x$ and $y$.

    \item Find all functions $f: \mathbb{Z} \rightarrow \mathbb{Z}$ such that, for all integers $a$, $b$, $c$ that satisfy $a + b + c = 0$, the following equality holds:
    \[f(a)^2 + f(b)^2 + f(c)^2 = 2f(a)f(b) + 2f(b)f(c) + 2f(c)f(a).\]

    \item Determine all functions $f: \mathbb{R} \to \mathbb{R}$ satisfying
    \[f(x) + f(y) = f(x + y) \quad \text{and} \quad f(xy) = f(x)f(y)\]for all $x, y \in \mathbb{R}$.

    \item Find all functions $f: \mathbb{R} \rightarrow \mathbb{R}$ such that for all $x,y \in \mathbb{R}$, the following equality holds
    \[f(\left\lfloor x\right\rfloor y)=f(x)\left\lfloor f(y)\right\rfloor\]where $\left\lfloor a\right\rfloor$ is the greatest integer not greater than $a$.

    \item $ \bigstar $ Let $k$ be a real number. Find all functions $f: \mathbb{R} \to \mathbb{R}$ satisfying
    \[|f(x) - f(y)| \le k(x - y)^2\]for all real $x$ and $y$.

    \item Let $f: \mathbb{N} \to \mathbb{N}$ be a function, and suppose that positive integers $k$ and $c$ satisfy
    \[f^k(n) = n + c\]for all $n \in \mathbb{N}$, where $f^k$ denotes $f$ applied $k$ times. Show that $k \mid c$.

    \item Find all functions $f: \mathbb{N} \to \mathbb{N}$ satisfying
    \[f(f(f(n))) + f(f(n)) + f(n) = 3n\]for every positive integer $n$.

    \item Let $S$ be the set of integers greater than $1$. Find all functions $f: S \to S$ such that
    (i) $f(n) \mid n$ for all $n \in S$,
    (ii) $f(a) \ge f(b)$ for all $a, b \in S$ with $a \mid b$.

    \item Let $\mathbb{R}$ be the set of real numbers. Determine all functions $f: \mathbb{R} \to \mathbb{R}$ such that \[f(x^2 - y^2) = x f(x) - y f(y)\]for all pairs of real numbers $x$ and $y$.

    \item $ \bigstar $ Let $T$ denote the set of all ordered triples $(p,q,r)$ of nonnegative integers. Find all functions $f: T \to \mathbb{R}$ satisfying
    \[f(p,q,r) = \begin{cases} 0 & \text{if} \; pqr = 0, \\ 1 + \frac{1}{6}(f(p + 1,q - 1,r) + f(p - 1,q + 1,r) & \\ + f(p - 1,q,r + 1) + f(p + 1,q,r - 1) & \\ + f(p,q + 1,r - 1) + f(p,q - 1,r + 1)) & \text{otherwise} \end{cases} \]for all nonnegative integers $p$, $q$, $r$.

    \item Determine all strictly increasing functions $f: \mathbb{N} \to \mathbb{N}$ satisfying $nf(f(n)) = f(n)^2$ for all positive integers $n$.

    \item Determine all functions $f: \mathbb{Z} \rightarrow \mathbb{Z}$ with the property that
    \[f(x - f(y)) = f(f(x)) - f(y) - 1\]holds for all $x,y \in \mathbb{Z}$.

    \item Find all real-valued functions $f$ defined on pairs of real numbers, having the following property: for all real numbers $a, b, c$, the median of $f(a,b), f(b,c), f(c,a)$ equals the median of $a, b, c$.

    \item Find all functions $f: \mathbb{N} \rightarrow \mathbb{N}$ such that, for all positive integer $n$, we have $f(f(n)) < f(n + 1)$.

    \item Let $f: \mathbb{N} \to \mathbb{N}$ be a function such that, for any $w, x, y, z \in N$,
    \[f(f(f(z))) f(wxf(yf(z))) = z^2 f(xf(y)) f(w).\]Show that $f(n!) \ge n!$ for every positive integer $n$.

    \item Find all functions $f: \mathbb{N} \rightarrow \mathbb{N}$ such that $f(n!) = f(n)!$ for all positive integers $n$ and such that $m-n$ divides $f(m) - f(n)$ for all distinct positive integers $m$, $n$.

    \item Find all functions $f$ from the reals to the reals such that
    \[(f(a) + f(b))(f(c) + f(d)) = f(ac + bd) + f(ad - bc)\]for all real $a, b, c, d$.

    \item Determine all functions $f$ defined on the natural numbers that take values among the natural numbers for which
    \[(f(n))^p \equiv n \pmod{f(p)}\]for all $n \in \mathbb{N}$ and all prime numbers $p$.

    \item Let $n \ge 4$ be an integer, and define $[n] = \{1, 2, \ldots, n\}$. Find all functions $W: [n]^2 \to \mathbb{R}$ such that for every partition $[n] = A \cup B \cup C$ into disjoint sets,
    \[\sum_{a \in A} \sum_{b \in B} \sum_{c \in C} W(a,b) W(b,c) = |A| |B| |C|.\]

    \item $ \bigstar $ Find all infinite sequences $a_1, a_2, \ldots$ of positive integers satisfying the following properties:
    (a) $a_1 < a_2 < a_3 < \cdots$,
    (b) there are no positive integers $i$, $j$, $k$, not necessarily distinct, such that $a_i + a_j = a_k$,
    (c) there are infinitely many $k$ such that $a_k = 2k - 1$.

    \item Show that there exists a bijective function $f:\mathbb N_0\to\mathbb N_0$ such that for all $m,n\in\mathbb{N}_{0}$, $$f(3mn + m + n) = 4f(m)f(n) + f(m) + f(n)$$

    \item Determine all functions $f: \mathbb Z \to \mathbb Z$ satisfying $$f(f(m) + n) + f(m) = f(n) + f(3m) + 2014$$ for all integers $m$ and $n$.

    \item Let $n \ge 3$ be a given positive integer. We wish to label each side and each diagonal of a regular $n$-gon $P_1 \ldots P_n$ with a positive integer less than or equal to $r$ so that:

\begin{enumerate}

        \item every integer between $1$ and $r$ occurs as a label;

        \item in each triangle $P_iP_jP_k$ two of the labels are equal and greater than the third.

\end{enumerate}

    Given these conditions:

\begin{enumerate}

        \item Determine the largest positive integer $r$ for which this can be done.

        \item For that value of $r$, how many such labellings are there?

\end{enumerate}

    \item $ \bigstar $ Suppose that $f$ and $g$ are two functions defined on the set of positive integers and taking positive integer values. Suppose also that the equations $f(g(n)) = f(n) + 1$ and $g(f(n)) = g(n) + 1$ hold for all positive integer $n$. Prove that $f(n) = g(n)$ for all positive integer $n$.

    \item Find all the functions $f: \mathbb N_0 \to \mathbb N_0$ satisfying the relation

    \item Let $\mathbb{R}$ be the set of real numbers. Determine all functions $f: \mathbb{R} \to \mathbb{R}$ that satisfy the equation
    \[f(x + f(x + y)) + f(xy) = x + f(x + y) + yf(x)\]for all real numbers $x$ and $y$.

    \item Suppose that $s_1, s_2, s_3, \ldots$ is a strictly increasing sequence of positive integers such that the sub-sequences
    \[s_{s_1}, s_{s_2}, s_{s_3}, \ldots \qquad\text{and}\qquad s_{s_1+1}, s_{s_2+1}, s_{s_3+1}, \ldots\]are both arithmetic progressions. Prove that the sequence $s_1, s_2, s_3, \ldots$ is itself an arithmetic progression.

    \item Find all functions $f$ from $\mathbb{N}_0$ to itself such that
    \[f(m + f(n)) = f(f(m)) + f(n)\]for all $m, n \in \mathbb{N}_0$.

    \item $ \bigstar $ Consider a function $f: \mathbb{N} \to \mathbb{N}$. For any $m, n \in \mathbb{N}$ we write $f^n(m) = \underbrace{f(f(\ldots f}_{n}(m)\ldots))$. Suppose that $f$ has the following two properties:

\begin{enumerate}

       \item if $m, n \in \mathbb{N}$, then $\frac{f^n(m) - m}{n} \in \mathbb{N}$;

        \item The set $\mathbb{N} \setminus \{f(n) \mid n\in \mathbb{N}\}$ is finite.

\end{enumerate}

    Prove that the sequence $f(1) - 1, f(2) - 2, f(3) - 3, \ldots$ is periodic.

    \item Let $\mathbb{N}$ be the set of positive integers. Find all functions $f: \mathbb{N} \to \mathbb{N}$ that satisfy the equation
    \[f^{abc-a}(abc) + f^{abc-b}(abc) + f^{abc-c}(abc) = a + b + c\]for all $a,b,c \ge 2$.

    \item Let $2\mathbb{Z} + 1$ denote the set of odd integers. Find all functions $f:\mathbb{Z} \to 2\mathbb{Z} + 1$ satisfying
    \[f(x + f(x) + y) + f(x - f(x) - y) = f(x + y) + f(x - y)\]for every $x, y \in \mathbb{Z}$.

\end{enumerate}


%&LaTeX

\section{Joe Fourier Was Not a Discrete Fellow}

\subsection{Lab Background}
By the end of this lab you should have a firm understanding of how the
Discrete Fourier Transform (DFT) can be implemented exactly using the
Fast Fourier Transform (FFT). In addition you should be able to
identify common problems using the DFT to analyze signals. You will
also be familiar with a new tool, the spectrogram, that uses the DFT
as a function of time.

\subsection{Implementing the DFT}

Recall that the DFT can be implemented directly from the analysis
equation. For a length $N$ signal $x[n]$,
\begin{align}
X[k]=\sum_{n=0}^{N-1}x[n]e^{-j \frac{2\pi}{N} nk} && \text{for $k = 0, 1, 2, \cdots N-1$}
\label{eq:dft}
\end{align}
The order of the implementation is $O(N)=N^2$. The following Java code
outlines implementation of a 256-point DFT. It is written without any
algorithmic speedup (i.e., it exactly mirrors equation \ref{eq:dft}).
\begin{lstlisting}[language=Java,basicstyle=\mlttfamily\small]
public class MyDFT
{
  // x is the input and y is the magnitude of the complex DFT
 public void computeDFT(double[] x, double[] y)
  {
  double[] yImag = new double[256];
  double[] yReal = new double[256];

  double twoPiOverN = 2*Math.PI/256;

  for (int k = 0 ; k < 256 ; k++)
  {
    yReal[k] = 0;
    yImag[k] = 0;
    for (int n = 0 ; n < 256 ; n++)
    {
      yReal[k] += x[n]*Math.cos(n*k*twoPiOverN);
      yImag[k] += -x[n]*Math.sin(n*k*twoPiOverN);
    }
    y[k] = Math.sqrt(yReal[k]*yReal[k] + yImag[k]*yImag[k]);
  }
 }
}
\end{lstlisting}

Note that, unlike in Matlab, there is no native support in Java for
complex numbers so this arithmetic is written out explicitly in the
code above. For example, the equation $y=x\times e^{a}$ (where x is a
real number) must be explicitly written out using Euler's formula, and
the real and imaginary portions saved in separate variables,
$y_{real}=x\times \cos(a)$ and $y_{imag}=x\times \sin(a)$.

The FFT algorithm can be used to reduce the computation time of the
DFT to $O(N)=N\log_2 N$ --- a significant speedup for even modest
length signals.

\subsection{The FFT in Matlab}

Matlab includes a \verb|fft| function (there are many more related
operations in the Signal Processing Toolbox, but we are sticking to
``vanilla'' Matlab here). You can look at the documentation for this
function; pay especial attention to the frequency values that
correspond to each element of the vector that this function returns,
and to how to specify the number of points in the FFT it computes.

\paragraph{Step 1.1} Implement your own \verb|myFFT| function in
Matlab that takes a real-valued vector as input, computes a 256-point
FFT, and returns a real-valued vector that is the magnitude of the
(single-sided, i.e., only positive frequencies) FFT. Implement this
function using loops (i.e., do not use recursion). The first part of
this code performs a bit reversal on the input array. You can use the
following code to perform the bit reversal efficiently (efficient bit
reversal algorithms in other languages are typically more complex):
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily\small]
% Assume that you want to do a bit reversal of the contents of the vector x
indices = [0 : length(x)-1];                 % binary indices need to start at zero
revIndices = bin2dec(fliplr(dec2bin(indices, 8))); % bit reversed indices
revX = x(revIndices+1);                      % Add 1 to get Matlab indices
\end{lstlisting}
This code converts the vector of in-order indices to an array of
8-character strings (representing those indices as binary 8-bit
numbers). Each string is then reversed, and converted back to
numbers. The resultant vector of indices (which is what they are after
we add one to each) is applied to the signal to pull its entries out
into a new vector, with the order of the entries in bit-reversed
order.

Once you've done this, all you need to do is iterate over the array
$\log_2 N$ times! Remember that you're performing complex arithmetic
and to compute the magnitude of the output array once the FFT is
computed. Include a copy of your Matlab code in your report.

\paragraph{Step 1.2}
Check your results using the Matlab \verb|fft| function. Your results
should be quite similar, if not identical; this should be apparent by
comparing graphs of the outputs. Take the FFT of a sinusoid with a
frequency of $\pi/4$ radians per second using your FFT implementation
and the \verb|fft| function.

\paragraph{Step 1.3}
Prove that the number of complex multiplies performed by your code is
$O(N \log N)$.

\subsection{Using the DFT}

\paragraph{Step 2.1}
Create a sum of two sinusoids. Use the built-in Matlab \verb|fft|
function (it will allow you, among other things, to apply an $n$-point
FFT to a signal with more than $n$ samples) to compute the FFT of the
sum and then plot the FFT magnitude. Use frequencies of $0.13\pi$ and
$0.19\pi$ for the two sinusoids. Make sure that there are at least 256
samples in each waveform, as you'll want to use a 256-point FFT! Also,
make sure you correctly compute the corresponding frequency values
(either from 0 to $\pi$ or $-\pi$ to $\pi$, depending on whether you
prefer plotting the single-sided magnitudes or not) for the FFT
X-axis, and label it appropriate in your graph. What does the result
look like? Does this make sense?

\paragraph{Step 2.2} At what index (or indices) does the FFT magnitude
reach its peak value(s)? What frequency (or frequencies) does this
correspond to?

\paragraph{Step 2.3} Change the frequencies of the sinusoids to
$0.13\pi$ and $0.14\pi$. Repeat steps 2.1 and 2.2. Do the results
still make sense?

\paragraph{Step 2.4} Replace the sum of two sinusoids with your
\verb|DTMFCoder| function from lab 6. Input a selection of button
values. Do the FFT function and plot show the separate frequencies for each
button?

\subsection{Spectrograms}

Comparing FFT graphs (as in the step 2.4 above) can be difficult to
do. But what if we could analyze the frequency content of a signal as
a function of time? That would make it easier to see differences in
frequency if a signal started changing (like a string of DTMF keys
pressed in turn). To do this we will need a new tool called the
\emph{Spectrogram}. The spectrogram is simply an algorithm for
computing the FFT of a signal at different times and plotting them as
a function of time. The spectrogram is computed in the following way:

\begin{enumerate}
\item A given signal is ``windowed.'' This means that we only take a
  certain number of points from the signal (for this example assume we
  are using a window of length 128 points). To start out, we take the
  first 128 points of the signal (points 1 through 128 of the input
  vector).
\item Take the FFT of the window and save the it in a separate array.
\item Advance the window in time by a certain number of points. For
  instance we can advance the window by 64 points so that we now have
  a window of indices 65 through 192 from the input signal array.
\item Repeat steps 1--3, saving the FFT of each window, until there
  are no longer any points in the input array.
\item Form a 2-D matrix whose columns are the FFT magnitudes of each
  window (placed in chronological order). In this way, each row
  represents a certain frequency, each column represents a given
  instant in time, and the value of the matrix represents the
  magnitude of the FFT.
\end{enumerate}

The result is called a \emph{spectrogram} and is usually displayed as
an RGB image where blue represents small FFT magnitudes and red
represents larger FFT magnitudes (the \emph{Jet} colormap if you are
familiar with color visualizations). There is an art to choosing the
correct parameters of the spectrogram (i.e., window size, FFT size,
how many points to advance the FFT, etc.). Each parameter has
tradeoffs for the time and frequency resolution of the resulting
spectrogram. For our purposes here, we will not be concerned with
these tradeoffs. Instead we will be more interested in getting
familiar with analysis using spectrograms.

The Matlab Signal Processing Toolbox has a \verb|spectrogram|
function, but you can retrieve a drop-in replacement for it from
\url{http://www.ee.columbia.edu/ln/rosa/matlab/sgram/} that will work
without that toolbox. See the online Matlab documentation for the
\verb|spectrogram| function to understand what the parameters to that
function are (though, for our purposes, you should just be able to use
the call \verb|y = myspecgram(x)|.

\paragraph{Step 3.1} Use your \verb|DTMFCoder| function again. Instead
of computing its FFT, compute its spectrogram instead. What does the
spectrogram look like for button 1? Are both frequencies present?

\paragraph{Step 3.2} For this step, use \verb|DTMFCoder| to generate
the codes for multiple buttons --- at least three --- and concatenate
them together to form a single vector. Does the spectrogram make it
easier to judge the frequency content of the keys? Can you clearly see
when the signal changes from one key to another?

% LocalWords:  WebQ MATLAB DSP


\documentclass{article} % For LaTeX2e
\usepackage[legalpaper, margin=0.5in]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts,dsfont}
\usepackage{amssymb}
\usepackage[ruled,vlined]{algorithm2e}

\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{xcolor}

\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator*{\argmax}{arg\,max}

\begin{document}

\section{Compact Description For Anomalies}

Providing explanations for anomalies is important and some recent work (among others) can be found in \cite{macha:2017}. We illustrate a different and yet very simple approach when AAD is used with \textit{Isolation Forest}.

The Isolation Forest version of AAD works by partitioning the space into smaller subspaces and weights each subspace to fit the expert feedback. The end result is: (1) a set of default (unsupervised) anomaly scores for each subspace and, (2) weights for each subspace inferred from feedback. We can generate compact descriptions for the anomalies using this information. The idea is to select some combination of (preferably) the smallest subspaces that \textbf{cover} all \textbf{discovered} anomalies. Therefore, we treat this as an instance of the \emph{set covering} problem. We illustrate this on a toy dataset below.

Assume that there are total $m$ subspaces across all Isolation Forest trees. We represent weights by ${\bf w} \in \mathbb{R}^m$, and the corresponding unsupervised anomaly scores by ${\bf d} \in \mathbb{R}^m$.

Now, the final anomaly scores for the \textbf{subspaces} (not instances), after incorporating feedback, is ${\bf a} = {\bf w} \circ {\bf d} \in \mathbb{R}^m$ where $\circ$ denotes the element wise (\textit{Hadamard}) product.

We sort the scores in ${\bf a}$ in descending order and select (say) $30$ top ranked subspaces. Next, we retain only those subspaces from this set which contain at least one anomaly \textbf{discovered} by the analyst. Let us denote the resulting set of subspaces by $\mathcal{S}$. Further, let $\mathcal{Z}$ be the set of discovered anomalies that belong to one or more subspaces in $\mathcal{S}$, and $|\mathcal{Z}|=n$. (Note that some of the discovered anomalies might not belong to any of the top ranked subspaces we selected.)

Let $|\mathcal{S}|=k$. Denote the \textit{volumes} of the subspaces in $\mathcal{S}$ by the vector ${\bf v} \in \mathbb{R}^k$. Now, assume that a binary vector ${\bf x} \in \{0, 1\}^k$ contains $1$ in locations corresponding to the subspaces in $\mathcal{S}$ which are included in the covering set, and $0$ otherwise. Let ${\bf u}_z \in \{0, 1\}^k$ denote a vector for each anomaly $z \in \mathcal{Z}$ which contains $1$ in all locations corresponding to the subspaces in $\mathcal{S}$ that $z$ belongs to. Let ${\bf U} \in \{0, 1\}^{[n \times k]}$ represent the matrix of all ${\bf u}$s.

The selection of the compact set of subspaces to describe all discovered anomalies can be formulated as:
\begin{align}
& \argmin_{{\bf x} \in \{0, 1\}^k} {\bf x} \cdot {\bf v}^p \label{eqn:opt} \\
\text{s.t.} & {\bf U} \cdot {\bf x} \geq {\bf 1} \nonumber \\
\text{where, } & \text{${\bf 1}$ is the column vector of $n$ 1s, and} \nonumber \\
& \text{$p$ is an integer $\geq 1$ (more below)} \nonumber
\end{align}

The parameter $p$ determines how severely to penalize larger volumes. This is usually $1$. However, a larger value will strongly discourage bigger volume subspaces from being selected.

We apply this idea on a toy data shown in Figure~\ref{fig:dataset}. The compact descriptions are shown in Figure~\ref{fig:compact_rects}.

Note that we have not considered labeled nominals in the optimization objective, but a more sophisticated objective might include them too. For instance, we could add another term in the objective which penalizes whenever a nominal is present in the selected subspace.

A different approach to generate descriptions might train a cost-based decision tree with the labeled anomalies and nominals. Once trained, the extracted rules would correspond to `descriptions'. This approach is possibly more principled because the decision tree splits are [usually] based on information gain or gini-index and are therefore more informative. The downside is that this would be separate from the anomaly detector and therefore it would not offer much insight into the anomaly detector's behavior -- ideally we would like the explanation or description of anomalies to reflect the detector's internals.

\begin{figure}
	\centering
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{toy2_dataset}
		\caption{Dataset}
		\label{fig:dataset}
	\end{subfigure}
	~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
	%(or a blank line to force the subfigure onto a new line)
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{toy2_iter_00}
		\caption{Initial score contours}
		\label{fig:baseline_contours}
	\end{subfigure}
	~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
	%(or a blank line to force the subfigure onto a new line)
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{toy2_iter_34}
		\caption{After 35 feedback}
		\label{fig:contours_35}
	\end{subfigure}
	\caption{Dataset and score contours. Figure~\ref{fig:dataset} shows a synthetic toy dataset. Figure~\ref{fig:baseline_contours} shows the initial Isolation Forest score contours. Figure~\ref{fig:contours_35} shows the score contours after 35 feedback iterations from the Oracle. The red dots are discovered anomalies (true positives). The green dots are discovered nominals (false positives). The red checks are undiscovered anomalies (false negatives).} \label{fig:dataset_and_contours}
\end{figure}

\begin{figure}
	\centering
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{top_30_anomalous_regions_100_trees_baseline}
		\caption{Baseline}
		\label{fig:baseline_rects}
	\end{subfigure}
	~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
	%(or a blank line to force the subfigure onto a new line)
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{top_30_anomalous_regions_100_trees_aad}
		\caption{AAD}
		\label{fig:aad_rects}
	\end{subfigure}
	~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
	%(or a blank line to force the subfigure onto a new line)
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{top_30_anomalous_regions_100_trees_compact}
		\caption{Compact Descriptions}
		\label{fig:compact_rects}
	\end{subfigure}
	\caption{Top $30$ subspaces ranked by ${\bf w}\circ{\bf d}$. Figure~\ref{fig:baseline_rects} shows the top $30$ most important subspaces (w.r.t their \textit{anomalousness}) without any feedback. We can see that initially, these simply correspond to the exterior regions of the dataset. AAD \textbf{learns the true importance of subspaces} automatically with feedback. For example, after incorporating the labels of $35$ instances, the subspaces around the labeled anomalies have emerged as the most important (Figure~\ref{fig:aad_rects}). Figure~\ref{fig:compact_rects} shows the set of \textbf{important} subspaces which compactly cover all labeled anomalies. These were computed with Equation~\ref{eqn:opt}. We might even think of this as a non-parametric clustering. Note that the compact subspaces only cover anomalies that were discovered in the $35$ feedback iterations. Anomalies which were not detected are likely to fall outside these compact subspaces.} \label{fig:rects}
\end{figure}

\section{Querying Diversity}
We can use the anomaly description to diversify our queries when we have the option to query labels for more than one instance per round of feedback. Following is how we go about it:
\begin{enumerate}
	\item Select the top ranked $m$ (say, $m=15$) instances. Denote this set by $\mathcal{C}$ (points in \textcolor{blue}{blue} in Figure~\ref{fig:candidate_regions}).
	\item Next, select the top (say) $5$ most anomalous regions for each instance in $\mathcal{C}$. Let $\mathcal{F}$ be the union of all these regions (rectangles in \textcolor{red}{red} in Figure~\ref{fig:candidate_regions}).
	\item Now, \emph{compactly} describe all instances in $\mathcal{C}$ using the regions in $\mathcal{F}$ (rectangles in \textcolor{red}{red} in Figure~\ref{fig:baseline_queries}).
	\item Now, start with the most anomalous instance, and select one-by-one $k$ (say, $k=5$) instances which have the fewest overlapping regions for querying (points circled in \textcolor{green}{green} in Figures \ref{fig:baseline_queries} and \ref{fig:diverse_queries}).
\end{enumerate}

\begin{figure}
	\centering
	\begin{subfigure}[b]{0.48\textwidth}
		\includegraphics[width=\textwidth]{query_candidate_regions_ntop5_100_trees}
		\caption{Candidate regions and instances}
		\label{fig:candidate_regions}
	\end{subfigure}
	%~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
	%(or a blank line to force the subfigure onto a new line)
	\begin{subfigure}[b]{0.48\textwidth}
		\includegraphics[width=\textwidth]{query_compact_ntop5_100_trees_baseline}
		\caption{Simple select-top query}
		\label{fig:baseline_queries}
	\end{subfigure} \\
    %~
	\begin{subfigure}[b]{0.48\textwidth}
		\includegraphics[width=\textwidth]{query_compact_ntop5_100_trees_aad}
		\caption{Select diverse query}
		\label{fig:diverse_queries}
	\end{subfigure}
	\caption{Diverse querying strategy using sub-space descriptions. These figures show the $15$ most anomalous instances (ranked by score) in \textcolor{blue}{blue}. We assume that the true labels are not known for these instances. We find the top $5$ most anomalous regions for each of the $15$ instances and the union of all these regions is shown as the \textcolor{red}{red} rectangles in Figure~\ref{fig:candidate_regions}. Figure~\ref{fig:baseline_queries} and Figure~\ref{fig:diverse_queries} show the regions which compactly contain (describe) the top-ranked instances. The instances circled in \textcolor{green}{green} are the ones selected for querying. The instances (in green) in Figure~\ref{fig:baseline_queries} are merely the top-ranked $5$ instances without taking into account any diversity. Figure~\ref{fig:diverse_queries} shows that when we select instances which have different descriptions (minimum region overlap), they are more diverse.} \label{fig:diverse}
\end{figure}

\subsection{Does the diverse querying strategy help?}
Whatever querying strategy we use, it should ideally not lower the number of anomalies discovered within a budget. For the diverse query strategy, we need to

\begin{figure}
	\centering
	\begin{subfigure}[b]{0.48\textwidth}
		\includegraphics[width=\textwidth]{toy2_processes}
		\caption{Underlying Processes}
		\label{fig:processes}
	\end{subfigure}
	%~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
	%(or a blank line to force the subfigure onto a new line)
	\begin{subfigure}[b]{0.48\textwidth}
		\includegraphics[width=\textwidth]{results_anoms_found_toy2}
		\caption{Anomalies discovered}
		\label{fig:discovery}
	\end{subfigure} \\
	%~
	\begin{subfigure}[b]{0.48\textwidth}
		\includegraphics[width=\textwidth]{results_diff_classes_toy2}
		\caption{Difference in class diversity per batch}
		\label{fig:class_diff}
	\end{subfigure}
	\caption{Diversity in the classes shown to an analyst per batch. Here we assume that the data contains \textbf{three} classes which represent three underlying processes (Figure~\ref{fig:processes}). Class $0$ is \emph{nominal}, while classes $1$ and $2$ are \emph{anomalous}. In Figure~\ref{fig:discovery}, we plot the anomaly discovery curves for five Isolation Forest-based algorithms: (a) \textbf{BAL (Adaptive Prior)} -- label only the single most anomalous instance (per iteration), (b) \textbf{ifor\_q1b3} -- label the top three most anomalous instances, (c) \textbf{BAL-D} -- label the top three \textbf{most diverse} instances among the top ten anomalous instances (employing the diversity strategy), (d) \textbf{BAL-E} -- label the top three instances \textbf{farthest in euclidean space} among the top ten anomalous instances, (e) \textbf{Unsupervised Baseline} -- unsupervised Isolation Forest, and (f) \textbf{ifor\_top\_random} -- label three instances selected uniformly at random from the top ten anomalous instances. We see that all algorithms other than the baseline have similar performance, and all active labeling algorithms perform better than the baseline. In Figure~\ref{fig:class_diff}, the solid line \texttt{(diverse - top)} shows the average difference in the number of unique classes shown to the analyst per batch between \textbf{BAL-D} and \textbf{ifor\_q1b3} (these differences are averaged over $10$ runs). The dashed line \texttt{(diverse - top\_random)} in Figure~\ref{fig:class_diff} shows the average difference in the number of unique classes shown to the analyst per batch between \textbf{BAL-D} and \textbf{ifor\_top\_random}. Since both these differences are mostly positive, we conclude that the diversity strategy indeed presents more diverse instances to the analyst to label. Moreover, as seen in Figure~\ref{fig:discovery}, this diversity does not lower the anomaly detection accuracy over a strictly select-top-anomalous query strategy. \textbf{We also see this pattern in most real-world datasets we experimented with.} The dash-dot line \texttt{(diverse - euclidean)} shows the difference between \textbf{BAL-D} and \textbf{BAL-E}. \textbf{BAL-E} is a standard way to find diverse instances. The performance of \textbf{BAL-D} and \textbf{BAL-E} are very similar; however, \textbf{BAL-D} is more user-friendly because it provides descriptions which can characterize multiple anomalies.} \label{fig:diverse_effect}
\end{figure}

\section{Comparison between Isolation Forest, HS Trees, RS Forest}
There are fundamental differences between Isolation Forest and the other two: HS Trees and RS Forest. These differences directly influence the effectiveness of incorporating feedback. Since the depths in HS Trees and RS Forest are fixed and they repeatedly split a dimension, there are $O(2^H)$ subspaces represented by the leaf nodes of these detectors and most are very small in volume. We see this property in Figures \ref{fig:hstrees_rects} and \ref{fig:rsforest_rects}. The implication is that more number of subspaces will be required to generate a compact representation for a set of instances. This also means that feedback at instance level is going to be shared by a much fewer set of instances. In contrast, in Isolation forest (Figure~\ref{fig:iforest_rects}), the depths of leaf nodes are not fixed; they are usually \textbf{much} shallower than in HS Trees and  RS Forest. As a result, the subspaces represented by the leaf nodes are larger and cover more number of instances, thereby requiring fewer subspaces to compactly represent a set of instances. Moreover, this results in feedback being shared by more instances in Isolation Forest. One remedy for HS Trees and RS Forest might be to dynamically determine when to stop splitting based on (maybe) the number of samples at a node. This might be a motivation for future research. My personal opinion right now is to stick to Isolation Forest because of its `nicer' properties.

\begin{figure}
	\centering
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{leaf_trees/query_candidate_regions_ntop5_100_trees_iforest}
		\caption{Isolation Forest}
		\label{fig:iforest_rects}
	\end{subfigure}
	~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
	%(or a blank line to force the subfigure onto a new line)
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{leaf_trees/query_candidate_regions_ntop5_50_trees_hstrees}
		\caption{HS Trees}
		\label{fig:hstrees_rects}
	\end{subfigure}
	~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
	%(or a blank line to force the subfigure onto a new line)
	\begin{subfigure}[b]{0.3\textwidth}
		\includegraphics[width=\textwidth]{leaf_trees/query_candidate_regions_ntop5_50_trees_rsforest}
		\caption{RS Forest}
		\label{fig:rsforest_rects}
	\end{subfigure} \\
    \begin{subfigure}[b]{0.3\textwidth}
    	\includegraphics[width=\textwidth]{leaf_trees/query_compact_ntop5_100_trees_aad_iforest}
    	\caption{IForest Compact}
    	\label{fig:iforest_compact_rects}
    \end{subfigure}
    ~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
    %(or a blank line to force the subfigure onto a new line)
    \begin{subfigure}[b]{0.3\textwidth}
    	\includegraphics[width=\textwidth]{leaf_trees/query_compact_ntop5_50_trees_aad_hstrees}
    	\caption{HST Compact}
    	\label{fig:hstrees_compact_rects}
    \end{subfigure}
    ~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
    %(or a blank line to force the subfigure onto a new line)
    \begin{subfigure}[b]{0.3\textwidth}
    	\includegraphics[width=\textwidth]{leaf_trees/query_compact_ntop5_50_trees_aad_rsforest}
    	\caption{RSF Compact}
    	\label{fig:rsforest_compact_rects}
    \end{subfigure}
	\caption{Top subspaces after $35$ feedback iterations ranked by ${\bf w}\circ{\bf d}$ which cover the top ranked $15$ instances. We find the top $5$ most anomalous regions for each of the $15$ instances and the union of all these regions is shown as the \textcolor{red}{red} rectangles (\textbf{top row}). The corresponding compact subspaces which describe the $15$ instances are shown in the \textbf{bottom row}. The points circled in \textcolor{green}{green} (in the bottom row) are the $5$ instances (out of the $15$) selected for query using a diverse querying strategy. The Isolation Forest subspaces tend to be larger and each subspace usually contains more instances. This lets the feedback in Isolation Forest to be shared across more instances than in HS Trees or RS Forest.} \label{fig:tree_diffs}
\end{figure}

\begin{thebibliography}{1}
\bibitem{macha:2017} Meghanath Macha and Leman Akoglu {\em {X-PACS:} eXPlaining Anomalies by Characterizing Subspaces},  2017, http://arxiv.org/abs/1708.05929.
\end{thebibliography}

\end{document}


\documentclass[10pt]{article}
\author{Alex Peyrard}
\title{Information and coding theory assignment}

\begin{document}
\maketitle
\section{Known equations}
Define a probability mass function
\[\mathcal{P}(X=x,Y=y,Z=z)\]
Where \[x\in \mathcal{X} = \{0,1\}, y\in \mathcal{Y} = \{0,1\}, z\in \mathcal{Z} = \{0,1\}\]
by
\[\mathcal{P}(X=0,Y=0,Z=0)=\mathcal{P}(X=1,Y=0,Z=1)=1/4\]
\[\mathcal{P}(X=0,Y=1,Z=1)=\mathcal{P}(X=1,Y=1,Z=0)=1/4\]
\[\mathcal{P}(X=0,Y=1,Z=0)=\mathcal{P}(X=0,Y=0,Z=1)=0\]
\[\mathcal{P}(X=1,Y=0,Z=0)=\mathcal{P}(X=1,Y=1,Z=1)=0\]

then
\[\mathcal{P}(X=0,Z=0)=\mathcal{P}(X=0,Y=0,Z=0)+\mathcal{P}(X=0,Y=1,Z=0)=1/4\]
\[\mathcal{P}(X=0,Z=1)=\mathcal{P}(X=0,Y=0,Z=1)+\mathcal{P}(X=0,Y=1,Z=1)=1/4\]
\[\mathcal{P}(X=1,Z=0)=\mathcal{P}(X=1,Y=0,Z=0)+\mathcal{P}(X=1,Y=1,Z=0)=1/4\]
\[\mathcal{P}(X=1,Z=1)=\mathcal{P}(X=1,Y=0,Z=1)+\mathcal{P}(X=1,Y=1,Z=1)=1/4\]
and
\[\mathcal{P}(X=0,Y=0)=\mathcal{P}(X=0,Y=0,Z=0)+\mathcal{P}(X=0,Y=0,Z=1)=1/4\]
\[\mathcal{P}(X=0,Y=1)=\mathcal{P}(X=0,Y=1,Z=0)+\mathcal{P}(X=0,Y=1,Z=1)=1/4\]
\[\mathcal{P}(X=1,Y=0)=\mathcal{P}(X=1,Y=0,Z=0)+\mathcal{P}(X=1,Y=0,Z=1)=1/4\]
\[\mathcal{P}(X=1,Y=1)=\mathcal{P}(X=1,Y=1,Z=0)+\mathcal{P}(X=1,Y=1,Z=1)=1/4\]
and
\[\mathcal{P}(Y=0,Z=0)=\mathcal{P}(X=0,Y=0,Z=0)+\mathcal{P}(X=1,Y=0,Z=0)=1/4\]
\[\mathcal{P}(Y=0,Z=1)=\mathcal{P}(X=0,Y=0,Z=1)+\mathcal{P}(X=1,Y=0,Z=1)=1/4\]
\[\mathcal{P}(Y=1,Z=0)=\mathcal{P}(X=0,Y=1,Z=0)+\mathcal{P}(X=1,Y=1,Z=0)=1/4\]
\[\mathcal{P}(Y=1,Z=1)=\mathcal{P}(X=0,Y=1,Z=1)+\mathcal{P}(X=1,Y=1,Z=1)=1/4\]

we can also find
\[\mathcal{P}(X=0)=\mathcal{P}(X=0,Z=0)+\mathcal{P}(X=0,Z=1)=1/2\]
\[\mathcal{P}(X=1)=\mathcal{P}(X=1,Y=0)+\mathcal{P}(X=1,Y=1)=1/2\]
\[\mathcal{P}(Y=0)=\mathcal{P}(Y=0,Z=0)+\mathcal{P}(Y=0,Z=1)=1/2\]
\[\mathcal{P}(Y=1)=\mathcal{P}(Y=1,Z=0)+\mathcal{P}(Y=1,Z=1)=1/2\]
\[\mathcal{P}(Z=0)=\mathcal{P}(X=0,Z=0)+\mathcal{P}(X=1,Z=0)=1/2\]
\[\mathcal{P}(Z=1)=\mathcal{P}(X=0,Z=1)+\mathcal{P}(X=1,Z=1)=1/2\]

\section{Mutual independence}
Thanks to the previous equations, we can find that
\[\mathcal{P}(X=0,Y=0,Z=0)=1/4\]
\[\mathcal{P}(X=0)\mathcal{P}(Y=0)\mathcal{P}(Z=0)=(1/2)^3=1/8\]
Thus
\[\mathcal{P}(X=0,Y=0,Z=0)\neq\mathcal{P}(X=0)\mathcal{P}(Y=0)\mathcal{P}(Z=0)\]
X,Y and Z are not mutually independent
\section{Pairwise independence}
Thanks to the previous equations we can find that
\[\mathcal{P}(X=0,Y=0)=\mathcal{P}(X=0)\mathcal{P}(Y=0)=1/4\]
\[\mathcal{P}(X=0,Y=1)=\mathcal{P}(X=0)\mathcal{P}(Y=1)=1/4\]
\[\mathcal{P}(X=1,Y=0)=\mathcal{P}(X=1)\mathcal{P}(Y=0)=1/4\]
\[\mathcal{P}(X=1,Y=1)=\mathcal{P}(X=1)\mathcal{P}(Y=1)=1/4\]

\[\mathcal{P}(X=0,Z=0)=\mathcal{P}(X=0)\mathcal{P}(Z=0)=1/4\]
\[\mathcal{P}(X=0,Z=1)=\mathcal{P}(X=0)\mathcal{P}(Z=1)=1/4\]
\[\mathcal{P}(X=1,Z=0)=\mathcal{P}(X=1)\mathcal{P}(Z=0)=1/4\]
\[\mathcal{P}(X=1,Z=1)=\mathcal{P}(X=1)\mathcal{P}(Z=1)=1/4\]

\[\mathcal{P}(Y=0,Z=0)=\mathcal{P}(Y=0)\mathcal{P}(Z=0)=1/4\]
\[\mathcal{P}(Y=0,Z=1)=\mathcal{P}(Y=0)\mathcal{P}(Z=1)=1/4\]
\[\mathcal{P}(Y=1,Z=0)=\mathcal{P}(Y=1)\mathcal{P}(Z=0)=1/4\]
\[\mathcal{P}(Y=1,Z=1)=\mathcal{P}(Y=1)\mathcal{P}(Z=1)=1/4\]

Thus

X,Y and Z are pairwise independent.
\section{Conditional independence}
For any random variables X1,X2 and X3
X1 is independent of X2 conditioning on X3 if and only if
\[\mathcal{P}(x1,x2,x3)\mathcal{P}(x2)=\mathcal{P}(x1,x2)\mathcal{P}(x2,x3)\]
In our case, for any correspondence between X1, X2, X3 and X,Y, Z $\mathcal{P}(x1,x2)\mathcal{P}(x2,x3)$ will always be equal to 1/8
On the other hand, $\mathcal{P}(x1,x2,x3)\mathcal{P}(x2)$ will be equal to 0 for some value of x,y and z, for example when X=Y=Z=1.
Thus, for any possible permutation of X,Y,and Z as X1, X2, and X3, none of them is conditionally independent.

\section{Equivalent definitions of Conditional independence}
Let's prove that the jumping form and shortened form are equivalent to the symetric form.

\subsection{Jumping form}
for $\mathcal{P}(x)>0, \mathcal{P}(y)>0$
\[\mathcal{P}(x,y,z)=\mathcal{P}(x)\mathcal{P}(y|x)\mathcal{P}(z|y)\]
\[\Leftrightarrow\]
$\mathcal{P}(x,y,z)=\mathcal{P}(x)\frac{\mathcal{P}(x,y)}{\mathcal{P}(x)}\frac{\mathcal{P}(y,z)}{\mathcal{P}(y)}$ using the formulae $\mathcal{P}(z|x,y) = \frac{\mathcal{P}(x,y,z)}{\mathcal{P}(x,y)}$
\[\Leftrightarrow\]
\[\mathcal{P}(x,y,z)\mathcal{P}(y)=\mathcal{P}(x,y)\mathcal{P}(y,z)
\]
QED
\\

\subsection{Shortened form}
for $\mathcal{P}(x,y)>0$
\[\mathcal{P}(z|x,y)=\mathcal{P}(z|y)\]
\[\Leftrightarrow\]
$\frac{\mathcal{P}(x,y,z)}{\mathcal{P}(x,y)}=\frac{\mathcal{P}(y,z)}{\mathcal{P}(y)}$ using the formulae $\mathcal{P}(z|x,y) = \frac{\mathcal{P}(x,y,z)}{\mathcal{P}(x,y)}$
\[\Leftrightarrow\]
\[\mathcal{P}(x,y,z)\mathcal{P}(y)=\mathcal{P}(x,y)\mathcal{P}(y,z)
\]

QED

\end{document}


\section{2-D DP}

\subsection{2-D DP (part 1)}
%%%%%%%%%%
\begin{frame}{2-D DP (part 1)}
  \begin{exampleblock}{LCS: longest common subsequence \pno{2.2.7}}
    \begin{itemize}
      \item $X = X_{1} \cdots X_{m}; Y = Y_{1} \cdots Y_{n}$
      \item find (the length of) an LCS of $X$ and $Y$
    \end{itemize}
    \begin{align*}
      X &= \langle A,\textcolor{blue}{B},\textcolor{blue}{C},\textcolor{blue}{B},D,\textcolor{blue}{A},B \rangle  \\
      Y &= \langle \textcolor{blue}{B},D,\textcolor{blue}{C},A,\textcolor{blue}{B},\textcolor{blue}{A} \rangle \\
      Z &= \langle B,C,B,A \rangle
    \end{align*}
  \end{exampleblock}

  \begin{block}{Solution.}
    \begin{itemize}
      \item subproblem: $L[i,j]$: the length of an LCS of $X[1 \cdots i]$ and $Y[1 \cdots j]$
      \item goal: $L[m,n]$
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}{2-D DP (part 1)}
  \begin{block}{Solution.}
    \begin{itemize}
      \item question: Is $X_{i} = Y_{j}$?
      \item recurrence:
	\begin{displaymath}
	  L[i,j] = \left\{ \begin{array}{ll}
	    L[i-1, j-1] + 1 & \textrm{if $X_{i} = Y_{j}$}\\
	    \max \set{L[i-1,j], L[i,j-1]} & \textrm{if $X_{i} \neq Y_{j}$}
	  \end{array} \right.
	\end{displaymath}
      \item<2-> initialization:
	\begin{align*}
	  L[i,0] &= 0, 0 \le i \le m \\
	  L[0,j] &= 0, 0 \le j \le n
	\end{align*}
    \end{itemize}
  \end{block}

  \uncover<3->{
  \begin{center}
    \textcolor{red}{It {\it may be} correct. But I feel quite uncomfortable without a proof.}
  \end{center}
  }
\end{frame}
%%%%%%%%%%
\begin{frame}{2-D DP (part 1)}
  \begin{alertblock}{Counterexample?}
    \[
      L[i,j] = L[i-1,j-1] + 1 \text{ if } X_{i} = Y_{j}
    \]

    \begin{columns}
      \column{0.50\textwidth}
	\begin{align*}
	  X &= \textcolor{blue}{a},\textcolor{blue}{b},c,c,\textcolor{red}{c} \\
	  Y &= \textcolor{blue}{a},\textcolor{blue}{b},\textcolor{red}{c}  \\
	  Z &= \textcolor{blue}{a},\textcolor{blue}{b},\textcolor{red}{c}
	\end{align*}
      \column{0.50\textwidth}
	\begin{align*}
	  X &= \textcolor{blue}{a},\textcolor{blue}{b},\textcolor{blue}{c},c,c \\
	  Y &= \textcolor{blue}{a},\textcolor{blue}{b},\textcolor{blue}{c}  \\
	  Z &= \textcolor{blue}{a},\textcolor{blue}{b},\textcolor{blue}{c}
	\end{align*}
    \end{columns}
  \end{alertblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{2-D DP (part 1)}
  \begin{block}{Correctness proof (I).}
    \begin{theorem}{}
      $L[i,j] = L[i-1,j-1] + 1$ if $X_{i} = Y_{j}$.
    \end{theorem}

    \begin{theorem}{}
      $Z[1 \cdots k]$ with $\textcolor{blue}{Z_{k} \equiv X_{i} \land Z_{k} \equiv Y_{j}}$ \emph{is} an LCS of $X[1 \cdots i]$ and $Y[1 \cdots j]$.
    \end{theorem}
    \begin{proof}
      \begin{enumerate}
	\item $Z_{k} = X_{i} = Y_{j}$ (by contradiction)
	% \item But, $Z_{k} = X_{i} \nRightarrow Z_{k} \equiv X_{i}; Z_{k} = Y_{i} \nRightarrow Z_{k} \equiv Y_{i}$
	\item $Z_{k} = X_{i} = Y_{j} \Rightarrow \text{ either } Z_{k} \equiv X_{i} \text{ or } Z_{k} \equiv Y_{j}$ (by contradiction)
	  \begin{enumerate}
	    \item $Z_{k} \equiv X_{i} \land Z_{k} \equiv Y_{j}$
	    \item $Z_{k} \not\equiv X_{i} \land Z_{k} \equiv Y_{j}$
	    \item $Z_{k} \equiv X_{i} \land Z_{k} \not\equiv Y_{j}$
	  \end{enumerate}
      \end{enumerate}
    \end{proof}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}{2-D DP (part 1)}
  \begin{block}{Correctness proof (II).}
    \begin{theorem}{}
      $L[i,j] = \max \set{L[i-1,j], L[i,j-1]} \text{ if } X_{i} \neq Y_{j}$
    \end{theorem}

    \begin{theorem}{}
      If $X_{i} \neq Y_{j}$, then either $X_{i} \notin \text{LCS}[i,j]$ or $Y_{j} \notin \text{LCS}[i,j]$.
    \end{theorem}
    \begin{proof}
      By contradiction.
    \end{proof}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}<beamer:0>{2-D DP (part 1)}
  \begin{exampleblock}{LCS with repetition of $X_{i}$ \pno{2.2.8}}
    \begin{enumerate}
      \item repetition of $X_{i}$
      \item $k$-bounded repetition of $X_{i}$
    \end{enumerate}
  \end{exampleblock}

  \begin{block}{Solution.}
    \begin{enumerate}
      \item repetition of $x_{i}$:
	\begin{displaymath}
	  L[i,j] = \left\{ \begin{array}{ll}
	    L[\textcolor{red}{i}, j-1] + 1 & \textrm{if $X_{i} = Y_{j}$}\\
	    \max \set{L[i-1,j], L[i,j-1]} & \textrm{if $X_{i} \neq Y_{j}$}
	  \end{array} \right.
	\end{displaymath}
      \item $k$-bounded repetition of $X_{i}$:

	$X^{(k)} = X_{1}^{(k)} \cdots X_{m}^{(k)}$
    \end{enumerate}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}<beamer:0>{2-D DP (part 1)}
  \begin{exampleblock}{Shortest common supersequence \pno{2.2.10}}
    \begin{itemize}
      \item $X = \set{x_{1} \cdots x_{m}}; Y = \set{y_{1} \cdots y_{n}}$
      \item to find (the length of) a SCS of $X$ and $Y$
    \end{itemize}
  \end{exampleblock}

  \begin{block}{Solution.}
    \begin{itemize}
      \item subproblem $L[i,j]$: the length of an SCS of $X[1 \cdots i]$ and $Y[1 \cdots j]$
      \item goal: $L[m,n]$
      \item question: is $X_{i} = Y_{j}$
      \item recurrence:
	\begin{displaymath}
	  L[i,j] = \left\{ \begin{array}{ll}
	    L[i-1, j-1] + 1 & \textrm{if $X_{i} = Y_{j}$}\\
	    \max \set{L[i-1,j] + 1, L[i,j-1] + 1} & \textrm{if $X_{i} \neq Y_{j}$}
	  \end{array} \right.
	\end{displaymath}
    \end{itemize}
  \end{block}

  \begin{alertblock}{Remark.}
    $\max(m,n) \le L(m,n) \le m+n$
  \end{alertblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{2-D DP (part 1)}
  \begin{exampleblock}{Edit distance revisited}
    \begin{displaymath}
      \text{ED}[i,j] = \min \left\{ \begin{array}{ll}
	\text{ED}[i-1,j] + 1 &  \\
	\text{ED}[i,j-1] + 1 & \\
	\text{ED}[i-1,j-1] + \text{I}\set{X_{i} = Y_{j}} &
      \end{array} \right.
    \end{displaymath}
  \end{exampleblock}

  \uncover<2->{
  \begin{exampleblock}{}
    \begin{displaymath}
      \text{ED}[i,j] = \left\{ \begin{array}{ll}
        \text{ED}[i-1,j-1] & \text{if } X_{i} = Y_{j}  \\
        \min \left\{ \begin{array}{ll}
          \text{ED}[i-1,j] + 1 &  \\
          \text{ED}[i,j-1] + 1 &  \\
          \text{ED}[i-1,j-1] + 1 \\
	\end{array} \right. & \text{if } X_{i} \neq Y_{j}
      \end{array} \right.
    \end{displaymath}
  \end{exampleblock}
  }

  \uncover<3->{
  \begin{theorem}
    If $X_{i} = Y_{j}$, then $\text{ED}[i-1,j-1] \le \text{ED}[i-1,j] + 1$.
  \end{theorem}
  }
\end{frame}
%%%%%%%%%%
\begin{frame}<beamer:0>{2-D DP (part 1)}
  \begin{exampleblock}{Shuffle of strings \pno{2.2.12}}
    \begin{itemize}
      \item $X[1 \cdots m]; Y[1 \cdots n], Z[1 \cdots m+n]$
      \item is $Z$ a shuffle of $X$ and $Y$?
    \end{itemize}
  \end{exampleblock}

  \begin{block}{Solution.}
    \begin{itemize}
      \item $S[i,j]$: Is $Z[1 \cdots i+j]$ a shuffle of $X[1 \cdots i]$ and $Y[1 \cdots j]$?
      \item goal: $S[m,n]$
      \item question: what is the relation among $X_{i}, Y_{j}, \text{ and } Z_{i+j}$?
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}<beamer:0>{2-D DP (part 1)}
  \begin{block}{Solution.}
    \begin{itemize}
      \item recurrence:
	\begin{displaymath}
	  S[i,j] = \left\{ \begin{array}{ll}
	    \text{false} & \textrm{if $Z_{i+j} \neq X_{i} \land Z_{i+j} \neq Y_{j}$}\\
	    S[i-1,j] & \textrm{if $Z_{i+j} = X_{i} \land Z_{i+j} \neq Y_{j}$}\\
	    S[i,j-1] & \textrm{if $Z_{i+j} \neq X_{i} \land Z_{i+j} = Y_{j}$}\\
	    S[i-1,j] \lor S[i,j-1] & \textrm{if $Z_{i+j} = X_{i} = Y_{j}$}
	  \end{array} \right.
	\end{displaymath}
      \item initialization:
	\begin{align*}
	  S[0,0] &= \text{true} \\
	  S[0,j] &= \left\{ \begin{array}{ll}
	    \text{true} & \text{if } Y = Z  \\
	    \text{false} & \text{if } Y \neq Z
	  \end{array} \right. \\
	  S[i,0] &= \left\{ \begin{array}{ll}
	    \text{true} & \text{if } X = Z  \\
	    \text{false} & \text{if } X \neq Z
	  \end{array} \right.
	\end{align*}
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\subsection{2-D DP (part 2)}

%%%%%%%%%%
\begin{frame}{2-D DP (part 2)}
  \begin{exampleblock}{Longest contiguous substring both forward and backward \pno{2.2.9}}
    \begin{itemize}
      \item string $T[1 \cdots n]$
      \item to find LCS both forward and backward
    \end{itemize}

    \begin{center}
      d\textcolor{blue}{ynam}icprogramming\textcolor{blue}{many}times
    \end{center}
  \end{exampleblock}

  \begin{alertblock}{Trial and error.}
    \begin{itemize}
      \item try subproblem $L[i]$: the length of an LCS in $T[1 \cdots i]$
      \item try subproblem $L[i,j]$: the length of an LCS in $T[i \cdots j]$
    \end{itemize}
  \end{alertblock}
\end{frame}
%%%%%%%%%%
\begin{frame}{2-D DP (part 2)}
  \begin{block}{Solution.}
    \begin{itemize}
      \item $L[i,j]$: the length of an LCS \textcolor{blue}{starting with $T_{i}$ and ending with $T_{j}$}
      \item goal: $\max_{1 \le i \le j \le n} L[i,j]$ (simply $O(n^3)$)
      \item<2-> question: Is $T_{i} = T_{j}$?
      \item<2-> recurrence:
	\begin{displaymath}
	  L[i,j] = \left\{ \begin{array}{ll}
	    0 & \textrm{if $T_{i} \neq T_{j}$}  \\
	    L[i+1,j-1] + 1 & \textrm{if $T_{i} = T_{j}$}
	  \end{array} \right.
	\end{displaymath}
      \item<3-> initialization:
	\begin{align*}
	  L[i,i] &= 0, 0 \le i \le n  \\
	  L[i,i+1] &= \left\{ \begin{array}{ll}
	    1 & \text{if } T_{i} = T_{i+1}  \\
	    0 & \text{if } T_{i} \neq T_{i+1}
	    \end{array} \right.
	\end{align*}
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}[fragile]{2-D DP (part 2)}
  \begin{block}{Code: three ways of filling the table.}
    \fignocaption{width = 0.50\textwidth}{fig/three-ways-filling-table.png}

    \begin{center}
        \begin{verbatim}
          for d = 2 to n-1
            for i = 1 to n-d
              j = i + d
              ...
          return max_{1 <= i <= j <= n} L[i,j]
       \end{verbatim}
    \end{center}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}[fragile]{2-D DP (part 2)}
  \begin{block}{Code: three ways of filling the table.}
    \begin{columns}[t]
      \column{0.50\textwidth}
        \begin{verbatim}
          for i = n-2 to 1
            for j = i+2 to n
              ...
          return ...
        \end{verbatim}
      \column{0.50\textwidth}
        \begin{verbatim}
          for j = 3 to n
            for i = j-2 to 1
              ...
          return ...
       \end{verbatim}
    \end{columns}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}<beamer:0>{2-D DP (part 2)}
  \begin{exampleblock}{Longest subsequence palindrome \pno{2.2.15 (a)}}
    \begin{itemize}
      \item string $S[1 \cdots n]$
      \item to find (the length of) a longest subsequence palindrome
    \end{itemize}
  \end{exampleblock}

  \pause
  \begin{block}{Solution.}
    \begin{itemize}
      \item subproblem $L[i,j]$: the length of the LSP in $S[i \cdots j]$
      \item goal: $L[1,n]$
      \item question: is $S[i] = S[j]$?
      \item recurrence
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}<beamer:0>{2-D DP (part 2)}
  \begin{exampleblock}{Longest subsequence palindrome \pno{2.2.15 (b)}}
    \begin{itemize}
      \item string $S[1 \cdots n]$
      \item decompose into a sequence of palindromes
    \end{itemize}
  \end{exampleblock}

  \begin{block}{Solution.}
    \begin{itemize}
      \item $\text{Num}[i,j]$: the minimum number of palindromes obtained from $S[i \cdots j]$
      \item subproblem $\text{MinPals}[i]$: the minimum number of palindromes obtained from $S[1 \cdots i]$
      \item goal: $\text{MinPals}[n]$
      \item question: what is the start index of the last palindrome?
      \item recurrence:
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}{2-D DP (part 2)}
  \begin{exampleblock}{String split problem \pno{2.2.16}}
    \begin{itemize}
      \item split a string $S$ into many pieces
      \item cost $|S| = n \Rightarrow n$
      \item given locations of $m$ cuts: $\textcolor{gray}{C_{0}}, C_{1}, \cdots, C_{m}, \textcolor{gray}{C_{m+1}}$
      \item to find the MinCost of splitting $S$ into $m+1$ pieces $S_{0} \cdots S_{m}$
    \end{itemize}
  \end{exampleblock}

  \begin{block}{Solution.}
    \begin{itemize}
      \item subproblem: $\text{MinCost}[i,j]$: the minimum cost of splitting substring $S_{i} \cdots S_{j-1}$ using cuts $C_{i+1} \cdots C_{j-1}$
      \item goal: $\text{MinCost}[0,m+1]$
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%
\begin{frame}{2-D DP (part 2)}
  \begin{block}{Solution.}
    \begin{itemize}
      \item question: what is the first cut in $C_{i+1} \cdots C_{j-1}$?
      \item recurrence:
	\[
	  \text{MinCost}[i,j] = \min_{i < k < j} \left( \text{MinCost}[i,k] + \text{MinCost}[k,j] + l(S_{i} \cdots S_{j-1}) \right)
	\]
      \item initialization:
	\[
	  \text{MinCost}[i, i+1] = 0
	\]
    \end{itemize}
  \end{block}
\end{frame}
%%%%%%%%%%


\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage[colorinlistoftodos]{todonotes}

\usepackage{fancyvrb}
\DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\small}
\DefineVerbatimEnvironment{example}{Verbatim}{fontsize=\small}
\newcommand{\ignore}[1]{}

\title{On general balanced trees}
\author{Slavomir Kaslev}

\begin{document}
\maketitle

\section{Definition}

General balanced trees are trees with all internal nodes having at least one
child and all leafs being equidistant from the root. Using algebraic data types,
one would define them as
\begin{code}
  data F a = F0 a | F1 (F (G a))
  data G a = G0 a | G1 a (G a)
\end{code}
or equivalently using generalized algebraic data types
\begin{code}
  data F a where
    F0 :: a -> F a
    F1 :: F (G a) -> F a
  data G a where
    G0 :: a -> G a
    G1 :: a -> G a -> G a
\end{code}

\section{Generating function}

The generating function $f(x)$ of general balanced trees satisfies the equations
\begin{align*}
  f(x) &= x + f(g(x)) \\
  g(x) &= x + x g(x)
\end{align*}
By noticing that $g(x) = \frac{x}{1-x}$ we can rewrite it as a single equation
\begin{equation}
  f(x) = x + f(\frac{x}{1-x})\label{eq:eq1}
\end{equation}
The solution to \eqref{eq:eq1} is
\begin{equation}
  f(x) = \frac{d}{dx}\ln\Gamma(-\frac{1}{x})\label{eq:eq2}
\end{equation}
or written in terms of the digamma function $\psi(x) = \frac{d}{dx}\ln\Gamma(x)
= \frac{\Gamma^\prime(x)}{\Gamma(x)}$
$$f(x) = \psi(-\frac{1}{x})$$

\section{Proof}

A quick proof starts with the functional equation of the gamma function
$$\Gamma(x+1) = x \Gamma(x)$$
Taking logarithm of both sides of the equation
$$\ln\Gamma(x+1) = \ln(x) + \ln\Gamma(x)$$
and differentiating gives
$$\frac{d}{dx}\ln\Gamma(x+1) = \frac{1}{x} + \frac{d}{dx}\ln\Gamma(x)$$
By substituting $x \to -\frac{1}{x}$ and rearranging terms we obtain
$$\frac{d}{dx}\ln\Gamma(-\frac{1}{x}) = x + \frac{d}{dx}\ln\Gamma(1-\frac{1}{x})$$
Since $g(x) = \frac{x}{1-x}$ we know that $1-\frac{1}{x} = -\frac{1}{g(x)}$ and
therefore we have
$$\frac{d}{dx}\ln\Gamma(-\frac{1}{x}) = x + \frac{d}{dx}\ln\Gamma(-\frac{1}{g(x)})$$
Finally, the substitution $f(x) = \frac{d}{dx}\ln\Gamma(-\frac{1}{x})$ brings us
to our original equation
\begin{align*}
  f(x) &= x + f(g(x)) \\
  g(x) &= x + x g(x)
\end{align*}

\end{document}


\chapter{Bit operations}
\label{chap:Bit operations}

libzahl provides a number of functions that operate on
bits. These can sometimes be used instead of arithmetic
functions for increased performance. You should read
the sections in order.

\vspace{1cm}
\minitoc

\newpage
\section{Boundary}
\label{sec:Boundary}

To retrieve the index of the lowest set bit, use

\begin{alltt}
   size_t zlsb(z_t a);
\end{alltt}

\noindent
It will return a zero-based index, that is, if the
least significant bit is indeed set, it will return 0.

If {\tt a} is a power of 2, it will return the power
of which 2 is raised, effectively calculating the
binary logarithm of {\tt a}. Note, this is only if
{\tt a} is a power of two. More generally, it returns
the number of trailing binary zeroes, if equivalently
the number of times {\tt a} can evenly be divided by
2. However, in the special case where $a = 0$,
{\tt SIZE\_MAX} is returned.

A similar function is

\begin{alltt}
   size_t zbit(z_t a);
\end{alltt}

\noindent
It returns the minimal number of bits require to
represent an integer. That is, $\lceil \log_2 a \rceil - 1$,
or equivalently, the number of times {\tt a} can be
divided by 2 before it gets the value 0. However, in
the special case where $a = 0$, 1 is returned. 0 is
never returned. If you want the value 0 to be returned
if $a = 0$, write

\begin{alltt}
   zzero(a) ? 0 : zbits(a)
\end{alltt}

The definition ``it returns the minimal number
of bits required to represent an integer,''
holds true if $a = 0$, the other divisions
do not hold true if $a = 0$.

\newpage
\section{Shift}
\label{sec:Shift}

There are two functions for shifting bits
in integers:

\begin{alltt}
   void zlsh(z_t r, z_t a, size_t b);
   void zrsh(z_t r, z_t a, size_t b);
\end{alltt}

\noindent
{\tt zlsh} performs a left-shift, and {\tt zrsh}
performs a right-shift. That is, {\tt zlsh} adds
{\tt b} trailing binary zeroes, and {\tt zrsh}
removes the lowest {\tt b} binary digits. So if

$a = \phantom{00}10000101_2$ then

$r = 1000010100_2$ after calling {\tt zlsh(r, a, 2)}, and

$r = \phantom{0100}100001_2$ after calling {\tt zrsh(r, a, 2)}.
\vspace{1em}

{\tt zlsh(r, a, b)} is equivalent to $r \gets a \cdot 2^b$,
and {\tt zrsh(r, a, b)} is equivalent to $r \gets a \div 2^b$,
with truncated division, {\tt zlsh} and {\tt zrsh} are
significantly faster than {\tt zpowu} and should be used
whenever possible. {\tt zpowu} does not check if it is
possible for it to use {\tt zlsh} instead, even if it
would, {\tt zlsh} and {\tt zrsh} would still be preferable
in most cases because it removes the need for {\tt zmul}
and {\tt zdiv}, respectively.

{\tt zlsh} and {\tt zrsh} are implemented in two steps:
(1) shift whole characters, that is, groups of aligned
64 bits, and (2) shift on a bit-level between characters.

If you are implementing a calculator, you may want to
create a wrapper for {\tt zpow} that uses {\tt zlsh}
whenever possible. One such wrapper could be

\begin{alltt}
   void
   pow(z_t r, z_t a, z_t b)
   \{
       size_t s1, s2;
       if ((s1 = zlsb(a)) + 1 == zbits(a) &&
                     zbits(b) <= 8 * sizeof(SIZE_MAX)) \{
           s2 = zzero(b) ? 0 : b->chars[0];
           if (s1 <= SIZE_MAX / s2) \{
               zsetu(r, 1);
               zlsh(r, r, s1 * s2);
               return;
           \}
       \}
       zpow(r, a, b);
   \}
\end{alltt}

\newpage
\section{Truncation}
\label{sec:Truncation}

In \secref{sec:Shift} we have seen how bit-shift
operations can be used to multiply or divide by a
power of two. There is also a bit-truncation
operation: {\tt ztrunc}, which is used to keep
only the lowest bits, or equivalently, calculate
the remainder of a division by a power of two.

\begin{alltt}
   void ztrunc(z_t r, z_t a, size_t b);
\end{alltt}

\noindent
is consistent with {\tt zmod}; like {\tt zlsh} and
{\tt zrsh}, {\tt a}'s sign is preserved into {\tt r}
assuming the result is non-zero.

{\tt ztrunc(r, a, b)} stores only the lowest {\tt b}
bits in {\tt a} into {\tt r}, or equivalently,
calculates $r \gets a \mod 2^b$. For example, if

$a = 100011000_2$ then

$r = \phantom{10001}1000_2$ after calling
{\tt ztrunc(r, a, 4)}.

\newpage
\section{Split}
\label{sec:Split}

In \secref{sec:Shift} and \secref{sec:Truncation}
we have seen how bit operations can be used to
calculate division by a power of two and
modulus a power of two efficiently using
bit-shift and bit-truncation operations. libzahl
also has a bit-split operation that can be used
to efficiently calculate both division and
modulus a power of two efficiently in the same
operation, or equivalently, storing low bits
in one integer and high bits in another integer.
This function is

\begin{alltt}
   void zsplit(z_t high, z_t low, z_t a, size_t b);
\end{alltt}

\noindent
Unlike {\tt zdivmod}, it is not more efficient
than calling {\tt zrsh} and {\tt ztrunc}, but
it is more convenient. {\tt zsplit} requires
that {\tt high} and {\tt low} are from each
other distinct references.

Calling {\tt zsplit(high, low, a, b)} is
equivalent to

\begin{alltt}
   ztrunc(low, a, delim);
   zrsh(high, a, delim);
\end{alltt}

\noindent
assuming {\tt a} and {\tt low} are not the
same reference (reverse the order of the
functions if they are the same reference.)

{\tt zsplit} copies the lowest {\tt b} bits
of {\tt a} to {\tt low}, and the rest of the
bits to {\tt high}, with the lowest {\tt b}
removesd. For example, if $a = 1010101111_2$,
then $high = 101010_2$ and $low = 1111_2$
after calling {\tt zsplit(high, low, a, 4)}.

{\tt zsplit} is especially useful in
divide-and-conquer algorithms.

\newpage
\section{Bit manipulation}
\label{sec:Bit manipulation}

The function

\begin{alltt}
   void zbset(z_t r, z_t a, size_t bit, int mode);
\end{alltt}

\noindent
is used to manipulate single bits in {\tt a}. It will
copy {\tt a} into {\tt r} and then, in {\tt r}, either
set, clear, or flip, the bit with the index {\tt bit}
— the least significant bit has the index 0. The
action depend on the value of {\tt mode}:

\begin{itemize}
\item
$mode > 0$ ($+1$): set
\item
$mode = 0$ ($0$): clear
\item
$mode < 0$ ($-1$): flip
\end{itemize}

\newpage
\section{Bit test}
\label{sec:Bit test}

libzahl provides a function for testing whether a bit
in a big integer is set:

\begin{alltt}
   int zbtest(z_t a, size_t bit);
\end{alltt}

\noindent
it will return 1 if the bit with the index {\tt bit}
is set in {\tt a}, counting from the least significant
bit, starting at zero. 0 is returned otherwise. The
sign of {\tt a} is ignored.

We can think of this like so: consider

$$ \lvert a \rvert = \sum_{i = 0}^\infty k_i 2^i,~ k_i \in \{0, 1\}, $$

\noindent
{\tt zbtest(a, b)} returns $k_b$. Equivalently, we can
think that {\tt zbtest(a, b)} return whether $b \in B$
where $B$ is defined by

$$ \lvert a \rvert = \sum_{b \in B} 2^b,~ B \subset \textbf{Z}_+, $$

\noindent
or as right-shifting $a$ by $b$ bits and returning
whether the least significant bit is set.

{\tt zbtest} always returns 1 or 0, but for good
code quality, you should avoid testing against 1,
rather you should test whether the value is a
truth-value or a falsehood-value. However, there
is nothing wrong with depending on the value being
restricted to being either 1 or 0 if you want to
sum up returned values or otherwise use them in
new values.

\newpage
\section{Connectives}
\label{sec:Connectives}

libzahl implements the four basic logical
connectives: and, or, exclusive or, and not.
The functions for these are named {\tt zand},
{\tt zor}, {\tt zxor}, and {\tt znot},
respectively.

The connectives apply to each bit in the
integers, as well as the sign. The sign is
treated as a bit that is set if the integer
is negative, and as cleared otherwise. For
example (integers are in binary):

\begin{alltt}
   zand(r, a, b)              zor(r, a, b)
   a = +1010  (input)         a = +1010  (input)
   b = -1100  (input)         b = -1100  (input)
   r = +1000  (output)        r = -1110  (output)

   zxor(r, a, b)              znot(r, a)
   a = +1010  (input)         a = +1010  (input)
   b = -1100  (input)         r = -0101  (output)
   r = -0110  (output)
\end{alltt}

Remember, in libzahl, integers are represented
with sign and magnitude, not two's complement,
even when using these connectives. Therefore,
more work than just changing the name of the
called function may be required when moving
between big integer libraries. Consequently,
{\tt znot} does not flip bits that are higher
than the highest set bit, which means that
{\tt znot} is nilpotent rather than self dual.

Below is a list of the value of {\tt a} when
{\tt znot(a, a)} is called repeatedly.

\begin{alltt}
   10101010
   -1010101
     101010
     -10101
       1010
       -101
         10
         -1
          0
          0
          0
\end{alltt}