\title{\bf Telescopes}

\section{Basics \& Nomenclature}

Telescopes are the fundamental tool of astronomers, with a handful of
exceptions ($\gamma$-ray detectors, cosmic ray detectors, neutrino
detectors, and gravitational wave detectors).

Telescope optics are design to do two things: to collect a lot of
light; arrange the collection process to separate the light according
to its incoming direction. Detectability of objects depends on how
many photons, or equivalently how much energy, is collected. This
number is the product of $f_\nu A t \Delta\nu$, where $A$ is the
telescope area, $t$ is the length of exposure, and $\Delta\nu$ is the
width of detector bandpass.

The two basic types of telescope are {\it refracting} and {\it
reflecting} telescopes. Refractors transmit the light through
transparent media with refractive indices different than air, bending
it through Snell's Law. Reflectors are mirrored surfaces, in the
optical typically aluminized, whose shapes manipulate beams of light
in the desired fashion. Reflecting telescopes virtually always have
some refracting elements inside of them.

For astronomical telescopes, they always focus at infinity, meaning
they are designed such that parallel rays of light are focused to a
single point in the focus of the telescope.  Typically, there is some
axis of symmetry to the telescope and rays parallel to this axis are
{\it on-axis}, and it is typically here that the telescope is designed
to have best focus. {\it Off-axis} rays may be well focused as well,
but as the angle off-axis becomes larger the rays do not all
converge. Off-axis rays can be obstructed by either parts of the
optics or part of the telescope structure, and generally are more
obstructed than on-axis rays; this effect is known as {\it
vignetting}.  The usable {\it field of view} of the telescope is
typically determined by the off-axis angle at which the beam is too
vignetted or too badly focused to be usable for the desired purpose.

% Below is a picture of a lens diagram for a refracting telescope. The
% lens has a focal length, which is what determines how far beyond the
% lens a set of parallel, on-axis rays will come to focus. Physically,
% the lens is bending the light according to Snell's law, and the shape
% of the front and back surface of the lens as a function of its radius
% is what conspires to direct all the on-axis light to the focus.

A key parameter in the optical design is the {\it focal ratio}, or
{\it $f$-ratio}, the ratio of the focal distance $f$ to the aperture
diameter $D$. This is usually expressed in the form ``$f/N$'' (which
is not a division), where $N = f/D$. This ratio determines the angular
size of the beam that converges on the focal point; in more complex
optical configurations, it is this beam width that defines the
$f$-ratio. The smaller the $f$-ratio the more the light is being bent;
this means that off-axis rays will usually fall out of focus more
quickly with off-axis angle without very careful design.

Slightly off-axis light focuses to a slightly offset spot in the focal
plane. The relationship between the off-axis angle and physical radius
in the focal plane from the on-axis focus (sometimes known as the {\it
plate scale}) is determined by the $f$-ratio. Specifically:
\begin{equation}
{\rm d}x \approx f {\rm d}\theta
\end{equation}
so:
\begin{equation}
\frac{{\rm d}x}{{\rm d}\theta} \approx f
= \left(\frac{f}{D}\right) D
\left(\frac{10^6~\mu{\rm m}}{{\rm m}}\right)v
\left(\frac{\rm rad}{(180/\pi)(3600)~{\rm arcsec}}\right)
= \left(\frac{f}{D}\right) \frac{D}{{\rm m}}
\left(4.85 \frac{\mu{\rm m}}{\rm arcsec}\right)
\end{equation}
or inversely as
\begin{equation}
\label{eq:platescale}
\frac{{\rm d}\theta}{{\rm d}x} \approx
\left(\frac{f}{D}\right)^{-1} \frac{{\rm m}}{D}
\left(0.206\frac{\rm arcsec}{\mu{\rm m}}\right)
\end{equation}
This equation holds given the final $f$-ratio of any of the optical
systems we describe here. It means the smaller the $f$-ratio, or the
smaller the telescope size, the more angular coverage per unit area
there is.

There are three major limitations of refracting telescopes:
\begin{ditemize}
\item The index of refraction of light in lens materials varies with
wavelength. This means that the focal length depends on wavelength as
well, and only one wavelength can be in perfect focus. This effect is
known as {\it chromatic aberration} and is a limitation of refracting
telescopes (and affects reflecting telescopes with refracting optics
inside them). It is somewhat mitigated with large $f$-ratios, which
minimize the angular deflections.
\item Mitigating chromatic aberration and maintaining focus off-axis
both drive the telescope design to reasonably high $f$-ratios. With a
refractor, this inevitably lengthens the telescope structure in ways
that can be avoided with reflecting telescope. A very long telescope
necessitates a large dome, a more difficult engineering problem to
manipulate the telescope, and more difficulty in minimizing flexure
(bending) in the system.
\item
The practical size limit for refracting optical elements in the
optical is about 1 meter. It is extremely hard to create precise
optical elements larger than that size at finite cost.
\end{ditemize}
These considerations drove astronomers in the late 1800s and early
1900s to move to reflecting telescopes for large telescope
applications.

The principles of reflecting telescopes to first order can be
understood through the mathematics of conic sections: parabolae,
ellipses, and hyperbolae. Reflecting surfaces in telescopes are close
approximations to axisymmetric surfaces of revolution whose radial
shape is defined by these conic sections. Conic section can be defined
in terms of their two {\it foci}. One of their mathematical properties
is that at any point on the curve, the normal to the curve bisects the
directions to the foci. This mathematical property has the consequence
that a ray of light emitted from the direction of one focus will be
reflected from the surface along the direction defined by the other
focus.

Astronomical observations are almost invariably of extremely distant
objects, the light rays from which are parallel to each other. This
corresponds to a {\it focus at infinity}. A conic section with one
focus at infinity and one at finite distance is a parabola. Therefore,
if a parabolic mirror is aligned with these rays, they will be
reflected towards the other focus of the parabola. A simple design for
a telescope therefore is to put a detector at the focus of a parabolic
mirror, referred to as the {\it primary}. A slight variant known as
the Newtonian design is to direct this {\it prime focus} with a flat
pickoff mirror.

More complex designs are possible with multiple curved surfaces. The
general principle behind these designs are to use the fact that conic
sections like ellipsoids and hyperboloids have two foci. A {\it
secondary mirror} is designed to have one focus coincide with the
prime focus; the reflective surface then will refocus the light to its
other focus. A third ({\it tertiary}) mirror is used in some designs
(for example in the Large Synoptic Survey Telescope).

There are multiple purposes of the extra surfaces. They allow the
focus to be redirected to a mechanically more convenient spot than the
primary. They allow freedom to manipulate the $f$-ratio of the
beam. For wide-angle observations, the design of the primary,
secondary, and other mirror shapes can be slightly perturbed from the
conic section ideals to allow off-axis light to remain in better focus
(at the cost of focus on-axis). In operation, that they are smaller
than the primary makes them more easily adjustable in real time to
perform focus changes and tip/tilt corrections for image motion.

Many reflecting telescope systems have {\it correctors}, which are
refractive elements near their focus used to reduce distortions across
the field of view.

The diffraction-limited point spread function of a telescope is
related to its aperture. The ``pupil'' of the system is the image of
the aperture including any obstructions in the optical path, for a
plane wave arriving at the telescope aperture. In detail, optical
imperfections will also lead to phase differences as a function of
position in the pupil. Thus the ``complex pupil function'' $A
e^{i\phi}$ gives the throughput $A$ and phase shift $\phi$ of each
point in the aperture. A consequence of Huygen's Principle of
diffraction isthat the plane wave is focused onto a spot with an
intensity function with a spatial form that is the square of the
Fourier transform of the complex pupil function.

For the simplest aperture, a circular one with no phase shifts, the
width of the diffraction-limited point spread function is related to
the aperture diameter $D$. Specifically:
\begin{equation}
\label{eq:psf_tophat}
I(x) \propto \left(\frac{J_1(x)}{x}\right)^2,
\end{equation}
where $J_1$ is the first-order Bessel function and $x=\pi D \theta
/\lambda$. Here $\theta$ is the equivalent angle from the center of
the spot.

Most of these general considerations hold for telescopes outside the
optical. However, the designs of these telescopes differ in
detail. From the UV through the mid-infrared they are usually quite
similar to the optical. High energy photons do not reflect easily off
aluminized surfaces, or near the normal to the surface. X-ray
observations therefore use gold-coated surfaces under ``grazing
incidence,'' with nested annular hyperboloid surfaces. Radio
telescopes simply require conductive surfaces and much lower
tolerances due to their longer wavelength, meaning they can be built
at fixed construction cost to much larger aperture than optical
telescopes. To avoid interference, radio telescopes also typically use
asymmetric sections of a paraboloid.

\section{Commentary}

Astrophysics and astronomy courses typically focus on the optical
aspects of telescopes and the nature of their detectors.  The
engineering aspects of telescopes, both in hardware and in software,
are not well covered in the astrophysics or in the engineering
disciplines.

\section{Key References}

\begin{itemize}
  \item
    {\it Design and Construction of Large Telescopes},
      \citet{bely03a}
\end{itemize}

\section{Order-of-magnitude Exercises}

\begin{enumerate}
\item By considering two on-axis light rays each hitting each
    side of a telescope of diameter $D$ and interfering near the focal
    plane, estimate the diffraction limit in terms of the wavelength
    of light $\lambda$ and diameter $D$.

\begin{answer}
By Huygens' Principle, the two rays can be treated as each producing a
spherically outgoing wave from their location on the aperture. If the
two rays are in phase when they hit the perfectly constructed mirror,
they will constructively interfere exactly at focus. Near but slightly
off focus in the focal plane, they will not exactly interfere because
the distances from the mirror for each ray will be slightly different,
so their phases at this location will not be the same.  The size of
the central image can be characterized by the position of the first
null --- i.e. when the phase difference of the two rays becomes $\pi$,
so that they destructively interfere.

The path length from either aperture location to perfect focus is:
\begin{equation}
d = \sqrt{f^2 + \left(\frac{D}{2}\right)^2},
\end{equation}
where $f$ is the focal length.  For some $\delta$ in distance away
from perfect focus, the path length for one ray will be:
\begin{eqnarray}
d_L &\approx& \sqrt{f^2 + \left(\frac{D}{2} + \delta\right)^2} \cr
&\approx& \sqrt{d^2 + D\delta} \cr
&\approx& d \left(1 + \frac{D \delta}{2 d}\right),
\end{eqnarray}
and the other ray will have a slightly shorter path by the same
amount:
\begin{eqnarray}
d_R &\approx&
d \left(1 - \frac{D \delta}{2 d}\right).
\end{eqnarray}
The difference in angle that $\delta$ corresponds to relative to
incoming rays on the sky will be $\alpha \approx \delta / d$. That is,
the difference in focal plane distance $\delta$ corresponds to a
difference in $\alpha$ on sky for perfect geometric options. So we can
write the difference in path length in units relative to sky:
\begin{equation}
\Delta d = d_L - d_R = D \alpha.
\end{equation}

These two waves will destructively interfere when  $\Delta d = \lambda
/ 2$. This will be when:
\begin{equation}
\alpha = \frac{\lambda}{2 D}
\end{equation}
This of course is smaller than the first null for an Airy function,
because the Airy function involves the interference of {\it all} of
the waves coming from the surface of the mirror, many of which have
much smaller separations. But this argument shows how $\lambda/D$
enters the problem.
\end{answer}

\item The LSST telescope in Cerro Pachon has a diameter of $\sim 8$ m
    and is $f/1.4$ at its focus. To sample with at least two pixels
    per full-width half maximum (e.g. the Nyquist critical sampling)
    for the best atmospheric seeing in Chile it might encounter (about
    0.5 arcsec FWHM), what must the detector pixel size be?

\begin{answer}
To sample with two pixels per FWHM requires $\theta\sim 0.25$ arcsec
$\sim 1.212 \times 10^{-6}$ radians.  The focal distance is $N D =
1.4\times 8 {\rm ~m} = 11.2 {\rm ~m}$, and we find therefore that the
pixel size should be $\theta N D \sim 13~\mu$m (or equivalently
consult Equation \ref{eq:platescale}). The actual LSST design has 10
$\mu$m pixels.
\end{answer}

\item Assuming that telescope diffraction is the dominant effect
defining the PSF of a telescope, how accurate does its mirror have to
    be in order to limit the change in the central flux of the PSF due
    to imperfections to less than 10\%? In the $X$-rays? the optical?
    the radio?

\begin{answer}[Author: Nanoom Lee]
Let's approximate the telescope as consisting of just two patches of
mirror and consider the interference from those two patches. If they
are perfectly accurately placed than the intensity of light at the
center of focus is just proportional to the the time-averaged square
of the electric field due to each one added together:
\begin{eqnarray}
I &\propto& \left\langle
\left|
\sin\left(\omega t - \frac{2\pi d}{\lambda}\right) +
\sin\left(\omega t - \frac{2\pi d}{\lambda}\right)
\right|^2 \right\rangle \cr
&\propto& 4 \left\langle
\left|
\sin\left(\omega t - \frac{2\pi d}{\lambda}\right)^2
\right|^2 \right\rangle
\end{eqnarray}
where $\lambda$ is the wavelength of the light, $\omega$ is its
frequency, and $d$ is the path length from the given mirror.

Now let's assume the mirror are not perfectly placed. This corresponds
to the case that a full mirror is not perfectly figured and
polished. The waves will no longer perfectly constructively interfere.
We quantify the level of imperfection as a small difference in the
path length, $\Delta d = \delta$. Then, the light ray reflected at
this point will interfere with the other light ray and change the
intensity of the light at the focus.
\begin{eqnarray}
I' &\propto& \left\langle\left|
\sin\left(\omega t - \frac{2\pi d}{\lambda}\right) +
\sin\left(\omega t - \frac{2\pi
(d+\delta)}{\lambda}\right)\right|^2\right\rangle \cr
&\propto& \left\langle\left[
\sin\left(\omega t - \frac{2\pi d}{\lambda}\right)^2 +
\sin\left(\omega t - \frac{2\pi (d+\delta)}{\lambda}\right)^2
+ 2 \sin\left(\omega t - \frac{2\pi d}{\lambda}\right)
\sin\left(\omega t - \frac{2\pi (d+\delta)}{\lambda}\right)
\right]\right\rangle \cr
&\propto& \frac{I}{2}
+ \left\langle 2 \sin\left(\omega t - \frac{2\pi d}{\lambda}\right)
\sin\left(\omega t - \frac{2\pi (d+\delta)}{\lambda}\right)
\right\rangle
\end{eqnarray}
where in the last line we just use the fact that the time average of
$\sin^2(\omega t + \phi)$ is independent of $\phi$. Considering the
second term, we can use trigonometric identities to simplify it:
\begin{eqnarray}
\left\langle 2 \sin\left(\omega t - \frac{2\pi d}{\lambda}\right)
\sin\left(\omega t - \frac{2\pi (d+\delta)}{\lambda}\right)
\right\rangle
&=&
\left\langle 2 \sin^2\left(\omega t - \frac{2\pi d}{\lambda}\right)
\cos\left(\frac{2\pi \delta}{\lambda}\right)
\right\rangle + \cr
& &
\left\langle 2
\sin\left(\omega t - \frac{2\pi d}{\lambda}\right)
\cos\left(\omega t - \frac{2\pi d}{\lambda}\right)
\sin\left(\frac{2\pi \delta}{\lambda}\right)
\right\rangle \cr
&=&
\left\langle 2 \sin^2\left(\omega t - \frac{2\pi d}{\lambda}\right)
\cos\left(\frac{2\pi \delta}{\lambda}\right)
\right\rangle
\end{eqnarray}
where in the last line we drop the time average sine times cosine,
which will be zero. Then we find:
\begin{eqnarray}
\frac{I'}{I} &=& \frac{1}{2} \left(1 +
\cos\left(\frac{2\pi \delta}{\lambda}\right) \right) \cr
&\approx& 1 - \frac{1}{4}\left(\frac{2\pi\delta}{\lambda}\right)^2
\end{eqnarray}
The question asks when this ratio is 0.9, which will be when:
\begin{equation}
\frac{\delta}{\lambda} \approx \frac{1}{\sqrt{10} \pi} \approx \frac{1}{10}
\end{equation}
So about $\delta \sim 0.1$ \AA\ for X-rays, $\sim 0.05~\mu$m  in the
optical, and $\delta \sim 1~$mm for
the cm-wavelength radio.
 \end{answer}
\end{enumerate}

\section{Analytic Exercises}

\begin{enumerate}
\item The Cassegraine design and Gregorian design both involve a
curved secondary redirecting the focus along the original primary
focal axis. In the Cassegraine, the secondary is before the prime
focus. In the Gregorian it is after. What are the shapes of each of
these secondary choices: ellipsoid, paraboloid, or hyperboloid? Draw
the mirrors and the paths for on-axis light in the two cases.

\begin{answer}[Author: Kate Storey-Fisher]
The Cassegraine secondary mirror needs to intercept the light from the
primary mirror before it reaches the focus, and redirect it through a
hole in the primary. This is achieved a by a convex hyperbolic mirror.

\begin{figure}[!h]
\includegraphics[width=1\columnwidth]{figures/cassegraine.jpg}
\includegraphics[width=1\columnwidth]{figures/gregorian.jpg}
\caption{The mirror design for the Cassegraine (top) and Gregorian
(bottom) telescopes.}
\label{fig:cassgreg}
\end{figure}

This type of reflector has two focii, one behind and one in front of
the mirror. It will reflect light directed at one focus to the
other. In the Cassegraine, the back focus is aligned to the focus of
the primary mirror so they share one focus. Then the front focus is
past the primary mirror, where the image will appear. This is shown in
the top panel of Figure~\ref{fig:cassgreg}.

The Gregorian design allows the light reflected from the primary
mirror to cross through the focus before hitting the secondary
mirror. This mirror is a concave ellipsoid, directing the light
through the hole in the primary to form an image. The closer focus of
the ellipsoid coincides with the primary mirror's focus. The shape
ensures there are no spherical aberrations. This design is shown in
the bottom panel of Figure~\ref{fig:cassgreg}.

\end{answer}

\item In the two above cases, there usually needs to be a hole in the
primary mirror (and the secondary would obstruct that area anyway).
What is the analytic form of the diffraction limited point spread
function for an aperture like this?
\end{enumerate}

\begin{answer}[Author: Nick Faucher]

The diffraction-limited point spread function of a telescope is equal
to the squared modulus of the Fourier transform of the pupil. In this
case, we are considering a pupil which allows light to pass through a
circular aperture of radius b, excluding the center of the circle out
to radius a with a $<$ b:
\begin{equation}
f(r,\theta)=
\begin{cases}
   0,& \text{if } r < a\\
   1,& \text{if } a \leq r \leq b\\
    0,              & \text{otherwise}
\end{cases}
\end{equation}

The two-dimensional Fourier transform for a single circular aperture
with radius b can be written in polar coordinates as

\begin{equation}
\mathcal{F}(k,\phi)= \int_{0}^{b} \int_{0}^{2\pi} e^{i \: k \: r \: \cos(\theta-\phi)} d\theta \: r \: dr
\end{equation}

Since the Fourier transform of a circularly symmetric function is also circularly symmetric, we can let $\phi=0$ to simplify the integral:

%\begin{equation}
\begin{align*}
\mathcal{F}(k) &= \int_{0}^{b} \int_{0}^{2\pi} e^{i \: k \: r \: \cos(\theta)} d\theta \: r \: dr \\ \\
&= 2 \: \pi \int_{0}^{b} J_0 (k \: r) r \: dr
\end{align*}
%\end{equation}

Where $J_0$ is the $0^{th}$ order Bessel function. Through the change of variables $u=k \: r$, we can  write the integral as
\begin{equation}
\mathcal{F}(k) = \frac{2\pi}{k^2} \int_{0}^{k \: b} J_0 (u) u \: du \\ \\
\end{equation}
which has the well known solution
\begin{equation}
\mathcal{F}(k) = \frac{2\pi}{k^2} J_1 (k \: b) k \: b  \\ \\
\end{equation}
or equivalently:
\begin{equation}
\mathcal{F}(k) = 2 \pi \: b^2 \bigg[ \frac{J_1 (k \: b)}{k \: b} \bigg]  \\ \\
\end{equation}
The wavenumber $k$ corresponds to a position in the focal plane
equivalent to some off-axis angle $\theta$; it turns out that (based
on the text below Equation \ref{eq:psf_tophat}) $k=2\pi\theta
/ \lambda$ (a future version of these solutions should explain why
those precise units!). Thus:
\begin{equation}
\mathcal{F}(\theta) = 2\pi \: b^2 \left[
\frac{J_1 (2\pi b \theta /\lambda)}{2\pi b \theta / \lambda} \right]  \\ \\
\end{equation}

To get the Fourier transform corresponding to Equation (1), we can
simply take the result from Equation (5) and subtract from it the same
equation with b replaced by a:
\begin{equation}
\mathcal{F}(\theta) = 2 \pi \: b^2 \bigg[
\frac{J_1 (2\pi b \theta/\lambda)}{2\pi b \theta / \lambda} \bigg] -
2 \: \pi \: a^2 \bigg[ \frac{J_1 (2 \pi a \theta/\lambda)}{2\pi
a \theta / \lambda} \bigg]  \\ \\
\end{equation}

The analytic form of the diffraction limited point spread function is
then given by

\begin{equation}
I(k) = \Bigg| 2 \: \pi \: b^2 \bigg[ \frac{J_1 (2 \pi b \theta
/\lambda)}{2\pi b \theta / \lambda} \bigg]
- 2 \: \pi \: a^2 \bigg[ \frac{J_1 (2 \pi a \theta / \lambda)}{2\pi
a \theta/\lambda} \bigg] \Bigg|^2  \\ \\
\end{equation}
\end{answer}

\section{Numerics and Data Exercises}

\begin{enumerate}
\item Calculate the ideal point spread function for a Cassegraine-type
design with four struts to hold the secondary creating an extra
obstruction. Compare to an actual color image from the Hubble Space
Telescope and comment on where the diffraction-related features in
that image come from.
\item Add random small scale phases shifts to your ideal
aperture. What is the effect of this? How large do these shifts need
to be before they strongly affect the point spread function?
\end{enumerate}

\bibliographystyle{apj}
\bibliography{exex}


\documentclass{article}
\usepackage{amsfonts}
\begin{document}

\newcommand{\op}[1]{\mathsf{#1}}
\newcommand{\cnt}{c}
\newcommand{\scst}{\sigma}
\newcommand{\dyst}{\delta}
\newcommand{\pest}{\pi}
\newcommand{\sym}{\iota}
\newcommand{\prop}[1]{\mathsf{#1}}
\newcommand{\fun}[1]{\mathsf{#1}}
\newcommand{\type}[1]{\mathcal{#1}}
\newcommand{\val}[1]{\mathsf{#1}}
\newcommand{\eval}{\mathcal{E}}
\newcommand{\syn}[1]{\mathsf{#1}}
\newcommand{\bif}{\quad \mathbf{if} \quad}

\section{Formal semantics}

(Note: The brace syntax, ie. $\{ a \in \mathbb{N}, b \in \mathbb{Z} \}$,
sometimes denotes a record with two named fields, $a$ and $b$. Probably need
less confusing syntax for this.)

\subsection{Definitions}

\[ \sym \in \type{I} \]

An \emph{identity}, an opaque symbol that identifies some object or program
component. Identities can be compare for equality and an identity is equal only
to itself.

\begin{eqnarray*}
v \in \type{V}
& \in & \val{integer}(\mathbb{Z}) \\
& | & \val{string}(\Sigma^*) \\
& | & \val{lambda}(\type{S}, \type{I}^*, \type{A}) \\
& | & \val{object}(\type{I}) \\
& | & \ldots
\end{eqnarray*}

Values.

\begin{eqnarray*}
a \in \type{A}
& \in & \syn{with\_escape}(\type{I}, \type{A}) \\
& | & \syn{ensure}(\type{A}, \type{A}) \\
& | & \syn{escape}(\type{I}, \type{A}) \\
& | & \syn{variable}(\type{I}) \\
& | & \ldots
\end{eqnarray*}

Abstract syntax.

\[ \cnt \in \type{C} = \type{V} \mapsto \type{P} \mapsto \type{V} \]

A continuation. Accepts the result of the immediately preceding evaluation and
the current pervasive state and produces another value, the result of continuing
the computation.

\[ \scst \in \type{S} = \{ b \in (\type{I} \times \type{V})^* \} \]

The \emph{lexical scope} state which encapsulates the locally scoped context of
a computation, the state that only depends on lexically enclosing
information. The lexical state is not passed from a caller into a method being
called, for instance.

The $b$ component is the bindings visible in the given scope, a mapping from
variables by identity to values.

\[ \dyst \in \type{D} = \{ e \in \type{I} \mapsto \type{C} \} \]

The \emph{dynamic scope} state encapsulates the dynamic context of a
computation.  Being dynamically scoped means that it may be passed from a caller
into a method being called but never backwards, from a called method into the
caller.

The $e$ component is the continuation to use when escaping non-locally. The
first argument is the identity of the destination to escape to.

\[ \pest \in \type{P} = \{ i \subset \type{I} \} \]

The \emph{pervasive} state encapsulates unconstrained, unscoped, state.
Side-effects operate on the pervasive state. The pervasive state is passed
linearly through each step of the computation so the effects of a method call on
the pervasive state is visible to the caller, unlike the lexically and
dynamically scoped state.

The pervasive state is immutable, the appearance of changing state is obtained
by producing a new pervasive state with the change applied and using that going
forward, discarding the old state. The new state is said to be \emph{downstream}
of the old state. This is how evaluation order is made explicit: given two
expressions $a_0$ and $a_1$ which are evaluated in pervasive states $\pest_0$
and $\pest_1$ respectively, $a_1$ behaves as if it comes after $a_0$ in
evaluation order exactly if $\pest_1$ is downstream of $\pest_0$.

The $i$ component is the finite set of identities that have already been used
during the evaluation.

\[ \eval \in \type{A} \mapsto \type{C} \mapsto \type{S} \mapsto \type{D} \mapsto \type{P} \mapsto \type{V} \]

The evaluation operator which accepts an expression, a continuation, and one of
each of the three kinds of environment an evaluation takes place within. The
$\eval$ operator is defined below.

\subsection{Utilities}

\begin{eqnarray*}
\fun{gensym}(\pest_0) & = & (\sym, \pest_1) \\
\sym & \notin & \pest_0.s \\
\pest_1 & = & \pest_0 / \{ s = \pest_0.s \cup \{ \sym \} \}
\end{eqnarray*}

The $\fun{gensym}$ function accepts a pervasive state and yields a fresh symbol
and a new pervasive state to use from that point on. It is guaranteed that the
same symbol will never be returned from the new pervasive state or any states
further downstream.

\subsection{Non-local escapes}

Three expression forms deal with non-local escaping, that is, aborting a
sub-evaluation abruptly and returning control to an arbitrarily distant
enclosing evaluation.  They are: $\syn{with\_escape}$ which sets up a location
that can be escaped to, $\syn{escape}$ which initiates an escape to a particular
location, and $\syn{ensure}$ which registers an expression, the ensure-block, to
always be evaluated however another expression, the body, completes, whether
normally or by escaping.

\subsubsection{With-escape}

The $\syn{with\_escape}(\sym, a) $ form creates a \emph{non-local escape} object
that, when called in a sub-evaluation, attempts to return control to the
$\syn{with\_escape}$ form that created it. The escape object's call method
accepts a value which will be used as the value of the $\syn{with\_escape}$
expression if escaping succeeds. How an escape may not be successful is
discussed under $\syn{escape}$.

If the body completes normally the value of the whole expression will be the
value of the body expression.

Here's an example of a typical use of $\syn{with\_escape}$,

\begin{verbatim}
def $result := with_escape ($break) {
  for ($k, $v) in $elements do {
    if $k == $key
      then $break($v);
  }
  null;
}
\end{verbatim}

This code iterates through key/value pairs from the collection
\texttt{\$elements} and if a pair if found whose key matches \texttt{\$key}, the
key we're looking for, we immediately return the value as the value of the
expression. If no key is found we don't need to escape and the computation
completes normally with value \texttt{null}.

A $\syn{with\_escape}$ can only be successfully escaped once; completing the
body normally is considered as a successful escape.

\begin{eqnarray*}
\eval(\syn{with\_escape}(\sym, a), \cnt, \scst_0, \dyst_0, \pest_0) & \to &
\eval(a, \cnt, \scst_1, \dyst_1, \pest_1)
\end{eqnarray*}

where,

\begin{eqnarray*}
(\sym_e, \pest_1) & = & \fun{genuid}(\pest_0) \\
\dyst_1 & = & \dyst_0 / \{ e = e_e \} \\
e_e(\sym_t) & = & c \bif \sym_t = \sym_e \\
e_e(\sym_t) & = & \dyst_0.e(\sym_t) \bif \sym_t \neq \sym_e \\
\scst_1 & = & \scst_0 / \{ b = b_e \} \\
b_e & = & (\sym, v_e) :: \scst_0.b \\
v_e & = & \val{lambda}(\bot, \sym_l, \syn{escape}(\sym_e, \syn{variable}(\sym_l)))
\end{eqnarray*}

The fresh symbol $\sym_e$ identifies this escape location and is used when
escaping to decide when we've reached the target. $e_e$ is the escape
continuation to be used in the body of the expression. If it is used to escape
to this expression it aborts by immediately calling the expression's
continuation. If the target is a different location it continues propagating
through the enclosing escape continuation. $v_e$ holds the escape object which
is made available to the body of the expression through the name $\sym$.

\subsubsection{Ensure}

The $\syn{ensure}(a_b, a_e) $ form evaluates its body expression, $a_b$ in a
context where, regardless of how the evaluation completes, normally or through a
non-local escape, the ensure expression $a_e $ will be executed. If the body
completes normally the value of the entire expression will be the body's value;
the ensure block will be evaluated but the value will be discarded.

\begin{eqnarray*}
\eval(\syn{ensure}(a_b, a_e), \cnt, \scst_0, \dyst_0, \pest_0) & \to &
\eval(a_b, c_n, \scst_0, \dyst_1, \pest_0)
\end{eqnarray*}

where

\begin{eqnarray*}
\dyst_1 & = & \dyst_0 / \{ e = e_e \} \\
e_e(\sym_t, v^e_b, \pest^e_1) & = & \eval(a_e, \dyst_0.e(\sym_t), \scst_0, \dyst_0, \pest^e_1) \\
\cnt_n(v^n_b, \pest^n_1) & = & \eval(a_e, c_d, \scst_0, \dyst_0, \pest^n_1) \\
\cnt_d(v_e, \pest^n_2) & = & \cnt(v^n_b, \pest^n_2)
\end{eqnarray*}

There are two ways the body can complete, either normally or by escaping. The
$e_e$ escape continuation handles the abrupt case: if the body escapes $e_e$
will be called and it will evaluate the ensure block. It passes the enclosing
escape continuation as the normal continuation such that on completion of the
ensure block the escape process will continue. The ensure block is evaluated in
the original dynamic scope such that if it escapes itself it is not evaluated
again.

The normal case is handled by $\cnt_n$ which evaluates the ensure block, then
discards the result; that's the purpose of $\cnt_d$, and finally yields the
value of the block as the result of the whole expression.

\subsubsection{Escape}

The $\syn{escape}(\sym, a)$ form is synthetic and only produced by the
$\syn{with\_escape}$ form. The symbol $\sym$ identifies where the expression
will attempt to escape to and the expression $a$ provides the value to escape
with.

The escape process takes care to call any ensure blocks between where it is
called and its destination. Since ensure blocks contain arbitrary code they
themselves can initiate non-local escapes, in which case the non-local escape
process that caused the ensure block to be evaluated will be abandoned. Because
of this an escape can be invoked meaningfully more than once as long as none of
the previous calls have succeeded in escaping to their target.

\begin{eqnarray*}
\eval(\syn{escape}(\sym, a), \cnt, \scst_0, \dyst_0, \pest_0) & \to &
\eval(a, \dyst_0.e(\sym), \scst_0, \dyst_0, \pest_0)
\end{eqnarray*}

Escaping abandons its normal continuation and replaces it in the evaluation of
the value with the current non-local escape continuation, binding the
destination to the escape's target.

\end{document}


%!TEX root = ../gronskiy_phd_thesis.tex
\chapter{Background}
\label{sec:background}

\hfill
\begin{minipage}[t]{.75\textwidth}
\cyrins{%
\textit{%
            ``Мы все учились понемногу \\
            Чему-нибудь и как-нибудь, \\
            Так воспитаньем, слава богу, \\
            У нас немудрено блеснуть.''}} \\[.2cm]
  \textit{(rus.
    ``We all meandered through our schooling \\
     haphazard; so, to God be thanks, \\
     it's easy, without too much fooling, \\
     to pass for cultured in our ranks.'')}\\
  \hrule
  \vspace{.2cm}
  \hfill
  \textsc{--- Alexander PUSHKIN}, ``Eugene Onegin''
\end{minipage}
\\[.5cm]

In this chapter, we gather some standard background assumptions, definitions,
theorems, tools and approaches to which we refer throughout the thesis.

\myargremark{On the notation}
Although the thesis involves a broad range of topics and fields (coding,
combinatorics, learning, algorithms and statistical mechanics), we made an
effort to unify the notation and keep it consistent \textit{across these
fields}. While it is not always fully possible, the reader can assume that from
now on he/she will be exposed to this unified notation. This is done on purpose
to allow a simpler grasping of analogies between the same notion in different
contexts. At the cost of this, sometimes the notation might seem to be
unconventional w.r.t. each specific topic.

\section{Probability Theory}
It is hard to find a field nowadays where probability theory is \textit{not}
used. Being no exception, we are going to utilize the standard axiomatization of
probability theory, developed by A.N.~Kolmogorov. While we refer the reader (in
case of interest) to~\citet{shiryaev95} for details, here we list just basic
information and our assumptions to keep the notation of the thesis
self-contained.

Due to the discrete nature of the problems considered in this thesis, we will
mostly work with discrete distributions (except for data generation sources,
which will be e.g. Gaussians), hence almost all the theorems and derivations
will be stated using $\sum$ sign.

For brevity, we will omit subscripts of expectations $\Expct[\cdot]$, variances
$\Var[\cdot]$ and covariances $\Cov(\cdot, \cdot)$, except for the cases where
there are several randomness sources and we need to distinguish between them.

We will utilize the term \textit{moment-generating function}
\index{Moment-generating function} defined as follows.
\begin{definition}[Moment-generating function]
  For a random variable $X$, we call a function
  \begin{equation}
    G_X(t) \coloneqq \Expct[e^{tX}], \quad t \in \mathbb{R}
  \end{equation}
  a moment-generating function of $X$.
\end{definition}
Moment-generating functions might not exist, and in some derivations we will pay
particular attention to that.

\section{Information Theory}

\index{Information theory}
In this thesis, we are going to utilize some terminology of information theory.
While one of the best books on the topic remains to be~\citep{Cover:2006}, we
give here some basic definitions and theorems.

\subsection{Bits, Nats and the Usage of Logarithm}
\index{Nat (measure of information)}
\index{Bit (measure of information)}
It is common to use logarithms to base $2$ in information theory since they
yield an easily interpretable measure of information called \textit{bit}, which
quantifies information in \textit{binary outcomes}.

However, one can resort, without loss of generality, to utilizing the natural
logarithm: in this case all the results of information theory hold, however, the
information measure is changed to \textit{nats}, which, informally speaking,
quantifies information in \textit{base-$e$ outcomes}.

Generally, the base-$e$ approach is characteristic for problems with a
dominant physical or general mathematical viewpoint, while the base $2$ approach
is reserved by information theory.

With a slight abuse of notation (but w.l.o.g.), we will utilize same notation
$\log(\cdot)$ for both binary and natural logarithm, and it is made clear from
the context, which of them is currently used. For a very general reference, a
binary logarithm is mainly used in Chapters~\ref{ch:gen_appch} and~\ref{ch:mst},
and a natural logarithm is used in
Chapters~\ref{ch:free_energy} and~\ref{ch:smbp_and_rem}, since the latter ones
are more inclined into statistical mechanics.

\subsection{Entropy and KL-Divergence}
One of the central points of this thesis is \textit{uncertainty}. This concept
obtained its rigorous definition\footnote{This very phrase ``rigorous definition
of uncertainty'' may already surprise!} through the notion of \textit{entropy}.
\index{Shannon entropy}
\index{Entropy|see{Shannon entropy}}
\begin{definition}[Shannon entropy]\label{def:shannon_entropy}
  Assume a discrete random variable $X$ is distributed according to $p(x)$. Then,
  the Shannon entropy of $X$ is defined as
  \begin{equation}
    H(X) \equiv H(p) \coloneqq -\sum_{x} p(x) \log p(x).
  \end{equation}
\end{definition}
\nomenclature[C, 00]{$H(X)$, $H(p)$}{Shannon entropy\nomnorefeqpage}%
Shannon entropy quantifies the average \textit{surprise of the outcome} $h(x)$:
\begin{equation}
  H(X) \equiv \Expct[h(x)] \equiv \Expct[ - \log p(x)].
\end{equation}
The term \textit{surprise} refers here to self-contained information contained
in the outcome, hence the entropy refers to the average self-contained
information. The higher is the entropy, the higher is the average self-contained
information of outcomes, hence the higher is uncertainty of the distribution. A
classical example is a coin toss experiment, where the heads probability
euqals $p$. Consider two characteristic cases:

a) for a highly unfair coin (i.e. $p = 0$ or $p = 1$), the outcome of each
toss does not bring much information (``surprise'') as it is known beforehand. The entropy
is the lowest (zero bits) in this case;

b) for a fair coin (i.e. $p = 1/2$), the outcome brings a lot of information, as
we cannot have any expectations about it beforehand. The entropy is the
highest (one bit) in this case.

The joint entropy of two random variables $X$ and $Y$ is defined similarly:
\begin{equation}
  H(X, Y) \coloneqq -\sum_{x, y} p(x, y) \log p(x, y).
\end{equation}

\index{Kullback-Leibler divergence}
Next, we will use the term \textit{Kullback-Leibler divergence} (KL-divergence).
\begin{definition}[KL-divergence]
\label{def:kl_divergence}
  Assuming two discrete distributions $p(x)$ and $q(x)$, a relative entropy, or
  Kullback-Leibler (KL) divergence is defined as
  \begin{equation}
    \KL(p \| q) \coloneqq \sum_{x} p(x) \log \biggl( \frac{p(x)}{q(x)} \biggr).
  \end{equation}
  \nomenclature[C, 04]{$\KL(p \parallel q)$}{Kullback-Leibler divergence\mynomdef{def:kl_divergence}}%
  \index{Kullback-Leibler divergence}
  \index{KL|see{Kullback-Leibler divergence}}
\end{definition}
KL-divergence is one of the possible measures of ``distance'' between
distributions, and it quantifies how much additional bits (nats) of information
is required for stable communication, if the receiver assumes source
distribution $q$ instead of the true source distribution $p$.

The next important quantity is \textit{mutual information}. \index{Mutual information}
\begin{definition}[Mutual information]
\label{def:inf_theory_mutual_information}
  Assuming two random variables $X$ and $Y$, the mutual information between them
  is defined as
  \begin{equation}
    \MI(X, Y) \coloneqq \KL(p(x, y) \| p(x) p(y)) = \sum_{x,y} p(x, y)
      \log \biggl( \frac{p(x, y)}{p(x)p(y)} \biggr).
  \end{equation}
  \nomenclature[C, 04]{$\MI(X, Y)$}{mutual information\mynomdef{def:inf_theory_mutual_information}}%
  \index{MI|see{Mutual information}}
\end{definition}
Mutual information is the information-theoretic measure of independence. In other
words, one can show that $\MI(X, Y) = 0 \Leftrightarrow X \independent Y$ (symbol
``$\independent$'' denotes stochastic independence).
\nomenclature[B, 07]{$X \independent Y$}{independence of random variables}%

We will also utilize the fact that
\begin{equation}\label{eq:background_mi_decomposition}
  \MI(X, Y) = H(X) + H(Y) - H(X, Y),
\end{equation}
which can be easily observed.

\subsection{Coding Theory, Channels and Code Rates}
\label{sec:background_coding}
Another concept of information theory, whose importance is exceptionally high
for our purposes, is \textit{coding theory}. \index{Coding theory} For a
detailed explanation of the following definitions we refer the reader to~\citep[Chapter 7]{Cover:2006}, while
here we give, as usual, only necessary basics.

\index{Discrete coding channel}
\index{Channel|see{Discrete coding channel}}
\index{Memoryless channel|see{Discrete coding channel}}
\begin{definition}[Channel] \label{def:discrete_coding_channel}
  A channel is defined by an input alphabet $\mathcal{X}$, an output alphabet
  $\mathcal{Y}$, an input distribution $p(x)$ as well as a transition probability
  matrix $p(y|x)$. We denote such channel as $(\mathcal{X}, p(y|x),
  \mathcal{Y})$. A message $X$ sent through it turns into a message $Y$ received
  according to the transition matrix $p(y|x)$.
\end{definition}
\nomenclature[C, 05a]{$\mathcal{X}$, $\mathcal{Y}$}{channel input-output alphabets\mynomdef{def:discrete_coding_channel}}%
\nomenclature[C, 05b]{$(\mathcal{X}, p(y \vert x), \mathcal{Y})$}{discrete coding channel\mynomdef{def:discrete_coding_channel}}%

\index{Memoryless channel|see{Discrete coding channel}}
\begin{definition}[Memoryless channel]
  A channel is called memoryless if the probability distribution of the output
  depends only on the input at that time and is conditionally independent of
  previous channel inputs or outputs.
\end{definition}

\index{Code}
\index{Codebook}
\index{Codebook!Vectors}
\begin{definition}[Code] \label{def:discrete_code}
  An $(M, n)$ code for channel $(\mathcal{X}, p(y \vert x), \mathcal{Y})$ consists of
  \begin{itemize}
    \item an index set $\{1, \ldots, M\}$;
    \item an encoding rule $f^n \colon \{1, \ldots, M\} \to \mathcal{X}^n$, where
      each $f^n(i)$ is called codebook vector. Set $\{ f^n(i)\}_{i=1}^M$ is called
      codebook.
    \item a deterministic decoding rule $g \colon \mathcal{Y}^n \to \{1, \ldots,
    M\}$.
  \end{itemize}
  \nomenclature[C, 05c]{$(M, n)$}{discrete code parameters\mynomdef{def:discrete_code}}%
\end{definition}
This definition will become highly important for understanding of Chapter~\ref{ch:gen_appch},
in conjunction with the definition of channel capacity and code rate.

\index{Channel capacity}
\begin{definition}[Channel capacity] \label{def:inf_theor_channel_capacity}
  The channel capacity of the channel $(\mathcal{X}, p(y \vert x), \mathcal{Y})$
  is defined as
  \begin{equation}
    C \coloneqq \max_{p(x)} \MI(X, Y).
  \end{equation}
  \nomenclature[C, 05d]{$C$}{channel capacity\mynomdef{def:inf_theor_channel_capacity}}%
\end{definition}

\index{Code!Rate}
\begin{definition}[Code rate] \label{def:inf_theor_code_rate}
  The rate $R_\mathrm{code}$ of an $(M, n)$ code is defined as
  \begin{equation}
    R_\mathrm{code} \coloneqq \frac{\log M}{n}
  \end{equation}
  bits (nats) per transmission.
  \nomenclature[C, 05e]{$R_\text{code}$}{code rate\mynomdef{def:inf_theor_code_rate}}%
\end{definition}

The rate is called \textit{achievable}, if there exists a sequence of
$(2^{nR_\text{code}}, n)$ codes such that the maximal probability of decoding
error goes to zero.

Finally, it is important to realize that the capacity $C$ is the supremum of
all achievable rates, according to Shannon's Channel Coding Theorem which will be
stated later. This fact establishes an important connection between the mutual information
and the bitrate \index{Channel!Bitrate} of the channel.

\section{Disordered Systems and Statistical Mechanics}

\index{Disordered systems}
\index{Statistical mechanics}
Large part of the thesis works with the terminology of statistical mechanics.
A good overview is given in~\citep{bovier2012statistical}, and here we summarize
the basics specific to our work.

\subsection{Disordered Systems}
\label{sec:background_disordered_systems}

Statistical mechanics, which will play a major role in the second half of this
thesis (Chapters~\ref{ch:free_energy} and~\ref{ch:smbp_and_rem}), focuses on
systems consisting of a large number of \textit{particles}, which can find
themselves in certain aggregated \textit{states}, or \textit{configurations}.

As an example, let us consider a so-called $n$-spin system. It is represented by
some physical matter which consists of $n$ particles (atoms) $s_i$, each of
which exhibits one of the two possible directions of the magnetic moment vector
of the atom, called \textit{spin}
\begin{equation}
  \sigma_i \in \S \equiv \{-1, +1\}
\end{equation}
(i.e.~atom's magnetic field is directed ``up'' or ``down'').
\index{State|see{Statistical mechanics}}
\index{Configuration|see{Statistical mechanics}}
\index{Statistical mechanics!State}
\index{Statistical mechanics!Configuration}
\index{Spin glass}
In this system, a configuration $c_{\boldsymbol\sigma} \in
\C$ is defined as an ensemble of spins of its particles, i.e. a vector
\begin{equation}
  c_{\boldsymbol\sigma} = \langle \sigma_1, \ldots, \sigma_n \rangle.
\end{equation}
\nomenclature[F, 20a]{$\langle \sigma_1, \ldots, \sigma_n \rangle$}{configuration of spins}%
\nomenclature[F, 20b]{$c_{\boldsymbol\sigma}$}{configuration}%
\nomenclature[F, 20c]{$J_{ij}$}{interaction matrix}%

\index{Hamiltonian}
An object used in conjunction with a configuration is its \textit{Hamiltonian},
or \textit{energy}:
\begin{equation}\label{eq:background_deterministic_hamiltonian}
  R(c_{\boldsymbol\sigma}) = -\sum_{i,j} J_{ij} \sigma_i \sigma_j,
\end{equation}
where $J_{ij}$ is a predefined interaction value between spin $i$ and spin
$j$\footnote{Other Hamiltonians are possible (e.g. the ones with external field,
etc.) which depends on the model; describing them is beyond the scope of the
thesis. A very good overview is given in~\citep{bovier2012statistical}.}.

\index{Disorder}
However, there is no randomness yet in this system, which would bring the
\textit{disorder}. To introduce it, assume that there is a random variable $X
\in \mathcal{X}$ which is distributed according to a certain law. In general case,
its nature should not necessarily be aligned with the nature of other model
elements (particles, configurations), i.e. from a model design point of view,
$X$ is a source of randomness and anything more than that. If we now assume that
the interaction matrix $J_{ij}$ depends on it, i.e. $J_{ij} = J_{ij}(X)$, the
Hamiltonian becomes random:
\begin{equation}
  R(c_{\boldsymbol\sigma}, X) = -\sum_{i,j} J_{ij}(X) \, \sigma_i \sigma_j.
\end{equation}
The described system which consists of \\
%
\hspace*{1.5\parindent} a) a source of randomness $X \in \mathcal{X}$, and \\
%
\hspace*{1.5\parindent} b) a set $\C$ of configurations\footnote{In the main part of
the thesis, we will call them ``solutions'', highlighting an optimization
prospective.} $c_{\boldsymbol\sigma}\in \C$, and \\
%
\hspace*{1.5\parindent} c) a random Hamiltonian\footnote{In the main part of the
thesis, we will call it a ``cost'', highlighting an optimization prospective.}
$R(c_{\boldsymbol\sigma}, X)$ \\
%
defines a \textit{disordered system}. Its nature is very closely related to the nature
of randomized optimization, and as we will see already in
Chapter~\ref{ch:gen_appch}, the ingredients of the latter are the same.

\subsection{Gibbs Measures}
The Gibbs measures are an important class of measures which arise naturally in conjunction
with statistical mechanics of disordered systems. It is defined as follows.

\begin{definition}
  The Gibbs measure over the configurations is the distribution
  \begin{equation}
    p_\beta(c | \data) =\frac{1}{Z(\beta, X)} \exp(-\beta R(c, \data)),
  \end{equation}
  where the normalization term
  \begin{equation}
    \quad Z(\beta, \data) = \sum_{c^\prime\in \C} \exp(-\beta R(c^\prime, \data))
  \end{equation}
  is called partition function.
\end{definition}

\myremark We remind the reader that the above quantities are random due to their the
parametrization by random source $X$. If it is dropped, then the Hamiltonians
turn from random into deterministic ones
(see~\eqref{eq:background_deterministic_hamiltonian} above).

\subsection{Thermodynamic Properties of Disordered Systems}
Large disordered systems are characterized by macroscopic thermodynamic
properties. One of the most important for us will be \textit{Helmholtz free
energy}. While we discuss its altered (adjusted to our needs) version in detail
later in Chapters~\ref{ch:free_energy} and~\ref{ch:smbp_and_rem}, here we state
its classical definition and provide motivation for it.

\begin{definition}\label{def:background_free_energy}
  Helmholtz free energy is originally defined as
  \begin{equation}
    \frenergy(\beta, X) = -\frac{1}{\beta}\log [Z(\beta, X)]
  \end{equation}
\end{definition}
Due to parametrization by a random source $X$, Helmholtz free energy is itself a
random variable. In this situation, studying its moments (e.g. the expected value) becomes a central
point~\citep[Chapter~9]{bovier2012statistical}. The same will hold for our case,
as explained in much more detail in Chapter~\ref{ch:free_energy} where we will define
\begin{equation}
  \frenergy(\beta) = \mathrm{const} \cdot \Expct_X \frenergy(\beta, X).
\end{equation}
The constant is not important for now.

The importance of the log-partition function is hard to overestimate: it
implicitly contains many other parameters of the system. For example, Shannon
entropy (see~Definition~\ref{def:shannon_entropy}) of the Gibbs measure can be
expressed through it:
\begin{align}
  H(p_\beta) &= -\sum_{c\in \C} p_\beta(c) \log p_\beta(c) \notag \\
    &= \log Z(\beta) - \Expct_{p_\beta(c)}[- \beta R(c)] \notag \\
    &= \log Z(\beta) - \sum_{c \in \C} \frac{- \beta R(c) e^{-\beta R(c)}}{Z(\beta)} \notag \\
    &= \log Z(\beta) - \beta \, \frac{\partial}{\partial \beta} \log Z(\beta).
\end{align}

\subsection{Maximum Entropy Principle}
\label{sec:background_max_entropy}

\index{Maximum entropy principle}
\index{Testable information}
\index{Occam's razor}
In the following, we will refer to Jaynes' \textit{maximum entropy}
principle~\citep{Jaynes57a,Jaynes57b}. This principle states, informally
speaking, that the current state of a physical system is represented by a
distribution which has the highest possible entropy under the given constraints.
This concept is related to a commonly used Occam's razor principle which states
that everything should be explained in the simplest possible way given the
observed constraints.

More formally speaking, under the observed data $X$, the distribution which is
followed by a physical system is the following:
\begin{equation}\label{eq:background_max_ent}
  \hat p(c) = \arg \max_{p \colon \mathrm{obs}(p) = X} H(p),
\end{equation}
where $\mathrm{obs}(p)$ is the function which yields observed data (also-called
\textit{testable information}; for example, it extracts moments of
the distribution~--- and thus in some sense ``tests'' it).

We will largely utilize the fact that the maximum entropy distribution under certain
constraints is the Gibbs distribution:
\begin{align}
  p_\beta(c|\data) &=\frac{1}{Z(\beta, X)} \exp(-\beta R(c, \data))
    \quad\text{with} \notag \\
  \quad Z(\beta, \data) &= \sum_{c^\prime\in \C} \exp(-\beta R(c^\prime, \data))\,,
\end{align}
where $R(c, X)$ is determined explicitly by the context, and $\beta$ comes from
the method of Lagrange multipliers when optimizing~\eqref{eq:background_max_ent}
and has a semantics of an \textit{inverse temperature}.


\subsection{Pre-training}

Pre-training. eg train on general pictures before specific stuff. means you fit many parameters for detecting edges etc firsts

\subsubsection{Learning rate}

Reduce the learning rate.


\small{
\section{Meshes used in the convergence study of APBS and Bempp}\label{sec:apbs_mesh}
Table \ref{tab:APBS_mesh} summarizes the mesh sizes of the molecules used in the convergence study of APBS and Bempp.
The former uses Cartesian grids while the latter uses surface meshes generated by \texttt{Nanoshaper}.

\begin{table}[]
    \centering
    \resizebox{\columnwidth}{!}{%
    \begin{tabular}{c|ccc|ccc}
    \multirow{2}{*}{ID} & \multicolumn{3}{c|}{APBS} & \multicolumn{3}{c}{Bempp} \\
                        & coarse  & medium  & fine & coarse  & medium  & fine   \\ \hline
    1AJJ                & $65 ^3$     & $129^3$     & $257^3$  & 8544    & 17200   & 34468  \\
    1VJW                & $97 ^3$     & $193^3$     & $385^3$  & 11544   & 23296   & 46660  \\
    5PTI                & $97 ^3$     & $193^3$     & $385^3$  & 12512   & 25204   & 50596  \\
    1R69                & $97 ^3$     & $193^3$     & $385^3$  & 12004   & 24216   & 48648  \\
    1A2S                & $65 ^3$     & $129^3$     & $257^3$  & 17784   & 35732   & 71648  \\
    1SVR                & $97 ^3$     & $193^3$     & $385^3$  & 18644   & 37348   & 75004  \\
    1A63                & $129^3$     & $257^3$     & $513^3$  & 27996   & 56356   & 113124 \\
    1A7M                & $129^3$     & $257^3$     & $513^3$  & 30576   & 61652   & 123776 \\
    1F6W                & $129^3$     & $257^3$     & $513^3$  & 77464   & 156140  & 313692
    \end{tabular}
    }
    \caption{Mesh sizes of the molecules used in the convergence study of APBS and Bempp.}
    \label{tab:APBS_mesh}
\end{table}

\section{Comparison with MIBPB}\label{sec:comp_mibpb}
We also compared the solvation energy computed from Bempp and another trusted finite-difference code MIBPB using the same set of molecules.
The version we used was downloaded from \href{https://weilab.math.msu.edu/MIBPB}{https://weilab.math.msu.edu/MIBPB}.
We enabled the linearized PB solver option in MIBPB and set the grid resolution parameter to 0.25 to produce finer grids.
Correspondingly, we selected Bempp's results from the finest mesh, whose size is shown in Table \ref{tab:APBS_mesh}, to compare.
Other parameters are the same as those in the grid-convergence study, listed in Table \ref{tab:convergence}.
Table \ref{tab:MIBPB_result} presents the grid sizes used in MIBPB and the solvation energy computed from both software.
The difference in $\Delta G_{solv}$ varies between 0.05\% and 1.9\% across all molecules.

\begin{table}[]
    \centering
    \resizebox{0.8\columnwidth}{!}{%
    \begin{tabular}{c|c|c|c}
    \multirow{2}{*}{ID} & \multicolumn{2}{c|}{MIBPB}      & Bempp             \\
                        & grid size   & $\Delta G_{solv}$ & $\Delta G_{solv}$ \\ \hline
    1AJJ                & $132\times 142\times 154$ & -271.59           & -269.90           \\
    1VJW                & $137\times 160\times 148$ & -307.71           & -305.56           \\
    5PTI                & $145\times 158\times 178$ & -317.54           & -317.37           \\
    1R69                & $167\times 155\times 148$ & -270.94           & -267.88           \\
    1A2S                & $173\times 166\times 186$ & -463.11           & -465.34           \\
    1SVR                & $174\times 187\times 192$ & -410.40           & -403.97           \\
    1A63                & $283\times 184\times 186$ & -581.02           & -573.83           \\
    1A7M                & $191\times 266\times 225$ & -531.93           & -536.91           \\
    1F6W                & $330\times 272\times 309$ & -1336.48          & -1311.17
    \end{tabular}
    }
    \caption{MIBPB and Bempp results of the solvation energy $\Delta G_{solv}$ of different molecules, in units of kcal/mol.}
    \label{tab:MIBPB_result}
\end{table}

}


\section{Introduction}
\subsection{History of the study of the brain and neuroscience}
It was thought in the Late Middle Kingdom era (2040-1782BC) that the heart was the holder of intelligence, given that in preparation for mummification, the brain was removed; hence to learn something by heart \cite{dintroduction}. By the fifth century BC it was the Pythagorean Alcmaeon of Croton who considered the brain to be where the mind was located \cite{inbook}. This was then taken further when Hippocrates believed the brain to be the seat of intelligence \cite{finger2001origins}. Over the next 2000 years, through dissection, more and more parts of the brain were being discovered.

Leaping forward to the first half of the $19^{th}$ century, we get a variety of discoveries  involving electrical currents and the brain. This led to the discovery in the middle of the $19^{th}$ century that neurons were electrically excitable and that the activity of these neurons instigated a change in the electrical state of adjacent neurons \cite{2013}. During the second half of the $19^{th}$ century, we had discoveries of the connection between the brain regions and sensory and motor functions. In particular, we look at the somatosensory region, which accounts for things like sense of touch and perception \cite{2005}.

The somatosensory cortex contains many columns. Each column contains 6 layers, although in some instances, this can be 4, though we do not discuss these here. These layers reach from the higher cortical layers, namely, 1 and 2, through to the deeper cortical layers of 5 and 6. It is one of these cortical columns that we take our focus on and describe in more detail in the next section \cite{Reimann_2017}.
\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=12cm]{graph/brain.jpg}
\caption{Regions of the brain  \cite{Whitlock2017PosteriorPC}}
\end{center}
\end{figure}
\subsection{Blue Brain Project (BBP)}
The Blue Brain Project was founded in May 2005 with the initial goal of creating a simulation of the rat neocortical column. In October of 2015, after various experimentations involving the brain \cite{2013}, the BBP created a first draft of a reconstruction of the neocortical microcircuitry in the somatosensory cortex in a juvenile rat \cite{2015}. This reconstruction contained approximately $31,000$ neurons, where, through patch-clamp studies, there were 55 layer-specific morphological and 207 morpho-electrical neuron sub-types. Through the digital reconstruction, where neurons are positioned according to a restriction to biological bouton densities and number of synapses per connection, their overlapping arbors form approximately 8 million connections from around 37 million synapses \cite{2015}.

From the BBP website on the construction of the neocortical column, we have results whereupon there are 35 instantiations of a biologically correct model that have been produced. That is, we have 5 rats, with 7 instantiations each, from which there are a further 7 instantiations that take the average of each of the instantiations of the 5 measured rats. We use the data representing the final instantiation, that is labelled as Bio-M in their paper \cite{Reimann_2017}.
\begin{figure}[H]
\begin{center}
\captionsetup{justification=centering}
\includegraphics[width=12cm]{graph/cell.png}
\caption{Visual representation of a synaptic connection between two neurons}
\end{center}
\end{figure}
\subsection{Models}
We have four random graph models of the MC plus the \ER model that we wish to compare using topological statistics as to their accuracy in replicating the statistics from the biologically correct model produced by the BBP, the Bio-M MC. The first of these random graph models, is the Configuration model, $\mathcal{G}_{C}$, which rewires the connections between the neurons, without allowing for any self loops while also preserving the in and out degree of each neuron.
Our second random graph model, the Geometric Configuration model, $\mathcal{G}_{GC}$ is identical to the Configuration model while also taking into account the observed distances between connected neurons.

The third random graph model, the Block Configuration model, $\mathcal{G}_{BC}$, extends the idea of the Configuration model by firstly subdividing the connectome into blocks on a layer by layer basis. We have 6 layers, whereby layers 2 and 3 are considered together. This accounts for 25 blocks. Within each block we have a subset of connected pairwise neurons of the connectome. Each block applies the same connection method as the Configuration model thereafter before being reassembled into a realisation of the connected random graph $\mathcal{G}_{BC}$.

In the final random graph model, the Block Geometric Configuration model, $\mathcal{G}_{BGC}$ we extend the ideas of the previous models,  $\mathcal{G}_{BC}$ and $\mathcal{G}_{GC}$, and combine them. That is to say, we again subdivide the connectome into blocks on the same basis as stated previously for the Block Configuration model, however, this time when we compute a permutation of the connected pairwise neurons in each subset, we take the added constraint of a distance-dependence. Upon completion of a rewiring of each block, we can reassemble the blocks, much like the Block Configuration model, to obtain a realisation of the random graph model $\mathcal{G}_{BGC}$.


\chapter{Algorithm}

First of all, we need to answer two questions:

\begin{enumerate}
  \item How the subgraph density should be measured?
  \item What is <<the most of the selected vertices>>?
\end{enumerate}

To compare two subgraphs obtained by different algorithms we will use edge density and the size of the resulting subgraph: $density(G) = \frac{2 \cdot |E(G)|}{|V(G)| \cdot (|V(G)| - 1)}$, $size(G) = |V(G)|$. Really, if the number of edges in obtained subgraph is quite big, we can assume that it is dense. However at the same time it should have the smallest possible size. What is <<the most of the selected vertices>> will be discussed a bit later.

\section{Idea of the algorithm}

Because there are quite few algorithms that solves exactly our problem, we have two ways: to come up with absolutely new idea and algorithm or to take an existing idea and try to improve it or to make it work on our problem instead of common \textit{CSP} with all query vertices needed to be in resulting subgraph. Starting from now we will use \textit{CSP} abbreviation for community search problem, where all query vertices should be presented in the resulting subgraph and \textit{NCSP} for noisy version of the problem, what we need only most of the query vertices in the answer.

Absolutely new algorithm may look like this: we can bruteforce the set of vertices which will be noise, then find dense subgraph that contains all query vertices except bruteforced ones using one of the described in first chapter solutions and we're done. This solution will obviously return the most optimal answer, but it works too slow, the complexity of this solution is not polynomial. So, we won't use this method and will try to come up with something else.

The easier way is to take some existing algorithm for \textit{CSP} and transform it for \textit{NCSP}. Really, it is proved that such algorithm works good and is one of the best for now, so we have a lot of chances to beat all current \textit{NCSP} solutions in that case. Here we go: let's take algorithm Barbieri et al. \cite{Barbieri15} for finding $k$-core with maximal $k$ and minimal size and try to optimize it and change so that is will solve \textit{NCSP}. One more note that makes us more assured about this idea is two articles~--- Bogdanov et al. \cite{Bogdanov13} and Cui et al. \cite{Cui14} which showed that maximization of the minimal degree in subgraph is very effective for finding the optimal community, so our idea absolutely may take place.

The idea of Barbieri et al. algorithm \cite{Barbieri15} is in finding in graph $G$ \textit{k-core} with the maximal $k$ and among all such solutions, we want to find the one with the minimal number of vertices in it. In their work, authors show that it is a NP-hard problem, so we need to use some heuristics to solve it. The idea of algorithm is based on one interesting fact, which we're going to use in our work as well: consider the core decomposition of graph $G$: $C = \{C_k\}_{k=1}^{k=k^*}$. Now let's find such maximal $k'$ that all vertices from query are lying in the same component of $H^* \in C_{k'}$. The fact is that all solutions for \textit{CSP} are lying in this component $H^*$. More formally: $k' = \max\{k | \exists H_i\mbox{~--- connected component } C_{k'}\mbox{, such that } \forall v \in Q: v \in H_i\}$. The found \textit{k-core} we will denote as $C_Q^*$ and the component which contains all query vertices~--- $H^*$. We need to note two things here: first~--- this statement helps us to decrease the size of the initial graph without loosing solutions and second~--- this statement is true even for \textit{NCSP}, because if $H^*$ contains all optimal subgraphs with all query vertices, it contains all optimal subgraphs containing only a subset of query vertices as well. It is possible that the resulting subgraph will be a \textit{k-core} with bigger $k$ than $H^*$ have, but nothing prevent us from starting our algorithm from $H^*$.

Let's explain the final idea of our algorithm: by given social network $G$ and the selected vertices $Q$, we will find \textit{k-core} $C_Q^*$ and its connected component $H^*$ that contains all optimal solutions for \textit{NCSP}. $H^*$ is actually one of the candidates for the answer, because it contains all vertices from $Q$, but it's too large and we want to make it smaller (also, this subgraph contains all query vertices, i.e. the noise as well). So, after we found $H^*$, we are going to apply some heuristics that will allow to decrease the size of $H^*$ without loosing the condition on minimal vertex degree (i.e. \textit{k-core} invariant) and therefore not making the answer worse.

\section{Step-by-step algorithm}

In this section we will describe the whole algorithm. Actually, we can divide the algorithm into two phases:

\begin{enumerate}
  \item Find $C_Q^*$ and $H^*$;
  \item Decrease size of $H^*$ without loosing the \textit{k-core} property.
\end{enumerate}

Actually, in future we will split the second phase into several more, but let's talk about it later. Now we will try to sort out with each phase separately.

\subsection{Phase 1. Finding $C_Q^*$ and $H^*$}

$C_Q^*$ and $H^*$ finding phase will be taken from Barbieri et al. article \cite{Barbieri15} as well as their idea of minimization of \textit{k-core}. It's easy to see that actually core decomposition doesn't depend on the query, so it's not necessary to build it each time. But unfortunately saving it in RAM before all the queries is not a good idea as well, because it's size may be too big. That's why we're going to do a pre-calculation which will build a core decomposition once, compress it and save on disk. It will be a so-called index which will allow us to find $C_Q^*$ and $H^*$ much faster for each query.

In index of our core decomposition $C = \{C_k\}_{k = 1}^{k = k^*}$ we will store the following information:

\begin{enumerate}
  \item Core indices for all vertices $c(v) = \max\{k \in [0..k^*] | v \in C_k \}$;
  \item For each \textit{k-core} $C_k$ we will store the set of its components $C_k = \{H_i\}$.
\end{enumerate}

The following picture illustrates the above statements~\ref{index-construct}:

\begin{figure}[!h]
\caption{Index construction}\label{index-construct}
\centering
  \begin{center}
    \makebox[\textwidth]{\includegraphics[scale=0.8]{pictures/index.png}}
  \end{center}
\end{figure}
\FloatBarrier

We also need to take a small note that some neighboring \textit{k-core}s are equal: $C_k = C_{k + 1}$. There is no need to store them twice, so we will store all duplicating neighboring \textit{k-core}s only once. For now it doesn't look like a very good optimization, but actually on the real datasets the number of different \textit{k-core}s may be even several orders less, so this optimization makes sense. We will also use variable $h$ in future~--- it means the number of different \textit{k-core}s in our core decomposition, i.e. the number of \textit{k-core}s which we're going to store in our index.

We will store code indices and sets of connected components in the appropriate data structures (hash tables). It will allow us to access the following object in $O(1)$ complexity:
\begin{itemize}
  \item $c(v)$;
  \item all components of the fixed \textit{k-core};
  \item a component of the fixes \textit{k-core} where the given vertex $v$ is located.
\end{itemize}

But actually we haven't said yet how to build this index. Actually it's quite simple: let's firstly build the set of components for $C_{k^*}$, after that all other \textit{k-core}s will be built in the following way: if \textit{k-core}s for $k^*, k^*-1, \ldots, k$ are already built, then to build \textit{$(k - 1)$-core} we will add all vertices from $C_{k - 1} \setminus C_k$ one by one and edges incident to them (according to the property that $C_{k - 1} \supseteq C_k$, it's enough) and then we will update connected components which according to the new vertices can union in new, bigger components.

This phase takes $O(h \cdot |V(G)| + |E(G)|)$ time or simply $O(hn + m)$, where $h$ as you can remember is the number of different \textit{k-core} in $G$. However this asymptotic won't be taken into the final consideration because for each graph this computation is taken only once (so we can wait if needed) and then the built index is used for finding $C_Q^*$ and $H^*$ for each query.

So, how can we find $C_Q^*$ and $H^*$ by our built index? Actually, it's very simple. Because $C_1 \supseteq C_2 \ldots \supseteq C_{k^*}$, to find \textit{k-core} with maximal order that contains all query vertices from $Q$ in its one component, we can use binary search by the \textit{k-core} order. To check if for some fixed $k$, $C_k$ contains all vertices of $Q$ in its one component, we will take for each vertex from $Q$ information about the component it is lying in (using the index, we can do it in $O(1)$ time) and then check that all these numbers are equal. So, the check for each binary search step will take $O(|Q|)$ time.

Summing up everything above, we see that the step of finding $C_Q^*$ and $H^*$ takes $O(|Q| \cdot \log(h))$.

\subsection{Phase 2. Decreasing $H^*$ size}

After finding $C_Q^*$ and its connected component $H^*$ that contains all vertices from $Q$ as we mentioned earlier it's easy to see that $H^*$ is the answer for our problem, because all its vertex degrees are at least $k$ and $k$ is the maximal possible (according to $H^*$ construction). However, we still have one more condition to be true~--- the resulting graph should have the minimal possible size, now it's obviously not true. This is what we're going to fix in this and the next phases. In this phase we are going to remove noise from $H^*$, i.e. to select its subgraph which contains most of the query vertices (except the noise).

Let's remind that the problem of finding minimal by size \textit{k-core}~--- NP-complete. Which heuristics can we use? The first idea which comes to mind is to delete weakly connected vertices or subgraph from $H^*$, therefore making its size smaller. However, because all vertices degrees are at least $k$, it may be hard to understand which vertices are weakly connected and which are not. Also, it's not obvious how to understand which subgraphs can be deleted without loosing the condition for minimal vertex degree.

We've chosen a reverse approach~--- adding vertices. We will build the final subgraph $H_{min}$ in the following way: let's take all query vertices $Q$ and delete all other vertices and edges. After that we will add other vertices one by one with some priority, making our subgraph $H_{min}$ larger and more connected. At each moment, when the current subgraph satisfies the answer for our problem (i.e. the component of the current subgraph $H_{min}$ with the maximal number of query vertices in it, contains <<the most of the query vertices>> and it is also <<dense enough>>), we will update the final answer. It's obvious that the process is finite~--- the number of vertices in the current subgraph is growing and we won't obtain the subgraph larger than $H^*$. However we face three global questions:

\begin{enumerate}
  \item What is the priority for adding vertices to the current subgraph?
  \item When do we need to update the answer (i.e. what is <<the most vertices from $Q$>>, and what is <<component is dense enough>>)?
  \item When do we need to stop adding vertices?
\end{enumerate}

\textbf{a. What is the priority for adding new vertex?}

There are tons of priority options. However let's think, what metrics are important for us. Firstly, we want vertices that form a community (i.e. the set of query vertices without the noise) to be united into one connected component as soon as possible and began to increase this component density. So, as the first priority we will take the number of components which the newly added vertex unite. I.e., $p_1(v) = |A'| - |A|$, where $A$ is the set of components before adding $v$ and $A'$ is the set after adding it. However after adding the new vertex in many cases the number of components won't change. What do we need to do in this case? Because vertex degrees in the final subgraph are also important for us, let's emphasize attention on it. When adding vertex $v$, degrees of its neighbors which are already contained in the current subgraph, are increased by $1$. We want to maximize this count. However, we also need to take into account the recently added vertex and its degree. So, let's make the following second priority: $p_2(v) = |N_{H_{min}}(v) \cap \{v \in V(H_{min}) | deg(v) < \mu(H^*)\}| - \max(0, \mu(H^*) - |N_{H_{min}}(v)|)$, where $N_{H_{min}}(v)$ is the set of vertices from $H_{min}$ which are connected with $v$ by edge. In other words, we take the number of neighbors of vertex $v$ from $H_{min}$ for which degree is less than needed $\mu(H^*)$ for now with plus sign and the number of edges which is needed for the recently added vertex to have degree $\mu(H^*)$ with minus sign.

\textbf{b. What is <<the most of the vertices from Q>>, what does <<the component is dense enough>> mean?}

We will say that subgraph $H \subset G$ contains for most of the vertices from $Q$ if the number of query vertices in it is at least $\alpha(|Q|) \cdot |Q|$. $\alpha(|Q|) \in (0, 1]$ or just $\alpha$ is some coefficient which depends on the number of query vertices. But what $\alpha$ should be equal to? From one side we want <<the most of the vertices>> to be really the most part of them, so we will assume that there is less than $\frac{|Q|}{2}$ noise vertices in the query (i.e. $\alpha \ge 0.5$). From the other hand, even $\frac{|Q|}{2}$ noise vertices is too much in most cases.

We are also interested in what <<the component is dense enough>> means. We will say that the component is dense enough if it contains enough edges (it obviously means that its dense is quite high). To select the bound of the edges count we tried several functions, and the most optimal one turned out to be the following: $|E(H_{min})| \ge (V(H_{min}) - |Q|) \cdot \mu(H^*) + \beta(|Q|) \cdot |Q|)$, i.e. for all query vertices we take their degree equal to $\beta$ (one more parameter) and for other ones degree should be at least $\mu(H^*)$ (actually if there is some noise in the query, degree should be even higher, however, if there is no noise, the previous statement is true).

To choose optimal values for parameters $\alpha$ and $\beta$ given the fixed $|Q|$ we decided to make some experiments (they we made on the same data as all other experiments described in chapter $3$). Results of the experiments are shown in the table below ($k$ is the order of \textit{k-core}, i.e. $k = \mu(H^*)$).

\begin{table}[!h]
\centering
\caption{Optimal values for $\alpha$ and $\beta$ for different $|Q|$}\label{parameters-research}
  \begin{tabular}{| l | l | p{1cm} |}
  \hline
  $|Q|$ & $\alpha$ & $\beta$ \\\hline
  2   & 1      & 1        \\\hline
  3   & 2 / 3  & 1        \\\hline
  4   & 1 / 2  & 1        \\\hline
  5   & 3 / 5  & 2        \\\hline
  6   & 2 / 3  & 2        \\\hline
  7   & 4 / 7  & 3        \\\hline
  8   & 3 / 4  & 3        \\\hline
  > 8 & 7 / 10 & 4        \\\hline
  \end{tabular}
\end{table}
\FloatBarrier

\textbf{c. When the algorithm should be stopped?}

It's simple to understand that the algorithm can be stopped when all vertices from $H^*$ are added. However, because the size of $H^*$ may be quite big, it's not the optimal solution~--- when our current subgraph became connected, it will contain all query vertices and thus all the noise, while our task is to avoid noise in the result subgraph. So let's stop our algorithm when our current subgraph became connected, we won't loose any answers in that case.

\begin{figure}[!h]
\caption{Example of phase $2$ work}\label{phase2-example}
\centering
  \begin{center}
    \makebox[\textwidth]{\includegraphics[scale=1.2]{pictures/phase2.png}}
  \end{center}
\end{figure}

Let's assume that $H^*$ looks like at the picture~\ref{phase2-example}. Query vertices are marked red, we're taking them at the first step. After that we will add vertices one by one, as it is shown on the picture: firstly we add vertex $1$, because it unites two components, then $2$ because it has the maximal second priority, etc. Our algorithm finishes when $H_{min}$ becomes connected, in that case it will happen when all edges will be added. However, the optimal subgraph will be the one without the dotted edges, because it will be more dense. And as we can see the vertex that was not added is an obvious noise, so we shouldn't add it to the result subgraph.

\subsection{Phase 3. Restoring the condition for vertex degrees}

After phase $2$ is completed, we have a subgraph $H_{min}^*$. This subgraph can be returned as the answer, but of course as we just saw on the picture~\ref{phase2-example}, $H_{min}^*$ can be far away from the optimal answer. Why? We were adding vertices one by one with, as it seemed, optimal priority. Actually, because we focus on adding vertices that unite our current components the best and take vertex degrees into account only as the second priority, the obtained subgraph can be non-optimal. That's why we need to do something with $H_{min}^*$ to satisfy the condition for vertex degrees.

You may now think that phase $2$ was redandunt, because we had the \textit{k-core} which was densely connected and we obtained the subgraph which is not \textit{k-core} and thus is not densely connected. That's not true, because the initial \textit{k-core} contained all query vertices and new subgraph doesn't contain query noise. Also, as you will see, we will apply some heuristics and new subgraph $H_{min}^*$ will become a dense \textit{k-core} of bigger order.

To begin with, let's remove weakly connected vertices. Because our new subgraph $H_{min}^*$ is not a \textit{k-core}, we can suggest logical conditions for that. Let's remember parameter $\beta$ which we introduced to be responsible for the minimal query vertex degrees. Let's remove all query vertices that have degree less than $\beta$, i.e. which doesn't follow our invariant. After deletion, let's run phase $1$ one more time on the left query vertices. Actually, we don't need to run the whole phase $1$, we only need to find the new value $k^{opt}$~--- the maximal order of \textit{k-core} in which all remaining query vertices lie in the same component.

Now we will do the main step of the final phase. Its idea is in the following: we will take all remaining query vertices and find the minimal number of non-query vertices that connect them. In other words, taking the remaining subset of query vertices $Q' \subseteq Q$ we want to find such set of vertices $V_{opt} \subseteq V(H_{min}^*) \setminus Q'$ of minimal size so that $G[V_{opt} \cup Q']$ is connected (to remind, $G[V]$ is a subgraph originated by the set of vertices $V$). It will give us a <<skeleton>> of subgraph $H_{min}^*$ which we will build up almost the same way as we were doing in phase $2$.

The problem that we have described above is a \textit{Steiner task}. The original version of Steiner task sounds as follows: given a weighted graph $G$ and a subset of selected vertices $Q$ in it, you need to find a minimal spanning tree on the selected vertices. It's quite interesting that for $|Q| = 2$ the task is just to find the minimal path between two vertices, if $|Q| = |V(G)|$, the task is just to find the minimal spanning tree, and in all other cases the task is NP-hard. You can mention that in our case graph is unweighted, but actually Steiner task remains NP-hard even on unweighted graphs. Steiner task have been investigated many years ago, and Kou et al. \cite{Kou81} gave us an optimal algoriths for solving it with approximation $2 - \frac{2}{|Q|}$ and proof that it's impossible to solve it better in polynomial time. We will take this solution and apply it to our task.

After finding the needed <<skeleton>>, we will apply slighly modified phase $2$ to it. First, skeleton is already connected and we don't need priority for uniting the components. Second, we want to guarantee the density of the final subgraph, so we need to modify condition a little bit:

\begin{enumerate}
  \item We will add new vertices only by second priority;
  \item We will stop only when the minimal vertex degree is at least $k^{opt}$, because we know that it's possible to build \textit{$k^{opt}$-core} on the remaning query vertices;
  \item We won't update the answer during these iterations, we will take the final subgraph as the answer.
\end{enumerate}

After changing the phase $2$ as described above, we apply it on $H_{min}^*$ with deleted weakly connected query vertices and get the final answer to the task.

\begin{figure}[!h]
\caption{Phase $3$ example}\label{phase3-example}
\centering
  \begin{center}
    \makebox[\textwidth]{\includegraphics[scale=1.0]{pictures/phase3.png}}
  \end{center}
\end{figure}

The work of this phase is presented on picture~\ref{phase3-example} where subgraph $H_{min}^*$ (3-core) is places on the left and the final subgraph obtained after phase $3$ is placed on the right. First, we remove weakly connected query vertex and then found two yellow vertices which along with remaining query vertices give as a skeleton of $5$ vertices. After that we apply modified phase $2$ on the skeleton and obtain the final subgraph. As you can see, it is smaller and more dense than the first one~--- it is even 4-core when the previous was only 3-core.


\title{Developing Inference Algorithms}

{{navbar}}

\subsubsection{Developing Inference Algorithms}

Edward uses class inheritance to provide a hierarchy of inference
methods. This enables fast experimentation on top of existing
algorithms, whether it be developing new black box algorithms or
new model-specific algorithms.
For examples of algorithms developed in Edward, see the inference
\href{/tutorials/}{tutorials}.

\includegraphics[width=700px]{/images/inference_structure.png}
{\small\textit{Dependency graph of several inference methods.
Nodes are classes in Edward and arrows represent class inheritance.}}

There is a base class \texttt{Inference}, from which all inference
methods are derived from. Note that \texttt{Inference} says nothing
about the class of models that an algorithm must work with. One can
build inference algorithms which are tailored to a restricted class of
models available in Edward (such as differentiable models or
conditionally conjugate models), or even tailor it to a single model.
The algorithm can raise an error if the model is outside this class.

We organize inference under two paradigms:
\texttt{VariationalInference} and \texttt{MonteCarlo} (or more plainly,
optimization and sampling). These inherit from \texttt{Inference} and each
have their own default methods.

For example, developing a new variational inference algorithm is as simple as
inheriting from \texttt{VariationalInference} and writing a
\texttt{build\_loss\_and\_gradients()} method. \texttt{VariationalInference} implements many default methods such
as \texttt{initialize()} with options for an optimizer.
For example, see the
\href{https://github.com/blei-lab/edward/blob/master/examples/iwvi.py}{importance
weighted variational inference} script.


\documentclass[11pt]{article}
%============Macros==================%
% you don't need to change anything. start editing from main body.
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{qcircuit}
\usepackage[margin=1in]{geometry}
%--------------Cosmetic----------------%
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{fullpage}
\usepackage{microtype}
\usepackage{xspace}
\usepackage[svgnames]{xcolor}
\usepackage[sc]{mathpazo}
\usepackage{enumitem}
\setlist[enumerate]{itemsep=1pt,topsep=2pt}
\setlist[itemize]{itemsep=1pt,topsep=2pt}
%--------------Header------------------%
\def\course{CS 410/510 Introduction to Quantum Computing}
\def\term{Portland State U, Spring 2017}
\def\prof{Lecturer: Fang Song}
\newcommand{\handout}[5]{
   \renewcommand{\thepage}{\arabic{page}}
   \begin{center}
   \framebox{
      \vbox{
    \hbox to 5.78in { \hfill \large{\course} \hfill }
       \vspace{2mm}
       \hbox to 6in { {\Large \hfill #5  \hfill} }
       \vspace{2mm}
       \hbox to 6in { \term \hfill \emph{#2}}
       \hbox to 6in { {#3 \hfill \emph{#4}}}
      }
   }
   \end{center}
   \vspace*{4mm}
}
\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{#4}{{Lecture #1}}}

%=============Main Doc=================%
\begin{document}
%-----Specs: change accordingly--------%
\def\lecdate{April 4, 2017} % put lecture date here
\def\scribe{Scribe: your name} % put your name here
\def\lecnum{1} % change the lecture number

\lecture{\lecnum}{\lecdate}{\prof}{\scribe}%

%==============================%
\section*{Draw quantum circuits using the \texttt{qcircuit} package}
%==============================%

Instructions on github (\url{https://github.com/CQuIC/qcircuit}). A
simple example here:
\[
\Qcircuit @C=1em @R=1em {
& \ctrl{2} & \qw & \gate{H} & \ctrl{1} &
\gate{H} & \qw \\
& \qw & \ctrl{1} & \gate{H} & \targ &
\gate{H} & \qw \\
& \targ & \targ & \gate{Z} & \qw & \ctrl{-1} &
\qw
}
\]

%==============================%
\section{Syllabus}
%==============================%

%==============================%
\section{A probabilistic model}
%==============================%

%==============================%
\section{Quantum theory: probabilistic model extended}
%==============================%

Postulates of quantum mechanics

\begin{enumerate}
\item Quantum superposition principle. Qubit and extension to
  composite systems (multiple qubits).
\item Measurement.
\item Unitary operations.
\end{enumerate}

%==============================%
\section{Dirac notation}
%==============================%

\end{document}


\section{Trying around}

\subsection{VI/BVI}

Memory: 2 numbers for every state

\begin{itemize}
	\item Initialize.

	L = 1 if target else 0

	U = 0 if can't reach else 1
	\item Repeat until U-L$<\varepsilon$ for initial state
	\begin{itemize}
		\item Bellman update for U and L on whole state space.
	\end{itemize}
\end{itemize}

\subsection{BRTDP}

Memory: 2 numbers for every seen state, potentially for every state.

\nextstate: Det, Unif, Prob, MaxDiff.

\stuck: PathLen (explored states, all states, iters, log, exp...), seenTwice, FSMC.
May have false positives, but no false negatives.

\begin{itemize}
	\item Repeat until $U-L<\varepsilon$ for initial state
	\begin{itemize}
		\item Repeat until last state in path is target or \stuck.
		\begin{itemize}
		\item Simulate: Repeatedly pick successor according to \nextstate. Save path (list of states).
		\end{itemize}
	\item Bellman update for U and L on states in path.

	New states always have L=0 and U=1, except target states have L=1 and we need some way to detect U=0. Using FSMC, we can set BSCC to 0. We can also check if we are in a BSCC when STUCK.

	\item Check BSCC without building full partial model (look for outgoing edges between the looping states)
	\end{itemize}
\end{itemize}

\textcolor{red}{Compare to other STUCK heuristics (FSMC p2, para 2). Compare to importance sampling/splitting - see Modest papers.}

\subsection{SMC}

Memory: Number for initial state, path to detect candidate.

\nextstate: Prob.

\stuck: FSMC.
Needs to be exact

\begin{itemize}
	\item Repeat enough times (enough depends on n and p and stuff)
	\begin{itemize}
		\item Repeat until last state in path is target or \stuck.
		\begin{itemize}
			\item Simulate: Repeatedly pick successor according to \nextstate. Save path (list of states).
		\end{itemize}
		\item Remember 0/1 depending on target or \stuck.
	\end{itemize}
\end{itemize}

Result is sum of remembered 0/1 divided by how often we simulated.

\subsection{Precise SMC}

Use other SMC to guide BRTDP? We already have something better than 1.3 by using MaxDiff.

\subsection{Experiments}

MODEST/PRISM-SMC/PRISM (Standard)/Our BRTDP/PLASMA LAB/STORM