% !TEX TS-program = pdflatexmk
\documentclass{article}

\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{amsthm}
\usepackage{natbib}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}

\title{Sparse Graph Prior for Knowledge Graph}
\date{\today}
\author{Dongwoo Kim\\ANU}

\begin{document}

\maketitle
\section{Completely Random Measure}
A completely random measure (CRM) $\mu$ on $\mathbb{R}_+$ is a random measure such that for any countable number of disjoint measurable sets $A_1, A_2, ...$ of $\mathbb{R}_+$, the random variable $\mu(A_1), \mu(A_2), ...$ are independent and $\mu(\cup_i A_i) = \sum_i \mu(A_i)$. If one assumes that the distribution of $\mu([t,s])$ only depends on the difference $t-s$ then the CRM takes the form of $\mu = \sum_{i=1}^{\infty}w_i\delta_{\theta_i}$ where $(w_i, \theta_i)$ are the points of a Poisson point process on $\mathbb{R}_+^2$ with L\'{e}vy intensity measure $\nu(dw, d\theta) = \rho(dw)\lambda(d\theta)$\footnote{Subordinator.}. The Laplace transform of $\mu(A)$ on any measurable set $A$ has a following representation: $\mathbb{E}[e^{-t\mu(A)}] = \exp(-\int_{\mathbb{R}_+ \times A}(1-e^{-tw})\rho(dw)\lambda(d\theta))$ for any $t>0$ and $\rho$ such that $\int_{\mathbb{R}_+}(1-e^{-w})\rho(dw) < \infty$. Laplace exponent is $\psi(t) = \int_{\mathbb{R}} (1 - e^{-t w}) \rho(dw)$.

\section{Caron and Fox Model}

\cite{Caron2015} propose a simple point process on $\mathbb{R}^2$ as a product measure of a complete random measure. They propose a hierarchical model for undirected graphs
\begin{align}
\mu &= \sum_{i=1}^{\infty} w_i \delta_{\theta_i} & &\mu \sim \text{CRM}(\rho, \lambda)\\
D &= \sum_{i,j} n_{ij} \delta_{(\theta_i, \theta_j)} & &D|\mu \sim \text{PP}(\mu \times \mu)\\
Z &=\sum_{i,j} \min(n_{ij} + n_{ji}, 1)\delta_{(\theta_i, \theta_j)}, \label{eqn:cnf}&&
\end{align}
with intensity measure $\nu$ factorising as $\nu(dw, d\theta) = \rho(dw) \lambda(d\theta)$ for a jump part of the measure $\rho$ and Lebesgue measure $\lambda$. $D$ is simply generated from a Poisson process with a product measure as an intensity and can be interpreted as a directed multi-graph.
Given $\mu$, we can directly specify the undirected graph $Z$ as
\[ Pr(z_{ij}=1|w) =
  \begin{cases}
    1 - \exp(-2w_iw_j)       & \quad i \neq j\\
    1 - \exp(-w_i^2) & \quad i = j.\\
  \end{cases}
\]

They show that the resulting graph is sparse, i.e. \# of edges = $o$(\# of nodes$^2$)\footnote{only counts the nodes which has at least one edge}, if the intensity measure\footnote{This is the L\'{e}vy intensity of the generalised gamma process} is
\begin{align}
\rho(dw) = \frac{1}{\Gamma(1-\sigma)}w^{-1-\sigma}e^{-\tau w}dw,
\end{align}
where the two parameters range
\begin{align}
(\sigma, \tau) \in (0,1) \times [0, +\infty)
\end{align}
and dense if the intensity measure is finite activity, i.e. $\int_{0}^{\infty} \rho(w)dw < \infty$.

The general construction of the sparse graph in Equation \ref{eqn:cnf} results an infinite number of edges due to $\mu(\mathbb{R}_+) = \infty$. A restriction of Lebesgue measure $\lambda$ on $[0, \alpha]$ is used to obtain a finite graph ($\lambda_\alpha = \lambda\delta_{[0, \alpha]}$). Therefore, restricted graph $Z_\alpha$ is defined on the box $[0,\alpha]^2$. We also denote the total mass on $[0, \alpha]^2$ by $Z_\alpha^* = Z_\alpha([0, \alpha]^2)$, and similarly for $D_\alpha^*$ and $\mu_\alpha^*$.

\section{Sparse Prior for Knowledge Graph}
A knowledge base consists of a set of triples (entity, entity, relation) such as (BarackObama, bornIn, Hawaii). The set of triples can be represented as a binary-valued three-way tensor where three dimensions represent entity, entity, and relation, respectively. Here, we directly extend the Caron and Fox's model for the three-way tensor based on two independent completely random measures.
\begin{align}
\mu &= \sum_{i=1}^{\infty} w_i \delta_{\theta_i} & &\mu \sim \text{CRM}(\rho, \lambda)\\
\mu' &= \sum_{k=1}^{\infty} w_k \delta_{\theta_k'} & &\mu' \sim \text{CRM}(\rho', \lambda)\\
D &= \sum_{i,j,k} n_{ijk} \delta_{(\theta_i, \theta_j, \theta_k')}& &D \sim \text{PP}(\mu \times \mu \times \mu') && \\
Z &=\sum_{i,j,k} \min(n_{ijk}, 1)\delta_{(\theta_i, \theta_j, \theta_k')}, &&
\end{align}
where $Z$ is asymmetric in $i$ and $j$ since the knowledge graph is a directed multi-graph. As done in the original model, we can also specify $Z$ as
\[ Pr(z_{ijk}=1|w, w') =
  \begin{cases}
    1 - \exp(-w_iw_jw_k')       & \quad i \neq j\\
    1 - \exp(-w_i^2w_k') & \quad i = j.\\
  \end{cases}
\]
If we consider $\theta_i$, $\theta_j$, and $\theta_k'$ as nodes in the graph, the above construction will generate a hypergraph where each edge connects three nodes. In the notion of knowledge graphs, it is more intuitive to consider a relation as a type of edge between two entities. In this case, we define two random measures on $\mathbb{R}_+^2$:
\begin{align}
\bar{D} &= \sum_{i,j}\sum_{k}z_{ijk}\delta_{\theta_i,\theta_j}\\
\bar{Z} &= \sum_{i,j}\min(\bar{D}(\{\theta_i, \theta_j\}), 1) \delta_{(\theta_i,\theta_j)},
\end{align}
where $\bar{D}$ is a multigraph, and $\bar{Z}$ is a binary graph of a knowledge base.
\[ Pr(\bar{z}_{ij}=1|w, w') =
  \begin{cases}
    1 - \exp(-w_iw_j\sum_{k}w_k')       & \quad i \neq j\\
    1 - \exp(-w_i^2\sum_{k}w_k') & \quad i = j.\\
  \end{cases}
\]
To obtain a finite hypergraph (the number of edges is finite), we consider restrictions ${D}_{\alpha\beta}$ and ${Z}_{\alpha\beta}$ to the box $[0,\alpha]^2\times[0,\beta]$. We denote by $Z_{\alpha\beta}^* = Z_{\alpha\beta}([0,\alpha]^2\times[0,\beta])$ the total mass on the restricted area, and similar for $D_{\alpha\beta}^*$ and $\mu_{\alpha}^*$.

\subsection{Generative Process through Urn approach}
Given restriction $\alpha$ and $\beta$, the generative process of $D_{\alpha\beta}$ can be specified as follows:
\begin{enumerate}
\item $\mu_\alpha \sim \text{CRM}(\rho, \lambda_\alpha)$
\item $\mu_\beta' \sim \text{CRM}(\rho', \lambda_\beta)$
\item $D_{\alpha\beta}^* | \mu_\alpha, \mu_\beta' \sim \text{Poisson}(\mu_\alpha^{*2}{\mu'}_\beta^{*})$
\item For $d=1,...,D_{\alpha\beta}^*$:
\begin{enumerate}
\item $\theta_{di} \sim \frac{\mu_\alpha}{\mu_\alpha^*}$
\item $\theta_{dj} \sim \frac{\mu_\alpha}{\mu_\alpha^*}$
\item $\theta_{dk}' \sim \frac{\mu_\beta}{{\mu'}_\beta^{*}}$
\end{enumerate}
\item $D_{\alpha\beta} = \sum_{d=1}^{D_{\alpha\beta}^*} \delta_{(\theta_{di}, \theta_{dj}, \theta_{dk})}$,
\end{enumerate}
where we have used that the total mass of $D_{\alpha\beta}^*$ follows the Poisson distribution. Each node $\theta_i$ is drawn from the normalised CRM (NRM), $\frac{\mu_\alpha}{\mu_\alpha^*}$, which is discrete with probability 1. However, it is not possible to sample $\mu_\alpha$ and $\mu'_\beta$ since these measures have infinite number of atoms. Instead we can simulate finite-dimensional generative process through the urn formulation. Let $\theta_1, ..., \theta_n$ drawn from the normalised CRM $\frac{\mu_\alpha}{\mu_\alpha^*}$. Since NRM is discrete, variables $\theta_1, ..., \theta_n$ takes $l \leq n$ distinct values $\phi_l$, and $m_l$ is the number of variables corresponding to $\phi_l$.
Given total mass $\mu_\alpha^*$ and $\theta_1, ..., \theta_n$, the conditional distribution of $\theta_{n+1}$ can be modelled in terms of exchangeable partition probability function (EPPF):
\begin{align}
\label{eqn:eppf}
\theta_{n+1} | \mu_\alpha^*, \theta_1,...,\theta_n \sim \frac{\Pi_{n+1}^{l+1}(m_1, ..., m_l, 1 | \mu_\alpha^*)}{\Pi_{n}^{l}(m_1, ..., m_l | \mu_\alpha^*)} \frac{1}{\alpha} \lambda_\alpha
+ \sum_{i=1}^{l}\frac{\Pi_{n+1}^{l}(m_1, ..., m_{i}+1, ..., m_l | \mu_\alpha^*)}{\Pi_{n}^{l}(m_1, ..., m_l| \mu_\alpha^*)} \delta_{\phi_l}
\end{align}
where
\begin{align}
\Pi_{n}^l(m_1, ..., m_l|\mu_\alpha^*) = \frac{\sigma^l \mu_\alpha^{*-n}}{\Gamma(n-l\sigma)g_{\sigma}(\mu_\alpha^*)} \int_{0}^{\mu_\alpha^*}s^{n-l\sigma-1}g_{\sigma}(\mu_\alpha^*-s)ds \bigg(\prod_{i=1}^{l} \frac{\Gamma(m_i-\sigma)}{\Gamma(1-\sigma)} \bigg),
\end{align}
and $g_\sigma$ is the pdf of the positive stable distribution.
Finally, the total mass of $\mu_\alpha^*$ and ${\mu'}_\beta^{*}$ follows an exponentially tilted stable distribution where the exact sampler exists \citep{devroye2009random,hofert2011sampling}.

Using this urn representation, we can rewrite the generative process as
\begin{enumerate}
\item $\mu_\alpha^* \sim P_{\mu_\alpha^*}$
\item ${\mu'}_\beta^{*} \sim P_{{\mu'}_\beta^{*}}$
\item $D_{\alpha\beta}^* | \mu_\alpha, \mu_\beta' \sim \text{Poisson}(\mu_\alpha^{*2}{\mu'}_\beta^{*})$
\item For $d=1,...,D_{\alpha\beta}^*$:
\begin{enumerate}
\item Sample $\theta_{di}$, $\theta_{dj}$, and $\theta_{dk}'$ with Urn process in Eqn \ref{eqn:eppf}
\end{enumerate}
\item $D_{\alpha\beta} = \sum_{d=1}^{D_{\alpha\beta}^*} \delta_{(\theta_{di}, \theta_{dj}, \theta_{dk})}$,
\end{enumerate}

\subsection{Characteristics of Random Graph in Gamma process case ($\sigma=0$)}
In case $\sigma=0$, $\rho(dw)$ is an intensity of the Gamma process where the sum of the weights $\mu_\alpha^*$ follows Gamma distribution with shape parameter $\alpha$ and scale parameter $\tau$.

\subsubsection{Expected number of triples}
From the generative process of the random graph, the number of total edge follows the poisson distribution with mean intensity $\mu^{*2}_\alpha \mu^*_\beta$.
\begin{align}
\mathbb{E}[D^*_{\alpha\beta}] & = \mathbb{E}[\mu^{*2}_\alpha]\mathbb{E}[\mu^*_\beta] \\
&= (\text{Var}(\mu^*_\alpha) + \mathbb{E}[\mu^{*}_\alpha]^2)\mathbb{E}[\mu^*_\beta]\\
& = \frac{\alpha(\alpha+1)}{\tau} \frac{\beta}{\tau}
\end{align}

\subsubsection{Expected number of entities and relations}
From the generative process of the random graph, we can compute the expected number of entities $N_\alpha$ as
\begin{align}
\mathbb{E}[N_\alpha|D^*_{\alpha\beta}] = \mathbb{E}\bigg[\sum_{i=1}^{2D^*_{\alpha\beta}} Y_i\bigg],
\end{align}
where
\begin{align}
Y_i \sim \text{Ber}\bigg(\frac{\alpha}{\alpha+i-1}\bigg).
\end{align}
So, the expected number of entities for the large number of $2D^*_{\alpha\beta}$ can be approximated as
\begin{align}
\mathbb{E}[N_\alpha|D^*_{\alpha\beta}] = \sum_{i=1}^{2D^*_{\alpha\beta}}\frac{\alpha}{\alpha+i-1} = \alpha(\Psi(\alpha + 2D^*_{\alpha\beta}) - \Psi(\alpha)) \approx \alpha \log (\alpha + 2D^*_{\alpha\beta})
\end{align}
where $\Psi$ is a digamma function \citep{arratia2003logarithmic}. By using Theorem 8 in \citep{Caron2015}, we can further show $\mathbb{E}[N_\alpha] = \Theta(\alpha \log\alpha)$ as $\alpha \rightarrow \infty$.
The expected number of relations can be computed in a similar way:
\begin{align}
\mathbb{E}[N_\beta|D^*_{\alpha\beta}] = \sum_{j=1}^{D^*_{\alpha\beta}}\frac{\beta}{\beta+j-1} = \beta(\Psi(\beta + D^*_{\alpha\beta}) - \Psi(\beta)) \approx \beta \log (\beta + D^*_{\alpha\beta})
\end{align}
Since $N_\alpha$ and $N_\beta$ is independent,
\begin{align}
\mathbb{E}[N_\alpha N_\beta|D^*_{\alpha\beta}] \approx \alpha \log (\alpha + 2D^*_{\alpha\beta}) \times \beta \log (\beta + D^*_{\alpha\beta})
\end{align}

%Unlike the two-dimensional space case, graph prior on three dimensional space may have various definition of the sparsity of the graph. Since our focus here is the growth rate of various statistics as the graph restriction $\alpha$ and $\beta$ increase. We list some statistics to characterise the random graph.
%
%To compute the (expected) growth rate of the number of triples with respect to the number of entities and relations, we first identify the growth rate of the number of triples, entities, and relations with respect to the varying graph restriction $\alpha$ and $\beta$. Let $N^{e}_{\alpha\beta}$ be the number of triples given $\alpha$ and $\beta$, $N_{\alpha}$ be the number of entities given $\alpha$, and $N_{\beta}$ be the number of relations given $\beta$. Here are some statistics that might help to shape these characteristics.
%
%\begin{align}
%\frac{E[N^e_{\alpha\beta}]}{E[N_\alpha N_\beta]} &\text{ as }\alpha \rightarrow \infty, \beta \rightarrow \infty \\
%\frac{E[N_\alpha|\beta]}{\alpha} &\text{ as } \alpha \rightarrow \infty \\
%\frac{E[N_\alpha]}{\alpha} &\text{ as } \alpha \rightarrow \infty \\
%\frac{E[N_\beta|\alpha]}{\beta} &\text{ as } \beta \rightarrow \infty \\
%\frac{E[N_\beta]}{\beta} &\text{ as } \beta \rightarrow \infty \\
%\frac{E[N^e_{\alpha\beta}]}{\alpha\beta} &\text{ as }\alpha \rightarrow \infty, \beta \rightarrow \infty \\
%\frac{E[N^e_{\alpha}|\beta]}{\alpha} &\text{ as }\alpha \rightarrow \infty
%\end{align}
%
%
%\begin{theorem} \label{thm:edge} Consider the point process $\bar{Z}$ with infinite-activity intensity measures $\rho(dw)$ and $\rho'(dw')$. Given $\mu'$ from $\rho'(dw')$, the number of edges in $\bar{Z}_{\alpha}$ grows quadratically as $\alpha \rightarrow \infty$ almost surely.
%\end{theorem}
%\begin{proof}
%$\sum_{k=1}^{\infty} w_k' < \infty$ a.s. When $\mu'$ is given and the sum of $w_k'$ is finite a.s., we can use the same proof technique used in \cite{Caron2015}.
%\end{proof}
%What if $\mu'$ is not given? Let $(X_i)$ and $(Y_k)$ be i.i.d. real-valued random variable from $p$ and $q$, respectively, and let $h(x_1, x_2, y_1)$ be a measurable function symmetric in the first two arugments.
%\begin{align}
%\frac{2 \sum_{i<j}\sum_{k} h(X_i, X_j, Y_k)}{n_x(n_x -1) n_y} \xrightarrow[]{?} \mathbb{E}[h(X_i, X_j, Y_k)]\quad a.s.\quad as \quad n\rightarrow \infty
%\end{align}
%If this strong law of the large numbers for two samples is correct, we may proof Theorem 3.1 in more general case ($\mu'$ is not given).
%
%\begin{theorem} Consider the point process $\bar{Z}$ with infinite-activity intensity measures $\rho(dw)$ and $\rho'(dw')$. Let $N_\alpha$ be a number of nodes having at least one connection. Given $\mu'$ from $\rho'(dw')$, the number of nodes $N_\alpha$ in $\bar{Z}_{\alpha}$ grows superlinearly as $\alpha \rightarrow \infty$ almost surely.
%\end{theorem}
%\begin{proof}
%As \ref{thm:edge}.
%\end{proof}

\subsection{Posterior inference}
We first characterise the posterior of $\mu_\alpha$ given $\mu'_\beta$ and $D_{\alpha\beta}$. The conditional Laplace functional of $\mu_\alpha$ given $D_{\alpha\beta}$ is $\mathbb{E}[e^{-\mu_\alpha(f)}|\mu'_\beta, D_{\alpha\beta}]$, for any non-negative measurable function $f$ such that $\mu_\alpha(f) = \sum_{i=1}^{\infty}w_i f(\theta_i)$. We have $\mu_\alpha(f) = \Pi(\tilde{f})$ where $\Pi = \sum_{i=1}^{\infty} \delta_{w_i, \theta_i}$ is a Poisson random measure on $\mathcal{S} = (0, \infty) \times [0, \alpha]$ with mean measure $\rho \times \lambda$ and $\tilde{f}(w, \theta) = wf(\theta)$. Let $n_{i**} = \sum_{j=1}^{N_\alpha}\sum_{k=1}^{N_\beta}n_{ijk}$, $m_i = \sum_{j=1}^{N_\alpha}\sum_{k=1}^{N_\beta} n_{ijk} + n_{jik}$, and $m'_k = \sum_{i=1}^{N_\alpha} \sum_{j=1}^{N_\alpha} n_{ijk}$.

\begin{align}
\label{eqn:lpl_d}
\mathbb{E}_{\mu_\alpha}[e^{-\mu_\alpha(f)}|D_{\alpha\beta}, \mu'_\beta]
&= \mathbb{E}_{\Pi}[e^{-\int \tilde{f}(w, \theta)\Pi(dw, d\theta)}|D_{\alpha\beta}, \mu'_\beta] \\
&= \frac{\mathbb{E}_{\Pi}[ e^{-\Pi(\tilde{f})} P(D_{\alpha\beta}|\Pi, \mu'_\beta)]}{\mathbb{E}_{\Pi}[P(D_{\alpha\beta}|\Pi, \mu'_\beta)]}\\
%&= \frac{\mathbb{E}_{\Pi}[e^{-\Pi(\tilde{f})} e^{-\Pi(h)^2{\mu'}_\beta^{*}} \prod_{i=1}^{N_\alpha} w_i^{m_i} \prod_{k=1}^{N_\beta} {w'_k}^{m_k} ]}{\mathbb{E}_{\Pi}[e^{-\Pi(h)^2{\mu'}_\beta^{*}} \prod_{i=1}^{N_\alpha} w_i^{m_i} \prod_{k=1}^{N_\beta} {w'_k}^{m_k} ]]}
&= \frac{\mathbb{E}_{\Pi}[e^{-\Pi(\tilde{f})} e^{-\Pi(h)^2{\mu'}_\beta^{*}} \prod_{i=1}^{N_\alpha} w_i^{m_i}]}{\mathbb{E}_{\Pi}[e^{-\Pi(h)^2{\mu'}_\beta^{*}} \prod_{i=1}^{N_\alpha} w_i^{m_i}]]}
\end{align}
where $h(w, \theta) = w$ and
\begin{align}
P(D_{\alpha\beta}|\Pi, \mu'_\beta) & = P(D_{\alpha\beta}|\mu_\alpha, \mu'_\beta)\\
& = \text{Poisson}(D^*_{\alpha\beta}|\mu_\alpha^{*2}{\mu'}_\beta^{*})
\prod_{i=1}^{N_\alpha} P(n_{i**}|\mu_\alpha) \prod_{j=1}^{N_\alpha} P(n_{*j*}|\mu_\alpha)
\prod_{k=1}^{N_\beta} P(n_{**k}|\mu_\beta) \\
& = \frac{ (\mu_\alpha^{*2}{\mu'}_\beta^{*})^{D^*_{\alpha\beta}} e^{-\mu_\alpha^{*2}{\mu'}_\beta^{*}} }{D^*_{\alpha\beta}!}
\prod_{i=1}^{N_\alpha} \Big( \frac{w_i}{\mu_\alpha^*} \Big)^{n_{i**}}
\prod_{j=1}^{N_\alpha} \Big( \frac{w_j}{\mu_\alpha^*} \Big)^{n_{*j*}}
\prod_{k=1}^{N_\beta} \Big( \frac{w'_k}{{\mu'}_\beta^{*}} \Big)^{n_{**k}}\\
& =\frac{e^{-\mu_\alpha^{*2}{\mu'}_\beta^{*}} }{D^*_{\alpha\beta}!}
\prod_{i=1}^{N_\alpha} w_i^{m_i}
\prod_{k=1}^{N_\beta} {w'_k}^{m_k}
 =\frac{e^{-\Pi(h)^2{\mu'}_\beta^{*}} }{D^*_{\alpha\beta}!}
\prod_{i=1}^{N_\alpha} w_i^{m_i}
\prod_{k=1}^{N_\beta} {w'_k}^{m_k}\\
\\
\mu_\alpha^* & = \sum_{i=1}^{\infty} w_i, \qquad {\mu'}_\beta^{*} = \sum_{k=1}^{\infty} w'_k = \sum_{k=1}^{N_\beta} w'_k + {w'}^*
\end{align}
Applying the generalised Palm formula to the numerator yields
\begin{align}
&\mathbb{E}_{\Pi}\Big[e^{-\Pi(\tilde{f})} e^{-\Pi(h)^2{\mu'}_\beta^{*}} \prod_{i=1}^{N_\alpha} w_i^{m_i} \Big] &\\
&= \mathbb{E}_{\Pi}\Big[e^{-\Pi(\tilde{f})} e^{-\Pi(h)^2{\mu'}_\beta^{*}} \prod_{i=1}^{N_\alpha} \sum_{w_j, \vartheta_j \in \Pi} w_j^{m_i} \mathbf{1}_{\theta_i}(\vartheta_j)\Big]& \\
&= \mathbb{E}_{\Pi}\Big[\int_{\mathcal{S}^{N_\alpha}} e^{-\Pi(\tilde{f})} e^{-\Pi(h)^2{\mu'}_\beta^{*}}  \prod_{i=1}^{N_\alpha} w_j^{m_i} \mathbf{1}_{\theta_i}(\vartheta_j) \Pi(dw_j, d\vartheta_j)\Big]& \\
&=  \int_{\mathcal{S}^{N_\alpha}} \mathbb{E}_{\Pi}\Big[ e^{-(\Pi+\sum_i^{N_\alpha}\delta_{(w_i, \theta_i)})(\tilde{f})} e^{-(\Pi+\sum_i^{N_\alpha}\delta_{(w_i, \theta_i)})(h)^2{\mu'}_\beta^{*}}\Big] \prod_{i=1}^{N_\alpha} w_j^{m_i} \mathbf{1}_{\theta_i}(\vartheta_j) \rho(dw_j)\lambda(d\vartheta_j) & \\
&=  \int_{\mathcal{S}^{N_\alpha}} \mathbb{E}_{\mu_\alpha}\Big[ e^{-\mu_\alpha(f) - \sum_{i=1}^{N_\alpha}w_if(\vartheta_j)} e^{-(\mu_\alpha(1) + \sum_{i=1}^{N_\alpha} w_i)^2{\mu'}_\beta^{*}}\Big] \prod_{i=1}^{N_\alpha} w_j^{m_i} \mathbf{1}_{\theta_i}(\vartheta_j) \rho(dw_j)\lambda(d\vartheta_j) &\\
&=  \int_{\mathcal{S}^{N_\alpha}} \mathbb{E}_{\mu_\alpha^*}\bigg[ \mathbb{E}_{\mu_\alpha} \Big[e^{-\mu_\alpha(f)}|\mu_\alpha^* \Big] e^{- \sum_{i=1}^{N_\alpha}w_if(\vartheta_j)} e^{-(\mu_\alpha^* + \sum_{i=1}^{N_\alpha} w_i)^2{\mu'}_\beta^{*}}\bigg] \prod_{i=1}^{N_\alpha} w_j^{m_i} \mathbf{1}_{\theta_i}(\vartheta_j) \rho(dw_j)\lambda(d\vartheta_j) &
\end{align}
The denominator is obtained by taking $f=0$.
\begin{align}
\mathbb{E}_{\mu_\alpha}[e^{-\mu_\alpha(f)}|D_{\alpha\beta}, \mu'_\beta] &= \int_{\mathbb{R}^{N_\alpha + 1}} E_{\mu_\alpha}[e^{-\mu_\alpha(f)}|\mu_\alpha^* = w^*]\\
&\quad \times e^{\sum_{i=1}^{N_\alpha}w_i f(\theta_i)} p(w_1, ..., w_{N_\alpha}, w^*|D_{\alpha\beta}, \mu_\beta) dw_{1:N_\alpha}dw^*
\end{align}
where
\begin{align}
p(w_1, ..., w_{N_\alpha}, w^*|D_{\alpha\beta}, \mu_\beta)&
= \frac{\prod_{i=1}^{N_\alpha} w_j^{m_i} \rho(w_i) e^{-(w^* + \sum_{i=1}^{N_\alpha} w_i)^2{\mu'}_\beta^{*}} g^*_\alpha(w^*)}
{\int_{\mathbb{R}^{N_\alpha + 1}}\prod_{i=1}^{N_\alpha} \tilde{w}_j^{m_i} \rho(\tilde{w}_i) e^{-(\tilde{w}^* + \sum_{i=1}^{N_\alpha} \tilde{w}_i)^2{\mu'}_\beta^{*}} g^*_\alpha(\tilde{w}^*) d\tilde{w}_{1:N_\alpha}d\tilde{w}^*}\\
\end{align}
$g^*_\alpha(w^*)$ is a density function of random variable $w^*$ of which Laplace transform is $\mathbb{E}[e^{tw^*}] = e^{\alpha\psi(t)}$. Therefore, the conditional of $\mu_\alpha$ given $D_{\alpha\beta}, \mu'_\beta$ is
\begin{align}
w^* \sum_{i=1}^{\infty}\tilde{P}_i\delta_{\tilde\theta_i} + \sum_{i=1}^{N_\alpha} w_i \delta_{\theta_i}
\end{align}
where $(\tilde{P})$ are distributed from a Poisson-Kingman distribution conditional on $w^*$, and the weights $w_1, ..., w_{N_\alpha}, w^*$ are jointly dependent conditional on $D_{\alpha\beta}$ and $\mu'_\beta$:
\begin{align}
p(w_1, ..., w_{N_\alpha}, w^* | D_{\alpha\beta}, \mu'_\beta) \propto \prod_{i=1}^{N_\alpha}{w_i}^{m_i} e^{(-w_* + \sum_{i=1}^{N_\alpha}w_i)^2 {\mu'}_\beta^*} \prod_{i=1}^{N_\alpha} \rho(w_i) g^*_\alpha(w^*)
\end{align}

The conditional Laplace functional of $\mu'_\beta$ given $\mu_\alpha$ and $D_{\alpha\beta}$ can be carried out in the same way as we've done in $\mu_\alpha$.

\bibliographystyle{apalike}
\bibliography{ref}

\end{document}


\subsection{Real numbers}\label{subsec:real_numbers}

\begin{definition}\label{def:set_of_rational_numbers}
  The \term{rational numbers} \( \BbbQ \) are the field of \hyperref[def:field_of_fractions]{fractions} of the \hyperref[def:set_of_integers]{integers}. Both operations from \( \BbbZ \) are inherited in \( \BbbQ \) and all nonzero elements in \( \BbbQ \) are now invertible, which makes \( \BbbQ \) a field.
\end{definition}

\begin{definition}\label{def:set_of_real_numbers}
  The \term{real numbers} \( \BbbR \) are the metric space \hyperref[def:complete_metric_space]{completion} of \( \BbbQ \) with respect to the absolute value. Unfortunately, real numbers are used for defining metric spaces, so we cannot rely on the theory of metric spaces. This can be circumvented by
  \begin{enumerate}
    \item Regarding \( \BbbQ \) as a \hyperref[def:uniform_space]{uniform space}.
    \item Using uniform space \hyperref[thm:uniform_space_completion]{completion} to obtain \( \BbbR \).
    \item Defining metric spaces.
    \item Showing that \( \BbbR \) is a metric space.
    \item Using \fullref{def:complete_metric_space/uniform} to automatically verify that \( \BbbR \) is complete as a metric space.
  \end{enumerate}
\end{definition}

\begin{definition}\label{def:extended_real_numbers}
  We are sometimes interested in \term{extended real numbers}. These can be any of the three sets
  \begin{itemize}
    \item \( \BbbR \cup \{ +\infty \} \),
    \item \( \BbbR \cup \{ -\infty \} \),
    \item \( \BbbR \cup \{ -\infty, +\infty \} \),
  \end{itemize}
  where \( -\infty \) and \( +\infty \) are both sentinel values that act as the \hyperref[def:partially_ordered_set_extremal_points/maximum_and_minimum]{greatest} and/or least real number.

  We generally avoid performing arithmetic operations on \( \pm \infty \), however it is sometimes convenient to define
  \begin{balign*}
    x + (+\infty)     & \coloneqq +\infty, x \in \BbbR
                      &                              &
    x \cdot (+\infty) & \coloneqq +\infty, x \in \BbbR
  \end{balign*}

  We leave the operations
  \begin{balign*}
     & (-\infty) + (+\infty)
     & (-\infty) \cdot (+\infty)
  \end{balign*}
  undefined.

  With these operations, the extended real numbers are no longer a \hyperref[def:field]{field}.
\end{definition}

\begin{definition}\label{def:floor_ceiling_functions}
  Let \( x \in \BbbR \) be a real number. In analogy with \fullref{def:commutative_ring_division}, we define its \term{floor}
  \begin{equation*}
    \floor(x) \coloneqq \max \{ n \in \BbbZ : n \leq x \},
  \end{equation*}
  its \term{ceiling}
  \begin{equation*}
    \ceil(x) \coloneqq \min \{ n \in \BbbZ : n \geq x \}
  \end{equation*}
  and its \term{fractional part}
  \begin{equation*}
    \op{frac}(x) \coloneqq x - \floor(x).
  \end{equation*}
\end{definition}

\begin{proposition}\label{thm:reals_not_algebraically_closed}
  The field \( \BbbR \) is not algebraically \hyperref[def:algebraically_closed_field]{closed}.

  In particular, the polynomial \( x^2 + 1 \) has no root.
\end{proposition}
\begin{proof}
  Assume that \( \BbbR \) is algebraically closed and that the polynomial \( x^2 + 1 \) has at least one root. Denote one of them by \( u \).

  By the \hyperref[def:binary_relation/trichotomic]{trichotomy} of the order \( < \) of \( \BbbR \), we have either \( u < 1 \) or \( u > 1 \) since \( u \neq 1 \).

  If \( u < 0 \), then \( u^2 = -1 < 0 \), which is impossible because the image of \( x \mapsto x^2 \) is the interval \( [0, \infty) \).

  If \( u > 0 \), then \( u^2 = -1 < 0 = 0 \), which is also impossible because \( x \mapsto x^2 \) is monotone on \( [0, \infty) \).

  Thus, \( u \) is not a root of \( x^2 + 1 \) and \( \BbbR \) is not algebraically closed.
\end{proof}

\begin{definition}\label{def:signum}
  We define the \term{signum} function \( \sgn: \BbbR \to \{ -1, 0, 1 \} \) as
  \begin{equation*}
    \sgn(x) \coloneqq \begin{cases}
      1,  & x > 0, \\
      0,  & x = 0, \\
      -1, & x < 0.
    \end{cases}
  \end{equation*}
\end{definition}


\section{Mathematical Preliminaries}
\label{sec:prelim}
In this paper, vector- and tensor-valued quantities
are denoted by bold letters (e.g. $\bh$ and $\mathbf{T}$).
Subscript indices of non-bold characters (e.g. $h_j$ or $T_{ij\ell}$)
are used to denote the entries within a vector or tensor.
We use the standard Einstein summation convention; i.e.,
there is an implied sum taken over the repeated indices of
any term (e.g. the symbol $a_{j} b_{j}$ is used to represent the sum
$\sum_{j} a_{j} b_{j}$).
If $\xx = (x_1,x_2)^\intercal$, then $\xx^\bot = (-x_2,x_1)^\intercal$.
Similarly, $\nabla^\bot = (-\partial_{x_2},\partial_{x_1})^\intercal$.
Upper-case script characters (e.g. $\mathcal{K}$) are reserved for
operators on Banach spaces, with $\mathcal{I}$ denoting the
identity. Given a set $X$, we denote the closure of $X$
by $\overline{X}$.

For a velocity field $\bu$ and pressure $p$, let $\bsigma(\bu,p)$
denote the Cauchy stress tensor is given by
\begin{equation}
\bsigma(\uu,p) = -p \II + 2 \be(\bu) \, ,
\end{equation}
where $\be(\bu)$ is the strain tensor given by
\begin{equation}
e_{ij}(\bu) = \frac{1}{2} \left( \partial_{x_j} u_i + \partial_{x_i} u_j \right) \; .
\end{equation}
When it is clear from context, we will drop the dependence of
$\bsigma$ on $\bu$ and $p$.
If $\Gamma$ is the boundary of a region $\Omega$ and $\bnu$ is the outward
normal to $\Gamma$, the surface traction $\bt$ on $\Gamma$
is the Neumann data, i.e.
\begin{equation}
\bt = \bsigma \cdot \bnu \, .
\end{equation}

We seek solutions of \cref{eq:ostokes} in the space
\begin{equation}
  A(\Omega) = \{ (\bu,p) \textrm{ s.t. } \bu \in
  \left ( C^2(\Omega)\times C^2(\Omega) \right ) \cap
  \left ( C(\bar{\Omega}) \times C(\bar{\Omega}) \right ) \, , \,
  p \in C^1(\Omega) \cap C(\bar{\Omega})\} \; ,
\end{equation}
where $\Omega$ is an open domain.

\subsection{Green's functions}

Let $\mathcal{L}_x$ denote a linear differential operator. A fundamental
solution $G(\xx,\yy)$ of $\mathcal{L}_x$ satisfies the equation
$\mathcal{L}_x G(\xx,\yy) = \delta_y(\xx)$ in the distributional sense, i.e.
for sufficiently smooth $f$
\begin{equation}
  \mathcal{L}_x \int_{\R^2} G(\xx,\yy) f(\yy) \, d \by = f(\xx) \; .
  \nonumber
\end{equation}
We consider here
free-space Green's functions, i.e. fundamental solutions which satisfy appropriate
radiation conditions as $|\xx-\yy| \to \infty$.
The Green's function of the oscillatory biharmonic equation,
\begin{equation}
  \Delta ( \Delta + k^2 ) u = 0 \; , \label{eq:obiharm} \nonumber
\end{equation}
is given by
\begin{equation}
  \Gbh(\xx,\yy) = \frac{1}{k^2}
  \left (\frac{1}{2\pi} \log |\xx-\yy| +
  \frac{i}{4} H_0^{(1)}(k|\xx-\yy|) \right ) \, ,
  \label{eq:Gbh}
\end{equation}
where $k$ is the Helmholtz parameter in the oscillatory biharmonic equation,
and $H_{0}^{1}(r)$ is the Hankel function of the first kind of order zero.
Note that this is a scaled difference of the Green's function for
Laplace, i.e.

\begin{equation}
  \Glap(\xx,\yy) = \frac{1}{2\pi} \log |\xx-\yy| \; , \nonumber
\end{equation}
and the Green's function for the Helmholtz equation

\begin{equation}
  \Ghelm(\xx,\yy) = -\frac{i}{4} H_0^{(1)}(k|\xx-\yy|) \; . \nonumber
\end{equation}

\subsection{The Fredholm Alternative}

We require some standard results from the theory of
Fredholm integral equations. Interested readers may
consult \cite{reed1972methods,colton1983integral,kress1989linear},
among others, for the relevant background.

We first recall some definitions.
Let $X$ and $Y$ be Banach spaces with a non-degenerate
bilinear form $\langle \cdot ,\cdot \rangle: X\times Y \to \C$.
\begin{itemize}
\item Two operators $\cA:X\to X$ and $\cB:Y\to Y$ are
adjoint operators if
$\langle A \phi,\psi \rangle = \langle \phi, B\psi \rangle$
for every $\phi \in X$ and $\psi \in Y$.
\item For an operator $\cM:X\to X$, we can define the range
  $R(\cM)$ as the set $\{\phi \in X: \exists \phi_0 \textrm{ with }
  \cM\phi_0 = \phi\}$ and the null space $N(\cM)$ as the
  set $\{\phi \in X: \cM \phi = 0 \}$.
\item An operator $\cA$ is said to be compact if
  $\overline{\cA V}$ is a compact set for any
  bounded subset $V\subset X$.
\item Given a subspace $V\subset X$,
  we can define the subspace $V^\perp\subset Y$ as the
  set $V^\perp = \{ \psi \in Y: \langle \phi,\psi \rangle = 0
  \textrm{ for each } \phi \in V \}$, with the analogous
  definition for subspaces of $Y$.
\end{itemize}

Operators of the form $\cI-\cA$
have existence and uniqueness properties analogous to
matrices. This is known as the Fredholm Alternative;
we present the version provided in \cite{colton1983integral}.

\begin{thrm}[Fredholm Alternative \cite{colton1983integral}]
  Let $X$ and $Y$ be Banach spaces and
  $\langle \cdot,\cdot \rangle: X\times Y \to \C$ be
  a bilinear form. Suppose that $\cA: X\to X$ and
  $\cB:Y \to Y$ are compact adjoint operators. Then
  $\dim N(\cI-\cA) = \dim N(\cI-\cB) \in \N$,
  $R(\cI-\cA) = N(\cI-\cB)^{\perp}$, and
  $R(\cI-\cB) = N(\cI-\cA)^\perp$.
\end{thrm}

\subsection{Properties of the oscillatory Stokes layer
  potentials}

Recall that, in the case $k=i\alpha$ for some real-valued $\alpha$,
the oscillatory Stokes equations \cref{eq:ostokes}
are known as the modified Stokes equations and are of particular
interest for their application to the analysis and numerical
simulation of unsteady flow
\cite{Pozrikidis1992,biros2002embedded,
  jiang2013second,ladyzhenskaya1969mathematical}.
The equations are well-studied in that setting and
integral representations which lead to second kind
integral equations have been developed. We review
some of the relevant results here, translating to
the oscillatory setting.

\subsubsection{Oscillatory Stokeslets and stresslets}
Consider the solution of
\cref{eq:ostokes} where a $\delta$-mass
centered at $\yy$ with strength $\ff$
has been added to the right-hand side of \cref{eq:ostokes}, i.e.

\begin{align}
  \nabla p - \Delta \uu - k^2 \uu &= \delta_\yy \ff \; ,
  \label{eq:ostokes_charge}  \\
  \nabla \cdot \uu &= 0 \; . \nonumber
\end{align}
Recall that

\begin{equation}
 \Delta \Glap(\xx,\yy) = \delta_\yy(\xx) \; . \label{eq:lapdelta}
\end{equation}
If we substitute \eqref{eq:lapdelta} into
\eqref{eq:ostokes_charge} and take the divergence,
we obtain

\begin{equation}
  p = \nabla \Glap(\xx,\yy) \cdot \ff \; . \nonumber
\end{equation}
We then have, formally,

\begin{align}
  \uu &= - (\Delta + k^2)^{-1} ( \Delta \Glap \ff
  - \nabla (\nabla \Glap \cdot \ff ) ) \nonumber \\
  &= \left ( -\Delta + \nabla \otimes \nabla \right )
  \Gbh \ff \; . \nonumber
\end{align}
The tensor

\begin{equation} \label{eq:ostokeslet}
  \GG = - \II \Delta \Gbh + \nabla \otimes \nabla \Gbh
\end{equation}
is then the analog of a Stokeslet
\cite{Pozrikidis1992} for \eqref{eq:ostokes}.

A related object is the stresslet, which is defined
in terms of the stress tensor of the velocity, pressure
pair induced by a Stokeslet. For these tensors, we find
that it is more convenient to express them in index notation
with the Einstein index summing convention.
Recall that the stress tensor $\bsigma$ is defined as

\begin{equation}
  \sigma_{ij} = -p \delta_{ij} + \left ( \partial_{x_j}u_i
  +\partial_{x_i} u_j \right ) \; , \nonumber
\end{equation}
where $\delta_{ij}$ is the standard Kronecker delta notation.
The stresslet $\TT$ is defined to be

\begin{align}
  T_{ij\ell} &= - \partial_{x_j} \Glap \delta_{i\ell}
  + \partial_{x_\ell} \left ( -\Delta \Gbh \delta_{ij} +
  \partial_{x_i} \left(\partial_{x_j} \Gbh \right) \right)
  \nonumber \\
  & \qquad+ \partial_{x_i} \left ( -\Delta \Gbh \delta_{\ell j} +
  \partial_{x_\ell} \left(\partial_{x_j} \Gbh \right) \right)
  \; . \label{eq:ostress}
\end{align}
Let $u_i = G_{ij} f_j$ and $p = \partial_{x_i} \Glap f_i$ be a
solution of the Stokes equations induced by a Stokeslet.
Then the corresponding stress tensor is given by
$\sigma_{i\ell} = T_{ij\ell} f_j$.

%For the layer potentials of the next section, the following
%formulas are useful. Let $\bnu$ be a given vector. When
%summing over the third index, we obtain
%
%\begin{equation}
%  \TT_{\cdot,\cdot,\ell} \nu_\ell = -\bnu \otimes \nabla \Glap
%  + \partial_\nu \left ( -\Delta \Gbh \II
%  + \nabla \otimes \nabla \Gbh \right)
%  + \nabla \otimes \left ( -\Delta \Gbh \bnu
%  + \partial_{\nu} \nabla \Gbh \right) \; . \nonumber
%\end{equation}
%Let $\btau = \bnu^\bot$. Then
%
%\begin{equation}
%  \TT_{\cdot,\cdot,k} \nu_k = -\bnu \otimes \nabla \Glap
%  - \nabla^\bot \otimes \nabla^\bot \partial_\nu \Gbh
%  +\nabla \otimes \nabla^\bot \partial_\tau \Gbh \; .
%  \nonumber
%\end{equation}

\subsubsection{Layer potentials}

We now use the Stokeslet and stresslet
to define the single
and double layer potentials for the oscillatory Stokes problem.
For $\xx \in \R^2$, the single layer potential with density $\bmu$
is defined to be

\begin{equation} \label{eq:singlelayer}
  \bS [\bmu] (\xx) = \int_\Gamma \GG (\xx,\yy) \bmu(\yy)
  \, dS(\yy) \; .
\end{equation}
We use the notation $\bsigma_\bS[\bmu]$ to denote the
stress tensor of the single layer at any given point
$\xx \in \R^2 \setminus \Gamma$.

For $\xx \in \R^2 \setminus \Gamma$, the double layer
potential with density $\bmu$ is defined to be

\begin{equation} \label{eq:doublelayer}
  \bD [\bmu] (\xx) = \int_\Gamma \left ( \TT_{\cdot,\cdot,\ell}(\xx,\yy)
  \nu_\ell(\yy)\right )^\intercal \bmu(\yy) \, dS(\yy) \; ,
\end{equation}
where $\bnu$ denotes the outward unit normal to the boundary.
If we write $\bmu = \bnu \mu_\nu + \btau \mu_\tau$,
where $\btau = \bnu^\bot$ is the positively oriented unit
tangent to the curve, then we have

\begin{align} \label{eq:stokesdlkernel}
  \left ( \TT_{\cdot,\cdot,\ell}(\xx,\yy)\nu_\ell(\yy) \right )^\intercal
  \bmu(\yy) &= \left ( - \nabla \Glap(\xx,\yy) + 2 \nabla^\bot
  \partial_{\nu\tau} \Gbh(\xx,\yy) \right ) \mu_\nu(\yy) \nonumber \\
  & \qquad+
  \nabla^\bot \left (\partial_{\tau\tau}-\partial_{\nu\nu} \right )
  \Gbh(\xx,\yy) \mu_\tau(\yy) \; .
\end{align}

Let $\cS[\bmu]: C(\Gamma) \to C(\Gamma)$, and
$\cD[\bmu]: C(\Gamma) \to C(\Gamma)$
denote the restrictions of the layer potentials
$\bS[\bmu]$ and $\bD[\bmu]$ on the boundary $\Gamma$, i.e.
for $\bx \in \Gamma$,

\begin{equation}
  \cS [\bmu] (\xx) = \int_\Gamma \GG (\xx,\yy) \bmu(\yy)
  \, dS(\yy)
\end{equation}
and
\begin{equation}
\label{eq:dlformula}
  \cD [\bmu] (\xx) = \pv \int_\Gamma \left ( \TT_{\cdot,\cdot,\ell}(\bx,\by)
  \nu_\ell(\yy)
  \right )^\intercal \bmu(\yy) \, dS(\yy) \; ,
\end{equation}
where the \pv indicates that the integral is to be
evaluated in the principal value sense.

For two vector valued
functions $\ff$ and $\bg$ defined on $\Gamma$, consider the bilinear
form
\begin{equation} \label{eq:bi_form}
  \langle \ff , \bg \rangle = \int_\Gamma \ff \cdot \bg dS \; .
\end{equation}
The definition of the adjoint used throughout the paper will be
the one induced by this form.

The adjoint of $\cD$ with respect to the above bilinear form
is of particular interest; and is given by
\begin{equation}
  \cD^{\intercal} [\bmu](\bx) =
  \pv \int_\Gamma \left ( \TT_{\cdot,\cdot,\ell}(\bx,\by)\nu_\ell(\xx)
  \right ) \bmu(\yy) \, dS(\yy) \; .
\end{equation}

In the following lemma, we review the limiting values of
the layer potentials $\bS$ and $\bD$ on the boundary $\Gamma$.

\begin{lem}[Jump conditions] \label{lem:jump-conds}
  Suppose that $\Omega$ is a bounded region with a $C^{2}$ boundary
  $\Gamma$.
  Let $\bnu(\bx)$ denote the outward pointing normal at $\bx \in \Gamma$.
  Suppose that $\bmu \in C(\Gamma)$.
  Then $\bS[\bmu]$
  is continuous across $\Gamma$, and the exterior and interior
  limits of the surface traction of $\bD[\bmu]$ are equal.
  Furthermore, for $\bx_{0} \in \Gamma$,

  \begin{align}
    \lim_{h \downarrow 0^{+}} \bsigma_\bS[\bmu](\xx_0 \pm h\bnu(\xx_0)) \cdot \bnu(\xx_0)
    &= \mp \frac{1}{2} \bmu(\xx_0) + \cDt[\bmu](\xx_0) \\
    \lim_{h \downarrow 0^{+}} \bD[\bmu](\xx_0 \pm h\bnu(\xx_0))
    &= \pm \frac{1}{2} \bmu(\xx_0) + \cD[\bmu](\xx_0)    \; .
  \end{align}
\end{lem}

The above expressions are derived by noting that the
leading order singularity of these integral kernels
is the same as for the original Stokes case, so that
the standard jump conditions for Stokes
\cite{KimSangtae1991,Pozrikidis1992}
apply.

\begin{lem} \label{lem:compact-sd}
  Suppose that $\Omega$ is a bounded region with a $C^{2}$ boundary
  $\Gamma$. Then the operators $\cS$ and $\cD$ defined
  above are compact operators on $C(\Gamma)\times C(\Gamma)$
  and $\mathbb{L}^2(\Gamma)\times \mathbb{L}^2(\Gamma)$.
\end{lem}

Compactness is proved by considering the
asymptotic expansion of each kernel about
$\xx=\yy$ and noting that each is at most
weakly singular.

\subsubsection{Representation Theorem}

In the following theorem, we sketch the proof of the equivalent of the
Green's identity for oscillatory Stokes setting, which is well-known
in the Stokes and modified Stokes settings
\cite{Pozrikidis1992,biros2002embedded,ladyzhenskaya1969mathematical}.

\begin{thrm} \label{thrm:rep-theorem}
  Let $\Omega$ be a bounded domain with $C^2$ boundary and let
  the pair $(\bu,p)$ satisfy the oscillatory Stokes equations
  \cref{eq:ostokes} in $\Omega$. Let $\bt$ denote the surface
  traction associated with $(\bu,p)$. Then

  \begin{equation} \label{eq:rep-theorem}
    \bS [\bt](\xx) - \bD[\bu](\xx) = \begin{cases}
    \bu(\xx) &\quad \xx \in \Omega \,  \\
    0 &\quad \xx \in E
    \end{cases} \; ,
  \end{equation}
  where $E=\R^2\setminus\bar\Omega$ is the
  exterior of the domain.
\end{thrm}

\begin{proof}
  Suppose that $\xx \in \Omega$.
  By the definitions of $\GG$ and $\Glap$, we have
  \begin{equation*}
    \bu(\xx) = \int_\Omega -(\Delta + k^2) \GG(\xx,\yy) \bu(\yy)
    + \nabla \otimes \nabla \Glap(\xx,\yy) \bu(\yy) \, dV(\yy) \; .
  \end{equation*}
  Applying Green's identity and the divergence theorem, we
  obtain
  \begin{equation}
    \bu(\xx) = \int_\Gamma \GG(\xx,\yy) \partial_\nu \bu
    - \partial_\nu \GG(\xx,\yy) \bu
    - \nabla \Glap(\xx,\yy) (\bnu \cdot \bu) \, dS
    - \int_\Omega \GG(\xx,\yy) (\Delta + k^2) u \, dV \; .
    \nonumber
  \end{equation}
  Substituting the definition of the PDE and applying the divergence
  theorem again, we obtain
  \begin{equation}
    \bu(\xx) = \int_\Gamma \GG \partial_\nu \bu - p \GG \bnu - \partial_\nu \GG \bu
    - \nabla \Glap (\bnu \cdot \bu) \, dS  \; . \label{eq:rep_proof_1}
  \end{equation}
  From the divergence theorem and the divergence-free properties of
  $\uu$ and $\GG$, we then get

  \begin{equation}
    \int_\Gamma \GG \nabla (\bu \cdot \bnu)
    - (\nabla \GG \bnu)^\intercal \bu \, dS = 0 \; .  \label{eq:rep_proof_2}
  \end{equation}
  Adding \cref{eq:rep_proof_1,eq:rep_proof_2}, we get the desired
  result. The argument for the case $\xx \in E$ is similar.
\end{proof}

A consequence of \cref{thrm:rep-theorem} and the analyticity
of $\Gbh$ is
\begin{cor}
\label{cor:analytic}
  Let $(\uu,p) \in A(\Omega)$ be a solution of \cref{eq:ostokes}.
  Then each component of $\uu(\xx)$ is an analytic function
  of the coordinates $\xx$ in $\Omega$.
\end{cor}

The proof of \cref{cor:analytic} follows the same reasoning as
that for the Helmholtz case; see \cite[Theorem 3.5]{colton1983integral}.

\subsubsection{Null-space correction \label{subsubsec:nullspacecorr}}

Without modification, the standard layer potentials
can result in rank-deficient representations for
the boundary value problems. The nature of this deficiency
is treated in~\cref{sec:analysis} but for now we introduce a standard
operator used to correct this. For any integrable density
$\bmu$, let $\cW[\bmu]$ be defined by

\begin{equation} \label{eq:ones_operator}
  \cW[\bmu](\xx) = \frac{1}{|\Gamma|} \int_\Gamma \bnu(\xx)
  \left ( \bnu(\yy) \cdot \bmu(\yy) \right )
  \, dS(\yy) \; ,
\end{equation}
for any $\xx \in \Gamma$. We have

\begin{lem}
  \label{lem:propnullspacecorr}

  Let $\Omega$ be a domain with $C^2$ boundary and $\bmu$ be
  an integrable function defined on $\Gamma$. Then
  \begin{itemize}
  \item $\cW[\cW[\bmu]] = \cW[\bmu]$,
  \item $\cW^\intercal = \cW$,
  \item $\cW[\bmu - 2 \cD[\bmu]] = 0$,
  \item $\cW[\cS[\bmu]] = 0$,
  \end{itemize}
  where the transpose is induced by the bilinear
  form \cref{eq:bi_form}.
\end{lem}

\begin{proof}
  The first two results follow from the definitions of $\cW$ and
  the normal vector. The other two follow from the fact that
  $\bS[\bmu]$ and $\bD[\bmu]$ are divergence-free and
  an application of \cref{lem:jump-conds}.
\end{proof}

\begin{remark}
The operators $\bS,\bD,\cD$ and $\cD^{\intercal}$
depend on the Helmholtz parameter $k$ of the oscillatory Stokes equation.
In places where it is essential to highlight this dependence, in a slight
abuse of notation, we will use the symbols $\bS_{k}, \bD_{k},\cD_{k}$ and $\cDt_{k}$ to
denote this dependence.
Similarly, we will use $A^{\Gamma}$ instead of the operator $A$ to highlight
the dependence of the operator $A$ on the boundary of the region $\Gamma$.
\end{remark}


% !TeX document-id = {d3b263d9-4ae8-4edf-a203-72d775f66a9e}
\documentclass[]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{dsfont} % /mathds{}
\usepackage{graphicx}
\usepackage{float}
\usepackage{hyperref} % Hyperlinks
\usepackage{xcolor} % \colorbox{} & \textcolor{}

%% \usepackage{minted} % Syntax Highlighting (Requires Python)
%% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]

% See https://tex.stackexchange.com/a/78393/1661
\usepackage[framed, numbered, autolinebreaks, useliterate]{mcode} % Syntax Highlighting MATLAB

% Math Symbols
% http://www.cs.put.poznan.pl/ksiek/latexmath.html

%\newtheorem{theorem}{Theorem}[section]
%\newtheorem{corollary}{Corollary}[theorem]
%\newtheorem*{lemma*}[theorem]{Lemma}
\newtheorem*{lemma}{Lemma}
\newtheorem*{remark}{Remark}

\DeclareMathOperator{\sign}{sign}
\DeclareMathOperator{\prox}{prox}
\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator{\Tr}{Tr}
\DeclareMathOperator{\cond}{cond}
\DeclareMathOperator{\chol}{chol}

% Math Commands
\newcommand{\MyParen}[1]{\left( #1 \right)}
\newcommand{\MyBrack}[1]{\left\lbrack #1 \right\rbrack}
\newcommand{\MyBrace}[1]{\left\lbrace #1 \right\rbrace}
\newcommand{\MyNorm}[2]{{\left\| #1 \right\|}_{#2}}
\newcommand{\MyNormSqr}[2]{{\left\| #1 \right\|}_{#2}^{2}}
\newcommand{\MyAbs}[1]{\left| #1 \right|}
\newcommand{\MyNormTwo}[1]{\MyNorm{#1}{2}}
\newcommand{\MyNormTwoSqr}[1]{\MyNormSqr{#1}{2}}
\newcommand{\MyCeil}[1]{\lceil #1 \rceil}
\newcommand{\MyProd}[2]{\langle #1, #2 \rangle}
\newcommand{\MyUndBrace}[2]{\underset{#2}{\underbrace{#1}}}
% \newcommand{\RR}[1]{\mathds{R}^{#1}} % Asaf's Style
\newcommand{\RR}[1]{\mathbb{R}^{#1}}
\newcommand{\EE}[1]{\mathbb{E} \MyBrack{#1}}

% Text Commands
\newcommand{\inlinecode}[1]{\colorbox{lightgray}{\texttt{#1}}}

\begin{document}

	\section*{Question - Calculate the Optimal Weights which Minimizes Variance}

	Given $ \MyBrace{ \boldsymbol{{x}_{1}}, \boldsymbol{{x}_{2}}, \ldots, \boldsymbol{{x}_{m}} } $ where $ {x}_{i} \in \RR{n} $ find $ w \in \RR{m} $ which minimized the Variance of $ y $ given by $ y = {w}_{1} \boldsymbol{{x}_{1}} + {w}_{2} \boldsymbol{{x}_{2}} + \cdots + {w}_{m} \boldsymbol{{x}_{m}} $ where $ \forall i \: {w}_{i} \geq 0 $ and $ \sum_{i = 1}^{m} {w}_{i} = 1 $.

	\begin{remark}
		The question is given at \href{https://stackoverflow.com/questions/44984132}{Question 44984132 on StackOverflow}.
	\end{remark}

	\section*{Answer - Calculate the Optimal Weights which Minimizes Variance}

	The above can be written as following:

	\begin{alignat*}{3}
	\arg \min_{w} 		& \quad && \frac{1}{2} \MyNormTwoSqr{ X w - \frac{1}{m} {e}^{T} X w e } \\
	\text{subject to} 	& \quad && w \succeq 0 \\
						& \quad && {e}^{T} w = 1 \\
	\end{alignat*}

	Where $ X $ is composed by $ \MyBrace{ \boldsymbol{{x}_{1}}, \boldsymbol{{x}_{2}}, \ldots, \boldsymbol{{x}_{m}} } $ as its columns, $ w = \MyBrack{{w}_{1}, {w}_{2}, \ldots, {w}_{m}}^{T} $ and $ e = \MyBrack{1, 1, \ldots, 1}^{T}, \, e \in \RR{m} $.

	The above is a Convex Problem where the solution is limited to the Unit Simplex.

	A method to solve is using \href{https://en.wikipedia.org/wiki/Subgradient_method}{Projected Sub Gradient Method}. The idea is to apply a Sub Gradient step and project the result onto the Unit Simplex.

	In order to so one have to calculate the following:
	\begin{itemize}
		\item The Sub Gradient (Gradient in the case above as the function is smooth) of the Objective Function.
		\item The Projection onto the unit simplex.
	\end{itemize}

	\subsubsection*{The Gradient of the Objective Function}
	% See https://en.wikipedia.org/wiki/Matrix_calculus for "Denominator Layout"
	\begin{align*}
	\frac{\partial }{\partial w} f \MyParen{w} & = \frac{\partial }{\partial w} \MyParen{\frac{1}{2} \MyNormTwoSqr{ X w - \frac{1}{m} {e}^{T} X w e }} && \text{} \\
	& = \frac{\partial }{\partial w} \MyParen{ X w - \frac{1}{m} {e}^{T} X w e } \frac{\partial }{\partial \MyParen{X w - \frac{1}{m} {e}^{T} X w e}} f \MyParen{w} && \text{} \\
	& = \MyParen{ {X}^{T} - \frac{1}{m} \frac{\partial }{\partial w} \MyParen{{e}^{T} X w e} } \MyParen{X w - \frac{1}{m} {e}^{T} X w e} && \text{} \\
	& = \MyParen{ {X}^{T} - \frac{1}{m} {X}^{T} e {e}^{T} } \MyParen{X w - \frac{1}{m} {e}^{T} X w e} && \text{} \\
	\end{align*}

	\subsubsection*{The Projection onto the Unit Simplex}
	There are 2 options to apply this:
	\begin{itemize}
		\item Solving the Projection Minimization as done in \href{https://math.stackexchange.com/a/2338491}{MathExchange Answer 2338491}.
		\item Iteratively projecting onto the Non Negative Half Space and the set of vectors which their sum is $ 1 $.
	\end{itemize}

	\begin{remark}
		The answer (With MATLAB code) is given at \href{https://stackoverflow.com/a/44986301/195787}{Answer 195787 on StackOverflow}.
	\end{remark}

\end{document}


\section{Top Level}

In top level, the system maintains the interaction with user and
adds the user input, namely, \textit{Axiom}, \textit{Definition}, \textit{Fixpoint}, and \textit{Inductive Definition},
into the context.\par
It is also in this part, the inductive rule forms.

\subsection{Acceptance of Command}
\subsubsection{Axiom}
After parsing and checking, the command will be like
$$
\tt Ax\ \ \it name\ term,
$$
where {\it term} denotes the type of this axiom.
Since it is an axiom, we do not have to (sometimes can not) build the corresponding term.\par
Just build the corresponding term as {\it Nothing} then put it into the context.

\subsubsection{Definition}
After parsing and checking, the command will be like
$$
\tt Def\ \ {\it name\ term2\ term1},
$$
where {\it term2} is the type of {\it term1}.\par
Simply bind the name, term, type together and put it into the context.

\subsubsection{Fixpoint}
After parsing and checking, the command will be like
$$
\tt Fix\ {\it name }\ \left(\lambda {\it f}:{\it term1},\ {\it term2}\right),
$$
where {\it term1} is the type of {\it term2} and it is a recursive function of {\it f}.\par
Since this recursive function has passed all the type check and safety check, we can safely use it without worrying termination
problem.
On the other hand, whether it is a {\it Fixpoint} definition will not influence any reduction, because the reduction
always finds the term in context according to its index.\par
So simply remove the {\it Fixpoint} mark and put it into the context.

\subsubsection{Inductive Definition}
After parsing and checking, the command will be like
$$
\tt Ind\ \ {\it name\ p\ term2\ term1\ constructors},
$$
where {\it p} is the number of parameters of the inductive type, {\it term1} is the type of the inductive definition,
and {\it term2} is the corresponding term.\par
Apart from the ordinary operation, we also need to add inductive rule, which is actually a type theory view of mathematical
induction. Since the proof of a claim becomes a term of certain type, the induction rule is a term offering inductive scheme.\par
For example,
\begin{center}
\begin{minted}{coq}
Inductive nat : Type :=
| O : nat
| S : nat -> nat.

(* build inductive rule*)

fun (P:nat -> Type)(f:P O)(f0:forall (n:nat), P n -> P (S n))
    fix F (n:nat) : P n :=
        match n as n0 in nat return (P n0) with
        | O => f
        | S n0 => f0 n0 (F n0)
        end
:
forall (P:nat -> Type), P O ->
    (forall (n:nat), P n -> P (S n)) ->
        forall (n:nat), P n
\end{minted}
\end{center}
The intuition here is that for a proposition \mintinline{coq}|P|,
\begin{itemize}
\item it is true on \mintinline{coq}|O|;
\item if it is true on \mintinline{coq}|n|, then it is true on \mintinline{coq}|S n|.
\end{itemize}
Then it is true on all term of \mintinline{coq}|nat| type, which is reasonable according
mathematical induction.\par
Basically, to build such term, we should build weakened assumptions for all constructors first,
like the \mintinline{coq}|f|,
\mintinline{coq}|f0| above. After that, the final proposition which applies to all the terms of such inductive type
shall come out, like the \mintinline{coq}|F| above.\par
Every occurrence of the inductive type on the constructors demands a verification of the proposition,
which explains why for constructor \mintinline{coq}|S : nat -> nat|, which depends on a \mintinline{coq}|nat| term,
the weakened assumption is \mintinline{coq}|f0 : forall (n:nat), P n -> P (S n)|.\par
The reason why the inductive rule of \mintinline{coq}|nat| requires {\it Fixpoint} is that some constructors of it
rely on the term of type \mintinline{coq}|nat|. Here is a case which do not need recursive function.
\begin{center}
\begin{minted}{coq}
Inductive eq (T : Type) (x : T) : T -> Type :=
| eq_refl : eq T x x.

(* build inductive rule*)

fun (T:Type) (x:T) (P:forall (t:T) (_:eq T x t), Type)
    (f:P x (eq_refl T x)) (t:T) (e:eq T x t) =>
        match e as e0 in eq _ _ a0 return (P a0 e0) with
        | eq_refl _ _ => f
        end
:
forall (T:Type) (x:T) (P:forall (t:T) (_:eq T x t), Type)
    (f:P x (eq_refl T x)) (t:T) (e:eq T x t),
        P t e
\end{minted}
\end{center}
Sadly, we have to admit that because of the lack of references, time, and energy,
the induction rule in our system is not complete.
The inductive definition acceptable to our system must satisfy:\par
Assume the inductive type is $\tt A_1\to A_2\to \cdots A_n$, then $\tt A_k$ must be
any one of the following
\begin{itemize}
\item A ordinary term, like \mintinline{coq}|Type|, \mintinline{coq}|T|.
\item An application, like \mintinline{coq}|P n|.
\item An inductive type, like \mintinline{coq}|nat|, \mintinline{coq}|eq T x y|.
\end{itemize}
Others like product type \mintinline{coq}|U -> V| is not supported.

\subsection{Requests to Environment}
\subsubsection{\tt Print \sl ident}
This command displays on the screen information about the declared or defined object referred by {\sl ident}.
\subsubsection{\tt Check \sl term}
This command displays the type of {\sl term}.

\subsection{Top Loop}
The main work flow of our top level loop:
\subsubsection*{Reading raw input}
The MiniProver will read the user's input until a dot (\textquotesingle .\textquotesingle), and any further input in the same line will be abandoned.
\subsubsection*{Parsing}
The raw input will be parsed without nameless representation.

Here is an example, the raw input
\begin{center}
\begin{minipage}{0.6\textwidth}
\begin{minted}{coq}
(* raw input *)
Fixpoint plus (n:nat) (m:nat) : nat :=
match n as n0 in nat return nat with
| O => m
| S n0 => S (plus n0 m)
end.
\end{minted}
\end{minipage}
\end{center}
will be parsed as the AST
\begin{center}
\begin{minipage}{0.9\textwidth}
\begin{minted}{haskell}
Fix "plus"
  ( TmFix (-1)
    ( TmLambda "plus"
      ( TmProd "n" ( TmVar "nat" )
        ( TmProd "m" ( TmVar "nat" ) ( TmVar "nat" )))
      ( TmLambda "n" ( TmVar "nat" )
        ( TmLambda "m" ( TmVar "nat" )
          ( TmMatch (-1) ( TmVar "n" ) "n0" [ "nat" ]
            ( TmVar "nat" )
            [ Equation [ "O" ] ( TmVar "m" )
            , Equation [ "S", "n0" ]
              ( TmAppl
                [ TmLambda "n" ( TmVar "nat" )
                  ( TmAppl [ TmVar "S", TmVar "n" ])
                , TmAppl [ TmVar "plus", TmVar "n0", TmVar "m" ]])])))))
\end{minted}
\end{minipage}
\end{center}
\subsubsection*{Duplicate global name checking}
After parsing, we can get the name of the input command, and the name should not be the same with
any defined or declared object in the environment.
\subsubsection*{Name checking}
Before building the nameless representation, there should be no unbounded name in the AST.
\subsubsection*{Nameless representation building}
If all names are bounded, we can build the nameless representation. The variable pointed to
a type constructor or a term constructor will be unfolded to its functional representation.

Here is an example, the nameless AST will be built for the previous AST:
\begin{center}
\begin{minipage}{0.9\textwidth}
\begin{minted}{haskell}
Fix "plus"
( TmFix (-1)
  ( TmLambda "plus"
    ( TmProd "n" ( TmIndType "nat" [])
      ( TmProd "m" ( TmIndType "nat" []) ( TmIndType "nat" [])))
    ( TmLambda "n" ( TmIndType "nat" [])
      ( TmLambda "m" ( TmIndType "nat" [])
        ( TmMatch 0 ( TmRel "n" 1 ) "n0" [ "nat" ] ( TmIndType "nat" [])
          [ Equation [ "O" ] ( TmRel "m" 0 )
          , Equation [ "S", "n0" ]
            ( TmAppl
              [ TmLambda "n" ( TmIndType "nat" [])
                ( TmConstr "S" [ TmRel "n" 0 ])
              , TmAppl
                [ TmRel "plus" 3
                , TmRel "n0" 0
                , TmRel "m" 1 ]])])))))
\end{minted}
\end{minipage}
\end{center}
\subsubsection*{Positivity Checking (Inductive Definition Only)}
For an inductive definition, after building it's nameless representation, the positivity
could be checked.
\subsubsection*{Termination Checking}
All subterms with fixpoint definitions will be checked if they are terminating. After checking,
annotations for the indices of decreasing variables will be added to the AST.
\subsubsection*{Type checking}
Before actually dealing with the command, the top level will check if it's a well-typed command.
\subsubsection*{Processing the command}
The definitions and declarations will be processed as described before. And an assertion will lead to the proof editing mode.


\documentclass[10pt]{article}

\usepackage[round]{natbib}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{amsmath}
\usepackage{mathrsfs}
\usepackage{listings}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother

\title{FAS Multigrid in Chombo}

\begin{document}
\maketitle

\section{Introduction}
This is an application with illustrates the use of the AMR Full Approximation Scheme (FAS) multigrid solver for nonlinear problems within Chombo \citep{Chombo}.

Here, we first describe the nonlinear problem that the code solves (\cref{sec:problem}). In \cref{sec:options} we describe some of options available in the application, and in \cref{sec:tests} we demonstrate the accuracy of the code. Finally, in \cref{sec:algorithm}, we describe how we implement the algorithm on an AMR hierarchy.

\section{Example problem}
\label{sec:problem}
We follow \cite{Henson2002} and consider the nonlinear problem
\begin{align}
- \nabla^2 u + \gamma \, u e^u = f,
\end{align}
where $\gamma$ is a constant, $f$ is some forcing term and $u$ is the solution we wish to obtain. The domain is a 2D unit square ($0<x<1, 0<y<1$), and boundary conditions are given by $u=0$ on all sides.

The size of $\gamma$ determines the strength of the nonlinearity; when $\gamma=0$ the problem reduces to solving a Poisson equation. We choose the forcing $f$ such that the problem has an analytic solution, allowing us to determine the accuracy of our numerical scheme.

The example code contains two difference problems. The problem which will be solver is determined by the \texttt{main.problem} parameter in the inputs file, which should be set to either $0$ for the `exact' problem (\cref{sec:exact})or $1$ for the `inexact' problem' (\cref{sec:inexact}).

\subsection{Exact}
\label{sec:exact}
Taking
\begin{align}
u=(x-x^2)(y-y^2),
\end{align}
the forcing term becomes
\begin{align}
f = 2 \left[ (x-x^2) + (y-y^2) \right] + \gamma (x-x^2)(y-y^2) e^{(x-x^2)(y-y^2)}.
\end{align}
As \cite{Henson2002} note, the solution here is a second order polynomial so there is no discretization error with our second order scheme. Hence, we refer to this test case as `exact'.

\subsection{Inexact}
\label{sec:inexact}
Choosing
\begin{align}
u=(x^2-x^3) \sin (3 \pi y),
\end{align}
the forcing term becomes
\begin{align}
f = \left\{ \left[9 \pi^2 + \gamma e^{(x^2-x^3) \sin (3 \pi y)} \right] (x^2 - x^3) +6x -2 \right\} \sin (3 \pi y),
\end{align}
which will now have a notable discretization error.

\section{Options}
\label{sec:options}
The \texttt{inputs} file controls various aspects of the application.

\subsection{Grids}
When \texttt{grids}.\texttt{max\char`_level} is greater than $0$, the grid hierarchy is determined by refining all cells where
\begin{align}
\text{max}(\nabla f) \Delta x > \texttt{grids}.\texttt{refine}\char`_\texttt{threshold}.
\end{align}
Alternatively, you can specify the desired grids by setting \texttt{grids.read\char`_in\char`_grids=true} and using the format introduced at the end of the inputs file.

\section{Tests}
\label{sec:tests}
The python script \texttt{runTests.py} executes a convergence test on a fixed AMR hierarchy of grids and prints the results. A typical output for $\gamma=100$ and the `exact' test problem looks like
\begin{verbatim}
==================================
Convergence test
----------------------------------
Nx   | Max err  | L1       | L2       | Rate  || Runtime (s)  | Rate
16   | 1.67e-04 | 5.60e-05 | 7.28e-05 | 0.000 ||   0.103      | 0.000
32   | 5.11e-05 | 1.45e-05 | 1.92e-05 | 1.634 ||   0.066      | 0.159
64   | 1.40e-05 | 3.66e-06 | 4.88e-06 | 1.825 ||   0.086      | 0.330
128  | 3.66e-06 | 9.18e-07 | 1.23e-06 | 1.913 ||   0.219      | 0.633
256  | 9.34e-07 | 2.30e-07 | 3.07e-07 | 1.959 ||   0.656      | 0.748
512  | 2.36e-07 | 5.74e-08 | 7.67e-08 | 1.979 ||   2.555      | 0.974
1024 | 5.93e-08 | 1.44e-08 | 1.92e-08 | 1.990 ||  11.466      | 1.122
==================================
\end{verbatim}
The convergence of the error $\epsilon$ between successive computations, $i$, is calculated as
\begin{align}
r_N = \frac{\epsilon(N_x^i)/ \epsilon(N_x^{i-1})}{N_x^i / N_x^{i-1}}.
\end{align}
The ratio between successive runtimes $\tau$ is scaled with the number of grid cells
\begin{align}
r_t = \frac{\tau(N_x^i) / \tau(N_x^{i-1})}{N_x^i / (N_x^{i-1})^2}.
\end{align}

\section{FAS Multigrid algorithm}
\label{sec:algorithm}
The FAS approach to geometric multigrid is a well established method for dealing with nonlinear problems - see e.g. \cite{Briggs2000, Henson2002} for a detailed introduction. The application of this method to an AMR hierarchy requires some care, as noted by \cite{Martin1996, Martin1998} for linear multigrid problems. Here we describe the AMR FAS algorithm used in Chombo, as originally written by Mark Adams, following the format introduced in the Chombo Design document.

We wish to solve the equation
\begin{align}
N^{comp} \psi^{comp} = \rho^{comp},
\end{align}
on an AMR hierarchy $\{ \Omega \}_{l=0}^{l_max}$. The algorithm (\cref{alg:AMRFAS}) proceeds in two stages. First we solve down to the coarsest AMR level, then the problem on this level is solved by standard FAS multigrid \texttt{FASVCycle}, before completing the AMR portion of the algorithm. In the psuedocode descriptions, the restriction $R$ and interpolation $I$ operators are as described in \cite{Chombo}. $N^{nf}(\psi^\ell, \psi^{\ell-1})$ is a two-level discretization of the nonlinear operator, whilst $N^\ell(\psi^\ell)$ is a single-level discretization. The crucial difference is that the two-level version includes a reflux correction.

Note that the \texttt{relax} procedure in both \texttt{AMRFASVCycle} and \texttt{FASVCycle} requires a coarse-fine boundary condition. During FAS multigrid it is the actual solution, not the error, which is smoothed, so boundary conditions are not simply $\psi=0$. A box on an initially refined level $\ell+1$ with a coarse-fine interface will retain its coarse-fine interface throughout the algorithm, so a boundary condition must be specified for smoothing steps. This boundary condition comes from level $\ell$, which must be appropriately coarsened. The ability to coarsen this boundary condition places a limit on the maximum depth of \texttt{FASVCycle}.

\begin{algorithm}
\caption{AMR FAS algorithm. In \texttt{relax}, $\lambda$ is the relaxation parameter for a Gauss-Seidel scheme. Note that bottom solver in FASVCycle is simply relaxation.}\label{alg:AMRFAS}
\begin{algorithmic}[1]
\State Residual $ = \rho - N(\psi)$
\While {$||\text{Residual}|| > \epsilon ||\rho||$}
\State AMRFASVCycle($\ell^{max}$)
\State Residual $ = \rho - N(\psi)$
\EndWhile

\Procedure{AMRFASVCycle}{level $\ell$}
\If {$\ell = \ell^{max}$} $r^\ell= \rho^\ell$ \EndIf
\If {$\ell > 0$}
\State relax($\psi^\ell$, $\psi^{\ell-1}$, $r^l$)
\State $r^{\ell-1} = R(r^\ell) - N^{nf}(\psi^\ell, \psi^{\ell-1})$ on $\Omega^{\ell-1}$

\State $r^{\ell-1} = R(r^\ell - N^{nf}(\psi^\ell, \psi^{\ell-1}))$ on $\mathcal{C}(\Omega^{\ell})$

\State $r^{\ell-1} = r^{\ell-1} + N^{\ell-1}(R(\psi^{\ell}))$ on $\mathcal{C}(\Omega^{\ell})$

\State $\psi^{\ell, save} = \psi^\ell$ on $\Omega^ell$

\State AMRFASVCycle($\ell-1$)

\State $\psi^{\ell, corr} = I(\psi^{\ell-1}) - \psi^{\ell, save}$ on $\Omega^ell$

\State $\psi^{\ell} = \psi^\ell + \psi^{\ell, corr}$ on $\Omega^ell$

\State relax($\psi^\ell$, $\psi^{\ell-1}$, $r^l$)

\Else
\State FASVCycle($\psi^0, r^0$)
\EndIf
\EndProcedure

\Procedure{FASVCycle}{$\psi^\ell$, $r^l$}
\State $\psi^{\ell, save} = \psi^\ell$
\If {$\ell = \ell^\text{max depth}$}
\State relax($\psi^\ell$, $\psi^{\ell-1}$, $r^\ell$)
\Else
%\State $\psi^{\ell, save} = \psi^\ell$
\State relax($\psi^\ell$, $\psi_{valid}^{\ell-1}$, $r^\ell$)
\State $r^{\ell-1} = R(r^\ell - N^\ell(\psi^\ell) + N^{\ell-1}(R(\psi^\ell))$
\State FASVCycle($R(\psi^{\ell})$, $r^{\ell-1}$)
\State $\psi^\ell = \psi^\ell + I(e^{\ell-1})$
\State relax($\psi^\ell$, $\psi_{valid}^{\ell-1}$, $r^\ell$)
%\State $e^\ell = \psi^\ell - \psi^{\ell, save}$
\EndIf
\State $e^\ell = \psi^\ell - \psi^{\ell, save}$
\EndProcedure

\Procedure{relax}{$\psi^\ell$, $\psi^{\ell-1}$, $r^l$}
\State Fill $\psi^\ell$ BCs using quadratic interpolation with $\psi^{\ell-1}$
\State $\psi^\ell = \psi^\ell + \lambda (N^{nf}(\psi^\ell, \psi^{\ell-1}) - r^\ell)$ on $\Omega^\ell$
\EndProcedure

\end{algorithmic}
\end{algorithm}

\bibliographystyle{plainnat}
\bibliography{references}

\end{document}


%% ---------------------------------------------------
%% Notice that we use the "report" class instead of "article"
%% ---------------------------------------------------
\documentclass{report}

\title{Project 7}
\author{\textbf{Jinhao Wei}}
\date{\textbf{5 March 2019}}

%% ---------------------------------------------------
%% CSBDformat specifies the format of our reports
%% ---------------------------------------------------
\usepackage{634format}

%% ---------------------------------------------------
%% enumerate
%% ---------------------------------------------------
\usepackage{enumerate}

%% ---------------------------------------------------
%% listings is used for including our source code in reports
%% ---------------------------------------------------
\usepackage{listings}
\usepackage{textcomp}

%% ---------------------------------------------------
%% Packages for math environments
%% ---------------------------------------------------
\usepackage{amsmath}

%% ---------------------------------------------------
%% Packages for URLs and hotlinks in the table of contents
%% and symbolic cross references using \ref
%% ---------------------------------------------------
\usepackage{hyperref}

%% ---------------------------------------------------
%% Packages for using HOL-generated macros and displays
%% ---------------------------------------------------
\usepackage{holtex}
\usepackage{holtexbasic}
\input{commands}

\begin{document}

\input{../HOL/HOLReports/HOLexType}
\input{../HOL/HOLReports/HOLnexp}

%% --------------------------------------------------- the listings
%% parameter "language" is set to "ML"
%% ---------------------------------------------------
\lstset{language=ML,breaklines}

\maketitle{}

\begin{abstract}
  This report is basically a summary on my attempts on Project 7, which includes proof by induction, function definition and datatype definiton. This report provides my solution on \emph{exercise 11.6.1}, \emph{11.6.2} and \emph{11.6.3}. In addition, I had fine printed the corresponding datatypes and proofs and put the reports in \emph{../HOL/HOLReports/exTypeReport.pdf} and \emph{../HOL/HOLReports/nexpReport.pdf}.
\end{abstract}

\begin{acknowledgments}
  This project follows the format and structure of \emph{sampleTheory} provided by Professor Shiu-Kai Chin. To make it more accurate, this project mostly followed the format of one of my previous projects, which is project 5, and project 5 followed the sturcture of Professor Shiu-Kai Chin's \emph{sampleTheory} project.
\end{acknowledgments}

\tableofcontents{}

\chapter{Executive Summary}
\label{cha:executive-summary}

\textbf{All requirements for this project are satisfied}.  In
particular, we defined all the datatypes and proved all the theorems in this project, pretty printed the HOL theories,
and made use of the \emph{EmitTeX} structure to typeset HOL theorems
in this report.

We gave definitions for the following functions or datatypes
\begin{quote}
  \HOLexTypeDefinitions
  \HOLnexpDatatypes
  \HOLnexpDefinitions
\end{quote}

and the following theorems are proved
\begin{quote}
  \HOLexTypeTheorems
  \HOLnexpTheorems
\end{quote}

\begin{description}
\item [Reproducibility in ML and \LaTeX]\ \\
All ML and \LaTeX{} source files compile well on the environment provided by this course.
\end{description}

\chapter{Exercise 11.6.1}
\label{cha:e1161}

\section{Problem Statement}
\label{sec:e1161ps}
In this exercise, we will define a function named \emph{APP}, according to the following formula \HOLexTypeDefinitionsAPPXXdef and prove the theorem
\HOLexTypeTheoremsLENGTHXXAPP

Before we go through the following sections, we will need to print
\begin{lstlisting}[frame=trBL]
open HolKernel Parse boolLib bossLib;
open arithmeticTheory listTheory;
\end{lstlisting}
in HOL session.
\section{Definition of \emph{APP}}
\label{sec:e1161definition}
\subsection{Code for defining \emph{APP}}
\label{subsec:e1161code}
We used the following code to define \emph{APP}
\begin{lstlisting}[frame=trBL]
val APP_def =
Define
`(APP [] (l:'a list) = l) /\
(APP (h::(l1:'a list)) (l2:'a list) = h::(APP l1 l2))`;

\end{lstlisting}

\subsection{Session Transcript}
\label{subsec:e1161st}
If we send the above code to HOL, we will see the transcript as below:
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # # # Definition has been stored under "APP_def"
val APP_def =
   |- (!(l :'a list). APP ([] :'a list) l = l) /\
   !(h :'a) (l1 :'a list) (l2 :'a list). APP (h::l1) l2 = h::APP l1 l2:
   thm
\end{verbatim}
\end{scriptsize}
\end{session}

\section{Proof for \emph{LENGTH_APP}}
\label{sec:e1161proof}
\subsection{Code for Proving \emph{LENGTH_APP}}
\label{subsec:e1161codeforproof}
\begin{lstlisting}[frame = trBL]
val LENGTH_APP =
TAC_PROOF(
([], ``!(l1:'a list)(l2:'a list). LENGTH (APP l1 l2) = LENGTH l1 + LENGTH l2``),
(Induct_on `l1` THEN
ASM_REWRITE_TAC [APP_def, LENGTH, ADD_CLAUSES] THEN
ASM_REWRITE_TAC [APP_def, LENGTH, ADD_CLAUSES]
)
)
\end{lstlisting}

\subsection{Session Transcript}
The above code will give us transcript as below:
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # # # # # # # val LENGTH_APP =
   |- !(l1 :'a list) (l2 :'a list).
     LENGTH (APP l1 l2) = LENGTH l1 + LENGTH l2:
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

\chapter{Exercise 11.6.2}

\section{Problem Statement}
In this exercise, we defined a function \emph{Map}, using the following formula

\HOLexTypeDefinitionsMapXXdef

 and proved the theorem \emph{Map_APP}:

\HOLexTypeTheoremsMapXXAPP

Before we go through the following sections, we will need to print
\begin{lstlisting}[frame=trBL]
open HolKernel Parse boolLib bossLib;
open arithmeticTheory listTheory;
\end{lstlisting}
in HOL session.

\section{Definition of \emph{Map}}

\subsection{Code for Defining \emph{Map}}
We use the following code to define \emph{Map}
\begin{lstlisting}[frame=trBL]
val Map_def =
Define
`(Map f [] = []) /\
 (Map f ((h:'a)::(l:'a list)) = (f h)::(Map f l))`;
\end{lstlisting}

\subsection{Session Transcript}
\label{sec:session-transcript}
The above code will give us transcript as below:
\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # # # <<HOL message: inventing new type variable names: 'b>>
Definition has been stored under "Map_def"
val Map_def =
   |- (!(f :'a -> 'b). Map f ([] :'a list) = ([] :'b list)) /\
   !(f :'a -> 'b) (h :'a) (l :'a list). Map f (h::l) = f h::Map f l:
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

\section{Proof of  \emph{Map_APP}}

\subsection{Code for Proving \emph{Map_APP}}
We will use the following code to prove \emph{Map_APP}.
\begin{lstlisting}[frame=trBL]
val Map_APP =
TAC_PROOF(
([], ``Map f (APP (l1:'a list)(l2:'a list)) = APP (Map f l1) (Map f l2)``),
(Induct_on`l1` THEN
 ASM_REWRITE_TAC [Map_def, APP_def] THEN
 ASM_REWRITE_TAC [APP_def, Map_def]
));
\end{lstlisting}

\subsection{Session Transcript}
The above code will give us transcript as below:
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # # # # # # <<HOL message: inventing new type variable names: 'b>>
val Map_APP =
   |- Map (f :'a -> 'b) (APP (l1 :'a list) (l2 :'a list)) =
   APP (Map f l1) (Map f l2):
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

\chapter{Exercise 11.6.3}

\section{Problem Statement}
In this exercise, we will define our datatype \emph{nexp}:
\begin{quote}
\HOLnexpDatatypesnexp
\end{quote}
 and its semantic \emph{nexpVal}

\begin{quote}
\HOLnexpDefinitions
\end{quote}
 then we will prove several theorems concerning the datatype, including
\begin{quote}
\HOLnexpTheorems
\end{quote}

Before we go through the following sections, we will need to enter the code below in HOL window.
\begin{lstlisting}[frame=trBL]
open HolKernel Parse boolLib bossLib;
open TypeBase boolTheory arithmeticTheory
\end{lstlisting}

\section{Definition of \emph{nexp}}

\subsection{Code for Defining \emph{nexp}}

\begin{lstlisting}[frame=trBL]
val _ = Datatype
`nexp = Num num | Add nexp nexp | Sub nexp nexp | Mult nexp nexp`;
\end{lstlisting}

\subsection{Session Transcript}
\label{sec:session-transcript}

\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # <<HOL message: Defined type: "nexp">>
\end{verbatim}
  \end{scriptsize}
\end{session}

\section{Definition of \emph{nexpVal}}

\subsection{Code for Defining \emph{nexpVal}}
\begin{lstlisting}[frame=trBL]
val nexpVal_def =
Define
`
(nexpVal (Num num) = num)/\
(nexpVal (Add f1 f2) = (nexpVal f1) + (nexpVal  f2))/\
(nexpVal (Sub f1 f2) = (nexpVal f1) - (nexpVal  f2))/\
(nexpVal (Mult f1 f2) = (nexpVal f1) * (nexpVal  f2))
`
\end{lstlisting}

\subsection{Session Transcript}
\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # # # # # # # Definition has been stored under "nexpVal_def"
val nexpVal_def =
   |- (!(num :num). nexpVal (Num num) = num) /\
   (!(f1 :nexp) (f2 :nexp).
      nexpVal (Add f1 f2) = nexpVal f1 + nexpVal f2) /\
   (!(f1 :nexp) (f2 :nexp).
      nexpVal (Sub f1 f2) = nexpVal f1 - nexpVal f2) /\
   !(f1 :nexp) (f2 :nexp).
     nexpVal (Mult f1 f2) = nexpVal f1 * nexpVal f2:
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

\section{Proof of \emph{Add_0}}

\subsection{Code for Proving \emph{Add_0}}
\begin{lstlisting}[frame=trBL]
val Add_0 =
TAC_PROOF(
([], ``!f.nexpVal (Add (Num 0) f) = nexpVal f``),
Induct_on `f` THEN
ASM_REWRITE_TAC [ADD] THEN
REWRITE_TAC [nexpVal_def] THEN
REWRITE_TAC [ADD] THEN
REPEAT (PROVE_TAC [ADD, SUB, MULT, nexpVal_def])
);
\end{lstlisting}

\subsection{Session Transcript}
\label{sec:session-transcript-1}

\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # # # # # # # # val Add_0 =
   |- !(f :nexp). nexpVal (Add (Num (0 :num)) f) = nexpVal f:
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

\section{Proof of \emph{Add_SYM}}

\subsection{Code for Proving \emph{Add_SYM}}
\begin{lstlisting}[frame=trBL]
val Add_SYM =
TAC_PROOF(
([], ``!f1 f2. nexpVal (Add f1 f2) = nexpVal (Add f2 f1)``),
REWRITE_TAC [ADD, nexpVal_def] THEN
REWRITE_TAC [Once ADD_COMM]
);
\end{lstlisting}

\subsection{Session Transcript}
\label{sec:session-transcript-1}

\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # # # # # val Add_SYM =
   |- !(f1 :nexp) (f2 :nexp). nexpVal (Add f1 f2) = nexpVal (Add f2 f1):
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

\section{Proof of \emph{Sub_0}}

\subsection{Code for Proving \emph{Sub_0}}
\begin{lstlisting}[frame=trBL]
val Sub_0 =
TAC_PROOF(
([], ``!f. (nexpVal (Sub (Num 0) f ) = 0)/\(nexpVal (Sub f (Num 0)) = nexpVal f)``),
REWRITE_TAC [SUB, nexpVal_def] THEN
REWRITE_TAC [SUB_0]
);
\end{lstlisting}

\subsection{Session Transcript}
\label{sec:session-transcript-1}

\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # # # # # val Sub_0 =
   |- !(f :nexp).
     (nexpVal (Sub (Num (0 :num)) f) = (0 :num)) /\
     (nexpVal (Sub f (Num (0 :num))) = nexpVal f):
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

\section{Proof of \emph{Mult_ASSOC}}

\subsection{Code for Proving \emph{Mult_ASSOC}}
\begin{lstlisting}[frame=trBL]
val Mult_ASSOC =
TAC_PROOF(
([], ``!f1 f2 f3. nexpVal (Mult f1 (Mult f2 f3)) = nexpVal (Mult (Mult f1 f2) f3)``),
REWRITE_TAC [nexpVal_def, MULT, MULT_ASSOC]
);
\end{lstlisting}

\subsection{Session Transcript}
\label{sec:session-transcript-1}

\setcounter{sessioncount}{0}
\begin{session}
  \begin{scriptsize}
\begin{verbatim}

> # # # # val Mult_ASSOC =
   |- !(f1 :nexp) (f2 :nexp) (f3 :nexp).
     nexpVal (Mult f1 (Mult f2 f3)) = nexpVal (Mult (Mult f1 f2) f3):
   thm
\end{verbatim}
  \end{scriptsize}
\end{session}

%% ------------------------------------------
%% Change to letters for appendix
%% ------------------------------------------

%% ------------------------------------------
%% this restarts the section numbering
%% ------------------------------------------
\appendix{}

%% ------------------------------------------
% label using capital letters
%% ------------------------------------------
\renewcommand{\thechapter}{\Alph{chapter}}

\chapter{Source Code for exTypeScript.sml}
\label{cha:source-code-sample}

The following code is from \emph{exTypeScript.sml}, which is located
in directory "../HOL/"
\lstinputlisting{../HOL/exTypeScript.sml}

\chapter{Source Code for nexpScript.sml}
\label{cha:source-code-sample}

The following code is from \emph{nexpScript.sml}, which is located
in directory "../HOL/"
\lstinputlisting{../HOL/nexpScript.sml}

\end{document}


\subsection{2n Points}

	\prob{https://artofproblemsolving.com/community/c5h1434584p8117190}{USAMO 2017 P4}{M}{Let $ P_1 $ , $ P_2 $ , $ \dots $ , $ P_{2n} $ be $ 2n $ distinct points on the unit circle $ x^2+y^2=1 $ , other than $ (1,0) $. Each point is colored either red or blue, with exactly $ n $ red points and $ n $ blue points. Let $ R_1 $ , $ R_2 $ , $ \dots $ , $ R_n $ be any ordering of the red points. Let $ B_1 $ be the nearest blue point to $ R_1 $ traveling counterclockwise around the circle starting from $ R_1 $. Then let $ B_2 $ be the nearest of the remaining blue points to $ R_2 $ travelling counterclockwise around the circle from $ R_2 $, and so on, until we have labeled all of the blue points $ B_1, \dots, B_n $. Show that the number of counterclockwise arcs of the form $ R_i \to B_i $ that contain the point $ (1,0) $ is independent of the way we chose the ordering $ R_1, \dots, R_n $ of the red points.}\label{problem:induction_type1_15}

		\solu{As the statement is saying that the condition is true for every positive integer $ n $, can we try induction? Which part of the condition makes the problem challenging? Obviously the circle condition and the $ (1, 0) $ point. So Lets remove a ``problematic'' pair $ R_t, B_t $. We encounter some problems, but we can pull it out of there.}

	\prob{}{SMMC}{}{Let $ 2n $ points be uniformly distributed on a circle. Paint $ n $ of them red, and $ n $ of them blue. Let $ B $ be the list of distances between each pair of blue points. Let $ R $ be the list of distances between each pair of blue points. Prove that $ A=B $}

	\prob{www.hehe.com}{Putnam 1979}{E}{Let $ A $ be a set of $ 2n $ points in the plane, no three of which are collinear, $ n $ of them are colored red and the other blue. Prove that there are $ n $ line segments, no two with a point in common, such that the endpoints of each segment are points of $ A $ having different colors.}\label{problem:convex_hull_1}\label{Putnam_1979}

		\solu{Strong induction, and a way to divide the points into two sets with the same number of red and blue points. Travel through the points around a certain point and keep track of the number of red and blue points.}

	\prob{https://artofproblemsolving.com/community/c6h34319p213018}{USAMO 2005 P5}{E}{Let $n$ be an integer greater than 1. Suppose $2n$ points are given in the plane, no three of which are collinear. Suppose $n$ of the given $2n$ points are colored blue and the other $n$ colored red. A line in the plane is called a balancing line if it passes through one blue and one red point and, for each side of the line, the number of blue points on that side is equal to the number of red points on the same side. Prove that there exist at least two balancing lines.}\label{problem:convex_hull_3}

		\solu{Using the same idea as in \hrf{Putnam_1979}{this} problem. }


\chapter{Structured Matrix Types}

\NOTE{Structured matrix types are experimental in {\ViennaCLversion}. Interface changes as well as considerable performance improvements may be included in
future releases!}

There are a number of structured dense matrices for which some algorithms such as matrix-vector products can be computed with much lower computational effort
than for the general dense matrix case. In the following, four structured dense matrix types included in {\ViennaCL} are discussed.
Example code can be found in \lstinline|examples/tutorial/structured-matrices.cpp|.

\section{Circulant Matrix}
A circulant matrix is a matrix of the form
\begin{align*}
 \left( \begin{array}{ccccc}
         c_0 & c_{n-1} & \ldots & c_2 & c_1 \\
         c_1 & c_0 & c_{n-1} & & c_2 \\
         \vdots & c_1 & c_0 & \ddots & \vdots \\
         c_{n-2} & & \ddots & \ddots & c_{n-1} \\
         c_{n-1} & c_{n-2} & \hdots & c_1 & c_0 \\
        \end{array} \right)
\end{align*}
and available in {\ViennaCL} via
\begin{lstlisting}
 #include "viennacl/circulant_matrix.hpp"

 std::size_t s = 42;
 viennacl::circulant_matrix circ_mat(s, s);
\end{lstlisting}
The \lstinline|circulant_matrix| type can be manipulated in the same way as the dense matrix type \lstinline|matrix|. Note that writing to a single element of
the matrix is structure-preserving, e.g.~changing \lstinline|circ_mat(1,2)| will automatically update \lstinline|circ_mat(0,1)|, \lstinline|circ_mat(2,3)| and
so on.

\section{Hankel Matrix}
A Hankel matrix is a matrix of the form
\begin{align*}
 \left( \begin{array}{cccc}
         a & b & c & d \\
         b & c & d & e \\
         c & d & e & f \\
         d & e & f & g \\
        \end{array} \right)
\end{align*}
and available in {\ViennaCL} via
\begin{lstlisting}
 #include "viennacl/hankel_matrix.hpp"

 std::size_t s = 42;
 viennacl::hankel_matrix hank_mat(s, s);
\end{lstlisting}
The \lstinline|hankel_matrix| type can be manipulated in the same way as the dense matrix type \lstinline|matrix|. Note that writing to a single element of
the matrix is structure-preserving, e.g.~changing \lstinline|hank_mat(1,2)| in the example above will also update \lstinline|hank_mat(0,3)|,
\lstinline|hank_mat(2,1)| and
\lstinline|hank_mat(3,0)|.

\section{Toeplitz Matrix}
A Toeplitz matrix is a matrix of the form
\begin{align*}
 \left( \begin{array}{cccc}
         a & b & c & d \\
         e & a & b & c \\
         f & e & a & b \\
         g & f & e & a \\
        \end{array} \right)
\end{align*}
and available in {\ViennaCL} via
\begin{lstlisting}
 #include "viennacl/toeplitz_matrix.hpp"

 std::size_t s = 42;
 viennacl::toeplitz_matrix toep_mat(s, s);
\end{lstlisting}
The \lstinline|toeplitz_matrix| type can be manipulated in the same way as the dense matrix type \lstinline|matrix|. Note that writing to a single element of
the matrix is structure-preserving, e.g.~changing \lstinline|toep_mat(1,2)| in the example above will also update \lstinline|toep_mat(0,1)| and
\lstinline|toep_mat(2,3)|.

\section{Vandermonde Matrix}
A Vandermonde matrix is a matrix of the form
\begin{align*}
 \left( \begin{array}{ccccc}
         1 & \alpha_1 & \alpha_1^2 & \ldots & \alpha_1^{n-1} \\
         1 & \alpha_2 & \alpha_2^2 & \ldots & \alpha_2^{n-1} \\
         1 & \vdots & \vdots & \vdots \\
         1 & \alpha_m & \alpha_m^2 & \ldots & \alpha_m^{n-1} \\
        \end{array} \right)
\end{align*}
and available in {\ViennaCL} via
\begin{lstlisting}
 #include "viennacl/vandermonde_matrix.hpp"

 std::size_t s = 42;
 viennacl::vandermonde_matrix vand_mat(s, s);
\end{lstlisting}
The \lstinline|vandermonde_matrix| type can be manipulated in the same way as the dense matrix type \lstinline|matrix|, but restrictions apply. For
example, the addition or subtraction of two Vandermonde matrices does not yield another Vandermonde matrix. Note that writing to a single element of
the matrix is structure-preserving, e.g.~changing \lstinline|vand_mat(1,2)| in the example above will automatically update \lstinline|vand_mat(1,3)|,
\lstinline|vand_mat(1,4)|, etc.


\documentclass[10pt]{article}

% Manage page layout
\usepackage[margin=2.5cm, includefoot, footskip=30pt]{geometry}
\pagestyle{plain}
\setlength{\parindent}{0em}
\setlength{\parskip}{1em}
\renewcommand{\baselinestretch}{1}

\usepackage{blkarray}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{enumerate}
\usepackage{tikz}
\usetikzlibrary{calc}
% Node styles
\tikzset{
    % Two node styles for game trees: solid and hollow
    solid node/.style={circle,draw,inner sep=1.5,fill=black}, hollow node/.style={circle,draw,inner sep=1.5}
    }

\title{\textbf{Week 8.} Games with incomplete information}
\date{}

\begin{document}
\maketitle

\subsection*{Bonus 1: Volunteer's dilemma with correlated costs}

Consider the volunteer's dilemma with payoffs

\begin{equation*}
    \begin{blockarray}{ccc}
        & C & D \\
        \begin{block}{c(cc)}
            C & (1 - c_1, 1 - c_2) & (1 - c_1, 1) \\
            D & (1, 1 - c_2) & (0, 0) \\
        \end{block}
    \end{blockarray}
\end{equation*}

Suppose \(c_1\) is randomly drawn from \([0, 2]\) uniformly. For \(c_2\) assume
that it is (anti-) correlated, such that \(c_1 + c_2 = 2\) is always satisfied
(whenever player 1 has a comparably high cost they know player 2 has a
comparable low cost). \textbf{Show that the following strategies are a Bayesian Nash
equilibrium.}

\begin{equation*}
    s^{(1)}(c_1) =
    \begin{cases}
        C,& \text{if } c_1 \leq 1\\
        D,              & \text{if } c_1 > 1
    \end{cases} \qquad
    s^{(2)}(c_2) =
    \begin{cases}
        C,& \text{if } c_2 < 1\\
        D,              & \text{if } c_2 \geq 1
    \end{cases}
\end{equation*}

\underline{\textbf{Bonus question:}} Are there any other Bayesian Nash equilibria?

[Hint: To show that (\(s^{(1)}\), \(s^{(2)}\)) is a Bayesian Nash equilibrium,
consider player 1. Show that for the three cases $c_1 < 1$, $c_1 > 1$ and $c_1 =
1$, the player has no reason to deviate from \(s^{(1)}\). Do the same for player
2.]

\subsection*{Bonus 2: Second-price sealed bid auction}

An item is sold to the highest bid among \(n\) players. The players' values for
the item, \(\mathbf{v}^{(i)}\), are uniformly and independently drawn from \([0, 1]\).
The players' strategy is to choose a bid, depending on how much the item would be
worth to them, \(s^{(i)}: \mathbf{v}^{(i)} \rightarrow b^{(i)}\).

The player with the highest bid wins the auction. However, they only have to
pay the second-highest bid.

\begin{equation*}
\text{Payoff: }    \pi^{(i)} (b^{(i)}, b^{(-i)}) =
    \begin{cases}
        \mathbf{v}^{(i)} - \max\limits_{j \neq i}(b^{(j)}), & \text{if } b^{(i)} > b^{(j)} \ \forall \ j \neq i \\
        (\mathbf{v}^{(i)} - \max\limits_{j \neq i}(b^{(j)})) / K, & \text{if } b^{(i)} = \max\limits_{j \neq i} (b^{(j)}) \ \& \ K \text{number of highest biddings}\\
        0,              & \text{otherwise}.
    \end{cases}
\end{equation*}

\textbf{Show that the strategy \(b^{(i)} = v^{(i)}\) weakly dominates any other
strategy.}

[Hint: Consider 3 cases. The first case is that \(\textbf{v}^{(i)} > \max\limits_{j \neq i} (b^{(j)})\).
Show that bidding \(b^{(i)} \neq \textbf{v}^{(i)}\) never yields a higher payoff than bidding
\(b^{(i)} = \textbf{v}^{(i)}\), but sometimes the payoff is worse. Similar for the cases
\(\textbf{v}^{(i)} = \max\limits_{j \neq i} (b^{(j)})\) and \(\textbf{v}^{(i)} < \max\limits_{j \neq i} (b^{(j)})\).]

\subsection*{Exercise 1: Revenue equivalence theorem}

Consider an auction between two players with valuations for an item again uniformly
drawn from \([0, 1]\). We already know the equilibria of the

\begin{itemize}
    \item first-price sealed bid auction: \(b ^ {(i)} =  \frac{1}{2} \textbf{v}^{(i)}\) (Example 4.5), and the
    \item second-price sealed bid auction: \(b ^ {(i)} =  \textbf{v}^{(i)}\) (Bonus 2).
\end{itemize}

\textbf{Show that the expected revenue for the seller (the price the winning bidder
has to pay) is the same for both auctions.}

[Hint: Suppose without loss of generality that the player with the highest valuation assigns a value
\(\textbf{v}\) to the item. Show that the expected revenue of the seller is
\(\frac{\textbf{v}}{2}\), in both cases.]

\subsection*{Exercise 2: Pooling equilibrium of the IMPRS game}

Revisit the IMPRS game from Example 4.8.

\begin{figure*}[!htbp]
    \begin{center}
    \begin{tikzpicture}[scale=1.5,font=\footnotesize] % Specify spacing for each level of the tree
        \tikzstyle{level 1}=[level distance=15mm,sibling distance=45mm] \tikzstyle{level 2}=[level distance=15mm,sibling distance=15mm]
        \tikzstyle{level 3}=[level distance=16mm]
        % The Tree
        \node(0)[solid node,label=above:{Nature}]{}
        child{node(1)[solid node]{}
        child{node[hollow node,label=below:{$(0, 0)$}]{} edge from parent node[above, rotate=65]{Not Apply}}
        child{node[solid node]{} child{node(3)[hollow node, label=below:{$(-1, 0)$}]{} edge from parent node[above, rotate=65, xshift=-0.2cm]{Don't accept}}
        child{node[hollow node, label=below:{$(-1, -3)$}]{} edge from parent node[above, rotate=-60]{Accept}}
        edge from parent node[above, rotate=-60]{Apply}}
        edge from parent node[left,xshift=-3]{bad $(\frac{1}{2})$}
            }
        child{node(2)[solid node]{}
        child{node[solid node]{}  child{node(4)[hollow node, label=below:{$(-1, 0)$}]{} edge from parent node[above, rotate=65, xshift=-0.2cm]{Don't accept}}
        child{node[hollow node, label=below:{$(3, 2)$}]{} edge from parent node[above, rotate=-60]{Accept}}
        edge from parent node[above, rotate=65]{Apply}}
        child{node[hollow node,label=below:{$(0, 0)$}]{} edge
        from parent node[above, rotate=-60]{Not apply}} edge from parent node[right,xshift=3]{good$(\frac{1}{2})$}
        };
        % information set
    \node at ($(1)!.5!(2)$) {Student};
    \node at (0, -3) {MPI};
    \draw[dashed,rounded corners=10]($(2) + (-.4,-1.33)$)rectangle($(3) +(.4, 1.4)$); % specify mover at 2nd information set
    \end{tikzpicture}
\end{center}
\end{figure*}

\textbf{Show that if the players use the following strategies, players have no incentive to
deviate.}

\begin{align*}
    s^{(1)}(\theta) & = \text{Not apply } \ \forall \ \theta \in \{\text{good, bad}\}, \\
    s^{(2)} & = \text{Don't accept}.
\end{align*}

[Hint: For this, note that in this equilibrium, no student ever applies. So if MPI
observes an application, it does not know from the student's strategies which
of the student applied. Assume that MPI thinks
\(\mathbb{P}(\text{good} \mid \text{Apply}) = \frac{1}{2}\).]
\end{document}


\section{Equality of stacks}
\Label{sec:stack-equality}

Defining equality of instances of non-trivial data types, in
particular in object-oriented languages, is not an easy task.
%
The book \emph{Programming in Scala}\cite[Chapter~28]{OderskyEtAl2008}
devotes to this topic a whole chapter of more than twenty pages.
%
In the following two sections we give a few hints how \acsl
and \framac can help to
correctly define equality for a simple data type.

We consider two stacks as equal if they have the same size and if they contain the same objects.
%
To be more precise, let~\inl{s} and~\inl{t} two pointers of type \stacktype,
then we define the predicate \StackEqual as in the following listing.

\input{Listings/Stack.acsl.tex}

Our use of labels in this listing makes
the specification somewhat hard to read (in particular in the last line
where we reuse the predicate \logicref{Equal}.
%
However, this definition of \StackEqual will allow us later to compare
the same stack object at different points of a program.
%
The logical expression \inl{StackEqual\{A,B\}(s,t)}
reads informally as:
{The stack object \inl{*s} at program point \inl{A}
equals the stack object \inl{*t} at program point \inl{B}}.

The reader might wonder why we exclude the capacity of a stack
into the definition of stack equality.
This approach can be motivated with the behavior of the method
\inl{capacity} of the class \inl{std::vector<T>}.
There, equal instances of type \inl{std::vector<T>} may very well
have different capacities.\footnote{
See \url{http://www.cplusplus.com/reference/vector/vector/capacity}
}

If equal stacks can have different capacities then, according to our
definition of the predicate \logicref{StackFull},
we can have to equal stacks where one is full and the other one is not.

A finer, but very important point in our specification of equality
of stacks is that the elements of the arrays \inl{s->obj} and \inl{t->obj}
are compared only up to \inl{s->size} and \emph{not} up  to
\inl{s->capacity}.
Thus the two stacks \inl{s} and \inl{t} in Figure~\ref{fig:equal-stacks}
are considered
equal although there is are obvious differences in their internal arrays.

\begin{figure}[hbt]
\centering
\includegraphics[width=0.75\linewidth]{Figures/stack12.pdf}
\caption{\Label{fig:equal-stacks} Example of two equal stacks}
\end{figure}

\FloatBarrier

If we define an equality relation $(=)$ of objects for a data
type such as \stacktype,
we have to make sure that the following rules hold.

\begin{subequations}
\Label{eq:equivalence-relation}
\begin{align}
   \text{reflexivity}\qquad && \forall s \in S&: s = s,\\
   \text{symmetry}\qquad &&    \forall s,t \in S&: s = t \implies t = s,\\
   \text{transitivity}\qquad &&    \forall s,t,u \in S&: s = t \land t = u \implies s = u.
\end{align}
\end{subequations}

Any relation that satisfies the conditions~\eqref{eq:equivalence-relation}
is referred to as an \emph{equivalence relation}.
%
The mathematical set of all instances that are considered equal to
some given instance \inl{s} is called the equivalence class of \inl{s}
with respect to that relation.

Our formalization of \logicref{StackEquality} shows
these three rules for the relation \StackEqual;
it can be automatically verified that they are a consequence of the
definition of \StackEqual.

The two stacks in Figure~\ref{fig:equal-stacks} show that
an equivalence class of \StackEqual
can contain more than one element.\footnote{
    This is a common situation in mathematics. For example,
    the equivalence class of
    the rational number $\frac{1}{2}$ contains infinitely many elements,
    viz.\ $\frac{1}{2},
    \frac{2}{4}, \frac{7}{14}, \ldots$.
}
The stacks \inl{s} and \inl{t} in Figure~\ref{fig:equal-stacks}
are also referred to as two \emph{representatives} of the
same equivalence class.
In such a situation, the question arises whether a function
that is defined on
a set with an equivalence relation can be defined in such a
way that its definition
is \emph{independent of the chosen representatives}.\footnote{
    This is why mathematicians know that
    $\frac{1}{2} + \frac{3}{5}$
    equals $\frac{7}{14} + \frac{3}{5}$.
}
We ask, in other words, whether the function is \emph{well-defined}
on the set of all equivalence classes of the relation \StackEqual.\footnote{
  See \url{http://en.wikipedia.org/wiki/Well-definition}.
}
The question of well-definition
will play an important role when verifying the functions of
the \stacktype (see \S\ref{sec:stack-functions}).


\subsection{Density Estimation}
\label{sec:density}

We use a histogram to estimate the merger rate as a function of $\mathcal{M}_c$, using Knuth's rule to determine the bin size (See Figure \ref{fig:chirp}). Since we are interested in the intrinsic rate, not just that of detected events, we weigh each point by the inverse of the spacetime volume in which we are sensitive to it, $w = 1 / VT$. Binaries with a higher chirp mass are easier to detect, so we do not want to count them as heavily. For a given chirp mass, we are sensitive out to a distance
%
\begin{equation}
  D(\mathcal{M}_c) =
  \SI{200}{\mega\parsec} \qty( \mathcal{M}_c / \SI{1.2}{\Msun} )^{5/6}
\end{equation}
%
which corresponds to a volume
%
\begin{equation}
  V(\mathcal{M}_c) = \frac{4}{3} \pi D^3(\mathcal{M}_c).
\end{equation}
%
Multiplying this by the time spent observing, $T = \SI{0.6}{yr}$, gives us the spacetime volume $V(\mathcal{M}_c) T$.

To obtain uncertainties in our histogram, we take the square root of the sum-of-squares of the weights within that bin, i.e.
%
\begin{equation}
  \sigma_k = \sqrt{\sum_i w_i^2},
\end{equation}
%
which was taken from \textcite{weighted-hist}. This reduces to $\sqrt{N}$ in the case of an unweighted histogram, as $w_i = 1$, so $\sum_i w_i^2 = N$.

We also over-plot a pure power law. To do this, we employ Bayesian linear regression, fitting a straight line to $\log r$ versus $\log \mathcal{M}_c$, and transforming back to linear space. This is also shown in Figure \ref{fig:chirp}.

\begin{figure*}[ht]
  \centering
  \begin{subfigure}[c]{\textwidth}
    \centering
    \includegraphics[height=0.4\textheight]{img/chirp-mass-distribution}
    \caption{}
    \label{fig:chirp-linear}
  \end{subfigure}

  \begin{subfigure}[c]{\textwidth}
    \centering
    \includegraphics[height=0.4\textheight]{img/chirp-mass-log-distribution}
    \caption{}
    \label{fig:chirp-log}
  \end{subfigure}

  \caption{Estimated rate of compact binary mergers, based on 5000 synthetic observations. Rate is shown in (\subref{fig:chirp-linear}) linear and (\subref{fig:chirp-log}) log scale. Blue line is weighted histogram fit. Red curve is power law fit. Shaded regions are 1-$\sigma$ error bars. Vertical dashed line is boundary between events with counterparts and without.}
  \label{fig:chirp}
\end{figure*}