\documentclass[Thesis.tex]{subfiles}
\begin{document}
\chapter{Verification and Benchmarking}
\label{chp:verfication}
\glsresetall

This chapter is dedicated to presenting some of the tests that we have done to
verify that the software we have developed is indeed functioning as advertised.
These types of tests are more large scale (integration tests) compared to low
level unit tests which is a part of the source code for QFLOW. By checking that
we can reproduce known benchmarks and observe behavior that is consistent with
our expectations we hope to increase the amount of trust assigned to the implementation.

\section{Setup}

We focus our tests on the idealized harmonic oscillator system in $D = 3$ dimensions
with $N$ non-interacting particles, i.e.\ the Hamiltonian given by
\cref{eq:ho-no-interaction-hamiltonian}:

\begin{align}
    \hat H_0 &= \sum_{i=1}^N\qty(-\frac{1}{2}\laplacian_i +
    \frac{1}{2}r_i^2),
\end{align}
where we have $\hbar = m = \omega = 1$, and
$r_i^2 = x_i^2 + y_i^2+z_i^2$. This has the ground state given by
\cref{eq:Phi-non-inter}, generalized to $N$ particles in three dimension and
omitting normalization constants:

\begin{align}
        \Phi(\mat X) &= \exp[-\frac{1}{2}\sum_{i=1}^N r_i^2],
\end{align}
where as before we have defined $\mat X$ as

\begin{align}
  \mat X &\defeq \mqty(\vx_1\\\vx_2\\\vdots\\\vx_N) \defeq \mqty(x_1&y_1&z_1\\x_2&y_2&z_2\\\vdots&\vdots&\vdots\\x_N&y_N&z_N)
\end{align}

For the trail wave function we shall use two different ones. First, the simple
Gaussian form of the ground state itself:

\begin{align}
  \psi_G(\mat X) = \exp(-\alpha\sum_{i = 1}^N r_i^2),
\end{align}
with $\alpha$ the only variational parameter. Learning the ideal parameters
should be trivial in this case, and we should expect perfect results.

Second, we use an ansatz resulting from a Gaussian-binary \gls{rbm}~\cite{Flugsrud-2018}, presented in \cref{eq:rbm-def}:

\begin{align}
  \psi_{RBM}(\vX) &=
        \exp[-\sum_i^{M} \frac{\qty(X_i-a_i)^2}{2\sigma^2}]
        \prod_j^H \qty(1 + \exp[b_j+\sum_i^M \frac{X_iW_{ij}}{\sigma^2}]),
\end{align}
where $M = \text{\# of particles}\times \text{\# of dimensions}$ is the number of degrees of freedom and $H$ is
the number of hidden nodes (set to 4 through this section). Note also that $X_i$
in the above refers to the $i$'th degree of freedom, counting through $\mat X$
in row major order. The variational parameters are $\vb a, \vb b$ and $\mat W$,
and we hold $\sigma^2=1$ constant in this case.

We use this wave function simply to make learning the true ground state slightly
more challenging than proposing a simple Gaussian straight away. Note that
setting $\vb{a}, \vb b$ and $\mat W$ all to zero yields the correct ground state in this particular case.

\section{Energy Estimates and Statistics}

We start by verifying that we can reproduce the expected values for $\expval{E_L}$,
$\expval{r}$ and $\expval{r^2}$ for the ideal harmonic oscillator. We still use
$D=3$ dimensions and set $N=100$ particles.

\Cref{tab:verify-energy-estimates} shows the energy obtained using $\psi_G$ with
two different values of $\alpha_G$. For the optimal choice $\alpha_G = 0.5$ we
get the exact analytic ground state energy, $\flatfrac{\expval{E_L}}{N} =
\flatfrac{D}{2}$. The reported variance is entirely due to the limited
precision of $\SI{64}{bit}$ floating point numbers.

For the non-optimal $\alpha_G=0.51$ we get a larger energy, as we would expect.
Furthermore, we get a reported \gls{sem} of $\sim \SI{2e-5}{\au}$. This
includes a correction from the automated blocking mechanism
by~\textcite{Jonsson-2018}. The results were obtained using $2^{23}$ \gls{mc}
samples, so by the standard prescription for \gls{sem} we should have
gotten $\flatfrac{\SI{8e-4}{\au}}{\sqrt{2^{23}}} \approx \SI{3e-7}{\au}$. This
large discrepancy can be explained by the presence of a large amount of
autocorrelation in the energy estimates. Inherent to \gls{mci} is
some degree of autocorrelation, but the reason for this large amount is because
of the large number of particles and how sampling is implemented. Each \gls{mc}
step we move only a single particle. For large $N$ this means it takes a
lot of samples to substantially change the positions of all particles. For smaller
$N$ the correction from blocking is smaller, albeit still present. This
exemplifies the importance of a proper calculation of statistical errors. Every
\gls{sem} presented in this thesis will include this correction.

\begin{table}[h]
  \centering
  \caption[Ground state energies of the ideal harmonic oscillator]{\label{tab:verify-energy-estimates}Estimated ground state energy
    using $\psi_G$ with two different values for $\alpha_G$. The energies,
    standard deviations and variances are
    given per particle, and were produced using \gls{is} and
    $2^{23}$ samples. Statistical errors are corrected for autocorrelation using
    blocking. Energies in atomic units $[\si{\au}]$.\citesource{writing/scripts/verify-energy-stats.py}}
  \input{scripts/verify-energy-stats.py.table1.tex}
\end{table}

Moving from energy to distance metrics, \cref{tab:verify-radius-estimates} shows
the mean radial displacement and its squared sibling for all the particles in the system.
The results were obtained using $\alpha_G=0.5$ and $2^{23}$ \gls{mc} samples. For
reference, the table also states the exact analytic results. Unlike the energy,
which for the ideal $\alpha_G$ is independent of position, these results are not
perfectly accurate. However, the results are correct to five significant digits,
and both $\expval{r}$ and $\expval{r^2}$ are within a few \glspl{sem} from
the analytic value. We take this as further confirmation of both the integration
implementation and the validity of the statistical estimates.

\begin{table}[h]
  \centering
  \caption[Radial metrics of the ideal harmonic oscillator]{\label{tab:verify-radius-estimates}Estimates for the mean radial
    displacement, $\expval{r}$, and the mean squared radial displacement,
    $\expval{r^2}$. For reference the exact analytic results are listed as well
    ($\expval{r}=\flatfrac{2}{\sqrt{\pi\omega}}$ and
    $\expval{r^2}=\flatfrac{D}{2}$), which can be easily verified by computing
    the corresponding integrals directly. Lengths in dimensionless units of $a_{ho}$.\citesource{writing/scripts/verify-energy-stats.py}}
  \input{scripts/verify-energy-stats.py.table2.tex}
\end{table}

\section{One-body Density}

Because the wave functions we typically encounter tend to be multidimensional,
visualizing them can be quite challenging. One way of reducing the
dimensionality is to integrate out the positions for all but one particle. The
result is a \gls{pdf} called the one-body density:

\begin{align}
  \label{eq:one-body-density-def}
  \rho(\vx_1) = \idotsint \dd{\vx_2}\dd{\vx_2}\dots\dd{\vx_N} \abs{\Psi(\vX)}^2,
\end{align}
where we have arbitrarily chosen to keep particle index $1$. This gives the
\gls{pdf} for where one might expect to find particle $1$,
averaged over all possible configurations of the other particles.

As a validating example, we consider the simple Gaussian wave function $\psi_G$.
We get:

\begin{align}
  \rho(\vx_1) &= \idotsint \dd{\vx_2}\dd{\vx_2}\dots\dd{\vx_N} \abs{\psi_G(\vX)}^2\\
  &= e^{-2\alpha r_1^2}\idotsint \dd{\vx_2}\dd{\vx_2}\dots\dd{\vx_N} \,e^{-2\alpha\sum_{i=2}^N r_i^2}\\
    &= C e^{-2\alpha r_1^2},\label{eq:ver-annon-1}
\end{align}
where $C$ is a normalization constant. Perhaps unsurprisingly, the particle will
tend to be located close to the center of the potential well, with exponentially
decreasing probability for increasing radii.

We can perform the integral in \cref{eq:one-body-density-def} for any wave
function using \gls{mci}. We simply make a histogram of the
particles position as we sample a large amount of configuration.\footnote{A
technical caveat is that when we discretize the radius $r_1$ into bins for the
histogram we must account for the different volumes (or areas or lengths in two
and one dimensions) of the bins. Greater $r$ will correspond to greater volumes,
and because of this they will receive a correspondingly greater proportion of
the samples. Dividing the bin counts by their respective volumes fixes this.}
\Cref{fig:verify-onebody} shows the resulting plot of $\rho(r_1)$ in a harmonic
oscillator with $N=100$ three-dimensional particles, using $2^{23}$
samples. After normalizing both the result and \cref{eq:ver-annon-1} the two
curves are indistinguishable, showing that our implementation is indeed correct.

\begin{figure}[h]
  \centering
  \resizebox{0.7\linewidth}{!}{%
    \input{scripts/verify-onebody-density.py.tex}
  }
  \caption[One-body density of the ideal harmonic oscillator]{\label{fig:verify-onebody}One-body density of a particle in a
    harmonic oscillator potential, as described by $\psi_G$ with $\alpha=0.5$.
    The curve is indistinguishable from the analytic result. The small
    discrepancy around $r_1=0$ is an artifact of the vanishing volume of the
    inner most bins, and the discrepancy becomes increasingly negligible with
    more samples. This result used $2^{23}$ \gls{mc} samples.\citesource{writing/scripts/verify-onebody-density.py}}
\end{figure}

\section{Two-body Density}

Similarly to the one-body density, we can integrate out all degrees of freedom
except for two particles. This will give us a two-dimensional \gls{pdf} showing
how the two particles are likely to be located relative to each other.

Mathematically we define it in a similar way,

\begin{align}
  \label{eq:two-body-density-def}
  \rho(\vx_1, \vx_2) = \idotsint \dd{\vx_3}\dd{\vx_4}\dots\dd{\vx_N} \abs{\Psi(\vX)}^2,
\end{align}
which for $N$ non-interacting particles governed by $\psi_G$ can be solved
analytically:

\begin{align}
  \label{eq:two-body-analytic}
  \rho(\vx_1, \vx_2) = Ce^{-2\alpha\qty(r_1^2 + r_2^2)}.
\end{align}

Again we can perform the integral numerically using \gls{mci}.
\Cref{fig:verify-twobody} shows a contour plot of the density along with the
exact contour lines from \cref{eq:two-body-analytic} indicated by the dashed
lines. Visually distinguishing the two is a little harder now, as we have turned
to colors to visualize the three-dimensional plot. Still, the two sets of
contour lines are very much in agreement, indicating that the implementation is
correct.

\begin{figure}[h]
  \centering
  \resizebox{0.7\linewidth}{!}{%
    \input{scripts/verify-twobody-density.py.tex}
  }
  \caption[Two-body density of the ideal harmonic oscillator]{\label{fig:verify-twobody}Contour plot of the two-body density of
$N=10$ particles in a harmonic oscillator potential, as described by $\psi_G$
with $\alpha=0.5$. The dotted lines are the contours given by
\cref{eq:two-body-analytic}. Again the numerical result follows closely that of
the exact result. This result used $2^{25}$ \gls{mc} samples.\citesource{writing/scripts/verify-twobody-density.py}}
\end{figure}

\section{Optimization}

\subsection{Integration Test}
The simplest complete test is to initialize $\psi_G$ with a non-optimal
parameter, e.g.\ $\alpha=0.3$, and attempt to learn the optimal value.
Optimizing this is trivially accomplished, and
\cref{fig:verify-gaussian-simplest} shows a training progression using $N=10$
particles. The hyperparameters have here been artificially tuned to avoid
immediate convergence to $\alpha =0.5$ so as to better illustrate
the process.

If we allow the training to progress a little further (or use more optimal
hyperparameters), it eventually finds
$\alpha = 0.5$ to within machine precision and we get
$\flatfrac{\expval{E_L}}{N} = \flatfrac{D}{2}$ with exactly zero variance. While
this test is not the most challenging, it is nevertheless a useful check.

\begin{figure}[h]
  \centering
    \resizebox{\linewidth}{!}{%
      \input{scripts/verify-simple-gaussian.py.tex}
    }
  \caption[Learning progression using \gls{vmc} on the ideal harmonic
  oscillator]{\label{fig:verify-gaussian-simplest}Example progression of
    optimizing the variational parameter $\alpha$, using $\psi_G$ as the trial wave function.
    Hyperparameters have been tuned so that we can see what happens, as opposed
    to immediate convergence to the perfect result.\citesource{writing/scripts/verify-simple-gaussian.py}}
\end{figure}

\subsection{Learning Rate Dependency}
\label{sec:verify-learning-rate-dep}

Successful training is highly dependent on using the correct hyperparameters.
Among the most important are the ones controlling the optimization scheme, such
as the learning rate in \gls{sgd}. The following plots aim at illustrating this
dependency, while also serving as a check that the implemented optimization
schemes work as expected.

Importantly, these results are not meant to infer that some schemes or learning
rates are superior to others. Which scheme works best for a given learning
problem will depend on a number of factors, such as the magnitude of the
gradients, number of parameters, variance in gradient estimates etc.

\subsubsection{Simple Problem - $\psi_G$}

\Cref{fig:verify-lr-gaussian} shows the absolute error of $\psi_G$ during
training with several different schemes. For standard SGD we see that a learning
rate around $\eta = 0.1$ (with $\eta$ defined as in \cref{eq:gradient-decent-definition}) performs best among these results, and SGD with $\eta
=0.01$ is the slowest to converge. Naturally, values of $0.01<\eta<0.1$
perform somewhere between the two.

From these results alone it might seem like
larger learning rates always perform better. To an extent this is true, as it
allows for more rapid learning. However, setting $\eta$ too high can lead to
divergence and unpredictable behavior. In less extreme cases, it can also keep
us from converging properly onto the correct parameters by oscillating around
the ideal values.

We have also included some runs using ADAM. Here we have more parameters to play
with, but only a few are shown here. In this trivial learning example it is hard
to beat properly tuned SGD, but we see how ADAM is able to follow closely. In
this particular case, we saw large improvements by reducing $\beta_1$, which
effectively reduces the momentum applied. An important fact is also that ADAM is
designed to be used with many parameters, with individual learning rates per
parameter. This enhancement does not show itself in this single-parameter example.

\begin{figure}[h]
  \centering
    \resizebox{\linewidth}{!}{%
      \input{scripts/verify-learning-rate-gaussian.py.tex}
    }
  \caption[Comparison of optimization schemes on a simple problem]{\label{fig:verify-lr-gaussian}Example training progression using
    $\psi_G$ as trial wave function with different optimization schemes. With
    sufficient time, all algorithms tend towards zero error.\citesource{writing/scripts/verify-learning-rate-gaussian.py}}
\end{figure}

\subsubsection{More Complex Problem - $\psi_{RBM}$}

We run the same test as above, now with $\psi_{RBM}$ as the trial wave function
instead. We do this to illustrate a common pitfall of gradient-based
optimization -- local minima. \cref{fig:verify-lr-rbm} shows three runs
plateauing around an error $\sim 10^{-6}\si{\au}$. Interestingly, the worst
result is obtained with the middle most value of $\eta$. This shows the random
nature of SGD, in that it is quite unpredictable when and where we might get
stuck due to a local minimum. Repeating the same experiment with different random
seeds does not consistently reproduce this particular result.

Similarly, we see that one of the ADAM runs did in fact stumble on to a
different, better local minimum. While this is also subject to randomness, we
find that ADAM tends to be at least as good as SGD whenever we have more than
one parameter to learn. This is to be expected, as ADAM can account for
different scales and variability in the components of the parameter gradient.

\begin{figure}[h]
  \centering
    \resizebox{\linewidth}{!}{%
      \input{scripts/verify-learning-rate-rbm.py.tex}
    }
  \caption[Comparison of optimization schemes on a complicated problem]{\label{fig:verify-lr-rbm}Example training progression using
    $\psi_{RBM}$ as trial wave function with different optimization schemes. We
    see evidence of learning getting stuck in local minima due to the overly
    complex wave function anstaz.\citesource{writing/scripts/verify-learning-rate-rbm.py}}
\end{figure}

\section{Sampling}
\label{sec:verify-sampling}

We will now investigate the behavior of the implemented sampling strategies.

\subsection{Step Dependency}

Similarly to the learning rate in optimization schemes, the \gls{mc} samplers
are highly dependent on an appropriate step parameter (see
\cref{alg:metropolis-simple,alg:metropolis-importance}).\footnote{We
  intentionally stick to calling these \say{step} parameters, without
  specifically mentioning which of the two ($\Delta x$ or $\Delta t$) we mean. When
necessary we will make it clear which type of step parameter is meant.} We want to use a
step size that balances two opposing attributes:

\begin{itemize}
\item Particles should be sufficiently mobile.
  \begin{itemize}
  \item Unchanging configurations lead to biased energy estimates with high
    autocorrelation.
  \end{itemize}
\item New configurations should be accepted as much as possible.
  \begin{itemize}
  \item Rejections imply wasted computation time as well as increased autocorrelation.
  \end{itemize}
\end{itemize}

\Cref{fig:verify-sampling-step} shows how the \gls{ar} changes with
different step sizes. For both Metropolis and \gls{is}, the \gls{ar} tends
to $\SI{100}{\percent}$ for low step sizes, and to $\SI{0}{\percent}$ for large
values. Both algorithms show a similar pattern in the middle region, with
a steeper decline for \gls{is}. A good trade-off between the above
considerations is achieved when the \gls{ar} is somewhere in the range
\SIrange{50}{99}{\percent}, with the exact best value dependent on the
particular problem at hand.

\begin{figure}[h]
  \centering
    \resizebox{\linewidth}{!}{%
      \input{scripts/verify-sampling-step.py.tex}
    }
    \caption[Comparison of sampling strategies]{\label{fig:verify-sampling-step}\emph{Solid lines:} Acceptance rate of
    Metropolis and \gls{is} as a function of step size. Note that the
  interpretation of the step size is different for the two algorithms. \emph{Dashed
  lines:} \gls{sem} of energy estimates obtained using the corresponding
  sampling algorithm and step size, using $\psi_G$ with $\alpha=0.51$.\citesource{writing/scripts/verify-sampling-step.py}}
\end{figure}

The dotted lines show the \gls{sem} obtained using the
corresponding sampler and step size, when calculating the local energy with
$\psi_G$ and $\alpha=0.51$.\footnote{The value $\alpha=0.51$ was used to avoid
  the zero variance of the ideal value $\alpha=0.5$, but still behaving
  similarly to the real system.} The errors were calculated using $2^{21}$ \gls{mc}
samples and corrected for autocorrelation using blocking~\cite{Jonsson-2018}.

We see Metropolis tending to very small errors in the range shown, with a large
spike around $\delta \approx 0.01$. We believe step sizes around this critical
point allows enough movement for the system to randomly get into unlikely
states, but still so small that getting out of these states takes a long time,
leading to a significant portion of samples from unimportant states. The low
error for both high and low step sizes can be explained by both extremes
resulting in similar behavior; no effective change. When the system remains
unchanged, either from rejected samples or effectively equivalent ones (for high
and low step sizes, respectively), the resulting local energies must necessarily
be very similar as well. Neither case is desirable considering accurate
integration results. Recall that the \gls{sem} is a statistical measure of
the \emph{precision} of the local energy estimate, and not a measure of its
\emph{accuracy}.\footnote{The number $3.14159$ is more precise than the number
$6$, but if the true value is $6.28$, then the latter is more accurate.}\\

\Gls{is} shows different behavior with respect to the \gls{sem}.
For the entire range of good step size choices, \gls{is} results in
smaller statistical errors. This is to be expected, and \gls{is} will
therefore be preferred whenever it is available to us.

Still, we would like to explain the behavior for non-optimal step sizes. The
error shows a spike as the step size decreases, in a similar way as seen for
Metropolis. Although not shown fully, the error also explodes once the
acceptance rate goes below a few percent. We believe this behavior is a result
of how the update rule for \gls{is} is dependent on both the step
size and its square root.

For small step sizes, the square root term will dominate, and the algorithm
effectively decays into standard Metropolis. The location of the spike is
shifted towards smaller step sizes (approximately the square of the Metropolis
spike location), and the spike is wider because the square root function grows
slower than linearly.

Finally, the unstable behavior for large step sizes can be explained the other
way. The little remaining movement is dominated by the drift force, and the
system will quickly get stuck in a local maximum of the wave function, and then be
unable to get out again. These are maxima with large probability amplitudes,
resulting in large values for the local energy and correspondingly large errors.

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{geometry}
\geometry{
a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}

\title{Thermodynamics}
\author{Neo Wang}
\date{\today}

\begin{document}

\maketitle

\section{Formulas}

\begin{itemize}
    \item Boyle's Law: $P_1V_1=P_2V_2$
    \item $k=1.38\times 10^{-23}J/K$
    \item Ideal Gas Law 1: $PV=nRT$
    \item Ideal Gas Law 2: $PV=NkT$ where $P$ is the absolute pressure of a gas, $V$ is the volume it occupies, $N$ is the number of atoms and molecules in the gas, and $T$ is its absolute temperature. $k$ is the \textit{Boltzmann constant}.
    \item Mechanical equivalent of heat: motion and heat are interchangable.
    \item Charles' Law: $$\frac{V_1}{T_1}=\frac{V_2}{T_2}$$
    \item First law of thermodynamics: $$\Delta U = Q - W$$
    \item $\propto$ means proportional to
    \item $T\propto KE$
    \item $$R=8.314\frac{J}{molK}$$
    \item $$P\Delta V=\textrm{Work}$$
    \item $$U = \frac{3}{2}nRT=\frac{3}{2}NK_BT$$
    \item $$e_{carnot} = 1 - \frac{T_C}{T_H}$$
\end{itemize}

\section{Solving}
\begin{itemize}
    \item Work done by an ideal gas (PV diagrams)
    \begin{itemize}
        \item $$W_{BY}=\int_{V_i}^{V_F}PdV$$
        \item Isochoric ($\Delta V = 0$)
        \item $W_{BY}=0J$. There is no area under the curve.
        \item Isobaric ($\Delta P = 0$)
        \item If P is constant then $$W_{BY}=P\Delta V=P(V_F-V_i)$$
        \item Isothermal ($\Delta T = 0$)
        \item Issue is temperature doesn't show up in the integral!
        \item Remember that if we have an ideal gas then $$W_{BY}=\int_{V_i}^{V_F}PdV=\int_{V_i}^{V_F}\frac{nRT}{V}dV=nRT\ln(\frac{V_F}{V_i})$$
        \item Adiabatic = (Q = 0)
        \item $$\Delta E_{int}=Q_{ON}-W_{BY}=W_{BY}=-\Delta E_{INT}=-nC_V\Delta T$$
        \item Note that this is steeper than the isotherm.
    \end{itemize}
    \item Carnot Cycle and Heat Engines
    \begin{itemize}
        \item $$e_{carnot} = 1 - \frac{T_C}{T_H}$$
        \item $$T_C=573K, T_H=773K$$
        \item If we plug this into the formula, we get $1-\frac{573}{773} = 26\%$
        \item A carnot engine is at temperatures 400K and 700K.
        \item If 14000J of heat is absorbed by the engine, how much heat is discarded into the cold reservoir. $Q_H=14000J$
        \item To solve this we use $$\frac{T_H}{T_C}=\frac{|Q_H|}{|Q_C|}$$
        \item Where T is temperature and Q is heat. H is our hot, and C is our cold.
        \item
    \end{itemize}
\end{itemize}

\end{document}


% Full title as you would like it to appear on the page
\chapter{Long paper 1 chapter title}
% Short title that appears in the header of pages within the chapter
\chaptermark{Short chapter title}

\section{Abstract}
Concise introduction, motivation, results, and conclusions.

\section{Introduction}
Examples of citations: we knew X from \cite{Croote201616022} and Y from \cite{Croote20181306}.

\section{Results}
\subsection{Result 1}
Here is a reference to Figure \ref{fig:paper1_fig1}.
We found bacteria, roughly 5 \si{\mu}m in length, that live at 95\degree C for roughly 90\% of the year. More symbols: \$, \#, 10\textsuperscript{5}, $\alpha$, $\beta$, $\gamma$, $\kappa$, \textbf{bold}, \textit{italics}.
Quotation marks are "correctly oriented" thanks to the csquotes package.
Now an inline equation: $E = mc^2$. Now a reference to Equation \ref{eqn:paper1_eqn1}:
\begin{equation}\label{eqn:paper1_eqn1}
d_t = \frac{c - pn_t}{n_t}
\end{equation}

\begin{figure}[hbt!]
\centering
\includegraphics[width=14cm, keepaspectratio]{figs/paper1/fig1.png}
\caption[Short figure caption for List of Figures]{Long figure caption text that explains everything.}
\label{fig:paper1_fig1}
\end{figure}

There will be more text here.

More text here.

More text here.

More text here.

More text here.

More text here.

\subsection{Result 2}
We discovered something else. Here is a reference to Table \ref{tab:paper1_tab1}.

\renewcommand{\arraystretch}{2}  % make spacing nicer
\begin{table}[hbt!]
\centering
\begin{tabularx}{\textwidth}{c|c|c|c}  % 4 columns center-justified
   \textbf{Col 1} & \textbf{Col 2} & \textbf{Col 3} & \textbf{Col 4} \\
   \hline  % horizontal line
   Text in Row 1a & Text in Row 1b & Text in Row 1c & Lots of text in Row 1d \\
                  & Row 1.5b & Row 1.5c & Row 1.5d \\
   \hline
   Row 2a & Row 2b & Row 2c & Row 2d \\
   \hline
   Row 3a & Row 3b & Row 3c & Row 3d \\
\end{tabularx}
\caption[Short table caption for List of Tables]{Long table caption explaining everything.}
\label{tab:paper1_tab1}
\end{table}

More text here.

More text here.

More text here.

More text here.

\begin{sloppypar}
FixAwkwardSpacingWithSloppypar FixAwkwardSpacingWithSloppypar FixAwkwardSpacingWithSloppypar FixAwkwardSpacingWithSloppypar FixAwkwardSpacingWithSloppypar.
\end{sloppypar}

More text here.

More text here.

\section{Conclusions}
The end of the paper.


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

  This chapter presents some useful definitions that constitute the
background of time-frequency analysis (most of the information presented in
this tutorial are extracted from \cite{FLA93}). After a brief recall on
time-domain and frequency-domain representations, we introduce the concepts
of time and frequency localizations, time-bandwidth product and the
constraint associated to this product (the Heisenberg-Gabor
inequality). Then, the instantaneous frequency and the group delay are
presented as a first solution to the problem of time localization of the
spectrum. We carry on by defining non-stationarity from its opposite,
stationarity, and show how to synthesize such non-stationary signals with
the toolbox. Finally, we show that in the case of multi-component signals,
these mono-dimensional functions (instantaneous frequency and group delay)
are not sufficient to represent these signals ; a two-dimensional
description (function of time {\em and} frequency) is necessary.

\section{Time representation and frequency representation}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  The time representation is usually the first (and the most natural)
description of a signal we consider, since almost all physical signals are
obtained by receivers recording variations with time.

  The frequency representation, obtained by the {\it Fourier transform}
\index{Fourier transform}
\[X(\nu) = \int_{-\infty}^{+\infty} x(t)\ e^{-j2\pi \nu t}\ dt,\]
is also a very powerful way to describe a signal, mainly because the
relevance of the concept of frequency is shared by many domains (physics,
astronomy, economics, biology \ldots) in which periodic events occur.

  But if we look more carefully at the spectrum $X(\nu)$, it can be viewed
as the coefficient function obtained by expanding the signal $x(t)$ into
the family of infinite waves, $\exp\{j2\pi \nu t\}$, which are completely
unlocalized in time. Thus, the spectrum essentially tells us which
frequencies are contained in the signal, as well as their corresponding
amplitudes and phases, but does not tell us at which times these
frequencies occur.

\section{Localization and the Heisenberg-Gabor\\ principle}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\markright{Localization and the Heisenberg-Gabor principle}
  A simple way to characterize a signal simultaneously in time and in
frequency is to consider its mean localizations and dispersions in each of
these representations. This can be obtained by considering $|x(t)|^2$ and
$|X(\nu)|^2$ as probability distributions, and looking at their mean values
and standard deviations : \index{average time}\index{average
frequency}\index{time spreading}\index{frequency spreading}
%\begin{xalignat}{2}
$$
\begin{array}{rcll}
t_m   &=& \frac{1}{E_x}\ \int_{-\infty}^{+\infty} t\ |x(t)|^2\ dt  &
\mbox{\it average time}\\
\nu_m &=& \frac{1}{E_x}\ \int_{-\infty}^{+\infty} \nu\ |X(\nu)|^2\ d\nu  &
\mbox{\it average frequency}\\
T^2   &=& \frac{
4\pi}{E_x}\ \int_{-\infty}^{+\infty} (t-t_m)^2\ |x(t)|^2\ dt
&  \mbox{\it time spreading}\\
B^2   &=& \frac{4\pi}{E_x}\ \int_{-\infty}^{+\infty} (\nu-\nu_m)^2\
|X(\nu)|^2\ d\nu &  \mbox{\it frequency spreading}
\end{array}
$$
%\end{eqnarray*}
%\end{xalignat}

where $E_x$ is the \index{energy} {\it energy} of the signal, assumed to be
finite (bounded) :
\[E_x = \int_{-\infty}^{+\infty} |x(t)|^2\ dt < + \infty.\]
Then a signal can be characterized in the time-frequency plane by its mean
position $(t_m, \nu_m)$ and a domain of main energy localization whose area
is proportional to the {\it time-bandwidth product} $T\times B$.\\
\index{time-bandwidth product}

\subsection{Example 1}
%'''''''''''''''''''''
\label{ex1}
These time and frequency localizations can be evaluated thanks to the
M-files \index{\ttfamily loctime}{\ttfamily loctime.m} and \index{\ttfamily
locfreq}{\ttfamily locfreq.m} of the Toolbox. The first one gives the
average time center ($t_m$) and the duration ($T$) of a signal, and the
second one the average normalized frequency ($\nu_m$) and the normalized
bandwidth ($B$). For example, for a linear chirp with a gaussian amplitude
modulation, we obtain (see fig. \ref{Ns2fig1})\,:
\begin{verbatim}
     >> sig=fmlin(256).*amgauss(256);
     >> [tm,T]=loctime(sig)           --->  tm=128     T=32
     >> [num,B]=locfreq(sig)          --->  num=0.249  B=0.0701
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=8cm
\centerline{\epsfbox{figure/ns2fig1.eps}}
\caption{\label{Ns2fig1}Linear chirp with a gaussian amplitude modulation}
\end{figure}

  One interesting property of this product $T\times B$ is that it is lower
bounded :
\[T \times B \geq 1.\]
\index{Heisenberg-Gabor inequality} This constraint, known as the {\it
Heisenberg-Gabor inequality}, illustrates the fact that a signal can not
have simultaneously an arbitrarily small support in time and in
frequency. This property is a consequence of the definition of the Fourier
transform. The lower bound $T\times B = 1$ is reached for gaussian
functions :
\[x(t) = C \exp{[-\alpha(t - t_m)^2 + j2\pi \nu_m(t-t_m)]}\]
%with $C \in \mathbb{R}$, $\alpha \in \mathbb{R}_{+}$. Therefore, the
with $C \in \Rset$, $\alpha \in \Rset_{+}$. Therefore, the
gaussian signals are those which minimize the time-bandwidth product
according to the Heisenberg-Gabor inequality.\\

\subsection{Example 2}
%'''''''''''''''''''''

 To check the Heisenberg-Gabor inequality numerically, we consider a
gaussian signal and calculate its time-bandwidth product (see
fig. \ref{Ns2fig2})\,:
\begin{verbatim}
     >> sig=amgauss(256);
     >> [tm,T]=loctime(sig);
     >> [fm,B]=locfreq(sig);
     >> [T,B,T*B]             --->  T=32  B=0.0312  T*B=1
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=8cm
\centerline{\epsfbox{figure/ns2fig2.eps}}
\caption{\label{Ns2fig2}gaussian signal : lower bound of the
Heisenberg-Gabor inequality}
\end{figure}
Hence, the time-bandwidth product obtained, when using the file
\index{\ttfamily amgauss}{\ttfamily amgauss.m}, is minimum.

\section{Instantaneous frequency}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\label{anasig}
\index{instantaneous frequency}
\index{analytic signal}
\index{Hilbert transform}
  Another way to describe a signal simultaneously in time and in frequency
is to consider its {\it instantaneous frequency}. In order to introduce such a
function, we must define first the concept of {\it analytic signal}.

  For any real valued signal $x(t)$, we associate a complex valued signal
$x_a(t)$ defined as
\[x_a(t) = x(t) + j HT(x(t))\]
where $HT(x)$ is the {\it Hilbert transform} of $x$ ($x_a$ can be obtained
using the M-file {\ttfamily hilbert.m} of the Signal Processing
Toolbox). $x_a(t)$ is called the analytic signal associated to $x(t)$. This
definition has a simple interpretation in the frequency domain since $X_a$
is a single-sided Fourier transform where the negative frequency values
have been removed, the strictly positive ones have been doubled, and the DC
component is kept unchanged :
\begin{eqnarray*}
	X_a(\nu) = 0 \ \ \ \ \ \ &\mbox{if}& \nu < 0 \\
	X_a(\nu) = X(0)\ \ &\mbox{if}& \nu = 0 \\
	X_a(\nu) = 2X(\nu) &\mbox{if}& \nu > 0
\end{eqnarray*}
($X$ is the Fourier transform of $x$, and $X_a$ the Fourier transform of
$x_a$). Thus, the analytic signal can be obtained from the real signal by
forcing to zero its spectrum for the negative frequencies, which do not
alter the information content since for a real signal, $X(-\nu)=X^*(\nu)$.

\index{instantaneous amplitude}
 From this signal, it is then possible to define in a unique way the
concepts of {\it instantaneous amplitude} and {\it instantaneous frequency} by :
\begin{eqnarray*}
a(t) &=& |x_a(t)| \ \ \ \ \ \ \ \ \ \ \ \mbox{\it  instantaneous amplitude} \\
f(t) &=& \frac{1}{2\pi} \frac{d\arg{x_a(t)}}{dt}\ \mbox{\it  instantaneous
frequency}
\end{eqnarray*}
An estimation of the instantaneous frequency is given by the M-file
\index{\ttfamily instfreq}{\ttfamily instfreq.m} of the Time-Frequency
toolbox :\\

   {\bf Example} (see fig. \ref{Ns3fig1})

\begin{verbatim}
     >> sig=fmlin(256); t=(3:256);
     >> ifr=instfreq(sig); plotifl(t,ifr);
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=6cm
\centerline{\epsfbox{figure/ns3fig1.eps}}
\caption{\label{Ns3fig1}Estimation of the instantaneous frequency of a
linear chirp}
\end{figure}
As we can see from this plot, the instantaneous frequency shows with
success the evolution with time of the frequency content of this signal.

\section{Group delay}
%~~~~~~~~~~~~~~~~~~~~
\index{group delay}
  The instantaneous frequency characterizes a local frequency behavior as a
function of time. In a dual way, the local time behavior as a function of
frequency is described by the {\it group delay} :
\[t_x(\nu) = -\frac{1}{2\pi}  \frac{d\arg{X_a(\nu)}}{d \nu}.\]
This quantity measures the average time arrival of the frequency $\nu$. The
M-file \index{\ttfamily sgrpdlay}{\ttfamily sgrpdlay.m} of the
Time-Frequency Toolbox gives an estimation of the group delay of a signal
(do not mistake it for the file {\ttfamily grpdelay.m} of the signal
processing toolbox which gives the group delay of a digital filter). For
example, with signal {\ttfamily sig} of the previous example, we obtain
(see fig. \ref{Ns4fig1})\,:
\begin{verbatim}
     >> sig=fmlin(256); fnorm=0:.05:.5;
     >> gd=sgrpdlay(sig,fnorm); plot(gd,fnorm);
\end{verbatim}

\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=6cm
\centerline{\epsfbox{figure/ns4fig1.eps}}
\caption{\label{Ns4fig1}Estimation of the group delay of the previous chirp}
\end{figure}

Be careful of the fact that in general, instantaneous frequency and group
delay define two different curves in the time-frequency plane. They are
approximatively identical only when the time-bandwidth product $T\times B$
is large. To illustrate this point, let us consider a simple example. We
calculate the instantaneous frequency and group delay of two signals, the
first one having a large $T\times B$ product, and the second one a small
$T\times B$ product (see fig. \ref{Ns4fig2})\,:
\begin{verbatim}
     >> t=2:255;
     >> sig1=amgauss(256,128,90).*fmlin(256,0,0.5);
     >> [tm,T1]=loctime(sig1); [fm,B1]=locfreq(sig1);
     >> T1*B1              --->  T1*B1=15.9138
     >> ifr1=instfreq(sig1,t); f1=linspace(0,0.5-1/256,256);
     >> gd1=sgrpdlay(sig1,f1); plot(t,ifr1,'*',gd1,f1,'-')
     >> sig2=amgauss(256,128,30).*fmlin(256,0.2,0.4);
     >> [tm,T2]=loctime(sig2); [fm,B2]=locfreq(sig2);
     >> T2*B2              --->  T2*B2=1.224
     >> ifr2=instfreq(sig2,t); f2=linspace(0.2,0.4,256);
     >> gd2=sgrpdlay(sig2,f2); plot(t,ifr2,'*',gd2,f2,'-')
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=8cm
\centerline{\epsfbox{figure/ns4fig2.eps}}
\caption{\label{Ns4fig2}Estimation of the instantaneous frequency (stars)
and group delay (line) of two different chirps with different amplitude
modulations. The first plot corresponds to a large $T\times B$ product
while the second corresponds to a small one}
\end{figure}
On the first plot, the two curves are almost superimposed (i.e. the
instantaneous frequency is the inverse transform of the group delay),
whereas on the second plot, the two curves are clearly different.

\section{About stationarity}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
\index{stationarity}
  Before talking about non-stationarity, which is a 'non-property', we must
define what we call {\it stationarity}.

  A deterministic signal is said to be {\it stationary} if it can be
written as a discrete sum of sinusoids :

\begin{eqnarray*}
x(t)&=&\sum_{k \in \Nset} A_k \cos{[2\pi \nu_k t + \Phi_k]} \ \ \ \
\mbox{ for a real signal} \\
x(t)&=&\sum_{k \in \Nset} A_k \exp{[j(2\pi \nu_k t + \Phi_k)]}
\mbox{for a complex signal}
\end{eqnarray*}
i.e. as a sum of elements which have constant instantaneous amplitude and
instantaneous frequency.

  In the random case, a signal $x(t)$ is said to be {\it wide-sense
stationary} (or stationary up to the second order) if its expectation is
independent of time and its autocorrelation function $E[x(t_1)x^*(t_2)]$
depends only on the time difference $t_2-t_1$. We can then show that the
associated analytic signal has constant instantaneous amplitude and
frequency expectations, which can be connected to the deterministic case.

\index{non-stationarity}
So a signal is said to be {\it non-stationary} if one of these fundamental
assumptions is no longer valid. For example, a finite duration signal, and
in particular a {\it transient signal} (for which the length is short
compared to the observation duration), is non-stationary.

\section{How to synthesize a mono-component non-stationary signal}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  One part of the Time-Frequency Toolbox is dedicated to the generation of
non-stationary signals. In that part, three groups of M-files are available\,:

\begin{enumerate}
\item The first one allows to synthesize different amplitude
modulations. These M-files begin with the prefix '{\ttfamily am}'. For
example, {\ttfamily amrect.m} computes a rectangular amplitude modulation,
{\ttfamily amgauss.m} a gaussian amplitude modulation \ldots

\item The second one proposes different frequency modulations.  These
M-files begin with '{\ttfamily fm}'. For example, {\ttfamily fmconst.m} is
a constant frequency modulation, {\ttfamily fmhyp.m} a hyperbolic frequency
modulation \ldots

\item The third one is a set of pre-defined signals. Some of them begin
with '{\ttfamily ana}' because these signals are analytic (for example
{\ttfamily anastep, anabpsk, anasing} \ldots), other have special names
({\ttfamily doppler, atoms} \ldots).
\end{enumerate}

  The first two groups of files can be combined to produce a large class of
non-stationary signals, multiplying an amplitude modulation and a frequency
modulation.\\

 {\bf Examples}

We can multiply the linear frequency modulation of Example 1 (see page
\pageref{ex1}) by a gaussian amplitude modulation (see
fig. \ref{Ns6fig1})\,:
\begin{verbatim}
     >> fm1=fmlin(256,0,0.5);
     >> am1=amgauss(256);
     >> sig1=am1.*fm1; plot(real(sig1));
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=6cm
\centerline{\epsfbox{figure/ns6fig1.eps}}
\caption{\label{Ns6fig1}Mono-component non-stationary signal with a linear
frequency modulation and a gaussian amplitude modulation}
\end{figure}
By default, the signal is centered on the middle (256/2=128), and its
spread is $T=32$. If you want to center it at an other position {\ttfamily
t0}, just replace {\ttfamily am1} by {\ttfamily amgauss(256,t0)}. A second
example can be to multiply a pure frequency (constant frequency modulation)
by a one-sided exponential window starting at {\ttfamily t=100} (see
fig. \ref{Ns6fig2})\,:
\begin{verbatim}
     >> fm2=fmconst(256,0.2);
     >> am2=amexpo1s(256,100);
     >> sig2=am2.*fm2; plot(real(sig2));
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=6cm
\centerline{\epsfbox{figure/ns6fig2.eps}}
\caption{\label{Ns6fig2}Mono-component non-stationary signal with a
constant frequency modulation and a one-sided exponential amplitude
modulation}
\end{figure}

As a third example of mono-component non-stationary signal, we can consider
the M-file \index{\ttfamily doppler}{\ttfamily doppler.m} : this function
generates a modelization of the signal received by a fixed observer from a
moving target emitting a pure frequency (see fig. \ref{Ns6fig3}).
\begin{verbatim}
     >> [fm3,am3]=doppler(256,200,4000/60,10,50);
     >> sig3=am3.*fm3; plot(real(sig3));
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=6cm
\centerline{\epsfbox{figure/ns6fig3.eps}}
\caption{\label{Ns6fig3}Doppler signal}
\end{figure}
This example corresponds to a target (a car for instance) moving straightly
at the speed of 50\,m/s, and passing at 10\,m from the observer (the
radar\,!). The rotating frequency of the engine is 4000\,revolutions per
minute, and the sampling frequency of the radar is 200\,Hz.\\

  In order to have a more realistic modelization of physical signals, we
may need to add some complex noise on these signals. To do so, two M-files
\index{\ttfamily noisecg}({\ttfamily noisecg} an \index{\ttfamily
noisecu}{\ttfamily noisecu}) of the Time-Frequency Toolbox are proposed :
{\ttfamily noisecg.m} generates a complex white or colored gaussian noise,
and {\ttfamily noisecu.m}, a complex white uniform noise. For example, if
we add complex colored gaussian noise on the signal {\ttfamily sig1} with a
signal to noise ratio of -10\,dB (see fig. \ref{Ns6fig4})
\begin{verbatim}
     >> noise=noisecg(256,.8);
     >> sign=sigmerge(sig1,noise,-10); plot(real(sign));
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=6cm
\centerline{\epsfbox{figure/ns6fig4.eps}}
\caption{\label{Ns6fig4}Gaussian transient signal ({\ttfamily sig1})
embedded in a -10\,dB colored gaussian noise}
\end{figure}
the deterministic signal {\ttfamily sig1} is now almost imperceptible from
the noise.

\section{What about multi-component non-stationary signals ?}
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  The notion of instantaneous frequency implicitly assumes that, at each
time instant, there exists only a single frequency component. A dual
restriction applies to the group delay : the implicit assumption is that a
given frequency is concentrated around a single time instant. Thus, if
these assumptions are no longer valid, which is the case for most of the
multi-component signals, the result obtained using the instantaneous
frequency or the group delay is meaningless.\\

  {\bf Example}

For example, let us consider the superposition of two linear frequency
modulations :
\begin{verbatim}
     >> N=128; x1=fmlin(N,0,0.2); x2=fmlin(N,0.3,0.5);
     >> x=x1+x2;
\end{verbatim}
At each time instant $t$, an ideal time-frequency representation should
represent two different frequencies with the same amplitude. The results
obtained using the instantaneous frequency and the group delay are of
course completely different, and therefore irrelevant (see
fig. \ref{Ns7fig1})\,:
\begin{verbatim}
     >> ifr=instfreq(x); subplot(211); plot(ifr);
     >> fn=0:0.01:0.5; gd=sgrpdlay(x,fn);
     >> subplot(212); plot(gd,fn);
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=6cm
\centerline{\epsfbox{figure/ns7fig1.eps}}
\caption{\label{Ns7fig1}Estimation of the instantaneous frequency (first
plot) and group-delay (second plot) of a multi-component signal}
\end{figure}
So these one-dimensional representations, instantaneous frequency and group
delay, are not sufficient to represent all the non-stationary signals. A
further step has to be made towards two-dimensional mixed representations,
jointly in time and in frequency. Even if no gain of information can be
expected since it is all contained in the time or in the frequency
representation, we can obtain a better structuring of this information, and
an improvement in the intelligibility of the representation.

  To have an idea of what can be made with a time-frequency decomposition,
let us anticipate the following and have a look at the result obtained on
this signal with the Short Time Fourier Transform (see
fig. \ref{Ns7fig2})\,:
\begin{verbatim}
     >> tfrstft(x);
\end{verbatim}
\begin{figure}[htb]
\epsfxsize=10cm
\epsfysize=8cm
\centerline{\epsfbox{figure/ns7fig2.eps}}
\caption{\label{Ns7fig2}Squared modulus of the short-time Fourier transform
of the previous multi-component non-stationary signal}
\end{figure}
Here two ``time-frequency components'' can be clearly seen, located around
the locus of the two frequency modulations.


\documentclass{article}
\usepackage{fullpage}
\usepackage{epsfig}
\usepackage{pdfsync}
\usepackage{amsfonts}

\begin{document}

\title{Documentation for the Multivariate Normal Gibbs sampler}
\author{Anand}
\maketitle

The Gibbs samplers in \texttt{Normal.py} can be applied to parameters $x$ whose children are distributed as follows: For each child $c_i$,
\begin{eqnarray*}
	(c_i | x, F_i, a_i, \tau_i) \sim \textup{N}(F_i x + a_i, \tau_i).
\end{eqnarray*}

\section{Conjugate}\label{conj}
The sampling method \texttt{cMVNormalWithMVNormalChildren} applies if $x$'s prior is normal:
\begin{eqnarray*}
	(x|\mu_p,\tau_p) \sim \textup{N}(\mu_p,\tau_p).
\end{eqnarray*}
In this case, the log conditional probability up to constants not involving $x$, $\sum_i \log p_i(c_i|x,\ldots) +\log p_x(x|\ldots)$, is equal to
\begin{eqnarray*}
	-\frac{1}{2}(c_i-F_ix-a_i)^T\tau_i(c_i-F_ix-a_i) - \frac{1}{2}(x-\mu_p)^T\tau_p(x-\mu_p),
\end{eqnarray*}
which after completing the square yields
\begin{eqnarray*}
	(x|\{c_i\},\ldots)\sim\textup{N}(\mu,\tau),\\
	\tau = \tau_p + \sum_i F_i^T \tau_i F_i,\\
	\mu = \tau^{-1}(\tau_p\mu_p + \sum_i F_i^T\tau_i(x_i-a_i)).
\end{eqnarray*}

This sampling method's constructor arguments are:
\begin{itemize}
	\item \texttt{parameter}: The parameter representing $x$.
	\item \texttt{F\_dict}: A dictionary, indexed by child objects, whose values are nodes whose values are the $F$ matrices.
	\item \texttt{a\_dict}: Same as \texttt{F\_dict}, but the values of the nodes are the $a$ arrays.
	\item \texttt{tau\_dict}: The obvious.
	\item \texttt{prior\_mu}: A node whose value is the prior mean of $x$.
	\item \texttt{prior\_tau}: Same as \texttt{prior\_mu}, but with the precision matrix.
\end{itemize}

\section{Nonconjugate}\label{non}
The sampling method \texttt{cMVNormalWithMVNormalChildren} applies if $x$'s prior $p_x(x)$ is non-normal. In this case, the log joint probability of $x$ up to unimportant constants, $\sum_i \log p_i(c_i|x,\ldots) + \log p_x(x)$, is equal to
\begin{eqnarray*}
	-\sum_i\frac{1}{2}(c_i-F_ix-a_i)^T\tau_i(c_i-F_ix-a_i) +\log p_x(x).
\end{eqnarray*}
A Metropolis-Hastings step algorithm for this distribution which tends to be efficient is the following:
\begin{enumerate}
	\item Propose a value $x^p$ for $x$ as if it had an uninformative prior ($\tau_p = \epsilon I$, with $\epsilon << 1$). That is,
	\begin{eqnarray*}
		(x_p|\{c_i\},\ldots)\sim\textup{N}(\mu,\tau),\\
		\tau = \sum_i F_i^T \tau_i F_i,\\
		\mu = \tau^{-1}\sum_i F_i^T\tau_i(x_i-a_i).
	\end{eqnarray*}
	\item Accept the jump with probability
	\begin{eqnarray*}
		\min\left\{1,\frac{p_x(x^p)}{p_x(x)}\right\}.
	\end{eqnarray*}
\end{enumerate}

This sampling method's constructor arguments are simply:
\begin{itemize}
	\item \texttt{parameter}: The parameter representing $x$.
	\item \texttt{F\_dict}: A dictionary, indexed by child objects, whose values are nodes whose values are the $F$ matrices.
	\item \texttt{a\_dict}: Same as \texttt{F\_dict}, but the values of the nodes are the $a$ arrays.
	\item \texttt{tau\_dict}: The obvious.
\end{itemize}

\section{Things that would be nice for Gibbs sampling in general}
I started a class called Gibbs in \texttt{PyMC2/special\_SamplingMethods/GibbsSampler.py}, but couldn't figure out how to make it work well. It would be nice if:
\begin{itemize}
	\item We come up with some consistent class factory for making conjugate/ nonconjugate pairs of Gibbs samplers, maybe similar to what we're doing in distributions.py.
	\item The accessory dictionaries that get passed in to the constructor should be allowed to contain parameters, nodes, or simple ndarrays. The PyMC objects should have their values extracted automatically somehow.
	\item Are we willing to insist on consistent parent keying schemes, to avoid having to pass in the accessory dictionaries? If so, we need to standardize across all Gibbs samplers and distributions.py. Compatibility for weird models will require heavy use of utility nodes.
	\item Each Gibbs sampler holds a OneAtATimeMetropolis instance called `default' or something which it uses if a Gibbs step fails.
\end{itemize}
\end{document}


\chapter{Convex functions}

\clearpage
\section{Basic properties and examples}

\clearpage
\section{Operations that preserve convexity}

\clearpage
\section{The conjugate function}

\clearpage
\section{Quasiconvex functions}

\clearpage
\section{Log-concave and log-convex functions}

\clearpage
\section{Convexity with respect to generalized inequalities}

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/067.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/069.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/072.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/073.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/076.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/077.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/085.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/091.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/096.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/099.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/100a.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/100b.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/113a.png}\hfil

\clearpage
\hfil\includegraphics[width=.5\textwidth]{../Graphics/113b.png}\hfil

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../LectureNotes-Optimization"
%%% End: