%!TEX TS-program = xelatex
% !TEX root = ../thesis.tex
% Do not delete; used to set build system

\chapter{Supplemental algorithms and figures for ``Template-based estimation of genome-wide nucleosome positioning via distributed HMC''}

\section{Algorithmic details of inference}

\subsection{Distributed HMC sampler}
\label{supp:nucleosomes:sec:mcmc}

Recall that the model specified in Section 2 is:
\begin{align}
 \label{supp:nucleosomes:eq:observation_model}
  y_k|\lambda_k         & \sim \hbox{Poisson } (\lambda_k) \\
 \label{supp:nucleosomes:eq:linear_model}
  \lambda_{(N\times 1)} & \equiv X_{(N\times (N - \ell_0))} ~ \beta_{((N - \ell_0)\times 1)}, \\
\nonumber & \quad \beta_k>0 \hbox{ for } k=\lfloor \ell_0 / 2 \rfloor + 1 \dots N - \lfloor \ell_0 / 2 \rfloor \\
\label{supp:nucleosomes:eq:positioning_model}
  \log \beta_k        & \sim \hbox{Normal } (\mu_{s_k},\sigma^2_{s_k})
\end{align}
given a segmentation function $\fnDef{s}{\range{1}{N}}{\range{1}{S}}$, which maps the $N$ base pair locations to $S$ regions in which coefficients $\beta_k$ can be assumed to be identically distributed.
$X$ specifies the contribution of a nucleosome positioned at base pair $k$ to the expected number of reads at base pair $m$ due to digestion variability, and $s(k)$ is denoted as $s_k$ for compactness.
This specification is completed with independent priors on each $(\mu_s, \sigma^2_s)$:
\begin{align}
\sigma^2_{s} &\sim \mathrm{InvGamma}(\alpha_0, \gamma_0) \ , \\
\mu_{s} | \sigma^2_{s} &\sim N(\mu_0, \frac{\sigma^2_{s}}{n_{s} \tau_0}) \ ,
\end{align}
where $n_{s}$ is the length of segment $s$.

Our MCMC sampler alternates between two conditional updates:
\begin{enumerate}
 \item Draw $(\bm \mu^{(r)}, \bm \sigma^{2\,(r)}) | \bm \beta^{(r-1)}$ directly, then
 \item Update $\bm \beta^{(r)} | (\bm \mu^{(r)}, \bm \sigma^{2\,(r)})$ via a distributed HMC step.
\end{enumerate}
The former is a standard conjugate draw, while the latter is done via a distributed version of the standard Hamiltonian Monte Carlo (HMC) routine.

\subsubsection{Hyperparameter updates}

In detail, step 1 consists of the following draws for each $(\mu_s, \sigma^2_s)$, defining $\bm \theta = \log \bm \beta$:
\begin{align}
 \sigma_s^{2\,(r)} | \bm \beta^{(r-1)} &\sim InvGamma{\bigg(}\frac{n_s}{2} + \alpha_0,
  \, \frac{1}{2} \sum_{k:s_k = s} (\theta_k^{(r-1)} - \bar{\theta}_s^{(r-1)})^2  \\
\nonumber
  & + \frac{\tau_0 n_s}{2 (1 + \tau_0)} (\bar{\theta}_s^{(r-1)} - \mu_0)^2
  + \gamma_0 {\bigg)} \ , \\
 \mu_s^{(r)} | \sigma_s^{2\,(r)}, \bm \beta^{(r-1)} &\sim N\left( \frac{\bar{\theta}_s^{(r-1)} + \tau_0 \mu_0}{1 + \tau_0}, \frac{\sigma_s^{2\,(r)}}{n_s (1 + \tau_0)} \right) \ ,
\end{align}
where $\bar{\theta}_s^{(r-1)} = \frac{1}{n_s} \sum_{k : s_k = s} \theta_k^{(r-1)}$.
These are standard conjugate updates and have computational and memory complexity $O(N)$.

\subsubsection{Distributed HMC update for \texorpdfstring{$\bm \beta$}{beta}}
\label{supp:nucleosomes:sec:distributedHMC}

The draws in step 2 proceed in two stages, using two partitions of $\bm \beta$.
The first starts at the beginning of $\bm \beta$ and proceeds forward with subvectors of length at most $B$ separated by $2w$, yielding
\begin{align}
D_1 &= \bm \beta_{[1 : B]}, \bm \beta_{[B + 2w + 1 : 2B + 2w]}, \ldots, \bm \beta_{[n_b (B + 2w) + 1 : N]} \ , \\
D_2 &= \bm \beta_{[B/2 + 1 : 3B/2]}, \bm \beta_{[3B/2 + 2w + 1 : 5B/2 + 2w]}, \ldots, \bm \beta_{[n_b (B + 2w) B/2 + 1 : N]} \ .
\end{align}
The subvectors within each partition are conditionally-independent given the $(\bm \mu, \bm \sigma^2)$ and the entries of $\bm \beta$ separating them.
Hence, we can update them in parallel across multiple processors.
The basic structure of these updates follows Algorithm \ref{supp:nucleosomes:alg:master}.
%
\begin{algorithm}%[ht]
 \hspace{-8pt} \textbf{Distributed HMC update}\\

 \tcc{Send conditioning information}
 Broadcast $\bm \mu^{(t-1)}$, and $\bm \sigma^{2\,(t-1)}$ to all workers \;

 \For{offset \KwTo (0, $B/2$)} {
 \tcc{Send first round of jobs to workers}
 \For{w \KwTo \Range{min(nWorkers, nBlocks)}}{
   $start = \max(0, (w - 1)(B + 2w) - 2w + \mbox{offset}) + 1$\;
   $end = \min(N, w(B + 2w) + 2w + \mbox{offset})$\;
   Send $\bm \theta[start:end] $ to worker process w with work tag attached \;
 }
 \If{ len(startVec) $<$ nWorkers }{
   Pause remaining workers \;
 }

 \tcc{Collect results}
 Set \textit{nComplete} = 0, \textit{nStarted} = min(nWorkers, nBlocks) \\
 \While{nComplete $<$ nBlocks } {
   Receive result $\bm \theta[start:end]$ from arbitrary worker with tag $b_1$ \;
   Incorporate result into working copy of $\bm \theta^{(t)}$ \;
   \textit{nComplete}++ \;
   \If{nStarted $<$ nBlocks} {
     \tcc{Send additional jobs as needed}
     $w = nStarted + 1$\;
     $start = \max(0, (w - 1)(B + 2w) - 2w + \mbox{offset}) + 1$\;
     $end = \min(N, w(B + 2w) + 2w + \mbox{offset})$\;
     Send $\bm \theta[start:end] $ to last completed worker process with work tag attached\;
     \textit{nStarted}++\;
   }
 }
}

 \caption{Distributed HMC update \label{supp:nucleosomes:alg:master}}
\end{algorithm}

The individual, worker-level HMC updates are done on $\bm \theta = \log \bm \beta$ and follow the standard leapfrog-based HMC procedure outlined in \citet{Neal2010}.
To compute these HMC updates, we require the log-posterior density of each subvector of $\bm \theta$ and its gradient.
First, define $\bm \lambda = X \bm \beta$, $\bm m = (\mu_{s_k} \mbox{ for } k = 1, \ldots, N)^\top$, and $\bm v = (\sigma^2_{s_k} \mbox{ for } k = 1, \ldots, N)^\top$.
Also, for vectors of equal dimension, let $/$ denote entrywise division and $**$ denote entrywise powers.
Then,
\begin{align}
\log p(\bm \theta | \bm \mu, \bm \sigma^2) &= -\bm 1^\top X \bm \beta + \sum_k y_k \log \left( \bm x_k^T \bm \beta \right) \\
\nonumber
&\phantom{=} - \frac{1}{2} \sum_k \frac{(\theta_k - \mu_{s_k})^2}{\sigma^2_{s_k}} + \mathrm{const} \ , \\
\nabla_{\bm \theta} \log p(\bm \theta | \bm \mu, \bm \sigma^2) &= -\diag{\bm \beta} X^\top (\bm 1 - \bm y / \bm \lambda) - (\bm \theta - \bm m) / \bm v \ , \\
\nabla_{\bm \theta} \nabla_{\bm \theta^\top} \log p(\bm \theta | \bm \mu, \bm \sigma^2) &= -\diag{\bm \beta} X'WX \diag{\bm \beta} \\
\nonumber
&\phantom{=} - \diag{\bm \beta} X' (\bm 1 - \bm y
/ \bm \lambda) - \diag{\bm 1 / \bm \sigma^2} \label{supp:nucleosomes:eq:hessian} \ ,
\end{align}
where $W = \diag{\bm y / \bm \lambda\mbox{**}2}$.
Due to the convolution structure of $X$, all matrix-vector products involving $X$ and $X^\top$ can be reduced to convolutions of vectors with the template vector $\bm t$.
This also enables efficient computation of the Hessian's diagonal, as
\begin{align}
\bm \lambda &= X \bm \beta = (\bm 0_{\lfloor \ell_0 / 2 \rfloor}^\top \; \bm \beta^\top \; \bm 0_{\lfloor \ell_0 / 2 \rfloor}^\top)^\top \convolution \bm t \\
X^\top (\bm 1 - y / \bm \lambda) &= (\bm 1 - y / \bm \lambda) \convolution \bm t \ , \\
\diag{X'WX} &= (\bm y / \bm \lambda\mbox{**}2) \convolution \bm t\mbox{**}2 \ .
\end{align}
%
This reduces the computational complexity of these evaluations to $O(B \log B)$ for each update of each subvector of $\bm \beta$.
Our block-level HMC steps are detailed in Algorithm \ref{supp:nucleosomes:alg:hmc}.
%
\begin{algorithm}%[ht]

 \KwData{Trajectory length $L$, $\bm \mu$, $\bm \sigma^2$, $\bm \theta[start:end]$, $\epsilon{\min}$, $\epsilon_{\max}$, block start $b$, template $\bm t$, chromosome length $N$}
 \tcc{Subset $\bm \theta[start:end]$ to B-length subvector to update and buffers}
 $\bm{\tilde \theta} = \bm \theta[ b : \min(b + B - 1, N)]$\;
 $\bm{\tilde \theta}_0 = \bm{\tilde \theta}$;
 $\bm{\underline \theta} = (\bm \theta[start:b], \bm \theta[\min(b + B - 1, N):end])$\;
 Draw step size $\epsilon \sim \Unif[\epsilon_{\min}, \epsilon_{\max}]$\;
 \tcc{Optionally estimate mass matrix from Hessian; default is identity}
 \If{Estimating mass matrix}{
  Maximize log conditional posterior to obtain $\bm \hat{\theta}$\;
  $M = -\nabla_{\bm{\tilde \theta}} \nabla_{\bm{\hat \theta}^\top} \log p(\bm{\hat \theta} | \bm \mu, \bm \sigma^2, \bm{\underline \theta} )$\;
  \If{Using diagonal mass matrix} {
   $M = \diag{M}$\;
  }
 }
 \Else{
  $M = I_{end - start}$\;
 }
 \tcc{Draw momentum}
 Draw $\bm p \sim N(\bm 0, M)$\;
 $\bm p_0 = \bm p$\;
 \tcc{Run leapfrog integration}
 $\bm p += \epsilon \nabla_{\bm{\tilde \theta}} \log p(\bm{\tilde \theta} | \bm \mu, \bm \sigma^2, \bm{\underline \theta}) / 2$\;
 \For{i \KwTo \Range{$L$}}{
  $\bm{\tilde \theta} += \epsilon M^{-1} p$\;
  \If{$i < L - 1$}{
   $\bm p += \epsilon \nabla_{\bm{\tilde \theta}} \log p(\bm{\tilde \theta} | \bm \mu, \bm \sigma^2, \bm{\underline \theta})$\;
  }
 }
 $\bm p += \epsilon \nabla_{\bm{\tilde \theta}} \log p(\bm{\tilde \theta} | \bm \mu, \bm \sigma^2, \bm{\underline \theta}) / 2$\;
 \tcc{Metropolis-Hastings step to correct for integration errors}
 $\log r = \log p(\bm{\tilde \theta} | \bm \mu, \bm \sigma^2, \bm{\underline \theta}) - \log p(\bm{\tilde \theta}_0 | \bm \mu, \bm \sigma^2, \bm{\underline \theta}) - 1/2( \bm p^\top M^{-1} \bm p - \bm p_0^\top M^{-1} \bm p_0)$\;
Draw $u ~ \Unif[0,1]$\;
\If{$u \leq r$} {
\Return{$(\bm{\tilde \theta}, 1)$} \tcc*{Accept update}
}
\Else {
\Return{$(\bm{\tilde \theta}_0, 0)$} \tcc*{Reject update}
}
 \caption{Worker-level HMC update \label{supp:nucleosomes:alg:hmc}}
\end{algorithm}
%
In practice, we fix $\epsilon_{\min} = 0.001$, $\epsilon_{\max} = 0.1$, and $L = 100$.
We also use a fixed diagonal mass matrix, although the algorithm can accommodate estimating it at every iteration if needed.
However, to maintain the $O(B \log B)$ scaling of our algorithm's complexity with block size, $M$ must remain diagonal.
If non-diagonal $M$ is used and/or estimated, our HMC update instead scales as $O(B^2)$.
In either case, the overall algorithm scales $O(N)$ for given a fixed block size $B$.
%
Memory requirements are $O(N)$ for the master process running the hyperparameter draws and coordinating the distributed HMC updates.
Each worker process requires $O(B \log B)$ memory to run the distributed HMC updates for diagonal $M$, while using a non-diagonal mass matrix $M$ requires $O(B^2)$ memory per worker.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Approximate EM algorithm}
\label{supp:nucleosomes:sec:approxem}

We develop an approximate EM algorithm, based on a Gaussian approximation of the conditional posterior of $\bm \theta$, as to obtain starting values for the MCMC sampler given in \ref{supp:nucleosomes:sec:mcmc}.
It provides a high-quality initialization for $\bm \beta$, $\bm \mu$, and $\bm \sigma^2$.
Simpler initializations are possible, but obtaining high-quality initial estimates can greatly reduce the number of MCMC iterations required for reliable inferences.

\subsubsection{Choice initial estimator}
\label{supp:nucleosomes:sec:objective}

We use $\hat \theta_k = E \left[ \theta_k | \bm y, \hat \mu_{s_k}, \hat \sigma^2_{s_k} \right]$ as an initial point estimate of $\theta_k$.
The distributed HMC sampler presented in Section \ref{supp:nucleosomes:sec:mcmc} yields information on the complete marginal posterior of $\bm \theta$ via simulation but, given the scale of this problem, an optimization-based approach is useful as a fast initialization method.
The approximate EM algorithm described in Section \ref{supp:nucleosomes:sec:approximate_em} provides both approximate marginal MAP estimates of the parameters $\bm \mu$ and $\bm \sigma^2$ and estimates of the target conditional expectations $\hat \theta_k$.

\subsubsection{Approximate EM algorithm via Laplace approximation}
\label{supp:nucleosomes:sec:approximate_em}

We implement an approximate EM algorithm to provide initial estimates of $(\bm \theta, \bm \mu, \bm \sigma)$.
In the E-step, we build an approximation of the conditional posterior of ${\bm \theta}$ given $(\bm y, \hat {\bm\mu}, \hat{\bm\sigma}^2)$ to estimate the $Q$ function, detailed below.
The M-step updates the estimates of $\bm \mu$ and $\bm \sigma^2$ toward the marginal posterior mode of $p(\bm \mu, \bm \sigma^2 | \bm y)$.

\paragraph{Approximate E-step}
%
In the E-step, the objective is to compute
\begin{equation}
  Q_t(\bm \mu, \bm \sigma^2) = E \left[ \log p(\bm \theta, \bm \mu, \bm \sigma^2 | \bm y ) | \bm y, \bm \mu^{(r-1)}, \bm \sigma^{2\,(r-1)} \right].
\end{equation}
%
The log joint posterior for $(\bm \mu, \bm \sigma^2, \bm \theta)$ is given by
\begin{eqnarray}\label{supp:nucleosomes:eq:logPosterior}
 \log p(\bm \theta, \bm \mu, \bm \sigma^2 | \bm y, \bm s, \tau_0) &=& -\sum_k
\bm x_k^T \beta_k + \sum_k y_k \log \left( \bm x_k^T \beta_k \right)  \\
 \nonumber && - \frac{1}{2} \sum_k \log \sigma^2_{s_k} -
  \frac{1}{2} \sum_k \frac{(\theta_k - \mu_{s_k})^2}{\sigma^2_{s_k}} \\
 \nonumber && - \frac{1}{2} \sum_s \log \left( \frac{\sigma^2_{s}}{n_{s} \tau_0}
  \right) - \frac{1}{2} \sum_s \frac{(\mu_s - \mu_0)^2}{\sigma^2_{s} / n_{s} \tau_0} \\
  \nonumber && - \sum_s \log \sigma^2_s + \mathrm{const}.
\end{eqnarray}
%
Thus, we can write the relevant portion of the expected log conditional
posterior for $\bm \theta$ given $\{\mu_{s_k}, \sigma^2_{s_k}\}$ as
\begin{eqnarray}
 Q_t(\bm \mu, \bm \sigma^2) &=& - \frac{1}{2} \sum_k \log \sigma^2_{s_k} -
  \frac{1}{2} \sum_k \frac{(\hat{\theta}_k - \mu_{s_k})^2}{\sigma^2_{s_k}}
 - \frac{1}{2} \sum_k \frac{\hat{V}_k}{\sigma^2_{s_k}} \\
 \nonumber && - \frac{1}{2} \sum_s \log \left( \frac{\sigma^2_{s}}{n_{s} \tau_0}
  \right) - \frac{1}{2} \sum_s \frac{(\mu_s - \mu_0)^2}{\sigma^2_{s} / n_{s} \tau_0}
 - \sum_s \log \sigma^2_s.
\end{eqnarray}
where $\hat{\theta}_k = E[ \theta_k | \bm \mu_{(t-1)}, \bm \sigma^2_{(t-1)} ]$
and $\hat{V}_k = \Var [ \theta_k | \bm \mu_{(t-1)}, \bm \sigma^2_{(t-1)} ]$.

While the conditional posterior $p(\bm \theta | \bm y, \bm \mu, \bm \sigma^2 )$ is available in close form,  the necessary  expectations $\hat{\theta}_k$ and variances $\hat V_k$ are not.
However, under the proposed log-Normal/Poisson model structure, the univariate conditional posteriors of $\theta_k$ given $\{\mu_{s_k}, \sigma^2_{s_k}\}$ are unimodal, log-concave, nearly symmetric, and have tails that go to zero as $\exp(-c \theta_k^2)$.
Thus, these conditional posteriors are nearly Gaussian and a Laplace approximation is appropriate.

To compute the Laplace approximation, we first find the posterior mode of
$\theta_k$ given $(\bm \mu^{(r-1)}, \bm \sigma^{2\,(r-1)})$. This amounts to
maximizing
\begin{equation}\label{supp:nucleosomes:eq:gFunction}
 g(\bm \theta) = -\sum_k \bm x_k^T \beta_k +
\sum_k y_k \log \left( \bm x_k^T \beta_k \right) -
\frac{1}{2} \sum_k \frac{(\theta_k - \mu_{s_k})^2}{\sigma^2_{s_k}}
\end{equation}
with respect to $\bm \theta$.
This mode is not available in closed form, but the given objective function is concave and has a continuous gradient, so numerical optimization is feasible.
%\footnote{To preserve the vector notation throughout the gradient and the Hessian calculations, we use the two following chain rules: $\frac{dg}{d\theta} = \frac{dg}{d\beta} \cdot \frac{d\beta}{d\theta} = \frac{dg}{d\beta} \cdot \beta$ and $\frac{d^2 g}{d\theta^2} = \frac{dg}{d\beta} \cdot \frac{d^2 \beta}{d\theta^2} + \frac{d^2 g}{d\beta^2} \cdot (\frac{d \beta}{d\theta})^2 = \frac{dg}{d\beta} \cdot \beta + \frac{d^2 g}{d\beta^2} \cdot \beta^2$}

%
The Laplace approximation then consists of substituting a Gaussian distribution with mean $\bm \hat{\theta}_t$ equal to the mode of $g$, and variance $\hat{V}_k = -\diag{H^{-1}}_k$ for the conditional posterior $p(\theta_k | \bm y, \mu_{s_k}, \sigma^2_{s_k} )$.

\paragraph{M-step}
%
The M-step consists of maximizing $Q_t(\bm \mu, \bm \sigma^2)$ with respect to $\mu_{s_k}$ and $\sigma^2_{s_k}$.
We obtain two simple closed-form solutions, summarized in Equations \ref{supp:nucleosomes:eq:mstep-mu} and \ref{supp:nucleosomes:eq:mstep-sigmasq}:
\begin{eqnarray} \label{supp:nucleosomes:eq:mstep-mu}
\hat{\mu}_{s} &=& \frac{1}{1 + \tau_0} \Big(\frac{1}{n_s}
 \sum_{k:s_k = s} \hat{\theta}_k \Big) +
 \frac{\tau_0}{1 + \tau_0} \mu_0 \\ \label{supp:nucleosomes:eq:mstep-sigmasq}
\hat{\sigma}^2_{s} &=& \frac{\frac{1}{n_s} \sum_{k:s_k = s}
 (\hat{\theta}_k - \hat{\mu}_{s})^2
 + \frac{1}{n_s} \sum_{k:s_k = s} \hat{V}_k +
 \tau_0 (\hat{\mu}_s - \mu_0)^2}{1+\tau_0 + 2 / n_s}
\end{eqnarray}

The term $\hat{V}_k$ differentiates the M-step update of $\sigma_s$ from the update obtained from joint maximization of the log-posterior.
The joint mode of this log-posterior is reached at $\bm \sigma^2 = \bm 0$ and $\theta_k = \mu_{s_k} \, \forall k$, as these values would allow the joint log-posterior density to increase without bound.
Algorithmically, the $\hat{V}_k$ term introduced by the EM algorithm prevents $\hat{\sigma}^2$ from collapsing to $0$, providing non-degenerate inferences.

% AWB: Perhaps add pointer to new algorithm without blocked E-step for overview of process; seems less necessary at this point

\subsubsection{Distributed approximate E-step}
\label{supp:nucleosomes:sec:parallelization}

We use the conditional independence structure of $\bm \beta$ given $(\bm \mu, \bm \sigma^2)$ and partitions discussed in Section \ref{supp:nucleosomes:sec:distributedHMC} to distribute our approximate E-step across multiple processors.
Given each partition of $\bm \theta$, we update the Laplace approximations in parallel, by finding the mode of the conditional posterior subvector-by-subvector.
The overall algorithm is blockwise coordinate ascent within each approximate E-step, with each E-step consisting of iterative maximization of $\log p(\bm \theta | \bm y, \bm \mu, \bm \sigma^2 )$ using different subsets of $\bm \theta$ (corresponding to different blocks) in each iteration.
This converges to the maximum of $g(\bm \theta)$ given $\bm \mu^{(r-1)}$ and $\bm \sigma^{2\,(r-1)}$.

The approximate E-step considers each block in each of the four configurations, during one iteration.
More details are give in Section \ref{supp:nucleosomes:sec:mpi}.
Within each block $m_1:m_2$, we maximize $\log p(\bm \theta_{m_1:m_2} | \bm y, \bm \mu, \bm \sigma^2, \bm \theta_{-(m_1:m_2)} )$ numerically via L-BFGS-B or a truncated Newton algorithm \citep{lbfgsb1997, Nash2000}; the latter is typically more efficient in this application.
We carry out this maximization directly, avoiding the data augmentation typically used in additive Poisson models of this type \citep[e.g.,][]{VanDykEtAl2006}.
Such data augmentation would require storing and computing at least $(2w+1)N$ additional variables, provide slower convergence, and slow the overall computation substantially.
By controlling the size of the blocks, we can keep the scale of each optimization problem small enough that direct numerical maximization of these conditional posteriors is not a limiting factor for the algorithm (less than 100ms, typically).

We compute the Hessian of the conditional log-posterior $\log p(\bm \theta | \bm y, \bm \mu, \bm \sigma^2 )$ after each complete scan through the partitions, completing the approximate E-step and providing the information necessary for our M-step.
The Hessian is sparse, but its inversion is computationally-intensive even with modern sparse-matrix solvers.
Thus, we typically use a diagonal approximation to the Hessian.
The diagonal approximation works well in our setting, even though one would expect the strong local dependence generated by the digestion matrix to produce a Hessian with large off-diagonal elements.
However, due to the use of exchangeable local regularization, the Hessian is typically diagonally-dominant.
The diagonal approximation is quite accurate; we observed few differences to 2-3 significant digits in comparisons of the estimated Hessian and its inverse on small portions of the genome (single ORFs with promoters, approximately 1,000 to 10,000bp in length).
%
We lay out the overall structure of this approximate EM algorithm in Algorithm \ref{supp:nucleosomes:alg:deconvolve}.

\begin{algorithm}%[ht]
\hspace{-8pt} \textbf{Outline of Approximate EM Algorithm} \\
 \While{not converged} {
  \tcc{E-step}
	\For{Partition \KwTo $(D_1, D_2)$}{
  Update $\hat{\bm \theta}$ via numerical maximization of $g(\bm \theta)$ within each subvector
	}
% AWB: Added nested for-loop with blockwise maximizations and scan
  Approximate $\Var \left[ \theta_k | \bm y, \mu_{s_k}, \sigma^2_{s_k}
    \right]$ by inverting Hessian of $p(\bm \theta | \bm y, \bm \mu, \bm
    \sigma^2 )$

  \tcc{M-step}
  Update $\bm \mu$ and $\bm \sigma^2$ as maximizers of $E \left[ p(\bm \theta,
    \bm \mu, \bm \sigma^2 | \bm y, \bm s, \tau_0) | \bm \mu_{(t-1)}, \bm
    \sigma^2_{(t-1)}, \bm s, \bm \tau_0 \right]$
 }
 \caption{Approximate EM Algorithm \label{supp:nucleosomes:alg:deconvolve}}
\end{algorithm}

\subsubsection{Algorithmic details}
\label{supp:nucleosomes:sec:mpi}

The algorithm outlined in Section \ref{supp:nucleosomes:sec:approximate_em} can be implemented on distributed systems with MPI, using the same techniques as the MCMC algorithm presented in Section \ref{supp:nucleosomes:sec:mcmc}.
Due to the use of a quasi-Newton optimization algorithm within each worker's approximate E-step, its computational complexity scales $O(B^2)$ for each such update.
However, it scales $O(N)$ in the length of genome given a fixed block size $B$.
Memory requirements are $O(N)$ for the master process running the M-step and coordinating the approximate E-step and $O(B^2)$ (independent of $N$) for the worker processes running the approximate E-step updates.
We lay out the details of this parallel approximate E-step in Algorithm \ref{supp:nucleosomes:alg:parallel}.

\begin{algorithm}%[ht]
 \hspace{-8pt} \textbf{Parallel Implementation of Approximate E-Step}\\
 \tcc{Send conditioning information}
 Broadcast $\bm \mu^{(t-1)}$, and $\bm \sigma^{2\,(t-1)}$ to all workers \;

 \For{offset \KwTo (0, $B/2$)} {
 \tcc{Send first round of jobs to workers}
 \For{w \KwTo \Range{min(nWorkers, nBlocks)}}{
   $start = \max(0, (w - 1)(B + 2w) - 2w + \mbox{offset}) + 1$\;
   $end = \min(N, w(B + 2w) + 2w + \mbox{offset})$\;
   Send $\bm \theta[start:end] $ to worker process w with work tag attached \;
 }
 \If{ len(startVec) $<$ nWorkers }{
   Pause remaining workers \;
 }

 \tcc{Collect results}
 Set \textit{nComplete} = 0, \textit{nStarted} = min(nWorkers, nBlocks) \\
 \While{nComplete $<$ nBlocks } {
   Receive result $\bm \theta[start:end]$ from arbitrary worker with tag $b_1$ \;
   Incorporate result into working copy of $\bm \theta^{(t)}$ \;
   \textit{nComplete}++ \;
   \If{nStarted $<$ nBlocks} {
     \tcc{Send additional jobs as needed}
     $w = nStarted + 1$\;
     $start = \max(0, (w - 1)(B + 2w) - 2w + \mbox{offset}) + 1$\;
     $end = \min(N, w(B + 2w) + 2w + \mbox{offset})$\;
     Send $\bm \theta[start:end] $ to last completed worker process with work tag attached\;
     \textit{nStarted}++\;
   }
 }
 }

 \tcc{Compute approximate variance, if needed}
 Compute approximate $\Var \left[ \theta_k | \bm y, \mu_{s_k}, \sigma^2_{s_k}  \right]$ using sparse Cholesky decomposition or diagonal approximation to Hessian\;

 \caption{Approximate E-Step \label{supp:nucleosomes:alg:parallel}}
\end{algorithm}

We this algorithm process a chromosome with $1.5e6$ base pairs in only 11 minutes using 256 threads on Amazon EC2; a chromosome with $2.4e5$ base pairs requires only 1 minute.
This method will easily scale to genomes of far greater size (e.g. mice) with this distributed structure, especially using resources such as EC2.
The one-to-one substitution of time and processors possible on the cloud makes it an ideal infrastructure for running this type of method.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Additional figures}

%\subsection{Reproducibility analysis---comparability of cluster-level estimators}

\begin{figure}[h]
\centering
\includegraphics[width=\textwidth]{figures/nucleosomes/figure_cluster_reproducibility_methods}
\caption{Joint distributions of of model-based and read-based estimates of cluster-level properties. \label{supp:nucleosomes:fig:methodComparison}}
\end{figure}

%\subsection{Power analysis---cluster locations}
%\label{supp:nucleosomes:sec:cluster}

\begin{sidewaysfigure}
\centering
\includegraphics[page=1,width=0.95\textheight]{figures/nucleosomes/plots_compare_power}
\caption{Power of model-based and Parzen window methods to detect cluster centers $\pm 5$bp vs. coverage by alternative position offset (rows) and effective magnitude of primary position (columns)}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includegraphics[page=5,width=0.95\textheight]{figures/nucleosomes/plots_compare_power}
\caption{Mean absolute position errors for model-based and Parzen window methods vs. coverage by alternative position offset (rows) and effective magnitude of primary position (columns)}
\end{sidewaysfigure}

%\subsection{Power analysis---local concentrations}
%\label{supp:nucleosomes:sec:detection}

%\subsubsection{Primary positions}

\begin{sidewaysfigure}
\centering
\includegraphics[page=1,width=0.95\textheight]{figures/nucleosomes/plots_power_pm3}
\caption{Power of model-based method to detect individual primary positions $\pm 5$bp vs. coverage by alternative position offset (rows) and effective magnitude of primary position (columns)}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\includegraphics[page=5,width=0.95\textheight]{figures/nucleosomes/plots_power_pm3}
\caption{Mean absolute position errors of model-based method for individual primary positions vs. coverage by alternative position offset (rows) and effective magnitude of primary position (columns)}
\end{sidewaysfigure}

%\subsubsection{Alternative positions}

\begin{sidewaysfigure}[h]
\includegraphics[page=9,width=0.95\textheight]{figures/nucleosomes/plots_power_pm3}
\caption{Power of model-based method to detect individual alternative positions $\pm 5$bp vs. coverage by alternative position offset (rows) and effective magnitude of alternative position (columns)}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\includegraphics[page=13,width=0.95\textheight]{figures/nucleosomes/plots_power_pm3}
\caption{Mean absolute position errors of model-based method for individual alternative positions vs. coverage by alternative position offset (rows) and effective magnitude of alternative position (columns)}
\end{sidewaysfigure}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% DFS and BFS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[../main.tex]{subfiles}
\begin{document}

% The basic breath-first and depth-first search we learned in Chapter~\ref{chapter_non_linear_graph} is like a linear search on linear data structures. There are more advanced universal graph search techniques:
% \begin{enumerate}

%     \item Better Efficiency:
%     \begin{enumerate}
%         \item Bidirectional Search that can  increase the efficiency of the BFS shown in Section~\ref{}.
%         \item Application of DFS on Problem Searching Space: Backtracking techniques to prune the searching space based on DFS in Section~\ref{sec_backtrack}.
%     \end{enumerate}
% \end{enumerate}

% \subsection{Implementation}

% A mutant for trees to visit it level by level
% \begin{lstlisting}[language = Python]
% def BFS(root):
%     q = [root]
%     root.visited = 1
%     level = 0
%     while q:
%         n=q.pop()
%         visit(n) #finish visit
%         for node in n.adjacent:
%             if not node.visited:
%                 node.visited = 1 #start to visit
%                 q.insert(0,node)
% \end{lstlisting}
%%%%%%%%%%%%%%%%%%%%%%Bidirectional%%%%%%%%%%%%%%%%%%%%%%%%

%\subsection{$A^*$}

% \section{Backtracking}% for Constraint Satisfaction Problem}
% \label{sec_backtrack}
% ``A  variant  of  Depth  First  Search  is  called  Back  Tracking search, which uses less memory. In this search only one successor  is  generated  at  a  time  rather  than  all successors. Each partially expanded node remembers which successor to generate  next.  In  this  way  only  $O(m)$  memory  is  needed rather  than  $O(b^m)$.  It  is  the  most  common  algorithm  for solving constraint satisfaction problem (CSP)." (Searching and Optimization Techniques in Artificial Intelligence: A Comparative Study $\&$
% Complexity Analysis)

\paragraph{Visualize Backtracking} Let us represent different $s$ as a node in a tree. Initial state $s=[]$ as the root node. The first level represents all possible states for $s=(s_0)$ of length 1, and the second level for $s=(s_0, s_1)$ of length 2. And the edge represents making a choice out of all items in the ordered set $A$. If we reach to end condition (leaf candidates) we succeed and the search stop. If the partial candidate can not satisfy the constraint, we return to the root node, and reset the state ('backtrack'). The process is shown in Fig.~\ref{fig:backtrack_tree}.
\begin{figure}[h!]
    \centering
    \includegraphics[width=0.98\columnwidth]{fig/back_tree_possibility.png}
    \caption{Tree of possibilities for a typical backtracking algorithm}
    \label{fig:backtrack_tree}
\end{figure}

\paragraph{Backtrack Template} We list the template here which we summarized after viewing different backtracking algorithms. It usually is composed of two parts: initialization and main dfs backtracking. After we figure out our state vector $s$ where in our template is \texttt{state\_tracker} with total search tree depth $n$. In the main backtracking state: we first generate candidates according to previous states and then try out each candidate by iteration. We set the state before call recursive function to the next depth and reset the state after we return and move on to try next candidate.
\begin{lstlisting}[language=Python]
def backtrack():
  # initialization
  A #a working data structure, either a list of candidates or a graph or a matrix representing a board
  state_tracker = []*n
  assist_state_tracker
  # main backtracking
  def dfs(d, n):
    '''d: depth representing level in the tree'''
    if d == n:
      return
    candidates = generate_candidates(state_tracker, assist_state_tracker)
    for c in candidates:
      set_state(state_tracker, assist_state_tracker, c)
      dfs(d+1, n)
      reset_state(state_tracler, assist_state_tracker, c)

  dfs(0, n)
\end{lstlisting}

\paragraph{Complexity Analysis} The time complexity of backtracking can be obtained from analyzing the search tree. The worst case incurs when the complete result occurs at the right most of the search tree, thus we need to traverse all the paths resulting visiting each node twice-one forward and one backward. Assume the cost to generate and visit a node is $O(2)$, and the total time complexity will be $O(|V|)$, where $|V|$ is the total nodes in the traverse tree.

In this chapter, the organization is as follows:
\begin{enumerate}
    \item Show Property 1: We will first show how backtrack construct the complete solution incrementally and how it backtracks to its previous state in Sec.~\ref{sec_enumeration}.
    \begin{enumerate}
        \item \textbf{On Implicit Graph:} start with the combination and permutation problem to show us how the backtracking works in Section~\ref{backtrack_permutation} and ~\ref{sec_combination} with simple and commonly seen combinatorial problems: combination and permutation.
        \item \textbf{On Explicit Graph:} Enumerating all paths between the source and target vertex in a graph drawing in Section~\ref{subsec_all_paths}. Similarly, it can be applied on enumerate all spanning trees, graph partition.
        \end{enumerate}
    \item  Show  Property 2: we demonstrate the application of search pruning in backtracking through CSP problems in Section~\ref{sec_sudoku}.
\end{enumerate}
%%%%%%%%%%%%%%%%%%%%Combination%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Enumeration}
\label{sec_enumeration}
\subsection{Permutation}
\label{backtrack_permutation}

\paragraph{Implicit Graph} In the graph, each node is either a partial or final solution. If we look it as a tree, the internal node is a partial solution and all leaves are final solutions. One edge represents generating the next solution based on the current solution. The vertices and edges are not given by an explicitly defined graph or trees, the vertices are generated on the fly and the edges are implicit relation between these nodes.

\paragraph{Backtracking VS DFS} The implementation of the state transfer we can use either BFS or DFS on the implicit vertices.  With recursive DFS, we can start from node [], and traverse to [1,2], then [1,2,3]. Then we backtrack to [1,2], backtrack to [1], and go to [1, 3], to [1, 3, 2].  To clear the relation between backtracking and DFS, we can say backtracking is a complete search technique which systematically builds the search tree (implicitly and not graph) and DFS is an ideal way to implement it.

\paragraph{Back to Permutation}
We can generalize Permutation, Permutations refer to the permutation of $n$ things taken $k$ at a time without repetition, the math formula is $A_{n}^{k} = n *(n-1)*(n-2)*...*k$. In Fig.~\ref{fig:backtrack_permutation}, we can see from each level $k$ shows all the solution of $A_{n}^{k}$. The generation of  $A_{n}^{k}$ is shown in the following Python Code.%Compared with combination,  [a, b] and [b, a] would be considered as different solution. The relation of the number of combination and permutation solution can be described in formula: $C_{n}^{k}=\frac{A_{n}^{k}}{k!}$, where $k!=k*(k-1)...*1$. So $A\ge C$.

% (N-Queens :
% permutations
%  with backtracking
% Soduko     :
% counting
%  with backtracking
% Scheduling:
% subsets
%  with backtracking\url{https://www.cs.princeton.edu/~rs/AlgsDS07/24CombinatorialSearch.pdf})

 % Also, in this process, we can trim the branches, so actually backtracking is DFS with trimming and backtrack to the previous stages.

\subsection{Combination}
\label{sec_combination}

Note: To generate the power set, backtracking is NOT the only solution, if you are interested right now, check out Section~\ref{part4_array_combine}.

% \subsection{Analysis}
% % \paragraph{Backtracking VS DFS} The implementation of Backtracking is equivalent to a DFS on the implicit or explicit search space and visiting each vertex no more than once.  Backtracking is a technique to build up solution spaces incrementally and each exactly only once. And once one path reaches to the end, it backtrack to its previous state and try another candidate. DFS is a natural way to implement backtarck technique.

% \paragraph{Applications} Backtracking can be applied where we can incrementally build up our final soultion from partial solution. It is a searching technique that applied on implicit graph which is built on-the-fly. It guarentees that it only visit each search vertex no more than once. The problems that backtracking can be used are these three types: (1) Combinations (Section~\ref{sec_combination}), (2) Permutations (Section~\ref{sec_permutation}), (3) enumerate all paths from a to b in graph. and (3) Optimization problems with constraints such as classical travels salesman, puzzles, and sudoku (Section~\ref{sec_sudoku}). In the first three cases, backtracking visit each implicit or explicit vertex exactly once in the searching space. And for problems with restraints, we can do search pruning and we end up amortizely visiting each vertex less than once which is more efficient compared with an exhaustive graph search such as DFS and BFS.

\section{Solve CSP with Search Prunning}
\label{sec_sudoku}

\end{document}


\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mhchem}
\usepackage{stmaryrd}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\graphicspath{ {./images/} }
\usepackage{bbold}

\begin{document}
\section{Contents}
1 Approximation Properties of Neural Network Function Class $\ldots \ldots \ldots \ldots \ldots \ldots \ldots \ldots$ $1.1$ Qualitative convergence results $\ldots \ldots \ldots \ldots \ldots \ldots \ldots \ldots \ldots \ldots \ldots \ldots$

\includegraphics[max width=\textwidth]{2022_01_05_aa5252bbfe3db7fb6bc8g-2}

\section{Approximation Properties of Neural Network Function Class}
\subsection{Qualitative convergence results}
The first question about $\mathrm{DNN}_{1}$ is about the approximation properties for any continuous functions. Here we have the next theorem.

The first proof for this lemma above can be found in [leshno1993multilayer] and summarized in [pinkus1999approximation]. The next theorem plays an important role in the proof of above lemma, which is first proved in [leshno1993multilayer] with several steps. Here we can present a more direct and simple version.

Theorem 1 (Universal Approximation Property of Shallow Neural Networks). Let $\sigma$ be a Riemann integrable function and $\sigma \in L_{l o c}^{\infty}(\mathbb{R}) .$ Then $\Sigma_{d}(\sigma)$ in dense in $C(\Omega)$ for any compact $\Omega \subset \mathbb{R}^{n}$ if and and only if $\sigma$ is not a polynomial!

Namely, if $\sigma$ is not a polynomial, then, for any $f \in C(\bar{\Omega})$, there exists a sequence $\phi_{n} \in \mathrm{DNN}_{1}$ such that
$$
\max _{x \in \bar{\Omega}}\left|\phi_{n}(x)-f(x)\right| \rightarrow 0, \quad n \rightarrow \infty
$$
Proof. Let us first prove the theorem in a special case that $\sigma \in C^{\infty}(\mathbb{R}) .$ Since $\sigma \in C^{\infty}(\mathbb{R})$, it follows that for every $\omega, b$,
$$
\frac{\partial}{\partial \omega_{j}} \sigma(\omega \cdot x+b)=\lim _{n \rightarrow \infty} \frac{\sigma\left(\left(\omega+h e_{j}\right) \cdot x+b\right)-\sigma(\omega \cdot x+b)}{h} \in \bar{\Sigma}_{d}(\sigma)
$$
for all $j=1, \ldots, d$.

By the same argument, for $\alpha=\left(\alpha_{1}, \ldots, \alpha_{d}\right)$
$$
D_{\omega}^{\alpha} \sigma(\omega \cdot x+b) \in \bar{\Sigma}_{d}(\sigma)
$$
for all $k \in \mathbb{N}, j=1, \ldots, d, \omega \in \mathbb{R}^{d}$ and $b \in \mathbb{R}$.

Now
$$
D_{\omega}^{\alpha} \sigma(\omega \cdot x+b)=x^{\alpha} \sigma^{(k)}(\omega \cdot x+b)
$$
where $k=|\alpha|$ and $x^{\alpha}=x_{1}^{\alpha_{1}} \cdots x_{d}^{\alpha_{d}}$. Since $\sigma$ is not a polynomial there exists a $\theta_{k} \in \mathbb{R}$ such that $\sigma^{(k)}\left(\theta_{k}\right) \neq 0$. Taking $\omega=0$ and $b=\theta_{k}$, we thus see that $x_{j}^{k} \in \bar{\Sigma}_{d}(\sigma) .$ Thus, all polynomials of the form $x_{1}^{k_{1}} \cdots x_{d}^{k_{d}}$ are in $\bar{\Sigma}_{d}(\sigma)$. This implies that $\bar{\Sigma}_{d}(\sigma)$ contains all polynomials. By Weierstrass's Theorem [stone1948generalized] it follows that $\bar{\Sigma}_{d}(\sigma)$ contains $C(K)$ for each compact $K \subset \mathbb{R}^{n} .$ That is $\Sigma_{d}(\sigma)$ is dense in $C\left(\mathbb{R}^{d}\right) .$

Now we consider the case that $\sigma$ is only Riemann integrable. Consider the mollifier $\eta$
$$
\eta(x)=\frac{1}{\sqrt{\pi}} e^{-x^{2}}
$$
Set $\eta_{\epsilon}=\frac{1}{\epsilon} \eta\left(\frac{x}{\epsilon}\right) .$ Then consider $\sigma_{\eta_{\epsilon}}$
$$
\sigma_{\eta_{\epsilon}}(x):=\sigma * \eta_{\epsilon}(x)=\int_{\mathbb{R}} \sigma(x-y) \eta_{\epsilon}(y) d y
$$
It can be seen that $\sigma_{\eta_{\epsilon}} \in C^{\infty}(\mathbb{R}) .$ We first notice that $\bar{\Sigma}_{1}\left(\sigma_{\eta_{\epsilon}}\right) \subset \bar{\Sigma}_{1}(\sigma)$, which can be done easily by checking the Riemann sum of $\sigma_{\eta_{\epsilon}}(x)=\int_{\mathbb{R}} \sigma(x-y) \eta_{\epsilon}(y) d y$ is in $\bar{\Sigma}_{1}(\sigma)$.

Following the argument in the beginning of the proof proposition, we want to show that $\left.\bar{\Sigma}_{1}\left(\sigma_{\eta_{\epsilon}}\right)\right)$ contains all polynomials. For this purpose, it suffices to show that there exists $\theta_{k}$ and $\sigma_{\eta_{\epsilon}}$ such that $\sigma_{\eta_{\epsilon}}^{(k)}\left(\theta_{k}\right) \neq 0$ for each $\mathrm{k}$. If not, then there must be $k_{0}$ such that $\sigma_{\eta_{\epsilon}}^{\left(k_{0}\right)}(\theta)=0$ for all $\theta \in \mathbb{R}$ and all $\epsilon>0$. Thus $\sigma_{\eta_{\epsilon}}$ 's are all polynomials with degree at most $k_{0}-1 .$ In particular, It is known that $\eta_{\epsilon} \in C_{0}^{\infty}(\mathbb{R})$ and $\sigma * \eta_{\epsilon}$ uniformly converges to $\sigma$ on compact sets in $\mathbb{R}$ and $\sigma * \eta_{\epsilon}$ 's are all polynomials of degree at most $k_{0}-1 .$ Polynomials of a fixed degree form a closed linear subspace, therefore $\sigma$ is also a polynomial of degree at most $k_{0}-1$, which leads to contradiction.

\section{Properties of polynomials using Fourier transform}
We make use of the theory of tempered distributions (see [strichartz2003guide, stein2016introduction] for an introduction) and we begin by collecting some results of independent interest, which will also be important later. We begin by noting that an activation function $\sigma$ which satisfies a polynomial growth condition $|\sigma(x)| \leq C(1+|x|)^{n}$ for some constants $C$ and $n$ is a tempered distribution. As a result, we make this assumption on our activation functions in the following theorems. We briefly note that this condition is sufficient, but not necessary (for instance an integrable function need not satisfy a pointwise polynomial growth bound) for $\sigma$ to be represent a tempered distribution.

We begin by studying the convolution of $\sigma$ with a Gaussian mollifier. Let $\eta$ be a Gaussian mollifier
$$
\eta(x)=\frac{1}{\sqrt{\pi}} e^{-x^{2}}
$$
Set $\eta_{\epsilon}=\frac{1}{\epsilon} \eta\left(\frac{x}{\epsilon}\right) .$ Then consider $\sigma_{\epsilon}$
$$
\sigma_{\epsilon}(x):=\sigma * \eta_{\epsilon}(x)=\int_{\mathbb{R}} \sigma(x-y) \eta_{\epsilon}(y) d y
$$
for a given activation function $\sigma$.

It is clear that $\sigma_{\epsilon} \in C^{\infty}(\mathbb{R}) .$ Moreover, by considering the Fourier transform (as a tempered distribution) we see that
$$
\hat{\sigma}_{\epsilon}=\hat{\sigma} \hat{\eta}_{\epsilon}=\hat{\sigma} \eta_{\epsilon^{-1}}
$$
We begin by stating a lemma which characterizes the set of polynomials in terms of their Fourier transform. CHAPTER 1. APPROXIMATION PROPERTIES OF NEURAL NETWORK

FUNCTION CLASS

Jinchao $\mathrm{Xu}$

Lemma 1. Given a tempered distribution $\sigma$, the following statements are equivalent:

    \begin{enumerate}
      \item $\sigma$ is a polynomial

      \item $\sigma_{\epsilon}$ given by (1.4) is a polynomial for any $\epsilon>0$.

      \item $\operatorname{supp}(\hat{\sigma}) \subset\{0\}$.

    \end{enumerate}
Proof. We begin by proving that (3) and (1) are equivalent. This follows from a characterization of distributions supported at a single point (see [strichartz2003guide], section 6.3). In particular, a distribution supported at 0 must be a finite linear combination of Dirac masses and their derivatives. In particular, if $\hat{\sigma}$ is supported at 0 , then
$$
\hat{\sigma}=\sum_{i=1}^{n} a_{i} \delta^{(i)} .
$$
Taking the inverse Fourier transform and noting that the inverse Fourier transform of $\delta^{(i)}$ is $c_{i} x^{i}$, we see that $\sigma$ is a polynomial. This shows that (3) implies (1), for the converse we simply take the Fourier transform of a polynomial and note that it is a finite linear combination of Dirac masses and their derivatives.

Finally, we prove the equivalence of (2) and (3). For this it suffices to show that $\hat{\sigma}$ is supported at 0 iff $\hat{\sigma}_{\epsilon}$ is supported at $0 .$ This follows from equation $1.5$ and the fact that $\eta_{\epsilon^{-1}}$ is nowhere vanishing.

As an application of Lemma 1, let us give a simple proof of the following result. The first proof of this result can be found in [leshno1993multilayer] and is summarized in [pinkus1999approximation]. Extending this result to the case of non-smooth activation is first done in several steps in [leshno1993multilayer]. Our contribution is to provide a much simpler argument based on Fourier analysis.

\end{document}


\chapter{Lecture 21 Jun 20th 2018}%
\label{chp:lecture_21_jun_20th_2018}
% chapter lecture_21_jun_20th_2018

\section{Rings (Continued)}%
\label{sec:rings_continued}
% section rings_continued

\subsection{Rings (Continued)}%
\label{sub:rings_continued}
% subsection rings_continued

\begin{note}[Notation]
  Given a ring $R$, to distinguish the difference between multiples in addition and in multiplication, for $n \in \mathbb{N} \, \land \, a \in R$, we write
  \begin{equation*}
    na = \underbrace{a + a + \hdots + a}_{n \text{ times }}
  \end{equation*}
  and
  \begin{equation*}
    a^n = \underbrace{a \cdot a \cdot \hdots \cdot a}_{n \text{ times }}
  \end{equation*}
  respectively. Also, we will define
  \begin{equation*}
    (-n) a = \underbrace{(-a) + (-a) + \hdots + (-a)}_{n \text{ times }}
  \end{equation*}
  and
  \begin{equation*}
    a^{-n} = \left( a^{-1} \right)^n
  \end{equation*}
  if $a^{-1}$ exists.
\end{note}

\begin{note}
  Recall that for a group $G$ and $g \in G$, we have $g^0 = 1$, $g^1 = g$, and $\left(g^{-1}\right)^{-1} = g$. Thus for addition, we have\sidenote{Note that the first $0$ is an integer while the second $0$ is a zero in $R$.}

  \begin{gather*}
    0 \cdot a = 0 \qquad 1 \cdot a = a \\
    -(-a)=a
  \end{gather*}

  Also, by \cref{propo:group_notations}, if $n, m \in \mathbb{Z}$, we have
  \begin{gather*}
    m \cdot a + n \cdot a = (m + n) \cdot a \\
    n(ma) = (nm)a \\
    n(a + b) = na + nb
  \end{gather*}
\end{note}

\begin{propo}[More Properties of Rings]
\label{propo:more_properties_of_rings}
  Let $R$ be a ring and $r, s \in \mathbb{R}$.\marginnote{This is a problem in A4.}
  \begin{enumerate}
    \item If $0$ is the zero of $R$, then $0 \cdot r = 0 = r \cdot 0$; \sidenote{i.e. all the $0$'s are zeros of $R$.}
    \item $-r (s) = - (rs) = r (-s)$;
    \item $(-r)(-s) = rs$;
    \item $\forall m, n \in \mathbb{Z}, \, (mr)(ns) = (mn)(rs)$.
  \end{enumerate}
\end{propo}

\begin{defn}[Trivial Ring]\index{Trivial Ring}
\label{defn:trivial_ring}
A \hlnoteb{trivial ring} is a ring of only one element. In this case, we have $1 = 0$, i.e. the unity is the zero and vice versa.
\end{defn}

\begin{remark}
  If $R$ is a ring with $R \neq \{0\}$, since $r = r \cdot 1$ for all $r \in R$, we have $1 \neq 0$. Otherwise, if $1 = 0$, then $r = r \cdot 1 = r \cdot 0 = 0$, i.e. $R = \{0\}$.
\end{remark}

\begin{eg}\label{eg:direct_product_of_rings}
  Let $R_1, R_2, ..., R_n$ be rings. We define component-wise operation on the product
  \begin{equation*}
    R_1 \times R_2 \times \hdots \times R_n
  \end{equation*}
  as follows:
  \begin{align*}
    (r_1, r_2, ..., r_n) + (s_1, s_2, ..., s_n) &= (r_1 + s_1, r_2 + s_2, ..., r_n + s_n) \\
    (r_1, r_2, ..., r_n)(s_1, s_2, ..., s_n) &= (r_1 s_1, r_2 s_2, ..., r_n s_n)
  \end{align*}
  We can check that $R_1 \times R_2 \times \hdots \times R_n$ is a ring with the zro being $(0, 0, ..., 0)$ and the unity being $(1, 1, ..., 1)$. This set
  \begin{equation*}
    R_1 \times R_2 \times \hdots \times R_n
  \end{equation*}
  is called the \hldefn{direct product} of $R_1, R_2, ..., R_n$.
\end{eg}

\begin{defn}[Characteristic of a Ring]\index{Characteristic}
\label{defn:characteristic_of_a_ring}
  If $R$ is a ring, we define the \hlnoteb{characteristic} of $R$, denoted by $\ch(R)$, in terms of the order of $1_R$ in the additive group $(R, +)$, by
  \begin{equation*}
    \ch(R) = \begin{cases}
      n & \text{if } o(1_R) = n \in \mathbb{N} \text{ in } (R, +) \\
      0 & \text{if } o(1_R) = \infty \text{ in } (R, +)
    \end{cases}
  \end{equation*}
\end{defn}

For $k \in \mathbb{Z}$, we write $kR = 0$ to mean that $\forall r \in R$, $kr = 0$.

By \cref{propo:more_properties_of_rings}, we have
\begin{equation*}
  kr = k (1_R \cdot r) = (k 1_R) \cdot r
\end{equation*}
and so $kR = 0$ if and only if $k 1_R = 0$. Then, since $(R, +)$ is a group, by \cref{propo:properties_of_elements_of_finite_order} and \cref{propo:property_of_elements_of_infinite_order}, it follows that:

\begin{propo}[Implications of the Characteristic]
\label{propo:implications_of_the_characteristic}
  Let $R$ be a ring and $k \in \mathbb{Z}$.\sidenote{This is why we defined $\ch(R) = 0$ if $o(1_R) = \infty$}
  \begin{enumerate}
    \item $\ch(R) = n \in \mathbb{N} \implies \left( kR = 0 \iff n \mid k \right)$
    \item $\ch(R) = 0 \implies \left( kR = 0 \iff k = 0 \right)$
  \end{enumerate}
\end{propo}

\begin{eg}
  Each of $\mathbb{Z}, \mathbb{Q}, \mathbb{R}$ and $\mathbb{C}$ has characteristic $0$. For $n \in \mathbb{N}$ with $n \geq 2$, the ring $\mathbb{Z}_n$ has characteristic $n$.
\end{eg}

% subsection rings_continued (end)

\subsection{Subring}%
\label{sub:subring}
% subsection subring

\begin{defn}[Subring]\index{Subring}
\label{defn:subring}
A subset $S$ of a ring $R$ is a subring if $S$ is a ring itself (under the same operations: addition and multiplication).\marginnote{Unlike subgroups, since there is no proper suggestion of a symbolic representation, I shall use $S \leq_r R$ to denote that $S$ is a subring of $R$, in comparison to $\leq$ for subgroups, which has no subscript. Note that this is purely for keeping my writing succinct, and so the subscript $r$ is used simply to indicate that the $\leq$ symbol is for denoting a subring and should not be confused with other $r$'s that may be used in a proof. This notation is also not used in class, and should be avoided during materials outside of this set of notes.}
\end{defn}

Note that properties (2), (3), (7) and (9) from \cref{defn:ring} are automatically satisfied. Thus, to show that $S$ is a subring, it suffices to show the following:

\hldefn{Subring Test}\label{spe:subring_test}
\begin{enumerate}
  \item $0, 1 \in S$ \sidenote{The $0 \in S$ is certainly not necessary to be shown, since from part $(2)$ we would have $s \in S \implies 0 \in ( s - s ) \in S$.}
  \item $s, t \in S \implies ( s - t ), st \in S$
\end{enumerate}

\begin{eg}
  We have the following chain of commutative rings:
  \begin{equation*}
    \mathbb{Z} \leq_r \mathbb{Q} \leq_r \mathbb{R} \leq_r \mathbb{C}
  \end{equation*}
\end{eg}

\begin{eg}
  If $R$ is a ring, the \textcolor{base16-eighties-blue}{center}\index{Center of a Ring} $Z(R)$ of $R$ is defined as
  \begin{equation*}
    Z(R) = \{ z \in R : zr = rz, r \in R \}.
  \end{equation*}
  Note taht $0, 1 \in Z(R)$. Also, if $s, t \in Z(R)$, then $\forall r \in R$,
  \begin{equation*}
    (s - t) r = sr - tr = rs - rt = r( s - t )
  \end{equation*}
  and so $(s - t) \in Z(R)$. Also,
  \begin{equation*}
    (st)r = s(tr) = s(rt) = (sr)t = (rs)t = r(st)
  \end{equation*}
  and so $st \in Z(R)$. By the \hlnotea{Subring Test}, $Z(R) \leq_r R$.
\end{eg}

\begin{eg}
  Let
  \begin{equation*}
    \mathbb{Z}[c] = \{a + bi : a, b \in \mathbb{Z}, \, i^2 = -1 \} \subseteq \mathbb{C}.
  \end{equation*}
  It can be shown that $\mathbb{Z}[i] \leq_r \mathbb{C}$, and is called the ring of \hldefn{Gaussian integers}.\sidenote{Proof that the Gaussian integers is a subring is in A4, which shall be included after the assignment is over.}
\end{eg}

% subsection subring (end)

% section rings_continued (end)

% chapter lecture_21_jun_20th_2018 (end)


\documentclass[jou]{apa6}

\usepackage[american]{babel}

\usepackage{csquotes}
\usepackage[style=apa,sortcites=true,sorting=nyt,backend=biber]{biblatex}
\DeclareLanguageMapping{american}{american-apa}
\addbibresource{bibliography.bib}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Discrete Structures
%% The start of RBS stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Working internal and external links in PDF
\usepackage{hyperref}
% Extra math symbols in LaTeX
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{amssymb}
% Enumerations with (a), (b), etc.
\usepackage{enumerate}
\usepackage{xcolor}

\let\OLDitemize\itemize
\renewcommand\itemize{\OLDitemize\addtolength{\itemsep}{-6pt}}

\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=3pt \partopsep=3pt }
\makeatother

% These sizes redefine APA for A4 paper size
\oddsidemargin 0.0in
\evensidemargin 0.0in
\textwidth 6.27in
\headheight 1.0in
\topmargin -24pt
\headheight 12pt
\headsep 12pt
\textheight 9.19in

\title{Sample Quiz 8}
\author{Discrete Structures, Spring 2020}
\affiliation{RBS}

\leftheader{Discrete Quiz 10}

\abstract{%
}

%\keywords{}

\setlength\parindent{0pt}

\begin{document}

%\thispagestyle{empty}

\twocolumn
\section{Quiz 10: Binary Relations}

\vspace{6pt}
{\bf Question 1}\\
Some people participate in an Einkaufshelden program \textendash{} they
go to one of the shops $A$, $B$ or $C$ and deliver the products to the endpoints
$X$, $Y$ or $Z$. Each of the $9$ edges is selected by the same probability
(Figure~\ref{fig:k33-weights})

\begin{figure}[!htb]
\center{\includegraphics[width=2in]{quiz-10/k33-weights.png}}
\caption{\label{fig:k33-weights} The Paths of a Delivery Service.}
\end{figure}

The sum of the numbers on both ends of an edge shows
how many kilograms of stuff were delivered. (For example, if your Student ID has
$A=0$, then on $AZ$ there are $0+7 = 7$ kilograms.
Let $X$ denote the random variable: the kilograms of stuff delivered
on a single edge.

Write the variance $V(X)$ as an irreducible fraction {\tt P/Q}.

\vspace{6pt}
{\bf Question 2.}\\
Given a set of the first positive integers $S = \{ 1,2,\ldots,A+B+(10-C) \}$
(where {\tt A,B,C} are the digits from your student ID).
We define a relation on $S$: $aRb$ is true iff $|a - b| \leq 2$.

Let $R^2$ be the second power of that relation $R$ and let $M_{R^2}$
be its matrix. Find the number of 1s in this matrix (In other words:
how many pairs belong to this relation?)

\vspace{6pt}
{\bf Question 3.}\\
Define a set $S$ of these six positive integers:
\begin{align}
S = \{ & 1+\mathtt{A},\; 2+\mathtt{A}+\mathtt{B},\; 3+\mathtt{A}+\mathtt{B}+\mathtt{C},\;
4 + 2\mathtt{A}+\mathtt{B}+\mathtt{C},\; \nonumber \\
 & 5 + 2\mathtt{A}+2\mathtt{B}+\mathtt{C},\;
6 + 2\mathtt{A}+2\mathtt{B}+2\mathtt{C}\}. \nonumber
\end{align}

Now compute the remainders of the elements of $S$ when divided by $16$.
You should get another set $S'$ where each element is between $0$ and $15$.
($S'$ may contain fewer elements than $S$, if some remainders are identical.)
Let $b_i$ be a sequence of bits ($i = 0,\ldots,15$):
$$b_i = 1\;\text{iff}\;i \in S'.$$

We define a matrix for relation $R$ as follows:
$$M_R = \left( \begin{array}{cccc}
b_{0} & b_1 & b_2 & b_3 \\
b_{4} & b_5 & b_6 & b_7 \\
b_{8} & b_9 & b_{10} & b_{11} \\
b_{12} & b_{13} & b_{14} & b_{15} \\
\end{array} \right)$$
Let $M^{\ast}$ be the matrix of the transitive closure of $R$.
Find the number of 1s in the matrix $M^{\ast}$.

\vspace{6pt}
{\bf Question 4.}\\
The size of a set $S$ is computed from your ID:
$$|S| = \mathtt{A}+\mathtt{B}+(10 - \mathtt{C}).$$
Let $N$ be the number of binary relations on $S$ that are
reflexive and symmetric at the same time.
Write the last $3$ digits of $N$ in your answer.

{\em Hint} If you need to find the last $3$ digits of some large number, you
can use periodicity (similar to this: \url{https://bit.ly/33NrJKI}) or Euler's theorem
(see \url{https://bit.ly/33PyI5Q}) or Chinese Remainder theorem.

\vspace{6pt}
{\bf Question 5.}\\
Find the join of the 3-ary relation:
\begin{verbatim}
{ (Wages,MS410,N507),
  (Rosen,CS540,N525),
  (Michaels,CS518,N504),
  (Michaels,MS410,N510) }
\end{verbatim}
and the 4-ary relation:
\begin{verbatim}
{ (MS410,N507,Monday,6:00),
  (MS410,N507,Wednesday,6:00),
  (CS540,N525,Monday,7:30),
  (CS518,N504,Tuesday,6:00),
  (CS518,N504,Thursday,6:00) }
\end{verbatim}
with respect to the last two fields of the first relation and
the first two fields of the second relation.

Write the number records in the join.

\vspace{6pt}
{\bf Question 6.}\\
Let $R$ be a relation on the set $\{x_1,x_2,x_3\}$ that is reflexive and transitive, but not antisymmetric.
Denote its matrix by
$$M_R =  \left( \begin{array}{ccc}
b_{11} & b_{12} & b_{13} \\
b_{21} & b_{22} & b_{23} \\
b_{31} & b_{32} & b_{33} \\
\end{array} \right)$$

If there are multiple answers, write the lexicographically first one.
Write all the 9 bits (as a sequence of 0s and 1s) in your answer: $b_{11}b_{12}b_{13}b_{21}b_{22}b_{23}b_{31}b_{32}b_{33}$.\\
{\footnotesize
\textcolor{red}{The original formulation did not have the lexicographical ordering requirement. Therefore,
any matrix matching the conditions is fine.}
}

\vspace{6pt}
{\bf Question 7.}\\
Let $R$ be a relation on $\{a, b, c\}$ that is reflexive and transitive, but not symmetric.
Denote its matrix by
$$M_R =  \left( \begin{array}{ccc}
b_{11} & b_{12} & b_{13} \\
b_{21} & b_{22} & b_{23} \\
b_{31} & b_{32} & b_{33} \\
\end{array} \right)$$

If there are multiple answers, write the lexicographically first one.
Write all the 9 bits (as a sequence of 0s and 1s) in your answer: $b_{11}b_{12}b_{13}b_{21}b_{22}b_{23}b_{31}b_{32}b_{33}$.\\
{\footnotesize
\textcolor{red}{The original formulation did not have the lexicographical ordering requirement. Therefore,
any matrix matching the conditions is fine.}
}

\newpage

\subsection{Answers}

In solutions we assume that $A = 4$, $B = 5$, $C = 6$.

\vspace{6pt}
{\bf Question 1.} Answer: {\tt 7} (if $A = 4$, $B = 5$, $C = 6$)\\

Figure~\ref{fig:k33-weights-456} shows all the weight combinations in this case.
The list of total weights on all $9$ edges is the following:
$$x_1 = 9,\;10,\;11,\;10,\;11,\;12,\;11,\;12,\;x_9 = 13.$$
The mean value is $11$. Variance $V(X)$ is the arithmetic mean of all squared deviations from that mean:
$$V(X) = \frac{\sum_{i=1}^9 (x_i - 11)^2}{9} = \frac{12}{9} = \frac{4}{3}.$$

Therefore $m+n = 4+3 = 7$.

\begin{figure}[!htb]
\center{\includegraphics[width=1.6in]{quiz-10/k33-weights-456.png}}
\caption{\label{fig:k33-weights-456} Delivery Service (specific values).}
\end{figure}

\vspace{6pt}
{\bf Question 2.} Answer: {\tt 97} (if $A = 4$, $B = 5$, $C = 6$)\\
$S = \{ 1,2,\ldots,A + B + (10-C) \} = \{ 1,2,\ldots,13 \}$,\\
The relation $R^2$ on any two $a,b \in S$ holds, iff
the distance between $a,b$ is no more than $4$.
This follows from the ``triangle inequality''.

Namely, $a(R^2)b$ holds, if there exists $c$ that $aRc$ and $cRb$.
Therefore $|a-c| \leq 2$ and $|c-b| \leq 2$ and we should have
$|a - b| \leq 4$. The matrix of $R^2$ has $13 \times 13$ entries.
Out of these entries the main diagonal (where $a=b$) and also
other diagonal lines that run parallel to that contain $1$s.
The total number of $1$'s is
$$9 + 10 + 11 + 12 + 13 + 12 + 11 + 10 + 9 = 97.$$

\vspace{6pt}
{\bf Question 3.} Answer: {\tt 16} (if $A = 4$, $B = 5$, $C = 6$)\\
Plug in the numbers $A,B,C$:
$$S = \{ 5, 11, 18, 23, 29, 36 \},$$
$$S' = \{ 5, 11, 2, 7, 13, 4 \},$$
$$M_R = \left( \begin{array}{cccc}
0 & 0 & 1 & 0 \\
1 & 1 & 0 & 1 \\
0 & 0 & 0 & 1 \\
0 & 1 & 0 & 0 \\
\end{array} \right).$$

This matrix only contains $6$ elements equal to $1$, but its transitive
closure is all $16$ elements equal to $1$. Indeed, if we consider the relation $R$
on a set of four elements: $A = \{ a_1, a_2, a_3, a_4 \}$, then there is a cyclical path:
$$a_1Ra_3,\;a_3Ra_4,\;a_4Ra_2,\;a_2Ra_1.$$
Therefore the transitive closure is a relation consisting of all pairs of elements $A \times A$.

\vspace{6pt}
{\bf Question 4.} Answer: {\tt 544} (if $A = 4$, $B = 5$, $C = 6$)\\
We get $|S| =  A + B + (10 - C) = 13$.
The matrix of any binary relation $R$ defined on $S$ has $13 \times 13 = 169$ elements.
Since $R$ is reflexive, all the elements on the main diagonal of that matrix are equal to $1$.
Moreover, any elements above the diagonal are symmetric to some element below the main diagonal.
Ultimately, we can only choose $(169 - 13)/2 = 78$ elements in the matrix.
Each element can have two values, therefore $N = 2^{78}$.

The last three digits in that number are $544$.
Indeed,
$$\left\{ \begin{array}{l}
2^{78} \equiv 0\; (\text{mod}\; 8), \\
2^{78} \equiv 44\;(\text{mod}\; 125). \\
\end{array} \right.$$

The first congruence is obvious (any large power of $2$ is divisible by $8$).
The second one follows from
\begin{align}
2^{78} & = 2^{64} \cdot 2^{8} \cdot 2^{4} \cdot 2^{2} \equiv  \nonumber \\
  & \equiv 116 \cdot 6 \cdot 16 \cdot 4 = \nonumber \\
  & = 44544 \equiv 44 \;(\text{mod}\; 125) \nonumber
\end{align}

\vspace{6pt}
{\bf Question 5.} Answer: {\tt 5}\\

{\footnotesize
\begin{tabular}{|l||ll||ll|} \hline
{\bf Table1} & {\bf Joined} &  & {\bf Table2} & \\ \hline
(T1.L1) Wages & MS410 & N507 & (T2.L1) Mon & 6:00 \\ \hline
(T1.L1) Wages & MS410 & N507 & (T2.L2) Wed & 6:00 \\ \hline
(T1.L2) Rosen & CS540 & N525 & (T2.L3) Mon & 7:30 \\ \hline
(T1.L3) Michaels & CS518 & N504 & (T2.L4) Tue & 6:00 \\ \hline
(T1.L3) Michaels & CS518 & N504 & (T2.L5) Thu & 6:00 \\ \hline
\end{tabular}
}

In this table the middle section shows the two joined columns.
The left section shows the lines from Table 1 (with respective line numbers
in that table). The right section shows the lines from Table 2 (also with line numbers).

\vspace{6pt}
{\bf Question 6.} Answer: {\tt 100011011}\\
We know that the relation is reflexive, so all the elements on its diagonal are $1$
($b_{11} = b_{22} = b_{33} = 1$).
It must not be antisymmetric, so there must be $x \neq y$ such that $xRy$ and $yRx$.
Since we want to have the lexicographically first matrix, we set the entries $b_{23}$ and
$b_{32} = 1$. The matrix is now as follows:

$$M_R =  \left( \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 1 \\
0 & 1 & 1 \\
\end{array} \right)$$

We can easily verify that the relationship $R$ is also transitive (in fact, it is an
equivalence relationship with two equivalence classes $\{x_1\}$ and
$\{x_2,x_3\}$).

\vspace{6pt}
{\bf Question 7.} Answer: {\tt 100010011}\\
We know that the relation is reflexive, so all the elements on its diagonal are $1$
($b_{11} = b_{22} = b_{33} = 1$).
It must not be symmetric, so we can set $b_{23} \neq b_{32}$. (In order to be
the lexicographically first, we do not want to touch earlier entries in that matrix.)

$$M_R =  \left( \begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 1 & 1 \\
\end{array} \right)$$

\end{document}


\section{Introduction}
Modern advancements in artifical neural networks (ANNs) have showed great potential as a viable solution for a wide variety of problems such as image and speech recognition, prediction and classification.

By building complex networks of interconnected neurons, the Hopfield network is able to store and recall multiple patterns, and is therefore an important model for understanding the mechanism behind associative memory for humans and animals. While the Hopfield network may not be the most used in the field of ANNs, use-cases do exist such as image reconstruction.

As humans we have the ability to recall memories from partial information, such as from incomplete images or by observing similar patterns. An apple is an apple, independent of its color or excact shape. The Hopfield Network may help us understand how it is possible to either restore lost information from partial inputs, or how we the brain is able to recall memories by association.

By borrowing the concept of energy and work from physics, we will create a physical interpretation of the Hopfield network and use this to explain the otherwise complex dynamics. We will investigate the dynamics and stability properties of the Hopfield network, using both numerical and analytical methods. We show how storing memories creates local equilibriums where the network remains stable, allowing the network to restore multiple memories using a predefined set of network weights based on Hebbian learning. We then investigate how each stored pattern coincides with two equilibrium points.


\documentclass[11pt]{article}

\setlength{\oddsidemargin}{-0.25 in}
\setlength{\evensidemargin}{-0.25 in}
\setlength{\topmargin}{-0.9 in}
\setlength{\textwidth}{7.0 in}
\setlength{\textheight}{9.0 in}
\setlength{\headsep}{0.75 in}
\setlength{\parindent}{0.3 in}
\setlength{\parskip}{0.1 in}
\usepackage{epsf}
\usepackage{pseudocode}
\usepackage[shortlabels]{enumitem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{color}
\usepackage[normalem]{ulem}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{wrapfig}
\pagenumbering{arabic}
\def\O{\mathop{\smash{O}}\nolimits}
\def\o{\mathop{\smash{o}}\nolimits}
\newcommand{\e}{{\rm e}}
\newcommand{\R}{{\bf R}}
\newcommand{\Z}{{\bf Z}}

%% display solutions or not
\newif\ifsol
% \soltrue % comment out to hide solutions

\title{Section 10: Bayes' Nets}
\date{}
\author{CS 182 - Artificial Intelligence}
\begin{document}
\maketitle

\renewcommand{\labelenumii}{\arabic{enumii}.}
\setlength{\parindent}{0pt}

\textbf{Bayes' nets:} are a technique for describing large complex joint distributions using simpler local conditional distributions and a model that relates those distributions. This is more generally called graphical models and the big ideas is that large joint distributions can be implicitly defined by a graph and a set of local conditional probability tables. This saves space as a joint distribution over $N$ variables is size $O(2^N)$ whereas an $N$ node Bayes net with at most $k$ parents per node is size $O(N \cdot 2^{k+1})$.

\textbf{Components:}
\begin{itemize}
  \item \textbf{Nodes}: one per variable (with domains) and can be assigned (observed) or unassigned (unobserved)
  \item \textbf{Bayes' Net}: A directed, acyclic graph connecting nodes which define all conditional interactions
  \item \textbf{Conditional Probability Table} for each node given all of the parents (see image on left below)
\end{itemize}
\begin{figure}[ht]
    \centering
    \includegraphics[width=0.25\textwidth]{figs/bayes1.png}
    \includegraphics[width=0.25\textwidth]{figs/bayes2.png}
    \includegraphics[scale=0.4]{figs/carrepair}
    \caption{Conditional implications example (left), example conditional probability tables (center), and example bayes net for car repair (right)}
    \label{fig:bayes}
\end{figure}

\textbf{Bayes' Nets encode conditional independence.} Just like how the Markov assumption allowed us to simplify equations based on conditional independence across time, the topology of Bayes' Nets can allow us to also assume conditional independence given various pieces of evidence (observed variables). \textbf{D-Separation} is one powerful and simple procedure for determining if $X_i$ and  $X_j$ are conditionally independent given other variables:
\begin{itemize}
    \item Check all undirected paths between $X_i$ and $X_j$
    \item If one or more paths active, then independence not guaranteed (a path is active if each triple is active). Otherwise (i.e. if all paths are inactive), then independence is guaranteed. See Figure~\ref{fig:triples} for the list of active and inactive triples to analyze on each path.
\end{itemize}

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.6\textwidth]{figs/triples.png}
    \caption{Active and inactive triples for D-Separation. Nodes in grey are assigned. Canonical names for active triples are common effect (left), common cause (bottom right), and causal chain (top right).}
    \label{fig:triples}
\end{figure}

\textbf{Inference} is the process of calculating some resulting quantity from a joint distribution (such as a posterior probability $P(X|E_1=e_1,E_2=e_2)$, or most likely explanation $argmax_x \; P(X=x|E_1=e_1,E_2=e_2)$).
\begin{itemize}
    \item \textbf{Enumeration} is the process of creating the full joint distribution over all of the variables and summing out any unneeded variables. For example if you wanted to determine the probability of the variable $Q$ given evidence $E_1=e_1 \ldots E_k = e_k$ but that also related to other variables $H_1 \ldots H_r$ (canonically called ``hidden variables'') you could:
    \begin{enumerate}
        \item Multiply all factors to construct the full joint distribution:
        $$P(Q, H_1, \dots H_r, E_1, \dots, E_k)$$
        \item Sum out all $H_i$ to get joint of query and evidence (selecting rows consistent with the evidence):
        $$P(Q, e_1 \cdots e_k) = \sum_{h_1 \cdots h_r} P(Q, h_1 \cdots h_r, e_1 \cdots e_k)$$
        \item Normalize across all $Q$:
        $$P(Q=q_k | e_1 \cdots e_k) = \frac{P(Q=q_k, e_1 \cdots e_k)}{\sum_{q_i} P(Q=q_i, e_1 \cdots e_k)}$$
    \end{enumerate}
    This proves to be slow as you have to join up the whole joint distribution over all $Q,H,E=e$ before you sum out the hidden variables.
    \item \textbf{Variable Elimination} is a (usually) faster approach (although still NP-Hard) which interleaves joining and marginalizing over the hidden variables. It relies on computing \textbf{factors} which are simply CPTs where some of the variables are observed (selected, known). The process starts with the initial factors, aka the CPTs selecting the known rows just like in enumeration. However instead of forming the full joint distribution we do (see Figure~\ref{fig:enumvselim} for a high level schematic difference):
    \begin{enumerate}
        \item Pick a hidden variable $H_k$
        \item Join all factors mentioning $H_k$ by forming joint distributions:\\
        \includegraphics[width=0.8\textwidth]{figs/join.png}
        \item Eliminate (sum out) $H_k$ by computing the marginal distribution:\\
        \includegraphics[width=0.4\textwidth]{figs/marginalize.png}
        \item Repeat until all hidden factors are removed. Then do a final join with anything that is left over and normalize!
    \end{enumerate}
    \begin{figure}[ht]
        \centering
        \includegraphics[width=0.6\textwidth]{figs/enumvselim.png}
        \caption{Summary of enumeration vs variable elimination.}
        \label{fig:enumvselim}
    \end{figure}
\end{itemize}

\textbf{Approximate Inference via Sampling}: both Enumeration and Variable Elimination are methods of exact inference (aka you compute the exact answer), but that can be hard as mentioned above. Sampling can be computationally cheaper and provide a very good approximation in many cases (with theoretical guarantees as the number of samples goes to $\infty$). There are a variety of ways to sample:
\begin{itemize}
    \item \textbf{Prior Sampling} is the simplest approach where one simply samples from the CPTs to get a list of samples. Then one can simply use this list to compute approximate probabilities via counting. This is very fast but does not take into account the inference problem we are trying to solve and is simply trying to sample the full joint distribution.
    \item \textbf{Rejection Sampling} modifies Prior Sampling by rejecting any samples that are irrelevant for our inference problem. This allows us to only have to store in memory and compute over the relevent samples. For example if we condition on $E_1 = 0$ (e.g., we are trying to compute $P(Q|E_1 = 0)$) then all samples with $E_1 \neq 0$ can be rejected. Unfortunately, this may reject many many samples.
    \item \textbf{Likelihood Weighting} reduces the number of wasted samples by fixing the evidence variables and then sampling the rest of the distribution. Importantly (to remain consistent) it weights these samples by their likelihood of occurring. Note: evidence only effects ``downstream'' variables from it.
    $$w(Q,E_1 = e_1) = \prod P(e_1 | \text{Parents}(E)) $$
    \item \textbf{Gibbs Sampling} improves Likelihood Weighting by constructing weights that take into account both upstream and downstream evidence through a process known as Markov Chain Monte Carlo (MCMC). You don't need to know this in detail.
\end{itemize}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Exercises:}
\begin{enumerate}
    \item Given the following Bayes' Net and CPT, calculate the joint distribution table:\\
    \includegraphics[scale=0.25]{figs/conditional.png} \quad \quad \quad \quad \quad \quad
    \ifsol
        \includegraphics[scale=0.25]{figs/joint.png}
    \fi
    \item Given this Bayes' Net, answer the following questions: \\
    \includegraphics[width=0.18\textwidth]{figs/net1}
    \begin{enumerate}[a)]
        \item  Are A and B conditionally independent, given D and F?\\
            \ifsol
                {\color{blue} No due to common effect (long one)}
            \else
                \vspace{1pt}
            \fi
        \item  Are A and B conditionally independent, given C?\\
            \ifsol
                {\color{blue} No due to common effect (short one)}
            \else
                \vspace{1pt}
            \fi
        \item  Are D and E conditionally independent, given C?\\
            \ifsol
                {\color{blue} Yes}
            \else
                \vspace{1pt}
            \fi
        \item  Are D and E marginally independent (aka with nothing conditioned on)?\\
            \ifsol
                {\color{blue} No due to common cause}
            \else
                \vspace{1pt}
            \fi
    \end{enumerate}
    \newpage
    \item For this Bayes’ net, we are given the query $P(Y | +z)$. All variables have binary domains. \\
    \includegraphics[scale=0.4]{figs/elimnet}\\
    We can solve this via enumeration by computing:
    $$P(Y|+z) = \sum_t \sum_u \sum_v \sum_w \sum_x P(T)P(U|T)P(V|T)P(W|T)P(X|T)P(Y|V,W)P(+z|X)$$
    However, as we know this is often very slow so instead we will use variable elimination to with the following variable elimination ordering: $X, T, U, V, W$.
    \begin{enumerate}[a)]
        \item After inserting evidence, what are the initial factors?\\
        \ifsol
            {\color{blue} $P(T),P(U|T),P(V|T),P(W|T),P(X|T),P(Y|V,W),P(+z|X)$}
        \else
            \vspace{1pt}
        \fi
        \item Eliminating X generates what new factor $f_1$?\\
        \ifsol
            {\color{blue} $f_1(T, +z) =  \sum_x P (x|T )P (+z|x)$}
        \else
            \vspace{1pt}
        \fi
        \item This leaves us with what factors?\\
        \ifsol
            {\color{blue} $P(T),P(U|T),P(V|T),P(W|T),P(Y|V,W),f_1(T,+z)$}
        \else
            \vspace{1pt}
        \fi
        \item Eliminating T generates what new factor $f_2$?\\
        \ifsol
            {\color{blue} $f_2(U,V,W,+z) =  \sum_t P(t)P(U|t)P(V |t)P(W|t)f_1(t,+z)$}
        \else
            \vspace{1pt}
        \fi
        \item This leaves us with what factors?\\
        \ifsol
            {\color{blue} $P(Y|V,W),f_2(U,V,W,+z)$}
        \else
            \vspace{1pt}
        \fi
        \item Eliminating U generates what new factor $f_3$?\\
        \ifsol
            {\color{blue} $f_3(V,W,+z) = \sum_u f_2(u,V,W,+z)$}
        \else
            \vspace{1pt}
        \fi
        \item This leaves us with what factors?\\
        \ifsol
            {\color{blue} $P(Y|V,W),f_3(V,W,+z)$}
        \else
            \vspace{1pt}
        \fi
        \item Eliminating V generates what new factor $f_4$?\\
        \ifsol
            {\color{blue} $f_4(W, Y, +z) =  \sum_v f_3(v, W, +z)P (Y |v, W )$}
        \else
            \vspace{1pt}
        \fi
        \item This leaves us with what factors?\\
        \ifsol
            {\color{blue} $f_4(W,Y,+z)$}
        \else
            \vspace{1pt}
        \fi
        \item Finally eliminating Y generates what new factor $f_5$?\\
        \ifsol
            {\color{blue} $f_5(Y, +z) =  \sum_w f_4(w, Y, +z)$}
        \else
            \vspace{1pt}
        \fi
        \item How would you obtain $P(Y | +z)$ from the remaining factors?\\
        \ifsol
            {\color{blue} Well we only have one factor left so simply renormalize $f_5(Y, +z)$ to obtain $P (Y | +z)$. $$P(y|+z)= \frac{f_5(y,+z)}{\sum_{y'} f_5(y',+z)}$$}
        \else
            \vspace{2pt}
        \fi
        \item What is the size of the largest factor that gets generated during the above process? Does there exist a better elimination ordering (one which generates smaller largest factors)?\\
        \ifsol
            {\color{blue} $f_2(U, V, W, +z)$, which has $3$ binary variables, has size $2^3 = 8$. Yes, elimination ordering of $X , U , T , V , W$ generates only factors of up to size $2^2 = 4$.}
        \else
            \vspace{2pt}
        \fi
    \end{enumerate}
    \clearpage
    \item Suppose you are given the following Bayes net and conditional probability distributions:
    \begin{figure}[ht]
        \centering
        \includegraphics[width=.1\textwidth]{figs/graphical}
        \includegraphics[width=.7\textwidth]{figs/tables}
    \end{figure}
    This is sufficient to specify a joint probability distribution for the four variables. We want to do approximate inference through sampling. We sample and produce the following samples for $(W_1, I_1, W_2, I_2)$:
    \begin{figure}[h]
        \centering
        \vspace{-1em}
        \includegraphics[width=.55\textwidth]{figs/samples}
        \vspace{-2em}
    \end{figure}
    \begin{enumerate}[a)]
        \item What is $\hat{P}(W_2=R)$, the probability that sampling assigns to the event $W_2 = R$?\\
        \ifsol
            \textcolor{blue}{$W_2 = R$ in 5 of the 10 samples, therefore the probability is 0.5.}
        \else
            \vspace{2em}
        \fi
        \item Cross of samples rejected by rejection sampling if we are computing $P(W_2|I_1=T, I_2=F)$\\
        \ifsol
            \begin{figure}[h]
                \centering
                \vspace{-1em}
                \includegraphics[width=.55\textwidth]{figs/crossed-off}
                \vspace{-1.5em}
            \end{figure}
        \else
            \begin{figure}[h]
                \centering
                \vspace{-1em}
                \includegraphics[width=.55\textwidth]{figs/samples}
                \vspace{-1.5em}
            \end{figure}
        \fi
    \end{enumerate}
    Rejection sampling seems to be wasting a lot of effort, so we decide to use likelihood weighting instead. Assume we generate the following six samples given the evidence $I_1 = T$ and $I_2 = F$:
    \begin{figure}[h!]
        \centering
        \includegraphics[width=.7\textwidth]{figs/samplelist}
    \end{figure}\\
    Recall that in likelihood weighting, we give weight to a sample according to:
    \[
    \prod_{\text{Evidence variables } e} P(e|\text{Parents}(e))
    \]
    \begin{enumerate}[c)]
        \item What is the weight of the first sample $(S,T,R,F)$?\\
        \ifsol
            \textcolor{blue}{The evidence is $I_1 = T, I_2 = F$. The weight of the first sample is therefore:
            $$w = P(I_1=T|W_1=S) \cdot P(I_2=F|W_2=R) = 0.9 \cdot 0.8 = 0.72$$}
            \vspace{-1em}
        \else
            \vspace{2em}
        \fi
    \end{enumerate}
    \begin{enumerate}[d)]
        \item Use likelihood weighting to estimate $\hat{P}(W_2|I_1=T,I_2=F$\\
        \ifsol
            \textcolor{blue}{The sample weights are shown in in the following table:
            \begin{figure}[h!]
            \centering
            \includegraphics[width=.35\textwidth]{figs/soltable}
            \end{figure}\\
            To compute the probabilities, we normalize the weights and find that:
            \begin{equation*}
                \begin{split}
                \hat{P}(W_2=R|I_1=T,I_2=F) &= \frac{0.72+0.16+0.72}{0.72+0.16+0.72+0.09+0.09+0.02} = 0.889 \\    \hat{P}(W_2=S|I_1=T,I_2=F) &= 1-0.889 =  0.111
                \end{split}
            \end{equation*}}
        \else
            \vspace{2em}
        \fi
    \end{enumerate}
\end{enumerate}
\end{document}


\subsection{Regularising the SVC}


\section{How to write well}
In this template we created some commands that provide to a non \LaTeX~ user an easy write without any knowledge about \TeX~or~\LaTeX technologies.

\subsection{Authors}

Fill the authors information. You can delete any of the authors or complementary information on them.

\subsection{Equations}

To use equations you can make them inline like this $x = y + z$ or you can do them like this:

\begin{equation}\label{eq:timehtol}
    \int_{0}^{tp_{HL}}dt = tp_{HL} = -C_L \int_{V_{dd}}^{V_{dd}/2}\frac{1}{I_{DSN}} dV_O
\end{equation}

And you can reference the equation like this \mintinline{Latex}{\ref{eq:timehtol}}, \textbf{e.g} \mintinline{Latex}{equation \ref{eq:timehtol}}, appearing like in the bold text \textbf{equation~\ref{eq:timehtol}}. It will automatically change if you other equations are inserted before, so this way you don't need to worry about equation numbering.

\subsection{Lists}

Making lists is very simple. Check the code bellow that makes the list showed in this section. Understand the code and change to your needs.

\begin{codebox}{Code to make lists}
    \begin{minted}{Latex}
        \begin{itemize}
            \item item 1
            \item group 1
            \begin{itemize}
                \item item 2
                \item item 3
            \end{itemize}
            \item item 4
        \end{itemize}
    \end{minted}
\end{codebox}

\begin{itemize}
    \item item 1
    \item group 1
    \begin{itemize}
        \item item 2
        \item item 3
    \end{itemize}
    \item item 4
\end{itemize}

\subsection{Images}

Images are placed like the example bellow. You can tune the width to make it fit your needs, it can take any units (cm, in, em ...). To reference an image you can follow the same approach as in equations: \mintinline{Latex}{\ref{img:engeniuslogo}}, image~\ref{img:engeniuslogo}.
To change the image just replace the \textbf{url} in \textit{includegraphics}. Don't forget that you can change the label! Is this label that you need to reference when you want to do so.

\begin{figure}[H]
    \begin{center}
        \includegraphics[width=\textwidth/3]{assets/engeniusLogo.png}
        \caption{Engenius Image Example}
        \label{img:engeniuslogo}
    \end{center}
\end{figure}

\subsection{Code}

Raw code can be inserted inline like this \mintinline{python}{print("Hello world!")}, or in a box like the one bellow. Check our code to see how to do it.

\begin{codebox}{Example Code}
    \begin{minted}{C}
        #include <stdio.h>
        int main()
        {
           // printf() displays the string inside quotation
           printf("Hello, World!");
           return 0;
        }
    \end{minted}
\end{codebox}

\subsection{References}

References must be used when you use information from external fonts. There is no shame in referencing everything, it gives you credibility. Your bibliography must be in the file \mintinline{text}{./biblio.bib} structured like the examples already there. To cite something just use \mintinline{Latex}{\cite{name}}, that will show like this \cite{einstein}. The reference will then appear in the references section.

\section{Questions, Bugs and Suggestions}

This template was created by Diogo Correia and João Santos for the Engenius team. This is a port of the template with a few commands to ease the modification of the original template.
Repository \textcolor{blue}{\href{https://https://github.com/dvcorreia/magda-latex-report}{here}}.

If you find any bugs please make us know so we can fix then.
If you have any suggestion or request feel free to request.
If you have any question or you need help just contact us.

All of that can be done in github's issues section at the following link:  \textcolor{blue}{\href{https://github.com/dvcorreia/magda-latex-report/issues/new}{here}}.


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage{color}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{indentfirst}
\usepackage{tcolorbox}
\usepackage[ruled]{algorithm2e}

\usepackage{apacite}

\usepackage{graphicx}
\usepackage{subcaption}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{definition}{Definition}

\title{Robust Single Linkage Algorithm and Extract Flat Clustering}
\author{Taoran Xue }
\date{April 2017}

\usepackage{natbib}
\usepackage{graphicx}

\begin{document}

\maketitle

\section{Instruction}

In unsupervised learning algorithm, there exists two problem to solve. First is that if our data set is infinity, in another word, its size can be increasing in a time, we need a cluster algorithm to fit the distribution density function of the data set. Second, if some bad or noise point occasionally draws between two cluster, it will influence the cluster result different from the original distribution of probability density function.

\section{Minimum spanning tree}

Before we go through today's topic, let's have a quick review of my last presentation about density based hierarchy clustering algorithm. Because we are trying to give a robust single linkage algorithm based on previous method.

\begin{tcolorbox}
For each $i$, set $r(x_i)$ to the distance from $x_i$ to its {\color{red}$k$}th nearest neighbor.

As $r$ grows from $0$ to $\infty$:
\begin{enumerate}
	\item Construct a graph $G_r$ with nodes $\{x_i : r(x_i) \leq r\}$. Include edge $(x_i, x_j)$ if $\|x_i - x_j\| \leq$ {\color{red}$\alpha r$}.
	\item Let $\mathbb{C}_n(r)$ be the connected components of $G_r$.
\end{enumerate}

\end{tcolorbox}

As we know, real data is messy and has corrupt data, and noise. So we need to make a new algorithm with single linkage algorithm and it can be sensitive to noise. As I mentioned, if a single noise data point is in the wrong place can act as a ``thin bridge'' between two cluster, gluing them together.

\begin{figure}[h!]
	\centering
	\includegraphics[scale=0.35]{a.png}
	\caption{``Thin bridge'' effect.}
	\label{fig:slabfig}
\end{figure}

So the authors \cite{chaudhuri2010rates} of previous paper give a method to robustly process against noise, and, in the paper presentation, I proved that this algorithm can deal with noise problem. Their idea is to define a new version of minimal spanning tree.

\begin{tcolorbox}
\begin{definition}
Set $r_k(x_i)$ to the distance to $k$th nearest neighbor. For any $r = \text{max}\{r_k(x_i)\}$, connect points $x_i$ and $x_j$, if $||x_i - x_j|| \leq \alpha r$.
\end{definition}
\end{tcolorbox}

\cite{McInnes2017} convert it to our new definition -- \textbf{mutual reachability distance}:

\begin{tcolorbox}
	\begin{definition}
		Set $core_k(x_i)$ to the distance to $k$th nearest neighbor.
		$$d_{\mathrm{mrd}_k}(x_i, x_j) = \text{max}\{core_k(x_i), core_k(x_j), ||x_i - x_j||\}$$
	\end{definition}
\end{tcolorbox}

As we have mutual reachability distance matrix, we can build the minimum spanning tree very efficiently via Prim's algorithm.

\begin{figure}[h!]
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{dis_figure1.png}
		\caption{}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{dis_figure2.png}
		\caption{}
	\end{subfigure}
	\caption{Mutual reachability distance}
\end{figure}

\section{Single linkage hierarchy algorithm}

Given the minimal spanning tree, the next step is to convert that into the hierarchy of connected components. This is most easily done in the reverse order: sort the edges of the tree by distance (in increasing order) and then iterate through, creating a new merged cluster for each edge.

\section{Condense tree}

Hierarchy cluster algorithm always gets a large and complicated cluster hierarchy tree. So \cite{campello2013density} try to condense down the cluster hierarchy into a smaller tree. In another word, we are trying to make cluster tree pruning to get a simpler cluster tree. As you can see in the hierarchy above it is often a cluster split is one or two points splitting off from a cluster. We need to eliminate cluster which has fewer points than the \textbf{minimum cluster size}.

Initially, we breadth-first search the whole hierarchy tree. When we arrive one cluster, there will be three conditions.

\begin{itemize}
	\item If left child cluster point number is greater than minimum cluster size, but right side is not, Figure \ref{fig:condtree} (a), keep the left branch and ignore right cluster;
	\item If left and right child clusters are both greater than minimum cluster size, Figure \ref{fig:condtree} (b), we consider that a cluster split and let the split persist the whole tree;
	\item If left and right child clusters are both fewer than minimum cluster size, Figure \ref{fig:condtree} (c), we ignore the two cluster.
\end{itemize}

\begin{figure}[h!]
	\centering
	\includegraphics[scale=0.45]{b.png}
	\caption{Condense tree.}
	\label{fig:condtree}
\end{figure}

\begin{algorithm}[H]
	\SetAlgoNoLine
	\SetKwData{minClusterSize}{minClusterSize}
	\SetKwData{numPoints}{numPoints}
	\SetKwData{left}{left}
	\SetKwData{leftCount}{leftCount}
	\SetKwData{right}{right}
	\SetKwData{rightCount}{rightCount}
	\SetKwData{p}{p}
	\SetKwData{tmpNode}{tmpNode}
	\SetKwArray{nodeList}{nodeList}
	\SetKwArray{H}{H}
	\SetKwArray{T}{T}
	\SetKwArray{nextLabel}{nextLabel}
	\SetKwFunction{BFS}{BFS}
	\KwIn{\H{$m$} $\leftarrow$ hierarchy tree contains a tuple of (child1,child2,distance,childrenSize), \minClusterSize $\leftarrow$ The minimum size of clusters to consider}
	\KwOut{\T{$n$} $\leftarrow$ condense tree contains a tuple of (parent,child,$\lambda$,childrenSize)}
	\nodeList $\leftarrow$ \BFS{$H$}\;
	\For{$r \leftarrow 0$ \KwTo $m$}{
		\If{\nodeList{r} is ignored} {Pass\;}
		\left $\leftarrow$ \nodeList{r}.child1\;
		\leftCount  $\leftarrow$ \H{\left}.childrenSize\;
		\right $\leftarrow$ \nodeList{r}.child2\;
		\rightCount  $\leftarrow$ \H{\right}.childrenSize\;
		\If{\leftCount $\geq$ \minClusterSize \textbf{and} \rightCount $\geq$ \minClusterSize}{
			\T{{\p}$++$} $\leftarrow$ (\nextLabel, $++${\nextLabel}, $1/\mathrm{distance}$, \leftCount)\;
			\T{{\p}$++$} $\leftarrow$ (\nextLabel, $++${\nextLabel}, $1/\mathrm{distance}$, \rightCount)\;
		}
		\If{\leftCount $<$ \minClusterSize}{
			\For{\tmpNode \textbf{in} \BFS{\left}}{
				\If{\tmpNode is leaf}{
					\T{{\p}$++$} $\leftarrow$ (\nextLabel, \tmpNode, $1/\mathrm{distance}$, $1$)\;
				}
				Ignore \tmpNode\;
			}
		}
		\If{\rightCount $<$ \minClusterSize}{
			\For{\tmpNode \textbf{in} \BFS{\right}}{
				\If{\tmpNode is leaf}{
					\T{{\p}$++$} $\leftarrow$ (\nextLabel, \tmpNode, $1/\mathrm{distance}$, $1$)\;
				}
				Ignore \tmpNode\;
			}
		}
	}
	\caption{Condense hierarchy cluster tree}
\end{algorithm}

\section{Cluster stability}

For a condense tree, intuitively, we want to extract the cluster that persists and has a long lifetime. It means the cluster is relatively stable unlikely to split into two clusters after it created. So we give a new definition to measure the persistence of a cluster.

\begin{tcolorbox}
	\begin{definition}
		Let $\lambda = \frac{1}{d_{\mathrm{mrd}_k}}$. For each cluster we give $\lambda_{\mathrm{birth}}$ and $\lambda_p$ to be the lambda value when the cluster split off then became it’s own cluster, and the lambda value (if any) when the cluster split into smaller clusters respectively.
	\end{definition}
\end{tcolorbox}

Now, for each cluster $C$ compute the stability to as:

$$ S(C) = \sum_{p \in {C}} (\lambda_p - \lambda_{\mathrm{birth}})$$

\begin{algorithm}[H]
	\SetAlgoNoLine
	\SetKwArray{birthLambda}{birth$\lambda$}
	\SetKwData{currChild}{currChild}
	\SetKwData{prevChild}{prevChild}
	\SetKwData{currLambda}{curr$\lambda$}
	\SetKwData{minLambda}{min$\lambda$}
	\SetKwFunction{Min}{Min}
	\SetKwArray{T}{T}
	\SetKwArray{S}{S}
	\KwIn{\T{$n$} $\leftarrow$ condense tree in reverse topological order contains a tuple of (parent,child,$\lambda$,childrenSize)}
	\KwOut{\S{$n$} $\leftarrow$  stability of every node in condense tree}
	\For{$r \leftarrow 0$ \KwTo $n$}{
		\currChild $\leftarrow$ \T{$r$}.child\;
		\currLambda $\leftarrow$ \T{$r$}.$\lambda$\;
		\eIf{\currChild $=$ \prevChild}{
			\minLambda $\leftarrow$ \Min{\minLambda, \currLambda}\;
		}{
			\birthLambda{\currChild} $\leftarrow$ \minLambda\;
			\prevChild $\leftarrow$ \currChild\;
			\minLambda $\leftarrow$ \currLambda\;
		}
	}
	\For{$r \leftarrow 0$ \KwTo $n$}{
		$\S{r} \leftarrow \S{r} + \T{r}.\lambda - $\birthLambda{\T{r}.$\mathrm{parent}$} $\times \T{r}. \mathrm{childrenSize}$ \;
	}
	\caption{Calculate stability of each cluster}
\end{algorithm}

\section{Flat clustering}

To make hierarchy cluster result to flat cluster result, we require that once the cluster is selected, then the descendant of it cannot be selected. Because hierarchy cluster is nested, selected cluster contains all cluster of its descendant.

\begin{tcolorbox}
	\begin{definition}
		Set $SC(C)$ is the sum of the stabilities of the child cluster of cluster $C$.
		$$SC(C) = \sum_{q \in C}S(c)$$
	\end{definition}
\end{tcolorbox}

\begin{algorithm}[H]
	\SetAlgoNoLine
	\SetKwData{subtreeStabilities}{subtreeStabilities}
	\SetKwData{childList}{childList}
	\SetKwData{tmpNode}{tmpNode}
	\SetKwFunction{Bfs}{Bfs}
	\SetKwArray{S}{S}
	\SetKwArray{L}{L}
	\KwIn{\S{$n$} $\leftarrow$  stability of every node in condense tree sorted in reverse topological order}
	\KwOut{\L{$n$} $\leftarrow$ \textbf{True} if index of cluster is selected; \textbf{False} otherwise}
	\L $\leftarrow$ \{\textbf{True}\}\;
	\For{$r \leftarrow 0$ \KwTo $n$}{
		\childList $\leftarrow$ \{list of node whose parent is $r$\}\;
		\subtreeStabilities $\leftarrow$ $\sum_{c \in \childList} \S{c}$\;
		\eIf{\subtreeStabilities $>$ \S{$r$}}{
			\L{$r$} $\leftarrow$ \textbf{False}\;
			\S{$r$} $\leftarrow$ \subtreeStabilities\;
		}{
			\For{\tmpNode \textbf{in} \BFS{$r$}}{
				\If{\tmpNode $\neq r$}{\L{\tmpNode} $\leftarrow$ \textbf{False}}
			}
		}
	}
	\caption{Abstract cluster with stabilities}
\end{algorithm}

\section{Experiment}

This experiment aims at showing characteristics of this clustering algorithms on datasets that are ``interesting'' but still in 2D. The last dataset is an example of a ``null’' situation for clustering.

\begin{figure}[h!]
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{b_figure_1.png}
		\caption{}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{b_figure_2.png}
		\caption{}
	\end{subfigure}
	\caption{Clusters of blobs}
\end{figure}

\begin{figure}[h!]
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{c_figure_1.png}
		\caption{}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{c_figure_2.png}
		\caption{}
	\end{subfigure}
	\caption{Clusters of circles}
\end{figure}

\begin{figure}[h!]
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{m_figure_1.png}
		\caption{}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{m_figure_2.png}
		\caption{}
	\end{subfigure}
	\caption{Clusters of moons}
\end{figure}

\begin{figure}[h!]
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{r_figure_1.png}
		\caption{}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=\linewidth]{r_figure_2.png}
		\caption{}
	\end{subfigure}
	\caption{Clusters of random}
\end{figure}

\newpage

For high dimensional data, we test our algorithm on UCI datasets and check the cover rate of each dataset.

\begin{center}
	\begin{tabular}{| l | l | l | l | l |}
		\hline
		Datasets & wine & iris & yeast & red wine\\ \hline
		RSLFC & 49.3\% & 89.3\% & 31.1\% & 4.1\% \\ \hline
		K-means & 44.9\% & 66.6\% & 20.3\% & 3.2\%  \\ \hline
		DBSCAN & 0.0\% & 25.1\% & 2.6\% & 0.0\%  \\ \hline
	\end{tabular}
\end{center}

\begin{figure}[h!]
	\centering
	\includegraphics[scale=0.8]{hist1.png}
\end{figure}

\bibliographystyle{apacite}
\bibliography{mybib.bib}

\end{document}