\section{Expectation Maximization in the Loop}

% Using a unimodal prior tends to result in predictions which look relatively similar in shape. To promote diversity among predicted 3D dog shapes, our method extends the formulation above to incorporate a mixture of Gaussians prior. We represent the mixture as a set of $M$ Gaussians, whose means are initialized by drawing samples from our existing prior:

% \begin{align}
%     \mu_{\shape}^{m} &\sim N(\mu_{\shape}, \Sigma_{\shape}) \\
%     \Sigma_{\shape}^{m} &:= \Sigma_{\shape}
% \end{align}

% We assign each training image $i$ with a set of mixture weights $\{w_{i}^{1}, \dots w_{i}^{M}\}$, where initially $w_{i}^{m} := \frac{1}{M}.$

% We can then apply the following mixture shape loss:

% \begin{equation}
%     L_{mixture}=\sum_{m=1}^M w_{i}^{m}L_{shape}(\shape_{i}, \mu_{\shape}^{m}, \Sigma_{\shape}^{m})
% \end{equation}

% In order to allow our mixture prior to learn ``in-the-loop" from the available training data, we apply expectation maximization every $k$ epochs during training. This step recomputes the means and variances for each mixture component based on the observed shapes in the training set, and updates the per-image mixture weights:

% \begin{align}
%     \mu_{\shape}^{m} :=& \mathrm{E}_{i}[\beta_{i}W_{i}^{m}]\\
%     \Sigma_{\shape}^{m} :=& \mathrm{Cov}_{i}[\beta_{i}W_{i}^{m}, \beta_{i}W_{i}^{m}]\\
%     w_{i}^{m} :=& \frac{L_{shape}(\shape_{i}, \mu_{\shape}^{m}, \Sigma_{\shape}^{m})}{\sum_{m'}^{M}L_{shape}(\shape_{i}, \mu_{\shape}^{m'}, \Sigma_{\shape}^{m'})}
% \end{align}

% \section{Attempt 2}

As previously discussed, the parameters for the mixture shape-scale prior are initialized using artist data which results in a prior that poorly represents the diverse shapes present in the real dog dataset. A key contribution presented in this chapter is the introduction of a procedure based on Expectation Maximization which gradually improves the representational power of the shape-scale prior by learning from monouclar images of in-the-wild dogs and their respective 2D training labels.

% \subsection{Assigning image weights}

\def\imgweight#1#2{w_{#1}^{#2}}

Firstly, each training image is assigned a set of latent variables $\{\imgweight{i}{1}, \dots, \imgweight{i}{M}\}$ which encode the likelihood that the dog in image~$i$ was generated according to each shape-scale component~$m \in \{1,\dots,M\}$.

Expectation Maximization (EM) is then used to regularly update these image weights, as well as the parameters for the mixture $(\shapescalemu^{m},\shapescalecov^{m},\shapescalepi^{m})_{i=1}^{M}$. During the training loop of the 3D reconstruction network, these parameters are tuned according to alternating expectation (`E-') and maximization (`M-') steps which are described below:

% TODO - add me!
% Each training image $i$ is assigned a set of latent variables encoding the probability of the dog shape in image~$i$ being generated by component~$m$.

% This is addressed by proposing to recover the latent variables $w_{i}^{m}$ and parameters $\shapescalemu^{m}$, $\shapescalecov^{m}$ and $\shapescalepi^{m}$ ($\mu_{\shape}^{m}$, $\Sigma_{\shape}^{m}$ and $\Pi_{\shape}^{m}$) of our 3D shape prior by learning from monocular images of in-the-wild dogs and their 2D training labels in our training dataset.

% We achieve this using Expectation Maximization (EM), which regularly updates the means and variances for each mixture component and per-image mixture weights based on the observed shapes in the training set. While training our 3D reconstruction network, we progressively update our shape mixture model with an alternating `E' step and `M' step described below:

\subsection{Expectation Maximization update steps}

\subsubsection{The `E' Step.}
The `E' step computes the expected value of the image weights~$w_{i}^{m}$
assuming fixed $(\shapescalemu^{m},\shapescalecov^{m},\shapescalepi^{m})$ for all $i \in \{1,\dots,N\}, m \in \{1,\dots,M\}$.

The update equation for an image $i$ with latest scale-shape prediction $\shapescale_{i}$ and cluster $m$ with parameters $(\shapescalemu^{m},\shapescalecov^{m},\shapescalepi^{m})$
is given as:

\begin{align}
    \imgweight{i}{m}
    :=&
    \frac{
        \mathcal{N}(\shapescale_{i} | \shapescalemu^{m},\shapescalecov^{m})\shapescalepi^{m}
    }
    {
        \sum_{m'=1}^{M}
        \mathcal{N}(\shapescale_{i} | \shapescalemu^{m'},\shapescalecov^{m'})\shapescalepi^{m'}
    }
\end{align}

To improve numerical stability, this is formulated using the log-sum-exp trick:

\begin{align}
    \log{\imgweight{i}{m}}
    :=&
    \frac{
        \log{
            \left[
            \shapescalepi^{m} (2\Pi)^{\frac{d}{2}}
            \left[\det{\shapescalecov^{m}}\right]^{-\frac{1}{2}}
            \exp{
                -\frac{1}{2}
                (\shapescale_{i} - \shapescalemu^{m})^{T}
                (\shapescalecov^{m})^{-1}
                (\shapescale_{i} - \shapescalemu^{m})
            }
        \right]
        }
    }
    {
        \log{
            \left[
            \sum_{m'=1}^{M}
            \shapescalepi^{m'} (2\Pi)^{\frac{d}{2}}
            \left[\det{\shapescalecov^{m'}}\right]^{-\frac{1}{2}}
            \exp{
                -\frac{1}{2}
                (\shapescale_{i} - \shapescalemu^{m'})^{T}
                (\shapescalecov^{m'})^{-1}
                (\shapescale_{i} - \shapescalemu^{m'})
            }
        \right]
        }
    }
\end{align}
And this simplifies to
\begin{multline}
    \log{\imgweight{i}{m}}
    :=
    \log{\shapescalepi^{m}} - \frac{1}{2}
        \left[
            d\log{2\pi} + \log{\det{\left(\shapescalecov^{m}\right)}} + M_{i,m}
        \right] \\ -
    \log{
        \sum_{m'=1}^{M}
        \exp{
            \log{\shapescalepi^{m'}} - \frac{1}{2}
            \left[
                d\log{2\pi} + \log{\det{\left(\shapescalecov^{m'}\right)}} + M_{i,m'}
            \right]
        }
    }
\end{multline}
where
\begin{align}
M_{i,m} :=&
    (\shapescale_{i} - \shapescalemu^{m})^{T}
    (\shapescalecov^{m})^{-1}
    (\shapescale_{i} - \shapescalemu^{m})
\end{align}

\subsubsection{The `M' Step.}
The `M' step computes new values for $(\shapescalemu^{m},\shapescalecov^{m},\shapescalepi^{m})$, assuming fixed $\imgweight{i}{m}$ for all $i \in \{1,\dots,N\}$ and $m \in \{1,\dots,M\}$. The update equations are given as follows:

\begin{equation}
    \shapescalemu^{m} :=
    \frac{
        \sum_{i} \imgweight{i}{m}\shapescale_{i}
    }
    {
        \sum_{i} \imgweight{i}{m}
    }
    \quad
    \shapescalecov^{m} :=
    \frac{
        \sum_{i}
        \imgweight{i}{m}
        (\shapescale_{i} - \shapescalecov^{m})
        (\shapescale_{i} - \shapescalecov^{m})^{T}
    }
    {
        \sum_{i}\imgweight{i}{m}
    }
    \quad
    \shapescalepi^{m} :=
    \frac{1}{N}\sum_{i} \imgweight{i}{m}
\end{equation}


% !TEX root = ../main.tex
\section{Hierarchical Variational Models}
For studying models with correlated random variables, such as frustrated spin systems~\citep{zdeborova2016statistical}, unstructured variational families such as the mean field are insufficient. \Acrfullpl{hvm} are one way to model correlated latent variables. An \gls{hvm} is defined by placing a `variational prior' on the variational parameters $\mbnu$ of the mean field variational family, in analogy to hierarchical probabilistic models. By leveraging neural networks to parameterize the variational prior, \glspl{hvm} can capture complex dependencies between random variables~\citep{ranganath2018black}.

\input{ch-hvm/fig/fig_markov_blanket_ising}

For studying a model $p(\mbx, \mbz)$, the variational family defined by an \gls{hvm} is defined as
\begin{equation}
  q_\hvm(\mbz; \mbtheta) = \int q(\mbnu; \mbtheta) \prod_i q(\mbz_i \mid \mbnu_i) d\mbnu \, ,
  \label{eq:hvm}
\end{equation}
where $q_\mf(\mbz \mid \mbnu) = \prod_i q(\mbz_i \mid \mbnu_i)$ is the mean field `variational likelihood' with parameters $\mbnu$, and $q(\mbnu; \mbtheta)$ is the variational prior with parameters $\mbtheta$. \Cref{fig:graphical-model} shows the graphical model for \glspl{hvm} as compared to the mean field family graphical model.

To use an \gls{hvm} in \gls{vi}, the variational lower bound must be optimized. But the variational lower bound in \Cref{eq:llbo} requires calculating the entropy of the variational distribution, and such integration in high dimensions can be intractable. As detailed in \citet{ranganath2018black}, the entropy can be lower-bounded by introducing an auxiliary `variational posterior' distribution $r(\mbnu \mid \mbz; \mbphi)$ with parameters $\mbphi$. This leads to the hierarchical evidence lower bound,
\begin{equation}
  \widetilde{{\cL}}(\mbtheta, \mbphi) = \E_{q(\mbz, \mbnu; \mbtheta)}[ \log p(\mbx, \mbz) + \log r(\mbnu \mid \mbz; \mbphi) - \log q(\mbz \mid \mbnu) - \log q(\mbnu; \mbtheta)]\, ,
  \label{eq:hier-elbo}
\end{equation}
and a stochastic optimization algorithm for this objective is developed in \citep{ranganath2018black}. \gls{vi} with an \gls{hvm} requires specifying the variational prior $q(\mbnu; \mbtheta)$ and the variational posterior ${r(\mbnu \mid \mbz; \mbphi)}$, then optimizing the hierarchical \gls{elbo} in \Cref{eq:hier-elbo}.

\paragraph{Specifying an \gls{hvm} with Normalizing Flows.} We study several choices of variational prior and recursive variational posterior. One choice of variational prior $q(\mbnu; \mbtheta)$ is an inverse autoregressive flow~\citep{kingma2016improved}. If the variational posterior $r(\mbnu \mid \mbz; \mbphi)$ is chosen to be a masked autoregressive flow~\citep{papamakarios2017masked}, the analytical forms of these flows are equivalent. These choices lead to a complexity of $\cO(L)$ for sampling latent variables in a system of size $L$. (This is because the noise used to sample from the variational prior can be drawn in parallel.) \glspl{hvm} should therefore be faster than \gls{van} approximations in large systems: the autoregressive requirement in \glspl{van} leads to a complexity of $\cO(L^2)$. A research question is whether the advantage in speed of \glspl{hvm} leads to a drop in accuracy that is too large to answer a statistical physics question.
\input{ch-hvm/fig/ising}

\paragraph{Scalable \glspl{hvm} using Ising Model Structure.} Although \glspl{hvm} with autoregressive flows scale linearly, such variational approximations do not leverage the structure about the statistical physics under study. For example, it is difficult to index random variables so that nearest neighbors are grouped together when fed to an autoregressive model. However, consider the Markov blanket of a random variable in the Ising model---it contains all the information needed to render a variable conditionally independent of the rest of the model. The Markov blanket of a node in an Ising model consists of a node's nearest neighbors and is shown in \Cref{fig:markov-blanket-ising}. This means that an autoregressive model is overparameterized. For example, the last variable to be fed to the model depends on all the previous in an autoregressive model, whereas an efficient model might only consider nodes in a Markov blanket. A convenient way to build this structure into an \gls{hvm} would ensure efficient use of information from nearest neighbors in the Ising model. One way to formalize this problem structure is with convolutional neural networks~\citep{lecun2015deep}. We parameterize the variational prior and recursive posterior with \gls{realnvp} transformations using a convolutional neural network architecture \citep{dinh2017density}. Specifically, we parameterize the convolutional kernels to mimic the Markov blanket shown in \Cref{fig:markov-blanket-ising}: for every node, only its nearest neighbors are conditioned on.


\documentclass[twoside]{MATH77}
\usepackage{multicol}
\usepackage[fleqn,reqno,centertags]{amsmath}
\begin{document}
\hyphenation{JTPREV}
\begmath 4.5 Sequential Solution of a Banded Least-Squares Problem

\silentfootnote{$^\copyright$1997 Calif. Inst. of Technology, \thisyear \ Math \`a la Carte, Inc.}

\subsection{Purpose}

Let a linear least-squares problem be denoted by
\begin{equation*}
A{\bf x}\simeq {\bf b}
\end{equation*}
where $A$ is a given $m\times n$ matrix with $m\geq n$, ${\bf b}$ is a given
$m$-vector, and it is required to find an $n$-vector, ${\bf x}$, that is an
approximate solution to this equation in the least-squares sense. The given
data for this problem can be regarded as the composite matrix, $[A:{\bf b}].$

Assume further that the matrix $A$ is {\em banded}, in the sense that there
is an integer, NB $\leq n$, such that in any row of $A$ all nonzero elements
occur within a set of NB contiguous positions. This set of subroutines is
intended for the case in which NB is significantly smaller than $n$ so there
is the possibility of achieving some significant saving of storage and
execution time by taking advantage of this band property.

Subroutine SBACC can be used to preprocess the data matrix, $[A:{\bf b}]$,
sequentially. Subroutine SBSOL can be used to solve the problem after the
transformation accomplished by SBACC. Subroutine SBSOL can also be used to
compute a covariance matrix for the solution vector.

\subsection{Usage}

\subsubsection{Sequential processing of data}

The user must make a sequence of calls to SBACC, sending a block of rows of $%
[A:{\bf b}]$ with each call. On the first call the user sets NB, the
bandwidth, which will remain unchanged after that. Also, on the first call
the user must set IR = 1, and JT = 1. After that IR will be updated by SBACC.

Along with each block the user sets two integers, MT and JT, indicating that
MT new rows are being provided, within these rows of $A$ only the NB columns
beginning with column JT are being provided, and that the elements in all
other columns in these rows of $A$ are zero. The user puts this block of
data in the array G(,), beginning with row IR of G(,). The data
corresponding to column JT of $A$ goes into the first column of G(,). Data
from the vector ${\bf b}$ of the problem goes into column NB + 1 of G(,).

The data blocks must be ordered so that from one call to the next, the value
of the column index JT either remains the same or increases.

\paragraph{Program Prototype, Single Precision}

\begin{description}
\item[INTEGER]  \ {\bf LDG, NB, IR, MT, JT, JTPREV, IERR2}

\item[REAL]  \ {\bf G}(LDG, $\geq $NB+1)
\end{description}

When starting a new problem set LDG, NB, and IR. On the initial call and all
subsequent calls for the same problem, set MT and JT and store MT rows of
data into G(,) beginning at row IR.

\begin{center}
\fbox{\begin{tabular}{@{\bf }c}
CALL SBACC(G, LDG, NB, IR, MT,\\
JT, JTPREV, IERR2)\\
\end{tabular}}
\end{center}

Following the call the contents of G(,) will have been altered, reflecting
the processing of the new data. The values of JTPREV and IR may have been
changed. IERR2 will be set.

\paragraph{Argument Definitions}

\begin{description}
\item[G(,)] \ [inout] The working array.  With MT, JT, and IR defined below,
  on each call to SBACC the user places columns JT to JT + NB $-$ 1 of MT rows
  of $A$ into G(IR:IR+MT$-$1, 1:NB) and the corresponding elements of
  $\mathbf{b}$ into G(IR:IR+MT$-$1, NB+1).  It is implied that within these MT
  rows of $A$ all elements not in columns JT through JT + NB $-$ 1 are zero.

\item[LDG]  \ [in] The leading dimensioning parameter for G(,). LDG must be
large enough so that on each call to SBACC one has $\text{IR}+\text{MT}-1\leq $
LDG.

MT can be set by the user on each call. Let $MT_{\max }$ denote the largest
value the user will assign to MT. If the user keeps JT $\leq n-\text{NB} +
1$ (see discussion in Section C) then the largest value of IR will be $n+2$,
so it would suffice to set LDG $=n+1+MT_{\max }.$

If the user permits JT to be as large as $n$, then LDG must be at least $n$
+ NB + $MT_{\max }.$

\item[NB]  \ [in] Set by user on the initial call for a problem. Must not be
changed during the processing for one problem. NB indicates the bandwidth of
the data matrix $A$. In any row of $A$ all nonzero elements must appear
in some set of NB consecutive columns.

\item[IR]  \ [inout] Index of first row of G(,) into which the user is to
place new data. The variable IR must be set by the user to the value~1 on
the initial call to SBACC and must not be altered after that by the user on
successive calls for the same problem. IR will be updated in SBACC by
(effectively) setting IR $=$ JT + $\min ($NB + 1, MT + $\max $(IR $-$ JT,~0)).

Let $IR_{in}$ denote the value of IR on entry to SBACC and $IR_{out}$ denote
its value on return. These quantities will satisfy%
\begin{equation*}
IR_{in}\leq IR_{out}\leq \text{JT + NB + 1}.
\end{equation*}
\item[MT]  \ [in] Set by user to indicate the number of new rows of data
being introduced by the current call to SBACC. SBACC will return immediately
if MT $\leq $ 0. MT will not be altered by SBACC.

\item[JT]  \ [in] Set by user to indicate the column of the new sub-block of
$A$ that the user is storing in the first column of the work array, G(,).
The user must set JT = 1 on the initial call to SBACC and must either leave
JT the same or increase it on each successive call for the same problem. Too
large an increase between successive calls can cause the problem to be
structurally singular. See Section E for more information on this. JT will not
be altered by SBACC.

\item[JTPREV]  \ [inout] Need not be set before the first call to
SBACC for a problem. Must not be altered by the user on later calls
for the same problem. JTPREV is used within SBACC to mark the row of
G(,) at which the method of packing data changes. Quantities in G(,)
at and below row JTPREV are subject to potential change during the
processing of additional data, whereas quantities above row JTPREV
are not.

On return, SBACC sets JTPREV = JT.  JTPREV, as set by SBACC, is needed
as input when calling SBSOL.

\item[IERR2]  \ [inout] Error status indicator. Need not be set before the
initial call for a problem. Will be set by SBACC to zero if no errors are
detected and to nonzero values, as described in Section E, if error
conditions are detected.
\end{description}

\paragraph{Modifications for Double Precision}

For double precision usage change the REAL statement to DOUBLE PRECISION
and change the subroutine name SBACC to DBACC.

\subsubsection{Computation of Solution Vector}

\paragraph{Program Prototype, Single Precision}

\begin{description}
\item[INTEGER]  \ {\bf MODE, LDG, NB, IR, JTPREV, N, IERR3}

\item[REAL]  \ {\bf G}(LDG, $\geq $NB+1){\bf , X}($\geq N)${\bf , RNORM}
\end{description}

Set MODE and N. If MODE is set to~2~or~3 then also store a vector, ${\bf p}$%
, into X(). G(,), NB, IR, and JTPREV should have values resulting from
previous calls to SBACC.

\begin{center}
\fbox{\begin{tabular}{@{\bf }c}
CALL SBSOL(MODE, G, LDG, NB, IR, \\
JTPREV, X, N, RNORM, IERR3)\\
\end{tabular}}
\end{center}

On return, X() and RNORM will contain computed results and IERR3 will be
set. No other quantities in the argument list will be modified.

\paragraph{Argument Definitions}

\begin{description}
\item[MODE]  \ [in] The previous processing of data by SBACC will have left
a representation of an upper triangular matrix, $R$, and a vector, ${\bf y}$%
, in the array, G(,). See Section~D for the interpretation of these quantities.
The user selects the desired solution process by setting MODE to~1,~2, or~3.

\begin{itemize}
\item[=1]  Solve $R{\bf x}={\bf y}$, where $R$ and ${\bf y}$ are
contained in G(,) as the result of previous calls to SBACC. The
solution vector, ${\bf x}$, will be stored in X(). This gives the solution
to the least-squares problem, $A{\bf x}\simeq {\bf b}.$

\item[=2]  Solve $R^t{\bf x}={\bf p}$, where $R$ is the matrix residing in
G(,) as the result of previous calls to SBACC, and ${\bf p}$ is a vector
placed in X() by the user. The solution vector, ${\bf x}$, will replace $%
{\bf p}$ in X().

\item[=3]  Solve $R{\bf x}={\bf p}$, where $R$ is the matrix residing in G(,)
as the result of previous calls to SBACC, and ${\bf p}$ is a vector placed
in X() by the user. The solution vector, ${\bf x}$, will replace ${\bf p}$
in X().
\end{itemize}

\item[G(,),LDG,NB,IR,JTPREV]  \ [in] These arguments must contain values as
they were defined upon the return from a preceding call to SBACC.

\item[X()]  \ [inout] On input, with MODE = 2 or 3, this array must contain
the N-dimensional right-side vector of the system to be solved. On return,
with MODE = 1, 2, or 3, this array will contain the N-dimensional solution
vector of the appropriate system that has been solved.

\item[N]  \ [in] Set by user to specify the dimensionality of the desired
solution vector. This causes the subroutine SBSOL to use only the leading N$%
\times $N submatrix of the triangular matrix currently represented in the
array G(,). An error is reported if this submatrix is singular.

\item[RNORM]  \ [out] If MODE = 1, RNORM is set by the subroutine to the
norm of the residual vector for the least-squares problem, $i.e.$, $\Vert
{\bf b}-A{\bf x}\Vert $. This number is computed as%
\begin{equation*}
\left[\, \sum_{I=\text{N}+1}^{\text{IR}-1}\text{G(}I\text{, NB + 1})^2\right]
^{1/2}
\end{equation*}
If MODE = 2 or 3, RNORM is set to zero.

\item[IERR3]  \ [out] Error status indicator set by SBSOL. Zero means no
errors detected. If a diagonal element of the leading N$\times $N submatrix
represented in G(,) is zero, an error message will be issued and IERR3 will
be set to the index of the first zero diagonal element. In this latter case
the solution vector X() will not be computed.
\end{description}

\paragraph{Changes for Double Precision}

For double precision usage change the REAL statement to DOUBLE PRECISION and
change the subroutine name SBSOL to DBSOL.

\subsection{Examples and Remarks}

\subsubsection{Computation of the covariance matrix for the solution vector}

The features provided by MODE = 2 and 3 are primarily intended to support
the computation of the unscaled covariance matrix, $C$, for the least-%
squares problem. This matrix, $C$, is defined by $C = (A^tA)^{-1}$. However,
since $R^tR = A^tA$ (See Section~D), $C$ is also given by $C = (R^tR)^{-1}$,
and thus $C$ satisfies the equation, $R^tRC = I$, where $I$ is the $n\times n
$ identity matrix. It follows that the matrix, $C$, can be computed in two
steps, first solving
\begin{equation*}
R^tZ = I
\end{equation*}
for $Z$, and then solving
\begin{equation*}
RC = Z
\end{equation*}
for $C$. Using SBSOL or DBSOL the matrices $Z$ and $C$ can be computed one
column at a time.

This matrix, $C$, must be multiplied by an estimate of the variance of the
data error to obtain the solution covariance matrix. This variance can be
estimated using N, MTOTAL, and RNORM as follows:

\hspace{.2in}DOF = MTOTAL $-$ N

\hspace{.2in}VAR = RNORM$**$2 / DOF

where MTOTAL is the total number of rows of $A$ introduced into the problem.

\subsubsection{Demonstration problem}

As a demonstration problem we compute the continuous piecewise linear
function that best fits a sample of 91~values of the sine function in the
least-squares sense. Sample values are values of the sine function at one
degree steps from zero to 90~degrees. The piecewise linear function will be
parameterized by a set of ten values, $y_i$, $i = 1$, 10, which will be the
values of the piecewise linear function at the arguments 0, 10, 20, ..., 90
degrees. The fitted function will be defined by linear interpolation between
adjacent pairs of these points.

Note that because the sine curve is concave down throughout this interval we
expect the knots, $y_i$, each to lie above the sine curve, with the linearly
interpolated segment between each adjacent pair of knots passing below the
sine curve.

Program DRDBACC illustrates the use of DBACC and DBSOL to compute the ten
values of $y_i$ for this problem. It also computes the (formal) $10\times 10$
covariance matrix for these quantities. The results are shown in ODDBACC.

Note from the listing of residuals that the residuals at $x = 0$, 10, 20,
... degrees are positive, while the residuals at $x = 5$, 15, 25, ...
degrees are negative, as expected.

This problem is not really statistical since the given data are essentially
exact. Thus the computed SIGFAC and covariance matrix do not really have a
statistical interpretation, but merely serve to illustrate how to use these
subroutines to compute these quantities. Note in particular that the
covariance matrix is symmetric (to some level of accuracy), as it should be,
even though this method computes each column of the covariance matrix
independently.

For an example of more general usage of DBACC see a listing of the MATH77
library subroutine, DC2FIT, Chapter~11.4.

\subsubsection{Data blocks having fewer than NB contiguous columns of nonzeros}

This subroutine requires the value of NB to be constant throughout the
processing of one problem. If the data block being sent to SBACC in one call
has all of its nonzeros in fewer than NB contiguous columns the user must
pad out the block to a width of NB columns by including zeros. The padding
columns may be either on the left or the right or both.

For example, suppose NB = 4 and one is introducing a row, or block of rows,
of $A$ having nonzeros only in columns 5, 6, and 7. One may either set JT =
4 and send columns 4, 5, 6, and 7, with column~4 containing zeros; or else
one may set JT = 5 and send columns 5, 6, 7, and 8, with column~8 containing
zeros.

In choosing between these alternatives there are two points to consider. JT
must be nondecreasing on successive calls to SBACC. Sending padding columns
that are beyond the last actual column of $A$ causes the algorithm to use
more rows of storage than would otherwise be necessary.

\subsection{Functional Description}

Subroutine SBACC uses Householder orthogonal transformations to process the
given data, producing an equivalent least-squares problem of the form%
\begin{equation*}
\left[
\begin{array}{c}
R \\
0
\end{array}
\right] \simeq \left[
\begin{array}{c}
{\bf y} \\ \alpha
\end{array}
\right]
\end{equation*}
where $R$ is an $n\times n$ upper triangular matrix with a bandwidth of NB, $%
{\bf y}$ is an $n$-vector, and $\alpha $ is a scalar quantity. These
quantities are related to the data, $[A:{\bf b}]$, by the relations, $%
R^tR=A^tA$, $R^t{\bf y}=A^t{\bf b}$, and ${\bf y}^t{\bf y}+\alpha ^2={\bf b}%
^t{\bf b}.$

The solution, ${\bf x}$, for this problem is also the solution for the given
least-squares problem, $A{\bf x}\simeq {\bf b}$, and can be computed by
solving the triangular system, $R{\bf x}={\bf y}$. This latter system is
solved for ${\bf x}$ when SBSOL is called with MODE = 1.

The residual vector for the transformed least-squares problem is
\begin{equation*}
\left[
\begin{array}{c}
\bf 0 \\
\alpha
\end{array}
\right].
\end{equation*}
The norm of this residual vector is $|\alpha |$ and this is also equal to $%
\Vert {\bf b}-A{\bf x}\Vert $. After $n$ linearly independent rows of $A$
have been accumulated, where $n$ is the number of columns of $A$, if a
solution is requested with N $=n$, then the value $|\alpha |$ will be
returned as RNORM. If a solution is requested with N $<n$, components of $%
{\bf y}$ beyond $y_N$ will also be used in computing RNORM.

Subroutine SBACC dynamically partitions the array G(,) into three segments
by groups of rows. These segments are rows~1 through JTPREV $-$ 1, rows JTPREV
through IR $-$ 1, and rows IR through LDG.

The first segment holds rows of $[R:{\bf y}]$ for which processing is
completed. In this segment element $r_{i,j}$ is stored in G($i,j-i+1).$

The second segment, consisting of at most NB + 1 rows, holds rows of%
\begin{equation*}
\left[
\begin{array}{ccc}
R & : & {\bf y} \\
0 & : & \alpha
\end{array}
\right]
\end{equation*}
that may be changed by data yet to be received. In this segment element $%
r_{i,j}$ is stored in G($i,j-\text{JTPREV}+1).$

The third segment is used to receive new rows of $[A:{\bf b}].$

The subroutine SBSOL alters only the contents of X() and RNORM. Thus it is
permissible to alternate between accumulating data using SBACC and obtaining
a current solution or covariance matrix using SBSOL. It is also permissible
to use SBSOL with N $< n$. The subroutine SBSOL can compute a solution
vector of length N whenever the portion of the $A$ matrix introduced to that
point has the property that its first N columns are linearly independent.

\bibliography{math77}
\bibliographystyle{math77}

\subsection{Error Procedures and Restrictions}

In response to detected error conditions, SBACC will set IERR2 nonzero and
issue an error message using the error message routines of Chapter~19.2. On
errors numbered 1 through~4, SBACC will return. Generally the calling program
should abandon the problem processing in these cases. When IERR2 = 3 it is
conceivable, but not likely, that the user might wish to continue the
processing, in which case the user must reset IERR2 to zero. If SBACC is
reentered with IERR2 $\neq 0$, (and IR $> 1)$ it will set IERR2 = 5 and
execute a STOP.

{\bf IERR2 \hspace{.2 in} Explanation}\vspace{-10pt}
\begin{itemize}
\item[1]  MT $>$ LDG $-$ IR + 1. To correct this problem either use a
larger dimension LDG, or a smaller block size MT.

\item[2]  JT $<$ JTPREV. This occurs if the data blocks are out of order, in
the sense that the current JT is smaller than JT on the previous call.

\item[3]  JT $>\min ($JTPREV + NB, IR). This occurs when the difference
between the current JT and JT on the previous call is so large that the
matrix would be structurally singular. This condition does not, however,
preclude the processing of the data to triangular form, and thus SBACC will
do the processing, unless there is a storage limitation, in which case IERR2
will be set to~4.

This condition is likely to be due to a program usage error.  If not, then
the problem needs either more data in preceding blocks or a mathematical
model with fewer or differently defined free parameters.  Alternatively,
see pp.~218--219 of \cite{Lawson:1974:SLS} for ideas on stabilizing such a
structurally singular problem.

\item[4]  The condition described above for IERR2 = 3 holds and there is a
storage limitation indicated by MT $>\text{LDG}-\text{JT}+1$. The storage
problem could be relieved by increasing LDG or decreasing MT. However, the
more fundamental problem indicated by IERR2 = 3 must still be dealt with.

\item[5]  SBACC has been entered with IERR2 $\neq 0$. SBACC will execute a
STOP since the calling program has ignored a nonzero setting of IERR2 on the
previous call.
\end{itemize}

The following conditions are not tested in SBACC and violation will have
unpredictable effects: IR, and JT must be set to~1 on the first call to
SBACC. JTPREV and IR must not subsequently be altered by the user during the
processing for one problem.

SBACC will return immediately if MT $\leq $ 0. This is not regarded as an
error condition.

If SBSOL encounters a zero diagonal term in the N$\times $N matrix R, IERR3
will be set to the index of the (first) zero term and an error message will
be issued. The solution X() will not be computed in this case.

\subsection{Supporting Information}

The source language is ANSI Fortran~77.

These subroutines are adaptations to the JPL MATH77 library of the
algorithms and subroutines BNDACC and BNDSOL that were developed by C.  L.
Lawson and R.  J.  Hanson at JPL in~1972 and described in detail in
\cite{Lawson:1974:SLS}.

\begin{tabular}{@{\bf}l@{\hspace{5pt}}l}
\bf Entry & \hspace{.35in} {\bf Required Files}\vspace{2pt} \\
DBACC & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DBACC, DHTCC, DNRM2, ERFIN, ERMSG, IERM1, IERV1\rule[-5pt]{0pt}{8pt}}\\
DBSOL & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
DBSOL, ERFIN, ERMSG, IERM1, IERV1\rule[-5pt]{0pt}{8pt}}\\
SBACC & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
ERFIN, ERMSG, IERM1, IERV1, SBACC, SHTCC, SNRM2\rule[-5pt]{0pt}{8pt}}\\
SBSOL & \parbox[t]{2.7in}{\hyphenpenalty10000 \raggedright
ERFIN, ERMSG, IERM1, IERV1, SBSOL}\\
\end{tabular}

\begcode

\medskip\
\lstset{language=[77]Fortran,showstringspaces=false}
\lstset{xleftmargin=.8in}

\centerline{\bf \large DRDBACC}\vspace{10pt}
\lstinputlisting{\codeloc{dbacc}}
\newpage
\vspace{30pt}\centerline{\bf \large ODDBACC}\vspace{10pt}
\lstset{language={}}
\lstinputlisting{\outputloc{dbacc}}

\end{document}


\documentclass{article}

\usepackage[a4paper, total={6in, 10in}]{geometry}
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{amsfonts}

\newcommand{\mc}{\mathcal}
\newcommand{\mb}{\mathbb}
\DeclareMathOperator{\sialge}{\sigma-algebra}
\DeclareMathOperator{\sifin}{\sigma-finite}
\DeclareMathOperator{\pisys}{\pi-system}
\DeclareMathOperator{\picls}{\pi-class}
\DeclareMathOperator{\lamsys}{\lambda-system}
\DeclareMathOperator{\lamcls}{\lambda-class}

\title{Notes on Measure Theory \& Probability}
\author{a1trl9}
\date{}
\setlength{\parindent}{0cm}

\begin{document}

\maketitle

\section{Measure}
\subsection{Classes of sets}
\textbf{Definition 1.1.1:} let \(\Omega\) be a nonempty set and \(\mc{P}(\Omega)\) be the power set of \(\Omega\).
A collection of sets \(\mc{F}\subset \mc{P}(\Omega)\) is called an \textit{algebra} if

\begin{enumerate}[label=(\alph*)]
\item \(\Omega \in \mc{F}\).
\item \(A \in \mc{F}\) implies \(A^c \in \mc{F}\).
\item \(A, B \in \mc{F}\) implies \(A \cup B \in \mc{F}\).
\end{enumerate}

It is easy to see with (b) (\textit{symmetric}), (c) is equivalent to

\[(c)' \quad A, B \in \mc{F} \implies A \cap B \in \mc{F} \]

\textbf{Proof:} to prove (c) \(\implies\) (c)', let \(A, B \in \mc{F}\), by (b), \(A^c, B^c \in \mc{F}\). By (c), \(
A^c \cup B^c \in \mc{F}\). By (b) again, \(\Omega \setminus (A^c \cup B^c) = (A^c \cup B^c)
\in \mc{F}\). \(\Omega \setminus (A^c \cup B^c) = (\Omega \setminus A^c) \cap (\Omega
\setminus B^c) = A \cap B\). Proved. The converse proof follows similar steps.

\vspace{2mm}
\textbf{Definition 1.1.2: } a class \(\mc{F} \subset \mc{P}(\Omega)\) is called an \textit{\(\sialge\)} if
it is an \textit{algebra} and

\[A_n \in \mc{F} \quad for \quad n \geq 1 \implies \bigcup_{n\geq 1}A_n \in \mc{F}\]

\textbf{Proposition 1.1.1: } a class \(\mc{F} \subset \mc{P}(\Omega)\) is a \(\sialge\) \textit{iff}
\(\mc{F}\) is
an \textit{algebra} and satisfies

\[A_n \in \mc{F}, A_n \subset A_{n+1} \quad for \: all \quad n \implies \bigcup_{n \geq 1}A_n \in \mc{F} \]

\textbf{Proof:} to prove \textit{if}, let \(\{B_n\}_{n\geq 1} \in \mc{F}\), then for all \(n\geq 1\),
\(\bigcup_{i=1}^nB_i \in \mc{F}\) (by (c)) and obviously \(\bigcup_{i}^{n-1}B_n \subset \bigcup_{i}^{n}B_n\).
Let \(A_n=\bigcup_{i=1}^nB_i\), easy to see \(\bigcup_{n\geq 1}A_n=\bigcup_{n\geq 1}B_n\). With the condition given,
\(\bigcup_{n\geq 1}B_n \in \mc{F}\). Proved. The \textit{only if} part is obvious.

\vspace{2mm}
Examples of \textit{algebra} or \(\sialge\) (or neither):

\begin{enumerate}
\item \(\mc{F}_1=\mc{P}(\Omega)\equiv\{A: A \subset \Omega\}\) (\(\sialge\)).
\item \(\mc{F}_2= \{\emptyset, \Omega \} \) (\(\sialge\), also called \textit{trival \(\sialge\)}).
\item \(\mc{F}_3= \{A \subset \Omega\: :\: either\: |A|\: is\: finite\: or\: |A^c|\: is\: finite \} \) (\textit{algebra} but
\item \(\mc{F}_4= \{A \subset \Omega\: :\: either\: |A|\: is\: countable\: or\: |A^c|\: is\: countable \} \) (
\(\sialge\)).
\end{enumerate}

For (3), if \(\Omega\) is infinite, let \({A_1, A_2, \cdots, A_n} \subset \Omega\) so that for all \(A_i, i\geq 1, A_i \in \mc{F}_3\)
. However both \(A\equiv \bigcup_{i\geq 1} A_{2i-1}\) and \(A^c\) are infinite, so \(A \not\in \mc{F}_3\).

\vspace{2mm}
For (4), suppose \(A_1, A_2, \cdots, A_n \subset \Omega\). If at least one of \(A_i, i\geq 1\) is countable, then \(\bigcap_{i\geq 1}A_n\)
is countable and \(\bigcap_{i\geq 1}A_n \in \mc{F}_4\). If all \(A_i \geq 1\) are uncountable, then all \(A_i^c \geq 1\) are
countable. \((\bigcup_{i\geq 1}A_n)^c=(\bigcap_{i\geq 1}A_i^c) \in \mc{F}_4\). As (b) in definition 1.1.1 is easy to prove, \(\bigcup_{i\geq 1}A_n \in \mc{F}_4\),
which is equivalent to \(\bigcap_{i\geq 1}A_n\in \mc{F}_4\).

\vspace{2mm}
\textbf{Definition 1.1.3: } if \(\mc{A}\) is a class of subsets of \(\Omega\), then the \(\sialge\) generated by \(\mc{A}\)
, denoted by \(\sigma(\mc{A})\) is defined as:

\[
\sigma(\mc{A}) = \bigcap_{\mc{F}\in \mc{I}(\mc{A})}\mc{F}
\]

where \(\mc{A}\equiv \{\mc{F}: \mc{A} \subset \mc{F}\) and \(\mc{F}\) is a \(\sialge\) on  \(\Omega\}\).

\vspace{2mm}
\textbf{Definition 1.1.4: } The \textit{Borel \(\sialge\)} on a topological space \(\mb{S}\) is defined as the \(\sialge\) generated
by the collection of open sets in \(\mb{S}\).

\vspace{2mm}
\textbf{Definition 1.1.5: } A class \(\mc{C}\) of subsets of \(\Omega\) is a \(\pisys\) or a \(\picls\) if
\(A, B \in \mc{C} \implies A \cap B \in \mc{C}\).

\vspace{2mm}
\textbf{Definition 1.1.6: } A class \(\mc{L}\) of subsets of \(\Omega\) is a \(\lamsys\) or a \(\lamcls\) if:

\begin{enumerate}[label=(\roman*)]
\item \(\Omega \in \mc{L}\).
\item \(A, B \in \mc{L}, A \subset B, \implies B\setminus A \in \mc{L} \).
\item \(A_n \in \mc{L}, A_{n} \subset A_{n+1}\) for all \(n \geq 1 \implies \bigcup_{n\geq 1}A_n \in \mc{L}\).
\end{enumerate}

It is easy to that every \(\sialge\) is a \(\lamsys\). The converse is not necessarily true.

\vspace{2mm}
\textbf{Theorem 1.1.2: }if \(\mc{C}\) is a \(\pisys\), then \(\lambda(\mc{C})=\sigma(\mc{C})\).

\vspace{2mm}
\textbf{Proof: } As mentioned, every \(\sialge\) of \(\mc{C}\) is a \(\lamsys\). Therefore, \(\lambda(\mc{C}) \subset
\sigma(\mc{C})\). Suppose the class of subsets of \(\mc{C}\): \(\lambda_1 \equiv \{A: A\in \lambda(\mc{C})\; |\; A \cap B \in \lambda(\mc{C})
\) for all \(B\in \lambda(\mc{C})\}\). It is obvious that \(\Omega \in \lambda_1(\mc{C})\). If \(A_1, A_2\in \lambda_1(\mc{C})\),
\(A_1\subset A_2\), then for all \(B\in \lambda(\mc{C})\), \((A_2\setminus A_1)\cap B = (A_2\setminus B)\cap (A_1\setminus B)\).
As both \(A_2\setminus B, A_1\setminus B \in \lambda(\mc{C})\), \((A_2\setminus B)\cap (A_1\setminus B)\in \lambda(\mc{C})\),
indicating \(A_2\setminus A_1\in \lambda_1(\mc{C})\). If \(A_1, A_2, \cdots, A_n \in \lambda_1(\mc{C})\),
\(A_n\subset A_{n+1}\) for \(n\geq 1\), then for all \(B\in \lambda(\mc{C})\), \((\bigcup_{n\geq 1}A_n)\cap B=\bigcup_{n\geq 1}(A_n\cap B)\),
as \(A_n\cap B \in \lambda(\mc{C})\), \((A_n\cap B)\subset (A_{n+1}\cap B)\), \(\bigcup_{n\geq 1}(A_n\cap B)\in \lambda(\mc{C})\), indicating
\(\bigcup_{n\geq 1}A_n \in \lambda_1(\mc{C})\). Therefore, \(\lambda_1(\mc{C})\) is a \(\lamsys\).

Meanwhile, suppose \(\lambda_2 \equiv \{A: A\in \lambda(\mc{C})\: |\: A \cap B \in \lambda(\mc{C})\) for all \(B\in \mc{C}\}\).
Obviously \(\lambda_1(\mc{C})\subset \lambda_2(\mc{C})\).
As \(\mc{C}\subset \lambda(\mc{C})\), it is easy to prove \(\lambda_2\) is also a \(\lamsys\). Besides,
as \(\mc{C}\) is a \(\pisys\), if \(A_1, A_2 \in \mc{C}\), \(A_1\cap A_2 \in \mc{C}\subset \lambda(\mc{C})\), indicating
\(\mc{C}\subset \lambda_2(\mc{C})\). While \(\lambda_2(\mc{C})\subset \lambda(\mc{C})\), \(\lambda_2(\mc{C})=\lambda(\mc{C})\).
Therefore, \(A\cap B\in \lambda(\mc{C})\) for each pair \(A\in \mc{C}, B\in \lambda(\mc{C})\), i.e. \(\mc{C}\subset \lambda_1(\mc{C})\).
While \(\lambda_1(\mc{C})\subset \lambda(\mc{C})\), \(\lambda_1(\mc{C})=\lambda(\mc{C})\). So \(\lambda(C)\) is close under intersection.
As being a \(\lamsys\) guanratees the other two conditions, \(\lambda(\mc{C})\) is also a \(\sialge\). With \(\lambda(\mc{C}\subset
\sigma(\mc{C}))\), proved.

\subsection{Measures}

\textbf{Definition 1.2.1: } Let \(\Omega\) be a nonempty set and \(\mc{F}\) be an algebra on \(\Omega\). Then a function
\(\mu\) on \(\mc{F}\) is called a measure if:

\begin{enumerate}[label=(\roman*)]
\item \(\mu(A)\in [0, \infty]\) for all \(A \in \mc{F}\).
\item \(\mu(\emptyset)=0\).
\item for any disjoint collection of sets \(A_1, A_2, \cdots, A_n \in \mc{F}\) with \(\bigcup_{n\geq 1}A_n\in \mc{F}\),
\(\mu(\bigcup_{n\geq 1}A_n)=\sum_{n=1}^{\infty}\mu(A_n)\).
\end{enumerate}

\vspace{2mm}
\textbf{Proposition 1.2.1: } Let \(\Omega\) be a nonempty set and \(\mc{F}\) be an algebra of subsets of \(\Omega\)
and \(\mu\) be a set function on \(\mc{F}\) with values in \([0, \infty]\) and with \(\mu(\emptyset)=0\).
Then \(\mu\) is a measure \textit{iff} \(\mu\) satisfies:

\begin{enumerate}[label=(\roman*)\('\)]
\item (finite additivity) for all \(A_1, A_2 \in \mc{F}\) with \(A_1\cap A_2 = \emptyset\),
\(\mu(A_1\cup A_2)=\mu(A_1)+\mu(A_2)\).
\item (monotone continuity from below or, m.c.f.b, in short) for any collection \(\{A_n\}_{n\geq 1}\)
of sets in \(\mc{F}\) such that \(A_n\subset A_{n+1}\) for all \(n\geq 1\) and \(\bigcup_{n\geq 1}A_n\in \mc{F}\),
\(\mu(\bigcup_{n\geq 1}A_n)=\lim_{n\to \infty}\mu(A_n)\).
\end{enumerate}

\vspace{2mm}
\textbf{Proof: } let \(\mu\) be a measure on \(\mc{F}\). Since \(\mu\) satisfies (iii), taking
\(A_3, A_4, \cdots, A_n = \emptyset\), \(\mu(A_1\cup A_2)=\mu(A_1)+\mu(A_2)\). Hence, if
\(A\subset B\) and \(A, B \in \mc{F}\), \(\mu(B)=\mu(A) + \mu(B\setminus A)\geq \mu(A)\), namely monotone.
So, for \(A_1, A_2, \cdots, A_n \in \mc{F}\), if one \(\mu(A_n)=\infty\), \(\mu(\bigcup_{n\geq 1}A_n)=\lim_{n\to \infty}\mu(A_n)
=\infty\). Suppose all \(\mu(A_n) \neq \infty\), let \(B_n=(A_n\setminus A_{n-1})\) (take \(A_0=\emptyset\)), then
\(B_1, B_2, \cdots, B_n\) are disjoint and \(\bigcup_{n\geq 1}A_n=\bigcup_{n\geq 1}B_n\),
So \(\mu(\bigcup_{n\geq 1}A_n)=\mu(\bigcup_{n\geq 1}B_n)\), with (iii), \(\mu(\bigcup_{n\geq 1}B_n)
=\sum_{n=1}^{\infty}\mu(B_n)=\sum_{n=1}^{\infty}\mu(A_n-A_{n-1})=\lim_{n\to \infty}\mu(A_n)\). (ii)\('\) proved.

\vspace{1mm}
Coversely, let \(\{B_n\}\) a disjoint collection in \(\mc{F}\), take \(A_n=\bigcup_{n\geq 1} B_{n}\), then
\(A_n \subset A_{n+1}\) and
\(\bigcup_{n\geq 1}A_n=\bigcup_{n\geq 1}B_n\). With (ii)\('\), \(\mu(\bigcup_{n\geq 1}B_n)=\mu(\bigcup_{n\geq 1}A_n)
=\lim_{n\to\infty}\mu(A_n)\), with (i), \(\mu(A_n)=\sum_{i=1}^n\mu(B_i)\), then \(\mu(\bigcup_{n\geq 1}B_n)
=\lim_{n\to\infty}\sum_{i=1}^n\mu(B_n)=\sum_{n=1}^{\infty}\mu(B_n)\). Proved.

\vspace{2mm}
\textbf{Definition 1.2.2: } a measure \(\mu\) is called \textit{finite} or \textit{infinite} according as
\(\mu(\Omega)<\infty\) or \(\mu(\Omega)=\infty\). A finite measure is called \textit{probability measure} if
\(\mu(\Omega)=1\). A measure \(\mu\) is called a \(\sifin\) if there exists a countable collection of sets
\(A_1, A_2, \cdots \in \mc{F}\), not necessarily disjoint, such that:

\begin{enumerate}[label=(\alph*)]
\item \(\bigcup_{n\geq 1}A_n=\Omega\).
\item \(\mu(A_n)<\infty\) for all \(n\geq 1\).
\end{enumerate}

\vspace{2mm}
\textbf{Proposition 1.2.2: } let \(\mu\) be a measure on an algebra \(\mc{F}\), and let \(A, B, A_1, A_2, \cdots, A_k\in \mc{F}\),
\(1\leq k < \infty\). Then:

\begin{enumerate}[label=(\roman*)]
\item (monotonicity) \(\mu(A)\leq\mu(B)\) if \(A\subset B\).
\item (finite subadditivity) \(\mu(A_1\cup A_2\cup \cdots\cup A_k )
\leq \sum_{i=1}^k\mu(A_i)\).
\item (inclusion-exclusion formula) if \(\mu(A_i)<\infty\) for all \(i=1,\cdots, k\), then
\(\mu(A_1\cup A_2\cup \cdots\cup A_k)=\sum_{i=1}^k\mu(A_k)-\sum_{1\leq i < j \leq k}
\mu(A_i\cap A_j)+\cdots+(-1)^{k-1}\mu(A_1\cap A_2\cap\cdots\cap A_k)\).
\end{enumerate}

\vspace{2mm}
\textbf{Proof: } if \(A\subset B\), \(A\cup (B\setminus) A = B\). With (iii) of \textbf{definition 1.2.1},
\(\mu(B)=\mu(A) + \mu(B\setminus A)\). As \(\mu(B\setminus A)\geq 0\),
\(\mu(B)\geq \mu(A)\). Meanwhile, suppose one of \(\mu(A_i), \mu(A_j) < \infty\),
then with (i), \(\mu(A_i\cap A_j)<\infty\) and \(\mu(A_i\cap A_j)-\mu(A_i\cap A_j)\)
is well defined.
\(\mu(A_i\cup A_j)=\mu(A_i\cup (A_j\setminus A_i))=\mu(A_i)+\mu(A_j\setminus A_i)
=\mu(A_i)+(\mu(A_j\setminus A_i) + \mu(A_i\cap A_j)) - \mu(A_\cap A_j)=\mu(A_i)+\mu(A_j)-\mu(A_i\cap A_j)\).
i.e. \(\mu(A_i\cup A_j)\geq \mu(A_i)+\mu(A_j)\).
For (iii), it obviously holds when \(k=2\). Assuming (iii) holds for \(n\), then for \(n+1\):

\begin{equation*}
\begin{split}
&\mu(\bigcup_{i=1}^{n+1}A_i)=\mu(\bigcup_{i=1}^nA_i)+\mu(A_{n+1})
-\mu[(\bigcup_{i=1}^nA_i)\cap A_{n+1}]\\
&=\sum_{i=1}^n\mu(A_i)-\sum_{1\leq i <j\leq n}(A_i\cap A_j) + \cdots +
(-1)^{n-1}\mu(A_1\cap A_2\cap\cdots\cap A_n)\\
&+\mu(A_{n+1})-\mu[\bigcup_{i=1}^n(A_i\cap A_{n+1})]\\
&=\sum_{i=1}^{n+1}\mu(A_i)-\sum_{1\leq i <j\leq n}(A_i\cap A_j) + \cdots +
(-1)^{n-1}\mu(A_1\cap A_2\cap\cdots\cap A_n)\\
&-\sum_{i=1}^n\mu(A_i\cap A_{n+1})+\sum_{1\leq i < j \leq n}\mu(A_i\cap A_j\cap A_{n+1})
-\cdots\\&-(-1)^{n-1}\mu(A_1\cap A_2\cap\cdots\cap A_{n+1})\\
&=\sum_{i=1}^{n+1}\mu(A_i)-\sum_{i\leq i < j\leq n+1}(A_i\cap A_j)+\cdots+
(-1)^k\mu(A_1\cap A_2\cap\cdots\cap A_{n+1})
\end{split}
\end{equation*}

Proved.

\end{document}


\chapter[Heuristics]{Heuristics}\label{chp:heuristics}

% Introduction
\chapterinitial{I}{t} is often necessary to find the most desirable choice from
a large, or indeed, infinite set of options. Sometimes this can be done using
exact techniques but often this is not possible and finding an almost perfect
choice quickly is just as good. This is where the field of
heuristics\index{heuristics} comes in to play.

\section{Problem}\label{sec:heuristics_problem}

A delivery company needs to deliver goods to 13 different stops.
They need to find a route for a driver that stops at each of the stops
once only, then returns to the first stop, the depot.

The stops are drawn in Figure~\ref{fig:tsp}.

\begin{figure}
    \begin{center}
        \includegraphics[width=.8\textwidth]{./assets/tsp/main.pdf}
    \end{center}
    \caption{The positions of the required stops.}
    \label{fig:tsp}
\end{figure}

The relevant information is the pairwise distances between each of the stops,
which is given by the distance matrix\index{matrix} in equation (\ref{eqn:tsp}).

\tiny{
    \input{./assets/tsp/main.tex}
}
\normalsize

The value \(d_{ij}\) gives the travel distance between
stops \(i\) and \(j\). For example, \(d_{23}=67\)
indicates that the distance between the 2nd and 3rd stop in the route is 67.

The delivery company would like to find the route around the 13 stops that gives
the smallest overall travel distance.

\section{Theory}\label{sec:heuristics_theory}

This problem is called a travelling salesman
problem\index{travelling salesman problem}, which can often be inefficient to
solve using exact methods~\cite{michalewicz2013solve}.
Heuristics are a family of methods that can be used to find a find a
\emph{sufficiently good} solution, though not necessarily the optimal solution,
where the emphasis is on prioritising computational efficiency.

The heuristic approach taken here will be to use a neighbourhood search algorithm.
This algorithm works by considering a given potential solution, evaluating it
and then trying another potential solution \emph{close} to it. What \emph{close}
means depends on different approaches and problems: it is referred to as the
neighbourhood. When a new solution is considered \emph{good}\footnote{`Good' is
again a term that depends on the approach and problem.} then the search
continues from the neighbourhood of this new solution.

For this problem, the steps are to first represent a possible solution, that is
a given route between all the potential stops as a \emph{tour}. If there are 3
total stops the tour must start and stop at the first one then there are two
possible tours:

\[
    t \in \{(1, 2, 3, 1), (1, 3, 2, 1)\}
\]

Given a distance matrix \(d\) such that \(d_{ij}\) is the distance between stop
\(i\) and \(j\) the total cost of a tour is given by:

\[
    C(t)=\sum_{i=1}^{n} d_{t_i, t_{i + 1}}
\]

Thus, with:

\[
    d = \begin{pmatrix}
        0 & 1 & 3\\
        1 & 0 & 15\\
        3 & 3 & 7
        \end{pmatrix}
\]

We have:

\begin{eqnarray*}
    C((1, 2, 3, 1)) &= d_{12} + d_{23} + d_{31} = 1 + 15 + 3 = 19\\
    C((1, 3, 2, 1)) &= d_{13} + d_{32} + d_{21} = 3 + 3 + 1 = 7
\end{eqnarray*}

In general, the neighbourhood search can be written down as:

\begin{enumerate}
    \item Start with a given tour: \(t\).
    \item Evaluate \(C(t)\).
    \item Identify a new \(\tilde t\) from \(t\) and accept it as a replacement
        for \(t\) if \(C(\tilde t)< C(t)\).
    \item Repeat the 3rd step until some stopping condition is met.
\end{enumerate}

This is shown diagrammatically in
Figure~\ref{fig:neighbourhood_search_flow_diagram}.

\begin{figure}[!hbtp]
    \begin{center}
        \includestandalone[width=.7\textwidth]{./assets/neighbourhood_search_flow_diagram/main}
    \end{center}
    \caption{The general neighbourhood search algorithm. \(N(t)\) refers to some
    neighbourhood of \(t\).}
    \label{fig:neighbourhood_search_flow_diagram}
\end{figure}

A number of stopping conditions can be used including some specific
overall cost or a number of total iterations of the algorithm.

The neighbourhood of a tour \(t\) is taken as some set of tours that can be
obtained from \(t\) using a specific and computationally efficient
\textbf{neighbourhood operator}.
To illustrate two such neighbourhoods operators, consider the following tour on
7 stops:

\[
    t = (0, 1, 2, 3, 4, 5, 6, 0)
\]

One possible neighbourhood is to choose 2 stops at random and swap. For
example, the tour \(\tilde t^{(1)}\in N(t)\) is obtained by swapping the stop
labelled 2 and the stop labelled 5.

\[
    \tilde t^{(1)} = (0, 1, 5, 3, 4, 2, 6, 0)
\]

Another possible neighbourhood is to choose 2 stops at random and reverse the
order of all stops between (including) those two stops. For example, the tour
\(\tilde t^{(2)} \in N(t)\) is obtained by reversing the order of all stops between
the stop labelled 2 and the stop labelled 5.

\[
    \tilde t^{(2)} = (0, 1, 5, 4, 3, 2, 6, 0)
\]

Examples of these tours are shown in
Figure~\ref{fig:tsp-effect-of-neighbourhood-operators}.

\begin{figure}[!hbtp]
    \begin{center}
        \includegraphics[width=0.8\textwidth]{./assets/tsp-effect-of-neighbourhood-operators/main.pdf}
    \end{center}
    \caption{The effect of two neighbourhood operators on \(t\). \(\tilde t^{(1)}\) is
    obtained by swapping stops labelled 2 and 5. \(\tilde t^{(2)}\) is obtained by reversing the
    path between stops labelled 2 and 5.}
    \label{fig:tsp-effect-of-neighbourhood-operators}
\end{figure}

\section{Solving with Python}\label{sec:heuristics_solving-with-python}

To solve this problem using Python, functions will be written that match the
first three steps in the Section~\ref{sec:heuristics_theory}.
The first step is to write the \mintinline{python}{get_initial_candidate}
function that creates an initial tour.

\begin{pyin}
import numpy as np

def get_initial_candidate(number_of_stops, seed):
    """Return an random initial tour.

    Args:
        number_of_stops: The number of stops
        seed: An integer seed.

    Returns:
        A tour starting an ending at stop with index 0.
    """
    internal_stops = list(range(1, number_of_stops))
    np.random.seed(seed)
    np.random.shuffle(internal_stops)
    return [0] + internal_stops + [0]
\end{pyin}

This gives a random tour on 13 stops:

\begin{pyin}
number_of_stops = 13
seed = 0
initial_candidate = get_initial_candidate(
    number_of_stops=number_of_stops,
    seed=seed,
)
print(initial_candidate)
\end{pyin}

\begin{pyout}
[0, 7, 12, 5, 11, 3, 9, 2, 8, 10, 4, 1, 6, 0]
\end{pyout}

To be able to evaluate any given tour its cost must be found. Here
\mintinline{python}{get_cost} does this:

\begin{pyin}
def get_cost(tour, distance_matrix):
    """Return the cost of a tour.

    Args:
        tour: A given tuple of successive stops.
        distance_matrix: The distance matrix of the problem.

    Returns:
        The cost
    """
    return sum(
        distance_matrix[current_stop, next_stop]
        for current_stop, next_stop in zip(tour[:-1], tour[1:])
    )
\end{pyin}

\begin{pyin}
distance_matrix = np.array(
    (
        (0, 35, 35, 29, 70, 35, 42, 27, 24, 44, 58, 71, 69),
        (35, 0, 67, 32, 72, 40, 71, 56, 36, 11, 66, 70, 37),
        (35, 67, 0, 63, 64, 68, 11, 12, 56, 77, 48, 67, 94),
        (29, 32, 63, 0, 93, 8, 71, 56, 8, 33, 84, 93, 69),
        (70, 72, 64, 93, 0, 101, 56, 56, 92, 81, 16, 5, 69),
        (35, 40, 68, 8, 101, 0, 76, 62, 11, 39, 91, 101, 76),
        (42, 71, 11, 71, 56, 76, 0, 15, 65, 81, 40, 60, 94),
        (27, 56, 12, 56, 56, 62, 15, 0, 50, 66, 41, 58, 82),
        (24, 36, 56, 8, 92, 11, 65, 50, 0, 39, 81, 91, 74),
        (44, 11, 77, 33, 81, 39, 81, 66, 39, 0, 77, 79, 37),
        (58, 66, 48, 84, 16, 91, 40, 41, 81, 77, 0, 20, 73),
        (71, 70, 67, 93, 5, 101, 60, 58, 91, 79, 20, 0, 65),
        (69, 37, 94, 69, 69, 76, 94, 82, 74, 37, 73, 65, 0),
    )
)
cost = get_cost(
    tour=initial_candidate,
    distance_matrix=distance_matrix,
)
print(cost)
\end{pyin}

\begin{pyout}
827
\end{pyout}

Now a function for neighbourhood operator will be written,
\mintinline{python}{swap_stops}, that swaps two stops in a given tour.

\begin{pyin}
def swap_stops(tour):
    """Return a new tour by swapping two stops.

    Args:
        tour: A given tuple of successive stops.

    Returns:
        A tour
    """
    number_of_stops = len(tour) - 1
    i, j = np.random.choice(range(1, number_of_stops), 2)
    new_tour = list(tour)
    new_tour[i], new_tour[j] = tour[j], tour[i]
    return new_tour
\end{pyin}

Applying this neighbourhood operator to the initial candidate gives:

\begin{pyin}
print(swap_stops(initial_candidate))
\end{pyin}

which swaps the 10th and 12th stops:

\begin{pyout}
[0, 7, 12, 5, 11, 3, 9, 2, 8, 1, 4, 10, 6, 0]
\end{pyout}

Now all the tools are in place to build a tool to carry out the
neighbourhood search \mintinline{python}{run_neighbourhood_search}.

\begin{pyin}
def run_neighbourhood_search(
    distance_matrix,
    iterations,
    seed,
    neighbourhood_operator=swap_stops,
):
    """Returns a tour by carrying out a neighbourhood search.

    Args:
        distance_matrix: the distance matrix
        iterations: the number of iterations for which to
                    run the algorithm
        seed: a random seed
        neighbourhood_operator: the neighbourhood operator
                                (default: swap_stops)

    Returns:
        A tour
    """
    number_of_stops = len(distance_matrix)
    candidate = get_initial_candidate(
        number_of_stops=number_of_stops,
        seed=seed,
    )
    best_cost = get_cost(
        tour=candidate,
        distance_matrix=distance_matrix,
    )
    for _ in range(iterations):
        new_candidate = neighbourhood_operator(candidate)
        cost = get_cost(
            tour=new_candidate,
            distance_matrix=distance_matrix,
        )
        if cost <= best_cost:
            best_cost = cost
            candidate = new_candidate

    return candidate
\end{pyin}

Now running this for 1000 iterations:

\begin{pyin}
number_of_iterations = 1000

solution_with_swap_stops = run_neighbourhood_search(
    distance_matrix=distance_matrix,
    iterations=number_of_iterations,
    seed=seed,
    neighbourhood_operator=swap_stops,
)
print(solution_with_swap_stops)
\end{pyin}

gives:

\begin{pyout}
[0, 7, 2, 8, 5, 3, 1, 9, 12, 11, 4, 10, 6, 0]
\end{pyout}

This has a cost:

\begin{pyin}
cost = get_cost(
    tour=solution_with_swap_stops,
    distance_matrix=distance_matrix,
)
print(cost)
\end{pyin}

\begin{pyout}
362
\end{pyout}

Therefore, using this particular algorithm, a pretty good route is found, with a
total distance of 362.

It is important to note that this may not be the optimal route, and different
algorithms may produce better solutions.
For example, one way to modify the algorithm is to use a different neighbourhood
operator.
Instead of swapping two stops, reverse the path between those two stops. This
corresponds to an algorithm called the ``2-opt''
algorithm\index{2-opt algorithm}.\footnote{The 2-opt algorithm was first
published in~\cite{croes1958method}.}
The \mintinline{python}{reverse_path} function does this:

\begin{pyin}
def reverse_path(tour):
    """Return a new tour by reversing the path between two stops.

    Args:
        tour: A given tuple of successive stops.

    Returns:
        A tour
    """
    number_of_stops = len(tour) - 1
    stops = np.random.choice(range(1, number_of_stops), 2)
    i, j = sorted(stops)
    new_tour = tour[:i] + tour[i : j + 1][::-1] + tour[j + 1 :]
    return new_tour
\end{pyin}

Applying this neighbourhood operator to the initial candidate gives:

\begin{pyin}
print(reverse_path(initial_candidate))
\end{pyin}

which reverses the order between the 3rd and the 11th stop:

\begin{pyout}
[0, 7, 4, 10, 8, 2, 9, 3, 11, 5, 12, 1, 6, 0]
\end{pyout}

Now running the neighbourhood search for 1000 iterations using the
\mintinline{python}{reverse_path} neighbourhood operator:

\begin{pyin}
solution_with_reverse_path = run_neighbourhood_search(
    distance_matrix=distance_matrix,
    iterations=number_of_iterations,
    seed=seed,
    neighbourhood_operator=reverse_path,
)
print(solution_with_reverse_path)
\end{pyin}

gives:

\begin{pyout}
[0, 8, 5, 3, 1, 9, 12, 11, 4, 10, 6, 2, 7, 0]
\end{pyout}

This now gives a different route.
Importantly, the costs differ substantially:

\begin{pyin}
cost = get_cost(
    tour=solution_with_reverse_path,
    distance_matrix=distance_matrix,
)
print(cost)
\end{pyin}

which gives:

\begin{pyout}
299
\end{pyout}

This improves on the solution found using the \mintinline{python}{swap_stops}
operator. Figure~\ref{fig:final-tsp-tours-python} shows the final obtained
routes given by both approaches.

\begin{figure}
    \begin{center}
        \includegraphics[width=\textwidth]{./assets/final-tsp-tours-with-python/main.pdf}
    \end{center}
    \caption{The final tours obtained by using the neighbourhood search in
    Python.}
    \label{fig:final-tsp-tours-python}
\end{figure}

\section{Solving with R}\label{sec:heuristics_solving-with-R}

To solve this problem using R, functions will be written that match the
first three steps in the Section~\ref{sec:heuristics_theory}.

The first step is to write the \mintinline{R}{get_initial_candidate}
function that creates an initial tour:

\begin{Rin}
#' Return an random initial tour.
#'
#' @param number_of_stops The number of stops.
#' @param seed An integer seed.
#'
#' @return A tour starting an ending at stop with index 0.
get_initial_candidate <- function(number_of_stops, seed){
  internal_stops <- 1:(number_of_stops - 1)
  set.seed(seed)
  internal_stops <- sample(internal_stops)
  c(0, internal_stops, 0)
}
\end{Rin}

This gives a random tour on 13 stops:

\begin{Rin}
number_of_stops <- 13
seed <- 1
initial_candidate <- get_initial_candidate(
  number_of_stops = number_of_stops,
  seed = seed)
print(initial_candidate)
\end{Rin}

\begin{Rout}
 [1]  0  9  4  7  1  2  5  3  8  6 11 12 10  0
\end{Rout}

To be able to evaluate any given tour its cost must be found. Here
\mintinline{R}{get_cost}  does this:

\begin{Rin}
#' Return the cost of a tour
#'
#' @param tour A given vector of successive stops.
#' @param seed The distance matrix of the problem.
#'
#' @return The cost
get_cost <- function(tour, distance_matrix){
  pairs <-  cbind(tour[-length(tour)], tour[-1]) + 1
  sum(distance_matrix[pairs])
}
\end{Rin}

\begin{Rin}
distance_matrix <- rbind(
        c(0, 35, 35, 29, 70, 35, 42, 27, 24, 44, 58, 71, 69),
        c(35, 0, 67, 32, 72, 40, 71, 56, 36, 11, 66, 70, 37),
        c(35, 67, 0, 63, 64, 68, 11, 12, 56, 77, 48, 67, 94),
        c(29, 32, 63, 0, 93, 8, 71, 56, 8, 33, 84, 93, 69),
        c(70, 72, 64, 93, 0, 101, 56, 56, 92, 81, 16, 5, 69),
        c(35, 40, 68, 8, 101, 0, 76, 62, 11, 39, 91, 101, 76),
        c(42, 71, 11, 71, 56, 76, 0, 15, 65, 81, 40, 60, 94),
        c(27, 56, 12, 56, 56, 62, 15, 0, 50, 66, 41, 58, 82),
        c(24, 36, 56, 8, 92, 11, 65, 50, 0, 39, 81, 91, 74),
        c(44, 11, 77, 33, 81, 39, 81, 66, 39, 0, 77, 79, 37),
        c(58, 66, 48, 84, 16, 91, 40, 41, 81, 77, 0, 20, 73),
        c(71, 70, 67, 93, 5, 101, 60, 58, 91, 79, 20, 0, 65),
        c(69, 37, 94, 69, 69, 76, 94, 82, 74, 37, 73, 65, 0)
)
cost <- get_cost(
  tour = initial_candidate,
  distance_matrix = distance_matrix)
print(cost)
\end{Rin}

\begin{Rout}
[1] 709
\end{Rout}

Now a function for a neighbourhood operator will be written,
\mintinline{R}{swap_stops}: swapping two stops in a given tour.

\begin{Rin}
#' Return a new tour by swapping two stops.
#'
#' @param tour A given vector of successive stops.
#'
#' @return A tour
swap_stops <- function(tour){
  number_of_stops <- length(tour) - 1
  stops_to_swap <- sample(2:number_of_stops, 2)
  new_tour <- replace(
    x = tour,
    list = stops_to_swap,
    values = rev(tour[stops_to_swap])
  )
}
\end{Rin}

Applying this neighbourhood operator to the initial candidate gives:

\begin{Rin}
new_tour <- swap_stops(initial_candidate)
print(new_tour)
\end{Rin}

which swaps the 6th and 11th stops:

\begin{Rout}
 [1]  0  9  4  7  1 11  5  3  8  6  2 12 10  0
\end{Rout}

Now all the tools are in place to build a tool to carry out the
neighbourhood search \mintinline{R}{run_neighbourhood_search}.

\begin{Rin}
#' Returns a tour by carrying out a neighbourhood search
#'
#' @param distance_matrix: the distance matrix
#' @param iterations: the number of iterations for
#'                    which to run the algorithm
#' @param seed: a random seed (default: None)
#' @param neighbourhood_operator: the neighbourhood operation
#'                                (default: swap_stops)
#'
#' @return A tour
run_neighbourhood_search <- function(
  distance_matrix,
  iterations,
  seed = NA,
  neighbourhood_operator = swap_stops
){
  number_of_stops <- nrow(distance_matrix)
  candidate <- get_initial_candidate(
    number_of_stops = number_of_stops,
    seed = seed
  )
  best_cost <- get_cost(
    tour = candidate,
    distance_matrix = distance_matrix
  )
  for (repetition in 1:iterations) {
    new_candidate <- neighbourhood_operator(candidate)
    cost <- get_cost(
        tour = new_candidate,
        distance_matrix = distance_matrix
    )
    if (cost <= best_cost) {
      best_cost <- cost
      candidate <- new_candidate
    }
  }
  candidate
}
\end{Rin}

Now running this for 1000 iterations:

\begin{Rin}
number_of_iterations <- 1000
solution_with_swap_stops <- run_neighbourhood_search(
  distance_matrix = distance_matrix,
  iterations = number_of_iterations,
  seed = seed,
  neighbourhood_operator = swap_stops
)
print(solution_with_swap_stops)
\end{Rin}

gives:

\begin{Rout}
 [1]  0 11  4 10  6  2  7 12  9  1  3  5  8  0
\end{Rout}

This has a cost:

\begin{Rin}
cost <- get_cost(
  tour = solution_with_swap_stops,
  distance_matrix = distance_matrix
)
print(cost)
\end{Rin}

which gives:

\begin{Rout}
[1] 360
\end{Rout}

Therefore, using this particular algorithm, a pretty good route is found, with a
total distance of 360.

It is important to note that this may not be the optimal route, and different
algorithms may produce better solutions.
For example, one way to modify the algorithm is to use a different neighbourhood
operator.
Instead of swapping two stops, reverse the path between those two stops. This
corresponds to an algorithm called the ``2-opt''
algorithm\index{2-opt algorithm}.\footnote{The 2 opt algorithm was first
published in~\cite{croes1958method}.}
The \mintinline{R}{reverse_path} function does this:

\begin{Rin}
#' Return a new tour by reversing the path between two stops.
#'
#' @param tour A given vector of successive stops.
#'
#' @return A tour
reverse_path <- function(tour){
  number_of_stops <- length(tour) - 1
  stops_to_swap <- sample(2:number_of_stops, 2)
  i <- min(stops_to_swap)
  j <- max(stops_to_swap)
  new_order <- c(c(1: (i - 1)), c(j:i), c( (j + 1): length(tour)))
  tour[new_order]
}
\end{Rin}

Applying this neighbourhood operator to the initial candidate gives:

\begin{Rin}
new_tour <- reverse_path(initial_candidate)
print(new_tour)
\end{Rin}

which reverses the order
between the 3rd and the 13th stop:

\begin{Rout}
 [1]  0  9 10 12 11  6  8  3  5  2  1  7  4  0
\end{Rout}

Now running the neighbourhood search for 1000 iterations using the
\mintinline{R}{reverse_path} neighbourhood operator:

\begin{Rin}
number_of_iterations <- 1000
solution_with_reverse_path <- run_neighbourhood_search(
  distance_matrix = distance_matrix,
  iterations = number_of_iterations,
  seed = seed,
  neighbourhood_operator = reverse_path
)
print(solution_with_reverse_path)
\end{Rin}

gives:

\begin{Rout}
 [1]  0  7  2  6 10  4 11 12  9  1  3  5  8  0
\end{Rout}

This now gives a different route.
Importantly, the costs differ substantially:

\begin{Rin}
cost <- get_cost(
  tour = solution_with_reverse_path,
  distance_matrix = distance_matrix
)
print(cost)
\end{Rin}

which gives:

\begin{Rout}
[1] 299
\end{Rout}

This is an improvement on the solution found using the \mintinline{R}{swap_stops}
operator. Figure~\ref{fig:final-tsp-tours-r} shows the final obtained routes
given by both approaches.

\begin{figure}
    \begin{center}
        \includegraphics[width=\textwidth]{./assets/final-tsp-tours-with-R/main.pdf}
    \end{center}
    \caption{The final tours obtained by using the neighbourhood search in R.}
    \label{fig:final-tsp-tours-r}
\end{figure}

\section{Wider context}\label{sec:heuristics_wider_context}

Heuristic methods, sometimes referred to as meta-heuristics, are a whole family
of algorithms used to find approximate solutions to combinatorial optimisation
problems. An overview is given in~\cite{bozorg2017meta}. These algorithms
include greedy searches, tabu searches\index{tabu search}, simulated
annealing\index{simulated annealing}, genetic algorithms\index{genetic algorithm},
as well \index{ant colony optimisation}. They are usually employed when the
problem is too large or complex to use exact methodologies.

The travelling salesman problem, described in this chapter, is a classic example
of one of these problems, formally described first in~\cite{menger1932},
although thought to have been discussed informally centuries before.
It is an example of a large number of types of problems collectively known as
vehicle routing problems, which often require heuristic methods for their
solutions. A survey is given in~\cite{braekers2016vehicle}. Variations of
the problem include multiple, heterogeneous and/or capacitated vehicles, and
stochastic or time-dependent travel times. A recent adaptation of the problem
is the green vehicle routing
problem\index{green vehicle routing problem}~\cite{moghdani2021green}, where the
cost function includes consideration of green house gas emissions and other
pollutants.

For more diverse applications of heuristic methods,
consider~\cite{lewis2016creating} which describes a tabu search algorithm
for finding seating plans for a wedding; and \cite{tong2013modeling} where
a genetic algorithm is used to build a prediction model for locations of
deep-sea wildlife habitats.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Bayesian model choice}
\begin{frame}{Bayesian model selection: testing all over again}
 Model choice (or selection) is a \textbf{major} topic within any school of inference: it is how scientists make decisions about competing theories/hypotheses in light of data.
 One can associate a set of models $\boldsymbol{\mathcal{M}} = \{ \mathcal{M}_1, \ldots \mathcal{M}_n \}$ with a set of indices $I$ such that $\mu \in I$ we want to estimate the posterior distribution of the indicator function $\mathbb{I}_{\boldsymbol{\Theta}_\mu}(\theta)$.

 Recall that estimating indicator functions over $\boldsymbol{\Theta}$ was the fundamental mechanic of Bayesian testing.
 In the setting of Bayesian model selection (BMS), we have something of the form
 \begin{equation*}
  \mathcal{M}_i : x \sim f_i(x \mid \theta_i), \theta_i \in \boldsymbol{\Theta}_i, i \in I.
 \end{equation*}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{M-completeness}
A key step in model selection is to identify in which regime the analyst finds themselves in.
\begin{defn}[M-open, M-closed, M-complete]
 \label{def:m-open}
 Model selection can be categorised in three settings:
 \begin{itemize}
  \item \textbf{M-closed}: a situation where the true data-generating model is one of $\mathcal{M}_i \in \boldsymbol{\mathcal{M}}$, even though  it is most often  unknown to the  analyst;
  \item \textbf{M-complete}: a situation where the true model exists and is out of the model set $\boldsymbol{\mathcal{M}}$.
  We nevertheless want to select one of the models in the set due to computational or mathematical tractability reasons.
  \item \textbf{M-open}: a situation in which we know the true data-generating model is not in $\boldsymbol{\mathcal{M}}$ and we have no idea what it looks like.
 \end{itemize}
\end{defn}
See~\cite{Bernardo2000} and \cite{Yao2018}.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{BMS: example I}
Suppose one has $x \in \mathbb{N}\cup \{0\}$, which measures, say, the number of eggs Balerion The Black Dread has laid in five consecutive breeding seasons.
One can conjure up
\begin{equation*}
 \mathcal{M}_1 : x \sim \operatorname{Poisson}(\lambda), \lambda > 0,
\end{equation*}
or, if feeling fancy,
\begin{equation*}
 \mathcal{M}_2 : x \sim \operatorname{Negative-binomial}(\lambda, \phi), \lambda, \phi > 0.
\end{equation*}
Notice that, under $\mathcal{M}_2$, $E[X] = \lambda$ and $\vr(X) = \lambda ( 1 + \lambda/\phi)$.
What happens as $\phi \to \infty$?
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{BMS: example II}
Take the famous Galaxy data set:
 \begin{center}
 \includegraphics[scale=0.3]{figures/galaxies.pdf}
\end{center}
A now classical model is a Gaussian mixture:
\begin{equation*}
 \mathcal{M}_i : v_j \sim \sum_{l=1}^i p_{il} \cdot \operatorname{Normal}(v_j; \mu_{li},\sigma^2_{li}).
\end{equation*}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{BMS: example III}
Consider the data:
 \begin{center}
 \includegraphics[scale=0.3]{figures/oranges.pdf}
\end{center}
Amongst the models we can consider,
\begin{align*}
\mathcal{M}_1 :& y_{it} \sim \operatorname{Normal}(\beta_{10} + b_{1i}, \sigma_1^2), \\
\mathcal{M}_2 :& y_{it} \sim \operatorname{Normal}(\beta_{20} + \beta_{21}T_t + b_{2i}, \sigma_2^2) , \\
\mathcal{M}_3 :& y_{it} \sim \operatorname{Normal}\left(\frac{\beta_{30}}{1 + \beta_{31}\exp\left(\beta_{32} T_t\right)}, \sigma_3^2\right), \\
\mathcal{M}_4 :& y_{it} \sim \operatorname{Normal}\left(\frac{\beta_{40} + b_{4i}}{1 + \beta_{41}\exp\left(\beta_{42} T_t\right)}, \sigma_4^2\right).
\end{align*}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Step 0: priors}
First, let us look at a convenient representation of model space:
\begin{equation*}
 \boldsymbol{\Theta} = \bigcup_{i \in I} \{i\} \times \boldsymbol{\Theta}_i.
\end{equation*}
Now, to each $\mathcal{M}_i$, we associate a prior $\pi_i(\theta_i)$  on each subspace and, by Bayes' theorem we get
\begin{align*}
 \pr(\mathcal{M}_i \mid x) & = \pr( \mu = i \mid x), \\
 &= \frac{w_i \int_{\boldsymbol{\Theta}_i} f_i(x\mid t_i)\pi_i(t_i)\,dt_i}{\sum_{j} w_j \int_{\boldsymbol{\Theta}_j} f_j(x\mid t_j)\pi_j(t_j)\,dt_j },
\end{align*}
where the $w_i$ are the \textbf{prior probabilities} for each model.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{An intuitive predictive}
A nice consequence of the formulation we just saw is that the predictive distribution looks quite intuitive:
\begin{align}
\nonumber
 p(\tilde{x} \mid \boldsymbol{x}) &= \sum_{j} w_j \int_{\boldsymbol{\Theta}_j} f_j(\tilde{x} \mid t_j) f_j(\boldsymbol{x}\mid t_j)\pi_j(t_j)\,dt_j,\\
 \label{eq:predictive_1}
 &= \sum_{j} \pr(\mathcal{M}_j \mid \boldsymbol{x}) m_j(\tilde{x}).
\end{align}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Hello, my old friend}
Here, Bayes factors also play a central role:
\begin{align*}
 \operatorname{BF}_{12} &= \frac{\pr(\mathcal{M}_1 \mid x)}{\pr(\mathcal{M}_2 \mid x)}\bigg/\frac{\pr(\mathcal{M}_1)}{\pr(\mathcal{M}_2)},\\
  &= \frac{w_1^\prime \cdot w_2}{w_2^\prime \cdot w_1},
\end{align*}
with $w_i^\prime := \pr(\mathcal{M}_1 \mid x)$.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Model averaging}
What if we simply \textbf{refuse} to select one model?
We can write
\begin{align}
 \nonumber
 p(\tilde{x} \mid \boldsymbol{x})  &= \int_{\boldsymbol{\Theta}} f(\tilde{x} \mid t) f(\boldsymbol{x}\mid t)\pi(t)\,dt,\\
 \nonumber
 &= \sum_{j} \int_{\boldsymbol{\Theta}_j} f_j(\tilde{x} \mid t_j) g(j, t_j \mid \boldsymbol{x})\,dt_j,\\
 \nonumber
 &= \sum_j p (\mathcal{M}_j \mid \boldsymbol{x}) \int_{\boldsymbol{\Theta}_j} f_j(\tilde{x} \mid t_j) p(t_j \mid \boldsymbol{x})\,dt_j,\\
 \label{eq:predictive_2}
  &= \sum_j w_j^\prime \int_{\boldsymbol{\Theta}_j} f_j(\tilde{x} \mid t_j) p(t_j \mid \boldsymbol{x})\,dt_j.
\end{align}
which is another version of the expression in (\ref{eq:predictive_1}).
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Model checking}
Modern Bayesian inference not only allows for, but actively encourages model interrogation and checking.
\begin{itemize}
 \item The central idea of \textbf{Leave-one-out cross-validation (LOO)} is to estimate the \textit{expected log pointwise predictive density for a new dataset}, elpd:
 \begin{equation*}
  \operatorname{elpd} = \sum_{i=1}^n \int m(\tilde{x}_i)\log p(\tilde{x}_i \mid \boldsymbol{x})\,d\tilde{x}_i.
 \end{equation*}
 See \cite{Vehtari2017}.
 \item With \textbf{Posterior predictive checks (PPCs)} we wish to compare  functions of the observed data, $f(\boldsymbol{x})$ with functions of the predictive distribution, $f(\boldsymbol{\tilde{x}})$.
  \begin{center}
 \includegraphics[scale=0.5]{figures/PPC.jpg}
\end{center}
 See \cite{Berkhof2000} and~\cite{Gabry2019}.
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Recommended reading}
\begin{itemize}
  \item[\faBook] \cite{Robert2007}, Ch. 7.
%  \item
 \item[\faForward] Next lecture: \cite{Schervish1995} Ch. 7.4.
 \end{itemize}
\end{frame}


\documentclass[../thesis.tex]{subfiles}

%!TeX spellcheck = en-GB

% chktex-file 18

\begin{document}

\chapter{Procedure}
\label{chap:procedure}

\section{The Hamiltonian}

In this chapter we will describe how we obtained the numerical results.
We begin by computing the matrix elements of the Hamiltonian in a basis
given by the eigenstates of an isotropic double harmonic oscillator.
In analogy with the classical case, we consider two independent
quantum numbers \(n_1, n_2\) corresponding to the two orthogonal
oscillating directions. We can define two number operators \(N_1, N_2\) such that
\(N_1 \ket{n_1, n_2} = n_1 \ket{n_1, n_2}\) and \(N_2 \ket{n_1, n_2} = n_2 \ket{n_1, n_2}\).
We consider the creation and annihilation operators \(\ad{1}, a_1\) and
\(\ad{2}, a_2\)
such that \(  N_1 = \ad{1} a_1 \) and \( N_2 = \ad{2} a_2 \).
In terms of the previously defined operators, the Hamiltonian of the isotropic
double harmonic oscillator is given by
\[
  H_0 = \hbar \omega_0 \left(\ad{1} a_1 + \frac{1}{2} I + \ad{2} a_2 + \frac{1}{2} I\right)
      = \hbar \omega_0 \left(N_1 + N_2 + I\right)
\]
and its eigenstates are given by
\[
  H_0 \ket{n_1, n_2} = \hbar \omega_0 \left( n_1 + n_2 + 1 \right) \ket{n_1, n_2}
\]

Since \(n_1 + n_2 = n\) can be obtained in \(\sum_{i=0}^n i = \frac{1}{2}\,n(n+1)\) ways,
the energy levels of the isotropic double harmonic oscillator are \(\frac{1}{2}\,n(n+1)\) fold
degenerated.

In the fundamental state \(\ket{0, 0}\), with \(n_1=n_2=0\), the two dimensional
oscillator has a \emph{zero point motion} with the energy equal to \(\hbar \omega_0\).
Since we are interested in the spacings between consecutive,
we can rescale the energy such that the energy of the fundamental state becomes \(0\).
Thus the new Hamiltonian will be given by
\[
  H_0 = A \left(N_1 + N_2\right),
\]
where \(A = \hbar \omega_0\).

We can construct the basis starting from the vacuum state \(\ket{0, 0}\) by acting
with the creation operators
\begin{align*}
  {\ad{1}}^{n_1} \ket{0, 0} &= \sqrt{n_1!} \ket{n_1, 0} \\
  {\ad{2}}^{n_2} \ket{0, 0} &= \sqrt{n_2!} \ket{0, n_2}
\end{align*}

Thus, by applying the operator \({\ad{1}}^{n_1} {\ad{2}}^{n_2}\)
we generate
% such that \(n_1 + n_2 = n\)
% (in order to obtain for each \(n\) all the states with the
% energy \(n\, \hbar \omega_0\))
% we obtain
the basis elements ordered as follows:
\[
  \ket{0, 0} \ket{0, 1} \ket{0, 2} \cdots \ket{0, n} \ket{1, 0} \ket{1, 1}
  \cdots \ket{1, n - 1} \cdots \ket{i, 0} \ket{i, 1} \cdots \ket{i, n - i} \cdots
  \ket{n, 0}.
\]

For our investigations, the Hamiltonian is expressed as a function of the creation and annihilation
operators up to fourth order terms as follows

\begin{equation}
\label{eq:hamilt}
\begin{split}
  H &= A \left( \ad{1} a_1 + \ad{2} a_2 \right)
    + \frac{B}{4} \bigg[ \left( 3 \ad{1} {\ad{2}}^2 + 3 a_1 a_2^2
                               - {\ad{1}}^3 - a_1^3 \right)   \\
  &\quad + 3 \left( a_1 {\ad{2}}^2 + \ad{1} a_2^2 - \ad{1} a_1^2 - {\ad{1}}^2 a_1
             + 2 a_1 \ad{2} a_2 + 2 \ad{1} \ad{2} a_2
          \right) \bigg]  \\
  &\quad + \frac{D}{16} \bigg[ 6 \left( {\ad{1}}^2 a_1^2 + {\ad{2}}^2 a_2^2 \right)
                        + 2 \left( a_1^2 {\ad{2}}^2 + {\ad{1}}^2 a_2^2 \right)
                        + 8 \ad{1} a_1 \ad{2} a_2  \\
  &\quad + 4 \left(\ad{1} a_1^3 + {\ad{1}}^3 a_1 + \ad{2} a_2^3 + {\ad{2}}^3 a_2
     + a_1^2 \ad{2} a_2 + {\ad{1}}^2 \ad{2} a_2 + \ad{1} a_1 a_2^2 + \ad{1} a_1 {\ad{2}}^2
        \right)  \\
  &\quad + \left( {\ad{1}}^4 + a_1^4 + {\ad{2}}^4 + a_2^4
     + 2 {\ad{1}}^2 {\ad{2}}^2 + 2 a_1^2 a_2^2
      \right)
                        \bigg].
\end{split}
\end{equation}

The physical origin of this Hamiltonian is related to quadrupole dynamics of
nuclear surfaces\cite{Baran1998, Baran1996}.
The operators \(a_1\) and \(a_2\) are related to the normal modes of the
quadrupole vibrations. In the case of a deformed nucleus, one mode named \(\beta \)-vibration
corresponds to oscillations preserving the axial symmetry.
For the second mode, \(\gamma \)-vibrations,
the transversal section perpendicularly to the symmetry axis changes periodically
from ellipse to circle.

The energy levels will be expressed in units of harmonic oscillator energy and therefore
from here on we will consider \(A = 1\).
We can obtain the eigenvalues and eigenvectors of the Hamiltonian by a
diagonalisation routine based on Relatively Robust Representations from
Intel\textsuperscript{\textregistered} Math Kernel Library~\cite{intel-mkl} used via a \texttt{Python}
program~\cite{NumPy-SciPy, scipy, matplotlib}%{\color{red} (+ citations)}.
Any such diagonalisation method requires a truncation of the Hilbert space
which induces errors concerning the eigenvalues. This errors increase as one moves to
the upper limit of the energy for a fixed dimension of the Hilbert space. Indeed we
expect this energies to have more important contributions from the states that were
eliminated by truncation.
We tested the stability of the energy levels by comparing the results obtained for
different sizes of the diagonalisation basis as is detailed in the next section.

\clearpage

\section{Stability}

We consider the \emph{stable levels} to be the eigenvalues which, at a change of
basis from one with a dimension of $N$ to one with dimension \(N+ \Delta N\),
do not change with more than a chosen threshold \(\delta_s\).
In the following figure we show the variation of the energy levels when
the dimension increases from \(N = 120\) to \(N = 140\).

\begin{center}
  \includegraphics{"B0.2 D0.4 N120/bar_E_diff"}
  \captionof{figure}{\(B = 0.2, D = 0.4, N = 120\)}
\end{center}

We can observe that the first 400--600 eigenvalues have a very good stability.
Thus we can choose the stability threshold for example at \(\delta_s = 10^{-9}\).
Qualitatively the shape of this distribution does not depend on the parameters of
the Hamiltonian or the dimension of the Hilbert space because it reflects the nature
of the approximation as discussed previously.
For example, for \(B = 0.55, D = 0.4, N = 260\) compared
with \(N = 280\)

\begin{center}
  \includegraphics{"B0.55 D0.4 N260/bar_E_diff"}
  \captionof{figure}{\(B = 0.55, D = 0.4, N = 260\)}
\end{center}

As expected, the number of stable levels increases with the
diagonalisation basis.
Roughly, for a given basis size $N$, the first 7--8\% levels differ with less
than \({\delta_s = 10^{-9}}\) when we compare with a basis of dimension
\(N + \Delta N\), with \(\Delta N = 20\).

\section{Statistics}

As mentioned in \cref{chap:quantum-chaos}, a spectrum can be characterised through the
probability distribution of the nearest neighbour spacing.
The \emph{spacing} is defined as the difference between two consecutive
energy levels. Similarly the \emph{relative spacing} is defined as
\[
  s = \frac{E_{i+1} - E_i}{\mean{\Delta E}},
\]
where \(\mean{\Delta E}\) is the average spacing \(\frac{E_n - E_0}{N}\).

The \emph{nearest neighbour spacing distributions} tell us the probability \(P(s)\dd{s}\) to
find a relative spacing $s$ when we move in the spectrum obtained by diagonalisation.
This probability is defined as follows
\[
  P(s)\dd{s} = \frac{N_{s,s+\Delta s}}{N},
\]
where \(N_{s,s+\Delta s}\) is the number of levels with the relative spacing
between \(s\) and \(s+ \Delta s\).
We can also define a \emph{cumulative probability distribution},
\[
  I(s) = \sum_{s_i=0}^s P(s_i) \Delta s
\]

\subsection{Irreducible representations}

In order to analyse the distributions of the previously obtained eigenvalues
we must first take into account the possible symmetries of the system.
A consistent analysis requires us to consider classes of states which transform
with the same \emph{irreducible representation}.

The Hamiltonian in eq.~\eqref{eq:hamilt} has the finite \(\mathcal{C}_{3v}\)
symmetry group.
This group has 3 irreducible representations: one bi-dimensional and
two unidimensional, one symmetric and one anti-symmetric, namely
\(\Gamma_b, \Gamma_s, \Gamma_a\).

\subsubsection{Separating the bi-dimensional representation}

The presence of the bi-dimensional representation corresponds a two-fold
degeneracy. These degeneracies can be identified  by scrutinising the
differences between consecutive levels \(\Delta E = E_{i+1} - E_i\).
An other option is to use directly the relative spacing, which is a rescaling
in units of average separation.
The separation of the symmetric and anti-symmetric irreducible representations
will be detailed later.

In figure~\ref{fig:bar_delta} we can see how \(\Delta E\) varies with the index of the
levels.

\begin{figure}[!h]
  \centering
  \includegraphics{"B0.2 D0.4 N60/bar_delta"}
  \caption{\(B=0.2, D=0.4, N=60\)}%
\label{fig:bar_delta}
\end{figure}

Due to of the finite precision of the numerical implementation, the
difference between two consecutive degenerate levels might not be exactly 0,
its value depending on the machine precision (as it can be seen in the above figure).
To take this fact into account we will consider that the levels which
have $s$ (or \(\Delta E\)) greater than a chosen \( \varepsilon \) as belonging
to one of the unidimensional representations.
In order to choose a suitable value for \( \varepsilon \), we use a histogram
to visualise the number of levels at different spacings
(see figure~\ref{fig:relsp-b0.2n120}).

\begin{figure}[h!]
  \centering
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.2 D0.4 N120/hist_relsp"}
    \caption{\(B=0.2, D=0.4, N=120\)}%
    \label{fig:relsp-b0.2n120}  % chktex 24
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.4 D0.4 N260/hist_relsp"}
    \caption{\(B=0.4, D=0.4, N=260\)}%
    \label{fig:relsp-b0.4n260}  % chktex 24
  \end{subfigure}
  \caption{The relative spacing histograms for different parameters}
\end{figure}

This bimodal shape of the histogram suggests clearly the presence of the degenerate
levels well separated from the rest.
For some particular values for $B$ (such as \(B = 0.4\)) and high values for
$N$ (\( N > 200 \)), we observed a splitting of the block corresponding to
the degenerated levels in two blocks, namely one at exactly 0 and the other at
very low values. (see~\cref{fig:relsp-b0.4n260})

If we plot the spacing as a function of the level index, we can see how
each level is situated with respect to the chosen \( \varepsilon \).
Once again we can observe a clear separation of the spacings corresponding to
the bi-dimensional representation (see~\cref{fig:relsp}).

\begin{figure}[h!]
  \centering
  \includegraphics{"B0.2 D0.4 N60/relsp"}
  \caption{\(B=0.2, D=0.4, N=60\)}
  \label{fig:relsp}
\end{figure}

\FloatBarrier

\subsubsection{Separating the unidimensional representations}

The unidimensional representations can be distinguished from the properties
of the states at reflections with respect to the \(Ox\) axis
(\(y \rightleftarrows -y\)).
We ca establish a correspondence between the symmetry of the states to
this operation and the values of the quantum number \(n_2\). The states
containing dominantly oscillator ket vectors with even \(n_2\) will
transform with the symmetric representation.

The values of the quantum numbers \(n_1\) and \(n_2\) depend on the ordering of the basis.
Because the diagonalisation algorithm returns the eigenvalues
(and the corresponding eigenvectors) in ascending order,
the initial ordering of the basis is lost.
One method to approximate \(n_1\) and \(n_2\) for a given eigenvector would be
to consider that their values are given by the index of the dominant coefficient.
Thus, if we have the following eigenvector
\[
\begin{pmatrix}
    C_{0,0} \\ C_{0,1} \\ \vdots \\ C_{0,n} \\ C_{1,0} \\ \vdots \\ C_{1,n-1} \\
    \vdots \\ C_{i,0} \\ \vdots \\ C_{i,n-i} \\ \vdots \\ C_{n,0}
  \end{pmatrix}
\]
and \(C_{i,j}\) is the greatest coefficient, than we assign to this eigenvector
the quantum numbers of the \(k\)-th element in the basis, where \(k\) is the
index of the coefficient.

For example, for the simplified case of the isotropic double harmonic oscillator \
(\(B=D=0\)) with \(N=3\), the Hamiltonian is given by
\[
H=
\begin{pmatrix}
  0 & 0 & 0 & 0 & 0 & 0\\
  0 & 1 & 0 & 0 & 0 & 0\\
  0 & 0 & 2 & 0 & 0 & 0\\
  0 & 0 & 0 & 1 & 0 & 0\\
  0 & 0 & 0 & 0 & 2 & 0\\
  0 & 0 & 0 & 0 & 0 & 2\\
\end{pmatrix}
\]
The eigenvalues will be \(E_i = 0, 1, 1, 2, 2, 2\) with the corresponding
eigenvectors
\[
  v_1 =
  \begin{pmatrix}
    1 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0
  \end{pmatrix},\
  v_{2} =
  \begin{pmatrix}
    0 \\ 1 \\ 0 \\ 0 \\ 0 \\ 0
  \end{pmatrix},\
  v_{3} =
  \begin{pmatrix}
    0 \\ 0 \\ 0 \\ 1 \\ 0 \\ 0
  \end{pmatrix},\
  v_{4} =
  \begin{pmatrix}
    0 \\ 0 \\ 1 \\ 0 \\ 0 \\ 0
  \end{pmatrix},\
  v_{5} =
  \begin{pmatrix}
    0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 1
  \end{pmatrix},\
  v_{6} =
  \begin{pmatrix}
    0 \\ 0 \\ 0 \\ 0 \\ 1 \\ 0
  \end{pmatrix}
\]

Since the basis is given by
\[
  \ket{0, 0} \ket{0, 1} \ket{0, 2} \ket{1, 0} \ket{1, 1} \ket{2, 0},
\]
the quantum numbers for the eigenvectors will be assigned as follows
\begin{align*}
  v_1 &\equiv \ket{0, 0} \text{since}\ k = 1 \\
  v_2 &\equiv \ket{0, 1} \text{since}\ k = 2 \\
  v_3 &\equiv \ket{1, 0} \text{since}\ k = 4 \\
  v_4 &\equiv \ket{0, 2} \text{since}\ k = 3 \\
  v_5 &\equiv \ket{2, 0} \text{since}\ k = 6 \\
  v_6 &\equiv \ket{1, 1} \text{since}\ k = 5
\end{align*}

% {\color{red} Maximum is not unique? \\ \centerline{\dots}}

In figures~\ref{fig:bar-rep-b0.2n120} and~\ref{fig:hist-rep-b0.2n120},
respectively~\ref{fig:bar-rep-b0.63n260} and~\ref{fig:hist-rep-b0.63n260}
we can see the relative spacing for each irreducible representation as a function
of index and as a histogram for \(B=0.2\) and \(B=0.63\).
Once the problem of selection of the states corresponding to a given
irreducible representation was solved, we calculated the average spacing for
different values of $B$ (see~\cref{fig:avgsp}).

% B=0.2 N=120
\begin{figure}[h]
  \centering
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.2 D0.4 N260/bar_rebde"}
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.2 D0.4 N260/bar_reuna"}
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.2 D0.4 N260/bar_reuns"}
  \end{subfigure}
  \caption{The relative spacing for each irreducible representation as a function
  of index for \(B=0.2, D=0.4, N=260\)}
  \label{fig:bar-rep-b0.2n120}  % chktex 24
\end{figure}

\begin{figure}
  \centering
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.2 D0.4 N260/rebde"}
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.2 D0.4 N260/reuna"}
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.2 D0.4 N260/reuns"}
  \end{subfigure}
  \caption{The relative spacing histogram for each irreducible representation for
  \(B=0.2, D=0.4, N=260\)}
\label{fig:hist-rep-b0.2n120}
\end{figure}

% B=0.63 N=260
\begin{figure}
  \centering
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.63 D0.4 N260/bar_rebde"}
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.63 D0.4 N260/bar_reuna"}
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.63 D0.4 N260/bar_reuns"}
  \end{subfigure}
  \caption{The relative spacing for each irreducible representation as a function
  of index for \(B=0.63, D=0.4, N=260\)}
  \label{fig:bar-rep-b0.63n260}  % chktex 24
\end{figure}

\begin{figure}
  \centering
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.63 D0.4 N260/rebde"}
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.63 D0.4 N260/reuna"}
  \end{subfigure}
  \begin{subfigure}[b]{0.49\textwidth}
    \centering
    \includegraphics{"B0.63 D0.4 N260/reuns"}
  \end{subfigure}
  \caption{The relative spacing histogram for each irreducible representation for
  \(B=0.63, D=0.4, N=260\)}
\label{fig:hist-rep-b0.63n260}
\end{figure}

\begin{figure}
  \centering
  \includegraphics{"avg_sp_N[220, 240, 260]"}
  \caption{\(\mean{\Delta E}\) as a function of $B$ for each representation}
\label{fig:avgsp}
\end{figure}

% {\color{red} Selection problems?}

\FloatBarrier

\subsection{Nearest neighbour distributions}

Having separated the states with respect irreducible representations we can also build
the nearest neighbour distributions. These histograms show the probability
of having a given spacing between consecutive levels as a function of $s$.

\Cref{fig:P(s)-b0.2n120,fig:I(s)-b0.2n120,fig:P(s)-b0.63n260,fig:I(s)-b0.63n260}
show the nearest spacing distributions \(P(s)\) and the cumulative
distributions \(I(s)\) for \(B=0.2\) and \(B=0.63\).

Taking into account the Berry-Tabor and Bohigas-Gianoni-Schmit conjectures,
we compare \(P(s)\) with the Poisson distribution \(P_P(s) = \ee^{-s}\)
and the Wigner distribution \(P_W(s) = \frac{\pi}{2} s \exp(-\frac{\pi}{4}s^2)\).

Since \(P(s)\) is a histogram, for an easier comparisom, we also computed the
histograms associated with the two distributions for the same spacing.

\begin{figure}[h]
  \includegraphics{"B0.2 D0.4 N260/P(s)_st_1e-09_eps_1e-08"}  % chktex 36
  \caption{\(B=0.2, D=0.4, N=120\)}
\label{fig:P(s)-b0.2n120}
\end{figure}

\begin{figure}
  \includegraphics{"B0.2 D0.4 N260/I(s)"}  % chktex 36
  \caption{\(B=0.2, D=0.4, N=120\)}
\label{fig:I(s)-b0.2n120}
\end{figure}

\begin{figure}
  \includegraphics{"B0.63 D0.4 N260/P(s)_st_1e-09_eps_1e-08"}  % chktex 36
  \caption{\(B=0.63, D=0.4, N=260\)}
\label{fig:P(s)-b0.63n260}
\end{figure}

\begin{figure}
  \includegraphics{"B0.63 D0.4 N260/I(s)"}  % chktex 36
  \caption{\(B=0.63, D=0.4, N=260\)}
\label{fig:I(s)-b0.63n260}
\end{figure}

\end{document}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{tfrrpmh}
\section*{\hspace*{-1.6cm} tfrrpmh}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Reassigned pseudo Margenau-Hill time-frequency distribution.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[tfr,rtfr,hat] = tfrrpmh(x)
[tfr,rtfr,hat] = tfrrpmh(x,t)
[tfr,rtfr,hat] = tfrrpmh(x,t,N)
[tfr,rtfr,hat] = tfrrpmh(x,t,N,h)
[tfr,rtfr,hat] = tfrrpmh(x,t,N,h,trace)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty tfrrpmh} computes the pseudo Margenau-Hill distribution and
        its reassigned version. The reassigned pseudo-MHD is given by the
        following expression\,:
\begin{eqnarray*}
\hspace*{-1cm}PMH_x^{(r)}(t',\nu';h)=\int_{-\infty}^{+\infty}
\int_{-\infty}^{+\infty} PMH_x(t,\nu;h)\ \delta(t'-\hat{t}(x;t,\nu))\
\delta(\nu'-\hat{\nu}(x;t,\nu))\ dt\ d\nu,
\end{eqnarray*}
where
\begin{eqnarray*}
\hat{t}(x;t,\nu)=t\ \ \mbox{ and }\ \
\hat{\nu}(x;t,\nu)=\nu+\Im\left\{\dfrac{F_x(t,\nu;\ens{D}_h)\ F_x^*(t,\nu;h)}
{2\pi|F_x(t,\nu;h)|^2}\right\}.
\end{eqnarray*}
$\ens{D}_h(t)=\frac{dh}{dt}(t)$ and $F_x(t,\nu;h)$ is the short-time
Fourier transform of $x(t)$ with analysis window $h(t)$.\\

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8cm} c}
Name & Description & Default value\\
\hline
        {\ty x}     & analyzed signal ({\ty Nx=length(x)})\\
        {\ty t}     & time instant(s)          & {\ty (1:Nx)}\\
        {\ty N}     & number of frequency bins & {\ty Nx}\\
        {\ty h}     & frequency smoothing window, {\ty h(0)} being forced to {\ty 1}
                                         & {\ty window(odd(N/4))}\\
        {\ty trace} & if nonzero, the progression of the algorithm is shown
                                         & {\ty 0}\\
     \hline {\ty tfr, rtfr} & time-frequency representation and its reassigned
            version\\
        {\ty hat}   & complex matrix of the reassignment vectors\\

\hline
\end{tabular*}
\vspace*{.2cm}

When called without output arguments, {\ty tfrrpmh} runs {\ty tfrqview}.
\end{minipage}

%\newpage

{\bf \large \sf Example}
\begin{verbatim}
         sig=fmlin(128,0.1,0.4);
         h=window(17,'Kaiser');
         tfrrpmh(sig,1:128,64,h,1);
\end{verbatim}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
all the {\ty tfr*} functions.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Reference}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
[1] F. Auger, P. Flandrin ``Improving the Readability of Time-Frequency and
Time-Scale Representations by the Reassignment Method'' IEEE Transactions
on Signal Processing, Vol. 43, No. 5, pp. 1068-89, 1995.
\end{minipage}


\documentclass{article}

\usepackage{isomath}
\usepackage{amsmath}
\usepackage{numprint}

\author{Daniel Fernandes Martins (danielfmt)}
\title{Question \#10 Solution}

\begin{document}

\maketitle

\textbf{Disclaimer.} This is the reasoning I used to solve the problem; it
may be wrong though. This is intended just as food for thought.

\section{Perceptron Learning Algorithm as SGD}

This question presents to us a few error functions $e_n(\mathbf{w})$ and asks
which one can be used in order to implement the Perceptron Learning Algorithm
(PLA) using Stochastic Gradient Descent (SGD).

\subsection{Similarities Between PLA And SGD}

The PLA is stochastic much like SGD in the sense that we randomly pick one
example at a time and adjust the weight vector $\mathbf{w}$ a little bit towards
the minimum; the main difference is that, in PLA, we only pick misclassified
points, where in SGD the error function is evaluated on all $N$ points of the
training set.

This observation alone is enough to eliminate all possible options but this one:

\begin{equation*}
  e_n(\mathbf{w}) = -min(0, y_n\mathbf{w}^\mathsf{T} \mathbf{x}_n)
\end{equation*}

\subsection{Intuition}

In the context of the Gradient Descent algorithm, the signal
$\mathbf{w}^\mathsf{T}\mathbf{x}_n$ translates to a probability distribution
that describes how a given example $\mathbf{x}_n$ will be classified; a largely
positive signal means $P(y_n=1 | \mathbf{x}_n) \approx 1$, as a largely negative
signal means $P(y_n=1 | \mathbf{x}_n) \approx 0$ , thanks to the logistic
function $\theta$.

In this particular error measure, the signal $\mathbf{w}^\mathsf{T}\mathbf{x}_n$
is multiplied by the target label $y_n$. If both the signal and the target
label agrees on the sign, i.e. both positive or negative, this means that the
signal classifies that example correctly, thus the result of this product is
some number $s \geq 0$ which will be killed by $min(0, s)$.

This trick is used so that only the gradient of misclassified examples are
allowed to contribute with changes in the weight vector $\mathbf{w}$,
which is exactly what PLA does.

\end{document}


\subsection{Method of Undetermined Coefficients}
\noindent
The type of equation we're trying to solve is a heterogeneous linear ODE with constant coefficients. These have the form
\begin{equation*}
	a_ny^{(n)} + a_{n-1}y^{(n-1)} + \ldots + a_1y' + a_0y = b(x)
\end{equation*}
We will assume that the solution has the form $y = y_h + y_p$, where $y_h$ is the general solution to the homogeneous equation ($b(x) = 0$), and $y_p$ is the particular solution.\\

\noindent
We know how to solve for $y_h$ exactly without guessing. However, we will make a guess for the form of $y_p$ based on the form of $b(x)$ and the form of $y_h$ using the rules described in the below examples. For each term in our guess for $y_p$, we will solve for a constant.\\

\ifodd\includeHigherOrderExamples\input{./higherOrder/nonHomeg/undeterminedCoeffs_examples.tex}\fi