\documentclass[Thesis.tex]{subfiles}
\begin{document}
\chapter{Monte Carlo Methods}
\label{chp:monte-carlo}

\glsresetall

In \cref{chp:variational-monte-carlo} we found that we needed a way to sample
system configurations $\vX$ from the \gls{pdf} described by the
wave function. We needed this as a means to evaluate integrals. Specifically, we
needed to evaluate the expectation value of the Hamiltonian w.r.t. a given wave
function $\psialpha$:

\begin{align}
    \expval{\hat H} &= \frac{\expval{\hat H}{\psialpha}}{\braket{\psialpha}} = \int\dd{\vX} P_{\psi_{\valpha}} (\vX) E_L(\vX)\label{eq:chp-mc-anon-1}\\
    &\approx \frac{1}{N} \sum_{i = 1}^N E_L(\vX_i)
    \qq{where} \vb X_i\disteq P_{\psi_{\valpha}}.
\end{align}
where $N$ is set sufficiently large to satisfy the required accuracy. Due to
the central nature of this method, we will start by a proper presentation of
this \gls{mc} technique as it is used here, followed the details of how to
obtain the samples we need.

\section{Monte Carlo Integration}

\acrfull{mci} is a general technique for numeric evaluation of
any arbitrary integral. In general it concerns evaluating any multidimensional
definite integral, written as:

\begin{align}
    I = \idotsint_\sigma\dd{\vx} f(\vx),
\end{align}
for some integrand $f(\vx)$ and where $\sigma$ denotes a subset of $\mathbb{R}^m$ with a $m$-dimensional volume given by:
\begin{align}
    V = \idotsint_\sigma\dd{\vx}
\end{align}
In its simplest form, \gls{mci} works by sampling $N$ \emph{uniformly} distributed points
$\vx_1,\dots,\vx_N$ from $\sigma$, and uses a Riemann sum formulation of the
integral $I$:\footnote{Notice that $\vx_i$ now denotes one random sample, and
  has nothing to do with the $i$'th particle. For the time being we don't talk
  about particles, and the subscripts indicate different samples.}
\begin{align}
  \label{eq:mci-def}
    I = \lim_{N\to\infty} M_N \defeq \lim_{N\to\infty}\sum_{i=1}^N f(\vx_i) \frac{V}{N} = V\expval{f(\vx)}_\sigma,
\end{align}
where $\expval{\cdot}_\sigma$ denotes the expectation value over all points in
$\sigma$ when all points are equally likely. The estimate becomes increasingly
accurate for increasing $N$, and is exact in the limit where $N\to\infty$.

\subsection{Estimating the Error}

For any finite $N$ the result of using \gls{mci} will not be exact. Because of
this, it is immensely useful to be able to assign a statistical certainty to the
result. For instance, say we estimate the ground state energy of a system using
two different wave functions. We perform the integrals and get (to the first
five digits) $\expval{H}_A = \SI{1.0315}{eV}$ and $\expval{H}_B =
\SI{1.0943}{eV}$. Recalling that the lowest energy is the most physically
accurate, can we say that $A$ is better than $B$? No, because we lack
information on the precision of the integrals. Assume that in reality, the
numbers were $\expval{H}_A=\SI{1.03(8)}{eV}$ and
$\expval{H}_B=\SI{1.09(8)}{eV}$, where the numbers in parenthesis indicate the
uncertainty in the last digit. Now we know that the two numbers are not
different in a statistically significant way, and any difference could be
entirely random. We would need to use more sample points to lower the
uncertainty to be able to say anything more about them.\\

We obtain a statistical estimate of the error we make in approximating the
integral by considering the variability of the individual terms of
\cref{eq:mci-def}. Let's start by estimating the variance of the integrand:
\begin{align}
    \Var[f(\vx)] \defeq \sigma^2_N = \frac{1}{N-1} \sum_{i=1}^N\qty(f(\vx_i) - \frac{1}{N}\sum_{j=1}^N f(\vx_j))^2.
\end{align}
It then follows, by the properties of variance,
\begin{align}
  \label{eq:mci-annon-2}
    \Var[M_N] = \Var\qty[ \frac{V}{N} \sum_{i=1}^Nf(\vx_i)] = \frac{V^2}{N^2}\sum_{i=1}^N\Var[f(\vx_i)] = \frac{V^2\sigma^2_N}{N}
\end{align}
\begin{align}
  \label{eq:mci-sem-def}
    \implies \Std[M_N] = \sqrt{\Var[M_N]} = \frac{V\sigma_N}{\sqrt N}.
\end{align}
\Cref{eq:mci-sem-def} is the standard deviation of the sample mean, otherwise
known as the \gls{sem}. It is common use the \gls{sem} as the estimate of uncertainty on
expectation values. Another common choice is to give confidence intervals
based on the \gls{sem}. Assuming the variance is normally
distributed,\footnote{Which for our wave functions is often times at least
  approximately true.} we can say with e.g.\ $\SI{95}{\percent}$ certainty that
the true expectation value, I, is in the interval

\begin{align}
  \qty[CI_{-}^{95}, CI_{+}^{95}] \defeq \qty[M_N - 1.96\Std[M_N], M_N + 1.96\Std[M_N]].
\end{align}

\Cref{eq:mci-sem-def} tells us that the expected statistical error we make when using \gls{mci} goes
like $\mathcal{O}(1 / \sqrt N)$, and depends linearly on the size of the volume
and standard deviation of the integrand itself. This illustrates both the
advantage, and disadvantage of \gls{mci} compared to other,
deterministic integration methods. Its advantage is its simple dependency on the
volume, and its independence from the particular number of dimensions in the
integral. Other methods tend to depend exponentially on the dimensionality, and
as such \gls{mci} is often the best choice for multidimensional integrals. Its
disadvantage is the relatively slow convergence rate, which is asymptotically
much worse than other approaches~\cite{Numerical-Recipes-Press-et-al}.

\subsubsection{Correction for Autocorrelation}

Actually, \cref{eq:mci-annon-2} includes a mistake. We applied a property of
variance on a sum of random variables. The equation is correct if and
only if the samples $\vx_i$ are perfectly independent. Unfortunately for us,
the samples will not always be so, depending on the algorithm used to generate
them. We can sample independently from a uniform distribution and some other
notable distributions (\cref{sec:sampling-arb-prob-dist-funcs}), but not in
general. This will be especially true for the algorithms we use to sample from
arbitrary wave functions (\cref{sec:metro-hastings-alg}).

The version of \cref{eq:mci-annon-2} that is true in general accounts for
autocorrelated samples:\footnote{Covariance is a measure of the joint
  variability between two random variables. Autocorrelation is used to refer to
  the covariance between a random variable and itself at pairs of time points.}

\begin{align}
  \label{eq:mci-annon-3}
    \Var[M_N] = \Var\qty[ \frac{V}{N} \sum_{i=1}^Nf(\vx_i)] = \frac{V^2}{N^2}\sum_{i=1}^N\sum_{j=1}^N\Cov[f(\vx_i), f(\vx_j)].
\end{align}
This is always greater than or equal to \cref{eq:mci-annon-2}, and equal only if
the off-diagonal covariance terms (where $i\neq j$) are all zero. Not accounting
for the autocorrelation in the samples will therefore lead us to underestimate
the error, which is arguably much worse than the opposite.

There is, unfortunately a heavy cost to computing the full covariance.
Typically, to keep the expectation values accurate, we will use very large
values for $N$, often on the order of tens or hundreds of millions. The double loop in
\cref{eq:mci-annon-3} will then quickly become unfeasible to compute, with
trillions or quadrillions of iterations. In practice we need to estimate it.
Yes, we are going to estimate the estimate of the error we make in an estimate.

The strategy we shall use for all later error analysis is called \emph{blocking},
and specifically we use an automated form presented in an article by
\textcite{Jonsson-2018}. Say we have a sequence of sample values $f(x_i)$:

\begin{align}
  D_0 = \mqty[f(x_1) & f(x_2) & \dots & f(x_N)],
\end{align}
and let's assume that there is some non-zero autocovariance between samples that are
sufficiently close in the sequence. The idea of blocking is to group
\say{blocks} of samples together, replacing them by their mean value. For
instance, after one blocking transformation of the above sequence we get:

\begin{align}
  D_1 = \mqty[\qty(\frac{f(x_1) + f(x_2)}{2}) & \qty(\frac{f(x_3) + f(x_4)}{2}) & \dots & \qty(\frac{f(x_{n-1}) + f(x_n)}{2})].
\end{align}
If $N = 2^d$ with $d > 1$ we can repeat the process to obtain $d$ different
sequences $D_i$. These transformations conserve the value of \cref{eq:mci-annon-3}
while decreasing the covariance between samples~\cite{Jonsson-2018}. That implies that measuring
the variance with \cref{eq:mci-annon-2} on each $D_i$ will yield greater and
greater errors, until there is no more covariance and the error estimate
converges.

\Cref{fig:blocking-example-diagram} shows this process in practice. The example
data is $2^{27}$ samples of the mean radial displacement of a single
one-dimensional particle placed in a harmonic oscillator. Due to how the data is
generated, we expect there to be a significant portion of autocorrelation
present. The figure shows the standard error from \cref{eq:mci-annon-2},
calculated on the data sets obtained by performing repeated blocking
transformations. We can see it rises steadily during the first $6$-$7$
transformations, after which the block sizes are big
enough to remove all the covariance and the estimate converges. As
the data set becomes too small the results start to become unpredictable. The
optimal estimate is indicated by the red circle, as determined by the automated
procedure developed by \textcite{Jonsson-2018}. In this particular case this occurred after
$\num{12}$ blocking transformations.

If not explicitly stated otherwise, all error estimates in this thesis will
include this correction.

\begin{figure}[h]
  \centering
  \resizebox{0.7\linewidth}{!}{%
    \input{scripts/blocking-example-diagram.py.tex}
  }
  \caption[Standard error as a function of blocking transformations]{The estimated standard error of a random process excibiting symptoms
    of autocorrelation, shown as a function of the effective size of the data set as
    blocking transformations are applied. As the block sizes increase the
    covariance goes away, while the variance increases. After convergence, the
    optimal estimate is illustrated by the red dot, as determined by an automated blocking
    procedure~\cite{Jonsson-2018}.\citesource{writing/scripts/blocking-example-diagram.py}}
  \label{fig:blocking-example-diagram}
\end{figure}

\subsection{Importance Sampling}

In some suitable cases we can improve quite dramatically on the simple,
straightforward integration approach given in \cref{eq:mci-def} with a technique
known as \gls{is}. To illustrate this, say we
would like to evaluate the following integral:

\begin{align}
    \label{eq:mci-importance-example-func}
    I = \intfy\dd{x}f(x)=\intfy\dd{x} \frac{\exp{-\flatfrac{x^2}{2} }}{\sqrt{2\pi \qty(1 +x^2)}}  = \num{0.78964}.
\end{align}

\begin{figure}
    \centering
    \resizebox{0.7\linewidth}{!}{%
    \begin{tikzpicture}
        \begin{axis}[every axis plot post/.append style={
                mark=none,domain=-5:5,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
            axis x line=bottom, % no box around the plot, only x and y axis
            axis y line=left,
            enlargelimits=upper] % extend the axes a bit to the right and top
            \addplot[semithick, color0] {1/(sqrt(2*pi))*exp(-(x^2)/2)*(1+x^2)^(-0.5)};
            \addplot[semithick, color1] {1/(sqrt(2*pi))*exp(-(x^2)/2)};
            \addlegendentry{$f(x)$}
            \addlegendentry{$\mathcal{N}(0, 1)$}
        \end{axis}
    \end{tikzpicture}
    }
    \caption[Illustration of example probability distributions]{\label{fig:mci-importance-example-func-plot}Plot of the function in
      \cref{eq:mci-importance-example-func}, enveloped by a standard normal distribution.\citesource{writing/MonteCarlo.tex}}
\end{figure}
\noindent The integrand is plotted in \cref{fig:mci-importance-example-func-plot}, and
the observant reader might recognise this as the product of a normal
distribution and a Student-t distribution. The correct value for the integral
is also given, so we have a reference for the results.

For comparison, let's start with the straightforward approach from before.
Because the integral goes to infinity the "volume" $V$ would not be well
defined, and we are forced to truncate the region manually. From looking at the
graph in \cref{fig:mci-importance-example-func-plot} we may say that $x\in
[-5, 5]$ should account for the vast majority of the total integral. That means
we use the following estimate:

\begin{align}
    \label{eq:chp-mc-anon-2}
    I\approx \frac{10}{N}\sum_{i=1}^Nf(x_i)\qq{where} x_i\disteq\text{Uniform}(-5, 5).
\end{align}

\noindent The main issue with this approach is that 1) we need to truncate the integral
manually and 2) no matter where we place the box boundaries we will tend to
sample a lot of $x_i$'s in areas where $f$ gives very small contributions to
the integral. Ideally we would like our sample points to be distributed as
closely as possible to $f$, in order to capture as much information as we can.
This is the idea of \gls{is}. Instead of using the uniform
distribution for sampling, we use some probability distribution which more
closely resembles the integrand, call it $g(x)$. Formally we then restate the integral as follows:

\begin{align}
    I = \intfy\dd{x}f(x) = \intfy\dd{x} \frac{f(x)}{g(x)} g(x).
\end{align}
This can be interpreted as the expectation of $z(x)=\flatfrac{f(x)}{g(x)}$ for $x$'s drawn from $g$, and so the corresponding estimation is then:

\begin{align}
    I \approx \frac{1}{N} \sum_{i=1}^N \frac{f(x_i)}{g(x_i)} \qq{where} x_i\disteq g.
\end{align}
Setting $g = \text{Uniform}(-5, 5)$ recovers \cref{eq:chp-mc-anon-2}, so this is
simply the natural generalization of the standard approach for an arbitrary
\gls{pdf}.

Going back to the example, we should now choose a distribution function that
closely resembles the integrand, while still being simple to sample from. In
this (contrived) example, a natural choice is to use the standard normal
distribution, $g = \mathcal{N}(0, 1)$. In
\cref{fig:mci-importance-example-func-plot} we can see how $g(x)$ is
enclosing $f(x)$ much more tightly than any rectangular box might hope to.

\begin{figure}
   \centering
    \resizebox{0.7\linewidth}{!}{%
        \input{scripts/monte_carlo_int_example.py.tex}
    }
    \caption[Convergence of Monte Carlo Integration]{\label{fig:mci-importance-example-func-convergence}Convergence of
    the integral in \cref{eq:mci-importance-example-func}, using regular
    \gls{mci} and \gls{is} with $g = \mathcal{N}(0,
    1)$. The points indicate the approximated value of the integral for each
    particular experiment, and the shaded areas indicate the corresponding
    $\SI{95}{\percent}$ confidence intervals for the expected value. We see the
    latter displaying both greater accuracy and tighter confidence intervals.
    \citesource{writing/scripts/monte_carlo_int_example.py}
    }
\end{figure}

\Cref{fig:mci-importance-example-func-convergence} shows the convergence of
the \gls{mc} approximations towards the correct value for an increasing
number of sampled points. The drawn line is the mean value at each run, while
the shaded areas show the corresponding $\SI{95}{\percent}$ confidence
intervals. Because of the more suited sampling distribution, we obtain results
which are more accurate, and perhaps most importantly, tighter confidence
bounds. In general, using better sampling distributions will tend to give us
lower variance results.

\section{Sampling from Arbitrary Probability Density Functions}
\label{sec:sampling-arb-prob-dist-funcs}

So far we have taken for granted the ability to sample numbers from various
\glspl{pdf}. We dedicate this section to discuss sampling from the bottom up,
culminating in a detailed presentation of the most central algorithm in this
thesis, the Metropolis-Hastings algorithm.

\subsection{The Uniform Distribution}

The basic building block upon which we shall build all other random sampling
techniques is the ability to sample random numbers from the uniform
distribution, $\text{Uniform}(a, b)$. This is the simplest distribution
possible, where all numbers in the range $[a, b]$ have the same probability
density. The definition of its \gls{pdf} is simply:

\begin{align}\label{eq:uniform-pdf-definition}
    p_U\qty(x\;\middle|\; a, b) \defeq  \begin{cases}
        \frac{1}{b - a} &\qfor x\in [a, b]\\
        0&\qotherwise
    \end{cases}.
\end{align}
Most commonly we operate only with the \emph{standard uniform distribution}, $\text{Uniform}(0, 1)$. Any other range can be simply related via the trivially verifiable identity
\begin{align}
    p_U\qty(x\;\middle|\; a,b) = p_U\qty( \frac{x-a}{b-a} \;\middle|\; 0, 1).
\end{align}

But how exactly do we obtain random realizations from this \gls{pdf}? First we need
to depart from the typical notion of randomness, and realize that we are unable to
write an algorithm which produces a truly random result. While we could in
principle rely on nature to provide sources of complete, true randomness (such as
the exact behavior of a quantum mechanical observable), this is impractical
when we need fast, on-demand samples. Instead we settle for
\emph{pseudo-random} samples. A pseudo-random sequence $x_1, x_2,\dots x_n$
implies that one cannot\footnote{In any practical way, barring exhaustive trial
and error of every conceivable underlying algorithm.} predict $x_{n+1}$ without
insight into how the sequence is generated. In other words, the (ideal)
pseudo-random sequence is indistinguishable from a truly random sequence for
anyone observing the numbers $x_i$ alone.

There are a multitude of algorithms that can generate a sequence of
pseudo-random \emph{integers}, all with varying properties.\footnote{Such properties
could include range of possible numbers, period, level of bias and how
cryptographically secure they are.} The simplest family of
such algorithms is a \gls{lcg}~\cite{Knuth-1997-ACP-270146}. It defines a
pseudo-random sequence by the simple recurrence relation
\begin{align}
    \label{eq:linear-congruential-generator-relation}
    x_{n+1} = (ax_n + c)\mod m,
\end{align}
where $a, c$ and $m$ are constants which determine the behaviour of the
algorithm, and $x_0$ needs to be specified manually. For instance,
\textcite{Numerical-Recipes-Press-et-al} use $a = 1664525$, $c=1013904223$ and
$m=2^{32}$.

This produces uniformly distributed integers in the range $[0, m)$. Numbers
from the standard uniform distribution can then simply be obtained by dividing
the integers by $m$. The complete algorithm is shown in \cref{alg:uniform-LCG-sampling}.

\begin{algorithm}[h]
    \caption{Sampling from $\text{Uniform}(l, u)$}
    \label{alg:uniform-LCG-sampling}
    \begin{algorithmic}[1]
        \Require $a, c, m$ and $x_0$
        \Function{unif}{lower bound $l$, upper bound $u$}
            \State $x\gets x_0$
            \Repeat
                \State $x\gets (ax + c)\mod m$
                \State \Yield $(\flatfrac{x}{m}) \times (u - l) + l$
            \Until{done}
        \EndFunction
    \end{algorithmic}
\end{algorithm}

\subsection{Inverse Transform Sampling}

Armed with uniformly distributed random numbers, we now turn towards generating
numbers from other \glspl{pdf}. Let $U\disteq\text{Uniform}(0,1)$ be a uniformly
distributed stochastic variable, and let $X$ be a stochastic variable
associated with a \gls{pdf}, $p$, and a corresponding \gls{cdf}, $F$, i.e.

\begin{align}
    F(x) = \text{Pr}\qty(X\leq x) = \int_{-\infty}^x \dd{t}p(t).
\end{align}
We would like to define a transformation $T:[0, 1]\mapsto\mathbb{R}$ that can map from uniform numbers to numbers that follow the given \gls{cdf}, i.e. define $T$ such that $T(U)\disteq X$. We have:
\begin{align}
    F(x) = \text{Pr}\qty(X\leq x) = \text{Pr}\qty(T(U)\leq x) = \text{Pr}\qty(U \leq T^{-1}(x)) = T^{-1}(x),
\end{align}
because $T^{-1}(x)\in[0, 1]$ by definition, and assuming that $T^{-1}(x)$ is strictly monotone. It follows then that $F^{-1}(U)\disteq X$, i.e. if the \gls{cdf} is strictly monotone (as \glspl{cdf} should be) then we can get realizations of $X$ by applying $F^{-1}$ to realizations of $U$.

The algorithm to sample from any \gls{pdf} with a tractable inverse \gls{cdf} is
then extremely simple, and for completeness it is listed in
\cref{alg:inverse-transform-sampling}.

\begin{algorithm}[h]
    \caption{Inverse transform sampling}
    \label{alg:inverse-transform-sampling}
    \begin{algorithmic}[1]
        \Require Cumulative distribution function $F$
        \Ensure Random $x$ with \gls{cdf} equal to $F$
        \Repeat
          \State $u\gets\text{unif}(0,1)$
          \State $x\gets F^{-1}(u)$
          \State \Yield $x$
        \Until{done}
    \end{algorithmic}
\end{algorithm}

\subsection{The Metropolis-Hastings Algorithm}
\label{sec:metro-hastings-alg}

\Cref{alg:inverse-transform-sampling} works great for a number of standard
\glspl{pdf}. For many cases, however, we do not have a tractable form for the inverse
\gls{cdf}, which renders the algorithm useless. So what about the wave functions we
are interested in? The \gls{pdf} in question is, as defined in \cref{eq:wavefunc-probability-density-def},

\begin{align}
    P_{\psi}(\vX) \defeq \frac{\abs{\psi}^2}{\int\dd{\vX}\abs{\psi}^2}.
\end{align}
Sadly, computing $F^{-1}$ for this \gls{pdf}, if even possible, would be a very costly
operation. In fact, the normalization integral in the denominator is enough to
make practical sampling from $P_\psi$ impossible. Even worse, considering that
$\psi$ is updated continuously throughout a \gls{vmc} calculation, we could not even
cache the result of the integral after computing it once.

Luckily, we have a solution: The Metropolis-Hastings Algorithm. This algorithm
is the workhorse behind thousands of applications, including \gls{vmc}, and is
considered to be among the most influential algorithms of the 20th century.
While the algorithm itself is quite simple, the argument for \emph{why} it
works is a little more involved. The following sections will be dedicated to the
mathematical underpinnings. In the mean time, the full algorithm is shown
in~\cref{alg:metropolis-hastings-general}.

\begin{algorithm}[h]
    \caption{Metropolis-Hastings sampling}
    \label{alg:metropolis-hastings-general}
    \begin{algorithmic}[1]
        \Require Probability density $P(\vx)$, proposal distribution $q\qty(\vx'\;\middle|\;\vx)$
        \Ensure Random $\vx$ drawn from $P$
        \State Initialize $\vx$, randomly or otherwise
        \Repeat
          \State $\vx'\gets q\qty(\vx'\;\middle|\;\vx)$
          \State $u\gets\text{unif}(0, 1)$
          \State $A\gets \flatfrac{P(\vx')q\qty(\vx\;\middle|\;\vx')}{P(\vx)q\qty(\vx'\;\middle|\;\vx)}$
          \If{$u \leq A$}
            \State $\vx \gets \vx'$
          \EndIf
          \State \Yield $\vx$
        \Until{done}
    \end{algorithmic}
\end{algorithm}

The most important thing about~\cref{alg:metropolis-hastings-general} is the
fact that we only need to know the ratios of probabilities,
$\flatfrac{P(\vx')}{P(\vx)}$. This means that we don't need the probabilities to
be normalized to unity\footnote{We do need the probabilities to be
\emph{normalizable} though. In our case, where $P\propto \abs{\Psi}^2$, we have
already assumed this requirement in~\cref{sec:requirements-of-wave-functions}.},
meaning we don't have to compute the costly integral
in~\cref{eq:wavefunc-probability-density-def}. For our purposes this is
essential, and~\cref{alg:metropolis-hastings-general} is the reason why \gls{vmc} is
possible.

\subsubsection{Formal Derivation}

The Metropolis-Hastings algorithm builds on what is called a \emph{Markov Chain}. A
Markov Chain is a type of stochastic model that describes a sequence of possible
states. We imagine some space of possible states and a set of transition
probabilities, $T\qty(\vx'\;\middle|\;\vx)$, describing the likelihood of moving between them.
The defining property of a Markov Chain, as opposed to other stochastic
processes is that the transition probabilities $T\qty(\vx'\;\middle|\;\vx)$ from a state $\vx$
to a state $\vx'$ depends only on the current state and not on any previous
states. In other words, it matters only where you are, not where you have been.

For well behaved Markov Chains, as the number of steps in the chain increases,
the distribution of the states will asymptotically approach a stationary
distribution, $\pi(\vx)$. The Metropolis-Hastings algorithm works by carefully
constructing $T\qty(\vx'\;\middle|\;\vx)$ such that $\pi(\vx)=P(\vx)$, where $P(\vx)$ is the
desired \gls{pdf}.

In order to find the correct $T\qty(\vx'\;\middle|\;\vx)$, we require that the stationary
distribution $\pi(\vx)$ exists and that it is unique:

\begin{description}
\item[Existence:] A sufficient condition for the existence of $\pi(\vx)$ is that
  of \emph{detailed balance}. This says that the probability of being in state
  $\vx$ and transitioning to $\vx'$ is equal to the probability of being in state
  $\vx'$ and transitioning to $\vx$:
  \begin{align}
    \label{eq:detailed-balance}
    \pi\qty(\vx)T\qty(\vx'\;\middle|\;\vx) = \pi\qty(\vx')T\qty(\vx\;\middle|\;\vx').
  \end{align}
\item[Uniqueness:] The distribution $\pi(\vx)$ is unique if the Markov chain is
  \emph{ergodic}. This is the case when we don't return to the same state after
  a fixed number of transitions, and that all states can be reached in a finite
  number of transitions.
\end{description}
Deriving the correct $T\qty(\vx'\;\middle|\;\vx)$ starts by factoring the transition
probabilities into a proposal distribution, $q\qty(\vx'\;\middle|\;\vx)$, and an
acceptance ratio $A\qty(\vx',\vx)$. The idea is to use the former to propose the
next state, and use the latter to accept or reject the proposal. Inserting this
into \cref{eq:detailed-balance} and rearranging we get:

\begin{align}
  \frac{A\qty(\vx',\vx)}{A\qty(\vx,\vx')} = \frac{P\qty(\vx')}{P\qty(\vx)}\frac{q\qty(\vx\;\middle|\;\vx')}{q\qty(\vx'\;\middle|\;\vx)}.
\end{align}
We can now freely choose an acceptance ratio that satisfies the above. The
common choice is:

\begin{align}
  A\qty(\vx',\vx) = \min\qty(1, \frac{P\qty(\vx')}{P\qty(\vx)}\frac{q\qty(\vx\;\middle|\;\vx')}{q\qty(\vx'\;\middle|\;\vx)}).
\end{align}

At this point, all that remains is to specify a proposal distribution
$q\qty(\vx'\;\middle|\;\vx)$. Depending on the choice of $q$ we get a sampler with
different attributes. We have implemented two version, presented in the following
sections.

\subsubsection{Metropolis Algorithm}

The simplest choice, referred to as simply the Metropolis algorithm, is to
choose a distribution such that
$q\qty(\vx'\;\middle|\;\vx)=q\qty(\vx\;\middle|\;\vx')$. That way the acceptance
ratio simplifies significantly, fully canceling out $q$. In our implementation
we have used a uniform proposal distribution:

\begin{align}
  q\qty(\vx' \;\middle|\; \vx) &= p_U\qty(\vx' \;\middle|\; \vx -\frac{\Delta x}{2}, \vx + \frac{\Delta x}{2}),
\end{align}
where $\Delta x$ is a \emph{step size} that regulates how large each
perturbation should be. This leads to the simplest form of
\cref{alg:metropolis-hastings-general}, given for completeness in
\cref{alg:metropolis-simple}. The algorithm given here is adapted particularly
for our purposes, where we want to sample a matrix, $\mat
X\in\mathbb{R}^{N\times D}$, for $N$ particles in $D$ dimensions. In particular,
our implementation yields a new sample after moving only one of the particles.
This is a common case for \gls{vmc} applications, because this allows for a code
optimization that is crucial when using Slater determinants in wave functions.
We don't use such wave functions in this thesis, but future work might benefit
from this choice.

\begin{algorithm}[h]
    \caption{Metropolis sampling}
    \label{alg:metropolis-simple}
    \begin{algorithmic}[1]
        \Require Probability density $P(\vX)=\abs{\psi(\vX)}^2$, step size $\Delta x$
        \Ensure Random $\vX$ drawn from $P$
        \State Initialize $\vX$, randomly or otherwise
        \Repeat
          \For{$i \gets 1:N$}
            \State $\vX'\gets \vX$
            \For{$d \gets 1:D$}
              \State $\delta\gets \text{unif}(-0.5, 0.5)$
              \State $X_{i,d}'\gets X_{i,d} + \Delta x \cdot \delta$
            \EndFor
            \State $u\gets\text{unif}(0, 1)$
            \State $A\gets \flatfrac{P\qty(\vX')}{P\qty(\vX)}$
            \If{$u \leq A$}
              \State $\vX \gets \vX'$
            \EndIf
            \State \Yield $\vX$
          \EndFor
        \Until{done}
    \end{algorithmic}
\end{algorithm}

\subsubsection{Importance Sampling}

\Cref{alg:metropolis-simple} suffers from the same issues that ordinary \gls{mci}
does, as opposed to the enhancement of \gls{is}. The proposal
distribution used in the Metropolis algorithm, while simple to implement, can
certainly be improved upon. The idea is to guide the random walk in space
towards areas of greater probability. We will now give a brief physical
motivation for the strategy, glossing over most technical details.

Particles will tend towards regions of space where $P(\vX)$ is large. We may say
that this is the result of a \emph{quantum drift force}. Without further
motivation, we define the drift force acting on particle $k$ as follows:

\begin{align}
  \vb{F}_k(\vX) = \frac{2\grad_k{\Psi(\vX)}}{\Psi(\vX)},
\end{align}
which points in the direction of greatest increase in $\Psi$ with respect to
particle $k$'s position. We also expect the
system to exhibit some degree of random motion, as it is a quantum system. This
combination of drift and random motion is described by the Langevin equation~\cite{Langevin-1908}, % TODO, what is the correction?

\begin{align}
 \pdv{\vx_k}{t} &= D\vb{F}_k + \vb{\eta},
\end{align}
where $D$ is called the drift coefficient, and $\vb{\eta}$ is a vector of
uniformly distributed random values (with zero mean) and accounts for the random
motion. We fix $D=\flatfrac{1}{2}$. Applying Euler's method to the Langevin
equation we can get:

\begin{align}
  \vx_k' = \vx_k + \frac{1}{2}\vb{F}_k\,\Delta t + \vb{\xi}\sqrt{\Delta t},
\end{align}
where $\Delta t$ is a time step similar to $\Delta x$ for plain Metropolis, and
$\vb{\xi}$ is a vector of values drawn from the standard normal distribution.
This is our new recipe for generating proposals $\vx'$. The final piece is
to find the corresponding \gls{pdf},
$q\qty(\vx'\;\middle|\;\vx)$.

To this end, we consider the Fokker-Planck equation, which for one particle in
one dimension is written as

\begin{align}
  \label{eq:mc-annon-4}
  \pdv{\Psi}{t} &= D\pdv{}{x}\qty(\pdv{}{x} - F)\Psi,
\end{align}
where $D$ is as before and $F$ is the one-dimensional analog of $\vb{F}_k$. This
describes the time-evolution of a probability distribution under the influence
of a drift force and random impulses. We've let $\Psi$ play the role of the
probability distribution. \cref{eq:mc-annon-4} yields a solution given by the
following Green's function (for one particle):

\begin{align}
  \label{eq:mc-annon-5}
  G\qty(\vx_k', \,\vx_k,\,\Delta t) &\propto \exp[-\frac{\norm{\vx_k' - \vx_k - D\Delta t\,\vb{F}_k}^2}{4D\Delta t}].
\end{align}
This is interpreted as the probability of transitioning to position $\vx_k'$
from $\vx_k$ within a time interval $\Delta t$, so that finally we have the
proposal distribution:

\begin{align}
  q\qty(\vx'\;\middle|\;\vx) &=G\qty(\vx', \,\vx,\,\Delta t).
\end{align}
Note that because we only need the ratio of $q$ values, there is no problem
that \cref{eq:mc-annon-5} only gives the proportionality. The final algorithm is
shown in \cref{alg:metropolis-importance}.
See \cref{sec:verify-sampling} for a demonstration of how
\cref{alg:metropolis-importance} compares to \cref{alg:metropolis-simple}.

\begin{algorithm}[h]
    \caption{Metropolis-Hastings importance sampling}
    \label{alg:metropolis-importance}
    \begin{algorithmic}[1]
        \Require Probability density $P(\vX) = \abs{\psi(\vX)}^2$, step size $\Delta t$
        \Ensure Random $\vX$ drawn from $P$
        \State Initialize $\vX$, randomly or otherwise
        \Repeat
          \For{$i \gets 1:N$}
            \State $\vX'\gets \vX$
            \For{$d \gets 1:D$}
              \State $\xi\gets \mathcal{N}(0, 1)$
              \State $X_{i,d}'\gets X_{i,d} + \sqrt{\Delta t} \cdot \xi +
              \frac{1}{2}\Delta t\,(\vb{F}_i(\psi))_d$
            \EndFor
            \State $u\gets\text{unif}(0, 1)$
            \State $A\gets \flatfrac{P\qty(\vX')}{P\qty(\vX)}$
            \State $A\gets A\cdot \flatfrac{q\qty(\vx_i\;\middle|\;\vx_i')}{q\qty(\vx_i'\;\middle|\;\vx_i)}$
            \If{$u \leq A$}
              \State $\vX \gets \vX'$
            \EndIf
            \State \Yield $\vX$
          \EndFor
        \Until{done}
    \end{algorithmic}
\end{algorithm}
\end{document}


\section{The PID actions}
\subsection{}

\begin{frame}
\frametitleTC{Foreword}
\framesubtitleTC{}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item We carry out this treatise in a DT LTI (comments on saturations later on) with sampling time $T_s$.
 \item This said, from the PID quasi-algorithm on slide~\ref{pag:PID-quasi-alg} we get the error-to-action
       transfer functions
       \begin{displaymath}
        \begin{array}{lclcl}
         C_P(z) &:=& \cfrac{u_P(k)}{e(k)} &=& K, \\ \\
         C_I(z) &:=& \cfrac{u_I(k)}{e(k)} &=& \cfrac{KT_s}{T_i} \, \cfrac{z}{z-1}, \\ \\
         C_D(z) &:=& \cfrac{u_D(k)}{e(k)} &=& \cfrac{KT_d(1-\beta)}{T_s} \, \cfrac{z-1}{z-\beta}.
        \end{array}
       \end{displaymath}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Foreword}
\framesubtitleTC{}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Block diagram of a PID control loop evidencing the three actions
       \begin{center}
        \includegraphics[width=0.85\columnwidth]{./Unit-07/img/PIDloop-actions.pdf}
       \end{center}
 \item \vspace{2mm}NOTE: the disturbance is added to the control signal, i.e., $H(z)=P(z)$;\\
       such a disturbance is called a \TC{load} or a \TC{matched} disturbance.
 \item Load disturbances can arise every time the disturbing and the\\
       control actions are physically homogeneous, which is not infrequent\\
       ---e.g., the control is allocating a resource, and the disturbing entity\\
       is somebody subtracting some amount of the same resource.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Computing the three actions}
\framesubtitleTC{}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item From
       \begin{center}
        \includegraphics[width=0.70\columnwidth]{./Unit-07/img/PIDloop-actions.pdf}
       \end{center}
       and setting $C(z) = C_P(z)+C_I(z)+C_D(z)$, we readily get
       \begin{displaymath}
        \begin{array}{llcll}
          \cfrac{u_P(k)}{w(k)} &\mkern-12mu =  \cfrac{C_P(z)}{1+P(z)C(z)},       &\quad&
          \cfrac{u_P(k)}{d(k)} &\mkern-12mu = -\cfrac{P(z)C_P(z)}{1+P(z)C(z)}, \\
          \cfrac{u_I(k)}{w(k)} &\mkern-12mu =  \cfrac{C_I(z)}{1+P(z)C(z)},       &\quad&
          \cfrac{u_I(k)}{d(k)} &\mkern-12mu = -\cfrac{P(z)C_I(z)}{1+P(z)C(z)}, \\
          \cfrac{u_D(k)}{w(k)} &\mkern-12mu =  \cfrac{C_D(z)}{1+P(z)C(z)},       &\quad&
          \cfrac{u_D(k)}{d(k)} &\mkern-12mu = -\cfrac{P(z)C_D(z)}{1+P(z)C(z)}.
        \end{array}
       \end{displaymath}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Computing the three actions}
\framesubtitleTC{}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item We now examine the behaviour of the scheme above with a first-order process\\
       with gain $\mu$ and time constant $T$, that recalling slide~\ref{pag:FOsystem-DT}, in transfer function\\
       form reads
       \begin{displaymath}
        P(z) = \frac{\mu\frac{T_s}{T+T_s}z}{z-\frac{T}{T+T_s}}.
       \end{displaymath}
 \item We start out with a PI tuned by cancellation as per the formul{\ae} of slide~\ref{pag:PItuning-lambda},\\
       that is,
       \begin{displaymath}
        K   = \frac{T}{\lambda\mu}, \quad
        T_i = T.
       \end{displaymath}
       where $\lambda$ is the desired closed-loop time constant (1/5 of the desired\\
       settling time).
 \item Then we add a bit of D action, and comment on all of the results.
 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Computing the three actions}
\framesubtitleTC{Scilab script (1/3)}
\myPause
 {\scriptsize
 \begin{verbatim}
 clear; clc;                                          // clear workspace & console
 z      = %z;                                         // allow to omit lots of %'s below

 mu     = 1;                                          // process gain
 T      = 10;                                         // and time constant
 lambda = 4;                                          // desired closed-loop TC
 Beta   = 0.6;                                        // derivative filter
 Ts     = 1;                                          // sampling time
 tfin   = 50;                                         // simulation end time

 K      = T/lambda/mu;                                // PID tuning
 Ti     = T;
 Td     = 0;

 P      = syslin(Ts,z*Ts/(T+Ts)/(z-T/(T+Ts)));        // process and controller
 Cp     = K;                                          // transfer functions:
 Ci     = syslin(Ts,K*Ts/Ti*z/(z-1));                 // a number instead of
 Cd     = syslin(Ts,K*Td*(1-Beta)/Ts*(z-1)/(z-Beta)); // 'd' sets the sampling
 C      = Cp+Ci+Cd;                                   // time for a DT system
 \end{verbatim}
 }
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Computing the three actions}
\framesubtitleTC{Scilab script (2/3)}
\myPause
 {\scriptsize
 \begin{verbatim}
 w2y    = tf2ss(C*P/(1+C*P));           // transfer function from w to y
 d2y    = tf2ss(P/(1+C*P));             // transfer function from d to y
 w2u    = tf2ss(C/(1+C*P));             // transfer function from w to u
 d2u    = tf2ss(-C*P/(1+C*P));          // transfer function from d to u
 w2up   = tf2ss(Cp/(1+C*P));            // transfer function from w to up
 w2ui   = tf2ss(Ci/(1+C*P));            // transfer function from w to ui
 w2ud   = tf2ss(Cd/(1+C*P));            // transfer function from w to ud
 d2up   = tf2ss(-P*Cp/(1+C*P));         // transfer function from d to up
 d2ui   = tf2ss(-P*Ci/(1+C*P));         // transfer function from d to ui
 d2ud   = tf2ss(-P*Cd/(1+C*P));         // transfer function from d to ud

 t      = 0:Ts:tfin;                    // time vector
 wd     = ones(t);                      // step with one zero sample
 wd(1)  = 0;                            // at the beginning
 yw     = dsimul(w2y,wd)';              // y in response to a w step
 yd     = dsimul(d2y,wd)';              // y in response to a d step
 uw     = dsimul(w2u,wd)';              // u in response to a w step
 ud     = dsimul(d2u,wd)';              // u in response to a d step
 upidw  = dsimul([w2up;w2ui;w2ud],wd)'; // u{p,i,d} in response to a w step
 upidd  = dsimul([d2up;d2ui;d2ud],wd)'; // u{p,i,d} in response to a d step
 \end{verbatim}
 }
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Computing the three actions}
\framesubtitleTC{Scilab script (3/3)}
\myPause
 {\scriptsize
 \begin{verbatim}
 hf=scf(0); clf; hf.figure_size = [1200,700];                    // plot stuff: see the Scilab
 subplot(321); title('Responses to a w unit step'); ylabel('y'); // documentation for technical
   plot(0,0,'k');                                                // details here inessential
   plot(t',ones(t'),'k:');
   plot(t',yw,'b','linewidth',4);
 subplot(322); title('Responses to a d unit step');
   plot(t',zeros(t'),'k:');
   plot(t',yd,'b','linewidth',4);
 subplot(323); xlabel('time'); ylabel('u,up,ui,ud (g,b,r,m)');
   plot(0,0,'k');
   plot(t',uw,'g','linewidth',4);
   plot(t',upidw(:,1),'b','linewidth',2);
   plot(t',upidw(:,2),'r','linewidth',2);
   plot(t',upidw(:,3),'m','linewidth',2);
 subplot(324); xlabel('time');
   plot(0,0,'k');
   plot(t',ud,'g','linewidth',4);
   plot(t',upidd(:,1),'b','linewidth',2);
   plot(t',upidd(:,2),'r','linewidth',2);
   plot(t',upidd(:,3),'m','linewidth',2);
 \end{verbatim}
 }
\end{frame}

\begin{frame}
\frametitleTC{Observing the three actions}
\framesubtitleTC{Scilab script -- example of the output}
\myPause
 \begin{center}
  \includegraphics[width=0.75\columnwidth]{./Unit-07/img/PIDactions-scilab-01.pdf}
 \end{center}
 \begin{itemize}[<+-| alert@+>]
 \item Top row: responses of $y$ to a unit step on $w$ (left) and on $d$ (right).
 \item Bottom row: breakdown of $u$ (green) into $u_P$ (blue), $u_I$ (red),\\
       and $u_D$ (magenta).
 \item Note that at steady state $u$ is made only of integral action.
 \item Let us now play around with PID parameters, also not following the\\
       tuning rule exactly, and see what happens.
 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Observing the three actions}
\framesubtitleTC{Playing around with PID parameters -- a few suggestions (1/3)}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Add some D action:
       {\scriptsize
       \begin{verbatim}
       K   = T/lambda/mu;
       Ti  = T;
       Td  = 0.2*Ti;
       \end{verbatim}
       }
 \item \vspace{-3mm}Speeds up initial transient, hardly any effect on settling time, just a \emph{very} small\\
       overshoot:
       \begin{center}
        \includegraphics[width=0.60\columnwidth]{./Unit-07/img/PIDactions-scilab-02.pdf}
       \end{center}
 \item Note that clearly the D action vanishes as well toward steady state \\
       (and faster than P).
 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Observing the three actions}
\framesubtitleTC{Playing around with PID parameters -- a few suggestions (2/3)}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Back to PI, but halve the integral time:
       {\scriptsize
       \begin{verbatim}
       K   = T/lambda/mu;
       Ti  = 0.5*T;
       Td  = 0;
       \end{verbatim}
       }
 \item \vspace{-3mm}Disturbance response improved at the cost of a slight oscillation\\
       ---i.e., some stability reduction:
       \begin{center}
        \includegraphics[width=0.60\columnwidth]{./Unit-07/img/PIDactions-scilab-03.pdf}
       \end{center}
 \item Try adjusting $K$ to eliminate the oscillation in the set point response.\\
       Do you succeed? Or just alter the oscillation period?
 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{Observing the three actions}
\framesubtitleTC{Playing around with PID parameters -- a few suggestions (3/3)}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item PID again, but with a definitely excessive integral time:
       {\scriptsize
       \begin{verbatim}
       K   = T/lambda/mu;
       Ti  = 4*T;
       Td  = 0.2*Ti;
       \end{verbatim}
       }
 \item \vspace{-3mm}When P and D vanish, I is still far from playing its full role:
       \begin{center}
        \includegraphics[width=0.60\columnwidth]{./Unit-07/img/PIDactions-scilab-04.pdf}
       \end{center}
 \item Try removing the D action. What effects do you observe on the set\\
       point and the disturbance response? Do these correspond to your\\
       expectations?
 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{The P, I, and D actions}
\framesubtitleTC{Suggestions for further activities}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Take the Scilab script above and add some noise on the feedback path---i.e., the \TC{measurement}
       of $y$ fed back to $C$ is corrupted by noise. Hint: Scilab has a \texttt{rand()} and a \texttt{randn()}
       function for uniformly and normally distributed random numbers.
 \item Always remember:
       \begin{itemize}[<+-| alert@+>]
       \item we would like to control physical quantities,
       \item but we can only control measurements.
       \end{itemize}
 \item \vfill Experiment with different numbers, observe, and comment.
 \end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitleTC{The P, I, and D actions}
\framesubtitleTC{Takeaways}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Looking at a response and diagnosing: too much P action, too few I,...
 \item A keyword for the curious here is ``tuning maps''.
 \item \vspace{2mm} Use your \TC{systems theory} knowledge to confirm or debunk some common beliefs:
       \begin{itemize}[<+-| alert@+>]
       \item adding D always makes a system faster --- sure, given one of the tests above?
       \item too much D destabilises --- hmmm, or maybe just amplifies noise?
       \item increasing $K$ does not reject disturbances faster, for that you need\\
             to reduce $T_i$ --- i.e., not just tune by cancellation,
       \item ...and so forth.
       \end{itemize}
 \item If interested go through the many PID-related web sites, read, test\\
       (you have \TC{methods} \& tools) and subject the material you found\\
       to a system-theoretically educated analysis.
 \item This is what we meant right from the outset for ``using (PID)\\
       control consciously''.
 \end{itemize}
\end{frame}

%Myths debunked
% D makes faster...

%Lessons learnt:
% diagnose, tuning maps


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{Sweave}

\title{G4.P-1}
\author{David Emanuel Craciunescu \and Laura P<e9>rez Medeiro}

\begin{document}
\input{PL3-G4-concordance}

\maketitle

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

\section{Data analysis using Hunt decision algorithm and linear regression for qualifications and planets data}

  \subsection{}The data use in this exercise will be nine students marks and califications, as it has been done in the teorical classes. For this analysis, it wil be used the gain information meause, using Gini as the impurity measure.

  First step in the analysis is read the data, which is contains in a txt file called qualifications.txt:

\begin{Schunk}
\begin{Sinput}
> library(utils)
> qualifications <- read.table("qualification.txt")
> sample = data.frame(qualifications)
\end{Sinput}
\end{Schunk}

In order to make the analysis, the package rpart will be used. this means, that it should be install before working with the dataset. In order to manage R packages, it will be used Packrat

\begin{Schunk}
\begin{Sinput}
> library(rpart)
> clasification = rpart(C.G ~ .,
+                       data = sample,
+                       method = "class",
+                       minsplit = 1)
> clasification
\end{Sinput}
\begin{Soutput}
n= 9

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 9 3 Ss (0.3333333 0.6666667)
  2) Labo=A,B 5 2 Ap (0.6000000 0.4000000)
    4) Pract=A,B 3 0 Ap (1.0000000 0.0000000) *
    5) Pract=C,D 2 0 Ss (0.0000000 1.0000000) *
  3) Labo=C,D 4 0 Ss (0.0000000 1.0000000) *
\end{Soutput}
\end{Schunk}

Another package that can be used to do this analysis is tree:
\begin{Schunk}
\begin{Sinput}
> library(tree)
> (clasificationTree = tree(C.G ~ .,
+                           data = sample,
+                           mincut = 1,
+                           minsize = 2)
+ )
\end{Sinput}
\begin{Soutput}
node), split, n, deviance, yval, (yprob)
      * denotes terminal node

1) root 9 11.46 Ss ( 0.3333 0.6667 )
  2) Labo: A,B 5  6.73 Ap ( 0.6000 0.4000 )
    4) Pract: A,B 3  0.00 Ap ( 1.0000 0.0000 ) *
    5) Pract: C,D 2  0.00 Ss ( 0.0000 1.0000 ) *
  3) Labo: C,D 4  0.00 Ss ( 0.0000 1.0000 ) *
\end{Soutput}
\begin{Sinput}
> clasificationTree
\end{Sinput}
\begin{Soutput}
node), split, n, deviance, yval, (yprob)
      * denotes terminal node

1) root 9 11.46 Ss ( 0.3333 0.6667 )
  2) Labo: A,B 5  6.73 Ap ( 0.6000 0.4000 )
    4) Pract: A,B 3  0.00 Ap ( 1.0000 0.0000 ) *
    5) Pract: C,D 2  0.00 Ss ( 0.0000 1.0000 ) *
  3) Labo: C,D 4  0.00 Ss ( 0.0000 1.0000 ) *
\end{Soutput}
\end{Schunk}
  \subsection{} In this second part, tha dataset use is planets.txt. To this dataset, linear regression will be applied.

As it has been done before, the first step consist on reading data from a \textit{txt} file:

\begin{Schunk}
\begin{Sinput}
> library(utils)
> data <- read.table("planets.txt")
> data = data.frame(data)
> names(data)
\end{Sinput}
\begin{Soutput}
[1] "Radius"  "Density"
\end{Soutput}
\end{Schunk}

In order to quantify the correlation between the variables, it will be calculated the coeficient's matrix correlation:

\begin{Schunk}
\begin{Sinput}
> cor(data)
\end{Sinput}
\begin{Soutput}
          Radius  Density
Radius  1.000000 0.371063
Density 0.371063 1.000000
\end{Soutput}
\end{Schunk}

Then, it will be calculated and representated the minimun square error line:
\begin{Schunk}
\begin{Sinput}
> regression <- lm( Density~Radius, data)
> summary(regression)
\end{Sinput}
\begin{Soutput}
Call:
lm(formula = Density ~ Radius, data = data)

Residuals:
Mercurio    Venus   Tierra    Marte
 0.70312 -0.01253  0.24566 -0.93624

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   4.3624     1.2050   3.620   0.0685 .
Radius        0.1394     0.2466   0.565   0.6289
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.846 on 2 degrees of freedom
Multiple R-squared:  0.1377,	Adjusted R-squared:  -0.2935
F-statistic: 0.3193 on 1 and 2 DF,  p-value: 0.6289
\end{Soutput}
\end{Schunk}

The equation's line is y = 4.3624 + 0.1394x

\begin{Schunk}
\begin{Sinput}
> library(gplots)
> par(mar = rep(2,4))
> plot(data$Density, data$Radius)
> abline(regression)
\end{Sinput}
\end{Schunk}

Finally, it is necessary to calculate ANOVA in order to analysize correctly the relation between variables.

\begin{Schunk}
\begin{Sinput}
> anova <- aov(Density~Radius, data)
> summary(anova)
\end{Sinput}
\begin{Soutput}
            Df Sum Sq Mean Sq F value Pr(>F)
Radius       1 0.2286  0.2286   0.319  0.629
Residuals    2 1.4314  0.7157
\end{Soutput}
\end{Schunk}

\section{Data analysis using Hunt decision algorithm and linear regression for vehicules and pairs of data}

The following part consist on doing the same analysis as it has been done before, but now with new datasets.
\begin{Schunk}
\begin{Sinput}
> library(utils)
> vehicules <- read.table("vehiculos.txt")
> sampleV = data.frame(vehicules)
\end{Sinput}
\end{Schunk}
In this file, TC = license type, NR = number of roads that  has the vehicule, NP = number of people that can be in the vehicule and TV = vehicule's type.
\begin{Schunk}
\begin{Sinput}
> library(rpart)
> clasificationV = rpart(TV ~.,
+                        data = sampleV,
+                        method="class",
+                        minsplit =1)
> clasificationV
\end{Sinput}
\begin{Soutput}
n= 10

node), split, n, loss, yval, (yprob)
      * denotes terminal node

 1) root 10 7 Bicicleta (0.3000000 0.2000000 0.3000000 0.2000000)
   2) TC=N 3 0 Bicicleta (1.0000000 0.0000000 0.0000000 0.0000000) *
   3) TC=A,B 7 4 Coche (0.0000000 0.2857143 0.4285714 0.2857143)
     6) NR>=3 5 2 Coche (0.0000000 0.4000000 0.6000000 0.0000000)
      12) NR>=5 2 0 CamiÃ³n (0.0000000 1.0000000 0.0000000 0.0000000) *
      13) NR< 5 3 0 Coche (0.0000000 0.0000000 1.0000000 0.0000000) *
     7) NR< 3 2 0 Moto (0.0000000 0.0000000 0.0000000 1.0000000) *
\end{Soutput}
\end{Schunk}

\begin{Schunk}
\begin{Sinput}
> library(tree)
> (clasificationTreeV = tree(TV ~.,
+                           data = sampleV,
+                           mincut = 1,
+                           minsize = 2)
+ )
\end{Sinput}
\begin{Soutput}
node), split, n, deviance, yval, (yprob)
      * denotes terminal node

1) root 10 27.32 Bicicleta ( 0.3 0.2 0.3 0.2 )
  2) NR < 3 5  6.73 Bicicleta ( 0.6 0.0 0.0 0.4 )
    4) TC: A,B 2  0.00 Moto ( 0.0 0.0 0.0 1.0 ) *
    5) TC: N 3  0.00 Bicicleta ( 1.0 0.0 0.0 0.0 ) *
  3) NR > 3 5  6.73 Coche ( 0.0 0.4 0.6 0.0 )
    6) NR < 5 3  0.00 Coche ( 0.0 0.0 1.0 0.0 ) *
    7) NR > 5 2  0.00 CamiÃ³n ( 0.0 1.0 0.0 0.0 ) *
\end{Soutput}
\begin{Sinput}
> clasificationTreeV
\end{Sinput}
\begin{Soutput}
node), split, n, deviance, yval, (yprob)
      * denotes terminal node

1) root 10 27.32 Bicicleta ( 0.3 0.2 0.3 0.2 )
  2) NR < 3 5  6.73 Bicicleta ( 0.6 0.0 0.0 0.4 )
    4) TC: A,B 2  0.00 Moto ( 0.0 0.0 0.0 1.0 ) *
    5) TC: N 3  0.00 Bicicleta ( 1.0 0.0 0.0 0.0 ) *
  3) NR > 3 5  6.73 Coche ( 0.0 0.4 0.6 0.0 )
    6) NR < 5 3  0.00 Coche ( 0.0 0.0 1.0 0.0 ) *
    7) NR > 5 2  0.00 CamiÃ³n ( 0.0 1.0 0.0 0.0 ) *
\end{Soutput}
\end{Schunk}

\subsection{} Then, the lineal regression anaylisis will be done:
\begin{Schunk}
\begin{Sinput}
> library(utils)
> pairs <- read.table("pair1.txt")
> dataP = data.frame(pairs)
\end{Sinput}
\end{Schunk}

\section{}
\end{document}


\section{Discussion} \label{discussion}
The Lasso-based procedure performed on microarray data is enhanced by a permutation approach that consistently improves the stability of the inferred network structure. The purpose of permuting the response variable is to break the link with the other independent variables by optimising an equivalent convex function which selects a number of variables close to those selected for the original (not permuted) response. Since the permutation affects only the response gene, the structure of the permuted data is equivalent to the original one. Moreover, using the same $\lambda$ increases the speed of the algorithm due to the fact that cross-validation is no longer required.
Results from simulated genetic data are encouraging and consent to perform our approach to predict genetic interactions from real biological datasets. However, we address some limitations we intend to investigate in the near future.

As already stated, genetic data are usually affected by measurement noise and high number of variables collected from different datasets such as gene expression profiles, SNPs, methylation and clinical data.

The curse of dimensionality can set a limit on the number of permutations to perform. Due to the fact that our method relies on permuting each response variable in order to increase the stability of the discovered interactions, the overall performance is directly affected by the total number of genes in the dataset.

%We are investigating possible solutions to mitigate the curse of dimensionality by limiting the discovery of interactions to highly connected genes. This strategy would detect the local structure around genes usually referred to as network hubs. We do not interpret this fact as a limitation since biological networks usually manifest a scale free topology, in which only few nodes are highly connected to the rest of the graph (\citealp{BAR03a, evidencescalefree}). FUTURE WORK

The variable selection procedure consistently depends on the value of the shrinkage factor $\lambda$, estimated on a subset of the covariates. Obviously, it might occur a prior exclusion of significant genes from further analyses in the case of a too restrictive shrinkage factor. An alleviation to this risk (which can  directly determine the false negative rate) consists in replacing the pure Lasso penalty with an elastic net procedure of the type

\begin{equation}
\label{eq:elnet}
    \hat{\Theta}^{a,\lambda} =
    \argmin_{%
      \substack{%
        \text{s.\,t.}\, \Theta:\Theta_a = 0 \\
        \phantom{}\,
      }
    }
    (\frac{1}{n} \| X_i - X\Theta \|^{2}_2 + \alpha \| \Theta \|_{1} + (1-\alpha) \| \Theta \|^{2})
  \end{equation}

In such a scenario it would be necessary to estimate an additional parameter $\alpha$. To the other extreme, a pure ridge-regression procedure would not benefit from the permutation-based stability test, due to the fact that ridge-regression procedures tend to include all the covariates in the model. Moreover, our method ignores the value of the regression coefficients and selects a subset of genes with the best permutation score. In a ridge-regression setting all covariates would be selected an equal number of times.

Another aspect we intend to probe regards the direction of the interactions. In our analysis we ignore the direction of each edge in the graph. A relaxation of the problem of learning the network topology consists in considering the interaction $i \rightarrow j$ equivalent to the interaction $j \rightarrow i$. Although this simplification makes the construction of the overall network consistently easier, it might lead to inconsistencies from a biological perspective. As a matter of fact, gene regulations are known to have a direction, usually referred to as activation and inhibition. Activation and inhibition are essential regulatory mechanisms in the transcriptional machinery of the cell and are causes for up- and down-regulation of particular genes (\citealp{GeistlingerCKMZ11}).

 Learning the directionality of network edges represents an additional complexity that is plausible to deal with only in the presence of a large number of samples, or by integrating complementary data sources of known interactions.
Therefore, the need for integrating different data sources is twofold: data integration can increase the stability of all discovered interactions and their direction and, specifically to our method, it can reduce the number of required permutations per gene. We believe that data integration can consistently  improve the overall performance of the described approach.

We endorse our approach to be deployed in a data analysis pipeline in order to 1) analyse different data sources 2) build the local network from each dataset 3) increase the stability of predicted interactions by permutation and 4) integrate each singular network into a more stable and complete graph.
We are currently extending our network inference method to implement the aforementioned data analysis pipeline.


\subsection{Vector spaces}\label{subsec:vector_spaces}

This subsection is about the algebraic properties of vector spaces. See \fullref{subsec:vector_space_geometry} for \enquote{geometric} concepts like hyperplanes and convexity.

\begin{definition}\label{def:vector_space}
  A \term{vector space} \( (V, +, \cdot) \) is a \hyperref[def:left_module]{left module} over a field \( \BbbK \).

  We call elements of \( \BbbK \) \term{scalars} and elements of \( V \) \term{vectors}.

  The category of vector spaces over \( \BbbK \) is denoted by \( \cat{Vect}_{\BbbK} \).
\end{definition}

\begin{definition}\label{def:vector_field}
  Let \( V \) be a vector space over \( \BbbK \). Functions of the type
  \begin{equation*}
    f: \BbbK \to V
  \end{equation*}
  are called \term{vector fields}. To avoid confusion, \( \BbbK \) is sometimes referred to as a \term{scalar field}. This convention comes from physics and is dominant in areas that are far from algebraic field theory, hence in practice it does not cause a lot of confusion.
\end{definition}

\begin{remark}\label{rem:real_vector_space}
  Outside of algebra, we are usually only interested in vector spaces over the fields \( \BbbR \) or \( \BbbC \). We call them \term{real vector spaces} and \term{complex vector spaces}, respectively.
\end{remark}

\begin{definition}\label{def:complex_conjucate_vector_space}
  Let \( V \) be a vector space over the complex numbers \( \BbbC \). Its \term{complex conjugate vector space} \( \overline V \) is the same space, but with scalar multiplication defined as
  \begin{equation*}
    t \cdot_{\overline V} x \coloneqq \overline t \cdot_V x.
  \end{equation*}
\end{definition}

\begin{proposition}\label{thm:field_extension_is_vector_space}
  Let \( \BbbK \) be a field \hyperref[def:field_extension]{extension} of \( G \). Then \( \BbbK \) is a vector space over \( G \).
\end{proposition}
\begin{proof}
  Since \( \BbbK \) already has the structure of an abelian group, we must only define scalar multiplication
  \begin{balign*}
     & \circ: G \times \BbbK \to \BbbK, \\
     & g \circ f \coloneqq gf,
  \end{balign*}
  where the product in the definition is simply multiplication in \( \BbbK \). The well-definedness of \( \circ \) follows from the well-definedness of multiplication in \( \BbbK \).
\end{proof}

\begin{remark}\label{rem:linear_span_only_for_vector_spaces}
  The definition for linear \hyperref[def:linear_span]{span} applies to general commutative \hyperref[def:left_module]{modules}. However, since \fullref{thm:vector_space_linear_dependence,thm:vector_space_basis} do not apply to general commutative modules, it makes sense to only use linear spans withing the context of vector spaces.
\end{remark}

\begin{definition}\label{def:linear_span}
  For a set \( A \subseteq X \) of vectors, the set of all linear combinations of finite subsets of \( A \) is called its span and is denoted by \( \linspan{A} \).
\end{definition}

\begin{proposition}\label{thm:vector_space_linear_dependence}
  The set \( A \subseteq V \) is linearly dependent in the sense of \fullref{def:left_module_linear_dependence} if and only if there exists a vector \( x \in V \) such that
  \begin{equation*}
    x \in \linspan{A} \setminus \{ x \}.
  \end{equation*}
\end{proposition}
\begin{proof}
  \SufficiencySubProof Let \( A \subseteq M \) and let
  \begin{equation*}
    0_M \coloneqq \sum_{k=1}^n t_k x_k,
  \end{equation*}
  where \( t_1, \ldots, t_n \) have at least one nonzero scalar and where \( x_1, \ldots, x_n \) are nonzero vectors. Without loss of generality, assume that \( t_{n_0} \) is the nonzero scalar. Then
  \begin{balign*}
    0_M             & = \sum_{k=1}^n t_k x_k,                                                                                \\
    t_{k_0} x_{k_0} & = -\sum_{k=1}^n t_k x_k,                                                                                \\
    x_{k_0}         & = \sum_{k=1}^n \left(-\frac {t_k} {t_{k_0}} \right) x_k \in \linspan{A} \setminus \left\{ x_{k_0} \right\}.
  \end{balign*}

  \NecessitySubProof Let \( A \subseteq M \) and \( x \in \linspan{A} \setminus \{ 0_M, x \} \). By \fullref{def:linear_combination}, there exist nonzero vectors \( x_1, \ldots, x_n \in A \) and scalars \( t_1, \ldots, t_n \in R \) such that
  \begin{equation*}
    x \coloneqq \sum_{k=1}^n t_k x_k,
  \end{equation*}
  where at least one of \( t_1, \ldots, t_n \) is nonzero.

  Then \( 0_M \) is a nontrivial linear combination of the nonzero vectors \( x_1, \ldots, x_n, x \):
  \begin{equation*}
    0_M = \sum_{k=1}^n t_k x_k - x.
  \end{equation*}
\end{proof}

\begin{definition}\label{affine_independence}
  Given a vector space \( V \) over \( F \), we say that a set \( A \subseteq V \) of vectors is \term{affinely independent} in \( V \) if the set
  \begin{equation*}
    \{ (x, 1) \colon x \in A \}
  \end{equation*}
  is linearly independent in \( V \times F \).
\end{definition}

\begin{proposition}\label{thm:vector_space_basis}
  The set \( B \subseteq V \) is a basis in the sense of \fullref{def:left_module_hamel_basis} if and only if it is linearly independent and
  \begin{equation*}
    V = \linspan{B}.
  \end{equation*}
\end{proposition}
\begin{proof}
  \SufficiencySubProof We will prove the contraposition, that is, if \( \linspan{B} \neq M \), then \( B \) is not a maximal linearly independent set.

  If \( \linspan{B} \subsetneq M \), then there exists a vector \( x \in M \) such that \( x \) is not a linear combination of any subset of \( B \). Thus, \( B \cup \{ x \} \) does not have a nontrivial linear combination that equals zero. Hence, \( B \cup \{ x \} \) is linearly independent.

  \NecessitySubProof Let \( B \subseteq M \) and \( \linspan{B} = M \). Assume that there exists a vector \( x \in M \setminus B \) such that the set \( B \cup \{ x \} \) is linearly independent.

  Then, for any \( b \in B \), the vector \( x + b \) is a linear combination of elements, one of which is independent of \( B \). Thus, by \fullref{thm:vector_space_linear_dependence},
  \begin{equation*}
    M = \linspan{B} \subsetneq \linspan{B \cup \{ x \}} \subseteq M,
  \end{equation*}
  which is a contradiction.
\end{proof}

\begin{theorem}[Every vector space has a basis]\label{thm:every_vector_space_has_a_basis}
  Every vector space has a \hyperref[def:left_module_hamel_basis]{basis}. Equivalently, all vector spaces are \hyperref[def:free_left_module]{free modules}.

  Compare this to finite-dimensional vector spaces of order \( n \) over \( \BbbK \), which are all isomorphic to \( \BbbK^n \).

  In \hyperref[def:zfc]{\logic{ZF}} this theorem is equivalent to the \hyperref[def:zfc/choice]{axiom of choice} --- see \fullref{thm:axiom_of_choice_equivalences/vector_space_bases}.
\end{theorem}
\begin{proof}
  Let \( V \) be a vector space. Assume that it does not have a basis. Let \( \mathcal{B} \) be the family of all linearly independent \hyperref[def:linear_combination]{subsets} of \( V \).

  The family \( \mathcal{B} \) is obviously nonempty since any \hyperref[rem:singleton_sets]{singleton} from \( V \) belongs to \( \mathcal{B} \). The union of any chain \( \mathcal{B}' \subseteq \mathcal{B} \) can then contain only linearly independent elements since otherwise we would have that some set in \( \mathcal{B}' \) is not linearly independent. Thus, we can apply \fullref{thm:zorns_lemma} to obtain a maximal element \( B \).

  Assume that \( B \) is not a basis, that is,
  \begin{equation*}
    \linspan B \subsetneq V.
  \end{equation*}

  Take \( V \in V \setminus \linspan B \). Then the set \( B \cup \{ v \} \) is linearly independent, which contradicts the assumption that \( B \) is not a basis. Thus, \( B \) is a basis of \( V \) and \( V \) is a free module.
\end{proof}

\begin{definition}\label{def:vector_space_dimension}
  The \hyperref[def:free_left_module]{free module rank} of a vector space \( V \) is called the \term{dimension} \( \dim V \) of \( V \). If \( U \) is a vector subspace of \( V \), we call \( \co\dim_V U \coloneqq \dim(V/U) \) the \term{codimension} of \( U \) relative to \( V \).
\end{definition}

\begin{proposition}\label{thm:linear_maps_form_algebra}
  The set \( \hom(U, V) \) is a vector space.
\end{proposition}
\begin{proof}
  By \fullref{thm:functions_over_ring_form_algebra}, \( \hom(U, V) \) forms an \( \BbbK \)-vector space.
\end{proof}

\begin{remark}\label{rem:functional}
  The term \enquote{functional} does not have a strict meaning. For example, logicians use terms like \enquote{primitive recursive functional} for certain generalized functions. Functions are also ill-defined, see \fullref{rem:function_definition}. Outside of logic, however, the term \enquote{functional} usually refers to a function from a vector space \( V \) to its base field \( \BbbK \). Examples include linear \hyperref[def:linear_operator]{functionals}, like projection \hyperref[def:left_module_basis_projection]{maps} and \hyperref[def:differentiability]{derivatives}, and nonlinear functionals, like the Minkowski \hyperref[def:minkowski_functional]{functionals}.
\end{remark}

\begin{definition}\label{def:eigenpair}
  Let \( f: U \to V \) be a function between vector spaces over \( \BbbK \).

  An \term{eigenpair} of \( f \) consists of an \term{eigenvalue} \( \lambda \in \BbbK \) and an \term{eigenvector} \( x \in U \) such that
  \begin{equation*}
    f(x) = \lambda x.
  \end{equation*}
\end{definition}


%%\begin{CodeBlock}{51mm}{\file{feff.inp} file:}

\subsection{Constraints}
\begin{slide}{ Constraints in {\feffit} }

  Examples of Path Parameters written as functions of the Generalized
  Variables:
  \vmm
  \setbeamertemplate{blocks}[rounded][shadow=true]

  \begin{tabular}{ll}
    \begin{minipage}{45mm}
     \begin{block}
        {Parameter = Variable}
{\tiny{\begin{alltt}

{\Red{guess e0    = 1.0 }}
path(1,  e0 = e0)
path(2,  e0 = e0)
\end{alltt}}}
 \end{block}
   \vspace{1.75mm}
   \onslide+<2->
   \begin{block}
{mixed coordination shell}

{\tiny{\begin{alltt}
 set S02   = 0.80

 {\Red{guess x   = 0.5}}

 path(1,  Amp= S02 * x )
 path(2,  Amp= S02 * (1-x))

\end{alltt}}}
   \end{block}
   \end{minipage}
    & \hspace{0.2mm}
     \begin{minipage}{65mm}
       %% \onslide+<3->
%       \begin{block}
%  { Fit Einstein Temperature }

% {\tiny{\begin{alltt}

%  set   factor  = 24.254337   {\Blue{#= (hbar*c)^2/(2 k_boltz)}}
% {\Blue{# mass and reduced mass in amu}}
%  set   mass1 = 63.54,  mass2 = 63.54
%  set   r_mass =  1/ (1/mass1 +  1/mass2)

% {\Blue{# the Einstein Temp will be adjusted in the fit!}}
%  {\Red{guess thetaE = 200}}
% {\Blue{# use for data set 1, T=77}}
%  set   temp1 = 77
%  {\Red{def ss2_path1 = factor*coth(thetaE/(2*temp1))/r_mass )}}
%  path(101,  sigma2 = ss2_path1   )

% {\Blue{# use for data set 2, T=300}}
%  set   temp2 = 300
%  {\Red{def ss2_path2 = factor*coth(thetaE/(2*temp2))/r_mass )}}
%  path(201,  sigma2 = ss2_path2   )
% \end{alltt}}
% \end{block}
\end{minipage}\\
\end{tabular}

\vmm \vmm

\onslide+<4-> Other Examples:

\begin{itemize}
\item force one $R$ for the same bond for data taken from different  edges.
\item model complex distortions (height of a sorbed atom above a surface).
\end{itemize}

\end{slide}


\subsection{Heapsort}

\begin{frame}{Heapsort - Algorithm 1 / 10}
  \textbf{Heapsort:}
  \begin{itemize}
    \item
      The principle stays the same
    \item
      Better structure for finding the smallest element quicker
  \end{itemize}
  \vspace{1em}
  \onslide<2- |handout:1>{\textbf{Binary heap:}}
  \begin{itemize}
    \item<2- |handout:1>
      Preferably a complete binary tree
    \item<2- |handout:1>
      \textbf{Heap property:} Each child is {\color{MainA}smaller} (larger) than the parent
      element
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Heapsort - Algorithm 2 / 10}
  \textbf{Min heap:}
  \begin{itemize}
    \item<1- |handout:1>
      \textbf{Heap property:} Each child is {\color{MainA}smaller}
      (larger) than the parent element
  \item<2- |handout:1>
    A valid heap fulfills the property at each node
  \end{itemize}
  \vspace{-1em}
  \begin{columns}%
    \begin{column}[b]{0.45\textwidth}%
      \begin{figure}[!h]%
        \begin{adjustbox}{height=0.75\linewidth}%
          \input{Images/Heap/MinHeap_Valid.tikz}
        \end{adjustbox}
        \caption{Valid min heap}
        \label{fig:minheap_valid}
      \end{figure}
    \end{column}%
    \hspace*{0.1em}%
    \begin{column}[b]{0.45\textwidth}%
      \begin{figure}[!h]%
        \begin{adjustbox}{height=0.75\linewidth}%
          \input{Images/Heap/MinHeap_Invalid.tikz}
        \end{adjustbox}
        \caption{Invalid min heap}
        \label{fig:minheap_invalid}
      \end{figure}
    \end{column}
  \end{columns}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Heapsort - Algorithm 3 / 10}
  \textbf{How to save the heap?}\\[0.25em]
  \begin{itemize}
    \item
      We number all nodes from top to bottom and left to right starting at
      {\color{MainA}0}
      \begin{itemize}
        \item
          The children of node {\color{MainA}$i$} are
          {\color{MainA}$2i + 1$} and {\color{MainA}$2i + 2$}
        \vspace*{0.5em}
        \item
          The parent node of node {\color{MainA}$i$} is
          {\color{MainA}$\mathrm{floor}\left(\frac{i-1}{2}\right)$}
      \end{itemize}
  \end{itemize}%
  \vspace{-2em}%
  \begin{columns}%
    \begin{column}{0.5\textwidth}
      \begin{figure}[!h]%
        \begin{adjustbox}{width=\linewidth}
          \input{Images/Heap/MinHeap_Numbered.tikz}%
        \end{adjustbox}
        \vspace*{-0.5em}%
        \caption{Min heap}%
        \label{fig:minheap_numbered}%
      \end{figure}%
    \end{column}
    \begin{column}{0.5\textwidth}
      \begin{table}[!h]
        \caption{Elements can be stored in array}
        \label{tab:minheap_numbered}
        \begin{tabular}{ccccccc}
          \onslide<2- |handout:1>{\color{MainB}0}&%
          \onslide<3- |handout:1>{\color{MainB}1}&%
          \onslide<4- |handout:1>{\color{MainB}2}&%
          \onslide<5- |handout:1>{\color{MainB}3}&%
          \onslide<6- |handout:1>{\color{MainB}4}&%
          \onslide<7- |handout:1>{\color{MainB}5}&%
          \onslide<8- |handout:1>{\color{MainB}6}\\
          \hline
          \multicolumn{1}{|c}{\onslide<2- |handout:1>{2}}&%
          \multicolumn{1}{|c}{\onslide<3- |handout:1>{3}}&%
          \multicolumn{1}{|c}{\onslide<4- |handout:1>{4}}&%
          \multicolumn{1}{|c}{\onslide<5- |handout:1>{11}}&%
          \multicolumn{1}{|c}{\onslide<6- |handout:1>{7}}&%
          \multicolumn{1}{|c}{\onslide<7- |handout:1>{5}}&%
          \multicolumn{1}{|c|}{\onslide<8- |handout:1>{8}}\\
          \hline
        \end{tabular}
      \end{table}
    \end{column}
  \end{columns}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Heapsort - Algorithm 4 / 10}
  \textbf{Repairing after taking the smallest element:} \texttt{heap.pop()}
  \begin{itemize}
    \item<2- |handout:1>
      Remove the smallest element (root node)
    \item<3- |handout:1>
      Replace the root with the last node
    \item<4- |handout:1>
      {\color{MainA}Sift} the new root node down until the
      {\color{MainA}heap property} is satisfied
  \end{itemize}
  \onslide<5- |handout:1>{
    \begin{figure}[!h]%
      \begin{columns}%
        \begin{column}{0.3\textwidth}%
          \begin{adjustbox}{width=\linewidth}
            \input{Images/HeapSort/MinHeap_Repair_First.tikz}%
          \end{adjustbox}%
        \end{column}%
        \hspace*{0.05em}%
        \begin{column}{0.3\textwidth}<7- |handout:1>%
          \begin{adjustbox}{width=\linewidth}
            \input{Images/HeapSort/MinHeap_Repair_Second.tikz}%
          \end{adjustbox}
        \end{column}%
        \hspace*{0.05em}%
        \begin{column}{0.3\textwidth}<9- |handout:1>%
          \begin{adjustbox}{width=\linewidth}
            \input{Images/HeapSort/MinHeap_Repair_Third.tikz}%
          \end{adjustbox}%
        \end{column}%
      \end{columns}%
      \caption{Repairing a min heap via sifting}%
      \label{fig:minheap_repair}%
    \end{figure}
  }
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Heapsort - Algorithm 5 / 10}
  \textbf{Heapsort:}
  \begin{itemize}
    \item
      Organize the {\color{MainA}$n$} elements as heap
    \item
      While the heap still contains elements
      \begin{itemize}
        \item
          Take the smallest element
        \item
          Move the last node to the root
        \item
          Repair the heap as described
      \end{itemize}
    \item<2- |handout:1>
      % Output: {\color{MainB}4}%
      % \onslide<6- |handout:1>{, {\color{MainB}5}, {\color{MainB}\ldots}}
       Output: {\color{MainB}2}%
       \onslide<9- |handout:1>{, {\color{MainB}3}, {\color{MainB}\ldots}}
  \end{itemize}
  \vspace*{-0.5em}
  \onslide<2- |handout:1>{
    \begin{center}
      \begin{figure}[!h]%
        \begin{columns}%
          \begin{column}{0.33\textwidth}%
            \begin{centering}
              \begin{adjustbox}{height=8em}
                \input{Images/HeapSort/HeapSort_First.tikz}%
              \end{adjustbox}%
            \end{centering}
          \end{column}%
          \begin{column}{0.33\textwidth}<4- |handout:1>%
            \begin{centering}
              \begin{adjustbox}{height=8em}
                \input{Images/HeapSort/HeapSort_Second.tikz}%
              \end{adjustbox}%
            \end{centering}
          \end{column}%
          \begin{column}{0.33\textwidth}<8- |handout:1>%
            \begin{centering}
              \begin{adjustbox}{height=8em}
                \input{Images/HeapSort/HeapSort_Third.tikz}%
              \end{adjustbox}%
            \end{centering}
          \end{column}%
        \end{columns}%
        \caption{One iteration of Heapsort}%
        \label{fig:heapsort_repair}%
      \end{figure}
    \end{center}
  }
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Heapsort - Algorithm 6 / 10}
  \textbf{Creating a heap:}
  \begin{itemize}
    \item
      This operation is called {\color{MainA}heapify}
    \item<2- |handout:1>
      The {\color{MainA}$n$} elements are already stored in an array
    \item<3- |handout:1>
      Interpret the array as binary heap where the {\color{MainA}heap property} is not yet satisfied
    \item<4- |handout:1>
      We repair the heap from bottom up (in layers) with {\color{MainA}sifting}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Heapsort - Algorithm 7 / 10}
  \vspace{-1.0em}
  \begin{table}[!h]%
    \caption{Input in array}%
    \label{tab:heapify_numbers}%
    \begin{tabular}{ccccccc}
      {\color{MainB}0}&
      {\color{MainB}1}&
      {\color{MainB}2}&
      {\color{MainB}3}&
      {\color{MainB}4}&
      {\color{MainB}5}&
      {\color{MainB}6}\\
      \hline
      \multicolumn{1}{|c}{11}&%
      \multicolumn{1}{|c}{7}&%
      \multicolumn{1}{|c}{8}&%
      \multicolumn{1}{|c}{3}&%
      \multicolumn{1}{|c}{2}&%
      \multicolumn{1}{|c}{5}&%
      \multicolumn{1}{|c|}{4}\\
      \hline
    \end{tabular}
  \end{table}
  \vspace*{-0.5em}
  \begin{centering}
    \begin{figure}[!h]%
      \begin{columns}%
        \begin{column}{0.425\textwidth}%
          \begin{adjustbox}{width=\linewidth}%
            \input{Images/HeapSort/Heapify_First.tikz}%
          \end{adjustbox}%
        \end{column}%
        \begin{column}{0.425\textwidth}<2- |handout:1>%
          \begin{adjustbox}{width=\linewidth}%
              \input{Images/HeapSort/Heapify_Second.tikz}%
          \end{adjustbox}%
        \end{column}%
      \end{columns}%
      \caption{Heapify lower layer}%
      \label{fig:heapify_lower}%
    \end{figure}
  \end{centering}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Heapsort - Algorithm 8 / 10}
  \begin{centering}
   \begin{figure}[!h]%
     \begin{columns}%
       \begin{column}{0.425\textwidth}%
         \begin{adjustbox}{width=\linewidth}%
           \input{Images/HeapSort/Heapify_Third.tikz}%
         \end{adjustbox}%
       \end{column}%
       \begin{column}{0.425\textwidth}<2- |handout:1>%
          \begin{adjustbox}{width=\linewidth}%
            \input{Images/HeapSort/Heapify_Fourth.tikz}%
          \end{adjustbox}%
        \end{column}%
      \end{columns}%
      \caption{Heapify upper layer}%
      \label{fig:heapify_upper}%
    \end{figure}
  \end{centering}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Heapsort - Algorithm 9 / 10}
  \begin{centering}
    \begin{figure}[!h]
      \begin{adjustbox}{width=0.425\linewidth}%
        \input{Images/HeapSort/Heapify_Fifth.tikz}%
      \end{adjustbox}%
      \caption{Resulting heap}%
      \label{fig:heapify_upper_final}%
    \end{figure}
  \end{centering}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Heapsort - Algorithm 10 / 10}
  \textbf{Finding the minimum is intuitive:}
  \begin{itemize}
    \item
      \textbf{Minsort:} Iterate through all non-sorted elements
    \item
      \textbf{Heapsort:} Finding the minimum is trivial (concept)
      \begin{center}
        \textit{Just take the root of the heap}
      \end{center}
  \end{itemize}
  \vspace*{1.5em}
  \onslide<2- |handout:1>{
    \textbf{Removing the minimum in Heapsort:}
    \begin{itemize}
      \item
        Repair the heap and restore the {\color{MainA}heap property}
        \begin{itemize}
          \item
            We don't have to repair the whole heap
        \end{itemize}
      \item
        More of this in the next lecture
    \end{itemize}
  }
\end{frame}

%%% ===================================================================
%%% This should be at the END of the file !!!!!!
%%%
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../../Lecture.tex"
%%% End:
%%% ===================================================================