\documentclass{article} % For LaTeX2e
\usepackage[a4paper,margin=1.5in]{geometry}
\usepackage[mathlines]{lineno}
\usepackage{hyperref}
\usepackage{url}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{natbib}
\usepackage{latexsym,amsbsy,amssymb,color,xspace, booktabs}

\include{macros}
\include{local_macros}
\def\linenumberfont{\normalfont\small\sffamily}

\title{Multiple Output Regression}

\newcommand{\fix}{\marginpar{FIX}}
\newcommand{\new}{\marginpar{NEW}}

%\nipsfinalcopy % Uncomment for camera-ready version

\begin{document}
\maketitle
\linenumbers

\section{Model Description}
We describe a multiple-output regression model.
Suppose that we have inputs $\X = \{\x_n\}_{n=1}^N$ and outputs $\y = \{\y_n\}_{n=1}^N$, with $\x_n \in \calR^D$ and $\y_n \in \calR^P$, i.e. the output is $P$-dimensional.
For ease of exposition, we assume that there are no missing values in any output dimension.
The most dominant approach in GP-based multiple output regression is to compose the different outputs as some \textit{linear} mixing (combination) of some basic processes (random functions).
Our model is also underpinned by this composition approach.
However, to really address the issue of scalability, this model is built upon sparse GPs from the ground up.

\subsection{Prior and Likelihood}
We use a simple composition:
\begin{align}
f_i(\x) = w_i g(\x) + h_i(\x), \quad i = 1 \hdots P,
\end{align}
where each (latent) output is the sum of two functions: one scaled latent function that is shared by all outputs and one latent function unique to the output.
This allows the outputs to be correlated via the shared function $g(\x)$ while still having their own independent processes.
Furthermore we assume that $g(\x) \sim \GP(0, k_g(\x,\x'; \vectheta_g))$ and $h_i(\x) \sim \GP(0, k_i(\x,\x'; \vectheta_i))$, and that $h_i$ are independent GPs with their own set of hyperparameters. \\

\noindent The auto-variance of the latent output using this composition is
\begin{align}
\nonumber
cov[f_i(\x), f_i(\x')]
&= cov \left[ \left(w_i g(\x) + h_i(\x) \right) \left(w_{i} g(\x') + h_i(\x') \right) \right] \\
&= w_i^2 k_g(\x, \x') + k_i(\x,\x').
\end{align}
\\
\noindent The cross-covariance of the latent outputs is
\begin{align}
\nonumber
cov[f_i(\x), f_{i'}(\x')]
&= cov \left[ \left(w_i g(\x) + h_i(\x) \right) \left(w_{i'} g(\x') + h_{i'}(\x') \right) \right] \\
&= w_i w_{i'} k_g(\x, \x').
\end{align}

\noindent It can be seen that the induced covariance function of $f_i(\x)$ is very similar to that in the MTGP model.
Specifically, in MTGP the task-correlation matrix is a free-form positive definite matrix $B$ whereas here it is the matrix $\w \w^T$ where $\w = [w_1, \hdots, w_P]^T$.
However this model is more flexible compared to MTGP because each output is also influenced by its own independent process $h_i(\x)$.
Note that we can also use more than one shared function $g(\x)$, in which case it becomes the semiparametric latent force model (SLFM), again with the addition of independent process in each output dimension. \\

\noindent
\textbf{Augmented sparse GPs}
Toward scalable modeling, we replace standard GPs with sparse GPs augmented with \textit{different} set of inducing inputs.
This adds much flexibility to the model as the roles of $g(\x)$ and $h_i(\x)$ can be quite different, so it is necessary that each has its own inducing inputs.
Furthermore, $g(\x)$ can be seen as a \textit{global} function operating on the entire input space (of all output dimensions), while each $h_i(\x)$ operates only on the inputs of the $i$-th output, which can be smaller than that of $g(\x)$.
If more than one $g(\x)$ is used, each $g(\x)$ may capture a different pattern so they should also have separate inducing inputs. \\

\noindent
\textbf{Prior}
\newcommand{\Zg}{\Z_g}
\newcommand{\Zi}{\Z_i}
Let $\g = g(\X), \h_i = h_i(\X), \h = \{\h_i \}_{i=1}^P$. Let $\Zg$ and $\mat{Z}^h = \{\Zi \}_{i=1}^P$ be the set of inducing inputs for $g(\x)$ and $h_i(\x)$; the corresponding inducing points $\u_g$ and $\u^h = \{\u_i \}_{i=1}^P$.
The prior of the augmented model is given by:
\begin{align}
p(\g | \u_g) &= \Normal(\g; \BigMu_g, \tilde{\K}_g )\\
p(\u_g) &= \Normal(\u_g; \vec{0}, k_g(\Zg, \Zg)) \\
p(\h | \u^h) &= \prod_{i=1}^P \Normal(\h_i; \BigMu_i, \tilde{\K}_i)\\
p(\u^h) &= \prod_{i=1}^P \Normal(\u_i; \vec{0}, k_i(\Zi, \Zi)),
\end{align}
where $\BigMu_g = k_g(\X,\Zg)k(\Zg,\Zg)^{-1}\u_g$ and $\tilde{\K}_g = k_g(\X,\X) - k(\X,\Zg)k(\Zg,\Zg)^{-1}k(\Zg,\X)$ and $\BigMu_i, \tilde{\K}_i$ are similarly defined.

\noindent
\textbf{Likelihood}
The likelihood as usual follows the standard iid Gaussian likelihood,
\begin{align}
p(\y | \g, \h ) = \prod_{i=1}^P p( \y_i ; \g, \h_i) = \prod_{i=1}^P \Normal( \y_i ; w_i \g + \h_i, \beta_i^{-1} \I).
\end{align}

\subsection{Variational Inference in Sparse GPs Revisited}
In this section we review inference in sparse GPs (as presented in Titsias).
The posterior in an augmented model is $p(\f, \u | \y) = p(\f | \u, \y) p(\u | \y)$.
The key property of such augmented GPs  is the notation of \textit{sufficient statistics}: given the inducing points $\u$, the latent values $\f$ are independent with any other set of latent values (e.g. the test set).
In the optimal setting when $\u$ is the sufficient statistics of $\f$, it should hold that $p(\f | \u, \y) = p(\f | \u)$ as $\y$ is only the noisy version of $\f$.
This leads to choosing a variational approximation of the posterior which factorizes as $q(\f, \u | \y) = p(\f | \u) q(\u | \y)$.
Since the conditional $p(\f | \u)$ is known, variational inference becomes learning an optimal posterior $q(\u | \y)$ only. \\

\noindent Also as a consequence of sufficient statistics, the approximate prediction for test targets $\vfstar$ at test inputs $\X_*$ is
\begin{align}
\nonumber
p(\vfstar | \y, \X_*) &= \int p(\vfstar | \f, \u, \X_*) q(\f, \u | \y) \der \f \der \u \\
\nonumber
&= \int p(\vfstar | \u) q(\u| \y) p(\f | \u) \der \f \der \u \\ \nonumber
&= \int p(\vfstar | \u) q(\u| \y) \der \u \\
\label{eq:sorprediction}
&= \Normal(\vfstar; \bs{\mu_*},\vec{s_*})
\end{align}
where,
\begin{align}
\nonumber
\bs{\mu_*} &= \K_{*z} \K_{zz}^{-1}\m \\
\nonumber
\S_* &= \K_{**} - \K_{*z} \left(\K_{zz}^{-1} - \K_{zz}^{-1} \S \K_{zz}^{-1} \right) \K_{*z}^T.
\end{align}
 Here $\K_{*z}$ is the covariance matrix between test and inducing inputs, $\K_{zz}$ is the covariance matrix of the inducing inputs.

\subsection{Variational Inference}
\newcommand{\ug}{\u_g}
\newcommand{\uh}{\u^h}
Our goal of inference is to find the posterior $p(\g, \h, \ug, \uh | \y)$.
Following the previous discussion, we assume a variational distribution which factorizes as:
\begin{align}
\nonumber
q(\g, \h, \ug, \uh | \y)
 &= p(\g | \ug) q(\ug | \y) \prod_{i=1}^P p(\h_i | \u_i) q(\u_i | \y) \\
 &=  q(\ug, \uh) p(\g | \ug) \prod_{i=1}^P p(\h_i | \u_i)
\end{align}
Since the conditionals $p(\g | \ug)$ and $p(\h_i | \u_i)$ are given, we need only to find the optimum $q(\ug, \uh) = q(\ug | \y) \prod_{i=1}^P q(\u_i | \y)$.
Let $q(\ug | \y) = \Normal(\ug; \m_g, \S_g)$ and $q(\u_i | \y) = \Normal(\u_i; \m_i, \S_i)$. \\

\noindent
To find the optimum $q(\ug, \uh)$ we optimize the evidence lowerbound of the log marginal,
\begin{align}
\nonumber
\log p(\y) \ge& \int q(\ug, \uh) \log \frac{p(\y | \ug, \uh) p(\ug, \uh)}{q(\ug, \uh)} \der \ug \der \uh \\
\nonumber
=& \int q(\ug, \uh) \log p(\y | \ug, \uh)  \der \ug \der \uh
+ \int q(\ug, \uh) \log \frac{p(\ug, \uh)}{q(\ug, \uh)} \der \ug \der \uh \\
\label{eq:elbo}
=& \int q(\ug, \uh) \log p(\y | \ug, \uh)  \der \ug \der \uh
- \left( \KL[q(\ug) || p(\ug)] + \sum_{i=1}^P \KL[q(\u_i) || p(\u_i)] \right),
\end{align}
where the last equality occurs because both of $q(\ug, \uh)$ and $p(\ug, \uh)$ fully factorize.
Since $q(\ug), q(\u_i), p(\ug), p(\u_i)$ are all multivariate Gaussian distribution, the KL divergences are analytically tractable and require $\calO(M^3)$ computation, where $M$ is the largest number of inducing inputs (recall that $g(\x)$ and $h_i(\x)$ use separate set of inducing inputs). \\

\noindent To compute the above equation we first focus on the term:
\newcommand{\llangle}{\left\langle}
\newcommand{\rrangle}{\right\rangle}
\begin{align}
\nonumber
\log p(\y | \ug, \uh)
 &= \log \Eb{p(\y | \g, \h)}_{p(\g,\h | \ug, \uh)} \\
 \nonumber
&\ge \Eb{\log p(\y | \g, \h)}_{p(\g,\h | \ug, \uh)} \quad &\text{(Jensen's inequality)} \\
\nonumber
&=  \Eb{\sum_{i=1}^P \sum_{n=1}^N \log p(y_{in} | g_n, h_{in}) }_{p(\g,\h | \ug, \uh)}  \quad &\text{(factorized likelihood)} \\
&= \sum_{i=1}^P \sum_{n=1}^N \Eb{\log p(y_{in} | g_n, h_{in}) }_{p(\g,\h_i | \ug, \u_i)} \quad &\text{(linear operation)}
\end{align}
Observe that this is very similar to the SVI for standard GP in Hensman et al.
In this multiple-output model, the outputs factorize given $\g$ and $\h$ hence the lowerbound can be seen as sum of the lowerbounds in the single-output setting.

\noindent Each individual term $l_{in} = \llangle \log p(y_{in} | g_n, h_{in}) \rrangle_{p(\g,\h_i | \ug, \u_i)}$ can be computed as follows:
\begin{align}
\nonumber
l_{in} &= \int \log p(y_{in} | g_n, h_{in}) p(\g | \ug) p(\h_i | \u_i) \der \g \der \h_i \\
\nonumber
&= \int \log \Normal(y_{in} ; w_i g_n + h_{in}, \beta_i^{-1})
\Normal(g_n ; \mu_{gn}, \tilde{k}_{gnn})
\Normal(h_{in} ; \mu_{in}, (\tilde{k}_{inn}) \der g_n \der h_{in} \\
\nonumber
&= -\frac{1}{2} \log 2 \pi \beta_i^{-1} - \frac{1}{2} \int (y_{in} - w_i g_n - h_{in}) \beta_i (y_{in} - w_i g_n - h_{in})
\Normal(g_n ; \mu_{gn}, \tilde{k}_{gnn})
\Normal(h_{in} ; \mu_{in}, \tilde{k}_{inn}) \der g_n \der h_{in} \\
\nonumber
&= -\frac{1}{2} \log 2 \pi \beta_i^{-1} - \frac{1}{2} \int \left[(y_{in} - h_{in} - w_i \mu_{gn}) \beta_i (y_{in} - h_{in} - w_i \mu_{gn}) + w_i^2 \beta_i \tilde{k}_{gnn} \right]
\Normal(h_{in} ; \mu_{in}, \tilde{k}_{inn}) \der h_{in} \\
\nonumber
&= -\frac{1}{2} \log 2 \pi \beta_i^{-1} - \frac{1}{2} w_i^2 \beta_i \tilde{k}_{gnn}
- \frac{1}{2} \beta_i \tilde{k}_{inn} - \frac{1}{2} (y_{in} - w_i \mu_{gn} - \mu_{in}) \beta_i (y_{in} - w_i \mu_{gn} - \mu_{in}) \\
&= \log \Normal(y_{in}; w_i \mu_{gn} + \mu_{in}, \beta_i^{-1})  - \frac{1}{2} \beta_i (w_i^2 \tilde{k}_{gnn} + \tilde{k}_{inn}),
\end{align}
where $\tilde{k}_{gnn} = (\tilde{\K}_g)_{n,n}, \tilde{k}_{inn} = (\tilde{\K}_i)_{n,n}, \mu_{gn} = (\Mu_g)_n, \text{ and } \mu_{in} = (\Mu_i)_n$.
Notice that the above expression is very similar to the single-output case.
Hence our derivation here can be seen as the \textit{generalization} of SVI from the standard GP regression to multiple-output regression.

\noindent Substituting $l_{in}$ into equation \ref{eq:elbo} and carrying similar integration we get,
\begin{align}
\nonumber
\log p(\y)
\ge& \sum_{i=1}^P \sum_{n=1}^N
 \left( \log \Normal(y_{in}; \tilde{\mu}_{in}, \beta^{-1})
         - \frac{1}{2} \beta_i (w_i^2 \tilde{k}_{gnn} + \tilde{k}_{inn})
         - \frac{1}{2} \trace \left( w_i^2 \S_g \mat{\Lambda}_{gn} + \S_i \mat{\Lambda}_{in} \right)
\right) \\
&- \left( \KL[q(\ug) || p(\ug)] + \sum_{i=1}^P \KL[q(\u_i) || p(\u_i)] \right) \define \calL,
\end{align}
where
%TODO \Lambda_gn depends on the output i so need a better notation
\begin{align}
\tilde{\mu}_{in}
&= w_i k_g(\x_n, \Zg)k_g(\Zg,\Zg)^{-1}\m_g + k_i(\x_n, \Zi)k_i(\Zi,\Zi)^{-1}\m_i \\
\mat{\Lambda}_{gn}
&= \beta_i k_g(\Zg,\Zg)^{-1} k_g(\Zg, \x_n) k_g(\x_n, \Zg) k_g(\Zg,\Zg)^{-1} \\
\mat{\Lambda}_{in}
&= \beta_i k_i(\Zi,\Zi)^{-1} k_i(\Zi, \x_n) k_i(\x_n, \Zg) k_i(\Zg,\Zg)^{-1}.
\end{align}

\noindent Again, this clearly shows that this model generalizes the standard GP regression. This can be verified by setting $P = 1$, $w_i = 1$ and $h_i(\x) = 0$ to recover the bound in Hensman et al.
Due to the decomposition of this bound, we can use stochastic gradient descent to learn the variational parameters.

\subsubsection{Variational Parameters Derivatives}
\newcommand{\oi}{\vec{o}_i}
% some notation
Before diving into the details, we first define the indexing operator of a matrix: $\B(\vec{r},\vec{c})$ extracts the submatrix in rows $\vec{r}$ and columns $\vec{c}$ of $\B$.
To index all columns we use $\B(\vec{r},:)$ and similarly for all rows $\B(:,\vec{c})$.
Readers familiar with this operator will recognize that this is the MATLAB indexing operator.

The optimal posteriors are found by setting the gradients of the lowerbound $\calL$ wrt to the parameters of $q(\ug)$ and $q(\u_i)$.
Recall that in the model, different outputs can be observed at different inputs (i.e. the case of missing values).
Let $\oi$ be the indice of the observed inputs of the output dimension $i$.
We denote its set of observed inputs and targets as: $\X_i = \X(\oi,:)$ and $\y_i = y_{i}(\oi)$.

\noindent As a function of the parameters of $q(\ug)$, the lowerbound $\calL$ is:
\begin{align}
\nonumber
\calL_g \define&
 \sum_{i=1}^P \log \Normal(\y_i; w_i \A_g(\oi,:) \m_g + \A_i \m_i, \beta_i^{-1} \I)  \\
 &- \frac{1}{2} \sum_{i=1}^P \bigg(\beta_i \trace w_i^2 \tilde{\K}_g(\oi,\oi)
 + \beta_i \trace w_i^2 \S_g \A_g(\oi,:)^T \A_g(\oi,:) \bigg)
 \\
 &- \frac{1}{2} \log |k_g(\Zg,\Zg) \S_g^{-1}| -\frac{1}{2} \trace k_g(\Zg,\Zg)^{-1} (\m_g \m_g^T + \S_g) ,
\end{align}
where $\A_g = k_g(\X,\Z_g)k_g(\Z_g,\Z_g)^{-1}$, which gives $\A_g(\oi,:) = k_g(\X_i,\Zg) k_g(\Z_g,\Z_g)^{-1}$, and
$\A_i = k_i(\X_i,\Z_i)k_i(\Z_i,\Z_i)^{-1}$. \\

%------------------------------------------
% derivatives of q(u_g)
\noindent The derivatives of $\calL_q$ wrt $\m_g$ and $\S_g$ are given by:
\begin{align}
\deriv{\calL_g}{\m_g}
& = \sum_{i=1}^P \beta_i w_i \A_g(\oi,:)^T (\y_i - \A_i \m_i) - \left[k_g(\Zg,\Zg)^{-1} + \sum_{i=1}^P \beta_i w_i^2 \A_g(\oi,:)^T \A_g(\oi,:) \right] \m_g \\
\deriv{\calL_g}{\S_g}
&= \frac{1}{2} \S_g^{-1} - \frac{1}{2} \left[ k_g(\Zg,\Zg)^{-1} + \sum_{i=1}^P \beta_i w_i^2 \A_g(\oi,:)^T \A_g(\oi,:) \right].
\end{align}

%-------------------------------------------
%  derivatives of q(u_i)
\noindent As a function of the parameters of $q(\u_i)$, the lowerbound $\calL$ is:
\begin{align}
\nonumber
\calL_i \define&
 \log \Normal(\y_i; w_i \A_g(\oi,:) \m_g + \A_i \m_i, \beta_i^{-1} \I)
 - \frac{1}{2} \beta_i \trace \tilde{\K}_i(\oi,\oi)
 - \frac{1}{2} \beta_i \trace \S_i \A_i^T \A_i
 \\
  &- \frac{1}{2} \log |k_i(\Zi,\Zi) \S_i^{-1}| -\frac{1}{2} \trace k_i(\Zi,\Zi)^{-1} (\m_i \m_i^T + \S_i) ,
\end{align}

\noindent The derivatives of $\calL_i$ wrt $\m_i$ and $\S_i$ are given by:
\begin{align}
\deriv{\calL_i}{\m_i}
& = \beta_i \A_i^T (\y_i - w_i \A_g(\oi,:)^T \m_g) - \left[k_i(\Zi,\Zi)^{-1} +  \beta_i \A_i^T \A_i \right] \m_i \\
\deriv{\calL_i}{\S_i}
&= \frac{1}{2} \S_i^{-1} - \frac{1}{2} \left[ k_i(\Zi,\Zi)^{-1} + \beta_i \A_i^T \A_i \right].
\end{align}

% comment on computation
\noindent It can be seen that the derivatives of the parameters of $q(\u_i)$ only involve the observations of the output dimension $i$.
The derivatives of the parameters of $q(\u_g)$ involve the observations across all output dimensions but decompose as a sum of contributions from individual outputs.
Therefore, computation of the derivatives (and hence the update equations) can be distributed or parallelized easily.
This attractive property allows the model to scale to a very large number of outputs.

%\subsubsection{Update Equations}
% and comment on the intuition of the update equations: e.g. y(x) - g(x) for h(x)
\subsection{Prediction}
The predictive distribution of the $i$-th output for a test input $\x_*$ is
\begin{align}
p(\fstar | \y, \x_*) = \int \Normal(\fstar; w_i g_* + h_{i*}, 0) p(g_* | \y, \x_*) p(h_{i*} | \y, \x_*) \der g_* \der h_{i*},
\end{align}
where $p(g_* | \y, \x_*) = \Normal(g_*; \mu_{g*}, s_{g*})$ and $p(h_{i*} | \y, \x_*) = p(h_{i*}; \mu_{i*}, s_{i*})$ are the predictive distributions of the sparse GPs as given in \ref{eq:sorprediction}.
Therefore we have:
\begin{align}
p(\fstar | \y, \x_*) = \Normal(\fstar; w_i \mu_{g*} + \mu_{i*}, w_i^2 s_{g*} + s_{i*}).
\end{align}

%\begin{linenomath}
%\begin{align}
%\sum_{i,h} \lambda_i \lambda_h cov [f(\x, \x')]
%&= \sum_{i,h}   \sum_{j,j'} \lambda_i g(\x_i - \vs_j) k(\vs_j, \vs_{j'}) \lambda_h g(\x_h - \vs_j') \\
%\end{align}
%\end{linenomath}

\section{Toy Experiments}
The first toy experiment uses two identical outputs which are noisy version of the same version plus some noise: $y_1(x) = sin(x) + \epsilon$ and $y_2(x) = sin(x) + \epsilon$, $\epsilon \sim \Normal(0,0.01)$.
In this case the shared function $g(x)$ is $sin(x)$ and the independent processes $h_1(x)$ and $h_2(x)$ are just white noises.
Each output has missing values in one region of the input space.
The predictive distributions of the multiple-gp model compared to that of independent gps are shown in \ref{fig4}.

\noindent The second toy experiment uses similar setting as the first one, except that now $y_1(x) = sin(x) + \epsilon$ and $y_2(x) = -sin(x) + \epsilon$.
In this case the shared function is still $g(x) = sin(x)$, but the weights should be opposite i.e. $w_1 = 1$ and $w_2 = 1$.
The learning procedure was indeed able to recovered this relation and learned that $w_1 = 1.2$ and $w_2 = -1.3$.
The predictive distributions are shown in \ref{fig5}.

\begin{figure*}
\centering
\begin{tabular}{cc}
\includegraphics[scale=0.5]{figures/ssvi-y1.eps} &
\includegraphics[scale=0.5]{figures/ssvi-svi1.eps} \\
\includegraphics[scale=0.5]{figures/ssvi-y2.eps} &
\includegraphics[scale=0.5]{figures/ssvi-svi2.eps} \\
\multicolumn{2}{c}{\includegraphics[scale=0.5]{figures/ssvi-y1byg.eps} }
\end{tabular}
\label{fig4}
\caption{Predictive distributions of the multipe-output gps (left column) and independent gps (right column) for the first toy example. The predictive distribution by $g(x)$ for $y_1(x)$ is shown in the bottom figure.}
\end{figure*}

\begin{figure*}
\centering
\begin{tabular}{cc}
\includegraphics[scale=0.5]{figures/ssvi2-y1.eps} &
\includegraphics[scale=0.5]{figures/ssvi2-svi1.eps} \\
\includegraphics[scale=0.5]{figures/ssvi2-y2.eps} &
\includegraphics[scale=0.5]{figures/ssvi2-svi2.eps} \\
\multicolumn{2}{c}{\includegraphics[scale=0.5]{figures/ssvi-y1byg.eps} }
\end{tabular}
\label{fig5}
\caption{Predictive distributions of the multipe-output gps (left column) and independent gps (right column) for the second toy experiment. The predictive distribution by $g(x)$ for $y_1(x)$ is shown in the bottom figure.}
\end{figure*}

\section{Appendix}
Here we derive the gradients of the lower bound wrt the  hyperparameters.
As a template, consider the lower bound in Hensman et al (as a function of the hyperparameters):
\begin{align}
\nonumber
\calL
=& \log \Normal(\y; \K_{NM} \K_{MM}^{-1} \m, \beta^{-1}\I)
 - \frac{1}{2} \beta \trace \tilde{\K}
 - \frac{1}{2} \beta \trace (\S\K_{MM}^{-1} \K_{MN} \K_{NM} \K_{MM}^{-1}) \\  \nonumber
&- \frac{1}{2} \left( \log |\K_{MM}| + \trace(\K_{MM}^{-1}(\m \m^T + \S)) \right) \\ \nonumber
=& \underbrace{\log \Normal(\y; \A \m, \beta^{-1}\I)}_{\calL_1}
 - \underbrace{\frac{1}{2} \beta \trace (\K_{NN} - \A\K_{MN})}_{\calL_2}
 - \underbrace{\frac{1}{2} \beta \trace (\S\A^T\A)}_{\calL_3} \\
&- \underbrace{\frac{1}{2} \left( \log |\K_{MM}| + \trace(\K_{MM}^{-1}(\m \m^T + \S)) \right)}_{\calL_4},
\end{align}
where $\A = \K_{NM} \K_{MM}^{-1}$.
Notice that we have re-written the sum of individual terms in matrix form which will make the derivation and also computation easier.

\subsection{Derivative of the Noise Hyperparameter}
The derivative of the noise hyperparameter $\beta$ is easily computed as:
\begin{align}
\deriv{\calL}{\beta} = \frac{N}{2\beta} - \frac{1}{2} (\y - \A\m)^T (\y - \A\m) - \frac{\calL_2}{\beta} - \frac{\calL_3}{\beta}.
\end{align}
\subsection{Derivatives of the Covariance Hyperparameters}
To simplify the math, we utilize the matrix $\A$ defined above.
Firstly, the derivative of $\A$ wrt a covariance hyperparameter $t$ is given by:
\begin{align}
\deriv{\A}{t} = \left(\deriv{\K_{NM}}{t} - \A \deriv{\K_{MM}}{t}\right)\K_{MM}^{-1}.
\end{align}
The derivatives of $\calL_1, \calL_2, \calL_3 \text{ and } \calL_4$ are thus given by:
\begin{align}
\deriv{\calL_1}{t} &= \beta (\y - \A\m)^T \deriv{\A}{t} \m \\
\deriv{\calL_2}{t} &= \frac{1}{2}\beta \trace \left(\deriv{\K_{NN}}{t} - \A \deriv{\K_{MN}}{t} - \deriv{\A}{t} \K_{MN}\right) \\
\deriv{\calL_3}{t} &= \beta \trace \left(\A \S \deriv{\A^T}{t} \right) \\
\deriv{\calL_4}{t} &= \frac{1}{2}  \trace \left(\K_{MM}^{-1} \deriv{ \K_{MM}}{t}\right) - \frac{1}{2} \trace \left(\K_{MM}^{-1} \deriv{\K_{MM}}{t} \K_{MM}^{-1} (\m \m^T + \S) \right)
\end{align}
The derivatives are then computed by taking the derivatives of the covariance matrices $\K_{NN} (\text{the diagonal only}), \K_{NM} \text{ and }  \K_{MM}$, hence the covariance function, wrt the hyperparameters.

\subsection{Derivatives of the Inducing Inputs}
To compute the derivatives of $\calL$ wrt the inducing inputs, first notice that $\Z = \{\z_m\}_{m=1}^M$ are also parameters of the covariance matrices $\K_{NM}$ and $\K_{MM}$.
Hence the derivative wrt a single dimension of an inducing input, i.e. $z_{mj}$, is the same as that of $\deriv{ \calL}{t}$.

%Since $MD$ parameters are needed for the inducing inputs, it appears that the derivatives of all inducing inputs would require $\calO(MD \times M^3)$ in computation.
%However, this complexity can actually be reduced to $\calO(DM^3)$ using the following lemma. \\
%
%\noindent \textbf{Lemma} Let $A, B$ be two matrices of size $N \times M$ and $M \times N$, respectively. Furthermore, $B$ has the property that only one of its rows or columns is non-zero. The complexity of $\trace(AB)$ is only $\calO(N)$. \\
%
%\noindent \textbf{Proof} Let $m <= M$ be the non-zero row of $B$. We have
%\begin{align}
%\trace (AB) = \sum_{i=1}^N \sum_{j=1}^M A_{ij} B_{ji} = \sum_{i=1}^N A_{im} B_{mi},
%\end{align}
%which clearly takes $\calO(N)$. It is easy to see that the lemma also holds when $B$ is symmetric and only one of its row and the corresponding column is non-zero. \\
%
%\noindent To exploit the property in the Lemma, we re-write $\frac{d \calL_1}{dt}, \frac{d \calL_2}{dt}, \frac{d \calL_3}{dt}, \frac{d \calL_4}{dt}$ by expanding $\frac{d\A}{dt}$ (here $t = z_{mj}$):

\noindent We re-write $\deriv{\calL_1}{t}, \deriv{\calL_2}{t}, \deriv{ \calL_3}{t}, \deriv{\calL_4}{t}$ by expanding $\deriv{\A}{t}$ (here $t = z_{mj}$):
\begin{align}
\nonumber
% dL1
\deriv{\calL_1}{t}
 &= \beta \trace (\y - \A\m)^T \left(\deriv{\K_{NM}}{t} -  \A \deriv{ \K_{MM}}{t}\right)\K_{MM}^{-1} \m \\
&= \beta \trace \K_{MM}^{-1} \m (\y - \A\m)^T \deriv{\K_{NM}}{t}
-\beta \trace \K_{MM}^{-1} \m (\y - \A\m)^T \A \deriv{\K_{MM}}{t} \\
%dL2
\deriv{\calL_2}{t}
&= - \beta \trace \A^T \deriv{\K_{NM}}{t}
 + \frac{1}{2} \beta \trace \A^T \A \deriv{\K_{MM}}{t}  \\
% dL3
\deriv{\calL_3}{t}
&= \beta \trace \K_{MM}^{-1} \S \A^T \deriv{\K_{NM}}{t}
 - \beta \trace \K_{MM}^{-1} \S \A^T \A \deriv{\K_{MM}}{t}\\
% dL4
\deriv{\calL_4}{t}
 &= \frac{1}{2}  \trace \K_{MM}^{-1} \deriv{\K_{MM}}{t}
  - \frac{1}{2} \trace \K_{MM}^{-1} (\m \m^T + \S) \K_{MM}^{-1} \deriv{\K_{MM}}{t}
\end{align}
From the above equations we get,
\begin{align}
\deriv{\calL}{t} = \trace \D_1 \deriv{\K_{NM}}{t} + \trace \D_2 \deriv{ \K_{MM}}{t},
\end{align}
where
\begin{align}
\D_1 =& \beta \K_{MM}^{-1} \m (\y - \A\m)^T
 + \beta \A^T
 - \beta \K_{MM}^{-1} \S \A^T \\ \nonumber
\D_2 =& -\beta \trace \K_{MM}^{-1} \m (\y - \A\m)^T \A
 - \frac{1}{2} \beta \A^T \A
  + \beta \K_{MM}^{-1} \S \A^T \A	 \\
  &-\frac{1}{2} \K_{MM}^{-1} + \frac{1}{2} \K_{MM}^{-1} (\m \m^T + \S) \K_{MM}^{-1}
\end{align}
Notice that $\D_1$ and $\D_2$ can be pre-computed with a cost of $\calO(M^3)$ (or $\calO(N_bM^2)$ if the minibatch size $N_b > M$).
The computational cost of taking derivatives of $MD$ inducing parameters is thus $\calO(M^3 + MDM) = \calO(M^3)$ as the cost of the two $\trace$ operators is $\calO(M)$ due to the fact that only $\calO(M)$ elements of $\deriv{\K_{MM}}{t}$ or $\deriv{\K_{NM}}{t}$ are non-zero.
This fact can be further exploited to perform vectorized operation, for e.g. in Matlab, such that the iteration over all inducing inputs can be avoided.

\subsection{Analysis on Learning of the Inducing Inputs}
In this section we present some analysis on learning of the inducing inputs under the stochastic variational inference procedure on some toy problems.
We use three real-valued truth functions of scalar inputs: the first one was used in Snelson et al, the second one is a function $f(x) = \sin(x) + \cos(x)$, and the third one is a sample function generated from a GP with squared exponential with ARD covariance function with a lengthscale of 2 and signal variance of 1.

\noindent \paragraph{Experimental Settings} We use gpsvi to optimize for \textit{all} of the parameters in the model, i.e. the variational parameters of the posterior, the covariance hyperparameters, the noise hyperparameter, and the inducing inputs.
For the hyperparameters, we use a momentum of $0.9$ and a fixed learn rate of $1e-5$.
For the variational parameters, we use a learn rate of $0.01$ (no momentum was used).
We vary the learn rate of the inducing inputs from $1e{-2}$ to $1e{-6}$.
Using momentum for the inducing inputs does not seem to have much effects.
The maximum number of iterations is 500, the batch size 5, and the number of inducing values 10.

% effect of learn rate on inducing inputs
\noindent \paragraph{Effects of the Learn Rates }
In Figure \ref{fig1}, \ref{fig2}, and \ref{fig3}, we show the lower bounds, predictive distributions, and the learned inducing inputs using stochastic variational inference with varying learn rates for the inducing inputs.
We also show the results with FITC for comparison.
It can be seen from the figures that, as typical in stochastic optimization, the behavior of GPSVI is very sensitive to the learn rate.
In particular, when the rate is slow e.g. $1e{-5}$, almost no progress was made in learning the inducing inputs.
However, when the rate increases to e.g. $1e{-3}$, the inducing inputs are spread out more evenly in the input space compared to the initial locations.
Although not shown the in figures, when the learn rate is too large, e.g. $1e{-02}$, the inducing inputs may spread to regions far outside of the training intervals.
An example of this phenomenon can be observed in the top left figure in Figure \ref{fig1}.
The learn rates also affect the evidence lower bound which seems to wiggle more compared to not learning the inducing inputs.
The predictive distributions are sensible particularly because the toy functions exhibit sparsity and are easy to learn.  \\

\noindent The results of this qualitative analysis do not seem to suggest that learning of the inducing inputs does not work in GPSVI.
Its effectiveness perhaps rests upon empirical performance on real datasets.

\noindent \paragraph{The Noise Parameter} It seems harder to set the initial value and the learning rate of the noise parameter compared to standard GP.
While a large range of values of noise can be used in standard gp or fitc (e.g. 0.5), a very small value of noise $1e{-02}$, which is the true noise, is required for gpsvi to work well.
It appears that an adaptive learning rate may be required for the noise parameter.

\begin{figure*}
\centering
\begin{tabular}{ccc}
\includegraphics[scale=0.3]{figures/func1-svi-lrate1e-03.eps} &
\includegraphics[scale=0.3]{figures/func1-svi-lrate1e-04.eps} &
\includegraphics[scale=0.3]{figures/func1-svi-lrate1e-05.eps} \\
\includegraphics[scale=0.3]{figures/func1-svi-lrate1e-03-bound.eps} &
\includegraphics[scale=0.3]{figures/func1-svi-lrate1e-04-bound.eps} &
\includegraphics[scale=0.3]{figures/func1-svi-lrate1e-05-bound.eps} \\
(a) learn rate = $1e{-3}$ & (b) learn rate = $1e{-4}$ & (c) learn rate = $1e{-5}$ \\
\multicolumn{3}{c}{\includegraphics[scale=0.4]{figures/func1-fitc.eps}}
\end{tabular}
\caption{Predictive distributions and learned inducing inputs by GPSVI and FITC for the first function. Top row: GPSVI with different learning rates (the rates decreases from left to right). Bottom row: FITC. Magenta dots : training points. Solid blue line: predictive mean. Grey-shaded area and solid black lines: two standard deviations of the predictive distributions. Black (+) crosses: initial locations of inducing inputs. Magenta (+) crosses: learned locations of inducing inputs.  Middle row: the evidence lower bound of gpsvi vs. iteration.}
\label{fig1}
\end{figure*}

\begin{figure*}
\centering
\begin{tabular}{ccc}
\includegraphics[scale=0.3]{figures/func2-svi-lrate1e-03.eps} &
\includegraphics[scale=0.3]{figures/func2-svi-lrate1e-04.eps} &
\includegraphics[scale=0.3]{figures/func2-svi-lrate1e-05.eps} \\
\includegraphics[scale=0.3]{figures/func2-svi-lrate1e-03-bound.eps} &
\includegraphics[scale=0.3]{figures/func2-svi-lrate1e-04-bound.eps} &
\includegraphics[scale=0.3]{figures/func2-svi-lrate1e-05-bound.eps} \\
(a) learn rate = $1e{-3}$ & (b) learn rate = $1e{-4}$ & (c) learn rate = $1e{-5}$ \\
\multicolumn{3}{c}{\includegraphics[scale=0.4]{figures/func2-fitc.eps}}
\end{tabular}
\caption{Predictive distributions and learned inducing inputs by GPSVI and FITC for the second function. The legends are same as in Figure 1.}
\label{fig2}
\end{figure*}

\begin{figure*}
\centering
\begin{tabular}{ccc}
\includegraphics[scale=0.3]{figures/func3-svi-lrate1e-03.eps} &
\includegraphics[scale=0.3]{figures/func3-svi-lrate1e-04.eps} &
\includegraphics[scale=0.3]{figures/func3-svi-lrate1e-05.eps} \\
\includegraphics[scale=0.3]{figures/func3-svi-lrate1e-03-bound.eps} &
\includegraphics[scale=0.3]{figures/func3-svi-lrate1e-04-bound.eps} &
\includegraphics[scale=0.3]{figures/func3-svi-lrate1e-05-bound.eps} \\
(a) learn rate = $1e{-3}$ & (b) learn rate = $1e{-4}$ & (c) learn rate = $1e{-5}$ \\
\multicolumn{3}{c}{\includegraphics[scale=0.4]{figures/func3-fitc.eps}}
\end{tabular}
\caption{Predictive distributions and learned inducing inputs by GPSVI and FITC for the third function. The legends are same as in Figure 1.}
\label{fig3}
\end{figure*}

\end{document}


Make sure the \mintinline[]{text}{rico} directory is in your Matlab
path.

\begin{minted}[]{matlab}
addpath('../rico')
\end{minted}

\hypertarget{phasor-docs}{%
\section{\texorpdfstring{\mintinline[]{text}{phasor}
docs}{ docs}}\label{phasor-docs}}

The following is the source code.

\inputminted{matlab}{../rico/phasor.m}

\hypertarget{tf_factor-docs}{%
\section{\texorpdfstring{\mintinline[]{text}{tf_factor}
docs}{ docs}}\label{tf_factor-docs}}

The following is the source code.

\inputminted{matlab}{../rico/tf_factor.m}

\hypertarget{usage-and-examples}{%
\subsection{Usage and examples}\label{usage-and-examples}}

\hypertarget{bode_multi-docs}{%
\section{\texorpdfstring{\mintinline[]{text}{bode_multi}
docs}{ docs}}\label{bode_multi-docs}}

The following is the source code.

\inputminted{matlab}{../rico/bode_multi.m}

\hypertarget{usage-and-examples-1}{%
\subsection{Usage and examples}\label{usage-and-examples-1}}

\begin{minted}[]{matlab}
sys = tf([5,3,5],[1,6,1,20])
\end{minted}

\begin{minted}[]{text}
sys =

    5 s^2 + 3 s + 5
  --------------------
  s^3 + 6 s^2 + s + 20

Continuous-time transfer function.
\end{minted}

\begin{minted}[]{matlab}
sys_a = tf_factor(sys)
\end{minted}

\begin{minted}[]{text}
sys_a(:,:,1,1) =

          3.155
  ----------------------
  s^2 - 0.3399 s + 3.155

sys_a(:,:,2,1) =

    6.34
  --------
  s + 6.34

sys_a(:,:,3,1) =

  s^2 + 0.6 s + 1

sys_a(:,:,4,1) =

  0.25

4x1 array of continuous-time transfer functions.
\end{minted}

\begin{minted}[]{matlab}
% [f,ax_mag,ax_phase] = bode_multi(G); % get axis handles
f = bode_multi(sys_a);

hgsave(f,'figures/temp');
\end{minted}

\begin{figure}
\centering
\input{figures/bode_multi_docs.tex}
\caption{a bode multi example output.}
\label{fig:bode_multi_docs}
\end{figure}

\hypertarget{pole-docs}{%
\section{\texorpdfstring{\mintinline[]{text}{pole}
docs}{ docs}}\label{pole-docs}}

The following is the source code.

\inputminted{matlab}{../rico/pole.m}

\hypertarget{usage-and-examples-2}{%
\subsection{Usage and examples}\label{usage-and-examples-2}}

\hypertarget{zero-docs}{%
\section{\texorpdfstring{\mintinline[]{text}{zero}
docs}{ docs}}\label{zero-docs}}

The following is the source code.

\inputminted{matlab}{../rico/zero.m}

\hypertarget{usage-and-examples-3}{%
\subsection{Usage and examples}\label{usage-and-examples-3}}

\hypertarget{tf2latex-docs}{%
\section{\texorpdfstring{\mintinline[]{text}{tf2latex}
docs}{ docs}}\label{tf2latex-docs}}

The following is the source code.

\inputminted{matlab}{../rico/tf2latex.m}

\hypertarget{usage-and-examples-4}{%
\subsection{Usage and examples}\label{usage-and-examples-4}}


\documentclass{article}
\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{March 14, 2014}
\maketitle
very interesting use of laplace transforms. straight from the text
\section*{lesson 14 page 122}
duhamel's principle.

\subsubsection*{easy problem}
\begin{align*}
  \text{PDE}&&w_t&=w_{xx}\\
  \text{BC}&&w(0,t)&=0\\
  &&w(1,t)&=1\\
  \text{IC}&&w(x,0)&=0
\end{align*}
subsubsection*{hard problem}
\begin{align*}
  &&&&&&z&=u+v\\
  \text{PDE}&&u_t&=u_{xx}&v_t&=v_{xx}
  &z_t&=z_{xx}\\
  \text{BC}&&u(0,t)&=0&v(0,t)&=0
  &z(0,t)&=0\\
  &&u(1,t)&=g(t)&v(1,t)&=0
  &z(1,t)&=g(t)\\
  \text{IC}&&u(x,0)&=0&v(x,0)&=\phi(x)
  &z(x,0)&=\phi(x)\\
\end{align*}
$\mathcal{L}$ with respect to time
\begin{align*}
  sW(x,s)-\underbrace{w(x,0)}_{\to 0}&=W_{xx}(x,s)\\
  W_{xx}-SW&=0\text{ on }0<x<1&U_{xx}-sU&=0\\
  W(0,s)&=0 \text{ and } W(1,s)=\frac{1}{s}&U(0,s)&=0 \text{ and }U(1,s)=G(s)\\
  W&=c_1\sinh(\sqrt{s}x)+c_2\cosh(\sqrt{s}x)\\
  \frac{\mathrm{d}^2y}{\mathrm{d}x^2}-sy&=0&y&=e^{rx}\\
  &&r^2-s&=0&r&=\pm\sqrt{s}\\
  y&=a_1e^{-\sqrt{s}x}+a_2e^{\sqrt{s}x}\\
  \sinh(z)&=\frac{1}{2}(e^z-e^{-z})\\
  \cosh(z)&=\frac{1}{2}(e^z+e^{-z})\\
  W&=c_1\sinh(\sqrt{s}x)\\
  \frac{1}{s}&=c_1\sinh(\sqrt{s})\\
  W(x,s)&=\frac{1}{s}\frac{\sinh(\sqrt{s}x)}{\cosh(\sqrt{s})}\\
  U&=c_1\sinh(\sqrt{s}x)\\
  G(s)&=c_1\sinh(\sqrt{s})\\
  U(x,s)&=G(s)\frac{\sinh(\sqrt{s}x}{\sinh(\sqrt{s})}=G(s)sW(x,s)\\
  \text{note: }sW(x,s)-\underbrace{w(x,0)}_{\to 0}&=\mathcal{L}\{w_t\}\\
  u(x,t)&=\int_0^t{g(t-u)w_t(x,u)\,\mathrm{d}u}\\
  &=g(t-u)w(x,u)\mid_0^t-\int_0^t{(-g'(t-u)w(x,u)\,\mathrm{d}u}
  \intertext{page 107 (124)}
  w(x,t)&=x+\frac{2}{\pi}\sum\limits_{n=1}^\infty{\frac{(-1)^n}{n}e^{-(n\pi)^2t}\sin(n\pi x)}\text{ from eigenfunction expansion}
\end{align*}

homework \#27
lesson 14, exercise 4 $g(t)=\sin(t)$. take $\alpha^2=1$. due friday, 28 march.
\end{document}


\section{Estimator output}
\subsection{Estimator definition}
For simplicity, consider a local property $O(\bs{R})$, where $\bs{R}$ is the collection of all particle coordinates. An \textit{estimator} for $O(\bs{R}) $ is a weighted average over walkers:
\begin{align}
E[O] = \left(\sum\limits_{i=1}^{N^{tot}_{walker}} w_i O(\bs{R}_i) \right) / \left( \sum \limits_{i=1}^{N^{tot}_{walker}} w_i \right). \label{eq:estimator}
\end{align}
$N^{tot}_{walker}$ is the total number of walkers collected in the entire simulation. Notice that $N^{tot}_{walker}$ is typically far larger than the number of walkers held in memory at any given simulation step. $w_i$ is the weight of walker $i$.

In a VMC simulation, the weight of every walker is 1.0. Further, the number of walkers is constant at each step. Therefore, Equation~\ref{eq:estimator} simplifies to
\begin{align}
E_{VMC}[O] = \frac{1}{N_{step}N_{walker}^{ensemble}} \sum_{s,e} O(\bs{R}_{s,e})\:.
\end{align}
Each walker $\bs{R}_{s,e}$ is labeled by \textit{step index} s and \textit{ensemble index} e.

In a DMC simulation, the weight of each walker is different and may change from step to step. Further, the ensemble size varies from step to step. Therefore, Equation~\ref{eq:estimator} simplifies to
\begin{align}
E_{DMC}[O] = \frac{1}{N_{step}} \sum_{s} \left\{ \left(\sum_e w_{s,e} O(\bs{R}_{s,e})  \right) / \left( \sum \limits_{e} w_{s,e} \right)  \right\}\:.
\end{align}

We will refer to the average in the $\{\}$ as \textit{ensemble average} and to the remaining averages as \textit{block average}. The process of calculating $O(\bs{R})$ is \textit{evaluate}.

\subsection{Class relations}
A large number of classes are involved in the estimator collection process. They often have misleading class or method names. Check out the document gotchas in the following list:
\begin{enumerate}
\item \icode{EstimatorManager} is an unused copy of \icode{EstimatorManagerBase}. \icode{EstimatorManagerBase} is the class used in the QMC drivers. (PR \#371 explains this.)
\item \icode{EstimatorManagerBase::Estimators} is completely different from \icode{QMCDriver::Estimators}, which is subtly different from \icode{OperatorBase::Estimators}. The first is a list of pointers to \icode{ScalarEstimatorBase}. The second is the master estimator (one per MPI group). The third is the slave estimator that exists one per OpenMP thread.
\item \icode{QMCHamiltonian} is NOT a parent class of \icode{OperatorBase}. Instead, \icode{QMCHamiltonian} owns two lists of \icode{OperatorBase} named \icode{H} and \icode{auxH}.
\item \icode{QMCDriver::H} is NOT the same as \icode{QMCHamiltonian::H}. The first is a pointer to a \icode{QMCHamiltonian}. \icode{QMCHamiltonian::H} is a list.
\item \icode{EstimatorManager::stopBlock(std::vector)} is completely different from \icode{EstimatorManager::}
\icode{stopBlock(RealType)}, which is the same as \icode{stopBlock(RealType, true)} but that is subtly different from \icode{stopBlock(RealType, false)}. The first three methods are intended to be called by the master estimator, which exists one per MPI group. The last method is intended to be called by the slave estimator, which exists one per OpenMP thread.
\end{enumerate}

\subsection{Estimator output stages}
%In QMCPACK, evaluation is done by \icode{OperatorBase}; ensemble average is done either by a ``CloneDriver'' (e.g. \icode{VMCSingleOMP}, \icode{DMCOMP}) or \icode{ScalarEstimatorBase}; block average is done by \icode{ScalarEstimatorBase} or \icode{EstimatorManagerBase}. Walkers can be accessed by ``CloneDriver'' and \icode{OperatorBase} but not by \icode{EstimatorManagerBase} or \icode{ScalarEstimatorBase}. Output files can be accessed by the latter two classes but not the former two. Therefore, in order to output estimators to file, data must be transferred from \textit{evaluate} classes to \textit{average} classes.

Estimators take four conceptual stages to propagate to the output files: evaluate, load ensemble, unload ensemble, and collect. They are easier to understand in reverse order.

\subsubsection{Collect stage}
File output is performed by the master \icode{EstimatorManager} owned by \icode{QMCDriver}. The first 8+ entries in \icode{EstimatorManagerBase::AverageCache} will be written to \icode{scalar.dat}. The remaining entries in \icode{AverageCache} will be written to \icode{stat.h5}. File writing is triggered by \icode{EstimatorManagerBase}\\ \icode{::collectBlockAverages} inside \icode{EstimatorManagerBase::stopBlock}.

\begin{lstlisting}
// In EstimatorManagerBase.cpp::collectBlockAverages
  if(Archive)
  {
    *Archive << std::setw(10) << RecordCount;
    int maxobjs=std::min(BlockAverages.size(),max4ascii);
    for(int j=0; j<maxobjs; j++)
      *Archive << std::setw(FieldWidth) << AverageCache[j];
    for(int j=0; j<PropertyCache.size(); j++)
      *Archive << std::setw(FieldWidth) << PropertyCache[j];
    *Archive << std::endl;
    for(int o=0; o<h5desc.size(); ++o)
      h5desc[o]->write(AverageCache.data(),SquaredAverageCache.data());
    H5Fflush(h_file,H5F_SCOPE_LOCAL);
  }
\end{lstlisting}

\icode{EstimatorManagerBase::collectBlockAverages} is triggered from the master-thread estimator via either \icode{stopBlock(std::vector)} or \icode{stopBlock(RealType, true)}. Notice that file writing is NOT triggered by the slave-thread estimator method \icode{stopBlock(RealType, false)}.

\begin{lstlisting}
// In EstimatorManagerBase.cpp
void EstimatorManagerBase::stopBlock(RealType accept, bool collectall)
{
  //take block averages and update properties per block
  PropertyCache[weightInd]=BlockWeight;
  PropertyCache[cpuInd] = MyTimer.elapsed();
  PropertyCache[acceptInd] = accept;
  for(int i=0; i<Estimators.size(); i++)
    Estimators[i]->takeBlockAverage(AverageCache.begin(),SquaredAverageCache.begin());
  if(Collectables)
  {
    Collectables->takeBlockAverage(AverageCache.begin(),SquaredAverageCache.begin());
  }
  if(collectall)
    collectBlockAverages(1);
}
\end{lstlisting}

\begin{lstlisting}
// In ScalarEstimatorBase.h
template<typename IT>
inline void takeBlockAverage(IT first, IT first_sq)
{
  first += FirstIndex;
  first_sq += FirstIndex;
  for(int i=0; i<scalars.size(); i++)
  {
    *first++ = scalars[i].mean();
    *first_sq++ = scalars[i].mean2();
    scalars_saved[i]=scalars[i]; //save current block
    scalars[i].clear();
  }
}
\end{lstlisting}

At the collect stage, \icode{ScalarEstimatorBase::scalars} must be populated with ensemble-averaged data. Two derived classes of \icode{ScalarEstimatorBase} are crucial: \icode{LocalEnergyEstimator} will carry \icode{Properties}, where as \icode{CollectablesEstimator} will carry \icode{Collectables}.

\subsubsection{Unload ensemble stage}
\icode{LocalEnergyEstimator::scalars} are populated by
\icode{ScalarEstimatorBase::accumulate}, whereas
\icode{CollectablesEstimator::scalars} are populated by
\icode{CollectablesEstimator::} \icode{accumulate_all}. Both
accumulate methods are triggered by
\icode{EstimatorManagerBase::accumulate}. One confusing aspect about
the unload stage is that \icode{EstimatorManagerBase::accumulate} has
a master and a slave call signature. A slave estimator such as
\icode{QMCUpdateBase::Estimators} should unload a subset of
walkers. Thus, the slave estimator should call
\icode{accumulate(W,it,it_end)}. However, the master estimator, such
as \icode{SimpleFixedNodeBranch::myEstimator}, should unload data from
the entire walker ensemble. This is achieved by calling
\icode{accumulate(W)}.

\begin{lstlisting}
void EstimatorManagerBase::accumulate(MCWalkerConfiguration& W)
{ // intended to be called by master estimator only
  BlockWeight += W.getActiveWalkers();
  RealType norm=1.0/W.getGlobalNumWalkers();
  for(int i=0; i< Estimators.size(); i++)
    Estimators[i]->accumulate(W,W.begin(),W.end(),norm);
  if(Collectables)//collectables are normalized by QMC drivers
    Collectables->accumulate_all(W.Collectables,1.0);
}
\end{lstlisting}

\begin{lstlisting}
void EstimatorManagerBase::accumulate(MCWalkerConfiguration& W
 , MCWalkerConfiguration::iterator it
 , MCWalkerConfiguration::iterator it_end)
{ // intended to be called slaveEstimator only
  BlockWeight += it_end-it;
  RealType norm=1.0/W.getGlobalNumWalkers();
  for(int i=0; i< Estimators.size(); i++)
    Estimators[i]->accumulate(W,it,it_end,norm);
  if(Collectables)
    Collectables->accumulate_all(W.Collectables,1.0);
}
\end{lstlisting}

\begin{lstlisting}
// In LocalEnergyEstimator.h
inline void accumulate(const Walker_t& awalker, RealType wgt)
{ // ensemble average W.Properties
  // expect ePtr to be W.Properties; expect wgt = 1/GlobalNumberOfWalkers
  const RealType* restrict ePtr = awalker.getPropertyBase();
  RealType wwght= wgt* awalker.Weight;
  scalars[0](ePtr[LOCALENERGY],wwght);
  scalars[1](ePtr[LOCALENERGY]*ePtr[LOCALENERGY],wwght);
  scalars[2](ePtr[LOCALPOTENTIAL],wwght);
  for(int target=3, source=FirstHamiltonian; target<scalars.size(); ++target, ++source)
    scalars[target](ePtr[source],wwght);
}
\end{lstlisting}

\begin{lstlisting}
// In CollectablesEstimator.h
inline void accumulate_all(const MCWalkerConfiguration::Buffer_t& data, RealType wgt)
{ // ensemble average W.Collectables
  // expect data to be W.Collectables; expect wgt = 1.0
  for(int i=0; i<data.size(); ++i)
    scalars[i](data[i], wgt);
}
\end{lstlisting}

At the unload ensemble stage, the data structures \icode{Properties} and \icode{Collectables} must be populated by appropriately normalized values so that the ensemble average can be correctly taken. \icode{QMCDriver} is responsible for the correct loading of data onto the walker ensemble.

\subsubsection{Load ensemble stage}
\icode{Properties} in the MC ensemble of walkers \icode{QMCDriver::W} is populated by \icode{QMCHamiltonian}\\ \icode{::saveProperties}. The master \icode{QMCHamiltonian::LocalEnergy}, \icode{::KineticEnergy}, and \icode{::Observables} must be properly populated at the end of the evaluate stage.
\begin{lstlisting}
// In QMCHamiltonian.h
  template<class IT>
  inline
  void saveProperty(IT first)
  { // expect first to be W.Properties
    first[LOCALPOTENTIAL]= LocalEnergy-KineticEnergy;
    copy(Observables.begin(),Observables.end(),first+myIndex);
  }
\end{lstlisting}

\icode{Collectables}'s load stage is combined with its evaluate stage.

\subsubsection{Evaluate stage}

The master \icode{QMCHamiltonian::Observables} is populated by slave \icode{OperatorBase}
\icode{::setObservables}. However, the call signature must be \icode{OperatorBase::setObservables}
\icode{(QMCHamiltonian::} \\\icode{Observables)}. This call signature is enforced by \icode{QMCHamiltonian::evaluate} and \icode{QMCHamiltonian::} \\\icode{auxHevaluate}.

\begin{lstlisting}
// In QMCHamiltonian.cpp
QMCHamiltonian::Return_t
QMCHamiltonian::evaluate(ParticleSet& P)
{
  LocalEnergy = 0.0;
  for(int i=0; i<H.size(); ++i)
  {
    myTimers[i]->start();
    LocalEnergy += H[i]->evaluate(P);
    H[i]->setObservables(Observables);
#if !defined(REMOVE_TRACEMANAGER)
    H[i]->collect_scalar_traces();
#endif
    myTimers[i]->stop();
    H[i]->setParticlePropertyList(P.PropertyList,myIndex);
  }
  KineticEnergy=H[0]->Value;
  P.PropertyList[LOCALENERGY]=LocalEnergy;
  P.PropertyList[LOCALPOTENTIAL]=LocalEnergy-KineticEnergy;
  // auxHevaluate(P);
  return LocalEnergy;
}
\end{lstlisting}

\begin{lstlisting}
// In QMCHamiltonian.cpp
void QMCHamiltonian::auxHevaluate(ParticleSet& P, Walker_t& ThisWalker)
{
#if !defined(REMOVE_TRACEMANAGER)
  collect_walker_traces(ThisWalker,P.current_step);
#endif
  for(int i=0; i<auxH.size(); ++i)
  {
    auxH[i]->setHistories(ThisWalker);
    RealType sink = auxH[i]->evaluate(P);
    auxH[i]->setObservables(Observables);
#if !defined(REMOVE_TRACEMANAGER)
    auxH[i]->collect_scalar_traces();
#endif
    auxH[i]->setParticlePropertyList(P.PropertyList,myIndex);
  }
}
\end{lstlisting}

\subsection{Estimator use cases}

\subsubsection{VMCSingleOMP pseudo code}
\begin{lstlisting}
bool VMCSingleOMP::run()
{
  masterEstimator->start(nBlocks);
  for (int ip=0; ip<NumThreads; ++ip)
    Movers[ip]->startRun(nBlocks,false);  // slaveEstimator->start(blocks, record)

  do // block
  {
    #pragma omp parallel
    {
      Movers[ip]->startBlock(nSteps);  // slaveEstimator->startBlock(steps)
      RealType cnorm = 1.0/static_cast<RealType>(wPerNode[ip+1]-wPerNode[ip]);
      do // step
      {
        wClones[ip]->resetCollectables();
        Movers[ip]->advanceWalkers(wit, wit_end, recompute);
        wClones[ip]->Collectables *= cnorm;
        Movers[ip]->accumulate(wit, wit_end);
      } // end step
      Movers[ip]->stopBlock(false);  // slaveEstimator->stopBlock(acc, false)
    } // end omp
    masterEstimator->stopBlock(estimatorClones);  // write files
  } // end block
  masterEstimator->stop(estimatorClones);
}
\end{lstlisting}

\subsubsection{DMCOMP  pseudo code}
\begin{lstlisting}
bool DMCOMP::run()
{
  masterEstimator->setCollectionMode(true);

  masterEstimator->start(nBlocks);
  for(int ip=0; ip<NumThreads; ip++)
    Movers[ip]->startRun(nBlocks,false);  // slaveEstimator->start(blocks, record)

  do // block
  {
    masterEstimator->startBlock(nSteps);
    for(int ip=0; ip<NumThreads; ip++)
      Movers[ip]->startBlock(nSteps);  // slaveEstimator->startBlock(steps)

    do // step
    {
      #pragma omp parallel
      {
      wClones[ip]->resetCollectables();
      // advanceWalkers
      } // end omp

      //branchEngine->branch
      { // In WalkerControlMPI.cpp::branch
      wgt_inv=WalkerController->NumContexts/WalkerController->EnsembleProperty.Weight;
      walkers.Collectables *= wgt_inv;
      slaveEstimator->accumulate(walkers);
      }
      masterEstimator->stopBlock(acc)  // write files
    }  // end for step
  }  // end for block

  masterEstimator->stop();
}
\end{lstlisting}

\subsection{Summary}

Two ensemble-level data structures, \icode{ParticleSet::Properties} and \icode{::Collectables}, serve as intermediaries between evaluate classes and output classes to \icode{scalar.dat} and \icode{stat.h5}. \icode{Properties} appears in both \icode{scalar.dat} and \icode{stat.h5}, whereas \icode{Collectables} appears only in \icode{stat.h5}. \icode{Properties} is overwritten by \icode{QMCHamiltonian::Observables} at the end of each step. \icode{QMCHamiltonian::Observables} is filled upon call to \icode{QMCHamiltonian::evaluate} and \icode{::auxHevaluate}. \icode{Collectables} is zeroed at the beginning of each step and accumulated upon call to \icode{::auxHevaluate}.

Data are output to \icode{scalar.dat} in four stages: evaluate, load, unload, and collect. In the evaluate stage, \icode{QMCHamiltonian::Observables} is populated by a list of \icode{OperatorBase}. In the load stage, \icode{QMCHamiltonian::Observables} is transfered to \icode{Properties} by \icode{QMCDriver}. In the unload stage, \icode{Properties} is copied to \icode{LocalEnergyEstimator::scalars}. In the collect stage, \icode{LocalEnergyEstimator::scalars} is block-averaged to \icode{EstimatorManagerBase}\\ \icode{::AverageCache} and dumped to file. For \icode{Collectables}, the evaluate and load stages are combined in a call to \icode{QMCHamiltonian::auxHevaluate}. In the unload stage, \icode{Collectables} is copied to \icode{CollectablesEstimator::scalars}. In the collect stage, \icode{CollectablesEstimator}\\ \icode{::scalars} is block-averaged to \icode{EstimatorManagerBase::AverageCache} and dumped to file.

\subsection{Appendix: dmc.dat}

\begin{sloppypar}
There is an additional data structure, \icode{ParticleSet::EnsembleProperty}, that is managed by \icode{WalkerControlBase::EnsembleProperty} and directly dumped to \icode{dmc.dat} via its own averaging procedure. \icode{dmc.dat} is written by \icode{WalkerControlBase::measureProperties}, which is called by \icode{WalkerControlBase::branch}, which is called by \icode{SimpleFixedNodeBranch}\\ \icode{::branch}, for example.
\end{sloppypar}


\documentclass{article}

%% preamble
\usepackage{hyperref}
\usepackage{verbatim}
\usepackage{color}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{cancel}

\topmargin 0pt
\advance \topmargin by -\headheight
\advance \topmargin by -\headsep
\textheight 9.in
\oddsidemargin 0pt
\evensidemargin \oddsidemargin
\marginparwidth 0.5in
\textwidth 6.5in
\newcommand{\myhrule}{ \begin{center}\rule{.9\linewidth}{.25mm}\end{center} }
\definecolor{darkgray}{rgb}{0.95,0.95,0.95}
\definecolor{heavygray}{rgb}{0.05,0.05,0.05}
\definecolor{foo}{rgb}{.8,0,.8}
\newcommand{\pad}{\vspace{8pt}\noindent}
\newcommand{\red}[1]{{\color{red}#1\color{black}}}
\newcommand{\myhref}[2]{\href{#1}{\color{foo}\underline{#2}\color{black}}}

\newcommand{\Mod}[1]{\ (\mathrm{mod}\ #1)}

\begin{document}

\title{CMDA 3634 Spring 2018 Homework 01}

%% change this to your name
\author{Weichen Li}
\vspace{-64pt}\maketitle
\begin{center}\underline{You must complete the following task by 5pm on Tuesday 02/06/18.}\end{center}
Your write up for this homework should be presented in a {\LaTeX} formatted PDF document. You may copy the \LaTeX{} used to prepare this report as follows

\begin{enumerate}
\item Sign up for a \myhref{http://sharelatex.com}{http://sharelatex.com} account.
\item Click on this  \myhref{https://www.sharelatex.com/read/jgjxnrcskhbc}{link}
\item Click on Menu/Copy Project.
\item Modify the HW01.tex document to respond to the following questions.
\item Remember: click the Recompile button to rebuild the document when you have made edits.
\item Remember: Change the author.

\end{enumerate}

\pad \emph{Each student} must individually upload the following files to the CMDA 3634 Canvas page at \myhref{https://canvas.vt.edu}{https://canvas.vt.edu}

\begin{enumerate}
\item \verb|firstnameLastnameHW01.tex| {\LaTeX} file.
\item Any figure files to be included by \verb|firstnameLastnameHW01.tex| file.
\item \verb|firstnameLastnameHW01.pdf| PDF file.
\item All source code ({\verb .c }) files.
\end{enumerate}

\pad You must complete this assignment on your own.

\vspace{16pt}
\begin{center}
\underline{\bf 100 points will be awarded for a successful completion.}
\vspace{8pt}\underline{\bf Extra credit will be awarded as appropriate.}
\end{center}

\newpage

\section*{ElGamal Public-key Cyptography}

Suppose there are two individuals, lets call them Alice and Bob, who wish to hold a conversation where only they can read the contents of each of their messages. That is, no outside eavesdropper can discern what the contents of each message is. Let's call Eve one such potential eavesdropper. For such a system, Alice and Bob need to agree on a way to encrypt and decrypt their messages in a completely public way such that Eve cannot decyrpt their messages.

Let consider just the case of Bob sending an encrypted message to Alice. A possible cryptographic system that Alice and Bob could agree on is a {\em public-key cryptographic system}. Such a system would contain the following steps.
\begin{enumerate}
\item Alice chooses in secret a {\em secret key} and publicly broadcasts a {\em public key}.
\item Bob uses the public key to encrypt his message and sends to Alice.
\item Alice uses her secret key to decrypt Bob's message.
\end{enumerate}
In this process, Eve has access to the public key that was broadcast by Alice. She can also see the encrypted message send to Alice by Bob. The cryptographic system should therefore be such that
\begin{itemize}
\item Encryption is easy using the public key.
\item Decryption is easy using the secret key.
\item Finding the secret key is {\em really hard}.
\end{itemize}
The public-key cryptographic system that we will consider in this class is the ElGamal encryption. We begin by introducing some concepts regarding integers modulo prime numbers.

\pad {\bf Integers Modulo Primes}:
While ElGamal encryption can work using more exotic mathematical structures, we will focus on integers modulo some prime $p$. We define the set $\mathbb{Z}_p$ to be the following numbers,
\[
\mathbb{Z}_p = \{1,2,3,\ldots,p-1\},
\]
for some prime number $p$. Since $p$ is prime, notice that $\mathbb{Z}_p$ is closed under multiplication modulo $p$. That is,
\[
\forall  a, b\in \mathbb{Z}_p, \qquad ab\Mod{p} \in \mathbb{Z}_p.
\]
In particular, the product $ab$ cannot be zero. We'll drop the $\Mod{p}$ in products from now on, but note that all products are assumed to be modulo $p$ so that all numbers remain in the set $\mathbb{Z}_p$.

A less obvious property of $\mathbb{Z}_p$ is that there will exist at least one number $g$ in $\mathbb{Z}_p$ such that
\[
\mathbb{Z}_p = \left\{g,g^2,g^3,\ldots,g^{p-1}\right\}.
\]
We call such a number a {\em generator} of $\mathbb{Z}_p$.

Another less-than-obvious property is that for each number $a$ in $\mathbb{Z}_p$, we have that $a^{p-1} = 1\Mod{p}$. This fact is a consequence of a theorem known as Cauchy's Theorem in a field of mathematics called Group Theory. It is especially useful when we notice that, consequently, every number $a$ in $\mathbb{Z}_p$ has a multiplicative `inverse', denoted $a^{-1}$ in $\mathbb{Z}_p$, such that $aa^{-1} = 1$. In particular, $a^{-1} = a^{p-2}$.

\vspace*{0.5em}

\pad{\bf Key Generation}:
The key generation for ElGamal encryption works as follows.
\begin{enumerate}
\item Alice chooses prime number $p$.
\item Alice selects a generator $g$ of the group $\mathbb{Z}_p$.
\item Alice chooses, in secret, a number $x$ in $\mathbb{Z}_p$ and computes $h=g^x$.
\item Alice publishes the set $(p,g,h)$ as her public key and retains $x$ as her secret key.
\end{enumerate}

\pad{\bf Encryption}:
The ElGamal encryption for a message $m$ works as follows. We'll suppose for simplicity that the message $m$ is already a number in $\mathbb{Z}_p$ and consider how to cast actual message text into numbers in $\mathbb{Z}_p$ later.
\begin{enumerate}
\item Bob chooses a random $y$ in $\mathbb{Z}_p$.
\item Bob computes $a = g^y$ and $s = h^y$.
\item Bob computes the cyphertext $\hat{m}$ as $\hat{m} = ms$.
\item Bob sends the pair $(a,\hat{m})$ to Alice.
\end{enumerate}

\pad{\bf Decryption}:
The ElGamal decryption of the cyphertext $\hat{m}$ works as follows.
\begin{enumerate}
\item Alice uses her secret key $x$ to compute the shared secret $s = a^x = g^{xy}$.
\item Alice computes $s^{-1} = s^{p-2}$.
\item Alice decrypts the intended message as $\hat{m}s^{-1} = m ss^{-1} = m$.
\end{enumerate}

\pad{\bf Security}:
The security of the ElGamal encryption algorithm relies on the problem of finding the secret key $x$ being difficult. Since Eve knows both $g$ and $h$ from Alice's public key, this amounts to her finding $x$ such that
\[
g^x = h\Mod{p}.
\]
This problem is known as the {\em discrete logarithm problem} and is considered to be computationally intractable for large prime numbers $p$. Indeed, our goal for the assignments in this class will be to implement our own ElGamal cryptographic system and determine how big the prime number $p$ must be before we simply will not have the computational power to crack it.

%\myhrule
\section*{Questions}

\noindent{\bf Q1}(5 points) Show that 2, 6, 7, and 11 are all generators of $\mathbb{Z}_{13}$.
\begin{align}
    \mathbb{Z}_{13} &= \{1,2,3,\ldots,12\} \\
    2: \{2, 2^2, 2^3, \ldots, 2^{12}\} &= \{2, 4, 8, \ldots, 1\} \in \mathbb{Z}_{13} \\
    6: \{6, 6^2, 6^3, \ldots, 6^{12}\} &= \{6, 10, 8 \ldots, 1\} \in \mathbb{Z}_{13} \\
    7:  \{7, 7^2, 7^3, \ldots, 7^{12}\} &= \{7, 10, 5, \ldots, 1\} \in \mathbb{Z}_{13} \\
    11:  \{11, 11^2, 11^3, \ldots, 11^{12}\} &= \{11, 4, 5, \ldots, 1\} \in \mathbb{Z}_{13}
\end {align}
\vspace*{1em}

\noindent{\bf Q2}(5 points) What is $6^{-1}$ in $\mathbb{Z}_{7}$?
\begin{align}
    6*6^{-1} &= 1 \\
    6^{-1} &= 6^5 \\
    6^{-1} &= 6
\end{align}
\vspace*{1em}

\noindent{\bf Q3.1}(5 points) Given an ElGamal public-key of $(11,2,3)$, that is the prime $p = 11$, the generator $g = 2$, and $h = 3$, encrypt the message $m = 9$ using $y = 2$.
\begin{align}
    a &= g^y = 2^2 = 4 \\
    s &= h^y = 3^2 = 9 \\
    \hat{m} &= ms = 4 \\
    (a, \hat{m}) &= (4, 4)
\end{align}
\vspace*{1em}

\noindent{\bf Q3.2}(10 points) Given that the secret key in the ElGamal public key in Q3.1 is $x=8$, decrypt the cyphertext you computed in Q3.1 and confirm that you recover the original message.
\begin{align}
    s &= a^x = 4^8 = 9 = g^{xy} = 2^{8y} = 2^{16} \\
    s^{-1} &= s^{p - 2} = 9^9 = 5 \\
    \hat{m}s^{-1} &= mss^{-1} = m \\
    m &= 4 \times 5 = 9
\end{align}
\vspace*{1em}

\noindent{\bf Q4.1}(20 points) In order to write a C program to perform an ElGamal encryption, we'll need a few simple functions to help out with prime numbers. One such function is the greatest common divisor (GCD) of two numbers. This function inputs two numbers $a$ and $b$ and returns the greatest integer $z$ such that $z$ divides both $a$ and $b$. For example $GCD(15,25) = 5$ since 5 divides both 15 and 25.

Without loss of generality let's assume $a\geq b$. The algorithm to compute the GCD of $a$ and $b$ can be written compactly as
\[
GCD(a,b) = \begin{cases}
GCD(b, a\Mod{b}) & \mathrm{if}\; b\neq 0, \\
a & \mathrm{if}\; b = 0. \\
\end{cases}
\]
Write a C program {\verb gcd.c } which replicates the following functionality.

\begin{verbatim}
    >./gcd
    Enter the first number: 15
    Enter the second number: 25
    The greatest common divisor of 15 and 25 is 5.
\end{verbatim}
That is, your program should read in two values from the user and print the greatest common divisor of the two values. To read in values use the {\verb scanf } function.

HINT: In C it is legal for functions to call themselves, i.e. make recursive functions.

\vspace*{1em}
\noindent{\bf Q4.2}(5 points) Another useful function is the least common multiple (LCM) of two numbers. This function inputs two numbers $a$ and $b$ and returns the smallest number $z$ such that both $a$ and $b$ divide $z$. For example $LCM(32,20) = 160$.

The algorithm to compute the LCM of $a$ and $b$ can be written using the GCD function as follows
\[
LCM(a,b) = \frac{ab}{GCD(a,b)}.
\]
Write a C program {\verb lcm.c } which replicates the following functionality.
\begin{verbatim}
    >./lcm
    Enter the first number: 32
    Enter the second number: 20
    The least common multiple of 32 and 20 is 160.
\end{verbatim}

\vspace*{1em}
\noindent{\bf Q4.3}(5 points) Two numbers $a$ and $b$ are considered coprime if the only positive number which divides both $a$ and $b$ is 1, i.e. $GCD(a,b) =1$. Write a C program {\verb isCoprime.c } which replicates the following functionality,

\begin{verbatim}
    >./isCoprime
    Enter the first number: 16
    Enter the second number: 9
    16 and 9 are coprime.
\end{verbatim}
and
\begin{verbatim}
    >./isCoprime
    Enter the first number: 32
    Enter the second number: 12
    32 and 12 are not coprime.
\end{verbatim}

\vspace*{1em}
\noindent{\bf Q5}(20 points) To generate public keys in the ElGamal encryption we'll need a way to select prime numbers. Write a C program {\verb isPrime.c } which replicates the following functionality,
\begin{verbatim}
    >./isPrime
    Enter a number: 32
    32 is not prime.
\end{verbatim}
and
\begin{verbatim}
    >./isPrime
    Enter a number: 17
    17 is prime.
\end{verbatim}
There are many sophisticated ways to accomplish this but we are only interested in whether your program can correctly operate for the first several hundred prime numbers. Don't worry about being inefficient for now.
\vspace*{1em}

\noindent{\bf Q6}(25 points) Once we have a prime number, the next task is to find a generator of $\mathbb{Z}_p$. If you experiment with some other numbers in Q1 you may notice that if $a$ is not a generator of $\mathbb{Z}_p$ then there will exist a number $r$, where $0<r<p-1$, such that $a^r = 1$. Therefore, for a number $g$ to be a generator of $\mathbb{Z}_p$ it must satisfy
\[
g^r \neq 1, \quad \forall 0<r<p-1.
\]
Write a C program {\verb findGenerator.c } which replicates the following functionality,
\begin{verbatim}
    >./findGenerator
    Enter a prime number: 11
    2 is a generator of Z_11.
\end{verbatim}

\vspace*{1em}
\noindent{\bf Bonus}(20 points) We are going to need to examine each part of our code to make sure we don't run into trouble when using very large integers. For each of your codes in this assignment, give some comments on whether you will encounter issues when your inputs become very large numbers. Will your program seem to `hang' due to computational load? Or fail to function correctly at all?
\begin{verbatim}
    >./gcd
    Enter the first number: 2
    Enter the second number: 222222222222222222222222222
    The greatest common divisor of -1 and 2 is -1.
\end{verbatim}
gcd fails to function.
\begin{verbatim}
    >./lcm
    Enter the first number: 5
    Enter the second number: 5555555555555555555555555555
    The least common multiple of 5 and -1 is 5.
\end{verbatim}
lcm has a similar error comparing to gcd.
\begin{verbatim}
    >./isCoprime
    Enter the first number: 31364
    Enter the second number: 1346234623465
    1909859817 and 31364 are coprime.
\end{verbatim}
isCoprime fails as well.
\begin{verbatim}
    >./isPrime
    Enter a number: 2835768726358764758
    -809063210 is prime.
\end{verbatim}
isPrime fails to function when the input gets very large. It uses a for loop to check if the number can be divided by from 2 to half of itself, and the efficiency is pretty low. Also when the input gets large.
\begin{verbatim}
    >./findGenerator
    Enter a prime number: 28374234567293845
    1277705109 is not prime. You have to enter a prime number
    >./findGenerator
    Enter a prime number: 2384523423453245
    530387005 is not prime. You have to enter a prime number
    >./findGenerator
    Enter a prime number: 1273461782634871263478
\end{verbatim}
findGenerator involves isPrime to check if the input is a prime number, so it is similar to the previous one. If I remove the check and enters a large number, the program hangs.
%\myhrule

%\begin{thebibliography}{9}
%\bibitem{gol} Wikipedia -- description of the Game of Life
%\url{https://en.wikipedia.org/wiki/Conway's_Game_of_Life}

%\end{thebibliography}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%               This is the LaTeX2e file for                       %%
%%                                                                  %%
%%           Small exotic 4--manifolds with $b_2^+=3$               %%
%%                                                                  %%
%%                          by                                      %%
%%           Andras I. Stipsicz and  Zoltan Szabo                   %%
%%                      Dec   2004                                  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[11pt]{gtart}
\usepackage{amsmath, amssymb, graphicx, epsfig, verbatim}
\usepackage{epic}

\newtheorem{thm}{Theorem}[section]
\newtheorem{mthm}[thm]{Main Theorem}
\newtheorem{cor}[thm]{Corollary}
\newtheorem{lem}[thm]{Lemma}
\newtheorem{prop}[thm]{Proposition}

\theoremstyle{definition}
\newtheorem{defn}[thm]{Definition}
\newtheorem{conj}[thm]{Conjecture}
\newtheorem{exa}[thm]{Example}
%\theoremstyle{remark}
\newtheorem{rem}[thm]{Remark}
\newtheorem{rems}[thm]{Remarks}

\numberwithin{equation}{section}

\mathsurround=1pt
\setlength{\parindent}{0em}
\setlength{\parskip}{1.2ex}

%
%   MACROS
%

%
%  Greek letters
%

\newcommand{\al}{\alpha}
\newcommand{\be}{\beta}
\newcommand{\ga}{\gamma}
\newcommand{\Ga}{\Gamma}
\newcommand{\De}{\Delta}
\newcommand{\de}{\delta}
\newcommand{\ep}{\epsilon}
\newcommand{\et}{\eta}
\newcommand{\Fi}{\Phi}
\newcommand{\ka}{\kappa}
\newcommand{\La}{\Lambda}
\newcommand{\la}{\lambda}
\newcommand{\Om}{\Omega}
\newcommand{\om}{\omega}
\newcommand{\ro}{\rho}
\newcommand{\si}{\sigma}
\newcommand{\Si}{\Sigma}
\renewcommand{\th}{\theta}
\newcommand{\Th}{\Theta}
\newcommand{\Up}{\Upsilon}
\newcommand{\va}{\varphi}
\newcommand{\csi}{\xi}
\newcommand{\ze}{\zeta}
\newcommand{\bfn}{{\mathbb {N}}}
\newcommand{\bfz}{{\mathbb {Z}}}
\newcommand{\J}{{\mathcal {J}}}
\newcommand{\ot}{{\overline {\t}}}
\newcommand{\os}{{\overline {\s}}}
\newcommand{\oa}{{\overline {a}}}
\newcommand{\ob}{{\overline {b}}}

%
%    Various operators
%

\newcommand{\x}{\times}
\newcommand{\s}{\mathbf s}
\renewcommand{\u}{\mathbf u}
\renewcommand{\t}{\mathbf t}
\newcommand{\z}{\mathbf z}
\renewcommand{\L}{{\mathfrak {L}}}
\newcommand{\D}{\mathbf D}
\newcommand{\DD}{\mathcal D}
\newcommand{\EE}{\mathcal E}
\newcommand{\MM}{\mathcal M}
\newcommand{\GG}{\mathcal G}
\newcommand{\FF}{\mathcal F}
\newcommand{\C}{\mathbb C}
\newcommand{\Z}{\mathbb Z}
\newcommand{\N}{\mathbb N}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\bfr}{\mathbb R}
\newcommand{\RP}{{\mathbb R}{\mathbb P}}
\newcommand{\CP}{{\mathbb C}{\mathbb P}}
\newcommand{\cpkk}{{\overline {{\mathbb C}{\mathbb P}^2}}}
\newcommand{\cpk}{{\mathbb {CP}}^2}
\newcommand{\cphat}{{\mathbb {CP}}^2\# 6{\overline {{\mathbb C}{\mathbb P}^2}}}
\newcommand{\cphet}{{\mathbb {CP}}^2\# 7{\overline {{\mathbb C}{\mathbb P}^2}}}
\newcommand{\cpnyolc}{{\mathbb {CP}}^2\# 8{\overline {{\mathbb C}{\mathbb P}^2}}}
\newcommand{\eegy}{{\mathbb {CP}}^2\# 9{\overline {{\mathbb C}{\mathbb P}^2}}}
\newcommand{\ra}{\rightarrow}
\newcommand{\del}{\partial}
\newcommand{\hra}{\hookrightarrow}
\newcommand{\lra}{\longrightarrow}
\newcommand{\hf}{{{\widehat {HF}}}}
\newcommand{\cf}{{\widehat {CF}}}
\newcommand{\Li}{\mathbb {L}}

\DeclareMathOperator{\Hom}{Hom}
\DeclareMathOperator{\tb}{tb}
\DeclareMathOperator{\rot}{rot}

\DeclareMathOperator{\SW}{SW}
\DeclareMathOperator{\Tor}{Tor}
\DeclareMathOperator{\PD}{PD}
\DeclareMathOperator{\Spin}{Spin}
\DeclareMathOperator{\Id}{Id}
\DeclareMathOperator{\End}{End}
\DeclareMathOperator{\Fr}{Fr}
%
%   End macros
%

\begin{document}

\title{ Small exotic 4--manifolds with $b_2^+=3$}

\author{Andr\'{a}s I. Stipsicz}
\address{R\'enyi Institute of Mathematics\\
Hungarian Academy of Sciences\\
H-1053 Budapest\\
Re\'altanoda utca 13--15, Hungary and\\
Institute for Advanced Study, Princeton, NJ}

\email{stipsicz@renyi.hu, stipsicz@math.ias.edu}

\secondauthor{Zolt\'an Szab\'o}
\secondaddress{Department of Mathematics\\
Princeton University,\\
 Princeton, NJ, 08540}
\secondemail{szabo@math.princeton.edu}

\begin{abstract}
We construct an infinite  family of simply connected,
pairwise nondiffeomorphic  4--manifolds,
all homeomorphic to $3\cpk \# 9 \cpkk$.
Similar ideas provide examples of 4--manifolds  with $b_2^+=3$,
$b_2^-=8$, vanishing first homology and nontrivial Seiberg--Witten
invariants.
\end{abstract}

\maketitle

\section{Introduction}
Many simply connected, smoothable topological 4--manifolds (with
$b_2^+$ odd) are known to admit infintely many distinct smooth
structures. The existence of such \emph{exotic} structures are,
however, less clear when the Euler characteristic of the 4--manifold
is small, for example if the manifold is homeomorphic to the (blow--up
of the) complex projective plane. Using the rational blow--down
construction \cite{FS1} together with knot surgery \cite{FSknot} in
double node neighborhoods \cite{FSuj}, many new smooth 4--manifolds
have been discovered with $b_2^+=1$ and $b_2^-\geq 5$ \cite{FSuj, P,
PSS, SS}. Similar ideas can be applied to get examples of irreducible
exotic simply connected 4--manifolds with $b_2^+=3$ and relatively
small $b_2^-$.  The study of exotic structures on simply connected
manifolds with $b_2^+=3$ has a rich history. Recall that the $K3$
surface $E(2)$ is such an example with $b_2^-=19$. Applying
appropriate logarithmic transformations on $E(2)$ it was shown that
$3\cpk \# 19 \cpkk$ admits infinitely many smooth structures
\cite{FMbook, MO, SSz}. Later works, relying on Gompf's symplectic
normal connected sum operation, together with Donaldson theory showed
that the topological manifolds $3\cpk \# n \cpkk$ with $n\geq 14$
admit infinitely many smooth structures \cite{Screll, Sztop}. More
recent results of Park \cite{Doug1, Doug2, Doug3} proved the same
statement with $n\geq 10$. Our main result in this paper improves this
bound:

\begin{thm} \label{t:m1}
The simply connected topological 4--manifold $3\cpk \# 9 \cpkk$ admits
infinitely many distinct smooth structures.
\end{thm}

By modifying the construction used in the proof of Theorem~\ref{t:m1},
we can define a set of 4--manifolds which still have $b_2^+=3$, but
their Euler characteristic is smaller than the above examples. In
these cases, however, we were unable to show that the manifolds are
simply connected.

\begin{thm} \label{t:m2}
There are infinitely many pairwise nondiffeomorphic smooth, closed
4--manifolds with vanishing first homology, $b_2^+=3$, $b_2^-=8$
and nontrivial Seiberg--Witten invariants.
\end{thm}

The proof of the results will involve two steps. First we desribe how
to construct the manifolds claimed and then we use Seiberg--Witten
theory in proving that they are nondiffeomorphic. In the construction
we will apply mapping class group arguments and the theory of
Lefschetz fibrations.  Using the knot surgery construction then we can
identify configurations of curves in the resulting 4--manifolds which
can be rationally blown down, leading us to the desired examples.

\section{Elliptic fibrations and mapping class groups}

Recall that a genus--1 Lefschetz fibration $f\colon X^4\to S^2$ can be
described by the word in the mapping class group $\Gamma _1$ of the
2--torus $T^2$ corresponding to the monodromy presentation of
$f$. More precisely, a point $x\in X$ where $df$ is not onto (called a
singular point of the fibration) gives rise to a singular fiber
$f^{-1}(f(x))$, and the monodromy of the fibration around such a fiber
can be given by the composition of Dehn twists along the circles
corresponding to the vanishing cycles of the singular points of the
fiber. By traversing through the singular fibers in a counterclockwise
manner relative to a fixed base point $s_0\in S^2$, we get the above
mentioned word describing the fibration.  Notice that we do not assume
that $f$ is injective on the set of its singular points, that is, a
singular fiber can contain more than one singular points.  The
assumption that the map $f$ is a Lefschetz fibration implies that the
vanishing cycles corresponding to the singular points in one fixed
singular fiber can be chosen to be disjoint.

It is known that the mapping class group $\Gamma _1$ can be generated by
two elements $a,b\in \Gamma _1$ which are subject to the two relations
\[
aba=bab \qquad {\mbox{ and }} \qquad (ab)^6=1.
\]
In fact, $\Gamma _1$ can be shown to be isomorphic to $SL(2; \bfz )$
by mapping $a$ to
$\left(\begin{smallmatrix}
 1 & 1 \\
 0 & 1
\end{smallmatrix}\right)$ and $b$ to
$\left(\begin{smallmatrix}
 1 & 0 \\
 -1 & 1
\end{smallmatrix}\right)$.
Since the forgetful map from the mapping class group $\Gamma _1 ^1$ of the
2--torus with one marked point to $\Gamma _1$ is an isomorphism, we get that
any genus--1 Lefschetz fibration admits a section.

Genus--1 Lefschetz fibrations were classified by Moishezon
\cite{Mois}, who showed that after a possible perturbation such a
fibration over $S^2$ is equivalent to one of the fibrations given by
the words $(ab)^{6n}$ ($n\in \bfn$) in $\Gamma _1$. The resulting
4--manifold is usually called $E(n)$ (the simply connected elliptic
surface with section and of holomorphic Euler characteristic $n$), and
$E(2)$ is the famous $K3$ surface.  It can be shown that a section of
$E(n)\to S^2$ has self--intersection $-n$.

Following \cite{HKK} we call a fiber with monodromy conjugate to $a^k$
of \emph{type $I_{k}$} ($k\in \bfn$).  When $k=1$, the corresponding
fiber is also called a \emph{fishtail} fiber.  It is easy to see that
topologically a singular fiber of type $I_k$ ($k\geq 2$) is a plumbing
of $k$ smooth 2--spheres of self--intersection $-2$ plumbed along a
circle (see \cite[page 35]{HKK}), while a fishtail fiber is an
immersed 2--sphere with one positive double point.  Since in $T^2$
nonisotopic simple closed curves necessarily intersect each other, it is
easy to see that a genus--1 Lefschetz fibration can have only
$I_k$--fibers as singular fibers. The fibration $f$ can be perturbed
near a singular fiber $F$ of type $I_k$ into a fibration $f'$ which is
the same as $f$ outside of $F$ but breaks $F$ into two singular fibers
of types $I_{k_1}$ and $I_{k_2}$ with $k_1+k_2=k$.  This fact implies
that a generic genus--1 Lefschetz fibration admits only fishtail
fibers. In our study however, we will find it most helpful to
understand what kind of other singular fibers an elliptic fibration
can admit.

\section{The construction for $b_2^-=9$}

We start with a proposition showing the existence of a particular
genus--1 fibration on $E(2)$.

\begin{prop}\label{p:mcg}
There exists an elliptic Lefschetz fibration on the $K3$ surface
$E(2)$ with a section, a singular fiber $F$ of type $I_{16}$, three
singular fibers $F_1, F_2,F_3$ of type $I_2$ and two further fishtail
fibers.
\end{prop}
\begin{proof}
It is not hard to see that the word $(ab)^{12}$ (defining a genus--1
Lefschetz fibration on the $K3$ surface $E(2)$) in the mapping class
group $\Gamma _1$ of the torus is equivalent to
\[
a^4ba^2b^2a^2b^2a^4ba^2b^2a^2.
\]
(Alternatively, by substituting $a$ and $b$ with the matrices they
correspond to under the map $\Gamma _1 \to SL(2; \bfz )$, we can check
that the above product is equal to the identity matrix.)  By
collecting the powers of $a$ in the front using conjugation, we get
$a^{16}$ followed by the product of three squares of some conjugates
of $b$ and two further conjugates of $b$. Since a conjugate of $b$ by
a word $x\in \Gamma _1$ corresponds to the Dehn twist along the image
under the diffeomorphism $x$ of the curve inducing $b$, the
proposition follows.  As we already mentioned, genus--1 Lefschetz
fibrations always admit sections.
\end{proof}

Perturb first the above fibration near the $I_2$ fibers $F_i$
($i=1,2,3$) in a way that these give rise to fishtail fibers
$F_i', F_i ''$ with isotopic vanishing cycles. Let us denote the
resulting Lefschetz fibration by $f\colon E(2) \to S^2$.

%In order to prove Theorem~\ref{t:m1}, we will apply a further
%perturbation of $f$ near the $I_{16}$--fiber $F$, resulting a map
%$f'\colon E(2)\to S^2$ with a type $I_{15}$ fiber and nine fishtails,
%with three pairs $F_i', F_i''$ admitting isotopic vanishing cycles. We
%will use this fibration in the proof of Theorem~\ref{t:m1}.

Let $K_1,K_2,K_3$ be three twist knots as depicted in \cite{FSuj}.
Let $Z_{K_1,K_2,K_3}$ denote the 4--manifold we get after performing
three knot surgeries with knots $K_1,K_2,K_3$ along three regular
fibers in the fibration on the $K3$ surface found above.  Because of
the existence of fishtail fibers in the complement with nonisotopic
vanishing cycles, we conclude that $\pi _1(Z_{K_1,K_2,K_2})=1$. If we
perform the surgeries in the double node neighborhoods near the
fishtail fibers $F_1',F_1''$, and $F_2',F_2''$ and $F_3',F_3''$
respectively, then we can find a 'pseudo--section' $S$ as in
\cite{FSuj} which is an immersed sphere of self--intersection $(-2)$
with three positive double points, intersecting the further fishtail
fibers and the $I_{16}$ fiber $F$ transversally. Next smooth the
intersections of this pesudo--section $S$ with two further fishtail
fibers. The result is an immersed sphere of self--intersection 2
having 5 positive double points. Now blow up the 4--manifold
$Z_{K_1,K_2,K_3}$ in the five double points of this sphere, and find
an embedded sphere of self--intersection $-18$ in $Z_{K_1,K_2,K_3}\# 5
\cpkk$. Let $C$ denote the tubular neighborhood of the linear plumbing
of spheres given by this $(-18)$--sphere together with 14 of the
$(-2)$--spheres in the $I_{16}$ fiber $F$. It is not hard to see that
$C$ is diffeomorphic to $C_{16,1}$ in the notation of \cite{SS},
cf. also \cite{FS1, Pratb}.  It is then easy to show that $\partial
C=\partial C_{16,1}$ can be given as the oriented boundary of the
rational ball $B_{16,1}$, see \cite{CH, FS1, SS}.  Define
$X_{K_1,K_2,K_3}$ as the rational blow--down of $Z_{K_1,K_2,K_3}\# 5
\cpkk$ along $C$, that is,
\[
X_{K_1,K_2,K_3}=(Z_{K_1,K_2,K_3}\# 5\cpkk -{\mbox { int }}C)\cup B_{16,1}.
\]

\begin{prop}\label{p:hom}
For any twist knots $K_1,K_2,K_3$ the 4--manifold $X_{K_1,K_2,K_3}$ is
homeomorphic to $3\cpk \# 9 \cpkk$.
\end{prop}
\begin{proof}
Notice that $Z_{K_1,K_2,K_3}$ is simply connected, and the complement
of $C$ in $Z_{K_1,K_2,K_3} \# 5 \cpkk$ is simply connected since the
$(-2)$--sphere in $F$ intersecting the last $(-2)$--sphere of the
linear chain $C$ provides a hemisphere which contracts the generator
of the fundamental group of $\partial C$.  Since $\pi _1 (\partial
B_{16,1})$ surjects onto $\pi _1 (B_{16,1})$ under the natural
embedding, Van Kampen's Theorem implies that $X_{K_1,K_2,K_3}$ is
simply connected. Now simple signature and Euler characteristics
computation together with Freedman's Theorem \cite{Fr} verifies the
result.
\end{proof}

For short, let $X_n$ denote the 4--manifold $X_{K_1,K_2,K_3}$ if
$K_1=K_2=K_3=$ the $n$--twist knot $T_n$. The following proposition is
true in a wider generality, we restrict our attention to the special
case $K_1=K_2=K_3$ in order to keep our discussion as simple as possible.
Using the results of \cite{FS1, FSknot} the Seiberg--Witten invariants
of $X_n$ can be easily computed. This computation immediately shows

\begin{thm}\label{t:sw}
There are two cohomology classes $\pm L\in H^2 (X_n ; \bfz )$ with the
property that the Seiberg--Witten function $SW_{X_n}$ of $X_n$ is
equal to zero for all classes $K\neq \pm L$ and $SW_{X_n}(\pm L)=\pm
n^3$.
\end{thm}
\begin{proof}
By \cite[Theorem~1.1]{FSknot} the Seiberg--Witten invariant of
$Z_n=Z_{T_n,T_n,T_n}$ can be computed to be equal to
\[
(n\exp (2T)-(2n-1)+n\exp (-2T))^3
\]
(use the facts that the Seiberg--Witten function of the $K3$ surface
is equal to 1 and the Alexander polynomial of $T_n$ is $\Delta
_{T_n}=nt-(2n-1)+nt^{-1}$). This result, together with the blow--up
formula shows that the five--fold blow--up $Z_n \# 5 \cpkk$ has
exactly two Seiberg--Witten basic classes $\pm K$ which evaluate on
the $(-18)$--sphere of the configuration $C$ as $\pm 16$. Moreover,
the value of the Seiberg--Witten function on these basic classes is
$\pm n^3$.  Now \cite[Theorem~8.5]{FS1} implies that $X_n$ has two
basic classes, on which the value of the Seiberg--Witten function is
equal to $\pm n^3$, verifying the result.
\end{proof}

\begin{cor}\label{c:nondiffeo}
The 4--manifolds $X_n$ are pairwise nondiffeomorphic irreducible smooth
4--manifolds. \qed
\end{cor}

\begin{proof}[Proof of Theorem~\ref{t:m1}]
The 4--manifolds $X_n$ provide an infinite family of smooth
4--manifolds all homeomorphic to $3\cpk \# 9 \cpkk$ according to
Proposition~\ref{p:hom}, and by Corollary~\ref{c:nondiffeo} these
manifolds are pairwise nondiffeomorphic. Therefore the set $\{ X_n
\mid n \in \bfn \}$ provides an infinite collection of distinct
smooth structures on $3\cpk \# 9 \cpkk$, hence the proof is complete.
\end{proof}

\subsection{4--manifolds with $b_2^+=3$ and $b_2^-=8$}
Using a variation of the above procedure, closed 4--manifolds with
$b_2^+=3$ and $b_2^-=8$ can be constructed as follows. Consider the
fibration $f\colon E(2) \to S^2$ found above, containing a singular
fiber of type $I_{16}$ and eight fishtail fibers, out of which three
pairs $F_i', F_i''$ ($i=1,2,3$) have isotopic vanishing cycles.
Proceed as before by doing three knot surgeries along three regular
fibers with twist knots $K_1,K_2,K_3$ and --- using the double node
neighborhoods provided by the fishtail fibers $F_i',F_i''$ ---
identify the 'pseudo--section', which is again an immersed sphere
with homological square $-2$ and has three positive double points. As
before, resolve the two positive intersections of this
pseudo--section with the remaining two fishtail fibers, and find the
immersed sphere with 5 double point and homological square 2. Blow up
the 4--manifold at the double points of the pesudo--section, and
consider the resulting sphere of square $-18$ in
$Z_{K_1,K_2,K_3}$. This sphere intersects the $I_{16}$ fiber $F$
transversally in a unique point $P$ which is on the $(-2)$--sphere
$\Sigma _0 \subset F$. Now $\Sigma _0$ is intersected by two other
spheres in $F$, let $\Sigma _1$ be one of them and denote the
intersection point of $\Sigma _0$ and $\Sigma _1$ by $Q$. Apply 17
infinitely close blow--ups at $Q$. The resulting plumbing manifold
$C_{305,17}$ can be given by the linear plumbing
\[
(-18,-19,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-3,-2,-2,
\]
\[
-2,-2,-2,-2,-2,-2, -2,-2,-2,-2,-2,-2,-2,-2).
\]
As before, it is routine to see that the boundary
$\partial C_{305,17}$ can be given as the boundary of the rational
ball $B_{305,17}$, hence we can blow it down, resulting the
4--manifold $Y_{K_1,K_2,K_3}$. Since the normal circle of the
pseudo--section (which circle generates the first homology
of $\partial C_{305,17}$) vanishes in the homology of the
complement $Z_{K_1,K_2,K_3}\#22\cpkk - {\mbox { int }}C_{305,17}$
(as it is shown by a regular fiber), the Mayer--Vietoris sequence
implies that $H_1(Y_{K_1,K_2,K_3}; \bfz )$ vanishes. Simple
Euler characteristic and signature computations now imply
\begin{lem}
The 4--manifolds $Y_{K_1,K_2,K_3}$ have $b_2^+=3$ and $b_2^-=8$. \qed
\end{lem}

Finally, by computing Seiberg--Witten invariants of these manifolds in
the fashion it was done in Theorem~\ref{t:sw}, we get that the
4--manifolds $Y_n=Y_{T_n, T_n,T_n}$ are pairwise nondiffeomorphic, all
with nonvanishing Seiberg--Witten invariants, verifying the claim of
Theorem~\ref{t:m2}.

\begin{thebibliography}[AAA]

%\bibitem{Bar}
%{\bf R. Barlow},
%{\it A simply connected surface of general type with $p_g=0$},
%Invent. Math.  {\bf79} (1985), 293--301.

%\bibitem{BK}
%{\bf V. Braungardt and D. Kotschick},
%{\it Clustering of critical points in Lefschetz fibrations and the
%symplectic Szpiro inequality}, Trans. AMS {\bf355} (2003), 3217--3228.

\bibitem{CH}
{\bf A. Casson and J. Harer},
{\it Some homology lens spaces which bound rational homology balls},
Pacific J. Math. {\bf96} (1981), 23--36.

%\bibitem{D}
%{\bf S. Donaldson},
%{\it An application of gauge theory to four dimensional topology},
%J. Diff. Geom. {\bf18} (1983), 279--315.

%\bibitem{D1}
%{\bf S. Donaldson},
%{\it Irrationality and the h-cobordism conjecture}, J. Diff. Geom.
%{\bf 26} (1987), 141-168.

\bibitem{FS1} {\bf R. Fintushel and R. Stern},
{\it Rational blowdowns of smooth 4--manifolds}, J. Diff. Geom. {\bf46}
(1997), 181--235.

\bibitem{FSknot} {\bf R. Fintushel and R. Stern},
{\it Knots, links and 4--manifolds}, Invent. Math. {\bf134}
(1998), 363--400.

\bibitem{FSuj} {\bf R. Fintushel and R. Stern},
{\it Double node neighborhoods and families of simply connected
4--manifolds with $b^+=1$}, arXiv:math.GT/0412126

\bibitem{Fr} {\bf M. Freedman},
{\it The topology of four--dimensional manifolds},
J. Diff. Geom. {\bf17} (1982), 357--453.

%\bibitem{FM} {\bf R. Friedman and J. Morgan}, {\it On the
%diffeomorphism types of certain algebraic surfaces I., II.}
%J. Diff. Geom.  {\bf27} (1988),  297--369. and 371--398.

\bibitem{FMbook} {\bf R. Friedman and J. Morgan},
{\it Smooth 4--manifolds and complex surfaces}, Ergebnisse der Mathematik und
ihrer Grenzgebiete {\bf27}, Springer--Verlag, 1994.

%\bibitem{GS}
%{\bf R. Gompf and A. Stipsicz},
%{\it 4--manifolds and Kirby calculus},
%AMS Grad. Studies in Math. {\bf20}, 1999.

\bibitem{HKK} {\bf J. Harer, A. Kas and R. Kirby},
{\it Handlebody decompositions of complex surfaces},
Memoirs of the AMS, vol. {\bf62}, 1986.

%\bibitem{Kod}
%{\bf K. Kodaira},
%{\it On compact analytic surfaces: II},
%Ann. Math. {\bf77} (1963), 563--626.

%\bibitem{Kot}
%{\bf D. Kotschick}, {\it On manifolds homeomorphic to $\cpnyolc$},
%Invent. Math. {\bf95} (1989), 591--600.

\bibitem{Mois} {\bf B. Moishezon},
{\it Complex surfaces and connected sums of complex
projective planes}, Lecture Notes in Mathematics {\bf603}, Springer--Verlag,
1977.

\bibitem{MO}
{\bf J. Morgan and K. O'Grady}, {\it Differential topology
of complex surfaces. Elliptic surfaces with $p_g=1$: smooth
classification}, Lecture Notes in Mathematics {\bf1545},
Springer-Verlag 1993.

\bibitem{Doug1} {\bf D. Park},
{\it Exotic smooth structures on $3\cpk \# n \cpkk$}, Proc. Amer. Math. Soc.
{\bf128} (2000), 3057--3065.

\bibitem{Doug2} {\bf D. Park},
{\it Exotic smooth structures on $3\cpk \# n \cpkk$, Part II},
Proc. Amer. Math. Soc. {\bf128} (2000), 3067--3073.

\bibitem{Doug3} {\bf D. Park},
{\it Constructing infinitely many smooth structures on $3\cpk \# n \cpkk$}
Math. Ann. {\bf322} (2000), 267--278.

\bibitem{Pratb} {\bf J. Park},
{\it Seiberg--Witten invariants of generalized rational blow--downs},
Bull. Austral. Math. Soc. {\bf56} (1997), 363--384.

\bibitem{P} {\bf J. Park},
{\it Simply connected symplectic 4--manifolds with $b_2^+=1$ and $c^2_1=2$},
to appear in Invent. Math., arXiv:math.GT/0311395

\bibitem{PSS} {\bf J. Park, A. Stipsicz and Z. Szab\'o},
{\it Exotic smooth structures on $\cpk \# 5 \cpkk$}, arXiv.math:GT/0412216

\bibitem{Screll}
{\bf A. Stipsicz},
{\it Donaldson series and $(-1)$--tori}, J. Reine Angew. Math. {\bf465}
(1995), 133--144.

\bibitem{SSz} {\bf A. Stipsicz and Z. Szab\'o},
{\it The smooth classification of elliptic surfaces with $b_2^+>1$},
Duke Math. J. {\bf75} (1994), 1--50.

\bibitem{SS} {\bf A. Stipsicz and Z. Szab\'o},
{\it An exotic smooth structure on $\cphat$}, arXiv.math:GT/0411258

%\bibitem{Sym} {\bf M. Symington},
%{\it Generalized symplectic rational blowdowns},
%Algebr. Geom. Topol. {\bf1} (2001), 503--518.

\bibitem{Sztop} {\bf Z. Szab\'o},
{\it Irreducible four--manifolds with small Euler characteristics},
Topology {\bf35} (1996), 411--426.

%{\it Exotic 4--manifolds with $b_2^+=1$}, Math. Res. Letters {\bf3}
%(1996), 731--741.
\end{thebibliography}
\end{document}


We study the loop version of the factorial function.

\[
	\begin{array}{l@{\hspace{0.3em}}c@{\hspace{1em}}l}
	\hline
		l_1 & : & \mathtt{x := 10} \\
		l_2 & : & \mathtt{f := 1} \\
		l_3 & : & \mathtt{\textbf{while } (x\geq 1) \textbf{ do }} \\
		l_4 & : & \mathtt{\;\;f = f*x} \\
		l_5 & : & \mathtt{\;\;x=x-1} \\
		l_6 & : & \mathtt{\textbf{end while}}\\
		l_7 & : & \mathtt{\cdots}\\
	\hline
	\end{array}
\]
\label{simple:example}

We seek to prove two formulae.

\[\varphi_1 \equiv (l_5 \orcond l_4 \to \mathtt{x}\geq 1) \;\; \wedge \;\; \varphi_2 \equiv \mathtt{x} \geq 0\]

First, we reduce the program to its \VC.

\[
	\begin{array}{l}
		 \tau_1 \equiv\pc(T) = l_1 \andcond \pc\prime (T) = l_2 \andcond \mathtt{f\prime =f} \andcond \mathtt{x}\prime  = 10\\
		 \tau_2 \equiv\pc(T) = l_2 \andcond \pc\prime (T) = l_3 \andcond \mathtt{f}\prime  = 1 \andcond x\prime =x\\
		 \tau_3 \equiv\pc(T) = l_3 \andcond \pc\prime (T) = l_4 \andcond \mathtt{f\prime =f} \andcond x\prime \geq 1\\
		 \tau_4 \equiv\pc(T) = l_4 \andcond \pc\prime (T) = l_5 \andcond \mathtt{f}\prime  = \mathtt{f*x} \andcond \mathtt{x\prime =x}\\
		 \tau_5 \equiv\pc(T) = l_5 \andcond \pc\prime (T) = l_3 \andcond \mathtt{f\prime =f} \andcond \mathtt{x\prime =x-1}\\
		 \tau_6 \equiv\pc(T) = l_3 \andcond \pc\prime (T) = l_7 \andcond \mathtt{f\prime =f} \andcond \mathtt{x<1}
	\end{array}
\]

We need to prove, for $i=1,2$:

\[
	\left\{
		\begin{array}{l}
			\tau_1 \andcond \varphi_i \to \varphi_i\prime  \\
			\tau_2 \andcond \varphi_i \to \varphi_i\prime \\
			\tau_3 \andcond \varphi_i \to \varphi_i\prime  \\
			\tau_4 \andcond \varphi_i \to \varphi_i\prime \\
			\tau_5 \andcond \varphi_i \to \varphi_i\prime  \\
			\tau_6 \andcond \varphi_i \to \varphi_i\prime
		\end{array}
	\right.
\]

\begin{center}\rule{4cm}{0.4pt}  $\varphi_1$  \rule{4cm}{0.4pt}\end{center}

	 $\tau_1 \andcond \varphi_1 \to \varphi_1\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{align*}
		(
			\underbrace{\pc(T) = l_1 \andcond \pc\prime (T) = l_2 \andcond \mathtt{f\prime =f} \andcond \mathtt{x}\prime  = 10}_{\tau_1} \andcond (\underbrace{[\pc(T) = l_5 \orcond \pc(T) = l_4 ] \to \mathtt{x}\geq 1}_{\varphi_1})
		) \\
				\to(\underbrace{[\pc\prime (T) = l_5 \orcond \pc\prime(T) = l_4] \to \mathtt{x}\prime  \geq 1}_{\varphi_1\prime })\;\;\;\;
	\end{align*}
%	\end{dmath*}

	The formula is valid because $\pc(T) = l_2 \neq l_5 \andcond l_2 \neq l_4$ thus the $\varphi\prime _1$ is true.

	 $\tau_2 \andcond \varphi_1 \to \varphi_1\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{align*}
		(
			\underbrace{\pc(T) = l_2 \andcond \pc\prime (T) = l_3 \andcond \mathtt{f}\prime  = 1 \andcond x\prime =x}_{\tau_2} \andcond (\underbrace{[\pc(T) = l_5 \orcond \pc(T) = l_4 ] \to \mathtt{x}\geq 1}_{\varphi_1})
		) \\
			\to(\underbrace{[\pc\prime (T) = l_5 \orcond \pc\prime(T) = l_4] \to \mathtt{x}\prime  \geq 1}_{\varphi_1\prime })\;\;\;\;
	\end{align*}
%	\end{dmath*}

	The formula is valid because $\pc(T) = l_3 \neq l_5 \andcond l_2 \neq l_4$ thus the $\varphi\prime _1$ is true.

	 $\tau_3 \andcond \varphi_1 \to \varphi_1\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{align*}
		(
			\underbrace{\pc(T) = l_3 \andcond \pc\prime (T) = l_4 \andcond \mathtt{f\prime =f} \andcond \textcolor{orange}{x\prime \geq 1}}_{\tau_3} \andcond (\underbrace{[\pc(T) = l_5 \orcond \pc(T) = l_4 ] \to \mathtt{x}\geq 1}_{\varphi_1})
		) \\
			\to(\underbrace{[\pc\prime (T) = l_5 \orcond \pc\prime(T) = l_4] \to \mathtt{x}\prime  \geq 1}_{\varphi_1\prime })\;\;\;\;
	\end{align*}
%	\end{dmath*}

		The formula is valid because $x\prime \geq 1$ thus the $\varphi\prime _1$ is true.

	 \;$\tau_4 \andcond \varphi_1 \to \varphi_1\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{align*}
		(
			\underbrace{\pc(T) = l_4 \andcond \pc\prime (T) = l_5 \andcond \mathtt{f}\prime  = \mathtt{f*x} \andcond \mathtt{x\prime =x}}_{\tau_4} \andcond (\underbrace{[\pc(T) = l_5 \orcond \pc(T) = l_4 ] \to \mathtt{x}\geq 1}_{\varphi_1})
		) \\
			\to(\underbrace{[\pc\prime (T) = l_5 \orcond \pc\prime(T) = l_4] \to \mathtt{x}\prime  \geq 1}_{\varphi_1\prime })\;\;\;\;
	\end{align*}
%	\end{dmath*}

	The formula is equivalent (applying resolution) to

%	\begin{dmath*}[indentstep={0em}]
	\begin{align*}
		(
			\mathtt{x\prime =x} \andcond  \mathtt{x}\geq 1
		)
		\to (\mathtt{x}\prime \geq 1)
	\end{align*}
%	\end{dmath*}

	Which is valid because of equality congruence.

	 $\tau_5 \andcond \varphi_1 \to \varphi_1\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{align*}
		(
			\underbrace{[\pc(T) = l_5 \orcond \pc(T) = l_4 ]\andcond \pc\prime (T) = l_3 \andcond \mathtt{f\prime =f} \andcond \mathtt{x\prime =x-1}}_{\tau_5} \andcond (\underbrace{[\pc(T) = l_5 \orcond \pc(T) = l_4 ] \to \mathtt{x}\geq 1}_{\varphi_1})
		) \\
			\to(\underbrace{[\pc\prime (T) = l_5 \orcond \pc\prime(T) = l_4] \to \mathtt{x}\prime  \geq 1}_{\varphi_1\prime })\;\;\;\;
	\end{align*}
%	\end{dmath*}

	The formula is valid because $\pc(T) = l_3 \neq l_5 \andcond l_2 \neq l_4$ thus the $\varphi\prime _1$ is true.

	 $\tau_6 \andcond \varphi_1 \to \varphi_1\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{align*}
		(
			\underbrace{\pc(T) = l_3 \andcond \pc\prime (T) = l_7 \andcond \mathtt{f\prime =f} \andcond \mathtt{x<1}}_{\tau_6} \andcond \underbrace{\pc(T) = l_5 \to \mathtt{x} \geq 1}_{\varphi_1}
		) \\
			\to (\underbrace{[\pc\prime (T) = l_5 \orcond \pc\prime(T) = l_4] \to \mathtt{x}\prime  \geq 1}_{\varphi_1\prime })\;\;\;\;
	\end{align*}
%	\end{dmath*}

	The formula is valid because $\pc(T) = l_7 \neq l_5$ thus the $\varphi\prime _1$ is true.

\paragraph{Conclusion:} we have proven that $\pc(T) = l_5 \to x \geq 1$.
%
This is called an \concept[Invariant]{invariant} because it is always true in all executions of the program.
%
This invariant has been chosen specially because it is needed in the proof of $\varphi_2$.

\begin{center}\rule{4cm}{0.4pt}  $\varphi_2$  \rule{4cm}{0.4pt}\end{center}

	\; $\tau_1 \andcond \varphi_2 \to \varphi_2\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{equation*}
		(
			\underbrace{\pc(T) = l_1 \andcond \pc\prime (T) = l_2 \andcond \mathtt{f\prime =f} \andcond \mathtt{x}\prime  = 10}_{\tau_1} \andcond \underbrace{\mathtt{x} \geq 0}_{\varphi_2}
		)
				\to  \underbrace{\mathtt{x}\prime  \geq 0}_{\varphi_2\prime }\\\\
	\end{equation*}
%	\end{dmath*}

	The formula is valid because $x\prime =10 \to x\prime \geq 0$.

	\; $\tau_2 \andcond \varphi_2 \to \varphi_2\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{equation*}
		(
			\underbrace{\pc(T) = l_2 \andcond \pc\prime (T) = l_3 \andcond \mathtt{f}\prime  = 1 \andcond x\prime =x}_{\tau_2} \andcond \underbrace{\mathtt{x} \geq 0}_{\varphi_2}
		)
			\to \underbrace{\mathtt{x}\prime  \geq 0}_{\varphi_2\prime }\\\\
	\end{equation*}
%	\end{dmath*}

	The formula is valid because of the congruence of equality used in  $x\prime =x \andcond x\geq 0 \to x\prime \geq 0$

	\; $\tau_3 \andcond \varphi_2 \to \varphi_2\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{equation*}
		(
			\underbrace{\pc(T) = l_3 \andcond \pc\prime (T) = l_4 \andcond \mathtt{f\prime =f} \andcond x\prime \geq 1}_{\tau_3} \andcond \underbrace{\mathtt{x} \geq 0}_{\varphi_2}
		)
			\to \underbrace{\mathtt{x}\prime  \geq 0}_{\varphi_2\prime }\\\\
	\end{equation*}
%	\end{dmath*}

	The formula is valid because $x\prime \geq 1 \to x\prime \geq 0$.
	\; $\tau_4 \andcond \varphi_2 \to \varphi_2\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{equation*}
		(
			\underbrace{\pc(T) = l_4 \andcond \pc\prime (T) = l_5 \andcond \mathtt{f}\prime  = \mathtt{f*x} \andcond \mathtt{x\prime =x}}_{\tau_4} \andcond \underbrace{\mathtt{x} \geq 0}_{\varphi_2}
		)
			\to \underbrace{\mathtt{x}\prime  \geq 0}_{\varphi_2\prime }\\\\
	\end{equation*}
%	\end{dmath*}

	The formula is valid because of the congruence of equality used in  $x\prime =x \andcond x\geq 0 \to x\prime \geq 0$

	\; $\tau_5 \andcond \varphi_2 \to \varphi_2\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{equation*}
		(
			\underbrace{\pc(T) = l_5 \andcond \pc\prime (T) = l_3 \andcond \mathtt{f\prime =f} \andcond \mathtt{x\prime =x-1}}_{\tau_5} \andcond \underbrace{\mathtt{x} \geq 0}_{\varphi_2}
		)
			\to \underbrace{\mathtt{x}\prime  \geq 0}_{\varphi_2\prime }\\\\
	\end{equation*}
%	\end{dmath*}

	The formula has some more difficulty.
	%
	Inside the loop $x$ should be greater than 1.
	%
	However, that information is not encoded in the formula.

	The solution is use some \concept{support}.
	%
	A support formula is an invariant formula added to the antecedent of an implication to give more information.
	%
	This addition does not change the validity of the formula.
	%
	We could equivalently prove

	\[
		(\tau_5 \andcond \varphi_1 \andcond \varphi_2 \to \varphi_2\prime ) \rightarrow (\varphi_2\to\varphi_2\prime )
	\]

	And this is exactly the solution to proof this \gls{VC}

%	\begin{dmath*}[indentstep={0em}]
	\begin{equation*}
		(
			\underbrace{\pc(T) = l_5 \andcond \pc\prime (T) = l_3 \andcond \mathtt{f\prime =f} \andcond \mathtt{x\prime =x-1}}_{\tau_5} \andcond \underbrace{\mathtt{x} \geq 0}_{\varphi_2} \andcond \underbrace{[\pc(T) = l_5 \orcond \pc(T) = l_4] \to \mathtt{x} \geq 1}_{\varphi_1}
		)
			\to \underbrace{\mathtt{x}\prime  \geq 0}_{\varphi_2\prime }\\\\
	\end{equation*}
%	\end{dmath*}

	And this formula is valid. Applying resolution we get an equivalent valid formula:

	\[
		( \mathtt{x\prime =x-1} \andcond \mathtt{x\prime }\geq 1) \to \mathtt{x} \geq 0
	\]

	\; $\tau_6 \andcond \varphi_2 \to \varphi_2\prime $:
%	\begin{dmath*}[indentstep={0em}]
	\begin{equation*}
		(
			\underbrace{\pc(T) = l_3 \andcond \pc\prime (T) = l_7 \andcond \mathtt{f\prime =f} \andcond \mathtt{x<1} \andcond \mathtt{x\prime =x} }_{\tau_6} \andcond \underbrace{\mathtt{x} \geq 0}_{\varphi_2}
		)
			\to \underbrace{\mathtt{x}\prime  \geq 0}_{\varphi_2\prime }\\\\
	\end{equation*}
%	\end{dmath*}

	And this formula is valid. Applying resolution we get an equivalent valid formula:

	\[
		(
			\mathtt{x\prime =x}  \andcond \mathtt{x}\geq 0 \to \mathtt{x\prime }\geq 0
		)
	\]

\paragraph{Conclusion} We have proof that $\varphi_1$ and $\varphi_2$ are invariants.