\documentclass{beamer}
\usetheme[sectionpage=none]{metropolis}
\usepackage[utf8]{inputenc} %needed for Umlaute
\usepackage{amsmath, amsfonts, amssymb, amsthm, dsfont}
\usepackage{csquotes}
\usepackage{cleveref}
\usepackage{hyperref}
\usepackage[T1]{fontenc} %8bit font encoding instead of 7bit

\title{Integer programming formulations for pigment sequencing}

\begin{document}
\begin{frame}
  \tableofcontents
\end{frame}

\section{Preliminaries}
\begin{frame}{Preliminaries}
    \begin{itemize}
      \item a set $T = \{0,\ldots,m-1\}$ representing $m$ types
      \item a set $P = \{1,\ldots,n\}$ representing $n$ time periods
  \end{itemize}
  \end{frame}

\section{Simple formulation}
\subsection{Variables}
\begin{frame}{Production variables}
  \begin{itemize}
    \item $x^t_p \in \{0,1\}$ for $t \in T,~p \in P$ with $x^t_p = 1
      \Leftrightarrow$ an item of type $t$ is produced in time period
      $p$
    \end{itemize}
\end{frame}

\begin{frame}{Configuration variables}
    \begin{itemize}
      \item $y^t_p \in \{0,1\}$ for $t \in T,~p \in \{0\} \cup P$ with
        $y^t_p = 1 \Leftrightarrow$ the machine is configured for type
        $t$ in time period $p$
    \end{itemize}
  \end{frame}

  \begin{frame}{Stock variables}
    \begin{itemize}
      \item $s^t_p \in \mathbb{R}_{\geq0}$ for $t \in T,~p \in \{0\}
        \cup P$ with $s^t_p = \ell$ representing that $\ell$ items of
        type $t$ are kept in stock in time period $p$
    \end{itemize}
  \end{frame}

  \begin{frame}{Transition variables}
    \begin{itemize}
      \item $u^{ij}_p \in \{0,1\}$ for $i \in T,~j \in T,~p \in P$
        with $u^{ij}_p = 1$ $\Leftrightarrow$ the production changes
        from type $i$ to type $j$ in time period $p$
    \end{itemize}
  \end{frame}

  \subsection{Constraints}
  \begin{frame}{Initial stock constraints}
    \begin{itemize}
    \item ensure an empty stock for each type before production begins
    \item[]
    \end{itemize}
    \begin{equation}s^{t}_{0} = 0~\text{for}~t \in T\label{stock-cons}\end{equation}
  \end{frame}

  \begin{frame}{Demand constraints}
    \begin{itemize}
      \item ensure that the demand for each type is met by production
        and stock
      \item let $d^t_p \in \{0,1\}$ represent whether an item of type
        $t$ is demanded in time period $p$
      \item[]
    \end{itemize}
    \begin{equation}s^t_{p-1} + x^t_p = d^t_p + s^t_p~\text{for}~t \in T,~p \in P\label{demand-cons}\end{equation}
  \end{frame}

  \begin{frame}{Configuration constraints}
    \begin{itemize}
      \item ensure that the machine is configured correctly when
        producing type $t$ in period $p$
        \item[]
    \end{itemize}
    \begin{equation}x^t_p \leq y^t_p~\text{for}~t \in T,~p \in P\label{state-cons}\end{equation}
  \end{frame}

  \begin{frame}{State constraints}
    \begin{itemize}
    \item ensure that the machine is in exactly one configuration at any time
      \item[]
    \end{itemize}
    \begin{equation}\sum\limits_{t \in T} y^t_p = 1~\text{for}~p \in P\label{config-cons}\end{equation}
  \end{frame}

  \begin{frame}{Transition constraints}
    \begin{itemize}
    \item ensure that the values of the transition variables are set
      correctly
    \item[]
    \end{itemize}
    \begin{equation}u^{ij}_p \geq y^i_{p-1} + y^j_p -1~\text{for}~i \in T,~j \in T,~p \in P\label{transition-cons}\end{equation}
  \end{frame}

  \subsection{Objective}
  \begin{frame}{Simple formulation}
    \begin{itemize}
      \item let $h \in \mathbb{R}$ be the stocking cost representing
        the cost for stocking an item (of any type) for one time
        period
      \item let $c_{ij} \in \mathbb{R}$ for $i \in T,~j \in T$ be the
        transition cost representing the cost for changing the machine
        configuration from type $i$ to type $j$
      \item[]
    \end{itemize}
    \begin{align*}\min \sum\limits_{t \in T} \sum\limits_{p \in P} &h s^t_p + \sum\limits_{i \in T} \sum\limits_{j \in T} \sum\limits_{p \in P} c_{ij} u^{ij}_p\\
      \textbf{s.t.}~~&\eqref{stock-cons} - \eqref{transition-cons}.
    \end{align*}
  \end{frame}

\section{Extended formulation}
\subsection{Additional variables}

\begin{frame}{Predecessor state variables}
  \begin{itemize}
  \item $v^t_p \in \{0,1\}$ for $t \in T,~p \in P \setminus \{0\}$
    with $v^t_p = 1 \Leftrightarrow$ the machine is configured for
    type $t$ in time period $p$ but not in time period $p-1$
  \end{itemize}
\end{frame}

\begin{frame}{Successor state variables}
  \begin{itemize}
  \item $w^t_p \in \{0,1\}$ for $t \in T,~p \in \{0\} \cup P$ with
    $w^t_p = 1 \Leftrightarrow$ the machine is configured for type $t$
    in time period $p$ but not in time period $p+1$
  \end{itemize}
\end{frame}

\end{document}


%!TEX root = paper.tex
\subsection{Solitary wave on a simple beach}
This is an analytic testcase and a laboratory experiment conducted at the California Institute of Technology, Pasadena, California. A linear solitary (better called single) wave propagates over a  constant bathymetry, until it reaches a linear sloping beach, s.t. the wave inundates the dry area and the offtake takes place after the wave has reached the maximum runup point.
The experimental data include the surface elevation measured at different points in time for a non-breaking and a breaking wave. The analytic testcase considers only a non-breaking wave, but yields comparison data in terms of time series as well as data at two locations.
The experimental data serve for validation of the models concerning their ability to model inundation. Additionally to the experimental data, a linear analytic solution is provided, s.t. verification of our models is also tested.

The initial condition is prescribed as a linear analytic solitary wave solution (similar to the one in section \ref{sec:B_compositebeach})
\begin{align}
\xi(\bx,t)&=a \ \text{cosh}^{-2}(K(x-ct-x_0)), \\
u(\bx,t)&=-c\frac{\xi(\bx,t)}{d},
\end{align}
with the initial actual amplitude $a$, propagation velocity $c=\csw$ with scale factor $K=\sqrt{\left(\frac{3a}{4d^3}\right)}$ and displacement $x_0=X_0+L=d \, \text{cot}(\beta)+\text{arccosh}(\sqrt{20})\frac{1}{Kd}$, with $\beta=\text{arccot}(19.85)$. The entire domain length is $L=48 \, \text{m}$. The simulation time is $40$ seconds.

In the analytic testcase, the ratio between amplitude and depth is $\frac{a}{d}=0.019$, whereas the laboratory experiment uses  $\frac{a}{d}=0.0185$.
For breaking waves in the laboratory experiment, the ratio between amplitude and depth is $\frac{a}{d}=0.3$. We choose $d=1$m and $a$ accordingly.
We impose reflecting boundary conditions at the boundary in x-direction and periodic boundary conditions in y-direction. For the setup see figure \ref{fig:simplebeach_setup}.

\begin{figure}[htbp]
\includegraphics[width=\textwidth]{simplebeach_setup}
\caption{Setup of the testcase solitary wave on a simple beach. (Letters do not fit to text decription.)}
\label{fig:simplebeach_setup}
\end{figure}

\subsubsection{Results of \nh\ model}
Figures \eqref{fig:nh_simplebeach_ana_nh_Nhy} display the comparison of the nonlinear SWE with the linear analytic solution. The first five figures present simulation results at required time steps, whereas the sixth figure shows the comparison at two positions over time.
The models results compared to the analytic solution are prone to oszillations, especially close to the shoreline.

Need to improve, and also tested only with Lhy.
Add maximal runup and convergence test.
\begin{figure}[htbp]
\begin{minipage}{\textwidth}
\includegraphics[width=0.48\textwidth]{simplebeach_ana_t=25tau_nh_Nhy}
\includegraphics[width=0.48\textwidth]{simplebeach_ana_t=35tau_nh_Nhy}
\end{minipage} \\
\begin{minipage}{\textwidth}
\includegraphics[width=0.48\textwidth]{simplebeach_ana_t=45tau_nh_Nhy}
\includegraphics[width=0.48\textwidth]{simplebeach_ana_t=55tau_nh_Nhy}
\end{minipage} \\
\begin{minipage}{\textwidth}
\includegraphics[width=0.48\textwidth]{simplebeach_ana_t=65tau_nh_Nhy}
\includegraphics[width=0.48\textwidth]{simplebeach_ana_x_nh_Nhy}
\end{minipage}
\caption{Comparison of the linear analytical (red) sea surface height of the
solitary wave with the simulation results of the \nh\ model in its version of nonlinear shallow water equations (blue). (The x-axis values should be multiplied by (-1), and colors changed.)}
\label{fig:nh_simplebeach_ana_nh_Nhy}
\end{figure}


\documentclass[../main.tex]{subfiles}
		\lstset{
			language=bash,
			basicstyle=\ttfamily
		}

\begin{document}
This is the last part of this project which covers an open-ended optimization of MNIST dataset classification on an FPGA.
For better comparison, the \texttt{Batch size} ($\approx 8k$) of the processed subset is the same as for previous design optimiztations in part 1 and part 2.

\paragraph{Learning}
First, the learning algorithm will be further optimized by introducing the \textit{cross-entropy} as new error function and the \textit{SELU}(Self-normalizing Linear Unit} and \textit{softmax} functions as chained transfer function. The goal is to increase classifiction accuracy to at least $85\si{\percent}$ without increasing the overall hardware usage.
Consequently, the network cannot be extended by another layer of neurons. The output has to be one hot encoded, such that the number of neurons can not be changed either.

Initially the given learning algorithm uses a quadratic error function to calculate the cost after each prediction. But for this particular classification problem, we have two more contraints on the prediction outcome which are ignored, by the current error function:
\begin{itemize}
	\item the prediction outcome should follow the one hot encoding scheme.
	\item the prediction outcome should be a valid probability distribution.
\end{itemize}
Therefore the \textit{cross-entropy} will be used as new error function in combination with the \textit{softmax} function as output transfer function.
The \textit{SELU} function and helps to keep the weigths show a self-normalizing behavior. The two distribution parameters $\lambda$ and $\alpha$ are determined in order to gurarantee this behavior for normalized input values. Therefore the inputs now must be scaled to $[0,1]$.
To improve accuracy and to better fit the weights, the training algorithm is repeated \texttt{ITERATION} times with random permutations of the training set.
The file \texttt{mnist.py} had to be slightly changed to integrate the new learning algorithm. Furthermore extra functionality to plot weight representation and error rate over the training period was added for debugging purposes.
The optimized learning algorithm can be found in the file \texttt{CrossSLP.py}.

\paragraph{Prediction acceleration}
Since softmax and \textit{SELU} only scale the denditric potential of the networks neurons, the following equation still holds:
\begin{align*}
	\text{argmax}(x) = \text{argmax}(\text{softmax}(\text{SELU}(x)))
\end{align*}
As we see from the equation above, the transferfunctions are only required for learning, not for prediction.
In previous implementations in part 1 and 2, we just computed the $\text{argmax}(x)$ of the denditric potentials, which gave us the correct prediction output.
This means even after changing the learning algorithm and its transfer functions, the hardware does not necessarily have to change, because the network structure remains the same.

Nevertheless the harware is also optimized in part 3.
In the previous implementations, the output of the accellerator transmitted back to the cpu was always the whole output vector of the network.
We still had to compute the $\text{argmax}(x)$  on the cpu. First this produces unnecessary transmission overhead, which can be avoided, second the computation of the \textit{argmax} function can also be offloaded. So the harware design was changed accordingly and now the output is only one byte long indicating the predicted number between 0 and 9. Because the output buffer size is reduced by $90\si{\percent}$ more memory can be used for the input buffer, such that the \texttt{TILING} size can be increased to $128$ samples. This also give a small latency speedup.

\begin{lstlisting}
FPGA accuracy: 8.15\% validation error
CPU accuracy:  8.06\% validation error
FPGA time:  0.004762924000033308
CPU time:  0.2810866919999171
FPGA has a 59.02x speedup
(* real speedup is slightly higher, because of argmax offloading)
\end{lstlisting}

The simulation, predicts a lower latency of what can be performed on the FPGA.

\end{document}


\section{Enhancing Forecasting Models with External Data}
\label{enhanced_feats}

In this appendix, we show how the feature matrix in Sub-section
    \ref{ml_models} can be extended with features other than historical order
    data.
Then, we provide an overview of what external data we tried out as predictors
    in our empirical study.

\subsection{Enhanced Feature Matrices}

Feature matrices can naturally be extended by appending new feature columns
    $x_{t,f}$ or $x_f$ on the right where the former represent predictors
    changing throughout a day and the latter being static either within a
    pixel or across a city.
$f$ refers to an external predictor variable, such as one of the examples
    listed below.
In the SVR case, the columns should be standardized before fitting as external
    predictors are most likely on a different scale than the historic order
    data.
Thus, for a matrix with seasonally-adjusted order data $a_t$ in it, an
    enhanced matrix looks as follows:

$$
\vec{y}
=
\begin{pmatrix}
    a_T \\
    a_{T-1} \\
    \dots \\
    a_{H+1}
\end{pmatrix}
~~~~~
\mat{X}
=
\begin{bmatrix}
    a_{T-1}         & a_{T-2} & \dots & a_{T-H}     & ~~~
        & x_{T,A}   & \dots   & x_{B} & \dots \\
    a_{T-2}         & a_{T-3} & \dots & a_{T-(H+1)} & ~~~
        & x_{T-1,A} & \dots   & x_{B} & \dots \\
    \dots           & \dots   & \dots & \dots       & ~~~
        & \dots     & \dots   & \dots & \dots \\
    a_H             & a_{H-1} & \dots & a_1         & ~~~
        & x_{H+1,A} & \dots   & x_{B} & \dots
\end{bmatrix}
$$
\

Similarly, we can also enhance the tabular matrices from
    \ref{tabular_ml_models}.
The same comments as for their pure equivalents in Sub-section \ref{ml_models}
    apply, in particular, that ML models trained with an enhanced matrix can
    process real-time data without being retrained.

\subsection{External Data in the Empirical Study}
\label{external_data}

In the empirical study, we tested four groups of external features that we
    briefly describe here.

\vskip 0.1in

\textbf{Calendar Features}:
\begin{itemize}
    \item Time of day (as synthesized integers: e.g., 1,050 for 10:30 am,
                       or 1,600 for 4 pm)
    \item Day of week (as one-hot encoded booleans)
    \item Work day or not (as booleans)
\end{itemize}

\vskip 0.1in

\textbf{Features derived from the historical Order Data}:
\begin{itemize}
    \item Number of pre-orders for a time step (as integers)
    \item 7-day SMA of the percentages of discounted orders (as percentages):
          The platform is known for running marketing campaigns aimed at
          first-time customers at irregular intervals. Consequently, the
          order data show a wave-like pattern of coupons redeemed when looking
          at the relative share of discounted orders per day.
\end{itemize}

\vskip 0.1in

\textbf{Neighborhood Features}:
\begin{itemize}
    \item Ambient population (as integers) as obtained from the ORNL LandScan
          database
    \item Number of active platform restaurants (as integers)
    \item Number of overall restaurants, food outlets, retailers, and other
          businesses (as integers) as obtained from the Google Maps and Yelp
          web services
\end{itemize}

\vskip 0.1in

\textbf{Real-time Weather} (raw data obtained from IBM's
                            Wunderground database):
\begin{itemize}
    \item Absolute temperature, wind speed, and humidity
          (as decimals and percentages)
    \item Relative temperature with respect to 3-day and 7-day historical
          means (as decimals)
    \item Day vs. night defined by sunset (as booleans)
    \item Summarized description (as indicators $-1$, $0$, and $+1$)
    \item Lags of the absolute temperature and the summaries covering the
          previous three hours
\end{itemize}

\vskip 0.1in

Unfortunately, we must report that none of the mentioned external data
    improved the accuracy of the forecasts.
Some led to models overfitting the data, which could not be regulated.
Manual tests revealed that real-time weather data are the most promising
    external source.
Nevertheless, the data provided by IBM's Wunderground database originate from
    weather stations close to airports, which implies that we only have the
    same aggregate weather data for the entire city.
If weather data is available on a more granular basis in the future, we see
    some potential for exploitation.


\documentclass[letterpaper,12pt,preprint]{hack_aastex}

\input{dfm_stylez}
\pagestyle{myheadings}
\markright{\textsf{\footnotesize %
                   Astro Hack Week /
                   2018 /
                   MCMC exercises by Dan Foreman-Mackey}}

% Single-spacing.
\def\baselinestretch{1.1}

\include{figures/numbers-mh}
\include{figures/numbers-emcee}

\newcommand{\question}{\emph}

\begin{document}

In this exercise session you will implement a simple Metropolis MCMC sampler
in your programming language of choice.
You will test this sampler on a simple toy problem~--~drawing samples from a
two-dimensional Gaussian~--~and then on two more realistic problems.
The exercises are designed to take longer than the allotted 45~minutes so
don't worry if you don't finish.
Hopefully people with all levels of experience will learn something!
If you have previous experience with MCMC and if you have already implemented
a Metropolis sampler, why don't you take this time to implement a more
sophisticated method (ensemble, Hamiltonian, nested, or otherwise), try out a
new programming language, or help out other students with less experience.

\paragraph{Toy problem: a two-dimensional Gaussian}

In the following section you will implement your own Metropolis MCMC sampler
and test it by drawing samples from a two-dimensional Gaussian but the first
step is to implement the probability distribution as a function that takes in
a two-dimensional vector $\theta$ and returns:
\begin{eqnarray}
\ln p(\theta) &=& -\frac{1}{2}\,\theta^\mathrm{T}\,\Sigma^{-1}\,\theta
    + \mathrm{constant}
\end{eqnarray}
where
\begin{eqnarray}
\Sigma &=& \left(\begin{array}{cc}
1.0 & -0.08 \\
-0.08 & 0.01 \\
\end{array}\right)
\end{eqnarray}
Note: you definitely want to compute $\ln p$ (not just $p$).
\question{Why?}

\question{To test your probability function, ensure that the difference
between the function evaluated at two (random) points in parameter space is
the same as what I get.}
For example, make sure that you find the following:
\begin{eqnarray}
\ln p([0.5, -0.1]^\mathrm{T}) - \ln p([-0.01, 0.3]^\mathrm{T}) &\equiv&
    11.80847\ldots \quad.
\end{eqnarray}
If you don't get this result on the first try, debug your function until you
do.

\paragraph{A homegrown Metropolis sampler}

In this section, you will implement a Metropolis MCMC sampler to draw samples
from the distribution defined in the previous section.
As a reminder, the steps in a Metropolis MCMC are described here in words:
\begin{enumerate}

\item Initialize the parameters as $\theta(t=0)$.

\item Propose an update $q = \theta(t) + \sigma\,\delta$ where $\sigma$ is a
tuning parameter and $\delta$ is drawn from a zero mean, unit variance
Gaussian. Alternatively, you can try updating $\theta$ only one dimension.

\item Compute the acceptance probability $r =
\mathrm{min}\left(1,\,\frac{p(q)}{p(\theta(t))}\right)$.

\item Draw $u$ from a uniform distribution between 0 and 1. If $u < r$, accept
the proposal and set $\theta(t+1) = q$ to the chain. Otherwise, reject the
proposal and set $\theta(t+1) = \theta(t)$. Append $\theta(t+1)$ to the chain.
\emph{Note: in every iteration of this procedure a new entry is added to the
chain~--~even if the sample isn't accepted!}

\item Go to step 2 and repeat.

\end{enumerate}
Here are some suggestions to keep in mind for your implementation:
\begin{itemize}

\item In this function, you should track the acceptance fraction.
Keep track of how many proposals you accept.

\item As mentioned above, don't compute or use $p(\theta)$ directly~--~you
should only ever use the logarithm of this function.

\item Your implementation must support parameter \emph{vectors}.
For this problem, we're working in 2-D but don't special case to that because
it might be necessary to be able to sample more parameters later.

\item Similarly, you should implement the sampler in such a way that it is
easy to swap out a different probability function. Most programming languages
allow you to pass function pointers so taking the log-probability function as
an argument is probably the best interface.

\item Finally, to start the sampler, you must choose an initial location in
parameter space. Don't hard-code this location~--~take it as an
argument~--~because we'll test different initialization methods later.

\end{itemize}
\question{Implement the Metropolis MCMC method as a function with a calling
sequence something like}
\begin{center}
\texttt{samples, acc\_frac = metropolis(log\_prob\_func, sigma, inital\_theta,
num\_steps)}
\end{center}
This function should return the chain of samples and the accumulated
acceptance fraction.

\paragraph{Testing, tuning, and convergence (aka time to break your sampler)}

MCMC methods are notoriously hard to test rigorously so instead you can just
test your implementation qualitatively for today.
To start, initialize at a reasonable location (\question{What is a good way to
choose this in general?}) and \question{run some chains and look at a few
diagnostics}
\begin{itemize}

\item What is the acceptance fraction? Is this unreasonably high or low?
Note: this is a very easy problem so the acceptance fraction will
probably be higher than you would get in any problem in The Real
World\textsuperscript{\textsf{TM}}.

\item Compute the sample mean and covariance matrix of the chain. Is this
close to what you would expect? What do you expect and why? What happens as
you run the chain for longer?

\item Plot the trace (value as a function of step number) for each parameter.
See Figure~\ref{fig:traces} for an example of what to expect.

\item Plot the scatterplot matrix or corner plot to visualize the covariances
in the problem.
In Python you can use
\textsf{corner.py}\footnote{\url{http://corner.readthedocs.io}} and other
plotting libraries probably have similar functionality but, otherwise, you can
just make a scatterplot of the chain values.
Figure~\ref{fig:corner} shows an example using \textsf{corner.py}.

\item Try changing $\sigma$ and your initialization and remake these plots to
see what happens.
What happens as you make $\sigma$ extremely large $10^4$ or small $10^{-4}$?
What happens if you move the initial guess far away from the peak of the
distribution?

\end{itemize}

As discussed in lecture, the best quantification of sampler performance is the
integrated autocorrelation time $\tau_\mathrm{int}$.\footnote{Note: there isn't
just \emph{one} autocorrelation time.
$\tau_\mathrm{int}$ will, in general, be different for every different
$f(\theta)$ in Equation~\ref{eq:integral}.}
This provides an estimate of the number of steps of MCMC required to obtain an
independent sample.
Since MCMC is used to compute integrals, an estimate of $\tau_\mathrm{int}$ is
required to compute the sampling uncertainty on the approximation
\begin{eqnarray}\label{eq:integral}
\int f(\theta)\,p(\theta)\,\mathrm{d}\theta &\approx&
    \frac{1}{N} \sum_{n=1}^N f(\theta^{(n)}) \quad
    \mathrm{where}\,\theta^{(n)} \sim p(\theta)
\end{eqnarray}
and the sampler that produces a chain with a smallest value of
$\tau_\mathrm{int}$ at fixed computational cost is the best.

\question{Write or find a function that computes the autocorrelation
function\footnote{If you write your own function, don't directly sum the
variance at each lag~--~this will be way too slow. Instead, you could try
using an FFT.} of a one-dimensional chain and compute this for a few choices
of $f(\theta)$.} Figure~\ref{fig:autocorr} gives a few suggestions for
functions to consider.
\question{Then, find or implement a function that estimates the integrated
autocorrelation time}:
\begin{eqnarray}
\tau_\mathrm{int,est} &\approx& C(0) + 2\,\sum_{\tau=1}^W C(\tau)
\end{eqnarray}
where $C(\tau)$ is the autocorrelation function and $W$ is a tuning parameter.
Note: autocorrelation time estimates are always very noisy and you need to run
a long chain to get a reliable estimate.
Furthermore, the choice of $W$ can be subtle. Try several values of $W$ or
read A.~Sokal's notes\footnote{See pages 15--16 of this note:
\url{http://www.stat.unc.edu/faculty/cji/Sokal.pdf}} on how to choose $W$ more
robustly.
For more information about autocorrelation analysis, including Python code,
take a look at this blog post: \url{https://dfm.io/posts/autocorr/}.

For $\sigma = 0.1$, I find integrated autocorrelation times of \taua~steps and
\taub~steps for $f(\theta)=\theta_1$ and $f(\theta)=\theta_2$ respectively.
\question{Do you find the same? If not, why not?
Try changing $\sigma$ to see how the autocorrelation function and times
change.
Can you find a better choice of $\sigma$? What if you use different values of
$\sigma$ for each parameter? How many tuning parameters are there in this
method?}

\textbf{Bonus:} Try running a different sampling algorithm on the same problem
and compare performance using an estimate of the autocorrelation time.
For example, without any tuning,
\textsf{emcee}\footnote{\url{http://emcee.readthedocs.io}} gets
autocorrelation times of about \etaua~steps for both $f(\theta)=\theta_1$ and
$f(\theta)=\theta_2$.
Can you match that performance using Metropolis? What changes did you have to
make?

\ssfigure{figures/traces.pdf}{0.5}{%
The parameter traces for Metropolis MCMC sampling of a two-dimensional
Gaussian.
The plots show the parameter values as a function of step number in the chain.
To show the autocorrelation on this plot, I zoomed in to only show the first
few steps but the full chain used for the following figures had $4\times 10^5$
steps.
\label{fig:traces}}

\ssfigure{figures/corner.pdf}{0.5}{%
A scatterplot matrix or corner plot of the chain from Figure~\ref{fig:traces}.
The central panel shows the scatterplot of the parameter values from the chain
and the histograms show the corresponding projections.
The scatterplot shows the Monte Carlo estimate of the joint probability
density $p(\theta_1,\,\theta_2\,|\,\mathrm{data})$ and the histograms are
estimates of the marginalized probability densities
$p(\theta_1\,|\,\mathrm{data})$ and $p(\theta_2\,|\,\mathrm{data})$.
\label{fig:corner}}

\ssfigure{figures/autocorr.pdf}{0.5}{%
The autocorrelation function for chains of $\{f_k(\theta^{(n)})\}_{n=1}^N$ for
different choices of $f_k$.
Compare this to Figure~\ref{fig:traces} to see how the autocorrelation relates
to the chain behavior.
Why do the autocorrelation functions have different scales?
Is the sampler well tuned for $f(\theta) = \theta_1$?
\label{fig:autocorr}}

% \ssfigure{figures/ensemble.pdf}{0.5}{%
% The autocorrelation function.
% \label{fig:ensemble}}

\clearpage
\paragraph{A more realistic problem: fitting a line to data}

In this section, you will fit a line to the small data set plotted in
Figure~\ref{fig:data} and available online at
\url{https://github.com/dfm/ahw2018/blob/master/data.txt}\footnote{In this
file, the columns are $x$, $y$, $\sigma_y$, and $\sigma_x$.}.
If you just have data points with Gaussian uncertainties and Gaussian or broad
priors, you probably shouldn't use MCMC (because the posterior can be sampled
analytically) so let's make it a little more interesting: there is an
intrinsic scatter in the relationship and there are uncertainties on the $x$
values.

We'll start by ignoring the $x$ uncertainties but including the intrinsic
scatter parameterized by the logarithm of the scatter $\ln s$.
In this case, the likelihood is:
\begin{eqnarray}
\ln p (y\,|\,m,\,b,\,\ln s) &=& -\frac{1}{2} \sum_{n=1}^N\left[
\left(\frac{y_n-m\,x_n-b}{\sigma^2 + s^2}\right)^2 + \ln(2\,\pi\,(\sigma^2 +
s^2))\right]
\end{eqnarray}
(this derivation is left as an exercise)
and a reasonable choice of ``uninformative'' prior is\footnote{See
J.~VanderPlas's blog for a discussion of this choice:
\url{http://jakevdp.github.io/blog/2014/06/14/frequentism-and-bayesianism-4-bayesian-in-python/}.}:
\begin{eqnarray}
\ln p(m,\,b,\,\ln s) &\propto& -\frac{3}{2}\ln(1+b^2) + B(m,\,b,\,\ln s)
\end{eqnarray}
where $B(m,\,b,\,\ln s)$ is a finite constant when $m$, $b$, and $\ln s$ take
values in a ``reasonable range'' (How should you choose this in general?) and
$-\infty$ otherwise.

Implement this model in code and try sampling from it using the sampler that
you implemented previously or another package.
Make all the diagnostic plots from above but include one new plot: the
posterior predictive distribution (see Figure~\ref{fig:line1}).
To achieve this, plot the line predicted for a few samples randomly selected
from the chain on top of the observed data.

Finally, implement the full model where uncertainties in $x$ are also
included.
To do this, you will need to choose a prior on $x_\mathrm{true}$~--~maybe
something like $\mathcal{U}(0,\,10)$~--~and sample the 13-dimensional problem
instead of the 3-dimensional case from before.
Before starting on this, you should draw the graphical model and consult with
any other students who are working on this part.

\newpage

\ssfigure{figures/data.pdf}{0.43}{%
The black points with error bars show the simulated data used for this
experiment.
The blue line is the true underlying model that generated the data and the
orange line is what you get if you run linear least squares and ignore the
intrinsic scatter in the relation.
\label{fig:data}}

\ssfigure{figures/line1.pdf}{0.43}{%
Like Figure~\ref{fig:data} but the gray lines show the prediction for 100
random samples from the chain.
The uncertainty is larger than for the least squares result but the true model
is within the credible region.
\label{fig:line1}}

\ssfigure{figures/line2.pdf}{0.43}{%
Like Figure~\ref{fig:line2} but taking the $x$ uncertainties into account.
\label{fig:line2}}

\newpage

% \begin{multicols}{2}
% {\centering\bf REFERENCES\par}
% \vspace{0.2em}
% \begin{thebibliography}{}%
% \raggedright\raggedbottom\scriptsize\setlength{\parskip}{-0.5em}%

% \end{thebibliography}
% \end{multicols}

\end{document}


% Copyright (C) He Guanyuming 2020
% The file is licensed under the MIT license.

\subsection{General Principles}
This section describes the overall principles of the document. It illuminates how notations are explained,
in what structure this document is written and so forth. This section should be read and understood
comprehensively prior to reading the main content of the document.

\subsection{Definitions}
\begin{description}
\item[The document] The phrase \emph{the document} means this document (what you are reading) itself.
\item[The book] The phrase \emph{the book} represents Tao's \emph{Analysis} (both volume I and II).
\end{description}

\subsection{Indices}
The book has two volumes:
\emph{Analysis I} and \emph{Analysis II}. We may notice that the indices of the two volumes both start
from 1. It may lead to some confusions. So in the document, the indices are organized in such a way that:
If the content comes from \emph{Analysis I}, the corresponding index is the same as the book's.
Otherwise, the corresponding index is prefixed with ``2.''.

For example, Exercise 3.1.3 in \emph{Analysis I} is indexed as Exercise 3.1.3 in the document, but
Exercise 3.1.3 in \emph{Analysis II} is indexed as Exercise 2.3.1.3.

\subsection{Notations}
In the answers to some exercises, you may notice that the content are divided by numbers enclosed with
parentheses (e.g. \textbf{(1)}, \textbf{(2)}). Tao often puts multiple questions into a single exercise,
so these numbers indicates the number of the sub-questions.

For example, Exercise 3.5.4 is
\begin{quotation}
Exercise 3.5.4. Let $A,B,C$ be sets. Show that $A\times(B\cup C) = (A\times B)\cup(A\times C)$,
that $A\times(B\cap C) = (A\times B)\cap(A\times C)$, and that
$ A\times(B\setminus C) = (A\times B)\setminus(A\times C)$.
\end{quotation}
Then (1) indicates the question ``Show that $A\times(B\cup C) = (A\times B)\cup(A\times C)$.'',
(2) indicates the question ``Show that $A\times(B\cap C) = (A\times B)\cap(A\times C)$.'', and
(3) indicates the question ``Show that $A\times(B\setminus C) = (A\times B)\setminus(A\times C)$''.

In logical contents,
\[
\Longrightarrow, \Rightarrow, \longrightarrow, \rightarrow,
\]
have the same meaning ``implies''. And
\[
\Longleftarrow, \Leftarrow, \longleftarrow, \leftarrow,
\]
also have the same meaning ($P \leftarrow Q$ means that $Q$ implies $P$).
Finally, these following symbols all indicate logical equality.
\[
\leftrightarrow, \longleftrightarrow, \Leftrightarrow, \Longleftrightarrow, \equiv
\].

For nested quantifiers, their order is ``from left to right''. For example, the following statement
\[
\forall x \exists y (P(x,y))
\]
means that for all object $x$, their exists a object $y$ such that $P(x,y)$ is true.
That is,
\[
\forall x(\exists y(P(x,y)))
\]

Tao uses $++$ to denote the successor of a natural number. However, in the document, it is denoted by
$S(n)$ most of the times.

\[
\bigvee_{i=1}^{n} P(i), \bigwedge_{i=1}^{n} P(i)
\]
mean that for $1\leq i \leq n$, at least one $P(i)$ is true; and for all $1\leq i \leq n$, $P(i)$ is
true, respectively.

Some sets that have special meanings (e.g. the set of all natural numbers, the set of all real numbers)
are denoted in whiteboard font (e.g. $\mathbb{N}, \mathbb{R}$).

Without special interpretation, the notation
\[
(\forall x P(x))(Q(x))
\]
is interpreted as
\[
\forall x(P(x) \Longrightarrow Q(x))
\]
For example,
\[
(\forall x \in X)(Q(x)) \equiv \forall x(x \in X \Longrightarrow Q(x))
\]
While
\[
(\exists x P(x))(Q(x))
\]
is interpreted as
\[
\exists x(P(x) \wedge Q(x))
\].
For example,
\[
(\exists x \in X)(Q(x)) \equiv \exists x(x \in X \wedge Q(x))
\]

\subsection{Abbreviations}
We often leave off some descriptions for a number's properties. For example, we may refer a
\emph{positive natural number} $n$ as a \emph{positive number} when we haven't learned the rationals and
the reals.


\chapter{The Collatz Tree}

\section{The Connection between Groups and Graphs}
\label{sec:groups_graphs}
Let $(a_k)$ be a numerical sequence with $a_k=g^{(k)}(m)$, then a reversion produces an infinite number of sequences of reversely-written Collatz members \cite{Ref_Klisse_2010}.

Let $S$ be a set containing two elements $q$ and $r$, which are bijective functions over $\mathbb{Q}$:
\begin{equation}
\begin{array}{l}
q(x)=2x \\
r(x)=\frac{1}{3}(x-1)
\end{array}
\end{equation}

Let a binary operation be the right-to-left composition of functions $q\circ r$, where $q\circ r(x)=q(r(x))$. Composing functions is an associative operation. All compositions of the bijections $q$ and $r$ and their inverses $q^{-1}$ and $r^{-1}$ are again bijective. The set, whose elements are all these compositions, is closed under that operation. It forms a free group $F$ of rank 2 with respect to the free generating set $S$, where the group's binary operation $\circ$ is the function composition and the group's identity element is the identity function $id_{\mathbb{Q}}=e$. We call $e$ an \textit{empty string}. $F$ consists of all expressions (strings) that can be concatenated from the generators $q$ and $r$. The corresponding Cayley graph $Cay(F,S)=G$ is a regular tree whose vertices have four neighbors \cite[p.~66]{Ref_Loeh}. A tree is called \textit{regular} or \textit{homogeneous} when every vertex has the same degree, in this case, $d(v)=4$ for every vertex $v$ in $G$. The Cayley graph's set of vertices is $V(G)=F$, and its set of edges is $E(G)=\left\{\left\{f,f\circ s\right\}\mid f\in F,s\in\left(S\cup S^{-1}\right)\setminus\left\{e\right\}\right\}$ \cite[p.~57]{Ref_Loeh}. More precisely, the vertices are \textit{labeled} by the elements (strings) of $F$.

In conformance with graph-theoretical precepts \cite{Ref_Bondy_Murty},
\cite{Ref_Bonnington_Little}, \cite{Ref_Bender_Williamson}
we specify a subgraph $H$ of $G$ as a triple $\left(V(H),E(H),\psi_{H}\right)$ consisting of a set $V(H)$ of vertices, a set $E(H)$ of edges and an incidence function $\psi_{H}$. The latter is, in our case, the restriction $\psi_{G}\vert_{E(H)}$ of the Cayley graph's incidence function to the set of edges that only join vertices, which are labeled by a string over alphabet $\{r,q\}$ without the inverses: $E(H)=\left\{\left\{f,f\circ s\right\}\mid f\in F,s\in S\setminus\left\{e
\right\}\right\}$.

This subgraph corresponds to the monoid $S^\ast$, which is freely generated by $S$ follows related thoughts \cite{Ref_Truemper_2014} that examine the Collatz problem in terms of a free semigroup on the set $S^{-1}$ of inverse generators. Note that this semigroup is not to be confused with an \textit{inverse semigroup} "in which every element has a unique inverse" \cite[p.~26]{Ref_Almeida}, \cite[p.~22]{Ref_Loeh}.

Let $Y^X=\{f\mid f\text{ is a map }X\rightarrow Y\}$ be the set of functions, which in category theory is referred to as the \textit{exponential object} for any sets $X$, $Y$. The evaluation function $ev:Y^X\times X\to Y$ sends the pair $(f,x)$ to $f(x)$. For a detailed description of this concept, see \cite[p.~127]{Ref_Johnsonbaugh}, \cite[p.~155]{Ref_MacLane_Birkhoff}, \cite[p.~54]{Ref_Novak_etal} and \cite[p.~188]{Ref_Pellissier}. We define the evaluation function $ev_{S^\ast}:S^\ast\times\{1\}\rightarrow\mathbb{Q}$ that assesses an element of $S^\ast$, id est a composition of $q$ and $r$, for the given input value $1$. Furthermore we define the corestriction ${ev^0_{S^\ast}}$ of $ev_{S^\ast}$ to $\mathbb{N}$. Since a corestricion of a function restricts the function's codomain \cite[p.~3]{Ref_Helemskii}, the function $ev^0_{S^\ast}$ operates on a subset $T\subset S^\ast$ that contains only those compositions of $q$ and $r$, which return a natural number when inputting the value $1$.

The set $T$ does not form a monoid under function composition, for example $ev_{S^\ast}(qrq^4,1)=10$ and $ev_{S^\ast}(rq^6,1)=21$, but the composition $qrq^4rq^6$ does not lie in $T$, because the evaluation $ev_{S^\ast}(qrq^4rq^6,1)$ yields a value outside the codomain $\mathbb{N}$. However, each element of this set labels a vertex of a tree $H_{T}\subset H$, which is a proper subtree of $H$.

Let $U\subset T$ be a subset of $T$, which does not contain a reduced word with two or more successive characters $r$. The corresponding tree $H_{U}\subset H_{T}$ reflects Collatz sequences as demonstrated in figure~\ref{fig:1}.

\begin{remark}
When talking about trees having a root ("rooted trees"), another important concept should be explained: the \textbf{depth of a vertex} in a tree is the length of the path from the root to this vertex \cite[p.~226]{Ref_Sedgewick_2011}. In other words, it is the vertex's distance (the number of edges in the path) from the root. The root node has a depth of zero. The \textbf{level of a vertex} is its depth plus one $level(v)=depth(v)+1$. The level of a tree's root is always one. The \textbf{height of a vertex} is the number of edges on the longest downward path from that vertex and a leaf. Any leaf node has a height of zero. The \textbf{height of a tree} is the height of its root, or equivalently, the depth of its deepest vertex \cite[p.~226]{Ref_Sedgewick_2011}. The \textbf{size of a tree} is its number of nodes. One has to be careful, because the literature provides varying definitions. According to Rosen \cite[p.~804]{Ref_Rosen} for example, the level of a vertex is the length of the path from the root to this vertex; and the height of a tree is the largest vertex level in this tree.
\end{remark}

% trim=left bottom right top
\begin{figure}
	\includegraphics[trim=2.3cm 5.8cm 5.9cm 4.8cm,
	width=1.00\textwidth,page=1]{figures/caytree.pdf}
	\caption{Small section of $H_T$ with darkly highlighted subtree $H_U$}
	\label{fig:1}
\end{figure}

\section{Defining the Tree}
The starting point for specifying our tree is $H_U$. Due to its significance, we first concertize $H_U$ by the definition~\ref{def:H_U} below, which establishes four essential characteristics.

\begin{definition}
The graph $H_U$ possess the following key properties:
\begin{itemize}
	\item \mbox{\boldmath$H_U$} \textbf{is a directed graph (digraph):} Fundamentally, when we consider the more general case, an undirected graph as a triple $(V,E,\psi)$, the incidence function maps an edge to an arbitrary vertex pair $\psi : E\rightarrow\{X\subseteq V:\left|X\right|=2\}$. In a digraph, the set $V\times V$ represents ordered vertex pairs. Accordingly the incidence function is more specifically defined, namely as a mapping of the edges to that set $\psi : E\rightarrow\{(v,w)\in V\times V:v\neq w\}$, see \cite[p.~15]{Ref_Korte_Vygen}.
	\item \mbox{\boldmath$H_U$} \textbf{is a rooted tree:} According to Rosen \cite[p.~747]{Ref_Rosen}, a rooted tree is, "a tree in which one vertex has been designated as the root and every edge is directed away from the root." Peculiarly, this definition considers the directionality as an inherent part of rooted trees. Unlike Mehlhorn and Sanders \cite[p.~52]{Ref_Mehlhorn_Sanders}, for example, who distinguish between an undirected and directed rooted tree.
	\par\smallskip
	\textit{Note: As long as we do not stipulate that vertices may collapse, it is absolutely guaranteed that the graph is a tree.}
	\item \mbox{\boldmath$H_U$} \textbf{is an out-tree:} There is exactly one	path from the root to every other node \cite[p.~52]{Ref_Mehlhorn_Sanders}, which means that edge directions go from parents to children \cite[p.~108]{Ref_Du_Ko_Hu}. This property is implied in Rosen's definition for a rooted tree as well by saying "every edge is directed away from the root." An out-tree is sometimes designated as \textit{out-arborescence} \cite[p.~108]{Ref_Du_Ko_Hu}.
	\item \mbox{\boldmath$H_U$} \textbf{is a labeled tree:} For defining a labeled graph, Ehrig et al. \cite[p.~23]{Ref_Ehrig_etal} use a label alphabet consisting of a vertex label set and an edge label set. Since we only label the vertices, in our case the specification of a vertex label set $L_V$ together with the vertex label function $l_V:V\rightarrow L_V$ is sufficient. Originally, we said vertex labels are strings over the alphabet $S=\{q,r\}$, through which the free monoid $S^\ast$ is generated. We illustrate labeling $H_U$ by defining $l_{V(H_U)}(v)=ev^0_{S^\ast}(l_{V(G)}(\iota(v)),1)$, where $\iota:V(H_U)\hookrightarrow V(G)$ is the inclusion map \cite[p.~142]{Ref_Childs} from the set of vertices of $H_U$ to the set of vertices from the previously defined Cayley graph $G$.
\end{itemize}
\label{def:H_U}
\end{definition}

We define a tree $H_{C,3}$ by taking the tree $H_U$ as a basis and for every vertex $v\in V(H_U)$ satisfying $2\mid l_{V(H_U)}(v)$, we contract the incoming edge. We attach the label of the parent of $v$ to the new vertex, which results by replacing (merging) the two overlapping vertices that the contracted edge used to connect with. Visually, we obtain $H_{C,3}$ by contracting all edges in $H_U$ that have an even-labeled target vertex, which (due to contraction) becomes "merged into its parent." Edge contraction is occasionally referred to as \textit{collapsing an edge}. For more details and examples on edge contraction, one can see Voloshin \cite[p.~27]{Ref_Voloshin} and Loehr \cite{Ref_Loehr}.

The tree $H_{C,3}$ is well known as the so-called \textit{Syracuse tree}, see for example \cite{Ref_Kleinnijenhuis_2020a}, \cite{Ref_Aberkane_2017}, and \cite{Ref_Aberkane_2020}. It is a \textit{minor of $H_U$}, since it can be obtained from $H_U$ "by a sequence of any vertex deletions, edge deletions and edge contractions" \cite[p.~32]{Ref_Voloshin}. The sequence of contracting the edges between adjacent (in our case even-labeled) vertices is called \textit{path contraction}.

A small section of the tree $H_{C,3}$, the Syracuse tree, is displayed in figure~\ref{fig:2}. Other definitions of the same tree exist, see for example Conrow \cite{Ref_Conrow}, Bauer \cite[p.~379]{Ref_Bauer}, Batang \cite{Ref_Batang} or Jan Kleinnijenhuis and Alissa M. Kleinnijenhuis \cite{Ref_Kleinnijenhuis_2020a}, \cite{Ref_Kleinnijenhuis_2020b}.

\begin{figure}
	\includegraphics[width=1.00\textwidth]{figures/h_c3.png}
	\caption{Section of the Syracuse tree $H_{C,3}$ (displaying the trivial cycle is waived)}
	\label{fig:2}
\end{figure}

\section{\texorpdfstring{Relationship of successive nodes in $H_{C,3}$}{Relationship of successive nodes in HC3}}

Let $v_1$ and $v_{n+1}$ be two vertices of $H_{C,3}$, where $v_1$ is reachable from $v_{n+1}$ with $depth(v_1)-depth(v_{n+1})=n$. Hence, a path $(v_{n+1},\ldots,v_1)$ exists between these two vertices. Theorem~\ref{theo:1} specifies the following relationship between $v_1$ and $v_{n+1}$, empirically identified by Koch \cite{Ref_Koch_Github}.

\begin{theorem}
	\label{theo:1}
	$l_{V(H_{C,3})}(v_{n+1})=3^nl_{V(H_{C,3})}(v_1)\prod_{i=1}^{n}\left(1+\frac{1}{3l_{V(H_{C,3})}(v_{i})}\right)2^{-\alpha_i}$.
	In order to simplify readability, we waive writing down the vertex label function and put it shortly:\\
	$v_{n+1}=3^nv_1\prod_{i=1}^{n}\left(1+\frac{1}{3v_{i}}\right)2^{-\alpha_i}$.
	The value $\alpha_i\in\mathbb{N}$ is the number of edges which have been contracted between $v_i$ and $v_{i+1}$ in $H_U$.
\end{theorem}

In order to demonstrate the construction produced by theorem~\ref{theo:1} in an illustrative fashion, example~\ref{ex:vertices} runs through a concrete path in $H_{C,3}$.

\begin{example}
	\label{ex:vertices}
	For example, the two vertices $v_1=45$ and $v_{1+3}=v_4=5$ are
	connected
	via the path $(5,13,17,45)$, see figure~\ref{fig:2}. Furthermore, one
	can retrace in figure~\ref{fig:3} the uncontracted path between these
	two nodes within $H_U$. When applied to this example,
	theorem~\ref{theo:1} produces the following:
	\begin{center}
		$5=v_{1+3}=3^3\cdot45\cdot\left(1+\frac{1}{3\cdot45}\right)\cdot2^{-3}
		\cdot\left(1+\frac{1}{3\cdot17}\right)\cdot2^{-2}
		\cdot\left(1+\frac{1}{3\cdot13}\right)\cdot2^{-3}$
	\end{center}
\end{example}

\begin{proof}
	\label{proof:1}
	This relationship of successive nodes can simply be proven inductively. For the base case, we set $n=1$ and retrieve
	\begin{center}
		$v_{1+1}=3v_1\left(1+\frac{1}{3v_1}\right)2^{-\alpha_1}
		=\left(3v_1+1\right)2^{-\alpha_1}=v_2$
	\end{center}
	The path from $v_2$ to $v_1$ can uniformly be expressed by a string $rq\cdots q$ of $S^\ast$, because of $v_1=r\circ q^{\alpha_1}\left(v_2\right)$. We set $n=n+1$ for the step case, which leads to
	\begin{equation*}
	\begin{array}{cl}
	v_{n+2} &
	=3^{n+1}v_1\prod_{i=1}^{n+1}\left(1+\frac{1}{3v_i}\right)2^{-\alpha_i}\\
	&
	=3^{n+1}v_1\left(1+\frac{1}{3v_{n+1}}\right)2^{-\alpha_{n+1}}\prod_{i=1}^{n}\left(1+\frac{1}{3v_i}\right)2^{-\alpha_i}\\
	&
	=3\left(1+\frac{1}{3v_{n+1}}\right)2^{-\alpha_{n+1}}3^nv_1\prod_{i=1}^{n}\left(1+\frac{1}{3v_i}\right)2^{-\alpha_i}\\
	&
	=3\left(1+\frac{1}{3v_{n+1}}\right)2^{-\alpha_{n+1}}v_{n+1}\\
	&
	=\left(3v_{n+1}+1\right)2^{-\alpha_{n+1}}
	\end{array}
	\end{equation*}
	In this case the path from $v_{n+2}$ to $v_{n+1}$ is correspondingly
	expressed by a string $rq\cdots q$ of $S^\ast$ too, since
	$v_{n+1}=r\circ q^{\alpha_{n+1}}\left(v_{n+2}\right)$.
\end{proof}

Even though the tree may theoretically contain two or more identically labeled vertices, it is essential to emphasize that we only consider such paths $(v_{n+1},\ldots,v_1)$ whose vertices are all labeled differently. Later in section~\ref{sec:cycles}, we even require that identically labeled nodes are one and the same. In order to correctly determine successive nodes using theorem~\ref{theo:1}, we must consider the halting conditions. These are specified in definition~\ref{def:halting_conditions}, which was introduced by Koch et al. \cite{Ref_Koch_2020}.

\begin{definition}
	\label{def:halting_conditions}
	Being compliant with the Collatz conjecture, the algorithms (that calculate successive nodes for a given one) in theorem~\ref{theo:1} and equation~\ref{eq:generalized_reachability} halt if at least one of the following conditions is fulfilled:
	\begin{enumerate}
		\item $v_{n+1}=1$
		\item $v_{n+1}\in\{v_1,v_2,\ldots,v_n\}$
	\end{enumerate}
	When the first condition applies, the Collatz conjecture is true for a specific sequence. If the second condition is fulfilled, the sequence has led to a cycle. For every starting value, except $v_1=1$, the Collatz conjecture is therefore falsified. Let us consider the example $v_1=13$ and $n=2$. Inserting these values into theorem~\ref{theo:1} yields:

	\[
	v_{n+1}=3^2\cdot\left(1+\frac{1}{3\cdot13}\right)\left(1+\frac{1}{3\cdot5}\right)\cdot2^{-7}=1
	\]

	In the above example the algorithm halts after two iterations because the first condition is fulfilled. If we examine the case $v_1=1$, we realize that the algorithm finishes after the first iteration, since both halting conditions are true:

	\[
	v_{n+1}=v_1=3^1\cdot1\cdot\left(1+\frac{1}{3\cdot1}\right)2^{-2}=1
	\]

	The sequence stops in the example above due to the result being one. Apart from that, the sequence has led to a cycle.
\end{definition}

\noindent
Theorem~\ref{theo:1} can be used for specifying the condition of a cycle as follows:

\begin{equation}
\label{eq:func_cycle}
\begin{array}{l}
v_{1}=3^nv_1\prod_{i=1}^{n}\left(1+\frac{1}{3v_i}\right)2^{-\alpha_i}
\\[\medskipamount]
2^{\alpha_1+\cdots+\alpha_n}=\prod_{i=1}^{n}\left(3+\frac{1}{v_i}\right)
\end{array}
\end{equation}

A similar condition has been formulated by Hercher \cite{Ref_Hercher} and Eric Roosendaal \cite{Ref_Roosendaal_2020}. Taking a first look at equation~\ref{eq:func_cycle}, we are able to recognize the trivial cycle for $n=1$. One might easily come to the false conclusion that the term only results in a natural number for this trivial cycle, since we are multiplying fractions. The following counterexample, starting at $v_1=31$, disproves this assumption:
\begin{equation*}
20480=\left(3+\frac{1}{31}\right)\left(3+\frac{1}{47}\right)
\left(3+\frac{1}{71}\right)\left(3+\frac{1}{107}\right)\left(3+\frac{1}{161}\right)\left(3+\frac{1}{121}\right)\left(3+\frac{1}{91}\right)\left(3+\frac{1}{137}\right)\left(3+\frac{1}{103}\right)
\end{equation*}

According to OESIS \cite{Ref_OESIS_A005184}, the integer $v_1=31$ is called \textit{self-contained}. The term self-contained is based on the fact that the node $v_{n+1}=v_{10}=155$ is divisible by the starting node $v_1=31$. Moreover, $v_{10}$ results from applying one and the same function (in this case the Collatz function) using $v_1$ as input, see also Guy \cite[p.~332]{Ref_Guy}. For such a case equation~\ref{eq:func_cycle} leads to a natural number, but not necessarily to a cycle. A cycle only occurs if the term results in a power of two. One such example is the trivial cycle. We find another case when we choose the factor $5$ instead of $3$:
\begin{center}
	$128=2^7=\left(5+\frac{1}{13}\right)\left(5+\frac{1}{33}\right)
	\left(5+\frac{1}{83}\right)$
\end{center}

The above example demonstrates that non-trivial cycles can be found if we generalize the Collatz conjecture by replacing the factor $3$ with the variable $k$. We study this generalized form and the occurrence of cycles in section~\ref{sec:cycles}. A detailed elaboration of the divisibility and a deeper understanding of the tree $H_{C,3}$ needs to be performed in order to achieve a proof of the Collatz conjecture.

Generally, for any variant $kx+1$ it applies that if $v_1\mid v_{n+1}$, the product $\prod_{i=1}^n\left(k+\nicefrac{1}{v_i}\right)$ is natural.

\newpage

\begin{figure}[H]
	\includegraphics[width=1.00\textwidth]{figures/h_u.png}
	\caption{Section of $H_U$ containing the path from $5$ to $45$}
	\label{fig:3}
\end{figure}

\section{A note on the functions left-child and right-sibling}
Referring to the "left-child, right-sibling representation" of rooted trees \cite[p.~246]{Ref_Cormen_Leiserson_Rivest_Stein}, the function $\textit{left-child}:V\rightarrow V$ returns the leftmost child of a vertex $v$. Nesting this function $n$ times leads to the definition of a vertex's $n$-fold left-child, which is given by $\textit{left-child}^n(v)$. As delineated in figure~\ref{fig:2}, for example $\textit{left-child}^3(13)=7$.

The function $\textit{right-sibling}:V\rightarrow V$ points to the sibling of a vertex $v$ immediately to its right \cite[p.~246]{Ref_Cormen_Leiserson_Rivest_Stein}. If this function is nested $n$ times, we get a vertex's $n$-fold right-sibling defined by $\textit{right-sibling}^n(v)$. One example is $\textit{right-sibling}^2(113)=1813$ which has been demonstrated in figure~\ref{fig:2} too.

\section{\texorpdfstring{Relationship of sibling nodes in $H_{C,3}$}{Relationship of sibling nodes in HC3}}
\label{sec:relationship_sibling_nodes_k3}

In a rooted tree, vertices which have the same parent are called "siblings" \cite[p.~702]{Ref_Johnsonbaugh}, \cite[p.~747]{Ref_Rosen}. Sibling vertices accordingly have the same depth and thus the same level.

Let $w$ be a vertex, from which a path exists to the vertex $v_1$. Let $v_2$ be the immediate right-sibling of $v_1$, then $l_{V\left(H_{C,3}\right)}\left(v_2\right)=4\cdot l_{V\left(H_{C,3}\right)}\left(v_1\right)+1$. This fact has been expressed differently by Kak \cite{Ref_Kak_2014} as follows: "If an odd number $a$ leads to another odd number (after several applications of the Collatz transformation) $b$, then $4a+1$ also leads to $b$."

Applied to our approach, consider $w$ as the parent of $v_1$ and $v_2$. Suppose, in $H_U$, a path consisting of $n+1$ edges goes from $w$ to $v_1$. Then we can straightforwardly show that $n$ edges in $H_U$ have been contracted between both nodes $w$ and $v_1$ and $n+2$ edges between $w$ and $v_2$ (for simplicity we again omit writing the label function):
\begin{equation}
\label{eq:next_sibling_k3}
\begin{array}{l}
		v_1=\frac{w\cdot2^n-1}{3}
		\\[\medskipamount]
		v_2=\frac{w\cdot2^{n+2}-1}{3}=4\cdot v_1+1
\end{array}
\end{equation}

For example, $n=3$ edges in $H_U$ have been contracted between $w=5$ and $v_1=13$ and $n+2=5$ edges between $w$ and $v_2=53$, whereby in $H_{C,3}$, the vertex $v_2$ is the right-sibling of $v_1$ and these two sibling vertices are immediate children of $w$.

Batang \cite{Ref_Batang} demonstrated that using the geometric series $s_n=1+4+4^2+\ldots+4^{n-1}=\nicefrac{4^n-1}{3}$ we are able to directly calculate the sibling nodes (see \cite[p.~191-192]{Ref_Teschl_2013} for more details on geometric series). Let us consider the sibling nodes $\{5,21,85,341\}$. The first sibling of $5$ is calculated by $s_1+4^1\cdot5=21$, the second sibling is $s_2+4^2\cdot5=85$, and the third is $s_3+4^3\cdot5=341$.

The same principle applies to the siblings $\{13,53,213,853\}$. The first sibling of $13$ is calculated by $s_1+4^1\cdot13=53$, the next one is $s_2+4^2\cdot13=213$, and the third is $s_3+4^3\cdot13=853$.

\section{\texorpdfstring{A vertex's left-child, $n$-fold right-sibling in $H_{C,3}$}{A vertex's left-child, n-fold right-sibling in HC3}}
\label{sec:left_child_right_sibling_3}

Let $w$ be a vertex in $H_{C,3}$ and $v_0$ the left-child of $w$. Using techniques of data science \cite{Ref_Koch_Github}, we have found out empirically that the $n$-fold right-sibling of $v_0$ can be calculated as follows:
\begin{equation}
\label{eq:nfold_right_sibling_3}
	v_n=\textit{right-sibling}^n(v_0)=\frac{1}{3}\left(w\cdot2^{2n+\pi_3(w\bmod 3)}-1\right)
\end{equation}

Here the function $\pi_3$, which appears in the exponent, is the self-inverse permutation (involution):
\begin{equation}
\label{eq:pi_3}
	\pi_3=\left(\begin{array}{cc}
	1 & 2\\
	2 & 1
	\end{array}\right)
\end{equation}

We consider permutations of the set $\{1,2\}$ and not of $\{0,1,2\}$, due to the fact that $w\bmod 3$ cannot be zero. A node $w$ in $H_{C,3}$, which is labeled by an integer divisible by $3$ is a leaf; and therefore such node has no left-child. More specifically, it has no children at all. When setting $n=0$, we trivially retrieve the vertex's $w$ left-child:
\begin{center}
	$v_0=\textit{left-child}(w)=\frac{1}{3}\left(w\cdot2^{\pi_3(w\bmod 3)}-1\right)$
\end{center}

\begin{example}
\label{ex:siblings}
Let us refer to figure~\ref{fig:2} again and pick out $w=5$. Then the	vertex's $w$ left-child is $v_0=3$ and the threefold right-sibling
$v_3=213$:

\begin{equation*}
\begin{array}{l}
	v_0=\frac{1}{3}\left(5\cdot2^{\pi_3(5\bmod 3)}-1\right)=3
	\\[\medskipamount]
	v_3=\frac{1}{3}\left(5\cdot2^{2\cdot3+\pi_3(5\bmod 3)}-1\right)=213
\end{array}
\end{equation*}
\end{example}

We will now explain the reasons that are underlying this behavior. Essentially, two integers $a$ and $b$ are congruent modulo $n$ if their difference $a-b$ is divisible by $n$ or, to put it differently, if $a$ and $b$ have the same remainder when divided by $n$ \cite[p.~15]{Ref_Wolfart_2011}, \cite[p.~44]{Ref_Forster_2015}, \cite[p.~19]{Ref_Mueller-Stach_2011}, \cite[p.~142]{Ref_Iwanowski_Lang_2014}:
\begin{equation}
\label{eq:congruence}
n|(a-b)\leftrightarrow a\equiv b\pmod n
\end{equation}

In modular arithmetic we are allowed to interpret integers as names, or to be more specific as \textit{representatives}, for their equivalence class and therefore reduce (or expand) a congruence as follows:
\begin{equation}
\label{eq:congruence_reduction}
(a+n)\equiv b\pmod n\leftrightarrow a\equiv b\pmod n
\end{equation}

This means, that in modular arithmetic both operations -- addition and multiplication -- are independent from the choice of representatives in the residue classes \cite[p.~16]{Ref_Wolfart_2011}.

The residue class (also termed congruence class) of the integers for a modulus $n$ is the set $[a]_n=\{a+k\cdot n|k\in\mathbb{Z}\}$ and sometimes denoted by $\bar a_n$ or by $a+n\mathbb{Z}$, see \cite[p.~15]{Ref_Wolfart_2011}, \cite[p.~122]{Ref_Schubert_2012}, \cite[p.~25]{Ref_Mueller-Stach_2011}, \cite[p.~141]{Ref_Iwanowski_Lang_2014}. Let us put all possible remainders that arise from the division modulo $n$ together into a new set -- the set of all residue classes $[a]_n$. This set is known as the ring of integers modulo $n$ and denoted by $\mathbb{Z}/n\mathbb{Z}=\{[a]_n|a\in\mathbb{Z}\}$ and trivially $\mathbb{Z}/0\mathbb{Z}=\mathbb{Z}$ and for all $n\ne0$ we have $\mathbb{Z}/n\mathbb{Z}=\{[0],[1],\ldots,[n-1]\}$, see \cite[p.~15]{Ref_Wolfart_2011}, \cite[p.~25]{Ref_Mueller-Stach_2011}, \cite[p.~81]{Ref_Teschl_2013}.

Now there is one more tool that we will make use of later, and that is the \textit{Congruence Power Rule (CPR)}. It states that we are allowed to raise both sides of a congruence to the $m$-th power \cite[p.~19]{Ref_Mueller-Stach_2011}, \cite[p.~117]{Ref_Benjamin_2009}:
\begin{equation}
\label{eq:congruence_power_rule}
a\equiv b\pmod n\leftrightarrow a^m\equiv b^m\pmod n
\end{equation}

Let $G$ be a group and $a\in G$. If there exists an integer $d>0$ with $a^d=e$, then the smallest such $d$ is called the \textit{order} of $a$, written $d=\ord(a)$ \cite[p.~35]{Ref_Wolfart_2011}, \cite[p.~50]{Ref_Forster_2015}, \cite[p.~240]{Ref_Modler_Kreh_2012}. If no such $d$ exists, we formally write $\ord(a)=\infty$. The number of elements of $G$ is called the \textit{order} of $G$, written $\ord(G)$ \cite[p.~26]{Ref_Wolfart_2011}, \cite[p.~50]{Ref_Forster_2015}.

Let $G$ be a group and $a\in G$ an element of $G$. We consider the set of all elements $a^n\in G$ with $n\in\mathbb{Z}$. Since $a^na^m=a^{n+m}=a^ma^n$ and $(a^n)^{-1}=a^{-n}$, this set forms an abelian subgroup $H_a$ of $G$. This subgroup $H_a$ is also written $\left<a\right>$ and called the subgroup of $G$ \textit{generated} by $a$ \cite[p.~50]{Ref_Forster_2015}. A group $G$ is called \textit{cyclic}, if an $a\in G$ exists so that $G$ consists only of powers of $a$ (with exponents in $\mathbb{Z}$), thus if $G=\left<a\right>$ \cite[p.~34]{Ref_Wolfart_2011}, \cite[p.~50]{Ref_Forster_2015}, \cite[p.~240]{Ref_Modler_Kreh_2012}. In this case $\ord(a)=\ord(G)$, id est the order of an element $a\in G$ is equal to the order of the cyclic subgroup $\left<a\right>$ \cite[p.~50]{Ref_Forster_2015}.

Let us consider the cyclic group $\left<a\right>=\{e,a,a^2,\ldots,a^{n-1}\}$ and an element $b\in\left<a\right>$. Now let us face the question, "How do we find the unique integer $0\le j\le n-1$, such that $a^j=b$?" This integer $j$ is denoted by $j=log_ab$ and it is called the \textit{discrete logarithm} of $b$ \cite[p.~255-256]{Ref_Stinson_Paterson_2019}. To make it more clear that we are talking about the discrete logarithm we write $j=\dlog_ab$ or more specifically $j=\dlog_{a,k}b$ if we solve the congruence $a^j\equiv b\pmod k$ which means we solve the equation $a^j\bmod k=b$, see \cite{Ref_Jain_2011}.

The multiplicative group of integers modulo $n$, denoted as $(\mathbb{Z}/n\mathbb{Z})^\times$ or briefly as $\mathbb{Z}_n^\ast$ contains non-negative elements from $\mathbb{Z}/n\mathbb{Z}$ whose representatives are coprime to $n$ \cite[p.~87]{Ref_Teschl_2013}:
\begin{equation}
\label{eq:multiplicative_group_mod_n}
Z_n^\ast=\{a\in\mathbb{Z}/n\mathbb{Z}|\gcd(a,n)=1\}
\end{equation}

This group $\mathbb{Z}_n^\ast$ is a finite abelian group, which contains only the elements from the ring $\mathbb{Z}/n\mathbb{Z}$ that are invertible with respect to multiplication -- the units of $\mathbb{Z}/n\mathbb{Z}$. That is why the group $\mathbb{Z}_n^\ast$ is often denoted by $U(n)$, where $U$ stands for units. Within the ring $\mathbb{Z}/n\mathbb{Z}$ an element $a$ is invertible exactly if there exists an element $b$ such that $a*b\equiv1\pmod n$. An element inverse to $a$ exists exactly if $\gcd(a,n)=1$.

The \textit{multiplicative order} $\ord(a)$ of an element $a\in\mathbb{Z}_n^\ast$ is the smallest natural exponent $d$ which satisfies $a^d=1$. In other words, for a positive integer $n$ we say that an integer $a$ has multiplicative order $d$ modulo $n$ if $a^d\equiv1\pmod n$ where again $d$ is the smallest possible exponent \cite[p.~76]{Ref_Hutz_2018}, \cite[p.~32]{Ref_Shoup_2008}. To indicate that the order of $a$ refers to the modulus $n$, it is also often written $d=\ord_n(a)$. Recall that $\gcd(a,n)=1$, since $a\in\mathbb{Z}_n^\ast$

We remember that groups also have an order. The order of a multiplicative group of integers modulo $n$ is given precisely by \textit{Euler's totient function}, see \cite[p.~27]{Ref_Wolfart_2011}:
\begin{equation}
\label{eq:multiplicative_group_order}
\ord(\mathbb{Z}_n^\ast)=\phi(n)
\end{equation}

Euler's totient function $\phi(n)$ counts the positive integers up to a given integer $n$ that are coprime to $n$ \cite[p.~49]{Ref_Forster_2015}. The fact that equation~\ref{eq:multiplicative_group_order} is correct follows directly from the definition of $\phi(n)$ -- we include into $\mathbb{Z}_n^\ast$ exactly those integers (representatives) from $\mathbb{Z}/n\mathbb{Z}$ that are coprime to $n$.

\begin{remark}
The elements of the ring of integers modulo $n$ do not form a group with respect to multiplication, because the element $0$ can not be inverted. But also $\mathbb{Z}/n\mathbb{Z}\setminus\{0\}$ does not form a group for a composite $n$, since there are always products of elements $a\ne0,b\ne0$ with $a*b=0$, which means that the "closure" property is not given \cite{Ref_Schwalen_2014}.
\end{remark}

An important theorem related to Euler's totient function, which we will use at a later stage, is Euler's theorem. Euler's theorem states that for an integer $a\ge2$ coprime to the modulus $n$ the following congruence holds \cite[p.~37]{Ref_Mueller-Stach_2011}, \cite[p.~56]{Ref_Forster_2015}, \cite[p.~104]{Ref_Teschl_2013}:
\begin{equation}
\label{eq:eulers_theorem}
a^{\phi(n)}\equiv1\pmod n
\end{equation}

This means that given $\left<a\right>=\mathbb{Z}_n^\ast$, for any generator $a$ coprime to the modulus $n$ the congruence $a^{\phi(n)}\equiv1\pmod n$ becomes true, where again $\phi(n)$ is the order of $\mathbb{Z}_n^\ast$ and thus the order of $a$ (see \ref{eq:multiplicative_group_order}). If $\phi(n)\equiv2\pmod4$ then the group $\mathbb{Z}_n^\ast$ is cyclic. Consequently the multiplicative group of integers modulo $n$ is cyclic for $n\in\{1,2,4,p^j,2p^j\}$, where $p$ being an odd prime and $j\ge1$ \cite{Ref_Schwalen_2014}, \cite[p.~172]{Ref_Gallian}, \cite{Ref_Guichard}.

At this point it is appropriate that we explain the mapping (permutation) given by \ref{eq:pi_3} in more detail. A helpful tool that we can use as a point of departure is the multiplicative group of integers modulo $3$. The element $2$ is a generator $\left<2\right>=\{1,2\}=\mathbb{Z}_3^\ast$, since $2\equiv\boldsymbol{2}\pmod3$ and $2^2\equiv4\equiv\boldsymbol{1}\pmod3$. The order of $2$ is $2$, since $2^2\equiv1\pmod3$. Now we use the CPR given by \ref{eq:congruence_power_rule} and obtain $(2^2)^{n+1}\equiv1^{n+1}\pmod3$ and the following generic congruence:
\begin{equation}
\label{eq:congruence_k3}
2^j2^{2n+2-j}\equiv1\pmod3
\end{equation}

Setting $j=0,1$ leads to the following behavior, which explains the formula~\ref{eq:nfold_right_sibling_3} and the mapping~\ref{eq:pi_3}:
{\renewcommand{\arraystretch}{1.8}
\begin{table}[H]
	\centering
	\begin{tabular}{|L|L|L|L|L|}
		\hline
		\thead{\boldsymbol{j}} &
		\thead{\textbf{congruence}~\ref{eq:congruence_k3}} &
		\thead{\textbf{node}\ \boldsymbol{w}} &
		\thead{\textbf{setting}\ \boldsymbol{w}\ \textbf{as per}\ \ref{eq:congruence_reduction}} &
		\thead{\textbf{divisibility as per}\ \ref{eq:congruence}}\\
		\hline
		0
		& 1\cdot2^{2n+2}\equiv1
		& w\in[1]_3
		& \multirowcell{2}{w\cdot2^{2n+\pi_3(w\bmod3)}\equiv1}
		& \multirowcell{2}{3|(w\cdot2^{2n+\pi_3(w\bmod3)}-1)}
		\\ \cline{1-3}
		1
		& 2\cdot2^{2n+1}\equiv1
		& w\in[2]_3
		&
		&
		\\ \hline
	\end{tabular}
\end{table}}

\begin{remark}
If addition is the group operation, as it is the case for example with the additive group of integers modulo $3$, denoted as $(\mathbb{Z}/3\mathbb{Z},+)$ or as $(\mathbb{Z}_3,+)$, then for an element $a$ the term $a^n$ means add (and not multiply) $a$ to itself $n$ times. In this specific case the group contains three elements $\mathbb{Z}_3=\{0,1,2\}$ and the identity element is $e=0$. The element $2$ is a generator $\left<2\right>=\{0,1,2\}=\mathbb{Z}_3$, since $2\equiv\boldsymbol{2}\pmod3$ and $2+2\equiv4\equiv\boldsymbol{1}\pmod3$ and $2+2+2\equiv6\equiv\boldsymbol{0}\pmod3$. The order of $2$ is $3$, because $2^3\equiv0\pmod3$.
\end{remark}

\section{\texorpdfstring{A vertex's left-child, $n$-fold right-sibling in $H_{C,5}$}{A vertex's left-child, n-fold right-sibling in HC5}}
\label{sec:left_child_right_sibling_5}

In the following we take a look at the tree $H_{C,5}$ -- the $5x+1$ variant of $H_C$. We must note that it is not a tree and moreover that not all of its vertices are reachable from the root, which makes it particularly interesting as a counterexample. We define the permutation $\pi_5$ as follows:
\begin{center}
\label{eq:pi_5}
    $\pi_5=\left(\begin{array}{cccc}
    	1 & 2 & 3 & 4\\
    	4 & 3 & 1 & 2
    \end{array}\right)$
\end{center}

Next, by letting $w$ be a vertex in $H_{C,5}$ and $v_0$ the left-child of $w$
we obtain the $n$-fold right-sibling of $v_0$ by the function that
is slightly different to the one defined by \ref{eq:nfold_right_sibling_3}:
\begin{equation}
\label{eq:nfold_right_sibling_5}
    v_n=\textit{right-sibling}^n(v_0)=\frac{1}{5}\left(w\cdot2^{4n+\pi_5(w\bmod 5)}-1\right)
\end{equation}

Analogous to \ref{eq:pi_3} only permutations on the set without zero
\{1,2,3,4\} need to be considered, since $w\bmod 5$ cannot be zero.
Otherwise, if $w\equiv 0 (\bmod 5)$ which means that $w$ would be labeled
by an integer divisible by 5, then the node $w$ has no successor in $H_{C,5}$.

\noindent
By setting $n=0$, the function (above given by \ref{eq:nfold_right_sibling_5}) returns the left child of $w$:
\begin{center}
	$v_0=\textit{left-child}(w)=\frac{1}{5}\left(w\cdot2^{\pi_5(w\bmod 5)}-1\right)$
\end{center}

Equation~\ref{eq:nfold_right_sibling_5} has been identified empirically as well and can be explained using the cyclic group $\left<2\right>=\{1,2,3,4\}=\mathbb{Z}_5^\ast$. First of all, it is obvious that $2$ generates this group, since $2\equiv\boldsymbol{2}\pmod5$ and $2^2\equiv\boldsymbol{4}\pmod5$ and $2^3\equiv8\equiv\boldsymbol{3}\pmod5$ and $2^4\equiv16\equiv\boldsymbol{1}\pmod5$. The order is $4$ and according to \ref{eq:eulers_theorem} and \ref{eq:multiplicative_group_order} we have $2^{\ord(\mathbb{Z}_5^\ast)}\equiv2^{\phi(5)}\equiv2^4\equiv1\pmod5$. Again we use the CPR given by \ref{eq:congruence_power_rule} and obtain $(2^4)^{n+1}\equiv1^{n+1}\pmod5$ and the following generic congruence:
\begin{equation}
\label{eq:congruence_k5}
2^j2^{4n+4-j}\equiv1\pmod5
\end{equation}

Setting $j=0,1,2,3$ leads to the following behavior, which explains the formula~\ref{eq:nfold_right_sibling_5} and the mapping~\ref{eq:pi_5}:
{\renewcommand{\arraystretch}{1.8}
\begin{table}[H]
	\centering
	\begin{tabular}{|L|L|L|L|L|}
		\hline
		\thead{\boldsymbol{j}} &
		\thead{\textbf{congruence}~\ref{eq:congruence_k5}} &
		\thead{\textbf{node}\ \boldsymbol{w}} &
		\thead{\textbf{setting}\ \boldsymbol{w}\ \textbf{as per}\ \ref{eq:congruence_reduction}} &
		\thead{\textbf{divisibility as per}\ \ref{eq:congruence}}\\
		\hline
		0
		& 1\cdot2^{4n+4}\equiv1
		& w\in[1]_5
		& \multirowcell{4}{w\cdot2^{4n+\pi_5(w\bmod5)}\equiv1}
		& \multirowcell{4}{5|(w\cdot2^{4n+\pi_5(w\bmod5)}-1)}
		\\ \cline{1-3}
		1
		& 2\cdot2^{4n+3}\equiv1
		& w\in[2]_5
		&
		&
		\\ \cline{1-3}
		2
		& 4\cdot2^{4n+2}\equiv1
		& w\in[4]_5
		&
		&
		\\ \cline{1-3}
		3
		& 8\cdot2^{4n+1}\equiv1
		& w\in[3]_5
		&
		&
		\\ \hline
	\end{tabular}
\end{table}}

Figure~\ref{fig:hc5} illustrates a small section of $H_{C,5}$ starting at its root. The particularly interesting thing about the graph $H_{C,5}$ is that it contains three cycles, the trivial cycle starting from the root $(1,3)$ and two non-trivial cycles $(43,17,27)$ and $(83,33,13)$. To be precise, three cycles are known (as it will become apparent later in section~\ref{sec:non_trivial_cycles}), and on the basis of present knowledge it cannot be ruled out with any certainty that other cycles exist.

\begin{figure}[H]
	\includegraphics[width=1.00\textwidth]{figures/h_c5b.png}
	\caption{Section of the graph $H_{C,5}$ starting at its root (without branches that reflect a subsequence containing the trivial cycle)}
	\label{fig:hc5}
\end{figure}

\section{\texorpdfstring{A vertex's left-child, $n$-fold right-sibling in $H_{C,7}$}{A vertex's left-child, n-fold right-sibling in HC7}}
\label{sec:left_child_right_sibling_7}

Now we are able to develop the formula deductively that calculates for a given node $w$ the left-child and right-sibling in $H_{C,7}$. We refer to the cyclic group $\mathbb{Z}_7^\ast=\{1,2,3,4,5,6\}$. Note that in this case $2$ is not a generator of this group. But nevertheless $\mathbb{Z}_7^\ast$ is cyclic and $\ord(2)=3$ which gives $2^3\equiv1\pmod7$. Again we use the CPR given by \ref{eq:congruence_power_rule} and obtain $(2^3)^{n+1}\equiv1^{n+1}\pmod7$ and the following generic congruence:
\begin{equation}
\label{eq:congruence_k7}
2^j2^{3n+3-j}\equiv1\pmod7
\end{equation}

Setting $j=0,1,2$ leads to the following behavior, which produces formula~\ref{eq:nfold_right_sibling_7} and the mapping~\ref{eq:pi_7}:

{\renewcommand{\arraystretch}{1.8}
\begin{table}[H]
	\centering
	\begin{tabular}{|L|L|L|L|L|}
		\hline
		\thead{\boldsymbol{j}} &
		\thead{\textbf{congruence}~\ref{eq:congruence_k7}} &
		\thead{\textbf{node}\ \boldsymbol{w}} &
		\thead{\textbf{setting}\ \boldsymbol{w}\ \textbf{as per}\ \ref{eq:congruence_reduction}} &
		\thead{\textbf{divisibility as per}\ \ref{eq:congruence}}\\
		\hline
		0
		& 1\cdot2^{3n+3}\equiv1
		& w\in[1]_7
		& \multirowcell{3}{w\cdot2^{3n+\pi_7(w\bmod7)}\equiv1}
		& \multirowcell{3}{7|(w\cdot2^{3n+\pi_7(w\bmod7)}-1)}
		\\ \cline{1-3}
		1
		& 2\cdot2^{3n+2}\equiv1
		& w\in[2]_7
		&
		&
		\\ \cline{1-3}
		2
		& 4\cdot2^{3n+1}\equiv1
		& w\in[4]_7
		&
		&
		\\ \hline
	\end{tabular}
\end{table}}

\begin{equation}
\label{eq:nfold_right_sibling_7}
v_n=\textit{right-sibling}^n(v_0)=\frac{1}{7}\left(w\cdot2^{3n+\pi_7(w\bmod 7)}-1\right)
\end{equation}

The mapping \ref{eq:pi_7} is not a permutation as in the case of $\pi_3$ and $\pi_5$, it is defined as follows:
\begin{equation}
\label{eq:pi_7}
\pi_7(n)=\begin{cases}
3	&	n=1\\
2	&	n=2\\
1   &   n=4
\end{cases}
\end{equation}

\begin{figure}[H]
	\includegraphics[width=1.00\textwidth]{figures/h_c7.png}
	\caption{Section of the graph $H_{C,7}$ starting at its root (without branches that reflect a subsequence containing the trivial cycle)}
	\label{fig:hc7}
\end{figure}

\section{\texorpdfstring{Generalizing the relationship of successive nodes for $H_{C,k}$}{Generalizing the relationship of successive nodes for HCk}}
Let us refer to $H_{C,k}$. By having introduced and proven theorem~\ref{theo:1} we already started an assertion about the reachability of successive nodes in $H_{C,3}$. This reachability relationship can be generalized for any graph $H_{C,k}$ as follows:
\begin{equation}
	\label{eq:generalized_reachability}
	v_{n+1}=k^nv_1\prod_{i=1}^{n}\left(1+\frac{1}{kv_{i}}\right)2^{-\alpha_i}
\end{equation}

This generalization will be later utilized in chapter~\ref{ch:cycles} for closer observations of cycles in various $kx+1$ variants of the graph $H_C$.

\section{\texorpdfstring{Generalizing the relationship of sibling nodes for $H_{C,k}$}{Generalizing the relationship of sibling nodes for HCk}}
In section~\ref{sec:relationship_sibling_nodes_k3} we have taken a closer look at the relationship of sibling nodes in $H_{C,3}$. But what is the formula for calculating the $n$-fold right-sibling of a given node $v_0$ generalized to the $kx+1$ variant of $H_C$? We remember that the multiplicative order $d=\ord_k(2)$ is the smallest natural exponent $d$ such that $2^d\equiv 1\pmod k$. In the case $k=3$ we can calculate the next sibling $v_1$ of a given node $v_0$ as follows: $v_1=v_0\cdot4+1$, see \ref{eq:next_sibling_k3}. Within $H_{C,7}$, the next sibling of $v_0$ is given by $v_1=v_0\cdot8+1$. In the case of $k=5$, we calculate the next sibling $v_1=v_0\cdot16+3$ and for $k=9$ we obtain the next sibling $v_1$ of a given node $v_0$ by $v_1=v_0\cdot64+7$. The general formula for calculating a given node's $v_0$ immediate right sibling is:

\begin{equation}
\label{eq:right_sibling_k}
v_1=v_0\cdot2^{\ord_k(2)}+\frac{1}{k}\left(2^{\ord_k(2)}-1\right)
\end{equation}

For example the node $v_0=243$ in $H_{C,5}$ has the right sibling $v_1=243\cdot2^4+(2^4-1)/5=3891$ (see figure~\ref{fig:hc5}). In order to calculate the $n$-fold right sibling of a given node $v_0$, we need to repeatedly nest $n$ times the (linear) function~\ref{eq:right_sibling_k}. For the sake of simplicity, let us substitute $a=2^{\ord_k(2)}$ and $b=\frac{1}{k}(2^{\ord_k(2)}-1)$. Then the $n$-fold right sibling of $v_0$ is obtained by the following structure:

\begin{flalign*}
v_n=\textit{right-sibling}^n(v_0)&=\left(\left(\left(v_0\cdot a+b\right)\cdot a+b\right)\cdot a+b\right)\cdots\\
&=v_0\cdot a^n+b(a^{n-1}+\ldots+a^2+a+1)=v_0\cdot a^n+b\frac{a^n-1}{a-1}
\end{flalign*}

Note that the term $b(a^{n-1}+\ldots+a^2+a+1)$ can be simplified using the $n$-th partial sum of a geometric series (\cite[p.~192]{Ref_Teschl_2013}). The resubstitution of both coefficients $a$ and $b$ leads us to the final generalized formula that calculates the $n$-fold right sibling of a node $v_0$ in $H_{C,k}$ for a natural $n\ge0$:
\begin{equation}
\label{eq:n_fold_right_sibling_k}
v_n=\textit{right-sibling}^n(v_0)=v_0\cdot2^{n\cdot\ord_k(2)}+\frac{1}{k}\left(2^{\ord_k(2)}-1\right)\cdot\frac{2^{n\cdot\ord_k(2)}-1}{2^{\ord_k(2)}-1}
\end{equation}

This can be verified by inserting $n=0$ and $n=1$ into formula~\ref{eq:left_child_n_fold_right_sibling_k} that calculates the a vertex's left-child, $n$-fold right-sibling of $H_{C,k}$:

\[\arraycolsep=1.6em
\begin{array}{ll}
v_0=\frac{1}{k}\left(w\cdot2^{\ord_k(2)-\dlog_{2,k}w}-1\right) & kv_0+1=w\cdot2^{\ord_k(2)-\dlog_{2,k}w}\\
v_1=\frac{1}{k}\left(w\cdot2^{2\ord_k(2)-\dlog_{2,k}w}-1\right) & kv_1+1=w\cdot2^{2\ord_k(2)-\dlog_{2,k}w}
\end{array}
\]

This brings us to the following quotient leading to the basic relationship between two sibling nodes that is given by equation~\ref{eq:right_sibling_k} in the form of $v_1=v_0\cdot a+b$:

\[
\frac{kv_1+1}{kv_0+1}=\frac{2^{2\ord_k(2)-\dlog_{2,k}w}}{2^{\ord_k(2)-\dlog_{2,k}w}}=2^{\ord_k(2)}
\]

Here we point out that the equation~\ref{eq:n_fold_right_sibling_k} of course only works for such $k$, where the order of two is not infinity $\ord_k(2)\ne\infty$. This means that, for instance, it does not work for $k=1$, id est for the $1x+1$ variant of $H_C$. This variant is very instructive due to its simple nature and for the sake of completeness we have added a picture including some few words about $H_{C,1}$ in appendix~\ref{appx:x_plus_1_variant}.

\section{\texorpdfstring{Generalizing a vertex's left-child, $n$-fold right-sibling for $H_{C,k}$}{Generalizing vertex's left-child, n-fold right-sibling for HCk}}
Let $n\ge0$ be an integer. We generalize the formulas, which have been developed in sections~\ref{sec:left_child_right_sibling_3}~-~\ref{sec:left_child_right_sibling_7} to calculate the left-child, $n$-fold right-sibling for a given node $w$ that is the direct parent node of $v_0$ as follows:
\begin{equation}
\label{eq:left_child_n_fold_right_sibling_k}
v_n=\textit{right-sibling}^n(v_0)=\frac{1}{k}\left(w\cdot2^{\ord_k(2)\cdot n+\ord_k(2)-\dlog_{2,k}w}-1\right)
\end{equation}

To put the procedure of computation a bit simpler: We start at an arbitrary parent node $w$, calculate its left-child $v_0$ and then determine the $n$-fold right-sibling (the $n$-th neighbor from the right) of $v_0$. Let us choose for example the node $w=2633$ from $H_{C,7}$, whose left child is $v_0=3009$. The fourth right-sibling of this left-child is $v_4=(2633\cdot2^{3\cdot4+3-0}-1)/7=12325449$ (see figure~\ref{fig:hc7}).

Recall that the discrete logarithm $\dlog_{2,k}w=j$ finds the smallest exponent $j$ such that $2^j\equiv w\pmod k$ respectively solves the equation $2^j\bmod k=w$.


\chapter{Single Mauthner Cell Model - Theory}
	In this chapter we will explain the theoretical aspects of the neuronal model for a single
	Mauthner Cell.
	By 'single' Mauthner cell we only mean that we are considering the mechanisms of the
	surrounding circuit involving one of the two existing Mauthner cells instead of both.
	We will start with the description of the full model and continue with two reductions that
	assume a separation of timescales and thus provide stationary approximations of the model.
	\section{Full neuronal model}
	The full neuronal model of a single Mauthner cell consists of a rate-based model for the
	population of inhibitory interneurons that provide the feed-forward inhibition and a LIF model
	for the M-cell itself.
	Both the inhibitory population and the M-cell get their input from a single source.
	In our case this input will represent the visual information coming from the optic tectum which
	will be described in more detail in the next chapter.
	The time evolution of the activity $\rho$ of the inhibitory population is described by the
	following equation:
	\begin{equation}
	\tau _{\rho} \frac{d\rho}{dt} = - (\rho(t) - \rho_{0}) + c_{\rho} I(t) +
	\eta _{\rho},
	\label{eq:inhib}
	\end{equation}
	where $\tau _{_\rho}$ is the time constant, $\rho _{0}$ is the resting activity of the
	population, $c_{\rho}$ is a scaling factor, $I(t)$ is the time dependent input and $\eta
	_{\rho}$ is a Gaussian noise term.
    While we assume that the resting activity $\rho_{0}$ is constant during a single trial of an experiment, we sample its value during a single trial from a random distribution that we further specify in the next chapter.\\
	For the M-cell we use a LIF model where the time evolution of the membrane potential $V_m$ is
	described by the following equation:
	\begin{equation}
	\tau _m \frac{dV_m}{dt} = - (V(t) - E_{L}) + R_{m} I(t) - \rho (t) +  \eta
	_m,
	\label{eq:mcell}
	\end{equation}
	where $\tau_{m}$ is the membrane time constant, $E_L$ is the resting potential, $R_m$ is the
	membrane resistance and $\eta_{m}$ is again a Gaussian noise term.
	The M-cell thus gets the direct visual input $I(t)$ and is inhibited by $\rho(t)$.
	If the membrane potential $V_m$ crosses a threshold $V_t$ an action potential is artificially
	produced and the membrane potential is reset to the resting potential $E_L$.
	Additional to the noise terms in equations \ref{eq:inhib} and \ref{eq:mcell} we will also
	consider fluctuations of the firing threshold $V_t$:
	\begin{equation}
	V_t (t) = V_t + \eta_t(t),
	\label{eq:thrs}
	\end{equation}
	where $\eta_t$ is a Gaussian noise term.\\
	The basic parameters of the LIF model, i.e. $E_L$, $R_m$, $\tau_m$ and $V_t$, have been fitted to experimental data in a previous study by \cite{Koyama2016} using recordings from four larval zebrafish at four days post-fertilization(dpf).
	For the details of the fitting procedure see their methods section.\\
	One important property of this dynamic system are the time scales on which the described activity is going on.
	Since we know that the synapses at the inhibitory interneurons are electric, at least for the auditory input, the time constant, and therefore the relevant time scale, of $\rho$ is in the order of milliseconds.
	As we will see later on, in the experiments that we want to reproduce the changes in the input over time are on much bigger time scales of at least hundreds of milliseconds.
	This fact motivates the reduction in the next section where we approximate the activity of the inhibitory population by an adiabatic ansatz assuming a separation of time scales.
	\section{Stationary Approximation of Inhibitory Population}\label{approx inhibition}
	Here we reduce the model by approximating the activity of the inhibitory population by its
	stationary solution.
	This approximation is the more accurate the higher the difference is between the time scale of the dynamics of the inhibitory population and the time scale of the input.
	If we use $\tau_{\rho}$ as the time scale of the inhibitory population and denote $\tau_{in}$ as the time scale of the input, the approximation becomes equivalent for the limit $\tau_{\rho}/ \tau_{in} \rightarrow 0$.
	In the model, this means that equation \ref{eq:inhib} becomes:
	\begin{equation}
	\hat{\rho} (t) = \rho_{0} + c_{\rho} I(t) + \eta_{\rho}.
	\label{eq:inhib_approx}
	\end{equation}
	Now we can replace $\rho (t)$ in equation \ref{eq:mcell} and get:
	\begin{equation}
	\tau _m \frac{dV_m}{dt} = - (V(t) - E_{L}) + I(t)(R_{m} - c_{\rho}) - \rho_{0} -
	\eta_{\rho} +  \eta _m.
	\label{eq:mcell_approx1}
	\end{equation}
	In the resulting LIF model the input is now weighted by the difference between the scaling
	factor $c_{\rho}$ and the membrane resistance $R_m$.
	If we ignore the noise terms for a moment and assume that $\rho_{0}=0$, this means that the
	input can only excite the M-cell and therefore evoke an action potential if $c_{\rho} < R_m$.
	Increasing $\rho_{0}$ would effectively increase the firing threshold $V_t$.
	\section{Stationary Approximation of Full Model}\label{approx full model}
	As a next step we can further approximate the LIF model in equation \ref{eq:mcell_approx1} by
	its stationary solution:
	\begin{equation}
	\hat{V}_m(t) = E_{L} + I(t)(R_{m} - c_{\rho}) - \rho_{0} -
	\eta_{\rho} +  \eta _m.
	\end{equation}
	If we set all noise to zero we can derive an expression for the input at which the membrane
	potential reaches the threshold $V_{t}$:
	\begin{equation}
	\hat{V}_m(t) \overset{!}{=} V_t
	\end{equation}
	\begin{equation}
	\Leftrightarrow E_{L} + I(t)(R_{m} - c_{\rho}) - \rho_{0}
	\overset{!}{=} V_t
	\end{equation}
	\begin{equation}
	\Leftrightarrow I(t)
	\overset{!}{=} \frac{V_t - E_{L} + \rho_{0}}{(R_{m} - c_{\rho})}
	\label{eq:crit_input}
	%TODO: look up solution for simple LIF equation even if it's only for linear input
	%TODO: say that this is comparable to first-passage time problems such as in the
	%drift-diffusion model for decision making(maybe cite ratcliff2002 or so)
	\end{equation}
%----------------------------------------------------------------------------------------


%% CHAPTER 2
% Initialization of the system
%  - Analysis of the image in the HSV space
%  - Writing down the classifier. Implementing it in class ParkSpot

\section{Initialization of the System}

	\subsection{Classification with Histograms}

	Here is presented the algorithm used to initialize the system. The algorithm is
	a classifier developed upon the classification of two main parameters that
	tends to separate busy parking spot, from free ones.

	Taken an image converted in HSV color space, we could extract the mean and the
	standard deviation for each component. The plot of the standard
	deviation of the Saturation components against the mean of Value component, for
	a single frame, will give us this single situation. Parking spot status is
	known, and we can see a strong separation (see figure \ref{fig:separate}, on
	the left).

	The two different status could be separated with two degrees of freedom of a
	line. The inclination and offset of the line could be used to derive
	rotation plus translation equation that will help us to discriminate between
	the busy and the free parking spot. Given a point ${\sigma_{2},\mu_{3}}^{T}$, and a
	separation line in the form $\mu_{3} = \alpha \sigma_{2}+\eta$, we could derive
	this transformation:
	\begin{equation}
		\left\{ \begin{array}{c}
\xi_{1}\\
\xi_{2}
\end{array}\right\} =\left[\begin{array}{cc}
\cos(\alpha) & \sin(\alpha)\\
-\sin(\alpha) & \cos(\alpha)
\end{array}\right]\left\{ \begin{array}{c}
\sigma_{2}\\
\mu_{3}
\end{array}\right\} -\left\{ \begin{array}{c}
1\\
1
\end{array}\right\} \eta
	\end{equation}
	the algorithm has only to check:
	\begin{equation}
		\xi_{2} \geq 0
	\end{equation}
	if this condition is true, than the park spot is busy, else the parking spot is
	free.
	% TODO Figura separazione singola e rispetto al tempo label: fig:separate
	\begin{figure}[H] \label{fig:separate}
		\centering
			\includegraphics[keepaspectratio, scale=0.4]{img/img1.pdf}
		\caption{Classifier data in 2D representation}
	\end{figure}

	Referring to the image on the right, in figure \ref{fig:separate}, it is easy
	to understand that this classification is not robust in time, if not
	expanded with tuning of the parameters each frame (learning
	algorithm). Means tend to remain almost equal, but standard deviations tend to
	change in time. The learning method should change the angle of the separation
	line (and also the discrimination algorithm) to get a good classification. We
	have decided to leave this method, for something that is little more
	sophisticated, and to discover more about the \verb+openCV+ libraries.

	As a drawback, we can also consider the fact that there will be no control on
	the evolution of the classifier discrimination parameters.

	\subsection{Diving in the Code}
	In the code, this initialization script is called when a new object
	\verb+ParkSpotObj+ is created, as \verb+int ParkSpotObj::initialStatus()+
	method. The projection of the two characteristics is made by the single object,
	to follow the self-containment philosophy. The parameters that drive the
	algorithm are the number 3 and 4 in the \verb+param+ element of the
	configuration script.

	The code is almost at a good level of optimization, because the number of bins
	extracted for the histograms are 32, and the area on which is evaluated the
	status is relatively small.


\subsection{Electrostatic interaction energy}
\label{sec:distributed_multipoles}
\index{distributed multipoles}
\index{site energy!distributed multipoles}

We represent the molecular charge density by choosing multiple expansion sites (``polar sites'') per molecule in such a way as to accurately reproduce the molecular electrostatic potential (ESP), with a set of suitably chosen multipole moments $\{Q_{lk}^a\}$ (in spherical-tensor notation) allocated to each site. The expression for the electrostatic interaction energy between two molecules $A$ and $B$ in the multi-point expansion includes an implicit sum over expansion sites $a\epsilon A$ and $b\epsilon B$,
\begin{align}
 U_{AB} & = \sum_{a\epsilon A} \sum_{b\epsilon B} \hat{Q}_{l_1k_1}^a T_{l_1k_1l_2k_2}^{a,b} \hat{Q}_{l_2k_2}^b \equiv  \hat{Q}_{l_1k_1}^a T_{l_1k_1l_2k_2}^{a,b} \hat{Q}_{l_2k_2}^b,
 \label{equ:mol_distributed_U}
\end{align}
where we have used the Einstein sum convention for the site indices $a$ and $b$ on the right-hand side of the equation, in addition to the sum convention that is in place for the multipole-moment components $t\equiv l_1 k_1$ and $u\equiv l_2 k_2$. The $T_{l_1k_1l_2k_2}^{a,b}$ are tensors that mediate the interaction between a multipole component $l_1 k_1$ on site $a$ with the moment $l_2 k_2$ on site $b$. If we include the molecular environment into a perturbative term $W$ to enter in the single-molecule Hamiltonian, the above expression is exactly the first-order correction to the energy where the quantum-mechanical detail has been absorbed in classical multipole moments.

The are a number of strategies how to arrive at such a collection of {\em distributed multipoles}. They can be classified according to whether the multipoles are derived (a) from the electrostatic potential generated by the SCF charge density or (b) from a decomposition of the wavefunction itself. Here, we will only draft two of those approaches, CHELPG~\cite{breneman_determining_1990} from category (a) and DMA~\cite{stone_distributed_1985} from category (b).

The CHELPG (CHarges from ELectrostatic Potentials, Grid-based) method relies on performing a least-squares fit of atom-placed charges to reproduce the electrostatic potential as evaluated from the SCF density on a regularly spaced grid~\cite{breneman_determining_1990}. The fitted charges result from minimizing the Lagrangian function~\cite{chirlian_atomic_1987}
\begin{align}
 z(\{q_i\}) = \sum_{k=1}^M \left( \phi(\vec{r}_k) - \sum_{i=1}^N \frac{1}{4\pi\varepsilon_0} \frac{q_i}{|\vec{r}_i-\vec{r}_k|} \right) + \lambda \left( q_\textrm{mol} - \sum_{i=1}^N q_i \right),
\end{align}
with $M$ grid points, $N$ atomic sites, the set of atomic partial charges $\{q_i\}$ and the SCF potential $\phi$. The Lagrange multiplier $\lambda$ constrains the sum of the fitted charges to the molecular charge $q_\textrm{mol}$. The main difference from other fitting schemes~\cite{singh_approach_1984} is the algorithm that selects the positions at which the potential is evaluated (we note that the choice of grid points can have substantial effects especially for bulky molecules).
Clearly, the CHELPG method can be (and has been) extended to include higher atomic multipoles. It should be noted, however, how already the inclusion of atomic dipoles hardly improves the parametrization, and can in fact be harmful to its conformational stability.

The Distributed-Multipole-Analysis (DMA) approach~\cite{stone_distributed_1985, stone_distributed_2005}, developed by A. Stone, operates directly on the quantum-mechanical density matrix, expanded in terms of atom- and bond-centered Gaussian functions $\chi_\alpha = R_{LK}(\vec{x}-\vec{s}_\alpha) \exp[-\zeta(\vec{x}-\vec{s}_\alpha)^2]$,
\begin{align}
 \rho(\vec{x}) = \sum_{\alpha,\beta} \rho_{\alpha\beta} \chi_\alpha(\vec{x}-\vec{s}_\alpha) \chi_\beta(\vec{x}-\vec{s}_\beta).
\end{align}
The aim is to compute multipole moments according in a distributed fashion: If we use that the overlap product $\chi_\alpha \chi_\beta$ of two Gaussian basis functions yields itself a Gaussian centered at $\vec{P} = (\zeta_\alpha \vec{s}_\alpha + \zeta_\beta \vec{s}_\beta) / (\zeta_\alpha + \zeta_\beta)$, it is possible to proceed in two steps: First, we compute the multipole moments associated with a specific summand in the density matrix, referred to the overlap center $\vec{P}$:
\begin{align}
 Q_{LK}[\vec{P}] = - \int R_{LK}(\vec{x}-\vec{P}) \rho_{\alpha\beta} \chi_\alpha \chi_\beta d^3\!x.
\end{align}
Second, we transfer the resulting $Q_{lk}[\vec{P}]$ to the position $\vec{S}$ of a polar site according to the rule~\cite{stone_distributed_1985}
\begin{align}
 Q_{nm}[\vec{S}] = \sum_{l=0}^L \sum_{k=-l}^l \left[ \left(\begin{array}{c} n+m \\ l+k \end{array}\right)\left(\begin{array}{c} n-m \\ l-k \end{array}\right) \right]^{1/2} R_{n-l,m-k}(\vec{S}-\vec{P})\cdot Q_{lk}[\vec{P}].
\end{align}
Note how this requires a rule for the choice of the expansion site to which the multipole moment should be transferred. In the near past~\cite{stone_distributed_2005}, the nearest-site algorithm, which allocates the multipole moments to the site closest to the overlap center, was replaced for diffuse functions by an algorithm based on a smooth weighting function in conjunction with grid-based integration methods in order to decrease the basis-set dependence of the resulting set of distributed multipoles.

One important advantage of the DMA approach over fitting algorithms such as CHELPG or Merz-Kollman (MK) is that higher-order moments can also be derived without too large an ambiguity.

The `mps' file format used by VOTCA for the definition of distributed multipoles (as well as point polarizabilities, see subsequent section) is based on the GDMA punch format of A. Stone's GDMA program~\cite{stone_distributed_2005} (the punch output file can be immediately plugged into VOTCA without any conversions to be applied). Furthermore the log-file of different QM packages (currently \gaussian, \turbomole and \nwchem) may be fed into the \toolref{log2mps} \tool, which will subsequently generate the appropriate mps-file.

\votcacommand{Read in ESP charges from a QM log file}{\cmdlogmps}