\section{The {\secit Body} of The Paper}
Typically, the body of a paper is organized
into a hierarchical structure, with numbered or unnumbered
headings for sections, subsections, sub-subsections, and even
smaller sections.  The command \texttt{{\char'134}section} that
precedes this paragraph is part of such a
hierarchy.\footnote{This is the second footnote.  It
starts a series of three footnotes that add nothing
informational, but just give an idea of how footnotes work
and look. It is a wordy one, just so you see
how a longish one plays out.} \LaTeX\ handles the numbering
and placement of these headings for you, when you use
the appropriate heading commands around the titles
of the headings.  If you want a sub-subsection or
smaller part to be unnumbered in your output, simply append an
asterisk to the command name.  Examples of both
numbered and unnumbered headings will appear throughout the
balance of this sample document.

Because the entire article is contained in
the \textbf{document} environment, you can indicate the
start of a new paragraph with a blank line in your
input file; that is why this sentence forms a separate paragraph.

\subsection{Type Changes and {\subsecit Special} Characters}
We have already seen several typeface changes in this sample.  You
can indicate italicized words or phrases in your text with
the command \texttt{{\char'134}textit}; emboldening with the
command \texttt{{\char'134}textbf}
and typewriter-style (for instance, for computer code) with
\texttt{{\char'134}texttt}.  But remember, you do not
have to indicate typestyle changes when such changes are
part of the \textit{structural} elements of your
article; for instance, the heading of this subsection will
be in a sans serif\footnote{A third footnote, here.
Let's make this a rather short one to
see how it looks.} typeface, but that is handled by the
document class file. Take care with the use
of\footnote{A fourth, and last, footnote.}
the curly braces in typeface changes; they mark
the beginning and end of
the text that is to be in the different typeface.

You can use whatever symbols, accented characters, or
non-English characters you need anywhere in your document;
you can find a complete list of what is
available in the \textit{\LaTeX\
User's Guide}\cite{Lamport:LaTeX}.

\subsection{Math Equations}
You may want to display math equations in three distinct styles:
inline, numbered or non-numbered display.  Each of
the three are discussed in the next sections.

\subsubsection{Inline (In-text) Equations}
A formula that appears in the running text is called an
inline or in-text formula.  It is produced by the
\textbf{math} environment, which can be
invoked with the usual \texttt{{\char'134}begin. . .{\char'134}end}
construction or with the short form \texttt{\$. . .\$}. You
can use any of the symbols and structures,
from $\alpha$ to $\omega$, available in
\LaTeX\cite{Lamport:LaTeX}; this section will simply show a
few examples of in-text equations in context. Notice how
this equation: \begin{math}\lim_{n\rightarrow \infty}x=0\end{math},
set here in in-line math style, looks slightly different when
set in display style.  (See next section).

\subsubsection{Display Equations}
A numbered display equation -- one set off by vertical space
from the text and centered horizontally -- is produced
by the \textbf{equation} environment. An unnumbered display
equation is produced by the \textbf{displaymath} environment.

Again, in either environment, you can use any of the symbols
and structures available in \LaTeX; this section will just
give a couple of examples of display equations in context.
First, consider the equation, shown as an inline equation above:
\begin{equation}\lim_{n\rightarrow \infty}x=0\end{equation}
Notice how it is formatted somewhat differently in
the \textbf{displaymath}
environment.  Now, we'll enter an unnumbered equation:
\begin{displaymath}\sum_{i=0}^{\infty} x + 1\end{displaymath}
and follow it with another numbered equation:
\begin{equation}\sum_{i=0}^{\infty}x_i=\int_{0}^{\pi+2} f\end{equation}
just to demonstrate \LaTeX's able handling of numbering.

\subsection{Citations}
Citations to articles \cite{bowman:reasoning,
clark:pct, braams:babel, herlihy:methodology},
conference proceedings \cite{clark:pct} or
books \cite{salas:calculus, Lamport:LaTeX} listed
in the Bibliography section of your
article will occur throughout the text of your article.
You should use BibTeX to automatically produce this bibliography;
you simply need to insert one of several citation commands with
a key of the item cited in the proper location in
the \texttt{.tex} file \cite{Lamport:LaTeX}.
The key is a short reference you invent to uniquely
identify each work; in this sample document, the key is
the first author's surname and a
word from the title.  This identifying key is included
with each item in the \texttt{.bib} file for your article.

The details of the construction of the \texttt{.bib} file
are beyond the scope of this sample document, but more
information can be found in the \textit{Author's Guide},
and exhaustive details in the \textit{\LaTeX\ User's
Guide}\cite{Lamport:LaTeX}.

This article shows only the plainest form
of the citation command, using \texttt{{\char'134}cite}.
This is what is stipulated in the SIGS style specifications.
No other citation format is endorsed or supported.

\subsection{Tables}
Because tables cannot be split across pages, the best
placement for them is typically the top of the page
nearest their initial cite.  To
ensure this proper ``floating'' placement of tables, use the
environment \textbf{table} to enclose the table's contents and
the table caption.  The contents of the table itself must go
in the \textbf{tabular} environment, to
be aligned properly in rows and columns, with the desired
horizontal and vertical rules.  Again, detailed instructions
on \textbf{tabular} material
is found in the \textit{\LaTeX\ User's Guide}.

Immediately following this sentence is the point at which
Table 1 is included in the input file; compare the
placement of the table here with the table in the printed
dvi output of this document.

\begin{table}
\centering
\caption{Frequency of Special Characters}
\begin{tabular}{|c|c|l|} \hline
Non-English or Math&Frequency&Comments\\ \hline
\O & 1 in 1,000& For Swedish names\\ \hline
$\pi$ & 1 in 5& Common in math\\ \hline
\$ & 4 in 5 & Used in business\\ \hline
$\Psi^2_1$ & 1 in 40,000& Unexplained usage\\
\hline\end{tabular}
\end{table}

To set a wider table, which takes up the whole width of
the page's live area, use the environment
\textbf{table*} to enclose the table's contents and
the table caption.  As with a single-column table, this wide
table will ``float" to a location deemed more desirable.
Immediately following this sentence is the point at which
Table 2 is included in the input file; again, it is
instructive to compare the placement of the
table here with the table in the printed dvi
output of this document.

\begin{table*}
\centering
\caption{Some Typical Commands}
\begin{tabular}{|c|c|l|} \hline
Command&A Number&Comments\\ \hline
\texttt{{\char'134}alignauthor} & 100& Author alignment\\ \hline
\texttt{{\char'134}numberofauthors}& 200& Author enumeration\\ \hline
\texttt{{\char'134}table}& 300 & For tables\\ \hline
\texttt{{\char'134}table*}& 400& For wider tables\\ \hline\end{tabular}
\end{table*}
% end the environment with {table*}, NOTE not {table}!

\subsection{Figures}
Like tables, figures cannot be split across pages; the
best placement for them
is typically the top or the bottom of the page nearest
their initial cite.  To ensure this proper ``floating'' placement
of figures, use the environment
\textbf{figure} to enclose the figure and its caption.

This sample document contains examples of \textbf{.eps} files to be
displayable with \LaTeX.  If you work with pdf\LaTeX, use files in the
\textbf{.pdf} format.  Note that most modern \TeX\ system will convert
\textbf{.eps} to \textbf{.pdf} for you on the fly.  More details on
each of these is found in the \textit{Author's Guide}.

\begin{figure}
\centering
\includegraphics{./fig/fly}
\caption{A sample black and white graphic.}
\end{figure}

\begin{figure}
\centering
\includegraphics[height=1in, width=1in]{./fig/fly}
\caption{A sample black and white graphic
that has been resized with the \texttt{includegraphics} command.}
\end{figure}

As was the case with tables, you may want a figure
that spans two columns.  To do this, and still to
ensure proper ``floating'' placement of tables, use the environment
\textbf{figure*} to enclose the figure and its caption.
and don't forget to end the environment with
{figure*}, not {figure}!

\begin{figure*}
\centering
\includegraphics{./fig/flies}
\caption{A sample black and white graphic
that needs to span two columns of text.}
\end{figure*}

\begin{figure}
\centering
\includegraphics[height=1in, width=1in]{./fig/rosette}
\caption{A sample black and white graphic that has
been resized with the \texttt{includegraphics} command.}
\vskip -6pt
\end{figure}

\subsection{Theorem-like Constructs}
Other common constructs that may occur in your article are
the forms for logical constructs like theorems, axioms,
corollaries and proofs.  There are
two forms, one produced by the
command \texttt{{\char'134}newtheorem} and the
other by the command \texttt{{\char'134}newdef}; perhaps
the clearest and easiest way to distinguish them is
to compare the two in the output of this sample document:

This uses the \textbf{theorem} environment, created by
the\linebreak\texttt{{\char'134}newtheorem} command:
\newtheorem{theorem}{Theorem}
\begin{theorem}
Let $f$ be continuous on $[a,b]$.  If $G$ is
an antiderivative for $f$ on $[a,b]$, then
\begin{displaymath}\int^b_af(t)dt = G(b) - G(a).\end{displaymath}
\end{theorem}

The other uses the \textbf{definition} environment, created
by the \texttt{{\char'134}newdef} command:
\newdef{definition}{Definition}
\begin{definition}
If $z$ is irrational, then by $e^z$ we mean the
unique number which has
logarithm $z$: \begin{displaymath}{\log e^z = z}\end{displaymath}
\end{definition}

Two lists of constructs that use one of these
forms is given in the
\textit{Author's  Guidelines}.

There is one other similar construct environment, which is
already set up
for you; i.e. you must \textit{not} use
a \texttt{{\char'134}newdef} command to
create it: the \textbf{proof} environment.  Here
is a example of its use:
\begin{proof}
Suppose on the contrary there exists a real number $L$ such that
\begin{displaymath}
\lim_{x\rightarrow\infty} \frac{f(x)}{g(x)} = L.
\end{displaymath}
Then
\begin{displaymath}
l=\lim_{x\rightarrow c} f(x)
= \lim_{x\rightarrow c}
\left[ g{x} \cdot \frac{f(x)}{g(x)} \right ]
= \lim_{x\rightarrow c} g(x) \cdot \lim_{x\rightarrow c}
\frac{f(x)}{g(x)} = 0\cdot L = 0,
\end{displaymath}
which contradicts our assumption that $l\neq 0$.
\end{proof}

Complete rules about using these environments and using the
two different creation commands are in the
\textit{Author's Guide}; please consult it for more
detailed instructions.  If you need to use another construct,
not listed therein, which you want to have the same
formatting as the Theorem
or the Definition\cite{salas:calculus} shown above,
use the \texttt{{\char'134}newtheorem} or the
\texttt{{\char'134}newdef} command,
respectively, to create it.

\subsection*{A {\secit Caveat} for the \TeX\ Expert}
Because you have just been given permission to
use the \texttt{{\char'134}newdef} command to create a
new form, you might think you can
use \TeX's \texttt{{\char'134}def} to create a
new command: \textit{Please refrain from doing this!}
Remember that your \LaTeX\ source code is primarily intended
to create camera-ready copy, but may be converted
to other forms -- e.g. HTML. If you inadvertently omit
some or all of the \texttt{{\char'134}def}s recompilation will
be, to say the least, problematic.


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{titlesec}
% \usepackage{amsmath,amssymb}
\usepackage{amsfonts,amsmath,amssymb}
\usepackage{cleveref}
\usepackage{bm}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{pifont}
\usepackage{color}
\usepackage[a4paper, total={6in, 8in}]{geometry}

% TODO
% check that \*z_k = (z_{k,1} ... z_{k,J}) is introduced correctly
% handle initial states in M-Step section

\crefname{section}{§}{§§}

\DeclareMathOperator{\E}{\mathbb{E}}
\DeclareMathOperator{\Pp}{\mathbb{P}}
\DeclareMathOperator*{\argmax}{arg\,max}

\newcommand{\g}{\mathbf{g}}
\newcommand{\m}{\mathbf{m}}
\newcommand{\vv}{\mathbf{v}}
\newcommand{\n}{\mathbf{n}}
% \newcommand{\*x}{\mathbf{x}}
\newcommand{\z}{\mathbf{z}}
\newcommand{\G}{\mathbf{G}}
\newcommand{\Hh}{\mathbf{H}}
% \newcommand{\*x}{\mathbf{X}}
\newcommand{\W}{\mathbf{W}}
\newcommand{\V}{\mathbf{V}}
\newcommand{\data}{\mathcal{D}}
\newcommand{\tr}{\text{T}}

\def\code#1{\texttt{#1}}
\def\*#1{\mathbf{#1}}

\title{Spectrogram Estimation for Spiking Data}
\author{Andrew Song, John Tauber}
\date{2021}

\begin{document}

\maketitle

\section{Notation}
The goal of this document will be to outline a generative model and inference
framework for estimating a spectrogram from spiking observations. This will
serve as the basis for combining with lfp data. The notation is as follows:

\begin{align*}
   &\text{window } k = 1, \ldots, K \\
   &\text{timebin } j = 1, \ldots, J \text{ time within window K} \\
   &n^c_{k,j} \in \{0,1\} := \text{spike data at time $j$ within window $k$ for neuron $c$} \\
   &\lambda^c_{k,j} := \text{CIF at time $j$ in window $k$ for neuron c} \\
   &\W \in \mathbb{C}^{J \times J} := \text{inverse-DFT matrix} \\
   &\*x^{\text{T}} := \text{transpose of vector } \*x \\
   &\*z_k\in\mathbb{C}^J := (z_{k,1} \ldots, z_{k,J} )^{\text{T}}
      \text{as in \cite{Kim2018}}\\
   &\mathcal{L}(\cdot \mid \cdot) - \text{emphasizes likelihood function}
\end{align*}

\section{Generative Model}
Assume we have a time series of spiking observations of length $T$ consisting of
data from $C$ neurons. We divide the data into $K$ windows of length $J$, such
that $T = KJ$.  We denote the full set of
spiking observations as
\begin{equation*}
   \data = \{n^c_{k,j}\}_{k=1, j=1, c=1}^{K,J,C}
\end{equation*}

We assume as our model for $\data$ a discrete-time approximation to a
point process, where the conditional intensity function (CIF) for neuron $c$ at time $t = k+j$
is a logistic function of a latent process $X_t$
\begin{equation}
   \lambda^c_{k,j} = \frac{1}{1 + \exp(-(\mu^c + \beta^c x_{k,j}))}
\end{equation}
such that, for $c = 1, \ldots, C$:
\begin{equation}
   n^c_{k,j} \sim \text{Bernoulli}(\lambda^c_{k,j})
\end{equation}

We assume that in each window $k$ the latent process $X_k,j$ is second-order
stationary. Letting $\*x_k$ denote $(x_{k,1}, \ldots, x_{k,J})^{\text{T}}$, and
following the approach of \cite{Kim2018}, we let:
\begin{equation}
   \*x_k = \W \*z_k
\end{equation}
where $\W$ is an inverse spectral transformation matrix (see Appendix B1), and
$\*z_k$ is the random walk process across the windows:
\begin{equation}
   \*z_k = \*z_{k-1} + \nu_k \quad \quad \nu_k \sim \mathcal{N}(0,
      \operatorname{diag}(\sigma_{\nu,1}^2,\ldots, \sigma_{\nu,J}^2))
\end{equation}

Note that without the prior $\*z_k = \*z_{k-1} + \nu_k$, we
could estimate the $\*z_k$ independently across the windows as in
\cite{Miran2017}. However, we would like to have the stochastic continuity
constraint, as this provides denoising of the spectrogram - when estimating a
spectrogram in a specific window, the algorithm effectively pools together
estimates from neighboring windows~\cite{Kim2018}. We also add there are
different ways to encode stochastic continuity, such as through integrated
Wiener Process (IWP)~\cite{Song18}. This completes the setup of the generative
model.

\section{Inference}
Let $\theta = \{\sigma^2_{\nu,j}, \mu^c, \beta^c \}_{j=1,c=1}^{J,C}$. For
inference, we are interested in 1) parameter estimation of $\theta$ to obtain
$\widehat{\theta}$ and 2) computation of the posterior distribution $\Pp
(\*z_k\mid \data, \widehat{\theta})$. The complete-data likelihood can be
expressed as:
\begin{align}
   \log \mathcal{L}(\theta) &= \log \Pp(\data, \*z_{1:K} \mid \theta) \nonumber \\
   &= \log \Pp (\data \mid \*z_{1:K}, \theta) + \log \Pp(\*z_{1:K} \mid \theta),
\end{align}
which under our generative model is:
\begin{align}
      \sum_{k=1}^K & \sum_{c=1}^C \sum_{j=1}^J  n_{k,j}^c (\mu^c + \beta^c (\text{W}\*z_k)_j)
      - \log \big(1 + \exp( \mu^c + \beta^c (\text{W} \*z_k)_j) \big) \nonumber \\
   &-(K\sum_{j=1}^J\log(\pi \sigma^2_{\nu,j}) - \sum_{k=1}^K \sum_{j=1}^J \frac{\mid \mid z_{k,j}  -
      z_{k-1,j} \mid \mid^2}{\sigma_{\epsilon, j}^2}
\end{align}
Because we do not observe the $\*z_k$'s, however, we will look to
expectation maximization for a solution, which leads us to a state space model
construction.

However, the point-process likelihood (using either Poisson or binomial forms)
presents a challenge, since the Gaussian prior on $\*z_k$ is not a conjugate
prior to the likelihood. For this reason, we don't have access to the analytical
form for the posterior distribution. A few solutions exist. First, one could use
data-augmentation scheme via Polya-Gamma data augmentation~\cite{Polson13} and
obtain Monte Carlo (MC) samples from the exact posterior distribution. With MC
samples, one can approximate the required expectations in the E-step, leading to
Monte Carlo EM (MCEM)~\cite{Zhang18}.\\\\
The second option is to be make a Laplace approximation to the binomial
likelihood. The naive approach (Laplace approximation to the entire data) would
be computationally expensive, as this results in a covariance matrix of dimension
$KJ\times KJ$. Rather, we make the following observations: 1) if the
observations were instead complex Gaussian instead of Binomial, the problem
would be reduced to a frequency domain Kalman filter (FDKF) as in
\cite{Kim2018}, and 2) for a given window $k$, we can use a Laplace
approximation to the point process spectra as in \cite{Miran2017}. See Appendix
B2 for details.
\newline

\section{Algorithm}

At the $(s+1)^{\text{th}}$ iteration of the EM algorithm, we want to take the
expectation of the complete data log-likelihood $\log \mathcal{L}(\theta)$ with
respect to the posterior distribution $\Pp(\*z_{1:K}|\data,\theta^{(s)})$. We
thus have
\begin{align}
   \E [\log \mathcal{L}(\theta)\,|\,\data,\theta^{(s)}] &= \E [\log \Pp(\data, \*z_{1:K} \mid \theta)\,|\,\data,\theta^{(s)}] \nonumber \\
   &= \E [\log \Pp (\data \mid \*z_{1:K}, \theta) + \log \Pp(_{1:K} \mid \theta)\,|\,\data,\theta^{(s)} ], \nonumber
\end{align}
which under our model is
\begin{align}\label{eq:qfunc}
   \E [\log \mathcal{L}(\theta)|\data,\theta^{(s)}]=\E & \Bigg[ \sum_{k=1}^K \sum_{j=1}^J \sum_{c=1}^C n_{k,j}^c (\mu^c + \beta^c (\text{W}\*z_k)_j)
      - \log \big(1 + \exp( \mu^c + \beta^c (\text{W} \*z_k)_j ) \big) \nonumber \\
   & -(K\sum_{j=1}^J\log(\pi \sigma^2_{\nu,j}) - \sum_{k=1}^K \sum_{j=1}^J \frac{\mid \mid z_{k,j}  -
      z_{k-1,j}  \mid \mid^2}{\sigma_{\nu, j}^2} \,\Big|\,\data,\theta^{(s)}\Bigg].
\end{align}
The following quantities are needed for computing the expectation in
Eq.~(\ref{eq:qfunc})
\begin{equation}\label{eq:exps}
   \begin{split}
   \*z_{k \mid K}  &= \E [ \*z_k \mid
      \data, \theta^{(s)}] \\
   \Lambda_{k \mid K} &= \E [ || \*z_k ||^2
      \mid \data, \theta^{{(s)}}] \\
   \Lambda_{k, k-1 \mid K} &= \E [ \*z_k \*z^*_{k-1}
      \mid \data, \theta^{{(s)}}]
   \end{split}
\end{equation}
\subsection{Filter}
These quantities can be efficiently computed using a Kalman filtering and
smoothing algorithm. Let us focus primarily on the Kalman filtering, as the
Kalman smoother is straightforward once we compute the prediction/update
quantities in the filter step. First the one-step prediction for filtering is
simply given as
\begin{equation}
    \begin{split}
    \*z_{k \mid k-1} &= \*z_{k-1 \mid k-1}  \\
   \sigma^2_{k \mid k-1,j} &= \sigma^2_{k-1 \mid k-1, j} + \sigma^2_{\nu, j}. \\
   \end{split}
\end{equation}
The one-step update requires more attention, due to the fact that $\Pp
(\data_k|\*z_k)$ is not a Gaussian distribution. To this end, we perform
Gaussian approximation, i.e., Laplace approximation, to the posterior
distribution $\Pp (\*z_k \mid \data_{1:k},\theta)$, such that $\Pp (\*z_k \mid
\data_{1:k},\theta)\sim
\mathcal{N}(\*z_{k|k},\operatorname{diag}(\sigma_{k|k,1}^2,\ldots,\sigma_{k|k,J}^2))$~\cite{Smith03}.\\

Using Bayes's rule on $\Pp (\*z_k \mid \data_{1:k})$ (we drop $\theta$ for notational simplicity), we have
\begin{equation}\label{eq:kf_bayes}
    \begin{split}
   \Pp ( \*z_k \mid \data_{1:k}) & \propto\int\Pp(\data_k,\*z_k, \*z_{k-1}|\data_{1:k-1})\,d(\*z_{k-1})\\
   & =
      \int\Pp (\data_k \mid\*z_k)
      \Pp (\*z_k \mid\*z_{k-1})\Pp(\*z_{k-1}| \data_{1:k-1}) \,d(\*z_{k-1}) \\
  & =
      \Pp (\data_k \mid\*z_k) \underbrace{\int
      \Pp (\*z_k \mid\*z_{k-1})\overbrace{\Pp(\*z_{k-1}| \data_{1:k-1})}^{\data(\*z_{k-1|k-1},\Sigma_{k-1|k-1})} \,d(\*z_{k-1})}_{\Pp (\*z_k \mid \data_{1:k-1})=\data(\*z_{k|k-1},\Sigma_{k|k-1})} \\
   &= \Pp (\data_k \mid \*z_k) \Pp (\*z_k \mid \data_{1:k-1}).
      \end{split}
\end{equation}
Note that all conditional independence relations follow from our generative
model. With Eq.~(\ref{eq:kf_bayes}), we can proceed with the Laplace
approximation as follows
\begin{equation}
    \begin{split}
      \*z_{k|k}&=\argmax_{\*z_k}\log \Pp(\*z_k | \data_{1:k})\\
      &=\argmax_{\*z_k}\{\log\Pp (\data_k \mid \*z_k) +\log\Pp (\*z_k \mid \data_{1:k-1})\}\\
      &= \argmax_{\*z_k} \Big\{ \sum_{c=1}^C \sum_{j=1}^J n_{k,j}^c ( \mu^c + \beta^c (\W \*z_k)_j)
         - \log \big(1 + \exp ( \mu^c + \beta^c(\W \*z_k )_j ) \big)\\
      &\quad \quad \quad \quad \quad \quad - \sum_{j=1}^J \frac{|| z_{k,j} -
         z_{k|k-1,j}||^2} {\sigma^2_{\nu,j}}\Big\}.
    \end{split}
\end{equation}
To compute $\Sigma_{k|k}$, we compute Hessian of both sides. The full details are given in \ref{Newton}

% TODO Update for pp params
\subsection{Newton's Method for State Update}
\begin{algorithm}
\caption{Newton's Method for Window $k$ Gaussian Approximation}
\label{Newton}
\begin{algorithmic}[1]
\State $\z^{(0)} = \mathbf{0}; \quad i=0$
\State $\bar{n} = \sum_{c=1}^C n^c_{k,j}$
\While {$\text{crit} = 0$}
\State $i \leftarrow i + 1$
\State $\*x = \W \z^{(i-1)}$
\State $\lambda = \frac{1}{1 + e^{\*x}}$
\State $\vv = \Big(\frac{z_1^{(i-1)} - \*z_{k-1 \mid k-1}(\omega_1)}{\sigma^{2(s)}_{\nu,1}},
   \cdots, \frac{z_J^{(i-1)} - \*z_{k-1 \mid k-1}(\omega_J)}{\sigma^{2(s)}_{\nu,J}} \Big)^{\tr}$
\State $\g = C\W^{\tr}(\bar{\n} - \lambda) - \vv \quad$
\State $\V = \text{diag}\Big( \frac{1}{\sigma^{2(s)}_{\nu,1}}, \cdots,
   \frac{1}{\sigma^{2(s)}_{\nu,J}}\Big)$
\State $\G = \text{diag}\Big(\frac{e^{x_1}}{(1+e^{x_1})^2}, \cdots, \frac{e^{x_K}}{(1+e^{x_K})^2} \Big) $
\State $\Hh = -C \W^{\tr} \G \W - \V \quad$
\State $\z^{(i)} = \z^{(i-1)} - \Hh^{-1}\g$
\EndWhile
\State $\*z^{(s+1)}_{k \mid k} = \z^{(i)}$
\State $\*x = \W \z^{(1)}$
\State $\G = \text{diag}\Big(\frac{e^{x_1}}{(1+e^{x_1})^2}, \cdots, \frac{e^{x_K}}{(1+e^{x_K})^2} \Big) $
\State $\Hh = -C \W^{\tr} \G \W - \V \quad$
\State $\sigma^{2(s+1)}_{k \mid k, j} = - \frac{1}{\Hh_{(j,j)}}$
\end{algorithmic}
\end{algorithm}

\subsection{Smoother}
The smoothing algorithm is performed as in \cite{Kim2018}:
\begin{equation}
   \begin{split}
      G_{k,j} &= \sigma^2_{k \mid k, j} (\sigma^2_{k + 1\mid k, j})^{-1} \\
      z_{k \mid K,j} &= z_{k \mid k, j}
         + G_{k,j} ( z_{k+1 \mid K, j} - z_{k+1 \mid k,j} ) \\
      \sigma^2_{k \mid K,j} &= \sigma^2_{k\mid k,j} + G^2_{k,j}(\sigma^2_{k+1 \mid K}
         - \sigma^2_{k+1 \mid k, j})
   \end{split}
\end{equation}

% TODO update/understand sigmas (add summation over j) to Q func
% TODO check sigma MLE with new babadi paper
\subsection{M-Step}
In the M-Step, we maximize the expected log-likelihood of Eq.~(\ref{eq:qfunc}),
using the values computed in Eqs.~(\ref{eq:exps}). Omitting the initial state
and variance for now, for a given frequency $j$ we have:
% TODO - need to add initial state
\begin{equation}\label{eq:Q}
   \begin{split}
      Q(\theta \mid \theta^{(s)}) &= \E [\log \mathcal{L}_j(\theta)|\data,\theta^{(s)}] \\
      &= \sum_{k=1}^K \sum_{j=1}^J \sum_{c=1}^C n_{k,j}^c (\mu^c + \beta^c (\text{W}\*z_{k \mid K})_j) \\
      &- \sum_{k=1}^K \sum_{j=1}^J \sum_{c=1}^C \E \Big[ \log \big(1 + \exp( \mu^c + \beta^c (\text{W} \*z_k)_j ) \big) \Big ]\\
      &- \sum_{j=1}^J (K)\log(\sigma^2_{\nu,j}) - \frac{1}{\sigma^2_{\nu,j}}
      \sum_{k=1}^K \Big(\Lambda_{k \mid K,j} + -2\Lambda_{k,k-1 \mid K, j} + \Lambda_{k-1 \mid K, j}\Big) %\\
      % & + \text{const.}
   \end{split}
\end{equation}
% TODO write out expectations in (8) as is done in Abbas
% ok, approach here: let SSMT code/paper guide this
For the process variances we calculate a closed-form maximum likelihood estimate (MLE):
% TODO fix for initial state
\begin{equation}\label{eq:sigma_mle}
   \hat{\sigma}^2_{\nu,j} = \frac{1}{K} \sum_{k=1}^K \Big(\Lambda_{k \mid K,j} +
   -2\Lambda_{k,k-1 \mid K, j} + \Lambda_{k-1 \mid K, j}\Big) \\
\end{equation}
where the values in Eqs.~(\ref{eq:exps}) are computed as
\begin{equation}\label{exps_est}
   \begin{split}
      \Lambda_{k\mid K,j} &= z_{k \mid K,j}^2  + \sigma^2_{k \mid K,j} \\
      \Lambda_{k,k-1 \mid K, j} &= z_{k \mid K,j}  z^*_{k-1 \mid K,j}
         + G_{k-1,j} \sigma^2_{k\mid K,j}
   \end{split}
\end{equation}

The remaining expectation in Eq.~(\ref{eq:Q}) does not have a simple closed form
solution. In order to find estimates for $\mu^c$ and $\beta^c$, we proceed with
a Monte-Carlo EM procedure to approximate the expectation and find the MLE
numerically. We maximize the function $R$:
\begin{equation}
   \begin{split}
      R(\mu^1, \beta^1, \ldots, \mu^C, \beta^C) &= \sum_{k=1}^K \sum_{j=1}^J \sum_{c=1}^C n_{k,j}^c (\mu^c + \beta^c (\text{W}\*z_{k \mid K})_j) \\
      &- \sum_{k=1}^K \sum_{j=1}^J \sum_{c=1}^C \sum_{m=1}^M \log \big(1 + \exp( \mu^c + \beta^c (\text{W} \*z_k^{(m)})_j ) \big) \\
      \simeq \sum_{k=1}^K \sum_{j=1}^J \sum_{c=1}^C n_{k,j}^c &(\mu^c + \beta^c (\text{W}\*z_{k \mid K})_j)
         - \sum_{k=1}^K \sum_{j=1}^J \sum_{c=1}^C \E \Big[ \log \big(1 + \exp( \mu^c + \beta^c (\text{W} \*z_k)_j ) \big) \Big ]\\
   \end{split}
\end{equation}

\noindent where $\*z_1^{(m)}, \ldots, \*z_K^{(m)}$ are draws from the posterior
distribution over $\*z_1, \ldots, \*z_K$ computed during the E-Step. The gradients are computed using
\begin{equation}
   \begin{split}
      \frac{\partial R}{\partial \mu^c} &= \sum_{k=1}^K \sum_{j=1}^J n_{k,j}^c
         - \sum_{k=1}^K \sum_{j=1}^J \sum_{m=1}^M \frac{\exp( \mu^c + \beta^c (\text{W} \*z_k^{(m)})_j )}{1 + \exp( \mu^c + \beta^c (\text{W} \*z_k^{(m)})_j ) } \\
      \frac{\partial R}{\partial \beta^c} &= \sum_{k=1}^K \sum_{j=1}^J n_{k,j}^c(\text{W} \*z_{k \mid K})_j
         - \sum_{k=1}^K \sum_{j=1}^J \sum_{m=1}^M \frac{(\text{W} \*z_k^{(m)})_j \exp( \mu^c + \beta^c (\text{W} \*z_k)_j )}{1 + \exp( \mu^c + \beta^c (\text{W} \*z_k^{(m)})_j ) } \\
   \end{split}
\end{equation}

% TODO

% I am unsure about the form / derivation of Eqs.~(\ref{exps_est}), although it seems to be what is
% implemented in the code for \cite{Kim2018}. Also, note that in \cite{Kim2018},
% they use Re$(\Lambda_{k,k-1 \mid K, j})$. I have been unable to figure out why
% that is the case, or what ramifications that may have on our implementation.

\newpage
\bibliographystyle{unsrt}
\bibliography{specspike}

\end{document}


\input{../header.tex}
\title{\vspace{-2cm}INF3490/INF4490 Exercises - Particle Swarms \& Cartesian Genetic Programming}
\author{Eivind Samuelsen\input{../author_footnote.tex}}
\date{}

% Removing paragraph indents is sometimes useful:
\setlength\parindent{0pt}
% ==============================================================================

% ================================= DOCUMENT ===================================
\begin{document}
    \renewcommand\marginsymbol[1][0pt]{%
  \tabto*{0cm}\makebox[-1cm][c]{$\mathbb{P}$}\tabto*{\TabPrevPos}}

\maketitle
\input{../intro.tex}

\section{Particle Swarm Optimizations}
The particle swarm velocity update formula is
\begin{equation}
    v_i^{(t+1)} \leftarrow \alpha v_i ^{(t)} + U(0,\beta)(p_i-x_i^{(t)}) + U(0,\beta)(p_g - x_i^{(t)})
\end{equation}
If we replaced the random terms related to personal and global best with a term proportional to the local objective function gradient, like this:
\begin{equation}
    v_i^{(t+1)} \leftarrow \alpha v_i ^{(t)} + \gamma \nabla f(x_i^{(t)})
\end{equation}
How would the particles behave? How does this compare to gradient ascent?

\section{Cartesian Genetic Programming}
\begin{figure}[H]
\begin{center}
\includegraphics[width=0.6\textwidth]{cartesian.png}
\end{center}
\end{figure}
Using the setup above, construct circuits from the Cartesian genetic programming genotypes below:
\begin{itemize}
    \item 231 110 323 121 165 046 154 176 11 5 8 9
    \item 003 332 123 010 167 075 345 365 9 10 11 4
\end{itemize}

\input{../contact.tex}
\end{document}
% ==============================================================================


\chapter{ArrayMatrix}

\section{arraymatrix}\hypertarget{arraymatrix}
Class constructor.
\subsection{Syntax}
\begin{verbatim}
    M = arraymatrix
    M = arraymatrix(A)
    M = arraymatrix(A,m)
    M = arraymatrix(nx,ny,m)
\end{verbatim}

\subsection{Description}
\verb"M = arraymatrix" returns an empty structure with element
matrix dimension 2*2.\\
\verb"M = arraymatrix(A)" where A is a matrix of size nx*ny*m.
Returns a structure containing A with matrix dimension nx*ny and
length m.\\
\verb"M = arraymatrix(A,m)" where A is a matrix of size nx*ny and
m is a positive scalar. returns a structure with matrix A repeated
m times.\\
\verb"M = arraymatrix(nx,ny,m)" where nx, ny and m is positive
scalars. returns a structure containing zeros with matrix
dimension nx*ny and length m.
\subsection{Examples}
\begin{verbatim}
    >> A=[1,2;3,4];
    >> arraymatrix(A)
    arraymatrix-object
     dimension:  2 x 2
     elements:   1

    >> arraymatrix(A,3)
    arraymatrix-object
     dimension:  2 x 2
     elements:   3
\end{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{display}\hypertarget{display}
Display arraymatrix.
\subsection{Syntax}
\begin{verbatim}
    display(M)
\end{verbatim}
\subsection{Description}
Overloads \verb"display". Displays info about the arraymatrix when
semicolon isn't used for line termination.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{inv}\hypertarget{inv}
Inverse of each element in arraymatrix.
\subsection{Syntax}
\begin{verbatim}
    Y = inv(X)
\end{verbatim}
\subsection{Description}
\verb"Y = inv(X)" returns the inverse of the square arraymatrix
\verb"X". If dimension of \verb"X" is equal to two an explicit
algorithm is used, otherwise for-loops are used.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{mtimes}\hypertarget{mtimes}
Matrix multiply.
\subsection{Syntax}
\begin{verbatim}
    C = A*B
    C = A*b
    C = a*B
\end{verbatim}
\subsection{Description}
Overloads the multiplication operator \verb"*". This routine
handles multiplication with scalars, matrices and other
ArrayMatrix-objects.
\subsection{See Also}
\hyperlink{rdivide}{rdivide} \hyperlink{plus}{plus}
\hyperlink{minus}{minus} \hyperlink{uminus}{uminus}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{rdivide}\hypertarget{rdivide}
Right matrix divide
\subsection{Syntax}
\begin{verbatim}
    C = A/B
    C = A/b
    C = a/B
\end{verbatim}
\subsection{Description}
Overloads the division operator \verb"/". This routine handles
division with scalars, square matrices and other
ArrayMatrix-objects.
\subsection{See Also}
\hyperlink{mtimes}{mtimes} \hyperlink{plus}{plus}
\hyperlink{minus}{minus} \hyperlink{uminus}{uminus}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{plus}\hypertarget{plus}
Addition
\subsection{Syntax}
\begin{verbatim}
    C = A + B
    C = A + b
    C = a + B
\end{verbatim}
\subsection{Description}
Overloads the addition operator \verb"-". This routine handles
addition with scalars, matrices and other ArrayMatrix-objects.
\subsection{See Also}
\hyperlink{mtimes}{mtimes} \hyperlink{rdivide}{rdivide}
\hyperlink{minus}{minus} \hyperlink{uminus}{uminus}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{minus}\hypertarget{minus}
Subtraction.
\subsection{Syntax}
\begin{verbatim}
    C = A-B
    C = A-b
    C = a-B
\end{verbatim}
\subsection{Description}
Overloads the subtraction operator \verb"-". This routine handles
subtraction with scalars, matrices and other ArrayMatrix-objects.
\subsection{See Also}
\hyperlink{mtimes}{mtimes} \hyperlink{rdivide}{rdivide}
\hyperlink{plus}{plus} \hyperlink{uminus}{uminus}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{minus}\hypertarget{minus}
Unary minus.
\subsection{Syntax}
\begin{verbatim}
    Y = -Y
\end{verbatim}
\subsection{Description}
\subsection*{uminus}
Overloads the unary minus operator.
\subsection{See Also}
\hyperlink{mtimes}{mtimes} \hyperlink{rdivide}{rdivide}
\hyperlink{plus}{plus} \hyperlink{minus}{minus}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{transpose}\hypertarget{transpose}
Matrix transpose.
\subsection{Syntax}
\begin{verbatim}
    Y = X.'
\end{verbatim}
\subsection{Description}
Overloads the transpose operator \verb".*".
\subsection{See Also}
\hyperlink{ctranspose}{ctranspose}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{ctranspose}\hypertarget{ctranspose}
Hermitian matrix transpose.
\subsection{Syntax}
\begin{verbatim}
    Y = X'
\end{verbatim}
\subsection{Description}
Overloads the hermitian transpose operator \verb"*".
\subsection{See Also}
\hyperlink{transpose}{transpose}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{subsref}\hypertarget{subsref}
Subscripted reference.
\subsection{Syntax}
\begin{verbatim}
    Y = X(I)
    Y = X(nx,ny)
\end{verbatim}
\subsection{Description}
\verb"Y=X(I)" is an arraymatrix formed from the elements of
\verb"X" specified by the index vector \verb"I".\\
\verb"Y=X(nx,ny)" is a vector containing the \verb"(nx,ny)"
element of each matrix.
\subsection{Se Also}
%\hyperref{subsasgn}{subsasgn}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{subsasgn}\hypertarget{subsasgn}
Subscripted assigment.
\subsection{Syntax}
\begin{verbatim}
    X(I) = Y
    X(nx,ny) = Y
\end{verbatim}
\subsection{Description}
\verb"X(I)=Y" assigns the values of \verb"Y" to the positions of
\verb"X" specified by the index vector \verb"I".\\
\verb"X(nx,ny)=Y" assigns the values of vector \verb"Y" to the
\verb"(nx,ny)" element of each matrix.
\subsection{Se Also}
%\hyperref{subsref}{subsref}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{length}\hypertarget{length}
Length of object.
\subsection{Syntax}
\begin{verbatim}
    Y = length(X)
\end{verbatim}
\subsection{Description}
\verb"Y = length(X)" is the number of elements in X.
\subsection{See Also}
%\hyperref{size}{size}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{size}\hypertarget{size}
Size of object.
\subsection{Syntax}
\begin{verbatim}
    [NX,NY,M] = size(X)
    Y = size(X,dim)
\end{verbatim}
\subsection{Description}
\verb"[NX,NY,M] = size(X)" is the vector containing the size of
\verb"X".\\
\verb"Y = size(X,dim)" is the size of \verb"X" along the dimension
\verb"dim".
\subsection{See Also}
%\hyperref{length}{length}

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{get}\hypertarget{get}
Get object properties.
\subsection{Syntax}
\begin{verbatim}
    Y = get(X,property)
\end{verbatim}
\subsection{Description}
Provides access to the internals of the class.\\
\verb"Y = get(X,'nx')" is the number of rows in each matrix.\\
\verb"Y = get(X,'ny')" is the number of columns in each matrix.\\
\verb"Y = get(X,'m')" is the number of elements in the array.\\
\verb"Y = get(X,'mtrx')" is the three dimensional matrix holding
the data.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{conj}\hypertarget{conj}
Matrix conjugate.
\subsection{Syntax}
\begin{verbatim}
    Y = conj(X)
\end{verbatim}
\subsection{Description}
Calculates the conjugate of each matrix in the array.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{real}\hypertarget{real}
Real part.
\subsection{Syntax}
\begin{verbatim}
    Y = real(X)
\end{verbatim}
\subsection{Description}
Calculates the real part of each matrix in the array.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{imag}\hypertarget{imag}
Imaginary part.
\subsection{Syntax}
\begin{verbatim}
    Y = imag(X)
\end{verbatim}
\subsection{Description}
Calculates the imaginary part of each matrix in the array.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{angle}\hypertarget{angle}
Angle of complex number.
\subsection{Syntax}
\begin{verbatim}
    Y = angle(X)
\end{verbatim}
\subsection{Description}
Calculates the angle (in radians) of each matrix-entry in the
array.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{abs}\hypertarget{abs}
Absolute value.
\subsection{Syntax}
\begin{verbatim}
    Y = abs(X)
\end{verbatim}
\subsection{Description}
Calculates the absolute value of each matrix-entry in the array.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{det}\hypertarget{det}
Determinant.
\subsection{Syntax}
\begin{verbatim}
    Y = det(X)
\end{verbatim}
\subsection{Description}
Calculates the determinant of each matrix in the array.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{ident}\hypertarget{ident}
Determinant.
\subsection{Syntax}
\begin{verbatim}
    Y = ident(X)
\end{verbatim}
\subsection{Description}
\subsection*{ident}
Returns an appropriate identity matrix. Quite useless provided for
backwards compatibility.

%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{3mm} \hrule
\section{skip}\hypertarget{skip}
Drops rows and columns.
\subsection{Syntax}
\begin{verbatim}
    Y = skip(X,[row col])
\end{verbatim}
\subsection{Description}
\subsection*{ident}
Skips (or drops) the selected row and column in each matrix.
\verb"Y = skip(X,[row col])" drops the \verb"row"'th row and
\verb"col"'th column in each matrix.


\subsubsection{Formal encoding of recursive datatypes.}

We define the compilation scheme for recursive datatype bindings in
\cref{fig:compile-recursive-datatypes}, along with a number of auxiliary
functions. We will reuse some of the functions from
\cref{fig:compile-datatypes}, but many of them need variants for the
recursive case, which are denoted with a $\rec$ superscript.

\begin{figure}[!t]
  \centering
  \begin{displaymath}
  \begin{array}{l@{\ }l@{\ }l}
  \multicolumn{3}{l}{\textsf{Throughout this figure when $l$, $d$, or $c$ is an argument}}\\
  \multicolumn{3}{l}{l = \tlet\, \rec\, \seq{d} \tin\, t} \\
  \multicolumn{3}{l}{d = \datatype{X}{(\seq{Y :: K})}{x}{(\seq{c})}} \\
  \multicolumn{3}{l}{c = x(\seq{T})}\\
  \\
  \multicolumn{3}{l}{\textsc{Auxiliary functions}}\\
  \tagKind{l}
  &=& \seqKindArr{\dataKind{d}}{\Type}\\
  \dtTag{l}{d}{k}
  &=& \lambda (\seq{Y::K}) . \lambda (\seq{X :: \dataKind{d}}) . X_k\ \seq{Y}\\
  \dtInst{f}{l}{d}{k}
  &=& \lambda (\seq{Y::K}). f\ (\dtTag{l}{d}{k}\ \seq{Y})\\
  \dtFamily{l}
  &=& \lambda (r :: \seqKindArr{\dataKind{d}}{\Type})\ . \lambda (t :: \tagKind{l}) .
  \tlet\, \seq{X = \dtInst{\fixed{r}}{\fixed{l}}{d_j}{j}}^j\, \tin\, t\  \seq{\scottTy{d}}\\
  \dtInstFinal{l}{d}{k}
  &=& \lambda (\seq{Y::K}) . \ifix\ \dtFamily{l}\ (\dtTag{l}{d}{k}\ \seq{Y})\\
  \unveilRec{l}{t}
  &=& t\seq{\subst{X}{\dtInstFinal{\fixed{l}}{d_j}{j}}}^j \\
  \constrRec{l}{d}{c}{k}{m}
  &=&\Lambda (\seq{Y::K}) .
  \lambda (\seq{a : T}) .
  \wrap\ \dtFamily{l}\ (\dtTag{l}{d}{k}\ \seq{Y})\
  (\Lambda R .
  \lambda (\seq{b : \branchTy{c}{\fixed{R}}}) .
  ~b_m ~ \seq{a})\\
  \constrsRec{l}{d}{k} &=& \seq{\constrRec{\fixed{l}}{\fixed{d}}{c_j}{k}{j}}^j\\
  \matchRec{l}{d}{k}
  &=& \Lambda (\seq{Y::K}). \lambda (x : \dtInstFinal{l}{d}{k}\ \seq{Y}) . \unwrap x\\
  \\
  \multicolumn{3}{l}{\textsc{Compilation function}}\\
  \compiledatarec(l)
  &=&(\Lambda (\seq{\dataBind{d}}) . \lambda (\seq{\constrBinds{d}}) . \lambda (\seq{\matchBind{d}}) . t)\\
  &&\{ \seq{\dtInstFinal{l}{d_j}{j}}^j \} \\
  &&\seq{\constrsRec{\fixed{l}}{d_j}{j}}^j\\
  &&\seq{\matchRec{\fixed{l}}{d_j}{j}}^j
  \end{array}
  \end{displaymath}

  \captionof{figure}{Compilation of recursive datatype bindings}
  \label{fig:compile-recursive-datatypes}
\end{figure}

\noindent Let's go through the functions again, this time using $\Tree$ and
$\Forest$ as examples:
\begin{align*}
d_1 &\defeq \datatype{\Tree}{A}{\textsf{matchTree}}{(\Node (A, \Forest A))}\\
d_2 &\defeq \datatype{\Forest}{A}{\textsf{matchForest}}{(\NNil(), \CCons(\Tree A, \Forest A))}
\end{align*}
\begin{itemize}
  \item $\tagKind{l}$ defines the kind of the type-level tags for our
    datatype family, which is a Scott-encoded tuple of types.
    $$\tagKind{l} = (\Type \kindArrow \Type) \kindArrow (\Type \kindArrow \Type) \kindArrow \Type$$
  \item $\dtTag{l}{d}{k}$ defines the tag type for the datatype $d$ in the family.
    \begin{align*}
    \dtTag{l}{\Tree}{1} &= \lambda A . \lambda (v_1 :: \Type \kindArrow \Type) (v_2 :: \Type \kindArrow \Type) . v_1\ A\\
    \dtTag{l}{\Forest}{2} &= \lambda A . \lambda (v_1 :: \Type \kindArrow \Type) (v_2 :: \Type \kindArrow \Type) . v_2\ A
    \end{align*}
  \item $\dtInst{f}{l}{d}{k}$ instantiates the family type $f$ for the
    datatype $d$ in the family by applying it to the datatype tag.
    \begin{align*}
    \dtInst{f}{l}{\Tree}{1} &= \lambda A . f\ (\dtTag{l}{\Tree}{1}\ A)\\
    \dtInst{f}{l}{\Forest}{2} &= \lambda A . f\ (\dtTag{l}{\Forest}{2}\ A)
    \end{align*}
  \item $\dtFamily{l}$ defines the datatype family itself. This takes a
    recursive argument and a tag argument, and applies the tag to the
    Scott-encoded types of the datatype components, where the types themselves
    are instantiated using the recursive argument.
    \begin{align*}
    \dtFamily{l} =&\ \lambda r\ t . \tlet \\
        &\quad\Tree = \dtInst{r}{l}{\Tree}{1}\\
        &\quad\Forest = \dtInst{r}{l}{\Forest}{2}\\
      &\tin\, t\ \scottTy{d_1}\ \scottTy{d_2}\\
    \scottTy{d_1} =&\ \lambda A . \forall R . (A \rightarrow \Forest A \rightarrow R) \rightarrow R\\
    \scottTy{d_2} =&\ \lambda A . \forall R . R \rightarrow (\Tree A \rightarrow \Forest A \rightarrow R) \rightarrow R
    \end{align*}
  \item $\dtInstFinal{l}{d}{k}$ is the full recursive datatype family
    instantiated for the datatype $d$, much like $\dtInst{f}{l}{d}{k}$, but
    with the full datatype family.
    $$\dtInstFinal{l}{\Tree}{1} = \lambda A . \ifix\  (\dtFamily{l})\ (\dtTag{l}{\Tree}{1}\ A)$$
  \item $\unveilRec{l}{t}$ ``unveils'' the datatypes as before, but
    unveils all the datatypes and replaces them with the full recursive
    definition instead of just the Scott-encoded type.
  \item $\constrRec{l}{d}{c}{k}{m}$ defines the constructor $c$ of the
    datatype $d$ in the family. It is similar to before, but includes a use of $\wrap$.
    \begin{align*}
    \constrRec{l}{\Tree}{\Node}{1}{1} =&\ \Lambda A . \lambda (v_1 : A) (v_2 : \Forest A) .\\
                               &\wrap\  (\dtInstFinal{l}{\Tree}{1})\ A\\
                               &(\Lambda R . \lambda (b_1 : A \rightarrow \Forest A \rightarrow R) . b_1\ v_1\ v_2)\\
    \constrRec{l}{\Forest}{\NNil}{2}{1} =&\ \Lambda A . \\
                               &\wrap\  (\dtInstFinal{l}{\Forest}{2})\ A\\
                               &(\Lambda R . \lambda (b_1 : R) (b_2 : \Tree A \rightarrow \Forest A \rightarrow R) . b_1)\\
      \constrRec{l}{\Forest}{\CCons}{2}{2} =&\ \Lambda A . \lambda (v_1 : \Tree A) (v_2 : \Forest A) . \\
                               &\wrap\  (\dtInstFinal{l}{\Forest}{2})\ A\\
                               &(\Lambda R . \lambda (b_1 : R) (b_2 : \Tree A \rightarrow \Forest A \rightarrow R) . b_2\ v_1\ v_2)
    \end{align*}
  \item $\matchRec{l}{d}{k}$ defines the matcher of the datatype $d$ as
    before, but includes a use of $\unwrap$.
    \begin{align*}
    \matchRec{l}{\Tree}{1} &= \Lambda A . \lambda (v : \Tree A) . \unwrap\ v\\
    \matchRec{l}{\Forest}{2} &= \Lambda A . \lambda (v : \Forest A) . \unwrap\ v
    \end{align*}
\end{itemize}


\insertion{\thelibrary}{
\section{Overloading}
\label{overload.sec}
Two forms of overloading are available:
\begin{itemize}
\item Certain special constants are overloaded.  For example,
{\tt 0w5} may have type $\WORD$ or some other type, depending on
the surrounding program text;
\item Certain operators are overloaded. For example,
{\tt +} may have type $\INT\ast\INT\to\INT$ or
$\REAL\ast\REAL\to\REAL$, depending on
the surrounding program text;
\end{itemize}
Programmers cannot define their own overloaded constants or operators.

Although a formal treatment of overloading is outside the scope
of this document, we do give a complete list of the overloaded operators
and of types with overloaded special constants.
This list is consistent with the Basis Library\cite{mllib96}.

Every overloaded constant and value identifier has among its types a
{\em default type},
which is assigned to it,
when the surrounding text does not resolve the overloading.
For this purpose, the surrounding text is no larger than the smallest
enclosing structure-level declaration; an implementation may require
that a smaller context determines the type.

\subsection{Overloaded special constants}
Libraries may extend the set $\T_0$ of
Appendix~\ref{init-stat-bas-app} with additional type names. Thereafter, certain
subsets of $T_0$ have a special significance;
they are called {\sl overloading classes}
and they are:\medskip

\halign{\indent#\ \hfil&\ $#$\ &\ $#$\hfil\cr
\Int &\supseteq&\{\INT\}\cr
\Real &\supseteq&\{\REAL\}\cr
\Word &\supseteq&\{\WORD\}\cr
\String&\supseteq&\{\STRING\}\cr
\Char&\supseteq&\{\CHAR\}\cr
\WordInt&=&\Word\cup\INt\cr
\RealInt&=&\Real\cup\INt\cr
\Num&=&\Word\cup\Real\cup\INt\cr
\NumTxt&=&\Word\cup\Real\cup\INt\cup\String\cup\Char\cr}
\medskip

\noindent
Among these, the five first ($\Int$, $\Real$, $\Word$, $\String$ and $\Char$) are said to be
{\sl basic}; the remaining are said to be {\sl composite}.
The reason that the basic classes are specified using
$\supseteq$ rather than $=$ is that libraries may extend
each of the  basic overloading
classes with further type names.  Special constants are overloaded
within each of the basic overloading classes.  However, the basic
overloading classes must be arranged so that every special constant can be
assigned types from at most one of the basic overloading classes.  For
example, to \boxml{0w5} may be assigned type $\WORD$, or
some other member of $\Word$, depending on the surrounding text.  If
the surrounding text does not determine the type of the constant, a
default type is used. The default types for the five sets are $\INT$,
$\REAL$, $\WORD$, $\STRING$ and $\CHAR$ respectively.

       Once overloading resolution has determined the type of a special constant,
       it is a compile-time error if the constant does not make sense or does not
       denote a value within the machine representation chosen for the type.
       For example, an escape sequence of the form $\uconst$ in a string constant
       of 8-bit characters only makes sense if $xxxx$  denotes
       a number in the range $[0, 255]$.

\subsection{Overloaded value identifiers}
Overloaded identifiers all have identifier status $\isv$. An
overloaded identifier may be re-bound with any status ($\isv$, $\isc$
and $\ise$) but then it is not overloaded within the scope of
the binding.

\begin{figure}
\begin{center}
\vskip-12pt
\begin{tabular}{|rl|rl|}
\multicolumn{2}{c}{NONFIX}&     \multicolumn{2}{c}{INFIX}\\
\hline
$\var$     & $\mapsto\ \hbox{set of monotypes}$
                          & $\var$ & $\mapsto\ \hbox{set of monotypes}$\\
\hline
\boxml{abs} & $\mapsto \REALINT\to\REALINT$
                       & \multicolumn{2}{l|}{Precedence 7, left associative :} \\
\NEG    & $\mapsto \REALINT\to\REALINT $                      &
                            \boxml{div} & $\mapsto \WORDINT\ \ast\ \WORDINT
                                                                 \to\WORDINT$\\
 &
                                             &
                            \boxml{mod} & $\mapsto \WORDINT\ \ast\ \WORDINT
                                                                 \to\WORDINT$\\
  &                       &
                            \boxml{*} &$\mapsto \NUM\ \ast\ \NUM
                                                                 \to\NUM$\\
  &                       &
                            \boxml{/} &$\mapsto \RREAL\ \ast\ \RREAL
                                                                 \to\RREAL$\\
  & &
                            \multicolumn{2}{l|}{Precedence 6, left associative :} \\
  &                       &
                            \boxml{+} &$\mapsto \NUM\ \ast\ \NUM
                                                                 \to\NUM$\\
  &                       &
                            \boxml{-} &$\mapsto \NUM\ \ast\ \NUM\to\NUM$\\
  &
                          & \multicolumn{2}{l|}{Precedence 4, left associative :}\\
              &           &
                            \boxml{<} & $\mapsto\NUMTEXT *\NUMTEXT \to \NUMTEXT$\\
              &           &
                            \boxml{>} & $\mapsto\NUMTEXT *\NUMTEXT \to \NUMTEXT$\\
              &           &
                            \boxml{<=} & $\mapsto\NUMTEXT *\NUMTEXT \to \NUMTEXT$\\
              &           &
                            \boxml{>=} & $\mapsto\NUMTEXT *\NUMTEXT \to \NUMTEXT$\\
\hline
\end{tabular}
\end{center}
\vskip-15pt
\caption{Overloaded identifiers}
\label{overload.fig}
\end{figure}
}
The overloaded identifiers are given in Figure~\ref{overload.fig}.
For example, the entry
$$\boxml{abs}\mapsto\REALINT\to\REALINT$$
states that $\boxml{abs}$ may assume one of the types
$\{\t\to\t\,\mid\,\t\in\RealInt\}$.
In general, the same type name must be chosen
throughout the entire type of the overloaded operator;
thus $\boxml{abs}$ does not have type $\REAL\to\INT$.

The operator \boxml{/} is overloaded on all members of $\Real$,
with default type $\REAL\ast\REAL\to\REAL$.
The default type of any other identifier is that one of its types
which contains the type name {\tt int}.
For example, the program ~~\boxml{fun double(x) = x + x;}~~
declares a function of type $\INT\ \ast\ \INT\to\INT$, while
~~\boxml{fun double(x:real) = x + x;}~~ declares a function of type
$\REAL\ \ast\ \REAL\to\REAL$.

The dynamic semantics of the overloaded operators is defined in
\cite{mllib96}.


\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, graphs}
\usetikzlibrary{arrows,%
                petri,%
                topaths}%

\usepackage[latin1]{inputenc}

\title{Riddler Express \\ Precipitation Permutations}
\author{Samuel Eklund\\linkedin.com/in/sameklund\\ \\Travis Chambers\\linkedin.com/in/travisjchambers}
\date{2018-12-07}

\begin{document}
\maketitle

\section{Problem}
Louie walks to and from work every day. In his city, there is a 50 percent chance of rain each morning and an independent 40 percent chance each evening. His habit is to bring (and use) an umbrella if it's raining when he leaves the house or office, but to leave them all behind if not. Louie owns three umbrellas.

On Sunday night, two are with him at home and one is at his office. Assuming it never starts raining during his walk to his home or office, what is the probability that he makes it through the work week without getting wet?

\section{Description}
We start by describing the various states of the system. We  distinguish between Morning $(M)$ and Evening $(E)$. We also count how many umbrellas are at Home $(H_{n})$ and at the Office $(O_{n})$. Using this notation, we describe the starting state as $(M; H_{2}, O_{1})$. We also designate $W_{0}$ and $W_{1}$ as the absorbing states that represent Louie getting wet (as he is traveling with no umbrella while it is raining).

\newpage

\section{Solution}
We construct a graph of all states, along with their transition probabilities. Blue transitions represent rain and black transitions represent no rain. We also mark the absorbing states $(W_{0}, W_{1})$ in red.

\begin{center}
\begin{tikzpicture}[
state/.style={rectangle, draw, node distance = 2cm},
rain/.style={bend left, auto, ->, color=blue},
no_rain/.style={bend left, auto, ->}
]
% States
	\node[state] (w0) [color=red] {$W_{0}$};
	\node[state] (e1) [below left of=w0] {$E; H_{3}, O_{0}$};
	\node[state] (m1) [below left of=e1] {$M; H_{3}, O_{0}$};
	\node[state] (e2) [below left of=m1] {$E; H_{2}, O_{1}$};
	\node[initial, state] (m2) [below left of=e2] {$M; H_{2}, O_{1}$};
	\node[state] (e3) [below right of=m2] {$E; H_{1}, O_{2}$};
	\node[state] (m3) [below right of=e3] {$M; H_{1}, O_{2}$};
	\node[state] (e4) [below right of=m3] {$E; H_{0}, O_{3}$};
	\node[state] (m4) [below right of=e4] {$M; H_{0}, O_{3}$};
	\node[state] (w1) [below right of=m4, color=red] {$W_{1}$};

%Transitions
	\path[rain] (e1) edge node [draw=none] {0.4} (w0);
	\path[no_rain] (e1) edge node [draw=none] {0.6} (m1);
	\path[no_rain] (m1) edge node [draw=none] {0.5} (e1);
	\path[rain] (m1) edge node [draw=none] {0.5} (e2);
	\path[rain] (e2) edge node [draw=none] {0.4} (m1);
	\path[no_rain] (e2) edge node [draw=none] {0.6} (m2);
	\path[no_rain] (m2) edge node [draw=none] {0.5} (e2);
	\path[rain] (m2) edge node [draw=none] {0.5} (e3);
	\path[rain] (e3) edge node [draw=none] {0.4} (m2);
	\path[no_rain] (e3) edge node [draw=none] {0.6} (m3);
	\path[no_rain] (m3) edge node [draw=none] {0.5} (e3);
	\path[rain] (m3) edge node [draw=none] {0.5} (e4);
	\path[rain] (e4) edge node [draw=none] {0.4} (m3);
	\path[no_rain] (e4) edge node [draw=none] {0.6} (m4);
	\path[no_rain] (m4) edge node [draw=none] {0.5} (e4);
	\path[rain] (m4) edge node [draw=none] {0.5} (w1);
\end{tikzpicture}
\end{center}

\newpage
We can create a table of the probabilities of each state based on the previous state, as below. Day 0 represents the initial state, when the probability of moving to $M; H_{2}, O_{1}$ is 1. We omit unnecessary values to reduce calculation, as we only need the values for $W_{0}$ and $W_{1}$.

\begin{center}
{\footnotesize
\begin{tabular}{|c||c|c|c|c|c|c|c|c|c|c|}
\multicolumn{10}{c}{State} \\ \cline{2-11}
\multicolumn{1}{l|}{Day} & \rotatebox{90}{$W_{0}$} & \rotatebox{90}{$E; H_{3}, O_{0}$} & \rotatebox{90}{$M; H_{3}, O_{0}$} & \rotatebox{90}{$E; H_{2}, O_{1}$} & \rotatebox{90}{$M; H_{2}, O_{1}$} & \rotatebox{90}{$E; H_{1}, O_{2}$} & \rotatebox{90}{$M; H_{1}, O_{2}$} & \rotatebox{90}{$E; H_{0}, O_{3}$} & \rotatebox{90}{$M; H_{0}, O_{3}$} & \rotatebox{90}{$W_{1}$} \\ \hline
0.0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\
0.5 & 0 & 0 & 0 & 0.5 & 0 & 0.5 & 0 & 0 & 0 & 0 \\
1.0 & 0 & 0 & 0.2 & 0 & 0.5 & 0 & 0.3 & 0 & 0 & 0 \\
1.5 & 0 & 0.1 & 0 & 0.35 & 0 & 0.4 & 0 & 0.15 & 0 & 0 \\
2.0 & 0.04 & 0 & 0.2 & 0 & 0.37 & 0 & 0.3 & 0 & 0.09 & 0 \\
2.5 & 0.04 & 0.1 & 0 & 0.285 & 0 & 0.335 & 0 & 0.195 & 0 & 0.045 \\
3.0 & 0.08 & 0 & 0.174 & 0 & 0.305 & 0 & 0.279 & 0 & 0.117 & 0.045 \\
3.5 & 0.08 & 0.087 & 0 & 0.2395 &  &  & 0 & 0.198 & 0 & 0.1035 \\
4.0 & 0.1148 & 0 & 0.148 &  &  &  &  & 0 & 0.1188 & 0.1035 \\
4.5 & 0.1148 & 0.074 &  &  &  &  &  &  & 0 & 0.1629 \\
5.0 & 0.1444 &  &  &  & &  & &  & & 0.1629 \\ \hline
\end{tabular}
}
\end{center}

The probability that Louie does not get wet during the workweek is:
\begin{equation*}
Pr(\text{Dry}) = 1 - Pr(\text{Wet}) = 1 - (W_{0} + W_{1}) = 1 - (0.1444 + 0.1629) = 0.6927 = 69.27\%
\end{equation*}

\end{document}