\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[USenglish]{babel}
\usepackage{csquotes}

\usepackage{amsmath,amssymb}
\usepackage{fullpage}

\usepackage{hyperref}

\usepackage[style=authoryear,maxbibnames=12,maxcitenames=2]{biblatex}
\addbibresource{refs.bib}

\usepackage[capitalize]{cleveref}

\DeclareMathOperator{\E}{\mathbb{E}}
\DeclareMathOperator{\Var}{Var}
\DeclareMathOperator{\Cov}{Cov}
\DeclareMathOperator{\mmd}{MMD}
\DeclareMathOperator{\mmdsqu}{\widehat{MMD}_U^2}
\newcommand{\tp}{^\mathsf{T}}

\newcommand{\Kxy}{\mathbf{K_{XY}}}
\newcommand{\Kxx}{\mathbf{K_{XX}}}
\newcommand{\Kyy}{\mathbf{K_{YY}}}
\newcommand{\Kxz}{\mathbf{K_{XZ}}}
\newcommand{\Kzz}{\mathbf{K_{ZZ}}}
\newcommand{\Ktxx}{\mathbf{\tilde{K}_{XX}}}
\newcommand{\Ktyy}{\mathbf{\tilde{K}_{YY}}}
\newcommand{\Ktzz}{\mathbf{\tilde{K}_{ZZ}}}

\newcommand{\one}{\mathbf{1}}

\newcommand{\X}{\mathcal{X}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\h}{\mathcal{H}}

\newcommand{\PX}{\mathbb{P}_X}
\newcommand{\PY}{\mathbb{P}_Y}
\newcommand{\PZ}{\mathbb{P}_Z}

\newcommand{\muX}{\mu_X}
\newcommand{\muY}{\mu_Y}
\newcommand{\muZ}{\mu_Z}

\newcommand{\setX}{\mathbf{X}}
\newcommand{\setY}{\mathbf{Y}}
\newcommand{\setZ}{\mathbf{Z}}
\newcommand{\setU}{\mathbf{U}}
\newcommand{\setW}{\mathbf{W}}

\makeatletter

\DeclareRobustCommand{\abs}{\@ifstar\@@abs\@abs}
\newcommand{\@abs}[1]{\left\lVert #1 \right\rvert}
\newcommand{\@@abs}[1]{\lVert #1 \rvert}

\DeclareRobustCommand{\norm}{\@ifstar\@@norm\@norm}
\newcommand{\@norm}[1]{\left\lVert #1 \right\rVert}
\newcommand{\@@norm}[1]{\lVert #1 \rVert}

\DeclareRobustCommand{\inner}{\@ifstar\@@inner\@inner}
\newcommand{\@inner}[2]{\left\langle #1, #2 \right\rangle}
\newcommand{\@@inner}[2]{\langle #1, #2 \rangle}

\makeatother

\title{Unbiased estimators for the variance of MMD estimators}
\author{D.\ J.\ Sutherland}
\date{June 2019}

\begin{document}
\maketitle
% \allowdisplaybreaks

\begin{abstract}
The maximum mean discrepancy (MMD) is a kernel-based distance between probability distributions useful in many applications \parencite{mmd-jmlr},
bearing a simple estimator with pleasing computational and statistical properties.
Being able to efficiently estimate the variance of this estimator is very helpful to various problems in two-sample testing.
Towards this end,
\textcite{three-sample} used the theory of U-statistics
to derive estimators for the variance of an MMD estimator,
and differences between two such estimators.
Their estimator, however, drops lower-order terms, and is unnecessarily biased.
We show in this note~--~extending and correcting work of \textcite{opt-mmd}~--~%
that we can find a truly unbiased estimator for the actual variance of both the squared MMD estimator and the difference of two correlated squared MMD estimators,
at essentially no additional computational cost.
\end{abstract}

We give only minimal background in this note;
see \textcite{three-sample,opt-mmd} for uses of these estimators.

Given a positive semidefinite kernel $k : \X \times \X \to \R$ corresponding to an RKHS $\h$,
there exists a feature map $\varphi : \X \to \h$
such that $k(X, Y) = \inner{\varphi(X)}{\varphi(Y)}$.
The mean embedding \parencite{mean-embeddings} of a distribution $\PX$ is
$\muX := \E_{X \sim \PX}[ \varphi(X) ] \in \h$,
and the MMD is merely the distance between mean embeddings:
\begin{align*}
  \mmd^2(\PX, \PY)
  &= \norm{\muX - \muY}^2
\\&=  \E_{X, X' \sim \PX}\left[ k(X, X') \right]
    + \E_{Y, Y' \sim \PY}\left[ k(Y, Y') \right]
    - 2 \E_{X \sim \PX, Y \sim \PY}\left[ k(X, Y) \right]
.\end{align*}

Suppose we have independent samples
$\setX := \{ X_i \}_{i=1}^m \sim \PX^m$,
$\setY := \{ Y_i \}_{i=1}^m \sim \PY^m$,
$\setZ := \{ Z_i \}_{i=1}^m \sim \PZ^m$.
The following is an unbiased estimator of $\mmd(\PX, \PY)$ with \emph{nearly} minimal variance among unbiased estimators \parencite{mmd-jmlr}:
\begin{equation} \label{eq:mmd-est}
    \mmdsqu(\setX, \setY)
    := \frac{1}{m (m - 1)} \sum_{i \ne j}^m \left[
        k(X_i, X_j) + k(Y_i, Y_j) - k(X_i, Y_j) - k(X_j, Y_i)
    \right]
.\end{equation}
Compared to the MVUE, terms of the form $k(X_i, Y_i)$ are dropped.
This estimator, however, is a $U$ statistic,
for which there is well-established theory \parencite[Chapter~5]{serfling},
including expressions for the variance.

We assume Bochner integrability throughout this note,
to be able to exchange expectations with inner products in $\h$.
This technical condition holds automatically for continuous bounded kernels
or for continuous kernels on compact domains,
but must be verified in other situations.

In this note, we first employ that theory to derive expressions for the variance in terms of various expectations of inner products in $\h$ (\cref{sec:var-expressions}).
Then, in \cref{sec:estimators}, we derive unbiased estimators for these expressions,
yielding the final results \eqref{eq:final-mmd-est} and \eqref{eq:final-diff-est}
which are unbiased variance estimators evaluable in the same $\mathcal{O}(m^2)$ time
it takes to evaluate \eqref{eq:mmd-est}.

\section{Variance expressions} \label{sec:var-expressions}

We will first derive expressions for the variances of
$\mmdsqu(\setX, \setY)$
and $\mmdsqu(\setX, \setY) - \mmdsqu(\setX, \setZ)$.
This section is quite similar to Appendix A of \textcite{three-sample},
but avoids unnecessarily dropping lower-order terms
(which provides almost no computational advantage, and may harm the accuracy for small sample sizes, although it does make for a less tedious derivation).
The result \eqref{eq:final-mmd-exp} of \cref{sec:mmd-expression} is identical to that of \textcite{opt-mmd}.

\subsection{Variance of the MMD estimator} \label{sec:mmd-expression}

Let $U_i$ denote the pair $(X_i, Y_i)$,
and define the function
\[
    h(U_1, U_2) := k(X_1, X_2) + k(Y_1, Y_2) - k(X_1, Y_2) - k(X_2, Y_1)
.\]
Then \[
\mmdsqu(\setX, \setY) = \frac{1}{m (m-1)} \sum_{i \ne j} h(U_i, U_j)
,\]
which is a $U$-statistic on the joint data $\setU$.
Thus standard results \parencite[Section 5.2.1, Lemma A]{serfling}
give us
\[
    \Var\left[ \mmdsqu(\setX, \setY) \right]
    = V_m
    := \frac{4 (m-2)}{m (m-1)} \zeta_1 + \frac{2}{m (m-1)} \zeta_2
,\] where
\[
    \zeta_1 := \Var_{U_1}\left[ \E_{U_2}\left[ h(U_1, U_2) \right] \right],
    \qquad
    \zeta_2 := \Var_{U_1, U_2}\left[ h(U_1, U_2) \right]
.\]

The first-order term $\zeta_1$ is:
\begin{align*}
    \zeta_1
  &=
    \Var_{U_1}\left[ \E_{U_2}\left[ h(U_1, U_2) \right] \right]
\\&=
    \Var_{X_1, Y_1}\left[
      \E_{X_2} k(X_1, X_2)
    + \E_{Y_2} k(Y_1, Y_2)
    - \E_{Y_2} k(X_1, Y_2)
    - \E_{X_2} k(X_2, Y_1)
    \right]
\\&=
    \Var_{X, Y}\left[
      \inner{\varphi(X)}{\muX}
    + \inner{\varphi(Y)}{\muY}
    - \inner{\varphi(X)}{\muY}
    - \inner{\muX}{\varphi(Y)}
    \right]
\\&=
    \Var\left[ \inner{\varphi(X)}{\muX} \rangle \right]
  + \Var\left[ \inner{\varphi(Y)}{\muY} \rangle \right]
  + \Var\left[ \inner{\varphi(X)}{\muY} \rangle \right]
  + \Var\left[ \inner{\muX}{\varphi(Y)} \rangle \right]
\\&\quad
  - 2 \Cov\left( \inner{\varphi(X)}{\muX}, \inner{\varphi(X)}{\muY} \right)
  - 2 \Cov\left( \inner{\varphi(Y)}{\muY}, \inner{\muX}{\varphi(Y)} \right)
.\end{align*}
Noting that
\begin{align*}
    \Var\left[ \inner{\varphi(A)}{\mu_B} \right]
  &=
    \E\left[ \inner{\varphi(A)}{\mu_B}^2 \right]
  - \inner{\mu_A}{\mu_B}^2
\\
    \Cov\left( \inner{\varphi(A)}{\mu_B}, \inner{\varphi(A)}{\mu_C} \right)
  &=
    \E\left[ \inner{\varphi(A)}{\mu_B} \inner{\varphi(A)}{\mu_C} \right]
  - \inner{\mu_A}{\mu_B} \inner{\mu_A}{\mu_C}
\end{align*}
we have
\begin{align*}
    \zeta_1
&=
    \E\left[ \inner{\varphi(X)}{\muX}^2 \right] - \inner{\muX}{\muX}^2
\\&\quad
  + \E\left[ \inner{\varphi(Y)}{\muY}^2 \right] - \inner{\muY}{\muY}^2
\\&\quad
  + \E\left[ \inner{\varphi(X)}{\muY}^2 \right] - \inner{\muX}{\muY}^2
\\&\quad
  + \E\left[ \inner{\varphi(Y)}{\muX}^2 \right] - \inner{\muY}{\muX}^2
\\&\quad
  - 2 \E\left[ \inner{\varphi(X)}{\muX} \inner{\varphi(X)}{\muY} \right]
  + 2 \inner{\muX}{\muX} \inner{\muX}{\muY}
\\&\quad
 - 2 \E\left[ \inner{\varphi(Y)}{\muY} \inner{\varphi(Y)}{\muX} \right]
 + 2 \inner{\muY}{\muY} \inner{\muX}{\muY}
.\end{align*}

We can similarly compute the second-order term $\zeta_2$ as:
\begin{align*}
\zeta_2
  &= \Var\left[ h(U_1, U_2) \right]
\\&= \Var\left[ k(X_1, X_2) + k(Y_1, Y_2) - k(X_1, Y_2) - k(X_2, Y_1) \right]
\\&=
    \Var\left[ k(X_1, X_2) \right]
  + \Var\left[ k(Y_1, Y_2) \right]
  + \Var\left[ k(X_1, Y_2) \right]
  + \Var\left[ k(X_2, Y_1) \right]
\\&\quad
  - 2 \Cov\left( k(X_1, X_2), k(X_1, Y_2) \right)
  - 2 \Cov\left( k(X_1, X_2), k(X_2, Y_1) \right)
\\&\quad
  - 2 \Cov\left( k(Y_1, Y_2), k(X_1, Y_2) \right)
  - 2 \Cov\left( k(Y_1, Y_2), k(X_2, Y_1) \right)
\\&=
    \Var\left[ k(X_1, X_2) \right]
  + \Var\left[ k(Y_1, Y_2) \right]
  + 2 \Var\left[ k(X, Y) \right]
\\&\quad
  - 4 \Cov\left( k(X_1, X_2), k(X_1, Y) \right)
  - 4 \Cov\left( k(Y_1, Y_2), k(Y_1, X) \right)
\\&=
    \E\left[ k(X_1, X_2)^2 \right] - \inner{\muX}{\muX}^2
\\&\quad
  + \E\left[ k(Y_1, Y_2)^2 \right] - \inner{\muY}{\muY}^2
\\&\quad
  + 2 \E\left[ k(X, Y)^2 \right] - 2 \inner{\muX}{\muY}^2
\\&\quad
  - 4 \E\left[ \inner{\varphi(X)}{\muX} \inner{\varphi(X)}{\muY} \right]
  + 4 \inner{\muX}{\muX} \inner{\muX}{\muY}
\\&\quad
  - 4 \E\left[ \inner{\varphi(Y)}{\muY} \inner{\varphi(Y)}{\muX} \right]
  + 4 \inner{\muY}{\muY} \inner{\muX}{\muY}
.\end{align*}

Combining the two yields
an expression for $\Var[\mmdsqu(\setX, \setY)]$ of
\begin{align*}
    V_m
  &= \frac{2}{m (m-1)} \Big[ 2 (m-2) \zeta_1 + \zeta_2 \Big]
\\&= \frac{2}{m (m-1)} \Bigg[
    2 (m-2) \E\left[ \inner{\varphi(X)}{\muX}^2 \right]
  - 2 (m-2) \inner{\muX}{\muX}^2
\\&\qquad\qquad\qquad
  + 2 (m-2) \E\left[ \inner{\varphi(Y)}{\muY}^2 \right]
  - 2 (m-2) \inner{\muY}{\muY}^2
\\&\qquad\qquad\qquad
  + 2 (m-2) \E\left[ \inner{\varphi(X)}{\muY}^2 \right]
  - 2 (m-2) \inner{\muX}{\muY}^2
\\&\qquad\qquad\qquad
  + 2 (m-2) \E\left[ \inner{\varphi(Y)}{\muX}^2 \right]
  - 2 (m-2) \inner{\muY}{\muX}^2
\\&\qquad\qquad\qquad
  - 4 (m-2) \E\left[ \inner{\varphi(X)}{\muX} \inner{\varphi(X)}{\muY} \right]
  + 4 (m-2) \inner{\muX}{\muX} \inner{\muX}{\muY}
\\&\qquad\qquad\qquad
  - 4 (m-2) \E\left[ \inner{\varphi(Y)}{\muY} \inner{\varphi(Y)}{\muX} \right]
  + 4 (m-2) \inner{\muY}{\muY} \inner{\muX}{\muY}
\\&\qquad\qquad\qquad
  + \E\left[ k(X_1, X_2)^2 \right] - \inner{\muX}{\muX}^2
\\&\qquad\qquad\qquad
  + \E\left[ k(Y_1, Y_2)^2 \right] - \inner{\muY}{\muY}^2
\\&\qquad\qquad\qquad
  + 2 \E\left[ k(X, Y)^2 \right] - 2 \inner{\muX}{\muY}^2
\\&\qquad\qquad\qquad
  - 4 \E\left[ \inner{\varphi(X)}{\muX} \inner{\varphi(X)}{\muY} \right]
  + 4 \inner{\muX}{\muX} \inner{\muX}{\muY}
\\&\qquad\qquad\qquad
  - 4 \E\left[ \inner{\varphi(Y)}{\muY} \inner{\varphi(Y)}{\muX} \right]
  + 4 \inner{\muY}{\muY} \inner{\muX}{\muY}
  \Bigg]
\end{align*}
and so
\begin{equation} \label{eq:final-mmd-exp}
\begin{aligned}
    V_m
  &= \frac{2}{m (m-1)} \Bigg[
    2 (m-2) \E\left[ \inner{\varphi(X)}{\muX}^2 \right]
  - (2 m - 3) \inner{\muX}{\muX}^2
\\&\qquad\qquad\qquad
  + 2 (m-2) \E\left[ \inner{\varphi(Y)}{\muY}^2 \right]
  - (2 m - 3) \inner{\muY}{\muY}^2
\\&\qquad\qquad\qquad
  + 2 (m-2) \left( \E\left[ \inner{\varphi(X)}{\muY}^2 \right] + \E\left[ \inner{\varphi(Y)}{\muX}^2 \right] \right)
  - (4 m - 6) \inner{\muX}{\muY}^2
\\&\qquad\qquad\qquad
  - 4 (m-1) \E\left[ \inner{\varphi(X)}{\muX} \inner{\varphi(X)}{\muY} \right]
  + 4 (m-1) \inner{\muX}{\muX} \inner{\muX}{\muY}
\\&\qquad\qquad\qquad
  - 4 (m-1) \E\left[ \inner{\varphi(Y)}{\muY} \inner{\varphi(Y)}{\muX} \right]
  + 4 (m-1) \inner{\muY}{\muY} \inner{\muX}{\muY}
\\&\qquad\qquad\qquad
  + \E\left[ k(X_1, X_2)^2 \right]
  + \E\left[ k(Y_1, Y_2)^2 \right]
  + 2 \E\left[ k(X, Y)^2 \right]
  \Bigg]
.\end{aligned}
\end{equation}

We can see that, compared to $\xi_1$,
\eqref{eq:final-mmd-exp} mostly just tweaks constants.
The only new terms are the expectations of squared kernels,
which as we'll see later will in fact not introduce any new computational expense
as their estimators will combine with terms needed to correct biases in the other terms.

\subsection{Variance of the difference of MMD estimators} \label{sec:diff-expression}

Let $W_i := (X_i, Y_i, Z_i)$, and
\[
f(W_1, W_2) :=
\left( k(Y_1, Y_2) - k(X_1, Y_2) - k(X_2, Y_1) \right)
- \left( k(Z_1, Z_2) - k(X_1, Z_2) - k(X_2, Z_1) \right)
.\]
Then
\[
  \mmdsqu(\setX, \setY) - \mmdsqu(\setX, \setZ)
  = \frac{1}{m (m-1)} \sum_{i \ne j} f(W_i, W_j)
\]
is a $U$-statistic on $\setW$, so again
\begin{gather*}
  \Var[\mmdsqu(\setX, \setY) - \mmdsqu(\setX, \setZ)]
  = \nu_m
  := \frac{4 (m-2)}{m (m-1)} \xi_1 + \frac{2}{m (m-1)} \xi_2
\\
  \xi_1 := \Var_{W_1}\left[ \E_{W_2}\left[ f(W_1, W_2) \right] \right]
  \qquad
  \xi_2 := \Var_{W_1, W_2}\left[ f(W_1, W_2) \right]
.\end{gather*}

We can proceed as before, but with more terms.
The first-order term $\xi_1$ is
\begin{align*}
     \xi_1
  &= \Var_{W_1}\left[ \E_{W_2}\left[ f(W_1, W_2 )\right] \right]
\\&=
  \Var_{X, Y, Z}\left[
    \inner{\varphi(Y)}{\muY}
  - \inner{\varphi(X)}{\muY}
  - \inner{\muX}{\varphi(Y)}
  - \inner{\varphi(Z)}{\muZ}
  + \inner{\varphi(X)}{\muZ}
  + \inner{\muX}{\varphi(Z)}
  \right]
\\&=
    \Var\left[ \inner{\varphi(X)}{\muY} \right]
  + \Var\left[ \inner{\varphi(X)}{\muZ} \right]
  + \Var\left[ \inner{\varphi(Y)}{\muX} \right]
  + \Var\left[ \inner{\varphi(Y)}{\muY} \right]
  + \Var\left[ \inner{\varphi(Z)}{\muX} \right]
  + \Var\left[ \inner{\varphi(Z)}{\muZ} \right]
\\&\qquad
  - 2 \Cov\left( \inner{\varphi(X)}{\muY}, \inner{\varphi(X)}{\muZ} \right)
  - 2 \Cov\left( \inner{\varphi(Y)}{\muX}, \inner{\varphi(Y)}{\muY} \right)
  - 2 \Cov\left( \inner{\varphi(Z)}{\muX}, \inner{\varphi(Z)}{\muZ} \right)
\\&=
    \E\left[ \inner{\varphi(X)}{\muY}^2 \right]
  - \inner{\muX}{\muY}^2
\\&\qquad
  + \E\left[ \inner{\varphi(X)}{\muZ}^2 \right]
  - \inner{\muX}{\muZ}^2
\\&\qquad
  + \E\left[ \inner{\varphi(Y)}{\muX}^2 \right]
  - \inner{\muY}{\muX}^2
\\&\qquad
  + \E\left[ \inner{\varphi(Y)}{\muY}^2 \right]
  - \inner{\muY}{\muY}^2
\\&\qquad
  + \E\left[ \inner{\varphi(Z)}{\muX}^2 \right]
  - \inner{\muZ}{\muX}^2
\\&\qquad
  + \E\left[ \inner{\varphi(Z)}{\muZ}^2 \right]
  - \inner{\muZ}{\muZ}^2
\\&\qquad
  - 2 \E\left[ \inner{\varphi(X)}{\muY} \inner{\varphi(X)}{\muZ} \right]
  + 2 \inner{\muX}{\muY} \inner{\muX}{\muZ}
\\&\qquad
  - 2 \E\left[ \inner{\varphi(Y)}{\muX} \inner{\varphi(Y)}{\muY} \right]
  + 2 \inner{\muX}{\muY} \inner{\muY}{\muY}
\\&\qquad
  - 2 \E\left[ \inner{\varphi(Z)}{\muX} \inner{\varphi(Z)}{\muZ} \right]
  + 2 \inner{\muX}{\muZ} \inner{\muZ}{\muZ}
.\end{align*}

The second-order term $\xi_2$ is
\begin{align*}
    \xi_2
  &=
    \Var\left[ - k(X_1, Y_2) - k(X_2, Y_1) + k(X_1, Z_2) + k(X_2, Z_1) + k(Y_1, Y_2) - k(Z_1, Z_2) \right]
\\&=
    \Var\left[ k(X_1, Y_2) \right]
  + \Var\left[ k(X_2, Y_1) \right]
  + \Var\left[ k(X_1, Z_2) \right]
  + \Var\left[ k(X_2, Z_1) \right]
  + \Var\left[ k(Y_1, Y_2) \right]
  + \Var\left[ k(Z_1, Z_2) \right]
\\&\qquad
  - 2 \Cov\left( k(X_1, Y_2), k(X_1, Z_2) \right)
  - 2 \Cov\left( k(X_1, Y_2), k(Y_1, Y_2) \right)
\\&\qquad
  - 2 \Cov\left( k(X_2, Y_1), k(X_2, Z_1) \right)
  - 2 \Cov\left( k(X_2, Y_1), k(Y_1, Y_2) \right)
\\&\qquad
  - 2 \Cov\left( k(X_1, Z_2), k(Z_1, Z_2) \right)
  - 2 \Cov\left( k(X_2, Z_1), k(Z_1, Z_2) \right)
%
\\&=
    2 \Var\left[ k(X, Y) \right]
  + 2 \Var\left[ k(X, Z) \right]
  + \Var\left[ k(Y_1, Y_2) \right]
  + \Var\left[ k(Z_1, Z_2) \right]
\\&\qquad
  - 4 \Cov\left( k(X, Y), k(X, Z) \right)
  - 4 \Cov\left( k(X, Y_1), k(Y_1, Y_2) \right)
  - 4 \Cov\left( k(X, Z_1), k(Z_1, Z_2) \right)
%
\\&=
    2 \E\left[ k(X, Y)^2 \right]
  - 2 \inner{\muX}{\muY}^2
\\&\qquad
  + 2 \E\left[ k(X, Z)^2 \right]
  - 2 \inner{\muX}{\muZ}^2
\\&\qquad
  + \E\left[ k(Y_1, Y_2)^2 \right]
  - \inner{\muY}{\muY}^2
\\&\qquad
  + \E\left[ k(Z_1, Z_2)^2 \right]
  - \inner{\muZ}{\muZ}^2
\\&\qquad
  - 4 \E\left[ \inner{\varphi(X)}{\muY} \inner{\varphi(X)}{\muZ} \right]
  + 4 \inner{\muX}{\muY} \inner{\muX}{\muZ}
\\&\qquad
  - 4 \E\left[ \inner{\varphi(Y)}{\muX} \inner{\varphi(Y)}{\muY} \right]
  + 4 \inner{\muX}{\muY} \inner{\muY}{\muY}
\\&\qquad
  - 4 \E\left[ \inner{\varphi(Z)}{\muX} \inner{\varphi(Z)}{\muZ} \right]
  + 4 \inner{\muX}{\muZ} \inner{\muZ}{\muZ}
.\end{align*}

Combining the two
gives an expression for $\Var\left[\mmdsqu(\setX, \setY) - \mmdsqu(\setX, \setZ)\right]$ of
\begin{align*}
    \nu_m
  &= \frac{2}{m (m-1)} \Big[ 2 (m-2) \xi_1 + \xi_2 \Big]
\\&= \frac{2}{m (m-1)} \Bigg[
    2(m-2) \E\left[ \inner{\varphi(X)}{\muY}^2 \right]
  - 2(m-2) \inner{\muX}{\muY}^2
\\&\qquad\qquad\qquad
  + 2(m-2) \E\left[ \inner{\varphi(X)}{\muZ}^2 \right]
  - 2(m-2) \inner{\muX}{\muZ}^2
\\&\qquad\qquad\qquad
  + 2(m-2) \E\left[ \inner{\varphi(Y)}{\muX}^2 \right]
  - 2(m-2) \inner{\muY}{\muX}^2
\\&\qquad\qquad\qquad
  + 2(m-2) \E\left[ \inner{\varphi(Y)}{\muY}^2 \right]
  - 2(m-2) \inner{\muY}{\muY}^2
\\&\qquad\qquad\qquad
  + 2(m-2) \E\left[ \inner{\varphi(Z)}{\muX}^2 \right]
  - 2(m-2) \inner{\muZ}{\muX}^2
\\&\qquad\qquad\qquad
  + 2(m-2) \E\left[ \inner{\varphi(Z)}{\muZ}^2 \right]
  - 2(m-2) \inner{\muZ}{\muZ}^2
\\&\qquad\qquad\qquad
  - 4(m-2) \E\left[ \inner{\varphi(X)}{\muY} \inner{\varphi(X)}{\muZ} \right]
  + 4(m-2) \inner{\muX}{\muY} \inner{\muX}{\muZ}
\\&\qquad\qquad\qquad
  - 4(m-2) \E\left[ \inner{\varphi(Y)}{\muX} \inner{\varphi(Y)}{\muY} \right]
  + 4(m-2) \inner{\muX}{\muY} \inner{\muY}{\muY}
\\&\qquad\qquad\qquad
  - 4(m-2) \E\left[ \inner{\varphi(Z)}{\muX} \inner{\varphi(Z)}{\muZ} \right]
  + 4(m-2) \inner{\muX}{\muZ} \inner{\muZ}{\muZ}
\\&\qquad\qquad\qquad
  + 2 \E\left[ k(X, Y)^2 \right]
  - 2 \inner{\muX}{\muY}^2
\\&\qquad\qquad\qquad
  + 2 \E\left[ k(X, Z)^2 \right]
  - 2 \inner{\muX}{\muZ}^2
\\&\qquad\qquad\qquad
  + \E\left[ k(Y_1, Y_2)^2 \right]
  - \inner{\muY}{\muY}^2
\\&\qquad\qquad\qquad
  + \E\left[ k(Z_1, Z_2)^2 \right]
  - \inner{\muZ}{\muZ}^2
\\&\qquad\qquad\qquad
  - 4 \E\left[ \inner{\varphi(X)}{\muY} \inner{\varphi(X)}{\muZ} \right]
  + 4 \inner{\muX}{\muY} \inner{\muX}{\muZ}
\\&\qquad\qquad\qquad
  - 4 \E\left[ \inner{\varphi(Y)}{\muX} \inner{\varphi(Y)}{\muY} \right]
  + 4 \inner{\muX}{\muY} \inner{\muY}{\muY}
\\&\qquad\qquad\qquad
  - 4 \E\left[ \inner{\varphi(Z)}{\muX} \inner{\varphi(Z)}{\muZ} \right]
  + 4 \inner{\muX}{\muZ} \inner{\muZ}{\muZ}
  \Bigg]
\end{align*}
so that
\begin{equation} \label{eq:final-diff-exp}
\begin{aligned}
    \nu_m
  &= \frac{2}{m (m-1)} \Bigg[
    2(m-2) \left[
      \E\left[ \inner{\varphi(X)}{\muY}^2 \right]
    + \E\left[ \inner{\varphi(X)}{\muZ}^2 \right]
    + \E\left[ \inner{\varphi(Y)}{\muX}^2 \right]
    + \E\left[ \inner{\varphi(Z)}{\muX}^2 \right]
    \right]
\\&\qquad\qquad\qquad
  + 2(m-2) \left[
      \E\left[ \inner{\varphi(Y)}{\muY}^2 \right]
    + \E\left[ \inner{\varphi(Z)}{\muZ}^2 \right]
    \right]
\\&\qquad\qquad\qquad
  - 2(2m-3) \left[
      \inner{\muX}{\muY}^2
    + \inner{\muX}{\muZ}^2
    \right]
  - (2m-3) \left[
      \inner{\muY}{\muY}^2
    + \inner{\muZ}{\muZ}^2
    \right]
\\&\qquad\qquad\qquad
  + 4(m-1) \left[
      \inner{\muX}{\muY} \inner{\muX}{\muZ}
    + \inner{\muX}{\muY} \inner{\muY}{\muY}
    + \inner{\muX}{\muZ} \inner{\muZ}{\muZ}
  \right]
\\&\qquad\qquad\qquad
  - 4(m-1) \E\left[ \inner{\varphi(X)}{\muY} \inner{\varphi(X)}{\muZ} \right]
\\&\qquad\qquad\qquad
  - 4(m-1) \E\left[ \inner{\varphi(Y)}{\muX} \inner{\varphi(Y)}{\muY} \right]
\\&\qquad\qquad\qquad
  - 4(m-1) \E\left[ \inner{\varphi(Z)}{\muX} \inner{\varphi(Z)}{\muZ} \right]
\\&\qquad\qquad\qquad
  + 2 \E\left[ k(X, Y)^2 \right]
  + 2 \E\left[ k(X, Z)^2 \right]
  + \E\left[ k(Y_1, Y_2)^2 \right]
  + \E\left[ k(Z_1, Z_2)^2 \right]
  \Bigg]
.\end{aligned}
\end{equation}

\section{Estimators of terms} \label{sec:estimators}

\subsection{Sub-expressions} \label{sec:sub-estimators}
We now give unbiased estimators of the various terms in the variance results of \cref{sec:var-expressions}.

Define an $m \times m$ matrix $\Kxy$ by $(\Kxy)_{ij} = k(X_i, Y_j)$,
and $\Kxz$, $\Kxx$, $\Kyy$, $\Kzz$ similarly.
Let $\Ktxx$, $\Ktyy$, $\Ktzz$ be $\Kxx$, $\Kyy$, $\Kzz$ with their diagonals set to zero.
Let $\one$ be the $m$-vector of all ones.
We'll also use the falling factorial notation
$(m)_k := m (m-1) \cdots (m - k + 1)$.

For unbiased estimators, the important thing is to subtract off elements of sums which share data points. For example,
\begin{gather*}
\inner{\muX}{\muY}
= \inner{\E_X \varphi(X)}{\E_Y \varphi(Y)}
= \E_{X, Y} k(X, Y)
\approx \frac{1}{m^2} \sum_{i,j} k(X_i, Y_j)
   = \frac{1}{m^2} \one\tp \Kxy \one
\\
\inner{\muX}{\muX}
= \inner{\E_X \varphi(X)}{\E_{X'} \varphi(X')}
= \E_{X, X'} k(X, X')
\approx \frac{1}{m (m-1)} \sum_{i \ne j} k(X_i, X_j)
   = \frac{1}{m (m-1)} \one\tp \Ktxx \one
.\end{gather*}
It is also important to do so for products of these terms:
this caused the bias present in the publication version of \textcite{opt-mmd}.
For instance,
the square of an unbiased estimator for $\inner{\muX}{\muY}$
is not unbiased for $\inner{\muX}{\muY}^2$,
but the following is:
\begin{align*}
     \inner{\muX}{\muY}^2
  &= \inner{\muX}{\muY} \inner{\muX}{\muY}
   = \E_{X, X', Y, Y'}\left[ k(X, Y) k(X', Y') \right]
\\&\approx
    \frac{1}{m^2} \sum_{i,j} k(X_i, Y_j)
      \frac{1}{(m-1)^2} \sum_{i' \ne i} \sum_{j' \ne j} k(X_{i'}, Y_{j'})
\\&=
    \frac{1}{m^2 (m-1)^2} \sum_{i,j} k(X_i, Y_j) \left[
      \sum_{i',j'} k(X_{i'}, Y_{j'})
    - \sum_{i'} k(X_{i'}, Y_j)
    - \sum_{j'} k(X_i, Y_{j'})
    + k(X_i, Y_j)
    \right]
\\&= \frac{1}{m^2 (m-1)^2} \left[
      \sum_{i,j,i',j'} (\Kxy)_{ij} (\Kxy)_{i'j'}
    - \sum_{i,j,i'} (\Kxy)_{ij} (\Kxy)_{i'j}
    - \sum_{i,j,j'} (\Kxy)_{ij} (\Kxy)_{ij'}
    + \sum_{i,j} (\Kxy)_{ij}^2
    \right]
\\&= \frac{1}{m^2 (m-1)^2} \left[
      \left( \one\tp \Kxy \one \right)^2
    - \one\tp \Kxy \Kxy\tp \one
    - \one\tp \Kxy\tp \Kxy \one
    + \norm{\Kxy}_F^2
    \right]
\\&= \frac{1}{m^2 (m-1)^2} \left[
      \left( \one\tp \Kxy \one \right)^2
    - \norm{\Kxy\tp \one}^2
    - \norm{\Kxy \one}^2
    + \norm{\Kxy}_F^2
    \right]
.\end{align*}
Similarly,
\begin{align*}
     \inner{\muX}{\muX}^2
  &= \E_{X_1, X_2, X_3, X_4}\left[ k(X_1, X_2) k(X_3, X_4) \right]
\\&\approx
    \frac{1}{(m)_4} \sum_{i} \sum_{j \ne i} k(X_i, X_j) \sum_{a \notin \{i, j\}} \sum_{b \notin \{i, j, a\}} k(X_a, X_b)
\\&=
    \frac{1}{(m)_4} \sum_{ij} (\Ktxx)_{ij} \sum_{a, b \notin \{i, j\}} (\Ktxx)_{ab}
\\&=
    \frac{1}{(m)_4} \sum_{ij} (\Ktxx)_{ij} \Bigg[
      \sum_{a b} (\Ktxx)_{ab}
    - \sum_a (\Ktxx)_{ai}
    - \sum_a (\Ktxx)_{aj}
    - \sum_b (\Ktxx)_{ib}
    - \sum_b (\Ktxx)_{jb}
\\&\qquad\qquad\qquad\qquad\qquad
    + (\Ktxx)_{ii}
    + (\Ktxx)_{ij}
    + (\Ktxx)_{ji}
    + (\Ktxx)_{jj}
    \Bigg]
\\&=
    \frac{1}{(m)_4} \sum_{ij} (\Ktxx)_{ij} \Bigg[
      \sum_{a b} (\Ktxx)_{ab}
    - 2 \sum_a (\Ktxx)_{ai}
    - 2 \sum_a (\Ktxx)_{aj}
    + 2 (\Ktxx)_{ij}
    \Bigg]
\\&=
    \frac{1}{(m)_4} \Bigg[
      \sum_{i j a b} (\Ktxx)_{ij} (\Ktxx)_{ab}
    - 2 \sum_{i j a} (\Ktxx)_{ij} (\Ktxx)_{ai}
    - 2 \sum_{i j a} (\Ktxx)_{ij} (\Ktxx)_{aj}
    + 2 \sum_{ij} (\Ktxx)_{ij}^2
    \Bigg]
\\&=
    \frac{1}{(m)_4} \Bigg[
      \left( \one\tp \Ktxx \one \right)^2
    - 4 \norm{\Ktxx \one}^2
    + 2 \norm{\Ktxx}_F^2
    \Bigg]
.\end{align*}
We also need
\begin{align*}
    \inner{\muX}{\muX} \inner{\muX}{\muY}
  &= \E_{X_1, X_2, X_3, Y}\left[ k(X_1, X_2) k(X_3, Y) \right]
\\&\approx
    \frac{1}{m (m)_3} \sum_i \sum_{j \ne i} k(X_i, X_j)
      \sum_{\ell \notin \{i, j\}} \sum_a k(X_\ell, Y_a)
\\&=
    \frac{1}{m (m)_3} \sum_{ij} (\Ktxx)_{ij} \left[
      \sum_{\ell a} (\Kxy)_{\ell a}
    - \sum_a (\Kxy)_{i a}
    - \sum_a (\Kxy)_{j a}
    \right]
\\&=
    \frac{1}{m (m)_3} \left[
      \sum_{i j \ell a} (\Ktxx)_{ij} (\Kxy)_{\ell a}
    - \sum_{i j a} (\Ktxx)_{ij} (\Kxy)_{i a}
    - \sum_{i j a} (\Ktxx)_{ij} (\Kxy)_{j a}
    \right]
\\&=
    \frac{1}{m (m)_3} \left[
      \one\tp \Ktxx \one \one\tp \Kxy \one
    - 2 \, \one\tp \Ktxx \Kxy \one
    \right]
\end{align*}
and
\begin{align*}
    \inner{\muX}{\muY} \inner{\muX}{\muZ}
  &\approx
    \frac{1}{m^3 (m-1)} \sum_i \sum_a \sum_{j \ne i} \sum_b k(X_i, Y_a) k(X_j, Z_b)
\\&= \frac{1}{m^3 (m-1)} \left[
      \sum_{i j a b} (\Kxy)_{ia} (\Kxz)_{jb}
    - \sum_{i a b} (\Kxy)_{ia} (\Kxz)_{ib}
    \right]
\\&= \frac{1}{m^3 (m-1)} \left[
      \one\tp \Kxy \one \one\tp \Kxz \one
    - \one\tp \Kxy\tp \Kxz \one
    \right]
.\end{align*}

We also need some similar terms with $\varphi(X)$ shared:
\begin{align*}
    \E\left[ \inner{\varphi(X)}{\muX}^2 \right]
  &\approx
    \frac{1}{(m)_3} \sum_i \sum_{j \ne i} \sum_{\ell \notin \{i, j\}} k(X_i, X_j) k(X_i, X_\ell)
\\&= \frac{1}{(m)_3} \sum_{ij} \sum_{\ell \notin \{i, j\}} (\Ktxx)_{ij} k(X_i, X_\ell)
\\&= \frac{1}{(m)_3} \sum_{ij} \sum_{\ell \ne j} (\Ktxx)_{ij} (\Ktxx)_{i\ell}
\\&= \frac{1}{(m)_3} \left[
      \sum_{ij\ell} (\Ktxx)_{ij} (\Ktxx)_{i\ell}
    - \sum_{ij} (\Ktxx)_{ij}^2
    \right]
\\&= \frac{1}{(m)_3} \left[
      \one\tp \Ktxx \Ktxx \one
    - \norm{\Ktxx}_F^2
    \right]
\\&= \frac{1}{(m)_3} \left[
      \norm{\Ktxx \one}^2
    - \norm{\Ktxx}_F^2
    \right]
,\end{align*}
\begin{align*}
% \\
    \E\left[ \inner{\varphi(X)}{\muY}^2 \right]
  &\approx
    \frac{1}{m^2 (m-1)} \sum_i \sum_j \sum_{\ell \ne j} k(X_i, Y_j) k(X_i, Y_\ell)
\\&= \frac{1}{m^2 (m-1)} \left[
      \sum_{i j \ell} (\Kxy)_{ij} (\Kxy)_{i\ell}
    - \sum_{i j} (\Kxy)_{ij}^2
    \right]
% \\&= \frac{1}{m^2 (m-1)} \left[
%       \one\tp \Kxy\tp \Kxy \one
%     - \norm{\Kxy}_F^2
%     \right]
\\&= \frac{1}{m^2 (m-1)} \left[
      \norm{\Kxy \one}^2
    - \norm{\Kxy}_F^2
    \right]
,\end{align*}
\begin{align*}
    \E\left[ \inner{\varphi(X)}{\muX} \inner{\varphi(X)}{\muY} \right]
  &\approx
    \frac{1}{m^2 (m-1)} \sum_i \sum_{j \ne i} \sum_{\ell} k(X_i, X_j) k(X_i, Y_\ell)
\\&= \frac{1}{m^2 (m-1)} \sum_{i j \ell} (\Ktxx)_{ij} (\Kxy)_{i \ell}
\\&= \frac{1}{m^2 (m-1)} \one\tp \Ktxx \Kxy \one
,\end{align*}
and
\begin{align*}
    \E\left[ \inner{\varphi(X)}{\muY} \inner{\varphi(X)}{\muZ} \right]
  &\approx
    \frac{1}{m^3} \sum_{ij\ell} k(X_i, Y_j) k(X_i, Z_\ell)
   = \frac{1}{m^3} \one\tp \Kxy\tp \Kxz \one
.\end{align*}

Finally, we need a few terms that don't involve any mean embeddings:
\begin{align*}
    \E\left[ k(X_1, X_2)^2 \right]
  &\approx \frac{1}{m (m-1)} \sum_{i \ne j} k(X_i, X_j)^2
   = \frac{1}{m (m-1)} \norm{\Ktxx}_F^2
\\
    \E\left[ k(X, Y)^2 \right]
  &\approx \frac{1}{m^2} \norm{\Kxy}_F^2
.\end{align*}

\subsection{Final MMD variance estimator}
Recall the variance $V_m = \Var\left[ \mmdsqu(\setX, \setY) \right]$ of \eqref{eq:final-mmd-exp}:
\begin{align*}
    V_m
  &=
    \frac{4 (m-2)}{m (m-1)} \E\left[ \inner{\varphi(X)}{\muX}^2 \right]
  - \frac{2 (2 m - 3)}{m (m-1)} \inner{\muX}{\muX}^2
\\&\qquad
  + \frac{4 (m-2)}{m (m-1)} \E\left[ \inner{\varphi(Y)}{\muY}^2 \right]
  - \frac{2 (2 m - 3)}{m (m-1)} \inner{\muY}{\muY}^2
\\&\qquad
  + \frac{4 (m-2)}{m (m-1)} \left( \E\left[ \inner{\varphi(X)}{\muY}^2 \right] + \E\left[ \inner{\varphi(Y)}{\muX}^2 \right] \right)
  - \frac{4 (2 m - 3)}{m (m-1)} \inner{\muX}{\muY}^2
\\&\qquad
  - \frac{8}{m} \E\left[ \inner{\varphi(X)}{\muX} \inner{\varphi(X)}{\muY} \right]
  + \frac{8}{m} \inner{\muX}{\muX} \inner{\muX}{\muY}
\\&\qquad
  - \frac{8}{m} \E\left[ \inner{\varphi(Y)}{\muY} \inner{\varphi(Y)}{\muX} \right]
  + \frac{8}{m} \inner{\muY}{\muY} \inner{\muX}{\muY}
\\&\qquad
  + \frac{2}{m (m-1)} \E\left[ k(X_1, X_2)^2 \right]
  + \frac{2}{m (m-1)} \E\left[ k(Y_1, Y_2)^2 \right]
  + \frac{4}{m (m-1)} \E\left[ k(X, Y)^2 \right]
.\end{align*}
Plugging in the estimators of \cref{sec:sub-estimators}, we at last get an estimator for the variance:
\begin{align*}
    \hat V_m
  &=
  \frac{4}{m^2 (m-1)^2} \left[
      \norm{\Ktxx \one}^2
    + \norm{\Ktyy \one}^2
    - \norm{\Ktxx}_F^2
    - \norm{\Ktyy}_F^2
    \right]
\\&\qquad
  + \frac{2 (2 m - 3)}{(m)_2 (m)_4} \left[
    - \left(\one\tp \Ktxx \one \right)^2
    - \left(\one\tp \Ktyy \one \right)^2
    + 4 \norm{\Ktxx \one}^2
    + 4 \norm{\Ktyy \one}^2
    - 2 \norm{\Ktxx}_F^2
    - 2 \norm{\Ktyy}_F^2
    \right]
\\&\qquad
  + \frac{4 (m-2)}{m^3 (m-1)^2} \left(
      \norm{\Kxy \one}^2
    - \norm{\Kxy}_F^2
    + \norm{\Kxy\tp \one}^2
    - \norm{\Kxy}_F^2
    \right)
\\&\qquad
  + \frac{4 (2 m - 3)}{m^3 (m-1)^3} \left[
    - \left(\one\tp \Kxy \one\right)^2
    + \norm{\Kxy\tp \one}^2
    + \norm{\Kxy \one}^2
    - \norm{\Kxy}_F^2
  \right]
\\&\qquad
  - \frac{8}{m^3 (m-1)} \one\tp \Ktxx \Kxy \one
  + \frac{8}{m^2 (m)_3} \left[
      \one\tp \Ktxx \one \one\tp \Kxy \one
    - 2 \, \one\tp \Ktxx \Kxy \one
    \right]
\\&\qquad
  - \frac{8}{m^3 (m-1)} \one\tp \Ktyy \Kxy\tp \one
  + \frac{8}{m^2 (m)_3} \left[
      \one\tp \Ktyy \one \one\tp \Kxy \one
    - 2 \, \one\tp \Ktyy \Kxy\tp \one
    \right]
\\&\qquad
  + \frac{2}{m^2 (m-1)^2} \left[
      \norm{\Ktxx}_F^2
    + \norm{\Ktyy}_F^2
    \right]
  + \frac{4}{m^3 (m-1)} \norm{\Kxy}_F^2
,\end{align*}
which simplifies to
\begin{equation} \label{eq:final-mmd-est}
\begin{aligned}
    \hat V_m
  &=
    \frac{4}{(m)_4} \left[ \norm{\Ktxx \one}^2 + \norm{\Ktyy \one}^2 \right]
  + \frac{4 (m^2 - m - 1)}{m^3 (m-1)^2} \left[ \norm{\Kxy \one}^2 + \norm*{\Kxy\tp \one}^2 \right]
\\&\qquad
  - \frac{8}{m^2 (m^2 - 3 m + 2)} \left[ \one\tp \Ktxx \Kxy \one + \one\tp \Ktyy \Kxy\tp \one \right]
\\&\qquad
  + \frac{8}{m^2 (m)_3} \left[ \left( \one\tp \Ktxx \one + \one\tp \Ktyy \one \right) \left( \one\tp \Kxy \one \right) \right]
\\&\qquad
  - \frac{2 (2 m - 3)}{(m)_2 (m)_4} \left[
      \left(\one\tp \Ktxx \one \right)^2
    + \left(\one\tp \Ktyy \one \right)^2
  \right]
  - \frac{4 (2 m - 3)}{m^3 (m-1)^3} \left[
      \left(\one\tp \Kxy \one\right)^2
  \right]
\\&\qquad
  - \frac{2}{m (m^3 - 6 m^2 + 11 m - 6)} \left[ \norm{\Ktxx}_F^2 + \norm{\Ktyy}_F^2 \right]
  + \frac{4 (m-2)}{m^2 (m-1)^3} \norm{\Kxy}_F^2
.\end{aligned}
\end{equation}

\subsection{Final difference of MMD variance estimator}
The result \eqref{eq:final-diff-exp} of \cref{sec:diff-expression} was that
$\Var\left[ \mmdsqu(\setX, \setY) - \mmdsqu(\setX, \setZ) \right]$
is
\begin{align*}
     \nu_m
  &=
    \frac{4 (m-2)}{m (m-1)} \left[
      \E\left[ \inner{\varphi(X)}{\muY}^2 \right]
    + \E\left[ \inner{\varphi(X)}{\muZ}^2 \right]
    + \E\left[ \inner{\varphi(Y)}{\muX}^2 \right]
    + \E\left[ \inner{\varphi(Z)}{\muX}^2 \right]
    \right]
\\&\qquad
  + \frac{4 (m-2)}{m (m-1)} \left[
      \E\left[ \inner{\varphi(Y)}{\muY}^2 \right]
    + \E\left[ \inner{\varphi(Z)}{\muZ}^2 \right]
    \right]
\\&\qquad
  - \frac{4 (2m-3)}{m (m-1)} \left[
      \inner{\muX}{\muY}^2
    + \inner{\muX}{\muZ}^2
    \right]
  - \frac{2 (2m-3)}{m (m-1)} \left[
      \inner{\muY}{\muY}^2
    + \inner{\muZ}{\muZ}^2
    \right]
\\&\qquad
  + \frac{8}{m} \left[
      \inner{\muX}{\muY} \inner{\muX}{\muZ}
    + \inner{\muX}{\muY} \inner{\muY}{\muY}
    + \inner{\muX}{\muZ} \inner{\muZ}{\muZ}
  \right]
\\&\qquad
  - \frac{8}{m} \E\left[ \inner{\varphi(X)}{\muY} \inner{\varphi(X)}{\muZ} \right]
\\&\qquad
  - \frac{8}{m} \left(
      \E\left[ \inner{\varphi(Y)}{\muX} \inner{\varphi(Y)}{\muY} \right]
    + \E\left[ \inner{\varphi(Z)}{\muX} \inner{\varphi(Z)}{\muZ} \right]
    \right)
\\&\qquad
  + \frac{4}{m (m-1)} \left(
      \E\left[ k(X, Y)^2 \right]
    + \E\left[ k(X, Z)^2 \right]
    \right)
  + \frac{2}{m (m-1)} \left(
      \E\left[ k(Y_1, Y_2)^2 \right]
    + \E\left[ k(Z_1, Z_2)^2 \right]
    \right)
,\end{align*}
which gives us the estimator
\begin{align*}
     \nu_m
  &=
    \frac{4 (m-2)}{m^3 (m-1)^2} \left[
      \norm{\Kxy \one}^2
    + \norm{\Kxy\tp \one}^2
    + \norm{\Kxz \one}^2
    + \norm{\Kxz\tp \one}^2
    - 2 \norm{\Kxy}_F^2
    - 2 \norm{\Kxz}_F^2
    \right]
\\&\qquad
  + \frac{4}{m^2 (m-1)^2} \left[
      \norm{\Ktyy \one}^2
    - \norm{\Ktyy}_F^2
    + \norm{\Ktzz \one}^2
    - \norm{\Ktzz}_F^2
    \right]
\\&\qquad
  - \frac{4 (2m-3)}{m^3 (m-1)^3} \left[
      \left(\one\tp \Kxy \one\right)^2
    + \left(\one\tp \Kxz \one\right)^2
    - \norm{\Kxy \one}^2
    - \norm{\Kxz \one}^2
    - \norm{\Kxy\tp \one}^2
    - \norm{\Kxz\tp \one}^2
\right.\\&\qquad\qquad\qquad\qquad\qquad\left.
    + \norm{\Kxy}_F^2
    + \norm{\Kxz}_F^2
    \right]
\\&\qquad
  - \frac{2 (2m-3)}{(m)_2 (m)_4} \left[
      \left( \one\tp \Ktyy \one\right)^2
    + \left( \one\tp \Ktzz \one\right)^2
    - 4 \norm{\Ktyy \one}^2
    - 4 \norm{\Ktzz \one}^2
    + 2 \norm{\Ktyy}_F^2
    + 2 \norm{\Ktzz}_F^2
    \right]
\\&\qquad
  + \frac{8}{m^4 (m-1)} \left[
      \one\tp \Kxy \one \one\tp \Kxz \one
    - \one\tp \Kxy\tp \Kxz \one
    \right]
\\&\qquad
  + \frac{8}{m^2 (m)_3} \left[
      \one\tp \Ktyy \one \one\tp \Kxy \one
    + \one\tp \Ktzz \one \one\tp \Kxz \one
    - 2 \, \one\tp \Ktyy \Kxy\tp \one
    - 2 \, \one\tp \Ktzz \Kxz\tp \one
  \right]
\\&\qquad
  - \frac{8}{m^4} \one\tp \Kxy\tp \Kxz \one
\\&\qquad
  - \frac{8}{m^3 (m-1)} \left[
      \one\tp \Ktyy \Kxy\tp \one
    + \one\tp \Ktzz \Kxz\tp \one
    \right]
\\&\qquad
  + \frac{4}{m^3 (m-1)} \left(
      \norm{\Kxy}_F^2
    + \norm{\Kxz}_F^2
    \right)
  + \frac{2}{m^2 (m-1)^2} \left(
      \norm{\Ktyy}_F^2
    + \norm{\Ktzz}_F^2
    \right)
.\end{align*}
Combining like terms, we obtain
\begin{equation} \label{eq:final-diff-est}
\begin{aligned}
     \hat\nu_m
  &=
    \frac{4 (m^2 - m - 1)}{m^3 (m-1)^3} \left[
      \norm{\Kxy \one}^2
    + \norm{\Kxy\tp \one}^2
    + \norm{\Kxz \one}^2
    + \norm{\Kxz\tp \one}^2
    \right]
\\&\qquad
  + \frac{4}{(m)_4} \left[
      \norm{\Ktyy \one}^2
    + \norm{\Ktzz \one}^2
    \right]
  - \frac{8}{m^3 (m-1)} \one\tp \Kxy\tp \Kxz \one
\\&\qquad
  - \frac{8}{m^2 (m^2 - 3m + 2)} \left[
      \one\tp \Ktyy \Kxy\tp \one
    + \one\tp \Ktzz \Kxz\tp \one
    \right]
\\&\qquad
  - \frac{4 (2m-3)}{m^3 (m-1)^3} \left[
      \left(\one\tp \Kxy \one\right)^2
    + \left(\one\tp \Kxz \one\right)^2
    \right]
  - \frac{2 (2m-3)}{(m)_2 (m)_4} \left[
      \left( \one\tp \Ktyy \one\right)^2
    + \left( \one\tp \Ktzz \one\right)^2
    \right]
\\&\qquad
  + \frac{8}{m^4 (m-1)} \one\tp \Kxy \one \one\tp \Kxz \one
  + \frac{8}{m^2 (m)_3} \left[
      \one\tp \Ktyy \one \one\tp \Kxy \one
    + \one\tp \Ktzz \one \one\tp \Kxz \one
    \right]
\\&\qquad
  - \frac{4 (m-2)}{m^2 (m-1)^3} \left[
      \norm{\Kxy}_F^2
    + \norm{\Kxz}_F^2
    \right]
  - \frac{2}{m (m^3 - 6 m^2 + 11m - 6)} \left[
      \norm{\Ktyy}_F^2
    + \norm{\Ktzz}_F^2
    \right]
.\end{aligned}
\end{equation}

\printbibliography

\end{document}


\subsection{Constraints and Generalized Variables}
\begin{frame}[fragile] \frametitle{Constraints and Generalized Variables}

  \begin{cenpage}{135mm}

  Instead of varying the Path Parameters directly, we write them in terms of
  {\RedEmph{Generalized Variables}}.  This allows simple {\RedEmph{Constraints}} and model building:

\begin{columns}[T]
  \begin{column}{53mm}

  \begin{CodeBlock}{50mm}{Parameter=Variable}
{\Blue{\# one variable e0 for 2 paths}}
{\Red{params = group(e0 = guess(1.0), \ldots)}}

path1  = feffpath('feo.dat', {\Red{e0='e0'}})
path2  = feffpath('fefe.dat', {\Red{e0='e0'}})
  \end{CodeBlock}
  \hspace{1mm}   \vmm

  \onslide+<2->
  \begin{CodeBlock}{50mm}{Mixed Coordination Shell}
{\Blue{\# mix O and S in 1st coordination shell}}
params = group(s02   = param(0.80, vary=False),
               sfrac = guess(0.5))

path1 = feffpath('feo.dat', {\Red{s02='s02*sfrac'}})
path2 = feffpath('fes.dat', {\Red{s02='s02*(1-sfrac)'}})
  \end{CodeBlock}

  \vmm

  \end{column}
  \begin{column}{57mm}
    \onslide+<3->
  \begin{CodeBlock}{55mm}{Einstein Temperature }
{\Blue{\# Use 1 ``theta'' to set sigma2 for multiple paths}}

params = group(amp=param(1, vary=True),
               theta=param(250, min=0, vary=True), \ldots)

path1_100K = feffpath('fefe.dat', s02='amp', \ldots,
                      sigma2='sigma2_eins(100, theta)')

path1_200K = feffpath('fefe.dat', s02='amp', \ldots,
                      sigma2='sigma2_eins(200, theta)')

path1_300K = feffpath('fefe.dat', s02='amp', \ldots,
                      sigma2='sigma2_eins(300, theta)')

  \end{CodeBlock}\\
\end{column}
\end{columns}

\vmm This allows us to use {\BlueEmph{Prior  Knowledge}} into the data analysis, and
consider more complicated problems:
\vmm

  \begin{itemize}
  \item force one $R$ for the same bond for data taken from different
    edges.

  \item model complex distortions (height of a sorbed atom above a surface).
  \end{itemize}

  \vmm
  {\RedEmph{Also}}: Each Parameter can have upper and lower bounds, say to ensure that $\sigma^2 \ge 0$.

  \vfill
\end{cenpage}

\end{frame}

% \begin{slide}{Constraints, Generalized Variables examples}

%   Constraints and Generalized Variables are one kind of ``Prior
%   Knowledge'',  allowing us to build and compare simple physical models for
%   our data.

%   \vmm   This allows us to consider more complicated problems:

%   \vmm

%   {\Red{
%       \begin{tabular}{clcl}
%         &multiple neighboring species  &&  multiple sites for central atom \\
%         &multiple scattering paths     &&  multiple polarizations \\
%         &multiple data sets            &\hspace{4mm}&  multiple edges to co-refine   \\
%       \end{tabular}
%     }}

%   \vmm Other constraints:

%   \begin{itemize}
%   \item force one $R$ for the same bond for data taken from different
%     edges.

%   \item model complex distortions (height of a sorbed atom above a surface).
%   \end{itemize}

% \vfill
% \end{slide}


\documentclass[11pt, letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{bm}
\title{Lecture 3: Dipole interactions and introduction to Cartesian tensors}
\begin{document}
\maketitle

The main aim of this lecture is to understand the origin of $-1/r^6$ attractive potential. This potential arises due to dipole-dipole interactions and is responsible for cohesion amongst liquid molecules.

\section{A general idea}
We start with the derivation of potential due to a dipole and use it to obtain the energy of interaction between two dipoles for a polar liquid. We then briefly derive the same result for non-polar liquids and see that dipole interactions have identical dependence in the two cases, namely, $-1/r^6$. Finally we write down the general case where molecules may have some permanent and some instantaneous electronic polarization and define the Van der Waal's forces of attraction.

\section{Dipole-dipole interactions}
\subsection{Polar liquids}

The Coulomb potential due to point charge is given by
$$
V(r)=\frac{q}{4\pi\epsilon_0 r}
$$

Based on this, we can derive the potential due to a dipole at any spatial location $\bm{X}$. The result is
$$
V(\bm{X})\equiv V({r,\theta}) =\frac{D \cos(\theta)}{4\pi\epsilon_0 r^2} \rightarrow\text{ \footnotesize{Faster decay}}
$$
Where $\bm{D}=D\bm{p}$ is the dipole moment and $\theta$ is the angle between $\bm{X}$ and $\bm{p}$. Then, the inner product of $\bm{D}$ and $\bm{X}$ becomes $D r \cos(\theta)$. Hence we can write
$$
V({r,\theta}) =\frac{D r \cos(\theta)}{4\pi\epsilon_0 r^3}\equiv\frac{D \bm{p}\cdot\bm{X}}{4\pi\epsilon_0 r^3}
$$
Since potential goes like $1/r^2$ the electric field $\bm{E}$ decays as $1/r^3$. Once we have the potential due to a single dipole, we can find out the energy of a second dipole placed in the field of the first. Assuming that the field is uniform over the length of the second dipole (it is placed far enough), we can obtain this energy to be
$$
E_{12} = D\bm{p}\cdot\bm{E}
$$

Now, the dipole moment of the other dipole is not yet known. Although its intrinsic moment might be $\bm{D}$, due to thermal fluctuations, it would sample all possible orientations and lead to a net zero dipole moment over time. However, the electric field of the first dipole $\bm{E}$ will cause the second dipole to have a preferential orientation. We can hence deduce that the dipole moment of the second dipole will be proportional to $E$ and of course also to $D$. Then we must construct a dimensionless variable containing $E$. This is given by the Boltzmann factor $DE/kT$. Also, since $\bm{E}$ induces the dipole moment in the second dipole, it is reasonable to assume that first effects of $\bm{E}$ will be linear in $E$. Hence, the dipole moment of the second dipole becomes $D(DE/kT)$, giving a small moment if thermal energy is large and a large moment if electric field is strong.

Finally we can obtain the force between two dipoles using expression for the electric field of a dipole.
$$
\bm{F} = \underbrace{-q\bm{E}(\bm{X} - \bm{p}\delta l /2)}_{\text{force due to field at the negative charge}} + \underbrace{q\bm{E}(\bm{X} + \bm{p}\delta l /2)}_{\text{force due to field at the positive charge}}
$$

Using Taylor expansion, we see that leading order terms cancel and we get a force dependent on the gradient of $E$.
$$
\bm{F} = q\delta l \bm{p}\cdot\nabla\bm{E}
$$

The dipole moment is now given by  $D(DE/kT)$ and hence
$$
\bm{F} = \frac{D^2E}{kT} \bm{p}\cdot\nabla\bm{E}
$$
$$
\Rightarrow \bm{F} \sim  \nabla\frac{D^2\bm{E^2}}{kT} \propto \frac{1}{r^7} \text{(\hfill since $E\propto\frac{1}{r^3})$}
$$
and hence potential between two permanent dipoles varies as $r^{-6}$. Next, we look at non-polar liquids.

\section{Non-polar liquids}

In non-polar liquids molecules do not have a permanent dipole moment but the electron cloud around the nucleus has a fluctuating distribution. This causes instantaneous polarization of the molecule. The field induced by this dipole then causes polarization of the nearby constituents. Yet, we will see that the force and the potential in this case have dependence identical to the case of polar liquids. Let the induced dipole moment be $\bm{p_{ind}}$. Then
$$
\bm{p_{ind}} = \alpha \bm{E}
$$
where $\alpha$ is the polarizability of the molecule and $\bm{E}$ is the electric field of the other dipole. The force is then given by
$$
\bm{F} = \bm{p_{ind}}\cdot \nabla\bm{E}
$$
$$
\Rightarrow \bm{F} = \alpha \bm{E} \cdot \nabla\bm{E} \propto \nabla\bm{E}^2
$$
Since $E\propto r^{-3}$, we again have the $1/r^{7}$ dependence of force and $1/r^{6}$ dependence of potential for interaction of two induced dipoles.

In general, a molecule may have some permanent polarization and some electronic polarization due to fluctuations in the electron cloud. This will result in a permanent electric field superposed with a fluctuating field. Therefore, in general, we can write
$$
\bm{F} = \bm{p}\cdot \nabla\bm{E}
$$
$$
\Rightarrow \bm{F} = (\bm{p}_{permanent}+\bm{p}_{eletronic})\cdot \nabla(\bm{E}_{permanent}+\bm{E}_{fluctuating})
$$
This gives rise to three kinds of interactions
\begin{itemize}
\item permanent dipoles with permanent dipoles - Keesom interactions
\item permanent dipoles with instantaneous electronic dipoles - Debye interactions
\item instantaneous dipoles with instantaneous dipoles - London interactions
\end{itemize}

Together, these three are known as Van der Waal's forces.

\end{document}


\chapter{Cubic Partition Pair Congruences}

\section{Introduction}
To understand Lin's work and his conjectures, we introduce some basic definitions relating to cubic partition functions.
\subsection{Cubic Partition Function}

Chan in [6, 7, 8] introduces the cubic partition function $a(n)$

\begin{definition}
$$ \sum_{n=0}^{\infty} a(n)q^n := \frac{1}{(q;q)_\infty(q^2;q^2)_\infty} $$.
\end{definition}

On investigation the analogues of cubic partition function, Kim in [14] introduced the overcubic partition function $\overline{a}(n)$

\begin{definition}
$$ \sum_{n=0}^{\infty} \overline{a}(n)q^n := \frac{(-q;q)_\infty(-q^2;q^2)_\infty}{(q;q)_\infty(q^2;q^2)_\infty} $$
\end{definition}

\subsection{Number of Cubic Partition Pairs}
Zhao and Zhong  in [20] then studied the partition function $b(n)$

\begin{definition}
$$ \sum_{n=0}^{\infty} b(n)q^n := \frac{1}{(q;q)^2_\infty(q^2;q^2)^2_\infty} $$.
\end{definition}

Kim established Ramanujan type congruences $b(5n+4) \equiv 0 \pmod 5$ in [15]. There, Kim referred to $b(n)$ as the number of cubic partition pairs, since it counts pair of cubic partitions.

In [16], Kim introduced the overpartition analogue for the number of cubic partition pairs $\overline{b}(n)$

\begin{definition}
$$ \sum_{n=0}^{\infty} \overline{b}(n)q^n := \frac{(-q;q)_\infty^2(-q^2;q^2)_\infty^2}{(q;q)_\infty^2(q^2;q^2)_\infty^2} $$
\end{definition}

\section{Preliminaries}
\begin{definition}
We will be following the below mentioned notation
\begin{equation}
    f_t := (q^t;q^t)_\infty, \; t\in \mathbb{N}
\end{equation}
\begin{equation}
    a(q) = \sum_{m,n = -\infty}^\infty q^{m^2 + mn + n^2}
\end{equation}
\end{definition}

Trivially we have,
\begin{align*}
   f_1 = \sum^\infty_{m = -\infty}(-1)^nq^{m(3m+1)/2}
\end{align*}
using Eulers Pentagonal number theorem from Part A.

\begin{lemma}
Applying the binomial theorem, we establish the following,
\begin{align*}
    f_1^3 &\equiv f_3 \pmod 3,
   \\f_1^9 &\equiv f_3^3 \pmod 9.
\end{align*}
\end{lemma}
\begin{lemma}
Now using Jacobi's triple product identity, we get
\begin{align*}
\varphi(-q) &= \frac{f_1^2}{f_2},
\\ \psi(q) &= \frac{f_2^2}{f_1}.
\end{align*}
\end{lemma}

\begin{lemma}
\begin{equation}
    \frac{1}{f_1f_2} = a(q^6)\frac{f^3_9}{f^4_3f^3_6} + qa(q^3)\frac{f^3_{18}}{f^3_3f^4_6}+3q^2\frac{f^3_9f^3_{18}}{f^4_3f^4_6},
\end{equation}
\begin{equation}
    f_1f_2 = \frac{f_6f^4_9}{f_3f^2_{18}} -qf_9f_{18}-2q^2\frac{f_3f^4_{18}}{f_6f^2_9}.
\end{equation}
\end{lemma}
A proof for these can be found in [20]

\begin{theorem}
\begin{equation}
    \sum_{n=0}^\infty b(9n + 7)q^n \equiv 18f_1f_2\bigg(\frac{f^7_3}{f_6}+q\frac{f^7_6}{f_3}\bigg)-9f^2_3f^2_6 \pmod{27}.
\end{equation}
\end{theorem}
\begin{proof}
    See [17]
\end{proof}

\begin{lemma}
\begin{equation}
    \sum_{n=0}^\infty b(9n + 7)q^n \equiv 18\bigg( \frac{f_6f^4_9}{f_3f^2_{18}} - qf_9f_{18} - 2q^2\frac{f_3f^4_{18}}{f_6f^2_9}\bigg)\bigg( \frac{f^7_3}{f_6} + q\frac{f^7_6}{f_3} \bigg) - 9f^2_3f^2_6 \pmod{27}.
\end{equation}
\end{lemma}
\begin{proof}
    This follows immediately from combining (5.4) and (5.5).
\end{proof}

\section{Lin's Work}
\begin{theorem}
For any $ n \geq 0$,
\begin{equation}
    b(81n + 61) \equiv 0. \pmod{27}
\end{equation}
\end{theorem}
\begin{proof}
    Collecting the terms from (5.6) where powers of q are multiples of 3, replacing $q^3$ by q, we get
\begin{equation}
\begin{split}
    \sum_{n=0}^\infty b(27n+7)q^n &\equiv 18\bigg(\frac{f_1^7}{f_2}.\frac{f_2f_3^4}{f_1f_6^2} - 2q\frac{f_2^7}{f_1}.\frac{f_1f_6^4}{f_2f_3^2}\bigg) - 9f_1^2f_2^2 \pmod{27}
    \\&\equiv 18\bigg(\frac{f_3^6}{f_6^2}-2q\frac{f_6^6}{f_3^2}\bigg) - 9\frac{f_3f_6}{f_1f_2}. \pmod{27}.
\end{split}
\end{equation}

By (5.3), we find that
\begin{equation}
\begin{split}
    \sum_{n=0}^\infty b(27n+7)q^n &\equiv 18\bigg(\frac{f_3^6}{f_6^2}-2q\frac{f_6^6}{f_3^2}\bigg) - 9f_3f_6\bigg(a(q^6) \frac{f_9^3}{f_3^4f_6^3}+qa(q^3)\frac{f_{18}^3}{f_3^4f_6^4} + 3q^2\frac{f_9^3f_{18}^3}{f_3^4f_6^4}\bigg) \pmod{27}
    \\&\equiv 18\bigg(\frac{f_3^6}{f_6^2} - 2q\frac{f_6^6}{f_3^2}\bigg) - 9f_3f_6\bigg(a(q^6)\frac{f_9^3}{f_3^4f_6^3}+qa(q^3)\frac{f_{18}^3}{f_3^3f_6^4}\bigg) \pmod{27}
\end{split}
\end{equation}
Since we don't have any term of the form $q^{3k+2}$ in the above equation, we can equate the coefficient of $q^{3k+2}$ and get the required result.

\end{proof}


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

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

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

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

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

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty tfrpmh} computes the Pseudo Margenau-Hill distribution of a
        discrete-time signal {\ty x}, or the cross Pseudo Margenau-Hill
        representation between two signals. Its expression is
\[PMH_x(t,\nu)=\int_{-\infty}^{+\infty} \frac{h(\tau)}{2}\ (x(t+\tau)\ x^*(t)+x(t)\
x^*(t-\tau))\ e^{-j2\pi \nu \tau}\ d\tau.\]

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8cm} c}
Name & Description & Default value\\
\hline
        {\ty x}     & signal if auto-PMH, or {\ty [x1,x2]} if cross-PMH
			({\ty Nx=length(x)})\\
        {\ty t}     & time instant(s)          & {\ty (1:Nx)}\\
        {\ty N}     & number of frequency bins & {\ty Nx}\\
        {\ty h}     & frequency smoothing window, {\ty h(0)} being forced to {\ty 1}
                                         & {\ty window(odd(N/4))}\\
        {\ty trace} & if nonzero, the progression of the algorithm is shown
                                         & {\ty 0}\\
     \hline {\ty tfr}   & time-frequency representation \\
        {\ty f}     & vector of normalized frequencies\\

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

When called without output arguments, {\ty tfrpmh} runs {\ty tfrqview}.
\end{minipage}
\vspace*{1cm}

{\bf \large \sf Example}
\begin{verbatim}
         sig=fmlin(128,0.1,0.4); t=1:128;
         h=window(63,'Kaiser');
         tfrpmh(sig,t,128,h,1);
\end{verbatim}

%\newpage

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

{\bf \large \sf References}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
[1] H. Margenhau, R. Hill ``Correlation between Measurements in Quantum
Theory'', Prog. Theor. Phys. Vol. 26, pp. 722-738, 1961.\\

[2] R. Hippenstiel, P. De Oliviera ``Time-Varying Spectral Estimation Using
the Instantaneous Power Spectrum (IPS)'' IEEE Trans. on Acoust., Speech and
Signal Proc. Vol. 38, No. 10, pp. 1752-1759, 1990.
\end{minipage}


\section*{Max flow summary - Ada Nzulumike}

Introduction:
\\
- Definition of a flow (capacity constraint and flow conservation)
\\
- Value of a flow

Solving the Maximum-flow-problem:
\\
- Ford-Fulkerson method
\\
- The residual network (residual capacity, augmenting paths)
\\
- Example

Max-flow min-cut theorem:
\\
- Definition of a cut
\\
- Capacity of a cut
\\
- Proof of the theorem


\section{Two Dimensional Method of Characteristics (Irrotational Flow)}

\subsection{Derivation of Characteristic and Compatibility Equations}

	The gas dynamic equation (Eq. \ref{eqn:gasdynq}), stated again here for convenience,
can be rewritten as follows.

\begin{displaymath}
	\vec{q}\cdot\nabla(\frac{q^2}{2}) - a^2(\nabla \cdot \vec{q}) = 0
\end{displaymath}

	Taking $\vec{q} = u + v$ where $u$ and $v$ are the velocities parallel and normal to the axisymmetric
axis respectively, then the dot product $\nabla \cdot \vec{q}$ can be expressed in cylindrical co-ordinates as,

\begin{equation}
	\nabla\cdot\vec{q} = \frac{\partial u}{\partial x} + \frac{\partial v}{\partial r}
	+ \frac{v}{r}
\label{eqn:dotprod}
\end{equation}

	Substituting this into the gas dynamic equation yields,

\begin{displaymath}
	\vec{q}\cdot\nabla(\frac{q^2}{2}) - a^2\Big\{\frac{\partial u}{\partial x} + \frac{\partial v}
	{\partial r} + \frac{v}{r}\Big\} = 0
\end{displaymath}

	Expanding out the first term one can write,

\begin{displaymath}
	(u+v)\cdot\Big\{\frac{1}{2}(\frac{\partial q^2}{\partial x} + \frac{\partial q^2}{\partial r})\Big\}
	- a^2\Big\{\frac{\partial u}{\partial x} + \frac{\partial v}
	{\partial r} + \frac{v}{r}\Big\}=\frac{1}{2}(u\frac{\partial q^2}{\partial x} + v\frac{\partial q^2}
	{\partial r})- a^2\Big\{\frac{\partial u}{\partial x} + \frac{\partial v}
	{\partial r} + \frac{v}{r}\Big\} = 0
\end{displaymath}

	Given the definition of $q$ one can write for the derivative of its square,

\begin{displaymath}
	\frac{\partial q^2}{\partial x} = \frac{\partial}{\partial x}(u^2 + v^2) =
	2(u\frac{\partial u}{\partial x} + v\frac{\partial v}{\partial x})
\end{displaymath}

	and

\begin{displaymath}
	\frac{\partial q^2}{\partial r} = \frac{\partial}{\partial r}(u^2 + v^2) =
	2(u\frac{\partial u}{\partial r} + v\frac{\partial v}{\partial r})
\end{displaymath}

	which when substituted into the gas dynamic equation yields,

\begin{displaymath}
	u(u\frac{\partial u}{\partial x} + v\frac{\partial v}{\partial x}) +
	v(u\frac{\partial u}{\partial r} + v\frac{\partial v}{\partial r})
	- a^2(\frac{\partial u}{\partial x} + \frac{\partial v}
	{\partial r} + \frac{v}{r}) = 0
\end{displaymath}

	If the flow is assumed irrotational then one also write the relation

\begin{equation}
	\frac{du}{dr} - \frac{dv}{dx} = 0
\label{eqn:irrotational}
\end{equation}

	which allows the above to be simplified to,

\begin{equation}
	(u^2-a^2)\frac{du}{dx} + (v^2-a^2)\frac{dv}{dr} + 2uv\frac{du}{dr}
	- \frac{a^2v}{r} = 0
\label{eqn:gasdynaxi}
\end{equation}

	Equations \ref{eqn:irrotational} and \ref{eqn:gasdynaxi} form the basis of the two dimensional axisymmetric
method of characteristics,
as from these one can develop both the compatibility and characteristic equations.  Starting
by multiplying Eq. \ref{eqn:gasdynaxi} by $\sigma_1$ and Eq. \ref{eqn:irrotational} by
$\sigma_2$ (where $\sigma_1$ and $\sigma_2$ are arbitrary functions to be determined later)
and summing the resulting equations to zero yields,

\begin{displaymath}
	\sigma_1(u^2-a^2)\frac{du}{dx} + \sigma_1(v^2-a^2)\frac{dv}{dr} + \sigma_1 2uv\frac{du}{dr}
	- \sigma_1\frac{a^2v}{r} + \sigma_2\frac{du}{dr} - \sigma_2\frac{dv}{dx} = 0
\end{displaymath}

	which after grouping the derivatives of a single velocity together and rearranging yields,

\begin{equation}
	\sigma_1(u^2-a^2)\Big\{\frac{\partial u}{\partial x} + \frac{\sigma_1 2uv + \sigma_2}
	{\sigma_1(u^2-a^2)}\frac{\partial u}{\partial r}\Big\} - \sigma_2\Big\{\frac{\partial v}
	{\partial x} + \frac{\sigma_1(v^2-a^2)}{-\sigma_2}\frac{\partial v}{\partial r}\Big\}
	- \sigma_1\frac{a^2v}{r} = 0
\label{eqn:big}
\end{equation}

	Since one of the properties enforced on the dependent variables ($u(x,y)$ and $v(x,y)$)
is that they be continuous functions across the characteristic curves one can write,

\begin{displaymath}
	\frac{du}{dx} = \frac{\partial u}{\partial x} + \lambda\frac{\partial u}{\partial r}
	\hspace{2cm}\textrm{and}\hspace{2cm} \frac{dv}{dx} = \frac{\partial v}{\partial x} +
	\lambda\frac{\partial v}{\partial r}
\end{displaymath}

	where $\lambda = \frac{dy}{dx}$ and is hence the slope of the characteristic in the
physical plane along which the total derivatives can be defined as above.  Therefore the
characteristic curves for Eq. \ref{eqn:big} must have slopes of,

\begin{equation}
	\lambda = \frac{\sigma_1 2uv + \sigma_2}{\sigma_1(u^2-a^2)} \hspace{2cm}\textrm{and}\hspace{2cm}
	\lambda = \frac{\sigma_1(v^2-a^2)}{-\sigma_2}
\label{eqn:lambdasig}
\end{equation}

	which then allows Eq. \ref{eqn:big} to be written as,

\begin{equation}
	\sigma_1(u^2-a^2)du - \sigma_2 dv - \sigma_1\frac{a^2v}{r}dx = 0
\label{eqn:small}
\end{equation}

	Equation \ref{eqn:small} is the compatibility equation for axisymmetric irrotational flow,
as defined by Eqs. \ref{eqn:irrotational} and \ref{eqn:gasdynaxi}.  However, for this equation to
be of use, the arbitrary functions must be phrased in terms of known quantities.  Therefore,
rewriting Eqs. \ref{eqn:lambdasig} in matrix form gives,

\begin{displaymath}
	\left[\begin{array}{cc}
	(u^2-a^2)\lambda - 2uv & -1 \\ (v^2-a^2) & \lambda
	\end{array}\right]
	\left[\begin{array}{c}
	\sigma_1 \\ \sigma_2
	\end{array}\right] = 0
\end{displaymath}

	Taking the determinant of the co-efficient matrix and setting this equal to zero yields,

\begin{displaymath}
	(u^2-a^2)\lambda^2 -2uv\lambda + (v^2-a^2) = 0
\end{displaymath}

	which can be solved using the quadratic formula to give the result,

\begin{equation}
	\lambda_\pm = \frac{uv \pm a^2\sqrt{M^2-1}}{u^2-a^2}
\label{eqn:lambdamach}
\end{equation}

	where the $+$ sign refers to the positive square root and the $-$ refers to the negative.
Since $\lambda$ is the slope of the characteristic in the physical plane, this means that for flows
where $M > 1$ there exist two distinct characteristic curves.  Equation \ref{eqn:lambdamach} can
be rephrased by noting that,

\begin{displaymath}
	\theta  = \tan^{-1}(\frac{v}{u}) \hspace{1.5cm} \textrm{,} \hspace{1.5cm}
	u = q\cos\theta \hspace{1.5cm} \textrm{,} \hspace{1.5cm} v = q\sin\theta
\end{displaymath}

	while from the Mach triangle one can write (where $\alpha$ is the Mach angle),

\begin{displaymath}
	\alpha = \sin^{-1}(\frac{1}{M}) \hspace{1.5cm} \textrm{,} \hspace{1.5cm}
	\tan\alpha = \frac{1}{\sqrt{M^2-1}}
\end{displaymath}

	Substituting these relations into Eq. \ref{eqn:lambdamach} yields,

\begin{displaymath}
	\lambda_\pm = \frac{(q\cos\theta) (q\sin\theta) \pm a^2(\frac{1}{\tan\alpha})}
	{(q\cos\theta)^2-a^2}
\end{displaymath}

	while dividing through by $q^2$ and noting that $\frac{a^2}{q^2} = M^{-2}$ yields,

\begin{displaymath}
	\lambda_\pm = \frac{\cos\theta \sin\theta \pm \frac{1}{\tan\alpha}M^{-2}}
	{\cos^2\theta-M^{-2}}
\end{displaymath}

	Using the definition of the Mach angle allows the above to be simplified to,

\begin{displaymath}
	\lambda_\pm = \frac{\cos\theta \sin\theta \pm \cos\alpha \sin\alpha}
	{\cos^2\theta-\sin^2\alpha}
\end{displaymath}

	The above can be further simplified by making use of trigonometric identities
allowing an alternate form for $\lambda$ to be expressed as,

\begin{equation}
	\fbox{$
	\lambda_\pm = (\frac{dy}{dx})_\pm = \tan(\theta \pm \alpha)
	$}
\label{eqn:lambda}
\end{equation}

	Equation \ref{eqn:lambda} is the characteristic equation for axisymmetric irrotational
flow.  Having determined the characteristic equation one can return to the compatibility equation
to eliminate the arbitrary functions $\sigma_1$ and $\sigma_2$ as follows.  From Eqs.
\ref{eqn:lambdasig} one can express $\sigma_2$ as,

\begin{displaymath}
	\sigma_1\Big\{(u^2-a^2)\lambda -2uv\Big\} = \sigma_2 = -\frac{\sigma_1(v^2-a^2)}{\lambda}
\end{displaymath}

	Although not immediately apparent, these two equations are not independent and thus either
one can be substituted back into Eq. \ref{eqn:small} to eliminate both arbitrary functions,

\begin{displaymath}
	\sigma_1(u^2-a^2)du - \Big\{\sigma_1\Big[(u^2-a^2)\lambda -2uv\Big]\Big\}dv
	- \sigma_1\frac{a^2v}{r}dx = 0
\end{displaymath}

	which after dividing through by $\sigma_1$ yields,

\begin{equation}
	\fbox{$
	(u^2-a^2)du_{\pm} - \Big[(u^2-a^2)\lambda_{\pm} -2uv\Big]dv_{\pm} - \frac{a^2v}{r}dx_{\pm} = 0
	$}
\label{eqn:compatibility}
\end{equation}

	where if the positive value of $\lambda$ is used, then Eq. \ref{eqn:compatibility}
applies along the $C_+$ characteristic and hence all values $du$, $dv$, and $dx$ apply to
this curve.  It is noted that if the other definition of $\sigma_2$ is substituted into
Eq. \ref{eqn:small} to eliminate the arbitrary values, then a slightly different form of the
compatibility equation is obtained.  However, this merely produces an alternate form of the
same equation, not an independent relation that is also applicable along the characteristic.

%--------------------------------------------------------------------------------------------
\subsection{Numerical Implementation of the Method of Characteristics}

	Having derived the characteristic (Eq. \ref{eqn:lambda}) and compatibility (Eq.
\ref{eqn:compatibility}) equations, one can now use these in a numerical integration
scheme to completely determine a supersonic flow field.  If one defines the following
parameters,

\begin{equation}
	Q = u^2 - a^2 \hspace {1.5cm} R = 2uv - Q\lambda \hspace{1.5cm} S = \frac{a^2v}{y}
\label{eqn:variables}
\end{equation}

	then the characteristic and compatibility equations can be written in finite difference
form as,

\begin{equation}
	\Delta y_{\pm} = \lambda_{\pm} \Delta x_{\pm}
\label{eqn:charfinite}
\end{equation}

\begin{equation}
	Q_{\pm}\Delta u_{\pm} + R_{\pm}\Delta v_{\pm} - S_{\pm}\Delta x_{\pm} = 0
\label{eqn:compfinite}
\end{equation}

	If one considers a line along which the flow field in completely known, then from
two points (points 1 and 2) along this line, two opposing characteristic curves will intersect
each other at some unknown point 4.  Using these three points, one can use Eq. \ref{eqn:charfinite}
to determine the intersection point (4) as follows,

\begin{displaymath}
	y_4 = y_1 + \lambda_-(x_4 - x_1)
\end{displaymath}

	while from the other characteristic,

\begin{equation}
	\fbox{$
	y_4 = y_2 + \lambda_+(x_4 - x_2)
	$}
\label{eqn:y4}
\end{equation}

	thus yielding for $x_4$,

\begin{equation}
	\fbox{$
	x_4 = \frac{y_1 - y_2 - \lambda_-x_1 + \lambda_+x_2}{\lambda_+ - \lambda_-}
	$}
\label{eqn:x4}
\end{equation}

	Once $x_4$ is found, expression in Eq. \ref{eqn:y4} can be used to completely
determine the physical location of the intersection point of the two opposing characteristic
curves emanating from points 1 and 2.  To determine the flow properties at this point one
can make use of Eq. \ref{eqn:compfinite} by calculating along each characteristic,

\begin{equation}
	\begin{array}{c}
	T_{-} = S_{-}(x_4 - x_1) + Q_{-}u_1 + R_{-}v_1 \\ \vspace{0.5cm}
	T_{+} = S_{+}(x_4 - x_2) + Q_{+}u_2 + R_{+}v_2
	\end{array}
\label{eqn:tpm}
\end{equation}

	which then from Eq. \ref{eqn:compfinite} along the $C_-$ characteristic,

\begin{displaymath}
	Q_-(u_4 - u_1) + R_-(v_4 - v_1) -S_-(x_4 - x_1) = 0
\end{displaymath}

	while rearranging and substituting Eq. \ref{eqn:tpm} yields,

\begin{displaymath}
	Q_-u_4 + R_-v_4 = S_-(x_4 - x_1) + Q_-u_1 + R_-v_1 = T_-
\end{displaymath}

	therefore,

\begin{equation}
	\fbox{$
	v_4 = \frac{T_- - Q_-u_4}{R_-}
	$}
\label{eqn:v4}
\end{equation}

	Along the $C_+$ characteristic one can write,

\begin{displaymath}
	Q_+u_4 + R_+v_4 = S_+(x_4 - x_2) + Q_+u_2 + R_+v_2 = T_+
\end{displaymath}

	which when Eq. \ref{eqn:v4} is used to replace $v_4$ yields a solution for $u_4$,

\begin{displaymath}
	Q_+u_4 + R_+\Big\{\frac{T_- - Q_-u_4}{R_-}\Big\} = T_+
\end{displaymath}

\begin{displaymath}
	R_+T_- - R_+Q_-u_4 = R_-T_+ - R_-Q_+u_4
\end{displaymath}

\begin{equation}
	\fbox{$
	u_4 = \frac{R_+T_- - R_-T_+}{R_+Q_- -  R_-Q_+}
	$}
\label{eqn:u4}
\end{equation}

	with the velocity components found from Eqs. \ref{eqn:v4} and \ref{eqn:u4} at
the intersection point 4, one has now determined the flow conditions at a
point further downstream (or upstream, depending on the direction chosen) of the original
line at which the flow conditions are known (noting that each subsequent intersection point
must still lie within a supersonic flow field).

	The accuracy of the characteristic mesh being
built can be improved by employing a predictor-corrector scheme to the solution of point 4
as follows.  After having completely determined the intersection point as outlined above (the predictor step),
the procedure is repeated using the average values of the flow properties along each characteristic
(between points 1 and 4 and points 2 and 4) to recalculate the values found from Eqs. \ref{eqn:variables}
and \ref{eqn:tpm} which are then used to find a new intersection point 4.  This procedure can be repeated until a
specified tolerance level is reached.

\begin{equation}
	\begin{array}{c}
	y_-^{corr}= \frac{1}{2}(y_1 + y_4) \hspace{1.5cm} u_-^{corr} = \frac{1}{2}(u_1 + u_4)
	\hspace{1.5cm} v_-^{corr} = \frac{1}{2}(v_1 + v_4) \\
	y_+^{corr}= \frac{1}{2}(y_2 + y_4) \hspace{1.5cm} u_+^{corr} = \frac{1}{2}(u_2 + u_4)
	\hspace{1.5cm} v_+^{corr} = \frac{1}{2}(v_2 + v_4)
	\end{array}
\label{eqn:corrector}
\end{equation}